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