Mercurial > vim
annotate src/fileio.c @ 4352:04736b4030ec v7.3.925
updated for version 7.3.925
Problem: Typos in source files.
Solution: Fix the typos. (Ken Takata)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Mon, 06 May 2013 04:24:17 +0200 |
parents | d8053e6bf72a |
children | c80838526eeb |
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 #ifdef FEAT_CRYPT | |
2204
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
28 /* crypt_magic[0] is pkzip crypt, crypt_magic[1] is sha2+blowfish */ |
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
29 static char *crypt_magic[] = {"VimCrypt~01!", "VimCrypt~02!"}; |
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
30 static char crypt_magic_head[] = "VimCrypt~"; |
7 | 31 # define CRYPT_MAGIC_LEN 12 /* must be multiple of 4! */ |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
32 |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
33 /* For blowfish, after the magic header, we store 8 bytes of salt and then 8 |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
34 * bytes of seed (initialisation vector). */ |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
35 static int crypt_salt_len[] = {0, 8}; |
2204
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
36 static int crypt_seed_len[] = {0, 8}; |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
37 #define CRYPT_SALT_LEN_MAX 8 |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
38 #define CRYPT_SEED_LEN_MAX 8 |
7 | 39 #endif |
40 | |
41 /* Is there any system that doesn't have access()? */ | |
574 | 42 #define USE_MCH_ACCESS |
7 | 43 |
1313 | 44 #if defined(sun) && defined(S_ISCHR) |
45 # define OPEN_CHR_FILES | |
46 static int is_dev_fd_file(char_u *fname); | |
47 #endif | |
7 | 48 #ifdef FEAT_MBYTE |
49 static char_u *next_fenc __ARGS((char_u **pp)); | |
50 # ifdef FEAT_EVAL | |
51 static char_u *readfile_charconvert __ARGS((char_u *fname, char_u *fenc, int *fdp)); | |
52 # endif | |
53 #endif | |
54 #ifdef FEAT_VIMINFO | |
55 static void check_marks_read __ARGS((void)); | |
56 #endif | |
57 #ifdef FEAT_CRYPT | |
2360
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
58 static int crypt_method_from_magic __ARGS((char *ptr, int len)); |
2267 | 59 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 | 60 #endif |
61 #ifdef UNIX | |
62 static void set_file_time __ARGS((char_u *fname, time_t atime, time_t mtime)); | |
63 #endif | |
633 | 64 static int set_rw_fname __ARGS((char_u *fname, char_u *sfname)); |
7 | 65 static int msg_add_fileformat __ARGS((int eol_type)); |
66 static void msg_add_eol __ARGS((void)); | |
67 static int check_mtime __ARGS((buf_T *buf, struct stat *s)); | |
68 static int time_differs __ARGS((long t1, long t2)); | |
69 #ifdef FEAT_AUTOCMD | |
661 | 70 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 | 71 static int au_find_group __ARGS((char_u *name)); |
72 | |
73 # define AUGROUP_DEFAULT -1 /* default autocmd group */ | |
1834 | 74 # define AUGROUP_ERROR -2 /* erroneous autocmd group */ |
676 | 75 # define AUGROUP_ALL -3 /* all autocmd groups */ |
7 | 76 #endif |
77 | |
78 #if defined(FEAT_CRYPT) || defined(FEAT_MBYTE) | |
79 # define HAS_BW_FLAGS | |
80 # define FIO_LATIN1 0x01 /* convert Latin1 */ | |
81 # define FIO_UTF8 0x02 /* convert UTF-8 */ | |
82 # define FIO_UCS2 0x04 /* convert UCS-2 */ | |
83 # define FIO_UCS4 0x08 /* convert UCS-4 */ | |
84 # define FIO_UTF16 0x10 /* convert UTF-16 */ | |
85 # ifdef WIN3264 | |
86 # define FIO_CODEPAGE 0x20 /* convert MS-Windows codepage */ | |
87 # define FIO_PUT_CP(x) (((x) & 0xffff) << 16) /* put codepage in top word */ | |
88 # define FIO_GET_CP(x) (((x)>>16) & 0xffff) /* get codepage from top word */ | |
89 # endif | |
90 # ifdef MACOS_X | |
91 # define FIO_MACROMAN 0x20 /* convert MacRoman */ | |
92 # endif | |
93 # define FIO_ENDIAN_L 0x80 /* little endian */ | |
94 # define FIO_ENCRYPTED 0x1000 /* encrypt written bytes */ | |
95 # define FIO_NOCONVERT 0x2000 /* skip encoding conversion */ | |
96 # define FIO_UCSBOM 0x4000 /* check for BOM at start of file */ | |
97 # define FIO_ALL -1 /* allow all formats */ | |
98 #endif | |
99 | |
100 /* When converting, a read() or write() may leave some bytes to be converted | |
101 * for the next call. The value is guessed... */ | |
102 #define CONV_RESTLEN 30 | |
103 | |
104 /* We have to guess how much a sequence of bytes may expand when converting | |
105 * with iconv() to be able to allocate a buffer. */ | |
106 #define ICONV_MULT 8 | |
107 | |
108 /* | |
109 * Structure to pass arguments from buf_write() to buf_write_bytes(). | |
110 */ | |
111 struct bw_info | |
112 { | |
113 int bw_fd; /* file descriptor */ | |
114 char_u *bw_buf; /* buffer with data to be written */ | |
1411 | 115 int bw_len; /* length of data */ |
7 | 116 #ifdef HAS_BW_FLAGS |
117 int bw_flags; /* FIO_ flags */ | |
118 #endif | |
119 #ifdef FEAT_MBYTE | |
120 char_u bw_rest[CONV_RESTLEN]; /* not converted bytes */ | |
121 int bw_restlen; /* nr of bytes in bw_rest[] */ | |
122 int bw_first; /* first write call */ | |
123 char_u *bw_conv_buf; /* buffer for writing converted chars */ | |
124 int bw_conv_buflen; /* size of bw_conv_buf */ | |
125 int bw_conv_error; /* set for conversion error */ | |
1947 | 126 linenr_T bw_conv_error_lnum; /* first line with error or zero */ |
127 linenr_T bw_start_lnum; /* line number at start of buffer */ | |
7 | 128 # ifdef USE_ICONV |
129 iconv_t bw_iconv_fd; /* descriptor for iconv() or -1 */ | |
130 # endif | |
131 #endif | |
132 }; | |
133 | |
134 static int buf_write_bytes __ARGS((struct bw_info *ip)); | |
135 | |
136 #ifdef FEAT_MBYTE | |
595 | 137 static linenr_T readfile_linenr __ARGS((linenr_T linecnt, char_u *p, char_u *endp)); |
7 | 138 static int ucs2bytes __ARGS((unsigned c, char_u **pp, int flags)); |
1948 | 139 static int need_conversion __ARGS((char_u *fenc)); |
7 | 140 static int get_fio_flags __ARGS((char_u *ptr)); |
141 static char_u *check_for_bom __ARGS((char_u *p, long size, int *lenp, int flags)); | |
142 static int make_bom __ARGS((char_u *buf, char_u *name)); | |
143 # ifdef WIN3264 | |
144 static int get_win_fio_flags __ARGS((char_u *ptr)); | |
145 # endif | |
146 # ifdef MACOS_X | |
147 static int get_mac_fio_flags __ARGS((char_u *ptr)); | |
148 # endif | |
149 #endif | |
150 static int move_lines __ARGS((buf_T *frombuf, buf_T *tobuf)); | |
2006 | 151 #ifdef TEMPDIRNAMES |
1997 | 152 static void vim_settempdir __ARGS((char_u *tempdir)); |
2006 | 153 #endif |
1834 | 154 #ifdef FEAT_AUTOCMD |
155 static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name"); | |
156 #endif | |
595 | 157 |
7 | 158 void |
159 filemess(buf, name, s, attr) | |
160 buf_T *buf; | |
161 char_u *name; | |
162 char_u *s; | |
163 int attr; | |
164 { | |
165 int msg_scroll_save; | |
166 | |
167 if (msg_silent != 0) | |
168 return; | |
169 msg_add_fname(buf, name); /* put file name in IObuff with quotes */ | |
170 /* If it's extremely long, truncate it. */ | |
171 if (STRLEN(IObuff) > IOSIZE - 80) | |
172 IObuff[IOSIZE - 80] = NUL; | |
173 STRCAT(IObuff, s); | |
174 /* | |
175 * For the first message may have to start a new line. | |
176 * For further ones overwrite the previous one, reset msg_scroll before | |
177 * calling filemess(). | |
178 */ | |
179 msg_scroll_save = msg_scroll; | |
180 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) | |
181 msg_scroll = FALSE; | |
182 if (!msg_scroll) /* wait a bit when overwriting an error msg */ | |
183 check_for_delay(FALSE); | |
184 msg_start(); | |
185 msg_scroll = msg_scroll_save; | |
186 msg_scrolled_ign = TRUE; | |
187 /* may truncate the message to avoid a hit-return prompt */ | |
188 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr); | |
189 msg_clr_eos(); | |
190 out_flush(); | |
191 msg_scrolled_ign = FALSE; | |
192 } | |
193 | |
194 /* | |
195 * Read lines from file "fname" into the buffer after line "from". | |
196 * | |
197 * 1. We allocate blocks with lalloc, as big as possible. | |
198 * 2. Each block is filled with characters from the file with a single read(). | |
199 * 3. The lines are inserted in the buffer with ml_append(). | |
200 * | |
201 * (caller must check that fname != NULL, unless READ_STDIN is used) | |
202 * | |
203 * "lines_to_skip" is the number of lines that must be skipped | |
204 * "lines_to_read" is the number of lines that are appended | |
205 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM. | |
206 * | |
207 * flags: | |
208 * READ_NEW starting to edit a new buffer | |
209 * READ_FILTER reading filter output | |
210 * READ_STDIN read from stdin instead of a file | |
211 * READ_BUFFER read from curbuf instead of a file (converting after reading | |
212 * stdin) | |
213 * 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
|
214 * READ_KEEP_UNDO don't clear undo info or read it from a file |
7 | 215 * |
216 * return FAIL for failure, OK otherwise | |
217 */ | |
218 int | |
219 readfile(fname, sfname, from, lines_to_skip, lines_to_read, eap, flags) | |
220 char_u *fname; | |
221 char_u *sfname; | |
222 linenr_T from; | |
223 linenr_T lines_to_skip; | |
224 linenr_T lines_to_read; | |
225 exarg_T *eap; /* can be NULL! */ | |
226 int flags; | |
227 { | |
228 int fd = 0; | |
229 int newfile = (flags & READ_NEW); | |
230 int check_readonly; | |
231 int filtering = (flags & READ_FILTER); | |
232 int read_stdin = (flags & READ_STDIN); | |
233 int read_buffer = (flags & READ_BUFFER); | |
1486 | 234 int set_options = newfile || read_buffer |
235 || (eap != NULL && eap->read_edit); | |
7 | 236 linenr_T read_buf_lnum = 1; /* next line to read from curbuf */ |
237 colnr_T read_buf_col = 0; /* next char to read from this line */ | |
238 char_u c; | |
239 linenr_T lnum = from; | |
240 char_u *ptr = NULL; /* pointer into read buffer */ | |
241 char_u *buffer = NULL; /* read buffer */ | |
242 char_u *new_buffer = NULL; /* init to shut up gcc */ | |
243 char_u *line_start = NULL; /* init to shut up gcc */ | |
244 int wasempty; /* buffer was empty before reading */ | |
245 colnr_T len; | |
246 long size = 0; | |
247 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
|
248 off_t filesize = 0; |
7 | 249 int skip_read = FALSE; |
250 #ifdef FEAT_CRYPT | |
251 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
|
252 int did_ask_for_key = FALSE; |
2717 | 253 int crypt_method_used; |
7 | 254 #endif |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
255 #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
|
256 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
|
257 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
|
258 #endif |
7 | 259 int split = 0; /* number of split lines */ |
260 #define UNKNOWN 0x0fffffff /* file size is unknown */ | |
261 linenr_T linecnt; | |
262 int error = FALSE; /* errors encountered */ | |
263 int ff_error = EOL_UNKNOWN; /* file format with errors */ | |
264 long linerest = 0; /* remaining chars in line */ | |
265 #ifdef UNIX | |
266 int perm = 0; | |
267 int swap_mode = -1; /* protection bits for swap file */ | |
268 #else | |
269 int perm; | |
270 #endif | |
271 int fileformat = 0; /* end-of-line format */ | |
272 int keep_fileformat = FALSE; | |
273 struct stat st; | |
274 int file_readonly; | |
275 linenr_T skip_count = 0; | |
276 linenr_T read_count = 0; | |
277 int msg_save = msg_scroll; | |
278 linenr_T read_no_eol_lnum = 0; /* non-zero lnum when last line of | |
279 * last read was missing the eol */ | |
280 int try_mac = (vim_strchr(p_ffs, 'm') != NULL); | |
281 int try_dos = (vim_strchr(p_ffs, 'd') != NULL); | |
282 int try_unix = (vim_strchr(p_ffs, 'x') != NULL); | |
283 int file_rewind = FALSE; | |
284 #ifdef FEAT_MBYTE | |
285 int can_retry; | |
595 | 286 linenr_T conv_error = 0; /* line nr with conversion error */ |
287 linenr_T illegal_byte = 0; /* line nr with illegal byte */ | |
7 | 288 int keep_dest_enc = FALSE; /* don't retry when char doesn't fit |
289 in destination encoding */ | |
595 | 290 int bad_char_behavior = BAD_REPLACE; |
291 /* BAD_KEEP, BAD_DROP or character to | |
292 * replace with */ | |
7 | 293 char_u *tmpname = NULL; /* name of 'charconvert' output file */ |
294 int fio_flags = 0; | |
295 char_u *fenc; /* fileencoding to use */ | |
296 int fenc_alloced; /* fenc_next is in allocated memory */ | |
297 char_u *fenc_next = NULL; /* next item in 'fencs' or NULL */ | |
298 int advance_fenc = FALSE; | |
299 long real_size = 0; | |
300 # ifdef USE_ICONV | |
301 iconv_t iconv_fd = (iconv_t)-1; /* descriptor for iconv() or -1 */ | |
302 # ifdef FEAT_EVAL | |
303 int did_iconv = FALSE; /* TRUE when iconv() failed and trying | |
304 'charconvert' next */ | |
305 # endif | |
306 # endif | |
307 int converted = FALSE; /* TRUE if conversion done */ | |
308 int notconverted = FALSE; /* TRUE if conversion wanted but it | |
309 wasn't possible */ | |
310 char_u conv_rest[CONV_RESTLEN]; | |
311 int conv_restlen = 0; /* nr of bytes in conv_rest[] */ | |
312 #endif | |
1834 | 313 #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
|
314 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
|
315 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
|
316 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
|
317 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
|
318 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
|
319 #endif |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
320 |
2707 | 321 curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */ |
7 | 322 |
323 /* | |
324 * If there is no file name yet, use the one for the read file. | |
325 * BF_NOTEDITED is set to reflect this. | |
326 * Don't do this for a read from a filter. | |
327 * Only do this when 'cpoptions' contains the 'f' flag. | |
328 */ | |
329 if (curbuf->b_ffname == NULL | |
330 && !filtering | |
331 && fname != NULL | |
332 && vim_strchr(p_cpo, CPO_FNAMER) != NULL | |
333 && !(flags & READ_DUMMY)) | |
334 { | |
633 | 335 if (set_rw_fname(fname, sfname) == FAIL) |
336 return FAIL; | |
7 | 337 } |
338 | |
2563
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
339 #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
|
340 /* 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
|
341 * 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
|
342 * 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
|
343 * 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
|
344 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
|
345 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
|
346 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
|
347 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
|
348 || (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
|
349 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
|
350 #endif |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
351 |
167 | 352 /* After reading a file the cursor line changes but we don't want to |
353 * display the line. */ | |
354 ex_no_reprint = TRUE; | |
355 | |
968 | 356 /* don't display the file info for another buffer now */ |
357 need_fileinfo = FALSE; | |
358 | |
7 | 359 /* |
360 * For Unix: Use the short file name whenever possible. | |
361 * Avoids problems with networks and when directory names are changed. | |
362 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to | |
363 * another directory, which we don't detect. | |
364 */ | |
365 if (sfname == NULL) | |
366 sfname = fname; | |
367 #if defined(UNIX) || defined(__EMX__) | |
368 fname = sfname; | |
369 #endif | |
370 | |
371 #ifdef FEAT_AUTOCMD | |
372 /* | |
373 * The BufReadCmd and FileReadCmd events intercept the reading process by | |
374 * executing the associated commands instead. | |
375 */ | |
376 if (!filtering && !read_stdin && !read_buffer) | |
377 { | |
378 pos_T pos; | |
379 | |
380 pos = curbuf->b_op_start; | |
381 | |
382 /* Set '[ mark to the line above where the lines go (line 1 if zero). */ | |
383 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); | |
384 curbuf->b_op_start.col = 0; | |
385 | |
386 if (newfile) | |
387 { | |
388 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname, | |
389 FALSE, curbuf, eap)) | |
390 #ifdef FEAT_EVAL | |
391 return aborting() ? FAIL : OK; | |
392 #else | |
393 return OK; | |
394 #endif | |
395 } | |
396 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname, | |
397 FALSE, NULL, eap)) | |
398 #ifdef FEAT_EVAL | |
399 return aborting() ? FAIL : OK; | |
400 #else | |
401 return OK; | |
402 #endif | |
403 | |
404 curbuf->b_op_start = pos; | |
405 } | |
406 #endif | |
407 | |
408 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0) | |
409 msg_scroll = FALSE; /* overwrite previous file message */ | |
410 else | |
411 msg_scroll = TRUE; /* don't overwrite previous file message */ | |
412 | |
413 /* | |
414 * If the name ends in a path separator, we can't open it. Check here, | |
415 * because reading the file may actually work, but then creating the swap | |
416 * file may destroy it! Reported on MS-DOS and Win 95. | |
417 * If the name is too long we might crash further on, quit here. | |
418 */ | |
23 | 419 if (fname != NULL && *fname != NUL) |
420 { | |
39 | 421 p = fname + STRLEN(fname); |
422 if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL) | |
23 | 423 { |
424 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0); | |
425 msg_end(); | |
426 msg_scroll = msg_save; | |
427 return FAIL; | |
428 } | |
7 | 429 } |
430 | |
431 #ifdef UNIX | |
432 /* | |
433 * On Unix it is possible to read a directory, so we have to | |
434 * check for it before the mch_open(). | |
435 */ | |
436 if (!read_stdin && !read_buffer) | |
437 { | |
438 perm = mch_getperm(fname); | |
439 if (perm >= 0 && !S_ISREG(perm) /* not a regular file ... */ | |
440 # ifdef S_ISFIFO | |
441 && !S_ISFIFO(perm) /* ... or fifo */ | |
442 # endif | |
443 # ifdef S_ISSOCK | |
444 && !S_ISSOCK(perm) /* ... or socket */ | |
445 # endif | |
1313 | 446 # ifdef OPEN_CHR_FILES |
447 && !(S_ISCHR(perm) && is_dev_fd_file(fname)) | |
448 /* ... or a character special file named /dev/fd/<n> */ | |
449 # endif | |
7 | 450 ) |
451 { | |
452 if (S_ISDIR(perm)) | |
453 filemess(curbuf, fname, (char_u *)_("is a directory"), 0); | |
454 else | |
455 filemess(curbuf, fname, (char_u *)_("is not a file"), 0); | |
456 msg_end(); | |
457 msg_scroll = msg_save; | |
458 return FAIL; | |
459 } | |
1006 | 460 |
461 # if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
462 /* | |
463 * MS-Windows allows opening a device, but we will probably get stuck | |
464 * trying to read it. | |
465 */ | |
466 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE) | |
467 { | |
1303 | 468 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0); |
1006 | 469 msg_end(); |
470 msg_scroll = msg_save; | |
471 return FAIL; | |
472 } | |
473 # endif | |
1004 | 474 } |
475 #endif | |
476 | |
7 | 477 /* set default 'fileformat' */ |
819 | 478 if (set_options) |
7 | 479 { |
480 if (eap != NULL && eap->force_ff != 0) | |
481 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL); | |
482 else if (*p_ffs != NUL) | |
483 set_fileformat(default_fileformat(), OPT_LOCAL); | |
484 } | |
485 | |
486 /* set or reset 'binary' */ | |
487 if (eap != NULL && eap->force_bin != 0) | |
488 { | |
489 int oldval = curbuf->b_p_bin; | |
490 | |
491 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN); | |
492 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL); | |
493 } | |
494 | |
495 /* | |
496 * When opening a new file we take the readonly flag from the file. | |
497 * Default is r/w, can be set to r/o below. | |
498 * Don't reset it when in readonly mode | |
499 * Only set/reset b_p_ro when BF_CHECK_RO is set. | |
500 */ | |
501 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO)); | |
164 | 502 if (check_readonly && !readonlymode) |
7 | 503 curbuf->b_p_ro = FALSE; |
504 | |
505 if (newfile && !read_stdin && !read_buffer) | |
506 { | |
2823 | 507 /* Remember time of file. */ |
7 | 508 if (mch_stat((char *)fname, &st) >= 0) |
509 { | |
510 buf_store_time(curbuf, &st, fname); | |
511 curbuf->b_mtime_read = curbuf->b_mtime; | |
512 #ifdef UNIX | |
513 /* | |
514 * Use the protection bits of the original file for the swap file. | |
515 * This makes it possible for others to read the name of the | |
516 * edited file from the swapfile, but only if they can read the | |
517 * edited file. | |
518 * Remove the "write" and "execute" bits for group and others | |
519 * (they must not write the swapfile). | |
520 * Add the "read" and "write" bits for the user, otherwise we may | |
521 * not be able to write to the file ourselves. | |
522 * Setting the bits is done below, after creating the swap file. | |
523 */ | |
524 swap_mode = (st.st_mode & 0644) | 0600; | |
525 #endif | |
526 #ifdef FEAT_CW_EDITOR | |
527 /* Get the FSSpec on MacOS | |
528 * TODO: Update it properly when the buffer name changes | |
529 */ | |
530 (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec); | |
531 #endif | |
532 #ifdef VMS | |
533 curbuf->b_fab_rfm = st.st_fab_rfm; | |
22 | 534 curbuf->b_fab_rat = st.st_fab_rat; |
535 curbuf->b_fab_mrs = st.st_fab_mrs; | |
7 | 536 #endif |
537 } | |
538 else | |
539 { | |
540 curbuf->b_mtime = 0; | |
541 curbuf->b_mtime_read = 0; | |
542 curbuf->b_orig_size = 0; | |
543 curbuf->b_orig_mode = 0; | |
544 } | |
545 | |
546 /* Reset the "new file" flag. It will be set again below when the | |
547 * file doesn't exist. */ | |
548 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W); | |
549 } | |
550 | |
551 /* | |
552 * for UNIX: check readonly with perm and mch_access() | |
553 * for MSDOS and Amiga: check readonly by trying to open the file for writing | |
554 */ | |
555 file_readonly = FALSE; | |
556 if (read_stdin) | |
557 { | |
558 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
559 /* Force binary I/O on stdin to avoid CR-LF -> LF conversion. */ | |
560 setmode(0, O_BINARY); | |
561 #endif | |
562 } | |
563 else if (!read_buffer) | |
564 { | |
565 #ifdef USE_MCH_ACCESS | |
566 if ( | |
567 # ifdef UNIX | |
568 !(perm & 0222) || | |
569 # endif | |
570 mch_access((char *)fname, W_OK)) | |
571 file_readonly = TRUE; | |
572 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
573 #else | |
574 if (!newfile | |
575 || readonlymode | |
576 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0) | |
577 { | |
578 file_readonly = TRUE; | |
579 /* try to open ro */ | |
580 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
581 } | |
582 #endif | |
583 } | |
584 | |
585 if (fd < 0) /* cannot open at all */ | |
586 { | |
587 #ifndef UNIX | |
588 int isdir_f; | |
589 #endif | |
590 msg_scroll = msg_save; | |
591 #ifndef UNIX | |
592 /* | |
593 * On MSDOS and Amiga we can't open a directory, check here. | |
594 */ | |
595 isdir_f = (mch_isdir(fname)); | |
596 perm = mch_getperm(fname); /* check if the file exists */ | |
597 if (isdir_f) | |
598 { | |
599 filemess(curbuf, sfname, (char_u *)_("is a directory"), 0); | |
600 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
601 } | |
602 else | |
603 #endif | |
604 if (newfile) | |
605 { | |
2147
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
606 if (perm < 0 |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
607 #ifdef ENOENT |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
608 && errno == ENOENT |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
609 #endif |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
610 ) |
7 | 611 { |
612 /* | |
613 * Set the 'new-file' flag, so that when the file has | |
614 * been created by someone else, a ":w" will complain. | |
615 */ | |
616 curbuf->b_flags |= BF_NEW; | |
617 | |
618 /* Create a swap file now, so that other Vims are warned | |
619 * that we are editing this file. Don't do this for a | |
620 * "nofile" or "nowrite" buffer type. */ | |
621 #ifdef FEAT_QUICKFIX | |
622 if (!bt_dontwrite(curbuf)) | |
623 #endif | |
1834 | 624 { |
7 | 625 check_need_swap(newfile); |
1834 | 626 #ifdef FEAT_AUTOCMD |
627 /* SwapExists autocommand may mess things up */ | |
628 if (curbuf != old_curbuf | |
629 || (using_b_ffname | |
630 && (old_b_ffname != curbuf->b_ffname)) | |
631 || (using_b_fname | |
632 && (old_b_fname != curbuf->b_fname))) | |
633 { | |
634 EMSG(_(e_auchangedbuf)); | |
635 return FAIL; | |
636 } | |
637 #endif | |
638 } | |
592 | 639 if (dir_of_file_exists(fname)) |
640 filemess(curbuf, sfname, (char_u *)_("[New File]"), 0); | |
641 else | |
642 filemess(curbuf, sfname, | |
643 (char_u *)_("[New DIRECTORY]"), 0); | |
7 | 644 #ifdef FEAT_VIMINFO |
645 /* Even though this is a new file, it might have been | |
646 * edited before and deleted. Get the old marks. */ | |
647 check_marks_read(); | |
648 #endif | |
649 #ifdef FEAT_MBYTE | |
650 if (eap != NULL && eap->force_enc != 0) | |
651 { | |
652 /* set forced 'fileencoding' */ | |
653 fenc = enc_canonize(eap->cmd + eap->force_enc); | |
654 if (fenc != NULL) | |
655 set_string_option_direct((char_u *)"fenc", -1, | |
694 | 656 fenc, OPT_FREE|OPT_LOCAL, 0); |
7 | 657 vim_free(fenc); |
658 } | |
659 #endif | |
660 #ifdef FEAT_AUTOCMD | |
661 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname, | |
662 FALSE, curbuf, eap); | |
663 #endif | |
664 /* remember the current fileformat */ | |
665 save_file_ff(curbuf); | |
666 | |
667 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
668 if (aborting()) /* autocmds may abort script processing */ | |
669 return FAIL; | |
670 #endif | |
671 return OK; /* a new file is not an error */ | |
672 } | |
673 else | |
674 { | |
550 | 675 filemess(curbuf, sfname, (char_u *)( |
676 # ifdef EFBIG | |
677 (errno == EFBIG) ? _("[File too big]") : | |
678 # endif | |
2147
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
679 # ifdef EOVERFLOW |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
680 (errno == EOVERFLOW) ? _("[File too big]") : |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
681 # endif |
550 | 682 _("[Permission Denied]")), 0); |
7 | 683 curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
684 } | |
685 } | |
686 | |
687 return FAIL; | |
688 } | |
689 | |
690 /* | |
691 * Only set the 'ro' flag for readonly files the first time they are | |
692 * loaded. Help files always get readonly mode | |
693 */ | |
694 if ((check_readonly && file_readonly) || curbuf->b_help) | |
695 curbuf->b_p_ro = TRUE; | |
696 | |
819 | 697 if (set_options) |
7 | 698 { |
1486 | 699 /* Don't change 'eol' if reading from buffer as it will already be |
700 * correctly set when reading stdin. */ | |
701 if (!read_buffer) | |
702 { | |
703 curbuf->b_p_eol = TRUE; | |
704 curbuf->b_start_eol = TRUE; | |
705 } | |
7 | 706 #ifdef FEAT_MBYTE |
707 curbuf->b_p_bomb = FALSE; | |
1352 | 708 curbuf->b_start_bomb = FALSE; |
7 | 709 #endif |
710 } | |
711 | |
712 /* Create a swap file now, so that other Vims are warned that we are | |
713 * editing this file. | |
714 * Don't do this for a "nofile" or "nowrite" buffer type. */ | |
715 #ifdef FEAT_QUICKFIX | |
716 if (!bt_dontwrite(curbuf)) | |
717 #endif | |
718 { | |
719 check_need_swap(newfile); | |
1834 | 720 #ifdef FEAT_AUTOCMD |
721 if (!read_stdin && (curbuf != old_curbuf | |
722 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) | |
723 || (using_b_fname && (old_b_fname != curbuf->b_fname)))) | |
724 { | |
725 EMSG(_(e_auchangedbuf)); | |
726 if (!read_buffer) | |
727 close(fd); | |
728 return FAIL; | |
729 } | |
730 #endif | |
7 | 731 #ifdef UNIX |
732 /* Set swap file protection bits after creating it. */ | |
1918 | 733 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL |
734 && curbuf->b_ml.ml_mfp->mf_fname != NULL) | |
7 | 735 (void)mch_setperm(curbuf->b_ml.ml_mfp->mf_fname, (long)swap_mode); |
736 #endif | |
737 } | |
738 | |
578 | 739 #if defined(HAS_SWAP_EXISTS_ACTION) |
7 | 740 /* If "Quit" selected at ATTENTION dialog, don't load the file */ |
741 if (swap_exists_action == SEA_QUIT) | |
742 { | |
743 if (!read_buffer && !read_stdin) | |
744 close(fd); | |
745 return FAIL; | |
746 } | |
747 #endif | |
748 | |
749 ++no_wait_return; /* don't wait for return yet */ | |
750 | |
751 /* | |
752 * Set '[ mark to the line above where the lines go (line 1 if zero). | |
753 */ | |
754 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); | |
755 curbuf->b_op_start.col = 0; | |
756 | |
757 #ifdef FEAT_AUTOCMD | |
758 if (!read_buffer) | |
759 { | |
760 int m = msg_scroll; | |
761 int n = msg_scrolled; | |
762 | |
763 /* | |
764 * The file must be closed again, the autocommands may want to change | |
765 * the file before reading it. | |
766 */ | |
767 if (!read_stdin) | |
768 close(fd); /* ignore errors */ | |
769 | |
770 /* | |
771 * The output from the autocommands should not overwrite anything and | |
772 * should not be overwritten: Set msg_scroll, restore its value if no | |
773 * output was done. | |
774 */ | |
775 msg_scroll = TRUE; | |
776 if (filtering) | |
777 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname, | |
778 FALSE, curbuf, eap); | |
779 else if (read_stdin) | |
780 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname, | |
781 FALSE, curbuf, eap); | |
782 else if (newfile) | |
783 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname, | |
784 FALSE, curbuf, eap); | |
785 else | |
786 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname, | |
787 FALSE, NULL, eap); | |
788 if (msg_scrolled == n) | |
789 msg_scroll = m; | |
790 | |
791 #ifdef FEAT_EVAL | |
792 if (aborting()) /* autocmds may abort script processing */ | |
793 { | |
794 --no_wait_return; | |
795 msg_scroll = msg_save; | |
796 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
797 return FAIL; | |
798 } | |
799 #endif | |
800 /* | |
801 * Don't allow the autocommands to change the current buffer. | |
802 * Try to re-open the file. | |
1834 | 803 * |
804 * Don't allow the autocommands to change the buffer name either | |
805 * (cd for example) if it invalidates fname or sfname. | |
7 | 806 */ |
807 if (!read_stdin && (curbuf != old_curbuf | |
1834 | 808 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) |
809 || (using_b_fname && (old_b_fname != curbuf->b_fname)) | |
7 | 810 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0)) |
811 { | |
812 --no_wait_return; | |
813 msg_scroll = msg_save; | |
814 if (fd < 0) | |
815 EMSG(_("E200: *ReadPre autocommands made the file unreadable")); | |
816 else | |
817 EMSG(_("E201: *ReadPre autocommands must not change current buffer")); | |
818 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
819 return FAIL; | |
820 } | |
821 } | |
822 #endif /* FEAT_AUTOCMD */ | |
823 | |
824 /* Autocommands may add lines to the file, need to check if it is empty */ | |
825 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY); | |
826 | |
827 if (!recoverymode && !filtering && !(flags & READ_DUMMY)) | |
828 { | |
829 /* | |
830 * Show the user that we are busy reading the input. Sometimes this | |
831 * may take a while. When reading from stdin another program may | |
832 * still be running, don't move the cursor to the last line, unless | |
833 * always using the GUI. | |
834 */ | |
835 if (read_stdin) | |
836 { | |
837 #ifndef ALWAYS_USE_GUI | |
838 mch_msg(_("Vim: Reading from stdin...\n")); | |
839 #endif | |
840 #ifdef FEAT_GUI | |
841 /* Also write a message in the GUI window, if there is one. */ | |
842 if (gui.in_use && !gui.dying && !gui.starting) | |
843 { | |
844 p = (char_u *)_("Reading from stdin..."); | |
845 gui_write(p, (int)STRLEN(p)); | |
846 } | |
847 #endif | |
848 } | |
849 else if (!read_buffer) | |
850 filemess(curbuf, sfname, (char_u *)"", 0); | |
851 } | |
852 | |
853 msg_scroll = FALSE; /* overwrite the file message */ | |
854 | |
855 /* | |
856 * Set linecnt now, before the "retry" caused by a wrong guess for | |
857 * fileformat, and after the autocommands, which may change them. | |
858 */ | |
859 linecnt = curbuf->b_ml.ml_line_count; | |
860 | |
861 #ifdef FEAT_MBYTE | |
595 | 862 /* "++bad=" argument. */ |
863 if (eap != NULL && eap->bad_char != 0) | |
612 | 864 { |
595 | 865 bad_char_behavior = eap->bad_char; |
819 | 866 if (set_options) |
612 | 867 curbuf->b_bad_char = eap->bad_char; |
868 } | |
869 else | |
870 curbuf->b_bad_char = 0; | |
595 | 871 |
7 | 872 /* |
595 | 873 * Decide which 'encoding' to use or use first. |
7 | 874 */ |
875 if (eap != NULL && eap->force_enc != 0) | |
876 { | |
877 fenc = enc_canonize(eap->cmd + eap->force_enc); | |
878 fenc_alloced = TRUE; | |
595 | 879 keep_dest_enc = TRUE; |
7 | 880 } |
881 else if (curbuf->b_p_bin) | |
882 { | |
883 fenc = (char_u *)""; /* binary: don't convert */ | |
884 fenc_alloced = FALSE; | |
885 } | |
886 else if (curbuf->b_help) | |
887 { | |
888 char_u firstline[80]; | |
301 | 889 int fc; |
7 | 890 |
891 /* Help files are either utf-8 or latin1. Try utf-8 first, if this | |
892 * fails it must be latin1. | |
893 * Always do this when 'encoding' is "utf-8". Otherwise only do | |
894 * this when needed to avoid [converted] remarks all the time. | |
895 * It is needed when the first line contains non-ASCII characters. | |
896 * That is only in *.??x files. */ | |
897 fenc = (char_u *)"latin1"; | |
898 c = enc_utf8; | |
301 | 899 if (!c && !read_stdin) |
900 { | |
901 fc = fname[STRLEN(fname) - 1]; | |
902 if (TOLOWER_ASC(fc) == 'x') | |
903 { | |
904 /* Read the first line (and a bit more). Immediately rewind to | |
905 * the start of the file. If the read() fails "len" is -1. */ | |
2664 | 906 len = read_eintr(fd, firstline, 80); |
301 | 907 lseek(fd, (off_t)0L, SEEK_SET); |
908 for (p = firstline; p < firstline + len; ++p) | |
909 if (*p >= 0x80) | |
910 { | |
911 c = TRUE; | |
912 break; | |
913 } | |
914 } | |
7 | 915 } |
916 | |
917 if (c) | |
918 { | |
919 fenc_next = fenc; | |
920 fenc = (char_u *)"utf-8"; | |
921 | |
922 /* When the file is utf-8 but a character doesn't fit in | |
923 * 'encoding' don't retry. In help text editing utf-8 bytes | |
924 * doesn't make sense. */ | |
844 | 925 if (!enc_utf8) |
926 keep_dest_enc = TRUE; | |
7 | 927 } |
928 fenc_alloced = FALSE; | |
929 } | |
930 else if (*p_fencs == NUL) | |
931 { | |
932 fenc = curbuf->b_p_fenc; /* use format from buffer */ | |
933 fenc_alloced = FALSE; | |
934 } | |
935 else | |
936 { | |
937 fenc_next = p_fencs; /* try items in 'fileencodings' */ | |
938 fenc = next_fenc(&fenc_next); | |
939 fenc_alloced = TRUE; | |
940 } | |
941 #endif | |
942 | |
943 /* | |
944 * Jump back here to retry reading the file in different ways. | |
945 * Reasons to retry: | |
946 * - encoding conversion failed: try another one from "fenc_next" | |
947 * - BOM detected and fenc was set, need to setup conversion | |
948 * - "fileformat" check failed: try another | |
949 * | |
950 * Variables set for special retry actions: | |
951 * "file_rewind" Rewind the file to start reading it again. | |
952 * "advance_fenc" Advance "fenc" using "fenc_next". | |
953 * "skip_read" Re-use already read bytes (BOM detected). | |
954 * "did_iconv" iconv() conversion failed, try 'charconvert'. | |
955 * "keep_fileformat" Don't reset "fileformat". | |
956 * | |
957 * Other status indicators: | |
958 * "tmpname" When != NULL did conversion with 'charconvert'. | |
959 * Output file has to be deleted afterwards. | |
960 * "iconv_fd" When != -1 did conversion with iconv(). | |
961 */ | |
962 retry: | |
963 | |
964 if (file_rewind) | |
965 { | |
966 if (read_buffer) | |
967 { | |
968 read_buf_lnum = 1; | |
969 read_buf_col = 0; | |
970 } | |
971 else if (read_stdin || lseek(fd, (off_t)0L, SEEK_SET) != 0) | |
972 { | |
973 /* Can't rewind the file, give up. */ | |
974 error = TRUE; | |
975 goto failed; | |
976 } | |
977 /* Delete the previously read lines. */ | |
978 while (lnum > from) | |
979 ml_delete(lnum--, FALSE); | |
980 file_rewind = FALSE; | |
981 #ifdef FEAT_MBYTE | |
819 | 982 if (set_options) |
1352 | 983 { |
7 | 984 curbuf->b_p_bomb = FALSE; |
1352 | 985 curbuf->b_start_bomb = FALSE; |
986 } | |
595 | 987 conv_error = 0; |
7 | 988 #endif |
989 } | |
990 | |
2267 | 991 #ifdef FEAT_CRYPT |
992 if (cryptkey != NULL) | |
993 /* Need to reset the state, but keep the key, don't want to ask for it | |
994 * again. */ | |
995 crypt_pop_state(); | |
996 #endif | |
997 | |
7 | 998 /* |
999 * When retrying with another "fenc" and the first time "fileformat" | |
1000 * will be reset. | |
1001 */ | |
1002 if (keep_fileformat) | |
1003 keep_fileformat = FALSE; | |
1004 else | |
1005 { | |
1006 if (eap != NULL && eap->force_ff != 0) | |
1742 | 1007 { |
7 | 1008 fileformat = get_fileformat_force(curbuf, eap); |
1742 | 1009 try_unix = try_dos = try_mac = FALSE; |
1010 } | |
7 | 1011 else if (curbuf->b_p_bin) |
1012 fileformat = EOL_UNIX; /* binary: use Unix format */ | |
1013 else if (*p_ffs == NUL) | |
1014 fileformat = get_fileformat(curbuf);/* use format from buffer */ | |
1015 else | |
1016 fileformat = EOL_UNKNOWN; /* detect from file */ | |
1017 } | |
1018 | |
1019 #ifdef FEAT_MBYTE | |
1020 # ifdef USE_ICONV | |
1021 if (iconv_fd != (iconv_t)-1) | |
1022 { | |
1023 /* aborted conversion with iconv(), close the descriptor */ | |
1024 iconv_close(iconv_fd); | |
1025 iconv_fd = (iconv_t)-1; | |
1026 } | |
1027 # endif | |
1028 | |
1029 if (advance_fenc) | |
1030 { | |
1031 /* | |
1032 * Try the next entry in 'fileencodings'. | |
1033 */ | |
1034 advance_fenc = FALSE; | |
1035 | |
1036 if (eap != NULL && eap->force_enc != 0) | |
1037 { | |
1038 /* Conversion given with "++cc=" wasn't possible, read | |
1039 * without conversion. */ | |
1040 notconverted = TRUE; | |
595 | 1041 conv_error = 0; |
7 | 1042 if (fenc_alloced) |
1043 vim_free(fenc); | |
1044 fenc = (char_u *)""; | |
1045 fenc_alloced = FALSE; | |
1046 } | |
1047 else | |
1048 { | |
1049 if (fenc_alloced) | |
1050 vim_free(fenc); | |
1051 if (fenc_next != NULL) | |
1052 { | |
1053 fenc = next_fenc(&fenc_next); | |
1054 fenc_alloced = (fenc_next != NULL); | |
1055 } | |
1056 else | |
1057 { | |
1058 fenc = (char_u *)""; | |
1059 fenc_alloced = FALSE; | |
1060 } | |
1061 } | |
1062 if (tmpname != NULL) | |
1063 { | |
1064 mch_remove(tmpname); /* delete converted file */ | |
1065 vim_free(tmpname); | |
1066 tmpname = NULL; | |
1067 } | |
1068 } | |
1069 | |
1070 /* | |
1948 | 1071 * Conversion may be required when the encoding of the file is different |
1072 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4. | |
7 | 1073 */ |
1074 fio_flags = 0; | |
1948 | 1075 converted = need_conversion(fenc); |
1076 if (converted) | |
7 | 1077 { |
1078 | |
1079 /* "ucs-bom" means we need to check the first bytes of the file | |
1080 * for a BOM. */ | |
1081 if (STRCMP(fenc, ENC_UCSBOM) == 0) | |
1082 fio_flags = FIO_UCSBOM; | |
1083 | |
1084 /* | |
1085 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be | |
1086 * done. This is handled below after read(). Prepare the | |
1087 * fio_flags to avoid having to parse the string each time. | |
1088 * Also check for Unicode to Latin1 conversion, because iconv() | |
1089 * appears not to handle this correctly. This works just like | |
1090 * conversion to UTF-8 except how the resulting character is put in | |
1091 * the buffer. | |
1092 */ | |
1093 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
1094 fio_flags = get_fio_flags(fenc); | |
1095 | |
1096 # ifdef WIN3264 | |
1097 /* | |
1098 * Conversion from an MS-Windows codepage to UTF-8 or another codepage | |
1099 * is handled with MultiByteToWideChar(). | |
1100 */ | |
1101 if (fio_flags == 0) | |
1102 fio_flags = get_win_fio_flags(fenc); | |
1103 # endif | |
1104 | |
1105 # ifdef MACOS_X | |
1106 /* Conversion from Apple MacRoman to latin1 or UTF-8 */ | |
1107 if (fio_flags == 0) | |
1108 fio_flags = get_mac_fio_flags(fenc); | |
1109 # endif | |
1110 | |
1111 # ifdef USE_ICONV | |
1112 /* | |
1113 * Try using iconv() if we can't convert internally. | |
1114 */ | |
1115 if (fio_flags == 0 | |
1116 # ifdef FEAT_EVAL | |
1117 && !did_iconv | |
1118 # endif | |
1119 ) | |
1120 iconv_fd = (iconv_t)my_iconv_open( | |
1121 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc); | |
1122 # endif | |
1123 | |
1124 # ifdef FEAT_EVAL | |
1125 /* | |
1126 * Use the 'charconvert' expression when conversion is required | |
1127 * and we can't do it internally or with iconv(). | |
1128 */ | |
1129 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL | |
1130 # ifdef USE_ICONV | |
1131 && iconv_fd == (iconv_t)-1 | |
1132 # endif | |
1133 ) | |
1134 { | |
1135 # ifdef USE_ICONV | |
1136 did_iconv = FALSE; | |
1137 # endif | |
1138 /* Skip conversion when it's already done (retry for wrong | |
1139 * "fileformat"). */ | |
1140 if (tmpname == NULL) | |
1141 { | |
1142 tmpname = readfile_charconvert(fname, fenc, &fd); | |
1143 if (tmpname == NULL) | |
1144 { | |
1145 /* Conversion failed. Try another one. */ | |
1146 advance_fenc = TRUE; | |
1147 if (fd < 0) | |
1148 { | |
1149 /* Re-opening the original file failed! */ | |
1150 EMSG(_("E202: Conversion made file unreadable!")); | |
1151 error = TRUE; | |
1152 goto failed; | |
1153 } | |
1154 goto retry; | |
1155 } | |
1156 } | |
1157 } | |
1158 else | |
1159 # endif | |
1160 { | |
1161 if (fio_flags == 0 | |
1162 # ifdef USE_ICONV | |
1163 && iconv_fd == (iconv_t)-1 | |
1164 # endif | |
1165 ) | |
1166 { | |
1167 /* Conversion wanted but we can't. | |
1168 * Try the next conversion in 'fileencodings' */ | |
1169 advance_fenc = TRUE; | |
1170 goto retry; | |
1171 } | |
1172 } | |
1173 } | |
1174 | |
595 | 1175 /* Set "can_retry" when it's possible to rewind the file and try with |
7 | 1176 * another "fenc" value. It's FALSE when no other "fenc" to try, reading |
595 | 1177 * stdin or fixed at a specific encoding. */ |
1178 can_retry = (*fenc != NUL && !read_stdin && !keep_dest_enc); | |
7 | 1179 #endif |
1180 | |
1181 if (!skip_read) | |
1182 { | |
1183 linerest = 0; | |
1184 filesize = 0; | |
1185 skip_count = lines_to_skip; | |
1186 read_count = lines_to_read; | |
1187 #ifdef FEAT_MBYTE | |
1188 conv_restlen = 0; | |
1189 #endif | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1190 #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
|
1191 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
|
1192 && 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
|
1193 && 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
|
1194 && !filtering |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1195 && !read_stdin |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1196 && !read_buffer); |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1197 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
|
1198 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
|
1199 #endif |
7 | 1200 } |
1201 | |
1202 while (!error && !got_int) | |
1203 { | |
1204 /* | |
1205 * We allocate as much space for the file as we can get, plus | |
1206 * space for the old line plus room for one terminating NUL. | |
1207 * The amount is limited by the fact that read() only can read | |
1208 * upto max_unsigned characters (and other things). | |
1209 */ | |
1210 #if SIZEOF_INT <= 2 | |
1211 if (linerest >= 0x7ff0) | |
1212 { | |
1213 ++split; | |
1214 *ptr = NL; /* split line by inserting a NL */ | |
1215 size = 1; | |
1216 } | |
1217 else | |
1218 #endif | |
1219 { | |
1220 if (!skip_read) | |
1221 { | |
1222 #if SIZEOF_INT > 2 | |
1076 | 1223 # if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L) |
7 | 1224 size = SSIZE_MAX; /* use max I/O size, 52K */ |
1225 # else | |
1226 size = 0x10000L; /* use buffer >= 64K */ | |
1227 # endif | |
1228 #else | |
1229 size = 0x7ff0L - linerest; /* limit buffer to 32K */ | |
1230 #endif | |
1231 | |
836 | 1232 for ( ; size >= 10; size = (long)((long_u)size >> 1)) |
7 | 1233 { |
1234 if ((new_buffer = lalloc((long_u)(size + linerest + 1), | |
1235 FALSE)) != NULL) | |
1236 break; | |
1237 } | |
1238 if (new_buffer == NULL) | |
1239 { | |
1240 do_outofmem_msg((long_u)(size * 2 + linerest + 1)); | |
1241 error = TRUE; | |
1242 break; | |
1243 } | |
1244 if (linerest) /* copy characters from the previous buffer */ | |
1245 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest); | |
1246 vim_free(buffer); | |
1247 buffer = new_buffer; | |
1248 ptr = buffer + linerest; | |
1249 line_start = buffer; | |
1250 | |
1251 #ifdef FEAT_MBYTE | |
1252 /* May need room to translate into. | |
1253 * For iconv() we don't really know the required space, use a | |
1254 * factor ICONV_MULT. | |
1255 * latin1 to utf-8: 1 byte becomes up to 2 bytes | |
1256 * utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes | |
1257 * become up to 4 bytes, size must be multiple of 2 | |
1258 * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be | |
1259 * multiple of 2 | |
1260 * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be | |
1261 * multiple of 4 */ | |
835 | 1262 real_size = (int)size; |
7 | 1263 # ifdef USE_ICONV |
1264 if (iconv_fd != (iconv_t)-1) | |
1265 size = size / ICONV_MULT; | |
1266 else | |
1267 # endif | |
1268 if (fio_flags & FIO_LATIN1) | |
1269 size = size / 2; | |
1270 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1271 size = (size * 2 / 3) & ~1; | |
1272 else if (fio_flags & FIO_UCS4) | |
1273 size = (size * 2 / 3) & ~3; | |
1274 else if (fio_flags == FIO_UCSBOM) | |
1275 size = size / ICONV_MULT; /* worst case */ | |
1276 # ifdef WIN3264 | |
1277 else if (fio_flags & FIO_CODEPAGE) | |
1278 size = size / ICONV_MULT; /* also worst case */ | |
1279 # endif | |
1280 # ifdef MACOS_X | |
1281 else if (fio_flags & FIO_MACROMAN) | |
1282 size = size / ICONV_MULT; /* also worst case */ | |
1283 # endif | |
1284 #endif | |
1285 | |
1286 #ifdef FEAT_MBYTE | |
1287 if (conv_restlen > 0) | |
1288 { | |
1289 /* Insert unconverted bytes from previous line. */ | |
1290 mch_memmove(ptr, conv_rest, conv_restlen); | |
1291 ptr += conv_restlen; | |
1292 size -= conv_restlen; | |
1293 } | |
1294 #endif | |
1295 | |
1296 if (read_buffer) | |
1297 { | |
1298 /* | |
1299 * Read bytes from curbuf. Used for converting text read | |
1300 * from stdin. | |
1301 */ | |
1302 if (read_buf_lnum > from) | |
1303 size = 0; | |
1304 else | |
1305 { | |
1306 int n, ni; | |
1307 long tlen; | |
1308 | |
1309 tlen = 0; | |
1310 for (;;) | |
1311 { | |
1312 p = ml_get(read_buf_lnum) + read_buf_col; | |
1313 n = (int)STRLEN(p); | |
1314 if ((int)tlen + n + 1 > size) | |
1315 { | |
1316 /* Filled up to "size", append partial line. | |
1317 * Change NL to NUL to reverse the effect done | |
1318 * below. */ | |
835 | 1319 n = (int)(size - tlen); |
7 | 1320 for (ni = 0; ni < n; ++ni) |
1321 { | |
1322 if (p[ni] == NL) | |
1323 ptr[tlen++] = NUL; | |
1324 else | |
1325 ptr[tlen++] = p[ni]; | |
1326 } | |
1327 read_buf_col += n; | |
1328 break; | |
1329 } | |
1330 else | |
1331 { | |
1332 /* Append whole line and new-line. Change NL | |
1333 * to NUL to reverse the effect done below. */ | |
1334 for (ni = 0; ni < n; ++ni) | |
1335 { | |
1336 if (p[ni] == NL) | |
1337 ptr[tlen++] = NUL; | |
1338 else | |
1339 ptr[tlen++] = p[ni]; | |
1340 } | |
1341 ptr[tlen++] = NL; | |
1342 read_buf_col = 0; | |
1343 if (++read_buf_lnum > from) | |
1344 { | |
1345 /* When the last line didn't have an | |
1346 * end-of-line don't add it now either. */ | |
1347 if (!curbuf->b_p_eol) | |
1348 --tlen; | |
1349 size = tlen; | |
1350 break; | |
1351 } | |
1352 } | |
1353 } | |
1354 } | |
1355 } | |
1356 else | |
1357 { | |
1358 /* | |
1359 * Read bytes from the file. | |
1360 */ | |
2664 | 1361 size = read_eintr(fd, ptr, size); |
7 | 1362 } |
1363 | |
1364 if (size <= 0) | |
1365 { | |
1366 if (size < 0) /* read error */ | |
1367 error = TRUE; | |
1368 #ifdef FEAT_MBYTE | |
1369 else if (conv_restlen > 0) | |
595 | 1370 { |
1597 | 1371 /* |
1372 * Reached end-of-file but some trailing bytes could | |
1373 * not be converted. Truncated file? | |
1374 */ | |
1375 | |
1376 /* When we did a conversion report an error. */ | |
1377 if (fio_flags != 0 | |
1378 # ifdef USE_ICONV | |
1379 || iconv_fd != (iconv_t)-1 | |
1380 # endif | |
1381 ) | |
1382 { | |
4331 | 1383 if (can_retry) |
1384 goto rewind_retry; | |
1597 | 1385 if (conv_error == 0) |
1386 conv_error = curbuf->b_ml.ml_line_count | |
1387 - linecnt + 1; | |
1388 } | |
1389 /* Remember the first linenr with an illegal byte */ | |
1390 else if (illegal_byte == 0) | |
1391 illegal_byte = curbuf->b_ml.ml_line_count | |
1392 - linecnt + 1; | |
1393 if (bad_char_behavior == BAD_DROP) | |
595 | 1394 { |
1597 | 1395 *(ptr - conv_restlen) = NUL; |
1396 conv_restlen = 0; | |
1397 } | |
1398 else | |
1399 { | |
1400 /* Replace the trailing bytes with the replacement | |
1401 * character if we were converting; if we weren't, | |
1402 * leave the UTF8 checking code to do it, as it | |
1403 * works slightly differently. */ | |
1404 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0 | |
1405 # ifdef USE_ICONV | |
1406 || iconv_fd != (iconv_t)-1 | |
1407 # endif | |
1408 )) | |
1409 { | |
1410 while (conv_restlen > 0) | |
1411 { | |
1412 *(--ptr) = bad_char_behavior; | |
1413 --conv_restlen; | |
1414 } | |
1415 } | |
595 | 1416 fio_flags = 0; /* don't convert this */ |
834 | 1417 # ifdef USE_ICONV |
1418 if (iconv_fd != (iconv_t)-1) | |
1419 { | |
1420 iconv_close(iconv_fd); | |
1421 iconv_fd = (iconv_t)-1; | |
1422 } | |
1423 # endif | |
595 | 1424 } |
1425 } | |
7 | 1426 #endif |
1427 } | |
1428 | |
1429 #ifdef FEAT_CRYPT | |
1430 /* | |
1431 * At start of file: Check for magic number of encryption. | |
1432 */ | |
1433 if (filesize == 0) | |
1434 cryptkey = check_for_cryptkey(cryptkey, ptr, &size, | |
2267 | 1435 &filesize, newfile, sfname, |
1436 &did_ask_for_key); | |
7 | 1437 /* |
1438 * Decrypt the read bytes. | |
1439 */ | |
1440 if (cryptkey != NULL && size > 0) | |
2244 | 1441 crypt_decode(ptr, size); |
7 | 1442 #endif |
1443 } | |
1444 skip_read = FALSE; | |
1445 | |
1446 #ifdef FEAT_MBYTE | |
1447 /* | |
1448 * At start of file (or after crypt magic number): Check for BOM. | |
1449 * Also check for a BOM for other Unicode encodings, but not after | |
1450 * converting with 'charconvert' or when a BOM has already been | |
1451 * found. | |
1452 */ | |
1453 if ((filesize == 0 | |
1454 # ifdef FEAT_CRYPT | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
1455 || (filesize == (CRYPT_MAGIC_LEN |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
1456 + crypt_salt_len[use_crypt_method] |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
1457 + crypt_seed_len[use_crypt_method]) |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
1458 && cryptkey != NULL) |
7 | 1459 # endif |
1460 ) | |
1461 && (fio_flags == FIO_UCSBOM | |
1462 || (!curbuf->b_p_bomb | |
1463 && tmpname == NULL | |
1464 && (*fenc == 'u' || (*fenc == NUL && enc_utf8))))) | |
1465 { | |
1466 char_u *ccname; | |
1467 int blen; | |
1468 | |
1469 /* no BOM detection in a short file or in binary mode */ | |
1470 if (size < 2 || curbuf->b_p_bin) | |
1471 ccname = NULL; | |
1472 else | |
1473 ccname = check_for_bom(ptr, size, &blen, | |
1474 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc)); | |
1475 if (ccname != NULL) | |
1476 { | |
1477 /* Remove BOM from the text */ | |
1478 filesize += blen; | |
1479 size -= blen; | |
1480 mch_memmove(ptr, ptr + blen, (size_t)size); | |
819 | 1481 if (set_options) |
1352 | 1482 { |
7 | 1483 curbuf->b_p_bomb = TRUE; |
1352 | 1484 curbuf->b_start_bomb = TRUE; |
1485 } | |
7 | 1486 } |
1487 | |
1488 if (fio_flags == FIO_UCSBOM) | |
1489 { | |
1490 if (ccname == NULL) | |
1491 { | |
1492 /* No BOM detected: retry with next encoding. */ | |
1493 advance_fenc = TRUE; | |
1494 } | |
1495 else | |
1496 { | |
1497 /* BOM detected: set "fenc" and jump back */ | |
1498 if (fenc_alloced) | |
1499 vim_free(fenc); | |
1500 fenc = ccname; | |
1501 fenc_alloced = FALSE; | |
1502 } | |
1503 /* retry reading without getting new bytes or rewinding */ | |
1504 skip_read = TRUE; | |
1505 goto retry; | |
1506 } | |
1507 } | |
1597 | 1508 |
1509 /* Include not converted bytes. */ | |
1510 ptr -= conv_restlen; | |
1511 size += conv_restlen; | |
1512 conv_restlen = 0; | |
7 | 1513 #endif |
1514 /* | |
1515 * Break here for a read error or end-of-file. | |
1516 */ | |
1517 if (size <= 0) | |
1518 break; | |
1519 | |
1520 #ifdef FEAT_MBYTE | |
1521 | |
1522 # ifdef USE_ICONV | |
1523 if (iconv_fd != (iconv_t)-1) | |
1524 { | |
1525 /* | |
1526 * Attempt conversion of the read bytes to 'encoding' using | |
1527 * iconv(). | |
1528 */ | |
1529 const char *fromp; | |
1530 char *top; | |
1531 size_t from_size; | |
1532 size_t to_size; | |
1533 | |
1534 fromp = (char *)ptr; | |
1535 from_size = size; | |
1536 ptr += size; | |
1537 top = (char *)ptr; | |
1538 to_size = real_size - size; | |
1539 | |
1540 /* | |
1541 * If there is conversion error or not enough room try using | |
179 | 1542 * another conversion. Except for when there is no |
1543 * alternative (help files). | |
7 | 1544 */ |
177 | 1545 while ((iconv(iconv_fd, (void *)&fromp, &from_size, |
1546 &top, &to_size) | |
7 | 1547 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) |
1548 || from_size > CONV_RESTLEN) | |
177 | 1549 { |
595 | 1550 if (can_retry) |
375 | 1551 goto rewind_retry; |
595 | 1552 if (conv_error == 0) |
1553 conv_error = readfile_linenr(linecnt, | |
1554 ptr, (char_u *)top); | |
1555 | |
1556 /* Deal with a bad byte and continue with the next. */ | |
177 | 1557 ++fromp; |
1558 --from_size; | |
595 | 1559 if (bad_char_behavior == BAD_KEEP) |
1560 { | |
1561 *top++ = *(fromp - 1); | |
1562 --to_size; | |
1563 } | |
1564 else if (bad_char_behavior != BAD_DROP) | |
1565 { | |
1566 *top++ = bad_char_behavior; | |
1567 --to_size; | |
1568 } | |
177 | 1569 } |
7 | 1570 |
1571 if (from_size > 0) | |
1572 { | |
1573 /* Some remaining characters, keep them for the next | |
1574 * round. */ | |
1575 mch_memmove(conv_rest, (char_u *)fromp, from_size); | |
1576 conv_restlen = (int)from_size; | |
1577 } | |
1578 | |
1579 /* move the linerest to before the converted characters */ | |
1580 line_start = ptr - linerest; | |
1581 mch_memmove(line_start, buffer, (size_t)linerest); | |
1582 size = (long)((char_u *)top - ptr); | |
1583 } | |
1584 # endif | |
1585 | |
1586 # ifdef WIN3264 | |
1587 if (fio_flags & FIO_CODEPAGE) | |
1588 { | |
595 | 1589 char_u *src, *dst; |
1590 WCHAR ucs2buf[3]; | |
1591 int ucs2len; | |
1592 int codepage = FIO_GET_CP(fio_flags); | |
1593 int bytelen; | |
1594 int found_bad; | |
1595 char replstr[2]; | |
1596 | |
7 | 1597 /* |
1598 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or | |
595 | 1599 * a codepage, using standard MS-Windows functions. This |
1600 * requires two steps: | |
1601 * 1. convert from 'fileencoding' to ucs-2 | |
1602 * 2. convert from ucs-2 to 'encoding' | |
1603 * | |
1604 * Because there may be illegal bytes AND an incomplete byte | |
1605 * sequence at the end, we may have to do the conversion one | |
1606 * character at a time to get it right. | |
7 | 1607 */ |
595 | 1608 |
1609 /* Replacement string for WideCharToMultiByte(). */ | |
1610 if (bad_char_behavior > 0) | |
1611 replstr[0] = bad_char_behavior; | |
1612 else | |
1613 replstr[0] = '?'; | |
1614 replstr[1] = NUL; | |
1615 | |
1616 /* | |
1617 * Move the bytes to the end of the buffer, so that we have | |
1618 * room to put the result at the start. | |
1619 */ | |
1620 src = ptr + real_size - size; | |
1621 mch_memmove(src, ptr, size); | |
7 | 1622 |
1623 /* | |
595 | 1624 * Do the conversion. |
7 | 1625 */ |
595 | 1626 dst = ptr; |
1627 size = size; | |
1628 while (size > 0) | |
7 | 1629 { |
595 | 1630 found_bad = FALSE; |
7 | 1631 |
1632 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
595 | 1633 if (codepage == CP_UTF8) |
7 | 1634 { |
595 | 1635 /* Handle CP_UTF8 input ourselves to be able to handle |
1636 * trailing bytes properly. | |
1637 * Get one UTF-8 character from src. */ | |
835 | 1638 bytelen = (int)utf_ptr2len_len(src, size); |
595 | 1639 if (bytelen > size) |
1640 { | |
1641 /* Only got some bytes of a character. Normally | |
1642 * it's put in "conv_rest", but if it's too long | |
1643 * deal with it as if they were illegal bytes. */ | |
1644 if (bytelen <= CONV_RESTLEN) | |
1645 break; | |
1646 | |
1647 /* weird overlong byte sequence */ | |
1648 bytelen = size; | |
1649 found_bad = TRUE; | |
1650 } | |
1651 else | |
1652 { | |
799 | 1653 int u8c = utf_ptr2char(src); |
1654 | |
622 | 1655 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1)) |
595 | 1656 found_bad = TRUE; |
1657 ucs2buf[0] = u8c; | |
1658 ucs2len = 1; | |
1659 } | |
7 | 1660 } |
595 | 1661 else |
7 | 1662 # endif |
595 | 1663 { |
1664 /* We don't know how long the byte sequence is, try | |
1665 * from one to three bytes. */ | |
1666 for (bytelen = 1; bytelen <= size && bytelen <= 3; | |
1667 ++bytelen) | |
1668 { | |
1669 ucs2len = MultiByteToWideChar(codepage, | |
1670 MB_ERR_INVALID_CHARS, | |
1671 (LPCSTR)src, bytelen, | |
1672 ucs2buf, 3); | |
1673 if (ucs2len > 0) | |
1674 break; | |
1675 } | |
1676 if (ucs2len == 0) | |
1677 { | |
1678 /* If we have only one byte then it's probably an | |
1679 * incomplete byte sequence. Otherwise discard | |
1680 * one byte as a bad character. */ | |
1681 if (size == 1) | |
1682 break; | |
1683 found_bad = TRUE; | |
1684 bytelen = 1; | |
1685 } | |
1686 } | |
1687 | |
1688 if (!found_bad) | |
7 | 1689 { |
595 | 1690 int i; |
1691 | |
1692 /* Convert "ucs2buf[ucs2len]" to 'enc' in "dst". */ | |
1693 if (enc_utf8) | |
1694 { | |
1695 /* From UCS-2 to UTF-8. Cannot fail. */ | |
1696 for (i = 0; i < ucs2len; ++i) | |
1697 dst += utf_char2bytes(ucs2buf[i], dst); | |
1698 } | |
1699 else | |
1700 { | |
1701 BOOL bad = FALSE; | |
1702 int dstlen; | |
1703 | |
1704 /* From UCS-2 to "enc_codepage". If the | |
1705 * conversion uses the default character "?", | |
1706 * the data doesn't fit in this encoding. */ | |
1707 dstlen = WideCharToMultiByte(enc_codepage, 0, | |
1708 (LPCWSTR)ucs2buf, ucs2len, | |
835 | 1709 (LPSTR)dst, (int)(src - dst), |
595 | 1710 replstr, &bad); |
1711 if (bad) | |
1712 found_bad = TRUE; | |
1713 else | |
1714 dst += dstlen; | |
1715 } | |
7 | 1716 } |
595 | 1717 |
1718 if (found_bad) | |
1719 { | |
1720 /* Deal with bytes we can't convert. */ | |
1721 if (can_retry) | |
1722 goto rewind_retry; | |
1723 if (conv_error == 0) | |
1724 conv_error = readfile_linenr(linecnt, ptr, dst); | |
1725 if (bad_char_behavior != BAD_DROP) | |
1726 { | |
1727 if (bad_char_behavior == BAD_KEEP) | |
1728 { | |
1729 mch_memmove(dst, src, bytelen); | |
1730 dst += bytelen; | |
1731 } | |
1732 else | |
1733 *dst++ = bad_char_behavior; | |
1734 } | |
1735 } | |
1736 | |
1737 src += bytelen; | |
1738 size -= bytelen; | |
7 | 1739 } |
595 | 1740 |
1741 if (size > 0) | |
7 | 1742 { |
595 | 1743 /* An incomplete byte sequence remaining. */ |
1744 mch_memmove(conv_rest, src, size); | |
1745 conv_restlen = size; | |
7 | 1746 } |
595 | 1747 |
1748 /* The new size is equal to how much "dst" was advanced. */ | |
835 | 1749 size = (long)(dst - ptr); |
7 | 1750 } |
1751 else | |
1752 # endif | |
765 | 1753 # ifdef MACOS_CONVERT |
7 | 1754 if (fio_flags & FIO_MACROMAN) |
1755 { | |
1756 /* | |
1757 * Conversion from Apple MacRoman char encoding to UTF-8 or | |
18 | 1758 * latin1. This is in os_mac_conv.c. |
7 | 1759 */ |
18 | 1760 if (macroman2enc(ptr, &size, real_size) == FAIL) |
7 | 1761 goto rewind_retry; |
1762 } | |
1763 else | |
1764 # endif | |
1765 if (fio_flags != 0) | |
1766 { | |
1767 int u8c; | |
1768 char_u *dest; | |
1769 char_u *tail = NULL; | |
1770 | |
1771 /* | |
1772 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8. | |
1773 * "enc_utf8" not set: Convert Unicode to Latin1. | |
1774 * Go from end to start through the buffer, because the number | |
1775 * of bytes may increase. | |
1776 * "dest" points to after where the UTF-8 bytes go, "p" points | |
1777 * to after the next character to convert. | |
1778 */ | |
1779 dest = ptr + real_size; | |
1780 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8) | |
1781 { | |
1782 p = ptr + size; | |
1783 if (fio_flags == FIO_UTF8) | |
1784 { | |
1785 /* Check for a trailing incomplete UTF-8 sequence */ | |
1786 tail = ptr + size - 1; | |
1787 while (tail > ptr && (*tail & 0xc0) == 0x80) | |
1788 --tail; | |
1789 if (tail + utf_byte2len(*tail) <= ptr + size) | |
1790 tail = NULL; | |
1791 else | |
1792 p = tail; | |
1793 } | |
1794 } | |
1795 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1796 { | |
1797 /* Check for a trailing byte */ | |
1798 p = ptr + (size & ~1); | |
1799 if (size & 1) | |
1800 tail = p; | |
1801 if ((fio_flags & FIO_UTF16) && p > ptr) | |
1802 { | |
1803 /* Check for a trailing leading word */ | |
1804 if (fio_flags & FIO_ENDIAN_L) | |
1805 { | |
1806 u8c = (*--p << 8); | |
1807 u8c += *--p; | |
1808 } | |
1809 else | |
1810 { | |
1811 u8c = *--p; | |
1812 u8c += (*--p << 8); | |
1813 } | |
1814 if (u8c >= 0xd800 && u8c <= 0xdbff) | |
1815 tail = p; | |
1816 else | |
1817 p += 2; | |
1818 } | |
1819 } | |
1820 else /* FIO_UCS4 */ | |
1821 { | |
1822 /* Check for trailing 1, 2 or 3 bytes */ | |
1823 p = ptr + (size & ~3); | |
1824 if (size & 3) | |
1825 tail = p; | |
1826 } | |
1827 | |
1828 /* If there is a trailing incomplete sequence move it to | |
1829 * conv_rest[]. */ | |
1830 if (tail != NULL) | |
1831 { | |
1832 conv_restlen = (int)((ptr + size) - tail); | |
1833 mch_memmove(conv_rest, (char_u *)tail, conv_restlen); | |
1834 size -= conv_restlen; | |
1835 } | |
1836 | |
1837 | |
1838 while (p > ptr) | |
1839 { | |
1840 if (fio_flags & FIO_LATIN1) | |
1841 u8c = *--p; | |
1842 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1843 { | |
1844 if (fio_flags & FIO_ENDIAN_L) | |
1845 { | |
1846 u8c = (*--p << 8); | |
1847 u8c += *--p; | |
1848 } | |
1849 else | |
1850 { | |
1851 u8c = *--p; | |
1852 u8c += (*--p << 8); | |
1853 } | |
1854 if ((fio_flags & FIO_UTF16) | |
1855 && u8c >= 0xdc00 && u8c <= 0xdfff) | |
1856 { | |
1857 int u16c; | |
1858 | |
1859 if (p == ptr) | |
1860 { | |
1861 /* Missing leading word. */ | |
1862 if (can_retry) | |
1863 goto rewind_retry; | |
595 | 1864 if (conv_error == 0) |
1865 conv_error = readfile_linenr(linecnt, | |
1866 ptr, p); | |
1867 if (bad_char_behavior == BAD_DROP) | |
1868 continue; | |
1869 if (bad_char_behavior != BAD_KEEP) | |
1870 u8c = bad_char_behavior; | |
7 | 1871 } |
1872 | |
1873 /* found second word of double-word, get the first | |
1874 * word and compute the resulting character */ | |
1875 if (fio_flags & FIO_ENDIAN_L) | |
1876 { | |
1877 u16c = (*--p << 8); | |
1878 u16c += *--p; | |
1879 } | |
1880 else | |
1881 { | |
1882 u16c = *--p; | |
1883 u16c += (*--p << 8); | |
1884 } | |
595 | 1885 u8c = 0x10000 + ((u16c & 0x3ff) << 10) |
1886 + (u8c & 0x3ff); | |
1887 | |
7 | 1888 /* Check if the word is indeed a leading word. */ |
1889 if (u16c < 0xd800 || u16c > 0xdbff) | |
1890 { | |
1891 if (can_retry) | |
1892 goto rewind_retry; | |
595 | 1893 if (conv_error == 0) |
1894 conv_error = readfile_linenr(linecnt, | |
1895 ptr, p); | |
1896 if (bad_char_behavior == BAD_DROP) | |
1897 continue; | |
1898 if (bad_char_behavior != BAD_KEEP) | |
1899 u8c = bad_char_behavior; | |
7 | 1900 } |
1901 } | |
1902 } | |
1903 else if (fio_flags & FIO_UCS4) | |
1904 { | |
1905 if (fio_flags & FIO_ENDIAN_L) | |
1906 { | |
1907 u8c = (*--p << 24); | |
1908 u8c += (*--p << 16); | |
1909 u8c += (*--p << 8); | |
1910 u8c += *--p; | |
1911 } | |
1912 else /* big endian */ | |
1913 { | |
1914 u8c = *--p; | |
1915 u8c += (*--p << 8); | |
1916 u8c += (*--p << 16); | |
1917 u8c += (*--p << 24); | |
1918 } | |
1919 } | |
1920 else /* UTF-8 */ | |
1921 { | |
1922 if (*--p < 0x80) | |
1923 u8c = *p; | |
1924 else | |
1925 { | |
1926 len = utf_head_off(ptr, p); | |
595 | 1927 p -= len; |
1928 u8c = utf_ptr2char(p); | |
7 | 1929 if (len == 0) |
1930 { | |
1931 /* Not a valid UTF-8 character, retry with | |
1932 * another fenc when possible, otherwise just | |
1933 * report the error. */ | |
1934 if (can_retry) | |
1935 goto rewind_retry; | |
595 | 1936 if (conv_error == 0) |
1937 conv_error = readfile_linenr(linecnt, | |
1938 ptr, p); | |
1939 if (bad_char_behavior == BAD_DROP) | |
1940 continue; | |
1941 if (bad_char_behavior != BAD_KEEP) | |
1942 u8c = bad_char_behavior; | |
7 | 1943 } |
1944 } | |
1945 } | |
1946 if (enc_utf8) /* produce UTF-8 */ | |
1947 { | |
1948 dest -= utf_char2len(u8c); | |
1949 (void)utf_char2bytes(u8c, dest); | |
1950 } | |
1951 else /* produce Latin1 */ | |
1952 { | |
1953 --dest; | |
1954 if (u8c >= 0x100) | |
1955 { | |
1956 /* character doesn't fit in latin1, retry with | |
1957 * another fenc when possible, otherwise just | |
1958 * report the error. */ | |
595 | 1959 if (can_retry) |
7 | 1960 goto rewind_retry; |
595 | 1961 if (conv_error == 0) |
1962 conv_error = readfile_linenr(linecnt, ptr, p); | |
1963 if (bad_char_behavior == BAD_DROP) | |
1964 ++dest; | |
1965 else if (bad_char_behavior == BAD_KEEP) | |
1966 *dest = u8c; | |
1967 else if (eap != NULL && eap->bad_char != 0) | |
1968 *dest = bad_char_behavior; | |
1969 else | |
1970 *dest = 0xBF; | |
7 | 1971 } |
1972 else | |
1973 *dest = u8c; | |
1974 } | |
1975 } | |
1976 | |
1977 /* move the linerest to before the converted characters */ | |
1978 line_start = dest - linerest; | |
1979 mch_memmove(line_start, buffer, (size_t)linerest); | |
1980 size = (long)((ptr + real_size) - dest); | |
1981 ptr = dest; | |
1982 } | |
1597 | 1983 else if (enc_utf8 && !curbuf->b_p_bin) |
1984 { | |
1985 int incomplete_tail = FALSE; | |
1986 | |
1987 /* Reading UTF-8: Check if the bytes are valid UTF-8. */ | |
1988 for (p = ptr; ; ++p) | |
7 | 1989 { |
835 | 1990 int todo = (int)((ptr + size) - p); |
595 | 1991 int l; |
1992 | |
1993 if (todo <= 0) | |
1994 break; | |
7 | 1995 if (*p >= 0x80) |
1996 { | |
1997 /* A length of 1 means it's an illegal byte. Accept | |
1998 * an incomplete character at the end though, the next | |
1999 * read() will get the next bytes, we'll check it | |
2000 * then. */ | |
595 | 2001 l = utf_ptr2len_len(p, todo); |
1597 | 2002 if (l > todo && !incomplete_tail) |
7 | 2003 { |
1597 | 2004 /* Avoid retrying with a different encoding when |
2005 * a truncated file is more likely, or attempting | |
2006 * to read the rest of an incomplete sequence when | |
2007 * we have already done so. */ | |
2008 if (p > ptr || filesize > 0) | |
2009 incomplete_tail = TRUE; | |
2010 /* Incomplete byte sequence, move it to conv_rest[] | |
2011 * and try to read the rest of it, unless we've | |
2012 * already done so. */ | |
2013 if (p > ptr) | |
2014 { | |
2015 conv_restlen = todo; | |
2016 mch_memmove(conv_rest, p, conv_restlen); | |
2017 size -= conv_restlen; | |
2018 break; | |
2019 } | |
7 | 2020 } |
1597 | 2021 if (l == 1 || l > todo) |
595 | 2022 { |
2023 /* Illegal byte. If we can try another encoding | |
1597 | 2024 * do that, unless at EOF where a truncated |
2025 * file is more likely than a conversion error. */ | |
2026 if (can_retry && !incomplete_tail) | |
595 | 2027 break; |
2028 # ifdef USE_ICONV | |
2029 /* When we did a conversion report an error. */ | |
2030 if (iconv_fd != (iconv_t)-1 && conv_error == 0) | |
2031 conv_error = readfile_linenr(linecnt, ptr, p); | |
2032 # endif | |
1597 | 2033 /* Remember the first linenr with an illegal byte */ |
2034 if (conv_error == 0 && illegal_byte == 0) | |
2035 illegal_byte = readfile_linenr(linecnt, ptr, p); | |
595 | 2036 |
2037 /* Drop, keep or replace the bad byte. */ | |
2038 if (bad_char_behavior == BAD_DROP) | |
2039 { | |
1597 | 2040 mch_memmove(p, p + 1, todo - 1); |
595 | 2041 --p; |
2042 --size; | |
2043 } | |
2044 else if (bad_char_behavior != BAD_KEEP) | |
2045 *p = bad_char_behavior; | |
2046 } | |
1597 | 2047 else |
2048 p += l - 1; | |
7 | 2049 } |
2050 } | |
1597 | 2051 if (p < ptr + size && !incomplete_tail) |
7 | 2052 { |
2053 /* Detected a UTF-8 error. */ | |
2054 rewind_retry: | |
595 | 2055 /* Retry reading with another conversion. */ |
7 | 2056 # if defined(FEAT_EVAL) && defined(USE_ICONV) |
595 | 2057 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1) |
2058 /* iconv() failed, try 'charconvert' */ | |
2059 did_iconv = TRUE; | |
7 | 2060 else |
2061 # endif | |
595 | 2062 /* use next item from 'fileencodings' */ |
2063 advance_fenc = TRUE; | |
2064 file_rewind = TRUE; | |
2065 goto retry; | |
7 | 2066 } |
2067 } | |
2068 #endif | |
2069 | |
2070 /* count the number of characters (after conversion!) */ | |
2071 filesize += size; | |
2072 | |
2073 /* | |
2074 * when reading the first part of a file: guess EOL type | |
2075 */ | |
2076 if (fileformat == EOL_UNKNOWN) | |
2077 { | |
2078 /* First try finding a NL, for Dos and Unix */ | |
2079 if (try_dos || try_unix) | |
2080 { | |
2081 for (p = ptr; p < ptr + size; ++p) | |
2082 { | |
2083 if (*p == NL) | |
2084 { | |
2085 if (!try_unix | |
2086 || (try_dos && p > ptr && p[-1] == CAR)) | |
2087 fileformat = EOL_DOS; | |
2088 else | |
2089 fileformat = EOL_UNIX; | |
2090 break; | |
2091 } | |
2092 } | |
2093 | |
2094 /* Don't give in to EOL_UNIX if EOL_MAC is more likely */ | |
2095 if (fileformat == EOL_UNIX && try_mac) | |
2096 { | |
2097 /* Need to reset the counters when retrying fenc. */ | |
2098 try_mac = 1; | |
2099 try_unix = 1; | |
2100 for (; p >= ptr && *p != CAR; p--) | |
2101 ; | |
2102 if (p >= ptr) | |
2103 { | |
2104 for (p = ptr; p < ptr + size; ++p) | |
2105 { | |
2106 if (*p == NL) | |
2107 try_unix++; | |
2108 else if (*p == CAR) | |
2109 try_mac++; | |
2110 } | |
2111 if (try_mac > try_unix) | |
2112 fileformat = EOL_MAC; | |
2113 } | |
2114 } | |
2115 } | |
2116 | |
2117 /* No NL found: may use Mac format */ | |
2118 if (fileformat == EOL_UNKNOWN && try_mac) | |
2119 fileformat = EOL_MAC; | |
2120 | |
2121 /* Still nothing found? Use first format in 'ffs' */ | |
2122 if (fileformat == EOL_UNKNOWN) | |
2123 fileformat = default_fileformat(); | |
2124 | |
2125 /* if editing a new file: may set p_tx and p_ff */ | |
819 | 2126 if (set_options) |
7 | 2127 set_fileformat(fileformat, OPT_LOCAL); |
2128 } | |
2129 } | |
2130 | |
2131 /* | |
2132 * This loop is executed once for every character read. | |
2133 * Keep it fast! | |
2134 */ | |
2135 if (fileformat == EOL_MAC) | |
2136 { | |
2137 --ptr; | |
2138 while (++ptr, --size >= 0) | |
2139 { | |
2140 /* catch most common case first */ | |
2141 if ((c = *ptr) != NUL && c != CAR && c != NL) | |
2142 continue; | |
2143 if (c == NUL) | |
2144 *ptr = NL; /* NULs are replaced by newlines! */ | |
2145 else if (c == NL) | |
2146 *ptr = CAR; /* NLs are replaced by CRs! */ | |
2147 else | |
2148 { | |
2149 if (skip_count == 0) | |
2150 { | |
2151 *ptr = NUL; /* end of line */ | |
2152 len = (colnr_T) (ptr - line_start + 1); | |
2153 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2154 { | |
2155 error = TRUE; | |
2156 break; | |
2157 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2158 #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
|
2159 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
|
2160 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
|
2161 #endif |
7 | 2162 ++lnum; |
2163 if (--read_count == 0) | |
2164 { | |
2165 error = TRUE; /* break loop */ | |
2166 line_start = ptr; /* nothing left to write */ | |
2167 break; | |
2168 } | |
2169 } | |
2170 else | |
2171 --skip_count; | |
2172 line_start = ptr + 1; | |
2173 } | |
2174 } | |
2175 } | |
2176 else | |
2177 { | |
2178 --ptr; | |
2179 while (++ptr, --size >= 0) | |
2180 { | |
2181 if ((c = *ptr) != NUL && c != NL) /* catch most common case */ | |
2182 continue; | |
2183 if (c == NUL) | |
2184 *ptr = NL; /* NULs are replaced by newlines! */ | |
2185 else | |
2186 { | |
2187 if (skip_count == 0) | |
2188 { | |
2189 *ptr = NUL; /* end of line */ | |
2190 len = (colnr_T)(ptr - line_start + 1); | |
2191 if (fileformat == EOL_DOS) | |
2192 { | |
2193 if (ptr[-1] == CAR) /* remove CR */ | |
2194 { | |
2195 ptr[-1] = NUL; | |
2196 --len; | |
2197 } | |
2198 /* | |
2199 * Reading in Dos format, but no CR-LF found! | |
2200 * When 'fileformats' includes "unix", delete all | |
2201 * the lines read so far and start all over again. | |
2202 * Otherwise give an error message later. | |
2203 */ | |
2204 else if (ff_error != EOL_DOS) | |
2205 { | |
2206 if ( try_unix | |
2207 && !read_stdin | |
2208 && (read_buffer | |
2209 || lseek(fd, (off_t)0L, SEEK_SET) == 0)) | |
2210 { | |
2211 fileformat = EOL_UNIX; | |
819 | 2212 if (set_options) |
7 | 2213 set_fileformat(EOL_UNIX, OPT_LOCAL); |
2214 file_rewind = TRUE; | |
2215 keep_fileformat = TRUE; | |
2216 goto retry; | |
2217 } | |
2218 ff_error = EOL_DOS; | |
2219 } | |
2220 } | |
2221 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2222 { | |
2223 error = TRUE; | |
2224 break; | |
2225 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2226 #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
|
2227 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
|
2228 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
|
2229 #endif |
7 | 2230 ++lnum; |
2231 if (--read_count == 0) | |
2232 { | |
2233 error = TRUE; /* break loop */ | |
2234 line_start = ptr; /* nothing left to write */ | |
2235 break; | |
2236 } | |
2237 } | |
2238 else | |
2239 --skip_count; | |
2240 line_start = ptr + 1; | |
2241 } | |
2242 } | |
2243 } | |
2244 linerest = (long)(ptr - line_start); | |
2245 ui_breakcheck(); | |
2246 } | |
2247 | |
2248 failed: | |
2249 /* not an error, max. number of lines reached */ | |
2250 if (error && read_count == 0) | |
2251 error = FALSE; | |
2252 | |
2253 /* | |
2254 * If we get EOF in the middle of a line, note the fact and | |
2255 * complete the line ourselves. | |
2256 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set. | |
2257 */ | |
2258 if (!error | |
2259 && !got_int | |
2260 && linerest != 0 | |
2261 && !(!curbuf->b_p_bin | |
2262 && fileformat == EOL_DOS | |
2263 && *line_start == Ctrl_Z | |
2264 && ptr == line_start + 1)) | |
2265 { | |
819 | 2266 /* remember for when writing */ |
2267 if (set_options) | |
7 | 2268 curbuf->b_p_eol = FALSE; |
2269 *ptr = NUL; | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2270 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
|
2271 if (ml_append(lnum, line_start, len, newfile) == FAIL) |
7 | 2272 error = TRUE; |
2273 else | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2274 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2275 #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
|
2276 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
|
2277 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
|
2278 #endif |
7 | 2279 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
|
2280 } |
7 | 2281 } |
2282 | |
819 | 2283 if (set_options) |
7 | 2284 save_file_ff(curbuf); /* remember the current file format */ |
2285 | |
2286 #ifdef FEAT_CRYPT | |
2717 | 2287 crypt_method_used = use_crypt_method; |
2267 | 2288 if (cryptkey != NULL) |
2289 { | |
2290 crypt_pop_state(); | |
2291 if (cryptkey != curbuf->b_p_key) | |
2292 free_crypt_key(cryptkey); | |
2293 /* don't set cryptkey to NULL, it's used below as a flag that | |
2294 * encryption was used */ | |
2295 } | |
7 | 2296 #endif |
2297 | |
2298 #ifdef FEAT_MBYTE | |
819 | 2299 /* If editing a new file: set 'fenc' for the current buffer. |
2300 * Also for ":read ++edit file". */ | |
2301 if (set_options) | |
7 | 2302 set_string_option_direct((char_u *)"fenc", -1, fenc, |
694 | 2303 OPT_FREE|OPT_LOCAL, 0); |
7 | 2304 if (fenc_alloced) |
2305 vim_free(fenc); | |
2306 # ifdef USE_ICONV | |
2307 if (iconv_fd != (iconv_t)-1) | |
2308 { | |
2309 iconv_close(iconv_fd); | |
2310 iconv_fd = (iconv_t)-1; | |
2311 } | |
2312 # endif | |
2313 #endif | |
2314 | |
2315 if (!read_buffer && !read_stdin) | |
2316 close(fd); /* errors are ignored */ | |
2003 | 2317 #ifdef HAVE_FD_CLOEXEC |
2318 else | |
2319 { | |
2320 int fdflags = fcntl(fd, F_GETFD); | |
2321 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0) | |
2322 fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC); | |
2323 } | |
2324 #endif | |
7 | 2325 vim_free(buffer); |
2326 | |
2327 #ifdef HAVE_DUP | |
2328 if (read_stdin) | |
2329 { | |
2330 /* Use stderr for stdin, makes shell commands work. */ | |
2331 close(0); | |
1757 | 2332 ignored = dup(2); |
7 | 2333 } |
2334 #endif | |
2335 | |
2336 #ifdef FEAT_MBYTE | |
2337 if (tmpname != NULL) | |
2338 { | |
2339 mch_remove(tmpname); /* delete converted file */ | |
2340 vim_free(tmpname); | |
2341 } | |
2342 #endif | |
2343 --no_wait_return; /* may wait for return now */ | |
2344 | |
2345 /* | |
2346 * In recovery mode everything but autocommands is skipped. | |
2347 */ | |
2348 if (!recoverymode) | |
2349 { | |
2350 /* need to delete the last line, which comes from the empty buffer */ | |
2351 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY)) | |
2352 { | |
2353 #ifdef FEAT_NETBEANS_INTG | |
2354 netbeansFireChanges = 0; | |
2355 #endif | |
2356 ml_delete(curbuf->b_ml.ml_line_count, FALSE); | |
2357 #ifdef FEAT_NETBEANS_INTG | |
2358 netbeansFireChanges = 1; | |
2359 #endif | |
2360 --linecnt; | |
2361 } | |
2362 linecnt = curbuf->b_ml.ml_line_count - linecnt; | |
2363 if (filesize == 0) | |
2364 linecnt = 0; | |
2365 if (newfile || read_buffer) | |
1201 | 2366 { |
7 | 2367 redraw_curbuf_later(NOT_VALID); |
1201 | 2368 #ifdef FEAT_DIFF |
2369 /* After reading the text into the buffer the diff info needs to | |
2370 * be updated. */ | |
2371 diff_invalidate(curbuf); | |
2372 #endif | |
2373 #ifdef FEAT_FOLDING | |
2374 /* All folds in the window are invalid now. Mark them for update | |
2375 * before triggering autocommands. */ | |
2376 foldUpdateAll(curwin); | |
2377 #endif | |
2378 } | |
7 | 2379 else if (linecnt) /* appended at least one line */ |
2380 appended_lines_mark(from, linecnt); | |
2381 | |
2382 #ifndef ALWAYS_USE_GUI | |
2383 /* | |
2384 * If we were reading from the same terminal as where messages go, | |
2385 * the screen will have been messed up. | |
2386 * Switch on raw mode now and clear the screen. | |
2387 */ | |
2388 if (read_stdin) | |
2389 { | |
2390 settmode(TMODE_RAW); /* set to raw mode */ | |
2391 starttermcap(); | |
2392 screenclear(); | |
2393 } | |
2394 #endif | |
2395 | |
2396 if (got_int) | |
2397 { | |
2398 if (!(flags & READ_DUMMY)) | |
2399 { | |
2400 filemess(curbuf, sfname, (char_u *)_(e_interr), 0); | |
2401 if (newfile) | |
2402 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
2403 } | |
2404 msg_scroll = msg_save; | |
2405 #ifdef FEAT_VIMINFO | |
2406 check_marks_read(); | |
2407 #endif | |
2408 return OK; /* an interrupt isn't really an error */ | |
2409 } | |
2410 | |
2411 if (!filtering && !(flags & READ_DUMMY)) | |
2412 { | |
2413 msg_add_fname(curbuf, sfname); /* fname in IObuff with quotes */ | |
2414 c = FALSE; | |
2415 | |
2416 #ifdef UNIX | |
2417 # ifdef S_ISFIFO | |
2418 if (S_ISFIFO(perm)) /* fifo or socket */ | |
2419 { | |
2420 STRCAT(IObuff, _("[fifo/socket]")); | |
2421 c = TRUE; | |
2422 } | |
2423 # else | |
2424 # ifdef S_IFIFO | |
2425 if ((perm & S_IFMT) == S_IFIFO) /* fifo */ | |
2426 { | |
2427 STRCAT(IObuff, _("[fifo]")); | |
2428 c = TRUE; | |
2429 } | |
2430 # endif | |
2431 # ifdef S_IFSOCK | |
2432 if ((perm & S_IFMT) == S_IFSOCK) /* or socket */ | |
2433 { | |
2434 STRCAT(IObuff, _("[socket]")); | |
2435 c = TRUE; | |
2436 } | |
2437 # endif | |
2438 # endif | |
1313 | 2439 # ifdef OPEN_CHR_FILES |
2440 if (S_ISCHR(perm)) /* or character special */ | |
2441 { | |
2442 STRCAT(IObuff, _("[character special]")); | |
2443 c = TRUE; | |
2444 } | |
2445 # endif | |
7 | 2446 #endif |
2447 if (curbuf->b_p_ro) | |
2448 { | |
2449 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")); | |
2450 c = TRUE; | |
2451 } | |
2452 if (read_no_eol_lnum) | |
2453 { | |
2454 msg_add_eol(); | |
2455 c = TRUE; | |
2456 } | |
2457 if (ff_error == EOL_DOS) | |
2458 { | |
2459 STRCAT(IObuff, _("[CR missing]")); | |
2460 c = TRUE; | |
2461 } | |
2462 if (split) | |
2463 { | |
2464 STRCAT(IObuff, _("[long lines split]")); | |
2465 c = TRUE; | |
2466 } | |
2467 #ifdef FEAT_MBYTE | |
2468 if (notconverted) | |
2469 { | |
2470 STRCAT(IObuff, _("[NOT converted]")); | |
2471 c = TRUE; | |
2472 } | |
2473 else if (converted) | |
2474 { | |
2475 STRCAT(IObuff, _("[converted]")); | |
2476 c = TRUE; | |
2477 } | |
2478 #endif | |
2479 #ifdef FEAT_CRYPT | |
2480 if (cryptkey != NULL) | |
2481 { | |
2717 | 2482 if (crypt_method_used == 1) |
2483 STRCAT(IObuff, _("[blowfish]")); | |
2484 else | |
2485 STRCAT(IObuff, _("[crypted]")); | |
7 | 2486 c = TRUE; |
2487 } | |
2488 #endif | |
2489 #ifdef FEAT_MBYTE | |
595 | 2490 if (conv_error != 0) |
2491 { | |
2492 sprintf((char *)IObuff + STRLEN(IObuff), | |
2493 _("[CONVERSION ERROR in line %ld]"), (long)conv_error); | |
7 | 2494 c = TRUE; |
2495 } | |
2496 else if (illegal_byte > 0) | |
2497 { | |
2498 sprintf((char *)IObuff + STRLEN(IObuff), | |
2499 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte); | |
2500 c = TRUE; | |
2501 } | |
2502 else | |
2503 #endif | |
2504 if (error) | |
2505 { | |
2506 STRCAT(IObuff, _("[READ ERRORS]")); | |
2507 c = TRUE; | |
2508 } | |
2509 if (msg_add_fileformat(fileformat)) | |
2510 c = TRUE; | |
2511 #ifdef FEAT_CRYPT | |
2512 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
|
2513 msg_add_lines(c, (long)linecnt, filesize |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2514 - CRYPT_MAGIC_LEN |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2515 - crypt_salt_len[use_crypt_method] |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2516 - crypt_seed_len[use_crypt_method]); |
7 | 2517 else |
2518 #endif | |
2519 msg_add_lines(c, (long)linecnt, filesize); | |
2520 | |
2521 vim_free(keep_msg); | |
2522 keep_msg = NULL; | |
2523 msg_scrolled_ign = TRUE; | |
2524 #ifdef ALWAYS_USE_GUI | |
2525 /* Don't show the message when reading stdin, it would end up in a | |
2526 * message box (which might be shown when exiting!) */ | |
2527 if (read_stdin || read_buffer) | |
2528 p = msg_may_trunc(FALSE, IObuff); | |
2529 else | |
2530 #endif | |
2531 p = msg_trunc_attr(IObuff, FALSE, 0); | |
2532 if (read_stdin || read_buffer || restart_edit != 0 | |
540 | 2533 || (msg_scrolled != 0 && !need_wait_return)) |
7 | 2534 /* Need to repeat the message after redrawing when: |
2535 * - When reading from stdin (the screen will be cleared next). | |
2536 * - When restart_edit is set (otherwise there will be a delay | |
2537 * before redrawing). | |
2538 * - When the screen was scrolled but there is no wait-return | |
2539 * prompt. */ | |
679 | 2540 set_keep_msg(p, 0); |
7 | 2541 msg_scrolled_ign = FALSE; |
2542 } | |
2543 | |
2544 /* with errors writing the file requires ":w!" */ | |
2545 if (newfile && (error | |
2546 #ifdef FEAT_MBYTE | |
595 | 2547 || conv_error != 0 |
679 | 2548 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP) |
7 | 2549 #endif |
2550 )) | |
2551 curbuf->b_p_ro = TRUE; | |
2552 | |
2553 u_clearline(); /* cannot use "U" command after adding lines */ | |
2554 | |
2555 /* | |
2556 * In Ex mode: cursor at last new line. | |
2557 * Otherwise: cursor at first new line. | |
2558 */ | |
2559 if (exmode_active) | |
2560 curwin->w_cursor.lnum = from + linecnt; | |
2561 else | |
2562 curwin->w_cursor.lnum = from + 1; | |
2563 check_cursor_lnum(); | |
2564 beginline(BL_WHITE | BL_FIX); /* on first non-blank */ | |
2565 | |
2566 /* | |
2567 * Set '[ and '] marks to the newly read lines. | |
2568 */ | |
2569 curbuf->b_op_start.lnum = from + 1; | |
2570 curbuf->b_op_start.col = 0; | |
2571 curbuf->b_op_end.lnum = from + linecnt; | |
2572 curbuf->b_op_end.col = 0; | |
696 | 2573 |
2574 #ifdef WIN32 | |
2575 /* | |
2576 * Work around a weird problem: When a file has two links (only | |
2577 * possible on NTFS) and we write through one link, then stat() it | |
1651 | 2578 * through the other link, the timestamp information may be wrong. |
696 | 2579 * It's correct again after reading the file, thus reset the timestamp |
2580 * here. | |
2581 */ | |
2582 if (newfile && !read_stdin && !read_buffer | |
2583 && mch_stat((char *)fname, &st) >= 0) | |
2584 { | |
2585 buf_store_time(curbuf, &st, fname); | |
2586 curbuf->b_mtime_read = curbuf->b_mtime; | |
2587 } | |
2588 #endif | |
7 | 2589 } |
2590 msg_scroll = msg_save; | |
2591 | |
2592 #ifdef FEAT_VIMINFO | |
2593 /* | |
2594 * Get the marks before executing autocommands, so they can be used there. | |
2595 */ | |
2596 check_marks_read(); | |
2597 #endif | |
2598 | |
2599 /* | |
2600 * Trick: We remember if the last line of the read didn't have | |
2707 | 2601 * an eol even when 'binary' is off, for when writing it again with |
2602 * 'binary' on. This is required for | |
7 | 2603 * ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work. |
2604 */ | |
2707 | 2605 curbuf->b_no_eol_lnum = read_no_eol_lnum; |
7 | 2606 |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2607 /* 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
|
2608 * different. */ |
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2609 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
|
2610 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
|
2611 |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2612 #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
|
2613 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2614 * 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
|
2615 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2616 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
|
2617 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2618 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
|
2619 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2620 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
|
2621 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
|
2622 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2623 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2624 |
167 | 2625 #ifdef FEAT_AUTOCMD |
7 | 2626 if (!read_stdin && !read_buffer) |
2627 { | |
2628 int m = msg_scroll; | |
2629 int n = msg_scrolled; | |
2630 | |
2631 /* Save the fileformat now, otherwise the buffer will be considered | |
2632 * modified if the format/encoding was automatically detected. */ | |
819 | 2633 if (set_options) |
7 | 2634 save_file_ff(curbuf); |
2635 | |
2636 /* | |
2637 * The output from the autocommands should not overwrite anything and | |
2638 * should not be overwritten: Set msg_scroll, restore its value if no | |
2639 * output was done. | |
2640 */ | |
2641 msg_scroll = TRUE; | |
2642 if (filtering) | |
2643 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname, | |
2644 FALSE, curbuf, eap); | |
2645 else if (newfile) | |
2646 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname, | |
2647 FALSE, curbuf, eap); | |
2648 else | |
2649 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname, | |
2650 FALSE, NULL, eap); | |
2651 if (msg_scrolled == n) | |
2652 msg_scroll = m; | |
2707 | 2653 # ifdef FEAT_EVAL |
7 | 2654 if (aborting()) /* autocmds may abort script processing */ |
2655 return FAIL; | |
2707 | 2656 # endif |
2657 } | |
2658 #endif | |
2659 | |
7 | 2660 if (recoverymode && error) |
2661 return FAIL; | |
2662 return OK; | |
2663 } | |
2664 | |
1313 | 2665 #ifdef OPEN_CHR_FILES |
2666 /* | |
2667 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+", | |
2668 * which is the name of files used for process substitution output by | |
2669 * some shells on some operating systems, e.g., bash on SunOS. | |
2670 * Do not accept "/dev/fd/[012]", opening these may hang Vim. | |
2671 */ | |
2672 static int | |
2673 is_dev_fd_file(fname) | |
2674 char_u *fname; | |
2675 { | |
2676 return (STRNCMP(fname, "/dev/fd/", 8) == 0 | |
2677 && VIM_ISDIGIT(fname[8]) | |
2678 && *skipdigits(fname + 9) == NUL | |
2679 && (fname[9] != NUL | |
2680 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2'))); | |
2681 } | |
2682 #endif | |
2683 | |
595 | 2684 #ifdef FEAT_MBYTE |
2685 | |
2686 /* | |
2687 * From the current line count and characters read after that, estimate the | |
2688 * line number where we are now. | |
2689 * Used for error messages that include a line number. | |
2690 */ | |
2691 static linenr_T | |
2692 readfile_linenr(linecnt, p, endp) | |
2693 linenr_T linecnt; /* line count before reading more bytes */ | |
2694 char_u *p; /* start of more bytes read */ | |
2695 char_u *endp; /* end of more bytes read */ | |
2696 { | |
2697 char_u *s; | |
2698 linenr_T lnum; | |
2699 | |
2700 lnum = curbuf->b_ml.ml_line_count - linecnt + 1; | |
2701 for (s = p; s < endp; ++s) | |
2702 if (*s == '\n') | |
2703 ++lnum; | |
2704 return lnum; | |
2705 } | |
2706 #endif | |
2707 | |
7 | 2708 /* |
612 | 2709 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be |
2710 * equal to the buffer "buf". Used for calling readfile(). | |
7 | 2711 * Returns OK or FAIL. |
2712 */ | |
2713 int | |
2714 prep_exarg(eap, buf) | |
2715 exarg_T *eap; | |
2716 buf_T *buf; | |
2717 { | |
2718 eap->cmd = alloc((unsigned)(STRLEN(buf->b_p_ff) | |
2719 #ifdef FEAT_MBYTE | |
2720 + STRLEN(buf->b_p_fenc) | |
2721 #endif | |
2722 + 15)); | |
2723 if (eap->cmd == NULL) | |
2724 return FAIL; | |
2725 | |
2726 #ifdef FEAT_MBYTE | |
2727 sprintf((char *)eap->cmd, "e ++ff=%s ++enc=%s", buf->b_p_ff, buf->b_p_fenc); | |
2728 eap->force_enc = 14 + (int)STRLEN(buf->b_p_ff); | |
612 | 2729 eap->bad_char = buf->b_bad_char; |
7 | 2730 #else |
2731 sprintf((char *)eap->cmd, "e ++ff=%s", buf->b_p_ff); | |
2732 #endif | |
2733 eap->force_ff = 7; | |
612 | 2734 |
2735 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN; | |
819 | 2736 eap->read_edit = FALSE; |
612 | 2737 eap->forceit = FALSE; |
7 | 2738 return OK; |
2739 } | |
2740 | |
2741 #ifdef FEAT_MBYTE | |
2742 /* | |
2743 * Find next fileencoding to use from 'fileencodings'. | |
2744 * "pp" points to fenc_next. It's advanced to the next item. | |
2745 * When there are no more items, an empty string is returned and *pp is set to | |
2746 * NULL. | |
2747 * When *pp is not set to NULL, the result is in allocated memory. | |
2748 */ | |
2749 static char_u * | |
2750 next_fenc(pp) | |
2751 char_u **pp; | |
2752 { | |
2753 char_u *p; | |
2754 char_u *r; | |
2755 | |
2756 if (**pp == NUL) | |
2757 { | |
2758 *pp = NULL; | |
2759 return (char_u *)""; | |
2760 } | |
2761 p = vim_strchr(*pp, ','); | |
2762 if (p == NULL) | |
2763 { | |
2764 r = enc_canonize(*pp); | |
2765 *pp += STRLEN(*pp); | |
2766 } | |
2767 else | |
2768 { | |
2769 r = vim_strnsave(*pp, (int)(p - *pp)); | |
2770 *pp = p + 1; | |
2771 if (r != NULL) | |
2772 { | |
2773 p = enc_canonize(r); | |
2774 vim_free(r); | |
2775 r = p; | |
2776 } | |
2777 } | |
2778 if (r == NULL) /* out of memory */ | |
2779 { | |
2780 r = (char_u *)""; | |
2781 *pp = NULL; | |
2782 } | |
2783 return r; | |
2784 } | |
2785 | |
2786 # ifdef FEAT_EVAL | |
2787 /* | |
2788 * Convert a file with the 'charconvert' expression. | |
2789 * This closes the file which is to be read, converts it and opens the | |
2790 * resulting file for reading. | |
2791 * Returns name of the resulting converted file (the caller should delete it | |
2792 * after reading it). | |
2793 * Returns NULL if the conversion failed ("*fdp" is not set) . | |
2794 */ | |
2795 static char_u * | |
2796 readfile_charconvert(fname, fenc, fdp) | |
2797 char_u *fname; /* name of input file */ | |
2798 char_u *fenc; /* converted from */ | |
2799 int *fdp; /* in/out: file descriptor of file */ | |
2800 { | |
2801 char_u *tmpname; | |
2802 char_u *errmsg = NULL; | |
2803 | |
2804 tmpname = vim_tempname('r'); | |
2805 if (tmpname == NULL) | |
2806 errmsg = (char_u *)_("Can't find temp file for conversion"); | |
2807 else | |
2808 { | |
2809 close(*fdp); /* close the input file, ignore errors */ | |
2810 *fdp = -1; | |
2811 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc, | |
2812 fname, tmpname) == FAIL) | |
2813 errmsg = (char_u *)_("Conversion with 'charconvert' failed"); | |
2814 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname, | |
2815 O_RDONLY | O_EXTRA, 0)) < 0) | |
2816 errmsg = (char_u *)_("can't read output of 'charconvert'"); | |
2817 } | |
2818 | |
2819 if (errmsg != NULL) | |
2820 { | |
2821 /* Don't use emsg(), it breaks mappings, the retry with | |
2822 * another type of conversion might still work. */ | |
2823 MSG(errmsg); | |
2824 if (tmpname != NULL) | |
2825 { | |
2826 mch_remove(tmpname); /* delete converted file */ | |
2827 vim_free(tmpname); | |
2828 tmpname = NULL; | |
2829 } | |
2830 } | |
2831 | |
2832 /* If the input file is closed, open it (caller should check for error). */ | |
2833 if (*fdp < 0) | |
2834 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
2835 | |
2836 return tmpname; | |
2837 } | |
2838 # endif | |
2839 | |
2840 #endif | |
2841 | |
2842 #ifdef FEAT_VIMINFO | |
2843 /* | |
2844 * Read marks for the current buffer from the viminfo file, when we support | |
2845 * buffer marks and the buffer has a name. | |
2846 */ | |
2847 static void | |
2848 check_marks_read() | |
2849 { | |
2850 if (!curbuf->b_marks_read && get_viminfo_parameter('\'') > 0 | |
2851 && curbuf->b_ffname != NULL) | |
1733 | 2852 read_viminfo(NULL, VIF_WANT_MARKS); |
7 | 2853 |
2854 /* Always set b_marks_read; needed when 'viminfo' is changed to include | |
2855 * the ' parameter after opening a buffer. */ | |
2856 curbuf->b_marks_read = TRUE; | |
2857 } | |
2858 #endif | |
2859 | |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2860 #if defined(FEAT_CRYPT) || defined(PROTO) |
7 | 2861 /* |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2862 * Get the crypt method used for a file from "ptr[len]", the magic text at the |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2863 * start of the file. |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2864 * Returns -1 when no encryption used. |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2865 */ |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2866 static int |
2360
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
2867 crypt_method_from_magic(ptr, len) |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2868 char *ptr; |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2869 int len; |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2870 { |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2871 int i; |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2872 |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2873 for (i = 0; i < (int)(sizeof(crypt_magic) / sizeof(crypt_magic[0])); i++) |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2874 { |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2875 if (len < (CRYPT_MAGIC_LEN + crypt_salt_len[i] + crypt_seed_len[i])) |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2876 continue; |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2877 if (memcmp(ptr, crypt_magic[i], CRYPT_MAGIC_LEN) == 0) |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2878 return i; |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2879 } |
2204
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
2880 |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
2881 i = (int)STRLEN(crypt_magic_head); |
2204
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
2882 if (len >= i && memcmp(ptr, crypt_magic_head, i) == 0) |
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
2883 EMSG(_("E821: File is encrypted with unknown method")); |
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
2884 |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2885 return -1; |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2886 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2887 |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2888 /* |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2889 * Check for magic number used for encryption. Applies to the current buffer. |
7 | 2890 * If found, the magic number is removed from ptr[*sizep] and *sizep and |
2891 * *filesizep are updated. | |
2892 * Return the (new) encryption key, NULL for no encryption. | |
2893 */ | |
2894 static char_u * | |
2267 | 2895 check_for_cryptkey(cryptkey, ptr, sizep, filesizep, newfile, fname, did_ask) |
7 | 2896 char_u *cryptkey; /* previous encryption key or NULL */ |
2897 char_u *ptr; /* pointer to read bytes */ | |
2898 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
|
2899 off_t *filesizep; /* nr of bytes used from file */ |
7 | 2900 int newfile; /* editing a new buffer */ |
2267 | 2901 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
|
2902 int *did_ask; /* flag: whether already asked for key */ |
7 | 2903 { |
2360
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
2904 int method = crypt_method_from_magic((char *)ptr, *sizep); |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2905 |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2906 if (method >= 0) |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2907 { |
2360
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
2908 set_crypt_method(curbuf, method); |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2909 if (method > 0) |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2910 (void)blowfish_self_test(); |
2204
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
2911 if (cryptkey == NULL && !*did_ask) |
7 | 2912 { |
2913 if (*curbuf->b_p_key) | |
2914 cryptkey = curbuf->b_p_key; | |
2915 else | |
2916 { | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2917 /* 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
|
2918 * 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
|
2919 * 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
|
2920 * Happens when retrying to detect encoding. */ |
2267 | 2921 smsg((char_u *)_(need_key_msg), fname); |
2922 msg_scroll = TRUE; | |
7 | 2923 cryptkey = get_crypt_key(newfile, FALSE); |
2204
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
2924 *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
|
2925 |
7 | 2926 /* check if empty key entered */ |
2927 if (cryptkey != NULL && *cryptkey == NUL) | |
2928 { | |
2929 if (cryptkey != curbuf->b_p_key) | |
2930 vim_free(cryptkey); | |
2931 cryptkey = NULL; | |
2932 } | |
2933 } | |
2934 } | |
2935 | |
2936 if (cryptkey != NULL) | |
2937 { | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2938 int seed_len = crypt_seed_len[method]; |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2939 int salt_len = crypt_salt_len[method]; |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2940 |
2267 | 2941 crypt_push_state(); |
2942 use_crypt_method = method; | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2943 if (method == 0) |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2944 crypt_init_keys(cryptkey); |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2945 else |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2946 { |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2947 bf_key_init(cryptkey, ptr + CRYPT_MAGIC_LEN, salt_len); |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2948 bf_ofb_init(ptr + CRYPT_MAGIC_LEN + salt_len, seed_len); |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2949 } |
7 | 2950 |
2951 /* Remove magic number from the text */ | |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2952 *filesizep += CRYPT_MAGIC_LEN + salt_len + seed_len; |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2953 *sizep -= CRYPT_MAGIC_LEN + salt_len + seed_len; |
2267 | 2954 mch_memmove(ptr, ptr + CRYPT_MAGIC_LEN + salt_len + seed_len, |
2955 (size_t)*sizep); | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2956 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2957 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2958 /* 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
|
2959 * 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
|
2960 else if (newfile && *curbuf->b_p_key != NUL && !starting) |
7 | 2961 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL); |
2962 | |
2963 return cryptkey; | |
2964 } | |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2965 |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2966 /* |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2967 * Check for magic number used for encryption. Applies to the current buffer. |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2968 * If found and decryption is possible returns OK; |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2969 */ |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2970 int |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2971 prepare_crypt_read(fp) |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2972 FILE *fp; |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2973 { |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2974 int method; |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2975 char_u buffer[CRYPT_MAGIC_LEN + CRYPT_SALT_LEN_MAX |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2976 + CRYPT_SEED_LEN_MAX + 2]; |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2977 |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2978 if (fread(buffer, CRYPT_MAGIC_LEN, 1, fp) != 1) |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2979 return FAIL; |
2360
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
2980 method = crypt_method_from_magic((char *)buffer, |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2981 CRYPT_MAGIC_LEN + |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2281
diff
changeset
|
2982 CRYPT_SEED_LEN_MAX + |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2281
diff
changeset
|
2983 CRYPT_SALT_LEN_MAX); |
2360
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
2984 if (method < 0 || method != get_crypt_method(curbuf)) |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2985 return FAIL; |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2986 |
2267 | 2987 crypt_push_state(); |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2988 if (method == 0) |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2989 crypt_init_keys(curbuf->b_p_key); |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2990 else |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2991 { |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2992 int salt_len = crypt_salt_len[method]; |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2993 int seed_len = crypt_seed_len[method]; |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2994 |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2995 if (fread(buffer, salt_len + seed_len, 1, fp) != 1) |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2996 return FAIL; |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2997 bf_key_init(curbuf->b_p_key, buffer, salt_len); |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2998 bf_ofb_init(buffer + salt_len, seed_len); |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2999 } |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3000 return OK; |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3001 } |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3002 |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3003 /* |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3004 * Prepare for writing encrypted bytes for buffer "buf". |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3005 * Returns a pointer to an allocated header of length "*lenp". |
2267 | 3006 * When out of memory returns NULL. |
3007 * Otherwise calls crypt_push_state(), call crypt_pop_state() later. | |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3008 */ |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3009 char_u * |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3010 prepare_crypt_write(buf, lenp) |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3011 buf_T *buf; |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3012 int *lenp; |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3013 { |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3014 char_u *header; |
2360
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3015 int seed_len = crypt_seed_len[get_crypt_method(buf)]; |
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3016 int salt_len = crypt_salt_len[get_crypt_method(buf)]; |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3017 char_u *salt; |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3018 char_u *seed; |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3019 |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3020 header = alloc_clear(CRYPT_MAGIC_LEN + CRYPT_SALT_LEN_MAX |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3021 + CRYPT_SEED_LEN_MAX + 2); |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3022 if (header != NULL) |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3023 { |
2267 | 3024 crypt_push_state(); |
2360
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3025 use_crypt_method = get_crypt_method(buf); /* select zip or blowfish */ |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3026 vim_strncpy(header, (char_u *)crypt_magic[use_crypt_method], |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3027 CRYPT_MAGIC_LEN); |
2360
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3028 if (use_crypt_method == 0) |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3029 crypt_init_keys(buf->b_p_key); |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3030 else |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3031 { |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3032 /* Using blowfish, add salt and seed. */ |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3033 salt = header + CRYPT_MAGIC_LEN; |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3034 seed = salt + salt_len; |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3035 sha2_seed(salt, salt_len, seed, seed_len); |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3036 bf_key_init(buf->b_p_key, salt, salt_len); |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3037 bf_ofb_init(seed, seed_len); |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3038 } |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3039 } |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3040 *lenp = CRYPT_MAGIC_LEN + salt_len + seed_len; |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3041 return header; |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3042 } |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3043 |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3044 #endif /* FEAT_CRYPT */ |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3045 |
7 | 3046 #ifdef UNIX |
3047 static void | |
3048 set_file_time(fname, atime, mtime) | |
3049 char_u *fname; | |
3050 time_t atime; /* access time */ | |
3051 time_t mtime; /* modification time */ | |
3052 { | |
3053 # if defined(HAVE_UTIME) && defined(HAVE_UTIME_H) | |
3054 struct utimbuf buf; | |
3055 | |
3056 buf.actime = atime; | |
3057 buf.modtime = mtime; | |
3058 (void)utime((char *)fname, &buf); | |
3059 # else | |
3060 # if defined(HAVE_UTIMES) | |
3061 struct timeval tvp[2]; | |
3062 | |
3063 tvp[0].tv_sec = atime; | |
3064 tvp[0].tv_usec = 0; | |
3065 tvp[1].tv_sec = mtime; | |
3066 tvp[1].tv_usec = 0; | |
3067 # ifdef NeXT | |
3068 (void)utimes((char *)fname, tvp); | |
3069 # else | |
3070 (void)utimes((char *)fname, (const struct timeval *)&tvp); | |
3071 # endif | |
3072 # endif | |
3073 # endif | |
3074 } | |
3075 #endif /* UNIX */ | |
3076 | |
22 | 3077 #if defined(VMS) && !defined(MIN) |
3078 /* Older DECC compiler for VAX doesn't define MIN() */ | |
3079 # define MIN(a, b) ((a) < (b) ? (a) : (b)) | |
3080 #endif | |
3081 | |
7 | 3082 /* |
1303 | 3083 * Return TRUE if a file appears to be read-only from the file permissions. |
3084 */ | |
3085 int | |
3086 check_file_readonly(fname, perm) | |
3087 char_u *fname; /* full path to file */ | |
3088 int perm; /* known permissions on file */ | |
3089 { | |
3090 #ifndef USE_MCH_ACCESS | |
3091 int fd = 0; | |
3092 #endif | |
3093 | |
3094 return ( | |
3095 #ifdef USE_MCH_ACCESS | |
3096 # ifdef UNIX | |
3097 (perm & 0222) == 0 || | |
3098 # endif | |
3099 mch_access((char *)fname, W_OK) | |
3100 #else | |
3101 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0 | |
3102 ? TRUE : (close(fd), FALSE) | |
3103 #endif | |
3104 ); | |
3105 } | |
3106 | |
3107 | |
3108 /* | |
589 | 3109 * buf_write() - write to file "fname" lines "start" through "end" |
7 | 3110 * |
3111 * We do our own buffering here because fwrite() is so slow. | |
3112 * | |
589 | 3113 * If "forceit" is true, we don't care for errors when attempting backups. |
3114 * In case of an error everything possible is done to restore the original | |
1698 | 3115 * file. But when "forceit" is TRUE, we risk losing it. |
589 | 3116 * |
3117 * When "reset_changed" is TRUE and "append" == FALSE and "start" == 1 and | |
3118 * "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed. | |
7 | 3119 * |
3120 * This function must NOT use NameBuff (because it's called by autowrite()). | |
3121 * | |
3122 * return FAIL for failure, OK otherwise | |
3123 */ | |
3124 int | |
3125 buf_write(buf, fname, sfname, start, end, eap, append, forceit, | |
3126 reset_changed, filtering) | |
3127 buf_T *buf; | |
3128 char_u *fname; | |
3129 char_u *sfname; | |
3130 linenr_T start, end; | |
3131 exarg_T *eap; /* for forced 'ff' and 'fenc', can be | |
3132 NULL! */ | |
589 | 3133 int append; /* append to the file */ |
7 | 3134 int forceit; |
3135 int reset_changed; | |
3136 int filtering; | |
3137 { | |
3138 int fd; | |
3139 char_u *backup = NULL; | |
3140 int backup_copy = FALSE; /* copy the original file? */ | |
3141 int dobackup; | |
3142 char_u *ffname; | |
3143 char_u *wfname = NULL; /* name of file to write to */ | |
3144 char_u *s; | |
3145 char_u *ptr; | |
3146 char_u c; | |
3147 int len; | |
3148 linenr_T lnum; | |
3149 long nchars; | |
3150 char_u *errmsg = NULL; | |
1947 | 3151 int errmsg_allocated = FALSE; |
7 | 3152 char_u *errnum = NULL; |
3153 char_u *buffer; | |
3154 char_u smallbuf[SMBUFSIZE]; | |
3155 char_u *backup_ext; | |
3156 int bufsize; | |
3157 long perm; /* file permissions */ | |
3158 int retval = OK; | |
3159 int newfile = FALSE; /* TRUE if file doesn't exist yet */ | |
3160 int msg_save = msg_scroll; | |
3161 int overwriting; /* TRUE if writing over original */ | |
3162 int no_eol = FALSE; /* no end-of-line written */ | |
3163 int device = FALSE; /* writing to a device */ | |
3164 struct stat st_old; | |
3165 int prev_got_int = got_int; | |
3166 int file_readonly = FALSE; /* overwritten file is read-only */ | |
3167 static char *err_readonly = "is read-only (cannot override: \"W\" in 'cpoptions')"; | |
3168 #if defined(UNIX) || defined(__EMX__XX) /*XXX fix me sometime? */ | |
3169 int made_writable = FALSE; /* 'w' bit has been set */ | |
3170 #endif | |
3171 /* writing everything */ | |
3172 int whole = (start == 1 && end == buf->b_ml.ml_line_count); | |
3173 #ifdef FEAT_AUTOCMD | |
3174 linenr_T old_line_count = buf->b_ml.ml_line_count; | |
3175 #endif | |
3176 int attr; | |
3177 int fileformat; | |
3178 int write_bin; | |
3179 struct bw_info write_info; /* info for buf_write_bytes() */ | |
3180 #ifdef FEAT_MBYTE | |
3181 int converted = FALSE; | |
3182 int notconverted = FALSE; | |
3183 char_u *fenc; /* effective 'fileencoding' */ | |
3184 char_u *fenc_tofree = NULL; /* allocated "fenc" */ | |
3185 #endif | |
3186 #ifdef HAS_BW_FLAGS | |
3187 int wb_flags = 0; | |
3188 #endif | |
3189 #ifdef HAVE_ACL | |
3190 vim_acl_T acl = NULL; /* ACL copied from original file to | |
3191 backup or new file */ | |
3192 #endif | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3193 #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
|
3194 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
|
3195 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
|
3196 #endif |
2717 | 3197 #ifdef FEAT_CRYPT |
3198 int crypt_method_used; | |
3199 #endif | |
7 | 3200 |
3201 if (fname == NULL || *fname == NUL) /* safety check */ | |
3202 return FAIL; | |
2028 | 3203 if (buf->b_ml.ml_mfp == NULL) |
3204 { | |
3205 /* This can happen during startup when there is a stray "w" in the | |
3206 * vimrc file. */ | |
3207 EMSG(_(e_emptybuf)); | |
3208 return FAIL; | |
3209 } | |
7 | 3210 |
3211 /* | |
3212 * Disallow writing from .exrc and .vimrc in current directory for | |
3213 * security reasons. | |
3214 */ | |
3215 if (check_secure()) | |
3216 return FAIL; | |
3217 | |
3218 /* Avoid a crash for a long name. */ | |
3219 if (STRLEN(fname) >= MAXPATHL) | |
3220 { | |
3221 EMSG(_(e_longname)); | |
3222 return FAIL; | |
3223 } | |
3224 | |
3225 #ifdef FEAT_MBYTE | |
3226 /* must init bw_conv_buf and bw_iconv_fd before jumping to "fail" */ | |
3227 write_info.bw_conv_buf = NULL; | |
3228 write_info.bw_conv_error = FALSE; | |
1947 | 3229 write_info.bw_conv_error_lnum = 0; |
7 | 3230 write_info.bw_restlen = 0; |
3231 # ifdef USE_ICONV | |
3232 write_info.bw_iconv_fd = (iconv_t)-1; | |
3233 # endif | |
3234 #endif | |
3235 | |
167 | 3236 /* After writing a file changedtick changes but we don't want to display |
3237 * the line. */ | |
3238 ex_no_reprint = TRUE; | |
3239 | |
7 | 3240 /* |
3241 * If there is no file name yet, use the one for the written file. | |
3242 * BF_NOTEDITED is set to reflect this (in case the write fails). | |
3243 * Don't do this when the write is for a filter command. | |
589 | 3244 * Don't do this when appending. |
3245 * Only do this when 'cpoptions' contains the 'F' flag. | |
7 | 3246 */ |
633 | 3247 if (buf->b_ffname == NULL |
3248 && reset_changed | |
7 | 3249 && whole |
3250 && buf == curbuf | |
236 | 3251 #ifdef FEAT_QUICKFIX |
3252 && !bt_nofile(buf) | |
3253 #endif | |
7 | 3254 && !filtering |
589 | 3255 && (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL) |
7 | 3256 && vim_strchr(p_cpo, CPO_FNAMEW) != NULL) |
3257 { | |
633 | 3258 if (set_rw_fname(fname, sfname) == FAIL) |
7 | 3259 return FAIL; |
633 | 3260 buf = curbuf; /* just in case autocmds made "buf" invalid */ |
7 | 3261 } |
3262 | |
3263 if (sfname == NULL) | |
3264 sfname = fname; | |
3265 /* | |
3266 * For Unix: Use the short file name whenever possible. | |
3267 * Avoids problems with networks and when directory names are changed. | |
3268 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to | |
3269 * another directory, which we don't detect | |
3270 */ | |
3271 ffname = fname; /* remember full fname */ | |
3272 #ifdef UNIX | |
3273 fname = sfname; | |
3274 #endif | |
3275 | |
3276 if (buf->b_ffname != NULL && fnamecmp(ffname, buf->b_ffname) == 0) | |
3277 overwriting = TRUE; | |
3278 else | |
3279 overwriting = FALSE; | |
3280 | |
3281 if (exiting) | |
4352 | 3282 settmode(TMODE_COOK); /* when exiting allow typeahead now */ |
7 | 3283 |
3284 ++no_wait_return; /* don't wait for return yet */ | |
3285 | |
3286 /* | |
3287 * Set '[ and '] marks to the lines to be written. | |
3288 */ | |
3289 buf->b_op_start.lnum = start; | |
3290 buf->b_op_start.col = 0; | |
3291 buf->b_op_end.lnum = end; | |
3292 buf->b_op_end.col = 0; | |
3293 | |
3294 #ifdef FEAT_AUTOCMD | |
3295 { | |
3296 aco_save_T aco; | |
3297 int buf_ffname = FALSE; | |
3298 int buf_sfname = FALSE; | |
3299 int buf_fname_f = FALSE; | |
3300 int buf_fname_s = FALSE; | |
3301 int did_cmd = FALSE; | |
17 | 3302 int nofile_err = FALSE; |
161 | 3303 int empty_memline = (buf->b_ml.ml_mfp == NULL); |
7 | 3304 |
3305 /* | |
4352 | 3306 * Apply PRE autocommands. |
7 | 3307 * Set curbuf to the buffer to be written. |
3308 * Careful: The autocommands may call buf_write() recursively! | |
3309 */ | |
3310 if (ffname == buf->b_ffname) | |
3311 buf_ffname = TRUE; | |
3312 if (sfname == buf->b_sfname) | |
3313 buf_sfname = TRUE; | |
3314 if (fname == buf->b_ffname) | |
3315 buf_fname_f = TRUE; | |
3316 if (fname == buf->b_sfname) | |
3317 buf_fname_s = TRUE; | |
3318 | |
3319 /* set curwin/curbuf to buf and save a few things */ | |
3320 aucmd_prepbuf(&aco, buf); | |
3321 | |
3322 if (append) | |
3323 { | |
3324 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD, | |
3325 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3326 { |
635 | 3327 #ifdef FEAT_QUICKFIX |
18 | 3328 if (overwriting && bt_nofile(curbuf)) |
17 | 3329 nofile_err = TRUE; |
3330 else | |
635 | 3331 #endif |
17 | 3332 apply_autocmds_exarg(EVENT_FILEAPPENDPRE, |
7 | 3333 sfname, sfname, FALSE, curbuf, eap); |
17 | 3334 } |
7 | 3335 } |
3336 else if (filtering) | |
3337 { | |
3338 apply_autocmds_exarg(EVENT_FILTERWRITEPRE, | |
3339 NULL, sfname, FALSE, curbuf, eap); | |
3340 } | |
3341 else if (reset_changed && whole) | |
3342 { | |
3036 | 3343 int was_changed = curbufIsChanged(); |
3344 | |
3345 did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD, | |
3346 sfname, sfname, FALSE, curbuf, eap); | |
3347 if (did_cmd) | |
3348 { | |
3349 if (was_changed && !curbufIsChanged()) | |
3350 { | |
3351 /* Written everything correctly and BufWriteCmd has reset | |
3352 * 'modified': Correct the undo information so that an | |
3353 * undo now sets 'modified'. */ | |
3354 u_unchanged(curbuf); | |
3355 u_update_save_nr(curbuf); | |
3356 } | |
3357 } | |
3358 else | |
17 | 3359 { |
635 | 3360 #ifdef FEAT_QUICKFIX |
179 | 3361 if (overwriting && bt_nofile(curbuf)) |
17 | 3362 nofile_err = TRUE; |
3363 else | |
635 | 3364 #endif |
17 | 3365 apply_autocmds_exarg(EVENT_BUFWRITEPRE, |
7 | 3366 sfname, sfname, FALSE, curbuf, eap); |
17 | 3367 } |
7 | 3368 } |
3369 else | |
3370 { | |
3371 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD, | |
3372 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3373 { |
635 | 3374 #ifdef FEAT_QUICKFIX |
179 | 3375 if (overwriting && bt_nofile(curbuf)) |
17 | 3376 nofile_err = TRUE; |
3377 else | |
635 | 3378 #endif |
17 | 3379 apply_autocmds_exarg(EVENT_FILEWRITEPRE, |
7 | 3380 sfname, sfname, FALSE, curbuf, eap); |
17 | 3381 } |
7 | 3382 } |
3383 | |
3384 /* restore curwin/curbuf and a few other things */ | |
3385 aucmd_restbuf(&aco); | |
3386 | |
3387 /* | |
3388 * In three situations we return here and don't write the file: | |
3389 * 1. the autocommands deleted or unloaded the buffer. | |
3390 * 2. The autocommands abort script processing. | |
3391 * 3. If one of the "Cmd" autocommands was executed. | |
3392 */ | |
3393 if (!buf_valid(buf)) | |
3394 buf = NULL; | |
161 | 3395 if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline) |
532 | 3396 || did_cmd || nofile_err |
3397 #ifdef FEAT_EVAL | |
3398 || aborting() | |
3399 #endif | |
3400 ) | |
7 | 3401 { |
3402 --no_wait_return; | |
3403 msg_scroll = msg_save; | |
17 | 3404 if (nofile_err) |
3405 EMSG(_("E676: No matching autocommands for acwrite buffer")); | |
3406 | |
532 | 3407 if (nofile_err |
3408 #ifdef FEAT_EVAL | |
3409 || aborting() | |
3410 #endif | |
3411 ) | |
7 | 3412 /* An aborting error, interrupt or exception in the |
3413 * autocommands. */ | |
3414 return FAIL; | |
3415 if (did_cmd) | |
3416 { | |
3417 if (buf == NULL) | |
3418 /* The buffer was deleted. We assume it was written | |
3419 * (can't retry anyway). */ | |
3420 return OK; | |
3421 if (overwriting) | |
3422 { | |
3423 /* Assume the buffer was written, update the timestamp. */ | |
3424 ml_timestamp(buf); | |
589 | 3425 if (append) |
3426 buf->b_flags &= ~BF_NEW; | |
3427 else | |
3428 buf->b_flags &= ~BF_WRITE_MASK; | |
7 | 3429 } |
589 | 3430 if (reset_changed && buf->b_changed && !append |
39 | 3431 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)) |
7 | 3432 /* Buffer still changed, the autocommands didn't work |
3433 * properly. */ | |
3434 return FAIL; | |
3435 return OK; | |
3436 } | |
3437 #ifdef FEAT_EVAL | |
3438 if (!aborting()) | |
3439 #endif | |
3440 EMSG(_("E203: Autocommands deleted or unloaded buffer to be written")); | |
3441 return FAIL; | |
3442 } | |
3443 | |
3444 /* | |
3445 * The autocommands may have changed the number of lines in the file. | |
3446 * When writing the whole file, adjust the end. | |
3447 * When writing part of the file, assume that the autocommands only | |
3448 * changed the number of lines that are to be written (tricky!). | |
3449 */ | |
3450 if (buf->b_ml.ml_line_count != old_line_count) | |
3451 { | |
3452 if (whole) /* write all */ | |
3453 end = buf->b_ml.ml_line_count; | |
3454 else if (buf->b_ml.ml_line_count > old_line_count) /* more lines */ | |
3455 end += buf->b_ml.ml_line_count - old_line_count; | |
3456 else /* less lines */ | |
3457 { | |
3458 end -= old_line_count - buf->b_ml.ml_line_count; | |
3459 if (end < start) | |
3460 { | |
3461 --no_wait_return; | |
3462 msg_scroll = msg_save; | |
3463 EMSG(_("E204: Autocommand changed number of lines in unexpected way")); | |
3464 return FAIL; | |
3465 } | |
3466 } | |
3467 } | |
3468 | |
3469 /* | |
3470 * The autocommands may have changed the name of the buffer, which may | |
3471 * be kept in fname, ffname and sfname. | |
3472 */ | |
3473 if (buf_ffname) | |
3474 ffname = buf->b_ffname; | |
3475 if (buf_sfname) | |
3476 sfname = buf->b_sfname; | |
3477 if (buf_fname_f) | |
3478 fname = buf->b_ffname; | |
3479 if (buf_fname_s) | |
3480 fname = buf->b_sfname; | |
3481 } | |
3482 #endif | |
3483 | |
3484 #ifdef FEAT_NETBEANS_INTG | |
2210 | 3485 if (netbeans_active() && isNetbeansBuffer(buf)) |
7 | 3486 { |
3487 if (whole) | |
3488 { | |
3489 /* | |
3490 * b_changed can be 0 after an undo, but we still need to write | |
3491 * the buffer to NetBeans. | |
3492 */ | |
3493 if (buf->b_changed || isNetbeansModified(buf)) | |
3494 { | |
33 | 3495 --no_wait_return; /* may wait for return now */ |
3496 msg_scroll = msg_save; | |
3497 netbeans_save_buffer(buf); /* no error checking... */ | |
7 | 3498 return retval; |
3499 } | |
3500 else | |
3501 { | |
3502 errnum = (char_u *)"E656: "; | |
1653 | 3503 errmsg = (char_u *)_("NetBeans disallows writes of unmodified buffers"); |
7 | 3504 buffer = NULL; |
3505 goto fail; | |
3506 } | |
3507 } | |
3508 else | |
3509 { | |
3510 errnum = (char_u *)"E657: "; | |
3511 errmsg = (char_u *)_("Partial writes disallowed for NetBeans buffers"); | |
3512 buffer = NULL; | |
3513 goto fail; | |
3514 } | |
3515 } | |
3516 #endif | |
3517 | |
3518 if (shortmess(SHM_OVER) && !exiting) | |
3519 msg_scroll = FALSE; /* overwrite previous file message */ | |
3520 else | |
3521 msg_scroll = TRUE; /* don't overwrite previous file message */ | |
3522 if (!filtering) | |
3523 filemess(buf, | |
3524 #ifndef UNIX | |
3525 sfname, | |
3526 #else | |
3527 fname, | |
3528 #endif | |
3529 (char_u *)"", 0); /* show that we are busy */ | |
3530 msg_scroll = FALSE; /* always overwrite the file message now */ | |
3531 | |
3532 buffer = alloc(BUFSIZE); | |
3533 if (buffer == NULL) /* can't allocate big buffer, use small | |
3534 * one (to be able to write when out of | |
3535 * memory) */ | |
3536 { | |
3537 buffer = smallbuf; | |
3538 bufsize = SMBUFSIZE; | |
3539 } | |
3540 else | |
3541 bufsize = BUFSIZE; | |
3542 | |
3543 /* | |
3544 * Get information about original file (if there is one). | |
3545 */ | |
3546 #if defined(UNIX) && !defined(ARCHIE) | |
1438 | 3547 st_old.st_dev = 0; |
3548 st_old.st_ino = 0; | |
7 | 3549 perm = -1; |
3550 if (mch_stat((char *)fname, &st_old) < 0) | |
3551 newfile = TRUE; | |
3552 else | |
3553 { | |
3554 perm = st_old.st_mode; | |
3555 if (!S_ISREG(st_old.st_mode)) /* not a file */ | |
3556 { | |
3557 if (S_ISDIR(st_old.st_mode)) | |
3558 { | |
3559 errnum = (char_u *)"E502: "; | |
3560 errmsg = (char_u *)_("is a directory"); | |
3561 goto fail; | |
3562 } | |
3563 if (mch_nodetype(fname) != NODE_WRITABLE) | |
3564 { | |
3565 errnum = (char_u *)"E503: "; | |
3566 errmsg = (char_u *)_("is not a file or writable device"); | |
3567 goto fail; | |
3568 } | |
3569 /* It's a device of some kind (or a fifo) which we can write to | |
3570 * but for which we can't make a backup. */ | |
3571 device = TRUE; | |
3572 newfile = TRUE; | |
3573 perm = -1; | |
3574 } | |
3575 } | |
3576 #else /* !UNIX */ | |
3577 /* | |
3578 * Check for a writable device name. | |
3579 */ | |
3580 c = mch_nodetype(fname); | |
3581 if (c == NODE_OTHER) | |
3582 { | |
3583 errnum = (char_u *)"E503: "; | |
3584 errmsg = (char_u *)_("is not a file or writable device"); | |
3585 goto fail; | |
3586 } | |
3587 if (c == NODE_WRITABLE) | |
3588 { | |
1004 | 3589 # if defined(MSDOS) || defined(MSWIN) || defined(OS2) |
3590 /* MS-Windows allows opening a device, but we will probably get stuck | |
3591 * trying to write to it. */ | |
3592 if (!p_odev) | |
3593 { | |
3594 errnum = (char_u *)"E796: "; | |
3595 errmsg = (char_u *)_("writing to device disabled with 'opendevice' option"); | |
3596 goto fail; | |
3597 } | |
3598 # endif | |
7 | 3599 device = TRUE; |
3600 newfile = TRUE; | |
3601 perm = -1; | |
3602 } | |
3603 else | |
3604 { | |
3605 perm = mch_getperm(fname); | |
3606 if (perm < 0) | |
3607 newfile = TRUE; | |
3608 else if (mch_isdir(fname)) | |
3609 { | |
3610 errnum = (char_u *)"E502: "; | |
3611 errmsg = (char_u *)_("is a directory"); | |
3612 goto fail; | |
3613 } | |
3614 if (overwriting) | |
3615 (void)mch_stat((char *)fname, &st_old); | |
3616 } | |
3617 #endif /* !UNIX */ | |
3618 | |
3619 if (!device && !newfile) | |
3620 { | |
3621 /* | |
3622 * Check if the file is really writable (when renaming the file to | |
3623 * make a backup we won't discover it later). | |
3624 */ | |
1303 | 3625 file_readonly = check_file_readonly(fname, (int)perm); |
3626 | |
7 | 3627 if (!forceit && file_readonly) |
3628 { | |
3629 if (vim_strchr(p_cpo, CPO_FWRITE) != NULL) | |
3630 { | |
3631 errnum = (char_u *)"E504: "; | |
3632 errmsg = (char_u *)_(err_readonly); | |
3633 } | |
3634 else | |
3635 { | |
3636 errnum = (char_u *)"E505: "; | |
3637 errmsg = (char_u *)_("is read-only (add ! to override)"); | |
3638 } | |
3639 goto fail; | |
3640 } | |
3641 | |
3642 /* | |
3643 * Check if the timestamp hasn't changed since reading the file. | |
3644 */ | |
3645 if (overwriting) | |
3646 { | |
3647 retval = check_mtime(buf, &st_old); | |
3648 if (retval == FAIL) | |
3649 goto fail; | |
3650 } | |
3651 } | |
3652 | |
3653 #ifdef HAVE_ACL | |
3654 /* | |
3655 * For systems that support ACL: get the ACL from the original file. | |
3656 */ | |
3657 if (!newfile) | |
3658 acl = mch_get_acl(fname); | |
3659 #endif | |
3660 | |
3661 /* | |
3662 * If 'backupskip' is not empty, don't make a backup for some files. | |
3663 */ | |
3664 dobackup = (p_wb || p_bk || *p_pm != NUL); | |
3665 #ifdef FEAT_WILDIGN | |
3666 if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname)) | |
3667 dobackup = FALSE; | |
3668 #endif | |
3669 | |
3670 /* | |
3671 * Save the value of got_int and reset it. We don't want a previous | |
3672 * interruption cancel writing, only hitting CTRL-C while writing should | |
3673 * abort it. | |
3674 */ | |
3675 prev_got_int = got_int; | |
3676 got_int = FALSE; | |
3677 | |
3678 /* Mark the buffer as 'being saved' to prevent changed buffer warnings */ | |
3679 buf->b_saving = TRUE; | |
3680 | |
3681 /* | |
3682 * If we are not appending or filtering, the file exists, and the | |
3683 * 'writebackup', 'backup' or 'patchmode' option is set, need a backup. | |
3684 * When 'patchmode' is set also make a backup when appending. | |
3685 * | |
3686 * Do not make any backup, if 'writebackup' and 'backup' are both switched | |
3687 * off. This helps when editing large files on almost-full disks. | |
3688 */ | |
3689 if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup) | |
3690 { | |
3691 #if defined(UNIX) || defined(WIN32) | |
3692 struct stat st; | |
3693 #endif | |
3694 | |
3695 if ((bkc_flags & BKC_YES) || append) /* "yes" */ | |
3696 backup_copy = TRUE; | |
3697 #if defined(UNIX) || defined(WIN32) | |
3698 else if ((bkc_flags & BKC_AUTO)) /* "auto" */ | |
3699 { | |
3700 int i; | |
3701 | |
3702 # ifdef UNIX | |
3703 /* | |
3704 * Don't rename the file when: | |
3705 * - it's a hard link | |
3706 * - it's a symbolic link | |
3707 * - we don't have write permission in the directory | |
3708 * - we can't set the owner/group of the new file | |
3709 */ | |
3710 if (st_old.st_nlink > 1 | |
3711 || mch_lstat((char *)fname, &st) < 0 | |
3712 || st.st_dev != st_old.st_dev | |
557 | 3713 || st.st_ino != st_old.st_ino |
3714 # ifndef HAVE_FCHOWN | |
3715 || st.st_uid != st_old.st_uid | |
3716 || st.st_gid != st_old.st_gid | |
3717 # endif | |
3718 ) | |
7 | 3719 backup_copy = TRUE; |
3720 else | |
696 | 3721 # else |
3722 # ifdef WIN32 | |
3723 /* On NTFS file systems hard links are possible. */ | |
3724 if (mch_is_linked(fname)) | |
3725 backup_copy = TRUE; | |
3726 else | |
3727 # endif | |
7 | 3728 # endif |
3729 { | |
3730 /* | |
3731 * Check if we can create a file and set the owner/group to | |
3732 * the ones from the original file. | |
3733 * First find a file name that doesn't exist yet (use some | |
3734 * arbitrary numbers). | |
3735 */ | |
3736 STRCPY(IObuff, fname); | |
3737 for (i = 4913; ; i += 123) | |
3738 { | |
3739 sprintf((char *)gettail(IObuff), "%d", i); | |
557 | 3740 if (mch_lstat((char *)IObuff, &st) < 0) |
7 | 3741 break; |
3742 } | |
557 | 3743 fd = mch_open((char *)IObuff, |
3744 O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm); | |
7 | 3745 if (fd < 0) /* can't write in directory */ |
3746 backup_copy = TRUE; | |
3747 else | |
3748 { | |
3749 # ifdef UNIX | |
557 | 3750 # ifdef HAVE_FCHOWN |
1757 | 3751 ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
557 | 3752 # endif |
7 | 3753 if (mch_stat((char *)IObuff, &st) < 0 |
3754 || st.st_uid != st_old.st_uid | |
3755 || st.st_gid != st_old.st_gid | |
1877 | 3756 || (long)st.st_mode != perm) |
7 | 3757 backup_copy = TRUE; |
3758 # endif | |
567 | 3759 /* Close the file before removing it, on MS-Windows we |
3760 * can't delete an open file. */ | |
3761 close(fd); | |
7 | 3762 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
|
3763 # 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
|
3764 /* 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
|
3765 * 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
|
3766 * second. */ |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3767 { |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3768 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
|
3769 |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3770 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
|
3771 { |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3772 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
|
3773 break; |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3774 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
|
3775 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
|
3776 } |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3777 } |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3778 # endif |
7 | 3779 } |
3780 } | |
3781 } | |
3782 | |
3783 # ifdef UNIX | |
3784 /* | |
3785 * Break symlinks and/or hardlinks if we've been asked to. | |
3786 */ | |
3787 if ((bkc_flags & BKC_BREAKSYMLINK) || (bkc_flags & BKC_BREAKHARDLINK)) | |
3788 { | |
3789 int lstat_res; | |
3790 | |
3791 lstat_res = mch_lstat((char *)fname, &st); | |
3792 | |
3793 /* Symlinks. */ | |
3794 if ((bkc_flags & BKC_BREAKSYMLINK) | |
3795 && lstat_res == 0 | |
3796 && st.st_ino != st_old.st_ino) | |
3797 backup_copy = FALSE; | |
3798 | |
3799 /* Hardlinks. */ | |
3800 if ((bkc_flags & BKC_BREAKHARDLINK) | |
3801 && st_old.st_nlink > 1 | |
3802 && (lstat_res != 0 || st.st_ino == st_old.st_ino)) | |
3803 backup_copy = FALSE; | |
3804 } | |
3805 #endif | |
3806 | |
3807 #endif | |
3808 | |
3809 /* make sure we have a valid backup extension to use */ | |
3810 if (*p_bex == NUL) | |
3811 backup_ext = (char_u *)".bak"; | |
3812 else | |
3813 backup_ext = p_bex; | |
3814 | |
3815 if (backup_copy | |
3816 && (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) >= 0) | |
3817 { | |
3818 int bfd; | |
3819 char_u *copybuf, *wp; | |
3820 int some_error = FALSE; | |
3821 struct stat st_new; | |
3822 char_u *dirp; | |
3823 char_u *rootname; | |
344 | 3824 #if defined(UNIX) && !defined(SHORT_FNAME) |
7 | 3825 int did_set_shortname; |
3826 #endif | |
3827 | |
3828 copybuf = alloc(BUFSIZE + 1); | |
3829 if (copybuf == NULL) | |
3830 { | |
3831 some_error = TRUE; /* out of memory */ | |
3832 goto nobackup; | |
3833 } | |
3834 | |
3835 /* | |
3836 * Try to make the backup in each directory in the 'bdir' option. | |
3837 * | |
3838 * Unix semantics has it, that we may have a writable file, | |
3839 * that cannot be recreated with a simple open(..., O_CREAT, ) e.g: | |
3840 * - the directory is not writable, | |
3841 * - the file may be a symbolic link, | |
3842 * - the file may belong to another user/group, etc. | |
3843 * | |
3844 * For these reasons, the existing writable file must be truncated | |
3845 * and reused. Creation of a backup COPY will be attempted. | |
3846 */ | |
3847 dirp = p_bdir; | |
3848 while (*dirp) | |
3849 { | |
3850 #ifdef UNIX | |
3851 st_new.st_ino = 0; | |
3852 st_new.st_dev = 0; | |
3853 st_new.st_gid = 0; | |
3854 #endif | |
3855 | |
3856 /* | |
3857 * Isolate one directory name, using an entry in 'bdir'. | |
3858 */ | |
3859 (void)copy_option_part(&dirp, copybuf, BUFSIZE, ","); | |
3860 rootname = get_file_in_dir(fname, copybuf); | |
3861 if (rootname == NULL) | |
3862 { | |
3863 some_error = TRUE; /* out of memory */ | |
3864 goto nobackup; | |
3865 } | |
3866 | |
344 | 3867 #if defined(UNIX) && !defined(SHORT_FNAME) |
7 | 3868 did_set_shortname = FALSE; |
3869 #endif | |
3870 | |
3871 /* | |
3872 * May try twice if 'shortname' not set. | |
3873 */ | |
3874 for (;;) | |
3875 { | |
3876 /* | |
3877 * Make backup file name. | |
3878 */ | |
3879 backup = buf_modname( | |
3880 #ifdef SHORT_FNAME | |
3881 TRUE, | |
3882 #else | |
3883 (buf->b_p_sn || buf->b_shortname), | |
3884 #endif | |
3885 rootname, backup_ext, FALSE); | |
3886 if (backup == NULL) | |
3887 { | |
3888 vim_free(rootname); | |
3889 some_error = TRUE; /* out of memory */ | |
3890 goto nobackup; | |
3891 } | |
3892 | |
3893 /* | |
3894 * Check if backup file already exists. | |
3895 */ | |
3896 if (mch_stat((char *)backup, &st_new) >= 0) | |
3897 { | |
3898 #ifdef UNIX | |
3899 /* | |
3900 * Check if backup file is same as original file. | |
3901 * May happen when modname() gave the same file back. | |
3902 * E.g. silly link, or file name-length reached. | |
3903 * If we don't check here, we either ruin the file | |
3904 * when copying or erase it after writing. jw. | |
3905 */ | |
3906 if (st_new.st_dev == st_old.st_dev | |
3907 && st_new.st_ino == st_old.st_ino) | |
3908 { | |
3909 vim_free(backup); | |
3910 backup = NULL; /* no backup file to delete */ | |
3911 # ifndef SHORT_FNAME | |
3912 /* | |
3913 * may try again with 'shortname' set | |
3914 */ | |
3915 if (!(buf->b_shortname || buf->b_p_sn)) | |
3916 { | |
3917 buf->b_shortname = TRUE; | |
3918 did_set_shortname = TRUE; | |
3919 continue; | |
3920 } | |
3921 /* setting shortname didn't help */ | |
3922 if (did_set_shortname) | |
3923 buf->b_shortname = FALSE; | |
3924 # endif | |
3925 break; | |
3926 } | |
3927 #endif | |
3928 | |
3929 /* | |
3930 * If we are not going to keep the backup file, don't | |
3931 * delete an existing one, try to use another name. | |
3932 * Change one character, just before the extension. | |
3933 */ | |
3934 if (!p_bk) | |
3935 { | |
3936 wp = backup + STRLEN(backup) - 1 | |
3937 - STRLEN(backup_ext); | |
3938 if (wp < backup) /* empty file name ??? */ | |
3939 wp = backup; | |
3940 *wp = 'z'; | |
3941 while (*wp > 'a' | |
3942 && mch_stat((char *)backup, &st_new) >= 0) | |
3943 --*wp; | |
3944 /* They all exist??? Must be something wrong. */ | |
3945 if (*wp == 'a') | |
3946 { | |
3947 vim_free(backup); | |
3948 backup = NULL; | |
3949 } | |
3950 } | |
3951 } | |
3952 break; | |
3953 } | |
3954 vim_free(rootname); | |
3955 | |
3956 /* | |
3957 * Try to create the backup file | |
3958 */ | |
3959 if (backup != NULL) | |
3960 { | |
3961 /* remove old backup, if present */ | |
3962 mch_remove(backup); | |
3963 /* Open with O_EXCL to avoid the file being created while | |
3964 * we were sleeping (symlink hacker attack?) */ | |
3965 bfd = mch_open((char *)backup, | |
557 | 3966 O_WRONLY|O_CREAT|O_EXTRA|O_EXCL|O_NOFOLLOW, |
3967 perm & 0777); | |
7 | 3968 if (bfd < 0) |
3969 { | |
3970 vim_free(backup); | |
3971 backup = NULL; | |
3972 } | |
3973 else | |
3974 { | |
3975 /* set file protection same as original file, but | |
3976 * strip s-bit */ | |
3977 (void)mch_setperm(backup, perm & 0777); | |
3978 | |
3979 #ifdef UNIX | |
3980 /* | |
3981 * Try to set the group of the backup same as the | |
3982 * original file. If this fails, set the protection | |
3983 * bits for the group same as the protection bits for | |
3984 * others. | |
3985 */ | |
557 | 3986 if (st_new.st_gid != st_old.st_gid |
7 | 3987 # ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */ |
557 | 3988 && fchown(bfd, (uid_t)-1, st_old.st_gid) != 0 |
7 | 3989 # endif |
3990 ) | |
3991 mch_setperm(backup, | |
3992 (perm & 0707) | ((perm & 07) << 3)); | |
1583 | 3993 # ifdef HAVE_SELINUX |
3994 mch_copy_sec(fname, backup); | |
3995 # endif | |
7 | 3996 #endif |
3997 | |
3998 /* | |
3999 * copy the file. | |
4000 */ | |
4001 write_info.bw_fd = bfd; | |
4002 write_info.bw_buf = copybuf; | |
4003 #ifdef HAS_BW_FLAGS | |
4004 write_info.bw_flags = FIO_NOCONVERT; | |
4005 #endif | |
2664 | 4006 while ((write_info.bw_len = read_eintr(fd, copybuf, |
7 | 4007 BUFSIZE)) > 0) |
4008 { | |
4009 if (buf_write_bytes(&write_info) == FAIL) | |
4010 { | |
4011 errmsg = (char_u *)_("E506: Can't write to backup file (add ! to override)"); | |
4012 break; | |
4013 } | |
4014 ui_breakcheck(); | |
4015 if (got_int) | |
4016 { | |
4017 errmsg = (char_u *)_(e_interr); | |
4018 break; | |
4019 } | |
4020 } | |
4021 | |
4022 if (close(bfd) < 0 && errmsg == NULL) | |
4023 errmsg = (char_u *)_("E507: Close error for backup file (add ! to override)"); | |
4024 if (write_info.bw_len < 0) | |
4025 errmsg = (char_u *)_("E508: Can't read file for backup (add ! to override)"); | |
4026 #ifdef UNIX | |
4027 set_file_time(backup, st_old.st_atime, st_old.st_mtime); | |
4028 #endif | |
4029 #ifdef HAVE_ACL | |
4030 mch_set_acl(backup, acl); | |
4031 #endif | |
1583 | 4032 #ifdef HAVE_SELINUX |
4033 mch_copy_sec(fname, backup); | |
4034 #endif | |
7 | 4035 break; |
4036 } | |
4037 } | |
4038 } | |
4039 nobackup: | |
4040 close(fd); /* ignore errors for closing read file */ | |
4041 vim_free(copybuf); | |
4042 | |
4043 if (backup == NULL && errmsg == NULL) | |
4044 errmsg = (char_u *)_("E509: Cannot create backup file (add ! to override)"); | |
4045 /* ignore errors when forceit is TRUE */ | |
4046 if ((some_error || errmsg != NULL) && !forceit) | |
4047 { | |
4048 retval = FAIL; | |
4049 goto fail; | |
4050 } | |
4051 errmsg = NULL; | |
4052 } | |
4053 else | |
4054 { | |
4055 char_u *dirp; | |
4056 char_u *p; | |
4057 char_u *rootname; | |
4058 | |
4059 /* | |
4060 * Make a backup by renaming the original file. | |
4061 */ | |
4062 /* | |
4063 * If 'cpoptions' includes the "W" flag, we don't want to | |
4064 * overwrite a read-only file. But rename may be possible | |
4065 * anyway, thus we need an extra check here. | |
4066 */ | |
4067 if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL) | |
4068 { | |
4069 errnum = (char_u *)"E504: "; | |
4070 errmsg = (char_u *)_(err_readonly); | |
4071 goto fail; | |
4072 } | |
4073 | |
4074 /* | |
4075 * | |
4076 * Form the backup file name - change path/fo.o.h to | |
4077 * path/fo.o.h.bak Try all directories in 'backupdir', first one | |
4078 * that works is used. | |
4079 */ | |
4080 dirp = p_bdir; | |
4081 while (*dirp) | |
4082 { | |
4083 /* | |
4084 * Isolate one directory name and make the backup file name. | |
4085 */ | |
4086 (void)copy_option_part(&dirp, IObuff, IOSIZE, ","); | |
4087 rootname = get_file_in_dir(fname, IObuff); | |
4088 if (rootname == NULL) | |
4089 backup = NULL; | |
4090 else | |
4091 { | |
4092 backup = buf_modname( | |
4093 #ifdef SHORT_FNAME | |
4094 TRUE, | |
4095 #else | |
4096 (buf->b_p_sn || buf->b_shortname), | |
4097 #endif | |
4098 rootname, backup_ext, FALSE); | |
4099 vim_free(rootname); | |
4100 } | |
4101 | |
4102 if (backup != NULL) | |
4103 { | |
4104 /* | |
4105 * If we are not going to keep the backup file, don't | |
4106 * delete an existing one, try to use another name. | |
4107 * Change one character, just before the extension. | |
4108 */ | |
4109 if (!p_bk && mch_getperm(backup) >= 0) | |
4110 { | |
4111 p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext); | |
4112 if (p < backup) /* empty file name ??? */ | |
4113 p = backup; | |
4114 *p = 'z'; | |
4115 while (*p > 'a' && mch_getperm(backup) >= 0) | |
4116 --*p; | |
4117 /* They all exist??? Must be something wrong! */ | |
4118 if (*p == 'a') | |
4119 { | |
4120 vim_free(backup); | |
4121 backup = NULL; | |
4122 } | |
4123 } | |
4124 } | |
4125 if (backup != NULL) | |
4126 { | |
4127 /* | |
531 | 4128 * Delete any existing backup and move the current version |
4129 * to the backup. For safety, we don't remove the backup | |
4130 * until the write has finished successfully. And if the | |
4131 * 'backup' option is set, leave it around. | |
7 | 4132 */ |
4133 /* | |
4134 * If the renaming of the original file to the backup file | |
4135 * works, quit here. | |
4136 */ | |
4137 if (vim_rename(fname, backup) == 0) | |
4138 break; | |
4139 | |
4140 vim_free(backup); /* don't do the rename below */ | |
4141 backup = NULL; | |
4142 } | |
4143 } | |
4144 if (backup == NULL && !forceit) | |
4145 { | |
4146 errmsg = (char_u *)_("E510: Can't make backup file (add ! to override)"); | |
4147 goto fail; | |
4148 } | |
4149 } | |
4150 } | |
4151 | |
4152 #if defined(UNIX) && !defined(ARCHIE) | |
4153 /* When using ":w!" and the file was read-only: make it writable */ | |
4154 if (forceit && perm >= 0 && !(perm & 0200) && st_old.st_uid == getuid() | |
4155 && vim_strchr(p_cpo, CPO_FWRITE) == NULL) | |
4156 { | |
4157 perm |= 0200; | |
4158 (void)mch_setperm(fname, perm); | |
4159 made_writable = TRUE; | |
4160 } | |
4161 #endif | |
4162 | |
819 | 4163 /* When using ":w!" and writing to the current file, 'readonly' makes no |
164 | 4164 * sense, reset it, unless 'Z' appears in 'cpoptions'. */ |
4165 if (forceit && overwriting && vim_strchr(p_cpo, CPO_KEEPRO) == NULL) | |
7 | 4166 { |
4167 buf->b_p_ro = FALSE; | |
4168 #ifdef FEAT_TITLE | |
4169 need_maketitle = TRUE; /* set window title later */ | |
4170 #endif | |
4171 #ifdef FEAT_WINDOWS | |
4172 status_redraw_all(); /* redraw status lines later */ | |
4173 #endif | |
4174 } | |
4175 | |
4176 if (end > buf->b_ml.ml_line_count) | |
4177 end = buf->b_ml.ml_line_count; | |
4178 if (buf->b_ml.ml_flags & ML_EMPTY) | |
4179 start = end + 1; | |
4180 | |
4181 /* | |
4182 * If the original file is being overwritten, there is a small chance that | |
4183 * we crash in the middle of writing. Therefore the file is preserved now. | |
4184 * This makes all block numbers positive so that recovery does not need | |
4185 * the original file. | |
4186 * Don't do this if there is a backup file and we are exiting. | |
4187 */ | |
39 | 4188 if (reset_changed && !newfile && overwriting |
7 | 4189 && !(exiting && backup != NULL)) |
4190 { | |
4191 ml_preserve(buf, FALSE); | |
4192 if (got_int) | |
4193 { | |
4194 errmsg = (char_u *)_(e_interr); | |
4195 goto restore_backup; | |
4196 } | |
4197 } | |
4198 | |
4199 #ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */ | |
4200 /* | |
4201 * Before risking to lose the original file verify if there's | |
4202 * a resource fork to preserve, and if cannot be done warn | |
4203 * the users. This happens when overwriting without backups. | |
4204 */ | |
4205 if (backup == NULL && overwriting && !append) | |
4206 if (mch_has_resource_fork(fname)) | |
4207 { | |
4208 errmsg = (char_u *)_("E460: The resource fork would be lost (add ! to override)"); | |
4209 goto restore_backup; | |
4210 } | |
4211 #endif | |
4212 | |
4213 #ifdef VMS | |
4214 vms_remove_version(fname); /* remove version */ | |
4215 #endif | |
1651 | 4216 /* Default: write the file directly. May write to a temp file for |
7 | 4217 * multi-byte conversion. */ |
4218 wfname = fname; | |
4219 | |
4220 #ifdef FEAT_MBYTE | |
4221 /* Check for forced 'fileencoding' from "++opt=val" argument. */ | |
4222 if (eap != NULL && eap->force_enc != 0) | |
4223 { | |
4224 fenc = eap->cmd + eap->force_enc; | |
4225 fenc = enc_canonize(fenc); | |
4226 fenc_tofree = fenc; | |
4227 } | |
4228 else | |
4229 fenc = buf->b_p_fenc; | |
4230 | |
4231 /* | |
1948 | 4232 * Check if the file needs to be converted. |
7 | 4233 */ |
1948 | 4234 converted = need_conversion(fenc); |
7 | 4235 |
4236 /* | |
4237 * Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done. Or | |
4238 * Latin1 to Unicode conversion. This is handled in buf_write_bytes(). | |
4239 * Prepare the flags for it and allocate bw_conv_buf when needed. | |
4240 */ | |
4241 if (converted && (enc_utf8 || STRCMP(p_enc, "latin1") == 0)) | |
4242 { | |
4243 wb_flags = get_fio_flags(fenc); | |
4244 if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8)) | |
4245 { | |
4246 /* Need to allocate a buffer to translate into. */ | |
4247 if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8)) | |
4248 write_info.bw_conv_buflen = bufsize * 2; | |
4249 else /* FIO_UCS4 */ | |
4250 write_info.bw_conv_buflen = bufsize * 4; | |
4251 write_info.bw_conv_buf | |
4252 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4253 if (write_info.bw_conv_buf == NULL) | |
4254 end = 0; | |
4255 } | |
4256 } | |
4257 | |
4258 # ifdef WIN3264 | |
4259 if (converted && wb_flags == 0 && (wb_flags = get_win_fio_flags(fenc)) != 0) | |
4260 { | |
4261 /* Convert UTF-8 -> UCS-2 and UCS-2 -> DBCS. Worst-case * 4: */ | |
4262 write_info.bw_conv_buflen = bufsize * 4; | |
4263 write_info.bw_conv_buf | |
4264 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4265 if (write_info.bw_conv_buf == NULL) | |
4266 end = 0; | |
4267 } | |
4268 # endif | |
4269 | |
4270 # ifdef MACOS_X | |
4271 if (converted && wb_flags == 0 && (wb_flags = get_mac_fio_flags(fenc)) != 0) | |
4272 { | |
4273 write_info.bw_conv_buflen = bufsize * 3; | |
4274 write_info.bw_conv_buf | |
4275 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4276 if (write_info.bw_conv_buf == NULL) | |
4277 end = 0; | |
4278 } | |
4279 # endif | |
4280 | |
4281 # if defined(FEAT_EVAL) || defined(USE_ICONV) | |
4282 if (converted && wb_flags == 0) | |
4283 { | |
4284 # ifdef USE_ICONV | |
4285 /* | |
4286 * Use iconv() conversion when conversion is needed and it's not done | |
4287 * internally. | |
4288 */ | |
4289 write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc, | |
4290 enc_utf8 ? (char_u *)"utf-8" : p_enc); | |
4291 if (write_info.bw_iconv_fd != (iconv_t)-1) | |
4292 { | |
4293 /* We're going to use iconv(), allocate a buffer to convert in. */ | |
4294 write_info.bw_conv_buflen = bufsize * ICONV_MULT; | |
4295 write_info.bw_conv_buf | |
4296 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4297 if (write_info.bw_conv_buf == NULL) | |
4298 end = 0; | |
4299 write_info.bw_first = TRUE; | |
4300 } | |
4301 # ifdef FEAT_EVAL | |
4302 else | |
4303 # endif | |
4304 # endif | |
4305 | |
4306 # ifdef FEAT_EVAL | |
4307 /* | |
4308 * When the file needs to be converted with 'charconvert' after | |
4309 * writing, write to a temp file instead and let the conversion | |
4310 * overwrite the original file. | |
4311 */ | |
4312 if (*p_ccv != NUL) | |
4313 { | |
4314 wfname = vim_tempname('w'); | |
4315 if (wfname == NULL) /* Can't write without a tempfile! */ | |
4316 { | |
4317 errmsg = (char_u *)_("E214: Can't find temp file for writing"); | |
4318 goto restore_backup; | |
4319 } | |
4320 } | |
4321 # endif | |
4322 } | |
4323 # endif | |
4324 if (converted && wb_flags == 0 | |
4325 # ifdef USE_ICONV | |
4326 && write_info.bw_iconv_fd == (iconv_t)-1 | |
4327 # endif | |
4328 # ifdef FEAT_EVAL | |
4329 && wfname == fname | |
4330 # endif | |
4331 ) | |
4332 { | |
4333 if (!forceit) | |
4334 { | |
4335 errmsg = (char_u *)_("E213: Cannot convert (add ! to write without conversion)"); | |
4336 goto restore_backup; | |
4337 } | |
4338 notconverted = TRUE; | |
4339 } | |
4340 #endif | |
4341 | |
4342 /* | |
4343 * Open the file "wfname" for writing. | |
4344 * We may try to open the file twice: If we can't write to the | |
4345 * file and forceit is TRUE we delete the existing file and try to create | |
4346 * a new one. If this still fails we may have lost the original file! | |
4347 * (this may happen when the user reached his quotum for number of files). | |
4348 * Appending will fail if the file does not exist and forceit is FALSE. | |
4349 */ | |
4350 while ((fd = mch_open((char *)wfname, O_WRONLY | O_EXTRA | (append | |
4351 ? (forceit ? (O_APPEND | O_CREAT) : O_APPEND) | |
4352 : (O_CREAT | O_TRUNC)) | |
216 | 4353 , perm < 0 ? 0666 : (perm & 0777))) < 0) |
7 | 4354 { |
4355 /* | |
4356 * A forced write will try to create a new file if the old one is | |
4357 * still readonly. This may also happen when the directory is | |
4358 * read-only. In that case the mch_remove() will fail. | |
4359 */ | |
4360 if (errmsg == NULL) | |
4361 { | |
4362 #ifdef UNIX | |
4363 struct stat st; | |
4364 | |
4365 /* Don't delete the file when it's a hard or symbolic link. */ | |
4366 if ((!newfile && st_old.st_nlink > 1) | |
4367 || (mch_lstat((char *)fname, &st) == 0 | |
4368 && (st.st_dev != st_old.st_dev | |
4369 || st.st_ino != st_old.st_ino))) | |
4370 errmsg = (char_u *)_("E166: Can't open linked file for writing"); | |
4371 else | |
4372 #endif | |
4373 { | |
4374 errmsg = (char_u *)_("E212: Can't open file for writing"); | |
4375 if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL | |
4376 && perm >= 0) | |
4377 { | |
4378 #ifdef UNIX | |
4379 /* we write to the file, thus it should be marked | |
4380 writable after all */ | |
4381 if (!(perm & 0200)) | |
4382 made_writable = TRUE; | |
4383 perm |= 0200; | |
4384 if (st_old.st_uid != getuid() || st_old.st_gid != getgid()) | |
4385 perm &= 0777; | |
4386 #endif | |
4387 if (!append) /* don't remove when appending */ | |
4388 mch_remove(wfname); | |
4389 continue; | |
4390 } | |
4391 } | |
4392 } | |
4393 | |
4394 restore_backup: | |
4395 { | |
4396 struct stat st; | |
4397 | |
4398 /* | |
4399 * If we failed to open the file, we don't need a backup. Throw it | |
4400 * away. If we moved or removed the original file try to put the | |
4401 * backup in its place. | |
4402 */ | |
4403 if (backup != NULL && wfname == fname) | |
4404 { | |
4405 if (backup_copy) | |
4406 { | |
4407 /* | |
4408 * There is a small chance that we removed the original, | |
4409 * try to move the copy in its place. | |
4410 * This may not work if the vim_rename() fails. | |
4411 * In that case we leave the copy around. | |
4412 */ | |
4413 /* If file does not exist, put the copy in its place */ | |
4414 if (mch_stat((char *)fname, &st) < 0) | |
4415 vim_rename(backup, fname); | |
4416 /* if original file does exist throw away the copy */ | |
4417 if (mch_stat((char *)fname, &st) >= 0) | |
4418 mch_remove(backup); | |
4419 } | |
4420 else | |
4421 { | |
4422 /* try to put the original file back */ | |
4423 vim_rename(backup, fname); | |
4424 } | |
4425 } | |
4426 | |
4427 /* if original file no longer exists give an extra warning */ | |
4428 if (!newfile && mch_stat((char *)fname, &st) < 0) | |
4429 end = 0; | |
4430 } | |
4431 | |
4432 #ifdef FEAT_MBYTE | |
4433 if (wfname != fname) | |
4434 vim_free(wfname); | |
4435 #endif | |
4436 goto fail; | |
4437 } | |
4438 errmsg = NULL; | |
4439 | |
4440 #if defined(MACOS_CLASSIC) || defined(WIN3264) | |
4441 /* TODO: Is it need for MACOS_X? (Dany) */ | |
4442 /* | |
4443 * On macintosh copy the original files attributes (i.e. the backup) | |
1201 | 4444 * This is done in order to preserve the resource fork and the |
4445 * Finder attribute (label, comments, custom icons, file creator) | |
7 | 4446 */ |
4447 if (backup != NULL && overwriting && !append) | |
4448 { | |
4449 if (backup_copy) | |
4450 (void)mch_copy_file_attribute(wfname, backup); | |
4451 else | |
4452 (void)mch_copy_file_attribute(backup, wfname); | |
4453 } | |
4454 | |
4455 if (!overwriting && !append) | |
4456 { | |
4457 if (buf->b_ffname != NULL) | |
4458 (void)mch_copy_file_attribute(buf->b_ffname, wfname); | |
1201 | 4459 /* Should copy resource fork */ |
7 | 4460 } |
4461 #endif | |
4462 | |
4463 write_info.bw_fd = fd; | |
4464 | |
4465 #ifdef FEAT_CRYPT | |
2267 | 4466 if (*buf->b_p_key != NUL && !filtering) |
7 | 4467 { |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4468 char_u *header; |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4469 int header_len; |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4470 |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4471 header = prepare_crypt_write(buf, &header_len); |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4472 if (header == NULL) |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4473 end = 0; |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
4474 else |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
4475 { |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4476 /* Write magic number, so that Vim knows that this file is |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4477 * encrypted when reading it again. This also undergoes utf-8 to |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4478 * ucs-2/4 conversion when needed. */ |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4479 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
|
4480 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
|
4481 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
|
4482 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
|
4483 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
|
4484 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
|
4485 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
|
4486 } |
7 | 4487 } |
4488 #endif | |
4489 | |
4490 write_info.bw_buf = buffer; | |
4491 nchars = 0; | |
4492 | |
4493 /* use "++bin", "++nobin" or 'binary' */ | |
4494 if (eap != NULL && eap->force_bin != 0) | |
4495 write_bin = (eap->force_bin == FORCE_BIN); | |
4496 else | |
4497 write_bin = buf->b_p_bin; | |
4498 | |
4499 #ifdef FEAT_MBYTE | |
4500 /* | |
4501 * The BOM is written just after the encryption magic number. | |
24 | 4502 * Skip it when appending and the file already existed, the BOM only makes |
4503 * sense at the start of the file. | |
7 | 4504 */ |
24 | 4505 if (buf->b_p_bomb && !write_bin && (!append || perm < 0)) |
7 | 4506 { |
4507 write_info.bw_len = make_bom(buffer, fenc); | |
4508 if (write_info.bw_len > 0) | |
4509 { | |
4510 /* don't convert, do encryption */ | |
4511 write_info.bw_flags = FIO_NOCONVERT | wb_flags; | |
4512 if (buf_write_bytes(&write_info) == FAIL) | |
4513 end = 0; | |
4514 else | |
4515 nchars += write_info.bw_len; | |
4516 } | |
4517 } | |
1947 | 4518 write_info.bw_start_lnum = start; |
7 | 4519 #endif |
4520 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4521 #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
|
4522 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
|
4523 && !filtering && reset_changed); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4524 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
|
4525 /* 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
|
4526 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
|
4527 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4528 |
7 | 4529 write_info.bw_len = bufsize; |
4530 #ifdef HAS_BW_FLAGS | |
4531 write_info.bw_flags = wb_flags; | |
4532 #endif | |
4533 fileformat = get_fileformat_force(buf, eap); | |
4534 s = buffer; | |
4535 len = 0; | |
4536 for (lnum = start; lnum <= end; ++lnum) | |
4537 { | |
4538 /* | |
4539 * The next while loop is done once for each character written. | |
4540 * Keep it fast! | |
4541 */ | |
4542 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
|
4543 #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
|
4544 if (write_undo_file) |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
4545 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
|
4546 #endif |
7 | 4547 while ((c = *++ptr) != NUL) |
4548 { | |
4549 if (c == NL) | |
4550 *s = NUL; /* replace newlines with NULs */ | |
4551 else if (c == CAR && fileformat == EOL_MAC) | |
4552 *s = NL; /* Mac: replace CRs with NLs */ | |
4553 else | |
4554 *s = c; | |
4555 ++s; | |
4556 if (++len != bufsize) | |
4557 continue; | |
4558 if (buf_write_bytes(&write_info) == FAIL) | |
4559 { | |
4560 end = 0; /* write error: break loop */ | |
4561 break; | |
4562 } | |
4563 nchars += bufsize; | |
4564 s = buffer; | |
4565 len = 0; | |
1947 | 4566 #ifdef FEAT_MBYTE |
4567 write_info.bw_start_lnum = lnum; | |
4568 #endif | |
7 | 4569 } |
4570 /* write failed or last line has no EOL: stop here */ | |
4571 if (end == 0 | |
4572 || (lnum == end | |
4573 && write_bin | |
2707 | 4574 && (lnum == buf->b_no_eol_lnum |
7 | 4575 || (lnum == buf->b_ml.ml_line_count && !buf->b_p_eol)))) |
4576 { | |
4577 ++lnum; /* written the line, count it */ | |
4578 no_eol = TRUE; | |
4579 break; | |
4580 } | |
4581 if (fileformat == EOL_UNIX) | |
4582 *s++ = NL; | |
4583 else | |
4584 { | |
4585 *s++ = CAR; /* EOL_MAC or EOL_DOS: write CR */ | |
4586 if (fileformat == EOL_DOS) /* write CR-NL */ | |
4587 { | |
4588 if (++len == bufsize) | |
4589 { | |
4590 if (buf_write_bytes(&write_info) == FAIL) | |
4591 { | |
4592 end = 0; /* write error: break loop */ | |
4593 break; | |
4594 } | |
4595 nchars += bufsize; | |
4596 s = buffer; | |
4597 len = 0; | |
4598 } | |
4599 *s++ = NL; | |
4600 } | |
4601 } | |
4602 if (++len == bufsize && end) | |
4603 { | |
4604 if (buf_write_bytes(&write_info) == FAIL) | |
4605 { | |
4606 end = 0; /* write error: break loop */ | |
4607 break; | |
4608 } | |
4609 nchars += bufsize; | |
4610 s = buffer; | |
4611 len = 0; | |
4612 | |
4613 ui_breakcheck(); | |
4614 if (got_int) | |
4615 { | |
4616 end = 0; /* Interrupted, break loop */ | |
4617 break; | |
4618 } | |
4619 } | |
4620 #ifdef VMS | |
4621 /* | |
4622 * On VMS there is a problem: newlines get added when writing blocks | |
4623 * at a time. Fix it by writing a line at a time. | |
4624 * This is much slower! | |
22 | 4625 * Explanation: VAX/DECC RTL insists that records in some RMS |
4626 * structures end with a newline (carriage return) character, and if | |
4627 * they don't it adds one. | |
4628 * With other RMS structures it works perfect without this fix. | |
7 | 4629 */ |
1431 | 4630 if (buf->b_fab_rfm == FAB$C_VFC |
4631 || ((buf->b_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0)) | |
22 | 4632 { |
4633 int b2write; | |
4634 | |
4635 buf->b_fab_mrs = (buf->b_fab_mrs == 0 | |
4636 ? MIN(4096, bufsize) | |
4637 : MIN(buf->b_fab_mrs, bufsize)); | |
4638 | |
4639 b2write = len; | |
4640 while (b2write > 0) | |
7 | 4641 { |
22 | 4642 write_info.bw_len = MIN(b2write, buf->b_fab_mrs); |
4643 if (buf_write_bytes(&write_info) == FAIL) | |
4644 { | |
4645 end = 0; | |
4646 break; | |
4647 } | |
4648 b2write -= MIN(b2write, buf->b_fab_mrs); | |
7 | 4649 } |
4650 write_info.bw_len = bufsize; | |
4651 nchars += len; | |
4652 s = buffer; | |
4653 len = 0; | |
4654 } | |
4655 #endif | |
4656 } | |
4657 if (len > 0 && end > 0) | |
4658 { | |
4659 write_info.bw_len = len; | |
4660 if (buf_write_bytes(&write_info) == FAIL) | |
4661 end = 0; /* write error */ | |
4662 nchars += len; | |
4663 } | |
4664 | |
4665 #if defined(UNIX) && defined(HAVE_FSYNC) | |
4666 /* On many journalling file systems there is a bug that causes both the | |
4667 * original and the backup file to be lost when halting the system right | |
4668 * after writing the file. That's because only the meta-data is | |
4669 * journalled. Syncing the file slows down the system, but assures it has | |
10 | 4670 * been written to disk and we don't lose it. |
4671 * For a device do try the fsync() but don't complain if it does not work | |
36 | 4672 * (could be a pipe). |
4673 * If the 'fsync' option is FALSE, don't fsync(). Useful for laptops. */ | |
4674 if (p_fs && fsync(fd) != 0 && !device) | |
7 | 4675 { |
4676 errmsg = (char_u *)_("E667: Fsync failed"); | |
4677 end = 0; | |
4678 } | |
4679 #endif | |
4680 | |
1583 | 4681 #ifdef HAVE_SELINUX |
4682 /* Probably need to set the security context. */ | |
4683 if (!backup_copy) | |
4684 mch_copy_sec(backup, wfname); | |
4685 #endif | |
4686 | |
557 | 4687 #ifdef UNIX |
4688 /* When creating a new file, set its owner/group to that of the original | |
4689 * file. Get the new device and inode number. */ | |
4690 if (backup != NULL && !backup_copy) | |
4691 { | |
4692 # ifdef HAVE_FCHOWN | |
4693 struct stat st; | |
4694 | |
4695 /* don't change the owner when it's already OK, some systems remove | |
4696 * permission or ACL stuff */ | |
4697 if (mch_stat((char *)wfname, &st) < 0 | |
4698 || st.st_uid != st_old.st_uid | |
4699 || st.st_gid != st_old.st_gid) | |
4700 { | |
1757 | 4701 ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
557 | 4702 if (perm >= 0) /* set permission again, may have changed */ |
4703 (void)mch_setperm(wfname, perm); | |
4704 } | |
4705 # endif | |
4706 buf_setino(buf); | |
4707 } | |
1873 | 4708 else if (!buf->b_dev_valid) |
617 | 4709 /* Set the inode when creating a new file. */ |
4710 buf_setino(buf); | |
557 | 4711 #endif |
4712 | |
7 | 4713 if (close(fd) != 0) |
4714 { | |
4715 errmsg = (char_u *)_("E512: Close failed"); | |
4716 end = 0; | |
4717 } | |
4718 | |
4719 #ifdef UNIX | |
4720 if (made_writable) | |
4721 perm &= ~0200; /* reset 'w' bit for security reasons */ | |
4722 #endif | |
4723 if (perm >= 0) /* set perm. of new file same as old file */ | |
4724 (void)mch_setperm(wfname, perm); | |
4725 #ifdef HAVE_ACL | |
4726 /* Probably need to set the ACL before changing the user (can't set the | |
4727 * ACL on a file the user doesn't own). */ | |
4728 if (!backup_copy) | |
4729 mch_set_acl(wfname, acl); | |
4730 #endif | |
2267 | 4731 #ifdef FEAT_CRYPT |
2717 | 4732 crypt_method_used = use_crypt_method; |
2267 | 4733 if (wb_flags & FIO_ENCRYPTED) |
4734 crypt_pop_state(); | |
4735 #endif | |
7 | 4736 |
4737 | |
4738 #if defined(FEAT_MBYTE) && defined(FEAT_EVAL) | |
4739 if (wfname != fname) | |
4740 { | |
4741 /* | |
4742 * The file was written to a temp file, now it needs to be converted | |
4743 * with 'charconvert' to (overwrite) the output file. | |
4744 */ | |
4745 if (end != 0) | |
4746 { | |
4747 if (eval_charconvert(enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc, | |
4748 wfname, fname) == FAIL) | |
4749 { | |
4750 write_info.bw_conv_error = TRUE; | |
4751 end = 0; | |
4752 } | |
4753 } | |
4754 mch_remove(wfname); | |
4755 vim_free(wfname); | |
4756 } | |
4757 #endif | |
4758 | |
4759 if (end == 0) | |
4760 { | |
4761 if (errmsg == NULL) | |
4762 { | |
4763 #ifdef FEAT_MBYTE | |
4764 if (write_info.bw_conv_error) | |
1947 | 4765 { |
4766 if (write_info.bw_conv_error_lnum == 0) | |
4767 errmsg = (char_u *)_("E513: write error, conversion failed (make 'fenc' empty to override)"); | |
4768 else | |
4769 { | |
4770 errmsg_allocated = TRUE; | |
4771 errmsg = alloc(300); | |
4772 vim_snprintf((char *)errmsg, 300, _("E513: write error, conversion failed in line %ld (make 'fenc' empty to override)"), | |
4773 (long)write_info.bw_conv_error_lnum); | |
4774 } | |
4775 } | |
7 | 4776 else |
4777 #endif | |
4778 if (got_int) | |
4779 errmsg = (char_u *)_(e_interr); | |
4780 else | |
4781 errmsg = (char_u *)_("E514: write error (file system full?)"); | |
4782 } | |
4783 | |
4784 /* | |
4785 * If we have a backup file, try to put it in place of the new file, | |
1698 | 4786 * because the new file is probably corrupt. This avoids losing the |
7 | 4787 * original file when trying to make a backup when writing the file a |
4788 * second time. | |
4789 * When "backup_copy" is set we need to copy the backup over the new | |
4790 * file. Otherwise rename the backup file. | |
4791 * If this is OK, don't give the extra warning message. | |
4792 */ | |
4793 if (backup != NULL) | |
4794 { | |
4795 if (backup_copy) | |
4796 { | |
4797 /* This may take a while, if we were interrupted let the user | |
4798 * know we got the message. */ | |
4799 if (got_int) | |
4800 { | |
4801 MSG(_(e_interr)); | |
4802 out_flush(); | |
4803 } | |
4804 if ((fd = mch_open((char *)backup, O_RDONLY | O_EXTRA, 0)) >= 0) | |
4805 { | |
4806 if ((write_info.bw_fd = mch_open((char *)fname, | |
194 | 4807 O_WRONLY | O_CREAT | O_TRUNC | O_EXTRA, |
4808 perm & 0777)) >= 0) | |
7 | 4809 { |
4810 /* copy the file. */ | |
4811 write_info.bw_buf = smallbuf; | |
4812 #ifdef HAS_BW_FLAGS | |
4813 write_info.bw_flags = FIO_NOCONVERT; | |
4814 #endif | |
2664 | 4815 while ((write_info.bw_len = read_eintr(fd, smallbuf, |
7 | 4816 SMBUFSIZE)) > 0) |
4817 if (buf_write_bytes(&write_info) == FAIL) | |
4818 break; | |
4819 | |
4820 if (close(write_info.bw_fd) >= 0 | |
4821 && write_info.bw_len == 0) | |
4822 end = 1; /* success */ | |
4823 } | |
4824 close(fd); /* ignore errors for closing read file */ | |
4825 } | |
4826 } | |
4827 else | |
4828 { | |
4829 if (vim_rename(backup, fname) == 0) | |
4830 end = 1; | |
4831 } | |
4832 } | |
4833 goto fail; | |
4834 } | |
4835 | |
4836 lnum -= start; /* compute number of written lines */ | |
4837 --no_wait_return; /* may wait for return now */ | |
4838 | |
4839 #if !(defined(UNIX) || defined(VMS)) | |
4840 fname = sfname; /* use shortname now, for the messages */ | |
4841 #endif | |
4842 if (!filtering) | |
4843 { | |
4844 msg_add_fname(buf, fname); /* put fname in IObuff with quotes */ | |
4845 c = FALSE; | |
4846 #ifdef FEAT_MBYTE | |
4847 if (write_info.bw_conv_error) | |
4848 { | |
4849 STRCAT(IObuff, _(" CONVERSION ERROR")); | |
4850 c = TRUE; | |
1947 | 4851 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
|
4852 vim_snprintf_add((char *)IObuff, IOSIZE, _(" in line %ld;"), |
1947 | 4853 (long)write_info.bw_conv_error_lnum); |
7 | 4854 } |
4855 else if (notconverted) | |
4856 { | |
4857 STRCAT(IObuff, _("[NOT converted]")); | |
4858 c = TRUE; | |
4859 } | |
4860 else if (converted) | |
4861 { | |
4862 STRCAT(IObuff, _("[converted]")); | |
4863 c = TRUE; | |
4864 } | |
4865 #endif | |
4866 if (device) | |
4867 { | |
4868 STRCAT(IObuff, _("[Device]")); | |
4869 c = TRUE; | |
4870 } | |
4871 else if (newfile) | |
4872 { | |
4873 STRCAT(IObuff, shortmess(SHM_NEW) ? _("[New]") : _("[New File]")); | |
4874 c = TRUE; | |
4875 } | |
4876 if (no_eol) | |
4877 { | |
4878 msg_add_eol(); | |
4879 c = TRUE; | |
4880 } | |
4881 /* may add [unix/dos/mac] */ | |
4882 if (msg_add_fileformat(fileformat)) | |
4883 c = TRUE; | |
4884 #ifdef FEAT_CRYPT | |
4885 if (wb_flags & FIO_ENCRYPTED) | |
4886 { | |
2717 | 4887 if (crypt_method_used == 1) |
4888 STRCAT(IObuff, _("[blowfish]")); | |
4889 else | |
4890 STRCAT(IObuff, _("[crypted]")); | |
7 | 4891 c = TRUE; |
4892 } | |
4893 #endif | |
4894 msg_add_lines(c, (long)lnum, nchars); /* add line/char count */ | |
4895 if (!shortmess(SHM_WRITE)) | |
4896 { | |
4897 if (append) | |
4898 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [a]") : _(" appended")); | |
4899 else | |
4900 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [w]") : _(" written")); | |
4901 } | |
4902 | |
679 | 4903 set_keep_msg(msg_trunc_attr(IObuff, FALSE, 0), 0); |
7 | 4904 } |
4905 | |
39 | 4906 /* When written everything correctly: reset 'modified'. Unless not |
4907 * writing to the original file and '+' is not in 'cpoptions'. */ | |
589 | 4908 if (reset_changed && whole && !append |
7 | 4909 #ifdef FEAT_MBYTE |
4910 && !write_info.bw_conv_error | |
4911 #endif | |
39 | 4912 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL) |
4913 ) | |
7 | 4914 { |
4915 unchanged(buf, TRUE); | |
4916 u_unchanged(buf); | |
2281
e41433ea71df
Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents:
2280
diff
changeset
|
4917 u_update_save_nr(buf); |
7 | 4918 } |
4919 | |
4920 /* | |
4921 * If written to the current file, update the timestamp of the swap file | |
4922 * and reset the BF_WRITE_MASK flags. Also sets buf->b_mtime. | |
4923 */ | |
4924 if (overwriting) | |
4925 { | |
4926 ml_timestamp(buf); | |
589 | 4927 if (append) |
4928 buf->b_flags &= ~BF_NEW; | |
4929 else | |
4930 buf->b_flags &= ~BF_WRITE_MASK; | |
7 | 4931 } |
4932 | |
4933 /* | |
4934 * If we kept a backup until now, and we are in patch mode, then we make | |
4935 * the backup file our 'original' file. | |
4936 */ | |
4937 if (*p_pm && dobackup) | |
4938 { | |
4939 char *org = (char *)buf_modname( | |
4940 #ifdef SHORT_FNAME | |
4941 TRUE, | |
4942 #else | |
4943 (buf->b_p_sn || buf->b_shortname), | |
4944 #endif | |
4945 fname, p_pm, FALSE); | |
4946 | |
4947 if (backup != NULL) | |
4948 { | |
4949 struct stat st; | |
4950 | |
4951 /* | |
4952 * If the original file does not exist yet | |
4953 * the current backup file becomes the original file | |
4954 */ | |
4955 if (org == NULL) | |
4956 EMSG(_("E205: Patchmode: can't save original file")); | |
4957 else if (mch_stat(org, &st) < 0) | |
4958 { | |
4959 vim_rename(backup, (char_u *)org); | |
4960 vim_free(backup); /* don't delete the file */ | |
4961 backup = NULL; | |
4962 #ifdef UNIX | |
4963 set_file_time((char_u *)org, st_old.st_atime, st_old.st_mtime); | |
4964 #endif | |
4965 } | |
4966 } | |
4967 /* | |
4968 * If there is no backup file, remember that a (new) file was | |
4969 * created. | |
4970 */ | |
4971 else | |
4972 { | |
4973 int empty_fd; | |
4974 | |
4975 if (org == NULL | |
557 | 4976 || (empty_fd = mch_open(org, |
4977 O_CREAT | O_EXTRA | O_EXCL | O_NOFOLLOW, | |
216 | 4978 perm < 0 ? 0666 : (perm & 0777))) < 0) |
7 | 4979 EMSG(_("E206: patchmode: can't touch empty original file")); |
4980 else | |
4981 close(empty_fd); | |
4982 } | |
4983 if (org != NULL) | |
4984 { | |
4985 mch_setperm((char_u *)org, mch_getperm(fname) & 0777); | |
4986 vim_free(org); | |
4987 } | |
4988 } | |
4989 | |
4990 /* | |
4991 * Remove the backup unless 'backup' option is set | |
4992 */ | |
4993 if (!p_bk && backup != NULL && mch_remove(backup) != 0) | |
4994 EMSG(_("E207: Can't delete backup file")); | |
4995 | |
4996 #ifdef FEAT_SUN_WORKSHOP | |
4997 if (usingSunWorkShop) | |
4998 workshop_file_saved((char *) ffname); | |
4999 #endif | |
5000 | |
5001 goto nofail; | |
5002 | |
5003 /* | |
5004 * Finish up. We get here either after failure or success. | |
5005 */ | |
5006 fail: | |
5007 --no_wait_return; /* may wait for return now */ | |
5008 nofail: | |
5009 | |
5010 /* Done saving, we accept changed buffer warnings again */ | |
5011 buf->b_saving = FALSE; | |
5012 | |
5013 vim_free(backup); | |
5014 if (buffer != smallbuf) | |
5015 vim_free(buffer); | |
5016 #ifdef FEAT_MBYTE | |
5017 vim_free(fenc_tofree); | |
5018 vim_free(write_info.bw_conv_buf); | |
5019 # ifdef USE_ICONV | |
5020 if (write_info.bw_iconv_fd != (iconv_t)-1) | |
5021 { | |
5022 iconv_close(write_info.bw_iconv_fd); | |
5023 write_info.bw_iconv_fd = (iconv_t)-1; | |
5024 } | |
5025 # endif | |
5026 #endif | |
5027 #ifdef HAVE_ACL | |
5028 mch_free_acl(acl); | |
5029 #endif | |
5030 | |
5031 if (errmsg != NULL) | |
5032 { | |
835 | 5033 int numlen = errnum != NULL ? (int)STRLEN(errnum) : 0; |
7 | 5034 |
5035 attr = hl_attr(HLF_E); /* set highlight for error messages */ | |
5036 msg_add_fname(buf, | |
5037 #ifndef UNIX | |
5038 sfname | |
5039 #else | |
5040 fname | |
5041 #endif | |
5042 ); /* put file name in IObuff with quotes */ | |
5043 if (STRLEN(IObuff) + STRLEN(errmsg) + numlen >= IOSIZE) | |
5044 IObuff[IOSIZE - STRLEN(errmsg) - numlen - 1] = NUL; | |
5045 /* If the error message has the form "is ...", put the error number in | |
5046 * front of the file name. */ | |
5047 if (errnum != NULL) | |
5048 { | |
1620 | 5049 STRMOVE(IObuff + numlen, IObuff); |
7 | 5050 mch_memmove(IObuff, errnum, (size_t)numlen); |
5051 } | |
5052 STRCAT(IObuff, errmsg); | |
5053 emsg(IObuff); | |
1947 | 5054 if (errmsg_allocated) |
5055 vim_free(errmsg); | |
7 | 5056 |
5057 retval = FAIL; | |
5058 if (end == 0) | |
5059 { | |
5060 MSG_PUTS_ATTR(_("\nWARNING: Original file may be lost or damaged\n"), | |
5061 attr | MSG_HIST); | |
5062 MSG_PUTS_ATTR(_("don't quit the editor until the file is successfully written!"), | |
5063 attr | MSG_HIST); | |
5064 | |
5065 /* Update the timestamp to avoid an "overwrite changed file" | |
5066 * prompt when writing again. */ | |
5067 if (mch_stat((char *)fname, &st_old) >= 0) | |
5068 { | |
5069 buf_store_time(buf, &st_old, fname); | |
5070 buf->b_mtime_read = buf->b_mtime; | |
5071 } | |
5072 } | |
5073 } | |
5074 msg_scroll = msg_save; | |
5075 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5076 #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
|
5077 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5078 * 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
|
5079 * file. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5080 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5081 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
|
5082 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5083 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
|
5084 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5085 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
|
5086 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
|
5087 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5088 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5089 |
7 | 5090 #ifdef FEAT_AUTOCMD |
5091 #ifdef FEAT_EVAL | |
5092 if (!should_abort(retval)) | |
5093 #else | |
5094 if (!got_int) | |
5095 #endif | |
5096 { | |
5097 aco_save_T aco; | |
5098 | |
3482 | 5099 curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */ |
5100 | |
7 | 5101 /* |
5102 * Apply POST autocommands. | |
5103 * Careful: The autocommands may call buf_write() recursively! | |
5104 */ | |
5105 aucmd_prepbuf(&aco, buf); | |
5106 | |
5107 if (append) | |
5108 apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname, | |
5109 FALSE, curbuf, eap); | |
5110 else if (filtering) | |
5111 apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname, | |
5112 FALSE, curbuf, eap); | |
5113 else if (reset_changed && whole) | |
5114 apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname, | |
5115 FALSE, curbuf, eap); | |
5116 else | |
5117 apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname, | |
5118 FALSE, curbuf, eap); | |
5119 | |
5120 /* restore curwin/curbuf and a few other things */ | |
5121 aucmd_restbuf(&aco); | |
5122 | |
5123 #ifdef FEAT_EVAL | |
5124 if (aborting()) /* autocmds may abort script processing */ | |
5125 retval = FALSE; | |
5126 #endif | |
5127 } | |
5128 #endif | |
5129 | |
5130 got_int |= prev_got_int; | |
5131 | |
5132 #ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */ | |
5133 /* Update machine specific information. */ | |
5134 mch_post_buffer_write(buf); | |
5135 #endif | |
5136 return retval; | |
5137 } | |
5138 | |
5139 /* | |
633 | 5140 * Set the name of the current buffer. Use when the buffer doesn't have a |
5141 * name and a ":r" or ":w" command with a file name is used. | |
5142 */ | |
5143 static int | |
5144 set_rw_fname(fname, sfname) | |
5145 char_u *fname; | |
5146 char_u *sfname; | |
5147 { | |
5148 #ifdef FEAT_AUTOCMD | |
1905 | 5149 buf_T *buf = curbuf; |
5150 | |
633 | 5151 /* It's like the unnamed buffer is deleted.... */ |
5152 if (curbuf->b_p_bl) | |
5153 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); | |
5154 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf); | |
5155 # ifdef FEAT_EVAL | |
5156 if (aborting()) /* autocmds may abort script processing */ | |
5157 return FAIL; | |
5158 # endif | |
1905 | 5159 if (curbuf != buf) |
5160 { | |
5161 /* We are in another buffer now, don't do the renaming. */ | |
5162 EMSG(_(e_auchangedbuf)); | |
5163 return FAIL; | |
5164 } | |
633 | 5165 #endif |
5166 | |
5167 if (setfname(curbuf, fname, sfname, FALSE) == OK) | |
5168 curbuf->b_flags |= BF_NOTEDITED; | |
5169 | |
5170 #ifdef FEAT_AUTOCMD | |
5171 /* ....and a new named one is created */ | |
5172 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf); | |
5173 if (curbuf->b_p_bl) | |
5174 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf); | |
5175 # ifdef FEAT_EVAL | |
5176 if (aborting()) /* autocmds may abort script processing */ | |
5177 return FAIL; | |
5178 # endif | |
5179 | |
5180 /* Do filetype detection now if 'filetype' is empty. */ | |
5181 if (*curbuf->b_p_ft == NUL) | |
5182 { | |
819 | 5183 if (au_has_group((char_u *)"filetypedetect")) |
676 | 5184 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE); |
717 | 5185 do_modelines(0); |
633 | 5186 } |
5187 #endif | |
5188 | |
5189 return OK; | |
5190 } | |
5191 | |
5192 /* | |
7 | 5193 * Put file name into IObuff with quotes. |
5194 */ | |
33 | 5195 void |
7 | 5196 msg_add_fname(buf, fname) |
5197 buf_T *buf; | |
5198 char_u *fname; | |
5199 { | |
5200 if (fname == NULL) | |
5201 fname = (char_u *)"-stdin-"; | |
5202 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE); | |
5203 IObuff[0] = '"'; | |
5204 STRCAT(IObuff, "\" "); | |
5205 } | |
5206 | |
5207 /* | |
5208 * Append message for text mode to IObuff. | |
5209 * Return TRUE if something appended. | |
5210 */ | |
5211 static int | |
5212 msg_add_fileformat(eol_type) | |
5213 int eol_type; | |
5214 { | |
5215 #ifndef USE_CRNL | |
5216 if (eol_type == EOL_DOS) | |
5217 { | |
5218 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]")); | |
5219 return TRUE; | |
5220 } | |
5221 #endif | |
5222 #ifndef USE_CR | |
5223 if (eol_type == EOL_MAC) | |
5224 { | |
5225 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]")); | |
5226 return TRUE; | |
5227 } | |
5228 #endif | |
5229 #if defined(USE_CRNL) || defined(USE_CR) | |
5230 if (eol_type == EOL_UNIX) | |
5231 { | |
5232 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]")); | |
5233 return TRUE; | |
5234 } | |
5235 #endif | |
5236 return FALSE; | |
5237 } | |
5238 | |
5239 /* | |
5240 * Append line and character count to IObuff. | |
5241 */ | |
33 | 5242 void |
7 | 5243 msg_add_lines(insert_space, lnum, nchars) |
5244 int insert_space; | |
5245 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
|
5246 off_t nchars; |
7 | 5247 { |
5248 char_u *p; | |
5249 | |
5250 p = IObuff + STRLEN(IObuff); | |
5251 | |
5252 if (insert_space) | |
5253 *p++ = ' '; | |
5254 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
|
5255 sprintf((char *)p, |
60da25e3aab7
Correct use of long instead of off_t for file size. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2239
diff
changeset
|
5256 #ifdef LONG_LONG_OFF_T |
2261
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5257 "%ldL, %lldC", lnum, nchars |
2241
60da25e3aab7
Correct use of long instead of off_t for file size. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2239
diff
changeset
|
5258 #else |
2261
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5259 /* Explicit typecast avoids warning on Mac OS X 10.6 */ |
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5260 "%ldL, %ldC", lnum, (long)nchars |
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5261 #endif |
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5262 ); |
7 | 5263 else |
5264 { | |
5265 if (lnum == 1) | |
5266 STRCPY(p, _("1 line, ")); | |
5267 else | |
5268 sprintf((char *)p, _("%ld lines, "), lnum); | |
5269 p += STRLEN(p); | |
5270 if (nchars == 1) | |
5271 STRCPY(p, _("1 character")); | |
5272 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
|
5273 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
|
5274 #ifdef LONG_LONG_OFF_T |
2261
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5275 _("%lld characters"), nchars |
2241
60da25e3aab7
Correct use of long instead of off_t for file size. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2239
diff
changeset
|
5276 #else |
2261
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5277 /* 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
|
5278 _("%ld characters"), (long)nchars |
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5279 #endif |
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5280 ); |
7 | 5281 } |
5282 } | |
5283 | |
5284 /* | |
5285 * Append message for missing line separator to IObuff. | |
5286 */ | |
5287 static void | |
5288 msg_add_eol() | |
5289 { | |
5290 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]")); | |
5291 } | |
5292 | |
5293 /* | |
5294 * Check modification time of file, before writing to it. | |
5295 * The size isn't checked, because using a tool like "gzip" takes care of | |
5296 * using the same timestamp but can't set the size. | |
5297 */ | |
5298 static int | |
5299 check_mtime(buf, st) | |
5300 buf_T *buf; | |
5301 struct stat *st; | |
5302 { | |
5303 if (buf->b_mtime_read != 0 | |
5304 && time_differs((long)st->st_mtime, buf->b_mtime_read)) | |
5305 { | |
5306 msg_scroll = TRUE; /* don't overwrite messages here */ | |
5307 msg_silent = 0; /* must give this prompt */ | |
5308 /* don't use emsg() here, don't want to flush the buffers */ | |
5309 MSG_ATTR(_("WARNING: The file has been changed since reading it!!!"), | |
5310 hl_attr(HLF_E)); | |
5311 if (ask_yesno((char_u *)_("Do you really want to write to it"), | |
5312 TRUE) == 'n') | |
5313 return FAIL; | |
5314 msg_scroll = FALSE; /* always overwrite the file message now */ | |
5315 } | |
5316 return OK; | |
5317 } | |
5318 | |
5319 static int | |
5320 time_differs(t1, t2) | |
5321 long t1, t2; | |
5322 { | |
5323 #if defined(__linux__) || defined(MSDOS) || defined(MSWIN) | |
5324 /* On a FAT filesystem, esp. under Linux, there are only 5 bits to store | |
5325 * the seconds. Since the roundoff is done when flushing the inode, the | |
5326 * time may change unexpectedly by one second!!! */ | |
5327 return (t1 - t2 > 1 || t2 - t1 > 1); | |
5328 #else | |
5329 return (t1 != t2); | |
5330 #endif | |
5331 } | |
5332 | |
5333 /* | |
5334 * Call write() to write a number of bytes to the file. | |
2664 | 5335 * Handles encryption and 'encoding' conversion. |
7 | 5336 * |
5337 * Return FAIL for failure, OK otherwise. | |
5338 */ | |
5339 static int | |
5340 buf_write_bytes(ip) | |
5341 struct bw_info *ip; | |
5342 { | |
5343 int wlen; | |
5344 char_u *buf = ip->bw_buf; /* data to write */ | |
5345 int len = ip->bw_len; /* length of data */ | |
5346 #ifdef HAS_BW_FLAGS | |
5347 int flags = ip->bw_flags; /* extra flags */ | |
5348 #endif | |
5349 | |
5350 #ifdef FEAT_MBYTE | |
5351 /* | |
5352 * Skip conversion when writing the crypt magic number or the BOM. | |
5353 */ | |
5354 if (!(flags & FIO_NOCONVERT)) | |
5355 { | |
5356 char_u *p; | |
5357 unsigned c; | |
5358 int n; | |
5359 | |
5360 if (flags & FIO_UTF8) | |
5361 { | |
5362 /* | |
5363 * Convert latin1 in the buffer to UTF-8 in the file. | |
5364 */ | |
5365 p = ip->bw_conv_buf; /* translate to buffer */ | |
5366 for (wlen = 0; wlen < len; ++wlen) | |
5367 p += utf_char2bytes(buf[wlen], p); | |
5368 buf = ip->bw_conv_buf; | |
5369 len = (int)(p - ip->bw_conv_buf); | |
5370 } | |
5371 else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1)) | |
5372 { | |
5373 /* | |
5374 * Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or | |
5375 * Latin1 chars in the file. | |
5376 */ | |
5377 if (flags & FIO_LATIN1) | |
5378 p = buf; /* translate in-place (can only get shorter) */ | |
5379 else | |
5380 p = ip->bw_conv_buf; /* translate to buffer */ | |
5381 for (wlen = 0; wlen < len; wlen += n) | |
5382 { | |
5383 if (wlen == 0 && ip->bw_restlen != 0) | |
5384 { | |
5385 int l; | |
5386 | |
5387 /* Use remainder of previous call. Append the start of | |
5388 * buf[] to get a full sequence. Might still be too | |
5389 * short! */ | |
5390 l = CONV_RESTLEN - ip->bw_restlen; | |
5391 if (l > len) | |
5392 l = len; | |
5393 mch_memmove(ip->bw_rest + ip->bw_restlen, buf, (size_t)l); | |
474 | 5394 n = utf_ptr2len_len(ip->bw_rest, ip->bw_restlen + l); |
7 | 5395 if (n > ip->bw_restlen + len) |
5396 { | |
5397 /* We have an incomplete byte sequence at the end to | |
5398 * be written. We can't convert it without the | |
5399 * remaining bytes. Keep them for the next call. */ | |
5400 if (ip->bw_restlen + len > CONV_RESTLEN) | |
5401 return FAIL; | |
5402 ip->bw_restlen += len; | |
5403 break; | |
5404 } | |
5405 if (n > 1) | |
5406 c = utf_ptr2char(ip->bw_rest); | |
5407 else | |
5408 c = ip->bw_rest[0]; | |
5409 if (n >= ip->bw_restlen) | |
5410 { | |
5411 n -= ip->bw_restlen; | |
5412 ip->bw_restlen = 0; | |
5413 } | |
5414 else | |
5415 { | |
5416 ip->bw_restlen -= n; | |
5417 mch_memmove(ip->bw_rest, ip->bw_rest + n, | |
5418 (size_t)ip->bw_restlen); | |
5419 n = 0; | |
5420 } | |
5421 } | |
5422 else | |
5423 { | |
474 | 5424 n = utf_ptr2len_len(buf + wlen, len - wlen); |
7 | 5425 if (n > len - wlen) |
5426 { | |
5427 /* We have an incomplete byte sequence at the end to | |
5428 * be written. We can't convert it without the | |
5429 * remaining bytes. Keep them for the next call. */ | |
5430 if (len - wlen > CONV_RESTLEN) | |
5431 return FAIL; | |
5432 ip->bw_restlen = len - wlen; | |
5433 mch_memmove(ip->bw_rest, buf + wlen, | |
5434 (size_t)ip->bw_restlen); | |
5435 break; | |
5436 } | |
5437 if (n > 1) | |
5438 c = utf_ptr2char(buf + wlen); | |
5439 else | |
5440 c = buf[wlen]; | |
5441 } | |
5442 | |
1947 | 5443 if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error) |
5444 { | |
5445 ip->bw_conv_error = TRUE; | |
5446 ip->bw_conv_error_lnum = ip->bw_start_lnum; | |
5447 } | |
5448 if (c == NL) | |
5449 ++ip->bw_start_lnum; | |
7 | 5450 } |
5451 if (flags & FIO_LATIN1) | |
5452 len = (int)(p - buf); | |
5453 else | |
5454 { | |
5455 buf = ip->bw_conv_buf; | |
5456 len = (int)(p - ip->bw_conv_buf); | |
5457 } | |
5458 } | |
5459 | |
5460 # ifdef WIN3264 | |
5461 else if (flags & FIO_CODEPAGE) | |
5462 { | |
5463 /* | |
5464 * Convert UTF-8 or codepage to UCS-2 and then to MS-Windows | |
5465 * codepage. | |
5466 */ | |
5467 char_u *from; | |
5468 size_t fromlen; | |
5469 char_u *to; | |
5470 int u8c; | |
5471 BOOL bad = FALSE; | |
5472 int needed; | |
5473 | |
5474 if (ip->bw_restlen > 0) | |
5475 { | |
5476 /* Need to concatenate the remainder of the previous call and | |
5477 * the bytes of the current call. Use the end of the | |
5478 * conversion buffer for this. */ | |
5479 fromlen = len + ip->bw_restlen; | |
5480 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; | |
5481 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); | |
5482 mch_memmove(from + ip->bw_restlen, buf, (size_t)len); | |
5483 } | |
5484 else | |
5485 { | |
5486 from = buf; | |
5487 fromlen = len; | |
5488 } | |
5489 | |
5490 to = ip->bw_conv_buf; | |
5491 if (enc_utf8) | |
5492 { | |
5493 /* Convert from UTF-8 to UCS-2, to the start of the buffer. | |
5494 * The buffer has been allocated to be big enough. */ | |
5495 while (fromlen > 0) | |
5496 { | |
835 | 5497 n = (int)utf_ptr2len_len(from, (int)fromlen); |
7 | 5498 if (n > (int)fromlen) /* incomplete byte sequence */ |
5499 break; | |
5500 u8c = utf_ptr2char(from); | |
5501 *to++ = (u8c & 0xff); | |
5502 *to++ = (u8c >> 8); | |
5503 fromlen -= n; | |
5504 from += n; | |
5505 } | |
5506 | |
5507 /* Copy remainder to ip->bw_rest[] to be used for the next | |
5508 * call. */ | |
5509 if (fromlen > CONV_RESTLEN) | |
5510 { | |
5511 /* weird overlong sequence */ | |
5512 ip->bw_conv_error = TRUE; | |
5513 return FAIL; | |
5514 } | |
5515 mch_memmove(ip->bw_rest, from, fromlen); | |
835 | 5516 ip->bw_restlen = (int)fromlen; |
7 | 5517 } |
5518 else | |
5519 { | |
5520 /* Convert from enc_codepage to UCS-2, to the start of the | |
5521 * buffer. The buffer has been allocated to be big enough. */ | |
5522 ip->bw_restlen = 0; | |
5523 needed = MultiByteToWideChar(enc_codepage, | |
1142 | 5524 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen, |
7 | 5525 NULL, 0); |
5526 if (needed == 0) | |
5527 { | |
5528 /* When conversion fails there may be a trailing byte. */ | |
5529 needed = MultiByteToWideChar(enc_codepage, | |
1142 | 5530 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen - 1, |
7 | 5531 NULL, 0); |
5532 if (needed == 0) | |
5533 { | |
5534 /* Conversion doesn't work. */ | |
5535 ip->bw_conv_error = TRUE; | |
5536 return FAIL; | |
5537 } | |
5538 /* Save the trailing byte for the next call. */ | |
5539 ip->bw_rest[0] = from[fromlen - 1]; | |
5540 ip->bw_restlen = 1; | |
5541 } | |
5542 needed = MultiByteToWideChar(enc_codepage, MB_ERR_INVALID_CHARS, | |
1142 | 5543 (LPCSTR)from, (int)(fromlen - ip->bw_restlen), |
7 | 5544 (LPWSTR)to, needed); |
5545 if (needed == 0) | |
5546 { | |
5547 /* Safety check: Conversion doesn't work. */ | |
5548 ip->bw_conv_error = TRUE; | |
5549 return FAIL; | |
5550 } | |
5551 to += needed * 2; | |
5552 } | |
5553 | |
5554 fromlen = to - ip->bw_conv_buf; | |
5555 buf = to; | |
5556 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
5557 if (FIO_GET_CP(flags) == CP_UTF8) | |
5558 { | |
5559 /* Convert from UCS-2 to UTF-8, using the remainder of the | |
5560 * conversion buffer. Fails when out of space. */ | |
5561 for (from = ip->bw_conv_buf; fromlen > 1; fromlen -= 2) | |
5562 { | |
5563 u8c = *from++; | |
5564 u8c += (*from++ << 8); | |
5565 to += utf_char2bytes(u8c, to); | |
5566 if (to + 6 >= ip->bw_conv_buf + ip->bw_conv_buflen) | |
5567 { | |
5568 ip->bw_conv_error = TRUE; | |
5569 return FAIL; | |
5570 } | |
5571 } | |
835 | 5572 len = (int)(to - buf); |
7 | 5573 } |
5574 else | |
5575 #endif | |
5576 { | |
5577 /* Convert from UCS-2 to the codepage, using the remainder of | |
5578 * the conversion buffer. If the conversion uses the default | |
5579 * character "0", the data doesn't fit in this encoding, so | |
5580 * fail. */ | |
5581 len = WideCharToMultiByte(FIO_GET_CP(flags), 0, | |
5582 (LPCWSTR)ip->bw_conv_buf, (int)fromlen / sizeof(WCHAR), | |
1142 | 5583 (LPSTR)to, (int)(ip->bw_conv_buflen - fromlen), 0, |
5584 &bad); | |
7 | 5585 if (bad) |
5586 { | |
5587 ip->bw_conv_error = TRUE; | |
5588 return FAIL; | |
5589 } | |
5590 } | |
5591 } | |
5592 # endif | |
5593 | |
765 | 5594 # ifdef MACOS_CONVERT |
7 | 5595 else if (flags & FIO_MACROMAN) |
5596 { | |
5597 /* | |
5598 * Convert UTF-8 or latin1 to Apple MacRoman. | |
5599 */ | |
5600 char_u *from; | |
5601 size_t fromlen; | |
5602 | |
5603 if (ip->bw_restlen > 0) | |
5604 { | |
5605 /* Need to concatenate the remainder of the previous call and | |
5606 * the bytes of the current call. Use the end of the | |
5607 * conversion buffer for this. */ | |
5608 fromlen = len + ip->bw_restlen; | |
5609 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; | |
5610 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); | |
5611 mch_memmove(from + ip->bw_restlen, buf, (size_t)len); | |
5612 } | |
5613 else | |
5614 { | |
5615 from = buf; | |
5616 fromlen = len; | |
5617 } | |
5618 | |
18 | 5619 if (enc2macroman(from, fromlen, |
5620 ip->bw_conv_buf, &len, ip->bw_conv_buflen, | |
5621 ip->bw_rest, &ip->bw_restlen) == FAIL) | |
7 | 5622 { |
5623 ip->bw_conv_error = TRUE; | |
5624 return FAIL; | |
5625 } | |
5626 buf = ip->bw_conv_buf; | |
5627 } | |
5628 # endif | |
5629 | |
5630 # ifdef USE_ICONV | |
5631 if (ip->bw_iconv_fd != (iconv_t)-1) | |
5632 { | |
5633 const char *from; | |
5634 size_t fromlen; | |
5635 char *to; | |
5636 size_t tolen; | |
5637 | |
5638 /* Convert with iconv(). */ | |
5639 if (ip->bw_restlen > 0) | |
5640 { | |
1836 | 5641 char *fp; |
5642 | |
7 | 5643 /* Need to concatenate the remainder of the previous call and |
5644 * the bytes of the current call. Use the end of the | |
5645 * conversion buffer for this. */ | |
5646 fromlen = len + ip->bw_restlen; | |
1836 | 5647 fp = (char *)ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; |
5648 mch_memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen); | |
5649 mch_memmove(fp + ip->bw_restlen, buf, (size_t)len); | |
5650 from = fp; | |
7 | 5651 tolen = ip->bw_conv_buflen - fromlen; |
5652 } | |
5653 else | |
5654 { | |
5655 from = (const char *)buf; | |
5656 fromlen = len; | |
5657 tolen = ip->bw_conv_buflen; | |
5658 } | |
5659 to = (char *)ip->bw_conv_buf; | |
5660 | |
5661 if (ip->bw_first) | |
5662 { | |
5663 size_t save_len = tolen; | |
5664 | |
5665 /* output the initial shift state sequence */ | |
5666 (void)iconv(ip->bw_iconv_fd, NULL, NULL, &to, &tolen); | |
5667 | |
5668 /* There is a bug in iconv() on Linux (which appears to be | |
5669 * wide-spread) which sets "to" to NULL and messes up "tolen". | |
5670 */ | |
5671 if (to == NULL) | |
5672 { | |
5673 to = (char *)ip->bw_conv_buf; | |
5674 tolen = save_len; | |
5675 } | |
5676 ip->bw_first = FALSE; | |
5677 } | |
5678 | |
5679 /* | |
5680 * If iconv() has an error or there is not enough room, fail. | |
5681 */ | |
5682 if ((iconv(ip->bw_iconv_fd, (void *)&from, &fromlen, &to, &tolen) | |
5683 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) | |
5684 || fromlen > CONV_RESTLEN) | |
5685 { | |
5686 ip->bw_conv_error = TRUE; | |
5687 return FAIL; | |
5688 } | |
5689 | |
5690 /* copy remainder to ip->bw_rest[] to be used for the next call. */ | |
5691 if (fromlen > 0) | |
5692 mch_memmove(ip->bw_rest, (void *)from, fromlen); | |
5693 ip->bw_restlen = (int)fromlen; | |
5694 | |
5695 buf = ip->bw_conv_buf; | |
5696 len = (int)((char_u *)to - ip->bw_conv_buf); | |
5697 } | |
5698 # endif | |
5699 } | |
5700 #endif /* FEAT_MBYTE */ | |
5701 | |
5702 #ifdef FEAT_CRYPT | |
5703 if (flags & FIO_ENCRYPTED) /* encrypt the data */ | |
2244 | 5704 crypt_encode(buf, len, buf); |
7 | 5705 #endif |
5706 | |
2664 | 5707 wlen = write_eintr(ip->bw_fd, buf, len); |
5708 return (wlen < len) ? FAIL : OK; | |
7 | 5709 } |
5710 | |
5711 #ifdef FEAT_MBYTE | |
5712 /* | |
5713 * Convert a Unicode character to bytes. | |
1947 | 5714 * Return TRUE for an error, FALSE when it's OK. |
7 | 5715 */ |
5716 static int | |
5717 ucs2bytes(c, pp, flags) | |
5718 unsigned c; /* in: character */ | |
5719 char_u **pp; /* in/out: pointer to result */ | |
5720 int flags; /* FIO_ flags */ | |
5721 { | |
5722 char_u *p = *pp; | |
5723 int error = FALSE; | |
5724 int cc; | |
5725 | |
5726 | |
5727 if (flags & FIO_UCS4) | |
5728 { | |
5729 if (flags & FIO_ENDIAN_L) | |
5730 { | |
5731 *p++ = c; | |
5732 *p++ = (c >> 8); | |
5733 *p++ = (c >> 16); | |
5734 *p++ = (c >> 24); | |
5735 } | |
5736 else | |
5737 { | |
5738 *p++ = (c >> 24); | |
5739 *p++ = (c >> 16); | |
5740 *p++ = (c >> 8); | |
5741 *p++ = c; | |
5742 } | |
5743 } | |
5744 else if (flags & (FIO_UCS2 | FIO_UTF16)) | |
5745 { | |
5746 if (c >= 0x10000) | |
5747 { | |
5748 if (flags & FIO_UTF16) | |
5749 { | |
5750 /* Make two words, ten bits of the character in each. First | |
5751 * word is 0xd800 - 0xdbff, second one 0xdc00 - 0xdfff */ | |
5752 c -= 0x10000; | |
5753 if (c >= 0x100000) | |
5754 error = TRUE; | |
5755 cc = ((c >> 10) & 0x3ff) + 0xd800; | |
5756 if (flags & FIO_ENDIAN_L) | |
5757 { | |
5758 *p++ = cc; | |
5759 *p++ = ((unsigned)cc >> 8); | |
5760 } | |
5761 else | |
5762 { | |
5763 *p++ = ((unsigned)cc >> 8); | |
5764 *p++ = cc; | |
5765 } | |
5766 c = (c & 0x3ff) + 0xdc00; | |
5767 } | |
5768 else | |
5769 error = TRUE; | |
5770 } | |
5771 if (flags & FIO_ENDIAN_L) | |
5772 { | |
5773 *p++ = c; | |
5774 *p++ = (c >> 8); | |
5775 } | |
5776 else | |
5777 { | |
5778 *p++ = (c >> 8); | |
5779 *p++ = c; | |
5780 } | |
5781 } | |
5782 else /* Latin1 */ | |
5783 { | |
5784 if (c >= 0x100) | |
5785 { | |
5786 error = TRUE; | |
5787 *p++ = 0xBF; | |
5788 } | |
5789 else | |
5790 *p++ = c; | |
5791 } | |
5792 | |
5793 *pp = p; | |
5794 return error; | |
5795 } | |
5796 | |
5797 /* | |
1948 | 5798 * Return TRUE if file encoding "fenc" requires conversion from or to |
5799 * 'encoding'. | |
7 | 5800 */ |
5801 static int | |
1948 | 5802 need_conversion(fenc) |
5803 char_u *fenc; | |
7 | 5804 { |
1948 | 5805 int same_encoding; |
5806 int enc_flags; | |
5807 int fenc_flags; | |
5808 | |
5809 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0) | |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5810 { |
1948 | 5811 same_encoding = TRUE; |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5812 fenc_flags = 0; |
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5813 } |
1948 | 5814 else |
5815 { | |
5816 /* Ignore difference between "ansi" and "latin1", "ucs-4" and | |
5817 * "ucs-4be", etc. */ | |
5818 enc_flags = get_fio_flags(p_enc); | |
5819 fenc_flags = get_fio_flags(fenc); | |
5820 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags); | |
5821 } | |
5822 if (same_encoding) | |
5823 { | |
5824 /* Specified encoding matches with 'encoding'. This requires | |
5825 * conversion when 'encoding' is Unicode but not UTF-8. */ | |
5826 return enc_unicode != 0; | |
5827 } | |
5828 | |
5829 /* Encodings differ. However, conversion is not needed when 'enc' is any | |
5830 * Unicode encoding and the file is UTF-8. */ | |
5831 return !(enc_utf8 && fenc_flags == FIO_UTF8); | |
7 | 5832 } |
5833 | |
5834 /* | |
5835 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the | |
5836 * internal conversion. | |
5837 * if "ptr" is an empty string, use 'encoding'. | |
5838 */ | |
5839 static int | |
5840 get_fio_flags(ptr) | |
5841 char_u *ptr; | |
5842 { | |
5843 int prop; | |
5844 | |
5845 if (*ptr == NUL) | |
5846 ptr = p_enc; | |
5847 | |
5848 prop = enc_canon_props(ptr); | |
5849 if (prop & ENC_UNICODE) | |
5850 { | |
5851 if (prop & ENC_2BYTE) | |
5852 { | |
5853 if (prop & ENC_ENDIAN_L) | |
5854 return FIO_UCS2 | FIO_ENDIAN_L; | |
5855 return FIO_UCS2; | |
5856 } | |
5857 if (prop & ENC_4BYTE) | |
5858 { | |
5859 if (prop & ENC_ENDIAN_L) | |
5860 return FIO_UCS4 | FIO_ENDIAN_L; | |
5861 return FIO_UCS4; | |
5862 } | |
5863 if (prop & ENC_2WORD) | |
5864 { | |
5865 if (prop & ENC_ENDIAN_L) | |
5866 return FIO_UTF16 | FIO_ENDIAN_L; | |
5867 return FIO_UTF16; | |
5868 } | |
5869 return FIO_UTF8; | |
5870 } | |
5871 if (prop & ENC_LATIN1) | |
5872 return FIO_LATIN1; | |
5873 /* must be ENC_DBCS, requires iconv() */ | |
5874 return 0; | |
5875 } | |
5876 | |
5877 #ifdef WIN3264 | |
5878 /* | |
5879 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed | |
5880 * for the conversion MS-Windows can do for us. Also accept "utf-8". | |
5881 * Used for conversion between 'encoding' and 'fileencoding'. | |
5882 */ | |
5883 static int | |
5884 get_win_fio_flags(ptr) | |
5885 char_u *ptr; | |
5886 { | |
5887 int cp; | |
5888 | |
5889 /* Cannot do this when 'encoding' is not utf-8 and not a codepage. */ | |
5890 if (!enc_utf8 && enc_codepage <= 0) | |
5891 return 0; | |
5892 | |
5893 cp = encname2codepage(ptr); | |
5894 if (cp == 0) | |
5895 { | |
5896 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
5897 if (STRCMP(ptr, "utf-8") == 0) | |
5898 cp = CP_UTF8; | |
5899 else | |
5900 # endif | |
5901 return 0; | |
5902 } | |
5903 return FIO_PUT_CP(cp) | FIO_CODEPAGE; | |
5904 } | |
5905 #endif | |
5906 | |
5907 #ifdef MACOS_X | |
5908 /* | |
5909 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags | |
5910 * needed for the internal conversion to/from utf-8 or latin1. | |
5911 */ | |
5912 static int | |
5913 get_mac_fio_flags(ptr) | |
5914 char_u *ptr; | |
5915 { | |
5916 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
5917 && (enc_canon_props(ptr) & ENC_MACROMAN)) | |
5918 return FIO_MACROMAN; | |
5919 return 0; | |
5920 } | |
5921 #endif | |
5922 | |
5923 /* | |
5924 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size]. | |
5925 * "size" must be at least 2. | |
5926 * Return the name of the encoding and set "*lenp" to the length. | |
5927 * Returns NULL when no BOM found. | |
5928 */ | |
5929 static char_u * | |
5930 check_for_bom(p, size, lenp, flags) | |
5931 char_u *p; | |
5932 long size; | |
5933 int *lenp; | |
5934 int flags; | |
5935 { | |
5936 char *name = NULL; | |
5937 int len = 2; | |
5938 | |
5939 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf | |
1688 | 5940 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0)) |
7 | 5941 { |
5942 name = "utf-8"; /* EF BB BF */ | |
5943 len = 3; | |
5944 } | |
5945 else if (p[0] == 0xff && p[1] == 0xfe) | |
5946 { | |
5947 if (size >= 4 && p[2] == 0 && p[3] == 0 | |
5948 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L))) | |
5949 { | |
5950 name = "ucs-4le"; /* FF FE 00 00 */ | |
5951 len = 4; | |
5952 } | |
1735 | 5953 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L)) |
7 | 5954 name = "ucs-2le"; /* FF FE */ |
1735 | 5955 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L)) |
5956 /* utf-16le is preferred, it also works for ucs-2le text */ | |
7 | 5957 name = "utf-16le"; /* FF FE */ |
5958 } | |
5959 else if (p[0] == 0xfe && p[1] == 0xff | |
5960 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16)) | |
5961 { | |
1547 | 5962 /* Default to utf-16, it works also for ucs-2 text. */ |
5963 if (flags == FIO_UCS2) | |
5964 name = "ucs-2"; /* FE FF */ | |
5965 else | |
7 | 5966 name = "utf-16"; /* FE FF */ |
5967 } | |
5968 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe | |
5969 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4)) | |
5970 { | |
5971 name = "ucs-4"; /* 00 00 FE FF */ | |
5972 len = 4; | |
5973 } | |
5974 | |
5975 *lenp = len; | |
5976 return (char_u *)name; | |
5977 } | |
5978 | |
5979 /* | |
5980 * Generate a BOM in "buf[4]" for encoding "name". | |
5981 * Return the length of the BOM (zero when no BOM). | |
5982 */ | |
5983 static int | |
5984 make_bom(buf, name) | |
5985 char_u *buf; | |
5986 char_u *name; | |
5987 { | |
5988 int flags; | |
5989 char_u *p; | |
5990 | |
5991 flags = get_fio_flags(name); | |
5992 | |
5993 /* Can't put a BOM in a non-Unicode file. */ | |
5994 if (flags == FIO_LATIN1 || flags == 0) | |
5995 return 0; | |
5996 | |
5997 if (flags == FIO_UTF8) /* UTF-8 */ | |
5998 { | |
5999 buf[0] = 0xef; | |
6000 buf[1] = 0xbb; | |
6001 buf[2] = 0xbf; | |
6002 return 3; | |
6003 } | |
6004 p = buf; | |
6005 (void)ucs2bytes(0xfeff, &p, flags); | |
6006 return (int)(p - buf); | |
6007 } | |
6008 #endif | |
6009 | |
1418 | 6010 #if defined(FEAT_VIMINFO) || defined(FEAT_BROWSE) || \ |
1471 | 6011 defined(FEAT_QUICKFIX) || defined(FEAT_AUTOCMD) || defined(PROTO) |
7 | 6012 /* |
6013 * Try to find a shortname by comparing the fullname with the current | |
6014 * directory. | |
1411 | 6015 * Returns "full_path" or pointer into "full_path" if shortened. |
6016 */ | |
6017 char_u * | |
6018 shorten_fname1(full_path) | |
6019 char_u *full_path; | |
6020 { | |
2770 | 6021 char_u *dirname; |
1411 | 6022 char_u *p = full_path; |
6023 | |
2770 | 6024 dirname = alloc(MAXPATHL); |
6025 if (dirname == NULL) | |
6026 return full_path; | |
1411 | 6027 if (mch_dirname(dirname, MAXPATHL) == OK) |
6028 { | |
6029 p = shorten_fname(full_path, dirname); | |
6030 if (p == NULL || *p == NUL) | |
6031 p = full_path; | |
6032 } | |
2770 | 6033 vim_free(dirname); |
1411 | 6034 return p; |
6035 } | |
1418 | 6036 #endif |
1411 | 6037 |
6038 /* | |
6039 * Try to find a shortname by comparing the fullname with the current | |
6040 * directory. | |
7 | 6041 * Returns NULL if not shorter name possible, pointer into "full_path" |
6042 * otherwise. | |
6043 */ | |
6044 char_u * | |
6045 shorten_fname(full_path, dir_name) | |
6046 char_u *full_path; | |
6047 char_u *dir_name; | |
6048 { | |
6049 int len; | |
6050 char_u *p; | |
6051 | |
6052 if (full_path == NULL) | |
6053 return NULL; | |
6054 len = (int)STRLEN(dir_name); | |
6055 if (fnamencmp(dir_name, full_path, len) == 0) | |
6056 { | |
6057 p = full_path + len; | |
6058 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
6059 /* | |
6060 * MSDOS: when a file is in the root directory, dir_name will end in a | |
6061 * slash, since C: by itself does not define a specific dir. In this | |
6062 * case p may already be correct. <negri> | |
6063 */ | |
6064 if (!((len > 2) && (*(p - 2) == ':'))) | |
6065 #endif | |
6066 { | |
6067 if (vim_ispathsep(*p)) | |
6068 ++p; | |
6069 #ifndef VMS /* the path separator is always part of the path */ | |
6070 else | |
6071 p = NULL; | |
6072 #endif | |
6073 } | |
6074 } | |
6075 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
6076 /* | |
6077 * When using a file in the current drive, remove the drive name: | |
6078 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on | |
6079 * a floppy from "A:\dir" to "B:\dir". | |
6080 */ | |
6081 else if (len > 3 | |
6082 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0]) | |
6083 && full_path[1] == ':' | |
6084 && vim_ispathsep(full_path[2])) | |
6085 p = full_path + 2; | |
6086 #endif | |
6087 else | |
6088 p = NULL; | |
6089 return p; | |
6090 } | |
6091 | |
6092 /* | |
6093 * Shorten filenames for all buffers. | |
6094 * When "force" is TRUE: Use full path from now on for files currently being | |
6095 * edited, both for file name and swap file name. Try to shorten the file | |
6096 * names a bit, if safe to do so. | |
6097 * When "force" is FALSE: Only try to shorten absolute file names. | |
6098 * For buffers that have buftype "nofile" or "scratch": never change the file | |
6099 * name. | |
6100 */ | |
6101 void | |
6102 shorten_fnames(force) | |
6103 int force; | |
6104 { | |
6105 char_u dirname[MAXPATHL]; | |
6106 buf_T *buf; | |
6107 char_u *p; | |
6108 | |
6109 mch_dirname(dirname, MAXPATHL); | |
6110 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
6111 { | |
6112 if (buf->b_fname != NULL | |
6113 #ifdef FEAT_QUICKFIX | |
6114 && !bt_nofile(buf) | |
6115 #endif | |
6116 && !path_with_url(buf->b_fname) | |
6117 && (force | |
6118 || buf->b_sfname == NULL | |
6119 || mch_isFullName(buf->b_sfname))) | |
6120 { | |
6121 vim_free(buf->b_sfname); | |
6122 buf->b_sfname = NULL; | |
6123 p = shorten_fname(buf->b_ffname, dirname); | |
6124 if (p != NULL) | |
6125 { | |
6126 buf->b_sfname = vim_strsave(p); | |
6127 buf->b_fname = buf->b_sfname; | |
6128 } | |
6129 if (p == NULL || buf->b_fname == NULL) | |
6130 buf->b_fname = buf->b_ffname; | |
9 | 6131 } |
6132 | |
6133 /* Always make the swap file name a full path, a "nofile" buffer may | |
6134 * also have a swap file. */ | |
6135 mf_fullname(buf->b_ml.ml_mfp); | |
7 | 6136 } |
6137 #ifdef FEAT_WINDOWS | |
6138 status_redraw_all(); | |
672 | 6139 redraw_tabline = TRUE; |
7 | 6140 #endif |
6141 } | |
6142 | |
6143 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \ | |
6144 || defined(FEAT_GUI_MSWIN) \ | |
6145 || defined(FEAT_GUI_MAC) \ | |
6146 || defined(PROTO) | |
6147 /* | |
6148 * Shorten all filenames in "fnames[count]" by current directory. | |
6149 */ | |
6150 void | |
6151 shorten_filenames(fnames, count) | |
6152 char_u **fnames; | |
6153 int count; | |
6154 { | |
6155 int i; | |
6156 char_u dirname[MAXPATHL]; | |
6157 char_u *p; | |
6158 | |
6159 if (fnames == NULL || count < 1) | |
6160 return; | |
6161 mch_dirname(dirname, sizeof(dirname)); | |
6162 for (i = 0; i < count; ++i) | |
6163 { | |
6164 if ((p = shorten_fname(fnames[i], dirname)) != NULL) | |
6165 { | |
6166 /* shorten_fname() returns pointer in given "fnames[i]". If free | |
6167 * "fnames[i]" first, "p" becomes invalid. So we need to copy | |
6168 * "p" first then free fnames[i]. */ | |
6169 p = vim_strsave(p); | |
6170 vim_free(fnames[i]); | |
6171 fnames[i] = p; | |
6172 } | |
6173 } | |
6174 } | |
6175 #endif | |
6176 | |
6177 /* | |
1651 | 6178 * add extension to file name - change path/fo.o.h to path/fo.o.h.ext or |
7 | 6179 * fo_o_h.ext for MSDOS or when shortname option set. |
6180 * | |
6181 * Assumed that fname is a valid name found in the filesystem we assure that | |
6182 * the return value is a different name and ends in 'ext'. | |
6183 * "ext" MUST be at most 4 characters long if it starts with a dot, 3 | |
6184 * characters otherwise. | |
6185 * Space for the returned name is allocated, must be freed later. | |
6186 * Returns NULL when out of memory. | |
6187 */ | |
6188 char_u * | |
6189 modname(fname, ext, prepend_dot) | |
6190 char_u *fname, *ext; | |
6191 int prepend_dot; /* may prepend a '.' to file name */ | |
6192 { | |
6193 return buf_modname( | |
6194 #ifdef SHORT_FNAME | |
6195 TRUE, | |
6196 #else | |
6197 (curbuf->b_p_sn || curbuf->b_shortname), | |
6198 #endif | |
6199 fname, ext, prepend_dot); | |
6200 } | |
6201 | |
6202 char_u * | |
6203 buf_modname(shortname, fname, ext, prepend_dot) | |
6204 int shortname; /* use 8.3 file name */ | |
6205 char_u *fname, *ext; | |
6206 int prepend_dot; /* may prepend a '.' to file name */ | |
6207 { | |
6208 char_u *retval; | |
6209 char_u *s; | |
6210 char_u *e; | |
6211 char_u *ptr; | |
6212 int fnamelen, extlen; | |
6213 | |
6214 extlen = (int)STRLEN(ext); | |
6215 | |
6216 /* | |
6217 * If there is no file name we must get the name of the current directory | |
6218 * (we need the full path in case :cd is used). | |
6219 */ | |
6220 if (fname == NULL || *fname == NUL) | |
6221 { | |
6222 retval = alloc((unsigned)(MAXPATHL + extlen + 3)); | |
6223 if (retval == NULL) | |
6224 return NULL; | |
6225 if (mch_dirname(retval, MAXPATHL) == FAIL || | |
6226 (fnamelen = (int)STRLEN(retval)) == 0) | |
6227 { | |
6228 vim_free(retval); | |
6229 return NULL; | |
6230 } | |
39 | 6231 if (!after_pathsep(retval, retval + fnamelen)) |
7 | 6232 { |
6233 retval[fnamelen++] = PATHSEP; | |
6234 retval[fnamelen] = NUL; | |
6235 } | |
6236 #ifndef SHORT_FNAME | |
6237 prepend_dot = FALSE; /* nothing to prepend a dot to */ | |
6238 #endif | |
6239 } | |
6240 else | |
6241 { | |
6242 fnamelen = (int)STRLEN(fname); | |
6243 retval = alloc((unsigned)(fnamelen + extlen + 3)); | |
6244 if (retval == NULL) | |
6245 return NULL; | |
6246 STRCPY(retval, fname); | |
6247 #ifdef VMS | |
6248 vms_remove_version(retval); /* we do not need versions here */ | |
6249 #endif | |
6250 } | |
6251 | |
6252 /* | |
6253 * search backwards until we hit a '/', '\' or ':' replacing all '.' | |
6254 * by '_' for MSDOS or when shortname option set and ext starts with a dot. | |
6255 * Then truncate what is after the '/', '\' or ':' to 8 characters for | |
6256 * MSDOS and 26 characters for AMIGA, a lot more for UNIX. | |
6257 */ | |
391 | 6258 for (ptr = retval + fnamelen; ptr > retval; mb_ptr_back(retval, ptr)) |
7 | 6259 { |
6260 if (*ext == '.' | |
2823 | 6261 #ifdef USE_LONG_FNAME |
7 | 6262 && (!USE_LONG_FNAME || shortname) |
2823 | 6263 #else |
6264 # ifndef SHORT_FNAME | |
7 | 6265 && shortname |
6266 # endif | |
2823 | 6267 #endif |
7 | 6268 ) |
6269 if (*ptr == '.') /* replace '.' by '_' */ | |
6270 *ptr = '_'; | |
6271 if (vim_ispathsep(*ptr)) | |
391 | 6272 { |
6273 ++ptr; | |
7 | 6274 break; |
391 | 6275 } |
6276 } | |
7 | 6277 |
6278 /* the file name has at most BASENAMELEN characters. */ | |
6279 #ifndef SHORT_FNAME | |
6280 if (STRLEN(ptr) > (unsigned)BASENAMELEN) | |
6281 ptr[BASENAMELEN] = '\0'; | |
6282 #endif | |
6283 | |
6284 s = ptr + STRLEN(ptr); | |
6285 | |
6286 /* | |
6287 * For 8.3 file names we may have to reduce the length. | |
6288 */ | |
6289 #ifdef USE_LONG_FNAME | |
6290 if (!USE_LONG_FNAME || shortname) | |
6291 #else | |
6292 # ifndef SHORT_FNAME | |
6293 if (shortname) | |
6294 # endif | |
6295 #endif | |
6296 { | |
6297 /* | |
6298 * If there is no file name, or the file name ends in '/', and the | |
6299 * extension starts with '.', put a '_' before the dot, because just | |
6300 * ".ext" is invalid. | |
6301 */ | |
6302 if (fname == NULL || *fname == NUL | |
6303 || vim_ispathsep(fname[STRLEN(fname) - 1])) | |
6304 { | |
6305 if (*ext == '.') | |
6306 *s++ = '_'; | |
6307 } | |
6308 /* | |
6309 * If the extension starts with '.', truncate the base name at 8 | |
6310 * characters | |
6311 */ | |
6312 else if (*ext == '.') | |
6313 { | |
1877 | 6314 if ((size_t)(s - ptr) > (size_t)8) |
7 | 6315 { |
6316 s = ptr + 8; | |
6317 *s = '\0'; | |
6318 } | |
6319 } | |
6320 /* | |
6321 * If the extension doesn't start with '.', and the file name | |
6322 * doesn't have an extension yet, append a '.' | |
6323 */ | |
6324 else if ((e = vim_strchr(ptr, '.')) == NULL) | |
6325 *s++ = '.'; | |
6326 /* | |
6327 * If the extension doesn't start with '.', and there already is an | |
1201 | 6328 * extension, it may need to be truncated |
7 | 6329 */ |
6330 else if ((int)STRLEN(e) + extlen > 4) | |
6331 s = e + 4 - extlen; | |
6332 } | |
6333 #if defined(OS2) || defined(USE_LONG_FNAME) || defined(WIN3264) | |
6334 /* | |
6335 * If there is no file name, and the extension starts with '.', put a | |
6336 * '_' before the dot, because just ".ext" may be invalid if it's on a | |
6337 * FAT partition, and on HPFS it doesn't matter. | |
6338 */ | |
6339 else if ((fname == NULL || *fname == NUL) && *ext == '.') | |
6340 *s++ = '_'; | |
6341 #endif | |
6342 | |
6343 /* | |
1651 | 6344 * Append the extension. |
7 | 6345 * ext can start with '.' and cannot exceed 3 more characters. |
6346 */ | |
6347 STRCPY(s, ext); | |
6348 | |
6349 #ifndef SHORT_FNAME | |
6350 /* | |
6351 * Prepend the dot. | |
6352 */ | |
2823 | 6353 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.' |
7 | 6354 #ifdef USE_LONG_FNAME |
6355 && USE_LONG_FNAME | |
6356 #endif | |
6357 ) | |
6358 { | |
1620 | 6359 STRMOVE(e + 1, e); |
7 | 6360 *e = '.'; |
6361 } | |
6362 #endif | |
6363 | |
6364 /* | |
6365 * Check that, after appending the extension, the file name is really | |
6366 * different. | |
6367 */ | |
6368 if (fname != NULL && STRCMP(fname, retval) == 0) | |
6369 { | |
6370 /* we search for a character that can be replaced by '_' */ | |
6371 while (--s >= ptr) | |
6372 { | |
6373 if (*s != '_') | |
6374 { | |
6375 *s = '_'; | |
6376 break; | |
6377 } | |
6378 } | |
6379 if (s < ptr) /* fname was "________.<ext>", how tricky! */ | |
6380 *ptr = 'v'; | |
6381 } | |
6382 return retval; | |
6383 } | |
6384 | |
6385 /* | |
6386 * Like fgets(), but if the file line is too long, it is truncated and the | |
6387 * rest of the line is thrown away. Returns TRUE for end-of-file. | |
6388 */ | |
6389 int | |
6390 vim_fgets(buf, size, fp) | |
6391 char_u *buf; | |
6392 int size; | |
6393 FILE *fp; | |
6394 { | |
6395 char *eof; | |
6396 #define FGETS_SIZE 200 | |
6397 char tbuf[FGETS_SIZE]; | |
6398 | |
6399 buf[size - 2] = NUL; | |
6400 #ifdef USE_CR | |
6401 eof = fgets_cr((char *)buf, size, fp); | |
6402 #else | |
6403 eof = fgets((char *)buf, size, fp); | |
6404 #endif | |
6405 if (buf[size - 2] != NUL && buf[size - 2] != '\n') | |
6406 { | |
6407 buf[size - 1] = NUL; /* Truncate the line */ | |
6408 | |
6409 /* Now throw away the rest of the line: */ | |
6410 do | |
6411 { | |
6412 tbuf[FGETS_SIZE - 2] = NUL; | |
6413 #ifdef USE_CR | |
1757 | 6414 ignoredp = fgets_cr((char *)tbuf, FGETS_SIZE, fp); |
7 | 6415 #else |
1757 | 6416 ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp); |
7 | 6417 #endif |
6418 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n'); | |
6419 } | |
6420 return (eof == NULL); | |
6421 } | |
6422 | |
6423 #if defined(USE_CR) || defined(PROTO) | |
6424 /* | |
6425 * Like vim_fgets(), but accept any line terminator: CR, CR-LF or LF. | |
6426 * Returns TRUE for end-of-file. | |
6427 * Only used for the Mac, because it's much slower than vim_fgets(). | |
6428 */ | |
6429 int | |
6430 tag_fgets(buf, size, fp) | |
6431 char_u *buf; | |
6432 int size; | |
6433 FILE *fp; | |
6434 { | |
6435 int i = 0; | |
6436 int c; | |
6437 int eof = FALSE; | |
6438 | |
6439 for (;;) | |
6440 { | |
6441 c = fgetc(fp); | |
6442 if (c == EOF) | |
6443 { | |
6444 eof = TRUE; | |
6445 break; | |
6446 } | |
6447 if (c == '\r') | |
6448 { | |
6449 /* Always store a NL for end-of-line. */ | |
6450 if (i < size - 1) | |
6451 buf[i++] = '\n'; | |
6452 c = fgetc(fp); | |
6453 if (c != '\n') /* Macintosh format: single CR. */ | |
6454 ungetc(c, fp); | |
6455 break; | |
6456 } | |
6457 if (i < size - 1) | |
6458 buf[i++] = c; | |
6459 if (c == '\n') | |
6460 break; | |
6461 } | |
6462 buf[i] = NUL; | |
6463 return eof; | |
6464 } | |
6465 #endif | |
6466 | |
6467 /* | |
6468 * rename() only works if both files are on the same file system, this | |
6469 * function will (attempts to?) copy the file across if rename fails -- webb | |
6470 * Return -1 for failure, 0 for success. | |
6471 */ | |
6472 int | |
6473 vim_rename(from, to) | |
6474 char_u *from; | |
6475 char_u *to; | |
6476 { | |
6477 int fd_in; | |
6478 int fd_out; | |
6479 int n; | |
6480 char *errmsg = NULL; | |
6481 char *buffer; | |
6482 #ifdef AMIGA | |
6483 BPTR flock; | |
6484 #endif | |
6485 struct stat st; | |
194 | 6486 long perm; |
199 | 6487 #ifdef HAVE_ACL |
6488 vim_acl_T acl; /* ACL from original file */ | |
6489 #endif | |
1779 | 6490 int use_tmp_file = FALSE; |
7 | 6491 |
6492 /* | |
1779 | 6493 * When the names are identical, there is nothing to do. When they refer |
6494 * to the same file (ignoring case and slash/backslash differences) but | |
6495 * the file name differs we need to go through a temp file. | |
7 | 6496 */ |
6497 if (fnamecmp(from, to) == 0) | |
1779 | 6498 { |
4242 | 6499 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0) |
1779 | 6500 use_tmp_file = TRUE; |
6501 else | |
6502 return 0; | |
6503 } | |
7 | 6504 |
6505 /* | |
6506 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted. | |
6507 */ | |
6508 if (mch_stat((char *)from, &st) < 0) | |
6509 return -1; | |
6510 | |
1778 | 6511 #ifdef UNIX |
6512 { | |
6513 struct stat st_to; | |
6514 | |
6515 /* It's possible for the source and destination to be the same file. | |
6516 * This happens when "from" and "to" differ in case and are on a FAT32 | |
6517 * filesystem. In that case go through a temp file name. */ | |
6518 if (mch_stat((char *)to, &st_to) >= 0 | |
6519 && st.st_dev == st_to.st_dev | |
6520 && st.st_ino == st_to.st_ino) | |
1779 | 6521 use_tmp_file = TRUE; |
6522 } | |
6523 #endif | |
2793 | 6524 #ifdef WIN3264 |
6525 { | |
6526 BY_HANDLE_FILE_INFORMATION info1, info2; | |
6527 | |
6528 /* It's possible for the source and destination to be the same file. | |
6529 * In that case go through a temp file name. This makes rename("foo", | |
6530 * "./foo") a no-op (in a complicated way). */ | |
6531 if (win32_fileinfo(from, &info1) == FILEINFO_OK | |
6532 && win32_fileinfo(to, &info2) == FILEINFO_OK | |
6533 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber | |
6534 && info1.nFileIndexHigh == info2.nFileIndexHigh | |
6535 && info1.nFileIndexLow == info2.nFileIndexLow) | |
6536 use_tmp_file = TRUE; | |
6537 } | |
6538 #endif | |
1779 | 6539 |
6540 if (use_tmp_file) | |
6541 { | |
6542 char tempname[MAXPATHL + 1]; | |
6543 | |
6544 /* | |
6545 * Find a name that doesn't exist and is in the same directory. | |
6546 * Rename "from" to "tempname" and then rename "tempname" to "to". | |
6547 */ | |
6548 if (STRLEN(from) >= MAXPATHL - 5) | |
6549 return -1; | |
6550 STRCPY(tempname, from); | |
6551 for (n = 123; n < 99999; ++n) | |
6552 { | |
6553 sprintf((char *)gettail((char_u *)tempname), "%d", n); | |
6554 if (mch_stat(tempname, &st) < 0) | |
6555 { | |
6556 if (mch_rename((char *)from, tempname) == 0) | |
1778 | 6557 { |
1779 | 6558 if (mch_rename(tempname, (char *)to) == 0) |
6559 return 0; | |
6560 /* Strange, the second step failed. Try moving the | |
6561 * file back and return failure. */ | |
6562 mch_rename(tempname, (char *)from); | |
1778 | 6563 return -1; |
6564 } | |
1779 | 6565 /* If it fails for one temp name it will most likely fail |
6566 * for any temp name, give up. */ | |
6567 return -1; | |
6568 } | |
6569 } | |
6570 return -1; | |
1778 | 6571 } |
6572 | |
7 | 6573 /* |
6574 * Delete the "to" file, this is required on some systems to make the | |
6575 * mch_rename() work, on other systems it makes sure that we don't have | |
6576 * two files when the mch_rename() fails. | |
6577 */ | |
6578 | |
6579 #ifdef AMIGA | |
6580 /* | |
6581 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible | |
6582 * that the name of the "to" file is the same as the "from" file, even | |
1201 | 6583 * though the names are different. To avoid the chance of accidentally |
7 | 6584 * deleting the "from" file (horror!) we lock it during the remove. |
6585 * | |
6586 * When used for making a backup before writing the file: This should not | |
6587 * happen with ":w", because startscript() should detect this problem and | |
6588 * set buf->b_shortname, causing modname() to return a correct ".bak" file | |
6589 * name. This problem does exist with ":w filename", but then the | |
6590 * original file will be somewhere else so the backup isn't really | |
6591 * important. If autoscripting is off the rename may fail. | |
6592 */ | |
6593 flock = Lock((UBYTE *)from, (long)ACCESS_READ); | |
6594 #endif | |
6595 mch_remove(to); | |
6596 #ifdef AMIGA | |
6597 if (flock) | |
6598 UnLock(flock); | |
6599 #endif | |
6600 | |
6601 /* | |
6602 * First try a normal rename, return if it works. | |
6603 */ | |
6604 if (mch_rename((char *)from, (char *)to) == 0) | |
6605 return 0; | |
6606 | |
6607 /* | |
6608 * Rename() failed, try copying the file. | |
6609 */ | |
194 | 6610 perm = mch_getperm(from); |
199 | 6611 #ifdef HAVE_ACL |
6612 /* For systems that support ACL: get the ACL from the original file. */ | |
6613 acl = mch_get_acl(from); | |
6614 #endif | |
7 | 6615 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0); |
6616 if (fd_in == -1) | |
1651 | 6617 { |
6618 #ifdef HAVE_ACL | |
6619 mch_free_acl(acl); | |
6620 #endif | |
7 | 6621 return -1; |
1651 | 6622 } |
194 | 6623 |
6624 /* Create the new file with same permissions as the original. */ | |
557 | 6625 fd_out = mch_open((char *)to, |
6626 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm); | |
7 | 6627 if (fd_out == -1) |
6628 { | |
6629 close(fd_in); | |
1651 | 6630 #ifdef HAVE_ACL |
6631 mch_free_acl(acl); | |
6632 #endif | |
7 | 6633 return -1; |
6634 } | |
6635 | |
6636 buffer = (char *)alloc(BUFSIZE); | |
6637 if (buffer == NULL) | |
6638 { | |
1651 | 6639 close(fd_out); |
7 | 6640 close(fd_in); |
1651 | 6641 #ifdef HAVE_ACL |
6642 mch_free_acl(acl); | |
6643 #endif | |
7 | 6644 return -1; |
6645 } | |
6646 | |
2664 | 6647 while ((n = read_eintr(fd_in, buffer, BUFSIZE)) > 0) |
6648 if (write_eintr(fd_out, buffer, n) != n) | |
7 | 6649 { |
6650 errmsg = _("E208: Error writing to \"%s\""); | |
6651 break; | |
6652 } | |
6653 | |
6654 vim_free(buffer); | |
6655 close(fd_in); | |
6656 if (close(fd_out) < 0) | |
6657 errmsg = _("E209: Error closing \"%s\""); | |
6658 if (n < 0) | |
6659 { | |
6660 errmsg = _("E210: Error reading \"%s\""); | |
6661 to = from; | |
6662 } | |
1201 | 6663 #ifndef UNIX /* for Unix mch_open() already set the permission */ |
194 | 6664 mch_setperm(to, perm); |
570 | 6665 #endif |
199 | 6666 #ifdef HAVE_ACL |
6667 mch_set_acl(to, acl); | |
1651 | 6668 mch_free_acl(acl); |
199 | 6669 #endif |
7 | 6670 if (errmsg != NULL) |
6671 { | |
6672 EMSG2(errmsg, to); | |
6673 return -1; | |
6674 } | |
6675 mch_remove(from); | |
6676 return 0; | |
6677 } | |
6678 | |
6679 static int already_warned = FALSE; | |
6680 | |
6681 /* | |
6682 * Check if any not hidden buffer has been changed. | |
6683 * Postpone the check if there are characters in the stuff buffer, a global | |
6684 * command is being executed, a mapping is being executed or an autocommand is | |
6685 * busy. | |
6686 * Returns TRUE if some message was written (screen should be redrawn and | |
6687 * cursor positioned). | |
6688 */ | |
6689 int | |
6690 check_timestamps(focus) | |
6691 int focus; /* called for GUI focus event */ | |
6692 { | |
6693 buf_T *buf; | |
6694 int didit = 0; | |
6695 int n; | |
6696 | |
6697 /* Don't check timestamps while system() or another low-level function may | |
6698 * cause us to lose and gain focus. */ | |
6699 if (no_check_timestamps > 0) | |
6700 return FALSE; | |
6701 | |
6702 /* Avoid doing a check twice. The OK/Reload dialog can cause a focus | |
6703 * event and we would keep on checking if the file is steadily growing. | |
6704 * Do check again after typing something. */ | |
6705 if (focus && did_check_timestamps) | |
6706 { | |
6707 need_check_timestamps = TRUE; | |
6708 return FALSE; | |
6709 } | |
6710 | |
6711 if (!stuff_empty() || global_busy || !typebuf_typed() | |
6712 #ifdef FEAT_AUTOCMD | |
1834 | 6713 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0 |
7 | 6714 #endif |
6715 ) | |
6716 need_check_timestamps = TRUE; /* check later */ | |
6717 else | |
6718 { | |
6719 ++no_wait_return; | |
6720 did_check_timestamps = TRUE; | |
6721 already_warned = FALSE; | |
6722 for (buf = firstbuf; buf != NULL; ) | |
6723 { | |
6724 /* Only check buffers in a window. */ | |
6725 if (buf->b_nwindows > 0) | |
6726 { | |
6727 n = buf_check_timestamp(buf, focus); | |
6728 if (didit < n) | |
6729 didit = n; | |
6730 if (n > 0 && !buf_valid(buf)) | |
6731 { | |
6732 /* Autocommands have removed the buffer, start at the | |
6733 * first one again. */ | |
6734 buf = firstbuf; | |
6735 continue; | |
6736 } | |
6737 } | |
6738 buf = buf->b_next; | |
6739 } | |
6740 --no_wait_return; | |
6741 need_check_timestamps = FALSE; | |
6742 if (need_wait_return && didit == 2) | |
6743 { | |
6744 /* make sure msg isn't overwritten */ | |
6745 msg_puts((char_u *)"\n"); | |
6746 out_flush(); | |
6747 } | |
6748 } | |
6749 return didit; | |
6750 } | |
6751 | |
6752 /* | |
6753 * Move all the lines from buffer "frombuf" to buffer "tobuf". | |
6754 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not | |
6755 * empty. | |
6756 */ | |
6757 static int | |
6758 move_lines(frombuf, tobuf) | |
6759 buf_T *frombuf; | |
6760 buf_T *tobuf; | |
6761 { | |
6762 buf_T *tbuf = curbuf; | |
6763 int retval = OK; | |
6764 linenr_T lnum; | |
6765 char_u *p; | |
6766 | |
6767 /* Copy the lines in "frombuf" to "tobuf". */ | |
6768 curbuf = tobuf; | |
6769 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum) | |
6770 { | |
6771 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE)); | |
6772 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL) | |
6773 { | |
6774 vim_free(p); | |
6775 retval = FAIL; | |
6776 break; | |
6777 } | |
6778 vim_free(p); | |
6779 } | |
6780 | |
6781 /* Delete all the lines in "frombuf". */ | |
6782 if (retval != FAIL) | |
6783 { | |
6784 curbuf = frombuf; | |
1055 | 6785 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum) |
6786 if (ml_delete(lnum, FALSE) == FAIL) | |
7 | 6787 { |
6788 /* Oops! We could try putting back the saved lines, but that | |
6789 * might fail again... */ | |
6790 retval = FAIL; | |
6791 break; | |
6792 } | |
6793 } | |
6794 | |
6795 curbuf = tbuf; | |
6796 return retval; | |
6797 } | |
6798 | |
6799 /* | |
6800 * Check if buffer "buf" has been changed. | |
6801 * Also check if the file for a new buffer unexpectedly appeared. | |
6802 * return 1 if a changed buffer was found. | |
6803 * return 2 if a message has been displayed. | |
6804 * return 0 otherwise. | |
6805 */ | |
6806 int | |
6807 buf_check_timestamp(buf, focus) | |
6808 buf_T *buf; | |
1877 | 6809 int focus UNUSED; /* called for GUI focus event */ |
7 | 6810 { |
6811 struct stat st; | |
6812 int stat_res; | |
6813 int retval = 0; | |
6814 char_u *path; | |
6815 char_u *tbuf; | |
6816 char *mesg = NULL; | |
188 | 6817 char *mesg2 = ""; |
7 | 6818 int helpmesg = FALSE; |
6819 int reload = FALSE; | |
6820 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
6821 int can_reload = FALSE; | |
6822 #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
|
6823 off_t orig_size = buf->b_orig_size; |
7 | 6824 int orig_mode = buf->b_orig_mode; |
6825 #ifdef FEAT_GUI | |
6826 int save_mouse_correct = need_mouse_correct; | |
6827 #endif | |
6828 #ifdef FEAT_AUTOCMD | |
6829 static int busy = FALSE; | |
179 | 6830 int n; |
6831 char_u *s; | |
6832 #endif | |
6833 char *reason; | |
7 | 6834 |
6835 /* If there is no file name, the buffer is not loaded, 'buftype' is | |
6836 * set, we are in the middle of a save or being called recursively: ignore | |
6837 * this buffer. */ | |
6838 if (buf->b_ffname == NULL | |
6839 || buf->b_ml.ml_mfp == NULL | |
6840 #if defined(FEAT_QUICKFIX) | |
6841 || *buf->b_p_bt != NUL | |
6842 #endif | |
6843 || buf->b_saving | |
6844 #ifdef FEAT_AUTOCMD | |
6845 || busy | |
6846 #endif | |
33 | 6847 #ifdef FEAT_NETBEANS_INTG |
6848 || isNetbeansBuffer(buf) | |
6849 #endif | |
7 | 6850 ) |
6851 return 0; | |
6852 | |
6853 if ( !(buf->b_flags & BF_NOTEDITED) | |
6854 && buf->b_mtime != 0 | |
6855 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0 | |
6856 || time_differs((long)st.st_mtime, buf->b_mtime) | |
6857 #ifdef HAVE_ST_MODE | |
6858 || (int)st.st_mode != buf->b_orig_mode | |
6859 #else | |
6860 || mch_getperm(buf->b_ffname) != buf->b_orig_mode | |
6861 #endif | |
6862 )) | |
6863 { | |
6864 retval = 1; | |
6865 | |
628 | 6866 /* set b_mtime to stop further warnings (e.g., when executing |
6867 * FileChangedShell autocmd) */ | |
7 | 6868 if (stat_res < 0) |
6869 { | |
6870 buf->b_mtime = 0; | |
6871 buf->b_orig_size = 0; | |
6872 buf->b_orig_mode = 0; | |
6873 } | |
6874 else | |
6875 buf_store_time(buf, &st, buf->b_ffname); | |
6876 | |
6877 /* Don't do anything for a directory. Might contain the file | |
6878 * explorer. */ | |
6879 if (mch_isdir(buf->b_fname)) | |
6880 ; | |
6881 | |
6882 /* | |
6883 * If 'autoread' is set, the buffer has no changes and the file still | |
6884 * exists, reload the buffer. Use the buffer-local option value if it | |
6885 * was set, the global option value otherwise. | |
6886 */ | |
6887 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar) | |
6888 && !bufIsChanged(buf) && stat_res >= 0) | |
6889 reload = TRUE; | |
6890 else | |
6891 { | |
179 | 6892 if (stat_res < 0) |
6893 reason = "deleted"; | |
6894 else if (bufIsChanged(buf)) | |
6895 reason = "conflict"; | |
6896 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf)) | |
6897 reason = "changed"; | |
6898 else if (orig_mode != buf->b_orig_mode) | |
6899 reason = "mode"; | |
6900 else | |
6901 reason = "time"; | |
6902 | |
7 | 6903 #ifdef FEAT_AUTOCMD |
6904 /* | |
6905 * Only give the warning if there are no FileChangedShell | |
6906 * autocommands. | |
6907 * Avoid being called recursively by setting "busy". | |
6908 */ | |
6909 busy = TRUE; | |
532 | 6910 # ifdef FEAT_EVAL |
179 | 6911 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1); |
6912 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1); | |
532 | 6913 # endif |
1834 | 6914 ++allbuf_lock; |
7 | 6915 n = apply_autocmds(EVENT_FILECHANGEDSHELL, |
6916 buf->b_fname, buf->b_fname, FALSE, buf); | |
1834 | 6917 --allbuf_lock; |
7 | 6918 busy = FALSE; |
6919 if (n) | |
6920 { | |
6921 if (!buf_valid(buf)) | |
6922 EMSG(_("E246: FileChangedShell autocommand deleted buffer")); | |
532 | 6923 # ifdef FEAT_EVAL |
179 | 6924 s = get_vim_var_str(VV_FCS_CHOICE); |
6925 if (STRCMP(s, "reload") == 0 && *reason != 'd') | |
6926 reload = TRUE; | |
6927 else if (STRCMP(s, "ask") == 0) | |
6928 n = FALSE; | |
6929 else | |
532 | 6930 # endif |
179 | 6931 return 2; |
7 | 6932 } |
179 | 6933 if (!n) |
7 | 6934 #endif |
6935 { | |
179 | 6936 if (*reason == 'd') |
6937 mesg = _("E211: File \"%s\" no longer available"); | |
7 | 6938 else |
6939 { | |
6940 helpmesg = TRUE; | |
6941 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
6942 can_reload = TRUE; | |
6943 #endif | |
6944 /* | |
6945 * Check if the file contents really changed to avoid | |
6946 * giving a warning when only the timestamp was set (e.g., | |
6947 * checked out of CVS). Always warn when the buffer was | |
6948 * changed. | |
6949 */ | |
179 | 6950 if (reason[2] == 'n') |
6951 { | |
7 | 6952 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well"); |
179 | 6953 mesg2 = _("See \":help W12\" for more info."); |
6954 } | |
6955 else if (reason[1] == 'h') | |
6956 { | |
7 | 6957 mesg = _("W11: Warning: File \"%s\" has changed since editing started"); |
179 | 6958 mesg2 = _("See \":help W11\" for more info."); |
6959 } | |
6960 else if (*reason == 'm') | |
6961 { | |
7 | 6962 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started"); |
179 | 6963 mesg2 = _("See \":help W16\" for more info."); |
6964 } | |
1913 | 6965 else |
6966 /* Only timestamp changed, store it to avoid a warning | |
6967 * in check_mtime() later. */ | |
6968 buf->b_mtime_read = buf->b_mtime; | |
7 | 6969 } |
6970 } | |
6971 } | |
6972 | |
6973 } | |
6974 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W) | |
6975 && vim_fexists(buf->b_ffname)) | |
6976 { | |
6977 retval = 1; | |
6978 mesg = _("W13: Warning: File \"%s\" has been created after editing started"); | |
6979 buf->b_flags |= BF_NEW_W; | |
6980 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
6981 can_reload = TRUE; | |
6982 #endif | |
6983 } | |
6984 | |
6985 if (mesg != NULL) | |
6986 { | |
6987 path = home_replace_save(buf, buf->b_fname); | |
6988 if (path != NULL) | |
6989 { | |
179 | 6990 if (!helpmesg) |
7 | 6991 mesg2 = ""; |
6992 tbuf = alloc((unsigned)(STRLEN(path) + STRLEN(mesg) | |
6993 + STRLEN(mesg2) + 2)); | |
6994 sprintf((char *)tbuf, mesg, path); | |
1848 | 6995 #ifdef FEAT_EVAL |
6996 /* Set warningmsg here, before the unimportant and output-specific | |
6997 * mesg2 has been appended. */ | |
6998 set_vim_var_string(VV_WARNINGMSG, tbuf, -1); | |
6999 #endif | |
7 | 7000 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
7001 if (can_reload) | |
7002 { | |
7003 if (*mesg2 != NUL) | |
7004 { | |
7005 STRCAT(tbuf, "\n"); | |
7006 STRCAT(tbuf, mesg2); | |
7007 } | |
7008 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"), tbuf, | |
2684 | 7009 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2) |
7 | 7010 reload = TRUE; |
7011 } | |
7012 else | |
7013 #endif | |
7014 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned) | |
7015 { | |
7016 if (*mesg2 != NUL) | |
7017 { | |
7018 STRCAT(tbuf, "; "); | |
7019 STRCAT(tbuf, mesg2); | |
7020 } | |
7021 EMSG(tbuf); | |
7022 retval = 2; | |
7023 } | |
7024 else | |
7025 { | |
8 | 7026 # ifdef FEAT_AUTOCMD |
7 | 7027 if (!autocmd_busy) |
8 | 7028 # endif |
7 | 7029 { |
7030 msg_start(); | |
7031 msg_puts_attr(tbuf, hl_attr(HLF_E) + MSG_HIST); | |
7032 if (*mesg2 != NUL) | |
7033 msg_puts_attr((char_u *)mesg2, | |
7034 hl_attr(HLF_W) + MSG_HIST); | |
7035 msg_clr_eos(); | |
7036 (void)msg_end(); | |
7037 if (emsg_silent == 0) | |
7038 { | |
7039 out_flush(); | |
8 | 7040 # ifdef FEAT_GUI |
7 | 7041 if (!focus) |
8 | 7042 # endif |
7 | 7043 /* give the user some time to think about it */ |
7044 ui_delay(1000L, TRUE); | |
7045 | |
7046 /* don't redraw and erase the message */ | |
7047 redraw_cmdline = FALSE; | |
7048 } | |
7049 } | |
7050 already_warned = TRUE; | |
7051 } | |
7052 | |
7053 vim_free(path); | |
7054 vim_free(tbuf); | |
7055 } | |
7056 } | |
7057 | |
7058 if (reload) | |
3776 | 7059 { |
311 | 7060 /* Reload the buffer. */ |
628 | 7061 buf_reload(buf, orig_mode); |
3776 | 7062 #ifdef FEAT_PERSISTENT_UNDO |
7063 if (buf->b_p_udf && buf->b_ffname != NULL) | |
7064 { | |
7065 char_u hash[UNDO_HASH_SIZE]; | |
7066 buf_T *save_curbuf = curbuf; | |
7067 | |
7068 /* Any existing undo file is unusable, write it now. */ | |
7069 curbuf = buf; | |
7070 u_compute_hash(hash); | |
7071 u_write_undo(NULL, FALSE, buf, hash); | |
7072 curbuf = save_curbuf; | |
7073 } | |
7074 #endif | |
7075 } | |
7 | 7076 |
765 | 7077 #ifdef FEAT_AUTOCMD |
1620 | 7078 /* Trigger FileChangedShell when the file was changed in any way. */ |
7079 if (buf_valid(buf) && retval != 0) | |
765 | 7080 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, |
7081 buf->b_fname, buf->b_fname, FALSE, buf); | |
7082 #endif | |
7 | 7083 #ifdef FEAT_GUI |
7084 /* restore this in case an autocommand has set it; it would break | |
7085 * 'mousefocus' */ | |
7086 need_mouse_correct = save_mouse_correct; | |
7087 #endif | |
7088 | |
7089 return retval; | |
7090 } | |
7091 | |
311 | 7092 /* |
7093 * Reload a buffer that is already loaded. | |
7094 * Used when the file was changed outside of Vim. | |
628 | 7095 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected. |
7096 * buf->b_orig_mode may have been reset already. | |
311 | 7097 */ |
7098 void | |
628 | 7099 buf_reload(buf, orig_mode) |
311 | 7100 buf_T *buf; |
628 | 7101 int orig_mode; |
311 | 7102 { |
7103 exarg_T ea; | |
7104 pos_T old_cursor; | |
7105 linenr_T old_topline; | |
7106 int old_ro = buf->b_p_ro; | |
7107 buf_T *savebuf; | |
7108 int saved = OK; | |
7109 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
|
7110 int flags = READ_NEW; |
311 | 7111 |
7112 /* set curwin/curbuf for "buf" and save some things */ | |
7113 aucmd_prepbuf(&aco, buf); | |
7114 | |
7115 /* We only want to read the text from the file, not reset the syntax | |
7116 * highlighting, clear marks, diff status, etc. Force the fileformat | |
7117 * and encoding to be the same. */ | |
7118 if (prep_exarg(&ea, buf) == OK) | |
7119 { | |
7120 old_cursor = curwin->w_cursor; | |
7121 old_topline = curwin->w_topline; | |
7122 | |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
7123 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
|
7124 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7125 /* 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
|
7126 * 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
|
7127 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
|
7128 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
|
7129 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
|
7130 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7131 |
311 | 7132 /* |
7133 * To behave like when a new file is edited (matters for | |
7134 * BufReadPost autocommands) we first need to delete the current | |
7135 * buffer contents. But if reading the file fails we should keep | |
7136 * the old contents. Can't use memory only, the file might be | |
7137 * too big. Use a hidden buffer to move the buffer contents to. | |
7138 */ | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7139 if (bufempty() || saved == FAIL) |
311 | 7140 savebuf = NULL; |
7141 else | |
7142 { | |
7143 /* Allocate a buffer without putting it in the buffer list. */ | |
7144 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); | |
837 | 7145 if (savebuf != NULL && buf == curbuf) |
311 | 7146 { |
7147 /* Open the memline. */ | |
7148 curbuf = savebuf; | |
7149 curwin->w_buffer = savebuf; | |
625 | 7150 saved = ml_open(curbuf); |
311 | 7151 curbuf = buf; |
7152 curwin->w_buffer = buf; | |
7153 } | |
837 | 7154 if (savebuf == NULL || saved == FAIL || buf != curbuf |
311 | 7155 || move_lines(buf, savebuf) == FAIL) |
7156 { | |
7157 EMSG2(_("E462: Could not prepare for reloading \"%s\""), | |
7158 buf->b_fname); | |
7159 saved = FAIL; | |
7160 } | |
7161 } | |
7162 | |
7163 if (saved == OK) | |
7164 { | |
7165 curbuf->b_flags |= BF_CHECK_RO; /* check for RO again */ | |
7166 #ifdef FEAT_AUTOCMD | |
7167 keep_filetype = TRUE; /* don't detect 'filetype' */ | |
7168 #endif | |
7169 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, | |
7170 (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
|
7171 (linenr_T)MAXLNUM, &ea, flags) == FAIL) |
311 | 7172 { |
7173 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
7174 if (!aborting()) | |
7175 #endif | |
7176 EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname); | |
837 | 7177 if (savebuf != NULL && buf_valid(savebuf) && buf == curbuf) |
311 | 7178 { |
7179 /* Put the text back from the save buffer. First | |
7180 * delete any lines that readfile() added. */ | |
7181 while (!bufempty()) | |
837 | 7182 if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL) |
311 | 7183 break; |
7184 (void)move_lines(savebuf, buf); | |
7185 } | |
7186 } | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7187 else if (buf == curbuf) /* "buf" still valid */ |
311 | 7188 { |
7189 /* Mark the buffer as unmodified and free undo info. */ | |
7190 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
|
7191 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
|
7192 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7193 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
|
7194 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
|
7195 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7196 else |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
7197 { |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7198 /* 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
|
7199 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
|
7200 } |
311 | 7201 } |
7202 } | |
7203 vim_free(ea.cmd); | |
7204 | |
837 | 7205 if (savebuf != NULL && buf_valid(savebuf)) |
311 | 7206 wipe_buffer(savebuf, FALSE); |
7207 | |
7208 #ifdef FEAT_DIFF | |
7209 /* Invalidate diff info if necessary. */ | |
837 | 7210 diff_invalidate(curbuf); |
311 | 7211 #endif |
7212 | |
7213 /* Restore the topline and cursor position and check it (lines may | |
7214 * have been removed). */ | |
7215 if (old_topline > curbuf->b_ml.ml_line_count) | |
7216 curwin->w_topline = curbuf->b_ml.ml_line_count; | |
7217 else | |
7218 curwin->w_topline = old_topline; | |
7219 curwin->w_cursor = old_cursor; | |
7220 check_cursor(); | |
7221 update_topline(); | |
7222 #ifdef FEAT_AUTOCMD | |
7223 keep_filetype = FALSE; | |
7224 #endif | |
7225 #ifdef FEAT_FOLDING | |
7226 { | |
1863 | 7227 win_T *wp; |
7228 tabpage_T *tp; | |
311 | 7229 |
7230 /* Update folds unless they are defined manually. */ | |
1863 | 7231 FOR_ALL_TAB_WINDOWS(tp, wp) |
311 | 7232 if (wp->w_buffer == curwin->w_buffer |
7233 && !foldmethodIsManual(wp)) | |
7234 foldUpdateAll(wp); | |
7235 } | |
7236 #endif | |
7237 /* If the mode didn't change and 'readonly' was set, keep the old | |
7238 * value; the user probably used the ":view" command. But don't | |
7239 * reset it, might have had a read error. */ | |
7240 if (orig_mode == curbuf->b_orig_mode) | |
7241 curbuf->b_p_ro |= old_ro; | |
4071 | 7242 |
7243 /* Modelines must override settings done by autocommands. */ | |
7244 do_modelines(0); | |
311 | 7245 } |
7246 | |
7247 /* restore curwin/curbuf and a few other things */ | |
7248 aucmd_restbuf(&aco); | |
7249 /* Careful: autocommands may have made "buf" invalid! */ | |
7250 } | |
7251 | |
7 | 7252 void |
7253 buf_store_time(buf, st, fname) | |
7254 buf_T *buf; | |
7255 struct stat *st; | |
1877 | 7256 char_u *fname UNUSED; |
7 | 7257 { |
7258 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
|
7259 buf->b_orig_size = st->st_size; |
7 | 7260 #ifdef HAVE_ST_MODE |
7261 buf->b_orig_mode = (int)st->st_mode; | |
7262 #else | |
7263 buf->b_orig_mode = mch_getperm(fname); | |
7264 #endif | |
7265 } | |
7266 | |
7267 /* | |
7268 * Adjust the line with missing eol, used for the next write. | |
7269 * Used for do_filter(), when the input lines for the filter are deleted. | |
7270 */ | |
7271 void | |
7272 write_lnum_adjust(offset) | |
7273 linenr_T offset; | |
7274 { | |
2707 | 7275 if (curbuf->b_no_eol_lnum != 0) /* only if there is a missing eol */ |
7276 curbuf->b_no_eol_lnum += offset; | |
7 | 7277 } |
7278 | |
7279 #if defined(TEMPDIRNAMES) || defined(PROTO) | |
7280 static long temp_count = 0; /* Temp filename counter. */ | |
7281 | |
7282 /* | |
7283 * Delete the temp directory and all files it contains. | |
7284 */ | |
7285 void | |
7286 vim_deltempdir() | |
7287 { | |
7288 char_u **files; | |
7289 int file_count; | |
7290 int i; | |
7291 | |
7292 if (vim_tempdir != NULL) | |
7293 { | |
7294 sprintf((char *)NameBuff, "%s*", vim_tempdir); | |
7295 if (gen_expand_wildcards(1, &NameBuff, &file_count, &files, | |
7296 EW_DIR|EW_FILE|EW_SILENT) == OK) | |
7297 { | |
7298 for (i = 0; i < file_count; ++i) | |
7299 mch_remove(files[i]); | |
7300 FreeWild(file_count, files); | |
7301 } | |
7302 gettail(NameBuff)[-1] = NUL; | |
7303 (void)mch_rmdir(NameBuff); | |
7304 | |
7305 vim_free(vim_tempdir); | |
7306 vim_tempdir = NULL; | |
7307 } | |
7308 } | |
7309 #endif | |
7310 | |
2006 | 7311 #ifdef TEMPDIRNAMES |
7 | 7312 /* |
1997 | 7313 * Directory "tempdir" was created. Expand this name to a full path and put |
7314 * it in "vim_tempdir". This avoids that using ":cd" would confuse us. | |
7315 * "tempdir" must be no longer than MAXPATHL. | |
7316 */ | |
7317 static void | |
7318 vim_settempdir(tempdir) | |
7319 char_u *tempdir; | |
7320 { | |
7321 char_u *buf; | |
7322 | |
7323 buf = alloc((unsigned)MAXPATHL + 2); | |
7324 if (buf != NULL) | |
7325 { | |
7326 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL) | |
7327 STRCPY(buf, tempdir); | |
7328 # ifdef __EMX__ | |
7329 if (vim_strchr(buf, '/') != NULL) | |
7330 STRCAT(buf, "/"); | |
7331 else | |
7332 # endif | |
7333 add_pathsep(buf); | |
7334 vim_tempdir = vim_strsave(buf); | |
7335 vim_free(buf); | |
7336 } | |
7337 } | |
2006 | 7338 #endif |
1997 | 7339 |
7340 /* | |
7 | 7341 * vim_tempname(): Return a unique name that can be used for a temp file. |
7342 * | |
7343 * The temp file is NOT created. | |
7344 * | |
7345 * The returned pointer is to allocated memory. | |
7346 * The returned pointer is NULL if no valid name was found. | |
7347 */ | |
7348 char_u * | |
7349 vim_tempname(extra_char) | |
1877 | 7350 int extra_char UNUSED; /* char to use in the name instead of '?' */ |
7 | 7351 { |
7352 #ifdef USE_TMPNAM | |
7353 char_u itmp[L_tmpnam]; /* use tmpnam() */ | |
7354 #else | |
7355 char_u itmp[TEMPNAMELEN]; | |
7356 #endif | |
7357 | |
7358 #ifdef TEMPDIRNAMES | |
7359 static char *(tempdirs[]) = {TEMPDIRNAMES}; | |
7360 int i; | |
7361 # ifndef EEXIST | |
7362 struct stat st; | |
7363 # endif | |
7364 | |
7365 /* | |
7366 * This will create a directory for private use by this instance of Vim. | |
7367 * This is done once, and the same directory is used for all temp files. | |
7368 * This method avoids security problems because of symlink attacks et al. | |
7369 * It's also a bit faster, because we only need to check for an existing | |
7370 * file when creating the directory and not for each temp file. | |
7371 */ | |
7372 if (vim_tempdir == NULL) | |
7373 { | |
7374 /* | |
7375 * Try the entries in TEMPDIRNAMES to create the temp directory. | |
7376 */ | |
1877 | 7377 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i) |
7 | 7378 { |
2047
85da03763130
updated for version 7.2.333
Bram Moolenaar <bram@zimbu.org>
parents:
2028
diff
changeset
|
7379 # ifndef HAVE_MKDTEMP |
1997 | 7380 size_t itmplen; |
7381 long nr; | |
7382 long off; | |
7383 # endif | |
7384 | |
7 | 7385 /* expand $TMP, leave room for "/v1100000/999999999" */ |
7386 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20); | |
7387 if (mch_isdir(itmp)) /* directory exists */ | |
7388 { | |
7389 # ifdef __EMX__ | |
7390 /* If $TMP contains a forward slash (perhaps using bash or | |
7391 * tcsh), don't add a backslash, use a forward slash! | |
7392 * Adding 2 backslashes didn't work. */ | |
7393 if (vim_strchr(itmp, '/') != NULL) | |
7394 STRCAT(itmp, "/"); | |
7395 else | |
7396 # endif | |
7397 add_pathsep(itmp); | |
1997 | 7398 |
7399 # ifdef HAVE_MKDTEMP | |
7400 /* Leave room for filename */ | |
7401 STRCAT(itmp, "vXXXXXX"); | |
7402 if (mkdtemp((char *)itmp) != NULL) | |
7403 vim_settempdir(itmp); | |
7404 # else | |
7 | 7405 /* Get an arbitrary number of up to 6 digits. When it's |
7406 * unlikely that it already exists it will be faster, | |
7407 * otherwise it doesn't matter. The use of mkdir() avoids any | |
7408 * security problems because of the predictable number. */ | |
7409 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
|
7410 itmplen = STRLEN(itmp); |
7 | 7411 |
7412 /* Try up to 10000 different values until we find a name that | |
7413 * doesn't exist. */ | |
7414 for (off = 0; off < 10000L; ++off) | |
7415 { | |
7416 int r; | |
1997 | 7417 # if defined(UNIX) || defined(VMS) |
7 | 7418 mode_t umask_save; |
1997 | 7419 # endif |
7420 | |
7421 sprintf((char *)itmp + itmplen, "v%ld", nr + off); | |
7422 # ifndef EEXIST | |
7 | 7423 /* If mkdir() does not set errno to EEXIST, check for |
7424 * existing file here. There is a race condition then, | |
7425 * although it's fail-safe. */ | |
7426 if (mch_stat((char *)itmp, &st) >= 0) | |
7427 continue; | |
1997 | 7428 # endif |
7429 # if defined(UNIX) || defined(VMS) | |
7 | 7430 /* Make sure the umask doesn't remove the executable bit. |
7431 * "repl" has been reported to use "177". */ | |
7432 umask_save = umask(077); | |
1997 | 7433 # endif |
7 | 7434 r = vim_mkdir(itmp, 0700); |
1997 | 7435 # if defined(UNIX) || defined(VMS) |
7 | 7436 (void)umask(umask_save); |
1997 | 7437 # endif |
7 | 7438 if (r == 0) |
7439 { | |
1997 | 7440 vim_settempdir(itmp); |
7 | 7441 break; |
7442 } | |
1997 | 7443 # ifdef EEXIST |
7 | 7444 /* If the mkdir() didn't fail because the file/dir exists, |
7445 * we probably can't create any dir here, try another | |
7446 * place. */ | |
7447 if (errno != EEXIST) | |
1997 | 7448 # endif |
7 | 7449 break; |
7450 } | |
1997 | 7451 # endif /* HAVE_MKDTEMP */ |
7 | 7452 if (vim_tempdir != NULL) |
7453 break; | |
7454 } | |
7455 } | |
7456 } | |
7457 | |
7458 if (vim_tempdir != NULL) | |
7459 { | |
7460 /* There is no need to check if the file exists, because we own the | |
7461 * directory and nobody else creates a file in it. */ | |
7462 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++); | |
7463 return vim_strsave(itmp); | |
7464 } | |
7465 | |
7466 return NULL; | |
7467 | |
7468 #else /* TEMPDIRNAMES */ | |
7469 | |
7470 # ifdef WIN3264 | |
7471 char szTempFile[_MAX_PATH + 1]; | |
7472 char buf4[4]; | |
7473 char_u *retval; | |
7474 char_u *p; | |
7475 | |
7476 STRCPY(itmp, ""); | |
7477 if (GetTempPath(_MAX_PATH, szTempFile) == 0) | |
2695 | 7478 { |
7479 szTempFile[0] = '.'; /* GetTempPath() failed, use current dir */ | |
7480 szTempFile[1] = NUL; | |
7481 } | |
7 | 7482 strcpy(buf4, "VIM"); |
7483 buf4[2] = extra_char; /* make it "VIa", "VIb", etc. */ | |
7484 if (GetTempFileName(szTempFile, buf4, 0, itmp) == 0) | |
7485 return NULL; | |
7486 /* GetTempFileName() will create the file, we don't want that */ | |
7487 (void)DeleteFile(itmp); | |
7488 | |
7489 /* Backslashes in a temp file name cause problems when filtering with | |
7490 * "sh". NOTE: This also checks 'shellcmdflag' to help those people who | |
7491 * didn't set 'shellslash'. */ | |
7492 retval = vim_strsave(itmp); | |
7493 if (*p_shcf == '-' || p_ssl) | |
7494 for (p = retval; *p; ++p) | |
7495 if (*p == '\\') | |
7496 *p = '/'; | |
7497 return retval; | |
7498 | |
7499 # else /* WIN3264 */ | |
7500 | |
7501 # ifdef USE_TMPNAM | |
2697 | 7502 char_u *p; |
7503 | |
7 | 7504 /* tmpnam() will make its own name */ |
2697 | 7505 p = tmpnam((char *)itmp); |
7506 if (p == NULL || *p == NUL) | |
7 | 7507 return NULL; |
7508 # else | |
7509 char_u *p; | |
7510 | |
7511 # ifdef VMS_TEMPNAM | |
7512 /* mktemp() is not working on VMS. It seems to be | |
7513 * a do-nothing function. Therefore we use tempnam(). | |
7514 */ | |
7515 sprintf((char *)itmp, "VIM%c", extra_char); | |
7516 p = (char_u *)tempnam("tmp:", (char *)itmp); | |
7517 if (p != NULL) | |
7518 { | |
7519 /* VMS will use '.LOG' if we don't explicitly specify an extension, | |
7520 * and VIM will then be unable to find the file later */ | |
7521 STRCPY(itmp, p); | |
7522 STRCAT(itmp, ".txt"); | |
7523 free(p); | |
7524 } | |
7525 else | |
7526 return NULL; | |
7527 # else | |
7528 STRCPY(itmp, TEMPNAME); | |
7529 if ((p = vim_strchr(itmp, '?')) != NULL) | |
7530 *p = extra_char; | |
7531 if (mktemp((char *)itmp) == NULL) | |
7532 return NULL; | |
7533 # endif | |
7534 # endif | |
7535 | |
7536 return vim_strsave(itmp); | |
7537 # endif /* WIN3264 */ | |
7538 #endif /* TEMPDIRNAMES */ | |
7539 } | |
7540 | |
7541 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) | |
7542 /* | |
7543 * Convert all backslashes in fname to forward slashes in-place. | |
7544 */ | |
7545 void | |
7546 forward_slash(fname) | |
7547 char_u *fname; | |
7548 { | |
7549 char_u *p; | |
7550 | |
7551 for (p = fname; *p != NUL; ++p) | |
7552 # ifdef FEAT_MBYTE | |
7553 /* The Big5 encoding can have '\' in the trail byte. */ | |
474 | 7554 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 7555 ++p; |
7556 else | |
7557 # endif | |
7558 if (*p == '\\') | |
7559 *p = '/'; | |
7560 } | |
7561 #endif | |
7562 | |
7563 | |
7564 /* | |
7565 * Code for automatic commands. | |
7566 * | |
7567 * Only included when "FEAT_AUTOCMD" has been defined. | |
7568 */ | |
7569 | |
7570 #if defined(FEAT_AUTOCMD) || defined(PROTO) | |
7571 | |
7572 /* | |
7573 * The autocommands are stored in a list for each event. | |
7574 * Autocommands for the same pattern, that are consecutive, are joined | |
7575 * together, to avoid having to match the pattern too often. | |
7576 * The result is an array of Autopat lists, which point to AutoCmd lists: | |
7577 * | |
7578 * first_autopat[0] --> Autopat.next --> Autopat.next --> NULL | |
7579 * Autopat.cmds Autopat.cmds | |
7580 * | | | |
7581 * V V | |
7582 * AutoCmd.next AutoCmd.next | |
7583 * | | | |
7584 * V V | |
7585 * AutoCmd.next NULL | |
7586 * | | |
7587 * V | |
7588 * NULL | |
7589 * | |
7590 * first_autopat[1] --> Autopat.next --> NULL | |
7591 * Autopat.cmds | |
7592 * | | |
7593 * V | |
7594 * AutoCmd.next | |
7595 * | | |
7596 * V | |
7597 * NULL | |
7598 * etc. | |
7599 * | |
7600 * The order of AutoCmds is important, this is the order in which they were | |
7601 * defined and will have to be executed. | |
7602 */ | |
7603 typedef struct AutoCmd | |
7604 { | |
7605 char_u *cmd; /* The command to be executed (NULL | |
7606 when command has been removed) */ | |
7607 char nested; /* If autocommands nest here */ | |
7608 char last; /* last command in list */ | |
7609 #ifdef FEAT_EVAL | |
7610 scid_T scriptID; /* script ID where defined */ | |
7611 #endif | |
7612 struct AutoCmd *next; /* Next AutoCmd in list */ | |
7613 } AutoCmd; | |
7614 | |
7615 typedef struct AutoPat | |
7616 { | |
7617 int group; /* group ID */ | |
7618 char_u *pat; /* pattern as typed (NULL when pattern | |
7619 has been removed) */ | |
7620 int patlen; /* strlen() of pat */ | |
153 | 7621 regprog_T *reg_prog; /* compiled regprog for pattern */ |
7 | 7622 char allow_dirs; /* Pattern may match whole path */ |
7623 char last; /* last pattern for apply_autocmds() */ | |
7624 AutoCmd *cmds; /* list of commands to do */ | |
7625 struct AutoPat *next; /* next AutoPat in AutoPat list */ | |
40 | 7626 int buflocal_nr; /* !=0 for buffer-local AutoPat */ |
7 | 7627 } AutoPat; |
7628 | |
7629 static struct event_name | |
7630 { | |
7631 char *name; /* event name */ | |
661 | 7632 event_T event; /* event number */ |
7 | 7633 } event_names[] = |
7634 { | |
7635 {"BufAdd", EVENT_BUFADD}, | |
7636 {"BufCreate", EVENT_BUFADD}, | |
7637 {"BufDelete", EVENT_BUFDELETE}, | |
7638 {"BufEnter", EVENT_BUFENTER}, | |
7639 {"BufFilePost", EVENT_BUFFILEPOST}, | |
7640 {"BufFilePre", EVENT_BUFFILEPRE}, | |
7641 {"BufHidden", EVENT_BUFHIDDEN}, | |
7642 {"BufLeave", EVENT_BUFLEAVE}, | |
7643 {"BufNew", EVENT_BUFNEW}, | |
7644 {"BufNewFile", EVENT_BUFNEWFILE}, | |
7645 {"BufRead", EVENT_BUFREADPOST}, | |
7646 {"BufReadCmd", EVENT_BUFREADCMD}, | |
7647 {"BufReadPost", EVENT_BUFREADPOST}, | |
7648 {"BufReadPre", EVENT_BUFREADPRE}, | |
7649 {"BufUnload", EVENT_BUFUNLOAD}, | |
7650 {"BufWinEnter", EVENT_BUFWINENTER}, | |
7651 {"BufWinLeave", EVENT_BUFWINLEAVE}, | |
7652 {"BufWipeout", EVENT_BUFWIPEOUT}, | |
7653 {"BufWrite", EVENT_BUFWRITEPRE}, | |
7654 {"BufWritePost", EVENT_BUFWRITEPOST}, | |
7655 {"BufWritePre", EVENT_BUFWRITEPRE}, | |
7656 {"BufWriteCmd", EVENT_BUFWRITECMD}, | |
7657 {"CmdwinEnter", EVENT_CMDWINENTER}, | |
7658 {"CmdwinLeave", EVENT_CMDWINLEAVE}, | |
12 | 7659 {"ColorScheme", EVENT_COLORSCHEME}, |
3676 | 7660 {"CompleteDone", EVENT_COMPLETEDONE}, |
661 | 7661 {"CursorHold", EVENT_CURSORHOLD}, |
7662 {"CursorHoldI", EVENT_CURSORHOLDI}, | |
7663 {"CursorMoved", EVENT_CURSORMOVED}, | |
7664 {"CursorMovedI", EVENT_CURSORMOVEDI}, | |
7 | 7665 {"EncodingChanged", EVENT_ENCODINGCHANGED}, |
7666 {"FileEncoding", EVENT_ENCODINGCHANGED}, | |
7667 {"FileAppendPost", EVENT_FILEAPPENDPOST}, | |
7668 {"FileAppendPre", EVENT_FILEAPPENDPRE}, | |
7669 {"FileAppendCmd", EVENT_FILEAPPENDCMD}, | |
7670 {"FileChangedShell",EVENT_FILECHANGEDSHELL}, | |
765 | 7671 {"FileChangedShellPost",EVENT_FILECHANGEDSHELLPOST}, |
7 | 7672 {"FileChangedRO", EVENT_FILECHANGEDRO}, |
7673 {"FileReadPost", EVENT_FILEREADPOST}, | |
7674 {"FileReadPre", EVENT_FILEREADPRE}, | |
7675 {"FileReadCmd", EVENT_FILEREADCMD}, | |
7676 {"FileType", EVENT_FILETYPE}, | |
7677 {"FileWritePost", EVENT_FILEWRITEPOST}, | |
7678 {"FileWritePre", EVENT_FILEWRITEPRE}, | |
7679 {"FileWriteCmd", EVENT_FILEWRITECMD}, | |
7680 {"FilterReadPost", EVENT_FILTERREADPOST}, | |
7681 {"FilterReadPre", EVENT_FILTERREADPRE}, | |
7682 {"FilterWritePost", EVENT_FILTERWRITEPOST}, | |
7683 {"FilterWritePre", EVENT_FILTERWRITEPRE}, | |
7684 {"FocusGained", EVENT_FOCUSGAINED}, | |
7685 {"FocusLost", EVENT_FOCUSLOST}, | |
7686 {"FuncUndefined", EVENT_FUNCUNDEFINED}, | |
7687 {"GUIEnter", EVENT_GUIENTER}, | |
946 | 7688 {"GUIFailed", EVENT_GUIFAILED}, |
11 | 7689 {"InsertChange", EVENT_INSERTCHANGE}, |
7690 {"InsertEnter", EVENT_INSERTENTER}, | |
7691 {"InsertLeave", EVENT_INSERTLEAVE}, | |
2845 | 7692 {"InsertCharPre", EVENT_INSERTCHARPRE}, |
434 | 7693 {"MenuPopup", EVENT_MENUPOPUP}, |
161 | 7694 {"QuickFixCmdPost", EVENT_QUICKFIXCMDPOST}, |
7695 {"QuickFixCmdPre", EVENT_QUICKFIXCMDPRE}, | |
3568 | 7696 {"QuitPre", EVENT_QUITPRE}, |
7 | 7697 {"RemoteReply", EVENT_REMOTEREPLY}, |
574 | 7698 {"SessionLoadPost", EVENT_SESSIONLOADPOST}, |
723 | 7699 {"ShellCmdPost", EVENT_SHELLCMDPOST}, |
7700 {"ShellFilterPost", EVENT_SHELLFILTERPOST}, | |
715 | 7701 {"SourcePre", EVENT_SOURCEPRE}, |
1061 | 7702 {"SourceCmd", EVENT_SOURCECMD}, |
649 | 7703 {"SpellFileMissing",EVENT_SPELLFILEMISSING}, |
7 | 7704 {"StdinReadPost", EVENT_STDINREADPOST}, |
7705 {"StdinReadPre", EVENT_STDINREADPRE}, | |
674 | 7706 {"SwapExists", EVENT_SWAPEXISTS}, |
7 | 7707 {"Syntax", EVENT_SYNTAX}, |
676 | 7708 {"TabEnter", EVENT_TABENTER}, |
7709 {"TabLeave", EVENT_TABLEAVE}, | |
7 | 7710 {"TermChanged", EVENT_TERMCHANGED}, |
7711 {"TermResponse", EVENT_TERMRESPONSE}, | |
4232 | 7712 {"TextChanged", EVENT_TEXTCHANGED}, |
7713 {"TextChangedI", EVENT_TEXTCHANGEDI}, | |
7 | 7714 {"User", EVENT_USER}, |
7715 {"VimEnter", EVENT_VIMENTER}, | |
7716 {"VimLeave", EVENT_VIMLEAVE}, | |
7717 {"VimLeavePre", EVENT_VIMLEAVEPRE}, | |
7718 {"WinEnter", EVENT_WINENTER}, | |
7719 {"WinLeave", EVENT_WINLEAVE}, | |
765 | 7720 {"VimResized", EVENT_VIMRESIZED}, |
661 | 7721 {NULL, (event_T)0} |
7 | 7722 }; |
7723 | |
7724 static AutoPat *first_autopat[NUM_EVENTS] = | |
7725 { | |
7726 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7727 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7728 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7729 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
18 | 7730 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
7731 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL | |
7 | 7732 }; |
7733 | |
7734 /* | |
7735 * struct used to keep status while executing autocommands for an event. | |
7736 */ | |
7737 typedef struct AutoPatCmd | |
7738 { | |
7739 AutoPat *curpat; /* next AutoPat to examine */ | |
7740 AutoCmd *nextcmd; /* next AutoCmd to execute */ | |
7741 int group; /* group being used */ | |
7742 char_u *fname; /* fname to match with */ | |
7743 char_u *sfname; /* sfname to match with */ | |
7744 char_u *tail; /* tail of fname */ | |
661 | 7745 event_T event; /* current event */ |
40 | 7746 int arg_bufnr; /* initially equal to <abuf>, set to zero when |
7747 buf is deleted */ | |
7748 struct AutoPatCmd *next; /* chain of active apc-s for auto-invalidation*/ | |
7 | 7749 } AutoPatCmd; |
7750 | |
297 | 7751 static AutoPatCmd *active_apc_list = NULL; /* stack of active autocommands */ |
40 | 7752 |
7 | 7753 /* |
7754 * augroups stores a list of autocmd group names. | |
7755 */ | |
297 | 7756 static garray_T augroups = {0, 0, sizeof(char_u *), 10, NULL}; |
7 | 7757 #define AUGROUP_NAME(i) (((char_u **)augroups.ga_data)[i]) |
7758 | |
7759 /* | |
7760 * The ID of the current group. Group 0 is the default one. | |
7761 */ | |
7762 static int current_augroup = AUGROUP_DEFAULT; | |
7763 | |
7764 static int au_need_clean = FALSE; /* need to delete marked patterns */ | |
7765 | |
661 | 7766 static void show_autocmd __ARGS((AutoPat *ap, event_T event)); |
7 | 7767 static void au_remove_pat __ARGS((AutoPat *ap)); |
7768 static void au_remove_cmds __ARGS((AutoPat *ap)); | |
7769 static void au_cleanup __ARGS((void)); | |
7770 static int au_new_group __ARGS((char_u *name)); | |
7771 static void au_del_group __ARGS((char_u *name)); | |
661 | 7772 static event_T event_name2nr __ARGS((char_u *start, char_u **end)); |
7773 static char_u *event_nr2name __ARGS((event_T event)); | |
7 | 7774 static char_u *find_end_event __ARGS((char_u *arg, int have_group)); |
661 | 7775 static int event_ignored __ARGS((event_T event)); |
7 | 7776 static int au_get_grouparg __ARGS((char_u **argp)); |
661 | 7777 static int do_autocmd_event __ARGS((event_T event, char_u *pat, int nested, char_u *cmd, int forceit, int group)); |
7778 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 | 7779 static void auto_next_pat __ARGS((AutoPatCmd *apc, int stop_at_last)); |
7780 | |
40 | 7781 |
661 | 7782 static event_T last_event; |
7 | 7783 static int last_group; |
1410 | 7784 static int autocmd_blocked = 0; /* block all autocmds */ |
7 | 7785 |
7786 /* | |
7787 * Show the autocommands for one AutoPat. | |
7788 */ | |
7789 static void | |
7790 show_autocmd(ap, event) | |
7791 AutoPat *ap; | |
661 | 7792 event_T event; |
7 | 7793 { |
7794 AutoCmd *ac; | |
7795 | |
7796 /* Check for "got_int" (here and at various places below), which is set | |
7797 * when "q" has been hit for the "--more--" prompt */ | |
7798 if (got_int) | |
7799 return; | |
7800 if (ap->pat == NULL) /* pattern has been removed */ | |
7801 return; | |
7802 | |
7803 msg_putchar('\n'); | |
7804 if (got_int) | |
7805 return; | |
7806 if (event != last_event || ap->group != last_group) | |
7807 { | |
7808 if (ap->group != AUGROUP_DEFAULT) | |
7809 { | |
7810 if (AUGROUP_NAME(ap->group) == NULL) | |
7811 msg_puts_attr((char_u *)_("--Deleted--"), hl_attr(HLF_E)); | |
7812 else | |
7813 msg_puts_attr(AUGROUP_NAME(ap->group), hl_attr(HLF_T)); | |
7814 msg_puts((char_u *)" "); | |
7815 } | |
7816 msg_puts_attr(event_nr2name(event), hl_attr(HLF_T)); | |
7817 last_event = event; | |
7818 last_group = ap->group; | |
7819 msg_putchar('\n'); | |
7820 if (got_int) | |
7821 return; | |
7822 } | |
7823 msg_col = 4; | |
7824 msg_outtrans(ap->pat); | |
7825 | |
7826 for (ac = ap->cmds; ac != NULL; ac = ac->next) | |
7827 { | |
7828 if (ac->cmd != NULL) /* skip removed commands */ | |
7829 { | |
7830 if (msg_col >= 14) | |
7831 msg_putchar('\n'); | |
7832 msg_col = 14; | |
7833 if (got_int) | |
7834 return; | |
7835 msg_outtrans(ac->cmd); | |
500 | 7836 #ifdef FEAT_EVAL |
7837 if (p_verbose > 0) | |
7838 last_set_msg(ac->scriptID); | |
7839 #endif | |
7 | 7840 if (got_int) |
7841 return; | |
7842 if (ac->next != NULL) | |
7843 { | |
7844 msg_putchar('\n'); | |
7845 if (got_int) | |
7846 return; | |
7847 } | |
7848 } | |
7849 } | |
7850 } | |
7851 | |
7852 /* | |
7853 * Mark an autocommand pattern for deletion. | |
7854 */ | |
7855 static void | |
7856 au_remove_pat(ap) | |
7857 AutoPat *ap; | |
7858 { | |
7859 vim_free(ap->pat); | |
7860 ap->pat = NULL; | |
40 | 7861 ap->buflocal_nr = -1; |
7 | 7862 au_need_clean = TRUE; |
7863 } | |
7864 | |
7865 /* | |
7866 * Mark all commands for a pattern for deletion. | |
7867 */ | |
7868 static void | |
7869 au_remove_cmds(ap) | |
7870 AutoPat *ap; | |
7871 { | |
7872 AutoCmd *ac; | |
7873 | |
7874 for (ac = ap->cmds; ac != NULL; ac = ac->next) | |
7875 { | |
7876 vim_free(ac->cmd); | |
7877 ac->cmd = NULL; | |
7878 } | |
7879 au_need_clean = TRUE; | |
7880 } | |
7881 | |
7882 /* | |
7883 * Cleanup autocommands and patterns that have been deleted. | |
7884 * This is only done when not executing autocommands. | |
7885 */ | |
7886 static void | |
7887 au_cleanup() | |
7888 { | |
7889 AutoPat *ap, **prev_ap; | |
7890 AutoCmd *ac, **prev_ac; | |
661 | 7891 event_T event; |
7 | 7892 |
7893 if (autocmd_busy || !au_need_clean) | |
7894 return; | |
7895 | |
7896 /* loop over all events */ | |
661 | 7897 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
7898 event = (event_T)((int)event + 1)) | |
7 | 7899 { |
7900 /* loop over all autocommand patterns */ | |
7901 prev_ap = &(first_autopat[(int)event]); | |
7902 for (ap = *prev_ap; ap != NULL; ap = *prev_ap) | |
7903 { | |
7904 /* loop over all commands for this pattern */ | |
7905 prev_ac = &(ap->cmds); | |
7906 for (ac = *prev_ac; ac != NULL; ac = *prev_ac) | |
7907 { | |
7908 /* remove the command if the pattern is to be deleted or when | |
7909 * the command has been marked for deletion */ | |
7910 if (ap->pat == NULL || ac->cmd == NULL) | |
7911 { | |
7912 *prev_ac = ac->next; | |
7913 vim_free(ac->cmd); | |
7914 vim_free(ac); | |
7915 } | |
7916 else | |
7917 prev_ac = &(ac->next); | |
7918 } | |
7919 | |
7920 /* remove the pattern if it has been marked for deletion */ | |
7921 if (ap->pat == NULL) | |
7922 { | |
7923 *prev_ap = ap->next; | |
153 | 7924 vim_free(ap->reg_prog); |
7 | 7925 vim_free(ap); |
7926 } | |
7927 else | |
7928 prev_ap = &(ap->next); | |
7929 } | |
7930 } | |
7931 | |
7932 au_need_clean = FALSE; | |
7933 } | |
7934 | |
7935 /* | |
40 | 7936 * Called when buffer is freed, to remove/invalidate related buffer-local |
7937 * autocmds. | |
7938 */ | |
7939 void | |
7940 aubuflocal_remove(buf) | |
7941 buf_T *buf; | |
7942 { | |
7943 AutoPat *ap; | |
661 | 7944 event_T event; |
40 | 7945 AutoPatCmd *apc; |
7946 | |
7947 /* invalidate currently executing autocommands */ | |
7948 for (apc = active_apc_list; apc; apc = apc->next) | |
7949 if (buf->b_fnum == apc->arg_bufnr) | |
7950 apc->arg_bufnr = 0; | |
7951 | |
7952 /* invalidate buflocals looping through events */ | |
661 | 7953 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
7954 event = (event_T)((int)event + 1)) | |
40 | 7955 /* loop over all autocommand patterns */ |
7956 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
7957 if (ap->buflocal_nr == buf->b_fnum) | |
7958 { | |
7959 au_remove_pat(ap); | |
7960 if (p_verbose >= 6) | |
292 | 7961 { |
7962 verbose_enter(); | |
40 | 7963 smsg((char_u *) |
7964 _("auto-removing autocommand: %s <buffer=%d>"), | |
7965 event_nr2name(event), buf->b_fnum); | |
292 | 7966 verbose_leave(); |
7967 } | |
40 | 7968 } |
7969 au_cleanup(); | |
7970 } | |
7971 | |
7972 /* | |
7 | 7973 * Add an autocmd group name. |
7974 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. | |
7975 */ | |
7976 static int | |
7977 au_new_group(name) | |
7978 char_u *name; | |
7979 { | |
7980 int i; | |
7981 | |
7982 i = au_find_group(name); | |
7983 if (i == AUGROUP_ERROR) /* the group doesn't exist yet, add it */ | |
7984 { | |
7985 /* First try using a free entry. */ | |
7986 for (i = 0; i < augroups.ga_len; ++i) | |
7987 if (AUGROUP_NAME(i) == NULL) | |
7988 break; | |
7989 if (i == augroups.ga_len && ga_grow(&augroups, 1) == FAIL) | |
7990 return AUGROUP_ERROR; | |
7991 | |
7992 AUGROUP_NAME(i) = vim_strsave(name); | |
7993 if (AUGROUP_NAME(i) == NULL) | |
7994 return AUGROUP_ERROR; | |
7995 if (i == augroups.ga_len) | |
7996 ++augroups.ga_len; | |
7997 } | |
7998 | |
7999 return i; | |
8000 } | |
8001 | |
8002 static void | |
8003 au_del_group(name) | |
8004 char_u *name; | |
8005 { | |
8006 int i; | |
8007 | |
8008 i = au_find_group(name); | |
8009 if (i == AUGROUP_ERROR) /* the group doesn't exist */ | |
8010 EMSG2(_("E367: No such group: \"%s\""), name); | |
8011 else | |
8012 { | |
8013 vim_free(AUGROUP_NAME(i)); | |
8014 AUGROUP_NAME(i) = NULL; | |
8015 } | |
8016 } | |
8017 | |
8018 /* | |
8019 * Find the ID of an autocmd group name. | |
8020 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. | |
8021 */ | |
8022 static int | |
8023 au_find_group(name) | |
8024 char_u *name; | |
8025 { | |
8026 int i; | |
8027 | |
8028 for (i = 0; i < augroups.ga_len; ++i) | |
8029 if (AUGROUP_NAME(i) != NULL && STRCMP(AUGROUP_NAME(i), name) == 0) | |
8030 return i; | |
8031 return AUGROUP_ERROR; | |
8032 } | |
8033 | |
461 | 8034 /* |
8035 * Return TRUE if augroup "name" exists. | |
8036 */ | |
8037 int | |
8038 au_has_group(name) | |
8039 char_u *name; | |
8040 { | |
8041 return au_find_group(name) != AUGROUP_ERROR; | |
8042 } | |
8043 | |
7 | 8044 /* |
8045 * ":augroup {name}". | |
8046 */ | |
8047 void | |
8048 do_augroup(arg, del_group) | |
8049 char_u *arg; | |
8050 int del_group; | |
8051 { | |
8052 int i; | |
8053 | |
8054 if (del_group) | |
8055 { | |
8056 if (*arg == NUL) | |
8057 EMSG(_(e_argreq)); | |
8058 else | |
8059 au_del_group(arg); | |
8060 } | |
8061 else if (STRICMP(arg, "end") == 0) /* ":aug end": back to group 0 */ | |
8062 current_augroup = AUGROUP_DEFAULT; | |
8063 else if (*arg) /* ":aug xxx": switch to group xxx */ | |
8064 { | |
8065 i = au_new_group(arg); | |
8066 if (i != AUGROUP_ERROR) | |
8067 current_augroup = i; | |
8068 } | |
8069 else /* ":aug": list the group names */ | |
8070 { | |
8071 msg_start(); | |
8072 for (i = 0; i < augroups.ga_len; ++i) | |
8073 { | |
8074 if (AUGROUP_NAME(i) != NULL) | |
8075 { | |
8076 msg_puts(AUGROUP_NAME(i)); | |
8077 msg_puts((char_u *)" "); | |
8078 } | |
8079 } | |
8080 msg_clr_eos(); | |
8081 msg_end(); | |
8082 } | |
8083 } | |
8084 | |
355 | 8085 #if defined(EXITFREE) || defined(PROTO) |
8086 void | |
8087 free_all_autocmds() | |
8088 { | |
8089 for (current_augroup = -1; current_augroup < augroups.ga_len; | |
8090 ++current_augroup) | |
8091 do_autocmd((char_u *)"", TRUE); | |
8092 ga_clear_strings(&augroups); | |
8093 } | |
8094 #endif | |
8095 | |
7 | 8096 /* |
8097 * Return the event number for event name "start". | |
8098 * Return NUM_EVENTS if the event name was not found. | |
8099 * Return a pointer to the next event name in "end". | |
8100 */ | |
661 | 8101 static event_T |
7 | 8102 event_name2nr(start, end) |
8103 char_u *start; | |
8104 char_u **end; | |
8105 { | |
8106 char_u *p; | |
8107 int i; | |
8108 int len; | |
8109 | |
8110 /* the event name ends with end of line, a blank or a comma */ | |
8111 for (p = start; *p && !vim_iswhite(*p) && *p != ','; ++p) | |
8112 ; | |
8113 for (i = 0; event_names[i].name != NULL; ++i) | |
8114 { | |
8115 len = (int)STRLEN(event_names[i].name); | |
8116 if (len == p - start && STRNICMP(event_names[i].name, start, len) == 0) | |
8117 break; | |
8118 } | |
8119 if (*p == ',') | |
8120 ++p; | |
8121 *end = p; | |
8122 if (event_names[i].name == NULL) | |
8123 return NUM_EVENTS; | |
8124 return event_names[i].event; | |
8125 } | |
8126 | |
8127 /* | |
8128 * Return the name for event "event". | |
8129 */ | |
8130 static char_u * | |
8131 event_nr2name(event) | |
661 | 8132 event_T event; |
7 | 8133 { |
8134 int i; | |
8135 | |
8136 for (i = 0; event_names[i].name != NULL; ++i) | |
8137 if (event_names[i].event == event) | |
8138 return (char_u *)event_names[i].name; | |
8139 return (char_u *)"Unknown"; | |
8140 } | |
8141 | |
8142 /* | |
8143 * Scan over the events. "*" stands for all events. | |
8144 */ | |
8145 static char_u * | |
8146 find_end_event(arg, have_group) | |
8147 char_u *arg; | |
8148 int have_group; /* TRUE when group name was found */ | |
8149 { | |
8150 char_u *pat; | |
8151 char_u *p; | |
8152 | |
8153 if (*arg == '*') | |
8154 { | |
8155 if (arg[1] && !vim_iswhite(arg[1])) | |
8156 { | |
8157 EMSG2(_("E215: Illegal character after *: %s"), arg); | |
8158 return NULL; | |
8159 } | |
8160 pat = arg + 1; | |
8161 } | |
8162 else | |
8163 { | |
8164 for (pat = arg; *pat && !vim_iswhite(*pat); pat = p) | |
8165 { | |
8166 if ((int)event_name2nr(pat, &p) >= (int)NUM_EVENTS) | |
8167 { | |
8168 if (have_group) | |
8169 EMSG2(_("E216: No such event: %s"), pat); | |
8170 else | |
8171 EMSG2(_("E216: No such group or event: %s"), pat); | |
8172 return NULL; | |
8173 } | |
8174 } | |
8175 } | |
8176 return pat; | |
8177 } | |
8178 | |
8179 /* | |
8180 * Return TRUE if "event" is included in 'eventignore'. | |
8181 */ | |
8182 static int | |
8183 event_ignored(event) | |
661 | 8184 event_T event; |
7 | 8185 { |
8186 char_u *p = p_ei; | |
8187 | |
844 | 8188 while (*p != NUL) |
8189 { | |
8190 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) | |
8191 return TRUE; | |
7 | 8192 if (event_name2nr(p, &p) == event) |
8193 return TRUE; | |
844 | 8194 } |
7 | 8195 |
8196 return FALSE; | |
8197 } | |
8198 | |
8199 /* | |
8200 * Return OK when the contents of p_ei is valid, FAIL otherwise. | |
8201 */ | |
8202 int | |
8203 check_ei() | |
8204 { | |
8205 char_u *p = p_ei; | |
8206 | |
8207 while (*p) | |
844 | 8208 { |
8209 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) | |
8210 { | |
8211 p += 3; | |
8212 if (*p == ',') | |
8213 ++p; | |
8214 } | |
8215 else if (event_name2nr(p, &p) == NUM_EVENTS) | |
7 | 8216 return FAIL; |
844 | 8217 } |
7 | 8218 |
8219 return OK; | |
8220 } | |
8221 | |
123 | 8222 # if defined(FEAT_SYN_HL) || defined(PROTO) |
8223 | |
8224 /* | |
8225 * Add "what" to 'eventignore' to skip loading syntax highlighting for every | |
8226 * buffer loaded into the window. "what" must start with a comma. | |
8227 * Returns the old value of 'eventignore' in allocated memory. | |
8228 */ | |
8229 char_u * | |
8230 au_event_disable(what) | |
8231 char *what; | |
8232 { | |
8233 char_u *new_ei; | |
8234 char_u *save_ei; | |
8235 | |
8236 save_ei = vim_strsave(p_ei); | |
8237 if (save_ei != NULL) | |
8238 { | |
557 | 8239 new_ei = vim_strnsave(p_ei, (int)(STRLEN(p_ei) + STRLEN(what))); |
123 | 8240 if (new_ei != NULL) |
8241 { | |
2095
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8242 if (*what == ',' && *p_ei == NUL) |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8243 STRCPY(new_ei, what + 1); |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8244 else |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8245 STRCAT(new_ei, what); |
694 | 8246 set_string_option_direct((char_u *)"ei", -1, new_ei, |
8247 OPT_FREE, SID_NONE); | |
123 | 8248 vim_free(new_ei); |
8249 } | |
8250 } | |
8251 return save_ei; | |
8252 } | |
8253 | |
8254 void | |
8255 au_event_restore(old_ei) | |
8256 char_u *old_ei; | |
8257 { | |
8258 if (old_ei != NULL) | |
8259 { | |
694 | 8260 set_string_option_direct((char_u *)"ei", -1, old_ei, |
8261 OPT_FREE, SID_NONE); | |
123 | 8262 vim_free(old_ei); |
8263 } | |
8264 } | |
8265 # endif /* FEAT_SYN_HL */ | |
8266 | |
7 | 8267 /* |
8268 * do_autocmd() -- implements the :autocmd command. Can be used in the | |
8269 * following ways: | |
8270 * | |
8271 * :autocmd <event> <pat> <cmd> Add <cmd> to the list of commands that | |
8272 * will be automatically executed for <event> | |
8273 * when editing a file matching <pat>, in | |
8274 * the current group. | |
8275 * :autocmd <event> <pat> Show the auto-commands associated with | |
8276 * <event> and <pat>. | |
8277 * :autocmd <event> Show the auto-commands associated with | |
8278 * <event>. | |
8279 * :autocmd Show all auto-commands. | |
8280 * :autocmd! <event> <pat> <cmd> Remove all auto-commands associated with | |
8281 * <event> and <pat>, and add the command | |
8282 * <cmd>, for the current group. | |
8283 * :autocmd! <event> <pat> Remove all auto-commands associated with | |
8284 * <event> and <pat> for the current group. | |
8285 * :autocmd! <event> Remove all auto-commands associated with | |
8286 * <event> for the current group. | |
8287 * :autocmd! Remove ALL auto-commands for the current | |
8288 * group. | |
8289 * | |
8290 * Multiple events and patterns may be given separated by commas. Here are | |
8291 * some examples: | |
8292 * :autocmd bufread,bufenter *.c,*.h set tw=0 smartindent noic | |
8293 * :autocmd bufleave * set tw=79 nosmartindent ic infercase | |
8294 * | |
8295 * :autocmd * *.c show all autocommands for *.c files. | |
609 | 8296 * |
8297 * Mostly a {group} argument can optionally appear before <event>. | |
7 | 8298 */ |
8299 void | |
8300 do_autocmd(arg, forceit) | |
8301 char_u *arg; | |
8302 int forceit; | |
8303 { | |
8304 char_u *pat; | |
8305 char_u *envpat = NULL; | |
8306 char_u *cmd; | |
661 | 8307 event_T event; |
7 | 8308 int need_free = FALSE; |
8309 int nested = FALSE; | |
8310 int group; | |
8311 | |
8312 /* | |
8313 * Check for a legal group name. If not, use AUGROUP_ALL. | |
8314 */ | |
8315 group = au_get_grouparg(&arg); | |
8316 if (arg == NULL) /* out of memory */ | |
8317 return; | |
8318 | |
8319 /* | |
8320 * Scan over the events. | |
8321 * If we find an illegal name, return here, don't do anything. | |
8322 */ | |
8323 pat = find_end_event(arg, group != AUGROUP_ALL); | |
8324 if (pat == NULL) | |
8325 return; | |
8326 | |
8327 /* | |
8328 * Scan over the pattern. Put a NUL at the end. | |
8329 */ | |
8330 pat = skipwhite(pat); | |
8331 cmd = pat; | |
8332 while (*cmd && (!vim_iswhite(*cmd) || cmd[-1] == '\\')) | |
8333 cmd++; | |
8334 if (*cmd) | |
8335 *cmd++ = NUL; | |
8336 | |
8337 /* Expand environment variables in the pattern. Set 'shellslash', we want | |
8338 * forward slashes here. */ | |
8339 if (vim_strchr(pat, '$') != NULL || vim_strchr(pat, '~') != NULL) | |
8340 { | |
8341 #ifdef BACKSLASH_IN_FILENAME | |
8342 int p_ssl_save = p_ssl; | |
8343 | |
8344 p_ssl = TRUE; | |
8345 #endif | |
8346 envpat = expand_env_save(pat); | |
8347 #ifdef BACKSLASH_IN_FILENAME | |
8348 p_ssl = p_ssl_save; | |
8349 #endif | |
8350 if (envpat != NULL) | |
8351 pat = envpat; | |
8352 } | |
8353 | |
8354 /* | |
8355 * Check for "nested" flag. | |
8356 */ | |
8357 cmd = skipwhite(cmd); | |
8358 if (*cmd != NUL && STRNCMP(cmd, "nested", 6) == 0 && vim_iswhite(cmd[6])) | |
8359 { | |
8360 nested = TRUE; | |
8361 cmd = skipwhite(cmd + 6); | |
8362 } | |
8363 | |
8364 /* | |
8365 * Find the start of the commands. | |
8366 * Expand <sfile> in it. | |
8367 */ | |
8368 if (*cmd != NUL) | |
8369 { | |
8370 cmd = expand_sfile(cmd); | |
8371 if (cmd == NULL) /* some error */ | |
8372 return; | |
8373 need_free = TRUE; | |
8374 } | |
8375 | |
8376 /* | |
8377 * Print header when showing autocommands. | |
8378 */ | |
8379 if (!forceit && *cmd == NUL) | |
8380 { | |
8381 /* Highlight title */ | |
8382 MSG_PUTS_TITLE(_("\n--- Auto-Commands ---")); | |
8383 } | |
8384 | |
8385 /* | |
8386 * Loop over the events. | |
8387 */ | |
661 | 8388 last_event = (event_T)-1; /* for listing the event name */ |
7 | 8389 last_group = AUGROUP_ERROR; /* for listing the group name */ |
8390 if (*arg == '*' || *arg == NUL) | |
8391 { | |
661 | 8392 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
8393 event = (event_T)((int)event + 1)) | |
7 | 8394 if (do_autocmd_event(event, pat, |
8395 nested, cmd, forceit, group) == FAIL) | |
8396 break; | |
8397 } | |
8398 else | |
8399 { | |
8400 while (*arg && !vim_iswhite(*arg)) | |
8401 if (do_autocmd_event(event_name2nr(arg, &arg), pat, | |
8402 nested, cmd, forceit, group) == FAIL) | |
8403 break; | |
8404 } | |
8405 | |
8406 if (need_free) | |
8407 vim_free(cmd); | |
8408 vim_free(envpat); | |
8409 } | |
8410 | |
8411 /* | |
8412 * Find the group ID in a ":autocmd" or ":doautocmd" argument. | |
8413 * The "argp" argument is advanced to the following argument. | |
8414 * | |
8415 * Returns the group ID, AUGROUP_ERROR for error (out of memory). | |
8416 */ | |
8417 static int | |
8418 au_get_grouparg(argp) | |
8419 char_u **argp; | |
8420 { | |
8421 char_u *group_name; | |
8422 char_u *p; | |
8423 char_u *arg = *argp; | |
8424 int group = AUGROUP_ALL; | |
8425 | |
8426 p = skiptowhite(arg); | |
8427 if (p > arg) | |
8428 { | |
8429 group_name = vim_strnsave(arg, (int)(p - arg)); | |
8430 if (group_name == NULL) /* out of memory */ | |
8431 return AUGROUP_ERROR; | |
8432 group = au_find_group(group_name); | |
8433 if (group == AUGROUP_ERROR) | |
8434 group = AUGROUP_ALL; /* no match, use all groups */ | |
8435 else | |
8436 *argp = skipwhite(p); /* match, skip over group name */ | |
8437 vim_free(group_name); | |
8438 } | |
8439 return group; | |
8440 } | |
8441 | |
8442 /* | |
8443 * do_autocmd() for one event. | |
8444 * If *pat == NUL do for all patterns. | |
8445 * If *cmd == NUL show entries. | |
8446 * If forceit == TRUE delete entries. | |
8447 * If group is not AUGROUP_ALL, only use this group. | |
8448 */ | |
8449 static int | |
8450 do_autocmd_event(event, pat, nested, cmd, forceit, group) | |
661 | 8451 event_T event; |
7 | 8452 char_u *pat; |
8453 int nested; | |
8454 char_u *cmd; | |
8455 int forceit; | |
8456 int group; | |
8457 { | |
8458 AutoPat *ap; | |
8459 AutoPat **prev_ap; | |
8460 AutoCmd *ac; | |
8461 AutoCmd **prev_ac; | |
8462 int brace_level; | |
8463 char_u *endpat; | |
8464 int findgroup; | |
8465 int allgroups; | |
8466 int patlen; | |
856 | 8467 int is_buflocal; |
40 | 8468 int buflocal_nr; |
8469 char_u buflocal_pat[25]; /* for "<buffer=X>" */ | |
7 | 8470 |
8471 if (group == AUGROUP_ALL) | |
8472 findgroup = current_augroup; | |
8473 else | |
8474 findgroup = group; | |
8475 allgroups = (group == AUGROUP_ALL && !forceit && *cmd == NUL); | |
8476 | |
8477 /* | |
8478 * Show or delete all patterns for an event. | |
8479 */ | |
8480 if (*pat == NUL) | |
8481 { | |
8482 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
8483 { | |
8484 if (forceit) /* delete the AutoPat, if it's in the current group */ | |
8485 { | |
8486 if (ap->group == findgroup) | |
8487 au_remove_pat(ap); | |
8488 } | |
8489 else if (group == AUGROUP_ALL || ap->group == group) | |
8490 show_autocmd(ap, event); | |
8491 } | |
8492 } | |
8493 | |
8494 /* | |
8495 * Loop through all the specified patterns. | |
8496 */ | |
8497 for ( ; *pat; pat = (*endpat == ',' ? endpat + 1 : endpat)) | |
8498 { | |
8499 /* | |
8500 * Find end of the pattern. | |
8501 * Watch out for a comma in braces, like "*.\{obj,o\}". | |
8502 */ | |
8503 brace_level = 0; | |
8504 for (endpat = pat; *endpat && (*endpat != ',' || brace_level | |
8505 || endpat[-1] == '\\'); ++endpat) | |
8506 { | |
8507 if (*endpat == '{') | |
8508 brace_level++; | |
8509 else if (*endpat == '}') | |
8510 brace_level--; | |
8511 } | |
8512 if (pat == endpat) /* ignore single comma */ | |
8513 continue; | |
8514 patlen = (int)(endpat - pat); | |
8515 | |
8516 /* | |
40 | 8517 * detect special <buflocal[=X]> buffer-local patterns |
8518 */ | |
8519 is_buflocal = FALSE; | |
8520 buflocal_nr = 0; | |
8521 | |
8522 if (patlen >= 7 && STRNCMP(pat, "<buffer", 7) == 0 | |
8523 && pat[patlen - 1] == '>') | |
8524 { | |
8525 /* Error will be printed only for addition. printing and removing | |
8526 * will proceed silently. */ | |
8527 is_buflocal = TRUE; | |
8528 if (patlen == 8) | |
8529 buflocal_nr = curbuf->b_fnum; | |
8530 else if (patlen > 9 && pat[7] == '=') | |
8531 { | |
8532 /* <buffer=abuf> */ | |
8533 if (patlen == 13 && STRNICMP(pat, "<buffer=abuf>", 13)) | |
8534 buflocal_nr = autocmd_bufnr; | |
8535 /* <buffer=123> */ | |
8536 else if (skipdigits(pat + 8) == pat + patlen - 1) | |
8537 buflocal_nr = atoi((char *)pat + 8); | |
8538 } | |
8539 } | |
8540 | |
8541 if (is_buflocal) | |
8542 { | |
8543 /* normalize pat into standard "<buffer>#N" form */ | |
8544 sprintf((char *)buflocal_pat, "<buffer=%d>", buflocal_nr); | |
8545 pat = buflocal_pat; /* can modify pat and patlen */ | |
835 | 8546 patlen = (int)STRLEN(buflocal_pat); /* but not endpat */ |
40 | 8547 } |
8548 | |
8549 /* | |
7 | 8550 * Find AutoPat entries with this pattern. |
8551 */ | |
8552 prev_ap = &first_autopat[(int)event]; | |
8553 while ((ap = *prev_ap) != NULL) | |
8554 { | |
8555 if (ap->pat != NULL) | |
8556 { | |
8557 /* Accept a pattern when: | |
8558 * - a group was specified and it's that group, or a group was | |
8559 * not specified and it's the current group, or a group was | |
8560 * not specified and we are listing | |
8561 * - the length of the pattern matches | |
40 | 8562 * - the pattern matches. |
8563 * For <buffer[=X]>, this condition works because we normalize | |
8564 * all buffer-local patterns. | |
7 | 8565 */ |
8566 if ((allgroups || ap->group == findgroup) | |
8567 && ap->patlen == patlen | |
8568 && STRNCMP(pat, ap->pat, patlen) == 0) | |
8569 { | |
8570 /* | |
8571 * Remove existing autocommands. | |
8572 * If adding any new autocmd's for this AutoPat, don't | |
8573 * delete the pattern from the autopat list, append to | |
8574 * this list. | |
8575 */ | |
8576 if (forceit) | |
8577 { | |
8578 if (*cmd != NUL && ap->next == NULL) | |
8579 { | |
8580 au_remove_cmds(ap); | |
8581 break; | |
8582 } | |
8583 au_remove_pat(ap); | |
8584 } | |
8585 | |
8586 /* | |
40 | 8587 * Show autocmd's for this autopat, or buflocals <buffer=X> |
7 | 8588 */ |
8589 else if (*cmd == NUL) | |
8590 show_autocmd(ap, event); | |
8591 | |
8592 /* | |
8593 * Add autocmd to this autopat, if it's the last one. | |
8594 */ | |
8595 else if (ap->next == NULL) | |
8596 break; | |
8597 } | |
8598 } | |
8599 prev_ap = &ap->next; | |
8600 } | |
8601 | |
8602 /* | |
8603 * Add a new command. | |
8604 */ | |
8605 if (*cmd != NUL) | |
8606 { | |
8607 /* | |
8608 * If the pattern we want to add a command to does appear at the | |
8609 * end of the list (or not is not in the list at all), add the | |
8610 * pattern at the end of the list. | |
8611 */ | |
8612 if (ap == NULL) | |
8613 { | |
40 | 8614 /* refuse to add buffer-local ap if buffer number is invalid */ |
8615 if (is_buflocal && (buflocal_nr == 0 | |
8616 || buflist_findnr(buflocal_nr) == NULL)) | |
8617 { | |
8618 EMSGN(_("E680: <buffer=%d>: invalid buffer number "), | |
8619 buflocal_nr); | |
8620 return FAIL; | |
8621 } | |
8622 | |
7 | 8623 ap = (AutoPat *)alloc((unsigned)sizeof(AutoPat)); |
8624 if (ap == NULL) | |
8625 return FAIL; | |
8626 ap->pat = vim_strnsave(pat, patlen); | |
8627 ap->patlen = patlen; | |
8628 if (ap->pat == NULL) | |
8629 { | |
8630 vim_free(ap); | |
8631 return FAIL; | |
8632 } | |
40 | 8633 |
8634 if (is_buflocal) | |
8635 { | |
8636 ap->buflocal_nr = buflocal_nr; | |
153 | 8637 ap->reg_prog = NULL; |
40 | 8638 } |
8639 else | |
7 | 8640 { |
153 | 8641 char_u *reg_pat; |
8642 | |
40 | 8643 ap->buflocal_nr = 0; |
153 | 8644 reg_pat = file_pat_to_reg_pat(pat, endpat, |
40 | 8645 &ap->allow_dirs, TRUE); |
153 | 8646 if (reg_pat != NULL) |
8647 ap->reg_prog = vim_regcomp(reg_pat, RE_MAGIC); | |
355 | 8648 vim_free(reg_pat); |
153 | 8649 if (reg_pat == NULL || ap->reg_prog == NULL) |
40 | 8650 { |
8651 vim_free(ap->pat); | |
8652 vim_free(ap); | |
8653 return FAIL; | |
8654 } | |
7 | 8655 } |
8656 ap->cmds = NULL; | |
8657 *prev_ap = ap; | |
8658 ap->next = NULL; | |
8659 if (group == AUGROUP_ALL) | |
8660 ap->group = current_augroup; | |
8661 else | |
8662 ap->group = group; | |
8663 } | |
8664 | |
8665 /* | |
8666 * Add the autocmd at the end of the AutoCmd list. | |
8667 */ | |
8668 prev_ac = &(ap->cmds); | |
8669 while ((ac = *prev_ac) != NULL) | |
8670 prev_ac = &ac->next; | |
8671 ac = (AutoCmd *)alloc((unsigned)sizeof(AutoCmd)); | |
8672 if (ac == NULL) | |
8673 return FAIL; | |
8674 ac->cmd = vim_strsave(cmd); | |
8675 #ifdef FEAT_EVAL | |
8676 ac->scriptID = current_SID; | |
8677 #endif | |
8678 if (ac->cmd == NULL) | |
8679 { | |
8680 vim_free(ac); | |
8681 return FAIL; | |
8682 } | |
8683 ac->next = NULL; | |
8684 *prev_ac = ac; | |
8685 ac->nested = nested; | |
8686 } | |
8687 } | |
8688 | |
8689 au_cleanup(); /* may really delete removed patterns/commands now */ | |
8690 return OK; | |
8691 } | |
8692 | |
8693 /* | |
8694 * Implementation of ":doautocmd [group] event [fname]". | |
8695 * Return OK for success, FAIL for failure; | |
8696 */ | |
8697 int | |
8698 do_doautocmd(arg, do_msg) | |
8699 char_u *arg; | |
8700 int do_msg; /* give message for no matching autocmds? */ | |
8701 { | |
8702 char_u *fname; | |
8703 int nothing_done = TRUE; | |
8704 int group; | |
8705 | |
8706 /* | |
8707 * Check for a legal group name. If not, use AUGROUP_ALL. | |
8708 */ | |
8709 group = au_get_grouparg(&arg); | |
8710 if (arg == NULL) /* out of memory */ | |
8711 return FAIL; | |
8712 | |
8713 if (*arg == '*') | |
8714 { | |
8715 EMSG(_("E217: Can't execute autocommands for ALL events")); | |
8716 return FAIL; | |
8717 } | |
8718 | |
8719 /* | |
8720 * Scan over the events. | |
8721 * If we find an illegal name, return here, don't do anything. | |
8722 */ | |
8723 fname = find_end_event(arg, group != AUGROUP_ALL); | |
8724 if (fname == NULL) | |
8725 return FAIL; | |
8726 | |
8727 fname = skipwhite(fname); | |
8728 | |
8729 /* | |
8730 * Loop over the events. | |
8731 */ | |
8732 while (*arg && !vim_iswhite(*arg)) | |
8733 if (apply_autocmds_group(event_name2nr(arg, &arg), | |
8734 fname, NULL, TRUE, group, curbuf, NULL)) | |
8735 nothing_done = FALSE; | |
8736 | |
8737 if (nothing_done && do_msg) | |
8738 MSG(_("No matching autocommands")); | |
8739 | |
8740 #ifdef FEAT_EVAL | |
8741 return aborting() ? FAIL : OK; | |
8742 #else | |
8743 return OK; | |
8744 #endif | |
8745 } | |
8746 | |
8747 /* | |
8748 * ":doautoall": execute autocommands for each loaded buffer. | |
8749 */ | |
8750 void | |
8751 ex_doautoall(eap) | |
8752 exarg_T *eap; | |
8753 { | |
8754 int retval; | |
8755 aco_save_T aco; | |
8756 buf_T *buf; | |
3342 | 8757 char_u *arg = eap->arg; |
3350 | 8758 int call_do_modelines = check_nomodeline(&arg); |
7 | 8759 |
8760 /* | |
8761 * This is a bit tricky: For some commands curwin->w_buffer needs to be | |
8762 * equal to curbuf, but for some buffers there may not be a window. | |
8763 * So we change the buffer for the current window for a moment. This | |
8764 * gives problems when the autocommands make changes to the list of | |
8765 * buffers or windows... | |
8766 */ | |
8767 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
8768 { | |
1665 | 8769 if (buf->b_ml.ml_mfp != NULL) |
7 | 8770 { |
8771 /* find a window for this buffer and save some values */ | |
8772 aucmd_prepbuf(&aco, buf); | |
8773 | |
8774 /* execute the autocommands for this buffer */ | |
3342 | 8775 retval = do_doautocmd(arg, FALSE); |
8776 | |
8777 if (call_do_modelines) | |
8778 { | |
8779 /* Execute the modeline settings, but don't set window-local | |
8780 * options if we are using the current window for another | |
8781 * buffer. */ | |
8782 do_modelines(curwin == aucmd_win ? OPT_NOWIN : 0); | |
8783 } | |
7 | 8784 |
8785 /* restore the current window */ | |
8786 aucmd_restbuf(&aco); | |
8787 | |
8788 /* stop if there is some error or buffer was deleted */ | |
8789 if (retval == FAIL || !buf_valid(buf)) | |
8790 break; | |
8791 } | |
8792 } | |
8793 | |
8794 check_cursor(); /* just in case lines got deleted */ | |
8795 } | |
8796 | |
8797 /* | |
3350 | 8798 * Check *argp for <nomodeline>. When it is present return FALSE, otherwise |
8799 * return TRUE and advance *argp to after it. | |
8800 * Thus return TRUE when do_modelines() should be called. | |
8801 */ | |
8802 int | |
8803 check_nomodeline(argp) | |
8804 char_u **argp; | |
8805 { | |
8806 if (STRNCMP(*argp, "<nomodeline>", 12) == 0) | |
8807 { | |
8808 *argp = skipwhite(*argp + 12); | |
8809 return FALSE; | |
8810 } | |
8811 return TRUE; | |
8812 } | |
8813 | |
8814 /* | |
7 | 8815 * Prepare for executing autocommands for (hidden) buffer "buf". |
1906 | 8816 * Search for a visible window containing the current buffer. If there isn't |
8817 * one then use "aucmd_win". | |
7 | 8818 * Set "curbuf" and "curwin" to match "buf". |
934 | 8819 * When FEAT_AUTOCMD is not defined another version is used, see below. |
7 | 8820 */ |
8821 void | |
8822 aucmd_prepbuf(aco, buf) | |
8823 aco_save_T *aco; /* structure to save values in */ | |
8824 buf_T *buf; /* new curbuf */ | |
8825 { | |
8826 win_T *win; | |
1906 | 8827 #ifdef FEAT_WINDOWS |
8828 int save_ea; | |
8829 #endif | |
7 | 8830 |
8831 /* Find a window that is for the new buffer */ | |
8832 if (buf == curbuf) /* be quick when buf is curbuf */ | |
8833 win = curwin; | |
8834 else | |
8835 #ifdef FEAT_WINDOWS | |
8836 for (win = firstwin; win != NULL; win = win->w_next) | |
8837 if (win->w_buffer == buf) | |
8838 break; | |
8839 #else | |
8840 win = NULL; | |
8841 #endif | |
8842 | |
1906 | 8843 /* Allocate "aucmd_win" when needed. If this fails (out of memory) fall |
8844 * back to using the current window. */ | |
8845 if (win == NULL && aucmd_win == NULL) | |
8846 { | |
8847 win_alloc_aucmd_win(); | |
8848 if (aucmd_win == NULL) | |
8849 win = curwin; | |
8850 } | |
1944 | 8851 if (win == NULL && aucmd_win_used) |
8852 /* Strange recursive autocommand, fall back to using the current | |
8853 * window. Expect a few side effects... */ | |
8854 win = curwin; | |
1906 | 8855 |
8856 aco->save_curwin = curwin; | |
8857 aco->save_curbuf = curbuf; | |
7 | 8858 if (win != NULL) |
8859 { | |
1906 | 8860 /* There is a window for "buf" in the current tab page, make it the |
8861 * curwin. This is preferred, it has the least side effects (esp. if | |
8862 * "buf" is curbuf). */ | |
1944 | 8863 aco->use_aucmd_win = FALSE; |
7 | 8864 curwin = win; |
8865 } | |
8866 else | |
8867 { | |
1906 | 8868 /* There is no window for "buf", use "aucmd_win". To minimize the side |
4352 | 8869 * effects, insert it in the current tab page. |
1906 | 8870 * Anything related to a window (e.g., setting folds) may have |
8871 * unexpected results. */ | |
1944 | 8872 aco->use_aucmd_win = TRUE; |
8873 aucmd_win_used = TRUE; | |
1918 | 8874 aucmd_win->w_buffer = buf; |
2258
772bfca06c18
Fix crash when using ":grep".
Bram Moolenaar <bram@vim.org>
parents:
2244
diff
changeset
|
8875 aucmd_win->w_s = &buf->b_s; |
7 | 8876 ++buf->b_nwindows; |
1918 | 8877 win_init_empty(aucmd_win); /* set cursor and topline to safe values */ |
1944 | 8878 vim_free(aucmd_win->w_localdir); |
8879 aucmd_win->w_localdir = NULL; | |
8880 | |
8881 /* Make sure w_localdir and globaldir are NULL to avoid a chdir() in | |
8882 * win_enter_ext(). */ | |
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; | |
8895 (void)win_split_ins(0, WSP_TOP, aucmd_win, 0); | |
8896 (void)win_comp_pos(); /* recompute window positions */ | |
8897 p_ea = save_ea; | |
1923 | 8898 unblock_autocmds(); |
1906 | 8899 #endif |
1918 | 8900 curwin = aucmd_win; |
1906 | 8901 } |
7 | 8902 curbuf = buf; |
1906 | 8903 aco->new_curwin = curwin; |
8904 aco->new_curbuf = curbuf; | |
7 | 8905 } |
8906 | |
8907 /* | |
8908 * Cleanup after executing autocommands for a (hidden) buffer. | |
8909 * Restore the window as it was (if possible). | |
934 | 8910 * When FEAT_AUTOCMD is not defined another version is used, see below. |
7 | 8911 */ |
8912 void | |
8913 aucmd_restbuf(aco) | |
8914 aco_save_T *aco; /* structure holding saved values */ | |
8915 { | |
1906 | 8916 #ifdef FEAT_WINDOWS |
8917 int dummy; | |
8918 #endif | |
8919 | |
1944 | 8920 if (aco->use_aucmd_win) |
1906 | 8921 { |
8922 --curbuf->b_nwindows; | |
8923 #ifdef FEAT_WINDOWS | |
8924 /* Find "aucmd_win", it can't be closed, but it may be in another tab | |
1923 | 8925 * page. Do not trigger autocommands here. */ |
8926 block_autocmds(); | |
1906 | 8927 if (curwin != aucmd_win) |
8928 { | |
8929 tabpage_T *tp; | |
8930 win_T *wp; | |
8931 | |
8932 FOR_ALL_TAB_WINDOWS(tp, wp) | |
8933 { | |
8934 if (wp == aucmd_win) | |
8935 { | |
8936 if (tp != curtab) | |
3582 | 8937 goto_tabpage_tp(tp, TRUE); |
1906 | 8938 win_goto(aucmd_win); |
3340 | 8939 goto win_found; |
1906 | 8940 } |
8941 } | |
8942 } | |
3340 | 8943 win_found: |
1906 | 8944 |
8945 /* Remove the window and frame from the tree of frames. */ | |
8946 (void)winframe_remove(curwin, &dummy, NULL); | |
8947 win_remove(curwin, NULL); | |
1944 | 8948 aucmd_win_used = FALSE; |
1906 | 8949 last_status(FALSE); /* may need to remove last status line */ |
8950 restore_snapshot(SNAP_AUCMD_IDX, FALSE); | |
8951 (void)win_comp_pos(); /* recompute window positions */ | |
1923 | 8952 unblock_autocmds(); |
1906 | 8953 |
8954 if (win_valid(aco->save_curwin)) | |
8955 curwin = aco->save_curwin; | |
8956 else | |
8957 /* Hmm, original window disappeared. Just use the first one. */ | |
8958 curwin = firstwin; | |
8959 # ifdef FEAT_EVAL | |
4287 | 8960 vars_clear(&aucmd_win->w_vars->dv_hashtab); /* free all w: variables */ |
8961 hash_init(&aucmd_win->w_vars->dv_hashtab); /* re-use the hashtab */ | |
1906 | 8962 # endif |
8963 #else | |
8964 curwin = aco->save_curwin; | |
8965 #endif | |
8966 curbuf = curwin->w_buffer; | |
8967 | |
1944 | 8968 vim_free(globaldir); |
8969 globaldir = aco->globaldir; | |
8970 | |
1906 | 8971 /* the buffer contents may have changed */ |
8972 check_cursor(); | |
8973 if (curwin->w_topline > curbuf->b_ml.ml_line_count) | |
8974 { | |
8975 curwin->w_topline = curbuf->b_ml.ml_line_count; | |
8976 #ifdef FEAT_DIFF | |
8977 curwin->w_topfill = 0; | |
8978 #endif | |
8979 } | |
8980 #if defined(FEAT_GUI) | |
8981 /* Hide the scrollbars from the aucmd_win and update. */ | |
8982 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_LEFT], FALSE); | |
8983 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_RIGHT], FALSE); | |
8984 gui_may_update_scrollbars(); | |
8985 #endif | |
8986 } | |
8987 else | |
7 | 8988 { |
8989 /* restore curwin */ | |
8990 #ifdef FEAT_WINDOWS | |
8991 if (win_valid(aco->save_curwin)) | |
8992 #endif | |
8993 { | |
1906 | 8994 /* Restore the buffer which was previously edited by curwin, if |
1944 | 8995 * it was changed, we are still the same window and the buffer is |
1906 | 8996 * valid. */ |
7 | 8997 if (curwin == aco->new_curwin |
1906 | 8998 && curbuf != aco->new_curbuf |
8999 && buf_valid(aco->new_curbuf) | |
9000 && aco->new_curbuf->b_ml.ml_mfp != NULL) | |
7 | 9001 { |
3497 | 9002 # if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) |
9003 if (curwin->w_s == &curbuf->b_s) | |
9004 curwin->w_s = &aco->new_curbuf->b_s; | |
9005 # endif | |
7 | 9006 --curbuf->b_nwindows; |
1906 | 9007 curbuf = aco->new_curbuf; |
7 | 9008 curwin->w_buffer = curbuf; |
9009 ++curbuf->b_nwindows; | |
9010 } | |
9011 | |
9012 curwin = aco->save_curwin; | |
9013 curbuf = curwin->w_buffer; | |
9014 } | |
9015 } | |
9016 } | |
9017 | |
9018 static int autocmd_nested = FALSE; | |
9019 | |
9020 /* | |
9021 * Execute autocommands for "event" and file name "fname". | |
9022 * Return TRUE if some commands were executed. | |
9023 */ | |
9024 int | |
9025 apply_autocmds(event, fname, fname_io, force, buf) | |
661 | 9026 event_T event; |
7 | 9027 char_u *fname; /* NULL or empty means use actual file name */ |
9028 char_u *fname_io; /* fname to use for <afile> on cmdline */ | |
9029 int force; /* when TRUE, ignore autocmd_busy */ | |
9030 buf_T *buf; /* buffer for <abuf> */ | |
9031 { | |
9032 return apply_autocmds_group(event, fname, fname_io, force, | |
9033 AUGROUP_ALL, buf, NULL); | |
9034 } | |
9035 | |
9036 /* | |
9037 * Like apply_autocmds(), but with extra "eap" argument. This takes care of | |
9038 * setting v:filearg. | |
9039 */ | |
9040 static int | |
9041 apply_autocmds_exarg(event, fname, fname_io, force, buf, eap) | |
661 | 9042 event_T event; |
7 | 9043 char_u *fname; |
9044 char_u *fname_io; | |
9045 int force; | |
9046 buf_T *buf; | |
9047 exarg_T *eap; | |
9048 { | |
9049 return apply_autocmds_group(event, fname, fname_io, force, | |
9050 AUGROUP_ALL, buf, eap); | |
9051 } | |
9052 | |
9053 /* | |
9054 * Like apply_autocmds(), but handles the caller's retval. If the script | |
9055 * processing is being aborted or if retval is FAIL when inside a try | |
9056 * conditional, no autocommands are executed. If otherwise the autocommands | |
9057 * cause the script to be aborted, retval is set to FAIL. | |
9058 */ | |
9059 int | |
9060 apply_autocmds_retval(event, fname, fname_io, force, buf, retval) | |
661 | 9061 event_T event; |
7 | 9062 char_u *fname; /* NULL or empty means use actual file name */ |
9063 char_u *fname_io; /* fname to use for <afile> on cmdline */ | |
9064 int force; /* when TRUE, ignore autocmd_busy */ | |
9065 buf_T *buf; /* buffer for <abuf> */ | |
9066 int *retval; /* pointer to caller's retval */ | |
9067 { | |
9068 int did_cmd; | |
9069 | |
532 | 9070 #ifdef FEAT_EVAL |
7 | 9071 if (should_abort(*retval)) |
9072 return FALSE; | |
532 | 9073 #endif |
7 | 9074 |
9075 did_cmd = apply_autocmds_group(event, fname, fname_io, force, | |
9076 AUGROUP_ALL, buf, NULL); | |
532 | 9077 if (did_cmd |
9078 #ifdef FEAT_EVAL | |
9079 && aborting() | |
9080 #endif | |
9081 ) | |
7 | 9082 *retval = FAIL; |
9083 return did_cmd; | |
9084 } | |
9085 | |
609 | 9086 /* |
9087 * Return TRUE when there is a CursorHold autocommand defined. | |
9088 */ | |
7 | 9089 int |
9090 has_cursorhold() | |
9091 { | |
661 | 9092 return (first_autopat[(int)(get_real_state() == NORMAL_BUSY |
9093 ? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)] != NULL); | |
7 | 9094 } |
609 | 9095 |
9096 /* | |
9097 * Return TRUE if the CursorHold event can be triggered. | |
9098 */ | |
9099 int | |
9100 trigger_cursorhold() | |
9101 { | |
661 | 9102 int state; |
9103 | |
2976 | 9104 if (!did_cursorhold |
9105 && has_cursorhold() | |
9106 && !Recording | |
9107 && typebuf.tb_len == 0 | |
978 | 9108 #ifdef FEAT_INS_EXPAND |
9109 && !ins_compl_active() | |
9110 #endif | |
9111 ) | |
661 | 9112 { |
9113 state = get_real_state(); | |
9114 if (state == NORMAL_BUSY || (state & INSERT) != 0) | |
9115 return TRUE; | |
9116 } | |
9117 return FALSE; | |
609 | 9118 } |
661 | 9119 |
9120 /* | |
9121 * Return TRUE when there is a CursorMoved autocommand defined. | |
9122 */ | |
9123 int | |
9124 has_cursormoved() | |
9125 { | |
9126 return (first_autopat[(int)EVENT_CURSORMOVED] != NULL); | |
9127 } | |
9128 | |
9129 /* | |
9130 * Return TRUE when there is a CursorMovedI autocommand defined. | |
9131 */ | |
9132 int | |
9133 has_cursormovedI() | |
9134 { | |
9135 return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL); | |
9136 } | |
7 | 9137 |
3390 | 9138 /* |
4232 | 9139 * Return TRUE when there is a TextChanged autocommand defined. |
9140 */ | |
9141 int | |
9142 has_textchanged() | |
9143 { | |
9144 return (first_autopat[(int)EVENT_TEXTCHANGED] != NULL); | |
9145 } | |
9146 | |
9147 /* | |
9148 * Return TRUE when there is a TextChangedI autocommand defined. | |
9149 */ | |
9150 int | |
9151 has_textchangedI() | |
9152 { | |
9153 return (first_autopat[(int)EVENT_TEXTCHANGEDI] != NULL); | |
9154 } | |
9155 | |
9156 /* | |
3390 | 9157 * Return TRUE when there is an InsertCharPre autocommand defined. |
9158 */ | |
9159 int | |
9160 has_insertcharpre() | |
9161 { | |
9162 return (first_autopat[(int)EVENT_INSERTCHARPRE] != NULL); | |
9163 } | |
9164 | |
7 | 9165 static int |
9166 apply_autocmds_group(event, fname, fname_io, force, group, buf, eap) | |
661 | 9167 event_T event; |
7 | 9168 char_u *fname; /* NULL or empty means use actual file name */ |
9169 char_u *fname_io; /* fname to use for <afile> on cmdline, NULL means | |
9170 use fname */ | |
9171 int force; /* when TRUE, ignore autocmd_busy */ | |
9172 int group; /* group ID, or AUGROUP_ALL */ | |
9173 buf_T *buf; /* buffer for <abuf> */ | |
9174 exarg_T *eap; /* command arguments */ | |
9175 { | |
9176 char_u *sfname = NULL; /* short file name */ | |
9177 char_u *tail; | |
9178 int save_changed; | |
9179 buf_T *old_curbuf; | |
9180 int retval = FALSE; | |
9181 char_u *save_sourcing_name; | |
9182 linenr_T save_sourcing_lnum; | |
9183 char_u *save_autocmd_fname; | |
1723 | 9184 int save_autocmd_fname_full; |
7 | 9185 int save_autocmd_bufnr; |
9186 char_u *save_autocmd_match; | |
9187 int save_autocmd_busy; | |
9188 int save_autocmd_nested; | |
9189 static int nesting = 0; | |
9190 AutoPatCmd patcmd; | |
9191 AutoPat *ap; | |
9192 #ifdef FEAT_EVAL | |
9193 scid_T save_current_SID; | |
9194 void *save_funccalp; | |
9195 char_u *save_cmdarg; | |
9196 long save_cmdbang; | |
9197 #endif | |
9198 static int filechangeshell_busy = FALSE; | |
170 | 9199 #ifdef FEAT_PROFILE |
9200 proftime_T wait_time; | |
9201 #endif | |
7 | 9202 |
9203 /* | |
9204 * Quickly return if there are no autocommands for this event or | |
9205 * autocommands are blocked. | |
9206 */ | |
1410 | 9207 if (first_autopat[(int)event] == NULL || autocmd_blocked > 0) |
40 | 9208 goto BYPASS_AU; |
7 | 9209 |
9210 /* | |
9211 * When autocommands are busy, new autocommands are only executed when | |
9212 * explicitly enabled with the "nested" flag. | |
9213 */ | |
9214 if (autocmd_busy && !(force || autocmd_nested)) | |
40 | 9215 goto BYPASS_AU; |
7 | 9216 |
9217 #ifdef FEAT_EVAL | |
9218 /* | |
1201 | 9219 * Quickly return when immediately aborting on error, or when an interrupt |
7 | 9220 * occurred or an exception was thrown but not caught. |
9221 */ | |
9222 if (aborting()) | |
40 | 9223 goto BYPASS_AU; |
7 | 9224 #endif |
9225 | |
9226 /* | |
9227 * FileChangedShell never nests, because it can create an endless loop. | |
9228 */ | |
765 | 9229 if (filechangeshell_busy && (event == EVENT_FILECHANGEDSHELL |
9230 || event == EVENT_FILECHANGEDSHELLPOST)) | |
40 | 9231 goto BYPASS_AU; |
7 | 9232 |
9233 /* | |
9234 * Ignore events in 'eventignore'. | |
9235 */ | |
9236 if (event_ignored(event)) | |
40 | 9237 goto BYPASS_AU; |
7 | 9238 |
9239 /* | |
9240 * Allow nesting of autocommands, but restrict the depth, because it's | |
9241 * possible to create an endless loop. | |
9242 */ | |
9243 if (nesting == 10) | |
9244 { | |
9245 EMSG(_("E218: autocommand nesting too deep")); | |
40 | 9246 goto BYPASS_AU; |
7 | 9247 } |
9248 | |
9249 /* | |
9250 * Check if these autocommands are disabled. Used when doing ":all" or | |
9251 * ":ball". | |
9252 */ | |
9253 if ( (autocmd_no_enter | |
9254 && (event == EVENT_WINENTER || event == EVENT_BUFENTER)) | |
9255 || (autocmd_no_leave | |
9256 && (event == EVENT_WINLEAVE || event == EVENT_BUFLEAVE))) | |
40 | 9257 goto BYPASS_AU; |
7 | 9258 |
9259 /* | |
9260 * Save the autocmd_* variables and info about the current buffer. | |
9261 */ | |
9262 save_autocmd_fname = autocmd_fname; | |
1723 | 9263 save_autocmd_fname_full = autocmd_fname_full; |
7 | 9264 save_autocmd_bufnr = autocmd_bufnr; |
9265 save_autocmd_match = autocmd_match; | |
9266 save_autocmd_busy = autocmd_busy; | |
9267 save_autocmd_nested = autocmd_nested; | |
9268 save_changed = curbuf->b_changed; | |
9269 old_curbuf = curbuf; | |
9270 | |
9271 /* | |
9272 * Set the file name to be used for <afile>. | |
1471 | 9273 * Make a copy to avoid that changing a buffer name or directory makes it |
9274 * invalid. | |
7 | 9275 */ |
9276 if (fname_io == NULL) | |
9277 { | |
9278 if (fname != NULL && *fname != NUL) | |
9279 autocmd_fname = fname; | |
9280 else if (buf != NULL) | |
1723 | 9281 autocmd_fname = buf->b_ffname; |
7 | 9282 else |
9283 autocmd_fname = NULL; | |
9284 } | |
9285 else | |
9286 autocmd_fname = fname_io; | |
1471 | 9287 if (autocmd_fname != NULL) |
1723 | 9288 autocmd_fname = vim_strsave(autocmd_fname); |
9289 autocmd_fname_full = FALSE; /* call FullName_save() later */ | |
7 | 9290 |
9291 /* | |
9292 * Set the buffer number to be used for <abuf>. | |
9293 */ | |
9294 if (buf == NULL) | |
9295 autocmd_bufnr = 0; | |
9296 else | |
9297 autocmd_bufnr = buf->b_fnum; | |
9298 | |
9299 /* | |
9300 * When the file name is NULL or empty, use the file name of buffer "buf". | |
9301 * Always use the full path of the file name to match with, in case | |
9302 * "allow_dirs" is set. | |
9303 */ | |
9304 if (fname == NULL || *fname == NUL) | |
9305 { | |
9306 if (buf == NULL) | |
9307 fname = NULL; | |
9308 else | |
9309 { | |
9310 #ifdef FEAT_SYN_HL | |
9311 if (event == EVENT_SYNTAX) | |
9312 fname = buf->b_p_syn; | |
9313 else | |
9314 #endif | |
9315 if (event == EVENT_FILETYPE) | |
9316 fname = buf->b_p_ft; | |
9317 else | |
9318 { | |
9319 if (buf->b_sfname != NULL) | |
9320 sfname = vim_strsave(buf->b_sfname); | |
9321 fname = buf->b_ffname; | |
9322 } | |
9323 } | |
9324 if (fname == NULL) | |
9325 fname = (char_u *)""; | |
9326 fname = vim_strsave(fname); /* make a copy, so we can change it */ | |
9327 } | |
9328 else | |
9329 { | |
9330 sfname = vim_strsave(fname); | |
1867 | 9331 /* Don't try expanding FileType, Syntax, FuncUndefined, WindowID or |
9332 * QuickFixCmd* */ | |
161 | 9333 if (event == EVENT_FILETYPE |
9334 || event == EVENT_SYNTAX | |
1867 | 9335 || event == EVENT_FUNCUNDEFINED |
161 | 9336 || event == EVENT_REMOTEREPLY |
649 | 9337 || event == EVENT_SPELLFILEMISSING |
161 | 9338 || event == EVENT_QUICKFIXCMDPRE |
9339 || event == EVENT_QUICKFIXCMDPOST) | |
7 | 9340 fname = vim_strsave(fname); |
9341 else | |
9342 fname = FullName_save(fname, FALSE); | |
9343 } | |
9344 if (fname == NULL) /* out of memory */ | |
9345 { | |
9346 vim_free(sfname); | |
40 | 9347 retval = FALSE; |
9348 goto BYPASS_AU; | |
7 | 9349 } |
9350 | |
9351 #ifdef BACKSLASH_IN_FILENAME | |
9352 /* | |
9353 * Replace all backslashes with forward slashes. This makes the | |
9354 * autocommand patterns portable between Unix and MS-DOS. | |
9355 */ | |
9356 if (sfname != NULL) | |
9357 forward_slash(sfname); | |
9358 forward_slash(fname); | |
9359 #endif | |
9360 | |
9361 #ifdef VMS | |
9362 /* remove version for correct match */ | |
9363 if (sfname != NULL) | |
9364 vms_remove_version(sfname); | |
9365 vms_remove_version(fname); | |
9366 #endif | |
9367 | |
9368 /* | |
9369 * Set the name to be used for <amatch>. | |
9370 */ | |
9371 autocmd_match = fname; | |
9372 | |
9373 | |
9374 /* Don't redraw while doing auto commands. */ | |
9375 ++RedrawingDisabled; | |
9376 save_sourcing_name = sourcing_name; | |
9377 sourcing_name = NULL; /* don't free this one */ | |
9378 save_sourcing_lnum = sourcing_lnum; | |
9379 sourcing_lnum = 0; /* no line number here */ | |
9380 | |
9381 #ifdef FEAT_EVAL | |
9382 save_current_SID = current_SID; | |
9383 | |
170 | 9384 # ifdef FEAT_PROFILE |
788 | 9385 if (do_profiling == PROF_YES) |
170 | 9386 prof_child_enter(&wait_time); /* doesn't count for the caller itself */ |
9387 # endif | |
9388 | |
7 | 9389 /* Don't use local function variables, if called from a function */ |
9390 save_funccalp = save_funccal(); | |
9391 #endif | |
9392 | |
9393 /* | |
9394 * When starting to execute autocommands, save the search patterns. | |
9395 */ | |
9396 if (!autocmd_busy) | |
9397 { | |
9398 save_search_patterns(); | |
9399 saveRedobuff(); | |
9400 did_filetype = keep_filetype; | |
9401 } | |
9402 | |
9403 /* | |
9404 * Note that we are applying autocmds. Some commands need to know. | |
9405 */ | |
9406 autocmd_busy = TRUE; | |
9407 filechangeshell_busy = (event == EVENT_FILECHANGEDSHELL); | |
9408 ++nesting; /* see matching decrement below */ | |
9409 | |
9410 /* Remember that FileType was triggered. Used for did_filetype(). */ | |
9411 if (event == EVENT_FILETYPE) | |
9412 did_filetype = TRUE; | |
9413 | |
9414 tail = gettail(fname); | |
9415 | |
9416 /* Find first autocommand that matches */ | |
9417 patcmd.curpat = first_autopat[(int)event]; | |
9418 patcmd.nextcmd = NULL; | |
9419 patcmd.group = group; | |
9420 patcmd.fname = fname; | |
9421 patcmd.sfname = sfname; | |
9422 patcmd.tail = tail; | |
9423 patcmd.event = event; | |
40 | 9424 patcmd.arg_bufnr = autocmd_bufnr; |
9425 patcmd.next = NULL; | |
7 | 9426 auto_next_pat(&patcmd, FALSE); |
9427 | |
9428 /* found one, start executing the autocommands */ | |
9429 if (patcmd.curpat != NULL) | |
9430 { | |
40 | 9431 /* add to active_apc_list */ |
9432 patcmd.next = active_apc_list; | |
9433 active_apc_list = &patcmd; | |
9434 | |
7 | 9435 #ifdef FEAT_EVAL |
9436 /* set v:cmdarg (only when there is a matching pattern) */ | |
9437 save_cmdbang = get_vim_var_nr(VV_CMDBANG); | |
9438 if (eap != NULL) | |
9439 { | |
9440 save_cmdarg = set_cmdarg(eap, NULL); | |
9441 set_vim_var_nr(VV_CMDBANG, (long)eap->forceit); | |
9442 } | |
9443 else | |
9444 save_cmdarg = NULL; /* avoid gcc warning */ | |
9445 #endif | |
9446 retval = TRUE; | |
9447 /* mark the last pattern, to avoid an endless loop when more patterns | |
9448 * are added when executing autocommands */ | |
9449 for (ap = patcmd.curpat; ap->next != NULL; ap = ap->next) | |
9450 ap->last = FALSE; | |
9451 ap->last = TRUE; | |
9452 check_lnums(TRUE); /* make sure cursor and topline are valid */ | |
9453 do_cmdline(NULL, getnextac, (void *)&patcmd, | |
9454 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT); | |
9455 #ifdef FEAT_EVAL | |
9456 if (eap != NULL) | |
9457 { | |
9458 (void)set_cmdarg(NULL, save_cmdarg); | |
9459 set_vim_var_nr(VV_CMDBANG, save_cmdbang); | |
9460 } | |
9461 #endif | |
40 | 9462 /* delete from active_apc_list */ |
9463 if (active_apc_list == &patcmd) /* just in case */ | |
9464 active_apc_list = patcmd.next; | |
7 | 9465 } |
9466 | |
9467 --RedrawingDisabled; | |
9468 autocmd_busy = save_autocmd_busy; | |
9469 filechangeshell_busy = FALSE; | |
9470 autocmd_nested = save_autocmd_nested; | |
9471 vim_free(sourcing_name); | |
9472 sourcing_name = save_sourcing_name; | |
9473 sourcing_lnum = save_sourcing_lnum; | |
1471 | 9474 vim_free(autocmd_fname); |
7 | 9475 autocmd_fname = save_autocmd_fname; |
1723 | 9476 autocmd_fname_full = save_autocmd_fname_full; |
7 | 9477 autocmd_bufnr = save_autocmd_bufnr; |
9478 autocmd_match = save_autocmd_match; | |
9479 #ifdef FEAT_EVAL | |
9480 current_SID = save_current_SID; | |
9481 restore_funccal(save_funccalp); | |
170 | 9482 # ifdef FEAT_PROFILE |
788 | 9483 if (do_profiling == PROF_YES) |
170 | 9484 prof_child_exit(&wait_time); |
9485 # endif | |
7 | 9486 #endif |
9487 vim_free(fname); | |
9488 vim_free(sfname); | |
9489 --nesting; /* see matching increment above */ | |
9490 | |
9491 /* | |
9492 * When stopping to execute autocommands, restore the search patterns and | |
9493 * the redo buffer. | |
9494 */ | |
9495 if (!autocmd_busy) | |
9496 { | |
9497 restore_search_patterns(); | |
9498 restoreRedobuff(); | |
9499 did_filetype = FALSE; | |
9500 } | |
9501 | |
9502 /* | |
9503 * Some events don't set or reset the Changed flag. | |
9504 * Check if still in the same buffer! | |
9505 */ | |
9506 if (curbuf == old_curbuf | |
9507 && (event == EVENT_BUFREADPOST | |
9508 || event == EVENT_BUFWRITEPOST | |
9509 || event == EVENT_FILEAPPENDPOST | |
9510 || event == EVENT_VIMLEAVE | |
9511 || event == EVENT_VIMLEAVEPRE)) | |
9512 { | |
9513 #ifdef FEAT_TITLE | |
9514 if (curbuf->b_changed != save_changed) | |
9515 need_maketitle = TRUE; | |
9516 #endif | |
9517 curbuf->b_changed = save_changed; | |
9518 } | |
9519 | |
9520 au_cleanup(); /* may really delete removed patterns/commands now */ | |
40 | 9521 |
9522 BYPASS_AU: | |
9523 /* When wiping out a buffer make sure all its buffer-local autocommands | |
9524 * are deleted. */ | |
9525 if (event == EVENT_BUFWIPEOUT && buf != NULL) | |
9526 aubuflocal_remove(buf); | |
9527 | |
7 | 9528 return retval; |
9529 } | |
9530 | |
1410 | 9531 # ifdef FEAT_EVAL |
9532 static char_u *old_termresponse = NULL; | |
9533 # endif | |
9534 | |
9535 /* | |
9536 * Block triggering autocommands until unblock_autocmd() is called. | |
9537 * Can be used recursively, so long as it's symmetric. | |
9538 */ | |
9539 void | |
9540 block_autocmds() | |
9541 { | |
9542 # ifdef FEAT_EVAL | |
9543 /* Remember the value of v:termresponse. */ | |
9544 if (autocmd_blocked == 0) | |
9545 old_termresponse = get_vim_var_str(VV_TERMRESPONSE); | |
9546 # endif | |
9547 ++autocmd_blocked; | |
9548 } | |
9549 | |
9550 void | |
9551 unblock_autocmds() | |
9552 { | |
9553 --autocmd_blocked; | |
9554 | |
9555 # ifdef FEAT_EVAL | |
9556 /* When v:termresponse was set while autocommands were blocked, trigger | |
9557 * the autocommands now. Esp. useful when executing a shell command | |
9558 * during startup (vimdiff). */ | |
9559 if (autocmd_blocked == 0 | |
9560 && get_vim_var_str(VV_TERMRESPONSE) != old_termresponse) | |
9561 apply_autocmds(EVENT_TERMRESPONSE, NULL, NULL, FALSE, curbuf); | |
9562 # endif | |
9563 } | |
9564 | |
7 | 9565 /* |
9566 * Find next autocommand pattern that matches. | |
9567 */ | |
9568 static void | |
9569 auto_next_pat(apc, stop_at_last) | |
9570 AutoPatCmd *apc; | |
9571 int stop_at_last; /* stop when 'last' flag is set */ | |
9572 { | |
9573 AutoPat *ap; | |
9574 AutoCmd *cp; | |
9575 char_u *name; | |
9576 char *s; | |
9577 | |
9578 vim_free(sourcing_name); | |
9579 sourcing_name = NULL; | |
9580 | |
9581 for (ap = apc->curpat; ap != NULL && !got_int; ap = ap->next) | |
9582 { | |
9583 apc->curpat = NULL; | |
9584 | |
1723 | 9585 /* Only use a pattern when it has not been removed, has commands and |
40 | 9586 * the group matches. For buffer-local autocommands only check the |
9587 * buffer number. */ | |
7 | 9588 if (ap->pat != NULL && ap->cmds != NULL |
9589 && (apc->group == AUGROUP_ALL || apc->group == ap->group)) | |
9590 { | |
40 | 9591 /* execution-condition */ |
9592 if (ap->buflocal_nr == 0 | |
153 | 9593 ? (match_file_pat(NULL, ap->reg_prog, apc->fname, |
9594 apc->sfname, apc->tail, ap->allow_dirs)) | |
40 | 9595 : ap->buflocal_nr == apc->arg_bufnr) |
7 | 9596 { |
9597 name = event_nr2name(apc->event); | |
9598 s = _("%s Auto commands for \"%s\""); | |
9599 sourcing_name = alloc((unsigned)(STRLEN(s) | |
9600 + STRLEN(name) + ap->patlen + 1)); | |
9601 if (sourcing_name != NULL) | |
9602 { | |
9603 sprintf((char *)sourcing_name, s, | |
9604 (char *)name, (char *)ap->pat); | |
9605 if (p_verbose >= 8) | |
292 | 9606 { |
9607 verbose_enter(); | |
273 | 9608 smsg((char_u *)_("Executing %s"), sourcing_name); |
292 | 9609 verbose_leave(); |
9610 } | |
7 | 9611 } |
9612 | |
9613 apc->curpat = ap; | |
9614 apc->nextcmd = ap->cmds; | |
9615 /* mark last command */ | |
9616 for (cp = ap->cmds; cp->next != NULL; cp = cp->next) | |
9617 cp->last = FALSE; | |
9618 cp->last = TRUE; | |
9619 } | |
9620 line_breakcheck(); | |
9621 if (apc->curpat != NULL) /* found a match */ | |
9622 break; | |
9623 } | |
9624 if (stop_at_last && ap->last) | |
9625 break; | |
9626 } | |
9627 } | |
9628 | |
9629 /* | |
9630 * Get next autocommand command. | |
9631 * Called by do_cmdline() to get the next line for ":if". | |
9632 * Returns allocated string, or NULL for end of autocommands. | |
9633 */ | |
3997 | 9634 char_u * |
7 | 9635 getnextac(c, cookie, indent) |
1880 | 9636 int c UNUSED; |
7 | 9637 void *cookie; |
1880 | 9638 int indent UNUSED; |
7 | 9639 { |
9640 AutoPatCmd *acp = (AutoPatCmd *)cookie; | |
9641 char_u *retval; | |
9642 AutoCmd *ac; | |
9643 | |
9644 /* Can be called again after returning the last line. */ | |
9645 if (acp->curpat == NULL) | |
9646 return NULL; | |
9647 | |
9648 /* repeat until we find an autocommand to execute */ | |
9649 for (;;) | |
9650 { | |
9651 /* skip removed commands */ | |
9652 while (acp->nextcmd != NULL && acp->nextcmd->cmd == NULL) | |
9653 if (acp->nextcmd->last) | |
9654 acp->nextcmd = NULL; | |
9655 else | |
9656 acp->nextcmd = acp->nextcmd->next; | |
9657 | |
9658 if (acp->nextcmd != NULL) | |
9659 break; | |
9660 | |
9661 /* at end of commands, find next pattern that matches */ | |
9662 if (acp->curpat->last) | |
9663 acp->curpat = NULL; | |
9664 else | |
9665 acp->curpat = acp->curpat->next; | |
9666 if (acp->curpat != NULL) | |
9667 auto_next_pat(acp, TRUE); | |
9668 if (acp->curpat == NULL) | |
9669 return NULL; | |
9670 } | |
9671 | |
9672 ac = acp->nextcmd; | |
9673 | |
9674 if (p_verbose >= 9) | |
9675 { | |
292 | 9676 verbose_enter_scroll(); |
273 | 9677 smsg((char_u *)_("autocommand %s"), ac->cmd); |
7 | 9678 msg_puts((char_u *)"\n"); /* don't overwrite this either */ |
292 | 9679 verbose_leave_scroll(); |
7 | 9680 } |
9681 retval = vim_strsave(ac->cmd); | |
9682 autocmd_nested = ac->nested; | |
9683 #ifdef FEAT_EVAL | |
9684 current_SID = ac->scriptID; | |
9685 #endif | |
9686 if (ac->last) | |
9687 acp->nextcmd = NULL; | |
9688 else | |
9689 acp->nextcmd = ac->next; | |
9690 return retval; | |
9691 } | |
9692 | |
9693 /* | |
9694 * Return TRUE if there is a matching autocommand for "fname". | |
40 | 9695 * To account for buffer-local autocommands, function needs to know |
9696 * in which buffer the file will be opened. | |
7 | 9697 */ |
9698 int | |
40 | 9699 has_autocmd(event, sfname, buf) |
661 | 9700 event_T event; |
7 | 9701 char_u *sfname; |
40 | 9702 buf_T *buf; |
7 | 9703 { |
9704 AutoPat *ap; | |
9705 char_u *fname; | |
9706 char_u *tail = gettail(sfname); | |
9707 int retval = FALSE; | |
9708 | |
9709 fname = FullName_save(sfname, FALSE); | |
9710 if (fname == NULL) | |
9711 return FALSE; | |
9712 | |
9713 #ifdef BACKSLASH_IN_FILENAME | |
9714 /* | |
9715 * Replace all backslashes with forward slashes. This makes the | |
9716 * autocommand patterns portable between Unix and MS-DOS. | |
9717 */ | |
9718 sfname = vim_strsave(sfname); | |
9719 if (sfname != NULL) | |
9720 forward_slash(sfname); | |
9721 forward_slash(fname); | |
9722 #endif | |
9723 | |
9724 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
9725 if (ap->pat != NULL && ap->cmds != NULL | |
856 | 9726 && (ap->buflocal_nr == 0 |
153 | 9727 ? match_file_pat(NULL, ap->reg_prog, |
9728 fname, sfname, tail, ap->allow_dirs) | |
856 | 9729 : buf != NULL && ap->buflocal_nr == buf->b_fnum |
40 | 9730 )) |
7 | 9731 { |
9732 retval = TRUE; | |
9733 break; | |
9734 } | |
9735 | |
9736 vim_free(fname); | |
9737 #ifdef BACKSLASH_IN_FILENAME | |
9738 vim_free(sfname); | |
9739 #endif | |
9740 | |
9741 return retval; | |
9742 } | |
9743 | |
9744 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
9745 /* | |
9746 * Function given to ExpandGeneric() to obtain the list of autocommand group | |
9747 * names. | |
9748 */ | |
9749 char_u * | |
9750 get_augroup_name(xp, idx) | |
1880 | 9751 expand_T *xp UNUSED; |
7 | 9752 int idx; |
9753 { | |
9754 if (idx == augroups.ga_len) /* add "END" add the end */ | |
9755 return (char_u *)"END"; | |
9756 if (idx >= augroups.ga_len) /* end of list */ | |
9757 return NULL; | |
9758 if (AUGROUP_NAME(idx) == NULL) /* skip deleted entries */ | |
9759 return (char_u *)""; | |
9760 return AUGROUP_NAME(idx); /* return a name */ | |
9761 } | |
9762 | |
9763 static int include_groups = FALSE; | |
9764 | |
9765 char_u * | |
9766 set_context_in_autocmd(xp, arg, doautocmd) | |
9767 expand_T *xp; | |
9768 char_u *arg; | |
1733 | 9769 int doautocmd; /* TRUE for :doauto*, FALSE for :autocmd */ |
7 | 9770 { |
9771 char_u *p; | |
9772 int group; | |
9773 | |
9774 /* check for a group name, skip it if present */ | |
9775 include_groups = FALSE; | |
9776 p = arg; | |
9777 group = au_get_grouparg(&arg); | |
9778 if (group == AUGROUP_ERROR) | |
9779 return NULL; | |
9780 /* If there only is a group name that's what we expand. */ | |
9781 if (*arg == NUL && group != AUGROUP_ALL && !vim_iswhite(arg[-1])) | |
9782 { | |
9783 arg = p; | |
9784 group = AUGROUP_ALL; | |
9785 } | |
9786 | |
9787 /* skip over event name */ | |
9788 for (p = arg; *p != NUL && !vim_iswhite(*p); ++p) | |
9789 if (*p == ',') | |
9790 arg = p + 1; | |
9791 if (*p == NUL) | |
9792 { | |
9793 if (group == AUGROUP_ALL) | |
9794 include_groups = TRUE; | |
9795 xp->xp_context = EXPAND_EVENTS; /* expand event name */ | |
9796 xp->xp_pattern = arg; | |
9797 return NULL; | |
9798 } | |
9799 | |
9800 /* skip over pattern */ | |
9801 arg = skipwhite(p); | |
9802 while (*arg && (!vim_iswhite(*arg) || arg[-1] == '\\')) | |
9803 arg++; | |
9804 if (*arg) | |
9805 return arg; /* expand (next) command */ | |
9806 | |
9807 if (doautocmd) | |
9808 xp->xp_context = EXPAND_FILES; /* expand file names */ | |
9809 else | |
9810 xp->xp_context = EXPAND_NOTHING; /* pattern is not expanded */ | |
9811 return NULL; | |
9812 } | |
9813 | |
9814 /* | |
9815 * Function given to ExpandGeneric() to obtain the list of event names. | |
9816 */ | |
9817 char_u * | |
9818 get_event_name(xp, idx) | |
1880 | 9819 expand_T *xp UNUSED; |
7 | 9820 int idx; |
9821 { | |
9822 if (idx < augroups.ga_len) /* First list group names, if wanted */ | |
9823 { | |
9824 if (!include_groups || AUGROUP_NAME(idx) == NULL) | |
9825 return (char_u *)""; /* skip deleted entries */ | |
9826 return AUGROUP_NAME(idx); /* return a name */ | |
9827 } | |
9828 return (char_u *)event_names[idx - augroups.ga_len].name; | |
9829 } | |
9830 | |
9831 #endif /* FEAT_CMDL_COMPL */ | |
9832 | |
9833 /* | |
615 | 9834 * Return TRUE if autocmd is supported. |
9835 */ | |
9836 int | |
9837 autocmd_supported(name) | |
9838 char_u *name; | |
9839 { | |
9840 char_u *p; | |
9841 | |
9842 return (event_name2nr(name, &p) != NUM_EVENTS); | |
9843 } | |
9844 | |
9845 /* | |
612 | 9846 * Return TRUE if an autocommand is defined for a group, event and |
9847 * pattern: The group can be omitted to accept any group. "event" and "pattern" | |
9848 * can be NULL to accept any event and pattern. "pattern" can be NULL to accept | |
9849 * any pattern. Buffer-local patterns <buffer> or <buffer=N> are accepted. | |
9850 * Used for: | |
9851 * exists("#Group") or | |
9852 * exists("#Group#Event") or | |
9853 * exists("#Group#Event#pat") or | |
9854 * exists("#Event") or | |
9855 * exists("#Event#pat") | |
7 | 9856 */ |
9857 int | |
612 | 9858 au_exists(arg) |
9859 char_u *arg; | |
7 | 9860 { |
612 | 9861 char_u *arg_save; |
9862 char_u *pattern = NULL; | |
7 | 9863 char_u *event_name; |
9864 char_u *p; | |
661 | 9865 event_T event; |
7 | 9866 AutoPat *ap; |
40 | 9867 buf_T *buflocal_buf = NULL; |
612 | 9868 int group; |
9869 int retval = FALSE; | |
9870 | |
615 | 9871 /* Make a copy so that we can change the '#' chars to a NUL. */ |
612 | 9872 arg_save = vim_strsave(arg); |
9873 if (arg_save == NULL) | |
9874 return FALSE; | |
615 | 9875 p = vim_strchr(arg_save, '#'); |
612 | 9876 if (p != NULL) |
9877 *p++ = NUL; | |
9878 | |
9879 /* First, look for an autocmd group name */ | |
9880 group = au_find_group(arg_save); | |
9881 if (group == AUGROUP_ERROR) | |
9882 { | |
9883 /* Didn't match a group name, assume the first argument is an event. */ | |
9884 group = AUGROUP_ALL; | |
9885 event_name = arg_save; | |
9886 } | |
9887 else | |
9888 { | |
9889 if (p == NULL) | |
9890 { | |
9891 /* "Group": group name is present and it's recognized */ | |
9892 retval = TRUE; | |
9893 goto theend; | |
9894 } | |
9895 | |
9896 /* Must be "Group#Event" or "Group#Event#pat". */ | |
9897 event_name = p; | |
9898 p = vim_strchr(event_name, '#'); | |
9899 if (p != NULL) | |
9900 *p++ = NUL; /* "Group#Event#pat" */ | |
9901 } | |
9902 | |
9903 pattern = p; /* "pattern" is NULL when there is no pattern */ | |
7 | 9904 |
9905 /* find the index (enum) for the event name */ | |
9906 event = event_name2nr(event_name, &p); | |
9907 | |
9908 /* return FALSE if the event name is not recognized */ | |
612 | 9909 if (event == NUM_EVENTS) |
9910 goto theend; | |
7 | 9911 |
9912 /* Find the first autocommand for this event. | |
9913 * If there isn't any, return FALSE; | |
9914 * If there is one and no pattern given, return TRUE; */ | |
9915 ap = first_autopat[(int)event]; | |
9916 if (ap == NULL) | |
612 | 9917 goto theend; |
7 | 9918 |
40 | 9919 /* if pattern is "<buffer>", special handling is needed which uses curbuf */ |
9920 /* for pattern "<buffer=N>, fnamecmp() will work fine */ | |
1962 | 9921 if (pattern != NULL && STRICMP(pattern, "<buffer>") == 0) |
40 | 9922 buflocal_buf = curbuf; |
9923 | |
7 | 9924 /* Check if there is an autocommand with the given pattern. */ |
9925 for ( ; ap != NULL; ap = ap->next) | |
40 | 9926 /* only use a pattern when it has not been removed and has commands. */ |
9927 /* For buffer-local autocommands, fnamecmp() works fine. */ | |
7 | 9928 if (ap->pat != NULL && ap->cmds != NULL |
612 | 9929 && (group == AUGROUP_ALL || ap->group == group) |
1962 | 9930 && (pattern == NULL |
9931 || (buflocal_buf == NULL | |
9932 ? fnamecmp(ap->pat, pattern) == 0 | |
9933 : ap->buflocal_nr == buflocal_buf->b_fnum))) | |
612 | 9934 { |
9935 retval = TRUE; | |
9936 break; | |
9937 } | |
9938 | |
9939 theend: | |
9940 vim_free(arg_save); | |
9941 return retval; | |
7 | 9942 } |
40 | 9943 |
934 | 9944 #else /* FEAT_AUTOCMD */ |
9945 | |
9946 /* | |
9947 * Prepare for executing commands for (hidden) buffer "buf". | |
9948 * This is the non-autocommand version, it simply saves "curbuf" and sets | |
9949 * "curbuf" and "curwin" to match "buf". | |
9950 */ | |
9951 void | |
9952 aucmd_prepbuf(aco, buf) | |
9953 aco_save_T *aco; /* structure to save values in */ | |
9954 buf_T *buf; /* new curbuf */ | |
9955 { | |
1906 | 9956 aco->save_curbuf = curbuf; |
9957 --curbuf->b_nwindows; | |
934 | 9958 curbuf = buf; |
9959 curwin->w_buffer = buf; | |
1906 | 9960 ++curbuf->b_nwindows; |
934 | 9961 } |
9962 | |
9963 /* | |
9964 * Restore after executing commands for a (hidden) buffer. | |
9965 * This is the non-autocommand version. | |
9966 */ | |
9967 void | |
9968 aucmd_restbuf(aco) | |
9969 aco_save_T *aco; /* structure holding saved values */ | |
9970 { | |
1906 | 9971 --curbuf->b_nwindows; |
9972 curbuf = aco->save_curbuf; | |
934 | 9973 curwin->w_buffer = curbuf; |
1906 | 9974 ++curbuf->b_nwindows; |
934 | 9975 } |
9976 | |
7 | 9977 #endif /* FEAT_AUTOCMD */ |
9978 | |
934 | 9979 |
7 | 9980 #if defined(FEAT_AUTOCMD) || defined(FEAT_WILDIGN) || defined(PROTO) |
9981 /* | |
153 | 9982 * Try matching a filename with a "pattern" ("prog" is NULL), or use the |
9983 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling | |
9984 * vim_regcomp() often. | |
7 | 9985 * Used for autocommands and 'wildignore'. |
9986 * Returns TRUE if there is a match, FALSE otherwise. | |
9987 */ | |
9988 int | |
153 | 9989 match_file_pat(pattern, prog, fname, sfname, tail, allow_dirs) |
7 | 9990 char_u *pattern; /* pattern to match with */ |
153 | 9991 regprog_T *prog; /* pre-compiled regprog or NULL */ |
7 | 9992 char_u *fname; /* full path of file name */ |
9993 char_u *sfname; /* short file name or NULL */ | |
9994 char_u *tail; /* tail of path */ | |
9995 int allow_dirs; /* allow matching with dir */ | |
9996 { | |
9997 regmatch_T regmatch; | |
9998 int result = FALSE; | |
9999 #ifdef FEAT_OSFILETYPE | |
10000 int no_pattern = FALSE; /* TRUE if check is filetype only */ | |
10001 char_u *type_start; | |
10002 char_u c; | |
10003 int match = FALSE; | |
10004 #endif | |
10005 | |
4242 | 10006 regmatch.rm_ic = p_fic; /* ignore case if 'fileignorecase' is set */ |
7 | 10007 #ifdef FEAT_OSFILETYPE |
10008 if (*pattern == '<') | |
10009 { | |
10010 /* There is a filetype condition specified with this pattern. | |
10011 * Check the filetype matches first. If not, don't bother with the | |
10012 * pattern (set regprog to NULL). | |
10013 * Always use magic for the regexp. | |
10014 */ | |
10015 | |
10016 for (type_start = pattern + 1; (c = *pattern); pattern++) | |
10017 { | |
10018 if ((c == ';' || c == '>') && match == FALSE) | |
10019 { | |
10020 *pattern = NUL; /* Terminate the string */ | |
2976 | 10021 /* TODO: match with 'filetype' of buffer that "fname" comes |
10022 * from. */ | |
7 | 10023 match = mch_check_filetype(fname, type_start); |
10024 *pattern = c; /* Restore the terminator */ | |
10025 type_start = pattern + 1; | |
10026 } | |
10027 if (c == '>') | |
10028 break; | |
10029 } | |
10030 | |
10031 /* (c should never be NUL, but check anyway) */ | |
10032 if (match == FALSE || c == NUL) | |
10033 regmatch.regprog = NULL; /* Doesn't match - don't check pat. */ | |
10034 else if (*pattern == NUL) | |
10035 { | |
10036 regmatch.regprog = NULL; /* Vim will try to free regprog later */ | |
10037 no_pattern = TRUE; /* Always matches - don't check pat. */ | |
10038 } | |
10039 else | |
10040 regmatch.regprog = vim_regcomp(pattern + 1, RE_MAGIC); | |
10041 } | |
10042 else | |
10043 #endif | |
153 | 10044 { |
10045 if (prog != NULL) | |
10046 regmatch.regprog = prog; | |
10047 else | |
10048 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC); | |
10049 } | |
7 | 10050 |
10051 /* | |
10052 * Try for a match with the pattern with: | |
10053 * 1. the full file name, when the pattern has a '/'. | |
10054 * 2. the short file name, when the pattern has a '/'. | |
10055 * 3. the tail of the file name, when the pattern has no '/'. | |
10056 */ | |
10057 if ( | |
10058 #ifdef FEAT_OSFILETYPE | |
10059 /* If the check is for a filetype only and we don't care | |
10060 * about the path then skip all the regexp stuff. | |
10061 */ | |
10062 no_pattern || | |
10063 #endif | |
10064 (regmatch.regprog != NULL | |
10065 && ((allow_dirs | |
10066 && (vim_regexec(®match, fname, (colnr_T)0) | |
10067 || (sfname != NULL | |
10068 && vim_regexec(®match, sfname, (colnr_T)0)))) | |
10069 || (!allow_dirs && vim_regexec(®match, tail, (colnr_T)0))))) | |
10070 result = TRUE; | |
10071 | |
153 | 10072 if (prog == NULL) |
10073 vim_free(regmatch.regprog); | |
7 | 10074 return result; |
10075 } | |
10076 #endif | |
10077 | |
10078 #if defined(FEAT_WILDIGN) || defined(PROTO) | |
10079 /* | |
10080 * Return TRUE if a file matches with a pattern in "list". | |
10081 * "list" is a comma-separated list of patterns, like 'wildignore'. | |
10082 * "sfname" is the short file name or NULL, "ffname" the long file name. | |
10083 */ | |
10084 int | |
10085 match_file_list(list, sfname, ffname) | |
10086 char_u *list; | |
10087 char_u *sfname; | |
10088 char_u *ffname; | |
10089 { | |
10090 char_u buf[100]; | |
10091 char_u *tail; | |
10092 char_u *regpat; | |
10093 char allow_dirs; | |
10094 int match; | |
10095 char_u *p; | |
10096 | |
10097 tail = gettail(sfname); | |
10098 | |
10099 /* try all patterns in 'wildignore' */ | |
10100 p = list; | |
10101 while (*p) | |
10102 { | |
10103 copy_option_part(&p, buf, 100, ","); | |
10104 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE); | |
10105 if (regpat == NULL) | |
10106 break; | |
153 | 10107 match = match_file_pat(regpat, NULL, ffname, sfname, |
10108 tail, (int)allow_dirs); | |
7 | 10109 vim_free(regpat); |
10110 if (match) | |
10111 return TRUE; | |
10112 } | |
10113 return FALSE; | |
10114 } | |
10115 #endif | |
10116 | |
10117 /* | |
10118 * Convert the given pattern "pat" which has shell style wildcards in it, into | |
10119 * a regular expression, and return the result in allocated memory. If there | |
10120 * is a directory path separator to be matched, then TRUE is put in | |
10121 * allow_dirs, otherwise FALSE is put there -- webb. | |
10122 * Handle backslashes before special characters, like "\*" and "\ ". | |
10123 * | |
10124 * If FEAT_OSFILETYPE defined then pass initial <type> through unchanged. Eg: | |
10125 * '<html>myfile' becomes '<html>^myfile$' -- leonard. | |
10126 * | |
10127 * Returns NULL when out of memory. | |
10128 */ | |
10129 char_u * | |
10130 file_pat_to_reg_pat(pat, pat_end, allow_dirs, no_bslash) | |
10131 char_u *pat; | |
10132 char_u *pat_end; /* first char after pattern or NULL */ | |
10133 char *allow_dirs; /* Result passed back out in here */ | |
1877 | 10134 int no_bslash UNUSED; /* Don't use a backward slash as pathsep */ |
7 | 10135 { |
10136 int size; | |
10137 char_u *endp; | |
10138 char_u *reg_pat; | |
10139 char_u *p; | |
10140 int i; | |
10141 int nested = 0; | |
10142 int add_dollar = TRUE; | |
10143 #ifdef FEAT_OSFILETYPE | |
10144 int check_length = 0; | |
10145 #endif | |
10146 | |
10147 if (allow_dirs != NULL) | |
10148 *allow_dirs = FALSE; | |
10149 if (pat_end == NULL) | |
10150 pat_end = pat + STRLEN(pat); | |
10151 | |
10152 #ifdef FEAT_OSFILETYPE | |
10153 /* Find out how much of the string is the filetype check */ | |
10154 if (*pat == '<') | |
10155 { | |
10156 /* Count chars until the next '>' */ | |
10157 for (p = pat + 1; p < pat_end && *p != '>'; p++) | |
10158 ; | |
10159 if (p < pat_end) | |
10160 { | |
10161 /* Pattern is of the form <.*>.* */ | |
10162 check_length = p - pat + 1; | |
10163 if (p + 1 >= pat_end) | |
10164 { | |
10165 /* The 'pattern' is a filetype check ONLY */ | |
10166 reg_pat = (char_u *)alloc(check_length + 1); | |
10167 if (reg_pat != NULL) | |
10168 { | |
10169 mch_memmove(reg_pat, pat, (size_t)check_length); | |
10170 reg_pat[check_length] = NUL; | |
10171 } | |
10172 return reg_pat; | |
10173 } | |
10174 } | |
10175 /* else: there was no closing '>' - assume it was a normal pattern */ | |
10176 | |
10177 } | |
10178 pat += check_length; | |
10179 size = 2 + check_length; | |
10180 #else | |
10181 size = 2; /* '^' at start, '$' at end */ | |
10182 #endif | |
10183 | |
10184 for (p = pat; p < pat_end; p++) | |
10185 { | |
10186 switch (*p) | |
10187 { | |
10188 case '*': | |
10189 case '.': | |
10190 case ',': | |
10191 case '{': | |
10192 case '}': | |
10193 case '~': | |
10194 size += 2; /* extra backslash */ | |
10195 break; | |
10196 #ifdef BACKSLASH_IN_FILENAME | |
10197 case '\\': | |
10198 case '/': | |
10199 size += 4; /* could become "[\/]" */ | |
10200 break; | |
10201 #endif | |
10202 default: | |
10203 size++; | |
10204 # ifdef FEAT_MBYTE | |
474 | 10205 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 10206 { |
10207 ++p; | |
10208 ++size; | |
10209 } | |
10210 # endif | |
10211 break; | |
10212 } | |
10213 } | |
10214 reg_pat = alloc(size + 1); | |
10215 if (reg_pat == NULL) | |
10216 return NULL; | |
10217 | |
10218 #ifdef FEAT_OSFILETYPE | |
10219 /* Copy the type check in to the start. */ | |
10220 if (check_length) | |
10221 mch_memmove(reg_pat, pat - check_length, (size_t)check_length); | |
10222 i = check_length; | |
10223 #else | |
10224 i = 0; | |
10225 #endif | |
10226 | |
10227 if (pat[0] == '*') | |
10228 while (pat[0] == '*' && pat < pat_end - 1) | |
10229 pat++; | |
10230 else | |
10231 reg_pat[i++] = '^'; | |
10232 endp = pat_end - 1; | |
10233 if (*endp == '*') | |
10234 { | |
10235 while (endp - pat > 0 && *endp == '*') | |
10236 endp--; | |
10237 add_dollar = FALSE; | |
10238 } | |
10239 for (p = pat; *p && nested >= 0 && p <= endp; p++) | |
10240 { | |
10241 switch (*p) | |
10242 { | |
10243 case '*': | |
10244 reg_pat[i++] = '.'; | |
10245 reg_pat[i++] = '*'; | |
444 | 10246 while (p[1] == '*') /* "**" matches like "*" */ |
10247 ++p; | |
7 | 10248 break; |
10249 case '.': | |
10250 case '~': | |
10251 reg_pat[i++] = '\\'; | |
10252 reg_pat[i++] = *p; | |
10253 break; | |
10254 case '?': | |
10255 reg_pat[i++] = '.'; | |
10256 break; | |
10257 case '\\': | |
10258 if (p[1] == NUL) | |
10259 break; | |
10260 #ifdef BACKSLASH_IN_FILENAME | |
10261 if (!no_bslash) | |
10262 { | |
10263 /* translate: | |
10264 * "\x" to "\\x" e.g., "dir\file" | |
10265 * "\*" to "\\.*" e.g., "dir\*.c" | |
10266 * "\?" to "\\." e.g., "dir\??.c" | |
10267 * "\+" to "\+" e.g., "fileX\+.c" | |
10268 */ | |
10269 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?') | |
10270 && p[1] != '+') | |
10271 { | |
10272 reg_pat[i++] = '['; | |
10273 reg_pat[i++] = '\\'; | |
10274 reg_pat[i++] = '/'; | |
10275 reg_pat[i++] = ']'; | |
10276 if (allow_dirs != NULL) | |
10277 *allow_dirs = TRUE; | |
10278 break; | |
10279 } | |
10280 } | |
10281 #endif | |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10282 /* Undo escaping from ExpandEscape(): |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10283 * foo\?bar -> foo?bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10284 * foo\%bar -> foo%bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10285 * foo\,bar -> foo,bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10286 * foo\ bar -> foo bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10287 * Don't unescape \, * and others that are also special in a |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10288 * regexp. */ |
7 | 10289 if (*++p == '?' |
10290 #ifdef BACKSLASH_IN_FILENAME | |
10291 && no_bslash | |
10292 #endif | |
10293 ) | |
10294 reg_pat[i++] = '?'; | |
10295 else | |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10296 if (*p == ',' || *p == '%' || *p == '#' || *p == ' ') |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10297 reg_pat[i++] = *p; |
7 | 10298 else |
10299 { | |
10300 if (allow_dirs != NULL && vim_ispathsep(*p) | |
10301 #ifdef BACKSLASH_IN_FILENAME | |
10302 && (!no_bslash || *p != '\\') | |
10303 #endif | |
10304 ) | |
10305 *allow_dirs = TRUE; | |
10306 reg_pat[i++] = '\\'; | |
10307 reg_pat[i++] = *p; | |
10308 } | |
10309 break; | |
10310 #ifdef BACKSLASH_IN_FILENAME | |
10311 case '/': | |
10312 reg_pat[i++] = '['; | |
10313 reg_pat[i++] = '\\'; | |
10314 reg_pat[i++] = '/'; | |
10315 reg_pat[i++] = ']'; | |
10316 if (allow_dirs != NULL) | |
10317 *allow_dirs = TRUE; | |
10318 break; | |
10319 #endif | |
10320 case '{': | |
10321 reg_pat[i++] = '\\'; | |
10322 reg_pat[i++] = '('; | |
10323 nested++; | |
10324 break; | |
10325 case '}': | |
10326 reg_pat[i++] = '\\'; | |
10327 reg_pat[i++] = ')'; | |
10328 --nested; | |
10329 break; | |
10330 case ',': | |
10331 if (nested) | |
10332 { | |
10333 reg_pat[i++] = '\\'; | |
10334 reg_pat[i++] = '|'; | |
10335 } | |
10336 else | |
10337 reg_pat[i++] = ','; | |
10338 break; | |
10339 default: | |
10340 # ifdef FEAT_MBYTE | |
474 | 10341 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 10342 reg_pat[i++] = *p++; |
10343 else | |
10344 # endif | |
10345 if (allow_dirs != NULL && vim_ispathsep(*p)) | |
10346 *allow_dirs = TRUE; | |
10347 reg_pat[i++] = *p; | |
10348 break; | |
10349 } | |
10350 } | |
10351 if (add_dollar) | |
10352 reg_pat[i++] = '$'; | |
10353 reg_pat[i] = NUL; | |
10354 if (nested != 0) | |
10355 { | |
10356 if (nested < 0) | |
10357 EMSG(_("E219: Missing {.")); | |
10358 else | |
10359 EMSG(_("E220: Missing }.")); | |
10360 vim_free(reg_pat); | |
10361 reg_pat = NULL; | |
10362 } | |
10363 return reg_pat; | |
10364 } | |
2664 | 10365 |
10366 #if defined(EINTR) || defined(PROTO) | |
10367 /* | |
10368 * Version of read() that retries when interrupted by EINTR (possibly | |
10369 * by a SIGWINCH). | |
10370 */ | |
10371 long | |
10372 read_eintr(fd, buf, bufsize) | |
10373 int fd; | |
10374 void *buf; | |
10375 size_t bufsize; | |
10376 { | |
10377 long ret; | |
10378 | |
10379 for (;;) | |
10380 { | |
10381 ret = vim_read(fd, buf, bufsize); | |
10382 if (ret >= 0 || errno != EINTR) | |
10383 break; | |
10384 } | |
10385 return ret; | |
10386 } | |
10387 | |
10388 /* | |
10389 * Version of write() that retries when interrupted by EINTR (possibly | |
10390 * by a SIGWINCH). | |
10391 */ | |
10392 long | |
10393 write_eintr(fd, buf, bufsize) | |
10394 int fd; | |
10395 void *buf; | |
10396 size_t bufsize; | |
10397 { | |
10398 long ret = 0; | |
10399 long wlen; | |
10400 | |
10401 /* Repeat the write() so long it didn't fail, other than being interrupted | |
10402 * by a signal. */ | |
10403 while (ret < (long)bufsize) | |
10404 { | |
2666 | 10405 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret); |
2664 | 10406 if (wlen < 0) |
10407 { | |
10408 if (errno != EINTR) | |
10409 break; | |
10410 } | |
10411 else | |
10412 ret += wlen; | |
10413 } | |
10414 return ret; | |
10415 } | |
10416 #endif |