Mercurial > vim
annotate src/fileio.c @ 6618:a871b5070d59 v7.4.635
updated for version 7.4.635
Problem: If no NL or CR is found in the first block of a file then the
'fileformat' may be set to "mac". (Issue 77)
Solution: Check if a CR was found. (eswald)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Tue, 17 Feb 2015 14:15:19 +0100 |
parents | a5ba0921efcb |
children | a532340f3940 |
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 #include "vim.h" | |
15 | |
2014 | 16 #if defined(__TANDEM) || defined(__MINT__) |
7 | 17 # include <limits.h> /* for SSIZE_MAX */ |
18 #endif | |
19 | |
20 #if defined(HAVE_UTIME) && defined(HAVE_UTIME_H) | |
21 # include <utime.h> /* for struct utimbuf */ | |
22 #endif | |
23 | |
24 #define BUFSIZE 8192 /* size of normal write buffer */ | |
25 #define SMBUFSIZE 256 /* size of emergency write buffer */ | |
26 | |
27 /* Is there any system that doesn't have access()? */ | |
574 | 28 #define USE_MCH_ACCESS |
7 | 29 |
1313 | 30 #if defined(sun) && defined(S_ISCHR) |
31 # define OPEN_CHR_FILES | |
32 static int is_dev_fd_file(char_u *fname); | |
33 #endif | |
7 | 34 #ifdef FEAT_MBYTE |
35 static char_u *next_fenc __ARGS((char_u **pp)); | |
36 # ifdef FEAT_EVAL | |
37 static char_u *readfile_charconvert __ARGS((char_u *fname, char_u *fenc, int *fdp)); | |
38 # endif | |
39 #endif | |
40 #ifdef FEAT_VIMINFO | |
41 static void check_marks_read __ARGS((void)); | |
42 #endif | |
43 #ifdef FEAT_CRYPT | |
2267 | 44 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 | 45 #endif |
46 #ifdef UNIX | |
47 static void set_file_time __ARGS((char_u *fname, time_t atime, time_t mtime)); | |
48 #endif | |
633 | 49 static int set_rw_fname __ARGS((char_u *fname, char_u *sfname)); |
7 | 50 static int msg_add_fileformat __ARGS((int eol_type)); |
51 static void msg_add_eol __ARGS((void)); | |
52 static int check_mtime __ARGS((buf_T *buf, struct stat *s)); | |
53 static int time_differs __ARGS((long t1, long t2)); | |
54 #ifdef FEAT_AUTOCMD | |
661 | 55 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 | 56 static int au_find_group __ARGS((char_u *name)); |
57 | |
58 # define AUGROUP_DEFAULT -1 /* default autocmd group */ | |
1834 | 59 # define AUGROUP_ERROR -2 /* erroneous autocmd group */ |
676 | 60 # define AUGROUP_ALL -3 /* all autocmd groups */ |
7 | 61 #endif |
62 | |
63 #if defined(FEAT_CRYPT) || defined(FEAT_MBYTE) | |
64 # define HAS_BW_FLAGS | |
65 # define FIO_LATIN1 0x01 /* convert Latin1 */ | |
66 # define FIO_UTF8 0x02 /* convert UTF-8 */ | |
67 # define FIO_UCS2 0x04 /* convert UCS-2 */ | |
68 # define FIO_UCS4 0x08 /* convert UCS-4 */ | |
69 # define FIO_UTF16 0x10 /* convert UTF-16 */ | |
70 # ifdef WIN3264 | |
71 # define FIO_CODEPAGE 0x20 /* convert MS-Windows codepage */ | |
72 # define FIO_PUT_CP(x) (((x) & 0xffff) << 16) /* put codepage in top word */ | |
73 # define FIO_GET_CP(x) (((x)>>16) & 0xffff) /* get codepage from top word */ | |
74 # endif | |
75 # ifdef MACOS_X | |
76 # define FIO_MACROMAN 0x20 /* convert MacRoman */ | |
77 # endif | |
78 # define FIO_ENDIAN_L 0x80 /* little endian */ | |
79 # define FIO_ENCRYPTED 0x1000 /* encrypt written bytes */ | |
80 # define FIO_NOCONVERT 0x2000 /* skip encoding conversion */ | |
81 # define FIO_UCSBOM 0x4000 /* check for BOM at start of file */ | |
82 # define FIO_ALL -1 /* allow all formats */ | |
83 #endif | |
84 | |
85 /* When converting, a read() or write() may leave some bytes to be converted | |
86 * for the next call. The value is guessed... */ | |
87 #define CONV_RESTLEN 30 | |
88 | |
89 /* We have to guess how much a sequence of bytes may expand when converting | |
90 * with iconv() to be able to allocate a buffer. */ | |
91 #define ICONV_MULT 8 | |
92 | |
93 /* | |
94 * Structure to pass arguments from buf_write() to buf_write_bytes(). | |
95 */ | |
96 struct bw_info | |
97 { | |
98 int bw_fd; /* file descriptor */ | |
99 char_u *bw_buf; /* buffer with data to be written */ | |
1411 | 100 int bw_len; /* length of data */ |
7 | 101 #ifdef HAS_BW_FLAGS |
102 int bw_flags; /* FIO_ flags */ | |
103 #endif | |
6122 | 104 #ifdef FEAT_CRYPT |
105 buf_T *bw_buffer; /* buffer being written */ | |
106 #endif | |
7 | 107 #ifdef FEAT_MBYTE |
108 char_u bw_rest[CONV_RESTLEN]; /* not converted bytes */ | |
109 int bw_restlen; /* nr of bytes in bw_rest[] */ | |
110 int bw_first; /* first write call */ | |
111 char_u *bw_conv_buf; /* buffer for writing converted chars */ | |
112 int bw_conv_buflen; /* size of bw_conv_buf */ | |
113 int bw_conv_error; /* set for conversion error */ | |
1947 | 114 linenr_T bw_conv_error_lnum; /* first line with error or zero */ |
115 linenr_T bw_start_lnum; /* line number at start of buffer */ | |
7 | 116 # ifdef USE_ICONV |
117 iconv_t bw_iconv_fd; /* descriptor for iconv() or -1 */ | |
118 # endif | |
119 #endif | |
120 }; | |
121 | |
122 static int buf_write_bytes __ARGS((struct bw_info *ip)); | |
123 | |
124 #ifdef FEAT_MBYTE | |
595 | 125 static linenr_T readfile_linenr __ARGS((linenr_T linecnt, char_u *p, char_u *endp)); |
7 | 126 static int ucs2bytes __ARGS((unsigned c, char_u **pp, int flags)); |
1948 | 127 static int need_conversion __ARGS((char_u *fenc)); |
7 | 128 static int get_fio_flags __ARGS((char_u *ptr)); |
129 static char_u *check_for_bom __ARGS((char_u *p, long size, int *lenp, int flags)); | |
130 static int make_bom __ARGS((char_u *buf, char_u *name)); | |
131 # ifdef WIN3264 | |
132 static int get_win_fio_flags __ARGS((char_u *ptr)); | |
133 # endif | |
134 # ifdef MACOS_X | |
135 static int get_mac_fio_flags __ARGS((char_u *ptr)); | |
136 # endif | |
137 #endif | |
138 static int move_lines __ARGS((buf_T *frombuf, buf_T *tobuf)); | |
2006 | 139 #ifdef TEMPDIRNAMES |
1997 | 140 static void vim_settempdir __ARGS((char_u *tempdir)); |
2006 | 141 #endif |
1834 | 142 #ifdef FEAT_AUTOCMD |
143 static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name"); | |
144 #endif | |
595 | 145 |
7 | 146 void |
147 filemess(buf, name, s, attr) | |
148 buf_T *buf; | |
149 char_u *name; | |
150 char_u *s; | |
151 int attr; | |
152 { | |
153 int msg_scroll_save; | |
154 | |
155 if (msg_silent != 0) | |
156 return; | |
157 msg_add_fname(buf, name); /* put file name in IObuff with quotes */ | |
158 /* If it's extremely long, truncate it. */ | |
159 if (STRLEN(IObuff) > IOSIZE - 80) | |
160 IObuff[IOSIZE - 80] = NUL; | |
161 STRCAT(IObuff, s); | |
162 /* | |
163 * For the first message may have to start a new line. | |
164 * For further ones overwrite the previous one, reset msg_scroll before | |
165 * calling filemess(). | |
166 */ | |
167 msg_scroll_save = msg_scroll; | |
168 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) | |
169 msg_scroll = FALSE; | |
170 if (!msg_scroll) /* wait a bit when overwriting an error msg */ | |
171 check_for_delay(FALSE); | |
172 msg_start(); | |
173 msg_scroll = msg_scroll_save; | |
174 msg_scrolled_ign = TRUE; | |
175 /* may truncate the message to avoid a hit-return prompt */ | |
176 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr); | |
177 msg_clr_eos(); | |
178 out_flush(); | |
179 msg_scrolled_ign = FALSE; | |
180 } | |
181 | |
182 /* | |
183 * Read lines from file "fname" into the buffer after line "from". | |
184 * | |
185 * 1. We allocate blocks with lalloc, as big as possible. | |
186 * 2. Each block is filled with characters from the file with a single read(). | |
187 * 3. The lines are inserted in the buffer with ml_append(). | |
188 * | |
189 * (caller must check that fname != NULL, unless READ_STDIN is used) | |
190 * | |
191 * "lines_to_skip" is the number of lines that must be skipped | |
192 * "lines_to_read" is the number of lines that are appended | |
193 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM. | |
194 * | |
195 * flags: | |
196 * READ_NEW starting to edit a new buffer | |
197 * READ_FILTER reading filter output | |
198 * READ_STDIN read from stdin instead of a file | |
199 * READ_BUFFER read from curbuf instead of a file (converting after reading | |
200 * stdin) | |
201 * 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
|
202 * READ_KEEP_UNDO don't clear undo info or read it from a file |
7 | 203 * |
204 * return FAIL for failure, OK otherwise | |
205 */ | |
206 int | |
207 readfile(fname, sfname, from, lines_to_skip, lines_to_read, eap, flags) | |
208 char_u *fname; | |
209 char_u *sfname; | |
210 linenr_T from; | |
211 linenr_T lines_to_skip; | |
212 linenr_T lines_to_read; | |
213 exarg_T *eap; /* can be NULL! */ | |
214 int flags; | |
215 { | |
216 int fd = 0; | |
217 int newfile = (flags & READ_NEW); | |
218 int check_readonly; | |
219 int filtering = (flags & READ_FILTER); | |
220 int read_stdin = (flags & READ_STDIN); | |
221 int read_buffer = (flags & READ_BUFFER); | |
1486 | 222 int set_options = newfile || read_buffer |
223 || (eap != NULL && eap->read_edit); | |
7 | 224 linenr_T read_buf_lnum = 1; /* next line to read from curbuf */ |
225 colnr_T read_buf_col = 0; /* next char to read from this line */ | |
226 char_u c; | |
227 linenr_T lnum = from; | |
228 char_u *ptr = NULL; /* pointer into read buffer */ | |
229 char_u *buffer = NULL; /* read buffer */ | |
230 char_u *new_buffer = NULL; /* init to shut up gcc */ | |
231 char_u *line_start = NULL; /* init to shut up gcc */ | |
232 int wasempty; /* buffer was empty before reading */ | |
233 colnr_T len; | |
234 long size = 0; | |
235 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
|
236 off_t filesize = 0; |
7 | 237 int skip_read = FALSE; |
238 #ifdef FEAT_CRYPT | |
239 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
|
240 int did_ask_for_key = FALSE; |
7 | 241 #endif |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
242 #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
|
243 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
|
244 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
|
245 #endif |
7 | 246 int split = 0; /* number of split lines */ |
247 #define UNKNOWN 0x0fffffff /* file size is unknown */ | |
248 linenr_T linecnt; | |
249 int error = FALSE; /* errors encountered */ | |
250 int ff_error = EOL_UNKNOWN; /* file format with errors */ | |
251 long linerest = 0; /* remaining chars in line */ | |
252 #ifdef UNIX | |
253 int perm = 0; | |
254 int swap_mode = -1; /* protection bits for swap file */ | |
255 #else | |
256 int perm; | |
257 #endif | |
258 int fileformat = 0; /* end-of-line format */ | |
259 int keep_fileformat = FALSE; | |
260 struct stat st; | |
261 int file_readonly; | |
262 linenr_T skip_count = 0; | |
263 linenr_T read_count = 0; | |
264 int msg_save = msg_scroll; | |
265 linenr_T read_no_eol_lnum = 0; /* non-zero lnum when last line of | |
266 * last read was missing the eol */ | |
267 int try_mac = (vim_strchr(p_ffs, 'm') != NULL); | |
268 int try_dos = (vim_strchr(p_ffs, 'd') != NULL); | |
269 int try_unix = (vim_strchr(p_ffs, 'x') != NULL); | |
270 int file_rewind = FALSE; | |
271 #ifdef FEAT_MBYTE | |
272 int can_retry; | |
595 | 273 linenr_T conv_error = 0; /* line nr with conversion error */ |
274 linenr_T illegal_byte = 0; /* line nr with illegal byte */ | |
7 | 275 int keep_dest_enc = FALSE; /* don't retry when char doesn't fit |
276 in destination encoding */ | |
595 | 277 int bad_char_behavior = BAD_REPLACE; |
278 /* BAD_KEEP, BAD_DROP or character to | |
279 * replace with */ | |
7 | 280 char_u *tmpname = NULL; /* name of 'charconvert' output file */ |
281 int fio_flags = 0; | |
282 char_u *fenc; /* fileencoding to use */ | |
283 int fenc_alloced; /* fenc_next is in allocated memory */ | |
284 char_u *fenc_next = NULL; /* next item in 'fencs' or NULL */ | |
285 int advance_fenc = FALSE; | |
286 long real_size = 0; | |
287 # ifdef USE_ICONV | |
288 iconv_t iconv_fd = (iconv_t)-1; /* descriptor for iconv() or -1 */ | |
289 # ifdef FEAT_EVAL | |
290 int did_iconv = FALSE; /* TRUE when iconv() failed and trying | |
291 'charconvert' next */ | |
292 # endif | |
293 # endif | |
294 int converted = FALSE; /* TRUE if conversion done */ | |
295 int notconverted = FALSE; /* TRUE if conversion wanted but it | |
296 wasn't possible */ | |
297 char_u conv_rest[CONV_RESTLEN]; | |
298 int conv_restlen = 0; /* nr of bytes in conv_rest[] */ | |
299 #endif | |
1834 | 300 #ifdef FEAT_AUTOCMD |
2563
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
301 buf_T *old_curbuf; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
302 char_u *old_b_ffname; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
303 char_u *old_b_fname; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
304 int using_b_ffname; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
305 int using_b_fname; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
306 #endif |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
307 |
2707 | 308 curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */ |
7 | 309 |
310 /* | |
311 * If there is no file name yet, use the one for the read file. | |
312 * BF_NOTEDITED is set to reflect this. | |
313 * Don't do this for a read from a filter. | |
314 * Only do this when 'cpoptions' contains the 'f' flag. | |
315 */ | |
316 if (curbuf->b_ffname == NULL | |
317 && !filtering | |
318 && fname != NULL | |
319 && vim_strchr(p_cpo, CPO_FNAMER) != NULL | |
320 && !(flags & READ_DUMMY)) | |
321 { | |
633 | 322 if (set_rw_fname(fname, sfname) == FAIL) |
323 return FAIL; | |
7 | 324 } |
325 | |
2563
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
326 #ifdef FEAT_AUTOCMD |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
327 /* Remember the initial values of curbuf, curbuf->b_ffname and |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
328 * curbuf->b_fname to detect whether they are altered as a result of |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
329 * executing nasty autocommands. Also check if "fname" and "sfname" |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
330 * point to one of these values. */ |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
331 old_curbuf = curbuf; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
332 old_b_ffname = curbuf->b_ffname; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
333 old_b_fname = curbuf->b_fname; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
334 using_b_ffname = (fname == curbuf->b_ffname) |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
335 || (sfname == curbuf->b_ffname); |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
336 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname); |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
337 #endif |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
338 |
167 | 339 /* After reading a file the cursor line changes but we don't want to |
340 * display the line. */ | |
341 ex_no_reprint = TRUE; | |
342 | |
968 | 343 /* don't display the file info for another buffer now */ |
344 need_fileinfo = FALSE; | |
345 | |
7 | 346 /* |
347 * For Unix: Use the short file name whenever possible. | |
348 * Avoids problems with networks and when directory names are changed. | |
349 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to | |
350 * another directory, which we don't detect. | |
351 */ | |
352 if (sfname == NULL) | |
353 sfname = fname; | |
354 #if defined(UNIX) || defined(__EMX__) | |
355 fname = sfname; | |
356 #endif | |
357 | |
358 #ifdef FEAT_AUTOCMD | |
359 /* | |
360 * The BufReadCmd and FileReadCmd events intercept the reading process by | |
361 * executing the associated commands instead. | |
362 */ | |
363 if (!filtering && !read_stdin && !read_buffer) | |
364 { | |
365 pos_T pos; | |
366 | |
367 pos = curbuf->b_op_start; | |
368 | |
369 /* Set '[ mark to the line above where the lines go (line 1 if zero). */ | |
370 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); | |
371 curbuf->b_op_start.col = 0; | |
372 | |
373 if (newfile) | |
374 { | |
375 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname, | |
376 FALSE, curbuf, eap)) | |
377 #ifdef FEAT_EVAL | |
378 return aborting() ? FAIL : OK; | |
379 #else | |
380 return OK; | |
381 #endif | |
382 } | |
383 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname, | |
384 FALSE, NULL, eap)) | |
385 #ifdef FEAT_EVAL | |
386 return aborting() ? FAIL : OK; | |
387 #else | |
388 return OK; | |
389 #endif | |
390 | |
391 curbuf->b_op_start = pos; | |
392 } | |
393 #endif | |
394 | |
395 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0) | |
396 msg_scroll = FALSE; /* overwrite previous file message */ | |
397 else | |
398 msg_scroll = TRUE; /* don't overwrite previous file message */ | |
399 | |
400 /* | |
401 * If the name ends in a path separator, we can't open it. Check here, | |
402 * because reading the file may actually work, but then creating the swap | |
403 * file may destroy it! Reported on MS-DOS and Win 95. | |
404 * If the name is too long we might crash further on, quit here. | |
405 */ | |
23 | 406 if (fname != NULL && *fname != NUL) |
407 { | |
39 | 408 p = fname + STRLEN(fname); |
409 if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL) | |
23 | 410 { |
411 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0); | |
412 msg_end(); | |
413 msg_scroll = msg_save; | |
414 return FAIL; | |
415 } | |
7 | 416 } |
417 | |
5322 | 418 if (!read_stdin && !read_buffer) |
419 { | |
7 | 420 #ifdef UNIX |
5322 | 421 /* |
422 * On Unix it is possible to read a directory, so we have to | |
423 * check for it before the mch_open(). | |
424 */ | |
7 | 425 perm = mch_getperm(fname); |
426 if (perm >= 0 && !S_ISREG(perm) /* not a regular file ... */ | |
427 # ifdef S_ISFIFO | |
428 && !S_ISFIFO(perm) /* ... or fifo */ | |
429 # endif | |
430 # ifdef S_ISSOCK | |
431 && !S_ISSOCK(perm) /* ... or socket */ | |
432 # endif | |
1313 | 433 # ifdef OPEN_CHR_FILES |
434 && !(S_ISCHR(perm) && is_dev_fd_file(fname)) | |
435 /* ... or a character special file named /dev/fd/<n> */ | |
436 # endif | |
7 | 437 ) |
438 { | |
439 if (S_ISDIR(perm)) | |
440 filemess(curbuf, fname, (char_u *)_("is a directory"), 0); | |
441 else | |
442 filemess(curbuf, fname, (char_u *)_("is not a file"), 0); | |
443 msg_end(); | |
444 msg_scroll = msg_save; | |
445 return FAIL; | |
446 } | |
5322 | 447 #endif |
448 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
1006 | 449 /* |
450 * MS-Windows allows opening a device, but we will probably get stuck | |
451 * trying to read it. | |
452 */ | |
453 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE) | |
454 { | |
1303 | 455 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0); |
1006 | 456 msg_end(); |
457 msg_scroll = msg_save; | |
458 return FAIL; | |
459 } | |
5322 | 460 #endif |
461 } | |
1004 | 462 |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
463 /* Set default or forced 'fileformat' and 'binary'. */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
464 set_file_options(set_options, eap); |
7 | 465 |
466 /* | |
467 * When opening a new file we take the readonly flag from the file. | |
468 * Default is r/w, can be set to r/o below. | |
469 * Don't reset it when in readonly mode | |
470 * Only set/reset b_p_ro when BF_CHECK_RO is set. | |
471 */ | |
472 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO)); | |
164 | 473 if (check_readonly && !readonlymode) |
7 | 474 curbuf->b_p_ro = FALSE; |
475 | |
476 if (newfile && !read_stdin && !read_buffer) | |
477 { | |
2823 | 478 /* Remember time of file. */ |
7 | 479 if (mch_stat((char *)fname, &st) >= 0) |
480 { | |
481 buf_store_time(curbuf, &st, fname); | |
482 curbuf->b_mtime_read = curbuf->b_mtime; | |
483 #ifdef UNIX | |
484 /* | |
485 * Use the protection bits of the original file for the swap file. | |
486 * This makes it possible for others to read the name of the | |
487 * edited file from the swapfile, but only if they can read the | |
488 * edited file. | |
489 * Remove the "write" and "execute" bits for group and others | |
490 * (they must not write the swapfile). | |
491 * Add the "read" and "write" bits for the user, otherwise we may | |
492 * not be able to write to the file ourselves. | |
493 * Setting the bits is done below, after creating the swap file. | |
494 */ | |
495 swap_mode = (st.st_mode & 0644) | 0600; | |
496 #endif | |
497 #ifdef FEAT_CW_EDITOR | |
498 /* Get the FSSpec on MacOS | |
499 * TODO: Update it properly when the buffer name changes | |
500 */ | |
501 (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec); | |
502 #endif | |
503 #ifdef VMS | |
504 curbuf->b_fab_rfm = st.st_fab_rfm; | |
22 | 505 curbuf->b_fab_rat = st.st_fab_rat; |
506 curbuf->b_fab_mrs = st.st_fab_mrs; | |
7 | 507 #endif |
508 } | |
509 else | |
510 { | |
511 curbuf->b_mtime = 0; | |
512 curbuf->b_mtime_read = 0; | |
513 curbuf->b_orig_size = 0; | |
514 curbuf->b_orig_mode = 0; | |
515 } | |
516 | |
517 /* Reset the "new file" flag. It will be set again below when the | |
518 * file doesn't exist. */ | |
519 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W); | |
520 } | |
521 | |
522 /* | |
523 * for UNIX: check readonly with perm and mch_access() | |
524 * for MSDOS and Amiga: check readonly by trying to open the file for writing | |
525 */ | |
526 file_readonly = FALSE; | |
527 if (read_stdin) | |
528 { | |
529 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
530 /* Force binary I/O on stdin to avoid CR-LF -> LF conversion. */ | |
531 setmode(0, O_BINARY); | |
532 #endif | |
533 } | |
534 else if (!read_buffer) | |
535 { | |
536 #ifdef USE_MCH_ACCESS | |
537 if ( | |
538 # ifdef UNIX | |
539 !(perm & 0222) || | |
540 # endif | |
541 mch_access((char *)fname, W_OK)) | |
542 file_readonly = TRUE; | |
543 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
544 #else | |
545 if (!newfile | |
546 || readonlymode | |
547 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0) | |
548 { | |
549 file_readonly = TRUE; | |
550 /* try to open ro */ | |
551 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
552 } | |
553 #endif | |
554 } | |
555 | |
556 if (fd < 0) /* cannot open at all */ | |
557 { | |
558 #ifndef UNIX | |
559 int isdir_f; | |
560 #endif | |
561 msg_scroll = msg_save; | |
562 #ifndef UNIX | |
563 /* | |
564 * On MSDOS and Amiga we can't open a directory, check here. | |
565 */ | |
566 isdir_f = (mch_isdir(fname)); | |
567 perm = mch_getperm(fname); /* check if the file exists */ | |
568 if (isdir_f) | |
569 { | |
570 filemess(curbuf, sfname, (char_u *)_("is a directory"), 0); | |
571 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
572 } | |
573 else | |
574 #endif | |
575 if (newfile) | |
576 { | |
2147
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
577 if (perm < 0 |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
578 #ifdef ENOENT |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
579 && errno == ENOENT |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
580 #endif |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
581 ) |
7 | 582 { |
583 /* | |
584 * Set the 'new-file' flag, so that when the file has | |
585 * been created by someone else, a ":w" will complain. | |
586 */ | |
587 curbuf->b_flags |= BF_NEW; | |
588 | |
589 /* Create a swap file now, so that other Vims are warned | |
590 * that we are editing this file. Don't do this for a | |
591 * "nofile" or "nowrite" buffer type. */ | |
592 #ifdef FEAT_QUICKFIX | |
593 if (!bt_dontwrite(curbuf)) | |
594 #endif | |
1834 | 595 { |
7 | 596 check_need_swap(newfile); |
1834 | 597 #ifdef FEAT_AUTOCMD |
598 /* SwapExists autocommand may mess things up */ | |
599 if (curbuf != old_curbuf | |
600 || (using_b_ffname | |
601 && (old_b_ffname != curbuf->b_ffname)) | |
602 || (using_b_fname | |
603 && (old_b_fname != curbuf->b_fname))) | |
604 { | |
605 EMSG(_(e_auchangedbuf)); | |
606 return FAIL; | |
607 } | |
608 #endif | |
609 } | |
592 | 610 if (dir_of_file_exists(fname)) |
611 filemess(curbuf, sfname, (char_u *)_("[New File]"), 0); | |
612 else | |
613 filemess(curbuf, sfname, | |
614 (char_u *)_("[New DIRECTORY]"), 0); | |
7 | 615 #ifdef FEAT_VIMINFO |
616 /* Even though this is a new file, it might have been | |
617 * edited before and deleted. Get the old marks. */ | |
618 check_marks_read(); | |
619 #endif | |
620 #ifdef FEAT_MBYTE | |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
621 /* Set forced 'fileencoding'. */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
622 if (eap != NULL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
623 set_forced_fenc(eap); |
7 | 624 #endif |
625 #ifdef FEAT_AUTOCMD | |
626 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname, | |
627 FALSE, curbuf, eap); | |
628 #endif | |
629 /* remember the current fileformat */ | |
630 save_file_ff(curbuf); | |
631 | |
632 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
633 if (aborting()) /* autocmds may abort script processing */ | |
634 return FAIL; | |
635 #endif | |
636 return OK; /* a new file is not an error */ | |
637 } | |
638 else | |
639 { | |
550 | 640 filemess(curbuf, sfname, (char_u *)( |
641 # ifdef EFBIG | |
642 (errno == EFBIG) ? _("[File too big]") : | |
643 # endif | |
2147
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
644 # ifdef EOVERFLOW |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
645 (errno == EOVERFLOW) ? _("[File too big]") : |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
646 # endif |
550 | 647 _("[Permission Denied]")), 0); |
7 | 648 curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
649 } | |
650 } | |
651 | |
652 return FAIL; | |
653 } | |
654 | |
655 /* | |
656 * Only set the 'ro' flag for readonly files the first time they are | |
657 * loaded. Help files always get readonly mode | |
658 */ | |
659 if ((check_readonly && file_readonly) || curbuf->b_help) | |
660 curbuf->b_p_ro = TRUE; | |
661 | |
819 | 662 if (set_options) |
7 | 663 { |
1486 | 664 /* Don't change 'eol' if reading from buffer as it will already be |
665 * correctly set when reading stdin. */ | |
666 if (!read_buffer) | |
667 { | |
668 curbuf->b_p_eol = TRUE; | |
669 curbuf->b_start_eol = TRUE; | |
670 } | |
7 | 671 #ifdef FEAT_MBYTE |
672 curbuf->b_p_bomb = FALSE; | |
1352 | 673 curbuf->b_start_bomb = FALSE; |
7 | 674 #endif |
675 } | |
676 | |
677 /* Create a swap file now, so that other Vims are warned that we are | |
678 * editing this file. | |
679 * Don't do this for a "nofile" or "nowrite" buffer type. */ | |
680 #ifdef FEAT_QUICKFIX | |
681 if (!bt_dontwrite(curbuf)) | |
682 #endif | |
683 { | |
684 check_need_swap(newfile); | |
1834 | 685 #ifdef FEAT_AUTOCMD |
686 if (!read_stdin && (curbuf != old_curbuf | |
687 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) | |
688 || (using_b_fname && (old_b_fname != curbuf->b_fname)))) | |
689 { | |
690 EMSG(_(e_auchangedbuf)); | |
691 if (!read_buffer) | |
692 close(fd); | |
693 return FAIL; | |
694 } | |
695 #endif | |
7 | 696 #ifdef UNIX |
697 /* Set swap file protection bits after creating it. */ | |
1918 | 698 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL |
699 && curbuf->b_ml.ml_mfp->mf_fname != NULL) | |
7 | 700 (void)mch_setperm(curbuf->b_ml.ml_mfp->mf_fname, (long)swap_mode); |
701 #endif | |
702 } | |
703 | |
578 | 704 #if defined(HAS_SWAP_EXISTS_ACTION) |
7 | 705 /* If "Quit" selected at ATTENTION dialog, don't load the file */ |
706 if (swap_exists_action == SEA_QUIT) | |
707 { | |
708 if (!read_buffer && !read_stdin) | |
709 close(fd); | |
710 return FAIL; | |
711 } | |
712 #endif | |
713 | |
714 ++no_wait_return; /* don't wait for return yet */ | |
715 | |
716 /* | |
717 * Set '[ mark to the line above where the lines go (line 1 if zero). | |
718 */ | |
719 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); | |
720 curbuf->b_op_start.col = 0; | |
721 | |
722 #ifdef FEAT_AUTOCMD | |
723 if (!read_buffer) | |
724 { | |
725 int m = msg_scroll; | |
726 int n = msg_scrolled; | |
727 | |
728 /* | |
729 * The file must be closed again, the autocommands may want to change | |
730 * the file before reading it. | |
731 */ | |
732 if (!read_stdin) | |
733 close(fd); /* ignore errors */ | |
734 | |
735 /* | |
736 * The output from the autocommands should not overwrite anything and | |
737 * should not be overwritten: Set msg_scroll, restore its value if no | |
738 * output was done. | |
739 */ | |
740 msg_scroll = TRUE; | |
741 if (filtering) | |
742 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname, | |
743 FALSE, curbuf, eap); | |
744 else if (read_stdin) | |
745 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname, | |
746 FALSE, curbuf, eap); | |
747 else if (newfile) | |
748 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname, | |
749 FALSE, curbuf, eap); | |
750 else | |
751 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname, | |
752 FALSE, NULL, eap); | |
753 if (msg_scrolled == n) | |
754 msg_scroll = m; | |
755 | |
756 #ifdef FEAT_EVAL | |
757 if (aborting()) /* autocmds may abort script processing */ | |
758 { | |
759 --no_wait_return; | |
760 msg_scroll = msg_save; | |
761 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
762 return FAIL; | |
763 } | |
764 #endif | |
765 /* | |
766 * Don't allow the autocommands to change the current buffer. | |
767 * Try to re-open the file. | |
1834 | 768 * |
769 * Don't allow the autocommands to change the buffer name either | |
770 * (cd for example) if it invalidates fname or sfname. | |
7 | 771 */ |
772 if (!read_stdin && (curbuf != old_curbuf | |
1834 | 773 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) |
774 || (using_b_fname && (old_b_fname != curbuf->b_fname)) | |
7 | 775 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0)) |
776 { | |
777 --no_wait_return; | |
778 msg_scroll = msg_save; | |
779 if (fd < 0) | |
780 EMSG(_("E200: *ReadPre autocommands made the file unreadable")); | |
781 else | |
782 EMSG(_("E201: *ReadPre autocommands must not change current buffer")); | |
783 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
784 return FAIL; | |
785 } | |
786 } | |
787 #endif /* FEAT_AUTOCMD */ | |
788 | |
789 /* Autocommands may add lines to the file, need to check if it is empty */ | |
790 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY); | |
791 | |
792 if (!recoverymode && !filtering && !(flags & READ_DUMMY)) | |
793 { | |
794 /* | |
795 * Show the user that we are busy reading the input. Sometimes this | |
796 * may take a while. When reading from stdin another program may | |
797 * still be running, don't move the cursor to the last line, unless | |
798 * always using the GUI. | |
799 */ | |
800 if (read_stdin) | |
801 { | |
802 #ifndef ALWAYS_USE_GUI | |
803 mch_msg(_("Vim: Reading from stdin...\n")); | |
804 #endif | |
805 #ifdef FEAT_GUI | |
806 /* Also write a message in the GUI window, if there is one. */ | |
807 if (gui.in_use && !gui.dying && !gui.starting) | |
808 { | |
809 p = (char_u *)_("Reading from stdin..."); | |
810 gui_write(p, (int)STRLEN(p)); | |
811 } | |
812 #endif | |
813 } | |
814 else if (!read_buffer) | |
815 filemess(curbuf, sfname, (char_u *)"", 0); | |
816 } | |
817 | |
818 msg_scroll = FALSE; /* overwrite the file message */ | |
819 | |
820 /* | |
821 * Set linecnt now, before the "retry" caused by a wrong guess for | |
822 * fileformat, and after the autocommands, which may change them. | |
823 */ | |
824 linecnt = curbuf->b_ml.ml_line_count; | |
825 | |
826 #ifdef FEAT_MBYTE | |
595 | 827 /* "++bad=" argument. */ |
828 if (eap != NULL && eap->bad_char != 0) | |
612 | 829 { |
595 | 830 bad_char_behavior = eap->bad_char; |
819 | 831 if (set_options) |
612 | 832 curbuf->b_bad_char = eap->bad_char; |
833 } | |
834 else | |
835 curbuf->b_bad_char = 0; | |
595 | 836 |
7 | 837 /* |
595 | 838 * Decide which 'encoding' to use or use first. |
7 | 839 */ |
840 if (eap != NULL && eap->force_enc != 0) | |
841 { | |
842 fenc = enc_canonize(eap->cmd + eap->force_enc); | |
843 fenc_alloced = TRUE; | |
595 | 844 keep_dest_enc = TRUE; |
7 | 845 } |
846 else if (curbuf->b_p_bin) | |
847 { | |
848 fenc = (char_u *)""; /* binary: don't convert */ | |
849 fenc_alloced = FALSE; | |
850 } | |
851 else if (curbuf->b_help) | |
852 { | |
853 char_u firstline[80]; | |
301 | 854 int fc; |
7 | 855 |
856 /* Help files are either utf-8 or latin1. Try utf-8 first, if this | |
857 * fails it must be latin1. | |
858 * Always do this when 'encoding' is "utf-8". Otherwise only do | |
859 * this when needed to avoid [converted] remarks all the time. | |
860 * It is needed when the first line contains non-ASCII characters. | |
861 * That is only in *.??x files. */ | |
862 fenc = (char_u *)"latin1"; | |
863 c = enc_utf8; | |
301 | 864 if (!c && !read_stdin) |
865 { | |
866 fc = fname[STRLEN(fname) - 1]; | |
867 if (TOLOWER_ASC(fc) == 'x') | |
868 { | |
869 /* Read the first line (and a bit more). Immediately rewind to | |
870 * the start of the file. If the read() fails "len" is -1. */ | |
2664 | 871 len = read_eintr(fd, firstline, 80); |
301 | 872 lseek(fd, (off_t)0L, SEEK_SET); |
873 for (p = firstline; p < firstline + len; ++p) | |
874 if (*p >= 0x80) | |
875 { | |
876 c = TRUE; | |
877 break; | |
878 } | |
879 } | |
7 | 880 } |
881 | |
882 if (c) | |
883 { | |
884 fenc_next = fenc; | |
885 fenc = (char_u *)"utf-8"; | |
886 | |
887 /* When the file is utf-8 but a character doesn't fit in | |
888 * 'encoding' don't retry. In help text editing utf-8 bytes | |
889 * doesn't make sense. */ | |
844 | 890 if (!enc_utf8) |
891 keep_dest_enc = TRUE; | |
7 | 892 } |
893 fenc_alloced = FALSE; | |
894 } | |
895 else if (*p_fencs == NUL) | |
896 { | |
897 fenc = curbuf->b_p_fenc; /* use format from buffer */ | |
898 fenc_alloced = FALSE; | |
899 } | |
900 else | |
901 { | |
902 fenc_next = p_fencs; /* try items in 'fileencodings' */ | |
903 fenc = next_fenc(&fenc_next); | |
904 fenc_alloced = TRUE; | |
905 } | |
906 #endif | |
907 | |
908 /* | |
909 * Jump back here to retry reading the file in different ways. | |
910 * Reasons to retry: | |
911 * - encoding conversion failed: try another one from "fenc_next" | |
912 * - BOM detected and fenc was set, need to setup conversion | |
913 * - "fileformat" check failed: try another | |
914 * | |
915 * Variables set for special retry actions: | |
916 * "file_rewind" Rewind the file to start reading it again. | |
917 * "advance_fenc" Advance "fenc" using "fenc_next". | |
918 * "skip_read" Re-use already read bytes (BOM detected). | |
919 * "did_iconv" iconv() conversion failed, try 'charconvert'. | |
920 * "keep_fileformat" Don't reset "fileformat". | |
921 * | |
922 * Other status indicators: | |
923 * "tmpname" When != NULL did conversion with 'charconvert'. | |
924 * Output file has to be deleted afterwards. | |
925 * "iconv_fd" When != -1 did conversion with iconv(). | |
926 */ | |
927 retry: | |
928 | |
929 if (file_rewind) | |
930 { | |
931 if (read_buffer) | |
932 { | |
933 read_buf_lnum = 1; | |
934 read_buf_col = 0; | |
935 } | |
936 else if (read_stdin || lseek(fd, (off_t)0L, SEEK_SET) != 0) | |
937 { | |
938 /* Can't rewind the file, give up. */ | |
939 error = TRUE; | |
940 goto failed; | |
941 } | |
942 /* Delete the previously read lines. */ | |
943 while (lnum > from) | |
944 ml_delete(lnum--, FALSE); | |
945 file_rewind = FALSE; | |
946 #ifdef FEAT_MBYTE | |
819 | 947 if (set_options) |
1352 | 948 { |
7 | 949 curbuf->b_p_bomb = FALSE; |
1352 | 950 curbuf->b_start_bomb = FALSE; |
951 } | |
595 | 952 conv_error = 0; |
7 | 953 #endif |
954 } | |
955 | |
956 /* | |
957 * When retrying with another "fenc" and the first time "fileformat" | |
958 * will be reset. | |
959 */ | |
960 if (keep_fileformat) | |
961 keep_fileformat = FALSE; | |
962 else | |
963 { | |
964 if (eap != NULL && eap->force_ff != 0) | |
1742 | 965 { |
7 | 966 fileformat = get_fileformat_force(curbuf, eap); |
1742 | 967 try_unix = try_dos = try_mac = FALSE; |
968 } | |
7 | 969 else if (curbuf->b_p_bin) |
970 fileformat = EOL_UNIX; /* binary: use Unix format */ | |
971 else if (*p_ffs == NUL) | |
972 fileformat = get_fileformat(curbuf);/* use format from buffer */ | |
973 else | |
974 fileformat = EOL_UNKNOWN; /* detect from file */ | |
975 } | |
976 | |
977 #ifdef FEAT_MBYTE | |
978 # ifdef USE_ICONV | |
979 if (iconv_fd != (iconv_t)-1) | |
980 { | |
981 /* aborted conversion with iconv(), close the descriptor */ | |
982 iconv_close(iconv_fd); | |
983 iconv_fd = (iconv_t)-1; | |
984 } | |
985 # endif | |
986 | |
987 if (advance_fenc) | |
988 { | |
989 /* | |
990 * Try the next entry in 'fileencodings'. | |
991 */ | |
992 advance_fenc = FALSE; | |
993 | |
994 if (eap != NULL && eap->force_enc != 0) | |
995 { | |
996 /* Conversion given with "++cc=" wasn't possible, read | |
997 * without conversion. */ | |
998 notconverted = TRUE; | |
595 | 999 conv_error = 0; |
7 | 1000 if (fenc_alloced) |
1001 vim_free(fenc); | |
1002 fenc = (char_u *)""; | |
1003 fenc_alloced = FALSE; | |
1004 } | |
1005 else | |
1006 { | |
1007 if (fenc_alloced) | |
1008 vim_free(fenc); | |
1009 if (fenc_next != NULL) | |
1010 { | |
1011 fenc = next_fenc(&fenc_next); | |
1012 fenc_alloced = (fenc_next != NULL); | |
1013 } | |
1014 else | |
1015 { | |
1016 fenc = (char_u *)""; | |
1017 fenc_alloced = FALSE; | |
1018 } | |
1019 } | |
1020 if (tmpname != NULL) | |
1021 { | |
1022 mch_remove(tmpname); /* delete converted file */ | |
1023 vim_free(tmpname); | |
1024 tmpname = NULL; | |
1025 } | |
1026 } | |
1027 | |
1028 /* | |
1948 | 1029 * Conversion may be required when the encoding of the file is different |
1030 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4. | |
7 | 1031 */ |
1032 fio_flags = 0; | |
1948 | 1033 converted = need_conversion(fenc); |
1034 if (converted) | |
7 | 1035 { |
1036 | |
1037 /* "ucs-bom" means we need to check the first bytes of the file | |
1038 * for a BOM. */ | |
1039 if (STRCMP(fenc, ENC_UCSBOM) == 0) | |
1040 fio_flags = FIO_UCSBOM; | |
1041 | |
1042 /* | |
1043 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be | |
1044 * done. This is handled below after read(). Prepare the | |
1045 * fio_flags to avoid having to parse the string each time. | |
1046 * Also check for Unicode to Latin1 conversion, because iconv() | |
1047 * appears not to handle this correctly. This works just like | |
1048 * conversion to UTF-8 except how the resulting character is put in | |
1049 * the buffer. | |
1050 */ | |
1051 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
1052 fio_flags = get_fio_flags(fenc); | |
1053 | |
1054 # ifdef WIN3264 | |
1055 /* | |
1056 * Conversion from an MS-Windows codepage to UTF-8 or another codepage | |
1057 * is handled with MultiByteToWideChar(). | |
1058 */ | |
1059 if (fio_flags == 0) | |
1060 fio_flags = get_win_fio_flags(fenc); | |
1061 # endif | |
1062 | |
1063 # ifdef MACOS_X | |
1064 /* Conversion from Apple MacRoman to latin1 or UTF-8 */ | |
1065 if (fio_flags == 0) | |
1066 fio_flags = get_mac_fio_flags(fenc); | |
1067 # endif | |
1068 | |
1069 # ifdef USE_ICONV | |
1070 /* | |
1071 * Try using iconv() if we can't convert internally. | |
1072 */ | |
1073 if (fio_flags == 0 | |
1074 # ifdef FEAT_EVAL | |
1075 && !did_iconv | |
1076 # endif | |
1077 ) | |
1078 iconv_fd = (iconv_t)my_iconv_open( | |
1079 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc); | |
1080 # endif | |
1081 | |
1082 # ifdef FEAT_EVAL | |
1083 /* | |
1084 * Use the 'charconvert' expression when conversion is required | |
1085 * and we can't do it internally or with iconv(). | |
1086 */ | |
1087 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL | |
1088 # ifdef USE_ICONV | |
1089 && iconv_fd == (iconv_t)-1 | |
1090 # endif | |
1091 ) | |
1092 { | |
1093 # ifdef USE_ICONV | |
1094 did_iconv = FALSE; | |
1095 # endif | |
1096 /* Skip conversion when it's already done (retry for wrong | |
1097 * "fileformat"). */ | |
1098 if (tmpname == NULL) | |
1099 { | |
1100 tmpname = readfile_charconvert(fname, fenc, &fd); | |
1101 if (tmpname == NULL) | |
1102 { | |
1103 /* Conversion failed. Try another one. */ | |
1104 advance_fenc = TRUE; | |
1105 if (fd < 0) | |
1106 { | |
1107 /* Re-opening the original file failed! */ | |
1108 EMSG(_("E202: Conversion made file unreadable!")); | |
1109 error = TRUE; | |
1110 goto failed; | |
1111 } | |
1112 goto retry; | |
1113 } | |
1114 } | |
1115 } | |
1116 else | |
1117 # endif | |
1118 { | |
1119 if (fio_flags == 0 | |
1120 # ifdef USE_ICONV | |
1121 && iconv_fd == (iconv_t)-1 | |
1122 # endif | |
1123 ) | |
1124 { | |
1125 /* Conversion wanted but we can't. | |
1126 * Try the next conversion in 'fileencodings' */ | |
1127 advance_fenc = TRUE; | |
1128 goto retry; | |
1129 } | |
1130 } | |
1131 } | |
1132 | |
595 | 1133 /* Set "can_retry" when it's possible to rewind the file and try with |
7 | 1134 * another "fenc" value. It's FALSE when no other "fenc" to try, reading |
595 | 1135 * stdin or fixed at a specific encoding. */ |
1136 can_retry = (*fenc != NUL && !read_stdin && !keep_dest_enc); | |
7 | 1137 #endif |
1138 | |
1139 if (!skip_read) | |
1140 { | |
1141 linerest = 0; | |
1142 filesize = 0; | |
1143 skip_count = lines_to_skip; | |
1144 read_count = lines_to_read; | |
1145 #ifdef FEAT_MBYTE | |
1146 conv_restlen = 0; | |
1147 #endif | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1148 #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
|
1149 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
|
1150 && 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
|
1151 && 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
|
1152 && !filtering |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1153 && !read_stdin |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1154 && !read_buffer); |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1155 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
|
1156 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
|
1157 #endif |
6122 | 1158 #ifdef FEAT_CRYPT |
1159 if (curbuf->b_cryptstate != NULL) | |
1160 { | |
1161 /* Need to free the state, but keep the key, don't want to ask for | |
1162 * it again. */ | |
1163 crypt_free_state(curbuf->b_cryptstate); | |
1164 curbuf->b_cryptstate = NULL; | |
1165 } | |
1166 #endif | |
7 | 1167 } |
1168 | |
1169 while (!error && !got_int) | |
1170 { | |
1171 /* | |
1172 * We allocate as much space for the file as we can get, plus | |
1173 * space for the old line plus room for one terminating NUL. | |
1174 * The amount is limited by the fact that read() only can read | |
1175 * upto max_unsigned characters (and other things). | |
1176 */ | |
5684 | 1177 #if VIM_SIZEOF_INT <= 2 |
7 | 1178 if (linerest >= 0x7ff0) |
1179 { | |
1180 ++split; | |
1181 *ptr = NL; /* split line by inserting a NL */ | |
1182 size = 1; | |
1183 } | |
1184 else | |
1185 #endif | |
1186 { | |
1187 if (!skip_read) | |
1188 { | |
5684 | 1189 #if VIM_SIZEOF_INT > 2 |
1076 | 1190 # if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L) |
7 | 1191 size = SSIZE_MAX; /* use max I/O size, 52K */ |
1192 # else | |
1193 size = 0x10000L; /* use buffer >= 64K */ | |
1194 # endif | |
1195 #else | |
1196 size = 0x7ff0L - linerest; /* limit buffer to 32K */ | |
1197 #endif | |
1198 | |
836 | 1199 for ( ; size >= 10; size = (long)((long_u)size >> 1)) |
7 | 1200 { |
1201 if ((new_buffer = lalloc((long_u)(size + linerest + 1), | |
1202 FALSE)) != NULL) | |
1203 break; | |
1204 } | |
1205 if (new_buffer == NULL) | |
1206 { | |
1207 do_outofmem_msg((long_u)(size * 2 + linerest + 1)); | |
1208 error = TRUE; | |
1209 break; | |
1210 } | |
1211 if (linerest) /* copy characters from the previous buffer */ | |
1212 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest); | |
1213 vim_free(buffer); | |
1214 buffer = new_buffer; | |
1215 ptr = buffer + linerest; | |
1216 line_start = buffer; | |
1217 | |
1218 #ifdef FEAT_MBYTE | |
1219 /* May need room to translate into. | |
1220 * For iconv() we don't really know the required space, use a | |
1221 * factor ICONV_MULT. | |
1222 * latin1 to utf-8: 1 byte becomes up to 2 bytes | |
1223 * utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes | |
1224 * become up to 4 bytes, size must be multiple of 2 | |
1225 * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be | |
1226 * multiple of 2 | |
1227 * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be | |
1228 * multiple of 4 */ | |
835 | 1229 real_size = (int)size; |
7 | 1230 # ifdef USE_ICONV |
1231 if (iconv_fd != (iconv_t)-1) | |
1232 size = size / ICONV_MULT; | |
1233 else | |
1234 # endif | |
1235 if (fio_flags & FIO_LATIN1) | |
1236 size = size / 2; | |
1237 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1238 size = (size * 2 / 3) & ~1; | |
1239 else if (fio_flags & FIO_UCS4) | |
1240 size = (size * 2 / 3) & ~3; | |
1241 else if (fio_flags == FIO_UCSBOM) | |
1242 size = size / ICONV_MULT; /* worst case */ | |
1243 # ifdef WIN3264 | |
1244 else if (fio_flags & FIO_CODEPAGE) | |
1245 size = size / ICONV_MULT; /* also worst case */ | |
1246 # endif | |
1247 # ifdef MACOS_X | |
1248 else if (fio_flags & FIO_MACROMAN) | |
1249 size = size / ICONV_MULT; /* also worst case */ | |
1250 # endif | |
1251 #endif | |
1252 | |
1253 #ifdef FEAT_MBYTE | |
1254 if (conv_restlen > 0) | |
1255 { | |
1256 /* Insert unconverted bytes from previous line. */ | |
1257 mch_memmove(ptr, conv_rest, conv_restlen); | |
1258 ptr += conv_restlen; | |
1259 size -= conv_restlen; | |
1260 } | |
1261 #endif | |
1262 | |
1263 if (read_buffer) | |
1264 { | |
1265 /* | |
1266 * Read bytes from curbuf. Used for converting text read | |
1267 * from stdin. | |
1268 */ | |
1269 if (read_buf_lnum > from) | |
1270 size = 0; | |
1271 else | |
1272 { | |
1273 int n, ni; | |
1274 long tlen; | |
1275 | |
1276 tlen = 0; | |
1277 for (;;) | |
1278 { | |
1279 p = ml_get(read_buf_lnum) + read_buf_col; | |
1280 n = (int)STRLEN(p); | |
1281 if ((int)tlen + n + 1 > size) | |
1282 { | |
1283 /* Filled up to "size", append partial line. | |
1284 * Change NL to NUL to reverse the effect done | |
1285 * below. */ | |
835 | 1286 n = (int)(size - tlen); |
7 | 1287 for (ni = 0; ni < n; ++ni) |
1288 { | |
1289 if (p[ni] == NL) | |
1290 ptr[tlen++] = NUL; | |
1291 else | |
1292 ptr[tlen++] = p[ni]; | |
1293 } | |
1294 read_buf_col += n; | |
1295 break; | |
1296 } | |
1297 else | |
1298 { | |
1299 /* Append whole line and new-line. Change NL | |
1300 * to NUL to reverse the effect done below. */ | |
1301 for (ni = 0; ni < n; ++ni) | |
1302 { | |
1303 if (p[ni] == NL) | |
1304 ptr[tlen++] = NUL; | |
1305 else | |
1306 ptr[tlen++] = p[ni]; | |
1307 } | |
1308 ptr[tlen++] = NL; | |
1309 read_buf_col = 0; | |
1310 if (++read_buf_lnum > from) | |
1311 { | |
1312 /* When the last line didn't have an | |
1313 * end-of-line don't add it now either. */ | |
1314 if (!curbuf->b_p_eol) | |
1315 --tlen; | |
1316 size = tlen; | |
1317 break; | |
1318 } | |
1319 } | |
1320 } | |
1321 } | |
1322 } | |
1323 else | |
1324 { | |
1325 /* | |
1326 * Read bytes from the file. | |
1327 */ | |
2664 | 1328 size = read_eintr(fd, ptr, size); |
7 | 1329 } |
1330 | |
6122 | 1331 #ifdef FEAT_CRYPT |
1332 /* | |
1333 * At start of file: Check for magic number of encryption. | |
1334 */ | |
1335 if (filesize == 0 && size > 0) | |
1336 cryptkey = check_for_cryptkey(cryptkey, ptr, &size, | |
1337 &filesize, newfile, sfname, | |
1338 &did_ask_for_key); | |
1339 /* | |
1340 * Decrypt the read bytes. This is done before checking for | |
1341 * EOF because the crypt layer may be buffering. | |
1342 */ | |
1343 if (cryptkey != NULL && size > 0) | |
1344 { | |
1345 if (crypt_works_inplace(curbuf->b_cryptstate)) | |
1346 { | |
1347 crypt_decode_inplace(curbuf->b_cryptstate, ptr, size); | |
1348 } | |
1349 else | |
1350 { | |
1351 char_u *newptr = NULL; | |
1352 int decrypted_size; | |
1353 | |
1354 decrypted_size = crypt_decode_alloc( | |
1355 curbuf->b_cryptstate, ptr, size, &newptr); | |
1356 | |
1357 /* If the crypt layer is buffering, not producing | |
1358 * anything yet, need to read more. */ | |
1359 if (size > 0 && decrypted_size == 0) | |
1360 continue; | |
1361 | |
1362 if (linerest == 0) | |
1363 { | |
1364 /* Simple case: reuse returned buffer (may be | |
1365 * NULL, checked later). */ | |
1366 new_buffer = newptr; | |
1367 } | |
1368 else | |
1369 { | |
1370 long_u new_size; | |
1371 | |
1372 /* Need new buffer to add bytes carried over. */ | |
1373 new_size = (long_u)(decrypted_size + linerest + 1); | |
1374 new_buffer = lalloc(new_size, FALSE); | |
1375 if (new_buffer == NULL) | |
1376 { | |
1377 do_outofmem_msg(new_size); | |
1378 error = TRUE; | |
1379 break; | |
1380 } | |
1381 | |
1382 mch_memmove(new_buffer, buffer, linerest); | |
1383 if (newptr != NULL) | |
1384 mch_memmove(new_buffer + linerest, newptr, | |
1385 decrypted_size); | |
1386 } | |
1387 | |
1388 if (new_buffer != NULL) | |
1389 { | |
1390 vim_free(buffer); | |
1391 buffer = new_buffer; | |
1392 new_buffer = NULL; | |
1393 line_start = buffer; | |
1394 ptr = buffer + linerest; | |
1395 } | |
1396 size = decrypted_size; | |
1397 } | |
1398 } | |
1399 #endif | |
1400 | |
7 | 1401 if (size <= 0) |
1402 { | |
1403 if (size < 0) /* read error */ | |
1404 error = TRUE; | |
1405 #ifdef FEAT_MBYTE | |
1406 else if (conv_restlen > 0) | |
595 | 1407 { |
1597 | 1408 /* |
1409 * Reached end-of-file but some trailing bytes could | |
1410 * not be converted. Truncated file? | |
1411 */ | |
1412 | |
1413 /* When we did a conversion report an error. */ | |
1414 if (fio_flags != 0 | |
1415 # ifdef USE_ICONV | |
1416 || iconv_fd != (iconv_t)-1 | |
1417 # endif | |
1418 ) | |
1419 { | |
4331 | 1420 if (can_retry) |
1421 goto rewind_retry; | |
1597 | 1422 if (conv_error == 0) |
1423 conv_error = curbuf->b_ml.ml_line_count | |
1424 - linecnt + 1; | |
1425 } | |
1426 /* Remember the first linenr with an illegal byte */ | |
1427 else if (illegal_byte == 0) | |
1428 illegal_byte = curbuf->b_ml.ml_line_count | |
1429 - linecnt + 1; | |
1430 if (bad_char_behavior == BAD_DROP) | |
595 | 1431 { |
1597 | 1432 *(ptr - conv_restlen) = NUL; |
1433 conv_restlen = 0; | |
1434 } | |
1435 else | |
1436 { | |
1437 /* Replace the trailing bytes with the replacement | |
1438 * character if we were converting; if we weren't, | |
1439 * leave the UTF8 checking code to do it, as it | |
1440 * works slightly differently. */ | |
1441 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0 | |
1442 # ifdef USE_ICONV | |
1443 || iconv_fd != (iconv_t)-1 | |
1444 # endif | |
1445 )) | |
1446 { | |
1447 while (conv_restlen > 0) | |
1448 { | |
1449 *(--ptr) = bad_char_behavior; | |
1450 --conv_restlen; | |
1451 } | |
1452 } | |
595 | 1453 fio_flags = 0; /* don't convert this */ |
834 | 1454 # ifdef USE_ICONV |
1455 if (iconv_fd != (iconv_t)-1) | |
1456 { | |
1457 iconv_close(iconv_fd); | |
1458 iconv_fd = (iconv_t)-1; | |
1459 } | |
1460 # endif | |
595 | 1461 } |
1462 } | |
7 | 1463 #endif |
1464 } | |
1465 } | |
1466 skip_read = FALSE; | |
1467 | |
1468 #ifdef FEAT_MBYTE | |
1469 /* | |
1470 * At start of file (or after crypt magic number): Check for BOM. | |
1471 * Also check for a BOM for other Unicode encodings, but not after | |
1472 * converting with 'charconvert' or when a BOM has already been | |
1473 * found. | |
1474 */ | |
1475 if ((filesize == 0 | |
1476 # ifdef FEAT_CRYPT | |
6122 | 1477 || (cryptkey != NULL |
1478 && filesize == crypt_get_header_len( | |
1479 crypt_get_method_nr(curbuf))) | |
7 | 1480 # endif |
1481 ) | |
1482 && (fio_flags == FIO_UCSBOM | |
1483 || (!curbuf->b_p_bomb | |
1484 && tmpname == NULL | |
1485 && (*fenc == 'u' || (*fenc == NUL && enc_utf8))))) | |
1486 { | |
1487 char_u *ccname; | |
1488 int blen; | |
1489 | |
1490 /* no BOM detection in a short file or in binary mode */ | |
1491 if (size < 2 || curbuf->b_p_bin) | |
1492 ccname = NULL; | |
1493 else | |
1494 ccname = check_for_bom(ptr, size, &blen, | |
1495 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc)); | |
1496 if (ccname != NULL) | |
1497 { | |
1498 /* Remove BOM from the text */ | |
1499 filesize += blen; | |
1500 size -= blen; | |
1501 mch_memmove(ptr, ptr + blen, (size_t)size); | |
819 | 1502 if (set_options) |
1352 | 1503 { |
7 | 1504 curbuf->b_p_bomb = TRUE; |
1352 | 1505 curbuf->b_start_bomb = TRUE; |
1506 } | |
7 | 1507 } |
1508 | |
1509 if (fio_flags == FIO_UCSBOM) | |
1510 { | |
1511 if (ccname == NULL) | |
1512 { | |
1513 /* No BOM detected: retry with next encoding. */ | |
1514 advance_fenc = TRUE; | |
1515 } | |
1516 else | |
1517 { | |
1518 /* BOM detected: set "fenc" and jump back */ | |
1519 if (fenc_alloced) | |
1520 vim_free(fenc); | |
1521 fenc = ccname; | |
1522 fenc_alloced = FALSE; | |
1523 } | |
1524 /* retry reading without getting new bytes or rewinding */ | |
1525 skip_read = TRUE; | |
1526 goto retry; | |
1527 } | |
1528 } | |
1597 | 1529 |
1530 /* Include not converted bytes. */ | |
1531 ptr -= conv_restlen; | |
1532 size += conv_restlen; | |
1533 conv_restlen = 0; | |
7 | 1534 #endif |
1535 /* | |
1536 * Break here for a read error or end-of-file. | |
1537 */ | |
1538 if (size <= 0) | |
1539 break; | |
1540 | |
1541 #ifdef FEAT_MBYTE | |
1542 | |
1543 # ifdef USE_ICONV | |
1544 if (iconv_fd != (iconv_t)-1) | |
1545 { | |
1546 /* | |
1547 * Attempt conversion of the read bytes to 'encoding' using | |
1548 * iconv(). | |
1549 */ | |
1550 const char *fromp; | |
1551 char *top; | |
1552 size_t from_size; | |
1553 size_t to_size; | |
1554 | |
1555 fromp = (char *)ptr; | |
1556 from_size = size; | |
1557 ptr += size; | |
1558 top = (char *)ptr; | |
1559 to_size = real_size - size; | |
1560 | |
1561 /* | |
1562 * If there is conversion error or not enough room try using | |
179 | 1563 * another conversion. Except for when there is no |
1564 * alternative (help files). | |
7 | 1565 */ |
177 | 1566 while ((iconv(iconv_fd, (void *)&fromp, &from_size, |
1567 &top, &to_size) | |
7 | 1568 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) |
1569 || from_size > CONV_RESTLEN) | |
177 | 1570 { |
595 | 1571 if (can_retry) |
375 | 1572 goto rewind_retry; |
595 | 1573 if (conv_error == 0) |
1574 conv_error = readfile_linenr(linecnt, | |
1575 ptr, (char_u *)top); | |
1576 | |
1577 /* Deal with a bad byte and continue with the next. */ | |
177 | 1578 ++fromp; |
1579 --from_size; | |
595 | 1580 if (bad_char_behavior == BAD_KEEP) |
1581 { | |
1582 *top++ = *(fromp - 1); | |
1583 --to_size; | |
1584 } | |
1585 else if (bad_char_behavior != BAD_DROP) | |
1586 { | |
1587 *top++ = bad_char_behavior; | |
1588 --to_size; | |
1589 } | |
177 | 1590 } |
7 | 1591 |
1592 if (from_size > 0) | |
1593 { | |
1594 /* Some remaining characters, keep them for the next | |
1595 * round. */ | |
1596 mch_memmove(conv_rest, (char_u *)fromp, from_size); | |
1597 conv_restlen = (int)from_size; | |
1598 } | |
1599 | |
1600 /* move the linerest to before the converted characters */ | |
1601 line_start = ptr - linerest; | |
1602 mch_memmove(line_start, buffer, (size_t)linerest); | |
1603 size = (long)((char_u *)top - ptr); | |
1604 } | |
1605 # endif | |
1606 | |
1607 # ifdef WIN3264 | |
1608 if (fio_flags & FIO_CODEPAGE) | |
1609 { | |
595 | 1610 char_u *src, *dst; |
1611 WCHAR ucs2buf[3]; | |
1612 int ucs2len; | |
1613 int codepage = FIO_GET_CP(fio_flags); | |
1614 int bytelen; | |
1615 int found_bad; | |
1616 char replstr[2]; | |
1617 | |
7 | 1618 /* |
1619 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or | |
595 | 1620 * a codepage, using standard MS-Windows functions. This |
1621 * requires two steps: | |
1622 * 1. convert from 'fileencoding' to ucs-2 | |
1623 * 2. convert from ucs-2 to 'encoding' | |
1624 * | |
1625 * Because there may be illegal bytes AND an incomplete byte | |
1626 * sequence at the end, we may have to do the conversion one | |
1627 * character at a time to get it right. | |
7 | 1628 */ |
595 | 1629 |
1630 /* Replacement string for WideCharToMultiByte(). */ | |
1631 if (bad_char_behavior > 0) | |
1632 replstr[0] = bad_char_behavior; | |
1633 else | |
1634 replstr[0] = '?'; | |
1635 replstr[1] = NUL; | |
1636 | |
1637 /* | |
1638 * Move the bytes to the end of the buffer, so that we have | |
1639 * room to put the result at the start. | |
1640 */ | |
1641 src = ptr + real_size - size; | |
1642 mch_memmove(src, ptr, size); | |
7 | 1643 |
1644 /* | |
595 | 1645 * Do the conversion. |
7 | 1646 */ |
595 | 1647 dst = ptr; |
1648 size = size; | |
1649 while (size > 0) | |
7 | 1650 { |
595 | 1651 found_bad = FALSE; |
7 | 1652 |
1653 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
595 | 1654 if (codepage == CP_UTF8) |
7 | 1655 { |
595 | 1656 /* Handle CP_UTF8 input ourselves to be able to handle |
1657 * trailing bytes properly. | |
1658 * Get one UTF-8 character from src. */ | |
835 | 1659 bytelen = (int)utf_ptr2len_len(src, size); |
595 | 1660 if (bytelen > size) |
1661 { | |
1662 /* Only got some bytes of a character. Normally | |
1663 * it's put in "conv_rest", but if it's too long | |
1664 * deal with it as if they were illegal bytes. */ | |
1665 if (bytelen <= CONV_RESTLEN) | |
1666 break; | |
1667 | |
1668 /* weird overlong byte sequence */ | |
1669 bytelen = size; | |
1670 found_bad = TRUE; | |
1671 } | |
1672 else | |
1673 { | |
799 | 1674 int u8c = utf_ptr2char(src); |
1675 | |
622 | 1676 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1)) |
595 | 1677 found_bad = TRUE; |
1678 ucs2buf[0] = u8c; | |
1679 ucs2len = 1; | |
1680 } | |
7 | 1681 } |
595 | 1682 else |
7 | 1683 # endif |
595 | 1684 { |
1685 /* We don't know how long the byte sequence is, try | |
1686 * from one to three bytes. */ | |
1687 for (bytelen = 1; bytelen <= size && bytelen <= 3; | |
1688 ++bytelen) | |
1689 { | |
1690 ucs2len = MultiByteToWideChar(codepage, | |
1691 MB_ERR_INVALID_CHARS, | |
1692 (LPCSTR)src, bytelen, | |
1693 ucs2buf, 3); | |
1694 if (ucs2len > 0) | |
1695 break; | |
1696 } | |
1697 if (ucs2len == 0) | |
1698 { | |
1699 /* If we have only one byte then it's probably an | |
1700 * incomplete byte sequence. Otherwise discard | |
1701 * one byte as a bad character. */ | |
1702 if (size == 1) | |
1703 break; | |
1704 found_bad = TRUE; | |
1705 bytelen = 1; | |
1706 } | |
1707 } | |
1708 | |
1709 if (!found_bad) | |
7 | 1710 { |
595 | 1711 int i; |
1712 | |
1713 /* Convert "ucs2buf[ucs2len]" to 'enc' in "dst". */ | |
1714 if (enc_utf8) | |
1715 { | |
1716 /* From UCS-2 to UTF-8. Cannot fail. */ | |
1717 for (i = 0; i < ucs2len; ++i) | |
1718 dst += utf_char2bytes(ucs2buf[i], dst); | |
1719 } | |
1720 else | |
1721 { | |
1722 BOOL bad = FALSE; | |
1723 int dstlen; | |
1724 | |
1725 /* From UCS-2 to "enc_codepage". If the | |
1726 * conversion uses the default character "?", | |
1727 * the data doesn't fit in this encoding. */ | |
1728 dstlen = WideCharToMultiByte(enc_codepage, 0, | |
1729 (LPCWSTR)ucs2buf, ucs2len, | |
835 | 1730 (LPSTR)dst, (int)(src - dst), |
595 | 1731 replstr, &bad); |
1732 if (bad) | |
1733 found_bad = TRUE; | |
1734 else | |
1735 dst += dstlen; | |
1736 } | |
7 | 1737 } |
595 | 1738 |
1739 if (found_bad) | |
1740 { | |
1741 /* Deal with bytes we can't convert. */ | |
1742 if (can_retry) | |
1743 goto rewind_retry; | |
1744 if (conv_error == 0) | |
1745 conv_error = readfile_linenr(linecnt, ptr, dst); | |
1746 if (bad_char_behavior != BAD_DROP) | |
1747 { | |
1748 if (bad_char_behavior == BAD_KEEP) | |
1749 { | |
1750 mch_memmove(dst, src, bytelen); | |
1751 dst += bytelen; | |
1752 } | |
1753 else | |
1754 *dst++ = bad_char_behavior; | |
1755 } | |
1756 } | |
1757 | |
1758 src += bytelen; | |
1759 size -= bytelen; | |
7 | 1760 } |
595 | 1761 |
1762 if (size > 0) | |
7 | 1763 { |
595 | 1764 /* An incomplete byte sequence remaining. */ |
1765 mch_memmove(conv_rest, src, size); | |
1766 conv_restlen = size; | |
7 | 1767 } |
595 | 1768 |
1769 /* The new size is equal to how much "dst" was advanced. */ | |
835 | 1770 size = (long)(dst - ptr); |
7 | 1771 } |
1772 else | |
1773 # endif | |
765 | 1774 # ifdef MACOS_CONVERT |
7 | 1775 if (fio_flags & FIO_MACROMAN) |
1776 { | |
1777 /* | |
1778 * Conversion from Apple MacRoman char encoding to UTF-8 or | |
18 | 1779 * latin1. This is in os_mac_conv.c. |
7 | 1780 */ |
18 | 1781 if (macroman2enc(ptr, &size, real_size) == FAIL) |
7 | 1782 goto rewind_retry; |
1783 } | |
1784 else | |
1785 # endif | |
1786 if (fio_flags != 0) | |
1787 { | |
1788 int u8c; | |
1789 char_u *dest; | |
1790 char_u *tail = NULL; | |
1791 | |
1792 /* | |
1793 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8. | |
1794 * "enc_utf8" not set: Convert Unicode to Latin1. | |
1795 * Go from end to start through the buffer, because the number | |
1796 * of bytes may increase. | |
1797 * "dest" points to after where the UTF-8 bytes go, "p" points | |
1798 * to after the next character to convert. | |
1799 */ | |
1800 dest = ptr + real_size; | |
1801 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8) | |
1802 { | |
1803 p = ptr + size; | |
1804 if (fio_flags == FIO_UTF8) | |
1805 { | |
1806 /* Check for a trailing incomplete UTF-8 sequence */ | |
1807 tail = ptr + size - 1; | |
1808 while (tail > ptr && (*tail & 0xc0) == 0x80) | |
1809 --tail; | |
1810 if (tail + utf_byte2len(*tail) <= ptr + size) | |
1811 tail = NULL; | |
1812 else | |
1813 p = tail; | |
1814 } | |
1815 } | |
1816 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1817 { | |
1818 /* Check for a trailing byte */ | |
1819 p = ptr + (size & ~1); | |
1820 if (size & 1) | |
1821 tail = p; | |
1822 if ((fio_flags & FIO_UTF16) && p > ptr) | |
1823 { | |
1824 /* Check for a trailing leading word */ | |
1825 if (fio_flags & FIO_ENDIAN_L) | |
1826 { | |
1827 u8c = (*--p << 8); | |
1828 u8c += *--p; | |
1829 } | |
1830 else | |
1831 { | |
1832 u8c = *--p; | |
1833 u8c += (*--p << 8); | |
1834 } | |
1835 if (u8c >= 0xd800 && u8c <= 0xdbff) | |
1836 tail = p; | |
1837 else | |
1838 p += 2; | |
1839 } | |
1840 } | |
1841 else /* FIO_UCS4 */ | |
1842 { | |
1843 /* Check for trailing 1, 2 or 3 bytes */ | |
1844 p = ptr + (size & ~3); | |
1845 if (size & 3) | |
1846 tail = p; | |
1847 } | |
1848 | |
1849 /* If there is a trailing incomplete sequence move it to | |
1850 * conv_rest[]. */ | |
1851 if (tail != NULL) | |
1852 { | |
1853 conv_restlen = (int)((ptr + size) - tail); | |
1854 mch_memmove(conv_rest, (char_u *)tail, conv_restlen); | |
1855 size -= conv_restlen; | |
1856 } | |
1857 | |
1858 | |
1859 while (p > ptr) | |
1860 { | |
1861 if (fio_flags & FIO_LATIN1) | |
1862 u8c = *--p; | |
1863 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1864 { | |
1865 if (fio_flags & FIO_ENDIAN_L) | |
1866 { | |
1867 u8c = (*--p << 8); | |
1868 u8c += *--p; | |
1869 } | |
1870 else | |
1871 { | |
1872 u8c = *--p; | |
1873 u8c += (*--p << 8); | |
1874 } | |
1875 if ((fio_flags & FIO_UTF16) | |
1876 && u8c >= 0xdc00 && u8c <= 0xdfff) | |
1877 { | |
1878 int u16c; | |
1879 | |
1880 if (p == ptr) | |
1881 { | |
1882 /* Missing leading word. */ | |
1883 if (can_retry) | |
1884 goto rewind_retry; | |
595 | 1885 if (conv_error == 0) |
1886 conv_error = readfile_linenr(linecnt, | |
1887 ptr, p); | |
1888 if (bad_char_behavior == BAD_DROP) | |
1889 continue; | |
1890 if (bad_char_behavior != BAD_KEEP) | |
1891 u8c = bad_char_behavior; | |
7 | 1892 } |
1893 | |
1894 /* found second word of double-word, get the first | |
1895 * word and compute the resulting character */ | |
1896 if (fio_flags & FIO_ENDIAN_L) | |
1897 { | |
1898 u16c = (*--p << 8); | |
1899 u16c += *--p; | |
1900 } | |
1901 else | |
1902 { | |
1903 u16c = *--p; | |
1904 u16c += (*--p << 8); | |
1905 } | |
595 | 1906 u8c = 0x10000 + ((u16c & 0x3ff) << 10) |
1907 + (u8c & 0x3ff); | |
1908 | |
7 | 1909 /* Check if the word is indeed a leading word. */ |
1910 if (u16c < 0xd800 || u16c > 0xdbff) | |
1911 { | |
1912 if (can_retry) | |
1913 goto rewind_retry; | |
595 | 1914 if (conv_error == 0) |
1915 conv_error = readfile_linenr(linecnt, | |
1916 ptr, p); | |
1917 if (bad_char_behavior == BAD_DROP) | |
1918 continue; | |
1919 if (bad_char_behavior != BAD_KEEP) | |
1920 u8c = bad_char_behavior; | |
7 | 1921 } |
1922 } | |
1923 } | |
1924 else if (fio_flags & FIO_UCS4) | |
1925 { | |
1926 if (fio_flags & FIO_ENDIAN_L) | |
1927 { | |
1928 u8c = (*--p << 24); | |
1929 u8c += (*--p << 16); | |
1930 u8c += (*--p << 8); | |
1931 u8c += *--p; | |
1932 } | |
1933 else /* big endian */ | |
1934 { | |
1935 u8c = *--p; | |
1936 u8c += (*--p << 8); | |
1937 u8c += (*--p << 16); | |
1938 u8c += (*--p << 24); | |
1939 } | |
1940 } | |
1941 else /* UTF-8 */ | |
1942 { | |
1943 if (*--p < 0x80) | |
1944 u8c = *p; | |
1945 else | |
1946 { | |
1947 len = utf_head_off(ptr, p); | |
595 | 1948 p -= len; |
1949 u8c = utf_ptr2char(p); | |
7 | 1950 if (len == 0) |
1951 { | |
1952 /* Not a valid UTF-8 character, retry with | |
1953 * another fenc when possible, otherwise just | |
1954 * report the error. */ | |
1955 if (can_retry) | |
1956 goto rewind_retry; | |
595 | 1957 if (conv_error == 0) |
1958 conv_error = readfile_linenr(linecnt, | |
1959 ptr, p); | |
1960 if (bad_char_behavior == BAD_DROP) | |
1961 continue; | |
1962 if (bad_char_behavior != BAD_KEEP) | |
1963 u8c = bad_char_behavior; | |
7 | 1964 } |
1965 } | |
1966 } | |
1967 if (enc_utf8) /* produce UTF-8 */ | |
1968 { | |
1969 dest -= utf_char2len(u8c); | |
1970 (void)utf_char2bytes(u8c, dest); | |
1971 } | |
1972 else /* produce Latin1 */ | |
1973 { | |
1974 --dest; | |
1975 if (u8c >= 0x100) | |
1976 { | |
1977 /* character doesn't fit in latin1, retry with | |
1978 * another fenc when possible, otherwise just | |
1979 * report the error. */ | |
595 | 1980 if (can_retry) |
7 | 1981 goto rewind_retry; |
595 | 1982 if (conv_error == 0) |
1983 conv_error = readfile_linenr(linecnt, ptr, p); | |
1984 if (bad_char_behavior == BAD_DROP) | |
1985 ++dest; | |
1986 else if (bad_char_behavior == BAD_KEEP) | |
1987 *dest = u8c; | |
1988 else if (eap != NULL && eap->bad_char != 0) | |
1989 *dest = bad_char_behavior; | |
1990 else | |
1991 *dest = 0xBF; | |
7 | 1992 } |
1993 else | |
1994 *dest = u8c; | |
1995 } | |
1996 } | |
1997 | |
1998 /* move the linerest to before the converted characters */ | |
1999 line_start = dest - linerest; | |
2000 mch_memmove(line_start, buffer, (size_t)linerest); | |
2001 size = (long)((ptr + real_size) - dest); | |
2002 ptr = dest; | |
2003 } | |
1597 | 2004 else if (enc_utf8 && !curbuf->b_p_bin) |
2005 { | |
2006 int incomplete_tail = FALSE; | |
2007 | |
2008 /* Reading UTF-8: Check if the bytes are valid UTF-8. */ | |
2009 for (p = ptr; ; ++p) | |
7 | 2010 { |
835 | 2011 int todo = (int)((ptr + size) - p); |
595 | 2012 int l; |
2013 | |
2014 if (todo <= 0) | |
2015 break; | |
7 | 2016 if (*p >= 0x80) |
2017 { | |
2018 /* A length of 1 means it's an illegal byte. Accept | |
2019 * an incomplete character at the end though, the next | |
2020 * read() will get the next bytes, we'll check it | |
2021 * then. */ | |
595 | 2022 l = utf_ptr2len_len(p, todo); |
1597 | 2023 if (l > todo && !incomplete_tail) |
7 | 2024 { |
1597 | 2025 /* Avoid retrying with a different encoding when |
2026 * a truncated file is more likely, or attempting | |
2027 * to read the rest of an incomplete sequence when | |
2028 * we have already done so. */ | |
2029 if (p > ptr || filesize > 0) | |
2030 incomplete_tail = TRUE; | |
2031 /* Incomplete byte sequence, move it to conv_rest[] | |
2032 * and try to read the rest of it, unless we've | |
2033 * already done so. */ | |
2034 if (p > ptr) | |
2035 { | |
2036 conv_restlen = todo; | |
2037 mch_memmove(conv_rest, p, conv_restlen); | |
2038 size -= conv_restlen; | |
2039 break; | |
2040 } | |
7 | 2041 } |
1597 | 2042 if (l == 1 || l > todo) |
595 | 2043 { |
2044 /* Illegal byte. If we can try another encoding | |
1597 | 2045 * do that, unless at EOF where a truncated |
2046 * file is more likely than a conversion error. */ | |
2047 if (can_retry && !incomplete_tail) | |
595 | 2048 break; |
2049 # ifdef USE_ICONV | |
2050 /* When we did a conversion report an error. */ | |
2051 if (iconv_fd != (iconv_t)-1 && conv_error == 0) | |
2052 conv_error = readfile_linenr(linecnt, ptr, p); | |
2053 # endif | |
1597 | 2054 /* Remember the first linenr with an illegal byte */ |
2055 if (conv_error == 0 && illegal_byte == 0) | |
2056 illegal_byte = readfile_linenr(linecnt, ptr, p); | |
595 | 2057 |
2058 /* Drop, keep or replace the bad byte. */ | |
2059 if (bad_char_behavior == BAD_DROP) | |
2060 { | |
1597 | 2061 mch_memmove(p, p + 1, todo - 1); |
595 | 2062 --p; |
2063 --size; | |
2064 } | |
2065 else if (bad_char_behavior != BAD_KEEP) | |
2066 *p = bad_char_behavior; | |
2067 } | |
1597 | 2068 else |
2069 p += l - 1; | |
7 | 2070 } |
2071 } | |
1597 | 2072 if (p < ptr + size && !incomplete_tail) |
7 | 2073 { |
2074 /* Detected a UTF-8 error. */ | |
2075 rewind_retry: | |
595 | 2076 /* Retry reading with another conversion. */ |
7 | 2077 # if defined(FEAT_EVAL) && defined(USE_ICONV) |
595 | 2078 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1) |
2079 /* iconv() failed, try 'charconvert' */ | |
2080 did_iconv = TRUE; | |
7 | 2081 else |
2082 # endif | |
595 | 2083 /* use next item from 'fileencodings' */ |
2084 advance_fenc = TRUE; | |
2085 file_rewind = TRUE; | |
2086 goto retry; | |
7 | 2087 } |
2088 } | |
2089 #endif | |
2090 | |
2091 /* count the number of characters (after conversion!) */ | |
2092 filesize += size; | |
2093 | |
2094 /* | |
2095 * when reading the first part of a file: guess EOL type | |
2096 */ | |
2097 if (fileformat == EOL_UNKNOWN) | |
2098 { | |
2099 /* First try finding a NL, for Dos and Unix */ | |
2100 if (try_dos || try_unix) | |
2101 { | |
2102 for (p = ptr; p < ptr + size; ++p) | |
2103 { | |
6618 | 2104 /* Reset the carriage return counter. */ |
2105 if (try_mac) | |
2106 try_mac = 1; | |
2107 | |
7 | 2108 if (*p == NL) |
2109 { | |
2110 if (!try_unix | |
2111 || (try_dos && p > ptr && p[-1] == CAR)) | |
2112 fileformat = EOL_DOS; | |
2113 else | |
2114 fileformat = EOL_UNIX; | |
2115 break; | |
2116 } | |
6618 | 2117 else if (*p == CAR && try_mac) |
2118 try_mac++; | |
7 | 2119 } |
2120 | |
2121 /* Don't give in to EOL_UNIX if EOL_MAC is more likely */ | |
2122 if (fileformat == EOL_UNIX && try_mac) | |
2123 { | |
2124 /* Need to reset the counters when retrying fenc. */ | |
2125 try_mac = 1; | |
2126 try_unix = 1; | |
2127 for (; p >= ptr && *p != CAR; p--) | |
2128 ; | |
2129 if (p >= ptr) | |
2130 { | |
2131 for (p = ptr; p < ptr + size; ++p) | |
2132 { | |
2133 if (*p == NL) | |
2134 try_unix++; | |
2135 else if (*p == CAR) | |
2136 try_mac++; | |
2137 } | |
2138 if (try_mac > try_unix) | |
2139 fileformat = EOL_MAC; | |
2140 } | |
2141 } | |
6618 | 2142 else if (fileformat == EOL_UNKNOWN && try_mac == 1) |
2143 /* Looking for CR but found no end-of-line markers at | |
2144 * all: use the default format. */ | |
2145 fileformat = default_fileformat(); | |
7 | 2146 } |
2147 | |
2148 /* No NL found: may use Mac format */ | |
2149 if (fileformat == EOL_UNKNOWN && try_mac) | |
2150 fileformat = EOL_MAC; | |
2151 | |
2152 /* Still nothing found? Use first format in 'ffs' */ | |
2153 if (fileformat == EOL_UNKNOWN) | |
2154 fileformat = default_fileformat(); | |
2155 | |
2156 /* if editing a new file: may set p_tx and p_ff */ | |
819 | 2157 if (set_options) |
7 | 2158 set_fileformat(fileformat, OPT_LOCAL); |
2159 } | |
2160 } | |
2161 | |
2162 /* | |
2163 * This loop is executed once for every character read. | |
2164 * Keep it fast! | |
2165 */ | |
2166 if (fileformat == EOL_MAC) | |
2167 { | |
2168 --ptr; | |
2169 while (++ptr, --size >= 0) | |
2170 { | |
2171 /* catch most common case first */ | |
2172 if ((c = *ptr) != NUL && c != CAR && c != NL) | |
2173 continue; | |
2174 if (c == NUL) | |
2175 *ptr = NL; /* NULs are replaced by newlines! */ | |
2176 else if (c == NL) | |
2177 *ptr = CAR; /* NLs are replaced by CRs! */ | |
2178 else | |
2179 { | |
2180 if (skip_count == 0) | |
2181 { | |
2182 *ptr = NUL; /* end of line */ | |
2183 len = (colnr_T) (ptr - line_start + 1); | |
2184 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2185 { | |
2186 error = TRUE; | |
2187 break; | |
2188 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2189 #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
|
2190 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
|
2191 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
|
2192 #endif |
7 | 2193 ++lnum; |
2194 if (--read_count == 0) | |
2195 { | |
2196 error = TRUE; /* break loop */ | |
2197 line_start = ptr; /* nothing left to write */ | |
2198 break; | |
2199 } | |
2200 } | |
2201 else | |
2202 --skip_count; | |
2203 line_start = ptr + 1; | |
2204 } | |
2205 } | |
2206 } | |
2207 else | |
2208 { | |
2209 --ptr; | |
2210 while (++ptr, --size >= 0) | |
2211 { | |
2212 if ((c = *ptr) != NUL && c != NL) /* catch most common case */ | |
2213 continue; | |
2214 if (c == NUL) | |
2215 *ptr = NL; /* NULs are replaced by newlines! */ | |
2216 else | |
2217 { | |
2218 if (skip_count == 0) | |
2219 { | |
2220 *ptr = NUL; /* end of line */ | |
2221 len = (colnr_T)(ptr - line_start + 1); | |
2222 if (fileformat == EOL_DOS) | |
2223 { | |
2224 if (ptr[-1] == CAR) /* remove CR */ | |
2225 { | |
2226 ptr[-1] = NUL; | |
2227 --len; | |
2228 } | |
2229 /* | |
2230 * Reading in Dos format, but no CR-LF found! | |
2231 * When 'fileformats' includes "unix", delete all | |
2232 * the lines read so far and start all over again. | |
2233 * Otherwise give an error message later. | |
2234 */ | |
2235 else if (ff_error != EOL_DOS) | |
2236 { | |
2237 if ( try_unix | |
2238 && !read_stdin | |
2239 && (read_buffer | |
2240 || lseek(fd, (off_t)0L, SEEK_SET) == 0)) | |
2241 { | |
2242 fileformat = EOL_UNIX; | |
819 | 2243 if (set_options) |
7 | 2244 set_fileformat(EOL_UNIX, OPT_LOCAL); |
2245 file_rewind = TRUE; | |
2246 keep_fileformat = TRUE; | |
2247 goto retry; | |
2248 } | |
2249 ff_error = EOL_DOS; | |
2250 } | |
2251 } | |
2252 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2253 { | |
2254 error = TRUE; | |
2255 break; | |
2256 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2257 #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
|
2258 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
|
2259 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
|
2260 #endif |
7 | 2261 ++lnum; |
2262 if (--read_count == 0) | |
2263 { | |
2264 error = TRUE; /* break loop */ | |
2265 line_start = ptr; /* nothing left to write */ | |
2266 break; | |
2267 } | |
2268 } | |
2269 else | |
2270 --skip_count; | |
2271 line_start = ptr + 1; | |
2272 } | |
2273 } | |
2274 } | |
2275 linerest = (long)(ptr - line_start); | |
2276 ui_breakcheck(); | |
2277 } | |
2278 | |
2279 failed: | |
2280 /* not an error, max. number of lines reached */ | |
2281 if (error && read_count == 0) | |
2282 error = FALSE; | |
2283 | |
2284 /* | |
2285 * If we get EOF in the middle of a line, note the fact and | |
2286 * complete the line ourselves. | |
2287 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set. | |
2288 */ | |
2289 if (!error | |
2290 && !got_int | |
2291 && linerest != 0 | |
2292 && !(!curbuf->b_p_bin | |
2293 && fileformat == EOL_DOS | |
2294 && *line_start == Ctrl_Z | |
2295 && ptr == line_start + 1)) | |
2296 { | |
819 | 2297 /* remember for when writing */ |
2298 if (set_options) | |
7 | 2299 curbuf->b_p_eol = FALSE; |
2300 *ptr = NUL; | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2301 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
|
2302 if (ml_append(lnum, line_start, len, newfile) == FAIL) |
7 | 2303 error = TRUE; |
2304 else | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2305 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2306 #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
|
2307 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
|
2308 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
|
2309 #endif |
7 | 2310 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
|
2311 } |
7 | 2312 } |
2313 | |
819 | 2314 if (set_options) |
7 | 2315 save_file_ff(curbuf); /* remember the current file format */ |
2316 | |
2317 #ifdef FEAT_CRYPT | |
6122 | 2318 if (curbuf->b_cryptstate != NULL) |
2319 { | |
2320 crypt_free_state(curbuf->b_cryptstate); | |
2321 curbuf->b_cryptstate = NULL; | |
2322 } | |
2323 if (cryptkey != NULL && cryptkey != curbuf->b_p_key) | |
2324 crypt_free_key(cryptkey); | |
2325 /* Don't set cryptkey to NULL, it's used below as a flag that | |
2326 * encryption was used. */ | |
7 | 2327 #endif |
2328 | |
2329 #ifdef FEAT_MBYTE | |
819 | 2330 /* If editing a new file: set 'fenc' for the current buffer. |
2331 * Also for ":read ++edit file". */ | |
2332 if (set_options) | |
7 | 2333 set_string_option_direct((char_u *)"fenc", -1, fenc, |
694 | 2334 OPT_FREE|OPT_LOCAL, 0); |
7 | 2335 if (fenc_alloced) |
2336 vim_free(fenc); | |
2337 # ifdef USE_ICONV | |
2338 if (iconv_fd != (iconv_t)-1) | |
2339 { | |
2340 iconv_close(iconv_fd); | |
2341 iconv_fd = (iconv_t)-1; | |
2342 } | |
2343 # endif | |
2344 #endif | |
2345 | |
2346 if (!read_buffer && !read_stdin) | |
2347 close(fd); /* errors are ignored */ | |
2003 | 2348 #ifdef HAVE_FD_CLOEXEC |
2349 else | |
2350 { | |
2351 int fdflags = fcntl(fd, F_GETFD); | |
2352 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0) | |
2353 fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC); | |
2354 } | |
2355 #endif | |
7 | 2356 vim_free(buffer); |
2357 | |
2358 #ifdef HAVE_DUP | |
2359 if (read_stdin) | |
2360 { | |
2361 /* Use stderr for stdin, makes shell commands work. */ | |
2362 close(0); | |
1757 | 2363 ignored = dup(2); |
7 | 2364 } |
2365 #endif | |
2366 | |
2367 #ifdef FEAT_MBYTE | |
2368 if (tmpname != NULL) | |
2369 { | |
2370 mch_remove(tmpname); /* delete converted file */ | |
2371 vim_free(tmpname); | |
2372 } | |
2373 #endif | |
2374 --no_wait_return; /* may wait for return now */ | |
2375 | |
2376 /* | |
2377 * In recovery mode everything but autocommands is skipped. | |
2378 */ | |
2379 if (!recoverymode) | |
2380 { | |
2381 /* need to delete the last line, which comes from the empty buffer */ | |
2382 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY)) | |
2383 { | |
2384 #ifdef FEAT_NETBEANS_INTG | |
2385 netbeansFireChanges = 0; | |
2386 #endif | |
2387 ml_delete(curbuf->b_ml.ml_line_count, FALSE); | |
2388 #ifdef FEAT_NETBEANS_INTG | |
2389 netbeansFireChanges = 1; | |
2390 #endif | |
2391 --linecnt; | |
2392 } | |
2393 linecnt = curbuf->b_ml.ml_line_count - linecnt; | |
2394 if (filesize == 0) | |
2395 linecnt = 0; | |
2396 if (newfile || read_buffer) | |
1201 | 2397 { |
7 | 2398 redraw_curbuf_later(NOT_VALID); |
1201 | 2399 #ifdef FEAT_DIFF |
2400 /* After reading the text into the buffer the diff info needs to | |
2401 * be updated. */ | |
2402 diff_invalidate(curbuf); | |
2403 #endif | |
2404 #ifdef FEAT_FOLDING | |
2405 /* All folds in the window are invalid now. Mark them for update | |
2406 * before triggering autocommands. */ | |
2407 foldUpdateAll(curwin); | |
2408 #endif | |
2409 } | |
7 | 2410 else if (linecnt) /* appended at least one line */ |
2411 appended_lines_mark(from, linecnt); | |
2412 | |
2413 #ifndef ALWAYS_USE_GUI | |
2414 /* | |
2415 * If we were reading from the same terminal as where messages go, | |
2416 * the screen will have been messed up. | |
2417 * Switch on raw mode now and clear the screen. | |
2418 */ | |
2419 if (read_stdin) | |
2420 { | |
2421 settmode(TMODE_RAW); /* set to raw mode */ | |
2422 starttermcap(); | |
2423 screenclear(); | |
2424 } | |
2425 #endif | |
2426 | |
2427 if (got_int) | |
2428 { | |
2429 if (!(flags & READ_DUMMY)) | |
2430 { | |
2431 filemess(curbuf, sfname, (char_u *)_(e_interr), 0); | |
2432 if (newfile) | |
2433 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
2434 } | |
2435 msg_scroll = msg_save; | |
2436 #ifdef FEAT_VIMINFO | |
2437 check_marks_read(); | |
2438 #endif | |
2439 return OK; /* an interrupt isn't really an error */ | |
2440 } | |
2441 | |
2442 if (!filtering && !(flags & READ_DUMMY)) | |
2443 { | |
2444 msg_add_fname(curbuf, sfname); /* fname in IObuff with quotes */ | |
2445 c = FALSE; | |
2446 | |
2447 #ifdef UNIX | |
2448 # ifdef S_ISFIFO | |
2449 if (S_ISFIFO(perm)) /* fifo or socket */ | |
2450 { | |
2451 STRCAT(IObuff, _("[fifo/socket]")); | |
2452 c = TRUE; | |
2453 } | |
2454 # else | |
2455 # ifdef S_IFIFO | |
2456 if ((perm & S_IFMT) == S_IFIFO) /* fifo */ | |
2457 { | |
2458 STRCAT(IObuff, _("[fifo]")); | |
2459 c = TRUE; | |
2460 } | |
2461 # endif | |
2462 # ifdef S_IFSOCK | |
2463 if ((perm & S_IFMT) == S_IFSOCK) /* or socket */ | |
2464 { | |
2465 STRCAT(IObuff, _("[socket]")); | |
2466 c = TRUE; | |
2467 } | |
2468 # endif | |
2469 # endif | |
1313 | 2470 # ifdef OPEN_CHR_FILES |
2471 if (S_ISCHR(perm)) /* or character special */ | |
2472 { | |
2473 STRCAT(IObuff, _("[character special]")); | |
2474 c = TRUE; | |
2475 } | |
2476 # endif | |
7 | 2477 #endif |
2478 if (curbuf->b_p_ro) | |
2479 { | |
2480 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")); | |
2481 c = TRUE; | |
2482 } | |
2483 if (read_no_eol_lnum) | |
2484 { | |
2485 msg_add_eol(); | |
2486 c = TRUE; | |
2487 } | |
2488 if (ff_error == EOL_DOS) | |
2489 { | |
2490 STRCAT(IObuff, _("[CR missing]")); | |
2491 c = TRUE; | |
2492 } | |
2493 if (split) | |
2494 { | |
2495 STRCAT(IObuff, _("[long lines split]")); | |
2496 c = TRUE; | |
2497 } | |
2498 #ifdef FEAT_MBYTE | |
2499 if (notconverted) | |
2500 { | |
2501 STRCAT(IObuff, _("[NOT converted]")); | |
2502 c = TRUE; | |
2503 } | |
2504 else if (converted) | |
2505 { | |
2506 STRCAT(IObuff, _("[converted]")); | |
2507 c = TRUE; | |
2508 } | |
2509 #endif | |
2510 #ifdef FEAT_CRYPT | |
2511 if (cryptkey != NULL) | |
2512 { | |
6122 | 2513 crypt_append_msg(curbuf); |
7 | 2514 c = TRUE; |
2515 } | |
2516 #endif | |
2517 #ifdef FEAT_MBYTE | |
595 | 2518 if (conv_error != 0) |
2519 { | |
2520 sprintf((char *)IObuff + STRLEN(IObuff), | |
2521 _("[CONVERSION ERROR in line %ld]"), (long)conv_error); | |
7 | 2522 c = TRUE; |
2523 } | |
2524 else if (illegal_byte > 0) | |
2525 { | |
2526 sprintf((char *)IObuff + STRLEN(IObuff), | |
2527 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte); | |
2528 c = TRUE; | |
2529 } | |
2530 else | |
2531 #endif | |
2532 if (error) | |
2533 { | |
2534 STRCAT(IObuff, _("[READ ERRORS]")); | |
2535 c = TRUE; | |
2536 } | |
2537 if (msg_add_fileformat(fileformat)) | |
2538 c = TRUE; | |
2539 #ifdef FEAT_CRYPT | |
2540 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
|
2541 msg_add_lines(c, (long)linecnt, filesize |
6122 | 2542 - crypt_get_header_len(crypt_get_method_nr(curbuf))); |
7 | 2543 else |
2544 #endif | |
2545 msg_add_lines(c, (long)linecnt, filesize); | |
2546 | |
2547 vim_free(keep_msg); | |
2548 keep_msg = NULL; | |
2549 msg_scrolled_ign = TRUE; | |
2550 #ifdef ALWAYS_USE_GUI | |
2551 /* Don't show the message when reading stdin, it would end up in a | |
2552 * message box (which might be shown when exiting!) */ | |
2553 if (read_stdin || read_buffer) | |
2554 p = msg_may_trunc(FALSE, IObuff); | |
2555 else | |
2556 #endif | |
2557 p = msg_trunc_attr(IObuff, FALSE, 0); | |
2558 if (read_stdin || read_buffer || restart_edit != 0 | |
540 | 2559 || (msg_scrolled != 0 && !need_wait_return)) |
7 | 2560 /* Need to repeat the message after redrawing when: |
2561 * - When reading from stdin (the screen will be cleared next). | |
2562 * - When restart_edit is set (otherwise there will be a delay | |
2563 * before redrawing). | |
2564 * - When the screen was scrolled but there is no wait-return | |
2565 * prompt. */ | |
679 | 2566 set_keep_msg(p, 0); |
7 | 2567 msg_scrolled_ign = FALSE; |
2568 } | |
2569 | |
2570 /* with errors writing the file requires ":w!" */ | |
2571 if (newfile && (error | |
2572 #ifdef FEAT_MBYTE | |
595 | 2573 || conv_error != 0 |
679 | 2574 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP) |
7 | 2575 #endif |
2576 )) | |
2577 curbuf->b_p_ro = TRUE; | |
2578 | |
2579 u_clearline(); /* cannot use "U" command after adding lines */ | |
2580 | |
2581 /* | |
2582 * In Ex mode: cursor at last new line. | |
2583 * Otherwise: cursor at first new line. | |
2584 */ | |
2585 if (exmode_active) | |
2586 curwin->w_cursor.lnum = from + linecnt; | |
2587 else | |
2588 curwin->w_cursor.lnum = from + 1; | |
2589 check_cursor_lnum(); | |
2590 beginline(BL_WHITE | BL_FIX); /* on first non-blank */ | |
2591 | |
2592 /* | |
2593 * Set '[ and '] marks to the newly read lines. | |
2594 */ | |
2595 curbuf->b_op_start.lnum = from + 1; | |
2596 curbuf->b_op_start.col = 0; | |
2597 curbuf->b_op_end.lnum = from + linecnt; | |
2598 curbuf->b_op_end.col = 0; | |
696 | 2599 |
2600 #ifdef WIN32 | |
2601 /* | |
2602 * Work around a weird problem: When a file has two links (only | |
2603 * possible on NTFS) and we write through one link, then stat() it | |
1651 | 2604 * through the other link, the timestamp information may be wrong. |
696 | 2605 * It's correct again after reading the file, thus reset the timestamp |
2606 * here. | |
2607 */ | |
2608 if (newfile && !read_stdin && !read_buffer | |
2609 && mch_stat((char *)fname, &st) >= 0) | |
2610 { | |
2611 buf_store_time(curbuf, &st, fname); | |
2612 curbuf->b_mtime_read = curbuf->b_mtime; | |
2613 } | |
2614 #endif | |
7 | 2615 } |
2616 msg_scroll = msg_save; | |
2617 | |
2618 #ifdef FEAT_VIMINFO | |
2619 /* | |
2620 * Get the marks before executing autocommands, so they can be used there. | |
2621 */ | |
2622 check_marks_read(); | |
2623 #endif | |
2624 | |
2625 /* | |
2626 * Trick: We remember if the last line of the read didn't have | |
2707 | 2627 * an eol even when 'binary' is off, for when writing it again with |
2628 * 'binary' on. This is required for | |
7 | 2629 * ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work. |
2630 */ | |
2707 | 2631 curbuf->b_no_eol_lnum = read_no_eol_lnum; |
7 | 2632 |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2633 /* 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
|
2634 * different. */ |
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2635 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
|
2636 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
|
2637 |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2638 #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
|
2639 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2640 * 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
|
2641 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2642 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
|
2643 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2644 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
|
2645 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2646 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
|
2647 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
|
2648 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2649 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2650 |
167 | 2651 #ifdef FEAT_AUTOCMD |
7 | 2652 if (!read_stdin && !read_buffer) |
2653 { | |
2654 int m = msg_scroll; | |
2655 int n = msg_scrolled; | |
2656 | |
2657 /* Save the fileformat now, otherwise the buffer will be considered | |
2658 * modified if the format/encoding was automatically detected. */ | |
819 | 2659 if (set_options) |
7 | 2660 save_file_ff(curbuf); |
2661 | |
2662 /* | |
2663 * The output from the autocommands should not overwrite anything and | |
2664 * should not be overwritten: Set msg_scroll, restore its value if no | |
2665 * output was done. | |
2666 */ | |
2667 msg_scroll = TRUE; | |
2668 if (filtering) | |
2669 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname, | |
2670 FALSE, curbuf, eap); | |
2671 else if (newfile) | |
2672 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname, | |
2673 FALSE, curbuf, eap); | |
2674 else | |
2675 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname, | |
2676 FALSE, NULL, eap); | |
2677 if (msg_scrolled == n) | |
2678 msg_scroll = m; | |
2707 | 2679 # ifdef FEAT_EVAL |
7 | 2680 if (aborting()) /* autocmds may abort script processing */ |
2681 return FAIL; | |
2707 | 2682 # endif |
2683 } | |
2684 #endif | |
2685 | |
7 | 2686 if (recoverymode && error) |
2687 return FAIL; | |
2688 return OK; | |
2689 } | |
2690 | |
1313 | 2691 #ifdef OPEN_CHR_FILES |
2692 /* | |
2693 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+", | |
2694 * which is the name of files used for process substitution output by | |
2695 * some shells on some operating systems, e.g., bash on SunOS. | |
2696 * Do not accept "/dev/fd/[012]", opening these may hang Vim. | |
2697 */ | |
2698 static int | |
2699 is_dev_fd_file(fname) | |
2700 char_u *fname; | |
2701 { | |
2702 return (STRNCMP(fname, "/dev/fd/", 8) == 0 | |
2703 && VIM_ISDIGIT(fname[8]) | |
2704 && *skipdigits(fname + 9) == NUL | |
2705 && (fname[9] != NUL | |
2706 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2'))); | |
2707 } | |
2708 #endif | |
2709 | |
595 | 2710 #ifdef FEAT_MBYTE |
2711 | |
2712 /* | |
2713 * From the current line count and characters read after that, estimate the | |
2714 * line number where we are now. | |
2715 * Used for error messages that include a line number. | |
2716 */ | |
2717 static linenr_T | |
2718 readfile_linenr(linecnt, p, endp) | |
2719 linenr_T linecnt; /* line count before reading more bytes */ | |
2720 char_u *p; /* start of more bytes read */ | |
2721 char_u *endp; /* end of more bytes read */ | |
2722 { | |
2723 char_u *s; | |
2724 linenr_T lnum; | |
2725 | |
2726 lnum = curbuf->b_ml.ml_line_count - linecnt + 1; | |
2727 for (s = p; s < endp; ++s) | |
2728 if (*s == '\n') | |
2729 ++lnum; | |
2730 return lnum; | |
2731 } | |
2732 #endif | |
2733 | |
7 | 2734 /* |
612 | 2735 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be |
2736 * equal to the buffer "buf". Used for calling readfile(). | |
7 | 2737 * Returns OK or FAIL. |
2738 */ | |
2739 int | |
2740 prep_exarg(eap, buf) | |
2741 exarg_T *eap; | |
2742 buf_T *buf; | |
2743 { | |
2744 eap->cmd = alloc((unsigned)(STRLEN(buf->b_p_ff) | |
2745 #ifdef FEAT_MBYTE | |
2746 + STRLEN(buf->b_p_fenc) | |
2747 #endif | |
2748 + 15)); | |
2749 if (eap->cmd == NULL) | |
2750 return FAIL; | |
2751 | |
2752 #ifdef FEAT_MBYTE | |
2753 sprintf((char *)eap->cmd, "e ++ff=%s ++enc=%s", buf->b_p_ff, buf->b_p_fenc); | |
2754 eap->force_enc = 14 + (int)STRLEN(buf->b_p_ff); | |
612 | 2755 eap->bad_char = buf->b_bad_char; |
7 | 2756 #else |
2757 sprintf((char *)eap->cmd, "e ++ff=%s", buf->b_p_ff); | |
2758 #endif | |
2759 eap->force_ff = 7; | |
612 | 2760 |
2761 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN; | |
819 | 2762 eap->read_edit = FALSE; |
612 | 2763 eap->forceit = FALSE; |
7 | 2764 return OK; |
2765 } | |
2766 | |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2767 /* |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2768 * Set default or forced 'fileformat' and 'binary'. |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2769 */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2770 void |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2771 set_file_options(set_options, eap) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2772 int set_options; |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2773 exarg_T *eap; |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2774 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2775 /* set default 'fileformat' */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2776 if (set_options) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2777 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2778 if (eap != NULL && eap->force_ff != 0) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2779 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2780 else if (*p_ffs != NUL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2781 set_fileformat(default_fileformat(), OPT_LOCAL); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2782 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2783 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2784 /* set or reset 'binary' */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2785 if (eap != NULL && eap->force_bin != 0) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2786 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2787 int oldval = curbuf->b_p_bin; |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2788 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2789 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2790 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2791 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2792 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2793 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2794 #if defined(FEAT_MBYTE) || defined(PROTO) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2795 /* |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2796 * Set forced 'fileencoding'. |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2797 */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2798 void |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2799 set_forced_fenc(eap) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2800 exarg_T *eap; |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2801 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2802 if (eap->force_enc != 0) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2803 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2804 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2805 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2806 if (fenc != NULL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2807 set_string_option_direct((char_u *)"fenc", -1, |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2808 fenc, OPT_FREE|OPT_LOCAL, 0); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2809 vim_free(fenc); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2810 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2811 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2812 |
7 | 2813 /* |
2814 * Find next fileencoding to use from 'fileencodings'. | |
2815 * "pp" points to fenc_next. It's advanced to the next item. | |
2816 * When there are no more items, an empty string is returned and *pp is set to | |
2817 * NULL. | |
2818 * When *pp is not set to NULL, the result is in allocated memory. | |
2819 */ | |
2820 static char_u * | |
2821 next_fenc(pp) | |
2822 char_u **pp; | |
2823 { | |
2824 char_u *p; | |
2825 char_u *r; | |
2826 | |
2827 if (**pp == NUL) | |
2828 { | |
2829 *pp = NULL; | |
2830 return (char_u *)""; | |
2831 } | |
2832 p = vim_strchr(*pp, ','); | |
2833 if (p == NULL) | |
2834 { | |
2835 r = enc_canonize(*pp); | |
2836 *pp += STRLEN(*pp); | |
2837 } | |
2838 else | |
2839 { | |
2840 r = vim_strnsave(*pp, (int)(p - *pp)); | |
2841 *pp = p + 1; | |
2842 if (r != NULL) | |
2843 { | |
2844 p = enc_canonize(r); | |
2845 vim_free(r); | |
2846 r = p; | |
2847 } | |
2848 } | |
2849 if (r == NULL) /* out of memory */ | |
2850 { | |
2851 r = (char_u *)""; | |
2852 *pp = NULL; | |
2853 } | |
2854 return r; | |
2855 } | |
2856 | |
2857 # ifdef FEAT_EVAL | |
2858 /* | |
2859 * Convert a file with the 'charconvert' expression. | |
2860 * This closes the file which is to be read, converts it and opens the | |
2861 * resulting file for reading. | |
2862 * Returns name of the resulting converted file (the caller should delete it | |
2863 * after reading it). | |
2864 * Returns NULL if the conversion failed ("*fdp" is not set) . | |
2865 */ | |
2866 static char_u * | |
2867 readfile_charconvert(fname, fenc, fdp) | |
2868 char_u *fname; /* name of input file */ | |
2869 char_u *fenc; /* converted from */ | |
2870 int *fdp; /* in/out: file descriptor of file */ | |
2871 { | |
2872 char_u *tmpname; | |
2873 char_u *errmsg = NULL; | |
2874 | |
2875 tmpname = vim_tempname('r'); | |
2876 if (tmpname == NULL) | |
2877 errmsg = (char_u *)_("Can't find temp file for conversion"); | |
2878 else | |
2879 { | |
2880 close(*fdp); /* close the input file, ignore errors */ | |
2881 *fdp = -1; | |
2882 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc, | |
2883 fname, tmpname) == FAIL) | |
2884 errmsg = (char_u *)_("Conversion with 'charconvert' failed"); | |
2885 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname, | |
2886 O_RDONLY | O_EXTRA, 0)) < 0) | |
2887 errmsg = (char_u *)_("can't read output of 'charconvert'"); | |
2888 } | |
2889 | |
2890 if (errmsg != NULL) | |
2891 { | |
2892 /* Don't use emsg(), it breaks mappings, the retry with | |
2893 * another type of conversion might still work. */ | |
2894 MSG(errmsg); | |
2895 if (tmpname != NULL) | |
2896 { | |
2897 mch_remove(tmpname); /* delete converted file */ | |
2898 vim_free(tmpname); | |
2899 tmpname = NULL; | |
2900 } | |
2901 } | |
2902 | |
2903 /* If the input file is closed, open it (caller should check for error). */ | |
2904 if (*fdp < 0) | |
2905 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
2906 | |
2907 return tmpname; | |
2908 } | |
2909 # endif | |
2910 | |
2911 #endif | |
2912 | |
2913 #ifdef FEAT_VIMINFO | |
2914 /* | |
2915 * Read marks for the current buffer from the viminfo file, when we support | |
2916 * buffer marks and the buffer has a name. | |
2917 */ | |
2918 static void | |
2919 check_marks_read() | |
2920 { | |
2921 if (!curbuf->b_marks_read && get_viminfo_parameter('\'') > 0 | |
2922 && curbuf->b_ffname != NULL) | |
1733 | 2923 read_viminfo(NULL, VIF_WANT_MARKS); |
7 | 2924 |
2925 /* Always set b_marks_read; needed when 'viminfo' is changed to include | |
2926 * the ' parameter after opening a buffer. */ | |
2927 curbuf->b_marks_read = TRUE; | |
2928 } | |
2929 #endif | |
2930 | |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2931 #if defined(FEAT_CRYPT) || defined(PROTO) |
7 | 2932 /* |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2933 * Check for magic number used for encryption. Applies to the current buffer. |
7 | 2934 * If found, the magic number is removed from ptr[*sizep] and *sizep and |
2935 * *filesizep are updated. | |
2936 * Return the (new) encryption key, NULL for no encryption. | |
2937 */ | |
2938 static char_u * | |
2267 | 2939 check_for_cryptkey(cryptkey, ptr, sizep, filesizep, newfile, fname, did_ask) |
7 | 2940 char_u *cryptkey; /* previous encryption key or NULL */ |
2941 char_u *ptr; /* pointer to read bytes */ | |
2942 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
|
2943 off_t *filesizep; /* nr of bytes used from file */ |
7 | 2944 int newfile; /* editing a new buffer */ |
2267 | 2945 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
|
2946 int *did_ask; /* flag: whether already asked for key */ |
7 | 2947 { |
6122 | 2948 int method = crypt_method_nr_from_magic((char *)ptr, *sizep); |
5312 | 2949 int b_p_ro = curbuf->b_p_ro; |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2950 |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2951 if (method >= 0) |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2952 { |
5312 | 2953 /* Mark the buffer as read-only until the decryption has taken place. |
2954 * Avoids accidentally overwriting the file with garbage. */ | |
2955 curbuf->b_p_ro = TRUE; | |
2956 | |
6130 | 2957 /* Set the cryptmethod local to the buffer. */ |
6122 | 2958 crypt_set_cm_option(curbuf, method); |
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
|
2959 if (cryptkey == NULL && !*did_ask) |
7 | 2960 { |
2961 if (*curbuf->b_p_key) | |
2962 cryptkey = curbuf->b_p_key; | |
2963 else | |
2964 { | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2965 /* 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
|
2966 * 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
|
2967 * 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
|
2968 * Happens when retrying to detect encoding. */ |
2267 | 2969 smsg((char_u *)_(need_key_msg), fname); |
2970 msg_scroll = TRUE; | |
6353 | 2971 crypt_check_method(method); |
6122 | 2972 cryptkey = crypt_get_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
|
2973 *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
|
2974 |
7 | 2975 /* check if empty key entered */ |
2976 if (cryptkey != NULL && *cryptkey == NUL) | |
2977 { | |
2978 if (cryptkey != curbuf->b_p_key) | |
2979 vim_free(cryptkey); | |
2980 cryptkey = NULL; | |
2981 } | |
2982 } | |
2983 } | |
2984 | |
2985 if (cryptkey != NULL) | |
2986 { | |
6122 | 2987 int header_len; |
2988 | |
2989 curbuf->b_cryptstate = crypt_create_from_header( | |
2990 method, cryptkey, ptr); | |
2991 crypt_set_cm_option(curbuf, method); | |
2992 | |
2993 /* Remove cryptmethod specific header from the text. */ | |
2994 header_len = crypt_get_header_len(method); | |
2995 *filesizep += header_len; | |
2996 *sizep -= header_len; | |
2997 mch_memmove(ptr, ptr + header_len, (size_t)*sizep); | |
2998 | |
5312 | 2999 /* Restore the read-only flag. */ |
3000 curbuf->b_p_ro = b_p_ro; | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3001 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3002 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3003 /* 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
|
3004 * 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
|
3005 else if (newfile && *curbuf->b_p_key != NUL && !starting) |
7 | 3006 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL); |
3007 | |
3008 return cryptkey; | |
3009 } | |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3010 #endif /* FEAT_CRYPT */ |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3011 |
7 | 3012 #ifdef UNIX |
3013 static void | |
3014 set_file_time(fname, atime, mtime) | |
3015 char_u *fname; | |
3016 time_t atime; /* access time */ | |
3017 time_t mtime; /* modification time */ | |
3018 { | |
3019 # if defined(HAVE_UTIME) && defined(HAVE_UTIME_H) | |
3020 struct utimbuf buf; | |
3021 | |
3022 buf.actime = atime; | |
3023 buf.modtime = mtime; | |
3024 (void)utime((char *)fname, &buf); | |
3025 # else | |
3026 # if defined(HAVE_UTIMES) | |
3027 struct timeval tvp[2]; | |
3028 | |
3029 tvp[0].tv_sec = atime; | |
3030 tvp[0].tv_usec = 0; | |
3031 tvp[1].tv_sec = mtime; | |
3032 tvp[1].tv_usec = 0; | |
3033 # ifdef NeXT | |
3034 (void)utimes((char *)fname, tvp); | |
3035 # else | |
3036 (void)utimes((char *)fname, (const struct timeval *)&tvp); | |
3037 # endif | |
3038 # endif | |
3039 # endif | |
3040 } | |
3041 #endif /* UNIX */ | |
3042 | |
22 | 3043 #if defined(VMS) && !defined(MIN) |
3044 /* Older DECC compiler for VAX doesn't define MIN() */ | |
3045 # define MIN(a, b) ((a) < (b) ? (a) : (b)) | |
3046 #endif | |
3047 | |
7 | 3048 /* |
1303 | 3049 * Return TRUE if a file appears to be read-only from the file permissions. |
3050 */ | |
3051 int | |
3052 check_file_readonly(fname, perm) | |
3053 char_u *fname; /* full path to file */ | |
3054 int perm; /* known permissions on file */ | |
3055 { | |
3056 #ifndef USE_MCH_ACCESS | |
3057 int fd = 0; | |
3058 #endif | |
3059 | |
3060 return ( | |
3061 #ifdef USE_MCH_ACCESS | |
3062 # ifdef UNIX | |
3063 (perm & 0222) == 0 || | |
3064 # endif | |
3065 mch_access((char *)fname, W_OK) | |
3066 #else | |
3067 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0 | |
3068 ? TRUE : (close(fd), FALSE) | |
3069 #endif | |
3070 ); | |
3071 } | |
3072 | |
3073 | |
3074 /* | |
589 | 3075 * buf_write() - write to file "fname" lines "start" through "end" |
7 | 3076 * |
3077 * We do our own buffering here because fwrite() is so slow. | |
3078 * | |
589 | 3079 * If "forceit" is true, we don't care for errors when attempting backups. |
3080 * In case of an error everything possible is done to restore the original | |
1698 | 3081 * file. But when "forceit" is TRUE, we risk losing it. |
589 | 3082 * |
3083 * When "reset_changed" is TRUE and "append" == FALSE and "start" == 1 and | |
3084 * "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed. | |
7 | 3085 * |
3086 * This function must NOT use NameBuff (because it's called by autowrite()). | |
3087 * | |
3088 * return FAIL for failure, OK otherwise | |
3089 */ | |
3090 int | |
3091 buf_write(buf, fname, sfname, start, end, eap, append, forceit, | |
3092 reset_changed, filtering) | |
3093 buf_T *buf; | |
3094 char_u *fname; | |
3095 char_u *sfname; | |
3096 linenr_T start, end; | |
3097 exarg_T *eap; /* for forced 'ff' and 'fenc', can be | |
3098 NULL! */ | |
589 | 3099 int append; /* append to the file */ |
7 | 3100 int forceit; |
3101 int reset_changed; | |
3102 int filtering; | |
3103 { | |
3104 int fd; | |
3105 char_u *backup = NULL; | |
3106 int backup_copy = FALSE; /* copy the original file? */ | |
3107 int dobackup; | |
3108 char_u *ffname; | |
3109 char_u *wfname = NULL; /* name of file to write to */ | |
3110 char_u *s; | |
3111 char_u *ptr; | |
3112 char_u c; | |
3113 int len; | |
3114 linenr_T lnum; | |
3115 long nchars; | |
3116 char_u *errmsg = NULL; | |
1947 | 3117 int errmsg_allocated = FALSE; |
7 | 3118 char_u *errnum = NULL; |
3119 char_u *buffer; | |
3120 char_u smallbuf[SMBUFSIZE]; | |
3121 char_u *backup_ext; | |
3122 int bufsize; | |
3123 long perm; /* file permissions */ | |
3124 int retval = OK; | |
3125 int newfile = FALSE; /* TRUE if file doesn't exist yet */ | |
3126 int msg_save = msg_scroll; | |
3127 int overwriting; /* TRUE if writing over original */ | |
3128 int no_eol = FALSE; /* no end-of-line written */ | |
3129 int device = FALSE; /* writing to a device */ | |
3130 struct stat st_old; | |
3131 int prev_got_int = got_int; | |
3132 int file_readonly = FALSE; /* overwritten file is read-only */ | |
3133 static char *err_readonly = "is read-only (cannot override: \"W\" in 'cpoptions')"; | |
3134 #if defined(UNIX) || defined(__EMX__XX) /*XXX fix me sometime? */ | |
3135 int made_writable = FALSE; /* 'w' bit has been set */ | |
3136 #endif | |
3137 /* writing everything */ | |
3138 int whole = (start == 1 && end == buf->b_ml.ml_line_count); | |
3139 #ifdef FEAT_AUTOCMD | |
3140 linenr_T old_line_count = buf->b_ml.ml_line_count; | |
3141 #endif | |
3142 int attr; | |
3143 int fileformat; | |
3144 int write_bin; | |
3145 struct bw_info write_info; /* info for buf_write_bytes() */ | |
3146 #ifdef FEAT_MBYTE | |
3147 int converted = FALSE; | |
3148 int notconverted = FALSE; | |
3149 char_u *fenc; /* effective 'fileencoding' */ | |
3150 char_u *fenc_tofree = NULL; /* allocated "fenc" */ | |
3151 #endif | |
3152 #ifdef HAS_BW_FLAGS | |
3153 int wb_flags = 0; | |
3154 #endif | |
3155 #ifdef HAVE_ACL | |
3156 vim_acl_T acl = NULL; /* ACL copied from original file to | |
3157 backup or new file */ | |
3158 #endif | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3159 #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
|
3160 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
|
3161 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
|
3162 #endif |
6243 | 3163 unsigned int bkc = get_bkc_value(buf); |
7 | 3164 |
3165 if (fname == NULL || *fname == NUL) /* safety check */ | |
3166 return FAIL; | |
2028 | 3167 if (buf->b_ml.ml_mfp == NULL) |
3168 { | |
3169 /* This can happen during startup when there is a stray "w" in the | |
3170 * vimrc file. */ | |
3171 EMSG(_(e_emptybuf)); | |
3172 return FAIL; | |
3173 } | |
7 | 3174 |
3175 /* | |
3176 * Disallow writing from .exrc and .vimrc in current directory for | |
3177 * security reasons. | |
3178 */ | |
3179 if (check_secure()) | |
3180 return FAIL; | |
3181 | |
3182 /* Avoid a crash for a long name. */ | |
3183 if (STRLEN(fname) >= MAXPATHL) | |
3184 { | |
3185 EMSG(_(e_longname)); | |
3186 return FAIL; | |
3187 } | |
3188 | |
3189 #ifdef FEAT_MBYTE | |
3190 /* must init bw_conv_buf and bw_iconv_fd before jumping to "fail" */ | |
3191 write_info.bw_conv_buf = NULL; | |
3192 write_info.bw_conv_error = FALSE; | |
1947 | 3193 write_info.bw_conv_error_lnum = 0; |
7 | 3194 write_info.bw_restlen = 0; |
3195 # ifdef USE_ICONV | |
3196 write_info.bw_iconv_fd = (iconv_t)-1; | |
3197 # endif | |
3198 #endif | |
6122 | 3199 #ifdef FEAT_CRYPT |
3200 write_info.bw_buffer = buf; | |
3201 #endif | |
7 | 3202 |
167 | 3203 /* After writing a file changedtick changes but we don't want to display |
3204 * the line. */ | |
3205 ex_no_reprint = TRUE; | |
3206 | |
7 | 3207 /* |
3208 * If there is no file name yet, use the one for the written file. | |
3209 * BF_NOTEDITED is set to reflect this (in case the write fails). | |
3210 * Don't do this when the write is for a filter command. | |
589 | 3211 * Don't do this when appending. |
3212 * Only do this when 'cpoptions' contains the 'F' flag. | |
7 | 3213 */ |
633 | 3214 if (buf->b_ffname == NULL |
3215 && reset_changed | |
7 | 3216 && whole |
3217 && buf == curbuf | |
236 | 3218 #ifdef FEAT_QUICKFIX |
3219 && !bt_nofile(buf) | |
3220 #endif | |
7 | 3221 && !filtering |
589 | 3222 && (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL) |
7 | 3223 && vim_strchr(p_cpo, CPO_FNAMEW) != NULL) |
3224 { | |
633 | 3225 if (set_rw_fname(fname, sfname) == FAIL) |
7 | 3226 return FAIL; |
633 | 3227 buf = curbuf; /* just in case autocmds made "buf" invalid */ |
7 | 3228 } |
3229 | |
3230 if (sfname == NULL) | |
3231 sfname = fname; | |
3232 /* | |
3233 * For Unix: Use the short file name whenever possible. | |
3234 * Avoids problems with networks and when directory names are changed. | |
3235 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to | |
3236 * another directory, which we don't detect | |
3237 */ | |
3238 ffname = fname; /* remember full fname */ | |
3239 #ifdef UNIX | |
3240 fname = sfname; | |
3241 #endif | |
3242 | |
3243 if (buf->b_ffname != NULL && fnamecmp(ffname, buf->b_ffname) == 0) | |
3244 overwriting = TRUE; | |
3245 else | |
3246 overwriting = FALSE; | |
3247 | |
3248 if (exiting) | |
4352 | 3249 settmode(TMODE_COOK); /* when exiting allow typeahead now */ |
7 | 3250 |
3251 ++no_wait_return; /* don't wait for return yet */ | |
3252 | |
3253 /* | |
3254 * Set '[ and '] marks to the lines to be written. | |
3255 */ | |
3256 buf->b_op_start.lnum = start; | |
3257 buf->b_op_start.col = 0; | |
3258 buf->b_op_end.lnum = end; | |
3259 buf->b_op_end.col = 0; | |
3260 | |
3261 #ifdef FEAT_AUTOCMD | |
3262 { | |
3263 aco_save_T aco; | |
3264 int buf_ffname = FALSE; | |
3265 int buf_sfname = FALSE; | |
3266 int buf_fname_f = FALSE; | |
3267 int buf_fname_s = FALSE; | |
3268 int did_cmd = FALSE; | |
17 | 3269 int nofile_err = FALSE; |
161 | 3270 int empty_memline = (buf->b_ml.ml_mfp == NULL); |
7 | 3271 |
3272 /* | |
4352 | 3273 * Apply PRE autocommands. |
7 | 3274 * Set curbuf to the buffer to be written. |
3275 * Careful: The autocommands may call buf_write() recursively! | |
3276 */ | |
3277 if (ffname == buf->b_ffname) | |
3278 buf_ffname = TRUE; | |
3279 if (sfname == buf->b_sfname) | |
3280 buf_sfname = TRUE; | |
3281 if (fname == buf->b_ffname) | |
3282 buf_fname_f = TRUE; | |
3283 if (fname == buf->b_sfname) | |
3284 buf_fname_s = TRUE; | |
3285 | |
3286 /* set curwin/curbuf to buf and save a few things */ | |
3287 aucmd_prepbuf(&aco, buf); | |
3288 | |
3289 if (append) | |
3290 { | |
3291 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD, | |
3292 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3293 { |
635 | 3294 #ifdef FEAT_QUICKFIX |
18 | 3295 if (overwriting && bt_nofile(curbuf)) |
17 | 3296 nofile_err = TRUE; |
3297 else | |
635 | 3298 #endif |
17 | 3299 apply_autocmds_exarg(EVENT_FILEAPPENDPRE, |
7 | 3300 sfname, sfname, FALSE, curbuf, eap); |
17 | 3301 } |
7 | 3302 } |
3303 else if (filtering) | |
3304 { | |
3305 apply_autocmds_exarg(EVENT_FILTERWRITEPRE, | |
3306 NULL, sfname, FALSE, curbuf, eap); | |
3307 } | |
3308 else if (reset_changed && whole) | |
3309 { | |
3036 | 3310 int was_changed = curbufIsChanged(); |
3311 | |
3312 did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD, | |
3313 sfname, sfname, FALSE, curbuf, eap); | |
3314 if (did_cmd) | |
3315 { | |
3316 if (was_changed && !curbufIsChanged()) | |
3317 { | |
3318 /* Written everything correctly and BufWriteCmd has reset | |
3319 * 'modified': Correct the undo information so that an | |
3320 * undo now sets 'modified'. */ | |
3321 u_unchanged(curbuf); | |
3322 u_update_save_nr(curbuf); | |
3323 } | |
3324 } | |
3325 else | |
17 | 3326 { |
635 | 3327 #ifdef FEAT_QUICKFIX |
179 | 3328 if (overwriting && bt_nofile(curbuf)) |
17 | 3329 nofile_err = TRUE; |
3330 else | |
635 | 3331 #endif |
17 | 3332 apply_autocmds_exarg(EVENT_BUFWRITEPRE, |
7 | 3333 sfname, sfname, FALSE, curbuf, eap); |
17 | 3334 } |
7 | 3335 } |
3336 else | |
3337 { | |
3338 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD, | |
3339 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3340 { |
635 | 3341 #ifdef FEAT_QUICKFIX |
179 | 3342 if (overwriting && bt_nofile(curbuf)) |
17 | 3343 nofile_err = TRUE; |
3344 else | |
635 | 3345 #endif |
17 | 3346 apply_autocmds_exarg(EVENT_FILEWRITEPRE, |
7 | 3347 sfname, sfname, FALSE, curbuf, eap); |
17 | 3348 } |
7 | 3349 } |
3350 | |
3351 /* restore curwin/curbuf and a few other things */ | |
3352 aucmd_restbuf(&aco); | |
3353 | |
3354 /* | |
3355 * In three situations we return here and don't write the file: | |
3356 * 1. the autocommands deleted or unloaded the buffer. | |
3357 * 2. The autocommands abort script processing. | |
3358 * 3. If one of the "Cmd" autocommands was executed. | |
3359 */ | |
3360 if (!buf_valid(buf)) | |
3361 buf = NULL; | |
161 | 3362 if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline) |
532 | 3363 || did_cmd || nofile_err |
3364 #ifdef FEAT_EVAL | |
3365 || aborting() | |
3366 #endif | |
3367 ) | |
7 | 3368 { |
3369 --no_wait_return; | |
3370 msg_scroll = msg_save; | |
17 | 3371 if (nofile_err) |
3372 EMSG(_("E676: No matching autocommands for acwrite buffer")); | |
3373 | |
532 | 3374 if (nofile_err |
3375 #ifdef FEAT_EVAL | |
3376 || aborting() | |
3377 #endif | |
3378 ) | |
7 | 3379 /* An aborting error, interrupt or exception in the |
3380 * autocommands. */ | |
3381 return FAIL; | |
3382 if (did_cmd) | |
3383 { | |
3384 if (buf == NULL) | |
3385 /* The buffer was deleted. We assume it was written | |
3386 * (can't retry anyway). */ | |
3387 return OK; | |
3388 if (overwriting) | |
3389 { | |
3390 /* Assume the buffer was written, update the timestamp. */ | |
3391 ml_timestamp(buf); | |
589 | 3392 if (append) |
3393 buf->b_flags &= ~BF_NEW; | |
3394 else | |
3395 buf->b_flags &= ~BF_WRITE_MASK; | |
7 | 3396 } |
589 | 3397 if (reset_changed && buf->b_changed && !append |
39 | 3398 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)) |
7 | 3399 /* Buffer still changed, the autocommands didn't work |
3400 * properly. */ | |
3401 return FAIL; | |
3402 return OK; | |
3403 } | |
3404 #ifdef FEAT_EVAL | |
3405 if (!aborting()) | |
3406 #endif | |
3407 EMSG(_("E203: Autocommands deleted or unloaded buffer to be written")); | |
3408 return FAIL; | |
3409 } | |
3410 | |
3411 /* | |
3412 * The autocommands may have changed the number of lines in the file. | |
3413 * When writing the whole file, adjust the end. | |
3414 * When writing part of the file, assume that the autocommands only | |
3415 * changed the number of lines that are to be written (tricky!). | |
3416 */ | |
3417 if (buf->b_ml.ml_line_count != old_line_count) | |
3418 { | |
3419 if (whole) /* write all */ | |
3420 end = buf->b_ml.ml_line_count; | |
3421 else if (buf->b_ml.ml_line_count > old_line_count) /* more lines */ | |
3422 end += buf->b_ml.ml_line_count - old_line_count; | |
3423 else /* less lines */ | |
3424 { | |
3425 end -= old_line_count - buf->b_ml.ml_line_count; | |
3426 if (end < start) | |
3427 { | |
3428 --no_wait_return; | |
3429 msg_scroll = msg_save; | |
3430 EMSG(_("E204: Autocommand changed number of lines in unexpected way")); | |
3431 return FAIL; | |
3432 } | |
3433 } | |
3434 } | |
3435 | |
3436 /* | |
3437 * The autocommands may have changed the name of the buffer, which may | |
3438 * be kept in fname, ffname and sfname. | |
3439 */ | |
3440 if (buf_ffname) | |
3441 ffname = buf->b_ffname; | |
3442 if (buf_sfname) | |
3443 sfname = buf->b_sfname; | |
3444 if (buf_fname_f) | |
3445 fname = buf->b_ffname; | |
3446 if (buf_fname_s) | |
3447 fname = buf->b_sfname; | |
3448 } | |
3449 #endif | |
3450 | |
3451 #ifdef FEAT_NETBEANS_INTG | |
2210 | 3452 if (netbeans_active() && isNetbeansBuffer(buf)) |
7 | 3453 { |
3454 if (whole) | |
3455 { | |
3456 /* | |
3457 * b_changed can be 0 after an undo, but we still need to write | |
3458 * the buffer to NetBeans. | |
3459 */ | |
3460 if (buf->b_changed || isNetbeansModified(buf)) | |
3461 { | |
33 | 3462 --no_wait_return; /* may wait for return now */ |
3463 msg_scroll = msg_save; | |
3464 netbeans_save_buffer(buf); /* no error checking... */ | |
7 | 3465 return retval; |
3466 } | |
3467 else | |
3468 { | |
3469 errnum = (char_u *)"E656: "; | |
1653 | 3470 errmsg = (char_u *)_("NetBeans disallows writes of unmodified buffers"); |
7 | 3471 buffer = NULL; |
3472 goto fail; | |
3473 } | |
3474 } | |
3475 else | |
3476 { | |
3477 errnum = (char_u *)"E657: "; | |
3478 errmsg = (char_u *)_("Partial writes disallowed for NetBeans buffers"); | |
3479 buffer = NULL; | |
3480 goto fail; | |
3481 } | |
3482 } | |
3483 #endif | |
3484 | |
3485 if (shortmess(SHM_OVER) && !exiting) | |
3486 msg_scroll = FALSE; /* overwrite previous file message */ | |
3487 else | |
3488 msg_scroll = TRUE; /* don't overwrite previous file message */ | |
3489 if (!filtering) | |
3490 filemess(buf, | |
3491 #ifndef UNIX | |
3492 sfname, | |
3493 #else | |
3494 fname, | |
3495 #endif | |
3496 (char_u *)"", 0); /* show that we are busy */ | |
3497 msg_scroll = FALSE; /* always overwrite the file message now */ | |
3498 | |
3499 buffer = alloc(BUFSIZE); | |
3500 if (buffer == NULL) /* can't allocate big buffer, use small | |
3501 * one (to be able to write when out of | |
3502 * memory) */ | |
3503 { | |
3504 buffer = smallbuf; | |
3505 bufsize = SMBUFSIZE; | |
3506 } | |
3507 else | |
3508 bufsize = BUFSIZE; | |
3509 | |
3510 /* | |
3511 * Get information about original file (if there is one). | |
3512 */ | |
3513 #if defined(UNIX) && !defined(ARCHIE) | |
1438 | 3514 st_old.st_dev = 0; |
3515 st_old.st_ino = 0; | |
7 | 3516 perm = -1; |
3517 if (mch_stat((char *)fname, &st_old) < 0) | |
3518 newfile = TRUE; | |
3519 else | |
3520 { | |
3521 perm = st_old.st_mode; | |
3522 if (!S_ISREG(st_old.st_mode)) /* not a file */ | |
3523 { | |
3524 if (S_ISDIR(st_old.st_mode)) | |
3525 { | |
3526 errnum = (char_u *)"E502: "; | |
3527 errmsg = (char_u *)_("is a directory"); | |
3528 goto fail; | |
3529 } | |
3530 if (mch_nodetype(fname) != NODE_WRITABLE) | |
3531 { | |
3532 errnum = (char_u *)"E503: "; | |
3533 errmsg = (char_u *)_("is not a file or writable device"); | |
3534 goto fail; | |
3535 } | |
3536 /* It's a device of some kind (or a fifo) which we can write to | |
3537 * but for which we can't make a backup. */ | |
3538 device = TRUE; | |
3539 newfile = TRUE; | |
3540 perm = -1; | |
3541 } | |
3542 } | |
3543 #else /* !UNIX */ | |
3544 /* | |
3545 * Check for a writable device name. | |
3546 */ | |
3547 c = mch_nodetype(fname); | |
3548 if (c == NODE_OTHER) | |
3549 { | |
3550 errnum = (char_u *)"E503: "; | |
3551 errmsg = (char_u *)_("is not a file or writable device"); | |
3552 goto fail; | |
3553 } | |
3554 if (c == NODE_WRITABLE) | |
3555 { | |
1004 | 3556 # if defined(MSDOS) || defined(MSWIN) || defined(OS2) |
3557 /* MS-Windows allows opening a device, but we will probably get stuck | |
3558 * trying to write to it. */ | |
3559 if (!p_odev) | |
3560 { | |
3561 errnum = (char_u *)"E796: "; | |
3562 errmsg = (char_u *)_("writing to device disabled with 'opendevice' option"); | |
3563 goto fail; | |
3564 } | |
3565 # endif | |
7 | 3566 device = TRUE; |
3567 newfile = TRUE; | |
3568 perm = -1; | |
3569 } | |
3570 else | |
3571 { | |
3572 perm = mch_getperm(fname); | |
3573 if (perm < 0) | |
3574 newfile = TRUE; | |
3575 else if (mch_isdir(fname)) | |
3576 { | |
3577 errnum = (char_u *)"E502: "; | |
3578 errmsg = (char_u *)_("is a directory"); | |
3579 goto fail; | |
3580 } | |
3581 if (overwriting) | |
3582 (void)mch_stat((char *)fname, &st_old); | |
3583 } | |
3584 #endif /* !UNIX */ | |
3585 | |
3586 if (!device && !newfile) | |
3587 { | |
3588 /* | |
3589 * Check if the file is really writable (when renaming the file to | |
3590 * make a backup we won't discover it later). | |
3591 */ | |
1303 | 3592 file_readonly = check_file_readonly(fname, (int)perm); |
3593 | |
7 | 3594 if (!forceit && file_readonly) |
3595 { | |
3596 if (vim_strchr(p_cpo, CPO_FWRITE) != NULL) | |
3597 { | |
3598 errnum = (char_u *)"E504: "; | |
3599 errmsg = (char_u *)_(err_readonly); | |
3600 } | |
3601 else | |
3602 { | |
3603 errnum = (char_u *)"E505: "; | |
3604 errmsg = (char_u *)_("is read-only (add ! to override)"); | |
3605 } | |
3606 goto fail; | |
3607 } | |
3608 | |
3609 /* | |
3610 * Check if the timestamp hasn't changed since reading the file. | |
3611 */ | |
3612 if (overwriting) | |
3613 { | |
3614 retval = check_mtime(buf, &st_old); | |
3615 if (retval == FAIL) | |
3616 goto fail; | |
3617 } | |
3618 } | |
3619 | |
3620 #ifdef HAVE_ACL | |
3621 /* | |
3622 * For systems that support ACL: get the ACL from the original file. | |
3623 */ | |
3624 if (!newfile) | |
3625 acl = mch_get_acl(fname); | |
3626 #endif | |
3627 | |
3628 /* | |
3629 * If 'backupskip' is not empty, don't make a backup for some files. | |
3630 */ | |
3631 dobackup = (p_wb || p_bk || *p_pm != NUL); | |
3632 #ifdef FEAT_WILDIGN | |
3633 if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname)) | |
3634 dobackup = FALSE; | |
3635 #endif | |
3636 | |
3637 /* | |
3638 * Save the value of got_int and reset it. We don't want a previous | |
3639 * interruption cancel writing, only hitting CTRL-C while writing should | |
3640 * abort it. | |
3641 */ | |
3642 prev_got_int = got_int; | |
3643 got_int = FALSE; | |
3644 | |
3645 /* Mark the buffer as 'being saved' to prevent changed buffer warnings */ | |
3646 buf->b_saving = TRUE; | |
3647 | |
3648 /* | |
3649 * If we are not appending or filtering, the file exists, and the | |
3650 * 'writebackup', 'backup' or 'patchmode' option is set, need a backup. | |
3651 * When 'patchmode' is set also make a backup when appending. | |
3652 * | |
3653 * Do not make any backup, if 'writebackup' and 'backup' are both switched | |
3654 * off. This helps when editing large files on almost-full disks. | |
3655 */ | |
3656 if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup) | |
3657 { | |
3658 #if defined(UNIX) || defined(WIN32) | |
3659 struct stat st; | |
3660 #endif | |
3661 | |
6243 | 3662 if ((bkc & BKC_YES) || append) /* "yes" */ |
7 | 3663 backup_copy = TRUE; |
3664 #if defined(UNIX) || defined(WIN32) | |
6243 | 3665 else if ((bkc & BKC_AUTO)) /* "auto" */ |
7 | 3666 { |
3667 int i; | |
3668 | |
3669 # ifdef UNIX | |
3670 /* | |
3671 * Don't rename the file when: | |
3672 * - it's a hard link | |
3673 * - it's a symbolic link | |
3674 * - we don't have write permission in the directory | |
3675 * - we can't set the owner/group of the new file | |
3676 */ | |
3677 if (st_old.st_nlink > 1 | |
3678 || mch_lstat((char *)fname, &st) < 0 | |
3679 || st.st_dev != st_old.st_dev | |
557 | 3680 || st.st_ino != st_old.st_ino |
3681 # ifndef HAVE_FCHOWN | |
3682 || st.st_uid != st_old.st_uid | |
3683 || st.st_gid != st_old.st_gid | |
3684 # endif | |
3685 ) | |
7 | 3686 backup_copy = TRUE; |
3687 else | |
696 | 3688 # else |
3689 # ifdef WIN32 | |
3690 /* On NTFS file systems hard links are possible. */ | |
3691 if (mch_is_linked(fname)) | |
3692 backup_copy = TRUE; | |
3693 else | |
3694 # endif | |
7 | 3695 # endif |
3696 { | |
3697 /* | |
3698 * Check if we can create a file and set the owner/group to | |
3699 * the ones from the original file. | |
3700 * First find a file name that doesn't exist yet (use some | |
3701 * arbitrary numbers). | |
3702 */ | |
3703 STRCPY(IObuff, fname); | |
3704 for (i = 4913; ; i += 123) | |
3705 { | |
3706 sprintf((char *)gettail(IObuff), "%d", i); | |
557 | 3707 if (mch_lstat((char *)IObuff, &st) < 0) |
7 | 3708 break; |
3709 } | |
557 | 3710 fd = mch_open((char *)IObuff, |
3711 O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm); | |
7 | 3712 if (fd < 0) /* can't write in directory */ |
3713 backup_copy = TRUE; | |
3714 else | |
3715 { | |
3716 # ifdef UNIX | |
557 | 3717 # ifdef HAVE_FCHOWN |
1757 | 3718 ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
557 | 3719 # endif |
7 | 3720 if (mch_stat((char *)IObuff, &st) < 0 |
3721 || st.st_uid != st_old.st_uid | |
3722 || st.st_gid != st_old.st_gid | |
1877 | 3723 || (long)st.st_mode != perm) |
7 | 3724 backup_copy = TRUE; |
3725 # endif | |
567 | 3726 /* Close the file before removing it, on MS-Windows we |
3727 * can't delete an open file. */ | |
3728 close(fd); | |
7 | 3729 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
|
3730 # 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
|
3731 /* 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
|
3732 * 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
|
3733 * second. */ |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3734 { |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3735 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
|
3736 |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3737 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
|
3738 { |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3739 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
|
3740 break; |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3741 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
|
3742 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
|
3743 } |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3744 } |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3745 # endif |
7 | 3746 } |
3747 } | |
3748 } | |
3749 | |
3750 /* | |
3751 * Break symlinks and/or hardlinks if we've been asked to. | |
3752 */ | |
6243 | 3753 if ((bkc & BKC_BREAKSYMLINK) || (bkc & BKC_BREAKHARDLINK)) |
7 | 3754 { |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3755 # ifdef UNIX |
7 | 3756 int lstat_res; |
3757 | |
3758 lstat_res = mch_lstat((char *)fname, &st); | |
3759 | |
3760 /* Symlinks. */ | |
6243 | 3761 if ((bkc & BKC_BREAKSYMLINK) |
7 | 3762 && lstat_res == 0 |
3763 && st.st_ino != st_old.st_ino) | |
3764 backup_copy = FALSE; | |
3765 | |
3766 /* Hardlinks. */ | |
6243 | 3767 if ((bkc & BKC_BREAKHARDLINK) |
7 | 3768 && st_old.st_nlink > 1 |
3769 && (lstat_res != 0 || st.st_ino == st_old.st_ino)) | |
3770 backup_copy = FALSE; | |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3771 # else |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3772 # if defined(WIN32) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3773 /* Symlinks. */ |
6243 | 3774 if ((bkc & BKC_BREAKSYMLINK) && mch_is_symbolic_link(fname)) |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3775 backup_copy = FALSE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3776 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3777 /* Hardlinks. */ |
6243 | 3778 if ((bkc & BKC_BREAKHARDLINK) && mch_is_hard_link(fname)) |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3779 backup_copy = FALSE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3780 # endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3781 # endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3782 } |
7 | 3783 |
3784 #endif | |
3785 | |
3786 /* make sure we have a valid backup extension to use */ | |
3787 if (*p_bex == NUL) | |
3788 backup_ext = (char_u *)".bak"; | |
3789 else | |
3790 backup_ext = p_bex; | |
3791 | |
3792 if (backup_copy | |
3793 && (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) >= 0) | |
3794 { | |
3795 int bfd; | |
3796 char_u *copybuf, *wp; | |
3797 int some_error = FALSE; | |
3798 struct stat st_new; | |
3799 char_u *dirp; | |
3800 char_u *rootname; | |
344 | 3801 #if defined(UNIX) && !defined(SHORT_FNAME) |
7 | 3802 int did_set_shortname; |
3803 #endif | |
3804 | |
3805 copybuf = alloc(BUFSIZE + 1); | |
3806 if (copybuf == NULL) | |
3807 { | |
3808 some_error = TRUE; /* out of memory */ | |
3809 goto nobackup; | |
3810 } | |
3811 | |
3812 /* | |
3813 * Try to make the backup in each directory in the 'bdir' option. | |
3814 * | |
3815 * Unix semantics has it, that we may have a writable file, | |
3816 * that cannot be recreated with a simple open(..., O_CREAT, ) e.g: | |
3817 * - the directory is not writable, | |
3818 * - the file may be a symbolic link, | |
3819 * - the file may belong to another user/group, etc. | |
3820 * | |
3821 * For these reasons, the existing writable file must be truncated | |
3822 * and reused. Creation of a backup COPY will be attempted. | |
3823 */ | |
3824 dirp = p_bdir; | |
3825 while (*dirp) | |
3826 { | |
3827 #ifdef UNIX | |
3828 st_new.st_ino = 0; | |
3829 st_new.st_dev = 0; | |
3830 st_new.st_gid = 0; | |
3831 #endif | |
3832 | |
3833 /* | |
3834 * Isolate one directory name, using an entry in 'bdir'. | |
3835 */ | |
3836 (void)copy_option_part(&dirp, copybuf, BUFSIZE, ","); | |
3837 rootname = get_file_in_dir(fname, copybuf); | |
3838 if (rootname == NULL) | |
3839 { | |
3840 some_error = TRUE; /* out of memory */ | |
3841 goto nobackup; | |
3842 } | |
3843 | |
344 | 3844 #if defined(UNIX) && !defined(SHORT_FNAME) |
7 | 3845 did_set_shortname = FALSE; |
3846 #endif | |
3847 | |
3848 /* | |
3849 * May try twice if 'shortname' not set. | |
3850 */ | |
3851 for (;;) | |
3852 { | |
3853 /* | |
3854 * Make backup file name. | |
3855 */ | |
3856 backup = buf_modname( | |
3857 #ifdef SHORT_FNAME | |
3858 TRUE, | |
3859 #else | |
3860 (buf->b_p_sn || buf->b_shortname), | |
3861 #endif | |
3862 rootname, backup_ext, FALSE); | |
3863 if (backup == NULL) | |
3864 { | |
3865 vim_free(rootname); | |
3866 some_error = TRUE; /* out of memory */ | |
3867 goto nobackup; | |
3868 } | |
3869 | |
3870 /* | |
3871 * Check if backup file already exists. | |
3872 */ | |
3873 if (mch_stat((char *)backup, &st_new) >= 0) | |
3874 { | |
3875 #ifdef UNIX | |
3876 /* | |
3877 * Check if backup file is same as original file. | |
3878 * May happen when modname() gave the same file back. | |
3879 * E.g. silly link, or file name-length reached. | |
3880 * If we don't check here, we either ruin the file | |
3881 * when copying or erase it after writing. jw. | |
3882 */ | |
3883 if (st_new.st_dev == st_old.st_dev | |
3884 && st_new.st_ino == st_old.st_ino) | |
3885 { | |
3886 vim_free(backup); | |
3887 backup = NULL; /* no backup file to delete */ | |
3888 # ifndef SHORT_FNAME | |
3889 /* | |
3890 * may try again with 'shortname' set | |
3891 */ | |
3892 if (!(buf->b_shortname || buf->b_p_sn)) | |
3893 { | |
3894 buf->b_shortname = TRUE; | |
3895 did_set_shortname = TRUE; | |
3896 continue; | |
3897 } | |
3898 /* setting shortname didn't help */ | |
3899 if (did_set_shortname) | |
3900 buf->b_shortname = FALSE; | |
3901 # endif | |
3902 break; | |
3903 } | |
3904 #endif | |
3905 | |
3906 /* | |
3907 * If we are not going to keep the backup file, don't | |
3908 * delete an existing one, try to use another name. | |
3909 * Change one character, just before the extension. | |
3910 */ | |
3911 if (!p_bk) | |
3912 { | |
3913 wp = backup + STRLEN(backup) - 1 | |
3914 - STRLEN(backup_ext); | |
3915 if (wp < backup) /* empty file name ??? */ | |
3916 wp = backup; | |
3917 *wp = 'z'; | |
3918 while (*wp > 'a' | |
3919 && mch_stat((char *)backup, &st_new) >= 0) | |
3920 --*wp; | |
3921 /* They all exist??? Must be something wrong. */ | |
3922 if (*wp == 'a') | |
3923 { | |
3924 vim_free(backup); | |
3925 backup = NULL; | |
3926 } | |
3927 } | |
3928 } | |
3929 break; | |
3930 } | |
3931 vim_free(rootname); | |
3932 | |
3933 /* | |
3934 * Try to create the backup file | |
3935 */ | |
3936 if (backup != NULL) | |
3937 { | |
3938 /* remove old backup, if present */ | |
3939 mch_remove(backup); | |
3940 /* Open with O_EXCL to avoid the file being created while | |
3941 * we were sleeping (symlink hacker attack?) */ | |
3942 bfd = mch_open((char *)backup, | |
557 | 3943 O_WRONLY|O_CREAT|O_EXTRA|O_EXCL|O_NOFOLLOW, |
3944 perm & 0777); | |
7 | 3945 if (bfd < 0) |
3946 { | |
3947 vim_free(backup); | |
3948 backup = NULL; | |
3949 } | |
3950 else | |
3951 { | |
3952 /* set file protection same as original file, but | |
3953 * strip s-bit */ | |
3954 (void)mch_setperm(backup, perm & 0777); | |
3955 | |
3956 #ifdef UNIX | |
3957 /* | |
3958 * Try to set the group of the backup same as the | |
3959 * original file. If this fails, set the protection | |
3960 * bits for the group same as the protection bits for | |
3961 * others. | |
3962 */ | |
557 | 3963 if (st_new.st_gid != st_old.st_gid |
7 | 3964 # ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */ |
557 | 3965 && fchown(bfd, (uid_t)-1, st_old.st_gid) != 0 |
7 | 3966 # endif |
3967 ) | |
3968 mch_setperm(backup, | |
3969 (perm & 0707) | ((perm & 07) << 3)); | |
5788 | 3970 # if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
1583 | 3971 mch_copy_sec(fname, backup); |
3972 # endif | |
7 | 3973 #endif |
3974 | |
3975 /* | |
3976 * copy the file. | |
3977 */ | |
3978 write_info.bw_fd = bfd; | |
3979 write_info.bw_buf = copybuf; | |
3980 #ifdef HAS_BW_FLAGS | |
3981 write_info.bw_flags = FIO_NOCONVERT; | |
3982 #endif | |
2664 | 3983 while ((write_info.bw_len = read_eintr(fd, copybuf, |
7 | 3984 BUFSIZE)) > 0) |
3985 { | |
3986 if (buf_write_bytes(&write_info) == FAIL) | |
3987 { | |
3988 errmsg = (char_u *)_("E506: Can't write to backup file (add ! to override)"); | |
3989 break; | |
3990 } | |
3991 ui_breakcheck(); | |
3992 if (got_int) | |
3993 { | |
3994 errmsg = (char_u *)_(e_interr); | |
3995 break; | |
3996 } | |
3997 } | |
3998 | |
3999 if (close(bfd) < 0 && errmsg == NULL) | |
4000 errmsg = (char_u *)_("E507: Close error for backup file (add ! to override)"); | |
4001 if (write_info.bw_len < 0) | |
4002 errmsg = (char_u *)_("E508: Can't read file for backup (add ! to override)"); | |
4003 #ifdef UNIX | |
4004 set_file_time(backup, st_old.st_atime, st_old.st_mtime); | |
4005 #endif | |
4006 #ifdef HAVE_ACL | |
4007 mch_set_acl(backup, acl); | |
4008 #endif | |
5788 | 4009 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
1583 | 4010 mch_copy_sec(fname, backup); |
4011 #endif | |
7 | 4012 break; |
4013 } | |
4014 } | |
4015 } | |
4016 nobackup: | |
4017 close(fd); /* ignore errors for closing read file */ | |
4018 vim_free(copybuf); | |
4019 | |
4020 if (backup == NULL && errmsg == NULL) | |
4021 errmsg = (char_u *)_("E509: Cannot create backup file (add ! to override)"); | |
4022 /* ignore errors when forceit is TRUE */ | |
4023 if ((some_error || errmsg != NULL) && !forceit) | |
4024 { | |
4025 retval = FAIL; | |
4026 goto fail; | |
4027 } | |
4028 errmsg = NULL; | |
4029 } | |
4030 else | |
4031 { | |
4032 char_u *dirp; | |
4033 char_u *p; | |
4034 char_u *rootname; | |
4035 | |
4036 /* | |
4037 * Make a backup by renaming the original file. | |
4038 */ | |
4039 /* | |
4040 * If 'cpoptions' includes the "W" flag, we don't want to | |
4041 * overwrite a read-only file. But rename may be possible | |
4042 * anyway, thus we need an extra check here. | |
4043 */ | |
4044 if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL) | |
4045 { | |
4046 errnum = (char_u *)"E504: "; | |
4047 errmsg = (char_u *)_(err_readonly); | |
4048 goto fail; | |
4049 } | |
4050 | |
4051 /* | |
4052 * | |
4053 * Form the backup file name - change path/fo.o.h to | |
4054 * path/fo.o.h.bak Try all directories in 'backupdir', first one | |
4055 * that works is used. | |
4056 */ | |
4057 dirp = p_bdir; | |
4058 while (*dirp) | |
4059 { | |
4060 /* | |
4061 * Isolate one directory name and make the backup file name. | |
4062 */ | |
4063 (void)copy_option_part(&dirp, IObuff, IOSIZE, ","); | |
4064 rootname = get_file_in_dir(fname, IObuff); | |
4065 if (rootname == NULL) | |
4066 backup = NULL; | |
4067 else | |
4068 { | |
4069 backup = buf_modname( | |
4070 #ifdef SHORT_FNAME | |
4071 TRUE, | |
4072 #else | |
4073 (buf->b_p_sn || buf->b_shortname), | |
4074 #endif | |
4075 rootname, backup_ext, FALSE); | |
4076 vim_free(rootname); | |
4077 } | |
4078 | |
4079 if (backup != NULL) | |
4080 { | |
4081 /* | |
4082 * If we are not going to keep the backup file, don't | |
4083 * delete an existing one, try to use another name. | |
4084 * Change one character, just before the extension. | |
4085 */ | |
4086 if (!p_bk && mch_getperm(backup) >= 0) | |
4087 { | |
4088 p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext); | |
4089 if (p < backup) /* empty file name ??? */ | |
4090 p = backup; | |
4091 *p = 'z'; | |
4092 while (*p > 'a' && mch_getperm(backup) >= 0) | |
4093 --*p; | |
4094 /* They all exist??? Must be something wrong! */ | |
4095 if (*p == 'a') | |
4096 { | |
4097 vim_free(backup); | |
4098 backup = NULL; | |
4099 } | |
4100 } | |
4101 } | |
4102 if (backup != NULL) | |
4103 { | |
4104 /* | |
531 | 4105 * Delete any existing backup and move the current version |
4106 * to the backup. For safety, we don't remove the backup | |
4107 * until the write has finished successfully. And if the | |
4108 * 'backup' option is set, leave it around. | |
7 | 4109 */ |
4110 /* | |
4111 * If the renaming of the original file to the backup file | |
4112 * works, quit here. | |
4113 */ | |
4114 if (vim_rename(fname, backup) == 0) | |
4115 break; | |
4116 | |
4117 vim_free(backup); /* don't do the rename below */ | |
4118 backup = NULL; | |
4119 } | |
4120 } | |
4121 if (backup == NULL && !forceit) | |
4122 { | |
4123 errmsg = (char_u *)_("E510: Can't make backup file (add ! to override)"); | |
4124 goto fail; | |
4125 } | |
4126 } | |
4127 } | |
4128 | |
4129 #if defined(UNIX) && !defined(ARCHIE) | |
4130 /* When using ":w!" and the file was read-only: make it writable */ | |
4131 if (forceit && perm >= 0 && !(perm & 0200) && st_old.st_uid == getuid() | |
4132 && vim_strchr(p_cpo, CPO_FWRITE) == NULL) | |
4133 { | |
4134 perm |= 0200; | |
4135 (void)mch_setperm(fname, perm); | |
4136 made_writable = TRUE; | |
4137 } | |
4138 #endif | |
4139 | |
819 | 4140 /* When using ":w!" and writing to the current file, 'readonly' makes no |
164 | 4141 * sense, reset it, unless 'Z' appears in 'cpoptions'. */ |
4142 if (forceit && overwriting && vim_strchr(p_cpo, CPO_KEEPRO) == NULL) | |
7 | 4143 { |
4144 buf->b_p_ro = FALSE; | |
4145 #ifdef FEAT_TITLE | |
4146 need_maketitle = TRUE; /* set window title later */ | |
4147 #endif | |
4148 #ifdef FEAT_WINDOWS | |
4149 status_redraw_all(); /* redraw status lines later */ | |
4150 #endif | |
4151 } | |
4152 | |
4153 if (end > buf->b_ml.ml_line_count) | |
4154 end = buf->b_ml.ml_line_count; | |
4155 if (buf->b_ml.ml_flags & ML_EMPTY) | |
4156 start = end + 1; | |
4157 | |
4158 /* | |
4159 * If the original file is being overwritten, there is a small chance that | |
4160 * we crash in the middle of writing. Therefore the file is preserved now. | |
4161 * This makes all block numbers positive so that recovery does not need | |
4162 * the original file. | |
4163 * Don't do this if there is a backup file and we are exiting. | |
4164 */ | |
39 | 4165 if (reset_changed && !newfile && overwriting |
7 | 4166 && !(exiting && backup != NULL)) |
4167 { | |
4168 ml_preserve(buf, FALSE); | |
4169 if (got_int) | |
4170 { | |
4171 errmsg = (char_u *)_(e_interr); | |
4172 goto restore_backup; | |
4173 } | |
4174 } | |
4175 | |
4176 #ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */ | |
4177 /* | |
4178 * Before risking to lose the original file verify if there's | |
4179 * a resource fork to preserve, and if cannot be done warn | |
4180 * the users. This happens when overwriting without backups. | |
4181 */ | |
4182 if (backup == NULL && overwriting && !append) | |
4183 if (mch_has_resource_fork(fname)) | |
4184 { | |
4185 errmsg = (char_u *)_("E460: The resource fork would be lost (add ! to override)"); | |
4186 goto restore_backup; | |
4187 } | |
4188 #endif | |
4189 | |
4190 #ifdef VMS | |
4191 vms_remove_version(fname); /* remove version */ | |
4192 #endif | |
1651 | 4193 /* Default: write the file directly. May write to a temp file for |
7 | 4194 * multi-byte conversion. */ |
4195 wfname = fname; | |
4196 | |
4197 #ifdef FEAT_MBYTE | |
4198 /* Check for forced 'fileencoding' from "++opt=val" argument. */ | |
4199 if (eap != NULL && eap->force_enc != 0) | |
4200 { | |
4201 fenc = eap->cmd + eap->force_enc; | |
4202 fenc = enc_canonize(fenc); | |
4203 fenc_tofree = fenc; | |
4204 } | |
4205 else | |
4206 fenc = buf->b_p_fenc; | |
4207 | |
4208 /* | |
1948 | 4209 * Check if the file needs to be converted. |
7 | 4210 */ |
1948 | 4211 converted = need_conversion(fenc); |
7 | 4212 |
4213 /* | |
4214 * Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done. Or | |
4215 * Latin1 to Unicode conversion. This is handled in buf_write_bytes(). | |
4216 * Prepare the flags for it and allocate bw_conv_buf when needed. | |
4217 */ | |
4218 if (converted && (enc_utf8 || STRCMP(p_enc, "latin1") == 0)) | |
4219 { | |
4220 wb_flags = get_fio_flags(fenc); | |
4221 if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8)) | |
4222 { | |
4223 /* Need to allocate a buffer to translate into. */ | |
4224 if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8)) | |
4225 write_info.bw_conv_buflen = bufsize * 2; | |
4226 else /* FIO_UCS4 */ | |
4227 write_info.bw_conv_buflen = bufsize * 4; | |
4228 write_info.bw_conv_buf | |
4229 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4230 if (write_info.bw_conv_buf == NULL) | |
4231 end = 0; | |
4232 } | |
4233 } | |
4234 | |
4235 # ifdef WIN3264 | |
4236 if (converted && wb_flags == 0 && (wb_flags = get_win_fio_flags(fenc)) != 0) | |
4237 { | |
4238 /* Convert UTF-8 -> UCS-2 and UCS-2 -> DBCS. Worst-case * 4: */ | |
4239 write_info.bw_conv_buflen = bufsize * 4; | |
4240 write_info.bw_conv_buf | |
4241 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4242 if (write_info.bw_conv_buf == NULL) | |
4243 end = 0; | |
4244 } | |
4245 # endif | |
4246 | |
4247 # ifdef MACOS_X | |
4248 if (converted && wb_flags == 0 && (wb_flags = get_mac_fio_flags(fenc)) != 0) | |
4249 { | |
4250 write_info.bw_conv_buflen = bufsize * 3; | |
4251 write_info.bw_conv_buf | |
4252 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4253 if (write_info.bw_conv_buf == NULL) | |
4254 end = 0; | |
4255 } | |
4256 # endif | |
4257 | |
4258 # if defined(FEAT_EVAL) || defined(USE_ICONV) | |
4259 if (converted && wb_flags == 0) | |
4260 { | |
4261 # ifdef USE_ICONV | |
4262 /* | |
4263 * Use iconv() conversion when conversion is needed and it's not done | |
4264 * internally. | |
4265 */ | |
4266 write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc, | |
4267 enc_utf8 ? (char_u *)"utf-8" : p_enc); | |
4268 if (write_info.bw_iconv_fd != (iconv_t)-1) | |
4269 { | |
4270 /* We're going to use iconv(), allocate a buffer to convert in. */ | |
4271 write_info.bw_conv_buflen = bufsize * ICONV_MULT; | |
4272 write_info.bw_conv_buf | |
4273 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4274 if (write_info.bw_conv_buf == NULL) | |
4275 end = 0; | |
4276 write_info.bw_first = TRUE; | |
4277 } | |
4278 # ifdef FEAT_EVAL | |
4279 else | |
4280 # endif | |
4281 # endif | |
4282 | |
4283 # ifdef FEAT_EVAL | |
4284 /* | |
4285 * When the file needs to be converted with 'charconvert' after | |
4286 * writing, write to a temp file instead and let the conversion | |
4287 * overwrite the original file. | |
4288 */ | |
4289 if (*p_ccv != NUL) | |
4290 { | |
4291 wfname = vim_tempname('w'); | |
4292 if (wfname == NULL) /* Can't write without a tempfile! */ | |
4293 { | |
4294 errmsg = (char_u *)_("E214: Can't find temp file for writing"); | |
4295 goto restore_backup; | |
4296 } | |
4297 } | |
4298 # endif | |
4299 } | |
4300 # endif | |
4301 if (converted && wb_flags == 0 | |
4302 # ifdef USE_ICONV | |
4303 && write_info.bw_iconv_fd == (iconv_t)-1 | |
4304 # endif | |
4305 # ifdef FEAT_EVAL | |
4306 && wfname == fname | |
4307 # endif | |
4308 ) | |
4309 { | |
4310 if (!forceit) | |
4311 { | |
4312 errmsg = (char_u *)_("E213: Cannot convert (add ! to write without conversion)"); | |
4313 goto restore_backup; | |
4314 } | |
4315 notconverted = TRUE; | |
4316 } | |
4317 #endif | |
4318 | |
4319 /* | |
4320 * Open the file "wfname" for writing. | |
4321 * We may try to open the file twice: If we can't write to the | |
4322 * file and forceit is TRUE we delete the existing file and try to create | |
4323 * a new one. If this still fails we may have lost the original file! | |
4324 * (this may happen when the user reached his quotum for number of files). | |
4325 * Appending will fail if the file does not exist and forceit is FALSE. | |
4326 */ | |
4327 while ((fd = mch_open((char *)wfname, O_WRONLY | O_EXTRA | (append | |
4328 ? (forceit ? (O_APPEND | O_CREAT) : O_APPEND) | |
4329 : (O_CREAT | O_TRUNC)) | |
216 | 4330 , perm < 0 ? 0666 : (perm & 0777))) < 0) |
7 | 4331 { |
4332 /* | |
4333 * A forced write will try to create a new file if the old one is | |
4334 * still readonly. This may also happen when the directory is | |
4335 * read-only. In that case the mch_remove() will fail. | |
4336 */ | |
4337 if (errmsg == NULL) | |
4338 { | |
4339 #ifdef UNIX | |
4340 struct stat st; | |
4341 | |
4342 /* Don't delete the file when it's a hard or symbolic link. */ | |
4343 if ((!newfile && st_old.st_nlink > 1) | |
4344 || (mch_lstat((char *)fname, &st) == 0 | |
4345 && (st.st_dev != st_old.st_dev | |
4346 || st.st_ino != st_old.st_ino))) | |
4347 errmsg = (char_u *)_("E166: Can't open linked file for writing"); | |
4348 else | |
4349 #endif | |
4350 { | |
4351 errmsg = (char_u *)_("E212: Can't open file for writing"); | |
4352 if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL | |
4353 && perm >= 0) | |
4354 { | |
4355 #ifdef UNIX | |
4356 /* we write to the file, thus it should be marked | |
4357 writable after all */ | |
4358 if (!(perm & 0200)) | |
4359 made_writable = TRUE; | |
4360 perm |= 0200; | |
4361 if (st_old.st_uid != getuid() || st_old.st_gid != getgid()) | |
4362 perm &= 0777; | |
4363 #endif | |
4364 if (!append) /* don't remove when appending */ | |
4365 mch_remove(wfname); | |
4366 continue; | |
4367 } | |
4368 } | |
4369 } | |
4370 | |
4371 restore_backup: | |
4372 { | |
4373 struct stat st; | |
4374 | |
4375 /* | |
4376 * If we failed to open the file, we don't need a backup. Throw it | |
4377 * away. If we moved or removed the original file try to put the | |
4378 * backup in its place. | |
4379 */ | |
4380 if (backup != NULL && wfname == fname) | |
4381 { | |
4382 if (backup_copy) | |
4383 { | |
4384 /* | |
4385 * There is a small chance that we removed the original, | |
4386 * try to move the copy in its place. | |
4387 * This may not work if the vim_rename() fails. | |
4388 * In that case we leave the copy around. | |
4389 */ | |
4390 /* If file does not exist, put the copy in its place */ | |
4391 if (mch_stat((char *)fname, &st) < 0) | |
4392 vim_rename(backup, fname); | |
4393 /* if original file does exist throw away the copy */ | |
4394 if (mch_stat((char *)fname, &st) >= 0) | |
4395 mch_remove(backup); | |
4396 } | |
4397 else | |
4398 { | |
4399 /* try to put the original file back */ | |
4400 vim_rename(backup, fname); | |
4401 } | |
4402 } | |
4403 | |
4404 /* if original file no longer exists give an extra warning */ | |
4405 if (!newfile && mch_stat((char *)fname, &st) < 0) | |
4406 end = 0; | |
4407 } | |
4408 | |
4409 #ifdef FEAT_MBYTE | |
4410 if (wfname != fname) | |
4411 vim_free(wfname); | |
4412 #endif | |
4413 goto fail; | |
4414 } | |
4415 errmsg = NULL; | |
4416 | |
4417 #if defined(MACOS_CLASSIC) || defined(WIN3264) | |
4418 /* TODO: Is it need for MACOS_X? (Dany) */ | |
4419 /* | |
4420 * On macintosh copy the original files attributes (i.e. the backup) | |
1201 | 4421 * This is done in order to preserve the resource fork and the |
4422 * Finder attribute (label, comments, custom icons, file creator) | |
7 | 4423 */ |
4424 if (backup != NULL && overwriting && !append) | |
4425 { | |
4426 if (backup_copy) | |
4427 (void)mch_copy_file_attribute(wfname, backup); | |
4428 else | |
4429 (void)mch_copy_file_attribute(backup, wfname); | |
4430 } | |
4431 | |
4432 if (!overwriting && !append) | |
4433 { | |
4434 if (buf->b_ffname != NULL) | |
4435 (void)mch_copy_file_attribute(buf->b_ffname, wfname); | |
1201 | 4436 /* Should copy resource fork */ |
7 | 4437 } |
4438 #endif | |
4439 | |
4440 write_info.bw_fd = fd; | |
4441 | |
4442 #ifdef FEAT_CRYPT | |
2267 | 4443 if (*buf->b_p_key != NUL && !filtering) |
7 | 4444 { |
6122 | 4445 char_u *header; |
4446 int header_len; | |
4447 | |
4448 buf->b_cryptstate = crypt_create_for_writing(crypt_get_method_nr(buf), | |
4449 buf->b_p_key, &header, &header_len); | |
4450 if (buf->b_cryptstate == NULL || header == NULL) | |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4451 end = 0; |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
4452 else |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
4453 { |
6122 | 4454 /* Write magic number, so that Vim knows how this file is |
4455 * encrypted when reading it back. */ | |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4456 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
|
4457 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
|
4458 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
|
4459 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
|
4460 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
|
4461 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
|
4462 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
|
4463 } |
7 | 4464 } |
4465 #endif | |
4466 | |
4467 write_info.bw_buf = buffer; | |
4468 nchars = 0; | |
4469 | |
4470 /* use "++bin", "++nobin" or 'binary' */ | |
4471 if (eap != NULL && eap->force_bin != 0) | |
4472 write_bin = (eap->force_bin == FORCE_BIN); | |
4473 else | |
4474 write_bin = buf->b_p_bin; | |
4475 | |
4476 #ifdef FEAT_MBYTE | |
4477 /* | |
4478 * The BOM is written just after the encryption magic number. | |
24 | 4479 * Skip it when appending and the file already existed, the BOM only makes |
4480 * sense at the start of the file. | |
7 | 4481 */ |
24 | 4482 if (buf->b_p_bomb && !write_bin && (!append || perm < 0)) |
7 | 4483 { |
4484 write_info.bw_len = make_bom(buffer, fenc); | |
4485 if (write_info.bw_len > 0) | |
4486 { | |
4487 /* don't convert, do encryption */ | |
4488 write_info.bw_flags = FIO_NOCONVERT | wb_flags; | |
4489 if (buf_write_bytes(&write_info) == FAIL) | |
4490 end = 0; | |
4491 else | |
4492 nchars += write_info.bw_len; | |
4493 } | |
4494 } | |
1947 | 4495 write_info.bw_start_lnum = start; |
7 | 4496 #endif |
4497 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4498 #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
|
4499 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
|
4500 && !filtering && reset_changed); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4501 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
|
4502 /* 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
|
4503 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
|
4504 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4505 |
7 | 4506 write_info.bw_len = bufsize; |
4507 #ifdef HAS_BW_FLAGS | |
4508 write_info.bw_flags = wb_flags; | |
4509 #endif | |
4510 fileformat = get_fileformat_force(buf, eap); | |
4511 s = buffer; | |
4512 len = 0; | |
4513 for (lnum = start; lnum <= end; ++lnum) | |
4514 { | |
4515 /* | |
4516 * The next while loop is done once for each character written. | |
4517 * Keep it fast! | |
4518 */ | |
4519 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
|
4520 #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
|
4521 if (write_undo_file) |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
4522 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
|
4523 #endif |
7 | 4524 while ((c = *++ptr) != NUL) |
4525 { | |
4526 if (c == NL) | |
4527 *s = NUL; /* replace newlines with NULs */ | |
4528 else if (c == CAR && fileformat == EOL_MAC) | |
4529 *s = NL; /* Mac: replace CRs with NLs */ | |
4530 else | |
4531 *s = c; | |
4532 ++s; | |
4533 if (++len != bufsize) | |
4534 continue; | |
4535 if (buf_write_bytes(&write_info) == FAIL) | |
4536 { | |
4537 end = 0; /* write error: break loop */ | |
4538 break; | |
4539 } | |
4540 nchars += bufsize; | |
4541 s = buffer; | |
4542 len = 0; | |
1947 | 4543 #ifdef FEAT_MBYTE |
4544 write_info.bw_start_lnum = lnum; | |
4545 #endif | |
7 | 4546 } |
4547 /* write failed or last line has no EOL: stop here */ | |
4548 if (end == 0 | |
4549 || (lnum == end | |
4550 && write_bin | |
2707 | 4551 && (lnum == buf->b_no_eol_lnum |
7 | 4552 || (lnum == buf->b_ml.ml_line_count && !buf->b_p_eol)))) |
4553 { | |
4554 ++lnum; /* written the line, count it */ | |
4555 no_eol = TRUE; | |
4556 break; | |
4557 } | |
4558 if (fileformat == EOL_UNIX) | |
4559 *s++ = NL; | |
4560 else | |
4561 { | |
4562 *s++ = CAR; /* EOL_MAC or EOL_DOS: write CR */ | |
4563 if (fileformat == EOL_DOS) /* write CR-NL */ | |
4564 { | |
4565 if (++len == bufsize) | |
4566 { | |
4567 if (buf_write_bytes(&write_info) == FAIL) | |
4568 { | |
4569 end = 0; /* write error: break loop */ | |
4570 break; | |
4571 } | |
4572 nchars += bufsize; | |
4573 s = buffer; | |
4574 len = 0; | |
4575 } | |
4576 *s++ = NL; | |
4577 } | |
4578 } | |
4579 if (++len == bufsize && end) | |
4580 { | |
4581 if (buf_write_bytes(&write_info) == FAIL) | |
4582 { | |
4583 end = 0; /* write error: break loop */ | |
4584 break; | |
4585 } | |
4586 nchars += bufsize; | |
4587 s = buffer; | |
4588 len = 0; | |
4589 | |
4590 ui_breakcheck(); | |
4591 if (got_int) | |
4592 { | |
4593 end = 0; /* Interrupted, break loop */ | |
4594 break; | |
4595 } | |
4596 } | |
4597 #ifdef VMS | |
4598 /* | |
4599 * On VMS there is a problem: newlines get added when writing blocks | |
4600 * at a time. Fix it by writing a line at a time. | |
4601 * This is much slower! | |
22 | 4602 * Explanation: VAX/DECC RTL insists that records in some RMS |
4603 * structures end with a newline (carriage return) character, and if | |
4604 * they don't it adds one. | |
4605 * With other RMS structures it works perfect without this fix. | |
7 | 4606 */ |
1431 | 4607 if (buf->b_fab_rfm == FAB$C_VFC |
4608 || ((buf->b_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0)) | |
22 | 4609 { |
4610 int b2write; | |
4611 | |
4612 buf->b_fab_mrs = (buf->b_fab_mrs == 0 | |
4613 ? MIN(4096, bufsize) | |
4614 : MIN(buf->b_fab_mrs, bufsize)); | |
4615 | |
4616 b2write = len; | |
4617 while (b2write > 0) | |
7 | 4618 { |
22 | 4619 write_info.bw_len = MIN(b2write, buf->b_fab_mrs); |
4620 if (buf_write_bytes(&write_info) == FAIL) | |
4621 { | |
4622 end = 0; | |
4623 break; | |
4624 } | |
4625 b2write -= MIN(b2write, buf->b_fab_mrs); | |
7 | 4626 } |
4627 write_info.bw_len = bufsize; | |
4628 nchars += len; | |
4629 s = buffer; | |
4630 len = 0; | |
4631 } | |
4632 #endif | |
4633 } | |
4634 if (len > 0 && end > 0) | |
4635 { | |
4636 write_info.bw_len = len; | |
4637 if (buf_write_bytes(&write_info) == FAIL) | |
4638 end = 0; /* write error */ | |
4639 nchars += len; | |
4640 } | |
4641 | |
4642 #if defined(UNIX) && defined(HAVE_FSYNC) | |
4643 /* On many journalling file systems there is a bug that causes both the | |
4644 * original and the backup file to be lost when halting the system right | |
4645 * after writing the file. That's because only the meta-data is | |
4646 * journalled. Syncing the file slows down the system, but assures it has | |
10 | 4647 * been written to disk and we don't lose it. |
4648 * For a device do try the fsync() but don't complain if it does not work | |
36 | 4649 * (could be a pipe). |
4650 * If the 'fsync' option is FALSE, don't fsync(). Useful for laptops. */ | |
4651 if (p_fs && fsync(fd) != 0 && !device) | |
7 | 4652 { |
4653 errmsg = (char_u *)_("E667: Fsync failed"); | |
4654 end = 0; | |
4655 } | |
4656 #endif | |
4657 | |
5788 | 4658 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
1583 | 4659 /* Probably need to set the security context. */ |
4660 if (!backup_copy) | |
4661 mch_copy_sec(backup, wfname); | |
4662 #endif | |
4663 | |
557 | 4664 #ifdef UNIX |
4665 /* When creating a new file, set its owner/group to that of the original | |
4666 * file. Get the new device and inode number. */ | |
4667 if (backup != NULL && !backup_copy) | |
4668 { | |
4669 # ifdef HAVE_FCHOWN | |
4670 struct stat st; | |
4671 | |
4672 /* don't change the owner when it's already OK, some systems remove | |
4673 * permission or ACL stuff */ | |
4674 if (mch_stat((char *)wfname, &st) < 0 | |
4675 || st.st_uid != st_old.st_uid | |
4676 || st.st_gid != st_old.st_gid) | |
4677 { | |
1757 | 4678 ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
557 | 4679 if (perm >= 0) /* set permission again, may have changed */ |
4680 (void)mch_setperm(wfname, perm); | |
4681 } | |
4682 # endif | |
4683 buf_setino(buf); | |
4684 } | |
1873 | 4685 else if (!buf->b_dev_valid) |
617 | 4686 /* Set the inode when creating a new file. */ |
4687 buf_setino(buf); | |
557 | 4688 #endif |
4689 | |
7 | 4690 if (close(fd) != 0) |
4691 { | |
4692 errmsg = (char_u *)_("E512: Close failed"); | |
4693 end = 0; | |
4694 } | |
4695 | |
4696 #ifdef UNIX | |
4697 if (made_writable) | |
4698 perm &= ~0200; /* reset 'w' bit for security reasons */ | |
4699 #endif | |
4700 if (perm >= 0) /* set perm. of new file same as old file */ | |
4701 (void)mch_setperm(wfname, perm); | |
4702 #ifdef HAVE_ACL | |
4703 /* Probably need to set the ACL before changing the user (can't set the | |
4704 * ACL on a file the user doesn't own). */ | |
4705 if (!backup_copy) | |
4706 mch_set_acl(wfname, acl); | |
4707 #endif | |
2267 | 4708 #ifdef FEAT_CRYPT |
6122 | 4709 if (buf->b_cryptstate != NULL) |
4710 { | |
4711 crypt_free_state(buf->b_cryptstate); | |
4712 buf->b_cryptstate = NULL; | |
4713 } | |
4714 #endif | |
7 | 4715 |
4716 #if defined(FEAT_MBYTE) && defined(FEAT_EVAL) | |
4717 if (wfname != fname) | |
4718 { | |
4719 /* | |
4720 * The file was written to a temp file, now it needs to be converted | |
4721 * with 'charconvert' to (overwrite) the output file. | |
4722 */ | |
4723 if (end != 0) | |
4724 { | |
4725 if (eval_charconvert(enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc, | |
4726 wfname, fname) == FAIL) | |
4727 { | |
4728 write_info.bw_conv_error = TRUE; | |
4729 end = 0; | |
4730 } | |
4731 } | |
4732 mch_remove(wfname); | |
4733 vim_free(wfname); | |
4734 } | |
4735 #endif | |
4736 | |
4737 if (end == 0) | |
4738 { | |
4739 if (errmsg == NULL) | |
4740 { | |
4741 #ifdef FEAT_MBYTE | |
4742 if (write_info.bw_conv_error) | |
1947 | 4743 { |
4744 if (write_info.bw_conv_error_lnum == 0) | |
4745 errmsg = (char_u *)_("E513: write error, conversion failed (make 'fenc' empty to override)"); | |
4746 else | |
4747 { | |
4748 errmsg_allocated = TRUE; | |
4749 errmsg = alloc(300); | |
4750 vim_snprintf((char *)errmsg, 300, _("E513: write error, conversion failed in line %ld (make 'fenc' empty to override)"), | |
4751 (long)write_info.bw_conv_error_lnum); | |
4752 } | |
4753 } | |
7 | 4754 else |
4755 #endif | |
4756 if (got_int) | |
4757 errmsg = (char_u *)_(e_interr); | |
4758 else | |
4759 errmsg = (char_u *)_("E514: write error (file system full?)"); | |
4760 } | |
4761 | |
4762 /* | |
4763 * If we have a backup file, try to put it in place of the new file, | |
1698 | 4764 * because the new file is probably corrupt. This avoids losing the |
7 | 4765 * original file when trying to make a backup when writing the file a |
4766 * second time. | |
4767 * When "backup_copy" is set we need to copy the backup over the new | |
4768 * file. Otherwise rename the backup file. | |
4769 * If this is OK, don't give the extra warning message. | |
4770 */ | |
4771 if (backup != NULL) | |
4772 { | |
4773 if (backup_copy) | |
4774 { | |
4775 /* This may take a while, if we were interrupted let the user | |
4776 * know we got the message. */ | |
4777 if (got_int) | |
4778 { | |
4779 MSG(_(e_interr)); | |
4780 out_flush(); | |
4781 } | |
4782 if ((fd = mch_open((char *)backup, O_RDONLY | O_EXTRA, 0)) >= 0) | |
4783 { | |
4784 if ((write_info.bw_fd = mch_open((char *)fname, | |
194 | 4785 O_WRONLY | O_CREAT | O_TRUNC | O_EXTRA, |
4786 perm & 0777)) >= 0) | |
7 | 4787 { |
4788 /* copy the file. */ | |
4789 write_info.bw_buf = smallbuf; | |
4790 #ifdef HAS_BW_FLAGS | |
4791 write_info.bw_flags = FIO_NOCONVERT; | |
4792 #endif | |
2664 | 4793 while ((write_info.bw_len = read_eintr(fd, smallbuf, |
7 | 4794 SMBUFSIZE)) > 0) |
4795 if (buf_write_bytes(&write_info) == FAIL) | |
4796 break; | |
4797 | |
4798 if (close(write_info.bw_fd) >= 0 | |
4799 && write_info.bw_len == 0) | |
4800 end = 1; /* success */ | |
4801 } | |
4802 close(fd); /* ignore errors for closing read file */ | |
4803 } | |
4804 } | |
4805 else | |
4806 { | |
4807 if (vim_rename(backup, fname) == 0) | |
4808 end = 1; | |
4809 } | |
4810 } | |
4811 goto fail; | |
4812 } | |
4813 | |
4814 lnum -= start; /* compute number of written lines */ | |
4815 --no_wait_return; /* may wait for return now */ | |
4816 | |
4817 #if !(defined(UNIX) || defined(VMS)) | |
4818 fname = sfname; /* use shortname now, for the messages */ | |
4819 #endif | |
4820 if (!filtering) | |
4821 { | |
4822 msg_add_fname(buf, fname); /* put fname in IObuff with quotes */ | |
4823 c = FALSE; | |
4824 #ifdef FEAT_MBYTE | |
4825 if (write_info.bw_conv_error) | |
4826 { | |
4827 STRCAT(IObuff, _(" CONVERSION ERROR")); | |
4828 c = TRUE; | |
1947 | 4829 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
|
4830 vim_snprintf_add((char *)IObuff, IOSIZE, _(" in line %ld;"), |
1947 | 4831 (long)write_info.bw_conv_error_lnum); |
7 | 4832 } |
4833 else if (notconverted) | |
4834 { | |
4835 STRCAT(IObuff, _("[NOT converted]")); | |
4836 c = TRUE; | |
4837 } | |
4838 else if (converted) | |
4839 { | |
4840 STRCAT(IObuff, _("[converted]")); | |
4841 c = TRUE; | |
4842 } | |
4843 #endif | |
4844 if (device) | |
4845 { | |
4846 STRCAT(IObuff, _("[Device]")); | |
4847 c = TRUE; | |
4848 } | |
4849 else if (newfile) | |
4850 { | |
4851 STRCAT(IObuff, shortmess(SHM_NEW) ? _("[New]") : _("[New File]")); | |
4852 c = TRUE; | |
4853 } | |
4854 if (no_eol) | |
4855 { | |
4856 msg_add_eol(); | |
4857 c = TRUE; | |
4858 } | |
4859 /* may add [unix/dos/mac] */ | |
4860 if (msg_add_fileformat(fileformat)) | |
4861 c = TRUE; | |
4862 #ifdef FEAT_CRYPT | |
4863 if (wb_flags & FIO_ENCRYPTED) | |
4864 { | |
6122 | 4865 crypt_append_msg(buf); |
7 | 4866 c = TRUE; |
4867 } | |
4868 #endif | |
4869 msg_add_lines(c, (long)lnum, nchars); /* add line/char count */ | |
4870 if (!shortmess(SHM_WRITE)) | |
4871 { | |
4872 if (append) | |
4873 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [a]") : _(" appended")); | |
4874 else | |
4875 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [w]") : _(" written")); | |
4876 } | |
4877 | |
679 | 4878 set_keep_msg(msg_trunc_attr(IObuff, FALSE, 0), 0); |
7 | 4879 } |
4880 | |
39 | 4881 /* When written everything correctly: reset 'modified'. Unless not |
4882 * writing to the original file and '+' is not in 'cpoptions'. */ | |
589 | 4883 if (reset_changed && whole && !append |
7 | 4884 #ifdef FEAT_MBYTE |
4885 && !write_info.bw_conv_error | |
4886 #endif | |
39 | 4887 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL) |
4888 ) | |
7 | 4889 { |
4890 unchanged(buf, TRUE); | |
6320 | 4891 #ifdef FEAT_AUTOCMD |
4892 /* buf->b_changedtick is always incremented in unchanged() but that | |
4893 * should not trigger a TextChanged event. */ | |
4894 if (last_changedtick + 1 == buf->b_changedtick | |
4895 && last_changedtick_buf == buf) | |
4896 last_changedtick = buf->b_changedtick; | |
4897 #endif | |
7 | 4898 u_unchanged(buf); |
2281
e41433ea71df
Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents:
2280
diff
changeset
|
4899 u_update_save_nr(buf); |
7 | 4900 } |
4901 | |
4902 /* | |
4903 * If written to the current file, update the timestamp of the swap file | |
4904 * and reset the BF_WRITE_MASK flags. Also sets buf->b_mtime. | |
4905 */ | |
4906 if (overwriting) | |
4907 { | |
4908 ml_timestamp(buf); | |
589 | 4909 if (append) |
4910 buf->b_flags &= ~BF_NEW; | |
4911 else | |
4912 buf->b_flags &= ~BF_WRITE_MASK; | |
7 | 4913 } |
4914 | |
4915 /* | |
4916 * If we kept a backup until now, and we are in patch mode, then we make | |
4917 * the backup file our 'original' file. | |
4918 */ | |
4919 if (*p_pm && dobackup) | |
4920 { | |
4921 char *org = (char *)buf_modname( | |
4922 #ifdef SHORT_FNAME | |
4923 TRUE, | |
4924 #else | |
4925 (buf->b_p_sn || buf->b_shortname), | |
4926 #endif | |
4927 fname, p_pm, FALSE); | |
4928 | |
4929 if (backup != NULL) | |
4930 { | |
4931 struct stat st; | |
4932 | |
4933 /* | |
4934 * If the original file does not exist yet | |
4935 * the current backup file becomes the original file | |
4936 */ | |
4937 if (org == NULL) | |
4938 EMSG(_("E205: Patchmode: can't save original file")); | |
4939 else if (mch_stat(org, &st) < 0) | |
4940 { | |
4941 vim_rename(backup, (char_u *)org); | |
4942 vim_free(backup); /* don't delete the file */ | |
4943 backup = NULL; | |
4944 #ifdef UNIX | |
4945 set_file_time((char_u *)org, st_old.st_atime, st_old.st_mtime); | |
4946 #endif | |
4947 } | |
4948 } | |
4949 /* | |
4950 * If there is no backup file, remember that a (new) file was | |
4951 * created. | |
4952 */ | |
4953 else | |
4954 { | |
4955 int empty_fd; | |
4956 | |
4957 if (org == NULL | |
557 | 4958 || (empty_fd = mch_open(org, |
4959 O_CREAT | O_EXTRA | O_EXCL | O_NOFOLLOW, | |
216 | 4960 perm < 0 ? 0666 : (perm & 0777))) < 0) |
7 | 4961 EMSG(_("E206: patchmode: can't touch empty original file")); |
4962 else | |
4963 close(empty_fd); | |
4964 } | |
4965 if (org != NULL) | |
4966 { | |
4967 mch_setperm((char_u *)org, mch_getperm(fname) & 0777); | |
4968 vim_free(org); | |
4969 } | |
4970 } | |
4971 | |
4972 /* | |
4973 * Remove the backup unless 'backup' option is set | |
4974 */ | |
4975 if (!p_bk && backup != NULL && mch_remove(backup) != 0) | |
4976 EMSG(_("E207: Can't delete backup file")); | |
4977 | |
4978 #ifdef FEAT_SUN_WORKSHOP | |
4979 if (usingSunWorkShop) | |
4980 workshop_file_saved((char *) ffname); | |
4981 #endif | |
4982 | |
4983 goto nofail; | |
4984 | |
4985 /* | |
4986 * Finish up. We get here either after failure or success. | |
4987 */ | |
4988 fail: | |
4989 --no_wait_return; /* may wait for return now */ | |
4990 nofail: | |
4991 | |
4992 /* Done saving, we accept changed buffer warnings again */ | |
4993 buf->b_saving = FALSE; | |
4994 | |
4995 vim_free(backup); | |
4996 if (buffer != smallbuf) | |
4997 vim_free(buffer); | |
4998 #ifdef FEAT_MBYTE | |
4999 vim_free(fenc_tofree); | |
5000 vim_free(write_info.bw_conv_buf); | |
5001 # ifdef USE_ICONV | |
5002 if (write_info.bw_iconv_fd != (iconv_t)-1) | |
5003 { | |
5004 iconv_close(write_info.bw_iconv_fd); | |
5005 write_info.bw_iconv_fd = (iconv_t)-1; | |
5006 } | |
5007 # endif | |
5008 #endif | |
5009 #ifdef HAVE_ACL | |
5010 mch_free_acl(acl); | |
5011 #endif | |
5012 | |
5013 if (errmsg != NULL) | |
5014 { | |
835 | 5015 int numlen = errnum != NULL ? (int)STRLEN(errnum) : 0; |
7 | 5016 |
5017 attr = hl_attr(HLF_E); /* set highlight for error messages */ | |
5018 msg_add_fname(buf, | |
5019 #ifndef UNIX | |
5020 sfname | |
5021 #else | |
5022 fname | |
5023 #endif | |
5024 ); /* put file name in IObuff with quotes */ | |
5025 if (STRLEN(IObuff) + STRLEN(errmsg) + numlen >= IOSIZE) | |
5026 IObuff[IOSIZE - STRLEN(errmsg) - numlen - 1] = NUL; | |
5027 /* If the error message has the form "is ...", put the error number in | |
5028 * front of the file name. */ | |
5029 if (errnum != NULL) | |
5030 { | |
1620 | 5031 STRMOVE(IObuff + numlen, IObuff); |
7 | 5032 mch_memmove(IObuff, errnum, (size_t)numlen); |
5033 } | |
5034 STRCAT(IObuff, errmsg); | |
5035 emsg(IObuff); | |
1947 | 5036 if (errmsg_allocated) |
5037 vim_free(errmsg); | |
7 | 5038 |
5039 retval = FAIL; | |
5040 if (end == 0) | |
5041 { | |
5042 MSG_PUTS_ATTR(_("\nWARNING: Original file may be lost or damaged\n"), | |
5043 attr | MSG_HIST); | |
5044 MSG_PUTS_ATTR(_("don't quit the editor until the file is successfully written!"), | |
5045 attr | MSG_HIST); | |
5046 | |
5047 /* Update the timestamp to avoid an "overwrite changed file" | |
5048 * prompt when writing again. */ | |
5049 if (mch_stat((char *)fname, &st_old) >= 0) | |
5050 { | |
5051 buf_store_time(buf, &st_old, fname); | |
5052 buf->b_mtime_read = buf->b_mtime; | |
5053 } | |
5054 } | |
5055 } | |
5056 msg_scroll = msg_save; | |
5057 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5058 #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
|
5059 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5060 * 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
|
5061 * file. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5062 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5063 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
|
5064 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5065 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
|
5066 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5067 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
|
5068 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
|
5069 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5070 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5071 |
7 | 5072 #ifdef FEAT_AUTOCMD |
5073 #ifdef FEAT_EVAL | |
5074 if (!should_abort(retval)) | |
5075 #else | |
5076 if (!got_int) | |
5077 #endif | |
5078 { | |
5079 aco_save_T aco; | |
5080 | |
3482 | 5081 curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */ |
5082 | |
7 | 5083 /* |
5084 * Apply POST autocommands. | |
5085 * Careful: The autocommands may call buf_write() recursively! | |
5086 */ | |
5087 aucmd_prepbuf(&aco, buf); | |
5088 | |
5089 if (append) | |
5090 apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname, | |
5091 FALSE, curbuf, eap); | |
5092 else if (filtering) | |
5093 apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname, | |
5094 FALSE, curbuf, eap); | |
5095 else if (reset_changed && whole) | |
5096 apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname, | |
5097 FALSE, curbuf, eap); | |
5098 else | |
5099 apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname, | |
5100 FALSE, curbuf, eap); | |
5101 | |
5102 /* restore curwin/curbuf and a few other things */ | |
5103 aucmd_restbuf(&aco); | |
5104 | |
5105 #ifdef FEAT_EVAL | |
5106 if (aborting()) /* autocmds may abort script processing */ | |
5107 retval = FALSE; | |
5108 #endif | |
5109 } | |
5110 #endif | |
5111 | |
5112 got_int |= prev_got_int; | |
5113 | |
5114 #ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */ | |
5115 /* Update machine specific information. */ | |
5116 mch_post_buffer_write(buf); | |
5117 #endif | |
5118 return retval; | |
5119 } | |
5120 | |
5121 /* | |
633 | 5122 * Set the name of the current buffer. Use when the buffer doesn't have a |
5123 * name and a ":r" or ":w" command with a file name is used. | |
5124 */ | |
5125 static int | |
5126 set_rw_fname(fname, sfname) | |
5127 char_u *fname; | |
5128 char_u *sfname; | |
5129 { | |
5130 #ifdef FEAT_AUTOCMD | |
1905 | 5131 buf_T *buf = curbuf; |
5132 | |
633 | 5133 /* It's like the unnamed buffer is deleted.... */ |
5134 if (curbuf->b_p_bl) | |
5135 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); | |
5136 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf); | |
5137 # ifdef FEAT_EVAL | |
5138 if (aborting()) /* autocmds may abort script processing */ | |
5139 return FAIL; | |
5140 # endif | |
1905 | 5141 if (curbuf != buf) |
5142 { | |
5143 /* We are in another buffer now, don't do the renaming. */ | |
5144 EMSG(_(e_auchangedbuf)); | |
5145 return FAIL; | |
5146 } | |
633 | 5147 #endif |
5148 | |
5149 if (setfname(curbuf, fname, sfname, FALSE) == OK) | |
5150 curbuf->b_flags |= BF_NOTEDITED; | |
5151 | |
5152 #ifdef FEAT_AUTOCMD | |
5153 /* ....and a new named one is created */ | |
5154 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf); | |
5155 if (curbuf->b_p_bl) | |
5156 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf); | |
5157 # ifdef FEAT_EVAL | |
5158 if (aborting()) /* autocmds may abort script processing */ | |
5159 return FAIL; | |
5160 # endif | |
5161 | |
5162 /* Do filetype detection now if 'filetype' is empty. */ | |
5163 if (*curbuf->b_p_ft == NUL) | |
5164 { | |
819 | 5165 if (au_has_group((char_u *)"filetypedetect")) |
676 | 5166 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE); |
717 | 5167 do_modelines(0); |
633 | 5168 } |
5169 #endif | |
5170 | |
5171 return OK; | |
5172 } | |
5173 | |
5174 /* | |
7 | 5175 * Put file name into IObuff with quotes. |
5176 */ | |
33 | 5177 void |
7 | 5178 msg_add_fname(buf, fname) |
5179 buf_T *buf; | |
5180 char_u *fname; | |
5181 { | |
5182 if (fname == NULL) | |
5183 fname = (char_u *)"-stdin-"; | |
5184 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE); | |
5185 IObuff[0] = '"'; | |
5186 STRCAT(IObuff, "\" "); | |
5187 } | |
5188 | |
5189 /* | |
5190 * Append message for text mode to IObuff. | |
5191 * Return TRUE if something appended. | |
5192 */ | |
5193 static int | |
5194 msg_add_fileformat(eol_type) | |
5195 int eol_type; | |
5196 { | |
5197 #ifndef USE_CRNL | |
5198 if (eol_type == EOL_DOS) | |
5199 { | |
5200 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]")); | |
5201 return TRUE; | |
5202 } | |
5203 #endif | |
5204 #ifndef USE_CR | |
5205 if (eol_type == EOL_MAC) | |
5206 { | |
5207 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]")); | |
5208 return TRUE; | |
5209 } | |
5210 #endif | |
5211 #if defined(USE_CRNL) || defined(USE_CR) | |
5212 if (eol_type == EOL_UNIX) | |
5213 { | |
5214 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]")); | |
5215 return TRUE; | |
5216 } | |
5217 #endif | |
5218 return FALSE; | |
5219 } | |
5220 | |
5221 /* | |
5222 * Append line and character count to IObuff. | |
5223 */ | |
33 | 5224 void |
7 | 5225 msg_add_lines(insert_space, lnum, nchars) |
5226 int insert_space; | |
5227 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
|
5228 off_t nchars; |
7 | 5229 { |
5230 char_u *p; | |
5231 | |
5232 p = IObuff + STRLEN(IObuff); | |
5233 | |
5234 if (insert_space) | |
5235 *p++ = ' '; | |
5236 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
|
5237 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
|
5238 #ifdef LONG_LONG_OFF_T |
5688 | 5239 "%ldL, %lldC", lnum, (long long)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
|
5240 #else |
2261
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5241 /* 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
|
5242 "%ldL, %ldC", lnum, (long)nchars |
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5243 #endif |
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5244 ); |
7 | 5245 else |
5246 { | |
5247 if (lnum == 1) | |
5248 STRCPY(p, _("1 line, ")); | |
5249 else | |
5250 sprintf((char *)p, _("%ld lines, "), lnum); | |
5251 p += STRLEN(p); | |
5252 if (nchars == 1) | |
5253 STRCPY(p, _("1 character")); | |
5254 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
|
5255 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
|
5256 #ifdef LONG_LONG_OFF_T |
5688 | 5257 _("%lld characters"), (long long)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
|
5258 #else |
2261
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5259 /* 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
|
5260 _("%ld characters"), (long)nchars |
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5261 #endif |
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5262 ); |
7 | 5263 } |
5264 } | |
5265 | |
5266 /* | |
5267 * Append message for missing line separator to IObuff. | |
5268 */ | |
5269 static void | |
5270 msg_add_eol() | |
5271 { | |
5272 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]")); | |
5273 } | |
5274 | |
5275 /* | |
5276 * Check modification time of file, before writing to it. | |
5277 * The size isn't checked, because using a tool like "gzip" takes care of | |
5278 * using the same timestamp but can't set the size. | |
5279 */ | |
5280 static int | |
5281 check_mtime(buf, st) | |
5282 buf_T *buf; | |
5283 struct stat *st; | |
5284 { | |
5285 if (buf->b_mtime_read != 0 | |
5286 && time_differs((long)st->st_mtime, buf->b_mtime_read)) | |
5287 { | |
5288 msg_scroll = TRUE; /* don't overwrite messages here */ | |
5289 msg_silent = 0; /* must give this prompt */ | |
5290 /* don't use emsg() here, don't want to flush the buffers */ | |
5291 MSG_ATTR(_("WARNING: The file has been changed since reading it!!!"), | |
5292 hl_attr(HLF_E)); | |
5293 if (ask_yesno((char_u *)_("Do you really want to write to it"), | |
5294 TRUE) == 'n') | |
5295 return FAIL; | |
5296 msg_scroll = FALSE; /* always overwrite the file message now */ | |
5297 } | |
5298 return OK; | |
5299 } | |
5300 | |
5301 static int | |
5302 time_differs(t1, t2) | |
5303 long t1, t2; | |
5304 { | |
5305 #if defined(__linux__) || defined(MSDOS) || defined(MSWIN) | |
5306 /* On a FAT filesystem, esp. under Linux, there are only 5 bits to store | |
5307 * the seconds. Since the roundoff is done when flushing the inode, the | |
5308 * time may change unexpectedly by one second!!! */ | |
5309 return (t1 - t2 > 1 || t2 - t1 > 1); | |
5310 #else | |
5311 return (t1 != t2); | |
5312 #endif | |
5313 } | |
5314 | |
5315 /* | |
5316 * Call write() to write a number of bytes to the file. | |
2664 | 5317 * Handles encryption and 'encoding' conversion. |
7 | 5318 * |
5319 * Return FAIL for failure, OK otherwise. | |
5320 */ | |
5321 static int | |
5322 buf_write_bytes(ip) | |
5323 struct bw_info *ip; | |
5324 { | |
5325 int wlen; | |
5326 char_u *buf = ip->bw_buf; /* data to write */ | |
5327 int len = ip->bw_len; /* length of data */ | |
5328 #ifdef HAS_BW_FLAGS | |
5329 int flags = ip->bw_flags; /* extra flags */ | |
5330 #endif | |
5331 | |
5332 #ifdef FEAT_MBYTE | |
5333 /* | |
5334 * Skip conversion when writing the crypt magic number or the BOM. | |
5335 */ | |
5336 if (!(flags & FIO_NOCONVERT)) | |
5337 { | |
5338 char_u *p; | |
5339 unsigned c; | |
5340 int n; | |
5341 | |
5342 if (flags & FIO_UTF8) | |
5343 { | |
5344 /* | |
5345 * Convert latin1 in the buffer to UTF-8 in the file. | |
5346 */ | |
5347 p = ip->bw_conv_buf; /* translate to buffer */ | |
5348 for (wlen = 0; wlen < len; ++wlen) | |
5349 p += utf_char2bytes(buf[wlen], p); | |
5350 buf = ip->bw_conv_buf; | |
5351 len = (int)(p - ip->bw_conv_buf); | |
5352 } | |
5353 else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1)) | |
5354 { | |
5355 /* | |
5356 * Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or | |
5357 * Latin1 chars in the file. | |
5358 */ | |
5359 if (flags & FIO_LATIN1) | |
5360 p = buf; /* translate in-place (can only get shorter) */ | |
5361 else | |
5362 p = ip->bw_conv_buf; /* translate to buffer */ | |
5363 for (wlen = 0; wlen < len; wlen += n) | |
5364 { | |
5365 if (wlen == 0 && ip->bw_restlen != 0) | |
5366 { | |
5367 int l; | |
5368 | |
5369 /* Use remainder of previous call. Append the start of | |
5370 * buf[] to get a full sequence. Might still be too | |
5371 * short! */ | |
5372 l = CONV_RESTLEN - ip->bw_restlen; | |
5373 if (l > len) | |
5374 l = len; | |
5375 mch_memmove(ip->bw_rest + ip->bw_restlen, buf, (size_t)l); | |
474 | 5376 n = utf_ptr2len_len(ip->bw_rest, ip->bw_restlen + l); |
7 | 5377 if (n > ip->bw_restlen + len) |
5378 { | |
5379 /* We have an incomplete byte sequence at the end to | |
5380 * be written. We can't convert it without the | |
5381 * remaining bytes. Keep them for the next call. */ | |
5382 if (ip->bw_restlen + len > CONV_RESTLEN) | |
5383 return FAIL; | |
5384 ip->bw_restlen += len; | |
5385 break; | |
5386 } | |
5387 if (n > 1) | |
5388 c = utf_ptr2char(ip->bw_rest); | |
5389 else | |
5390 c = ip->bw_rest[0]; | |
5391 if (n >= ip->bw_restlen) | |
5392 { | |
5393 n -= ip->bw_restlen; | |
5394 ip->bw_restlen = 0; | |
5395 } | |
5396 else | |
5397 { | |
5398 ip->bw_restlen -= n; | |
5399 mch_memmove(ip->bw_rest, ip->bw_rest + n, | |
5400 (size_t)ip->bw_restlen); | |
5401 n = 0; | |
5402 } | |
5403 } | |
5404 else | |
5405 { | |
474 | 5406 n = utf_ptr2len_len(buf + wlen, len - wlen); |
7 | 5407 if (n > len - wlen) |
5408 { | |
5409 /* We have an incomplete byte sequence at the end to | |
5410 * be written. We can't convert it without the | |
5411 * remaining bytes. Keep them for the next call. */ | |
5412 if (len - wlen > CONV_RESTLEN) | |
5413 return FAIL; | |
5414 ip->bw_restlen = len - wlen; | |
5415 mch_memmove(ip->bw_rest, buf + wlen, | |
5416 (size_t)ip->bw_restlen); | |
5417 break; | |
5418 } | |
5419 if (n > 1) | |
5420 c = utf_ptr2char(buf + wlen); | |
5421 else | |
5422 c = buf[wlen]; | |
5423 } | |
5424 | |
1947 | 5425 if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error) |
5426 { | |
5427 ip->bw_conv_error = TRUE; | |
5428 ip->bw_conv_error_lnum = ip->bw_start_lnum; | |
5429 } | |
5430 if (c == NL) | |
5431 ++ip->bw_start_lnum; | |
7 | 5432 } |
5433 if (flags & FIO_LATIN1) | |
5434 len = (int)(p - buf); | |
5435 else | |
5436 { | |
5437 buf = ip->bw_conv_buf; | |
5438 len = (int)(p - ip->bw_conv_buf); | |
5439 } | |
5440 } | |
5441 | |
5442 # ifdef WIN3264 | |
5443 else if (flags & FIO_CODEPAGE) | |
5444 { | |
5445 /* | |
5446 * Convert UTF-8 or codepage to UCS-2 and then to MS-Windows | |
5447 * codepage. | |
5448 */ | |
5449 char_u *from; | |
5450 size_t fromlen; | |
5451 char_u *to; | |
5452 int u8c; | |
5453 BOOL bad = FALSE; | |
5454 int needed; | |
5455 | |
5456 if (ip->bw_restlen > 0) | |
5457 { | |
5458 /* Need to concatenate the remainder of the previous call and | |
5459 * the bytes of the current call. Use the end of the | |
5460 * conversion buffer for this. */ | |
5461 fromlen = len + ip->bw_restlen; | |
5462 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; | |
5463 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); | |
5464 mch_memmove(from + ip->bw_restlen, buf, (size_t)len); | |
5465 } | |
5466 else | |
5467 { | |
5468 from = buf; | |
5469 fromlen = len; | |
5470 } | |
5471 | |
5472 to = ip->bw_conv_buf; | |
5473 if (enc_utf8) | |
5474 { | |
5475 /* Convert from UTF-8 to UCS-2, to the start of the buffer. | |
5476 * The buffer has been allocated to be big enough. */ | |
5477 while (fromlen > 0) | |
5478 { | |
835 | 5479 n = (int)utf_ptr2len_len(from, (int)fromlen); |
7 | 5480 if (n > (int)fromlen) /* incomplete byte sequence */ |
5481 break; | |
5482 u8c = utf_ptr2char(from); | |
5483 *to++ = (u8c & 0xff); | |
5484 *to++ = (u8c >> 8); | |
5485 fromlen -= n; | |
5486 from += n; | |
5487 } | |
5488 | |
5489 /* Copy remainder to ip->bw_rest[] to be used for the next | |
5490 * call. */ | |
5491 if (fromlen > CONV_RESTLEN) | |
5492 { | |
5493 /* weird overlong sequence */ | |
5494 ip->bw_conv_error = TRUE; | |
5495 return FAIL; | |
5496 } | |
5497 mch_memmove(ip->bw_rest, from, fromlen); | |
835 | 5498 ip->bw_restlen = (int)fromlen; |
7 | 5499 } |
5500 else | |
5501 { | |
5502 /* Convert from enc_codepage to UCS-2, to the start of the | |
5503 * buffer. The buffer has been allocated to be big enough. */ | |
5504 ip->bw_restlen = 0; | |
5505 needed = MultiByteToWideChar(enc_codepage, | |
1142 | 5506 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen, |
7 | 5507 NULL, 0); |
5508 if (needed == 0) | |
5509 { | |
5510 /* When conversion fails there may be a trailing byte. */ | |
5511 needed = MultiByteToWideChar(enc_codepage, | |
1142 | 5512 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen - 1, |
7 | 5513 NULL, 0); |
5514 if (needed == 0) | |
5515 { | |
5516 /* Conversion doesn't work. */ | |
5517 ip->bw_conv_error = TRUE; | |
5518 return FAIL; | |
5519 } | |
5520 /* Save the trailing byte for the next call. */ | |
5521 ip->bw_rest[0] = from[fromlen - 1]; | |
5522 ip->bw_restlen = 1; | |
5523 } | |
5524 needed = MultiByteToWideChar(enc_codepage, MB_ERR_INVALID_CHARS, | |
1142 | 5525 (LPCSTR)from, (int)(fromlen - ip->bw_restlen), |
7 | 5526 (LPWSTR)to, needed); |
5527 if (needed == 0) | |
5528 { | |
5529 /* Safety check: Conversion doesn't work. */ | |
5530 ip->bw_conv_error = TRUE; | |
5531 return FAIL; | |
5532 } | |
5533 to += needed * 2; | |
5534 } | |
5535 | |
5536 fromlen = to - ip->bw_conv_buf; | |
5537 buf = to; | |
5538 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
5539 if (FIO_GET_CP(flags) == CP_UTF8) | |
5540 { | |
5541 /* Convert from UCS-2 to UTF-8, using the remainder of the | |
5542 * conversion buffer. Fails when out of space. */ | |
5543 for (from = ip->bw_conv_buf; fromlen > 1; fromlen -= 2) | |
5544 { | |
5545 u8c = *from++; | |
5546 u8c += (*from++ << 8); | |
5547 to += utf_char2bytes(u8c, to); | |
5548 if (to + 6 >= ip->bw_conv_buf + ip->bw_conv_buflen) | |
5549 { | |
5550 ip->bw_conv_error = TRUE; | |
5551 return FAIL; | |
5552 } | |
5553 } | |
835 | 5554 len = (int)(to - buf); |
7 | 5555 } |
5556 else | |
5557 #endif | |
5558 { | |
5559 /* Convert from UCS-2 to the codepage, using the remainder of | |
5560 * the conversion buffer. If the conversion uses the default | |
5561 * character "0", the data doesn't fit in this encoding, so | |
5562 * fail. */ | |
5563 len = WideCharToMultiByte(FIO_GET_CP(flags), 0, | |
5564 (LPCWSTR)ip->bw_conv_buf, (int)fromlen / sizeof(WCHAR), | |
1142 | 5565 (LPSTR)to, (int)(ip->bw_conv_buflen - fromlen), 0, |
5566 &bad); | |
7 | 5567 if (bad) |
5568 { | |
5569 ip->bw_conv_error = TRUE; | |
5570 return FAIL; | |
5571 } | |
5572 } | |
5573 } | |
5574 # endif | |
5575 | |
765 | 5576 # ifdef MACOS_CONVERT |
7 | 5577 else if (flags & FIO_MACROMAN) |
5578 { | |
5579 /* | |
5580 * Convert UTF-8 or latin1 to Apple MacRoman. | |
5581 */ | |
5582 char_u *from; | |
5583 size_t fromlen; | |
5584 | |
5585 if (ip->bw_restlen > 0) | |
5586 { | |
5587 /* Need to concatenate the remainder of the previous call and | |
5588 * the bytes of the current call. Use the end of the | |
5589 * conversion buffer for this. */ | |
5590 fromlen = len + ip->bw_restlen; | |
5591 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; | |
5592 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); | |
5593 mch_memmove(from + ip->bw_restlen, buf, (size_t)len); | |
5594 } | |
5595 else | |
5596 { | |
5597 from = buf; | |
5598 fromlen = len; | |
5599 } | |
5600 | |
18 | 5601 if (enc2macroman(from, fromlen, |
5602 ip->bw_conv_buf, &len, ip->bw_conv_buflen, | |
5603 ip->bw_rest, &ip->bw_restlen) == FAIL) | |
7 | 5604 { |
5605 ip->bw_conv_error = TRUE; | |
5606 return FAIL; | |
5607 } | |
5608 buf = ip->bw_conv_buf; | |
5609 } | |
5610 # endif | |
5611 | |
5612 # ifdef USE_ICONV | |
5613 if (ip->bw_iconv_fd != (iconv_t)-1) | |
5614 { | |
5615 const char *from; | |
5616 size_t fromlen; | |
5617 char *to; | |
5618 size_t tolen; | |
5619 | |
5620 /* Convert with iconv(). */ | |
5621 if (ip->bw_restlen > 0) | |
5622 { | |
1836 | 5623 char *fp; |
5624 | |
7 | 5625 /* Need to concatenate the remainder of the previous call and |
5626 * the bytes of the current call. Use the end of the | |
5627 * conversion buffer for this. */ | |
5628 fromlen = len + ip->bw_restlen; | |
1836 | 5629 fp = (char *)ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; |
5630 mch_memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen); | |
5631 mch_memmove(fp + ip->bw_restlen, buf, (size_t)len); | |
5632 from = fp; | |
7 | 5633 tolen = ip->bw_conv_buflen - fromlen; |
5634 } | |
5635 else | |
5636 { | |
5637 from = (const char *)buf; | |
5638 fromlen = len; | |
5639 tolen = ip->bw_conv_buflen; | |
5640 } | |
5641 to = (char *)ip->bw_conv_buf; | |
5642 | |
5643 if (ip->bw_first) | |
5644 { | |
5645 size_t save_len = tolen; | |
5646 | |
5647 /* output the initial shift state sequence */ | |
5648 (void)iconv(ip->bw_iconv_fd, NULL, NULL, &to, &tolen); | |
5649 | |
5650 /* There is a bug in iconv() on Linux (which appears to be | |
5651 * wide-spread) which sets "to" to NULL and messes up "tolen". | |
5652 */ | |
5653 if (to == NULL) | |
5654 { | |
5655 to = (char *)ip->bw_conv_buf; | |
5656 tolen = save_len; | |
5657 } | |
5658 ip->bw_first = FALSE; | |
5659 } | |
5660 | |
5661 /* | |
5662 * If iconv() has an error or there is not enough room, fail. | |
5663 */ | |
5664 if ((iconv(ip->bw_iconv_fd, (void *)&from, &fromlen, &to, &tolen) | |
5665 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) | |
5666 || fromlen > CONV_RESTLEN) | |
5667 { | |
5668 ip->bw_conv_error = TRUE; | |
5669 return FAIL; | |
5670 } | |
5671 | |
5672 /* copy remainder to ip->bw_rest[] to be used for the next call. */ | |
5673 if (fromlen > 0) | |
5674 mch_memmove(ip->bw_rest, (void *)from, fromlen); | |
5675 ip->bw_restlen = (int)fromlen; | |
5676 | |
5677 buf = ip->bw_conv_buf; | |
5678 len = (int)((char_u *)to - ip->bw_conv_buf); | |
5679 } | |
5680 # endif | |
5681 } | |
5682 #endif /* FEAT_MBYTE */ | |
5683 | |
5684 #ifdef FEAT_CRYPT | |
6122 | 5685 if (flags & FIO_ENCRYPTED) |
5686 { | |
5687 /* Encrypt the data. Do it in-place if possible, otherwise use an | |
5688 * allocated buffer. */ | |
5689 if (crypt_works_inplace(ip->bw_buffer->b_cryptstate)) | |
5690 { | |
5691 crypt_encode_inplace(ip->bw_buffer->b_cryptstate, buf, len); | |
5692 } | |
5693 else | |
5694 { | |
5695 char_u *outbuf; | |
5696 | |
5697 len = crypt_encode_alloc(curbuf->b_cryptstate, buf, len, &outbuf); | |
5698 if (len == 0) | |
5699 return OK; /* Crypt layer is buffering, will flush later. */ | |
5700 wlen = write_eintr(ip->bw_fd, outbuf, len); | |
5701 vim_free(outbuf); | |
5702 return (wlen < len) ? FAIL : OK; | |
5703 } | |
5704 } | |
7 | 5705 #endif |
5706 | |
2664 | 5707 wlen = write_eintr(ip->bw_fd, buf, len); |
5708 return (wlen < len) ? FAIL : OK; | |
7 | 5709 } |
5710 | |
5711 #ifdef FEAT_MBYTE | |
5712 /* | |
5713 * Convert a Unicode character to bytes. | |
1947 | 5714 * Return TRUE for an error, FALSE when it's OK. |
7 | 5715 */ |
5716 static int | |
5717 ucs2bytes(c, pp, flags) | |
5718 unsigned c; /* in: character */ | |
5719 char_u **pp; /* in/out: pointer to result */ | |
5720 int flags; /* FIO_ flags */ | |
5721 { | |
5722 char_u *p = *pp; | |
5723 int error = FALSE; | |
5724 int cc; | |
5725 | |
5726 | |
5727 if (flags & FIO_UCS4) | |
5728 { | |
5729 if (flags & FIO_ENDIAN_L) | |
5730 { | |
5731 *p++ = c; | |
5732 *p++ = (c >> 8); | |
5733 *p++ = (c >> 16); | |
5734 *p++ = (c >> 24); | |
5735 } | |
5736 else | |
5737 { | |
5738 *p++ = (c >> 24); | |
5739 *p++ = (c >> 16); | |
5740 *p++ = (c >> 8); | |
5741 *p++ = c; | |
5742 } | |
5743 } | |
5744 else if (flags & (FIO_UCS2 | FIO_UTF16)) | |
5745 { | |
5746 if (c >= 0x10000) | |
5747 { | |
5748 if (flags & FIO_UTF16) | |
5749 { | |
5750 /* Make two words, ten bits of the character in each. First | |
5751 * word is 0xd800 - 0xdbff, second one 0xdc00 - 0xdfff */ | |
5752 c -= 0x10000; | |
5753 if (c >= 0x100000) | |
5754 error = TRUE; | |
5755 cc = ((c >> 10) & 0x3ff) + 0xd800; | |
5756 if (flags & FIO_ENDIAN_L) | |
5757 { | |
5758 *p++ = cc; | |
5759 *p++ = ((unsigned)cc >> 8); | |
5760 } | |
5761 else | |
5762 { | |
5763 *p++ = ((unsigned)cc >> 8); | |
5764 *p++ = cc; | |
5765 } | |
5766 c = (c & 0x3ff) + 0xdc00; | |
5767 } | |
5768 else | |
5769 error = TRUE; | |
5770 } | |
5771 if (flags & FIO_ENDIAN_L) | |
5772 { | |
5773 *p++ = c; | |
5774 *p++ = (c >> 8); | |
5775 } | |
5776 else | |
5777 { | |
5778 *p++ = (c >> 8); | |
5779 *p++ = c; | |
5780 } | |
5781 } | |
5782 else /* Latin1 */ | |
5783 { | |
5784 if (c >= 0x100) | |
5785 { | |
5786 error = TRUE; | |
5787 *p++ = 0xBF; | |
5788 } | |
5789 else | |
5790 *p++ = c; | |
5791 } | |
5792 | |
5793 *pp = p; | |
5794 return error; | |
5795 } | |
5796 | |
5797 /* | |
1948 | 5798 * Return TRUE if file encoding "fenc" requires conversion from or to |
5799 * 'encoding'. | |
7 | 5800 */ |
5801 static int | |
1948 | 5802 need_conversion(fenc) |
5803 char_u *fenc; | |
7 | 5804 { |
1948 | 5805 int same_encoding; |
5806 int enc_flags; | |
5807 int fenc_flags; | |
5808 | |
5809 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0) | |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5810 { |
1948 | 5811 same_encoding = TRUE; |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5812 fenc_flags = 0; |
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5813 } |
1948 | 5814 else |
5815 { | |
5816 /* Ignore difference between "ansi" and "latin1", "ucs-4" and | |
5817 * "ucs-4be", etc. */ | |
5818 enc_flags = get_fio_flags(p_enc); | |
5819 fenc_flags = get_fio_flags(fenc); | |
5820 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags); | |
5821 } | |
5822 if (same_encoding) | |
5823 { | |
5824 /* Specified encoding matches with 'encoding'. This requires | |
5825 * conversion when 'encoding' is Unicode but not UTF-8. */ | |
5826 return enc_unicode != 0; | |
5827 } | |
5828 | |
5829 /* Encodings differ. However, conversion is not needed when 'enc' is any | |
5830 * Unicode encoding and the file is UTF-8. */ | |
5831 return !(enc_utf8 && fenc_flags == FIO_UTF8); | |
7 | 5832 } |
5833 | |
5834 /* | |
5835 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the | |
5836 * internal conversion. | |
5837 * if "ptr" is an empty string, use 'encoding'. | |
5838 */ | |
5839 static int | |
5840 get_fio_flags(ptr) | |
5841 char_u *ptr; | |
5842 { | |
5843 int prop; | |
5844 | |
5845 if (*ptr == NUL) | |
5846 ptr = p_enc; | |
5847 | |
5848 prop = enc_canon_props(ptr); | |
5849 if (prop & ENC_UNICODE) | |
5850 { | |
5851 if (prop & ENC_2BYTE) | |
5852 { | |
5853 if (prop & ENC_ENDIAN_L) | |
5854 return FIO_UCS2 | FIO_ENDIAN_L; | |
5855 return FIO_UCS2; | |
5856 } | |
5857 if (prop & ENC_4BYTE) | |
5858 { | |
5859 if (prop & ENC_ENDIAN_L) | |
5860 return FIO_UCS4 | FIO_ENDIAN_L; | |
5861 return FIO_UCS4; | |
5862 } | |
5863 if (prop & ENC_2WORD) | |
5864 { | |
5865 if (prop & ENC_ENDIAN_L) | |
5866 return FIO_UTF16 | FIO_ENDIAN_L; | |
5867 return FIO_UTF16; | |
5868 } | |
5869 return FIO_UTF8; | |
5870 } | |
5871 if (prop & ENC_LATIN1) | |
5872 return FIO_LATIN1; | |
5873 /* must be ENC_DBCS, requires iconv() */ | |
5874 return 0; | |
5875 } | |
5876 | |
5877 #ifdef WIN3264 | |
5878 /* | |
5879 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed | |
5880 * for the conversion MS-Windows can do for us. Also accept "utf-8". | |
5881 * Used for conversion between 'encoding' and 'fileencoding'. | |
5882 */ | |
5883 static int | |
5884 get_win_fio_flags(ptr) | |
5885 char_u *ptr; | |
5886 { | |
5887 int cp; | |
5888 | |
5889 /* Cannot do this when 'encoding' is not utf-8 and not a codepage. */ | |
5890 if (!enc_utf8 && enc_codepage <= 0) | |
5891 return 0; | |
5892 | |
5893 cp = encname2codepage(ptr); | |
5894 if (cp == 0) | |
5895 { | |
5896 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
5897 if (STRCMP(ptr, "utf-8") == 0) | |
5898 cp = CP_UTF8; | |
5899 else | |
5900 # endif | |
5901 return 0; | |
5902 } | |
5903 return FIO_PUT_CP(cp) | FIO_CODEPAGE; | |
5904 } | |
5905 #endif | |
5906 | |
5907 #ifdef MACOS_X | |
5908 /* | |
5909 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags | |
5910 * needed for the internal conversion to/from utf-8 or latin1. | |
5911 */ | |
5912 static int | |
5913 get_mac_fio_flags(ptr) | |
5914 char_u *ptr; | |
5915 { | |
5916 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
5917 && (enc_canon_props(ptr) & ENC_MACROMAN)) | |
5918 return FIO_MACROMAN; | |
5919 return 0; | |
5920 } | |
5921 #endif | |
5922 | |
5923 /* | |
5924 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size]. | |
5925 * "size" must be at least 2. | |
5926 * Return the name of the encoding and set "*lenp" to the length. | |
5927 * Returns NULL when no BOM found. | |
5928 */ | |
5929 static char_u * | |
5930 check_for_bom(p, size, lenp, flags) | |
5931 char_u *p; | |
5932 long size; | |
5933 int *lenp; | |
5934 int flags; | |
5935 { | |
5936 char *name = NULL; | |
5937 int len = 2; | |
5938 | |
5939 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf | |
1688 | 5940 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0)) |
7 | 5941 { |
5942 name = "utf-8"; /* EF BB BF */ | |
5943 len = 3; | |
5944 } | |
5945 else if (p[0] == 0xff && p[1] == 0xfe) | |
5946 { | |
5947 if (size >= 4 && p[2] == 0 && p[3] == 0 | |
5948 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L))) | |
5949 { | |
5950 name = "ucs-4le"; /* FF FE 00 00 */ | |
5951 len = 4; | |
5952 } | |
1735 | 5953 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L)) |
7 | 5954 name = "ucs-2le"; /* FF FE */ |
1735 | 5955 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L)) |
5956 /* utf-16le is preferred, it also works for ucs-2le text */ | |
7 | 5957 name = "utf-16le"; /* FF FE */ |
5958 } | |
5959 else if (p[0] == 0xfe && p[1] == 0xff | |
5960 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16)) | |
5961 { | |
1547 | 5962 /* Default to utf-16, it works also for ucs-2 text. */ |
5963 if (flags == FIO_UCS2) | |
5964 name = "ucs-2"; /* FE FF */ | |
5965 else | |
7 | 5966 name = "utf-16"; /* FE FF */ |
5967 } | |
5968 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe | |
5969 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4)) | |
5970 { | |
5971 name = "ucs-4"; /* 00 00 FE FF */ | |
5972 len = 4; | |
5973 } | |
5974 | |
5975 *lenp = len; | |
5976 return (char_u *)name; | |
5977 } | |
5978 | |
5979 /* | |
5980 * Generate a BOM in "buf[4]" for encoding "name". | |
5981 * Return the length of the BOM (zero when no BOM). | |
5982 */ | |
5983 static int | |
5984 make_bom(buf, name) | |
5985 char_u *buf; | |
5986 char_u *name; | |
5987 { | |
5988 int flags; | |
5989 char_u *p; | |
5990 | |
5991 flags = get_fio_flags(name); | |
5992 | |
5993 /* Can't put a BOM in a non-Unicode file. */ | |
5994 if (flags == FIO_LATIN1 || flags == 0) | |
5995 return 0; | |
5996 | |
5997 if (flags == FIO_UTF8) /* UTF-8 */ | |
5998 { | |
5999 buf[0] = 0xef; | |
6000 buf[1] = 0xbb; | |
6001 buf[2] = 0xbf; | |
6002 return 3; | |
6003 } | |
6004 p = buf; | |
6005 (void)ucs2bytes(0xfeff, &p, flags); | |
6006 return (int)(p - buf); | |
6007 } | |
6008 #endif | |
6009 | |
1418 | 6010 #if defined(FEAT_VIMINFO) || defined(FEAT_BROWSE) || \ |
1471 | 6011 defined(FEAT_QUICKFIX) || defined(FEAT_AUTOCMD) || defined(PROTO) |
7 | 6012 /* |
6013 * Try to find a shortname by comparing the fullname with the current | |
6014 * directory. | |
1411 | 6015 * Returns "full_path" or pointer into "full_path" if shortened. |
6016 */ | |
6017 char_u * | |
6018 shorten_fname1(full_path) | |
6019 char_u *full_path; | |
6020 { | |
2770 | 6021 char_u *dirname; |
1411 | 6022 char_u *p = full_path; |
6023 | |
2770 | 6024 dirname = alloc(MAXPATHL); |
6025 if (dirname == NULL) | |
6026 return full_path; | |
1411 | 6027 if (mch_dirname(dirname, MAXPATHL) == OK) |
6028 { | |
6029 p = shorten_fname(full_path, dirname); | |
6030 if (p == NULL || *p == NUL) | |
6031 p = full_path; | |
6032 } | |
2770 | 6033 vim_free(dirname); |
1411 | 6034 return p; |
6035 } | |
1418 | 6036 #endif |
1411 | 6037 |
6038 /* | |
6039 * Try to find a shortname by comparing the fullname with the current | |
6040 * directory. | |
7 | 6041 * Returns NULL if not shorter name possible, pointer into "full_path" |
6042 * otherwise. | |
6043 */ | |
6044 char_u * | |
6045 shorten_fname(full_path, dir_name) | |
6046 char_u *full_path; | |
6047 char_u *dir_name; | |
6048 { | |
6049 int len; | |
6050 char_u *p; | |
6051 | |
6052 if (full_path == NULL) | |
6053 return NULL; | |
6054 len = (int)STRLEN(dir_name); | |
6055 if (fnamencmp(dir_name, full_path, len) == 0) | |
6056 { | |
6057 p = full_path + len; | |
6058 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
6059 /* | |
6060 * MSDOS: when a file is in the root directory, dir_name will end in a | |
6061 * slash, since C: by itself does not define a specific dir. In this | |
6062 * case p may already be correct. <negri> | |
6063 */ | |
6064 if (!((len > 2) && (*(p - 2) == ':'))) | |
6065 #endif | |
6066 { | |
6067 if (vim_ispathsep(*p)) | |
6068 ++p; | |
6069 #ifndef VMS /* the path separator is always part of the path */ | |
6070 else | |
6071 p = NULL; | |
6072 #endif | |
6073 } | |
6074 } | |
6075 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
6076 /* | |
6077 * When using a file in the current drive, remove the drive name: | |
6078 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on | |
6079 * a floppy from "A:\dir" to "B:\dir". | |
6080 */ | |
6081 else if (len > 3 | |
6082 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0]) | |
6083 && full_path[1] == ':' | |
6084 && vim_ispathsep(full_path[2])) | |
6085 p = full_path + 2; | |
6086 #endif | |
6087 else | |
6088 p = NULL; | |
6089 return p; | |
6090 } | |
6091 | |
6092 /* | |
6093 * Shorten filenames for all buffers. | |
6094 * When "force" is TRUE: Use full path from now on for files currently being | |
6095 * edited, both for file name and swap file name. Try to shorten the file | |
6096 * names a bit, if safe to do so. | |
6097 * When "force" is FALSE: Only try to shorten absolute file names. | |
6098 * For buffers that have buftype "nofile" or "scratch": never change the file | |
6099 * name. | |
6100 */ | |
6101 void | |
6102 shorten_fnames(force) | |
6103 int force; | |
6104 { | |
6105 char_u dirname[MAXPATHL]; | |
6106 buf_T *buf; | |
6107 char_u *p; | |
6108 | |
6109 mch_dirname(dirname, MAXPATHL); | |
6110 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
6111 { | |
6112 if (buf->b_fname != NULL | |
6113 #ifdef FEAT_QUICKFIX | |
6114 && !bt_nofile(buf) | |
6115 #endif | |
6116 && !path_with_url(buf->b_fname) | |
6117 && (force | |
6118 || buf->b_sfname == NULL | |
6119 || mch_isFullName(buf->b_sfname))) | |
6120 { | |
6121 vim_free(buf->b_sfname); | |
6122 buf->b_sfname = NULL; | |
6123 p = shorten_fname(buf->b_ffname, dirname); | |
6124 if (p != NULL) | |
6125 { | |
6126 buf->b_sfname = vim_strsave(p); | |
6127 buf->b_fname = buf->b_sfname; | |
6128 } | |
6129 if (p == NULL || buf->b_fname == NULL) | |
6130 buf->b_fname = buf->b_ffname; | |
9 | 6131 } |
6132 | |
6133 /* Always make the swap file name a full path, a "nofile" buffer may | |
6134 * also have a swap file. */ | |
6135 mf_fullname(buf->b_ml.ml_mfp); | |
7 | 6136 } |
6137 #ifdef FEAT_WINDOWS | |
6138 status_redraw_all(); | |
672 | 6139 redraw_tabline = TRUE; |
7 | 6140 #endif |
6141 } | |
6142 | |
6143 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \ | |
6144 || defined(FEAT_GUI_MSWIN) \ | |
6145 || defined(FEAT_GUI_MAC) \ | |
6146 || defined(PROTO) | |
6147 /* | |
6148 * Shorten all filenames in "fnames[count]" by current directory. | |
6149 */ | |
6150 void | |
6151 shorten_filenames(fnames, count) | |
6152 char_u **fnames; | |
6153 int count; | |
6154 { | |
6155 int i; | |
6156 char_u dirname[MAXPATHL]; | |
6157 char_u *p; | |
6158 | |
6159 if (fnames == NULL || count < 1) | |
6160 return; | |
6161 mch_dirname(dirname, sizeof(dirname)); | |
6162 for (i = 0; i < count; ++i) | |
6163 { | |
6164 if ((p = shorten_fname(fnames[i], dirname)) != NULL) | |
6165 { | |
6166 /* shorten_fname() returns pointer in given "fnames[i]". If free | |
6167 * "fnames[i]" first, "p" becomes invalid. So we need to copy | |
6168 * "p" first then free fnames[i]. */ | |
6169 p = vim_strsave(p); | |
6170 vim_free(fnames[i]); | |
6171 fnames[i] = p; | |
6172 } | |
6173 } | |
6174 } | |
6175 #endif | |
6176 | |
6177 /* | |
1651 | 6178 * add extension to file name - change path/fo.o.h to path/fo.o.h.ext or |
7 | 6179 * fo_o_h.ext for MSDOS or when shortname option set. |
6180 * | |
6181 * Assumed that fname is a valid name found in the filesystem we assure that | |
6182 * the return value is a different name and ends in 'ext'. | |
6183 * "ext" MUST be at most 4 characters long if it starts with a dot, 3 | |
6184 * characters otherwise. | |
6185 * Space for the returned name is allocated, must be freed later. | |
6186 * Returns NULL when out of memory. | |
6187 */ | |
6188 char_u * | |
6189 modname(fname, ext, prepend_dot) | |
6190 char_u *fname, *ext; | |
6191 int prepend_dot; /* may prepend a '.' to file name */ | |
6192 { | |
6193 return buf_modname( | |
6194 #ifdef SHORT_FNAME | |
6195 TRUE, | |
6196 #else | |
6197 (curbuf->b_p_sn || curbuf->b_shortname), | |
6198 #endif | |
6199 fname, ext, prepend_dot); | |
6200 } | |
6201 | |
6202 char_u * | |
6203 buf_modname(shortname, fname, ext, prepend_dot) | |
6204 int shortname; /* use 8.3 file name */ | |
6205 char_u *fname, *ext; | |
6206 int prepend_dot; /* may prepend a '.' to file name */ | |
6207 { | |
6208 char_u *retval; | |
6209 char_u *s; | |
6210 char_u *e; | |
6211 char_u *ptr; | |
6212 int fnamelen, extlen; | |
6213 | |
6214 extlen = (int)STRLEN(ext); | |
6215 | |
6216 /* | |
6217 * If there is no file name we must get the name of the current directory | |
6218 * (we need the full path in case :cd is used). | |
6219 */ | |
6220 if (fname == NULL || *fname == NUL) | |
6221 { | |
6222 retval = alloc((unsigned)(MAXPATHL + extlen + 3)); | |
6223 if (retval == NULL) | |
6224 return NULL; | |
6225 if (mch_dirname(retval, MAXPATHL) == FAIL || | |
6226 (fnamelen = (int)STRLEN(retval)) == 0) | |
6227 { | |
6228 vim_free(retval); | |
6229 return NULL; | |
6230 } | |
39 | 6231 if (!after_pathsep(retval, retval + fnamelen)) |
7 | 6232 { |
6233 retval[fnamelen++] = PATHSEP; | |
6234 retval[fnamelen] = NUL; | |
6235 } | |
6236 #ifndef SHORT_FNAME | |
6237 prepend_dot = FALSE; /* nothing to prepend a dot to */ | |
6238 #endif | |
6239 } | |
6240 else | |
6241 { | |
6242 fnamelen = (int)STRLEN(fname); | |
6243 retval = alloc((unsigned)(fnamelen + extlen + 3)); | |
6244 if (retval == NULL) | |
6245 return NULL; | |
6246 STRCPY(retval, fname); | |
6247 #ifdef VMS | |
6248 vms_remove_version(retval); /* we do not need versions here */ | |
6249 #endif | |
6250 } | |
6251 | |
6252 /* | |
6253 * search backwards until we hit a '/', '\' or ':' replacing all '.' | |
6254 * by '_' for MSDOS or when shortname option set and ext starts with a dot. | |
6255 * Then truncate what is after the '/', '\' or ':' to 8 characters for | |
6256 * MSDOS and 26 characters for AMIGA, a lot more for UNIX. | |
6257 */ | |
391 | 6258 for (ptr = retval + fnamelen; ptr > retval; mb_ptr_back(retval, ptr)) |
7 | 6259 { |
6260 if (*ext == '.' | |
2823 | 6261 #ifdef USE_LONG_FNAME |
7 | 6262 && (!USE_LONG_FNAME || shortname) |
2823 | 6263 #else |
6264 # ifndef SHORT_FNAME | |
7 | 6265 && shortname |
6266 # endif | |
2823 | 6267 #endif |
7 | 6268 ) |
6269 if (*ptr == '.') /* replace '.' by '_' */ | |
6270 *ptr = '_'; | |
6271 if (vim_ispathsep(*ptr)) | |
391 | 6272 { |
6273 ++ptr; | |
7 | 6274 break; |
391 | 6275 } |
6276 } | |
7 | 6277 |
6278 /* the file name has at most BASENAMELEN characters. */ | |
6279 #ifndef SHORT_FNAME | |
6280 if (STRLEN(ptr) > (unsigned)BASENAMELEN) | |
6281 ptr[BASENAMELEN] = '\0'; | |
6282 #endif | |
6283 | |
6284 s = ptr + STRLEN(ptr); | |
6285 | |
6286 /* | |
6287 * For 8.3 file names we may have to reduce the length. | |
6288 */ | |
6289 #ifdef USE_LONG_FNAME | |
6290 if (!USE_LONG_FNAME || shortname) | |
6291 #else | |
6292 # ifndef SHORT_FNAME | |
6293 if (shortname) | |
6294 # endif | |
6295 #endif | |
6296 { | |
6297 /* | |
6298 * If there is no file name, or the file name ends in '/', and the | |
6299 * extension starts with '.', put a '_' before the dot, because just | |
6300 * ".ext" is invalid. | |
6301 */ | |
6302 if (fname == NULL || *fname == NUL | |
6303 || vim_ispathsep(fname[STRLEN(fname) - 1])) | |
6304 { | |
6305 if (*ext == '.') | |
6306 *s++ = '_'; | |
6307 } | |
6308 /* | |
6309 * If the extension starts with '.', truncate the base name at 8 | |
6310 * characters | |
6311 */ | |
6312 else if (*ext == '.') | |
6313 { | |
1877 | 6314 if ((size_t)(s - ptr) > (size_t)8) |
7 | 6315 { |
6316 s = ptr + 8; | |
6317 *s = '\0'; | |
6318 } | |
6319 } | |
6320 /* | |
6321 * If the extension doesn't start with '.', and the file name | |
6322 * doesn't have an extension yet, append a '.' | |
6323 */ | |
6324 else if ((e = vim_strchr(ptr, '.')) == NULL) | |
6325 *s++ = '.'; | |
6326 /* | |
6327 * If the extension doesn't start with '.', and there already is an | |
1201 | 6328 * extension, it may need to be truncated |
7 | 6329 */ |
6330 else if ((int)STRLEN(e) + extlen > 4) | |
6331 s = e + 4 - extlen; | |
6332 } | |
6333 #if defined(OS2) || defined(USE_LONG_FNAME) || defined(WIN3264) | |
6334 /* | |
6335 * If there is no file name, and the extension starts with '.', put a | |
6336 * '_' before the dot, because just ".ext" may be invalid if it's on a | |
6337 * FAT partition, and on HPFS it doesn't matter. | |
6338 */ | |
6339 else if ((fname == NULL || *fname == NUL) && *ext == '.') | |
6340 *s++ = '_'; | |
6341 #endif | |
6342 | |
6343 /* | |
1651 | 6344 * Append the extension. |
7 | 6345 * ext can start with '.' and cannot exceed 3 more characters. |
6346 */ | |
6347 STRCPY(s, ext); | |
6348 | |
6349 #ifndef SHORT_FNAME | |
6350 /* | |
6351 * Prepend the dot. | |
6352 */ | |
2823 | 6353 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.' |
7 | 6354 #ifdef USE_LONG_FNAME |
6355 && USE_LONG_FNAME | |
6356 #endif | |
6357 ) | |
6358 { | |
1620 | 6359 STRMOVE(e + 1, e); |
7 | 6360 *e = '.'; |
6361 } | |
6362 #endif | |
6363 | |
6364 /* | |
6365 * Check that, after appending the extension, the file name is really | |
6366 * different. | |
6367 */ | |
6368 if (fname != NULL && STRCMP(fname, retval) == 0) | |
6369 { | |
6370 /* we search for a character that can be replaced by '_' */ | |
6371 while (--s >= ptr) | |
6372 { | |
6373 if (*s != '_') | |
6374 { | |
6375 *s = '_'; | |
6376 break; | |
6377 } | |
6378 } | |
6379 if (s < ptr) /* fname was "________.<ext>", how tricky! */ | |
6380 *ptr = 'v'; | |
6381 } | |
6382 return retval; | |
6383 } | |
6384 | |
6385 /* | |
6386 * Like fgets(), but if the file line is too long, it is truncated and the | |
6387 * rest of the line is thrown away. Returns TRUE for end-of-file. | |
6388 */ | |
6389 int | |
6390 vim_fgets(buf, size, fp) | |
6391 char_u *buf; | |
6392 int size; | |
6393 FILE *fp; | |
6394 { | |
6395 char *eof; | |
6396 #define FGETS_SIZE 200 | |
6397 char tbuf[FGETS_SIZE]; | |
6398 | |
6399 buf[size - 2] = NUL; | |
6400 #ifdef USE_CR | |
6401 eof = fgets_cr((char *)buf, size, fp); | |
6402 #else | |
6403 eof = fgets((char *)buf, size, fp); | |
6404 #endif | |
6405 if (buf[size - 2] != NUL && buf[size - 2] != '\n') | |
6406 { | |
6407 buf[size - 1] = NUL; /* Truncate the line */ | |
6408 | |
6409 /* Now throw away the rest of the line: */ | |
6410 do | |
6411 { | |
6412 tbuf[FGETS_SIZE - 2] = NUL; | |
6413 #ifdef USE_CR | |
1757 | 6414 ignoredp = fgets_cr((char *)tbuf, FGETS_SIZE, fp); |
7 | 6415 #else |
1757 | 6416 ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp); |
7 | 6417 #endif |
6418 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n'); | |
6419 } | |
6420 return (eof == NULL); | |
6421 } | |
6422 | |
6423 #if defined(USE_CR) || defined(PROTO) | |
6424 /* | |
6425 * Like vim_fgets(), but accept any line terminator: CR, CR-LF or LF. | |
6426 * Returns TRUE for end-of-file. | |
6427 * Only used for the Mac, because it's much slower than vim_fgets(). | |
6428 */ | |
6429 int | |
6430 tag_fgets(buf, size, fp) | |
6431 char_u *buf; | |
6432 int size; | |
6433 FILE *fp; | |
6434 { | |
6435 int i = 0; | |
6436 int c; | |
6437 int eof = FALSE; | |
6438 | |
6439 for (;;) | |
6440 { | |
6441 c = fgetc(fp); | |
6442 if (c == EOF) | |
6443 { | |
6444 eof = TRUE; | |
6445 break; | |
6446 } | |
6447 if (c == '\r') | |
6448 { | |
6449 /* Always store a NL for end-of-line. */ | |
6450 if (i < size - 1) | |
6451 buf[i++] = '\n'; | |
6452 c = fgetc(fp); | |
6453 if (c != '\n') /* Macintosh format: single CR. */ | |
6454 ungetc(c, fp); | |
6455 break; | |
6456 } | |
6457 if (i < size - 1) | |
6458 buf[i++] = c; | |
6459 if (c == '\n') | |
6460 break; | |
6461 } | |
6462 buf[i] = NUL; | |
6463 return eof; | |
6464 } | |
6465 #endif | |
6466 | |
6467 /* | |
6468 * rename() only works if both files are on the same file system, this | |
6469 * function will (attempts to?) copy the file across if rename fails -- webb | |
6470 * Return -1 for failure, 0 for success. | |
6471 */ | |
6472 int | |
6473 vim_rename(from, to) | |
6474 char_u *from; | |
6475 char_u *to; | |
6476 { | |
6477 int fd_in; | |
6478 int fd_out; | |
6479 int n; | |
6480 char *errmsg = NULL; | |
6481 char *buffer; | |
6482 #ifdef AMIGA | |
6483 BPTR flock; | |
6484 #endif | |
6485 struct stat st; | |
194 | 6486 long perm; |
199 | 6487 #ifdef HAVE_ACL |
6488 vim_acl_T acl; /* ACL from original file */ | |
6489 #endif | |
1779 | 6490 int use_tmp_file = FALSE; |
7 | 6491 |
6492 /* | |
1779 | 6493 * When the names are identical, there is nothing to do. When they refer |
6494 * to the same file (ignoring case and slash/backslash differences) but | |
6495 * the file name differs we need to go through a temp file. | |
7 | 6496 */ |
6497 if (fnamecmp(from, to) == 0) | |
1779 | 6498 { |
4242 | 6499 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0) |
1779 | 6500 use_tmp_file = TRUE; |
6501 else | |
6502 return 0; | |
6503 } | |
7 | 6504 |
6505 /* | |
6506 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted. | |
6507 */ | |
6508 if (mch_stat((char *)from, &st) < 0) | |
6509 return -1; | |
6510 | |
1778 | 6511 #ifdef UNIX |
6512 { | |
6513 struct stat st_to; | |
6514 | |
6515 /* It's possible for the source and destination to be the same file. | |
6516 * This happens when "from" and "to" differ in case and are on a FAT32 | |
6517 * filesystem. In that case go through a temp file name. */ | |
6518 if (mch_stat((char *)to, &st_to) >= 0 | |
6519 && st.st_dev == st_to.st_dev | |
6520 && st.st_ino == st_to.st_ino) | |
1779 | 6521 use_tmp_file = TRUE; |
6522 } | |
6523 #endif | |
2793 | 6524 #ifdef WIN3264 |
6525 { | |
6526 BY_HANDLE_FILE_INFORMATION info1, info2; | |
6527 | |
6528 /* It's possible for the source and destination to be the same file. | |
6529 * In that case go through a temp file name. This makes rename("foo", | |
6530 * "./foo") a no-op (in a complicated way). */ | |
6531 if (win32_fileinfo(from, &info1) == FILEINFO_OK | |
6532 && win32_fileinfo(to, &info2) == FILEINFO_OK | |
6533 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber | |
6534 && info1.nFileIndexHigh == info2.nFileIndexHigh | |
6535 && info1.nFileIndexLow == info2.nFileIndexLow) | |
6536 use_tmp_file = TRUE; | |
6537 } | |
6538 #endif | |
1779 | 6539 |
6540 if (use_tmp_file) | |
6541 { | |
6542 char tempname[MAXPATHL + 1]; | |
6543 | |
6544 /* | |
6545 * Find a name that doesn't exist and is in the same directory. | |
6546 * Rename "from" to "tempname" and then rename "tempname" to "to". | |
6547 */ | |
6548 if (STRLEN(from) >= MAXPATHL - 5) | |
6549 return -1; | |
6550 STRCPY(tempname, from); | |
6551 for (n = 123; n < 99999; ++n) | |
6552 { | |
6553 sprintf((char *)gettail((char_u *)tempname), "%d", n); | |
6554 if (mch_stat(tempname, &st) < 0) | |
6555 { | |
6556 if (mch_rename((char *)from, tempname) == 0) | |
1778 | 6557 { |
1779 | 6558 if (mch_rename(tempname, (char *)to) == 0) |
6559 return 0; | |
6560 /* Strange, the second step failed. Try moving the | |
6561 * file back and return failure. */ | |
6562 mch_rename(tempname, (char *)from); | |
1778 | 6563 return -1; |
6564 } | |
1779 | 6565 /* If it fails for one temp name it will most likely fail |
6566 * for any temp name, give up. */ | |
6567 return -1; | |
6568 } | |
6569 } | |
6570 return -1; | |
1778 | 6571 } |
6572 | |
7 | 6573 /* |
6574 * Delete the "to" file, this is required on some systems to make the | |
6575 * mch_rename() work, on other systems it makes sure that we don't have | |
6576 * two files when the mch_rename() fails. | |
6577 */ | |
6578 | |
6579 #ifdef AMIGA | |
6580 /* | |
6581 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible | |
6582 * that the name of the "to" file is the same as the "from" file, even | |
1201 | 6583 * though the names are different. To avoid the chance of accidentally |
7 | 6584 * deleting the "from" file (horror!) we lock it during the remove. |
6585 * | |
6586 * When used for making a backup before writing the file: This should not | |
6587 * happen with ":w", because startscript() should detect this problem and | |
6588 * set buf->b_shortname, causing modname() to return a correct ".bak" file | |
6589 * name. This problem does exist with ":w filename", but then the | |
6590 * original file will be somewhere else so the backup isn't really | |
6591 * important. If autoscripting is off the rename may fail. | |
6592 */ | |
6593 flock = Lock((UBYTE *)from, (long)ACCESS_READ); | |
6594 #endif | |
6595 mch_remove(to); | |
6596 #ifdef AMIGA | |
6597 if (flock) | |
6598 UnLock(flock); | |
6599 #endif | |
6600 | |
6601 /* | |
6602 * First try a normal rename, return if it works. | |
6603 */ | |
6604 if (mch_rename((char *)from, (char *)to) == 0) | |
6605 return 0; | |
6606 | |
6607 /* | |
6608 * Rename() failed, try copying the file. | |
6609 */ | |
194 | 6610 perm = mch_getperm(from); |
199 | 6611 #ifdef HAVE_ACL |
6612 /* For systems that support ACL: get the ACL from the original file. */ | |
6613 acl = mch_get_acl(from); | |
6614 #endif | |
7 | 6615 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0); |
6616 if (fd_in == -1) | |
1651 | 6617 { |
6618 #ifdef HAVE_ACL | |
6619 mch_free_acl(acl); | |
6620 #endif | |
7 | 6621 return -1; |
1651 | 6622 } |
194 | 6623 |
6624 /* Create the new file with same permissions as the original. */ | |
557 | 6625 fd_out = mch_open((char *)to, |
6626 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm); | |
7 | 6627 if (fd_out == -1) |
6628 { | |
6629 close(fd_in); | |
1651 | 6630 #ifdef HAVE_ACL |
6631 mch_free_acl(acl); | |
6632 #endif | |
7 | 6633 return -1; |
6634 } | |
6635 | |
6636 buffer = (char *)alloc(BUFSIZE); | |
6637 if (buffer == NULL) | |
6638 { | |
1651 | 6639 close(fd_out); |
7 | 6640 close(fd_in); |
1651 | 6641 #ifdef HAVE_ACL |
6642 mch_free_acl(acl); | |
6643 #endif | |
7 | 6644 return -1; |
6645 } | |
6646 | |
2664 | 6647 while ((n = read_eintr(fd_in, buffer, BUFSIZE)) > 0) |
6648 if (write_eintr(fd_out, buffer, n) != n) | |
7 | 6649 { |
6650 errmsg = _("E208: Error writing to \"%s\""); | |
6651 break; | |
6652 } | |
6653 | |
6654 vim_free(buffer); | |
6655 close(fd_in); | |
6656 if (close(fd_out) < 0) | |
6657 errmsg = _("E209: Error closing \"%s\""); | |
6658 if (n < 0) | |
6659 { | |
6660 errmsg = _("E210: Error reading \"%s\""); | |
6661 to = from; | |
6662 } | |
1201 | 6663 #ifndef UNIX /* for Unix mch_open() already set the permission */ |
194 | 6664 mch_setperm(to, perm); |
570 | 6665 #endif |
199 | 6666 #ifdef HAVE_ACL |
6667 mch_set_acl(to, acl); | |
1651 | 6668 mch_free_acl(acl); |
199 | 6669 #endif |
5788 | 6670 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
5483 | 6671 mch_copy_sec(from, to); |
5479 | 6672 #endif |
7 | 6673 if (errmsg != NULL) |
6674 { | |
6675 EMSG2(errmsg, to); | |
6676 return -1; | |
6677 } | |
6678 mch_remove(from); | |
6679 return 0; | |
6680 } | |
6681 | |
6682 static int already_warned = FALSE; | |
6683 | |
6684 /* | |
6685 * Check if any not hidden buffer has been changed. | |
6686 * Postpone the check if there are characters in the stuff buffer, a global | |
6687 * command is being executed, a mapping is being executed or an autocommand is | |
6688 * busy. | |
6689 * Returns TRUE if some message was written (screen should be redrawn and | |
6690 * cursor positioned). | |
6691 */ | |
6692 int | |
6693 check_timestamps(focus) | |
6694 int focus; /* called for GUI focus event */ | |
6695 { | |
6696 buf_T *buf; | |
6697 int didit = 0; | |
6698 int n; | |
6699 | |
6700 /* Don't check timestamps while system() or another low-level function may | |
6701 * cause us to lose and gain focus. */ | |
6702 if (no_check_timestamps > 0) | |
6703 return FALSE; | |
6704 | |
6705 /* Avoid doing a check twice. The OK/Reload dialog can cause a focus | |
6706 * event and we would keep on checking if the file is steadily growing. | |
6707 * Do check again after typing something. */ | |
6708 if (focus && did_check_timestamps) | |
6709 { | |
6710 need_check_timestamps = TRUE; | |
6711 return FALSE; | |
6712 } | |
6713 | |
6714 if (!stuff_empty() || global_busy || !typebuf_typed() | |
6715 #ifdef FEAT_AUTOCMD | |
1834 | 6716 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0 |
7 | 6717 #endif |
6718 ) | |
6719 need_check_timestamps = TRUE; /* check later */ | |
6720 else | |
6721 { | |
6722 ++no_wait_return; | |
6723 did_check_timestamps = TRUE; | |
6724 already_warned = FALSE; | |
6725 for (buf = firstbuf; buf != NULL; ) | |
6726 { | |
6727 /* Only check buffers in a window. */ | |
6728 if (buf->b_nwindows > 0) | |
6729 { | |
6730 n = buf_check_timestamp(buf, focus); | |
6731 if (didit < n) | |
6732 didit = n; | |
6733 if (n > 0 && !buf_valid(buf)) | |
6734 { | |
6735 /* Autocommands have removed the buffer, start at the | |
6736 * first one again. */ | |
6737 buf = firstbuf; | |
6738 continue; | |
6739 } | |
6740 } | |
6741 buf = buf->b_next; | |
6742 } | |
6743 --no_wait_return; | |
6744 need_check_timestamps = FALSE; | |
6745 if (need_wait_return && didit == 2) | |
6746 { | |
6747 /* make sure msg isn't overwritten */ | |
6748 msg_puts((char_u *)"\n"); | |
6749 out_flush(); | |
6750 } | |
6751 } | |
6752 return didit; | |
6753 } | |
6754 | |
6755 /* | |
6756 * Move all the lines from buffer "frombuf" to buffer "tobuf". | |
6757 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not | |
6758 * empty. | |
6759 */ | |
6760 static int | |
6761 move_lines(frombuf, tobuf) | |
6762 buf_T *frombuf; | |
6763 buf_T *tobuf; | |
6764 { | |
6765 buf_T *tbuf = curbuf; | |
6766 int retval = OK; | |
6767 linenr_T lnum; | |
6768 char_u *p; | |
6769 | |
6770 /* Copy the lines in "frombuf" to "tobuf". */ | |
6771 curbuf = tobuf; | |
6772 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum) | |
6773 { | |
6774 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE)); | |
6775 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL) | |
6776 { | |
6777 vim_free(p); | |
6778 retval = FAIL; | |
6779 break; | |
6780 } | |
6781 vim_free(p); | |
6782 } | |
6783 | |
6784 /* Delete all the lines in "frombuf". */ | |
6785 if (retval != FAIL) | |
6786 { | |
6787 curbuf = frombuf; | |
1055 | 6788 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum) |
6789 if (ml_delete(lnum, FALSE) == FAIL) | |
7 | 6790 { |
6791 /* Oops! We could try putting back the saved lines, but that | |
6792 * might fail again... */ | |
6793 retval = FAIL; | |
6794 break; | |
6795 } | |
6796 } | |
6797 | |
6798 curbuf = tbuf; | |
6799 return retval; | |
6800 } | |
6801 | |
6802 /* | |
6803 * Check if buffer "buf" has been changed. | |
6804 * Also check if the file for a new buffer unexpectedly appeared. | |
6805 * return 1 if a changed buffer was found. | |
6806 * return 2 if a message has been displayed. | |
6807 * return 0 otherwise. | |
6808 */ | |
6809 int | |
6810 buf_check_timestamp(buf, focus) | |
6811 buf_T *buf; | |
1877 | 6812 int focus UNUSED; /* called for GUI focus event */ |
7 | 6813 { |
6814 struct stat st; | |
6815 int stat_res; | |
6816 int retval = 0; | |
6817 char_u *path; | |
6818 char_u *tbuf; | |
6819 char *mesg = NULL; | |
188 | 6820 char *mesg2 = ""; |
7 | 6821 int helpmesg = FALSE; |
6822 int reload = FALSE; | |
6823 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
6824 int can_reload = FALSE; | |
6825 #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
|
6826 off_t orig_size = buf->b_orig_size; |
7 | 6827 int orig_mode = buf->b_orig_mode; |
6828 #ifdef FEAT_GUI | |
6829 int save_mouse_correct = need_mouse_correct; | |
6830 #endif | |
6831 #ifdef FEAT_AUTOCMD | |
6832 static int busy = FALSE; | |
179 | 6833 int n; |
6834 char_u *s; | |
6835 #endif | |
6836 char *reason; | |
7 | 6837 |
6838 /* If there is no file name, the buffer is not loaded, 'buftype' is | |
6839 * set, we are in the middle of a save or being called recursively: ignore | |
6840 * this buffer. */ | |
6841 if (buf->b_ffname == NULL | |
6842 || buf->b_ml.ml_mfp == NULL | |
6843 #if defined(FEAT_QUICKFIX) | |
6844 || *buf->b_p_bt != NUL | |
6845 #endif | |
6846 || buf->b_saving | |
6847 #ifdef FEAT_AUTOCMD | |
6848 || busy | |
6849 #endif | |
33 | 6850 #ifdef FEAT_NETBEANS_INTG |
6851 || isNetbeansBuffer(buf) | |
6852 #endif | |
7 | 6853 ) |
6854 return 0; | |
6855 | |
6856 if ( !(buf->b_flags & BF_NOTEDITED) | |
6857 && buf->b_mtime != 0 | |
6858 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0 | |
6859 || time_differs((long)st.st_mtime, buf->b_mtime) | |
5863 | 6860 || st.st_size != buf->b_orig_size |
7 | 6861 #ifdef HAVE_ST_MODE |
6862 || (int)st.st_mode != buf->b_orig_mode | |
6863 #else | |
6864 || mch_getperm(buf->b_ffname) != buf->b_orig_mode | |
6865 #endif | |
6866 )) | |
6867 { | |
6868 retval = 1; | |
6869 | |
628 | 6870 /* set b_mtime to stop further warnings (e.g., when executing |
6871 * FileChangedShell autocmd) */ | |
7 | 6872 if (stat_res < 0) |
6873 { | |
6874 buf->b_mtime = 0; | |
6875 buf->b_orig_size = 0; | |
6876 buf->b_orig_mode = 0; | |
6877 } | |
6878 else | |
6879 buf_store_time(buf, &st, buf->b_ffname); | |
6880 | |
6881 /* Don't do anything for a directory. Might contain the file | |
6882 * explorer. */ | |
6883 if (mch_isdir(buf->b_fname)) | |
6884 ; | |
6885 | |
6886 /* | |
6887 * If 'autoread' is set, the buffer has no changes and the file still | |
6888 * exists, reload the buffer. Use the buffer-local option value if it | |
6889 * was set, the global option value otherwise. | |
6890 */ | |
6891 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar) | |
6892 && !bufIsChanged(buf) && stat_res >= 0) | |
6893 reload = TRUE; | |
6894 else | |
6895 { | |
179 | 6896 if (stat_res < 0) |
6897 reason = "deleted"; | |
6898 else if (bufIsChanged(buf)) | |
6899 reason = "conflict"; | |
6900 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf)) | |
6901 reason = "changed"; | |
6902 else if (orig_mode != buf->b_orig_mode) | |
6903 reason = "mode"; | |
6904 else | |
6905 reason = "time"; | |
6906 | |
7 | 6907 #ifdef FEAT_AUTOCMD |
6908 /* | |
6909 * Only give the warning if there are no FileChangedShell | |
6910 * autocommands. | |
6911 * Avoid being called recursively by setting "busy". | |
6912 */ | |
6913 busy = TRUE; | |
532 | 6914 # ifdef FEAT_EVAL |
179 | 6915 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1); |
6916 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1); | |
532 | 6917 # endif |
1834 | 6918 ++allbuf_lock; |
7 | 6919 n = apply_autocmds(EVENT_FILECHANGEDSHELL, |
6920 buf->b_fname, buf->b_fname, FALSE, buf); | |
1834 | 6921 --allbuf_lock; |
7 | 6922 busy = FALSE; |
6923 if (n) | |
6924 { | |
6925 if (!buf_valid(buf)) | |
6926 EMSG(_("E246: FileChangedShell autocommand deleted buffer")); | |
532 | 6927 # ifdef FEAT_EVAL |
179 | 6928 s = get_vim_var_str(VV_FCS_CHOICE); |
6929 if (STRCMP(s, "reload") == 0 && *reason != 'd') | |
6930 reload = TRUE; | |
6931 else if (STRCMP(s, "ask") == 0) | |
6932 n = FALSE; | |
6933 else | |
532 | 6934 # endif |
179 | 6935 return 2; |
7 | 6936 } |
179 | 6937 if (!n) |
7 | 6938 #endif |
6939 { | |
179 | 6940 if (*reason == 'd') |
6941 mesg = _("E211: File \"%s\" no longer available"); | |
7 | 6942 else |
6943 { | |
6944 helpmesg = TRUE; | |
6945 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
6946 can_reload = TRUE; | |
6947 #endif | |
6948 /* | |
6949 * Check if the file contents really changed to avoid | |
6950 * giving a warning when only the timestamp was set (e.g., | |
6951 * checked out of CVS). Always warn when the buffer was | |
6952 * changed. | |
6953 */ | |
179 | 6954 if (reason[2] == 'n') |
6955 { | |
7 | 6956 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well"); |
179 | 6957 mesg2 = _("See \":help W12\" for more info."); |
6958 } | |
6959 else if (reason[1] == 'h') | |
6960 { | |
7 | 6961 mesg = _("W11: Warning: File \"%s\" has changed since editing started"); |
179 | 6962 mesg2 = _("See \":help W11\" for more info."); |
6963 } | |
6964 else if (*reason == 'm') | |
6965 { | |
7 | 6966 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started"); |
179 | 6967 mesg2 = _("See \":help W16\" for more info."); |
6968 } | |
1913 | 6969 else |
6970 /* Only timestamp changed, store it to avoid a warning | |
6971 * in check_mtime() later. */ | |
6972 buf->b_mtime_read = buf->b_mtime; | |
7 | 6973 } |
6974 } | |
6975 } | |
6976 | |
6977 } | |
6978 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W) | |
6979 && vim_fexists(buf->b_ffname)) | |
6980 { | |
6981 retval = 1; | |
6982 mesg = _("W13: Warning: File \"%s\" has been created after editing started"); | |
6983 buf->b_flags |= BF_NEW_W; | |
6984 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
6985 can_reload = TRUE; | |
6986 #endif | |
6987 } | |
6988 | |
6989 if (mesg != NULL) | |
6990 { | |
6991 path = home_replace_save(buf, buf->b_fname); | |
6992 if (path != NULL) | |
6993 { | |
179 | 6994 if (!helpmesg) |
7 | 6995 mesg2 = ""; |
6996 tbuf = alloc((unsigned)(STRLEN(path) + STRLEN(mesg) | |
6997 + STRLEN(mesg2) + 2)); | |
6998 sprintf((char *)tbuf, mesg, path); | |
1848 | 6999 #ifdef FEAT_EVAL |
7000 /* Set warningmsg here, before the unimportant and output-specific | |
7001 * mesg2 has been appended. */ | |
7002 set_vim_var_string(VV_WARNINGMSG, tbuf, -1); | |
7003 #endif | |
7 | 7004 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
7005 if (can_reload) | |
7006 { | |
7007 if (*mesg2 != NUL) | |
7008 { | |
7009 STRCAT(tbuf, "\n"); | |
7010 STRCAT(tbuf, mesg2); | |
7011 } | |
7012 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"), tbuf, | |
2684 | 7013 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2) |
7 | 7014 reload = TRUE; |
7015 } | |
7016 else | |
7017 #endif | |
7018 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned) | |
7019 { | |
7020 if (*mesg2 != NUL) | |
7021 { | |
7022 STRCAT(tbuf, "; "); | |
7023 STRCAT(tbuf, mesg2); | |
7024 } | |
7025 EMSG(tbuf); | |
7026 retval = 2; | |
7027 } | |
7028 else | |
7029 { | |
8 | 7030 # ifdef FEAT_AUTOCMD |
7 | 7031 if (!autocmd_busy) |
8 | 7032 # endif |
7 | 7033 { |
7034 msg_start(); | |
7035 msg_puts_attr(tbuf, hl_attr(HLF_E) + MSG_HIST); | |
7036 if (*mesg2 != NUL) | |
7037 msg_puts_attr((char_u *)mesg2, | |
7038 hl_attr(HLF_W) + MSG_HIST); | |
7039 msg_clr_eos(); | |
7040 (void)msg_end(); | |
7041 if (emsg_silent == 0) | |
7042 { | |
7043 out_flush(); | |
8 | 7044 # ifdef FEAT_GUI |
7 | 7045 if (!focus) |
8 | 7046 # endif |
7 | 7047 /* give the user some time to think about it */ |
7048 ui_delay(1000L, TRUE); | |
7049 | |
7050 /* don't redraw and erase the message */ | |
7051 redraw_cmdline = FALSE; | |
7052 } | |
7053 } | |
7054 already_warned = TRUE; | |
7055 } | |
7056 | |
7057 vim_free(path); | |
7058 vim_free(tbuf); | |
7059 } | |
7060 } | |
7061 | |
7062 if (reload) | |
3776 | 7063 { |
311 | 7064 /* Reload the buffer. */ |
628 | 7065 buf_reload(buf, orig_mode); |
3776 | 7066 #ifdef FEAT_PERSISTENT_UNDO |
7067 if (buf->b_p_udf && buf->b_ffname != NULL) | |
7068 { | |
7069 char_u hash[UNDO_HASH_SIZE]; | |
7070 buf_T *save_curbuf = curbuf; | |
7071 | |
7072 /* Any existing undo file is unusable, write it now. */ | |
7073 curbuf = buf; | |
7074 u_compute_hash(hash); | |
7075 u_write_undo(NULL, FALSE, buf, hash); | |
7076 curbuf = save_curbuf; | |
7077 } | |
7078 #endif | |
7079 } | |
7 | 7080 |
765 | 7081 #ifdef FEAT_AUTOCMD |
1620 | 7082 /* Trigger FileChangedShell when the file was changed in any way. */ |
7083 if (buf_valid(buf) && retval != 0) | |
765 | 7084 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, |
7085 buf->b_fname, buf->b_fname, FALSE, buf); | |
7086 #endif | |
7 | 7087 #ifdef FEAT_GUI |
7088 /* restore this in case an autocommand has set it; it would break | |
7089 * 'mousefocus' */ | |
7090 need_mouse_correct = save_mouse_correct; | |
7091 #endif | |
7092 | |
7093 return retval; | |
7094 } | |
7095 | |
311 | 7096 /* |
7097 * Reload a buffer that is already loaded. | |
7098 * Used when the file was changed outside of Vim. | |
628 | 7099 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected. |
7100 * buf->b_orig_mode may have been reset already. | |
311 | 7101 */ |
7102 void | |
628 | 7103 buf_reload(buf, orig_mode) |
311 | 7104 buf_T *buf; |
628 | 7105 int orig_mode; |
311 | 7106 { |
7107 exarg_T ea; | |
7108 pos_T old_cursor; | |
7109 linenr_T old_topline; | |
7110 int old_ro = buf->b_p_ro; | |
7111 buf_T *savebuf; | |
7112 int saved = OK; | |
7113 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
|
7114 int flags = READ_NEW; |
311 | 7115 |
7116 /* set curwin/curbuf for "buf" and save some things */ | |
7117 aucmd_prepbuf(&aco, buf); | |
7118 | |
7119 /* We only want to read the text from the file, not reset the syntax | |
7120 * highlighting, clear marks, diff status, etc. Force the fileformat | |
7121 * and encoding to be the same. */ | |
7122 if (prep_exarg(&ea, buf) == OK) | |
7123 { | |
7124 old_cursor = curwin->w_cursor; | |
7125 old_topline = curwin->w_topline; | |
7126 | |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
7127 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
|
7128 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7129 /* 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
|
7130 * 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
|
7131 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
|
7132 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
|
7133 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
|
7134 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7135 |
311 | 7136 /* |
7137 * To behave like when a new file is edited (matters for | |
7138 * BufReadPost autocommands) we first need to delete the current | |
7139 * buffer contents. But if reading the file fails we should keep | |
7140 * the old contents. Can't use memory only, the file might be | |
7141 * too big. Use a hidden buffer to move the buffer contents to. | |
7142 */ | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7143 if (bufempty() || saved == FAIL) |
311 | 7144 savebuf = NULL; |
7145 else | |
7146 { | |
7147 /* Allocate a buffer without putting it in the buffer list. */ | |
7148 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); | |
837 | 7149 if (savebuf != NULL && buf == curbuf) |
311 | 7150 { |
7151 /* Open the memline. */ | |
7152 curbuf = savebuf; | |
7153 curwin->w_buffer = savebuf; | |
625 | 7154 saved = ml_open(curbuf); |
311 | 7155 curbuf = buf; |
7156 curwin->w_buffer = buf; | |
7157 } | |
837 | 7158 if (savebuf == NULL || saved == FAIL || buf != curbuf |
311 | 7159 || move_lines(buf, savebuf) == FAIL) |
7160 { | |
7161 EMSG2(_("E462: Could not prepare for reloading \"%s\""), | |
7162 buf->b_fname); | |
7163 saved = FAIL; | |
7164 } | |
7165 } | |
7166 | |
7167 if (saved == OK) | |
7168 { | |
7169 curbuf->b_flags |= BF_CHECK_RO; /* check for RO again */ | |
7170 #ifdef FEAT_AUTOCMD | |
7171 keep_filetype = TRUE; /* don't detect 'filetype' */ | |
7172 #endif | |
7173 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, | |
7174 (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
|
7175 (linenr_T)MAXLNUM, &ea, flags) == FAIL) |
311 | 7176 { |
7177 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
7178 if (!aborting()) | |
7179 #endif | |
7180 EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname); | |
837 | 7181 if (savebuf != NULL && buf_valid(savebuf) && buf == curbuf) |
311 | 7182 { |
7183 /* Put the text back from the save buffer. First | |
7184 * delete any lines that readfile() added. */ | |
7185 while (!bufempty()) | |
837 | 7186 if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL) |
311 | 7187 break; |
7188 (void)move_lines(savebuf, buf); | |
7189 } | |
7190 } | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7191 else if (buf == curbuf) /* "buf" still valid */ |
311 | 7192 { |
7193 /* Mark the buffer as unmodified and free undo info. */ | |
7194 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
|
7195 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
|
7196 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7197 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
|
7198 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
|
7199 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7200 else |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
7201 { |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7202 /* 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
|
7203 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
|
7204 } |
311 | 7205 } |
7206 } | |
7207 vim_free(ea.cmd); | |
7208 | |
837 | 7209 if (savebuf != NULL && buf_valid(savebuf)) |
311 | 7210 wipe_buffer(savebuf, FALSE); |
7211 | |
7212 #ifdef FEAT_DIFF | |
7213 /* Invalidate diff info if necessary. */ | |
837 | 7214 diff_invalidate(curbuf); |
311 | 7215 #endif |
7216 | |
7217 /* Restore the topline and cursor position and check it (lines may | |
7218 * have been removed). */ | |
7219 if (old_topline > curbuf->b_ml.ml_line_count) | |
7220 curwin->w_topline = curbuf->b_ml.ml_line_count; | |
7221 else | |
7222 curwin->w_topline = old_topline; | |
7223 curwin->w_cursor = old_cursor; | |
7224 check_cursor(); | |
7225 update_topline(); | |
7226 #ifdef FEAT_AUTOCMD | |
7227 keep_filetype = FALSE; | |
7228 #endif | |
7229 #ifdef FEAT_FOLDING | |
7230 { | |
1863 | 7231 win_T *wp; |
7232 tabpage_T *tp; | |
311 | 7233 |
7234 /* Update folds unless they are defined manually. */ | |
1863 | 7235 FOR_ALL_TAB_WINDOWS(tp, wp) |
311 | 7236 if (wp->w_buffer == curwin->w_buffer |
7237 && !foldmethodIsManual(wp)) | |
7238 foldUpdateAll(wp); | |
7239 } | |
7240 #endif | |
7241 /* If the mode didn't change and 'readonly' was set, keep the old | |
7242 * value; the user probably used the ":view" command. But don't | |
7243 * reset it, might have had a read error. */ | |
7244 if (orig_mode == curbuf->b_orig_mode) | |
7245 curbuf->b_p_ro |= old_ro; | |
4071 | 7246 |
7247 /* Modelines must override settings done by autocommands. */ | |
7248 do_modelines(0); | |
311 | 7249 } |
7250 | |
7251 /* restore curwin/curbuf and a few other things */ | |
7252 aucmd_restbuf(&aco); | |
7253 /* Careful: autocommands may have made "buf" invalid! */ | |
7254 } | |
7255 | |
7 | 7256 void |
7257 buf_store_time(buf, st, fname) | |
7258 buf_T *buf; | |
7259 struct stat *st; | |
1877 | 7260 char_u *fname UNUSED; |
7 | 7261 { |
7262 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
|
7263 buf->b_orig_size = st->st_size; |
7 | 7264 #ifdef HAVE_ST_MODE |
7265 buf->b_orig_mode = (int)st->st_mode; | |
7266 #else | |
7267 buf->b_orig_mode = mch_getperm(fname); | |
7268 #endif | |
7269 } | |
7270 | |
7271 /* | |
7272 * Adjust the line with missing eol, used for the next write. | |
7273 * Used for do_filter(), when the input lines for the filter are deleted. | |
7274 */ | |
7275 void | |
7276 write_lnum_adjust(offset) | |
7277 linenr_T offset; | |
7278 { | |
2707 | 7279 if (curbuf->b_no_eol_lnum != 0) /* only if there is a missing eol */ |
7280 curbuf->b_no_eol_lnum += offset; | |
7 | 7281 } |
7282 | |
7283 #if defined(TEMPDIRNAMES) || defined(PROTO) | |
7284 static long temp_count = 0; /* Temp filename counter. */ | |
7285 | |
7286 /* | |
7287 * Delete the temp directory and all files it contains. | |
7288 */ | |
7289 void | |
7290 vim_deltempdir() | |
7291 { | |
7292 char_u **files; | |
7293 int file_count; | |
7294 int i; | |
7295 | |
7296 if (vim_tempdir != NULL) | |
7297 { | |
7298 sprintf((char *)NameBuff, "%s*", vim_tempdir); | |
7299 if (gen_expand_wildcards(1, &NameBuff, &file_count, &files, | |
7300 EW_DIR|EW_FILE|EW_SILENT) == OK) | |
7301 { | |
7302 for (i = 0; i < file_count; ++i) | |
7303 mch_remove(files[i]); | |
7304 FreeWild(file_count, files); | |
7305 } | |
7306 gettail(NameBuff)[-1] = NUL; | |
7307 (void)mch_rmdir(NameBuff); | |
7308 | |
7309 vim_free(vim_tempdir); | |
7310 vim_tempdir = NULL; | |
7311 } | |
7312 } | |
7313 #endif | |
7314 | |
2006 | 7315 #ifdef TEMPDIRNAMES |
7 | 7316 /* |
1997 | 7317 * Directory "tempdir" was created. Expand this name to a full path and put |
7318 * it in "vim_tempdir". This avoids that using ":cd" would confuse us. | |
7319 * "tempdir" must be no longer than MAXPATHL. | |
7320 */ | |
7321 static void | |
7322 vim_settempdir(tempdir) | |
7323 char_u *tempdir; | |
7324 { | |
7325 char_u *buf; | |
7326 | |
7327 buf = alloc((unsigned)MAXPATHL + 2); | |
7328 if (buf != NULL) | |
7329 { | |
7330 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL) | |
7331 STRCPY(buf, tempdir); | |
7332 # ifdef __EMX__ | |
7333 if (vim_strchr(buf, '/') != NULL) | |
7334 STRCAT(buf, "/"); | |
7335 else | |
7336 # endif | |
7337 add_pathsep(buf); | |
7338 vim_tempdir = vim_strsave(buf); | |
7339 vim_free(buf); | |
7340 } | |
7341 } | |
2006 | 7342 #endif |
1997 | 7343 |
7344 /* | |
7 | 7345 * vim_tempname(): Return a unique name that can be used for a temp file. |
7346 * | |
7347 * The temp file is NOT created. | |
7348 * | |
7349 * The returned pointer is to allocated memory. | |
7350 * The returned pointer is NULL if no valid name was found. | |
7351 */ | |
7352 char_u * | |
7353 vim_tempname(extra_char) | |
1877 | 7354 int extra_char UNUSED; /* char to use in the name instead of '?' */ |
7 | 7355 { |
7356 #ifdef USE_TMPNAM | |
7357 char_u itmp[L_tmpnam]; /* use tmpnam() */ | |
7358 #else | |
7359 char_u itmp[TEMPNAMELEN]; | |
7360 #endif | |
7361 | |
7362 #ifdef TEMPDIRNAMES | |
7363 static char *(tempdirs[]) = {TEMPDIRNAMES}; | |
7364 int i; | |
7365 # ifndef EEXIST | |
7366 struct stat st; | |
7367 # endif | |
7368 | |
7369 /* | |
7370 * This will create a directory for private use by this instance of Vim. | |
7371 * This is done once, and the same directory is used for all temp files. | |
7372 * This method avoids security problems because of symlink attacks et al. | |
7373 * It's also a bit faster, because we only need to check for an existing | |
7374 * file when creating the directory and not for each temp file. | |
7375 */ | |
7376 if (vim_tempdir == NULL) | |
7377 { | |
7378 /* | |
7379 * Try the entries in TEMPDIRNAMES to create the temp directory. | |
7380 */ | |
1877 | 7381 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i) |
7 | 7382 { |
2047
85da03763130
updated for version 7.2.333
Bram Moolenaar <bram@zimbu.org>
parents:
2028
diff
changeset
|
7383 # ifndef HAVE_MKDTEMP |
1997 | 7384 size_t itmplen; |
7385 long nr; | |
7386 long off; | |
7387 # endif | |
7388 | |
7 | 7389 /* expand $TMP, leave room for "/v1100000/999999999" */ |
7390 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20); | |
7391 if (mch_isdir(itmp)) /* directory exists */ | |
7392 { | |
7393 # ifdef __EMX__ | |
7394 /* If $TMP contains a forward slash (perhaps using bash or | |
7395 * tcsh), don't add a backslash, use a forward slash! | |
7396 * Adding 2 backslashes didn't work. */ | |
7397 if (vim_strchr(itmp, '/') != NULL) | |
7398 STRCAT(itmp, "/"); | |
7399 else | |
7400 # endif | |
7401 add_pathsep(itmp); | |
1997 | 7402 |
7403 # ifdef HAVE_MKDTEMP | |
7404 /* Leave room for filename */ | |
7405 STRCAT(itmp, "vXXXXXX"); | |
7406 if (mkdtemp((char *)itmp) != NULL) | |
7407 vim_settempdir(itmp); | |
7408 # else | |
7 | 7409 /* Get an arbitrary number of up to 6 digits. When it's |
7410 * unlikely that it already exists it will be faster, | |
7411 * otherwise it doesn't matter. The use of mkdir() avoids any | |
7412 * security problems because of the predictable number. */ | |
7413 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
|
7414 itmplen = STRLEN(itmp); |
7 | 7415 |
7416 /* Try up to 10000 different values until we find a name that | |
7417 * doesn't exist. */ | |
7418 for (off = 0; off < 10000L; ++off) | |
7419 { | |
7420 int r; | |
1997 | 7421 # if defined(UNIX) || defined(VMS) |
7 | 7422 mode_t umask_save; |
1997 | 7423 # endif |
7424 | |
7425 sprintf((char *)itmp + itmplen, "v%ld", nr + off); | |
7426 # ifndef EEXIST | |
7 | 7427 /* If mkdir() does not set errno to EEXIST, check for |
7428 * existing file here. There is a race condition then, | |
7429 * although it's fail-safe. */ | |
7430 if (mch_stat((char *)itmp, &st) >= 0) | |
7431 continue; | |
1997 | 7432 # endif |
7433 # if defined(UNIX) || defined(VMS) | |
7 | 7434 /* Make sure the umask doesn't remove the executable bit. |
7435 * "repl" has been reported to use "177". */ | |
7436 umask_save = umask(077); | |
1997 | 7437 # endif |
7 | 7438 r = vim_mkdir(itmp, 0700); |
1997 | 7439 # if defined(UNIX) || defined(VMS) |
7 | 7440 (void)umask(umask_save); |
1997 | 7441 # endif |
7 | 7442 if (r == 0) |
7443 { | |
1997 | 7444 vim_settempdir(itmp); |
7 | 7445 break; |
7446 } | |
1997 | 7447 # ifdef EEXIST |
7 | 7448 /* If the mkdir() didn't fail because the file/dir exists, |
7449 * we probably can't create any dir here, try another | |
7450 * place. */ | |
7451 if (errno != EEXIST) | |
1997 | 7452 # endif |
7 | 7453 break; |
7454 } | |
1997 | 7455 # endif /* HAVE_MKDTEMP */ |
7 | 7456 if (vim_tempdir != NULL) |
7457 break; | |
7458 } | |
7459 } | |
7460 } | |
7461 | |
7462 if (vim_tempdir != NULL) | |
7463 { | |
7464 /* There is no need to check if the file exists, because we own the | |
7465 * directory and nobody else creates a file in it. */ | |
7466 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++); | |
7467 return vim_strsave(itmp); | |
7468 } | |
7469 | |
7470 return NULL; | |
7471 | |
7472 #else /* TEMPDIRNAMES */ | |
7473 | |
7474 # ifdef WIN3264 | |
7475 char szTempFile[_MAX_PATH + 1]; | |
7476 char buf4[4]; | |
7477 char_u *retval; | |
7478 char_u *p; | |
7479 | |
7480 STRCPY(itmp, ""); | |
7481 if (GetTempPath(_MAX_PATH, szTempFile) == 0) | |
2695 | 7482 { |
7483 szTempFile[0] = '.'; /* GetTempPath() failed, use current dir */ | |
7484 szTempFile[1] = NUL; | |
7485 } | |
7 | 7486 strcpy(buf4, "VIM"); |
7487 buf4[2] = extra_char; /* make it "VIa", "VIb", etc. */ | |
7488 if (GetTempFileName(szTempFile, buf4, 0, itmp) == 0) | |
7489 return NULL; | |
7490 /* GetTempFileName() will create the file, we don't want that */ | |
7491 (void)DeleteFile(itmp); | |
7492 | |
7493 /* Backslashes in a temp file name cause problems when filtering with | |
7494 * "sh". NOTE: This also checks 'shellcmdflag' to help those people who | |
7495 * didn't set 'shellslash'. */ | |
7496 retval = vim_strsave(itmp); | |
7497 if (*p_shcf == '-' || p_ssl) | |
7498 for (p = retval; *p; ++p) | |
7499 if (*p == '\\') | |
7500 *p = '/'; | |
7501 return retval; | |
7502 | |
7503 # else /* WIN3264 */ | |
7504 | |
7505 # ifdef USE_TMPNAM | |
2697 | 7506 char_u *p; |
7507 | |
7 | 7508 /* tmpnam() will make its own name */ |
2697 | 7509 p = tmpnam((char *)itmp); |
7510 if (p == NULL || *p == NUL) | |
7 | 7511 return NULL; |
7512 # else | |
7513 char_u *p; | |
7514 | |
7515 # ifdef VMS_TEMPNAM | |
7516 /* mktemp() is not working on VMS. It seems to be | |
7517 * a do-nothing function. Therefore we use tempnam(). | |
7518 */ | |
7519 sprintf((char *)itmp, "VIM%c", extra_char); | |
7520 p = (char_u *)tempnam("tmp:", (char *)itmp); | |
7521 if (p != NULL) | |
7522 { | |
5704 | 7523 /* VMS will use '.LIS' if we don't explicitly specify an extension, |
7 | 7524 * and VIM will then be unable to find the file later */ |
7525 STRCPY(itmp, p); | |
7526 STRCAT(itmp, ".txt"); | |
7527 free(p); | |
7528 } | |
7529 else | |
7530 return NULL; | |
7531 # else | |
7532 STRCPY(itmp, TEMPNAME); | |
7533 if ((p = vim_strchr(itmp, '?')) != NULL) | |
7534 *p = extra_char; | |
7535 if (mktemp((char *)itmp) == NULL) | |
7536 return NULL; | |
7537 # endif | |
7538 # endif | |
7539 | |
7540 return vim_strsave(itmp); | |
7541 # endif /* WIN3264 */ | |
7542 #endif /* TEMPDIRNAMES */ | |
7543 } | |
7544 | |
7545 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) | |
7546 /* | |
7547 * Convert all backslashes in fname to forward slashes in-place. | |
7548 */ | |
7549 void | |
7550 forward_slash(fname) | |
7551 char_u *fname; | |
7552 { | |
7553 char_u *p; | |
7554 | |
7555 for (p = fname; *p != NUL; ++p) | |
7556 # ifdef FEAT_MBYTE | |
7557 /* The Big5 encoding can have '\' in the trail byte. */ | |
474 | 7558 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 7559 ++p; |
7560 else | |
7561 # endif | |
7562 if (*p == '\\') | |
7563 *p = '/'; | |
7564 } | |
7565 #endif | |
7566 | |
7567 | |
7568 /* | |
7569 * Code for automatic commands. | |
7570 * | |
7571 * Only included when "FEAT_AUTOCMD" has been defined. | |
7572 */ | |
7573 | |
7574 #if defined(FEAT_AUTOCMD) || defined(PROTO) | |
7575 | |
7576 /* | |
7577 * The autocommands are stored in a list for each event. | |
7578 * Autocommands for the same pattern, that are consecutive, are joined | |
7579 * together, to avoid having to match the pattern too often. | |
7580 * The result is an array of Autopat lists, which point to AutoCmd lists: | |
7581 * | |
7582 * first_autopat[0] --> Autopat.next --> Autopat.next --> NULL | |
7583 * Autopat.cmds Autopat.cmds | |
7584 * | | | |
7585 * V V | |
7586 * AutoCmd.next AutoCmd.next | |
7587 * | | | |
7588 * V V | |
7589 * AutoCmd.next NULL | |
7590 * | | |
7591 * V | |
7592 * NULL | |
7593 * | |
7594 * first_autopat[1] --> Autopat.next --> NULL | |
7595 * Autopat.cmds | |
7596 * | | |
7597 * V | |
7598 * AutoCmd.next | |
7599 * | | |
7600 * V | |
7601 * NULL | |
7602 * etc. | |
7603 * | |
7604 * The order of AutoCmds is important, this is the order in which they were | |
7605 * defined and will have to be executed. | |
7606 */ | |
7607 typedef struct AutoCmd | |
7608 { | |
7609 char_u *cmd; /* The command to be executed (NULL | |
7610 when command has been removed) */ | |
7611 char nested; /* If autocommands nest here */ | |
7612 char last; /* last command in list */ | |
7613 #ifdef FEAT_EVAL | |
7614 scid_T scriptID; /* script ID where defined */ | |
7615 #endif | |
7616 struct AutoCmd *next; /* Next AutoCmd in list */ | |
7617 } AutoCmd; | |
7618 | |
7619 typedef struct AutoPat | |
7620 { | |
7621 char_u *pat; /* pattern as typed (NULL when pattern | |
7622 has been removed) */ | |
4861
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7623 regprog_T *reg_prog; /* compiled regprog for pattern */ |
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7624 AutoCmd *cmds; /* list of commands to do */ |
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7625 struct AutoPat *next; /* next AutoPat in AutoPat list */ |
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7626 int group; /* group ID */ |
7 | 7627 int patlen; /* strlen() of pat */ |
4861
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7628 int buflocal_nr; /* !=0 for buffer-local AutoPat */ |
7 | 7629 char allow_dirs; /* Pattern may match whole path */ |
7630 char last; /* last pattern for apply_autocmds() */ | |
7631 } AutoPat; | |
7632 | |
7633 static struct event_name | |
7634 { | |
7635 char *name; /* event name */ | |
661 | 7636 event_T event; /* event number */ |
7 | 7637 } event_names[] = |
7638 { | |
7639 {"BufAdd", EVENT_BUFADD}, | |
7640 {"BufCreate", EVENT_BUFADD}, | |
7641 {"BufDelete", EVENT_BUFDELETE}, | |
7642 {"BufEnter", EVENT_BUFENTER}, | |
7643 {"BufFilePost", EVENT_BUFFILEPOST}, | |
7644 {"BufFilePre", EVENT_BUFFILEPRE}, | |
7645 {"BufHidden", EVENT_BUFHIDDEN}, | |
7646 {"BufLeave", EVENT_BUFLEAVE}, | |
7647 {"BufNew", EVENT_BUFNEW}, | |
7648 {"BufNewFile", EVENT_BUFNEWFILE}, | |
7649 {"BufRead", EVENT_BUFREADPOST}, | |
7650 {"BufReadCmd", EVENT_BUFREADCMD}, | |
7651 {"BufReadPost", EVENT_BUFREADPOST}, | |
7652 {"BufReadPre", EVENT_BUFREADPRE}, | |
7653 {"BufUnload", EVENT_BUFUNLOAD}, | |
7654 {"BufWinEnter", EVENT_BUFWINENTER}, | |
7655 {"BufWinLeave", EVENT_BUFWINLEAVE}, | |
7656 {"BufWipeout", EVENT_BUFWIPEOUT}, | |
7657 {"BufWrite", EVENT_BUFWRITEPRE}, | |
7658 {"BufWritePost", EVENT_BUFWRITEPOST}, | |
7659 {"BufWritePre", EVENT_BUFWRITEPRE}, | |
7660 {"BufWriteCmd", EVENT_BUFWRITECMD}, | |
7661 {"CmdwinEnter", EVENT_CMDWINENTER}, | |
7662 {"CmdwinLeave", EVENT_CMDWINLEAVE}, | |
6154 | 7663 {"CmdUndefined", EVENT_CMDUNDEFINED}, |
12 | 7664 {"ColorScheme", EVENT_COLORSCHEME}, |
3676 | 7665 {"CompleteDone", EVENT_COMPLETEDONE}, |
661 | 7666 {"CursorHold", EVENT_CURSORHOLD}, |
7667 {"CursorHoldI", EVENT_CURSORHOLDI}, | |
7668 {"CursorMoved", EVENT_CURSORMOVED}, | |
7669 {"CursorMovedI", EVENT_CURSORMOVEDI}, | |
7 | 7670 {"EncodingChanged", EVENT_ENCODINGCHANGED}, |
7671 {"FileEncoding", EVENT_ENCODINGCHANGED}, | |
7672 {"FileAppendPost", EVENT_FILEAPPENDPOST}, | |
7673 {"FileAppendPre", EVENT_FILEAPPENDPRE}, | |
7674 {"FileAppendCmd", EVENT_FILEAPPENDCMD}, | |
7675 {"FileChangedShell",EVENT_FILECHANGEDSHELL}, | |
765 | 7676 {"FileChangedShellPost",EVENT_FILECHANGEDSHELLPOST}, |
7 | 7677 {"FileChangedRO", EVENT_FILECHANGEDRO}, |
7678 {"FileReadPost", EVENT_FILEREADPOST}, | |
7679 {"FileReadPre", EVENT_FILEREADPRE}, | |
7680 {"FileReadCmd", EVENT_FILEREADCMD}, | |
7681 {"FileType", EVENT_FILETYPE}, | |
7682 {"FileWritePost", EVENT_FILEWRITEPOST}, | |
7683 {"FileWritePre", EVENT_FILEWRITEPRE}, | |
7684 {"FileWriteCmd", EVENT_FILEWRITECMD}, | |
7685 {"FilterReadPost", EVENT_FILTERREADPOST}, | |
7686 {"FilterReadPre", EVENT_FILTERREADPRE}, | |
7687 {"FilterWritePost", EVENT_FILTERWRITEPOST}, | |
7688 {"FilterWritePre", EVENT_FILTERWRITEPRE}, | |
7689 {"FocusGained", EVENT_FOCUSGAINED}, | |
7690 {"FocusLost", EVENT_FOCUSLOST}, | |
7691 {"FuncUndefined", EVENT_FUNCUNDEFINED}, | |
7692 {"GUIEnter", EVENT_GUIENTER}, | |
946 | 7693 {"GUIFailed", EVENT_GUIFAILED}, |
11 | 7694 {"InsertChange", EVENT_INSERTCHANGE}, |
7695 {"InsertEnter", EVENT_INSERTENTER}, | |
7696 {"InsertLeave", EVENT_INSERTLEAVE}, | |
2845 | 7697 {"InsertCharPre", EVENT_INSERTCHARPRE}, |
434 | 7698 {"MenuPopup", EVENT_MENUPOPUP}, |
161 | 7699 {"QuickFixCmdPost", EVENT_QUICKFIXCMDPOST}, |
7700 {"QuickFixCmdPre", EVENT_QUICKFIXCMDPRE}, | |
3568 | 7701 {"QuitPre", EVENT_QUITPRE}, |
7 | 7702 {"RemoteReply", EVENT_REMOTEREPLY}, |
574 | 7703 {"SessionLoadPost", EVENT_SESSIONLOADPOST}, |
723 | 7704 {"ShellCmdPost", EVENT_SHELLCMDPOST}, |
7705 {"ShellFilterPost", EVENT_SHELLFILTERPOST}, | |
715 | 7706 {"SourcePre", EVENT_SOURCEPRE}, |
1061 | 7707 {"SourceCmd", EVENT_SOURCECMD}, |
649 | 7708 {"SpellFileMissing",EVENT_SPELLFILEMISSING}, |
7 | 7709 {"StdinReadPost", EVENT_STDINREADPOST}, |
7710 {"StdinReadPre", EVENT_STDINREADPRE}, | |
674 | 7711 {"SwapExists", EVENT_SWAPEXISTS}, |
7 | 7712 {"Syntax", EVENT_SYNTAX}, |
676 | 7713 {"TabEnter", EVENT_TABENTER}, |
7714 {"TabLeave", EVENT_TABLEAVE}, | |
7 | 7715 {"TermChanged", EVENT_TERMCHANGED}, |
7716 {"TermResponse", EVENT_TERMRESPONSE}, | |
4232 | 7717 {"TextChanged", EVENT_TEXTCHANGED}, |
7718 {"TextChangedI", EVENT_TEXTCHANGEDI}, | |
7 | 7719 {"User", EVENT_USER}, |
7720 {"VimEnter", EVENT_VIMENTER}, | |
7721 {"VimLeave", EVENT_VIMLEAVE}, | |
7722 {"VimLeavePre", EVENT_VIMLEAVEPRE}, | |
7723 {"WinEnter", EVENT_WINENTER}, | |
7724 {"WinLeave", EVENT_WINLEAVE}, | |
765 | 7725 {"VimResized", EVENT_VIMRESIZED}, |
661 | 7726 {NULL, (event_T)0} |
7 | 7727 }; |
7728 | |
7729 static AutoPat *first_autopat[NUM_EVENTS] = | |
7730 { | |
7731 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7732 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7733 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7734 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
18 | 7735 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
7736 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL | |
7 | 7737 }; |
7738 | |
7739 /* | |
7740 * struct used to keep status while executing autocommands for an event. | |
7741 */ | |
7742 typedef struct AutoPatCmd | |
7743 { | |
7744 AutoPat *curpat; /* next AutoPat to examine */ | |
7745 AutoCmd *nextcmd; /* next AutoCmd to execute */ | |
7746 int group; /* group being used */ | |
7747 char_u *fname; /* fname to match with */ | |
7748 char_u *sfname; /* sfname to match with */ | |
7749 char_u *tail; /* tail of fname */ | |
661 | 7750 event_T event; /* current event */ |
40 | 7751 int arg_bufnr; /* initially equal to <abuf>, set to zero when |
7752 buf is deleted */ | |
7753 struct AutoPatCmd *next; /* chain of active apc-s for auto-invalidation*/ | |
7 | 7754 } AutoPatCmd; |
7755 | |
297 | 7756 static AutoPatCmd *active_apc_list = NULL; /* stack of active autocommands */ |
40 | 7757 |
7 | 7758 /* |
7759 * augroups stores a list of autocmd group names. | |
7760 */ | |
297 | 7761 static garray_T augroups = {0, 0, sizeof(char_u *), 10, NULL}; |
7 | 7762 #define AUGROUP_NAME(i) (((char_u **)augroups.ga_data)[i]) |
7763 | |
7764 /* | |
7765 * The ID of the current group. Group 0 is the default one. | |
7766 */ | |
7767 static int current_augroup = AUGROUP_DEFAULT; | |
7768 | |
7769 static int au_need_clean = FALSE; /* need to delete marked patterns */ | |
7770 | |
661 | 7771 static void show_autocmd __ARGS((AutoPat *ap, event_T event)); |
7 | 7772 static void au_remove_pat __ARGS((AutoPat *ap)); |
7773 static void au_remove_cmds __ARGS((AutoPat *ap)); | |
7774 static void au_cleanup __ARGS((void)); | |
7775 static int au_new_group __ARGS((char_u *name)); | |
7776 static void au_del_group __ARGS((char_u *name)); | |
661 | 7777 static event_T event_name2nr __ARGS((char_u *start, char_u **end)); |
7778 static char_u *event_nr2name __ARGS((event_T event)); | |
7 | 7779 static char_u *find_end_event __ARGS((char_u *arg, int have_group)); |
661 | 7780 static int event_ignored __ARGS((event_T event)); |
7 | 7781 static int au_get_grouparg __ARGS((char_u **argp)); |
661 | 7782 static int do_autocmd_event __ARGS((event_T event, char_u *pat, int nested, char_u *cmd, int forceit, int group)); |
7783 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 | 7784 static void auto_next_pat __ARGS((AutoPatCmd *apc, int stop_at_last)); |
6375 | 7785 #if defined(FEAT_AUTOCMD) || defined(FEAT_WILDIGN) |
7786 static int match_file_pat __ARGS((char_u *pattern, regprog_T **prog, char_u *fname, char_u *sfname, char_u *tail, int allow_dirs)); | |
7787 #endif | |
7 | 7788 |
40 | 7789 |
661 | 7790 static event_T last_event; |
7 | 7791 static int last_group; |
1410 | 7792 static int autocmd_blocked = 0; /* block all autocmds */ |
7 | 7793 |
7794 /* | |
7795 * Show the autocommands for one AutoPat. | |
7796 */ | |
7797 static void | |
7798 show_autocmd(ap, event) | |
7799 AutoPat *ap; | |
661 | 7800 event_T event; |
7 | 7801 { |
7802 AutoCmd *ac; | |
7803 | |
7804 /* Check for "got_int" (here and at various places below), which is set | |
7805 * when "q" has been hit for the "--more--" prompt */ | |
7806 if (got_int) | |
7807 return; | |
7808 if (ap->pat == NULL) /* pattern has been removed */ | |
7809 return; | |
7810 | |
7811 msg_putchar('\n'); | |
7812 if (got_int) | |
7813 return; | |
7814 if (event != last_event || ap->group != last_group) | |
7815 { | |
7816 if (ap->group != AUGROUP_DEFAULT) | |
7817 { | |
7818 if (AUGROUP_NAME(ap->group) == NULL) | |
7819 msg_puts_attr((char_u *)_("--Deleted--"), hl_attr(HLF_E)); | |
7820 else | |
7821 msg_puts_attr(AUGROUP_NAME(ap->group), hl_attr(HLF_T)); | |
7822 msg_puts((char_u *)" "); | |
7823 } | |
7824 msg_puts_attr(event_nr2name(event), hl_attr(HLF_T)); | |
7825 last_event = event; | |
7826 last_group = ap->group; | |
7827 msg_putchar('\n'); | |
7828 if (got_int) | |
7829 return; | |
7830 } | |
7831 msg_col = 4; | |
7832 msg_outtrans(ap->pat); | |
7833 | |
7834 for (ac = ap->cmds; ac != NULL; ac = ac->next) | |
7835 { | |
7836 if (ac->cmd != NULL) /* skip removed commands */ | |
7837 { | |
7838 if (msg_col >= 14) | |
7839 msg_putchar('\n'); | |
7840 msg_col = 14; | |
7841 if (got_int) | |
7842 return; | |
7843 msg_outtrans(ac->cmd); | |
500 | 7844 #ifdef FEAT_EVAL |
7845 if (p_verbose > 0) | |
7846 last_set_msg(ac->scriptID); | |
7847 #endif | |
7 | 7848 if (got_int) |
7849 return; | |
7850 if (ac->next != NULL) | |
7851 { | |
7852 msg_putchar('\n'); | |
7853 if (got_int) | |
7854 return; | |
7855 } | |
7856 } | |
7857 } | |
7858 } | |
7859 | |
7860 /* | |
7861 * Mark an autocommand pattern for deletion. | |
7862 */ | |
7863 static void | |
7864 au_remove_pat(ap) | |
7865 AutoPat *ap; | |
7866 { | |
7867 vim_free(ap->pat); | |
7868 ap->pat = NULL; | |
40 | 7869 ap->buflocal_nr = -1; |
7 | 7870 au_need_clean = TRUE; |
7871 } | |
7872 | |
7873 /* | |
7874 * Mark all commands for a pattern for deletion. | |
7875 */ | |
7876 static void | |
7877 au_remove_cmds(ap) | |
7878 AutoPat *ap; | |
7879 { | |
7880 AutoCmd *ac; | |
7881 | |
7882 for (ac = ap->cmds; ac != NULL; ac = ac->next) | |
7883 { | |
7884 vim_free(ac->cmd); | |
7885 ac->cmd = NULL; | |
7886 } | |
7887 au_need_clean = TRUE; | |
7888 } | |
7889 | |
7890 /* | |
7891 * Cleanup autocommands and patterns that have been deleted. | |
7892 * This is only done when not executing autocommands. | |
7893 */ | |
7894 static void | |
7895 au_cleanup() | |
7896 { | |
7897 AutoPat *ap, **prev_ap; | |
7898 AutoCmd *ac, **prev_ac; | |
661 | 7899 event_T event; |
7 | 7900 |
7901 if (autocmd_busy || !au_need_clean) | |
7902 return; | |
7903 | |
7904 /* loop over all events */ | |
661 | 7905 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
7906 event = (event_T)((int)event + 1)) | |
7 | 7907 { |
7908 /* loop over all autocommand patterns */ | |
7909 prev_ap = &(first_autopat[(int)event]); | |
7910 for (ap = *prev_ap; ap != NULL; ap = *prev_ap) | |
7911 { | |
7912 /* loop over all commands for this pattern */ | |
7913 prev_ac = &(ap->cmds); | |
7914 for (ac = *prev_ac; ac != NULL; ac = *prev_ac) | |
7915 { | |
7916 /* remove the command if the pattern is to be deleted or when | |
7917 * the command has been marked for deletion */ | |
7918 if (ap->pat == NULL || ac->cmd == NULL) | |
7919 { | |
7920 *prev_ac = ac->next; | |
7921 vim_free(ac->cmd); | |
7922 vim_free(ac); | |
7923 } | |
7924 else | |
7925 prev_ac = &(ac->next); | |
7926 } | |
7927 | |
7928 /* remove the pattern if it has been marked for deletion */ | |
7929 if (ap->pat == NULL) | |
7930 { | |
7931 *prev_ap = ap->next; | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4354
diff
changeset
|
7932 vim_regfree(ap->reg_prog); |
7 | 7933 vim_free(ap); |
7934 } | |
7935 else | |
7936 prev_ap = &(ap->next); | |
7937 } | |
7938 } | |
7939 | |
7940 au_need_clean = FALSE; | |
7941 } | |
7942 | |
7943 /* | |
40 | 7944 * Called when buffer is freed, to remove/invalidate related buffer-local |
7945 * autocmds. | |
7946 */ | |
7947 void | |
7948 aubuflocal_remove(buf) | |
7949 buf_T *buf; | |
7950 { | |
7951 AutoPat *ap; | |
661 | 7952 event_T event; |
40 | 7953 AutoPatCmd *apc; |
7954 | |
7955 /* invalidate currently executing autocommands */ | |
7956 for (apc = active_apc_list; apc; apc = apc->next) | |
7957 if (buf->b_fnum == apc->arg_bufnr) | |
7958 apc->arg_bufnr = 0; | |
7959 | |
7960 /* invalidate buflocals looping through events */ | |
661 | 7961 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
7962 event = (event_T)((int)event + 1)) | |
40 | 7963 /* loop over all autocommand patterns */ |
7964 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
7965 if (ap->buflocal_nr == buf->b_fnum) | |
7966 { | |
7967 au_remove_pat(ap); | |
7968 if (p_verbose >= 6) | |
292 | 7969 { |
7970 verbose_enter(); | |
40 | 7971 smsg((char_u *) |
7972 _("auto-removing autocommand: %s <buffer=%d>"), | |
7973 event_nr2name(event), buf->b_fnum); | |
292 | 7974 verbose_leave(); |
7975 } | |
40 | 7976 } |
7977 au_cleanup(); | |
7978 } | |
7979 | |
7980 /* | |
7 | 7981 * Add an autocmd group name. |
7982 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. | |
7983 */ | |
7984 static int | |
7985 au_new_group(name) | |
7986 char_u *name; | |
7987 { | |
7988 int i; | |
7989 | |
7990 i = au_find_group(name); | |
7991 if (i == AUGROUP_ERROR) /* the group doesn't exist yet, add it */ | |
7992 { | |
7993 /* First try using a free entry. */ | |
7994 for (i = 0; i < augroups.ga_len; ++i) | |
7995 if (AUGROUP_NAME(i) == NULL) | |
7996 break; | |
7997 if (i == augroups.ga_len && ga_grow(&augroups, 1) == FAIL) | |
7998 return AUGROUP_ERROR; | |
7999 | |
8000 AUGROUP_NAME(i) = vim_strsave(name); | |
8001 if (AUGROUP_NAME(i) == NULL) | |
8002 return AUGROUP_ERROR; | |
8003 if (i == augroups.ga_len) | |
8004 ++augroups.ga_len; | |
8005 } | |
8006 | |
8007 return i; | |
8008 } | |
8009 | |
8010 static void | |
8011 au_del_group(name) | |
8012 char_u *name; | |
8013 { | |
8014 int i; | |
8015 | |
8016 i = au_find_group(name); | |
8017 if (i == AUGROUP_ERROR) /* the group doesn't exist */ | |
8018 EMSG2(_("E367: No such group: \"%s\""), name); | |
8019 else | |
8020 { | |
8021 vim_free(AUGROUP_NAME(i)); | |
8022 AUGROUP_NAME(i) = NULL; | |
8023 } | |
8024 } | |
8025 | |
8026 /* | |
8027 * Find the ID of an autocmd group name. | |
8028 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. | |
8029 */ | |
8030 static int | |
8031 au_find_group(name) | |
8032 char_u *name; | |
8033 { | |
8034 int i; | |
8035 | |
8036 for (i = 0; i < augroups.ga_len; ++i) | |
8037 if (AUGROUP_NAME(i) != NULL && STRCMP(AUGROUP_NAME(i), name) == 0) | |
8038 return i; | |
8039 return AUGROUP_ERROR; | |
8040 } | |
8041 | |
461 | 8042 /* |
8043 * Return TRUE if augroup "name" exists. | |
8044 */ | |
8045 int | |
8046 au_has_group(name) | |
8047 char_u *name; | |
8048 { | |
8049 return au_find_group(name) != AUGROUP_ERROR; | |
8050 } | |
8051 | |
7 | 8052 /* |
8053 * ":augroup {name}". | |
8054 */ | |
8055 void | |
8056 do_augroup(arg, del_group) | |
8057 char_u *arg; | |
8058 int del_group; | |
8059 { | |
8060 int i; | |
8061 | |
8062 if (del_group) | |
8063 { | |
8064 if (*arg == NUL) | |
8065 EMSG(_(e_argreq)); | |
8066 else | |
8067 au_del_group(arg); | |
8068 } | |
8069 else if (STRICMP(arg, "end") == 0) /* ":aug end": back to group 0 */ | |
8070 current_augroup = AUGROUP_DEFAULT; | |
8071 else if (*arg) /* ":aug xxx": switch to group xxx */ | |
8072 { | |
8073 i = au_new_group(arg); | |
8074 if (i != AUGROUP_ERROR) | |
8075 current_augroup = i; | |
8076 } | |
8077 else /* ":aug": list the group names */ | |
8078 { | |
8079 msg_start(); | |
8080 for (i = 0; i < augroups.ga_len; ++i) | |
8081 { | |
8082 if (AUGROUP_NAME(i) != NULL) | |
8083 { | |
8084 msg_puts(AUGROUP_NAME(i)); | |
8085 msg_puts((char_u *)" "); | |
8086 } | |
8087 } | |
8088 msg_clr_eos(); | |
8089 msg_end(); | |
8090 } | |
8091 } | |
8092 | |
355 | 8093 #if defined(EXITFREE) || defined(PROTO) |
8094 void | |
8095 free_all_autocmds() | |
8096 { | |
8097 for (current_augroup = -1; current_augroup < augroups.ga_len; | |
8098 ++current_augroup) | |
8099 do_autocmd((char_u *)"", TRUE); | |
8100 ga_clear_strings(&augroups); | |
8101 } | |
8102 #endif | |
8103 | |
7 | 8104 /* |
8105 * Return the event number for event name "start". | |
8106 * Return NUM_EVENTS if the event name was not found. | |
8107 * Return a pointer to the next event name in "end". | |
8108 */ | |
661 | 8109 static event_T |
7 | 8110 event_name2nr(start, end) |
8111 char_u *start; | |
8112 char_u **end; | |
8113 { | |
8114 char_u *p; | |
8115 int i; | |
8116 int len; | |
8117 | |
8118 /* the event name ends with end of line, a blank or a comma */ | |
8119 for (p = start; *p && !vim_iswhite(*p) && *p != ','; ++p) | |
8120 ; | |
8121 for (i = 0; event_names[i].name != NULL; ++i) | |
8122 { | |
8123 len = (int)STRLEN(event_names[i].name); | |
8124 if (len == p - start && STRNICMP(event_names[i].name, start, len) == 0) | |
8125 break; | |
8126 } | |
8127 if (*p == ',') | |
8128 ++p; | |
8129 *end = p; | |
8130 if (event_names[i].name == NULL) | |
8131 return NUM_EVENTS; | |
8132 return event_names[i].event; | |
8133 } | |
8134 | |
8135 /* | |
8136 * Return the name for event "event". | |
8137 */ | |
8138 static char_u * | |
8139 event_nr2name(event) | |
661 | 8140 event_T event; |
7 | 8141 { |
8142 int i; | |
8143 | |
8144 for (i = 0; event_names[i].name != NULL; ++i) | |
8145 if (event_names[i].event == event) | |
8146 return (char_u *)event_names[i].name; | |
8147 return (char_u *)"Unknown"; | |
8148 } | |
8149 | |
8150 /* | |
8151 * Scan over the events. "*" stands for all events. | |
8152 */ | |
8153 static char_u * | |
8154 find_end_event(arg, have_group) | |
8155 char_u *arg; | |
8156 int have_group; /* TRUE when group name was found */ | |
8157 { | |
8158 char_u *pat; | |
8159 char_u *p; | |
8160 | |
8161 if (*arg == '*') | |
8162 { | |
8163 if (arg[1] && !vim_iswhite(arg[1])) | |
8164 { | |
8165 EMSG2(_("E215: Illegal character after *: %s"), arg); | |
8166 return NULL; | |
8167 } | |
8168 pat = arg + 1; | |
8169 } | |
8170 else | |
8171 { | |
8172 for (pat = arg; *pat && !vim_iswhite(*pat); pat = p) | |
8173 { | |
8174 if ((int)event_name2nr(pat, &p) >= (int)NUM_EVENTS) | |
8175 { | |
8176 if (have_group) | |
8177 EMSG2(_("E216: No such event: %s"), pat); | |
8178 else | |
8179 EMSG2(_("E216: No such group or event: %s"), pat); | |
8180 return NULL; | |
8181 } | |
8182 } | |
8183 } | |
8184 return pat; | |
8185 } | |
8186 | |
8187 /* | |
8188 * Return TRUE if "event" is included in 'eventignore'. | |
8189 */ | |
8190 static int | |
8191 event_ignored(event) | |
661 | 8192 event_T event; |
7 | 8193 { |
8194 char_u *p = p_ei; | |
8195 | |
844 | 8196 while (*p != NUL) |
8197 { | |
8198 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) | |
8199 return TRUE; | |
7 | 8200 if (event_name2nr(p, &p) == event) |
8201 return TRUE; | |
844 | 8202 } |
7 | 8203 |
8204 return FALSE; | |
8205 } | |
8206 | |
8207 /* | |
8208 * Return OK when the contents of p_ei is valid, FAIL otherwise. | |
8209 */ | |
8210 int | |
8211 check_ei() | |
8212 { | |
8213 char_u *p = p_ei; | |
8214 | |
8215 while (*p) | |
844 | 8216 { |
8217 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) | |
8218 { | |
8219 p += 3; | |
8220 if (*p == ',') | |
8221 ++p; | |
8222 } | |
8223 else if (event_name2nr(p, &p) == NUM_EVENTS) | |
7 | 8224 return FAIL; |
844 | 8225 } |
7 | 8226 |
8227 return OK; | |
8228 } | |
8229 | |
123 | 8230 # if defined(FEAT_SYN_HL) || defined(PROTO) |
8231 | |
8232 /* | |
8233 * Add "what" to 'eventignore' to skip loading syntax highlighting for every | |
8234 * buffer loaded into the window. "what" must start with a comma. | |
8235 * Returns the old value of 'eventignore' in allocated memory. | |
8236 */ | |
8237 char_u * | |
8238 au_event_disable(what) | |
8239 char *what; | |
8240 { | |
8241 char_u *new_ei; | |
8242 char_u *save_ei; | |
8243 | |
8244 save_ei = vim_strsave(p_ei); | |
8245 if (save_ei != NULL) | |
8246 { | |
557 | 8247 new_ei = vim_strnsave(p_ei, (int)(STRLEN(p_ei) + STRLEN(what))); |
123 | 8248 if (new_ei != NULL) |
8249 { | |
2095
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8250 if (*what == ',' && *p_ei == NUL) |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8251 STRCPY(new_ei, what + 1); |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8252 else |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8253 STRCAT(new_ei, what); |
694 | 8254 set_string_option_direct((char_u *)"ei", -1, new_ei, |
8255 OPT_FREE, SID_NONE); | |
123 | 8256 vim_free(new_ei); |
8257 } | |
8258 } | |
8259 return save_ei; | |
8260 } | |
8261 | |
8262 void | |
8263 au_event_restore(old_ei) | |
8264 char_u *old_ei; | |
8265 { | |
8266 if (old_ei != NULL) | |
8267 { | |
694 | 8268 set_string_option_direct((char_u *)"ei", -1, old_ei, |
8269 OPT_FREE, SID_NONE); | |
123 | 8270 vim_free(old_ei); |
8271 } | |
8272 } | |
8273 # endif /* FEAT_SYN_HL */ | |
8274 | |
7 | 8275 /* |
8276 * do_autocmd() -- implements the :autocmd command. Can be used in the | |
8277 * following ways: | |
8278 * | |
8279 * :autocmd <event> <pat> <cmd> Add <cmd> to the list of commands that | |
8280 * will be automatically executed for <event> | |
8281 * when editing a file matching <pat>, in | |
8282 * the current group. | |
8283 * :autocmd <event> <pat> Show the auto-commands associated with | |
8284 * <event> and <pat>. | |
8285 * :autocmd <event> Show the auto-commands associated with | |
8286 * <event>. | |
8287 * :autocmd Show all auto-commands. | |
8288 * :autocmd! <event> <pat> <cmd> Remove all auto-commands associated with | |
8289 * <event> and <pat>, and add the command | |
8290 * <cmd>, for the current group. | |
8291 * :autocmd! <event> <pat> Remove all auto-commands associated with | |
8292 * <event> and <pat> for the current group. | |
8293 * :autocmd! <event> Remove all auto-commands associated with | |
8294 * <event> for the current group. | |
8295 * :autocmd! Remove ALL auto-commands for the current | |
8296 * group. | |
8297 * | |
8298 * Multiple events and patterns may be given separated by commas. Here are | |
8299 * some examples: | |
8300 * :autocmd bufread,bufenter *.c,*.h set tw=0 smartindent noic | |
8301 * :autocmd bufleave * set tw=79 nosmartindent ic infercase | |
8302 * | |
8303 * :autocmd * *.c show all autocommands for *.c files. | |
609 | 8304 * |
8305 * Mostly a {group} argument can optionally appear before <event>. | |
7 | 8306 */ |
8307 void | |
8308 do_autocmd(arg, forceit) | |
8309 char_u *arg; | |
8310 int forceit; | |
8311 { | |
8312 char_u *pat; | |
8313 char_u *envpat = NULL; | |
8314 char_u *cmd; | |
661 | 8315 event_T event; |
7 | 8316 int need_free = FALSE; |
8317 int nested = FALSE; | |
8318 int group; | |
8319 | |
8320 /* | |
8321 * Check for a legal group name. If not, use AUGROUP_ALL. | |
8322 */ | |
8323 group = au_get_grouparg(&arg); | |
8324 if (arg == NULL) /* out of memory */ | |
8325 return; | |
8326 | |
8327 /* | |
8328 * Scan over the events. | |
8329 * If we find an illegal name, return here, don't do anything. | |
8330 */ | |
8331 pat = find_end_event(arg, group != AUGROUP_ALL); | |
8332 if (pat == NULL) | |
8333 return; | |
8334 | |
8335 /* | |
8336 * Scan over the pattern. Put a NUL at the end. | |
8337 */ | |
8338 pat = skipwhite(pat); | |
8339 cmd = pat; | |
8340 while (*cmd && (!vim_iswhite(*cmd) || cmd[-1] == '\\')) | |
8341 cmd++; | |
8342 if (*cmd) | |
8343 *cmd++ = NUL; | |
8344 | |
8345 /* Expand environment variables in the pattern. Set 'shellslash', we want | |
8346 * forward slashes here. */ | |
8347 if (vim_strchr(pat, '$') != NULL || vim_strchr(pat, '~') != NULL) | |
8348 { | |
8349 #ifdef BACKSLASH_IN_FILENAME | |
8350 int p_ssl_save = p_ssl; | |
8351 | |
8352 p_ssl = TRUE; | |
8353 #endif | |
8354 envpat = expand_env_save(pat); | |
8355 #ifdef BACKSLASH_IN_FILENAME | |
8356 p_ssl = p_ssl_save; | |
8357 #endif | |
8358 if (envpat != NULL) | |
8359 pat = envpat; | |
8360 } | |
8361 | |
8362 /* | |
8363 * Check for "nested" flag. | |
8364 */ | |
8365 cmd = skipwhite(cmd); | |
8366 if (*cmd != NUL && STRNCMP(cmd, "nested", 6) == 0 && vim_iswhite(cmd[6])) | |
8367 { | |
8368 nested = TRUE; | |
8369 cmd = skipwhite(cmd + 6); | |
8370 } | |
8371 | |
8372 /* | |
8373 * Find the start of the commands. | |
8374 * Expand <sfile> in it. | |
8375 */ | |
8376 if (*cmd != NUL) | |
8377 { | |
8378 cmd = expand_sfile(cmd); | |
8379 if (cmd == NULL) /* some error */ | |
8380 return; | |
8381 need_free = TRUE; | |
8382 } | |
8383 | |
8384 /* | |
8385 * Print header when showing autocommands. | |
8386 */ | |
8387 if (!forceit && *cmd == NUL) | |
8388 { | |
8389 /* Highlight title */ | |
8390 MSG_PUTS_TITLE(_("\n--- Auto-Commands ---")); | |
8391 } | |
8392 | |
8393 /* | |
8394 * Loop over the events. | |
8395 */ | |
661 | 8396 last_event = (event_T)-1; /* for listing the event name */ |
7 | 8397 last_group = AUGROUP_ERROR; /* for listing the group name */ |
8398 if (*arg == '*' || *arg == NUL) | |
8399 { | |
661 | 8400 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
8401 event = (event_T)((int)event + 1)) | |
7 | 8402 if (do_autocmd_event(event, pat, |
8403 nested, cmd, forceit, group) == FAIL) | |
8404 break; | |
8405 } | |
8406 else | |
8407 { | |
8408 while (*arg && !vim_iswhite(*arg)) | |
8409 if (do_autocmd_event(event_name2nr(arg, &arg), pat, | |
8410 nested, cmd, forceit, group) == FAIL) | |
8411 break; | |
8412 } | |
8413 | |
8414 if (need_free) | |
8415 vim_free(cmd); | |
8416 vim_free(envpat); | |
8417 } | |
8418 | |
8419 /* | |
8420 * Find the group ID in a ":autocmd" or ":doautocmd" argument. | |
8421 * The "argp" argument is advanced to the following argument. | |
8422 * | |
8423 * Returns the group ID, AUGROUP_ERROR for error (out of memory). | |
8424 */ | |
8425 static int | |
8426 au_get_grouparg(argp) | |
8427 char_u **argp; | |
8428 { | |
8429 char_u *group_name; | |
8430 char_u *p; | |
8431 char_u *arg = *argp; | |
8432 int group = AUGROUP_ALL; | |
8433 | |
8434 p = skiptowhite(arg); | |
8435 if (p > arg) | |
8436 { | |
8437 group_name = vim_strnsave(arg, (int)(p - arg)); | |
8438 if (group_name == NULL) /* out of memory */ | |
8439 return AUGROUP_ERROR; | |
8440 group = au_find_group(group_name); | |
8441 if (group == AUGROUP_ERROR) | |
8442 group = AUGROUP_ALL; /* no match, use all groups */ | |
8443 else | |
8444 *argp = skipwhite(p); /* match, skip over group name */ | |
8445 vim_free(group_name); | |
8446 } | |
8447 return group; | |
8448 } | |
8449 | |
8450 /* | |
8451 * do_autocmd() for one event. | |
8452 * If *pat == NUL do for all patterns. | |
8453 * If *cmd == NUL show entries. | |
8454 * If forceit == TRUE delete entries. | |
8455 * If group is not AUGROUP_ALL, only use this group. | |
8456 */ | |
8457 static int | |
8458 do_autocmd_event(event, pat, nested, cmd, forceit, group) | |
661 | 8459 event_T event; |
7 | 8460 char_u *pat; |
8461 int nested; | |
8462 char_u *cmd; | |
8463 int forceit; | |
8464 int group; | |
8465 { | |
8466 AutoPat *ap; | |
8467 AutoPat **prev_ap; | |
8468 AutoCmd *ac; | |
8469 AutoCmd **prev_ac; | |
8470 int brace_level; | |
8471 char_u *endpat; | |
8472 int findgroup; | |
8473 int allgroups; | |
8474 int patlen; | |
856 | 8475 int is_buflocal; |
40 | 8476 int buflocal_nr; |
8477 char_u buflocal_pat[25]; /* for "<buffer=X>" */ | |
7 | 8478 |
8479 if (group == AUGROUP_ALL) | |
8480 findgroup = current_augroup; | |
8481 else | |
8482 findgroup = group; | |
8483 allgroups = (group == AUGROUP_ALL && !forceit && *cmd == NUL); | |
8484 | |
8485 /* | |
8486 * Show or delete all patterns for an event. | |
8487 */ | |
8488 if (*pat == NUL) | |
8489 { | |
8490 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
8491 { | |
8492 if (forceit) /* delete the AutoPat, if it's in the current group */ | |
8493 { | |
8494 if (ap->group == findgroup) | |
8495 au_remove_pat(ap); | |
8496 } | |
8497 else if (group == AUGROUP_ALL || ap->group == group) | |
8498 show_autocmd(ap, event); | |
8499 } | |
8500 } | |
8501 | |
8502 /* | |
8503 * Loop through all the specified patterns. | |
8504 */ | |
8505 for ( ; *pat; pat = (*endpat == ',' ? endpat + 1 : endpat)) | |
8506 { | |
8507 /* | |
8508 * Find end of the pattern. | |
8509 * Watch out for a comma in braces, like "*.\{obj,o\}". | |
8510 */ | |
8511 brace_level = 0; | |
8512 for (endpat = pat; *endpat && (*endpat != ',' || brace_level | |
8513 || endpat[-1] == '\\'); ++endpat) | |
8514 { | |
8515 if (*endpat == '{') | |
8516 brace_level++; | |
8517 else if (*endpat == '}') | |
8518 brace_level--; | |
8519 } | |
8520 if (pat == endpat) /* ignore single comma */ | |
8521 continue; | |
8522 patlen = (int)(endpat - pat); | |
8523 | |
8524 /* | |
40 | 8525 * detect special <buflocal[=X]> buffer-local patterns |
8526 */ | |
8527 is_buflocal = FALSE; | |
8528 buflocal_nr = 0; | |
8529 | |
8530 if (patlen >= 7 && STRNCMP(pat, "<buffer", 7) == 0 | |
8531 && pat[patlen - 1] == '>') | |
8532 { | |
8533 /* Error will be printed only for addition. printing and removing | |
8534 * will proceed silently. */ | |
8535 is_buflocal = TRUE; | |
8536 if (patlen == 8) | |
8537 buflocal_nr = curbuf->b_fnum; | |
8538 else if (patlen > 9 && pat[7] == '=') | |
8539 { | |
8540 /* <buffer=abuf> */ | |
8541 if (patlen == 13 && STRNICMP(pat, "<buffer=abuf>", 13)) | |
8542 buflocal_nr = autocmd_bufnr; | |
8543 /* <buffer=123> */ | |
8544 else if (skipdigits(pat + 8) == pat + patlen - 1) | |
8545 buflocal_nr = atoi((char *)pat + 8); | |
8546 } | |
8547 } | |
8548 | |
8549 if (is_buflocal) | |
8550 { | |
8551 /* normalize pat into standard "<buffer>#N" form */ | |
8552 sprintf((char *)buflocal_pat, "<buffer=%d>", buflocal_nr); | |
8553 pat = buflocal_pat; /* can modify pat and patlen */ | |
835 | 8554 patlen = (int)STRLEN(buflocal_pat); /* but not endpat */ |
40 | 8555 } |
8556 | |
8557 /* | |
7 | 8558 * Find AutoPat entries with this pattern. |
8559 */ | |
8560 prev_ap = &first_autopat[(int)event]; | |
8561 while ((ap = *prev_ap) != NULL) | |
8562 { | |
8563 if (ap->pat != NULL) | |
8564 { | |
8565 /* Accept a pattern when: | |
8566 * - a group was specified and it's that group, or a group was | |
8567 * not specified and it's the current group, or a group was | |
8568 * not specified and we are listing | |
8569 * - the length of the pattern matches | |
40 | 8570 * - the pattern matches. |
8571 * For <buffer[=X]>, this condition works because we normalize | |
8572 * all buffer-local patterns. | |
7 | 8573 */ |
8574 if ((allgroups || ap->group == findgroup) | |
8575 && ap->patlen == patlen | |
8576 && STRNCMP(pat, ap->pat, patlen) == 0) | |
8577 { | |
8578 /* | |
8579 * Remove existing autocommands. | |
8580 * If adding any new autocmd's for this AutoPat, don't | |
8581 * delete the pattern from the autopat list, append to | |
8582 * this list. | |
8583 */ | |
8584 if (forceit) | |
8585 { | |
8586 if (*cmd != NUL && ap->next == NULL) | |
8587 { | |
8588 au_remove_cmds(ap); | |
8589 break; | |
8590 } | |
8591 au_remove_pat(ap); | |
8592 } | |
8593 | |
8594 /* | |
40 | 8595 * Show autocmd's for this autopat, or buflocals <buffer=X> |
7 | 8596 */ |
8597 else if (*cmd == NUL) | |
8598 show_autocmd(ap, event); | |
8599 | |
8600 /* | |
8601 * Add autocmd to this autopat, if it's the last one. | |
8602 */ | |
8603 else if (ap->next == NULL) | |
8604 break; | |
8605 } | |
8606 } | |
8607 prev_ap = &ap->next; | |
8608 } | |
8609 | |
8610 /* | |
8611 * Add a new command. | |
8612 */ | |
8613 if (*cmd != NUL) | |
8614 { | |
8615 /* | |
8616 * If the pattern we want to add a command to does appear at the | |
8617 * end of the list (or not is not in the list at all), add the | |
8618 * pattern at the end of the list. | |
8619 */ | |
8620 if (ap == NULL) | |
8621 { | |
40 | 8622 /* refuse to add buffer-local ap if buffer number is invalid */ |
8623 if (is_buflocal && (buflocal_nr == 0 | |
8624 || buflist_findnr(buflocal_nr) == NULL)) | |
8625 { | |
8626 EMSGN(_("E680: <buffer=%d>: invalid buffer number "), | |
8627 buflocal_nr); | |
8628 return FAIL; | |
8629 } | |
8630 | |
7 | 8631 ap = (AutoPat *)alloc((unsigned)sizeof(AutoPat)); |
8632 if (ap == NULL) | |
8633 return FAIL; | |
8634 ap->pat = vim_strnsave(pat, patlen); | |
8635 ap->patlen = patlen; | |
8636 if (ap->pat == NULL) | |
8637 { | |
8638 vim_free(ap); | |
8639 return FAIL; | |
8640 } | |
40 | 8641 |
8642 if (is_buflocal) | |
8643 { | |
8644 ap->buflocal_nr = buflocal_nr; | |
153 | 8645 ap->reg_prog = NULL; |
40 | 8646 } |
8647 else | |
7 | 8648 { |
153 | 8649 char_u *reg_pat; |
8650 | |
40 | 8651 ap->buflocal_nr = 0; |
153 | 8652 reg_pat = file_pat_to_reg_pat(pat, endpat, |
40 | 8653 &ap->allow_dirs, TRUE); |
153 | 8654 if (reg_pat != NULL) |
8655 ap->reg_prog = vim_regcomp(reg_pat, RE_MAGIC); | |
355 | 8656 vim_free(reg_pat); |
153 | 8657 if (reg_pat == NULL || ap->reg_prog == NULL) |
40 | 8658 { |
8659 vim_free(ap->pat); | |
8660 vim_free(ap); | |
8661 return FAIL; | |
8662 } | |
7 | 8663 } |
8664 ap->cmds = NULL; | |
8665 *prev_ap = ap; | |
8666 ap->next = NULL; | |
8667 if (group == AUGROUP_ALL) | |
8668 ap->group = current_augroup; | |
8669 else | |
8670 ap->group = group; | |
8671 } | |
8672 | |
8673 /* | |
8674 * Add the autocmd at the end of the AutoCmd list. | |
8675 */ | |
8676 prev_ac = &(ap->cmds); | |
8677 while ((ac = *prev_ac) != NULL) | |
8678 prev_ac = &ac->next; | |
8679 ac = (AutoCmd *)alloc((unsigned)sizeof(AutoCmd)); | |
8680 if (ac == NULL) | |
8681 return FAIL; | |
8682 ac->cmd = vim_strsave(cmd); | |
8683 #ifdef FEAT_EVAL | |
8684 ac->scriptID = current_SID; | |
8685 #endif | |
8686 if (ac->cmd == NULL) | |
8687 { | |
8688 vim_free(ac); | |
8689 return FAIL; | |
8690 } | |
8691 ac->next = NULL; | |
8692 *prev_ac = ac; | |
8693 ac->nested = nested; | |
8694 } | |
8695 } | |
8696 | |
8697 au_cleanup(); /* may really delete removed patterns/commands now */ | |
8698 return OK; | |
8699 } | |
8700 | |
8701 /* | |
8702 * Implementation of ":doautocmd [group] event [fname]". | |
8703 * Return OK for success, FAIL for failure; | |
8704 */ | |
8705 int | |
8706 do_doautocmd(arg, do_msg) | |
8707 char_u *arg; | |
8708 int do_msg; /* give message for no matching autocmds? */ | |
8709 { | |
8710 char_u *fname; | |
8711 int nothing_done = TRUE; | |
8712 int group; | |
8713 | |
8714 /* | |
8715 * Check for a legal group name. If not, use AUGROUP_ALL. | |
8716 */ | |
8717 group = au_get_grouparg(&arg); | |
8718 if (arg == NULL) /* out of memory */ | |
8719 return FAIL; | |
8720 | |
8721 if (*arg == '*') | |
8722 { | |
8723 EMSG(_("E217: Can't execute autocommands for ALL events")); | |
8724 return FAIL; | |
8725 } | |
8726 | |
8727 /* | |
8728 * Scan over the events. | |
8729 * If we find an illegal name, return here, don't do anything. | |
8730 */ | |
8731 fname = find_end_event(arg, group != AUGROUP_ALL); | |
8732 if (fname == NULL) | |
8733 return FAIL; | |
8734 | |
8735 fname = skipwhite(fname); | |
8736 | |
8737 /* | |
8738 * Loop over the events. | |
8739 */ | |
8740 while (*arg && !vim_iswhite(*arg)) | |
8741 if (apply_autocmds_group(event_name2nr(arg, &arg), | |
8742 fname, NULL, TRUE, group, curbuf, NULL)) | |
8743 nothing_done = FALSE; | |
8744 | |
8745 if (nothing_done && do_msg) | |
8746 MSG(_("No matching autocommands")); | |
8747 | |
8748 #ifdef FEAT_EVAL | |
8749 return aborting() ? FAIL : OK; | |
8750 #else | |
8751 return OK; | |
8752 #endif | |
8753 } | |
8754 | |
8755 /* | |
8756 * ":doautoall": execute autocommands for each loaded buffer. | |
8757 */ | |
8758 void | |
8759 ex_doautoall(eap) | |
8760 exarg_T *eap; | |
8761 { | |
8762 int retval; | |
8763 aco_save_T aco; | |
8764 buf_T *buf; | |
3342 | 8765 char_u *arg = eap->arg; |
3350 | 8766 int call_do_modelines = check_nomodeline(&arg); |
7 | 8767 |
8768 /* | |
8769 * This is a bit tricky: For some commands curwin->w_buffer needs to be | |
8770 * equal to curbuf, but for some buffers there may not be a window. | |
8771 * So we change the buffer for the current window for a moment. This | |
8772 * gives problems when the autocommands make changes to the list of | |
8773 * buffers or windows... | |
8774 */ | |
8775 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
8776 { | |
1665 | 8777 if (buf->b_ml.ml_mfp != NULL) |
7 | 8778 { |
8779 /* find a window for this buffer and save some values */ | |
8780 aucmd_prepbuf(&aco, buf); | |
8781 | |
8782 /* execute the autocommands for this buffer */ | |
3342 | 8783 retval = do_doautocmd(arg, FALSE); |
8784 | |
8785 if (call_do_modelines) | |
8786 { | |
8787 /* Execute the modeline settings, but don't set window-local | |
8788 * options if we are using the current window for another | |
8789 * buffer. */ | |
8790 do_modelines(curwin == aucmd_win ? OPT_NOWIN : 0); | |
8791 } | |
7 | 8792 |
8793 /* restore the current window */ | |
8794 aucmd_restbuf(&aco); | |
8795 | |
8796 /* stop if there is some error or buffer was deleted */ | |
8797 if (retval == FAIL || !buf_valid(buf)) | |
8798 break; | |
8799 } | |
8800 } | |
8801 | |
8802 check_cursor(); /* just in case lines got deleted */ | |
8803 } | |
8804 | |
8805 /* | |
3350 | 8806 * Check *argp for <nomodeline>. When it is present return FALSE, otherwise |
8807 * return TRUE and advance *argp to after it. | |
8808 * Thus return TRUE when do_modelines() should be called. | |
8809 */ | |
8810 int | |
8811 check_nomodeline(argp) | |
8812 char_u **argp; | |
8813 { | |
8814 if (STRNCMP(*argp, "<nomodeline>", 12) == 0) | |
8815 { | |
8816 *argp = skipwhite(*argp + 12); | |
8817 return FALSE; | |
8818 } | |
8819 return TRUE; | |
8820 } | |
8821 | |
8822 /* | |
7 | 8823 * Prepare for executing autocommands for (hidden) buffer "buf". |
1906 | 8824 * Search for a visible window containing the current buffer. If there isn't |
8825 * one then use "aucmd_win". | |
7 | 8826 * Set "curbuf" and "curwin" to match "buf". |
934 | 8827 * When FEAT_AUTOCMD is not defined another version is used, see below. |
7 | 8828 */ |
8829 void | |
8830 aucmd_prepbuf(aco, buf) | |
8831 aco_save_T *aco; /* structure to save values in */ | |
8832 buf_T *buf; /* new curbuf */ | |
8833 { | |
8834 win_T *win; | |
1906 | 8835 #ifdef FEAT_WINDOWS |
8836 int save_ea; | |
8837 #endif | |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8838 #ifdef FEAT_AUTOCHDIR |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8839 int save_acd; |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8840 #endif |
7 | 8841 |
8842 /* Find a window that is for the new buffer */ | |
8843 if (buf == curbuf) /* be quick when buf is curbuf */ | |
8844 win = curwin; | |
8845 else | |
8846 #ifdef FEAT_WINDOWS | |
8847 for (win = firstwin; win != NULL; win = win->w_next) | |
8848 if (win->w_buffer == buf) | |
8849 break; | |
8850 #else | |
8851 win = NULL; | |
8852 #endif | |
8853 | |
1906 | 8854 /* Allocate "aucmd_win" when needed. If this fails (out of memory) fall |
8855 * back to using the current window. */ | |
8856 if (win == NULL && aucmd_win == NULL) | |
8857 { | |
8858 win_alloc_aucmd_win(); | |
8859 if (aucmd_win == NULL) | |
8860 win = curwin; | |
8861 } | |
1944 | 8862 if (win == NULL && aucmd_win_used) |
8863 /* Strange recursive autocommand, fall back to using the current | |
8864 * window. Expect a few side effects... */ | |
8865 win = curwin; | |
1906 | 8866 |
8867 aco->save_curwin = curwin; | |
8868 aco->save_curbuf = curbuf; | |
7 | 8869 if (win != NULL) |
8870 { | |
1906 | 8871 /* There is a window for "buf" in the current tab page, make it the |
8872 * curwin. This is preferred, it has the least side effects (esp. if | |
8873 * "buf" is curbuf). */ | |
1944 | 8874 aco->use_aucmd_win = FALSE; |
7 | 8875 curwin = win; |
8876 } | |
8877 else | |
8878 { | |
1906 | 8879 /* There is no window for "buf", use "aucmd_win". To minimize the side |
4352 | 8880 * effects, insert it in the current tab page. |
1906 | 8881 * Anything related to a window (e.g., setting folds) may have |
8882 * unexpected results. */ | |
1944 | 8883 aco->use_aucmd_win = TRUE; |
8884 aucmd_win_used = TRUE; | |
1918 | 8885 aucmd_win->w_buffer = buf; |
2258
772bfca06c18
Fix crash when using ":grep".
Bram Moolenaar <bram@vim.org>
parents:
2244
diff
changeset
|
8886 aucmd_win->w_s = &buf->b_s; |
7 | 8887 ++buf->b_nwindows; |
1918 | 8888 win_init_empty(aucmd_win); /* set cursor and topline to safe values */ |
1944 | 8889 |
8890 /* Make sure w_localdir and globaldir are NULL to avoid a chdir() in | |
8891 * win_enter_ext(). */ | |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8892 vim_free(aucmd_win->w_localdir); |
1944 | 8893 aucmd_win->w_localdir = NULL; |
8894 aco->globaldir = globaldir; | |
8895 globaldir = NULL; | |
8896 | |
7 | 8897 |
1906 | 8898 #ifdef FEAT_WINDOWS |
1923 | 8899 /* Split the current window, put the aucmd_win in the upper half. |
8900 * We don't want the BufEnter or WinEnter autocommands. */ | |
8901 block_autocmds(); | |
1906 | 8902 make_snapshot(SNAP_AUCMD_IDX); |
8903 save_ea = p_ea; | |
8904 p_ea = FALSE; | |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8905 |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8906 # ifdef FEAT_AUTOCHDIR |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8907 /* Prevent chdir() call in win_enter_ext(), through do_autochdir(). */ |
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8908 save_acd = p_acd; |
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8909 p_acd = FALSE; |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8910 # endif |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8911 |
1906 | 8912 (void)win_split_ins(0, WSP_TOP, aucmd_win, 0); |
8913 (void)win_comp_pos(); /* recompute window positions */ | |
8914 p_ea = save_ea; | |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8915 # ifdef FEAT_AUTOCHDIR |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8916 p_acd = save_acd; |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8917 # endif |
1923 | 8918 unblock_autocmds(); |
1906 | 8919 #endif |
1918 | 8920 curwin = aucmd_win; |
1906 | 8921 } |
7 | 8922 curbuf = buf; |
1906 | 8923 aco->new_curwin = curwin; |
8924 aco->new_curbuf = curbuf; | |
7 | 8925 } |
8926 | |
8927 /* | |
8928 * Cleanup after executing autocommands for a (hidden) buffer. | |
8929 * Restore the window as it was (if possible). | |
934 | 8930 * When FEAT_AUTOCMD is not defined another version is used, see below. |
7 | 8931 */ |
8932 void | |
8933 aucmd_restbuf(aco) | |
8934 aco_save_T *aco; /* structure holding saved values */ | |
8935 { | |
1906 | 8936 #ifdef FEAT_WINDOWS |
8937 int dummy; | |
8938 #endif | |
8939 | |
1944 | 8940 if (aco->use_aucmd_win) |
1906 | 8941 { |
8942 --curbuf->b_nwindows; | |
8943 #ifdef FEAT_WINDOWS | |
8944 /* Find "aucmd_win", it can't be closed, but it may be in another tab | |
1923 | 8945 * page. Do not trigger autocommands here. */ |
8946 block_autocmds(); | |
1906 | 8947 if (curwin != aucmd_win) |
8948 { | |
8949 tabpage_T *tp; | |
8950 win_T *wp; | |
8951 | |
8952 FOR_ALL_TAB_WINDOWS(tp, wp) | |
8953 { | |
8954 if (wp == aucmd_win) | |
8955 { | |
8956 if (tp != curtab) | |
4354 | 8957 goto_tabpage_tp(tp, TRUE, TRUE); |
1906 | 8958 win_goto(aucmd_win); |
3340 | 8959 goto win_found; |
1906 | 8960 } |
8961 } | |
8962 } | |
3340 | 8963 win_found: |
1906 | 8964 |
8965 /* Remove the window and frame from the tree of frames. */ | |
8966 (void)winframe_remove(curwin, &dummy, NULL); | |
8967 win_remove(curwin, NULL); | |
1944 | 8968 aucmd_win_used = FALSE; |
1906 | 8969 last_status(FALSE); /* may need to remove last status line */ |
8970 restore_snapshot(SNAP_AUCMD_IDX, FALSE); | |
8971 (void)win_comp_pos(); /* recompute window positions */ | |
1923 | 8972 unblock_autocmds(); |
1906 | 8973 |
8974 if (win_valid(aco->save_curwin)) | |
8975 curwin = aco->save_curwin; | |
8976 else | |
8977 /* Hmm, original window disappeared. Just use the first one. */ | |
8978 curwin = firstwin; | |
8979 # ifdef FEAT_EVAL | |
4287 | 8980 vars_clear(&aucmd_win->w_vars->dv_hashtab); /* free all w: variables */ |
8981 hash_init(&aucmd_win->w_vars->dv_hashtab); /* re-use the hashtab */ | |
1906 | 8982 # endif |
8983 #else | |
8984 curwin = aco->save_curwin; | |
8985 #endif | |
8986 curbuf = curwin->w_buffer; | |
8987 | |
1944 | 8988 vim_free(globaldir); |
8989 globaldir = aco->globaldir; | |
8990 | |
1906 | 8991 /* the buffer contents may have changed */ |
8992 check_cursor(); | |
8993 if (curwin->w_topline > curbuf->b_ml.ml_line_count) | |
8994 { | |
8995 curwin->w_topline = curbuf->b_ml.ml_line_count; | |
8996 #ifdef FEAT_DIFF | |
8997 curwin->w_topfill = 0; | |
8998 #endif | |
8999 } | |
9000 #if defined(FEAT_GUI) | |
9001 /* Hide the scrollbars from the aucmd_win and update. */ | |
9002 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_LEFT], FALSE); | |
9003 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_RIGHT], FALSE); | |
9004 gui_may_update_scrollbars(); | |
9005 #endif | |
9006 } | |
9007 else | |
7 | 9008 { |
9009 /* restore curwin */ | |
9010 #ifdef FEAT_WINDOWS | |
9011 if (win_valid(aco->save_curwin)) | |
9012 #endif | |
9013 { | |
1906 | 9014 /* Restore the buffer which was previously edited by curwin, if |
1944 | 9015 * it was changed, we are still the same window and the buffer is |
1906 | 9016 * valid. */ |
7 | 9017 if (curwin == aco->new_curwin |
1906 | 9018 && curbuf != aco->new_curbuf |
9019 && buf_valid(aco->new_curbuf) | |
9020 && aco->new_curbuf->b_ml.ml_mfp != NULL) | |
7 | 9021 { |
3497 | 9022 # if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) |
9023 if (curwin->w_s == &curbuf->b_s) | |
9024 curwin->w_s = &aco->new_curbuf->b_s; | |
9025 # endif | |
7 | 9026 --curbuf->b_nwindows; |
1906 | 9027 curbuf = aco->new_curbuf; |
7 | 9028 curwin->w_buffer = curbuf; |
9029 ++curbuf->b_nwindows; | |
9030 } | |
9031 | |
9032 curwin = aco->save_curwin; | |
9033 curbuf = curwin->w_buffer; | |
6201 | 9034 /* In case the autocommand move the cursor to a position that that |
9035 * not exist in curbuf. */ | |
9036 check_cursor(); | |
7 | 9037 } |
9038 } | |
9039 } | |
9040 | |
9041 static int autocmd_nested = FALSE; | |
9042 | |
9043 /* | |
9044 * Execute autocommands for "event" and file name "fname". | |
9045 * Return TRUE if some commands were executed. | |
9046 */ | |
9047 int | |
9048 apply_autocmds(event, fname, fname_io, force, buf) | |
661 | 9049 event_T event; |
7 | 9050 char_u *fname; /* NULL or empty means use actual file name */ |
9051 char_u *fname_io; /* fname to use for <afile> on cmdline */ | |
9052 int force; /* when TRUE, ignore autocmd_busy */ | |
9053 buf_T *buf; /* buffer for <abuf> */ | |
9054 { | |
9055 return apply_autocmds_group(event, fname, fname_io, force, | |
9056 AUGROUP_ALL, buf, NULL); | |
9057 } | |
9058 | |
9059 /* | |
9060 * Like apply_autocmds(), but with extra "eap" argument. This takes care of | |
9061 * setting v:filearg. | |
9062 */ | |
9063 static int | |
9064 apply_autocmds_exarg(event, fname, fname_io, force, buf, eap) | |
661 | 9065 event_T event; |
7 | 9066 char_u *fname; |
9067 char_u *fname_io; | |
9068 int force; | |
9069 buf_T *buf; | |
9070 exarg_T *eap; | |
9071 { | |
9072 return apply_autocmds_group(event, fname, fname_io, force, | |
9073 AUGROUP_ALL, buf, eap); | |
9074 } | |
9075 | |
9076 /* | |
9077 * Like apply_autocmds(), but handles the caller's retval. If the script | |
9078 * processing is being aborted or if retval is FAIL when inside a try | |
9079 * conditional, no autocommands are executed. If otherwise the autocommands | |
9080 * cause the script to be aborted, retval is set to FAIL. | |
9081 */ | |
9082 int | |
9083 apply_autocmds_retval(event, fname, fname_io, force, buf, retval) | |
661 | 9084 event_T event; |
7 | 9085 char_u *fname; /* NULL or empty means use actual file name */ |
9086 char_u *fname_io; /* fname to use for <afile> on cmdline */ | |
9087 int force; /* when TRUE, ignore autocmd_busy */ | |
9088 buf_T *buf; /* buffer for <abuf> */ | |
9089 int *retval; /* pointer to caller's retval */ | |
9090 { | |
9091 int did_cmd; | |
9092 | |
532 | 9093 #ifdef FEAT_EVAL |
7 | 9094 if (should_abort(*retval)) |
9095 return FALSE; | |
532 | 9096 #endif |
7 | 9097 |
9098 did_cmd = apply_autocmds_group(event, fname, fname_io, force, | |
9099 AUGROUP_ALL, buf, NULL); | |
532 | 9100 if (did_cmd |
9101 #ifdef FEAT_EVAL | |
9102 && aborting() | |
9103 #endif | |
9104 ) | |
7 | 9105 *retval = FAIL; |
9106 return did_cmd; | |
9107 } | |
9108 | |
609 | 9109 /* |
9110 * Return TRUE when there is a CursorHold autocommand defined. | |
9111 */ | |
7 | 9112 int |
9113 has_cursorhold() | |
9114 { | |
661 | 9115 return (first_autopat[(int)(get_real_state() == NORMAL_BUSY |
9116 ? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)] != NULL); | |
7 | 9117 } |
609 | 9118 |
9119 /* | |
9120 * Return TRUE if the CursorHold event can be triggered. | |
9121 */ | |
9122 int | |
9123 trigger_cursorhold() | |
9124 { | |
661 | 9125 int state; |
9126 | |
2976 | 9127 if (!did_cursorhold |
9128 && has_cursorhold() | |
9129 && !Recording | |
9130 && typebuf.tb_len == 0 | |
978 | 9131 #ifdef FEAT_INS_EXPAND |
9132 && !ins_compl_active() | |
9133 #endif | |
9134 ) | |
661 | 9135 { |
9136 state = get_real_state(); | |
9137 if (state == NORMAL_BUSY || (state & INSERT) != 0) | |
9138 return TRUE; | |
9139 } | |
9140 return FALSE; | |
609 | 9141 } |
661 | 9142 |
9143 /* | |
9144 * Return TRUE when there is a CursorMoved autocommand defined. | |
9145 */ | |
9146 int | |
9147 has_cursormoved() | |
9148 { | |
9149 return (first_autopat[(int)EVENT_CURSORMOVED] != NULL); | |
9150 } | |
9151 | |
9152 /* | |
9153 * Return TRUE when there is a CursorMovedI autocommand defined. | |
9154 */ | |
9155 int | |
9156 has_cursormovedI() | |
9157 { | |
9158 return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL); | |
9159 } | |
7 | 9160 |
3390 | 9161 /* |
4232 | 9162 * Return TRUE when there is a TextChanged autocommand defined. |
9163 */ | |
9164 int | |
9165 has_textchanged() | |
9166 { | |
9167 return (first_autopat[(int)EVENT_TEXTCHANGED] != NULL); | |
9168 } | |
9169 | |
9170 /* | |
9171 * Return TRUE when there is a TextChangedI autocommand defined. | |
9172 */ | |
9173 int | |
9174 has_textchangedI() | |
9175 { | |
9176 return (first_autopat[(int)EVENT_TEXTCHANGEDI] != NULL); | |
9177 } | |
9178 | |
9179 /* | |
3390 | 9180 * Return TRUE when there is an InsertCharPre autocommand defined. |
9181 */ | |
9182 int | |
9183 has_insertcharpre() | |
9184 { | |
9185 return (first_autopat[(int)EVENT_INSERTCHARPRE] != NULL); | |
9186 } | |
9187 | |
6154 | 9188 /* |
9189 * Return TRUE when there is an CmdUndefined autocommand defined. | |
9190 */ | |
9191 int | |
9192 has_cmdundefined() | |
9193 { | |
9194 return (first_autopat[(int)EVENT_CMDUNDEFINED] != NULL); | |
9195 } | |
9196 | |
9197 /* | |
9198 * Return TRUE when there is an FuncUndefined autocommand defined. | |
9199 */ | |
9200 int | |
9201 has_funcundefined() | |
9202 { | |
9203 return (first_autopat[(int)EVENT_FUNCUNDEFINED] != NULL); | |
9204 } | |
9205 | |
7 | 9206 static int |
9207 apply_autocmds_group(event, fname, fname_io, force, group, buf, eap) | |
661 | 9208 event_T event; |
7 | 9209 char_u *fname; /* NULL or empty means use actual file name */ |
9210 char_u *fname_io; /* fname to use for <afile> on cmdline, NULL means | |
9211 use fname */ | |
9212 int force; /* when TRUE, ignore autocmd_busy */ | |
9213 int group; /* group ID, or AUGROUP_ALL */ | |
9214 buf_T *buf; /* buffer for <abuf> */ | |
9215 exarg_T *eap; /* command arguments */ | |
9216 { | |
9217 char_u *sfname = NULL; /* short file name */ | |
9218 char_u *tail; | |
9219 int save_changed; | |
9220 buf_T *old_curbuf; | |
9221 int retval = FALSE; | |
9222 char_u *save_sourcing_name; | |
9223 linenr_T save_sourcing_lnum; | |
9224 char_u *save_autocmd_fname; | |
1723 | 9225 int save_autocmd_fname_full; |
7 | 9226 int save_autocmd_bufnr; |
9227 char_u *save_autocmd_match; | |
9228 int save_autocmd_busy; | |
9229 int save_autocmd_nested; | |
9230 static int nesting = 0; | |
9231 AutoPatCmd patcmd; | |
9232 AutoPat *ap; | |
9233 #ifdef FEAT_EVAL | |
9234 scid_T save_current_SID; | |
9235 void *save_funccalp; | |
9236 char_u *save_cmdarg; | |
9237 long save_cmdbang; | |
9238 #endif | |
9239 static int filechangeshell_busy = FALSE; | |
170 | 9240 #ifdef FEAT_PROFILE |
9241 proftime_T wait_time; | |
9242 #endif | |
6608 | 9243 int did_save_redobuff = FALSE; |
7 | 9244 |
9245 /* | |
9246 * Quickly return if there are no autocommands for this event or | |
9247 * autocommands are blocked. | |
9248 */ | |
1410 | 9249 if (first_autopat[(int)event] == NULL || autocmd_blocked > 0) |
40 | 9250 goto BYPASS_AU; |
7 | 9251 |
9252 /* | |
9253 * When autocommands are busy, new autocommands are only executed when | |
9254 * explicitly enabled with the "nested" flag. | |
9255 */ | |
9256 if (autocmd_busy && !(force || autocmd_nested)) | |
40 | 9257 goto BYPASS_AU; |
7 | 9258 |
9259 #ifdef FEAT_EVAL | |
9260 /* | |
1201 | 9261 * Quickly return when immediately aborting on error, or when an interrupt |
7 | 9262 * occurred or an exception was thrown but not caught. |
9263 */ | |
9264 if (aborting()) | |
40 | 9265 goto BYPASS_AU; |
7 | 9266 #endif |
9267 | |
9268 /* | |
9269 * FileChangedShell never nests, because it can create an endless loop. | |
9270 */ | |
765 | 9271 if (filechangeshell_busy && (event == EVENT_FILECHANGEDSHELL |
9272 || event == EVENT_FILECHANGEDSHELLPOST)) | |
40 | 9273 goto BYPASS_AU; |
7 | 9274 |
9275 /* | |
9276 * Ignore events in 'eventignore'. | |
9277 */ | |
9278 if (event_ignored(event)) | |
40 | 9279 goto BYPASS_AU; |
7 | 9280 |
9281 /* | |
9282 * Allow nesting of autocommands, but restrict the depth, because it's | |
9283 * possible to create an endless loop. | |
9284 */ | |
9285 if (nesting == 10) | |
9286 { | |
9287 EMSG(_("E218: autocommand nesting too deep")); | |
40 | 9288 goto BYPASS_AU; |
7 | 9289 } |
9290 | |
9291 /* | |
9292 * Check if these autocommands are disabled. Used when doing ":all" or | |
9293 * ":ball". | |
9294 */ | |
9295 if ( (autocmd_no_enter | |
9296 && (event == EVENT_WINENTER || event == EVENT_BUFENTER)) | |
9297 || (autocmd_no_leave | |
9298 && (event == EVENT_WINLEAVE || event == EVENT_BUFLEAVE))) | |
40 | 9299 goto BYPASS_AU; |
7 | 9300 |
9301 /* | |
9302 * Save the autocmd_* variables and info about the current buffer. | |
9303 */ | |
9304 save_autocmd_fname = autocmd_fname; | |
1723 | 9305 save_autocmd_fname_full = autocmd_fname_full; |
7 | 9306 save_autocmd_bufnr = autocmd_bufnr; |
9307 save_autocmd_match = autocmd_match; | |
9308 save_autocmd_busy = autocmd_busy; | |
9309 save_autocmd_nested = autocmd_nested; | |
9310 save_changed = curbuf->b_changed; | |
9311 old_curbuf = curbuf; | |
9312 | |
9313 /* | |
9314 * Set the file name to be used for <afile>. | |
1471 | 9315 * Make a copy to avoid that changing a buffer name or directory makes it |
9316 * invalid. | |
7 | 9317 */ |
9318 if (fname_io == NULL) | |
9319 { | |
5521 | 9320 if (event == EVENT_COLORSCHEME) |
9321 autocmd_fname = NULL; | |
9322 else if (fname != NULL && *fname != NUL) | |
7 | 9323 autocmd_fname = fname; |
9324 else if (buf != NULL) | |
1723 | 9325 autocmd_fname = buf->b_ffname; |
7 | 9326 else |
9327 autocmd_fname = NULL; | |
9328 } | |
9329 else | |
9330 autocmd_fname = fname_io; | |
1471 | 9331 if (autocmd_fname != NULL) |
1723 | 9332 autocmd_fname = vim_strsave(autocmd_fname); |
9333 autocmd_fname_full = FALSE; /* call FullName_save() later */ | |
7 | 9334 |
9335 /* | |
9336 * Set the buffer number to be used for <abuf>. | |
9337 */ | |
9338 if (buf == NULL) | |
9339 autocmd_bufnr = 0; | |
9340 else | |
9341 autocmd_bufnr = buf->b_fnum; | |
9342 | |
9343 /* | |
9344 * When the file name is NULL or empty, use the file name of buffer "buf". | |
9345 * Always use the full path of the file name to match with, in case | |
9346 * "allow_dirs" is set. | |
9347 */ | |
9348 if (fname == NULL || *fname == NUL) | |
9349 { | |
9350 if (buf == NULL) | |
9351 fname = NULL; | |
9352 else | |
9353 { | |
9354 #ifdef FEAT_SYN_HL | |
9355 if (event == EVENT_SYNTAX) | |
9356 fname = buf->b_p_syn; | |
9357 else | |
9358 #endif | |
9359 if (event == EVENT_FILETYPE) | |
9360 fname = buf->b_p_ft; | |
9361 else | |
9362 { | |
9363 if (buf->b_sfname != NULL) | |
9364 sfname = vim_strsave(buf->b_sfname); | |
9365 fname = buf->b_ffname; | |
9366 } | |
9367 } | |
9368 if (fname == NULL) | |
9369 fname = (char_u *)""; | |
9370 fname = vim_strsave(fname); /* make a copy, so we can change it */ | |
9371 } | |
9372 else | |
9373 { | |
9374 sfname = vim_strsave(fname); | |
5521 | 9375 /* Don't try expanding FileType, Syntax, FuncUndefined, WindowID, |
9376 * ColorScheme or QuickFixCmd* */ | |
161 | 9377 if (event == EVENT_FILETYPE |
9378 || event == EVENT_SYNTAX | |
1867 | 9379 || event == EVENT_FUNCUNDEFINED |
161 | 9380 || event == EVENT_REMOTEREPLY |
649 | 9381 || event == EVENT_SPELLFILEMISSING |
161 | 9382 || event == EVENT_QUICKFIXCMDPRE |
5521 | 9383 || event == EVENT_COLORSCHEME |
161 | 9384 || event == EVENT_QUICKFIXCMDPOST) |
7 | 9385 fname = vim_strsave(fname); |
9386 else | |
9387 fname = FullName_save(fname, FALSE); | |
9388 } | |
9389 if (fname == NULL) /* out of memory */ | |
9390 { | |
9391 vim_free(sfname); | |
40 | 9392 retval = FALSE; |
9393 goto BYPASS_AU; | |
7 | 9394 } |
9395 | |
9396 #ifdef BACKSLASH_IN_FILENAME | |
9397 /* | |
9398 * Replace all backslashes with forward slashes. This makes the | |
9399 * autocommand patterns portable between Unix and MS-DOS. | |
9400 */ | |
9401 if (sfname != NULL) | |
9402 forward_slash(sfname); | |
9403 forward_slash(fname); | |
9404 #endif | |
9405 | |
9406 #ifdef VMS | |
9407 /* remove version for correct match */ | |
9408 if (sfname != NULL) | |
9409 vms_remove_version(sfname); | |
9410 vms_remove_version(fname); | |
9411 #endif | |
9412 | |
9413 /* | |
9414 * Set the name to be used for <amatch>. | |
9415 */ | |
9416 autocmd_match = fname; | |
9417 | |
9418 | |
9419 /* Don't redraw while doing auto commands. */ | |
9420 ++RedrawingDisabled; | |
9421 save_sourcing_name = sourcing_name; | |
9422 sourcing_name = NULL; /* don't free this one */ | |
9423 save_sourcing_lnum = sourcing_lnum; | |
9424 sourcing_lnum = 0; /* no line number here */ | |
9425 | |
9426 #ifdef FEAT_EVAL | |
9427 save_current_SID = current_SID; | |
9428 | |
170 | 9429 # ifdef FEAT_PROFILE |
788 | 9430 if (do_profiling == PROF_YES) |
170 | 9431 prof_child_enter(&wait_time); /* doesn't count for the caller itself */ |
9432 # endif | |
9433 | |
7 | 9434 /* Don't use local function variables, if called from a function */ |
9435 save_funccalp = save_funccal(); | |
9436 #endif | |
9437 | |
9438 /* | |
9439 * When starting to execute autocommands, save the search patterns. | |
9440 */ | |
9441 if (!autocmd_busy) | |
9442 { | |
9443 save_search_patterns(); | |
6608 | 9444 if (!ins_compl_active()) |
9445 { | |
9446 saveRedobuff(); | |
9447 did_save_redobuff = TRUE; | |
9448 } | |
7 | 9449 did_filetype = keep_filetype; |
9450 } | |
9451 | |
9452 /* | |
9453 * Note that we are applying autocmds. Some commands need to know. | |
9454 */ | |
9455 autocmd_busy = TRUE; | |
9456 filechangeshell_busy = (event == EVENT_FILECHANGEDSHELL); | |
9457 ++nesting; /* see matching decrement below */ | |
9458 | |
9459 /* Remember that FileType was triggered. Used for did_filetype(). */ | |
9460 if (event == EVENT_FILETYPE) | |
9461 did_filetype = TRUE; | |
9462 | |
9463 tail = gettail(fname); | |
9464 | |
9465 /* Find first autocommand that matches */ | |
9466 patcmd.curpat = first_autopat[(int)event]; | |
9467 patcmd.nextcmd = NULL; | |
9468 patcmd.group = group; | |
9469 patcmd.fname = fname; | |
9470 patcmd.sfname = sfname; | |
9471 patcmd.tail = tail; | |
9472 patcmd.event = event; | |
40 | 9473 patcmd.arg_bufnr = autocmd_bufnr; |
9474 patcmd.next = NULL; | |
7 | 9475 auto_next_pat(&patcmd, FALSE); |
9476 | |
9477 /* found one, start executing the autocommands */ | |
9478 if (patcmd.curpat != NULL) | |
9479 { | |
40 | 9480 /* add to active_apc_list */ |
9481 patcmd.next = active_apc_list; | |
9482 active_apc_list = &patcmd; | |
9483 | |
7 | 9484 #ifdef FEAT_EVAL |
9485 /* set v:cmdarg (only when there is a matching pattern) */ | |
9486 save_cmdbang = get_vim_var_nr(VV_CMDBANG); | |
9487 if (eap != NULL) | |
9488 { | |
9489 save_cmdarg = set_cmdarg(eap, NULL); | |
9490 set_vim_var_nr(VV_CMDBANG, (long)eap->forceit); | |
9491 } | |
9492 else | |
9493 save_cmdarg = NULL; /* avoid gcc warning */ | |
9494 #endif | |
9495 retval = TRUE; | |
9496 /* mark the last pattern, to avoid an endless loop when more patterns | |
9497 * are added when executing autocommands */ | |
9498 for (ap = patcmd.curpat; ap->next != NULL; ap = ap->next) | |
9499 ap->last = FALSE; | |
9500 ap->last = TRUE; | |
9501 check_lnums(TRUE); /* make sure cursor and topline are valid */ | |
9502 do_cmdline(NULL, getnextac, (void *)&patcmd, | |
9503 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT); | |
9504 #ifdef FEAT_EVAL | |
9505 if (eap != NULL) | |
9506 { | |
9507 (void)set_cmdarg(NULL, save_cmdarg); | |
9508 set_vim_var_nr(VV_CMDBANG, save_cmdbang); | |
9509 } | |
9510 #endif | |
40 | 9511 /* delete from active_apc_list */ |
9512 if (active_apc_list == &patcmd) /* just in case */ | |
9513 active_apc_list = patcmd.next; | |
7 | 9514 } |
9515 | |
9516 --RedrawingDisabled; | |
9517 autocmd_busy = save_autocmd_busy; | |
9518 filechangeshell_busy = FALSE; | |
9519 autocmd_nested = save_autocmd_nested; | |
9520 vim_free(sourcing_name); | |
9521 sourcing_name = save_sourcing_name; | |
9522 sourcing_lnum = save_sourcing_lnum; | |
1471 | 9523 vim_free(autocmd_fname); |
7 | 9524 autocmd_fname = save_autocmd_fname; |
1723 | 9525 autocmd_fname_full = save_autocmd_fname_full; |
7 | 9526 autocmd_bufnr = save_autocmd_bufnr; |
9527 autocmd_match = save_autocmd_match; | |
9528 #ifdef FEAT_EVAL | |
9529 current_SID = save_current_SID; | |
9530 restore_funccal(save_funccalp); | |
170 | 9531 # ifdef FEAT_PROFILE |
788 | 9532 if (do_profiling == PROF_YES) |
170 | 9533 prof_child_exit(&wait_time); |
9534 # endif | |
7 | 9535 #endif |
9536 vim_free(fname); | |
9537 vim_free(sfname); | |
9538 --nesting; /* see matching increment above */ | |
9539 | |
9540 /* | |
9541 * When stopping to execute autocommands, restore the search patterns and | |
5958 | 9542 * the redo buffer. Free any buffers in the au_pending_free_buf list and |
9543 * free any windows in the au_pending_free_win list. | |
7 | 9544 */ |
9545 if (!autocmd_busy) | |
9546 { | |
9547 restore_search_patterns(); | |
6608 | 9548 if (did_save_redobuff) |
9549 restoreRedobuff(); | |
7 | 9550 did_filetype = FALSE; |
5816 | 9551 while (au_pending_free_buf != NULL) |
9552 { | |
9553 buf_T *b = au_pending_free_buf->b_next; | |
9554 vim_free(au_pending_free_buf); | |
9555 au_pending_free_buf = b; | |
9556 } | |
5958 | 9557 while (au_pending_free_win != NULL) |
9558 { | |
9559 win_T *w = au_pending_free_win->w_next; | |
9560 vim_free(au_pending_free_win); | |
9561 au_pending_free_win = w; | |
9562 } | |
7 | 9563 } |
9564 | |
9565 /* | |
9566 * Some events don't set or reset the Changed flag. | |
9567 * Check if still in the same buffer! | |
9568 */ | |
9569 if (curbuf == old_curbuf | |
9570 && (event == EVENT_BUFREADPOST | |
9571 || event == EVENT_BUFWRITEPOST | |
9572 || event == EVENT_FILEAPPENDPOST | |
9573 || event == EVENT_VIMLEAVE | |
9574 || event == EVENT_VIMLEAVEPRE)) | |
9575 { | |
9576 #ifdef FEAT_TITLE | |
9577 if (curbuf->b_changed != save_changed) | |
9578 need_maketitle = TRUE; | |
9579 #endif | |
9580 curbuf->b_changed = save_changed; | |
9581 } | |
9582 | |
9583 au_cleanup(); /* may really delete removed patterns/commands now */ | |
40 | 9584 |
9585 BYPASS_AU: | |
9586 /* When wiping out a buffer make sure all its buffer-local autocommands | |
9587 * are deleted. */ | |
9588 if (event == EVENT_BUFWIPEOUT && buf != NULL) | |
9589 aubuflocal_remove(buf); | |
9590 | |
7 | 9591 return retval; |
9592 } | |
9593 | |
1410 | 9594 # ifdef FEAT_EVAL |
9595 static char_u *old_termresponse = NULL; | |
9596 # endif | |
9597 | |
9598 /* | |
9599 * Block triggering autocommands until unblock_autocmd() is called. | |
9600 * Can be used recursively, so long as it's symmetric. | |
9601 */ | |
9602 void | |
9603 block_autocmds() | |
9604 { | |
9605 # ifdef FEAT_EVAL | |
9606 /* Remember the value of v:termresponse. */ | |
9607 if (autocmd_blocked == 0) | |
9608 old_termresponse = get_vim_var_str(VV_TERMRESPONSE); | |
9609 # endif | |
9610 ++autocmd_blocked; | |
9611 } | |
9612 | |
9613 void | |
9614 unblock_autocmds() | |
9615 { | |
9616 --autocmd_blocked; | |
9617 | |
9618 # ifdef FEAT_EVAL | |
9619 /* When v:termresponse was set while autocommands were blocked, trigger | |
9620 * the autocommands now. Esp. useful when executing a shell command | |
9621 * during startup (vimdiff). */ | |
9622 if (autocmd_blocked == 0 | |
9623 && get_vim_var_str(VV_TERMRESPONSE) != old_termresponse) | |
9624 apply_autocmds(EVENT_TERMRESPONSE, NULL, NULL, FALSE, curbuf); | |
9625 # endif | |
9626 } | |
9627 | |
5008
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9628 int |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9629 is_autocmd_blocked() |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9630 { |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9631 return autocmd_blocked != 0; |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9632 } |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9633 |
7 | 9634 /* |
9635 * Find next autocommand pattern that matches. | |
9636 */ | |
9637 static void | |
9638 auto_next_pat(apc, stop_at_last) | |
9639 AutoPatCmd *apc; | |
9640 int stop_at_last; /* stop when 'last' flag is set */ | |
9641 { | |
9642 AutoPat *ap; | |
9643 AutoCmd *cp; | |
9644 char_u *name; | |
9645 char *s; | |
9646 | |
9647 vim_free(sourcing_name); | |
9648 sourcing_name = NULL; | |
9649 | |
9650 for (ap = apc->curpat; ap != NULL && !got_int; ap = ap->next) | |
9651 { | |
9652 apc->curpat = NULL; | |
9653 | |
1723 | 9654 /* Only use a pattern when it has not been removed, has commands and |
40 | 9655 * the group matches. For buffer-local autocommands only check the |
9656 * buffer number. */ | |
7 | 9657 if (ap->pat != NULL && ap->cmds != NULL |
9658 && (apc->group == AUGROUP_ALL || apc->group == ap->group)) | |
9659 { | |
40 | 9660 /* execution-condition */ |
9661 if (ap->buflocal_nr == 0 | |
6375 | 9662 ? (match_file_pat(NULL, &ap->reg_prog, apc->fname, |
153 | 9663 apc->sfname, apc->tail, ap->allow_dirs)) |
40 | 9664 : ap->buflocal_nr == apc->arg_bufnr) |
7 | 9665 { |
9666 name = event_nr2name(apc->event); | |
9667 s = _("%s Auto commands for \"%s\""); | |
9668 sourcing_name = alloc((unsigned)(STRLEN(s) | |
9669 + STRLEN(name) + ap->patlen + 1)); | |
9670 if (sourcing_name != NULL) | |
9671 { | |
9672 sprintf((char *)sourcing_name, s, | |
9673 (char *)name, (char *)ap->pat); | |
9674 if (p_verbose >= 8) | |
292 | 9675 { |
9676 verbose_enter(); | |
273 | 9677 smsg((char_u *)_("Executing %s"), sourcing_name); |
292 | 9678 verbose_leave(); |
9679 } | |
7 | 9680 } |
9681 | |
9682 apc->curpat = ap; | |
9683 apc->nextcmd = ap->cmds; | |
9684 /* mark last command */ | |
9685 for (cp = ap->cmds; cp->next != NULL; cp = cp->next) | |
9686 cp->last = FALSE; | |
9687 cp->last = TRUE; | |
9688 } | |
9689 line_breakcheck(); | |
9690 if (apc->curpat != NULL) /* found a match */ | |
9691 break; | |
9692 } | |
9693 if (stop_at_last && ap->last) | |
9694 break; | |
9695 } | |
9696 } | |
9697 | |
9698 /* | |
9699 * Get next autocommand command. | |
9700 * Called by do_cmdline() to get the next line for ":if". | |
9701 * Returns allocated string, or NULL for end of autocommands. | |
9702 */ | |
3997 | 9703 char_u * |
7 | 9704 getnextac(c, cookie, indent) |
1880 | 9705 int c UNUSED; |
7 | 9706 void *cookie; |
1880 | 9707 int indent UNUSED; |
7 | 9708 { |
9709 AutoPatCmd *acp = (AutoPatCmd *)cookie; | |
9710 char_u *retval; | |
9711 AutoCmd *ac; | |
9712 | |
9713 /* Can be called again after returning the last line. */ | |
9714 if (acp->curpat == NULL) | |
9715 return NULL; | |
9716 | |
9717 /* repeat until we find an autocommand to execute */ | |
9718 for (;;) | |
9719 { | |
9720 /* skip removed commands */ | |
9721 while (acp->nextcmd != NULL && acp->nextcmd->cmd == NULL) | |
9722 if (acp->nextcmd->last) | |
9723 acp->nextcmd = NULL; | |
9724 else | |
9725 acp->nextcmd = acp->nextcmd->next; | |
9726 | |
9727 if (acp->nextcmd != NULL) | |
9728 break; | |
9729 | |
9730 /* at end of commands, find next pattern that matches */ | |
9731 if (acp->curpat->last) | |
9732 acp->curpat = NULL; | |
9733 else | |
9734 acp->curpat = acp->curpat->next; | |
9735 if (acp->curpat != NULL) | |
9736 auto_next_pat(acp, TRUE); | |
9737 if (acp->curpat == NULL) | |
9738 return NULL; | |
9739 } | |
9740 | |
9741 ac = acp->nextcmd; | |
9742 | |
9743 if (p_verbose >= 9) | |
9744 { | |
292 | 9745 verbose_enter_scroll(); |
273 | 9746 smsg((char_u *)_("autocommand %s"), ac->cmd); |
7 | 9747 msg_puts((char_u *)"\n"); /* don't overwrite this either */ |
292 | 9748 verbose_leave_scroll(); |
7 | 9749 } |
9750 retval = vim_strsave(ac->cmd); | |
9751 autocmd_nested = ac->nested; | |
9752 #ifdef FEAT_EVAL | |
9753 current_SID = ac->scriptID; | |
9754 #endif | |
9755 if (ac->last) | |
9756 acp->nextcmd = NULL; | |
9757 else | |
9758 acp->nextcmd = ac->next; | |
9759 return retval; | |
9760 } | |
9761 | |
9762 /* | |
9763 * Return TRUE if there is a matching autocommand for "fname". | |
40 | 9764 * To account for buffer-local autocommands, function needs to know |
9765 * in which buffer the file will be opened. | |
7 | 9766 */ |
9767 int | |
40 | 9768 has_autocmd(event, sfname, buf) |
661 | 9769 event_T event; |
7 | 9770 char_u *sfname; |
40 | 9771 buf_T *buf; |
7 | 9772 { |
9773 AutoPat *ap; | |
9774 char_u *fname; | |
9775 char_u *tail = gettail(sfname); | |
9776 int retval = FALSE; | |
9777 | |
9778 fname = FullName_save(sfname, FALSE); | |
9779 if (fname == NULL) | |
9780 return FALSE; | |
9781 | |
9782 #ifdef BACKSLASH_IN_FILENAME | |
9783 /* | |
9784 * Replace all backslashes with forward slashes. This makes the | |
9785 * autocommand patterns portable between Unix and MS-DOS. | |
9786 */ | |
9787 sfname = vim_strsave(sfname); | |
9788 if (sfname != NULL) | |
9789 forward_slash(sfname); | |
9790 forward_slash(fname); | |
9791 #endif | |
9792 | |
9793 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
9794 if (ap->pat != NULL && ap->cmds != NULL | |
856 | 9795 && (ap->buflocal_nr == 0 |
6375 | 9796 ? match_file_pat(NULL, &ap->reg_prog, |
153 | 9797 fname, sfname, tail, ap->allow_dirs) |
856 | 9798 : buf != NULL && ap->buflocal_nr == buf->b_fnum |
40 | 9799 )) |
7 | 9800 { |
9801 retval = TRUE; | |
9802 break; | |
9803 } | |
9804 | |
9805 vim_free(fname); | |
9806 #ifdef BACKSLASH_IN_FILENAME | |
9807 vim_free(sfname); | |
9808 #endif | |
9809 | |
9810 return retval; | |
9811 } | |
9812 | |
9813 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
9814 /* | |
9815 * Function given to ExpandGeneric() to obtain the list of autocommand group | |
9816 * names. | |
9817 */ | |
9818 char_u * | |
9819 get_augroup_name(xp, idx) | |
1880 | 9820 expand_T *xp UNUSED; |
7 | 9821 int idx; |
9822 { | |
9823 if (idx == augroups.ga_len) /* add "END" add the end */ | |
9824 return (char_u *)"END"; | |
9825 if (idx >= augroups.ga_len) /* end of list */ | |
9826 return NULL; | |
9827 if (AUGROUP_NAME(idx) == NULL) /* skip deleted entries */ | |
9828 return (char_u *)""; | |
9829 return AUGROUP_NAME(idx); /* return a name */ | |
9830 } | |
9831 | |
9832 static int include_groups = FALSE; | |
9833 | |
9834 char_u * | |
9835 set_context_in_autocmd(xp, arg, doautocmd) | |
9836 expand_T *xp; | |
9837 char_u *arg; | |
1733 | 9838 int doautocmd; /* TRUE for :doauto*, FALSE for :autocmd */ |
7 | 9839 { |
9840 char_u *p; | |
9841 int group; | |
9842 | |
9843 /* check for a group name, skip it if present */ | |
9844 include_groups = FALSE; | |
9845 p = arg; | |
9846 group = au_get_grouparg(&arg); | |
9847 if (group == AUGROUP_ERROR) | |
9848 return NULL; | |
9849 /* If there only is a group name that's what we expand. */ | |
9850 if (*arg == NUL && group != AUGROUP_ALL && !vim_iswhite(arg[-1])) | |
9851 { | |
9852 arg = p; | |
9853 group = AUGROUP_ALL; | |
9854 } | |
9855 | |
9856 /* skip over event name */ | |
9857 for (p = arg; *p != NUL && !vim_iswhite(*p); ++p) | |
9858 if (*p == ',') | |
9859 arg = p + 1; | |
9860 if (*p == NUL) | |
9861 { | |
9862 if (group == AUGROUP_ALL) | |
9863 include_groups = TRUE; | |
9864 xp->xp_context = EXPAND_EVENTS; /* expand event name */ | |
9865 xp->xp_pattern = arg; | |
9866 return NULL; | |
9867 } | |
9868 | |
9869 /* skip over pattern */ | |
9870 arg = skipwhite(p); | |
9871 while (*arg && (!vim_iswhite(*arg) || arg[-1] == '\\')) | |
9872 arg++; | |
9873 if (*arg) | |
9874 return arg; /* expand (next) command */ | |
9875 | |
9876 if (doautocmd) | |
9877 xp->xp_context = EXPAND_FILES; /* expand file names */ | |
9878 else | |
9879 xp->xp_context = EXPAND_NOTHING; /* pattern is not expanded */ | |
9880 return NULL; | |
9881 } | |
9882 | |
9883 /* | |
9884 * Function given to ExpandGeneric() to obtain the list of event names. | |
9885 */ | |
9886 char_u * | |
9887 get_event_name(xp, idx) | |
1880 | 9888 expand_T *xp UNUSED; |
7 | 9889 int idx; |
9890 { | |
9891 if (idx < augroups.ga_len) /* First list group names, if wanted */ | |
9892 { | |
9893 if (!include_groups || AUGROUP_NAME(idx) == NULL) | |
9894 return (char_u *)""; /* skip deleted entries */ | |
9895 return AUGROUP_NAME(idx); /* return a name */ | |
9896 } | |
9897 return (char_u *)event_names[idx - augroups.ga_len].name; | |
9898 } | |
9899 | |
9900 #endif /* FEAT_CMDL_COMPL */ | |
9901 | |
9902 /* | |
615 | 9903 * Return TRUE if autocmd is supported. |
9904 */ | |
9905 int | |
9906 autocmd_supported(name) | |
9907 char_u *name; | |
9908 { | |
9909 char_u *p; | |
9910 | |
9911 return (event_name2nr(name, &p) != NUM_EVENTS); | |
9912 } | |
9913 | |
9914 /* | |
612 | 9915 * Return TRUE if an autocommand is defined for a group, event and |
9916 * pattern: The group can be omitted to accept any group. "event" and "pattern" | |
9917 * can be NULL to accept any event and pattern. "pattern" can be NULL to accept | |
9918 * any pattern. Buffer-local patterns <buffer> or <buffer=N> are accepted. | |
9919 * Used for: | |
9920 * exists("#Group") or | |
9921 * exists("#Group#Event") or | |
9922 * exists("#Group#Event#pat") or | |
9923 * exists("#Event") or | |
9924 * exists("#Event#pat") | |
7 | 9925 */ |
9926 int | |
612 | 9927 au_exists(arg) |
9928 char_u *arg; | |
7 | 9929 { |
612 | 9930 char_u *arg_save; |
9931 char_u *pattern = NULL; | |
7 | 9932 char_u *event_name; |
9933 char_u *p; | |
661 | 9934 event_T event; |
7 | 9935 AutoPat *ap; |
40 | 9936 buf_T *buflocal_buf = NULL; |
612 | 9937 int group; |
9938 int retval = FALSE; | |
9939 | |
615 | 9940 /* Make a copy so that we can change the '#' chars to a NUL. */ |
612 | 9941 arg_save = vim_strsave(arg); |
9942 if (arg_save == NULL) | |
9943 return FALSE; | |
615 | 9944 p = vim_strchr(arg_save, '#'); |
612 | 9945 if (p != NULL) |
9946 *p++ = NUL; | |
9947 | |
9948 /* First, look for an autocmd group name */ | |
9949 group = au_find_group(arg_save); | |
9950 if (group == AUGROUP_ERROR) | |
9951 { | |
9952 /* Didn't match a group name, assume the first argument is an event. */ | |
9953 group = AUGROUP_ALL; | |
9954 event_name = arg_save; | |
9955 } | |
9956 else | |
9957 { | |
9958 if (p == NULL) | |
9959 { | |
9960 /* "Group": group name is present and it's recognized */ | |
9961 retval = TRUE; | |
9962 goto theend; | |
9963 } | |
9964 | |
9965 /* Must be "Group#Event" or "Group#Event#pat". */ | |
9966 event_name = p; | |
9967 p = vim_strchr(event_name, '#'); | |
9968 if (p != NULL) | |
9969 *p++ = NUL; /* "Group#Event#pat" */ | |
9970 } | |
9971 | |
9972 pattern = p; /* "pattern" is NULL when there is no pattern */ | |
7 | 9973 |
9974 /* find the index (enum) for the event name */ | |
9975 event = event_name2nr(event_name, &p); | |
9976 | |
9977 /* return FALSE if the event name is not recognized */ | |
612 | 9978 if (event == NUM_EVENTS) |
9979 goto theend; | |
7 | 9980 |
9981 /* Find the first autocommand for this event. | |
9982 * If there isn't any, return FALSE; | |
9983 * If there is one and no pattern given, return TRUE; */ | |
9984 ap = first_autopat[(int)event]; | |
9985 if (ap == NULL) | |
612 | 9986 goto theend; |
7 | 9987 |
40 | 9988 /* if pattern is "<buffer>", special handling is needed which uses curbuf */ |
9989 /* for pattern "<buffer=N>, fnamecmp() will work fine */ | |
1962 | 9990 if (pattern != NULL && STRICMP(pattern, "<buffer>") == 0) |
40 | 9991 buflocal_buf = curbuf; |
9992 | |
7 | 9993 /* Check if there is an autocommand with the given pattern. */ |
9994 for ( ; ap != NULL; ap = ap->next) | |
40 | 9995 /* only use a pattern when it has not been removed and has commands. */ |
9996 /* For buffer-local autocommands, fnamecmp() works fine. */ | |
7 | 9997 if (ap->pat != NULL && ap->cmds != NULL |
612 | 9998 && (group == AUGROUP_ALL || ap->group == group) |
1962 | 9999 && (pattern == NULL |
10000 || (buflocal_buf == NULL | |
10001 ? fnamecmp(ap->pat, pattern) == 0 | |
10002 : ap->buflocal_nr == buflocal_buf->b_fnum))) | |
612 | 10003 { |
10004 retval = TRUE; | |
10005 break; | |
10006 } | |
10007 | |
10008 theend: | |
10009 vim_free(arg_save); | |
10010 return retval; | |
7 | 10011 } |
40 | 10012 |
934 | 10013 #else /* FEAT_AUTOCMD */ |
10014 | |
10015 /* | |
10016 * Prepare for executing commands for (hidden) buffer "buf". | |
10017 * This is the non-autocommand version, it simply saves "curbuf" and sets | |
10018 * "curbuf" and "curwin" to match "buf". | |
10019 */ | |
10020 void | |
10021 aucmd_prepbuf(aco, buf) | |
10022 aco_save_T *aco; /* structure to save values in */ | |
10023 buf_T *buf; /* new curbuf */ | |
10024 { | |
1906 | 10025 aco->save_curbuf = curbuf; |
10026 --curbuf->b_nwindows; | |
934 | 10027 curbuf = buf; |
10028 curwin->w_buffer = buf; | |
1906 | 10029 ++curbuf->b_nwindows; |
934 | 10030 } |
10031 | |
10032 /* | |
10033 * Restore after executing commands for a (hidden) buffer. | |
10034 * This is the non-autocommand version. | |
10035 */ | |
10036 void | |
10037 aucmd_restbuf(aco) | |
10038 aco_save_T *aco; /* structure holding saved values */ | |
10039 { | |
1906 | 10040 --curbuf->b_nwindows; |
10041 curbuf = aco->save_curbuf; | |
934 | 10042 curwin->w_buffer = curbuf; |
1906 | 10043 ++curbuf->b_nwindows; |
934 | 10044 } |
10045 | |
7 | 10046 #endif /* FEAT_AUTOCMD */ |
10047 | |
934 | 10048 |
7 | 10049 #if defined(FEAT_AUTOCMD) || defined(FEAT_WILDIGN) || defined(PROTO) |
10050 /* | |
153 | 10051 * Try matching a filename with a "pattern" ("prog" is NULL), or use the |
10052 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling | |
10053 * vim_regcomp() often. | |
7 | 10054 * Used for autocommands and 'wildignore'. |
10055 * Returns TRUE if there is a match, FALSE otherwise. | |
10056 */ | |
6375 | 10057 static int |
153 | 10058 match_file_pat(pattern, prog, fname, sfname, tail, allow_dirs) |
7 | 10059 char_u *pattern; /* pattern to match with */ |
6375 | 10060 regprog_T **prog; /* pre-compiled regprog or NULL */ |
7 | 10061 char_u *fname; /* full path of file name */ |
10062 char_u *sfname; /* short file name or NULL */ | |
10063 char_u *tail; /* tail of path */ | |
10064 int allow_dirs; /* allow matching with dir */ | |
10065 { | |
10066 regmatch_T regmatch; | |
10067 int result = FALSE; | |
10068 | |
4242 | 10069 regmatch.rm_ic = p_fic; /* ignore case if 'fileignorecase' is set */ |
6470 | 10070 if (prog != NULL) |
10071 regmatch.regprog = *prog; | |
7 | 10072 else |
6470 | 10073 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC); |
7 | 10074 |
10075 /* | |
10076 * Try for a match with the pattern with: | |
10077 * 1. the full file name, when the pattern has a '/'. | |
10078 * 2. the short file name, when the pattern has a '/'. | |
10079 * 3. the tail of the file name, when the pattern has no '/'. | |
10080 */ | |
6470 | 10081 if (regmatch.regprog != NULL |
7 | 10082 && ((allow_dirs |
10083 && (vim_regexec(®match, fname, (colnr_T)0) | |
10084 || (sfname != NULL | |
10085 && vim_regexec(®match, sfname, (colnr_T)0)))) | |
6470 | 10086 || (!allow_dirs && vim_regexec(®match, tail, (colnr_T)0)))) |
7 | 10087 result = TRUE; |
10088 | |
6375 | 10089 if (prog != NULL) |
10090 *prog = regmatch.regprog; | |
10091 else | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4354
diff
changeset
|
10092 vim_regfree(regmatch.regprog); |
7 | 10093 return result; |
10094 } | |
10095 #endif | |
10096 | |
10097 #if defined(FEAT_WILDIGN) || defined(PROTO) | |
10098 /* | |
10099 * Return TRUE if a file matches with a pattern in "list". | |
10100 * "list" is a comma-separated list of patterns, like 'wildignore'. | |
10101 * "sfname" is the short file name or NULL, "ffname" the long file name. | |
10102 */ | |
10103 int | |
10104 match_file_list(list, sfname, ffname) | |
10105 char_u *list; | |
10106 char_u *sfname; | |
10107 char_u *ffname; | |
10108 { | |
10109 char_u buf[100]; | |
10110 char_u *tail; | |
10111 char_u *regpat; | |
10112 char allow_dirs; | |
10113 int match; | |
10114 char_u *p; | |
10115 | |
10116 tail = gettail(sfname); | |
10117 | |
10118 /* try all patterns in 'wildignore' */ | |
10119 p = list; | |
10120 while (*p) | |
10121 { | |
10122 copy_option_part(&p, buf, 100, ","); | |
10123 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE); | |
10124 if (regpat == NULL) | |
10125 break; | |
153 | 10126 match = match_file_pat(regpat, NULL, ffname, sfname, |
10127 tail, (int)allow_dirs); | |
7 | 10128 vim_free(regpat); |
10129 if (match) | |
10130 return TRUE; | |
10131 } | |
10132 return FALSE; | |
10133 } | |
10134 #endif | |
10135 | |
10136 /* | |
10137 * Convert the given pattern "pat" which has shell style wildcards in it, into | |
10138 * a regular expression, and return the result in allocated memory. If there | |
10139 * is a directory path separator to be matched, then TRUE is put in | |
10140 * allow_dirs, otherwise FALSE is put there -- webb. | |
10141 * Handle backslashes before special characters, like "\*" and "\ ". | |
10142 * | |
10143 * Returns NULL when out of memory. | |
10144 */ | |
10145 char_u * | |
10146 file_pat_to_reg_pat(pat, pat_end, allow_dirs, no_bslash) | |
10147 char_u *pat; | |
10148 char_u *pat_end; /* first char after pattern or NULL */ | |
10149 char *allow_dirs; /* Result passed back out in here */ | |
1877 | 10150 int no_bslash UNUSED; /* Don't use a backward slash as pathsep */ |
7 | 10151 { |
6470 | 10152 int size = 2; /* '^' at start, '$' at end */ |
7 | 10153 char_u *endp; |
10154 char_u *reg_pat; | |
10155 char_u *p; | |
10156 int i; | |
10157 int nested = 0; | |
10158 int add_dollar = TRUE; | |
10159 | |
10160 if (allow_dirs != NULL) | |
10161 *allow_dirs = FALSE; | |
10162 if (pat_end == NULL) | |
10163 pat_end = pat + STRLEN(pat); | |
10164 | |
10165 for (p = pat; p < pat_end; p++) | |
10166 { | |
10167 switch (*p) | |
10168 { | |
10169 case '*': | |
10170 case '.': | |
10171 case ',': | |
10172 case '{': | |
10173 case '}': | |
10174 case '~': | |
10175 size += 2; /* extra backslash */ | |
10176 break; | |
10177 #ifdef BACKSLASH_IN_FILENAME | |
10178 case '\\': | |
10179 case '/': | |
10180 size += 4; /* could become "[\/]" */ | |
10181 break; | |
10182 #endif | |
10183 default: | |
10184 size++; | |
10185 # ifdef FEAT_MBYTE | |
474 | 10186 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 10187 { |
10188 ++p; | |
10189 ++size; | |
10190 } | |
10191 # endif | |
10192 break; | |
10193 } | |
10194 } | |
10195 reg_pat = alloc(size + 1); | |
10196 if (reg_pat == NULL) | |
10197 return NULL; | |
10198 | |
10199 i = 0; | |
10200 | |
10201 if (pat[0] == '*') | |
10202 while (pat[0] == '*' && pat < pat_end - 1) | |
10203 pat++; | |
10204 else | |
10205 reg_pat[i++] = '^'; | |
10206 endp = pat_end - 1; | |
10207 if (*endp == '*') | |
10208 { | |
10209 while (endp - pat > 0 && *endp == '*') | |
10210 endp--; | |
10211 add_dollar = FALSE; | |
10212 } | |
10213 for (p = pat; *p && nested >= 0 && p <= endp; p++) | |
10214 { | |
10215 switch (*p) | |
10216 { | |
10217 case '*': | |
10218 reg_pat[i++] = '.'; | |
10219 reg_pat[i++] = '*'; | |
444 | 10220 while (p[1] == '*') /* "**" matches like "*" */ |
10221 ++p; | |
7 | 10222 break; |
10223 case '.': | |
10224 case '~': | |
10225 reg_pat[i++] = '\\'; | |
10226 reg_pat[i++] = *p; | |
10227 break; | |
10228 case '?': | |
10229 reg_pat[i++] = '.'; | |
10230 break; | |
10231 case '\\': | |
10232 if (p[1] == NUL) | |
10233 break; | |
10234 #ifdef BACKSLASH_IN_FILENAME | |
10235 if (!no_bslash) | |
10236 { | |
10237 /* translate: | |
10238 * "\x" to "\\x" e.g., "dir\file" | |
10239 * "\*" to "\\.*" e.g., "dir\*.c" | |
10240 * "\?" to "\\." e.g., "dir\??.c" | |
10241 * "\+" to "\+" e.g., "fileX\+.c" | |
10242 */ | |
10243 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?') | |
10244 && p[1] != '+') | |
10245 { | |
10246 reg_pat[i++] = '['; | |
10247 reg_pat[i++] = '\\'; | |
10248 reg_pat[i++] = '/'; | |
10249 reg_pat[i++] = ']'; | |
10250 if (allow_dirs != NULL) | |
10251 *allow_dirs = TRUE; | |
10252 break; | |
10253 } | |
10254 } | |
10255 #endif | |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10256 /* Undo escaping from ExpandEscape(): |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10257 * foo\?bar -> foo?bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10258 * foo\%bar -> foo%bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10259 * foo\,bar -> foo,bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10260 * foo\ bar -> foo bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10261 * Don't unescape \, * and others that are also special in a |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
5008
diff
changeset
|
10262 * regexp. |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
5008
diff
changeset
|
10263 * An escaped { must be unescaped since we use magic not |
5259
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10264 * verymagic. Use "\\\{n,m\}"" to get "\{n,m}". |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
5008
diff
changeset
|
10265 */ |
7 | 10266 if (*++p == '?' |
10267 #ifdef BACKSLASH_IN_FILENAME | |
10268 && no_bslash | |
10269 #endif | |
10270 ) | |
10271 reg_pat[i++] = '?'; | |
10272 else | |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
5008
diff
changeset
|
10273 if (*p == ',' || *p == '%' || *p == '#' |
5259
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10274 || *p == ' ' || *p == '{' || *p == '}') |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10275 reg_pat[i++] = *p; |
5259
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10276 else if (*p == '\\' && p[1] == '\\' && p[2] == '{') |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10277 { |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10278 reg_pat[i++] = '\\'; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10279 reg_pat[i++] = '{'; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10280 p += 2; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10281 } |
7 | 10282 else |
10283 { | |
10284 if (allow_dirs != NULL && vim_ispathsep(*p) | |
10285 #ifdef BACKSLASH_IN_FILENAME | |
10286 && (!no_bslash || *p != '\\') | |
10287 #endif | |
10288 ) | |
10289 *allow_dirs = TRUE; | |
10290 reg_pat[i++] = '\\'; | |
10291 reg_pat[i++] = *p; | |
10292 } | |
10293 break; | |
10294 #ifdef BACKSLASH_IN_FILENAME | |
10295 case '/': | |
10296 reg_pat[i++] = '['; | |
10297 reg_pat[i++] = '\\'; | |
10298 reg_pat[i++] = '/'; | |
10299 reg_pat[i++] = ']'; | |
10300 if (allow_dirs != NULL) | |
10301 *allow_dirs = TRUE; | |
10302 break; | |
10303 #endif | |
10304 case '{': | |
10305 reg_pat[i++] = '\\'; | |
10306 reg_pat[i++] = '('; | |
10307 nested++; | |
10308 break; | |
10309 case '}': | |
10310 reg_pat[i++] = '\\'; | |
10311 reg_pat[i++] = ')'; | |
10312 --nested; | |
10313 break; | |
10314 case ',': | |
10315 if (nested) | |
10316 { | |
10317 reg_pat[i++] = '\\'; | |
10318 reg_pat[i++] = '|'; | |
10319 } | |
10320 else | |
10321 reg_pat[i++] = ','; | |
10322 break; | |
10323 default: | |
10324 # ifdef FEAT_MBYTE | |
474 | 10325 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 10326 reg_pat[i++] = *p++; |
10327 else | |
10328 # endif | |
10329 if (allow_dirs != NULL && vim_ispathsep(*p)) | |
10330 *allow_dirs = TRUE; | |
10331 reg_pat[i++] = *p; | |
10332 break; | |
10333 } | |
10334 } | |
10335 if (add_dollar) | |
10336 reg_pat[i++] = '$'; | |
10337 reg_pat[i] = NUL; | |
10338 if (nested != 0) | |
10339 { | |
10340 if (nested < 0) | |
10341 EMSG(_("E219: Missing {.")); | |
10342 else | |
10343 EMSG(_("E220: Missing }.")); | |
10344 vim_free(reg_pat); | |
10345 reg_pat = NULL; | |
10346 } | |
10347 return reg_pat; | |
10348 } | |
2664 | 10349 |
10350 #if defined(EINTR) || defined(PROTO) | |
10351 /* | |
10352 * Version of read() that retries when interrupted by EINTR (possibly | |
10353 * by a SIGWINCH). | |
10354 */ | |
10355 long | |
10356 read_eintr(fd, buf, bufsize) | |
10357 int fd; | |
10358 void *buf; | |
10359 size_t bufsize; | |
10360 { | |
10361 long ret; | |
10362 | |
10363 for (;;) | |
10364 { | |
10365 ret = vim_read(fd, buf, bufsize); | |
10366 if (ret >= 0 || errno != EINTR) | |
10367 break; | |
10368 } | |
10369 return ret; | |
10370 } | |
10371 | |
10372 /* | |
10373 * Version of write() that retries when interrupted by EINTR (possibly | |
10374 * by a SIGWINCH). | |
10375 */ | |
10376 long | |
10377 write_eintr(fd, buf, bufsize) | |
10378 int fd; | |
10379 void *buf; | |
10380 size_t bufsize; | |
10381 { | |
10382 long ret = 0; | |
10383 long wlen; | |
10384 | |
10385 /* Repeat the write() so long it didn't fail, other than being interrupted | |
10386 * by a signal. */ | |
10387 while (ret < (long)bufsize) | |
10388 { | |
2666 | 10389 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret); |
2664 | 10390 if (wlen < 0) |
10391 { | |
10392 if (errno != EINTR) | |
10393 break; | |
10394 } | |
10395 else | |
10396 ret += wlen; | |
10397 } | |
10398 return ret; | |
10399 } | |
10400 #endif |