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