Mercurial > vim
comparison src/fileio.c @ 18779:8f05b3cf8557 v8.1.2379
patch 8.1.2379: using old C style comments
Commit: https://github.com/vim/vim/commit/217e1b8359447f5550dcb0d1ee43380a90c253c5
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun Dec 1 21:41:28 2019 +0100
patch 8.1.2379: using old C style comments
Problem: Using old C style comments.
Solution: Use // comments where appropriate.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sun, 01 Dec 2019 21:45:04 +0100 |
parents | 068337e86133 |
children | 4481f3b29fc5 |
comparison
equal
deleted
inserted
replaced
18778:2182f82b04e4 | 18779:8f05b3cf8557 |
---|---|
12 */ | 12 */ |
13 | 13 |
14 #include "vim.h" | 14 #include "vim.h" |
15 | 15 |
16 #if defined(__TANDEM) || defined(__MINT__) | 16 #if defined(__TANDEM) || defined(__MINT__) |
17 # include <limits.h> /* for SSIZE_MAX */ | 17 # include <limits.h> // for SSIZE_MAX |
18 #endif | 18 #endif |
19 | 19 |
20 /* Is there any system that doesn't have access()? */ | 20 // Is there any system that doesn't have access()? |
21 #define USE_MCH_ACCESS | 21 #define USE_MCH_ACCESS |
22 | 22 |
23 static char_u *next_fenc(char_u **pp, int *alloced); | 23 static char_u *next_fenc(char_u **pp, int *alloced); |
24 #ifdef FEAT_EVAL | 24 #ifdef FEAT_EVAL |
25 static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp); | 25 static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp); |
41 int msg_scroll_save; | 41 int msg_scroll_save; |
42 int prev_msg_col = msg_col; | 42 int prev_msg_col = msg_col; |
43 | 43 |
44 if (msg_silent != 0) | 44 if (msg_silent != 0) |
45 return; | 45 return; |
46 msg_add_fname(buf, name); /* put file name in IObuff with quotes */ | 46 msg_add_fname(buf, name); // put file name in IObuff with quotes |
47 /* If it's extremely long, truncate it. */ | 47 // If it's extremely long, truncate it. |
48 if (STRLEN(IObuff) > IOSIZE - 80) | 48 if (STRLEN(IObuff) > IOSIZE - 80) |
49 IObuff[IOSIZE - 80] = NUL; | 49 IObuff[IOSIZE - 80] = NUL; |
50 STRCAT(IObuff, s); | 50 STRCAT(IObuff, s); |
51 /* | 51 /* |
52 * For the first message may have to start a new line. | 52 * For the first message may have to start a new line. |
54 * calling filemess(). | 54 * calling filemess(). |
55 */ | 55 */ |
56 msg_scroll_save = msg_scroll; | 56 msg_scroll_save = msg_scroll; |
57 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) | 57 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) |
58 msg_scroll = FALSE; | 58 msg_scroll = FALSE; |
59 if (!msg_scroll) /* wait a bit when overwriting an error msg */ | 59 if (!msg_scroll) // wait a bit when overwriting an error msg |
60 check_for_delay(FALSE); | 60 check_for_delay(FALSE); |
61 msg_start(); | 61 msg_start(); |
62 if (prev_msg_col != 0 && msg_col == 0) | 62 if (prev_msg_col != 0 && msg_col == 0) |
63 msg_putchar('\r'); // overwrite any previous message. | 63 msg_putchar('\r'); // overwrite any previous message. |
64 msg_scroll = msg_scroll_save; | 64 msg_scroll = msg_scroll_save; |
65 msg_scrolled_ign = TRUE; | 65 msg_scrolled_ign = TRUE; |
66 /* may truncate the message to avoid a hit-return prompt */ | 66 // may truncate the message to avoid a hit-return prompt |
67 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr); | 67 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr); |
68 msg_clr_eos(); | 68 msg_clr_eos(); |
69 out_flush(); | 69 out_flush(); |
70 msg_scrolled_ign = FALSE; | 70 msg_scrolled_ign = FALSE; |
71 } | 71 } |
100 char_u *fname, | 100 char_u *fname, |
101 char_u *sfname, | 101 char_u *sfname, |
102 linenr_T from, | 102 linenr_T from, |
103 linenr_T lines_to_skip, | 103 linenr_T lines_to_skip, |
104 linenr_T lines_to_read, | 104 linenr_T lines_to_read, |
105 exarg_T *eap, /* can be NULL! */ | 105 exarg_T *eap, // can be NULL! |
106 int flags) | 106 int flags) |
107 { | 107 { |
108 int fd = 0; | 108 int fd = 0; |
109 int newfile = (flags & READ_NEW); | 109 int newfile = (flags & READ_NEW); |
110 int check_readonly; | 110 int check_readonly; |
112 int read_stdin = (flags & READ_STDIN); | 112 int read_stdin = (flags & READ_STDIN); |
113 int read_buffer = (flags & READ_BUFFER); | 113 int read_buffer = (flags & READ_BUFFER); |
114 int read_fifo = (flags & READ_FIFO); | 114 int read_fifo = (flags & READ_FIFO); |
115 int set_options = newfile || read_buffer | 115 int set_options = newfile || read_buffer |
116 || (eap != NULL && eap->read_edit); | 116 || (eap != NULL && eap->read_edit); |
117 linenr_T read_buf_lnum = 1; /* next line to read from curbuf */ | 117 linenr_T read_buf_lnum = 1; // next line to read from curbuf |
118 colnr_T read_buf_col = 0; /* next char to read from this line */ | 118 colnr_T read_buf_col = 0; // next char to read from this line |
119 char_u c; | 119 char_u c; |
120 linenr_T lnum = from; | 120 linenr_T lnum = from; |
121 char_u *ptr = NULL; /* pointer into read buffer */ | 121 char_u *ptr = NULL; // pointer into read buffer |
122 char_u *buffer = NULL; /* read buffer */ | 122 char_u *buffer = NULL; // read buffer |
123 char_u *new_buffer = NULL; /* init to shut up gcc */ | 123 char_u *new_buffer = NULL; // init to shut up gcc |
124 char_u *line_start = NULL; /* init to shut up gcc */ | 124 char_u *line_start = NULL; // init to shut up gcc |
125 int wasempty; /* buffer was empty before reading */ | 125 int wasempty; // buffer was empty before reading |
126 colnr_T len; | 126 colnr_T len; |
127 long size = 0; | 127 long size = 0; |
128 char_u *p; | 128 char_u *p; |
129 off_T filesize = 0; | 129 off_T filesize = 0; |
130 int skip_read = FALSE; | 130 int skip_read = FALSE; |
134 #endif | 134 #endif |
135 #ifdef FEAT_PERSISTENT_UNDO | 135 #ifdef FEAT_PERSISTENT_UNDO |
136 context_sha256_T sha_ctx; | 136 context_sha256_T sha_ctx; |
137 int read_undo_file = FALSE; | 137 int read_undo_file = FALSE; |
138 #endif | 138 #endif |
139 int split = 0; /* number of split lines */ | 139 int split = 0; // number of split lines |
140 #define UNKNOWN 0x0fffffff /* file size is unknown */ | 140 #define UNKNOWN 0x0fffffff // file size is unknown |
141 linenr_T linecnt; | 141 linenr_T linecnt; |
142 int error = FALSE; /* errors encountered */ | 142 int error = FALSE; // errors encountered |
143 int ff_error = EOL_UNKNOWN; /* file format with errors */ | 143 int ff_error = EOL_UNKNOWN; // file format with errors |
144 long linerest = 0; /* remaining chars in line */ | 144 long linerest = 0; // remaining chars in line |
145 #ifdef UNIX | 145 #ifdef UNIX |
146 int perm = 0; | 146 int perm = 0; |
147 int swap_mode = -1; /* protection bits for swap file */ | 147 int swap_mode = -1; // protection bits for swap file |
148 #else | 148 #else |
149 int perm; | 149 int perm; |
150 #endif | 150 #endif |
151 int fileformat = 0; /* end-of-line format */ | 151 int fileformat = 0; // end-of-line format |
152 int keep_fileformat = FALSE; | 152 int keep_fileformat = FALSE; |
153 stat_T st; | 153 stat_T st; |
154 int file_readonly; | 154 int file_readonly; |
155 linenr_T skip_count = 0; | 155 linenr_T skip_count = 0; |
156 linenr_T read_count = 0; | 156 linenr_T read_count = 0; |
157 int msg_save = msg_scroll; | 157 int msg_save = msg_scroll; |
158 linenr_T read_no_eol_lnum = 0; /* non-zero lnum when last line of | 158 linenr_T read_no_eol_lnum = 0; // non-zero lnum when last line of |
159 * last read was missing the eol */ | 159 // last read was missing the eol |
160 int try_mac; | 160 int try_mac; |
161 int try_dos; | 161 int try_dos; |
162 int try_unix; | 162 int try_unix; |
163 int file_rewind = FALSE; | 163 int file_rewind = FALSE; |
164 int can_retry; | 164 int can_retry; |
165 linenr_T conv_error = 0; /* line nr with conversion error */ | 165 linenr_T conv_error = 0; // line nr with conversion error |
166 linenr_T illegal_byte = 0; /* line nr with illegal byte */ | 166 linenr_T illegal_byte = 0; // line nr with illegal byte |
167 int keep_dest_enc = FALSE; /* don't retry when char doesn't fit | 167 int keep_dest_enc = FALSE; // don't retry when char doesn't fit |
168 in destination encoding */ | 168 // in destination encoding |
169 int bad_char_behavior = BAD_REPLACE; | 169 int bad_char_behavior = BAD_REPLACE; |
170 /* BAD_KEEP, BAD_DROP or character to | 170 // BAD_KEEP, BAD_DROP or character to |
171 * replace with */ | 171 // replace with |
172 char_u *tmpname = NULL; /* name of 'charconvert' output file */ | 172 char_u *tmpname = NULL; // name of 'charconvert' output file |
173 int fio_flags = 0; | 173 int fio_flags = 0; |
174 char_u *fenc; /* fileencoding to use */ | 174 char_u *fenc; // fileencoding to use |
175 int fenc_alloced; /* fenc_next is in allocated memory */ | 175 int fenc_alloced; // fenc_next is in allocated memory |
176 char_u *fenc_next = NULL; /* next item in 'fencs' or NULL */ | 176 char_u *fenc_next = NULL; // next item in 'fencs' or NULL |
177 int advance_fenc = FALSE; | 177 int advance_fenc = FALSE; |
178 long real_size = 0; | 178 long real_size = 0; |
179 #ifdef USE_ICONV | 179 #ifdef USE_ICONV |
180 iconv_t iconv_fd = (iconv_t)-1; /* descriptor for iconv() or -1 */ | 180 iconv_t iconv_fd = (iconv_t)-1; // descriptor for iconv() or -1 |
181 # ifdef FEAT_EVAL | 181 # ifdef FEAT_EVAL |
182 int did_iconv = FALSE; /* TRUE when iconv() failed and trying | 182 int did_iconv = FALSE; // TRUE when iconv() failed and trying |
183 'charconvert' next */ | 183 // 'charconvert' next |
184 # endif | 184 # endif |
185 #endif | 185 #endif |
186 int converted = FALSE; /* TRUE if conversion done */ | 186 int converted = FALSE; // TRUE if conversion done |
187 int notconverted = FALSE; /* TRUE if conversion wanted but it | 187 int notconverted = FALSE; // TRUE if conversion wanted but it |
188 wasn't possible */ | 188 // wasn't possible |
189 char_u conv_rest[CONV_RESTLEN]; | 189 char_u conv_rest[CONV_RESTLEN]; |
190 int conv_restlen = 0; /* nr of bytes in conv_rest[] */ | 190 int conv_restlen = 0; // nr of bytes in conv_rest[] |
191 pos_T orig_start; | 191 pos_T orig_start; |
192 buf_T *old_curbuf; | 192 buf_T *old_curbuf; |
193 char_u *old_b_ffname; | 193 char_u *old_b_ffname; |
194 char_u *old_b_fname; | 194 char_u *old_b_fname; |
195 int using_b_ffname; | 195 int using_b_ffname; |
196 int using_b_fname; | 196 int using_b_fname; |
197 | 197 |
198 au_did_filetype = FALSE; /* reset before triggering any autocommands */ | 198 au_did_filetype = FALSE; // reset before triggering any autocommands |
199 | 199 |
200 curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */ | 200 curbuf->b_no_eol_lnum = 0; // in case it was set by the previous read |
201 | 201 |
202 /* | 202 /* |
203 * If there is no file name yet, use the one for the read file. | 203 * If there is no file name yet, use the one for the read file. |
204 * BF_NOTEDITED is set to reflect this. | 204 * BF_NOTEDITED is set to reflect this. |
205 * Don't do this for a read from a filter. | 205 * Don't do this for a read from a filter. |
213 { | 213 { |
214 if (set_rw_fname(fname, sfname) == FAIL) | 214 if (set_rw_fname(fname, sfname) == FAIL) |
215 return FAIL; | 215 return FAIL; |
216 } | 216 } |
217 | 217 |
218 /* Remember the initial values of curbuf, curbuf->b_ffname and | 218 // Remember the initial values of curbuf, curbuf->b_ffname and |
219 * curbuf->b_fname to detect whether they are altered as a result of | 219 // curbuf->b_fname to detect whether they are altered as a result of |
220 * executing nasty autocommands. Also check if "fname" and "sfname" | 220 // executing nasty autocommands. Also check if "fname" and "sfname" |
221 * point to one of these values. */ | 221 // point to one of these values. |
222 old_curbuf = curbuf; | 222 old_curbuf = curbuf; |
223 old_b_ffname = curbuf->b_ffname; | 223 old_b_ffname = curbuf->b_ffname; |
224 old_b_fname = curbuf->b_fname; | 224 old_b_fname = curbuf->b_fname; |
225 using_b_ffname = (fname == curbuf->b_ffname) | 225 using_b_ffname = (fname == curbuf->b_ffname) |
226 || (sfname == curbuf->b_ffname); | 226 || (sfname == curbuf->b_ffname); |
227 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname); | 227 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname); |
228 | 228 |
229 /* After reading a file the cursor line changes but we don't want to | 229 // After reading a file the cursor line changes but we don't want to |
230 * display the line. */ | 230 // display the line. |
231 ex_no_reprint = TRUE; | 231 ex_no_reprint = TRUE; |
232 | 232 |
233 /* don't display the file info for another buffer now */ | 233 // don't display the file info for another buffer now |
234 need_fileinfo = FALSE; | 234 need_fileinfo = FALSE; |
235 | 235 |
236 /* | 236 /* |
237 * For Unix: Use the short file name whenever possible. | 237 * For Unix: Use the short file name whenever possible. |
238 * Avoids problems with networks and when directory names are changed. | 238 * Avoids problems with networks and when directory names are changed. |
251 */ | 251 */ |
252 if (!filtering && !read_stdin && !read_buffer) | 252 if (!filtering && !read_stdin && !read_buffer) |
253 { | 253 { |
254 orig_start = curbuf->b_op_start; | 254 orig_start = curbuf->b_op_start; |
255 | 255 |
256 /* Set '[ mark to the line above where the lines go (line 1 if zero). */ | 256 // Set '[ mark to the line above where the lines go (line 1 if zero). |
257 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); | 257 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); |
258 curbuf->b_op_start.col = 0; | 258 curbuf->b_op_start.col = 0; |
259 | 259 |
260 if (newfile) | 260 if (newfile) |
261 { | 261 { |
277 | 277 |
278 curbuf->b_op_start = orig_start; | 278 curbuf->b_op_start = orig_start; |
279 } | 279 } |
280 | 280 |
281 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0) | 281 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0) |
282 msg_scroll = FALSE; /* overwrite previous file message */ | 282 msg_scroll = FALSE; // overwrite previous file message |
283 else | 283 else |
284 msg_scroll = TRUE; /* don't overwrite previous file message */ | 284 msg_scroll = TRUE; // don't overwrite previous file message |
285 | 285 |
286 /* | 286 /* |
287 * If the name ends in a path separator, we can't open it. Check here, | 287 * If the name ends in a path separator, we can't open it. Check here, |
288 * because reading the file may actually work, but then creating the swap | 288 * because reading the file may actually work, but then creating the swap |
289 * file may destroy it! Reported on MS-DOS and Win 95. | 289 * file may destroy it! Reported on MS-DOS and Win 95. |
307 /* | 307 /* |
308 * On Unix it is possible to read a directory, so we have to | 308 * On Unix it is possible to read a directory, so we have to |
309 * check for it before the mch_open(). | 309 * check for it before the mch_open(). |
310 */ | 310 */ |
311 perm = mch_getperm(fname); | 311 perm = mch_getperm(fname); |
312 if (perm >= 0 && !S_ISREG(perm) /* not a regular file ... */ | 312 if (perm >= 0 && !S_ISREG(perm) // not a regular file ... |
313 && !S_ISFIFO(perm) /* ... or fifo */ | 313 && !S_ISFIFO(perm) // ... or fifo |
314 && !S_ISSOCK(perm) /* ... or socket */ | 314 && !S_ISSOCK(perm) // ... or socket |
315 # ifdef OPEN_CHR_FILES | 315 # ifdef OPEN_CHR_FILES |
316 && !(S_ISCHR(perm) && is_dev_fd_file(fname)) | 316 && !(S_ISCHR(perm) && is_dev_fd_file(fname)) |
317 /* ... or a character special file named /dev/fd/<n> */ | 317 // ... or a character special file named /dev/fd/<n> |
318 # endif | 318 # endif |
319 ) | 319 ) |
320 { | 320 { |
321 int retval = FAIL; | 321 int retval = FAIL; |
322 | 322 |
345 return FAIL; | 345 return FAIL; |
346 } | 346 } |
347 #endif | 347 #endif |
348 } | 348 } |
349 | 349 |
350 /* Set default or forced 'fileformat' and 'binary'. */ | 350 // Set default or forced 'fileformat' and 'binary'. |
351 set_file_options(set_options, eap); | 351 set_file_options(set_options, eap); |
352 | 352 |
353 /* | 353 /* |
354 * When opening a new file we take the readonly flag from the file. | 354 * When opening a new file we take the readonly flag from the file. |
355 * Default is r/w, can be set to r/o below. | 355 * Default is r/w, can be set to r/o below. |
360 if (check_readonly && !readonlymode) | 360 if (check_readonly && !readonlymode) |
361 curbuf->b_p_ro = FALSE; | 361 curbuf->b_p_ro = FALSE; |
362 | 362 |
363 if (newfile && !read_stdin && !read_buffer && !read_fifo) | 363 if (newfile && !read_stdin && !read_buffer && !read_fifo) |
364 { | 364 { |
365 /* Remember time of file. */ | 365 // Remember time of file. |
366 if (mch_stat((char *)fname, &st) >= 0) | 366 if (mch_stat((char *)fname, &st) >= 0) |
367 { | 367 { |
368 buf_store_time(curbuf, &st, fname); | 368 buf_store_time(curbuf, &st, fname); |
369 curbuf->b_mtime_read = curbuf->b_mtime; | 369 curbuf->b_mtime_read = curbuf->b_mtime; |
370 #ifdef UNIX | 370 #ifdef UNIX |
380 * Setting the bits is done below, after creating the swap file. | 380 * Setting the bits is done below, after creating the swap file. |
381 */ | 381 */ |
382 swap_mode = (st.st_mode & 0644) | 0600; | 382 swap_mode = (st.st_mode & 0644) | 0600; |
383 #endif | 383 #endif |
384 #ifdef FEAT_CW_EDITOR | 384 #ifdef FEAT_CW_EDITOR |
385 /* Get the FSSpec on MacOS | 385 // Get the FSSpec on MacOS |
386 * TODO: Update it properly when the buffer name changes | 386 // TODO: Update it properly when the buffer name changes |
387 */ | |
388 (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec); | 387 (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec); |
389 #endif | 388 #endif |
390 #ifdef VMS | 389 #ifdef VMS |
391 curbuf->b_fab_rfm = st.st_fab_rfm; | 390 curbuf->b_fab_rfm = st.st_fab_rfm; |
392 curbuf->b_fab_rat = st.st_fab_rat; | 391 curbuf->b_fab_rat = st.st_fab_rat; |
399 curbuf->b_mtime_read = 0; | 398 curbuf->b_mtime_read = 0; |
400 curbuf->b_orig_size = 0; | 399 curbuf->b_orig_size = 0; |
401 curbuf->b_orig_mode = 0; | 400 curbuf->b_orig_mode = 0; |
402 } | 401 } |
403 | 402 |
404 /* Reset the "new file" flag. It will be set again below when the | 403 // Reset the "new file" flag. It will be set again below when the |
405 * file doesn't exist. */ | 404 // file doesn't exist. |
406 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W); | 405 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W); |
407 } | 406 } |
408 | 407 |
409 /* | 408 /* |
410 * for UNIX: check readonly with perm and mch_access() | 409 * for UNIX: check readonly with perm and mch_access() |
412 */ | 411 */ |
413 file_readonly = FALSE; | 412 file_readonly = FALSE; |
414 if (read_stdin) | 413 if (read_stdin) |
415 { | 414 { |
416 #if defined(MSWIN) | 415 #if defined(MSWIN) |
417 /* Force binary I/O on stdin to avoid CR-LF -> LF conversion. */ | 416 // Force binary I/O on stdin to avoid CR-LF -> LF conversion. |
418 setmode(0, O_BINARY); | 417 setmode(0, O_BINARY); |
419 #endif | 418 #endif |
420 } | 419 } |
421 else if (!read_buffer) | 420 else if (!read_buffer) |
422 { | 421 { |
432 if (!newfile | 431 if (!newfile |
433 || readonlymode | 432 || readonlymode |
434 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0) | 433 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0) |
435 { | 434 { |
436 file_readonly = TRUE; | 435 file_readonly = TRUE; |
437 /* try to open ro */ | 436 // try to open ro |
438 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | 437 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); |
439 } | 438 } |
440 #endif | 439 #endif |
441 } | 440 } |
442 | 441 |
443 if (fd < 0) /* cannot open at all */ | 442 if (fd < 0) // cannot open at all |
444 { | 443 { |
445 #ifndef UNIX | 444 #ifndef UNIX |
446 int isdir_f; | 445 int isdir_f; |
447 #endif | 446 #endif |
448 msg_scroll = msg_save; | 447 msg_scroll = msg_save; |
449 #ifndef UNIX | 448 #ifndef UNIX |
450 /* | 449 /* |
451 * On Amiga we can't open a directory, check here. | 450 * On Amiga we can't open a directory, check here. |
452 */ | 451 */ |
453 isdir_f = (mch_isdir(fname)); | 452 isdir_f = (mch_isdir(fname)); |
454 perm = mch_getperm(fname); /* check if the file exists */ | 453 perm = mch_getperm(fname); // check if the file exists |
455 if (isdir_f) | 454 if (isdir_f) |
456 { | 455 { |
457 filemess(curbuf, sfname, (char_u *)_("is a directory"), 0); | 456 filemess(curbuf, sfname, (char_u *)_("is a directory"), 0); |
458 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | 457 curbuf->b_p_ro = TRUE; // must use "w!" now |
459 } | 458 } |
460 else | 459 else |
461 #endif | 460 #endif |
462 if (newfile) | 461 if (newfile) |
463 { | 462 { |
471 * Set the 'new-file' flag, so that when the file has | 470 * Set the 'new-file' flag, so that when the file has |
472 * been created by someone else, a ":w" will complain. | 471 * been created by someone else, a ":w" will complain. |
473 */ | 472 */ |
474 curbuf->b_flags |= BF_NEW; | 473 curbuf->b_flags |= BF_NEW; |
475 | 474 |
476 /* Create a swap file now, so that other Vims are warned | 475 // Create a swap file now, so that other Vims are warned |
477 * that we are editing this file. Don't do this for a | 476 // that we are editing this file. Don't do this for a |
478 * "nofile" or "nowrite" buffer type. */ | 477 // "nofile" or "nowrite" buffer type. |
479 #ifdef FEAT_QUICKFIX | 478 #ifdef FEAT_QUICKFIX |
480 if (!bt_dontwrite(curbuf)) | 479 if (!bt_dontwrite(curbuf)) |
481 #endif | 480 #endif |
482 { | 481 { |
483 check_need_swap(newfile); | 482 check_need_swap(newfile); |
484 /* SwapExists autocommand may mess things up */ | 483 // SwapExists autocommand may mess things up |
485 if (curbuf != old_curbuf | 484 if (curbuf != old_curbuf |
486 || (using_b_ffname | 485 || (using_b_ffname |
487 && (old_b_ffname != curbuf->b_ffname)) | 486 && (old_b_ffname != curbuf->b_ffname)) |
488 || (using_b_fname | 487 || (using_b_fname |
489 && (old_b_fname != curbuf->b_fname))) | 488 && (old_b_fname != curbuf->b_fname))) |
496 filemess(curbuf, sfname, (char_u *)_("[New File]"), 0); | 495 filemess(curbuf, sfname, (char_u *)_("[New File]"), 0); |
497 else | 496 else |
498 filemess(curbuf, sfname, | 497 filemess(curbuf, sfname, |
499 (char_u *)_("[New DIRECTORY]"), 0); | 498 (char_u *)_("[New DIRECTORY]"), 0); |
500 #ifdef FEAT_VIMINFO | 499 #ifdef FEAT_VIMINFO |
501 /* Even though this is a new file, it might have been | 500 // Even though this is a new file, it might have been |
502 * edited before and deleted. Get the old marks. */ | 501 // edited before and deleted. Get the old marks. |
503 check_marks_read(); | 502 check_marks_read(); |
504 #endif | 503 #endif |
505 /* Set forced 'fileencoding'. */ | 504 // Set forced 'fileencoding'. |
506 if (eap != NULL) | 505 if (eap != NULL) |
507 set_forced_fenc(eap); | 506 set_forced_fenc(eap); |
508 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname, | 507 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname, |
509 FALSE, curbuf, eap); | 508 FALSE, curbuf, eap); |
510 /* remember the current fileformat */ | 509 // remember the current fileformat |
511 save_file_ff(curbuf); | 510 save_file_ff(curbuf); |
512 | 511 |
513 #if defined(FEAT_EVAL) | 512 #if defined(FEAT_EVAL) |
514 if (aborting()) /* autocmds may abort script processing */ | 513 if (aborting()) // autocmds may abort script processing |
515 return FAIL; | 514 return FAIL; |
516 #endif | 515 #endif |
517 return OK; /* a new file is not an error */ | 516 return OK; // a new file is not an error |
518 } | 517 } |
519 else | 518 else |
520 { | 519 { |
521 filemess(curbuf, sfname, (char_u *)( | 520 filemess(curbuf, sfname, (char_u *)( |
522 # ifdef EFBIG | 521 # ifdef EFBIG |
524 # endif | 523 # endif |
525 # ifdef EOVERFLOW | 524 # ifdef EOVERFLOW |
526 (errno == EOVERFLOW) ? _("[File too big]") : | 525 (errno == EOVERFLOW) ? _("[File too big]") : |
527 # endif | 526 # endif |
528 _("[Permission Denied]")), 0); | 527 _("[Permission Denied]")), 0); |
529 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | 528 curbuf->b_p_ro = TRUE; // must use "w!" now |
530 } | 529 } |
531 } | 530 } |
532 | 531 |
533 return FAIL; | 532 return FAIL; |
534 } | 533 } |
540 if ((check_readonly && file_readonly) || curbuf->b_help) | 539 if ((check_readonly && file_readonly) || curbuf->b_help) |
541 curbuf->b_p_ro = TRUE; | 540 curbuf->b_p_ro = TRUE; |
542 | 541 |
543 if (set_options) | 542 if (set_options) |
544 { | 543 { |
545 /* Don't change 'eol' if reading from buffer as it will already be | 544 // Don't change 'eol' if reading from buffer as it will already be |
546 * correctly set when reading stdin. */ | 545 // correctly set when reading stdin. |
547 if (!read_buffer) | 546 if (!read_buffer) |
548 { | 547 { |
549 curbuf->b_p_eol = TRUE; | 548 curbuf->b_p_eol = TRUE; |
550 curbuf->b_start_eol = TRUE; | 549 curbuf->b_start_eol = TRUE; |
551 } | 550 } |
552 curbuf->b_p_bomb = FALSE; | 551 curbuf->b_p_bomb = FALSE; |
553 curbuf->b_start_bomb = FALSE; | 552 curbuf->b_start_bomb = FALSE; |
554 } | 553 } |
555 | 554 |
556 /* Create a swap file now, so that other Vims are warned that we are | 555 // Create a swap file now, so that other Vims are warned that we are |
557 * editing this file. | 556 // editing this file. |
558 * Don't do this for a "nofile" or "nowrite" buffer type. */ | 557 // Don't do this for a "nofile" or "nowrite" buffer type. |
559 #ifdef FEAT_QUICKFIX | 558 #ifdef FEAT_QUICKFIX |
560 if (!bt_dontwrite(curbuf)) | 559 if (!bt_dontwrite(curbuf)) |
561 #endif | 560 #endif |
562 { | 561 { |
563 check_need_swap(newfile); | 562 check_need_swap(newfile); |
569 if (!read_buffer) | 568 if (!read_buffer) |
570 close(fd); | 569 close(fd); |
571 return FAIL; | 570 return FAIL; |
572 } | 571 } |
573 #ifdef UNIX | 572 #ifdef UNIX |
574 /* Set swap file protection bits after creating it. */ | 573 // Set swap file protection bits after creating it. |
575 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL | 574 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL |
576 && curbuf->b_ml.ml_mfp->mf_fname != NULL) | 575 && curbuf->b_ml.ml_mfp->mf_fname != NULL) |
577 { | 576 { |
578 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname; | 577 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname; |
579 | 578 |
609 if (!read_buffer && !read_stdin) | 608 if (!read_buffer && !read_stdin) |
610 close(fd); | 609 close(fd); |
611 return FAIL; | 610 return FAIL; |
612 } | 611 } |
613 | 612 |
614 ++no_wait_return; /* don't wait for return yet */ | 613 ++no_wait_return; // don't wait for return yet |
615 | 614 |
616 /* | 615 /* |
617 * Set '[ mark to the line above where the lines go (line 1 if zero). | 616 * Set '[ mark to the line above where the lines go (line 1 if zero). |
618 */ | 617 */ |
619 orig_start = curbuf->b_op_start; | 618 orig_start = curbuf->b_op_start; |
632 /* | 631 /* |
633 * The file must be closed again, the autocommands may want to change | 632 * The file must be closed again, the autocommands may want to change |
634 * the file before reading it. | 633 * the file before reading it. |
635 */ | 634 */ |
636 if (!read_stdin) | 635 if (!read_stdin) |
637 close(fd); /* ignore errors */ | 636 close(fd); // ignore errors |
638 | 637 |
639 /* | 638 /* |
640 * The output from the autocommands should not overwrite anything and | 639 * The output from the autocommands should not overwrite anything and |
641 * should not be overwritten: Set msg_scroll, restore its value if no | 640 * should not be overwritten: Set msg_scroll, restore its value if no |
642 * output was done. | 641 * output was done. |
652 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname, | 651 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname, |
653 FALSE, curbuf, eap); | 652 FALSE, curbuf, eap); |
654 else | 653 else |
655 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname, | 654 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname, |
656 FALSE, NULL, eap); | 655 FALSE, NULL, eap); |
657 /* autocommands may have changed it */ | 656 // autocommands may have changed it |
658 try_mac = (vim_strchr(p_ffs, 'm') != NULL); | 657 try_mac = (vim_strchr(p_ffs, 'm') != NULL); |
659 try_dos = (vim_strchr(p_ffs, 'd') != NULL); | 658 try_dos = (vim_strchr(p_ffs, 'd') != NULL); |
660 try_unix = (vim_strchr(p_ffs, 'x') != NULL); | 659 try_unix = (vim_strchr(p_ffs, 'x') != NULL); |
661 curbuf->b_op_start = orig_start; | 660 curbuf->b_op_start = orig_start; |
662 | 661 |
663 if (msg_scrolled == n) | 662 if (msg_scrolled == n) |
664 msg_scroll = m; | 663 msg_scroll = m; |
665 | 664 |
666 #ifdef FEAT_EVAL | 665 #ifdef FEAT_EVAL |
667 if (aborting()) /* autocmds may abort script processing */ | 666 if (aborting()) // autocmds may abort script processing |
668 { | 667 { |
669 --no_wait_return; | 668 --no_wait_return; |
670 msg_scroll = msg_save; | 669 msg_scroll = msg_save; |
671 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | 670 curbuf->b_p_ro = TRUE; // must use "w!" now |
672 return FAIL; | 671 return FAIL; |
673 } | 672 } |
674 #endif | 673 #endif |
675 /* | 674 /* |
676 * Don't allow the autocommands to change the current buffer. | 675 * Don't allow the autocommands to change the current buffer. |
688 msg_scroll = msg_save; | 687 msg_scroll = msg_save; |
689 if (fd < 0) | 688 if (fd < 0) |
690 emsg(_("E200: *ReadPre autocommands made the file unreadable")); | 689 emsg(_("E200: *ReadPre autocommands made the file unreadable")); |
691 else | 690 else |
692 emsg(_("E201: *ReadPre autocommands must not change current buffer")); | 691 emsg(_("E201: *ReadPre autocommands must not change current buffer")); |
693 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | 692 curbuf->b_p_ro = TRUE; // must use "w!" now |
694 return FAIL; | 693 return FAIL; |
695 } | 694 } |
696 } | 695 } |
697 | 696 |
698 /* Autocommands may add lines to the file, need to check if it is empty */ | 697 // Autocommands may add lines to the file, need to check if it is empty |
699 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY); | 698 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY); |
700 | 699 |
701 if (!recoverymode && !filtering && !(flags & READ_DUMMY)) | 700 if (!recoverymode && !filtering && !(flags & READ_DUMMY)) |
702 { | 701 { |
703 /* | 702 /* |
715 if (!gui.in_use) | 714 if (!gui.in_use) |
716 # endif | 715 # endif |
717 mch_msg(_("Vim: Reading from stdin...\n")); | 716 mch_msg(_("Vim: Reading from stdin...\n")); |
718 #endif | 717 #endif |
719 #ifdef FEAT_GUI | 718 #ifdef FEAT_GUI |
720 /* Also write a message in the GUI window, if there is one. */ | 719 // Also write a message in the GUI window, if there is one. |
721 if (gui.in_use && !gui.dying && !gui.starting) | 720 if (gui.in_use && !gui.dying && !gui.starting) |
722 { | 721 { |
723 p = (char_u *)_("Reading from stdin..."); | 722 p = (char_u *)_("Reading from stdin..."); |
724 gui_write(p, (int)STRLEN(p)); | 723 gui_write(p, (int)STRLEN(p)); |
725 } | 724 } |
728 } | 727 } |
729 else if (!read_buffer) | 728 else if (!read_buffer) |
730 filemess(curbuf, sfname, (char_u *)"", 0); | 729 filemess(curbuf, sfname, (char_u *)"", 0); |
731 } | 730 } |
732 | 731 |
733 msg_scroll = FALSE; /* overwrite the file message */ | 732 msg_scroll = FALSE; // overwrite the file message |
734 | 733 |
735 /* | 734 /* |
736 * Set linecnt now, before the "retry" caused by a wrong guess for | 735 * Set linecnt now, before the "retry" caused by a wrong guess for |
737 * fileformat, and after the autocommands, which may change them. | 736 * fileformat, and after the autocommands, which may change them. |
738 */ | 737 */ |
739 linecnt = curbuf->b_ml.ml_line_count; | 738 linecnt = curbuf->b_ml.ml_line_count; |
740 | 739 |
741 /* "++bad=" argument. */ | 740 // "++bad=" argument. |
742 if (eap != NULL && eap->bad_char != 0) | 741 if (eap != NULL && eap->bad_char != 0) |
743 { | 742 { |
744 bad_char_behavior = eap->bad_char; | 743 bad_char_behavior = eap->bad_char; |
745 if (set_options) | 744 if (set_options) |
746 curbuf->b_bad_char = eap->bad_char; | 745 curbuf->b_bad_char = eap->bad_char; |
757 fenc_alloced = TRUE; | 756 fenc_alloced = TRUE; |
758 keep_dest_enc = TRUE; | 757 keep_dest_enc = TRUE; |
759 } | 758 } |
760 else if (curbuf->b_p_bin) | 759 else if (curbuf->b_p_bin) |
761 { | 760 { |
762 fenc = (char_u *)""; /* binary: don't convert */ | 761 fenc = (char_u *)""; // binary: don't convert |
763 fenc_alloced = FALSE; | 762 fenc_alloced = FALSE; |
764 } | 763 } |
765 else if (curbuf->b_help) | 764 else if (curbuf->b_help) |
766 { | 765 { |
767 char_u firstline[80]; | 766 char_u firstline[80]; |
768 int fc; | 767 int fc; |
769 | 768 |
770 /* Help files are either utf-8 or latin1. Try utf-8 first, if this | 769 // Help files are either utf-8 or latin1. Try utf-8 first, if this |
771 * fails it must be latin1. | 770 // fails it must be latin1. |
772 * Always do this when 'encoding' is "utf-8". Otherwise only do | 771 // Always do this when 'encoding' is "utf-8". Otherwise only do |
773 * this when needed to avoid [converted] remarks all the time. | 772 // this when needed to avoid [converted] remarks all the time. |
774 * It is needed when the first line contains non-ASCII characters. | 773 // It is needed when the first line contains non-ASCII characters. |
775 * That is only in *.??x files. */ | 774 // That is only in *.??x files. |
776 fenc = (char_u *)"latin1"; | 775 fenc = (char_u *)"latin1"; |
777 c = enc_utf8; | 776 c = enc_utf8; |
778 if (!c && !read_stdin) | 777 if (!c && !read_stdin) |
779 { | 778 { |
780 fc = fname[STRLEN(fname) - 1]; | 779 fc = fname[STRLEN(fname) - 1]; |
781 if (TOLOWER_ASC(fc) == 'x') | 780 if (TOLOWER_ASC(fc) == 'x') |
782 { | 781 { |
783 /* Read the first line (and a bit more). Immediately rewind to | 782 // Read the first line (and a bit more). Immediately rewind to |
784 * the start of the file. If the read() fails "len" is -1. */ | 783 // the start of the file. If the read() fails "len" is -1. |
785 len = read_eintr(fd, firstline, 80); | 784 len = read_eintr(fd, firstline, 80); |
786 vim_lseek(fd, (off_T)0L, SEEK_SET); | 785 vim_lseek(fd, (off_T)0L, SEEK_SET); |
787 for (p = firstline; p < firstline + len; ++p) | 786 for (p = firstline; p < firstline + len; ++p) |
788 if (*p >= 0x80) | 787 if (*p >= 0x80) |
789 { | 788 { |
796 if (c) | 795 if (c) |
797 { | 796 { |
798 fenc_next = fenc; | 797 fenc_next = fenc; |
799 fenc = (char_u *)"utf-8"; | 798 fenc = (char_u *)"utf-8"; |
800 | 799 |
801 /* When the file is utf-8 but a character doesn't fit in | 800 // When the file is utf-8 but a character doesn't fit in |
802 * 'encoding' don't retry. In help text editing utf-8 bytes | 801 // 'encoding' don't retry. In help text editing utf-8 bytes |
803 * doesn't make sense. */ | 802 // doesn't make sense. |
804 if (!enc_utf8) | 803 if (!enc_utf8) |
805 keep_dest_enc = TRUE; | 804 keep_dest_enc = TRUE; |
806 } | 805 } |
807 fenc_alloced = FALSE; | 806 fenc_alloced = FALSE; |
808 } | 807 } |
809 else if (*p_fencs == NUL) | 808 else if (*p_fencs == NUL) |
810 { | 809 { |
811 fenc = curbuf->b_p_fenc; /* use format from buffer */ | 810 fenc = curbuf->b_p_fenc; // use format from buffer |
812 fenc_alloced = FALSE; | 811 fenc_alloced = FALSE; |
813 } | 812 } |
814 else | 813 else |
815 { | 814 { |
816 fenc_next = p_fencs; /* try items in 'fileencodings' */ | 815 fenc_next = p_fencs; // try items in 'fileencodings' |
817 fenc = next_fenc(&fenc_next, &fenc_alloced); | 816 fenc = next_fenc(&fenc_next, &fenc_alloced); |
818 } | 817 } |
819 | 818 |
820 /* | 819 /* |
821 * Jump back here to retry reading the file in different ways. | 820 * Jump back here to retry reading the file in different ways. |
845 read_buf_lnum = 1; | 844 read_buf_lnum = 1; |
846 read_buf_col = 0; | 845 read_buf_col = 0; |
847 } | 846 } |
848 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0) | 847 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0) |
849 { | 848 { |
850 /* Can't rewind the file, give up. */ | 849 // Can't rewind the file, give up. |
851 error = TRUE; | 850 error = TRUE; |
852 goto failed; | 851 goto failed; |
853 } | 852 } |
854 /* Delete the previously read lines. */ | 853 // Delete the previously read lines. |
855 while (lnum > from) | 854 while (lnum > from) |
856 ml_delete(lnum--, FALSE); | 855 ml_delete(lnum--, FALSE); |
857 file_rewind = FALSE; | 856 file_rewind = FALSE; |
858 if (set_options) | 857 if (set_options) |
859 { | 858 { |
875 { | 874 { |
876 fileformat = get_fileformat_force(curbuf, eap); | 875 fileformat = get_fileformat_force(curbuf, eap); |
877 try_unix = try_dos = try_mac = FALSE; | 876 try_unix = try_dos = try_mac = FALSE; |
878 } | 877 } |
879 else if (curbuf->b_p_bin) | 878 else if (curbuf->b_p_bin) |
880 fileformat = EOL_UNIX; /* binary: use Unix format */ | 879 fileformat = EOL_UNIX; // binary: use Unix format |
881 else if (*p_ffs == NUL) | 880 else if (*p_ffs == NUL) |
882 fileformat = get_fileformat(curbuf);/* use format from buffer */ | 881 fileformat = get_fileformat(curbuf);// use format from buffer |
883 else | 882 else |
884 fileformat = EOL_UNKNOWN; /* detect from file */ | 883 fileformat = EOL_UNKNOWN; // detect from file |
885 } | 884 } |
886 | 885 |
887 #ifdef USE_ICONV | 886 #ifdef USE_ICONV |
888 if (iconv_fd != (iconv_t)-1) | 887 if (iconv_fd != (iconv_t)-1) |
889 { | 888 { |
890 /* aborted conversion with iconv(), close the descriptor */ | 889 // aborted conversion with iconv(), close the descriptor |
891 iconv_close(iconv_fd); | 890 iconv_close(iconv_fd); |
892 iconv_fd = (iconv_t)-1; | 891 iconv_fd = (iconv_t)-1; |
893 } | 892 } |
894 #endif | 893 #endif |
895 | 894 |
900 */ | 899 */ |
901 advance_fenc = FALSE; | 900 advance_fenc = FALSE; |
902 | 901 |
903 if (eap != NULL && eap->force_enc != 0) | 902 if (eap != NULL && eap->force_enc != 0) |
904 { | 903 { |
905 /* Conversion given with "++cc=" wasn't possible, read | 904 // Conversion given with "++cc=" wasn't possible, read |
906 * without conversion. */ | 905 // without conversion. |
907 notconverted = TRUE; | 906 notconverted = TRUE; |
908 conv_error = 0; | 907 conv_error = 0; |
909 if (fenc_alloced) | 908 if (fenc_alloced) |
910 vim_free(fenc); | 909 vim_free(fenc); |
911 fenc = (char_u *)""; | 910 fenc = (char_u *)""; |
925 fenc_alloced = FALSE; | 924 fenc_alloced = FALSE; |
926 } | 925 } |
927 } | 926 } |
928 if (tmpname != NULL) | 927 if (tmpname != NULL) |
929 { | 928 { |
930 mch_remove(tmpname); /* delete converted file */ | 929 mch_remove(tmpname); // delete converted file |
931 VIM_CLEAR(tmpname); | 930 VIM_CLEAR(tmpname); |
932 } | 931 } |
933 } | 932 } |
934 | 933 |
935 /* | 934 /* |
939 fio_flags = 0; | 938 fio_flags = 0; |
940 converted = need_conversion(fenc); | 939 converted = need_conversion(fenc); |
941 if (converted) | 940 if (converted) |
942 { | 941 { |
943 | 942 |
944 /* "ucs-bom" means we need to check the first bytes of the file | 943 // "ucs-bom" means we need to check the first bytes of the file |
945 * for a BOM. */ | 944 // for a BOM. |
946 if (STRCMP(fenc, ENC_UCSBOM) == 0) | 945 if (STRCMP(fenc, ENC_UCSBOM) == 0) |
947 fio_flags = FIO_UCSBOM; | 946 fio_flags = FIO_UCSBOM; |
948 | 947 |
949 /* | 948 /* |
950 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be | 949 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be |
966 if (fio_flags == 0) | 965 if (fio_flags == 0) |
967 fio_flags = get_win_fio_flags(fenc); | 966 fio_flags = get_win_fio_flags(fenc); |
968 #endif | 967 #endif |
969 | 968 |
970 #ifdef MACOS_CONVERT | 969 #ifdef MACOS_CONVERT |
971 /* Conversion from Apple MacRoman to latin1 or UTF-8 */ | 970 // Conversion from Apple MacRoman to latin1 or UTF-8 |
972 if (fio_flags == 0) | 971 if (fio_flags == 0) |
973 fio_flags = get_mac_fio_flags(fenc); | 972 fio_flags = get_mac_fio_flags(fenc); |
974 #endif | 973 #endif |
975 | 974 |
976 #ifdef USE_ICONV | 975 #ifdef USE_ICONV |
999 ) | 998 ) |
1000 { | 999 { |
1001 # ifdef USE_ICONV | 1000 # ifdef USE_ICONV |
1002 did_iconv = FALSE; | 1001 did_iconv = FALSE; |
1003 # endif | 1002 # endif |
1004 /* Skip conversion when it's already done (retry for wrong | 1003 // Skip conversion when it's already done (retry for wrong |
1005 * "fileformat"). */ | 1004 // "fileformat"). |
1006 if (tmpname == NULL) | 1005 if (tmpname == NULL) |
1007 { | 1006 { |
1008 tmpname = readfile_charconvert(fname, fenc, &fd); | 1007 tmpname = readfile_charconvert(fname, fenc, &fd); |
1009 if (tmpname == NULL) | 1008 if (tmpname == NULL) |
1010 { | 1009 { |
1011 /* Conversion failed. Try another one. */ | 1010 // Conversion failed. Try another one. |
1012 advance_fenc = TRUE; | 1011 advance_fenc = TRUE; |
1013 if (fd < 0) | 1012 if (fd < 0) |
1014 { | 1013 { |
1015 /* Re-opening the original file failed! */ | 1014 // Re-opening the original file failed! |
1016 emsg(_("E202: Conversion made file unreadable!")); | 1015 emsg(_("E202: Conversion made file unreadable!")); |
1017 error = TRUE; | 1016 error = TRUE; |
1018 goto failed; | 1017 goto failed; |
1019 } | 1018 } |
1020 goto retry; | 1019 goto retry; |
1028 #ifdef USE_ICONV | 1027 #ifdef USE_ICONV |
1029 && iconv_fd == (iconv_t)-1 | 1028 && iconv_fd == (iconv_t)-1 |
1030 #endif | 1029 #endif |
1031 ) | 1030 ) |
1032 { | 1031 { |
1033 /* Conversion wanted but we can't. | 1032 // Conversion wanted but we can't. |
1034 * Try the next conversion in 'fileencodings' */ | 1033 // Try the next conversion in 'fileencodings' |
1035 advance_fenc = TRUE; | 1034 advance_fenc = TRUE; |
1036 goto retry; | 1035 goto retry; |
1037 } | 1036 } |
1038 } | 1037 } |
1039 } | 1038 } |
1040 | 1039 |
1041 /* Set "can_retry" when it's possible to rewind the file and try with | 1040 // Set "can_retry" when it's possible to rewind the file and try with |
1042 * another "fenc" value. It's FALSE when no other "fenc" to try, reading | 1041 // another "fenc" value. It's FALSE when no other "fenc" to try, reading |
1043 * stdin or fixed at a specific encoding. */ | 1042 // stdin or fixed at a specific encoding. |
1044 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc); | 1043 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc); |
1045 | 1044 |
1046 if (!skip_read) | 1045 if (!skip_read) |
1047 { | 1046 { |
1048 linerest = 0; | 1047 linerest = 0; |
1062 sha256_start(&sha_ctx); | 1061 sha256_start(&sha_ctx); |
1063 #endif | 1062 #endif |
1064 #ifdef FEAT_CRYPT | 1063 #ifdef FEAT_CRYPT |
1065 if (curbuf->b_cryptstate != NULL) | 1064 if (curbuf->b_cryptstate != NULL) |
1066 { | 1065 { |
1067 /* Need to free the state, but keep the key, don't want to ask for | 1066 // Need to free the state, but keep the key, don't want to ask for |
1068 * it again. */ | 1067 // it again. |
1069 crypt_free_state(curbuf->b_cryptstate); | 1068 crypt_free_state(curbuf->b_cryptstate); |
1070 curbuf->b_cryptstate = NULL; | 1069 curbuf->b_cryptstate = NULL; |
1071 } | 1070 } |
1072 #endif | 1071 #endif |
1073 } | 1072 } |
1081 * up to max_unsigned characters (and other things). | 1080 * up to max_unsigned characters (and other things). |
1082 */ | 1081 */ |
1083 if (!skip_read) | 1082 if (!skip_read) |
1084 { | 1083 { |
1085 #if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L) | 1084 #if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L) |
1086 size = SSIZE_MAX; /* use max I/O size, 52K */ | 1085 size = SSIZE_MAX; // use max I/O size, 52K |
1087 #else | 1086 #else |
1088 /* Use buffer >= 64K. Add linerest to double the size if the | 1087 // Use buffer >= 64K. Add linerest to double the size if the |
1089 * line gets very long, to avoid a lot of copying. But don't | 1088 // line gets very long, to avoid a lot of copying. But don't |
1090 * read more than 1 Mbyte at a time, so we can be interrupted. | 1089 // read more than 1 Mbyte at a time, so we can be interrupted. |
1091 */ | |
1092 size = 0x10000L + linerest; | 1090 size = 0x10000L + linerest; |
1093 if (size > 0x100000L) | 1091 if (size > 0x100000L) |
1094 size = 0x100000L; | 1092 size = 0x100000L; |
1095 #endif | 1093 #endif |
1096 } | 1094 } |
1097 | 1095 |
1098 /* Protect against the argument of lalloc() going negative. */ | 1096 // Protect against the argument of lalloc() going negative. |
1099 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL) | 1097 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL) |
1100 { | 1098 { |
1101 ++split; | 1099 ++split; |
1102 *ptr = NL; /* split line by inserting a NL */ | 1100 *ptr = NL; // split line by inserting a NL |
1103 size = 1; | 1101 size = 1; |
1104 } | 1102 } |
1105 else | 1103 else |
1106 { | 1104 { |
1107 if (!skip_read) | 1105 if (!skip_read) |
1116 { | 1114 { |
1117 do_outofmem_msg((long_u)(size * 2 + linerest + 1)); | 1115 do_outofmem_msg((long_u)(size * 2 + linerest + 1)); |
1118 error = TRUE; | 1116 error = TRUE; |
1119 break; | 1117 break; |
1120 } | 1118 } |
1121 if (linerest) /* copy characters from the previous buffer */ | 1119 if (linerest) // copy characters from the previous buffer |
1122 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest); | 1120 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest); |
1123 vim_free(buffer); | 1121 vim_free(buffer); |
1124 buffer = new_buffer; | 1122 buffer = new_buffer; |
1125 ptr = buffer + linerest; | 1123 ptr = buffer + linerest; |
1126 line_start = buffer; | 1124 line_start = buffer; |
1127 | 1125 |
1128 /* May need room to translate into. | 1126 // May need room to translate into. |
1129 * For iconv() we don't really know the required space, use a | 1127 // For iconv() we don't really know the required space, use a |
1130 * factor ICONV_MULT. | 1128 // factor ICONV_MULT. |
1131 * latin1 to utf-8: 1 byte becomes up to 2 bytes | 1129 // latin1 to utf-8: 1 byte becomes up to 2 bytes |
1132 * utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes | 1130 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes |
1133 * become up to 4 bytes, size must be multiple of 2 | 1131 // become up to 4 bytes, size must be multiple of 2 |
1134 * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be | 1132 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be |
1135 * multiple of 2 | 1133 // multiple of 2 |
1136 * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be | 1134 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be |
1137 * multiple of 4 */ | 1135 // multiple of 4 |
1138 real_size = (int)size; | 1136 real_size = (int)size; |
1139 #ifdef USE_ICONV | 1137 #ifdef USE_ICONV |
1140 if (iconv_fd != (iconv_t)-1) | 1138 if (iconv_fd != (iconv_t)-1) |
1141 size = size / ICONV_MULT; | 1139 size = size / ICONV_MULT; |
1142 else | 1140 else |
1146 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | 1144 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) |
1147 size = (size * 2 / 3) & ~1; | 1145 size = (size * 2 / 3) & ~1; |
1148 else if (fio_flags & FIO_UCS4) | 1146 else if (fio_flags & FIO_UCS4) |
1149 size = (size * 2 / 3) & ~3; | 1147 size = (size * 2 / 3) & ~3; |
1150 else if (fio_flags == FIO_UCSBOM) | 1148 else if (fio_flags == FIO_UCSBOM) |
1151 size = size / ICONV_MULT; /* worst case */ | 1149 size = size / ICONV_MULT; // worst case |
1152 #ifdef MSWIN | 1150 #ifdef MSWIN |
1153 else if (fio_flags & FIO_CODEPAGE) | 1151 else if (fio_flags & FIO_CODEPAGE) |
1154 size = size / ICONV_MULT; /* also worst case */ | 1152 size = size / ICONV_MULT; // also worst case |
1155 #endif | 1153 #endif |
1156 #ifdef MACOS_CONVERT | 1154 #ifdef MACOS_CONVERT |
1157 else if (fio_flags & FIO_MACROMAN) | 1155 else if (fio_flags & FIO_MACROMAN) |
1158 size = size / ICONV_MULT; /* also worst case */ | 1156 size = size / ICONV_MULT; // also worst case |
1159 #endif | 1157 #endif |
1160 | 1158 |
1161 if (conv_restlen > 0) | 1159 if (conv_restlen > 0) |
1162 { | 1160 { |
1163 /* Insert unconverted bytes from previous line. */ | 1161 // Insert unconverted bytes from previous line. |
1164 mch_memmove(ptr, conv_rest, conv_restlen); | 1162 mch_memmove(ptr, conv_rest, conv_restlen); |
1165 ptr += conv_restlen; | 1163 ptr += conv_restlen; |
1166 size -= conv_restlen; | 1164 size -= conv_restlen; |
1167 } | 1165 } |
1168 | 1166 |
1184 { | 1182 { |
1185 p = ml_get(read_buf_lnum) + read_buf_col; | 1183 p = ml_get(read_buf_lnum) + read_buf_col; |
1186 n = (int)STRLEN(p); | 1184 n = (int)STRLEN(p); |
1187 if ((int)tlen + n + 1 > size) | 1185 if ((int)tlen + n + 1 > size) |
1188 { | 1186 { |
1189 /* Filled up to "size", append partial line. | 1187 // Filled up to "size", append partial line. |
1190 * Change NL to NUL to reverse the effect done | 1188 // Change NL to NUL to reverse the effect done |
1191 * below. */ | 1189 // below. |
1192 n = (int)(size - tlen); | 1190 n = (int)(size - tlen); |
1193 for (ni = 0; ni < n; ++ni) | 1191 for (ni = 0; ni < n; ++ni) |
1194 { | 1192 { |
1195 if (p[ni] == NL) | 1193 if (p[ni] == NL) |
1196 ptr[tlen++] = NUL; | 1194 ptr[tlen++] = NUL; |
1200 read_buf_col += n; | 1198 read_buf_col += n; |
1201 break; | 1199 break; |
1202 } | 1200 } |
1203 else | 1201 else |
1204 { | 1202 { |
1205 /* Append whole line and new-line. Change NL | 1203 // Append whole line and new-line. Change NL |
1206 * to NUL to reverse the effect done below. */ | 1204 // to NUL to reverse the effect done below. |
1207 for (ni = 0; ni < n; ++ni) | 1205 for (ni = 0; ni < n; ++ni) |
1208 { | 1206 { |
1209 if (p[ni] == NL) | 1207 if (p[ni] == NL) |
1210 ptr[tlen++] = NUL; | 1208 ptr[tlen++] = NUL; |
1211 else | 1209 else |
1213 } | 1211 } |
1214 ptr[tlen++] = NL; | 1212 ptr[tlen++] = NL; |
1215 read_buf_col = 0; | 1213 read_buf_col = 0; |
1216 if (++read_buf_lnum > from) | 1214 if (++read_buf_lnum > from) |
1217 { | 1215 { |
1218 /* When the last line didn't have an | 1216 // When the last line didn't have an |
1219 * end-of-line don't add it now either. */ | 1217 // end-of-line don't add it now either. |
1220 if (!curbuf->b_p_eol) | 1218 if (!curbuf->b_p_eol) |
1221 --tlen; | 1219 --tlen; |
1222 size = tlen; | 1220 size = tlen; |
1223 break; | 1221 break; |
1224 } | 1222 } |
1262 int decrypted_size; | 1260 int decrypted_size; |
1263 | 1261 |
1264 decrypted_size = crypt_decode_alloc( | 1262 decrypted_size = crypt_decode_alloc( |
1265 curbuf->b_cryptstate, ptr, size, &newptr); | 1263 curbuf->b_cryptstate, ptr, size, &newptr); |
1266 | 1264 |
1267 /* If the crypt layer is buffering, not producing | 1265 // If the crypt layer is buffering, not producing |
1268 * anything yet, need to read more. */ | 1266 // anything yet, need to read more. |
1269 if (decrypted_size == 0) | 1267 if (decrypted_size == 0) |
1270 continue; | 1268 continue; |
1271 | 1269 |
1272 if (linerest == 0) | 1270 if (linerest == 0) |
1273 { | 1271 { |
1274 /* Simple case: reuse returned buffer (may be | 1272 // Simple case: reuse returned buffer (may be |
1275 * NULL, checked later). */ | 1273 // NULL, checked later). |
1276 new_buffer = newptr; | 1274 new_buffer = newptr; |
1277 } | 1275 } |
1278 else | 1276 else |
1279 { | 1277 { |
1280 long_u new_size; | 1278 long_u new_size; |
1281 | 1279 |
1282 /* Need new buffer to add bytes carried over. */ | 1280 // Need new buffer to add bytes carried over. |
1283 new_size = (long_u)(decrypted_size + linerest + 1); | 1281 new_size = (long_u)(decrypted_size + linerest + 1); |
1284 new_buffer = lalloc(new_size, FALSE); | 1282 new_buffer = lalloc(new_size, FALSE); |
1285 if (new_buffer == NULL) | 1283 if (new_buffer == NULL) |
1286 { | 1284 { |
1287 do_outofmem_msg(new_size); | 1285 do_outofmem_msg(new_size); |
1309 } | 1307 } |
1310 #endif | 1308 #endif |
1311 | 1309 |
1312 if (size <= 0) | 1310 if (size <= 0) |
1313 { | 1311 { |
1314 if (size < 0) /* read error */ | 1312 if (size < 0) // read error |
1315 error = TRUE; | 1313 error = TRUE; |
1316 else if (conv_restlen > 0) | 1314 else if (conv_restlen > 0) |
1317 { | 1315 { |
1318 /* | 1316 /* |
1319 * Reached end-of-file but some trailing bytes could | 1317 * Reached end-of-file but some trailing bytes could |
1320 * not be converted. Truncated file? | 1318 * not be converted. Truncated file? |
1321 */ | 1319 */ |
1322 | 1320 |
1323 /* When we did a conversion report an error. */ | 1321 // When we did a conversion report an error. |
1324 if (fio_flags != 0 | 1322 if (fio_flags != 0 |
1325 #ifdef USE_ICONV | 1323 #ifdef USE_ICONV |
1326 || iconv_fd != (iconv_t)-1 | 1324 || iconv_fd != (iconv_t)-1 |
1327 #endif | 1325 #endif |
1328 ) | 1326 ) |
1331 goto rewind_retry; | 1329 goto rewind_retry; |
1332 if (conv_error == 0) | 1330 if (conv_error == 0) |
1333 conv_error = curbuf->b_ml.ml_line_count | 1331 conv_error = curbuf->b_ml.ml_line_count |
1334 - linecnt + 1; | 1332 - linecnt + 1; |
1335 } | 1333 } |
1336 /* Remember the first linenr with an illegal byte */ | 1334 // Remember the first linenr with an illegal byte |
1337 else if (illegal_byte == 0) | 1335 else if (illegal_byte == 0) |
1338 illegal_byte = curbuf->b_ml.ml_line_count | 1336 illegal_byte = curbuf->b_ml.ml_line_count |
1339 - linecnt + 1; | 1337 - linecnt + 1; |
1340 if (bad_char_behavior == BAD_DROP) | 1338 if (bad_char_behavior == BAD_DROP) |
1341 { | 1339 { |
1342 *(ptr - conv_restlen) = NUL; | 1340 *(ptr - conv_restlen) = NUL; |
1343 conv_restlen = 0; | 1341 conv_restlen = 0; |
1344 } | 1342 } |
1345 else | 1343 else |
1346 { | 1344 { |
1347 /* Replace the trailing bytes with the replacement | 1345 // Replace the trailing bytes with the replacement |
1348 * character if we were converting; if we weren't, | 1346 // character if we were converting; if we weren't, |
1349 * leave the UTF8 checking code to do it, as it | 1347 // leave the UTF8 checking code to do it, as it |
1350 * works slightly differently. */ | 1348 // works slightly differently. |
1351 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0 | 1349 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0 |
1352 #ifdef USE_ICONV | 1350 #ifdef USE_ICONV |
1353 || iconv_fd != (iconv_t)-1 | 1351 || iconv_fd != (iconv_t)-1 |
1354 #endif | 1352 #endif |
1355 )) | 1353 )) |
1358 { | 1356 { |
1359 *(--ptr) = bad_char_behavior; | 1357 *(--ptr) = bad_char_behavior; |
1360 --conv_restlen; | 1358 --conv_restlen; |
1361 } | 1359 } |
1362 } | 1360 } |
1363 fio_flags = 0; /* don't convert this */ | 1361 fio_flags = 0; // don't convert this |
1364 #ifdef USE_ICONV | 1362 #ifdef USE_ICONV |
1365 if (iconv_fd != (iconv_t)-1) | 1363 if (iconv_fd != (iconv_t)-1) |
1366 { | 1364 { |
1367 iconv_close(iconv_fd); | 1365 iconv_close(iconv_fd); |
1368 iconv_fd = (iconv_t)-1; | 1366 iconv_fd = (iconv_t)-1; |
1393 && (*fenc == 'u' || (*fenc == NUL && enc_utf8))))) | 1391 && (*fenc == 'u' || (*fenc == NUL && enc_utf8))))) |
1394 { | 1392 { |
1395 char_u *ccname; | 1393 char_u *ccname; |
1396 int blen; | 1394 int blen; |
1397 | 1395 |
1398 /* no BOM detection in a short file or in binary mode */ | 1396 // no BOM detection in a short file or in binary mode |
1399 if (size < 2 || curbuf->b_p_bin) | 1397 if (size < 2 || curbuf->b_p_bin) |
1400 ccname = NULL; | 1398 ccname = NULL; |
1401 else | 1399 else |
1402 ccname = check_for_bom(ptr, size, &blen, | 1400 ccname = check_for_bom(ptr, size, &blen, |
1403 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc)); | 1401 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc)); |
1404 if (ccname != NULL) | 1402 if (ccname != NULL) |
1405 { | 1403 { |
1406 /* Remove BOM from the text */ | 1404 // Remove BOM from the text |
1407 filesize += blen; | 1405 filesize += blen; |
1408 size -= blen; | 1406 size -= blen; |
1409 mch_memmove(ptr, ptr + blen, (size_t)size); | 1407 mch_memmove(ptr, ptr + blen, (size_t)size); |
1410 if (set_options) | 1408 if (set_options) |
1411 { | 1409 { |
1416 | 1414 |
1417 if (fio_flags == FIO_UCSBOM) | 1415 if (fio_flags == FIO_UCSBOM) |
1418 { | 1416 { |
1419 if (ccname == NULL) | 1417 if (ccname == NULL) |
1420 { | 1418 { |
1421 /* No BOM detected: retry with next encoding. */ | 1419 // No BOM detected: retry with next encoding. |
1422 advance_fenc = TRUE; | 1420 advance_fenc = TRUE; |
1423 } | 1421 } |
1424 else | 1422 else |
1425 { | 1423 { |
1426 /* BOM detected: set "fenc" and jump back */ | 1424 // BOM detected: set "fenc" and jump back |
1427 if (fenc_alloced) | 1425 if (fenc_alloced) |
1428 vim_free(fenc); | 1426 vim_free(fenc); |
1429 fenc = ccname; | 1427 fenc = ccname; |
1430 fenc_alloced = FALSE; | 1428 fenc_alloced = FALSE; |
1431 } | 1429 } |
1432 /* retry reading without getting new bytes or rewinding */ | 1430 // retry reading without getting new bytes or rewinding |
1433 skip_read = TRUE; | 1431 skip_read = TRUE; |
1434 goto retry; | 1432 goto retry; |
1435 } | 1433 } |
1436 } | 1434 } |
1437 | 1435 |
1438 /* Include not converted bytes. */ | 1436 // Include not converted bytes. |
1439 ptr -= conv_restlen; | 1437 ptr -= conv_restlen; |
1440 size += conv_restlen; | 1438 size += conv_restlen; |
1441 conv_restlen = 0; | 1439 conv_restlen = 0; |
1442 /* | 1440 /* |
1443 * Break here for a read error or end-of-file. | 1441 * Break here for a read error or end-of-file. |
1478 goto rewind_retry; | 1476 goto rewind_retry; |
1479 if (conv_error == 0) | 1477 if (conv_error == 0) |
1480 conv_error = readfile_linenr(linecnt, | 1478 conv_error = readfile_linenr(linecnt, |
1481 ptr, (char_u *)top); | 1479 ptr, (char_u *)top); |
1482 | 1480 |
1483 /* Deal with a bad byte and continue with the next. */ | 1481 // Deal with a bad byte and continue with the next. |
1484 ++fromp; | 1482 ++fromp; |
1485 --from_size; | 1483 --from_size; |
1486 if (bad_char_behavior == BAD_KEEP) | 1484 if (bad_char_behavior == BAD_KEEP) |
1487 { | 1485 { |
1488 *top++ = *(fromp - 1); | 1486 *top++ = *(fromp - 1); |
1495 } | 1493 } |
1496 } | 1494 } |
1497 | 1495 |
1498 if (from_size > 0) | 1496 if (from_size > 0) |
1499 { | 1497 { |
1500 /* Some remaining characters, keep them for the next | 1498 // Some remaining characters, keep them for the next |
1501 * round. */ | 1499 // round. |
1502 mch_memmove(conv_rest, (char_u *)fromp, from_size); | 1500 mch_memmove(conv_rest, (char_u *)fromp, from_size); |
1503 conv_restlen = (int)from_size; | 1501 conv_restlen = (int)from_size; |
1504 } | 1502 } |
1505 | 1503 |
1506 /* move the linerest to before the converted characters */ | 1504 // move the linerest to before the converted characters |
1507 line_start = ptr - linerest; | 1505 line_start = ptr - linerest; |
1508 mch_memmove(line_start, buffer, (size_t)linerest); | 1506 mch_memmove(line_start, buffer, (size_t)linerest); |
1509 size = (long)((char_u *)top - ptr); | 1507 size = (long)((char_u *)top - ptr); |
1510 } | 1508 } |
1511 #endif | 1509 #endif |
1531 * Because there may be illegal bytes AND an incomplete byte | 1529 * Because there may be illegal bytes AND an incomplete byte |
1532 * sequence at the end, we may have to do the conversion one | 1530 * sequence at the end, we may have to do the conversion one |
1533 * character at a time to get it right. | 1531 * character at a time to get it right. |
1534 */ | 1532 */ |
1535 | 1533 |
1536 /* Replacement string for WideCharToMultiByte(). */ | 1534 // Replacement string for WideCharToMultiByte(). |
1537 if (bad_char_behavior > 0) | 1535 if (bad_char_behavior > 0) |
1538 replstr[0] = bad_char_behavior; | 1536 replstr[0] = bad_char_behavior; |
1539 else | 1537 else |
1540 replstr[0] = '?'; | 1538 replstr[0] = '?'; |
1541 replstr[1] = NUL; | 1539 replstr[1] = NUL; |
1554 size = size; | 1552 size = size; |
1555 while (size > 0) | 1553 while (size > 0) |
1556 { | 1554 { |
1557 found_bad = FALSE; | 1555 found_bad = FALSE; |
1558 | 1556 |
1559 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | 1557 # ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8 |
1560 if (codepage == CP_UTF8) | 1558 if (codepage == CP_UTF8) |
1561 { | 1559 { |
1562 /* Handle CP_UTF8 input ourselves to be able to handle | 1560 // Handle CP_UTF8 input ourselves to be able to handle |
1563 * trailing bytes properly. | 1561 // trailing bytes properly. |
1564 * Get one UTF-8 character from src. */ | 1562 // Get one UTF-8 character from src. |
1565 bytelen = (int)utf_ptr2len_len(src, size); | 1563 bytelen = (int)utf_ptr2len_len(src, size); |
1566 if (bytelen > size) | 1564 if (bytelen > size) |
1567 { | 1565 { |
1568 /* Only got some bytes of a character. Normally | 1566 // Only got some bytes of a character. Normally |
1569 * it's put in "conv_rest", but if it's too long | 1567 // it's put in "conv_rest", but if it's too long |
1570 * deal with it as if they were illegal bytes. */ | 1568 // deal with it as if they were illegal bytes. |
1571 if (bytelen <= CONV_RESTLEN) | 1569 if (bytelen <= CONV_RESTLEN) |
1572 break; | 1570 break; |
1573 | 1571 |
1574 /* weird overlong byte sequence */ | 1572 // weird overlong byte sequence |
1575 bytelen = size; | 1573 bytelen = size; |
1576 found_bad = TRUE; | 1574 found_bad = TRUE; |
1577 } | 1575 } |
1578 else | 1576 else |
1579 { | 1577 { |
1586 } | 1584 } |
1587 } | 1585 } |
1588 else | 1586 else |
1589 # endif | 1587 # endif |
1590 { | 1588 { |
1591 /* We don't know how long the byte sequence is, try | 1589 // We don't know how long the byte sequence is, try |
1592 * from one to three bytes. */ | 1590 // from one to three bytes. |
1593 for (bytelen = 1; bytelen <= size && bytelen <= 3; | 1591 for (bytelen = 1; bytelen <= size && bytelen <= 3; |
1594 ++bytelen) | 1592 ++bytelen) |
1595 { | 1593 { |
1596 ucs2len = MultiByteToWideChar(codepage, | 1594 ucs2len = MultiByteToWideChar(codepage, |
1597 MB_ERR_INVALID_CHARS, | 1595 MB_ERR_INVALID_CHARS, |
1600 if (ucs2len > 0) | 1598 if (ucs2len > 0) |
1601 break; | 1599 break; |
1602 } | 1600 } |
1603 if (ucs2len == 0) | 1601 if (ucs2len == 0) |
1604 { | 1602 { |
1605 /* If we have only one byte then it's probably an | 1603 // If we have only one byte then it's probably an |
1606 * incomplete byte sequence. Otherwise discard | 1604 // incomplete byte sequence. Otherwise discard |
1607 * one byte as a bad character. */ | 1605 // one byte as a bad character. |
1608 if (size == 1) | 1606 if (size == 1) |
1609 break; | 1607 break; |
1610 found_bad = TRUE; | 1608 found_bad = TRUE; |
1611 bytelen = 1; | 1609 bytelen = 1; |
1612 } | 1610 } |
1614 | 1612 |
1615 if (!found_bad) | 1613 if (!found_bad) |
1616 { | 1614 { |
1617 int i; | 1615 int i; |
1618 | 1616 |
1619 /* Convert "ucs2buf[ucs2len]" to 'enc' in "dst". */ | 1617 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst". |
1620 if (enc_utf8) | 1618 if (enc_utf8) |
1621 { | 1619 { |
1622 /* From UCS-2 to UTF-8. Cannot fail. */ | 1620 // From UCS-2 to UTF-8. Cannot fail. |
1623 for (i = 0; i < ucs2len; ++i) | 1621 for (i = 0; i < ucs2len; ++i) |
1624 dst += utf_char2bytes(ucs2buf[i], dst); | 1622 dst += utf_char2bytes(ucs2buf[i], dst); |
1625 } | 1623 } |
1626 else | 1624 else |
1627 { | 1625 { |
1628 BOOL bad = FALSE; | 1626 BOOL bad = FALSE; |
1629 int dstlen; | 1627 int dstlen; |
1630 | 1628 |
1631 /* From UCS-2 to "enc_codepage". If the | 1629 // From UCS-2 to "enc_codepage". If the |
1632 * conversion uses the default character "?", | 1630 // conversion uses the default character "?", |
1633 * the data doesn't fit in this encoding. */ | 1631 // the data doesn't fit in this encoding. |
1634 dstlen = WideCharToMultiByte(enc_codepage, 0, | 1632 dstlen = WideCharToMultiByte(enc_codepage, 0, |
1635 (LPCWSTR)ucs2buf, ucs2len, | 1633 (LPCWSTR)ucs2buf, ucs2len, |
1636 (LPSTR)dst, (int)(src - dst), | 1634 (LPSTR)dst, (int)(src - dst), |
1637 replstr, &bad); | 1635 replstr, &bad); |
1638 if (bad) | 1636 if (bad) |
1642 } | 1640 } |
1643 } | 1641 } |
1644 | 1642 |
1645 if (found_bad) | 1643 if (found_bad) |
1646 { | 1644 { |
1647 /* Deal with bytes we can't convert. */ | 1645 // Deal with bytes we can't convert. |
1648 if (can_retry) | 1646 if (can_retry) |
1649 goto rewind_retry; | 1647 goto rewind_retry; |
1650 if (conv_error == 0) | 1648 if (conv_error == 0) |
1651 conv_error = readfile_linenr(linecnt, ptr, dst); | 1649 conv_error = readfile_linenr(linecnt, ptr, dst); |
1652 if (bad_char_behavior != BAD_DROP) | 1650 if (bad_char_behavior != BAD_DROP) |
1665 size -= bytelen; | 1663 size -= bytelen; |
1666 } | 1664 } |
1667 | 1665 |
1668 if (size > 0) | 1666 if (size > 0) |
1669 { | 1667 { |
1670 /* An incomplete byte sequence remaining. */ | 1668 // An incomplete byte sequence remaining. |
1671 mch_memmove(conv_rest, src, size); | 1669 mch_memmove(conv_rest, src, size); |
1672 conv_restlen = size; | 1670 conv_restlen = size; |
1673 } | 1671 } |
1674 | 1672 |
1675 /* The new size is equal to how much "dst" was advanced. */ | 1673 // The new size is equal to how much "dst" was advanced. |
1676 size = (long)(dst - ptr); | 1674 size = (long)(dst - ptr); |
1677 } | 1675 } |
1678 else | 1676 else |
1679 #endif | 1677 #endif |
1680 #ifdef MACOS_CONVERT | 1678 #ifdef MACOS_CONVERT |
1707 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8) | 1705 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8) |
1708 { | 1706 { |
1709 p = ptr + size; | 1707 p = ptr + size; |
1710 if (fio_flags == FIO_UTF8) | 1708 if (fio_flags == FIO_UTF8) |
1711 { | 1709 { |
1712 /* Check for a trailing incomplete UTF-8 sequence */ | 1710 // Check for a trailing incomplete UTF-8 sequence |
1713 tail = ptr + size - 1; | 1711 tail = ptr + size - 1; |
1714 while (tail > ptr && (*tail & 0xc0) == 0x80) | 1712 while (tail > ptr && (*tail & 0xc0) == 0x80) |
1715 --tail; | 1713 --tail; |
1716 if (tail + utf_byte2len(*tail) <= ptr + size) | 1714 if (tail + utf_byte2len(*tail) <= ptr + size) |
1717 tail = NULL; | 1715 tail = NULL; |
1719 p = tail; | 1717 p = tail; |
1720 } | 1718 } |
1721 } | 1719 } |
1722 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | 1720 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) |
1723 { | 1721 { |
1724 /* Check for a trailing byte */ | 1722 // Check for a trailing byte |
1725 p = ptr + (size & ~1); | 1723 p = ptr + (size & ~1); |
1726 if (size & 1) | 1724 if (size & 1) |
1727 tail = p; | 1725 tail = p; |
1728 if ((fio_flags & FIO_UTF16) && p > ptr) | 1726 if ((fio_flags & FIO_UTF16) && p > ptr) |
1729 { | 1727 { |
1730 /* Check for a trailing leading word */ | 1728 // Check for a trailing leading word |
1731 if (fio_flags & FIO_ENDIAN_L) | 1729 if (fio_flags & FIO_ENDIAN_L) |
1732 { | 1730 { |
1733 u8c = (*--p << 8); | 1731 u8c = (*--p << 8); |
1734 u8c += *--p; | 1732 u8c += *--p; |
1735 } | 1733 } |
1742 tail = p; | 1740 tail = p; |
1743 else | 1741 else |
1744 p += 2; | 1742 p += 2; |
1745 } | 1743 } |
1746 } | 1744 } |
1747 else /* FIO_UCS4 */ | 1745 else // FIO_UCS4 |
1748 { | 1746 { |
1749 /* Check for trailing 1, 2 or 3 bytes */ | 1747 // Check for trailing 1, 2 or 3 bytes |
1750 p = ptr + (size & ~3); | 1748 p = ptr + (size & ~3); |
1751 if (size & 3) | 1749 if (size & 3) |
1752 tail = p; | 1750 tail = p; |
1753 } | 1751 } |
1754 | 1752 |
1755 /* If there is a trailing incomplete sequence move it to | 1753 // If there is a trailing incomplete sequence move it to |
1756 * conv_rest[]. */ | 1754 // conv_rest[]. |
1757 if (tail != NULL) | 1755 if (tail != NULL) |
1758 { | 1756 { |
1759 conv_restlen = (int)((ptr + size) - tail); | 1757 conv_restlen = (int)((ptr + size) - tail); |
1760 mch_memmove(conv_rest, (char_u *)tail, conv_restlen); | 1758 mch_memmove(conv_rest, (char_u *)tail, conv_restlen); |
1761 size -= conv_restlen; | 1759 size -= conv_restlen; |
1783 { | 1781 { |
1784 int u16c; | 1782 int u16c; |
1785 | 1783 |
1786 if (p == ptr) | 1784 if (p == ptr) |
1787 { | 1785 { |
1788 /* Missing leading word. */ | 1786 // Missing leading word. |
1789 if (can_retry) | 1787 if (can_retry) |
1790 goto rewind_retry; | 1788 goto rewind_retry; |
1791 if (conv_error == 0) | 1789 if (conv_error == 0) |
1792 conv_error = readfile_linenr(linecnt, | 1790 conv_error = readfile_linenr(linecnt, |
1793 ptr, p); | 1791 ptr, p); |
1795 continue; | 1793 continue; |
1796 if (bad_char_behavior != BAD_KEEP) | 1794 if (bad_char_behavior != BAD_KEEP) |
1797 u8c = bad_char_behavior; | 1795 u8c = bad_char_behavior; |
1798 } | 1796 } |
1799 | 1797 |
1800 /* found second word of double-word, get the first | 1798 // found second word of double-word, get the first |
1801 * word and compute the resulting character */ | 1799 // word and compute the resulting character |
1802 if (fio_flags & FIO_ENDIAN_L) | 1800 if (fio_flags & FIO_ENDIAN_L) |
1803 { | 1801 { |
1804 u16c = (*--p << 8); | 1802 u16c = (*--p << 8); |
1805 u16c += *--p; | 1803 u16c += *--p; |
1806 } | 1804 } |
1810 u16c += (*--p << 8); | 1808 u16c += (*--p << 8); |
1811 } | 1809 } |
1812 u8c = 0x10000 + ((u16c & 0x3ff) << 10) | 1810 u8c = 0x10000 + ((u16c & 0x3ff) << 10) |
1813 + (u8c & 0x3ff); | 1811 + (u8c & 0x3ff); |
1814 | 1812 |
1815 /* Check if the word is indeed a leading word. */ | 1813 // Check if the word is indeed a leading word. |
1816 if (u16c < 0xd800 || u16c > 0xdbff) | 1814 if (u16c < 0xd800 || u16c > 0xdbff) |
1817 { | 1815 { |
1818 if (can_retry) | 1816 if (can_retry) |
1819 goto rewind_retry; | 1817 goto rewind_retry; |
1820 if (conv_error == 0) | 1818 if (conv_error == 0) |
1834 u8c = (unsigned)*--p << 24; | 1832 u8c = (unsigned)*--p << 24; |
1835 u8c += (unsigned)*--p << 16; | 1833 u8c += (unsigned)*--p << 16; |
1836 u8c += (unsigned)*--p << 8; | 1834 u8c += (unsigned)*--p << 8; |
1837 u8c += *--p; | 1835 u8c += *--p; |
1838 } | 1836 } |
1839 else /* big endian */ | 1837 else // big endian |
1840 { | 1838 { |
1841 u8c = *--p; | 1839 u8c = *--p; |
1842 u8c += (unsigned)*--p << 8; | 1840 u8c += (unsigned)*--p << 8; |
1843 u8c += (unsigned)*--p << 16; | 1841 u8c += (unsigned)*--p << 16; |
1844 u8c += (unsigned)*--p << 24; | 1842 u8c += (unsigned)*--p << 24; |
1845 } | 1843 } |
1846 } | 1844 } |
1847 else /* UTF-8 */ | 1845 else // UTF-8 |
1848 { | 1846 { |
1849 if (*--p < 0x80) | 1847 if (*--p < 0x80) |
1850 u8c = *p; | 1848 u8c = *p; |
1851 else | 1849 else |
1852 { | 1850 { |
1853 len = utf_head_off(ptr, p); | 1851 len = utf_head_off(ptr, p); |
1854 p -= len; | 1852 p -= len; |
1855 u8c = utf_ptr2char(p); | 1853 u8c = utf_ptr2char(p); |
1856 if (len == 0) | 1854 if (len == 0) |
1857 { | 1855 { |
1858 /* Not a valid UTF-8 character, retry with | 1856 // Not a valid UTF-8 character, retry with |
1859 * another fenc when possible, otherwise just | 1857 // another fenc when possible, otherwise just |
1860 * report the error. */ | 1858 // report the error. |
1861 if (can_retry) | 1859 if (can_retry) |
1862 goto rewind_retry; | 1860 goto rewind_retry; |
1863 if (conv_error == 0) | 1861 if (conv_error == 0) |
1864 conv_error = readfile_linenr(linecnt, | 1862 conv_error = readfile_linenr(linecnt, |
1865 ptr, p); | 1863 ptr, p); |
1868 if (bad_char_behavior != BAD_KEEP) | 1866 if (bad_char_behavior != BAD_KEEP) |
1869 u8c = bad_char_behavior; | 1867 u8c = bad_char_behavior; |
1870 } | 1868 } |
1871 } | 1869 } |
1872 } | 1870 } |
1873 if (enc_utf8) /* produce UTF-8 */ | 1871 if (enc_utf8) // produce UTF-8 |
1874 { | 1872 { |
1875 dest -= utf_char2len(u8c); | 1873 dest -= utf_char2len(u8c); |
1876 (void)utf_char2bytes(u8c, dest); | 1874 (void)utf_char2bytes(u8c, dest); |
1877 } | 1875 } |
1878 else /* produce Latin1 */ | 1876 else // produce Latin1 |
1879 { | 1877 { |
1880 --dest; | 1878 --dest; |
1881 if (u8c >= 0x100) | 1879 if (u8c >= 0x100) |
1882 { | 1880 { |
1883 /* character doesn't fit in latin1, retry with | 1881 // character doesn't fit in latin1, retry with |
1884 * another fenc when possible, otherwise just | 1882 // another fenc when possible, otherwise just |
1885 * report the error. */ | 1883 // report the error. |
1886 if (can_retry) | 1884 if (can_retry) |
1887 goto rewind_retry; | 1885 goto rewind_retry; |
1888 if (conv_error == 0) | 1886 if (conv_error == 0) |
1889 conv_error = readfile_linenr(linecnt, ptr, p); | 1887 conv_error = readfile_linenr(linecnt, ptr, p); |
1890 if (bad_char_behavior == BAD_DROP) | 1888 if (bad_char_behavior == BAD_DROP) |
1899 else | 1897 else |
1900 *dest = u8c; | 1898 *dest = u8c; |
1901 } | 1899 } |
1902 } | 1900 } |
1903 | 1901 |
1904 /* move the linerest to before the converted characters */ | 1902 // move the linerest to before the converted characters |
1905 line_start = dest - linerest; | 1903 line_start = dest - linerest; |
1906 mch_memmove(line_start, buffer, (size_t)linerest); | 1904 mch_memmove(line_start, buffer, (size_t)linerest); |
1907 size = (long)((ptr + real_size) - dest); | 1905 size = (long)((ptr + real_size) - dest); |
1908 ptr = dest; | 1906 ptr = dest; |
1909 } | 1907 } |
1910 else if (enc_utf8 && !curbuf->b_p_bin) | 1908 else if (enc_utf8 && !curbuf->b_p_bin) |
1911 { | 1909 { |
1912 int incomplete_tail = FALSE; | 1910 int incomplete_tail = FALSE; |
1913 | 1911 |
1914 /* Reading UTF-8: Check if the bytes are valid UTF-8. */ | 1912 // Reading UTF-8: Check if the bytes are valid UTF-8. |
1915 for (p = ptr; ; ++p) | 1913 for (p = ptr; ; ++p) |
1916 { | 1914 { |
1917 int todo = (int)((ptr + size) - p); | 1915 int todo = (int)((ptr + size) - p); |
1918 int l; | 1916 int l; |
1919 | 1917 |
1920 if (todo <= 0) | 1918 if (todo <= 0) |
1921 break; | 1919 break; |
1922 if (*p >= 0x80) | 1920 if (*p >= 0x80) |
1923 { | 1921 { |
1924 /* A length of 1 means it's an illegal byte. Accept | 1922 // A length of 1 means it's an illegal byte. Accept |
1925 * an incomplete character at the end though, the next | 1923 // an incomplete character at the end though, the next |
1926 * read() will get the next bytes, we'll check it | 1924 // read() will get the next bytes, we'll check it |
1927 * then. */ | 1925 // then. |
1928 l = utf_ptr2len_len(p, todo); | 1926 l = utf_ptr2len_len(p, todo); |
1929 if (l > todo && !incomplete_tail) | 1927 if (l > todo && !incomplete_tail) |
1930 { | 1928 { |
1931 /* Avoid retrying with a different encoding when | 1929 // Avoid retrying with a different encoding when |
1932 * a truncated file is more likely, or attempting | 1930 // a truncated file is more likely, or attempting |
1933 * to read the rest of an incomplete sequence when | 1931 // to read the rest of an incomplete sequence when |
1934 * we have already done so. */ | 1932 // we have already done so. |
1935 if (p > ptr || filesize > 0) | 1933 if (p > ptr || filesize > 0) |
1936 incomplete_tail = TRUE; | 1934 incomplete_tail = TRUE; |
1937 /* Incomplete byte sequence, move it to conv_rest[] | 1935 // Incomplete byte sequence, move it to conv_rest[] |
1938 * and try to read the rest of it, unless we've | 1936 // and try to read the rest of it, unless we've |
1939 * already done so. */ | 1937 // already done so. |
1940 if (p > ptr) | 1938 if (p > ptr) |
1941 { | 1939 { |
1942 conv_restlen = todo; | 1940 conv_restlen = todo; |
1943 mch_memmove(conv_rest, p, conv_restlen); | 1941 mch_memmove(conv_rest, p, conv_restlen); |
1944 size -= conv_restlen; | 1942 size -= conv_restlen; |
1945 break; | 1943 break; |
1946 } | 1944 } |
1947 } | 1945 } |
1948 if (l == 1 || l > todo) | 1946 if (l == 1 || l > todo) |
1949 { | 1947 { |
1950 /* Illegal byte. If we can try another encoding | 1948 // Illegal byte. If we can try another encoding |
1951 * do that, unless at EOF where a truncated | 1949 // do that, unless at EOF where a truncated |
1952 * file is more likely than a conversion error. */ | 1950 // file is more likely than a conversion error. |
1953 if (can_retry && !incomplete_tail) | 1951 if (can_retry && !incomplete_tail) |
1954 break; | 1952 break; |
1955 #ifdef USE_ICONV | 1953 #ifdef USE_ICONV |
1956 /* When we did a conversion report an error. */ | 1954 // When we did a conversion report an error. |
1957 if (iconv_fd != (iconv_t)-1 && conv_error == 0) | 1955 if (iconv_fd != (iconv_t)-1 && conv_error == 0) |
1958 conv_error = readfile_linenr(linecnt, ptr, p); | 1956 conv_error = readfile_linenr(linecnt, ptr, p); |
1959 #endif | 1957 #endif |
1960 /* Remember the first linenr with an illegal byte */ | 1958 // Remember the first linenr with an illegal byte |
1961 if (conv_error == 0 && illegal_byte == 0) | 1959 if (conv_error == 0 && illegal_byte == 0) |
1962 illegal_byte = readfile_linenr(linecnt, ptr, p); | 1960 illegal_byte = readfile_linenr(linecnt, ptr, p); |
1963 | 1961 |
1964 /* Drop, keep or replace the bad byte. */ | 1962 // Drop, keep or replace the bad byte. |
1965 if (bad_char_behavior == BAD_DROP) | 1963 if (bad_char_behavior == BAD_DROP) |
1966 { | 1964 { |
1967 mch_memmove(p, p + 1, todo - 1); | 1965 mch_memmove(p, p + 1, todo - 1); |
1968 --p; | 1966 --p; |
1969 --size; | 1967 --size; |
1975 p += l - 1; | 1973 p += l - 1; |
1976 } | 1974 } |
1977 } | 1975 } |
1978 if (p < ptr + size && !incomplete_tail) | 1976 if (p < ptr + size && !incomplete_tail) |
1979 { | 1977 { |
1980 /* Detected a UTF-8 error. */ | 1978 // Detected a UTF-8 error. |
1981 rewind_retry: | 1979 rewind_retry: |
1982 /* Retry reading with another conversion. */ | 1980 // Retry reading with another conversion. |
1983 #if defined(FEAT_EVAL) && defined(USE_ICONV) | 1981 #if defined(FEAT_EVAL) && defined(USE_ICONV) |
1984 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1) | 1982 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1) |
1985 /* iconv() failed, try 'charconvert' */ | 1983 // iconv() failed, try 'charconvert' |
1986 did_iconv = TRUE; | 1984 did_iconv = TRUE; |
1987 else | 1985 else |
1988 #endif | 1986 #endif |
1989 /* use next item from 'fileencodings' */ | 1987 // use next item from 'fileencodings' |
1990 advance_fenc = TRUE; | 1988 advance_fenc = TRUE; |
1991 file_rewind = TRUE; | 1989 file_rewind = TRUE; |
1992 goto retry; | 1990 goto retry; |
1993 } | 1991 } |
1994 } | 1992 } |
1995 | 1993 |
1996 /* count the number of characters (after conversion!) */ | 1994 // count the number of characters (after conversion!) |
1997 filesize += size; | 1995 filesize += size; |
1998 | 1996 |
1999 /* | 1997 /* |
2000 * when reading the first part of a file: guess EOL type | 1998 * when reading the first part of a file: guess EOL type |
2001 */ | 1999 */ |
2002 if (fileformat == EOL_UNKNOWN) | 2000 if (fileformat == EOL_UNKNOWN) |
2003 { | 2001 { |
2004 /* First try finding a NL, for Dos and Unix */ | 2002 // First try finding a NL, for Dos and Unix |
2005 if (try_dos || try_unix) | 2003 if (try_dos || try_unix) |
2006 { | 2004 { |
2007 /* Reset the carriage return counter. */ | 2005 // Reset the carriage return counter. |
2008 if (try_mac) | 2006 if (try_mac) |
2009 try_mac = 1; | 2007 try_mac = 1; |
2010 | 2008 |
2011 for (p = ptr; p < ptr + size; ++p) | 2009 for (p = ptr; p < ptr + size; ++p) |
2012 { | 2010 { |
2021 } | 2019 } |
2022 else if (*p == CAR && try_mac) | 2020 else if (*p == CAR && try_mac) |
2023 try_mac++; | 2021 try_mac++; |
2024 } | 2022 } |
2025 | 2023 |
2026 /* Don't give in to EOL_UNIX if EOL_MAC is more likely */ | 2024 // Don't give in to EOL_UNIX if EOL_MAC is more likely |
2027 if (fileformat == EOL_UNIX && try_mac) | 2025 if (fileformat == EOL_UNIX && try_mac) |
2028 { | 2026 { |
2029 /* Need to reset the counters when retrying fenc. */ | 2027 // Need to reset the counters when retrying fenc. |
2030 try_mac = 1; | 2028 try_mac = 1; |
2031 try_unix = 1; | 2029 try_unix = 1; |
2032 for (; p >= ptr && *p != CAR; p--) | 2030 for (; p >= ptr && *p != CAR; p--) |
2033 ; | 2031 ; |
2034 if (p >= ptr) | 2032 if (p >= ptr) |
2043 if (try_mac > try_unix) | 2041 if (try_mac > try_unix) |
2044 fileformat = EOL_MAC; | 2042 fileformat = EOL_MAC; |
2045 } | 2043 } |
2046 } | 2044 } |
2047 else if (fileformat == EOL_UNKNOWN && try_mac == 1) | 2045 else if (fileformat == EOL_UNKNOWN && try_mac == 1) |
2048 /* Looking for CR but found no end-of-line markers at | 2046 // Looking for CR but found no end-of-line markers at |
2049 * all: use the default format. */ | 2047 // all: use the default format. |
2050 fileformat = default_fileformat(); | 2048 fileformat = default_fileformat(); |
2051 } | 2049 } |
2052 | 2050 |
2053 /* No NL found: may use Mac format */ | 2051 // No NL found: may use Mac format |
2054 if (fileformat == EOL_UNKNOWN && try_mac) | 2052 if (fileformat == EOL_UNKNOWN && try_mac) |
2055 fileformat = EOL_MAC; | 2053 fileformat = EOL_MAC; |
2056 | 2054 |
2057 /* Still nothing found? Use first format in 'ffs' */ | 2055 // Still nothing found? Use first format in 'ffs' |
2058 if (fileformat == EOL_UNKNOWN) | 2056 if (fileformat == EOL_UNKNOWN) |
2059 fileformat = default_fileformat(); | 2057 fileformat = default_fileformat(); |
2060 | 2058 |
2061 /* if editing a new file: may set p_tx and p_ff */ | 2059 // if editing a new file: may set p_tx and p_ff |
2062 if (set_options) | 2060 if (set_options) |
2063 set_fileformat(fileformat, OPT_LOCAL); | 2061 set_fileformat(fileformat, OPT_LOCAL); |
2064 } | 2062 } |
2065 } | 2063 } |
2066 | 2064 |
2071 if (fileformat == EOL_MAC) | 2069 if (fileformat == EOL_MAC) |
2072 { | 2070 { |
2073 --ptr; | 2071 --ptr; |
2074 while (++ptr, --size >= 0) | 2072 while (++ptr, --size >= 0) |
2075 { | 2073 { |
2076 /* catch most common case first */ | 2074 // catch most common case first |
2077 if ((c = *ptr) != NUL && c != CAR && c != NL) | 2075 if ((c = *ptr) != NUL && c != CAR && c != NL) |
2078 continue; | 2076 continue; |
2079 if (c == NUL) | 2077 if (c == NUL) |
2080 *ptr = NL; /* NULs are replaced by newlines! */ | 2078 *ptr = NL; // NULs are replaced by newlines! |
2081 else if (c == NL) | 2079 else if (c == NL) |
2082 *ptr = CAR; /* NLs are replaced by CRs! */ | 2080 *ptr = CAR; // NLs are replaced by CRs! |
2083 else | 2081 else |
2084 { | 2082 { |
2085 if (skip_count == 0) | 2083 if (skip_count == 0) |
2086 { | 2084 { |
2087 *ptr = NUL; /* end of line */ | 2085 *ptr = NUL; // end of line |
2088 len = (colnr_T) (ptr - line_start + 1); | 2086 len = (colnr_T) (ptr - line_start + 1); |
2089 if (ml_append(lnum, line_start, len, newfile) == FAIL) | 2087 if (ml_append(lnum, line_start, len, newfile) == FAIL) |
2090 { | 2088 { |
2091 error = TRUE; | 2089 error = TRUE; |
2092 break; | 2090 break; |
2096 sha256_update(&sha_ctx, line_start, len); | 2094 sha256_update(&sha_ctx, line_start, len); |
2097 #endif | 2095 #endif |
2098 ++lnum; | 2096 ++lnum; |
2099 if (--read_count == 0) | 2097 if (--read_count == 0) |
2100 { | 2098 { |
2101 error = TRUE; /* break loop */ | 2099 error = TRUE; // break loop |
2102 line_start = ptr; /* nothing left to write */ | 2100 line_start = ptr; // nothing left to write |
2103 break; | 2101 break; |
2104 } | 2102 } |
2105 } | 2103 } |
2106 else | 2104 else |
2107 --skip_count; | 2105 --skip_count; |
2112 else | 2110 else |
2113 { | 2111 { |
2114 --ptr; | 2112 --ptr; |
2115 while (++ptr, --size >= 0) | 2113 while (++ptr, --size >= 0) |
2116 { | 2114 { |
2117 if ((c = *ptr) != NUL && c != NL) /* catch most common case */ | 2115 if ((c = *ptr) != NUL && c != NL) // catch most common case |
2118 continue; | 2116 continue; |
2119 if (c == NUL) | 2117 if (c == NUL) |
2120 *ptr = NL; /* NULs are replaced by newlines! */ | 2118 *ptr = NL; // NULs are replaced by newlines! |
2121 else | 2119 else |
2122 { | 2120 { |
2123 if (skip_count == 0) | 2121 if (skip_count == 0) |
2124 { | 2122 { |
2125 *ptr = NUL; /* end of line */ | 2123 *ptr = NUL; // end of line |
2126 len = (colnr_T)(ptr - line_start + 1); | 2124 len = (colnr_T)(ptr - line_start + 1); |
2127 if (fileformat == EOL_DOS) | 2125 if (fileformat == EOL_DOS) |
2128 { | 2126 { |
2129 if (ptr > line_start && ptr[-1] == CAR) | 2127 if (ptr > line_start && ptr[-1] == CAR) |
2130 { | 2128 { |
2131 /* remove CR before NL */ | 2129 // remove CR before NL |
2132 ptr[-1] = NUL; | 2130 ptr[-1] = NUL; |
2133 --len; | 2131 --len; |
2134 } | 2132 } |
2135 /* | 2133 /* |
2136 * Reading in Dos format, but no CR-LF found! | 2134 * Reading in Dos format, but no CR-LF found! |
2166 sha256_update(&sha_ctx, line_start, len); | 2164 sha256_update(&sha_ctx, line_start, len); |
2167 #endif | 2165 #endif |
2168 ++lnum; | 2166 ++lnum; |
2169 if (--read_count == 0) | 2167 if (--read_count == 0) |
2170 { | 2168 { |
2171 error = TRUE; /* break loop */ | 2169 error = TRUE; // break loop |
2172 line_start = ptr; /* nothing left to write */ | 2170 line_start = ptr; // nothing left to write |
2173 break; | 2171 break; |
2174 } | 2172 } |
2175 } | 2173 } |
2176 else | 2174 else |
2177 --skip_count; | 2175 --skip_count; |
2182 linerest = (long)(ptr - line_start); | 2180 linerest = (long)(ptr - line_start); |
2183 ui_breakcheck(); | 2181 ui_breakcheck(); |
2184 } | 2182 } |
2185 | 2183 |
2186 failed: | 2184 failed: |
2187 /* not an error, max. number of lines reached */ | 2185 // not an error, max. number of lines reached |
2188 if (error && read_count == 0) | 2186 if (error && read_count == 0) |
2189 error = FALSE; | 2187 error = FALSE; |
2190 | 2188 |
2191 /* | 2189 /* |
2192 * If we get EOF in the middle of a line, note the fact and | 2190 * If we get EOF in the middle of a line, note the fact and |
2199 && !(!curbuf->b_p_bin | 2197 && !(!curbuf->b_p_bin |
2200 && fileformat == EOL_DOS | 2198 && fileformat == EOL_DOS |
2201 && *line_start == Ctrl_Z | 2199 && *line_start == Ctrl_Z |
2202 && ptr == line_start + 1)) | 2200 && ptr == line_start + 1)) |
2203 { | 2201 { |
2204 /* remember for when writing */ | 2202 // remember for when writing |
2205 if (set_options) | 2203 if (set_options) |
2206 curbuf->b_p_eol = FALSE; | 2204 curbuf->b_p_eol = FALSE; |
2207 *ptr = NUL; | 2205 *ptr = NUL; |
2208 len = (colnr_T)(ptr - line_start + 1); | 2206 len = (colnr_T)(ptr - line_start + 1); |
2209 if (ml_append(lnum, line_start, len, newfile) == FAIL) | 2207 if (ml_append(lnum, line_start, len, newfile) == FAIL) |
2217 read_no_eol_lnum = ++lnum; | 2215 read_no_eol_lnum = ++lnum; |
2218 } | 2216 } |
2219 } | 2217 } |
2220 | 2218 |
2221 if (set_options) | 2219 if (set_options) |
2222 save_file_ff(curbuf); /* remember the current file format */ | 2220 save_file_ff(curbuf); // remember the current file format |
2223 | 2221 |
2224 #ifdef FEAT_CRYPT | 2222 #ifdef FEAT_CRYPT |
2225 if (curbuf->b_cryptstate != NULL) | 2223 if (curbuf->b_cryptstate != NULL) |
2226 { | 2224 { |
2227 crypt_free_state(curbuf->b_cryptstate); | 2225 crypt_free_state(curbuf->b_cryptstate); |
2228 curbuf->b_cryptstate = NULL; | 2226 curbuf->b_cryptstate = NULL; |
2229 } | 2227 } |
2230 if (cryptkey != NULL && cryptkey != curbuf->b_p_key) | 2228 if (cryptkey != NULL && cryptkey != curbuf->b_p_key) |
2231 crypt_free_key(cryptkey); | 2229 crypt_free_key(cryptkey); |
2232 /* Don't set cryptkey to NULL, it's used below as a flag that | 2230 // Don't set cryptkey to NULL, it's used below as a flag that |
2233 * encryption was used. */ | 2231 // encryption was used. |
2234 #endif | 2232 #endif |
2235 | 2233 |
2236 /* If editing a new file: set 'fenc' for the current buffer. | 2234 // If editing a new file: set 'fenc' for the current buffer. |
2237 * Also for ":read ++edit file". */ | 2235 // Also for ":read ++edit file". |
2238 if (set_options) | 2236 if (set_options) |
2239 set_string_option_direct((char_u *)"fenc", -1, fenc, | 2237 set_string_option_direct((char_u *)"fenc", -1, fenc, |
2240 OPT_FREE|OPT_LOCAL, 0); | 2238 OPT_FREE|OPT_LOCAL, 0); |
2241 if (fenc_alloced) | 2239 if (fenc_alloced) |
2242 vim_free(fenc); | 2240 vim_free(fenc); |
2244 if (iconv_fd != (iconv_t)-1) | 2242 if (iconv_fd != (iconv_t)-1) |
2245 iconv_close(iconv_fd); | 2243 iconv_close(iconv_fd); |
2246 #endif | 2244 #endif |
2247 | 2245 |
2248 if (!read_buffer && !read_stdin) | 2246 if (!read_buffer && !read_stdin) |
2249 close(fd); /* errors are ignored */ | 2247 close(fd); // errors are ignored |
2250 #ifdef HAVE_FD_CLOEXEC | 2248 #ifdef HAVE_FD_CLOEXEC |
2251 else | 2249 else |
2252 { | 2250 { |
2253 int fdflags = fcntl(fd, F_GETFD); | 2251 int fdflags = fcntl(fd, F_GETFD); |
2254 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0) | 2252 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0) |
2258 vim_free(buffer); | 2256 vim_free(buffer); |
2259 | 2257 |
2260 #ifdef HAVE_DUP | 2258 #ifdef HAVE_DUP |
2261 if (read_stdin) | 2259 if (read_stdin) |
2262 { | 2260 { |
2263 /* Use stderr for stdin, makes shell commands work. */ | 2261 // Use stderr for stdin, makes shell commands work. |
2264 close(0); | 2262 close(0); |
2265 vim_ignored = dup(2); | 2263 vim_ignored = dup(2); |
2266 } | 2264 } |
2267 #endif | 2265 #endif |
2268 | 2266 |
2269 if (tmpname != NULL) | 2267 if (tmpname != NULL) |
2270 { | 2268 { |
2271 mch_remove(tmpname); /* delete converted file */ | 2269 mch_remove(tmpname); // delete converted file |
2272 vim_free(tmpname); | 2270 vim_free(tmpname); |
2273 } | 2271 } |
2274 --no_wait_return; /* may wait for return now */ | 2272 --no_wait_return; // may wait for return now |
2275 | 2273 |
2276 /* | 2274 /* |
2277 * In recovery mode everything but autocommands is skipped. | 2275 * In recovery mode everything but autocommands is skipped. |
2278 */ | 2276 */ |
2279 if (!recoverymode) | 2277 if (!recoverymode) |
2280 { | 2278 { |
2281 /* need to delete the last line, which comes from the empty buffer */ | 2279 // need to delete the last line, which comes from the empty buffer |
2282 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY)) | 2280 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY)) |
2283 { | 2281 { |
2284 #ifdef FEAT_NETBEANS_INTG | 2282 #ifdef FEAT_NETBEANS_INTG |
2285 netbeansFireChanges = 0; | 2283 netbeansFireChanges = 0; |
2286 #endif | 2284 #endif |
2295 linecnt = 0; | 2293 linecnt = 0; |
2296 if (newfile || read_buffer) | 2294 if (newfile || read_buffer) |
2297 { | 2295 { |
2298 redraw_curbuf_later(NOT_VALID); | 2296 redraw_curbuf_later(NOT_VALID); |
2299 #ifdef FEAT_DIFF | 2297 #ifdef FEAT_DIFF |
2300 /* After reading the text into the buffer the diff info needs to | 2298 // After reading the text into the buffer the diff info needs to |
2301 * be updated. */ | 2299 // be updated. |
2302 diff_invalidate(curbuf); | 2300 diff_invalidate(curbuf); |
2303 #endif | 2301 #endif |
2304 #ifdef FEAT_FOLDING | 2302 #ifdef FEAT_FOLDING |
2305 /* All folds in the window are invalid now. Mark them for update | 2303 // All folds in the window are invalid now. Mark them for update |
2306 * before triggering autocommands. */ | 2304 // before triggering autocommands. |
2307 foldUpdateAll(curwin); | 2305 foldUpdateAll(curwin); |
2308 #endif | 2306 #endif |
2309 } | 2307 } |
2310 else if (linecnt) /* appended at least one line */ | 2308 else if (linecnt) // appended at least one line |
2311 appended_lines_mark(from, linecnt); | 2309 appended_lines_mark(from, linecnt); |
2312 | 2310 |
2313 #ifndef ALWAYS_USE_GUI | 2311 #ifndef ALWAYS_USE_GUI |
2314 /* | 2312 /* |
2315 * If we were reading from the same terminal as where messages go, | 2313 * If we were reading from the same terminal as where messages go, |
2316 * the screen will have been messed up. | 2314 * the screen will have been messed up. |
2317 * Switch on raw mode now and clear the screen. | 2315 * Switch on raw mode now and clear the screen. |
2318 */ | 2316 */ |
2319 if (read_stdin) | 2317 if (read_stdin) |
2320 { | 2318 { |
2321 settmode(TMODE_RAW); /* set to raw mode */ | 2319 settmode(TMODE_RAW); // set to raw mode |
2322 starttermcap(); | 2320 starttermcap(); |
2323 screenclear(); | 2321 screenclear(); |
2324 } | 2322 } |
2325 #endif | 2323 #endif |
2326 | 2324 |
2328 { | 2326 { |
2329 if (!(flags & READ_DUMMY)) | 2327 if (!(flags & READ_DUMMY)) |
2330 { | 2328 { |
2331 filemess(curbuf, sfname, (char_u *)_(e_interr), 0); | 2329 filemess(curbuf, sfname, (char_u *)_(e_interr), 0); |
2332 if (newfile) | 2330 if (newfile) |
2333 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | 2331 curbuf->b_p_ro = TRUE; // must use "w!" now |
2334 } | 2332 } |
2335 msg_scroll = msg_save; | 2333 msg_scroll = msg_save; |
2336 #ifdef FEAT_VIMINFO | 2334 #ifdef FEAT_VIMINFO |
2337 check_marks_read(); | 2335 check_marks_read(); |
2338 #endif | 2336 #endif |
2339 return OK; /* an interrupt isn't really an error */ | 2337 return OK; // an interrupt isn't really an error |
2340 } | 2338 } |
2341 | 2339 |
2342 if (!filtering && !(flags & READ_DUMMY)) | 2340 if (!filtering && !(flags & READ_DUMMY)) |
2343 { | 2341 { |
2344 msg_add_fname(curbuf, sfname); /* fname in IObuff with quotes */ | 2342 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes |
2345 c = FALSE; | 2343 c = FALSE; |
2346 | 2344 |
2347 #ifdef UNIX | 2345 #ifdef UNIX |
2348 if (S_ISFIFO(perm)) /* fifo */ | 2346 if (S_ISFIFO(perm)) // fifo |
2349 { | 2347 { |
2350 STRCAT(IObuff, _("[fifo]")); | 2348 STRCAT(IObuff, _("[fifo]")); |
2351 c = TRUE; | 2349 c = TRUE; |
2352 } | 2350 } |
2353 if (S_ISSOCK(perm)) /* or socket */ | 2351 if (S_ISSOCK(perm)) // or socket |
2354 { | 2352 { |
2355 STRCAT(IObuff, _("[socket]")); | 2353 STRCAT(IObuff, _("[socket]")); |
2356 c = TRUE; | 2354 c = TRUE; |
2357 } | 2355 } |
2358 # ifdef OPEN_CHR_FILES | 2356 # ifdef OPEN_CHR_FILES |
2359 if (S_ISCHR(perm)) /* or character special */ | 2357 if (S_ISCHR(perm)) // or character special |
2360 { | 2358 { |
2361 STRCAT(IObuff, _("[character special]")); | 2359 STRCAT(IObuff, _("[character special]")); |
2362 c = TRUE; | 2360 c = TRUE; |
2363 } | 2361 } |
2364 # endif | 2362 # endif |
2428 msg_add_lines(c, (long)linecnt, filesize); | 2426 msg_add_lines(c, (long)linecnt, filesize); |
2429 | 2427 |
2430 VIM_CLEAR(keep_msg); | 2428 VIM_CLEAR(keep_msg); |
2431 msg_scrolled_ign = TRUE; | 2429 msg_scrolled_ign = TRUE; |
2432 #ifdef ALWAYS_USE_GUI | 2430 #ifdef ALWAYS_USE_GUI |
2433 /* Don't show the message when reading stdin, it would end up in a | 2431 // Don't show the message when reading stdin, it would end up in a |
2434 * message box (which might be shown when exiting!) */ | 2432 // message box (which might be shown when exiting!) |
2435 if (read_stdin || read_buffer) | 2433 if (read_stdin || read_buffer) |
2436 p = msg_may_trunc(FALSE, IObuff); | 2434 p = msg_may_trunc(FALSE, IObuff); |
2437 else | 2435 else |
2438 #endif | 2436 #endif |
2439 { | 2437 { |
2441 msg_putchar('\r'); // overwrite previous message | 2439 msg_putchar('\r'); // overwrite previous message |
2442 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0); | 2440 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0); |
2443 } | 2441 } |
2444 if (read_stdin || read_buffer || restart_edit != 0 | 2442 if (read_stdin || read_buffer || restart_edit != 0 |
2445 || (msg_scrolled != 0 && !need_wait_return)) | 2443 || (msg_scrolled != 0 && !need_wait_return)) |
2446 /* Need to repeat the message after redrawing when: | 2444 // Need to repeat the message after redrawing when: |
2447 * - When reading from stdin (the screen will be cleared next). | 2445 // - When reading from stdin (the screen will be cleared next). |
2448 * - When restart_edit is set (otherwise there will be a delay | 2446 // - When restart_edit is set (otherwise there will be a delay |
2449 * before redrawing). | 2447 // before redrawing). |
2450 * - When the screen was scrolled but there is no wait-return | 2448 // - When the screen was scrolled but there is no wait-return |
2451 * prompt. */ | 2449 // prompt. |
2452 set_keep_msg(p, 0); | 2450 set_keep_msg(p, 0); |
2453 msg_scrolled_ign = FALSE; | 2451 msg_scrolled_ign = FALSE; |
2454 } | 2452 } |
2455 | 2453 |
2456 /* with errors writing the file requires ":w!" */ | 2454 // with errors writing the file requires ":w!" |
2457 if (newfile && (error | 2455 if (newfile && (error |
2458 || conv_error != 0 | 2456 || conv_error != 0 |
2459 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP))) | 2457 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP))) |
2460 curbuf->b_p_ro = TRUE; | 2458 curbuf->b_p_ro = TRUE; |
2461 | 2459 |
2462 u_clearline(); /* cannot use "U" command after adding lines */ | 2460 u_clearline(); // cannot use "U" command after adding lines |
2463 | 2461 |
2464 /* | 2462 /* |
2465 * In Ex mode: cursor at last new line. | 2463 * In Ex mode: cursor at last new line. |
2466 * Otherwise: cursor at first new line. | 2464 * Otherwise: cursor at first new line. |
2467 */ | 2465 */ |
2468 if (exmode_active) | 2466 if (exmode_active) |
2469 curwin->w_cursor.lnum = from + linecnt; | 2467 curwin->w_cursor.lnum = from + linecnt; |
2470 else | 2468 else |
2471 curwin->w_cursor.lnum = from + 1; | 2469 curwin->w_cursor.lnum = from + 1; |
2472 check_cursor_lnum(); | 2470 check_cursor_lnum(); |
2473 beginline(BL_WHITE | BL_FIX); /* on first non-blank */ | 2471 beginline(BL_WHITE | BL_FIX); // on first non-blank |
2474 | 2472 |
2475 if (!cmdmod.lockmarks) | 2473 if (!cmdmod.lockmarks) |
2476 { | 2474 { |
2477 // Set '[ and '] marks to the newly read lines. | 2475 // Set '[ and '] marks to the newly read lines. |
2478 curbuf->b_op_start.lnum = from + 1; | 2476 curbuf->b_op_start.lnum = from + 1; |
2512 * or writing the read again with 'binary' on. The latter is required | 2510 * or writing the read again with 'binary' on. The latter is required |
2513 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work. | 2511 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work. |
2514 */ | 2512 */ |
2515 curbuf->b_no_eol_lnum = read_no_eol_lnum; | 2513 curbuf->b_no_eol_lnum = read_no_eol_lnum; |
2516 | 2514 |
2517 /* When reloading a buffer put the cursor at the first line that is | 2515 // When reloading a buffer put the cursor at the first line that is |
2518 * different. */ | 2516 // different. |
2519 if (flags & READ_KEEP_UNDO) | 2517 if (flags & READ_KEEP_UNDO) |
2520 u_find_first_changed(); | 2518 u_find_first_changed(); |
2521 | 2519 |
2522 #ifdef FEAT_PERSISTENT_UNDO | 2520 #ifdef FEAT_PERSISTENT_UNDO |
2523 /* | 2521 /* |
2535 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL)) | 2533 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL)) |
2536 { | 2534 { |
2537 int m = msg_scroll; | 2535 int m = msg_scroll; |
2538 int n = msg_scrolled; | 2536 int n = msg_scrolled; |
2539 | 2537 |
2540 /* Save the fileformat now, otherwise the buffer will be considered | 2538 // Save the fileformat now, otherwise the buffer will be considered |
2541 * modified if the format/encoding was automatically detected. */ | 2539 // modified if the format/encoding was automatically detected. |
2542 if (set_options) | 2540 if (set_options) |
2543 save_file_ff(curbuf); | 2541 save_file_ff(curbuf); |
2544 | 2542 |
2545 /* | 2543 /* |
2546 * The output from the autocommands should not overwrite anything and | 2544 * The output from the autocommands should not overwrite anything and |
2567 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname, | 2565 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname, |
2568 FALSE, NULL, eap); | 2566 FALSE, NULL, eap); |
2569 if (msg_scrolled == n) | 2567 if (msg_scrolled == n) |
2570 msg_scroll = m; | 2568 msg_scroll = m; |
2571 # ifdef FEAT_EVAL | 2569 # ifdef FEAT_EVAL |
2572 if (aborting()) /* autocmds may abort script processing */ | 2570 if (aborting()) // autocmds may abort script processing |
2573 return FAIL; | 2571 return FAIL; |
2574 # endif | 2572 # endif |
2575 } | 2573 } |
2576 | 2574 |
2577 if (recoverymode && error) | 2575 if (recoverymode && error) |
2602 * line number where we are now. | 2600 * line number where we are now. |
2603 * Used for error messages that include a line number. | 2601 * Used for error messages that include a line number. |
2604 */ | 2602 */ |
2605 static linenr_T | 2603 static linenr_T |
2606 readfile_linenr( | 2604 readfile_linenr( |
2607 linenr_T linecnt, /* line count before reading more bytes */ | 2605 linenr_T linecnt, // line count before reading more bytes |
2608 char_u *p, /* start of more bytes read */ | 2606 char_u *p, // start of more bytes read |
2609 char_u *endp) /* end of more bytes read */ | 2607 char_u *endp) // end of more bytes read |
2610 { | 2608 { |
2611 char_u *s; | 2609 char_u *s; |
2612 linenr_T lnum; | 2610 linenr_T lnum; |
2613 | 2611 |
2614 lnum = curbuf->b_ml.ml_line_count - linecnt + 1; | 2612 lnum = curbuf->b_ml.ml_line_count - linecnt + 1; |
2645 * Set default or forced 'fileformat' and 'binary'. | 2643 * Set default or forced 'fileformat' and 'binary'. |
2646 */ | 2644 */ |
2647 void | 2645 void |
2648 set_file_options(int set_options, exarg_T *eap) | 2646 set_file_options(int set_options, exarg_T *eap) |
2649 { | 2647 { |
2650 /* set default 'fileformat' */ | 2648 // set default 'fileformat' |
2651 if (set_options) | 2649 if (set_options) |
2652 { | 2650 { |
2653 if (eap != NULL && eap->force_ff != 0) | 2651 if (eap != NULL && eap->force_ff != 0) |
2654 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL); | 2652 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL); |
2655 else if (*p_ffs != NUL) | 2653 else if (*p_ffs != NUL) |
2656 set_fileformat(default_fileformat(), OPT_LOCAL); | 2654 set_fileformat(default_fileformat(), OPT_LOCAL); |
2657 } | 2655 } |
2658 | 2656 |
2659 /* set or reset 'binary' */ | 2657 // set or reset 'binary' |
2660 if (eap != NULL && eap->force_bin != 0) | 2658 if (eap != NULL && eap->force_bin != 0) |
2661 { | 2659 { |
2662 int oldval = curbuf->b_p_bin; | 2660 int oldval = curbuf->b_p_bin; |
2663 | 2661 |
2664 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN); | 2662 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN); |
2740 * after reading it). | 2738 * after reading it). |
2741 * Returns NULL if the conversion failed ("*fdp" is not set) . | 2739 * Returns NULL if the conversion failed ("*fdp" is not set) . |
2742 */ | 2740 */ |
2743 static char_u * | 2741 static char_u * |
2744 readfile_charconvert( | 2742 readfile_charconvert( |
2745 char_u *fname, /* name of input file */ | 2743 char_u *fname, // name of input file |
2746 char_u *fenc, /* converted from */ | 2744 char_u *fenc, // converted from |
2747 int *fdp) /* in/out: file descriptor of file */ | 2745 int *fdp) // in/out: file descriptor of file |
2748 { | 2746 { |
2749 char_u *tmpname; | 2747 char_u *tmpname; |
2750 char *errmsg = NULL; | 2748 char *errmsg = NULL; |
2751 | 2749 |
2752 tmpname = vim_tempname('r', FALSE); | 2750 tmpname = vim_tempname('r', FALSE); |
2753 if (tmpname == NULL) | 2751 if (tmpname == NULL) |
2754 errmsg = _("Can't find temp file for conversion"); | 2752 errmsg = _("Can't find temp file for conversion"); |
2755 else | 2753 else |
2756 { | 2754 { |
2757 close(*fdp); /* close the input file, ignore errors */ | 2755 close(*fdp); // close the input file, ignore errors |
2758 *fdp = -1; | 2756 *fdp = -1; |
2759 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc, | 2757 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc, |
2760 fname, tmpname) == FAIL) | 2758 fname, tmpname) == FAIL) |
2761 errmsg = _("Conversion with 'charconvert' failed"); | 2759 errmsg = _("Conversion with 'charconvert' failed"); |
2762 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname, | 2760 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname, |
2764 errmsg = _("can't read output of 'charconvert'"); | 2762 errmsg = _("can't read output of 'charconvert'"); |
2765 } | 2763 } |
2766 | 2764 |
2767 if (errmsg != NULL) | 2765 if (errmsg != NULL) |
2768 { | 2766 { |
2769 /* Don't use emsg(), it breaks mappings, the retry with | 2767 // Don't use emsg(), it breaks mappings, the retry with |
2770 * another type of conversion might still work. */ | 2768 // another type of conversion might still work. |
2771 msg(errmsg); | 2769 msg(errmsg); |
2772 if (tmpname != NULL) | 2770 if (tmpname != NULL) |
2773 { | 2771 { |
2774 mch_remove(tmpname); /* delete converted file */ | 2772 mch_remove(tmpname); // delete converted file |
2775 VIM_CLEAR(tmpname); | 2773 VIM_CLEAR(tmpname); |
2776 } | 2774 } |
2777 } | 2775 } |
2778 | 2776 |
2779 /* If the input file is closed, open it (caller should check for error). */ | 2777 // If the input file is closed, open it (caller should check for error). |
2780 if (*fdp < 0) | 2778 if (*fdp < 0) |
2781 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | 2779 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); |
2782 | 2780 |
2783 return tmpname; | 2781 return tmpname; |
2784 } | 2782 } |
2791 * *filesizep are updated. | 2789 * *filesizep are updated. |
2792 * Return the (new) encryption key, NULL for no encryption. | 2790 * Return the (new) encryption key, NULL for no encryption. |
2793 */ | 2791 */ |
2794 static char_u * | 2792 static char_u * |
2795 check_for_cryptkey( | 2793 check_for_cryptkey( |
2796 char_u *cryptkey, /* previous encryption key or NULL */ | 2794 char_u *cryptkey, // previous encryption key or NULL |
2797 char_u *ptr, /* pointer to read bytes */ | 2795 char_u *ptr, // pointer to read bytes |
2798 long *sizep, /* length of read bytes */ | 2796 long *sizep, // length of read bytes |
2799 off_T *filesizep, /* nr of bytes used from file */ | 2797 off_T *filesizep, // nr of bytes used from file |
2800 int newfile, /* editing a new buffer */ | 2798 int newfile, // editing a new buffer |
2801 char_u *fname, /* file name to display */ | 2799 char_u *fname, // file name to display |
2802 int *did_ask) /* flag: whether already asked for key */ | 2800 int *did_ask) // flag: whether already asked for key |
2803 { | 2801 { |
2804 int method = crypt_method_nr_from_magic((char *)ptr, *sizep); | 2802 int method = crypt_method_nr_from_magic((char *)ptr, *sizep); |
2805 int b_p_ro = curbuf->b_p_ro; | 2803 int b_p_ro = curbuf->b_p_ro; |
2806 | 2804 |
2807 if (method >= 0) | 2805 if (method >= 0) |
2808 { | 2806 { |
2809 /* Mark the buffer as read-only until the decryption has taken place. | 2807 // Mark the buffer as read-only until the decryption has taken place. |
2810 * Avoids accidentally overwriting the file with garbage. */ | 2808 // Avoids accidentally overwriting the file with garbage. |
2811 curbuf->b_p_ro = TRUE; | 2809 curbuf->b_p_ro = TRUE; |
2812 | 2810 |
2813 /* Set the cryptmethod local to the buffer. */ | 2811 // Set the cryptmethod local to the buffer. |
2814 crypt_set_cm_option(curbuf, method); | 2812 crypt_set_cm_option(curbuf, method); |
2815 if (cryptkey == NULL && !*did_ask) | 2813 if (cryptkey == NULL && !*did_ask) |
2816 { | 2814 { |
2817 if (*curbuf->b_p_key) | 2815 if (*curbuf->b_p_key) |
2818 cryptkey = curbuf->b_p_key; | 2816 cryptkey = curbuf->b_p_key; |
2819 else | 2817 else |
2820 { | 2818 { |
2821 /* When newfile is TRUE, store the typed key in the 'key' | 2819 // When newfile is TRUE, store the typed key in the 'key' |
2822 * option and don't free it. bf needs hash of the key saved. | 2820 // option and don't free it. bf needs hash of the key saved. |
2823 * Don't ask for the key again when first time Enter was hit. | 2821 // Don't ask for the key again when first time Enter was hit. |
2824 * Happens when retrying to detect encoding. */ | 2822 // Happens when retrying to detect encoding. |
2825 smsg(_(need_key_msg), fname); | 2823 smsg(_(need_key_msg), fname); |
2826 msg_scroll = TRUE; | 2824 msg_scroll = TRUE; |
2827 crypt_check_method(method); | 2825 crypt_check_method(method); |
2828 cryptkey = crypt_get_key(newfile, FALSE); | 2826 cryptkey = crypt_get_key(newfile, FALSE); |
2829 *did_ask = TRUE; | 2827 *did_ask = TRUE; |
2830 | 2828 |
2831 /* check if empty key entered */ | 2829 // check if empty key entered |
2832 if (cryptkey != NULL && *cryptkey == NUL) | 2830 if (cryptkey != NULL && *cryptkey == NUL) |
2833 { | 2831 { |
2834 if (cryptkey != curbuf->b_p_key) | 2832 if (cryptkey != curbuf->b_p_key) |
2835 vim_free(cryptkey); | 2833 vim_free(cryptkey); |
2836 cryptkey = NULL; | 2834 cryptkey = NULL; |
2844 | 2842 |
2845 curbuf->b_cryptstate = crypt_create_from_header( | 2843 curbuf->b_cryptstate = crypt_create_from_header( |
2846 method, cryptkey, ptr); | 2844 method, cryptkey, ptr); |
2847 crypt_set_cm_option(curbuf, method); | 2845 crypt_set_cm_option(curbuf, method); |
2848 | 2846 |
2849 /* Remove cryptmethod specific header from the text. */ | 2847 // Remove cryptmethod specific header from the text. |
2850 header_len = crypt_get_header_len(method); | 2848 header_len = crypt_get_header_len(method); |
2851 if (*sizep <= header_len) | 2849 if (*sizep <= header_len) |
2852 /* invalid header, buffer can't be encrypted */ | 2850 // invalid header, buffer can't be encrypted |
2853 return NULL; | 2851 return NULL; |
2854 *filesizep += header_len; | 2852 *filesizep += header_len; |
2855 *sizep -= header_len; | 2853 *sizep -= header_len; |
2856 mch_memmove(ptr, ptr + header_len, (size_t)*sizep); | 2854 mch_memmove(ptr, ptr + header_len, (size_t)*sizep); |
2857 | 2855 |
2858 /* Restore the read-only flag. */ | 2856 // Restore the read-only flag. |
2859 curbuf->b_p_ro = b_p_ro; | 2857 curbuf->b_p_ro = b_p_ro; |
2860 } | 2858 } |
2861 } | 2859 } |
2862 /* When starting to edit a new file which does not have encryption, clear | 2860 // When starting to edit a new file which does not have encryption, clear |
2863 * the 'key' option, except when starting up (called with -x argument) */ | 2861 // the 'key' option, except when starting up (called with -x argument) |
2864 else if (newfile && *curbuf->b_p_key != NUL && !starting) | 2862 else if (newfile && *curbuf->b_p_key != NUL && !starting) |
2865 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL); | 2863 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL); |
2866 | 2864 |
2867 return cryptkey; | 2865 return cryptkey; |
2868 } | 2866 } |
2869 #endif /* FEAT_CRYPT */ | 2867 #endif // FEAT_CRYPT |
2870 | 2868 |
2871 #if defined(VMS) && !defined(MIN) | 2869 #if defined(VMS) && !defined(MIN) |
2872 /* Older DECC compiler for VAX doesn't define MIN() */ | 2870 // Older DECC compiler for VAX doesn't define MIN() |
2873 # define MIN(a, b) ((a) < (b) ? (a) : (b)) | 2871 # define MIN(a, b) ((a) < (b) ? (a) : (b)) |
2874 #endif | 2872 #endif |
2875 | 2873 |
2876 /* | 2874 /* |
2877 * Return TRUE if a file appears to be read-only from the file permissions. | 2875 * Return TRUE if a file appears to be read-only from the file permissions. |
2878 */ | 2876 */ |
2879 int | 2877 int |
2880 check_file_readonly( | 2878 check_file_readonly( |
2881 char_u *fname, /* full path to file */ | 2879 char_u *fname, // full path to file |
2882 int perm UNUSED) /* known permissions on file */ | 2880 int perm UNUSED) // known permissions on file |
2883 { | 2881 { |
2884 #ifndef USE_MCH_ACCESS | 2882 #ifndef USE_MCH_ACCESS |
2885 int fd = 0; | 2883 int fd = 0; |
2886 #endif | 2884 #endif |
2887 | 2885 |
2924 int | 2922 int |
2925 set_rw_fname(char_u *fname, char_u *sfname) | 2923 set_rw_fname(char_u *fname, char_u *sfname) |
2926 { | 2924 { |
2927 buf_T *buf = curbuf; | 2925 buf_T *buf = curbuf; |
2928 | 2926 |
2929 /* It's like the unnamed buffer is deleted.... */ | 2927 // It's like the unnamed buffer is deleted.... |
2930 if (curbuf->b_p_bl) | 2928 if (curbuf->b_p_bl) |
2931 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); | 2929 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); |
2932 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf); | 2930 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf); |
2933 #ifdef FEAT_EVAL | 2931 #ifdef FEAT_EVAL |
2934 if (aborting()) /* autocmds may abort script processing */ | 2932 if (aborting()) // autocmds may abort script processing |
2935 return FAIL; | 2933 return FAIL; |
2936 #endif | 2934 #endif |
2937 if (curbuf != buf) | 2935 if (curbuf != buf) |
2938 { | 2936 { |
2939 /* We are in another buffer now, don't do the renaming. */ | 2937 // We are in another buffer now, don't do the renaming. |
2940 emsg(_(e_auchangedbuf)); | 2938 emsg(_(e_auchangedbuf)); |
2941 return FAIL; | 2939 return FAIL; |
2942 } | 2940 } |
2943 | 2941 |
2944 if (setfname(curbuf, fname, sfname, FALSE) == OK) | 2942 if (setfname(curbuf, fname, sfname, FALSE) == OK) |
2945 curbuf->b_flags |= BF_NOTEDITED; | 2943 curbuf->b_flags |= BF_NOTEDITED; |
2946 | 2944 |
2947 /* ....and a new named one is created */ | 2945 // ....and a new named one is created |
2948 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf); | 2946 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf); |
2949 if (curbuf->b_p_bl) | 2947 if (curbuf->b_p_bl) |
2950 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf); | 2948 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf); |
2951 #ifdef FEAT_EVAL | 2949 #ifdef FEAT_EVAL |
2952 if (aborting()) /* autocmds may abort script processing */ | 2950 if (aborting()) // autocmds may abort script processing |
2953 return FAIL; | 2951 return FAIL; |
2954 #endif | 2952 #endif |
2955 | 2953 |
2956 /* Do filetype detection now if 'filetype' is empty. */ | 2954 // Do filetype detection now if 'filetype' is empty. |
2957 if (*curbuf->b_p_ft == NUL) | 2955 if (*curbuf->b_p_ft == NUL) |
2958 { | 2956 { |
2959 if (au_has_group((char_u *)"filetypedetect")) | 2957 if (au_has_group((char_u *)"filetypedetect")) |
2960 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL); | 2958 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL); |
2961 do_modelines(0); | 2959 do_modelines(0); |
3045 | 3043 |
3046 int | 3044 int |
3047 time_differs(long t1, long t2) | 3045 time_differs(long t1, long t2) |
3048 { | 3046 { |
3049 #if defined(__linux__) || defined(MSWIN) | 3047 #if defined(__linux__) || defined(MSWIN) |
3050 /* On a FAT filesystem, esp. under Linux, there are only 5 bits to store | 3048 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store |
3051 * the seconds. Since the roundoff is done when flushing the inode, the | 3049 // the seconds. Since the roundoff is done when flushing the inode, the |
3052 * time may change unexpectedly by one second!!! */ | 3050 // time may change unexpectedly by one second!!! |
3053 return (t1 - t2 > 1 || t2 - t1 > 1); | 3051 return (t1 - t2 > 1 || t2 - t1 > 1); |
3054 #else | 3052 #else |
3055 return (t1 != t2); | 3053 return (t1 != t2); |
3056 #endif | 3054 #endif |
3057 } | 3055 } |
3072 same_encoding = TRUE; | 3070 same_encoding = TRUE; |
3073 fenc_flags = 0; | 3071 fenc_flags = 0; |
3074 } | 3072 } |
3075 else | 3073 else |
3076 { | 3074 { |
3077 /* Ignore difference between "ansi" and "latin1", "ucs-4" and | 3075 // Ignore difference between "ansi" and "latin1", "ucs-4" and |
3078 * "ucs-4be", etc. */ | 3076 // "ucs-4be", etc. |
3079 enc_flags = get_fio_flags(p_enc); | 3077 enc_flags = get_fio_flags(p_enc); |
3080 fenc_flags = get_fio_flags(fenc); | 3078 fenc_flags = get_fio_flags(fenc); |
3081 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags); | 3079 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags); |
3082 } | 3080 } |
3083 if (same_encoding) | 3081 if (same_encoding) |
3084 { | 3082 { |
3085 /* Specified encoding matches with 'encoding'. This requires | 3083 // Specified encoding matches with 'encoding'. This requires |
3086 * conversion when 'encoding' is Unicode but not UTF-8. */ | 3084 // conversion when 'encoding' is Unicode but not UTF-8. |
3087 return enc_unicode != 0; | 3085 return enc_unicode != 0; |
3088 } | 3086 } |
3089 | 3087 |
3090 /* Encodings differ. However, conversion is not needed when 'enc' is any | 3088 // Encodings differ. However, conversion is not needed when 'enc' is any |
3091 * Unicode encoding and the file is UTF-8. */ | 3089 // Unicode encoding and the file is UTF-8. |
3092 return !(enc_utf8 && fenc_flags == FIO_UTF8); | 3090 return !(enc_utf8 && fenc_flags == FIO_UTF8); |
3093 } | 3091 } |
3094 | 3092 |
3095 /* | 3093 /* |
3096 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the | 3094 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the |
3128 } | 3126 } |
3129 return FIO_UTF8; | 3127 return FIO_UTF8; |
3130 } | 3128 } |
3131 if (prop & ENC_LATIN1) | 3129 if (prop & ENC_LATIN1) |
3132 return FIO_LATIN1; | 3130 return FIO_LATIN1; |
3133 /* must be ENC_DBCS, requires iconv() */ | 3131 // must be ENC_DBCS, requires iconv() |
3134 return 0; | 3132 return 0; |
3135 } | 3133 } |
3136 | 3134 |
3137 #if defined(MSWIN) || defined(PROTO) | 3135 #if defined(MSWIN) || defined(PROTO) |
3138 /* | 3136 /* |
3143 int | 3141 int |
3144 get_win_fio_flags(char_u *ptr) | 3142 get_win_fio_flags(char_u *ptr) |
3145 { | 3143 { |
3146 int cp; | 3144 int cp; |
3147 | 3145 |
3148 /* Cannot do this when 'encoding' is not utf-8 and not a codepage. */ | 3146 // Cannot do this when 'encoding' is not utf-8 and not a codepage. |
3149 if (!enc_utf8 && enc_codepage <= 0) | 3147 if (!enc_utf8 && enc_codepage <= 0) |
3150 return 0; | 3148 return 0; |
3151 | 3149 |
3152 cp = encname2codepage(ptr); | 3150 cp = encname2codepage(ptr); |
3153 if (cp == 0) | 3151 if (cp == 0) |
3154 { | 3152 { |
3155 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | 3153 # ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8 |
3156 if (STRCMP(ptr, "utf-8") == 0) | 3154 if (STRCMP(ptr, "utf-8") == 0) |
3157 cp = CP_UTF8; | 3155 cp = CP_UTF8; |
3158 else | 3156 else |
3159 # endif | 3157 # endif |
3160 return 0; | 3158 return 0; |
3195 int len = 2; | 3193 int len = 2; |
3196 | 3194 |
3197 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf | 3195 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf |
3198 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0)) | 3196 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0)) |
3199 { | 3197 { |
3200 name = "utf-8"; /* EF BB BF */ | 3198 name = "utf-8"; // EF BB BF |
3201 len = 3; | 3199 len = 3; |
3202 } | 3200 } |
3203 else if (p[0] == 0xff && p[1] == 0xfe) | 3201 else if (p[0] == 0xff && p[1] == 0xfe) |
3204 { | 3202 { |
3205 if (size >= 4 && p[2] == 0 && p[3] == 0 | 3203 if (size >= 4 && p[2] == 0 && p[3] == 0 |
3206 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L))) | 3204 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L))) |
3207 { | 3205 { |
3208 name = "ucs-4le"; /* FF FE 00 00 */ | 3206 name = "ucs-4le"; // FF FE 00 00 |
3209 len = 4; | 3207 len = 4; |
3210 } | 3208 } |
3211 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L)) | 3209 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L)) |
3212 name = "ucs-2le"; /* FF FE */ | 3210 name = "ucs-2le"; // FF FE |
3213 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L)) | 3211 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L)) |
3214 /* utf-16le is preferred, it also works for ucs-2le text */ | 3212 // utf-16le is preferred, it also works for ucs-2le text |
3215 name = "utf-16le"; /* FF FE */ | 3213 name = "utf-16le"; // FF FE |
3216 } | 3214 } |
3217 else if (p[0] == 0xfe && p[1] == 0xff | 3215 else if (p[0] == 0xfe && p[1] == 0xff |
3218 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16)) | 3216 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16)) |
3219 { | 3217 { |
3220 /* Default to utf-16, it works also for ucs-2 text. */ | 3218 // Default to utf-16, it works also for ucs-2 text. |
3221 if (flags == FIO_UCS2) | 3219 if (flags == FIO_UCS2) |
3222 name = "ucs-2"; /* FE FF */ | 3220 name = "ucs-2"; // FE FF |
3223 else | 3221 else |
3224 name = "utf-16"; /* FE FF */ | 3222 name = "utf-16"; // FE FF |
3225 } | 3223 } |
3226 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe | 3224 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe |
3227 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4)) | 3225 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4)) |
3228 { | 3226 { |
3229 name = "ucs-4"; /* 00 00 FE FF */ | 3227 name = "ucs-4"; // 00 00 FE FF |
3230 len = 4; | 3228 len = 4; |
3231 } | 3229 } |
3232 | 3230 |
3233 *lenp = len; | 3231 *lenp = len; |
3234 return (char_u *)name; | 3232 return (char_u *)name; |
3285 if (!((len > 2) && (*(p - 2) == ':'))) | 3283 if (!((len > 2) && (*(p - 2) == ':'))) |
3286 #endif | 3284 #endif |
3287 { | 3285 { |
3288 if (vim_ispathsep(*p)) | 3286 if (vim_ispathsep(*p)) |
3289 ++p; | 3287 ++p; |
3290 #ifndef VMS /* the path separator is always part of the path */ | 3288 #ifndef VMS // the path separator is always part of the path |
3291 else | 3289 else |
3292 p = NULL; | 3290 p = NULL; |
3293 #endif | 3291 #endif |
3294 } | 3292 } |
3295 } | 3293 } |
3358 mch_dirname(dirname, MAXPATHL); | 3356 mch_dirname(dirname, MAXPATHL); |
3359 FOR_ALL_BUFFERS(buf) | 3357 FOR_ALL_BUFFERS(buf) |
3360 { | 3358 { |
3361 shorten_buf_fname(buf, dirname, force); | 3359 shorten_buf_fname(buf, dirname, force); |
3362 | 3360 |
3363 /* Always make the swap file name a full path, a "nofile" buffer may | 3361 // Always make the swap file name a full path, a "nofile" buffer may |
3364 * also have a swap file. */ | 3362 // also have a swap file. |
3365 mf_fullname(buf->b_ml.ml_mfp); | 3363 mf_fullname(buf->b_ml.ml_mfp); |
3366 } | 3364 } |
3367 status_redraw_all(); | 3365 status_redraw_all(); |
3368 redraw_tabline = TRUE; | 3366 redraw_tabline = TRUE; |
3369 #if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX) | 3367 #if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX) |
3390 mch_dirname(dirname, sizeof(dirname)); | 3388 mch_dirname(dirname, sizeof(dirname)); |
3391 for (i = 0; i < count; ++i) | 3389 for (i = 0; i < count; ++i) |
3392 { | 3390 { |
3393 if ((p = shorten_fname(fnames[i], dirname)) != NULL) | 3391 if ((p = shorten_fname(fnames[i], dirname)) != NULL) |
3394 { | 3392 { |
3395 /* shorten_fname() returns pointer in given "fnames[i]". If free | 3393 // shorten_fname() returns pointer in given "fnames[i]". If free |
3396 * "fnames[i]" first, "p" becomes invalid. So we need to copy | 3394 // "fnames[i]" first, "p" becomes invalid. So we need to copy |
3397 * "p" first then free fnames[i]. */ | 3395 // "p" first then free fnames[i]. |
3398 p = vim_strsave(p); | 3396 p = vim_strsave(p); |
3399 vim_free(fnames[i]); | 3397 vim_free(fnames[i]); |
3400 fnames[i] = p; | 3398 fnames[i] = p; |
3401 } | 3399 } |
3402 } | 3400 } |
3416 */ | 3414 */ |
3417 char_u * | 3415 char_u * |
3418 modname( | 3416 modname( |
3419 char_u *fname, | 3417 char_u *fname, |
3420 char_u *ext, | 3418 char_u *ext, |
3421 int prepend_dot) /* may prepend a '.' to file name */ | 3419 int prepend_dot) // may prepend a '.' to file name |
3422 { | 3420 { |
3423 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname), | 3421 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname), |
3424 fname, ext, prepend_dot); | 3422 fname, ext, prepend_dot); |
3425 } | 3423 } |
3426 | 3424 |
3427 char_u * | 3425 char_u * |
3428 buf_modname( | 3426 buf_modname( |
3429 int shortname, /* use 8.3 file name */ | 3427 int shortname, // use 8.3 file name |
3430 char_u *fname, | 3428 char_u *fname, |
3431 char_u *ext, | 3429 char_u *ext, |
3432 int prepend_dot) /* may prepend a '.' to file name */ | 3430 int prepend_dot) // may prepend a '.' to file name |
3433 { | 3431 { |
3434 char_u *retval; | 3432 char_u *retval; |
3435 char_u *s; | 3433 char_u *s; |
3436 char_u *e; | 3434 char_u *e; |
3437 char_u *ptr; | 3435 char_u *ptr; |
3457 if (!after_pathsep(retval, retval + fnamelen)) | 3455 if (!after_pathsep(retval, retval + fnamelen)) |
3458 { | 3456 { |
3459 retval[fnamelen++] = PATHSEP; | 3457 retval[fnamelen++] = PATHSEP; |
3460 retval[fnamelen] = NUL; | 3458 retval[fnamelen] = NUL; |
3461 } | 3459 } |
3462 prepend_dot = FALSE; /* nothing to prepend a dot to */ | 3460 prepend_dot = FALSE; // nothing to prepend a dot to |
3463 } | 3461 } |
3464 else | 3462 else |
3465 { | 3463 { |
3466 fnamelen = (int)STRLEN(fname); | 3464 fnamelen = (int)STRLEN(fname); |
3467 retval = alloc(fnamelen + extlen + 3); | 3465 retval = alloc(fnamelen + extlen + 3); |
3468 if (retval == NULL) | 3466 if (retval == NULL) |
3469 return NULL; | 3467 return NULL; |
3470 STRCPY(retval, fname); | 3468 STRCPY(retval, fname); |
3471 #ifdef VMS | 3469 #ifdef VMS |
3472 vms_remove_version(retval); /* we do not need versions here */ | 3470 vms_remove_version(retval); // we do not need versions here |
3473 #endif | 3471 #endif |
3474 } | 3472 } |
3475 | 3473 |
3476 /* | 3474 /* |
3477 * search backwards until we hit a '/', '\' or ':' replacing all '.' | 3475 * search backwards until we hit a '/', '\' or ':' replacing all '.' |
3480 * MSDOS and 26 characters for AMIGA, a lot more for UNIX. | 3478 * MSDOS and 26 characters for AMIGA, a lot more for UNIX. |
3481 */ | 3479 */ |
3482 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr)) | 3480 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr)) |
3483 { | 3481 { |
3484 if (*ext == '.' && shortname) | 3482 if (*ext == '.' && shortname) |
3485 if (*ptr == '.') /* replace '.' by '_' */ | 3483 if (*ptr == '.') // replace '.' by '_' |
3486 *ptr = '_'; | 3484 *ptr = '_'; |
3487 if (vim_ispathsep(*ptr)) | 3485 if (vim_ispathsep(*ptr)) |
3488 { | 3486 { |
3489 ++ptr; | 3487 ++ptr; |
3490 break; | 3488 break; |
3491 } | 3489 } |
3492 } | 3490 } |
3493 | 3491 |
3494 /* the file name has at most BASENAMELEN characters. */ | 3492 // the file name has at most BASENAMELEN characters. |
3495 if (STRLEN(ptr) > (unsigned)BASENAMELEN) | 3493 if (STRLEN(ptr) > (unsigned)BASENAMELEN) |
3496 ptr[BASENAMELEN] = '\0'; | 3494 ptr[BASENAMELEN] = '\0'; |
3497 | 3495 |
3498 s = ptr + STRLEN(ptr); | 3496 s = ptr + STRLEN(ptr); |
3499 | 3497 |
3567 * Check that, after appending the extension, the file name is really | 3565 * Check that, after appending the extension, the file name is really |
3568 * different. | 3566 * different. |
3569 */ | 3567 */ |
3570 if (fname != NULL && STRCMP(fname, retval) == 0) | 3568 if (fname != NULL && STRCMP(fname, retval) == 0) |
3571 { | 3569 { |
3572 /* we search for a character that can be replaced by '_' */ | 3570 // we search for a character that can be replaced by '_' |
3573 while (--s >= ptr) | 3571 while (--s >= ptr) |
3574 { | 3572 { |
3575 if (*s != '_') | 3573 if (*s != '_') |
3576 { | 3574 { |
3577 *s = '_'; | 3575 *s = '_'; |
3578 break; | 3576 break; |
3579 } | 3577 } |
3580 } | 3578 } |
3581 if (s < ptr) /* fname was "________.<ext>", how tricky! */ | 3579 if (s < ptr) // fname was "________.<ext>", how tricky! |
3582 *ptr = 'v'; | 3580 *ptr = 'v'; |
3583 } | 3581 } |
3584 return retval; | 3582 return retval; |
3585 } | 3583 } |
3586 | 3584 |
3598 | 3596 |
3599 buf[size - 2] = NUL; | 3597 buf[size - 2] = NUL; |
3600 eof = fgets((char *)buf, size, fp); | 3598 eof = fgets((char *)buf, size, fp); |
3601 if (buf[size - 2] != NUL && buf[size - 2] != '\n') | 3599 if (buf[size - 2] != NUL && buf[size - 2] != '\n') |
3602 { | 3600 { |
3603 buf[size - 1] = NUL; /* Truncate the line */ | 3601 buf[size - 1] = NUL; // Truncate the line |
3604 | 3602 |
3605 /* Now throw away the rest of the line: */ | 3603 // Now throw away the rest of the line: |
3606 do | 3604 do |
3607 { | 3605 { |
3608 tbuf[FGETS_SIZE - 2] = NUL; | 3606 tbuf[FGETS_SIZE - 2] = NUL; |
3609 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp); | 3607 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp); |
3610 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n'); | 3608 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n'); |
3629 BPTR flock; | 3627 BPTR flock; |
3630 #endif | 3628 #endif |
3631 stat_T st; | 3629 stat_T st; |
3632 long perm; | 3630 long perm; |
3633 #ifdef HAVE_ACL | 3631 #ifdef HAVE_ACL |
3634 vim_acl_T acl; /* ACL from original file */ | 3632 vim_acl_T acl; // ACL from original file |
3635 #endif | 3633 #endif |
3636 int use_tmp_file = FALSE; | 3634 int use_tmp_file = FALSE; |
3637 | 3635 |
3638 /* | 3636 /* |
3639 * When the names are identical, there is nothing to do. When they refer | 3637 * When the names are identical, there is nothing to do. When they refer |
3656 | 3654 |
3657 #ifdef UNIX | 3655 #ifdef UNIX |
3658 { | 3656 { |
3659 stat_T st_to; | 3657 stat_T st_to; |
3660 | 3658 |
3661 /* It's possible for the source and destination to be the same file. | 3659 // It's possible for the source and destination to be the same file. |
3662 * This happens when "from" and "to" differ in case and are on a FAT32 | 3660 // This happens when "from" and "to" differ in case and are on a FAT32 |
3663 * filesystem. In that case go through a temp file name. */ | 3661 // filesystem. In that case go through a temp file name. |
3664 if (mch_stat((char *)to, &st_to) >= 0 | 3662 if (mch_stat((char *)to, &st_to) >= 0 |
3665 && st.st_dev == st_to.st_dev | 3663 && st.st_dev == st_to.st_dev |
3666 && st.st_ino == st_to.st_ino) | 3664 && st.st_ino == st_to.st_ino) |
3667 use_tmp_file = TRUE; | 3665 use_tmp_file = TRUE; |
3668 } | 3666 } |
3669 #endif | 3667 #endif |
3670 #ifdef MSWIN | 3668 #ifdef MSWIN |
3671 { | 3669 { |
3672 BY_HANDLE_FILE_INFORMATION info1, info2; | 3670 BY_HANDLE_FILE_INFORMATION info1, info2; |
3673 | 3671 |
3674 /* It's possible for the source and destination to be the same file. | 3672 // It's possible for the source and destination to be the same file. |
3675 * In that case go through a temp file name. This makes rename("foo", | 3673 // In that case go through a temp file name. This makes rename("foo", |
3676 * "./foo") a no-op (in a complicated way). */ | 3674 // "./foo") a no-op (in a complicated way). |
3677 if (win32_fileinfo(from, &info1) == FILEINFO_OK | 3675 if (win32_fileinfo(from, &info1) == FILEINFO_OK |
3678 && win32_fileinfo(to, &info2) == FILEINFO_OK | 3676 && win32_fileinfo(to, &info2) == FILEINFO_OK |
3679 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber | 3677 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber |
3680 && info1.nFileIndexHigh == info2.nFileIndexHigh | 3678 && info1.nFileIndexHigh == info2.nFileIndexHigh |
3681 && info1.nFileIndexLow == info2.nFileIndexLow) | 3679 && info1.nFileIndexLow == info2.nFileIndexLow) |
3701 { | 3699 { |
3702 if (mch_rename((char *)from, tempname) == 0) | 3700 if (mch_rename((char *)from, tempname) == 0) |
3703 { | 3701 { |
3704 if (mch_rename(tempname, (char *)to) == 0) | 3702 if (mch_rename(tempname, (char *)to) == 0) |
3705 return 0; | 3703 return 0; |
3706 /* Strange, the second step failed. Try moving the | 3704 // Strange, the second step failed. Try moving the |
3707 * file back and return failure. */ | 3705 // file back and return failure. |
3708 mch_rename(tempname, (char *)from); | 3706 mch_rename(tempname, (char *)from); |
3709 return -1; | 3707 return -1; |
3710 } | 3708 } |
3711 /* If it fails for one temp name it will most likely fail | 3709 // If it fails for one temp name it will most likely fail |
3712 * for any temp name, give up. */ | 3710 // for any temp name, give up. |
3713 return -1; | 3711 return -1; |
3714 } | 3712 } |
3715 } | 3713 } |
3716 return -1; | 3714 return -1; |
3717 } | 3715 } |
3753 /* | 3751 /* |
3754 * Rename() failed, try copying the file. | 3752 * Rename() failed, try copying the file. |
3755 */ | 3753 */ |
3756 perm = mch_getperm(from); | 3754 perm = mch_getperm(from); |
3757 #ifdef HAVE_ACL | 3755 #ifdef HAVE_ACL |
3758 /* For systems that support ACL: get the ACL from the original file. */ | 3756 // For systems that support ACL: get the ACL from the original file. |
3759 acl = mch_get_acl(from); | 3757 acl = mch_get_acl(from); |
3760 #endif | 3758 #endif |
3761 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0); | 3759 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0); |
3762 if (fd_in == -1) | 3760 if (fd_in == -1) |
3763 { | 3761 { |
3765 mch_free_acl(acl); | 3763 mch_free_acl(acl); |
3766 #endif | 3764 #endif |
3767 return -1; | 3765 return -1; |
3768 } | 3766 } |
3769 | 3767 |
3770 /* Create the new file with same permissions as the original. */ | 3768 // Create the new file with same permissions as the original. |
3771 fd_out = mch_open((char *)to, | 3769 fd_out = mch_open((char *)to, |
3772 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm); | 3770 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm); |
3773 if (fd_out == -1) | 3771 if (fd_out == -1) |
3774 { | 3772 { |
3775 close(fd_in); | 3773 close(fd_in); |
3804 if (n < 0) | 3802 if (n < 0) |
3805 { | 3803 { |
3806 errmsg = _("E210: Error reading \"%s\""); | 3804 errmsg = _("E210: Error reading \"%s\""); |
3807 to = from; | 3805 to = from; |
3808 } | 3806 } |
3809 #ifndef UNIX /* for Unix mch_open() already set the permission */ | 3807 #ifndef UNIX // for Unix mch_open() already set the permission |
3810 mch_setperm(to, perm); | 3808 mch_setperm(to, perm); |
3811 #endif | 3809 #endif |
3812 #ifdef HAVE_ACL | 3810 #ifdef HAVE_ACL |
3813 mch_set_acl(to, acl); | 3811 mch_set_acl(to, acl); |
3814 mch_free_acl(acl); | 3812 mch_free_acl(acl); |
3835 * Returns TRUE if some message was written (screen should be redrawn and | 3833 * Returns TRUE if some message was written (screen should be redrawn and |
3836 * cursor positioned). | 3834 * cursor positioned). |
3837 */ | 3835 */ |
3838 int | 3836 int |
3839 check_timestamps( | 3837 check_timestamps( |
3840 int focus) /* called for GUI focus event */ | 3838 int focus) // called for GUI focus event |
3841 { | 3839 { |
3842 buf_T *buf; | 3840 buf_T *buf; |
3843 int didit = 0; | 3841 int didit = 0; |
3844 int n; | 3842 int n; |
3845 | 3843 |
3846 /* Don't check timestamps while system() or another low-level function may | 3844 // Don't check timestamps while system() or another low-level function may |
3847 * cause us to lose and gain focus. */ | 3845 // cause us to lose and gain focus. |
3848 if (no_check_timestamps > 0) | 3846 if (no_check_timestamps > 0) |
3849 return FALSE; | 3847 return FALSE; |
3850 | 3848 |
3851 /* Avoid doing a check twice. The OK/Reload dialog can cause a focus | 3849 // Avoid doing a check twice. The OK/Reload dialog can cause a focus |
3852 * event and we would keep on checking if the file is steadily growing. | 3850 // event and we would keep on checking if the file is steadily growing. |
3853 * Do check again after typing something. */ | 3851 // Do check again after typing something. |
3854 if (focus && did_check_timestamps) | 3852 if (focus && did_check_timestamps) |
3855 { | 3853 { |
3856 need_check_timestamps = TRUE; | 3854 need_check_timestamps = TRUE; |
3857 return FALSE; | 3855 return FALSE; |
3858 } | 3856 } |
3859 | 3857 |
3860 if (!stuff_empty() || global_busy || !typebuf_typed() | 3858 if (!stuff_empty() || global_busy || !typebuf_typed() |
3861 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0) | 3859 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0) |
3862 need_check_timestamps = TRUE; /* check later */ | 3860 need_check_timestamps = TRUE; // check later |
3863 else | 3861 else |
3864 { | 3862 { |
3865 ++no_wait_return; | 3863 ++no_wait_return; |
3866 did_check_timestamps = TRUE; | 3864 did_check_timestamps = TRUE; |
3867 already_warned = FALSE; | 3865 already_warned = FALSE; |
3868 FOR_ALL_BUFFERS(buf) | 3866 FOR_ALL_BUFFERS(buf) |
3869 { | 3867 { |
3870 /* Only check buffers in a window. */ | 3868 // Only check buffers in a window. |
3871 if (buf->b_nwindows > 0) | 3869 if (buf->b_nwindows > 0) |
3872 { | 3870 { |
3873 bufref_T bufref; | 3871 bufref_T bufref; |
3874 | 3872 |
3875 set_bufref(&bufref, buf); | 3873 set_bufref(&bufref, buf); |
3876 n = buf_check_timestamp(buf, focus); | 3874 n = buf_check_timestamp(buf, focus); |
3877 if (didit < n) | 3875 if (didit < n) |
3878 didit = n; | 3876 didit = n; |
3879 if (n > 0 && !bufref_valid(&bufref)) | 3877 if (n > 0 && !bufref_valid(&bufref)) |
3880 { | 3878 { |
3881 /* Autocommands have removed the buffer, start at the | 3879 // Autocommands have removed the buffer, start at the |
3882 * first one again. */ | 3880 // first one again. |
3883 buf = firstbuf; | 3881 buf = firstbuf; |
3884 continue; | 3882 continue; |
3885 } | 3883 } |
3886 } | 3884 } |
3887 } | 3885 } |
3888 --no_wait_return; | 3886 --no_wait_return; |
3889 need_check_timestamps = FALSE; | 3887 need_check_timestamps = FALSE; |
3890 if (need_wait_return && didit == 2) | 3888 if (need_wait_return && didit == 2) |
3891 { | 3889 { |
3892 /* make sure msg isn't overwritten */ | 3890 // make sure msg isn't overwritten |
3893 msg_puts("\n"); | 3891 msg_puts("\n"); |
3894 out_flush(); | 3892 out_flush(); |
3895 } | 3893 } |
3896 } | 3894 } |
3897 return didit; | 3895 return didit; |
3908 buf_T *tbuf = curbuf; | 3906 buf_T *tbuf = curbuf; |
3909 int retval = OK; | 3907 int retval = OK; |
3910 linenr_T lnum; | 3908 linenr_T lnum; |
3911 char_u *p; | 3909 char_u *p; |
3912 | 3910 |
3913 /* Copy the lines in "frombuf" to "tobuf". */ | 3911 // Copy the lines in "frombuf" to "tobuf". |
3914 curbuf = tobuf; | 3912 curbuf = tobuf; |
3915 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum) | 3913 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum) |
3916 { | 3914 { |
3917 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE)); | 3915 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE)); |
3918 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL) | 3916 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL) |
3922 break; | 3920 break; |
3923 } | 3921 } |
3924 vim_free(p); | 3922 vim_free(p); |
3925 } | 3923 } |
3926 | 3924 |
3927 /* Delete all the lines in "frombuf". */ | 3925 // Delete all the lines in "frombuf". |
3928 if (retval != FAIL) | 3926 if (retval != FAIL) |
3929 { | 3927 { |
3930 curbuf = frombuf; | 3928 curbuf = frombuf; |
3931 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum) | 3929 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum) |
3932 if (ml_delete(lnum, FALSE) == FAIL) | 3930 if (ml_delete(lnum, FALSE) == FAIL) |
3933 { | 3931 { |
3934 /* Oops! We could try putting back the saved lines, but that | 3932 // Oops! We could try putting back the saved lines, but that |
3935 * might fail again... */ | 3933 // might fail again... |
3936 retval = FAIL; | 3934 retval = FAIL; |
3937 break; | 3935 break; |
3938 } | 3936 } |
3939 } | 3937 } |
3940 | 3938 |
3950 * return 0 otherwise. | 3948 * return 0 otherwise. |
3951 */ | 3949 */ |
3952 int | 3950 int |
3953 buf_check_timestamp( | 3951 buf_check_timestamp( |
3954 buf_T *buf, | 3952 buf_T *buf, |
3955 int focus UNUSED) /* called for GUI focus event */ | 3953 int focus UNUSED) // called for GUI focus event |
3956 { | 3954 { |
3957 stat_T st; | 3955 stat_T st; |
3958 int stat_res; | 3956 int stat_res; |
3959 int retval = 0; | 3957 int retval = 0; |
3960 char_u *path; | 3958 char_u *path; |
3979 #endif | 3977 #endif |
3980 bufref_T bufref; | 3978 bufref_T bufref; |
3981 | 3979 |
3982 set_bufref(&bufref, buf); | 3980 set_bufref(&bufref, buf); |
3983 | 3981 |
3984 /* If there is no file name, the buffer is not loaded, 'buftype' is | 3982 // If there is no file name, the buffer is not loaded, 'buftype' is |
3985 * set, we are in the middle of a save or being called recursively: ignore | 3983 // set, we are in the middle of a save or being called recursively: ignore |
3986 * this buffer. */ | 3984 // this buffer. |
3987 if (buf->b_ffname == NULL | 3985 if (buf->b_ffname == NULL |
3988 || buf->b_ml.ml_mfp == NULL | 3986 || buf->b_ml.ml_mfp == NULL |
3989 || !bt_normal(buf) | 3987 || !bt_normal(buf) |
3990 || buf->b_saving | 3988 || buf->b_saving |
3991 || busy | 3989 || busy |
4024 buf->b_orig_mode = 0; | 4022 buf->b_orig_mode = 0; |
4025 } | 4023 } |
4026 else | 4024 else |
4027 buf_store_time(buf, &st, buf->b_ffname); | 4025 buf_store_time(buf, &st, buf->b_ffname); |
4028 | 4026 |
4029 /* Don't do anything for a directory. Might contain the file | 4027 // Don't do anything for a directory. Might contain the file |
4030 * explorer. */ | 4028 // explorer. |
4031 if (mch_isdir(buf->b_fname)) | 4029 if (mch_isdir(buf->b_fname)) |
4032 ; | 4030 ; |
4033 | 4031 |
4034 /* | 4032 /* |
4035 * If 'autoread' is set, the buffer has no changes and the file still | 4033 * If 'autoread' is set, the buffer has no changes and the file still |
4114 { | 4112 { |
4115 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started"); | 4113 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started"); |
4116 mesg2 = _("See \":help W16\" for more info."); | 4114 mesg2 = _("See \":help W16\" for more info."); |
4117 } | 4115 } |
4118 else | 4116 else |
4119 /* Only timestamp changed, store it to avoid a warning | 4117 // Only timestamp changed, store it to avoid a warning |
4120 * in check_mtime() later. */ | 4118 // in check_mtime() later. |
4121 buf->b_mtime_read = buf->b_mtime; | 4119 buf->b_mtime_read = buf->b_mtime; |
4122 } | 4120 } |
4123 } | 4121 } |
4124 } | 4122 } |
4125 | 4123 |
4143 if (!helpmesg) | 4141 if (!helpmesg) |
4144 mesg2 = ""; | 4142 mesg2 = ""; |
4145 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2); | 4143 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2); |
4146 sprintf(tbuf, mesg, path); | 4144 sprintf(tbuf, mesg, path); |
4147 #ifdef FEAT_EVAL | 4145 #ifdef FEAT_EVAL |
4148 /* Set warningmsg here, before the unimportant and output-specific | 4146 // Set warningmsg here, before the unimportant and output-specific |
4149 * mesg2 has been appended. */ | 4147 // mesg2 has been appended. |
4150 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1); | 4148 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1); |
4151 #endif | 4149 #endif |
4152 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | 4150 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
4153 if (can_reload) | 4151 if (can_reload) |
4154 { | 4152 { |
4188 { | 4186 { |
4189 out_flush(); | 4187 out_flush(); |
4190 #ifdef FEAT_GUI | 4188 #ifdef FEAT_GUI |
4191 if (!focus) | 4189 if (!focus) |
4192 #endif | 4190 #endif |
4193 /* give the user some time to think about it */ | 4191 // give the user some time to think about it |
4194 ui_delay(1004L, TRUE); | 4192 ui_delay(1004L, TRUE); |
4195 | 4193 |
4196 /* don't redraw and erase the message */ | 4194 // don't redraw and erase the message |
4197 redraw_cmdline = FALSE; | 4195 redraw_cmdline = FALSE; |
4198 } | 4196 } |
4199 } | 4197 } |
4200 already_warned = TRUE; | 4198 already_warned = TRUE; |
4201 } | 4199 } |
4205 } | 4203 } |
4206 } | 4204 } |
4207 | 4205 |
4208 if (reload) | 4206 if (reload) |
4209 { | 4207 { |
4210 /* Reload the buffer. */ | 4208 // Reload the buffer. |
4211 buf_reload(buf, orig_mode); | 4209 buf_reload(buf, orig_mode); |
4212 #ifdef FEAT_PERSISTENT_UNDO | 4210 #ifdef FEAT_PERSISTENT_UNDO |
4213 if (buf->b_p_udf && buf->b_ffname != NULL) | 4211 if (buf->b_p_udf && buf->b_ffname != NULL) |
4214 { | 4212 { |
4215 char_u hash[UNDO_HASH_SIZE]; | 4213 char_u hash[UNDO_HASH_SIZE]; |
4216 buf_T *save_curbuf = curbuf; | 4214 buf_T *save_curbuf = curbuf; |
4217 | 4215 |
4218 /* Any existing undo file is unusable, write it now. */ | 4216 // Any existing undo file is unusable, write it now. |
4219 curbuf = buf; | 4217 curbuf = buf; |
4220 u_compute_hash(hash); | 4218 u_compute_hash(hash); |
4221 u_write_undo(NULL, FALSE, buf, hash); | 4219 u_write_undo(NULL, FALSE, buf, hash); |
4222 curbuf = save_curbuf; | 4220 curbuf = save_curbuf; |
4223 } | 4221 } |
4224 #endif | 4222 #endif |
4225 } | 4223 } |
4226 | 4224 |
4227 /* Trigger FileChangedShell when the file was changed in any way. */ | 4225 // Trigger FileChangedShell when the file was changed in any way. |
4228 if (bufref_valid(&bufref) && retval != 0) | 4226 if (bufref_valid(&bufref) && retval != 0) |
4229 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, | 4227 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, |
4230 buf->b_fname, buf->b_fname, FALSE, buf); | 4228 buf->b_fname, buf->b_fname, FALSE, buf); |
4231 #ifdef FEAT_GUI | 4229 #ifdef FEAT_GUI |
4232 /* restore this in case an autocommand has set it; it would break | 4230 // restore this in case an autocommand has set it; it would break |
4233 * 'mousefocus' */ | 4231 // 'mousefocus' |
4234 need_mouse_correct = save_mouse_correct; | 4232 need_mouse_correct = save_mouse_correct; |
4235 #endif | 4233 #endif |
4236 | 4234 |
4237 return retval; | 4235 return retval; |
4238 } | 4236 } |
4254 bufref_T bufref; | 4252 bufref_T bufref; |
4255 int saved = OK; | 4253 int saved = OK; |
4256 aco_save_T aco; | 4254 aco_save_T aco; |
4257 int flags = READ_NEW; | 4255 int flags = READ_NEW; |
4258 | 4256 |
4259 /* set curwin/curbuf for "buf" and save some things */ | 4257 // set curwin/curbuf for "buf" and save some things |
4260 aucmd_prepbuf(&aco, buf); | 4258 aucmd_prepbuf(&aco, buf); |
4261 | 4259 |
4262 /* We only want to read the text from the file, not reset the syntax | 4260 // We only want to read the text from the file, not reset the syntax |
4263 * highlighting, clear marks, diff status, etc. Force the fileformat | 4261 // highlighting, clear marks, diff status, etc. Force the fileformat |
4264 * and encoding to be the same. */ | 4262 // and encoding to be the same. |
4265 if (prep_exarg(&ea, buf) == OK) | 4263 if (prep_exarg(&ea, buf) == OK) |
4266 { | 4264 { |
4267 old_cursor = curwin->w_cursor; | 4265 old_cursor = curwin->w_cursor; |
4268 old_topline = curwin->w_topline; | 4266 old_topline = curwin->w_topline; |
4269 | 4267 |
4270 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur) | 4268 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur) |
4271 { | 4269 { |
4272 /* Save all the text, so that the reload can be undone. | 4270 // Save all the text, so that the reload can be undone. |
4273 * Sync first so that this is a separate undo-able action. */ | 4271 // Sync first so that this is a separate undo-able action. |
4274 u_sync(FALSE); | 4272 u_sync(FALSE); |
4275 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE); | 4273 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE); |
4276 flags |= READ_KEEP_UNDO; | 4274 flags |= READ_KEEP_UNDO; |
4277 } | 4275 } |
4278 | 4276 |
4285 */ | 4283 */ |
4286 if (BUFEMPTY() || saved == FAIL) | 4284 if (BUFEMPTY() || saved == FAIL) |
4287 savebuf = NULL; | 4285 savebuf = NULL; |
4288 else | 4286 else |
4289 { | 4287 { |
4290 /* Allocate a buffer without putting it in the buffer list. */ | 4288 // Allocate a buffer without putting it in the buffer list. |
4291 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); | 4289 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); |
4292 set_bufref(&bufref, savebuf); | 4290 set_bufref(&bufref, savebuf); |
4293 if (savebuf != NULL && buf == curbuf) | 4291 if (savebuf != NULL && buf == curbuf) |
4294 { | 4292 { |
4295 /* Open the memline. */ | 4293 // Open the memline. |
4296 curbuf = savebuf; | 4294 curbuf = savebuf; |
4297 curwin->w_buffer = savebuf; | 4295 curwin->w_buffer = savebuf; |
4298 saved = ml_open(curbuf); | 4296 saved = ml_open(curbuf); |
4299 curbuf = buf; | 4297 curbuf = buf; |
4300 curwin->w_buffer = buf; | 4298 curwin->w_buffer = buf; |
4308 } | 4306 } |
4309 } | 4307 } |
4310 | 4308 |
4311 if (saved == OK) | 4309 if (saved == OK) |
4312 { | 4310 { |
4313 curbuf->b_flags |= BF_CHECK_RO; /* check for RO again */ | 4311 curbuf->b_flags |= BF_CHECK_RO; // check for RO again |
4314 keep_filetype = TRUE; /* don't detect 'filetype' */ | 4312 keep_filetype = TRUE; // don't detect 'filetype' |
4315 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, | 4313 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, |
4316 (linenr_T)0, | 4314 (linenr_T)0, |
4317 (linenr_T)MAXLNUM, &ea, flags) != OK) | 4315 (linenr_T)MAXLNUM, &ea, flags) != OK) |
4318 { | 4316 { |
4319 #if defined(FEAT_EVAL) | 4317 #if defined(FEAT_EVAL) |
4320 if (!aborting()) | 4318 if (!aborting()) |
4321 #endif | 4319 #endif |
4322 semsg(_("E321: Could not reload \"%s\""), buf->b_fname); | 4320 semsg(_("E321: Could not reload \"%s\""), buf->b_fname); |
4323 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf) | 4321 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf) |
4324 { | 4322 { |
4325 /* Put the text back from the save buffer. First | 4323 // Put the text back from the save buffer. First |
4326 * delete any lines that readfile() added. */ | 4324 // delete any lines that readfile() added. |
4327 while (!BUFEMPTY()) | 4325 while (!BUFEMPTY()) |
4328 if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL) | 4326 if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL) |
4329 break; | 4327 break; |
4330 (void)move_lines(savebuf, buf); | 4328 (void)move_lines(savebuf, buf); |
4331 } | 4329 } |
4332 } | 4330 } |
4333 else if (buf == curbuf) /* "buf" still valid */ | 4331 else if (buf == curbuf) // "buf" still valid |
4334 { | 4332 { |
4335 /* Mark the buffer as unmodified and free undo info. */ | 4333 // Mark the buffer as unmodified and free undo info. |
4336 unchanged(buf, TRUE, TRUE); | 4334 unchanged(buf, TRUE, TRUE); |
4337 if ((flags & READ_KEEP_UNDO) == 0) | 4335 if ((flags & READ_KEEP_UNDO) == 0) |
4338 { | 4336 { |
4339 u_blockfree(buf); | 4337 u_blockfree(buf); |
4340 u_clearall(buf); | 4338 u_clearall(buf); |
4341 } | 4339 } |
4342 else | 4340 else |
4343 { | 4341 { |
4344 /* Mark all undo states as changed. */ | 4342 // Mark all undo states as changed. |
4345 u_unchanged(curbuf); | 4343 u_unchanged(curbuf); |
4346 } | 4344 } |
4347 } | 4345 } |
4348 } | 4346 } |
4349 vim_free(ea.cmd); | 4347 vim_free(ea.cmd); |
4350 | 4348 |
4351 if (savebuf != NULL && bufref_valid(&bufref)) | 4349 if (savebuf != NULL && bufref_valid(&bufref)) |
4352 wipe_buffer(savebuf, FALSE); | 4350 wipe_buffer(savebuf, FALSE); |
4353 | 4351 |
4354 #ifdef FEAT_DIFF | 4352 #ifdef FEAT_DIFF |
4355 /* Invalidate diff info if necessary. */ | 4353 // Invalidate diff info if necessary. |
4356 diff_invalidate(curbuf); | 4354 diff_invalidate(curbuf); |
4357 #endif | 4355 #endif |
4358 | 4356 |
4359 /* Restore the topline and cursor position and check it (lines may | 4357 // Restore the topline and cursor position and check it (lines may |
4360 * have been removed). */ | 4358 // have been removed). |
4361 if (old_topline > curbuf->b_ml.ml_line_count) | 4359 if (old_topline > curbuf->b_ml.ml_line_count) |
4362 curwin->w_topline = curbuf->b_ml.ml_line_count; | 4360 curwin->w_topline = curbuf->b_ml.ml_line_count; |
4363 else | 4361 else |
4364 curwin->w_topline = old_topline; | 4362 curwin->w_topline = old_topline; |
4365 curwin->w_cursor = old_cursor; | 4363 curwin->w_cursor = old_cursor; |
4369 #ifdef FEAT_FOLDING | 4367 #ifdef FEAT_FOLDING |
4370 { | 4368 { |
4371 win_T *wp; | 4369 win_T *wp; |
4372 tabpage_T *tp; | 4370 tabpage_T *tp; |
4373 | 4371 |
4374 /* Update folds unless they are defined manually. */ | 4372 // Update folds unless they are defined manually. |
4375 FOR_ALL_TAB_WINDOWS(tp, wp) | 4373 FOR_ALL_TAB_WINDOWS(tp, wp) |
4376 if (wp->w_buffer == curwin->w_buffer | 4374 if (wp->w_buffer == curwin->w_buffer |
4377 && !foldmethodIsManual(wp)) | 4375 && !foldmethodIsManual(wp)) |
4378 foldUpdateAll(wp); | 4376 foldUpdateAll(wp); |
4379 } | 4377 } |
4380 #endif | 4378 #endif |
4381 /* If the mode didn't change and 'readonly' was set, keep the old | 4379 // If the mode didn't change and 'readonly' was set, keep the old |
4382 * value; the user probably used the ":view" command. But don't | 4380 // value; the user probably used the ":view" command. But don't |
4383 * reset it, might have had a read error. */ | 4381 // reset it, might have had a read error. |
4384 if (orig_mode == curbuf->b_orig_mode) | 4382 if (orig_mode == curbuf->b_orig_mode) |
4385 curbuf->b_p_ro |= old_ro; | 4383 curbuf->b_p_ro |= old_ro; |
4386 | 4384 |
4387 /* Modelines must override settings done by autocommands. */ | 4385 // Modelines must override settings done by autocommands. |
4388 do_modelines(0); | 4386 do_modelines(0); |
4389 } | 4387 } |
4390 | 4388 |
4391 /* restore curwin/curbuf and a few other things */ | 4389 // restore curwin/curbuf and a few other things |
4392 aucmd_restbuf(&aco); | 4390 aucmd_restbuf(&aco); |
4393 /* Careful: autocommands may have made "buf" invalid! */ | 4391 // Careful: autocommands may have made "buf" invalid! |
4394 } | 4392 } |
4395 | 4393 |
4396 void | 4394 void |
4397 buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED) | 4395 buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED) |
4398 { | 4396 { |
4410 * Used for do_filter(), when the input lines for the filter are deleted. | 4408 * Used for do_filter(), when the input lines for the filter are deleted. |
4411 */ | 4409 */ |
4412 void | 4410 void |
4413 write_lnum_adjust(linenr_T offset) | 4411 write_lnum_adjust(linenr_T offset) |
4414 { | 4412 { |
4415 if (curbuf->b_no_eol_lnum != 0) /* only if there is a missing eol */ | 4413 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol |
4416 curbuf->b_no_eol_lnum += offset; | 4414 curbuf->b_no_eol_lnum += offset; |
4417 } | 4415 } |
4418 | 4416 |
4419 #if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO) | 4417 #if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO) |
4420 /* | 4418 /* |
4609 return result; | 4607 return result; |
4610 } | 4608 } |
4611 #endif | 4609 #endif |
4612 | 4610 |
4613 #if defined(TEMPDIRNAMES) || defined(PROTO) | 4611 #if defined(TEMPDIRNAMES) || defined(PROTO) |
4614 static long temp_count = 0; /* Temp filename counter. */ | 4612 static long temp_count = 0; // Temp filename counter. |
4615 | 4613 |
4616 /* | 4614 /* |
4617 * Delete the temp directory and all files it contains. | 4615 * Delete the temp directory and all files it contains. |
4618 */ | 4616 */ |
4619 void | 4617 void |
4620 vim_deltempdir(void) | 4618 vim_deltempdir(void) |
4621 { | 4619 { |
4622 if (vim_tempdir != NULL) | 4620 if (vim_tempdir != NULL) |
4623 { | 4621 { |
4624 /* remove the trailing path separator */ | 4622 // remove the trailing path separator |
4625 gettail(vim_tempdir)[-1] = NUL; | 4623 gettail(vim_tempdir)[-1] = NUL; |
4626 delete_recursive(vim_tempdir); | 4624 delete_recursive(vim_tempdir); |
4627 VIM_CLEAR(vim_tempdir); | 4625 VIM_CLEAR(vim_tempdir); |
4628 } | 4626 } |
4629 } | 4627 } |
4659 * The returned pointer is to allocated memory. | 4657 * The returned pointer is to allocated memory. |
4660 * The returned pointer is NULL if no valid name was found. | 4658 * The returned pointer is NULL if no valid name was found. |
4661 */ | 4659 */ |
4662 char_u * | 4660 char_u * |
4663 vim_tempname( | 4661 vim_tempname( |
4664 int extra_char UNUSED, /* char to use in the name instead of '?' */ | 4662 int extra_char UNUSED, // char to use in the name instead of '?' |
4665 int keep UNUSED) | 4663 int keep UNUSED) |
4666 { | 4664 { |
4667 #ifdef USE_TMPNAM | 4665 #ifdef USE_TMPNAM |
4668 char_u itmp[L_tmpnam]; /* use tmpnam() */ | 4666 char_u itmp[L_tmpnam]; // use tmpnam() |
4669 #elif defined(MSWIN) | 4667 #elif defined(MSWIN) |
4670 WCHAR itmp[TEMPNAMELEN]; | 4668 WCHAR itmp[TEMPNAMELEN]; |
4671 #else | 4669 #else |
4672 char_u itmp[TEMPNAMELEN]; | 4670 char_u itmp[TEMPNAMELEN]; |
4673 #endif | 4671 #endif |
4697 size_t itmplen; | 4695 size_t itmplen; |
4698 long nr; | 4696 long nr; |
4699 long off; | 4697 long off; |
4700 # endif | 4698 # endif |
4701 | 4699 |
4702 /* Expand $TMP, leave room for "/v1100000/999999999". | 4700 // Expand $TMP, leave room for "/v1100000/999999999". |
4703 * Skip the directory check if the expansion fails. */ | 4701 // Skip the directory check if the expansion fails. |
4704 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20); | 4702 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20); |
4705 if (itmp[0] != '$' && mch_isdir(itmp)) | 4703 if (itmp[0] != '$' && mch_isdir(itmp)) |
4706 { | 4704 { |
4707 /* directory exists */ | 4705 // directory exists |
4708 add_pathsep(itmp); | 4706 add_pathsep(itmp); |
4709 | 4707 |
4710 # ifdef HAVE_MKDTEMP | 4708 # ifdef HAVE_MKDTEMP |
4711 { | 4709 { |
4712 # if defined(UNIX) || defined(VMS) | 4710 # if defined(UNIX) || defined(VMS) |
4713 /* Make sure the umask doesn't remove the executable bit. | 4711 // Make sure the umask doesn't remove the executable bit. |
4714 * "repl" has been reported to use "177". */ | 4712 // "repl" has been reported to use "177". |
4715 mode_t umask_save = umask(077); | 4713 mode_t umask_save = umask(077); |
4716 # endif | 4714 # endif |
4717 /* Leave room for filename */ | 4715 // Leave room for filename |
4718 STRCAT(itmp, "vXXXXXX"); | 4716 STRCAT(itmp, "vXXXXXX"); |
4719 if (mkdtemp((char *)itmp) != NULL) | 4717 if (mkdtemp((char *)itmp) != NULL) |
4720 vim_settempdir(itmp); | 4718 vim_settempdir(itmp); |
4721 # if defined(UNIX) || defined(VMS) | 4719 # if defined(UNIX) || defined(VMS) |
4722 (void)umask(umask_save); | 4720 (void)umask(umask_save); |
4723 # endif | 4721 # endif |
4724 } | 4722 } |
4725 # else | 4723 # else |
4726 /* Get an arbitrary number of up to 6 digits. When it's | 4724 // Get an arbitrary number of up to 6 digits. When it's |
4727 * unlikely that it already exists it will be faster, | 4725 // unlikely that it already exists it will be faster, |
4728 * otherwise it doesn't matter. The use of mkdir() avoids any | 4726 // otherwise it doesn't matter. The use of mkdir() avoids any |
4729 * security problems because of the predictable number. */ | 4727 // security problems because of the predictable number. |
4730 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L; | 4728 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L; |
4731 itmplen = STRLEN(itmp); | 4729 itmplen = STRLEN(itmp); |
4732 | 4730 |
4733 /* Try up to 10000 different values until we find a name that | 4731 // Try up to 10000 different values until we find a name that |
4734 * doesn't exist. */ | 4732 // doesn't exist. |
4735 for (off = 0; off < 10000L; ++off) | 4733 for (off = 0; off < 10000L; ++off) |
4736 { | 4734 { |
4737 int r; | 4735 int r; |
4738 # if defined(UNIX) || defined(VMS) | 4736 # if defined(UNIX) || defined(VMS) |
4739 mode_t umask_save; | 4737 mode_t umask_save; |
4740 # endif | 4738 # endif |
4741 | 4739 |
4742 sprintf((char *)itmp + itmplen, "v%ld", nr + off); | 4740 sprintf((char *)itmp + itmplen, "v%ld", nr + off); |
4743 # ifndef EEXIST | 4741 # ifndef EEXIST |
4744 /* If mkdir() does not set errno to EEXIST, check for | 4742 // If mkdir() does not set errno to EEXIST, check for |
4745 * existing file here. There is a race condition then, | 4743 // existing file here. There is a race condition then, |
4746 * although it's fail-safe. */ | 4744 // although it's fail-safe. |
4747 if (mch_stat((char *)itmp, &st) >= 0) | 4745 if (mch_stat((char *)itmp, &st) >= 0) |
4748 continue; | 4746 continue; |
4749 # endif | 4747 # endif |
4750 # if defined(UNIX) || defined(VMS) | 4748 # if defined(UNIX) || defined(VMS) |
4751 /* Make sure the umask doesn't remove the executable bit. | 4749 // Make sure the umask doesn't remove the executable bit. |
4752 * "repl" has been reported to use "177". */ | 4750 // "repl" has been reported to use "177". |
4753 umask_save = umask(077); | 4751 umask_save = umask(077); |
4754 # endif | 4752 # endif |
4755 r = vim_mkdir(itmp, 0700); | 4753 r = vim_mkdir(itmp, 0700); |
4756 # if defined(UNIX) || defined(VMS) | 4754 # if defined(UNIX) || defined(VMS) |
4757 (void)umask(umask_save); | 4755 (void)umask(umask_save); |
4760 { | 4758 { |
4761 vim_settempdir(itmp); | 4759 vim_settempdir(itmp); |
4762 break; | 4760 break; |
4763 } | 4761 } |
4764 # ifdef EEXIST | 4762 # ifdef EEXIST |
4765 /* If the mkdir() didn't fail because the file/dir exists, | 4763 // If the mkdir() didn't fail because the file/dir exists, |
4766 * we probably can't create any dir here, try another | 4764 // we probably can't create any dir here, try another |
4767 * place. */ | 4765 // place. |
4768 if (errno != EEXIST) | 4766 if (errno != EEXIST) |
4769 # endif | 4767 # endif |
4770 break; | 4768 break; |
4771 } | 4769 } |
4772 # endif /* HAVE_MKDTEMP */ | 4770 # endif // HAVE_MKDTEMP |
4773 if (vim_tempdir != NULL) | 4771 if (vim_tempdir != NULL) |
4774 break; | 4772 break; |
4775 } | 4773 } |
4776 } | 4774 } |
4777 } | 4775 } |
4778 | 4776 |
4779 if (vim_tempdir != NULL) | 4777 if (vim_tempdir != NULL) |
4780 { | 4778 { |
4781 /* There is no need to check if the file exists, because we own the | 4779 // There is no need to check if the file exists, because we own the |
4782 * directory and nobody else creates a file in it. */ | 4780 // directory and nobody else creates a file in it. |
4783 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++); | 4781 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++); |
4784 return vim_strsave(itmp); | 4782 return vim_strsave(itmp); |
4785 } | 4783 } |
4786 | 4784 |
4787 return NULL; | 4785 return NULL; |
4788 | 4786 |
4789 #else /* TEMPDIRNAMES */ | 4787 #else // TEMPDIRNAMES |
4790 | 4788 |
4791 # ifdef MSWIN | 4789 # ifdef MSWIN |
4792 WCHAR wszTempFile[_MAX_PATH + 1]; | 4790 WCHAR wszTempFile[_MAX_PATH + 1]; |
4793 WCHAR buf4[4]; | 4791 WCHAR buf4[4]; |
4794 char_u *retval; | 4792 char_u *retval; |
4799 { | 4797 { |
4800 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir | 4798 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir |
4801 wszTempFile[1] = NUL; | 4799 wszTempFile[1] = NUL; |
4802 } | 4800 } |
4803 wcscpy(buf4, L"VIM"); | 4801 wcscpy(buf4, L"VIM"); |
4804 buf4[2] = extra_char; /* make it "VIa", "VIb", etc. */ | 4802 buf4[2] = extra_char; // make it "VIa", "VIb", etc. |
4805 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0) | 4803 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0) |
4806 return NULL; | 4804 return NULL; |
4807 if (!keep) | 4805 if (!keep) |
4808 // GetTempFileName() will create the file, we don't want that | 4806 // GetTempFileName() will create the file, we don't want that |
4809 (void)DeleteFileW(itmp); | 4807 (void)DeleteFileW(itmp); |
4821 # else // MSWIN | 4819 # else // MSWIN |
4822 | 4820 |
4823 # ifdef USE_TMPNAM | 4821 # ifdef USE_TMPNAM |
4824 char_u *p; | 4822 char_u *p; |
4825 | 4823 |
4826 /* tmpnam() will make its own name */ | 4824 // tmpnam() will make its own name |
4827 p = tmpnam((char *)itmp); | 4825 p = tmpnam((char *)itmp); |
4828 if (p == NULL || *p == NUL) | 4826 if (p == NULL || *p == NUL) |
4829 return NULL; | 4827 return NULL; |
4830 # else | 4828 # else |
4831 char_u *p; | 4829 char_u *p; |
4832 | 4830 |
4833 # ifdef VMS_TEMPNAM | 4831 # ifdef VMS_TEMPNAM |
4834 /* mktemp() is not working on VMS. It seems to be | 4832 // mktemp() is not working on VMS. It seems to be |
4835 * a do-nothing function. Therefore we use tempnam(). | 4833 // a do-nothing function. Therefore we use tempnam(). |
4836 */ | |
4837 sprintf((char *)itmp, "VIM%c", extra_char); | 4834 sprintf((char *)itmp, "VIM%c", extra_char); |
4838 p = (char_u *)tempnam("tmp:", (char *)itmp); | 4835 p = (char_u *)tempnam("tmp:", (char *)itmp); |
4839 if (p != NULL) | 4836 if (p != NULL) |
4840 { | 4837 { |
4841 /* VMS will use '.LIS' if we don't explicitly specify an extension, | 4838 // VMS will use '.LIS' if we don't explicitly specify an extension, |
4842 * and VIM will then be unable to find the file later */ | 4839 // and VIM will then be unable to find the file later |
4843 STRCPY(itmp, p); | 4840 STRCPY(itmp, p); |
4844 STRCAT(itmp, ".txt"); | 4841 STRCAT(itmp, ".txt"); |
4845 free(p); | 4842 free(p); |
4846 } | 4843 } |
4847 else | 4844 else |
4855 # endif | 4852 # endif |
4856 # endif | 4853 # endif |
4857 | 4854 |
4858 return vim_strsave(itmp); | 4855 return vim_strsave(itmp); |
4859 # endif // MSWIN | 4856 # endif // MSWIN |
4860 #endif /* TEMPDIRNAMES */ | 4857 #endif // TEMPDIRNAMES |
4861 } | 4858 } |
4862 | 4859 |
4863 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) | 4860 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) |
4864 /* | 4861 /* |
4865 * Convert all backslashes in fname to forward slashes in-place, unless when | 4862 * Convert all backslashes in fname to forward slashes in-place, unless when |
4871 char_u *p; | 4868 char_u *p; |
4872 | 4869 |
4873 if (path_with_url(fname)) | 4870 if (path_with_url(fname)) |
4874 return; | 4871 return; |
4875 for (p = fname; *p != NUL; ++p) | 4872 for (p = fname; *p != NUL; ++p) |
4876 /* The Big5 encoding can have '\' in the trail byte. */ | 4873 // The Big5 encoding can have '\' in the trail byte. |
4877 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) | 4874 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
4878 ++p; | 4875 ++p; |
4879 else if (*p == '\\') | 4876 else if (*p == '\\') |
4880 *p = '/'; | 4877 *p = '/'; |
4881 } | 4878 } |
4888 * Used for autocommands and 'wildignore'. | 4885 * Used for autocommands and 'wildignore'. |
4889 * Returns TRUE if there is a match, FALSE otherwise. | 4886 * Returns TRUE if there is a match, FALSE otherwise. |
4890 */ | 4887 */ |
4891 int | 4888 int |
4892 match_file_pat( | 4889 match_file_pat( |
4893 char_u *pattern, /* pattern to match with */ | 4890 char_u *pattern, // pattern to match with |
4894 regprog_T **prog, /* pre-compiled regprog or NULL */ | 4891 regprog_T **prog, // pre-compiled regprog or NULL |
4895 char_u *fname, /* full path of file name */ | 4892 char_u *fname, // full path of file name |
4896 char_u *sfname, /* short file name or NULL */ | 4893 char_u *sfname, // short file name or NULL |
4897 char_u *tail, /* tail of path */ | 4894 char_u *tail, // tail of path |
4898 int allow_dirs) /* allow matching with dir */ | 4895 int allow_dirs) // allow matching with dir |
4899 { | 4896 { |
4900 regmatch_T regmatch; | 4897 regmatch_T regmatch; |
4901 int result = FALSE; | 4898 int result = FALSE; |
4902 | 4899 |
4903 regmatch.rm_ic = p_fic; /* ignore case if 'fileignorecase' is set */ | 4900 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set |
4904 if (prog != NULL) | 4901 if (prog != NULL) |
4905 regmatch.regprog = *prog; | 4902 regmatch.regprog = *prog; |
4906 else | 4903 else |
4907 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC); | 4904 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC); |
4908 | 4905 |
4943 int match; | 4940 int match; |
4944 char_u *p; | 4941 char_u *p; |
4945 | 4942 |
4946 tail = gettail(sfname); | 4943 tail = gettail(sfname); |
4947 | 4944 |
4948 /* try all patterns in 'wildignore' */ | 4945 // try all patterns in 'wildignore' |
4949 p = list; | 4946 p = list; |
4950 while (*p) | 4947 while (*p) |
4951 { | 4948 { |
4952 copy_option_part(&p, buf, 100, ","); | 4949 copy_option_part(&p, buf, 100, ","); |
4953 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE); | 4950 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE); |
4973 * Returns NULL when out of memory. | 4970 * Returns NULL when out of memory. |
4974 */ | 4971 */ |
4975 char_u * | 4972 char_u * |
4976 file_pat_to_reg_pat( | 4973 file_pat_to_reg_pat( |
4977 char_u *pat, | 4974 char_u *pat, |
4978 char_u *pat_end, /* first char after pattern or NULL */ | 4975 char_u *pat_end, // first char after pattern or NULL |
4979 char *allow_dirs, /* Result passed back out in here */ | 4976 char *allow_dirs, // Result passed back out in here |
4980 int no_bslash UNUSED) /* Don't use a backward slash as pathsep */ | 4977 int no_bslash UNUSED) // Don't use a backward slash as pathsep |
4981 { | 4978 { |
4982 int size = 2; /* '^' at start, '$' at end */ | 4979 int size = 2; // '^' at start, '$' at end |
4983 char_u *endp; | 4980 char_u *endp; |
4984 char_u *reg_pat; | 4981 char_u *reg_pat; |
4985 char_u *p; | 4982 char_u *p; |
4986 int i; | 4983 int i; |
4987 int nested = 0; | 4984 int nested = 0; |
5000 case '.': | 4997 case '.': |
5001 case ',': | 4998 case ',': |
5002 case '{': | 4999 case '{': |
5003 case '}': | 5000 case '}': |
5004 case '~': | 5001 case '~': |
5005 size += 2; /* extra backslash */ | 5002 size += 2; // extra backslash |
5006 break; | 5003 break; |
5007 #ifdef BACKSLASH_IN_FILENAME | 5004 #ifdef BACKSLASH_IN_FILENAME |
5008 case '\\': | 5005 case '\\': |
5009 case '/': | 5006 case '/': |
5010 size += 4; /* could become "[\/]" */ | 5007 size += 4; // could become "[\/]" |
5011 break; | 5008 break; |
5012 #endif | 5009 #endif |
5013 default: | 5010 default: |
5014 size++; | 5011 size++; |
5015 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) | 5012 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
5043 switch (*p) | 5040 switch (*p) |
5044 { | 5041 { |
5045 case '*': | 5042 case '*': |
5046 reg_pat[i++] = '.'; | 5043 reg_pat[i++] = '.'; |
5047 reg_pat[i++] = '*'; | 5044 reg_pat[i++] = '*'; |
5048 while (p[1] == '*') /* "**" matches like "*" */ | 5045 while (p[1] == '*') // "**" matches like "*" |
5049 ++p; | 5046 ++p; |
5050 break; | 5047 break; |
5051 case '.': | 5048 case '.': |
5052 case '~': | 5049 case '~': |
5053 reg_pat[i++] = '\\'; | 5050 reg_pat[i++] = '\\'; |
5060 if (p[1] == NUL) | 5057 if (p[1] == NUL) |
5061 break; | 5058 break; |
5062 #ifdef BACKSLASH_IN_FILENAME | 5059 #ifdef BACKSLASH_IN_FILENAME |
5063 if (!no_bslash) | 5060 if (!no_bslash) |
5064 { | 5061 { |
5065 /* translate: | 5062 // translate: |
5066 * "\x" to "\\x" e.g., "dir\file" | 5063 // "\x" to "\\x" e.g., "dir\file" |
5067 * "\*" to "\\.*" e.g., "dir\*.c" | 5064 // "\*" to "\\.*" e.g., "dir\*.c" |
5068 * "\?" to "\\." e.g., "dir\??.c" | 5065 // "\?" to "\\." e.g., "dir\??.c" |
5069 * "\+" to "\+" e.g., "fileX\+.c" | 5066 // "\+" to "\+" e.g., "fileX\+.c" |
5070 */ | |
5071 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?') | 5067 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?') |
5072 && p[1] != '+') | 5068 && p[1] != '+') |
5073 { | 5069 { |
5074 reg_pat[i++] = '['; | 5070 reg_pat[i++] = '['; |
5075 reg_pat[i++] = '\\'; | 5071 reg_pat[i++] = '\\'; |
5079 *allow_dirs = TRUE; | 5075 *allow_dirs = TRUE; |
5080 break; | 5076 break; |
5081 } | 5077 } |
5082 } | 5078 } |
5083 #endif | 5079 #endif |
5084 /* Undo escaping from ExpandEscape(): | 5080 // Undo escaping from ExpandEscape(): |
5085 * foo\?bar -> foo?bar | 5081 // foo\?bar -> foo?bar |
5086 * foo\%bar -> foo%bar | 5082 // foo\%bar -> foo%bar |
5087 * foo\,bar -> foo,bar | 5083 // foo\,bar -> foo,bar |
5088 * foo\ bar -> foo bar | 5084 // foo\ bar -> foo bar |
5089 * Don't unescape \, * and others that are also special in a | 5085 // Don't unescape \, * and others that are also special in a |
5090 * regexp. | 5086 // regexp. |
5091 * An escaped { must be unescaped since we use magic not | 5087 // An escaped { must be unescaped since we use magic not |
5092 * verymagic. Use "\\\{n,m\}"" to get "\{n,m}". | 5088 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}". |
5093 */ | |
5094 if (*++p == '?' | 5089 if (*++p == '?' |
5095 #ifdef BACKSLASH_IN_FILENAME | 5090 #ifdef BACKSLASH_IN_FILENAME |
5096 && no_bslash | 5091 && no_bslash |
5097 #endif | 5092 #endif |
5098 ) | 5093 ) |
5198 write_eintr(int fd, void *buf, size_t bufsize) | 5193 write_eintr(int fd, void *buf, size_t bufsize) |
5199 { | 5194 { |
5200 long ret = 0; | 5195 long ret = 0; |
5201 long wlen; | 5196 long wlen; |
5202 | 5197 |
5203 /* Repeat the write() so long it didn't fail, other than being interrupted | 5198 // Repeat the write() so long it didn't fail, other than being interrupted |
5204 * by a signal. */ | 5199 // by a signal. |
5205 while (ret < (long)bufsize) | 5200 while (ret < (long)bufsize) |
5206 { | 5201 { |
5207 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret); | 5202 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret); |
5208 if (wlen < 0) | 5203 if (wlen < 0) |
5209 { | 5204 { |