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