Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * fileio.c: read from and write to a file |
| 12 | */ |
| 13 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 14 | #include "vim.h" |
| 15 | |
Bram Moolenaar | f4888d0 | 2009-12-02 12:31:27 +0000 | [diff] [blame] | 16 | #if defined(__TANDEM) || defined(__MINT__) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 17 | # include <limits.h> /* for SSIZE_MAX */ |
| 18 | #endif |
| 19 | |
| 20 | #if defined(HAVE_UTIME) && defined(HAVE_UTIME_H) |
| 21 | # include <utime.h> /* for struct utimbuf */ |
| 22 | #endif |
| 23 | |
| 24 | #define BUFSIZE 8192 /* size of normal write buffer */ |
| 25 | #define SMBUFSIZE 256 /* size of emergency write buffer */ |
| 26 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 27 | /* Is there any system that doesn't have access()? */ |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 28 | #define USE_MCH_ACCESS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 29 | |
| 30 | #ifdef FEAT_MBYTE |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 31 | static char_u *next_fenc(char_u **pp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 32 | # ifdef FEAT_EVAL |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 33 | static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 34 | # endif |
| 35 | #endif |
| 36 | #ifdef FEAT_VIMINFO |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 37 | static void check_marks_read(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 38 | #endif |
| 39 | #ifdef FEAT_CRYPT |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 40 | static char_u *check_for_cryptkey(char_u *cryptkey, char_u *ptr, long *sizep, off_T *filesizep, int newfile, char_u *fname, int *did_ask); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 41 | #endif |
| 42 | #ifdef UNIX |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 43 | static void set_file_time(char_u *fname, time_t atime, time_t mtime); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 44 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 45 | static int set_rw_fname(char_u *fname, char_u *sfname); |
| 46 | static int msg_add_fileformat(int eol_type); |
| 47 | static void msg_add_eol(void); |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 48 | static int check_mtime(buf_T *buf, stat_T *s); |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 49 | static int time_differs(long t1, long t2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 50 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 51 | static int apply_autocmds_exarg(event_T event, char_u *fname, char_u *fname_io, int force, buf_T *buf, exarg_T *eap); |
| 52 | static int au_find_group(char_u *name); |
Bram Moolenaar | 70836c8 | 2006-02-20 21:28:49 +0000 | [diff] [blame] | 53 | |
| 54 | # define AUGROUP_DEFAULT -1 /* default autocmd group */ |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 55 | # define AUGROUP_ERROR -2 /* erroneous autocmd group */ |
Bram Moolenaar | 70836c8 | 2006-02-20 21:28:49 +0000 | [diff] [blame] | 56 | # define AUGROUP_ALL -3 /* all autocmd groups */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 57 | #endif |
| 58 | |
| 59 | #if defined(FEAT_CRYPT) || defined(FEAT_MBYTE) |
| 60 | # define HAS_BW_FLAGS |
| 61 | # define FIO_LATIN1 0x01 /* convert Latin1 */ |
| 62 | # define FIO_UTF8 0x02 /* convert UTF-8 */ |
| 63 | # define FIO_UCS2 0x04 /* convert UCS-2 */ |
| 64 | # define FIO_UCS4 0x08 /* convert UCS-4 */ |
| 65 | # define FIO_UTF16 0x10 /* convert UTF-16 */ |
| 66 | # ifdef WIN3264 |
| 67 | # define FIO_CODEPAGE 0x20 /* convert MS-Windows codepage */ |
| 68 | # define FIO_PUT_CP(x) (((x) & 0xffff) << 16) /* put codepage in top word */ |
| 69 | # define FIO_GET_CP(x) (((x)>>16) & 0xffff) /* get codepage from top word */ |
| 70 | # endif |
| 71 | # ifdef MACOS_X |
| 72 | # define FIO_MACROMAN 0x20 /* convert MacRoman */ |
| 73 | # endif |
| 74 | # define FIO_ENDIAN_L 0x80 /* little endian */ |
| 75 | # define FIO_ENCRYPTED 0x1000 /* encrypt written bytes */ |
| 76 | # define FIO_NOCONVERT 0x2000 /* skip encoding conversion */ |
| 77 | # define FIO_UCSBOM 0x4000 /* check for BOM at start of file */ |
| 78 | # define FIO_ALL -1 /* allow all formats */ |
| 79 | #endif |
| 80 | |
| 81 | /* When converting, a read() or write() may leave some bytes to be converted |
| 82 | * for the next call. The value is guessed... */ |
| 83 | #define CONV_RESTLEN 30 |
| 84 | |
| 85 | /* We have to guess how much a sequence of bytes may expand when converting |
| 86 | * with iconv() to be able to allocate a buffer. */ |
| 87 | #define ICONV_MULT 8 |
| 88 | |
| 89 | /* |
| 90 | * Structure to pass arguments from buf_write() to buf_write_bytes(). |
| 91 | */ |
| 92 | struct bw_info |
| 93 | { |
| 94 | int bw_fd; /* file descriptor */ |
| 95 | char_u *bw_buf; /* buffer with data to be written */ |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 96 | int bw_len; /* length of data */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 97 | #ifdef HAS_BW_FLAGS |
| 98 | int bw_flags; /* FIO_ flags */ |
| 99 | #endif |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 100 | #ifdef FEAT_CRYPT |
| 101 | buf_T *bw_buffer; /* buffer being written */ |
| 102 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 103 | #ifdef FEAT_MBYTE |
| 104 | char_u bw_rest[CONV_RESTLEN]; /* not converted bytes */ |
| 105 | int bw_restlen; /* nr of bytes in bw_rest[] */ |
| 106 | int bw_first; /* first write call */ |
| 107 | char_u *bw_conv_buf; /* buffer for writing converted chars */ |
| 108 | int bw_conv_buflen; /* size of bw_conv_buf */ |
| 109 | int bw_conv_error; /* set for conversion error */ |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 110 | linenr_T bw_conv_error_lnum; /* first line with error or zero */ |
| 111 | linenr_T bw_start_lnum; /* line number at start of buffer */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 112 | # ifdef USE_ICONV |
| 113 | iconv_t bw_iconv_fd; /* descriptor for iconv() or -1 */ |
| 114 | # endif |
| 115 | #endif |
| 116 | }; |
| 117 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 118 | static int buf_write_bytes(struct bw_info *ip); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 119 | |
| 120 | #ifdef FEAT_MBYTE |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 121 | static linenr_T readfile_linenr(linenr_T linecnt, char_u *p, char_u *endp); |
| 122 | static int ucs2bytes(unsigned c, char_u **pp, int flags); |
| 123 | static int need_conversion(char_u *fenc); |
| 124 | static int get_fio_flags(char_u *ptr); |
| 125 | static char_u *check_for_bom(char_u *p, long size, int *lenp, int flags); |
| 126 | static int make_bom(char_u *buf, char_u *name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 127 | # ifdef WIN3264 |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 128 | static int get_win_fio_flags(char_u *ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 129 | # endif |
| 130 | # ifdef MACOS_X |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 131 | static int get_mac_fio_flags(char_u *ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 132 | # endif |
| 133 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 134 | static int move_lines(buf_T *frombuf, buf_T *tobuf); |
Bram Moolenaar | 4592dee | 2009-11-18 19:11:58 +0000 | [diff] [blame] | 135 | #ifdef TEMPDIRNAMES |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 136 | static void vim_settempdir(char_u *tempdir); |
Bram Moolenaar | 4592dee | 2009-11-18 19:11:58 +0000 | [diff] [blame] | 137 | #endif |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 138 | #ifdef FEAT_AUTOCMD |
| 139 | static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name"); |
| 140 | #endif |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 141 | |
Bram Moolenaar | c369133 | 2016-04-20 12:49:49 +0200 | [diff] [blame] | 142 | #ifdef FEAT_AUTOCMD |
| 143 | /* |
| 144 | * Set by the apply_autocmds_group function if the given event is equal to |
| 145 | * EVENT_FILETYPE. Used by the readfile function in order to determine if |
| 146 | * EVENT_BUFREADPOST triggered the EVENT_FILETYPE. |
| 147 | * |
| 148 | * Relying on this value requires one to reset it prior calling |
| 149 | * apply_autocmds_group. |
| 150 | */ |
| 151 | static int au_did_filetype INIT(= FALSE); |
| 152 | #endif |
| 153 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 154 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 155 | filemess( |
| 156 | buf_T *buf, |
| 157 | char_u *name, |
| 158 | char_u *s, |
| 159 | int attr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 160 | { |
| 161 | int msg_scroll_save; |
| 162 | |
| 163 | if (msg_silent != 0) |
| 164 | return; |
| 165 | msg_add_fname(buf, name); /* put file name in IObuff with quotes */ |
| 166 | /* If it's extremely long, truncate it. */ |
| 167 | if (STRLEN(IObuff) > IOSIZE - 80) |
| 168 | IObuff[IOSIZE - 80] = NUL; |
| 169 | STRCAT(IObuff, s); |
| 170 | /* |
| 171 | * For the first message may have to start a new line. |
| 172 | * For further ones overwrite the previous one, reset msg_scroll before |
| 173 | * calling filemess(). |
| 174 | */ |
| 175 | msg_scroll_save = msg_scroll; |
| 176 | if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) |
| 177 | msg_scroll = FALSE; |
| 178 | if (!msg_scroll) /* wait a bit when overwriting an error msg */ |
| 179 | check_for_delay(FALSE); |
| 180 | msg_start(); |
| 181 | msg_scroll = msg_scroll_save; |
| 182 | msg_scrolled_ign = TRUE; |
| 183 | /* may truncate the message to avoid a hit-return prompt */ |
| 184 | msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr); |
| 185 | msg_clr_eos(); |
| 186 | out_flush(); |
| 187 | msg_scrolled_ign = FALSE; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Read lines from file "fname" into the buffer after line "from". |
| 192 | * |
| 193 | * 1. We allocate blocks with lalloc, as big as possible. |
| 194 | * 2. Each block is filled with characters from the file with a single read(). |
| 195 | * 3. The lines are inserted in the buffer with ml_append(). |
| 196 | * |
| 197 | * (caller must check that fname != NULL, unless READ_STDIN is used) |
| 198 | * |
| 199 | * "lines_to_skip" is the number of lines that must be skipped |
| 200 | * "lines_to_read" is the number of lines that are appended |
| 201 | * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM. |
| 202 | * |
| 203 | * flags: |
| 204 | * READ_NEW starting to edit a new buffer |
| 205 | * READ_FILTER reading filter output |
| 206 | * READ_STDIN read from stdin instead of a file |
| 207 | * READ_BUFFER read from curbuf instead of a file (converting after reading |
| 208 | * stdin) |
| 209 | * READ_DUMMY read into a dummy buffer (to check if file contents changed) |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 210 | * READ_KEEP_UNDO don't clear undo info or read it from a file |
Bram Moolenaar | f71d7b9 | 2016-08-09 22:14:05 +0200 | [diff] [blame] | 211 | * READ_FIFO read from fifo/socket instead of a file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 212 | * |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 213 | * return FAIL for failure, NOTDONE for directory (failure), or OK |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 214 | */ |
| 215 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 216 | readfile( |
| 217 | char_u *fname, |
| 218 | char_u *sfname, |
| 219 | linenr_T from, |
| 220 | linenr_T lines_to_skip, |
| 221 | linenr_T lines_to_read, |
| 222 | exarg_T *eap, /* can be NULL! */ |
| 223 | int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 224 | { |
| 225 | int fd = 0; |
| 226 | int newfile = (flags & READ_NEW); |
| 227 | int check_readonly; |
| 228 | int filtering = (flags & READ_FILTER); |
| 229 | int read_stdin = (flags & READ_STDIN); |
| 230 | int read_buffer = (flags & READ_BUFFER); |
Bram Moolenaar | f71d7b9 | 2016-08-09 22:14:05 +0200 | [diff] [blame] | 231 | int read_fifo = (flags & READ_FIFO); |
Bram Moolenaar | 690ffc0 | 2008-01-04 15:31:21 +0000 | [diff] [blame] | 232 | int set_options = newfile || read_buffer |
| 233 | || (eap != NULL && eap->read_edit); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 234 | linenr_T read_buf_lnum = 1; /* next line to read from curbuf */ |
| 235 | colnr_T read_buf_col = 0; /* next char to read from this line */ |
| 236 | char_u c; |
| 237 | linenr_T lnum = from; |
| 238 | char_u *ptr = NULL; /* pointer into read buffer */ |
| 239 | char_u *buffer = NULL; /* read buffer */ |
| 240 | char_u *new_buffer = NULL; /* init to shut up gcc */ |
| 241 | char_u *line_start = NULL; /* init to shut up gcc */ |
| 242 | int wasempty; /* buffer was empty before reading */ |
| 243 | colnr_T len; |
| 244 | long size = 0; |
| 245 | char_u *p; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 246 | off_T filesize = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 247 | int skip_read = FALSE; |
| 248 | #ifdef FEAT_CRYPT |
| 249 | char_u *cryptkey = NULL; |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 250 | int did_ask_for_key = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 251 | #endif |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 252 | #ifdef FEAT_PERSISTENT_UNDO |
| 253 | context_sha256_T sha_ctx; |
| 254 | int read_undo_file = FALSE; |
| 255 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 256 | int split = 0; /* number of split lines */ |
| 257 | #define UNKNOWN 0x0fffffff /* file size is unknown */ |
| 258 | linenr_T linecnt; |
| 259 | int error = FALSE; /* errors encountered */ |
| 260 | int ff_error = EOL_UNKNOWN; /* file format with errors */ |
| 261 | long linerest = 0; /* remaining chars in line */ |
| 262 | #ifdef UNIX |
| 263 | int perm = 0; |
| 264 | int swap_mode = -1; /* protection bits for swap file */ |
| 265 | #else |
| 266 | int perm; |
| 267 | #endif |
| 268 | int fileformat = 0; /* end-of-line format */ |
| 269 | int keep_fileformat = FALSE; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 270 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 271 | int file_readonly; |
| 272 | linenr_T skip_count = 0; |
| 273 | linenr_T read_count = 0; |
| 274 | int msg_save = msg_scroll; |
| 275 | linenr_T read_no_eol_lnum = 0; /* non-zero lnum when last line of |
| 276 | * last read was missing the eol */ |
Bram Moolenaar | 7a2699e | 2017-01-23 21:31:09 +0100 | [diff] [blame] | 277 | int try_mac; |
| 278 | int try_dos; |
| 279 | int try_unix; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 280 | int file_rewind = FALSE; |
| 281 | #ifdef FEAT_MBYTE |
| 282 | int can_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 283 | linenr_T conv_error = 0; /* line nr with conversion error */ |
| 284 | linenr_T illegal_byte = 0; /* line nr with illegal byte */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 285 | int keep_dest_enc = FALSE; /* don't retry when char doesn't fit |
| 286 | in destination encoding */ |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 287 | int bad_char_behavior = BAD_REPLACE; |
| 288 | /* BAD_KEEP, BAD_DROP or character to |
| 289 | * replace with */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 290 | char_u *tmpname = NULL; /* name of 'charconvert' output file */ |
| 291 | int fio_flags = 0; |
| 292 | char_u *fenc; /* fileencoding to use */ |
| 293 | int fenc_alloced; /* fenc_next is in allocated memory */ |
| 294 | char_u *fenc_next = NULL; /* next item in 'fencs' or NULL */ |
| 295 | int advance_fenc = FALSE; |
| 296 | long real_size = 0; |
| 297 | # ifdef USE_ICONV |
| 298 | iconv_t iconv_fd = (iconv_t)-1; /* descriptor for iconv() or -1 */ |
| 299 | # ifdef FEAT_EVAL |
| 300 | int did_iconv = FALSE; /* TRUE when iconv() failed and trying |
| 301 | 'charconvert' next */ |
| 302 | # endif |
| 303 | # endif |
| 304 | int converted = FALSE; /* TRUE if conversion done */ |
| 305 | int notconverted = FALSE; /* TRUE if conversion wanted but it |
| 306 | wasn't possible */ |
| 307 | char_u conv_rest[CONV_RESTLEN]; |
| 308 | int conv_restlen = 0; /* nr of bytes in conv_rest[] */ |
| 309 | #endif |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 310 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | bb3d5dc | 2010-08-14 14:32:54 +0200 | [diff] [blame] | 311 | buf_T *old_curbuf; |
| 312 | char_u *old_b_ffname; |
| 313 | char_u *old_b_fname; |
| 314 | int using_b_ffname; |
| 315 | int using_b_fname; |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 316 | #endif |
Bram Moolenaar | bb3d5dc | 2010-08-14 14:32:54 +0200 | [diff] [blame] | 317 | |
Bram Moolenaar | c369133 | 2016-04-20 12:49:49 +0200 | [diff] [blame] | 318 | #ifdef FEAT_AUTOCMD |
| 319 | au_did_filetype = FALSE; /* reset before triggering any autocommands */ |
| 320 | #endif |
| 321 | |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 322 | curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 323 | |
| 324 | /* |
| 325 | * If there is no file name yet, use the one for the read file. |
| 326 | * BF_NOTEDITED is set to reflect this. |
| 327 | * Don't do this for a read from a filter. |
| 328 | * Only do this when 'cpoptions' contains the 'f' flag. |
| 329 | */ |
| 330 | if (curbuf->b_ffname == NULL |
| 331 | && !filtering |
| 332 | && fname != NULL |
| 333 | && vim_strchr(p_cpo, CPO_FNAMER) != NULL |
| 334 | && !(flags & READ_DUMMY)) |
| 335 | { |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 336 | if (set_rw_fname(fname, sfname) == FAIL) |
| 337 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Bram Moolenaar | bb3d5dc | 2010-08-14 14:32:54 +0200 | [diff] [blame] | 340 | #ifdef FEAT_AUTOCMD |
| 341 | /* Remember the initial values of curbuf, curbuf->b_ffname and |
| 342 | * curbuf->b_fname to detect whether they are altered as a result of |
| 343 | * executing nasty autocommands. Also check if "fname" and "sfname" |
| 344 | * point to one of these values. */ |
| 345 | old_curbuf = curbuf; |
| 346 | old_b_ffname = curbuf->b_ffname; |
| 347 | old_b_fname = curbuf->b_fname; |
| 348 | using_b_ffname = (fname == curbuf->b_ffname) |
| 349 | || (sfname == curbuf->b_ffname); |
| 350 | using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname); |
| 351 | #endif |
| 352 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 353 | /* After reading a file the cursor line changes but we don't want to |
| 354 | * display the line. */ |
| 355 | ex_no_reprint = TRUE; |
| 356 | |
Bram Moolenaar | 55b7cf8 | 2006-09-09 12:52:42 +0000 | [diff] [blame] | 357 | /* don't display the file info for another buffer now */ |
| 358 | need_fileinfo = FALSE; |
| 359 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 360 | /* |
| 361 | * For Unix: Use the short file name whenever possible. |
| 362 | * Avoids problems with networks and when directory names are changed. |
| 363 | * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to |
| 364 | * another directory, which we don't detect. |
| 365 | */ |
| 366 | if (sfname == NULL) |
| 367 | sfname = fname; |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 368 | #if defined(UNIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 369 | fname = sfname; |
| 370 | #endif |
| 371 | |
| 372 | #ifdef FEAT_AUTOCMD |
| 373 | /* |
| 374 | * The BufReadCmd and FileReadCmd events intercept the reading process by |
| 375 | * executing the associated commands instead. |
| 376 | */ |
| 377 | if (!filtering && !read_stdin && !read_buffer) |
| 378 | { |
| 379 | pos_T pos; |
| 380 | |
| 381 | pos = curbuf->b_op_start; |
| 382 | |
| 383 | /* Set '[ mark to the line above where the lines go (line 1 if zero). */ |
| 384 | curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); |
| 385 | curbuf->b_op_start.col = 0; |
| 386 | |
| 387 | if (newfile) |
| 388 | { |
| 389 | if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname, |
| 390 | FALSE, curbuf, eap)) |
| 391 | #ifdef FEAT_EVAL |
| 392 | return aborting() ? FAIL : OK; |
| 393 | #else |
| 394 | return OK; |
| 395 | #endif |
| 396 | } |
| 397 | else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname, |
| 398 | FALSE, NULL, eap)) |
| 399 | #ifdef FEAT_EVAL |
| 400 | return aborting() ? FAIL : OK; |
| 401 | #else |
| 402 | return OK; |
| 403 | #endif |
| 404 | |
| 405 | curbuf->b_op_start = pos; |
| 406 | } |
| 407 | #endif |
| 408 | |
| 409 | if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0) |
| 410 | msg_scroll = FALSE; /* overwrite previous file message */ |
| 411 | else |
| 412 | msg_scroll = TRUE; /* don't overwrite previous file message */ |
| 413 | |
| 414 | /* |
| 415 | * If the name ends in a path separator, we can't open it. Check here, |
| 416 | * because reading the file may actually work, but then creating the swap |
| 417 | * file may destroy it! Reported on MS-DOS and Win 95. |
| 418 | * If the name is too long we might crash further on, quit here. |
| 419 | */ |
Bram Moolenaar | 15d0a8c | 2004-09-06 17:44:46 +0000 | [diff] [blame] | 420 | if (fname != NULL && *fname != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 421 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 422 | p = fname + STRLEN(fname); |
| 423 | if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL) |
Bram Moolenaar | 15d0a8c | 2004-09-06 17:44:46 +0000 | [diff] [blame] | 424 | { |
| 425 | filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0); |
| 426 | msg_end(); |
| 427 | msg_scroll = msg_save; |
| 428 | return FAIL; |
| 429 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Bram Moolenaar | f71d7b9 | 2016-08-09 22:14:05 +0200 | [diff] [blame] | 432 | if (!read_stdin && !read_buffer && !read_fifo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 433 | { |
Bram Moolenaar | 4e4f529 | 2013-08-30 17:07:01 +0200 | [diff] [blame] | 434 | #ifdef UNIX |
| 435 | /* |
| 436 | * On Unix it is possible to read a directory, so we have to |
| 437 | * check for it before the mch_open(). |
| 438 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 439 | perm = mch_getperm(fname); |
| 440 | if (perm >= 0 && !S_ISREG(perm) /* not a regular file ... */ |
| 441 | # ifdef S_ISFIFO |
| 442 | && !S_ISFIFO(perm) /* ... or fifo */ |
| 443 | # endif |
| 444 | # ifdef S_ISSOCK |
| 445 | && !S_ISSOCK(perm) /* ... or socket */ |
| 446 | # endif |
Bram Moolenaar | fe1c56d | 2007-07-10 15:10:54 +0000 | [diff] [blame] | 447 | # ifdef OPEN_CHR_FILES |
| 448 | && !(S_ISCHR(perm) && is_dev_fd_file(fname)) |
| 449 | /* ... or a character special file named /dev/fd/<n> */ |
| 450 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 451 | ) |
| 452 | { |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 453 | int retval = FAIL; |
| 454 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 455 | if (S_ISDIR(perm)) |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 456 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 457 | filemess(curbuf, fname, (char_u *)_("is a directory"), 0); |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 458 | retval = NOTDONE; |
| 459 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 460 | else |
| 461 | filemess(curbuf, fname, (char_u *)_("is not a file"), 0); |
| 462 | msg_end(); |
| 463 | msg_scroll = msg_save; |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 464 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 465 | } |
Bram Moolenaar | 4e4f529 | 2013-08-30 17:07:01 +0200 | [diff] [blame] | 466 | #endif |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 467 | #if defined(MSWIN) |
Bram Moolenaar | c67764a | 2006-10-12 19:14:26 +0000 | [diff] [blame] | 468 | /* |
| 469 | * MS-Windows allows opening a device, but we will probably get stuck |
| 470 | * trying to read it. |
| 471 | */ |
| 472 | if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE) |
| 473 | { |
Bram Moolenaar | 5386a12 | 2007-06-28 20:02:32 +0000 | [diff] [blame] | 474 | filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0); |
Bram Moolenaar | c67764a | 2006-10-12 19:14:26 +0000 | [diff] [blame] | 475 | msg_end(); |
| 476 | msg_scroll = msg_save; |
| 477 | return FAIL; |
| 478 | } |
Bram Moolenaar | 043545e | 2006-10-10 16:44:07 +0000 | [diff] [blame] | 479 | #endif |
Bram Moolenaar | 4e4f529 | 2013-08-30 17:07:01 +0200 | [diff] [blame] | 480 | } |
Bram Moolenaar | 043545e | 2006-10-10 16:44:07 +0000 | [diff] [blame] | 481 | |
Bram Moolenaar | ad875fb | 2013-07-24 15:02:03 +0200 | [diff] [blame] | 482 | /* Set default or forced 'fileformat' and 'binary'. */ |
| 483 | set_file_options(set_options, eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 484 | |
| 485 | /* |
| 486 | * When opening a new file we take the readonly flag from the file. |
| 487 | * Default is r/w, can be set to r/o below. |
| 488 | * Don't reset it when in readonly mode |
| 489 | * Only set/reset b_p_ro when BF_CHECK_RO is set. |
| 490 | */ |
| 491 | check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO)); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 492 | if (check_readonly && !readonlymode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 493 | curbuf->b_p_ro = FALSE; |
| 494 | |
Bram Moolenaar | f71d7b9 | 2016-08-09 22:14:05 +0200 | [diff] [blame] | 495 | if (newfile && !read_stdin && !read_buffer && !read_fifo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 496 | { |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 497 | /* Remember time of file. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 498 | if (mch_stat((char *)fname, &st) >= 0) |
| 499 | { |
| 500 | buf_store_time(curbuf, &st, fname); |
| 501 | curbuf->b_mtime_read = curbuf->b_mtime; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 502 | #ifdef UNIX |
| 503 | /* |
| 504 | * Use the protection bits of the original file for the swap file. |
| 505 | * This makes it possible for others to read the name of the |
| 506 | * edited file from the swapfile, but only if they can read the |
| 507 | * edited file. |
| 508 | * Remove the "write" and "execute" bits for group and others |
| 509 | * (they must not write the swapfile). |
| 510 | * Add the "read" and "write" bits for the user, otherwise we may |
| 511 | * not be able to write to the file ourselves. |
| 512 | * Setting the bits is done below, after creating the swap file. |
| 513 | */ |
| 514 | swap_mode = (st.st_mode & 0644) | 0600; |
| 515 | #endif |
| 516 | #ifdef FEAT_CW_EDITOR |
| 517 | /* Get the FSSpec on MacOS |
| 518 | * TODO: Update it properly when the buffer name changes |
| 519 | */ |
| 520 | (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec); |
| 521 | #endif |
| 522 | #ifdef VMS |
| 523 | curbuf->b_fab_rfm = st.st_fab_rfm; |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 524 | curbuf->b_fab_rat = st.st_fab_rat; |
| 525 | curbuf->b_fab_mrs = st.st_fab_mrs; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 526 | #endif |
| 527 | } |
| 528 | else |
| 529 | { |
| 530 | curbuf->b_mtime = 0; |
| 531 | curbuf->b_mtime_read = 0; |
| 532 | curbuf->b_orig_size = 0; |
| 533 | curbuf->b_orig_mode = 0; |
| 534 | } |
| 535 | |
| 536 | /* Reset the "new file" flag. It will be set again below when the |
| 537 | * file doesn't exist. */ |
| 538 | curbuf->b_flags &= ~(BF_NEW | BF_NEW_W); |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * for UNIX: check readonly with perm and mch_access() |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 543 | * for Amiga: check readonly by trying to open the file for writing |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 544 | */ |
| 545 | file_readonly = FALSE; |
| 546 | if (read_stdin) |
| 547 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 548 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 549 | /* Force binary I/O on stdin to avoid CR-LF -> LF conversion. */ |
| 550 | setmode(0, O_BINARY); |
| 551 | #endif |
| 552 | } |
| 553 | else if (!read_buffer) |
| 554 | { |
| 555 | #ifdef USE_MCH_ACCESS |
| 556 | if ( |
| 557 | # ifdef UNIX |
| 558 | !(perm & 0222) || |
| 559 | # endif |
| 560 | mch_access((char *)fname, W_OK)) |
| 561 | file_readonly = TRUE; |
| 562 | fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); |
| 563 | #else |
| 564 | if (!newfile |
| 565 | || readonlymode |
| 566 | || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0) |
| 567 | { |
| 568 | file_readonly = TRUE; |
| 569 | /* try to open ro */ |
| 570 | fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); |
| 571 | } |
| 572 | #endif |
| 573 | } |
| 574 | |
| 575 | if (fd < 0) /* cannot open at all */ |
| 576 | { |
| 577 | #ifndef UNIX |
| 578 | int isdir_f; |
| 579 | #endif |
| 580 | msg_scroll = msg_save; |
| 581 | #ifndef UNIX |
| 582 | /* |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 583 | * On Amiga we can't open a directory, check here. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 584 | */ |
| 585 | isdir_f = (mch_isdir(fname)); |
| 586 | perm = mch_getperm(fname); /* check if the file exists */ |
| 587 | if (isdir_f) |
| 588 | { |
| 589 | filemess(curbuf, sfname, (char_u *)_("is a directory"), 0); |
| 590 | curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
| 591 | } |
| 592 | else |
| 593 | #endif |
| 594 | if (newfile) |
| 595 | { |
Bram Moolenaar | 2efbc66 | 2010-05-14 18:56:38 +0200 | [diff] [blame] | 596 | if (perm < 0 |
| 597 | #ifdef ENOENT |
| 598 | && errno == ENOENT |
| 599 | #endif |
| 600 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 601 | { |
| 602 | /* |
| 603 | * Set the 'new-file' flag, so that when the file has |
| 604 | * been created by someone else, a ":w" will complain. |
| 605 | */ |
| 606 | curbuf->b_flags |= BF_NEW; |
| 607 | |
| 608 | /* Create a swap file now, so that other Vims are warned |
| 609 | * that we are editing this file. Don't do this for a |
| 610 | * "nofile" or "nowrite" buffer type. */ |
| 611 | #ifdef FEAT_QUICKFIX |
| 612 | if (!bt_dontwrite(curbuf)) |
| 613 | #endif |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 614 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 615 | check_need_swap(newfile); |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 616 | #ifdef FEAT_AUTOCMD |
| 617 | /* SwapExists autocommand may mess things up */ |
| 618 | if (curbuf != old_curbuf |
| 619 | || (using_b_ffname |
| 620 | && (old_b_ffname != curbuf->b_ffname)) |
| 621 | || (using_b_fname |
| 622 | && (old_b_fname != curbuf->b_fname))) |
| 623 | { |
| 624 | EMSG(_(e_auchangedbuf)); |
| 625 | return FAIL; |
| 626 | } |
| 627 | #endif |
| 628 | } |
Bram Moolenaar | 5b962cf | 2005-12-12 21:58:40 +0000 | [diff] [blame] | 629 | if (dir_of_file_exists(fname)) |
| 630 | filemess(curbuf, sfname, (char_u *)_("[New File]"), 0); |
| 631 | else |
| 632 | filemess(curbuf, sfname, |
| 633 | (char_u *)_("[New DIRECTORY]"), 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 634 | #ifdef FEAT_VIMINFO |
| 635 | /* Even though this is a new file, it might have been |
| 636 | * edited before and deleted. Get the old marks. */ |
| 637 | check_marks_read(); |
| 638 | #endif |
| 639 | #ifdef FEAT_MBYTE |
Bram Moolenaar | ad875fb | 2013-07-24 15:02:03 +0200 | [diff] [blame] | 640 | /* Set forced 'fileencoding'. */ |
| 641 | if (eap != NULL) |
| 642 | set_forced_fenc(eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 643 | #endif |
| 644 | #ifdef FEAT_AUTOCMD |
| 645 | apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname, |
| 646 | FALSE, curbuf, eap); |
| 647 | #endif |
| 648 | /* remember the current fileformat */ |
| 649 | save_file_ff(curbuf); |
| 650 | |
| 651 | #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
| 652 | if (aborting()) /* autocmds may abort script processing */ |
| 653 | return FAIL; |
| 654 | #endif |
| 655 | return OK; /* a new file is not an error */ |
| 656 | } |
| 657 | else |
| 658 | { |
Bram Moolenaar | 202795b | 2005-10-11 20:29:39 +0000 | [diff] [blame] | 659 | filemess(curbuf, sfname, (char_u *)( |
| 660 | # ifdef EFBIG |
| 661 | (errno == EFBIG) ? _("[File too big]") : |
| 662 | # endif |
Bram Moolenaar | 2efbc66 | 2010-05-14 18:56:38 +0200 | [diff] [blame] | 663 | # ifdef EOVERFLOW |
| 664 | (errno == EOVERFLOW) ? _("[File too big]") : |
| 665 | # endif |
Bram Moolenaar | 202795b | 2005-10-11 20:29:39 +0000 | [diff] [blame] | 666 | _("[Permission Denied]")), 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 667 | curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | return FAIL; |
| 672 | } |
| 673 | |
| 674 | /* |
| 675 | * Only set the 'ro' flag for readonly files the first time they are |
| 676 | * loaded. Help files always get readonly mode |
| 677 | */ |
| 678 | if ((check_readonly && file_readonly) || curbuf->b_help) |
| 679 | curbuf->b_p_ro = TRUE; |
| 680 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 681 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 682 | { |
Bram Moolenaar | 690ffc0 | 2008-01-04 15:31:21 +0000 | [diff] [blame] | 683 | /* Don't change 'eol' if reading from buffer as it will already be |
| 684 | * correctly set when reading stdin. */ |
| 685 | if (!read_buffer) |
| 686 | { |
| 687 | curbuf->b_p_eol = TRUE; |
| 688 | curbuf->b_start_eol = TRUE; |
| 689 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 690 | #ifdef FEAT_MBYTE |
| 691 | curbuf->b_p_bomb = FALSE; |
Bram Moolenaar | 83eb885 | 2007-08-12 13:51:26 +0000 | [diff] [blame] | 692 | curbuf->b_start_bomb = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 693 | #endif |
| 694 | } |
| 695 | |
| 696 | /* Create a swap file now, so that other Vims are warned that we are |
| 697 | * editing this file. |
| 698 | * Don't do this for a "nofile" or "nowrite" buffer type. */ |
| 699 | #ifdef FEAT_QUICKFIX |
| 700 | if (!bt_dontwrite(curbuf)) |
| 701 | #endif |
| 702 | { |
| 703 | check_need_swap(newfile); |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 704 | #ifdef FEAT_AUTOCMD |
| 705 | if (!read_stdin && (curbuf != old_curbuf |
| 706 | || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) |
| 707 | || (using_b_fname && (old_b_fname != curbuf->b_fname)))) |
| 708 | { |
| 709 | EMSG(_(e_auchangedbuf)); |
| 710 | if (!read_buffer) |
| 711 | close(fd); |
| 712 | return FAIL; |
| 713 | } |
| 714 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 715 | #ifdef UNIX |
| 716 | /* Set swap file protection bits after creating it. */ |
Bram Moolenaar | f061e0b | 2009-06-24 15:32:01 +0000 | [diff] [blame] | 717 | if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL |
| 718 | && curbuf->b_ml.ml_mfp->mf_fname != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 719 | (void)mch_setperm(curbuf->b_ml.ml_mfp->mf_fname, (long)swap_mode); |
| 720 | #endif |
| 721 | } |
| 722 | |
Bram Moolenaar | b815dac | 2005-12-07 20:59:24 +0000 | [diff] [blame] | 723 | #if defined(HAS_SWAP_EXISTS_ACTION) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 724 | /* If "Quit" selected at ATTENTION dialog, don't load the file */ |
| 725 | if (swap_exists_action == SEA_QUIT) |
| 726 | { |
| 727 | if (!read_buffer && !read_stdin) |
| 728 | close(fd); |
| 729 | return FAIL; |
| 730 | } |
| 731 | #endif |
| 732 | |
| 733 | ++no_wait_return; /* don't wait for return yet */ |
| 734 | |
| 735 | /* |
| 736 | * Set '[ mark to the line above where the lines go (line 1 if zero). |
| 737 | */ |
| 738 | curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); |
| 739 | curbuf->b_op_start.col = 0; |
| 740 | |
Bram Moolenaar | 7a2699e | 2017-01-23 21:31:09 +0100 | [diff] [blame] | 741 | try_mac = (vim_strchr(p_ffs, 'm') != NULL); |
| 742 | try_dos = (vim_strchr(p_ffs, 'd') != NULL); |
| 743 | try_unix = (vim_strchr(p_ffs, 'x') != NULL); |
| 744 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 745 | #ifdef FEAT_AUTOCMD |
| 746 | if (!read_buffer) |
| 747 | { |
| 748 | int m = msg_scroll; |
| 749 | int n = msg_scrolled; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 750 | |
| 751 | /* |
| 752 | * The file must be closed again, the autocommands may want to change |
| 753 | * the file before reading it. |
| 754 | */ |
| 755 | if (!read_stdin) |
| 756 | close(fd); /* ignore errors */ |
| 757 | |
| 758 | /* |
| 759 | * The output from the autocommands should not overwrite anything and |
| 760 | * should not be overwritten: Set msg_scroll, restore its value if no |
| 761 | * output was done. |
| 762 | */ |
| 763 | msg_scroll = TRUE; |
| 764 | if (filtering) |
| 765 | apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname, |
| 766 | FALSE, curbuf, eap); |
| 767 | else if (read_stdin) |
| 768 | apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname, |
| 769 | FALSE, curbuf, eap); |
| 770 | else if (newfile) |
| 771 | apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname, |
| 772 | FALSE, curbuf, eap); |
| 773 | else |
| 774 | apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname, |
| 775 | FALSE, NULL, eap); |
Bram Moolenaar | 7a2699e | 2017-01-23 21:31:09 +0100 | [diff] [blame] | 776 | /* autocommands may have changed it */ |
| 777 | try_mac = (vim_strchr(p_ffs, 'm') != NULL); |
| 778 | try_dos = (vim_strchr(p_ffs, 'd') != NULL); |
| 779 | try_unix = (vim_strchr(p_ffs, 'x') != NULL); |
| 780 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 781 | if (msg_scrolled == n) |
| 782 | msg_scroll = m; |
| 783 | |
| 784 | #ifdef FEAT_EVAL |
| 785 | if (aborting()) /* autocmds may abort script processing */ |
| 786 | { |
| 787 | --no_wait_return; |
| 788 | msg_scroll = msg_save; |
| 789 | curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
| 790 | return FAIL; |
| 791 | } |
| 792 | #endif |
| 793 | /* |
| 794 | * Don't allow the autocommands to change the current buffer. |
| 795 | * Try to re-open the file. |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 796 | * |
| 797 | * Don't allow the autocommands to change the buffer name either |
| 798 | * (cd for example) if it invalidates fname or sfname. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 799 | */ |
| 800 | if (!read_stdin && (curbuf != old_curbuf |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 801 | || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) |
| 802 | || (using_b_fname && (old_b_fname != curbuf->b_fname)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 803 | || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0)) |
| 804 | { |
| 805 | --no_wait_return; |
| 806 | msg_scroll = msg_save; |
| 807 | if (fd < 0) |
| 808 | EMSG(_("E200: *ReadPre autocommands made the file unreadable")); |
| 809 | else |
| 810 | EMSG(_("E201: *ReadPre autocommands must not change current buffer")); |
| 811 | curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
| 812 | return FAIL; |
| 813 | } |
| 814 | } |
| 815 | #endif /* FEAT_AUTOCMD */ |
| 816 | |
| 817 | /* Autocommands may add lines to the file, need to check if it is empty */ |
| 818 | wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY); |
| 819 | |
| 820 | if (!recoverymode && !filtering && !(flags & READ_DUMMY)) |
| 821 | { |
| 822 | /* |
| 823 | * Show the user that we are busy reading the input. Sometimes this |
| 824 | * may take a while. When reading from stdin another program may |
| 825 | * still be running, don't move the cursor to the last line, unless |
| 826 | * always using the GUI. |
| 827 | */ |
| 828 | if (read_stdin) |
| 829 | { |
| 830 | #ifndef ALWAYS_USE_GUI |
| 831 | mch_msg(_("Vim: Reading from stdin...\n")); |
| 832 | #endif |
| 833 | #ifdef FEAT_GUI |
| 834 | /* Also write a message in the GUI window, if there is one. */ |
| 835 | if (gui.in_use && !gui.dying && !gui.starting) |
| 836 | { |
| 837 | p = (char_u *)_("Reading from stdin..."); |
| 838 | gui_write(p, (int)STRLEN(p)); |
| 839 | } |
| 840 | #endif |
| 841 | } |
| 842 | else if (!read_buffer) |
| 843 | filemess(curbuf, sfname, (char_u *)"", 0); |
| 844 | } |
| 845 | |
| 846 | msg_scroll = FALSE; /* overwrite the file message */ |
| 847 | |
| 848 | /* |
| 849 | * Set linecnt now, before the "retry" caused by a wrong guess for |
| 850 | * fileformat, and after the autocommands, which may change them. |
| 851 | */ |
| 852 | linecnt = curbuf->b_ml.ml_line_count; |
| 853 | |
| 854 | #ifdef FEAT_MBYTE |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 855 | /* "++bad=" argument. */ |
| 856 | if (eap != NULL && eap->bad_char != 0) |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 857 | { |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 858 | bad_char_behavior = eap->bad_char; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 859 | if (set_options) |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 860 | curbuf->b_bad_char = eap->bad_char; |
| 861 | } |
| 862 | else |
| 863 | curbuf->b_bad_char = 0; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 864 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 865 | /* |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 866 | * Decide which 'encoding' to use or use first. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 867 | */ |
| 868 | if (eap != NULL && eap->force_enc != 0) |
| 869 | { |
| 870 | fenc = enc_canonize(eap->cmd + eap->force_enc); |
| 871 | fenc_alloced = TRUE; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 872 | keep_dest_enc = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 873 | } |
| 874 | else if (curbuf->b_p_bin) |
| 875 | { |
| 876 | fenc = (char_u *)""; /* binary: don't convert */ |
| 877 | fenc_alloced = FALSE; |
| 878 | } |
| 879 | else if (curbuf->b_help) |
| 880 | { |
| 881 | char_u firstline[80]; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 882 | int fc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 883 | |
| 884 | /* Help files are either utf-8 or latin1. Try utf-8 first, if this |
| 885 | * fails it must be latin1. |
| 886 | * Always do this when 'encoding' is "utf-8". Otherwise only do |
| 887 | * this when needed to avoid [converted] remarks all the time. |
| 888 | * It is needed when the first line contains non-ASCII characters. |
| 889 | * That is only in *.??x files. */ |
| 890 | fenc = (char_u *)"latin1"; |
| 891 | c = enc_utf8; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 892 | if (!c && !read_stdin) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 893 | { |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 894 | fc = fname[STRLEN(fname) - 1]; |
| 895 | if (TOLOWER_ASC(fc) == 'x') |
| 896 | { |
| 897 | /* Read the first line (and a bit more). Immediately rewind to |
| 898 | * the start of the file. If the read() fails "len" is -1. */ |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 899 | len = read_eintr(fd, firstline, 80); |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 900 | vim_lseek(fd, (off_T)0L, SEEK_SET); |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 901 | for (p = firstline; p < firstline + len; ++p) |
| 902 | if (*p >= 0x80) |
| 903 | { |
| 904 | c = TRUE; |
| 905 | break; |
| 906 | } |
| 907 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | if (c) |
| 911 | { |
| 912 | fenc_next = fenc; |
| 913 | fenc = (char_u *)"utf-8"; |
| 914 | |
| 915 | /* When the file is utf-8 but a character doesn't fit in |
| 916 | * 'encoding' don't retry. In help text editing utf-8 bytes |
| 917 | * doesn't make sense. */ |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 918 | if (!enc_utf8) |
| 919 | keep_dest_enc = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 920 | } |
| 921 | fenc_alloced = FALSE; |
| 922 | } |
| 923 | else if (*p_fencs == NUL) |
| 924 | { |
| 925 | fenc = curbuf->b_p_fenc; /* use format from buffer */ |
| 926 | fenc_alloced = FALSE; |
| 927 | } |
| 928 | else |
| 929 | { |
| 930 | fenc_next = p_fencs; /* try items in 'fileencodings' */ |
| 931 | fenc = next_fenc(&fenc_next); |
| 932 | fenc_alloced = TRUE; |
| 933 | } |
| 934 | #endif |
| 935 | |
| 936 | /* |
| 937 | * Jump back here to retry reading the file in different ways. |
| 938 | * Reasons to retry: |
| 939 | * - encoding conversion failed: try another one from "fenc_next" |
| 940 | * - BOM detected and fenc was set, need to setup conversion |
| 941 | * - "fileformat" check failed: try another |
| 942 | * |
| 943 | * Variables set for special retry actions: |
| 944 | * "file_rewind" Rewind the file to start reading it again. |
| 945 | * "advance_fenc" Advance "fenc" using "fenc_next". |
| 946 | * "skip_read" Re-use already read bytes (BOM detected). |
| 947 | * "did_iconv" iconv() conversion failed, try 'charconvert'. |
| 948 | * "keep_fileformat" Don't reset "fileformat". |
| 949 | * |
| 950 | * Other status indicators: |
| 951 | * "tmpname" When != NULL did conversion with 'charconvert'. |
| 952 | * Output file has to be deleted afterwards. |
| 953 | * "iconv_fd" When != -1 did conversion with iconv(). |
| 954 | */ |
| 955 | retry: |
| 956 | |
| 957 | if (file_rewind) |
| 958 | { |
| 959 | if (read_buffer) |
| 960 | { |
| 961 | read_buf_lnum = 1; |
| 962 | read_buf_col = 0; |
| 963 | } |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 964 | else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 965 | { |
| 966 | /* Can't rewind the file, give up. */ |
| 967 | error = TRUE; |
| 968 | goto failed; |
| 969 | } |
| 970 | /* Delete the previously read lines. */ |
| 971 | while (lnum > from) |
| 972 | ml_delete(lnum--, FALSE); |
| 973 | file_rewind = FALSE; |
| 974 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 975 | if (set_options) |
Bram Moolenaar | 83eb885 | 2007-08-12 13:51:26 +0000 | [diff] [blame] | 976 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 977 | curbuf->b_p_bomb = FALSE; |
Bram Moolenaar | 83eb885 | 2007-08-12 13:51:26 +0000 | [diff] [blame] | 978 | curbuf->b_start_bomb = FALSE; |
| 979 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 980 | conv_error = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 981 | #endif |
| 982 | } |
| 983 | |
| 984 | /* |
| 985 | * When retrying with another "fenc" and the first time "fileformat" |
| 986 | * will be reset. |
| 987 | */ |
| 988 | if (keep_fileformat) |
| 989 | keep_fileformat = FALSE; |
| 990 | else |
| 991 | { |
| 992 | if (eap != NULL && eap->force_ff != 0) |
Bram Moolenaar | 1c86036 | 2008-11-12 15:05:21 +0000 | [diff] [blame] | 993 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 994 | fileformat = get_fileformat_force(curbuf, eap); |
Bram Moolenaar | 1c86036 | 2008-11-12 15:05:21 +0000 | [diff] [blame] | 995 | try_unix = try_dos = try_mac = FALSE; |
| 996 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 997 | else if (curbuf->b_p_bin) |
| 998 | fileformat = EOL_UNIX; /* binary: use Unix format */ |
| 999 | else if (*p_ffs == NUL) |
| 1000 | fileformat = get_fileformat(curbuf);/* use format from buffer */ |
| 1001 | else |
| 1002 | fileformat = EOL_UNKNOWN; /* detect from file */ |
| 1003 | } |
| 1004 | |
| 1005 | #ifdef FEAT_MBYTE |
| 1006 | # ifdef USE_ICONV |
| 1007 | if (iconv_fd != (iconv_t)-1) |
| 1008 | { |
| 1009 | /* aborted conversion with iconv(), close the descriptor */ |
| 1010 | iconv_close(iconv_fd); |
| 1011 | iconv_fd = (iconv_t)-1; |
| 1012 | } |
| 1013 | # endif |
| 1014 | |
| 1015 | if (advance_fenc) |
| 1016 | { |
| 1017 | /* |
| 1018 | * Try the next entry in 'fileencodings'. |
| 1019 | */ |
| 1020 | advance_fenc = FALSE; |
| 1021 | |
| 1022 | if (eap != NULL && eap->force_enc != 0) |
| 1023 | { |
| 1024 | /* Conversion given with "++cc=" wasn't possible, read |
| 1025 | * without conversion. */ |
| 1026 | notconverted = TRUE; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1027 | conv_error = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1028 | if (fenc_alloced) |
| 1029 | vim_free(fenc); |
| 1030 | fenc = (char_u *)""; |
| 1031 | fenc_alloced = FALSE; |
| 1032 | } |
| 1033 | else |
| 1034 | { |
| 1035 | if (fenc_alloced) |
| 1036 | vim_free(fenc); |
| 1037 | if (fenc_next != NULL) |
| 1038 | { |
| 1039 | fenc = next_fenc(&fenc_next); |
| 1040 | fenc_alloced = (fenc_next != NULL); |
| 1041 | } |
| 1042 | else |
| 1043 | { |
| 1044 | fenc = (char_u *)""; |
| 1045 | fenc_alloced = FALSE; |
| 1046 | } |
| 1047 | } |
| 1048 | if (tmpname != NULL) |
| 1049 | { |
| 1050 | mch_remove(tmpname); /* delete converted file */ |
| 1051 | vim_free(tmpname); |
| 1052 | tmpname = NULL; |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | /* |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 1057 | * Conversion may be required when the encoding of the file is different |
| 1058 | * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1059 | */ |
| 1060 | fio_flags = 0; |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 1061 | converted = need_conversion(fenc); |
| 1062 | if (converted) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1063 | { |
| 1064 | |
| 1065 | /* "ucs-bom" means we need to check the first bytes of the file |
| 1066 | * for a BOM. */ |
| 1067 | if (STRCMP(fenc, ENC_UCSBOM) == 0) |
| 1068 | fio_flags = FIO_UCSBOM; |
| 1069 | |
| 1070 | /* |
| 1071 | * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be |
| 1072 | * done. This is handled below after read(). Prepare the |
| 1073 | * fio_flags to avoid having to parse the string each time. |
| 1074 | * Also check for Unicode to Latin1 conversion, because iconv() |
| 1075 | * appears not to handle this correctly. This works just like |
| 1076 | * conversion to UTF-8 except how the resulting character is put in |
| 1077 | * the buffer. |
| 1078 | */ |
| 1079 | else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0) |
| 1080 | fio_flags = get_fio_flags(fenc); |
| 1081 | |
| 1082 | # ifdef WIN3264 |
| 1083 | /* |
| 1084 | * Conversion from an MS-Windows codepage to UTF-8 or another codepage |
| 1085 | * is handled with MultiByteToWideChar(). |
| 1086 | */ |
| 1087 | if (fio_flags == 0) |
| 1088 | fio_flags = get_win_fio_flags(fenc); |
| 1089 | # endif |
| 1090 | |
| 1091 | # ifdef MACOS_X |
| 1092 | /* Conversion from Apple MacRoman to latin1 or UTF-8 */ |
| 1093 | if (fio_flags == 0) |
| 1094 | fio_flags = get_mac_fio_flags(fenc); |
| 1095 | # endif |
| 1096 | |
| 1097 | # ifdef USE_ICONV |
| 1098 | /* |
| 1099 | * Try using iconv() if we can't convert internally. |
| 1100 | */ |
| 1101 | if (fio_flags == 0 |
| 1102 | # ifdef FEAT_EVAL |
| 1103 | && !did_iconv |
| 1104 | # endif |
| 1105 | ) |
| 1106 | iconv_fd = (iconv_t)my_iconv_open( |
| 1107 | enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc); |
| 1108 | # endif |
| 1109 | |
| 1110 | # ifdef FEAT_EVAL |
| 1111 | /* |
| 1112 | * Use the 'charconvert' expression when conversion is required |
| 1113 | * and we can't do it internally or with iconv(). |
| 1114 | */ |
| 1115 | if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL |
Bram Moolenaar | f71d7b9 | 2016-08-09 22:14:05 +0200 | [diff] [blame] | 1116 | && !read_fifo |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1117 | # ifdef USE_ICONV |
| 1118 | && iconv_fd == (iconv_t)-1 |
| 1119 | # endif |
| 1120 | ) |
| 1121 | { |
| 1122 | # ifdef USE_ICONV |
| 1123 | did_iconv = FALSE; |
| 1124 | # endif |
| 1125 | /* Skip conversion when it's already done (retry for wrong |
| 1126 | * "fileformat"). */ |
| 1127 | if (tmpname == NULL) |
| 1128 | { |
| 1129 | tmpname = readfile_charconvert(fname, fenc, &fd); |
| 1130 | if (tmpname == NULL) |
| 1131 | { |
| 1132 | /* Conversion failed. Try another one. */ |
| 1133 | advance_fenc = TRUE; |
| 1134 | if (fd < 0) |
| 1135 | { |
| 1136 | /* Re-opening the original file failed! */ |
| 1137 | EMSG(_("E202: Conversion made file unreadable!")); |
| 1138 | error = TRUE; |
| 1139 | goto failed; |
| 1140 | } |
| 1141 | goto retry; |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | else |
| 1146 | # endif |
| 1147 | { |
| 1148 | if (fio_flags == 0 |
| 1149 | # ifdef USE_ICONV |
| 1150 | && iconv_fd == (iconv_t)-1 |
| 1151 | # endif |
| 1152 | ) |
| 1153 | { |
| 1154 | /* Conversion wanted but we can't. |
| 1155 | * Try the next conversion in 'fileencodings' */ |
| 1156 | advance_fenc = TRUE; |
| 1157 | goto retry; |
| 1158 | } |
| 1159 | } |
| 1160 | } |
| 1161 | |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1162 | /* Set "can_retry" when it's possible to rewind the file and try with |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1163 | * another "fenc" value. It's FALSE when no other "fenc" to try, reading |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1164 | * stdin or fixed at a specific encoding. */ |
Bram Moolenaar | f71d7b9 | 2016-08-09 22:14:05 +0200 | [diff] [blame] | 1165 | can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1166 | #endif |
| 1167 | |
| 1168 | if (!skip_read) |
| 1169 | { |
| 1170 | linerest = 0; |
| 1171 | filesize = 0; |
| 1172 | skip_count = lines_to_skip; |
| 1173 | read_count = lines_to_read; |
| 1174 | #ifdef FEAT_MBYTE |
| 1175 | conv_restlen = 0; |
| 1176 | #endif |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 1177 | #ifdef FEAT_PERSISTENT_UNDO |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 1178 | read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0 |
| 1179 | && curbuf->b_ffname != NULL |
| 1180 | && curbuf->b_p_udf |
| 1181 | && !filtering |
Bram Moolenaar | f71d7b9 | 2016-08-09 22:14:05 +0200 | [diff] [blame] | 1182 | && !read_fifo |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 1183 | && !read_stdin |
| 1184 | && !read_buffer); |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 1185 | if (read_undo_file) |
| 1186 | sha256_start(&sha_ctx); |
| 1187 | #endif |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 1188 | #ifdef FEAT_CRYPT |
| 1189 | if (curbuf->b_cryptstate != NULL) |
| 1190 | { |
| 1191 | /* Need to free the state, but keep the key, don't want to ask for |
| 1192 | * it again. */ |
| 1193 | crypt_free_state(curbuf->b_cryptstate); |
| 1194 | curbuf->b_cryptstate = NULL; |
| 1195 | } |
| 1196 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | while (!error && !got_int) |
| 1200 | { |
| 1201 | /* |
| 1202 | * We allocate as much space for the file as we can get, plus |
| 1203 | * space for the old line plus room for one terminating NUL. |
| 1204 | * The amount is limited by the fact that read() only can read |
| 1205 | * upto max_unsigned characters (and other things). |
| 1206 | */ |
Bram Moolenaar | a2aa31a | 2014-02-23 22:52:40 +0100 | [diff] [blame] | 1207 | #if VIM_SIZEOF_INT <= 2 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1208 | if (linerest >= 0x7ff0) |
| 1209 | { |
| 1210 | ++split; |
| 1211 | *ptr = NL; /* split line by inserting a NL */ |
| 1212 | size = 1; |
| 1213 | } |
| 1214 | else |
| 1215 | #endif |
| 1216 | { |
| 1217 | if (!skip_read) |
| 1218 | { |
Bram Moolenaar | a2aa31a | 2014-02-23 22:52:40 +0100 | [diff] [blame] | 1219 | #if VIM_SIZEOF_INT > 2 |
Bram Moolenaar | 311d982 | 2007-02-27 15:48:28 +0000 | [diff] [blame] | 1220 | # if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1221 | size = SSIZE_MAX; /* use max I/O size, 52K */ |
| 1222 | # else |
| 1223 | size = 0x10000L; /* use buffer >= 64K */ |
| 1224 | # endif |
| 1225 | #else |
| 1226 | size = 0x7ff0L - linerest; /* limit buffer to 32K */ |
| 1227 | #endif |
| 1228 | |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 1229 | for ( ; size >= 10; size = (long)((long_u)size >> 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1230 | { |
| 1231 | if ((new_buffer = lalloc((long_u)(size + linerest + 1), |
| 1232 | FALSE)) != NULL) |
| 1233 | break; |
| 1234 | } |
| 1235 | if (new_buffer == NULL) |
| 1236 | { |
| 1237 | do_outofmem_msg((long_u)(size * 2 + linerest + 1)); |
| 1238 | error = TRUE; |
| 1239 | break; |
| 1240 | } |
| 1241 | if (linerest) /* copy characters from the previous buffer */ |
| 1242 | mch_memmove(new_buffer, ptr - linerest, (size_t)linerest); |
| 1243 | vim_free(buffer); |
| 1244 | buffer = new_buffer; |
| 1245 | ptr = buffer + linerest; |
| 1246 | line_start = buffer; |
| 1247 | |
| 1248 | #ifdef FEAT_MBYTE |
| 1249 | /* May need room to translate into. |
| 1250 | * For iconv() we don't really know the required space, use a |
| 1251 | * factor ICONV_MULT. |
| 1252 | * latin1 to utf-8: 1 byte becomes up to 2 bytes |
| 1253 | * utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes |
| 1254 | * become up to 4 bytes, size must be multiple of 2 |
| 1255 | * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be |
| 1256 | * multiple of 2 |
| 1257 | * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be |
| 1258 | * multiple of 4 */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1259 | real_size = (int)size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1260 | # ifdef USE_ICONV |
| 1261 | if (iconv_fd != (iconv_t)-1) |
| 1262 | size = size / ICONV_MULT; |
| 1263 | else |
| 1264 | # endif |
| 1265 | if (fio_flags & FIO_LATIN1) |
| 1266 | size = size / 2; |
| 1267 | else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) |
| 1268 | size = (size * 2 / 3) & ~1; |
| 1269 | else if (fio_flags & FIO_UCS4) |
| 1270 | size = (size * 2 / 3) & ~3; |
| 1271 | else if (fio_flags == FIO_UCSBOM) |
| 1272 | size = size / ICONV_MULT; /* worst case */ |
| 1273 | # ifdef WIN3264 |
| 1274 | else if (fio_flags & FIO_CODEPAGE) |
| 1275 | size = size / ICONV_MULT; /* also worst case */ |
| 1276 | # endif |
| 1277 | # ifdef MACOS_X |
| 1278 | else if (fio_flags & FIO_MACROMAN) |
| 1279 | size = size / ICONV_MULT; /* also worst case */ |
| 1280 | # endif |
| 1281 | #endif |
| 1282 | |
| 1283 | #ifdef FEAT_MBYTE |
| 1284 | if (conv_restlen > 0) |
| 1285 | { |
| 1286 | /* Insert unconverted bytes from previous line. */ |
| 1287 | mch_memmove(ptr, conv_rest, conv_restlen); |
| 1288 | ptr += conv_restlen; |
| 1289 | size -= conv_restlen; |
| 1290 | } |
| 1291 | #endif |
| 1292 | |
| 1293 | if (read_buffer) |
| 1294 | { |
| 1295 | /* |
| 1296 | * Read bytes from curbuf. Used for converting text read |
| 1297 | * from stdin. |
| 1298 | */ |
| 1299 | if (read_buf_lnum > from) |
| 1300 | size = 0; |
| 1301 | else |
| 1302 | { |
| 1303 | int n, ni; |
| 1304 | long tlen; |
| 1305 | |
| 1306 | tlen = 0; |
| 1307 | for (;;) |
| 1308 | { |
| 1309 | p = ml_get(read_buf_lnum) + read_buf_col; |
| 1310 | n = (int)STRLEN(p); |
| 1311 | if ((int)tlen + n + 1 > size) |
| 1312 | { |
| 1313 | /* Filled up to "size", append partial line. |
| 1314 | * Change NL to NUL to reverse the effect done |
| 1315 | * below. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1316 | n = (int)(size - tlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1317 | for (ni = 0; ni < n; ++ni) |
| 1318 | { |
| 1319 | if (p[ni] == NL) |
| 1320 | ptr[tlen++] = NUL; |
| 1321 | else |
| 1322 | ptr[tlen++] = p[ni]; |
| 1323 | } |
| 1324 | read_buf_col += n; |
| 1325 | break; |
| 1326 | } |
| 1327 | else |
| 1328 | { |
| 1329 | /* Append whole line and new-line. Change NL |
| 1330 | * to NUL to reverse the effect done below. */ |
| 1331 | for (ni = 0; ni < n; ++ni) |
| 1332 | { |
| 1333 | if (p[ni] == NL) |
| 1334 | ptr[tlen++] = NUL; |
| 1335 | else |
| 1336 | ptr[tlen++] = p[ni]; |
| 1337 | } |
| 1338 | ptr[tlen++] = NL; |
| 1339 | read_buf_col = 0; |
| 1340 | if (++read_buf_lnum > from) |
| 1341 | { |
| 1342 | /* When the last line didn't have an |
| 1343 | * end-of-line don't add it now either. */ |
| 1344 | if (!curbuf->b_p_eol) |
| 1345 | --tlen; |
| 1346 | size = tlen; |
| 1347 | break; |
| 1348 | } |
| 1349 | } |
| 1350 | } |
| 1351 | } |
| 1352 | } |
| 1353 | else |
| 1354 | { |
| 1355 | /* |
| 1356 | * Read bytes from the file. |
| 1357 | */ |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 1358 | size = read_eintr(fd, ptr, size); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 1361 | #ifdef FEAT_CRYPT |
| 1362 | /* |
| 1363 | * At start of file: Check for magic number of encryption. |
| 1364 | */ |
| 1365 | if (filesize == 0 && size > 0) |
| 1366 | cryptkey = check_for_cryptkey(cryptkey, ptr, &size, |
| 1367 | &filesize, newfile, sfname, |
| 1368 | &did_ask_for_key); |
| 1369 | /* |
| 1370 | * Decrypt the read bytes. This is done before checking for |
| 1371 | * EOF because the crypt layer may be buffering. |
| 1372 | */ |
| 1373 | if (cryptkey != NULL && size > 0) |
| 1374 | { |
| 1375 | if (crypt_works_inplace(curbuf->b_cryptstate)) |
| 1376 | { |
| 1377 | crypt_decode_inplace(curbuf->b_cryptstate, ptr, size); |
| 1378 | } |
| 1379 | else |
| 1380 | { |
| 1381 | char_u *newptr = NULL; |
| 1382 | int decrypted_size; |
| 1383 | |
| 1384 | decrypted_size = crypt_decode_alloc( |
| 1385 | curbuf->b_cryptstate, ptr, size, &newptr); |
| 1386 | |
| 1387 | /* If the crypt layer is buffering, not producing |
| 1388 | * anything yet, need to read more. */ |
| 1389 | if (size > 0 && decrypted_size == 0) |
| 1390 | continue; |
| 1391 | |
| 1392 | if (linerest == 0) |
| 1393 | { |
| 1394 | /* Simple case: reuse returned buffer (may be |
| 1395 | * NULL, checked later). */ |
| 1396 | new_buffer = newptr; |
| 1397 | } |
| 1398 | else |
| 1399 | { |
| 1400 | long_u new_size; |
| 1401 | |
| 1402 | /* Need new buffer to add bytes carried over. */ |
| 1403 | new_size = (long_u)(decrypted_size + linerest + 1); |
| 1404 | new_buffer = lalloc(new_size, FALSE); |
| 1405 | if (new_buffer == NULL) |
| 1406 | { |
| 1407 | do_outofmem_msg(new_size); |
| 1408 | error = TRUE; |
| 1409 | break; |
| 1410 | } |
| 1411 | |
| 1412 | mch_memmove(new_buffer, buffer, linerest); |
| 1413 | if (newptr != NULL) |
| 1414 | mch_memmove(new_buffer + linerest, newptr, |
| 1415 | decrypted_size); |
| 1416 | } |
| 1417 | |
| 1418 | if (new_buffer != NULL) |
| 1419 | { |
| 1420 | vim_free(buffer); |
| 1421 | buffer = new_buffer; |
| 1422 | new_buffer = NULL; |
| 1423 | line_start = buffer; |
| 1424 | ptr = buffer + linerest; |
| 1425 | } |
| 1426 | size = decrypted_size; |
| 1427 | } |
| 1428 | } |
| 1429 | #endif |
| 1430 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1431 | if (size <= 0) |
| 1432 | { |
| 1433 | if (size < 0) /* read error */ |
| 1434 | error = TRUE; |
| 1435 | #ifdef FEAT_MBYTE |
| 1436 | else if (conv_restlen > 0) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1437 | { |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1438 | /* |
| 1439 | * Reached end-of-file but some trailing bytes could |
| 1440 | * not be converted. Truncated file? |
| 1441 | */ |
| 1442 | |
| 1443 | /* When we did a conversion report an error. */ |
| 1444 | if (fio_flags != 0 |
| 1445 | # ifdef USE_ICONV |
| 1446 | || iconv_fd != (iconv_t)-1 |
| 1447 | # endif |
| 1448 | ) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1449 | { |
Bram Moolenaar | e8d9530 | 2013-04-24 16:34:02 +0200 | [diff] [blame] | 1450 | if (can_retry) |
| 1451 | goto rewind_retry; |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1452 | if (conv_error == 0) |
| 1453 | conv_error = curbuf->b_ml.ml_line_count |
| 1454 | - linecnt + 1; |
| 1455 | } |
| 1456 | /* Remember the first linenr with an illegal byte */ |
| 1457 | else if (illegal_byte == 0) |
| 1458 | illegal_byte = curbuf->b_ml.ml_line_count |
| 1459 | - linecnt + 1; |
| 1460 | if (bad_char_behavior == BAD_DROP) |
| 1461 | { |
| 1462 | *(ptr - conv_restlen) = NUL; |
| 1463 | conv_restlen = 0; |
| 1464 | } |
| 1465 | else |
| 1466 | { |
| 1467 | /* Replace the trailing bytes with the replacement |
| 1468 | * character if we were converting; if we weren't, |
| 1469 | * leave the UTF8 checking code to do it, as it |
| 1470 | * works slightly differently. */ |
| 1471 | if (bad_char_behavior != BAD_KEEP && (fio_flags != 0 |
| 1472 | # ifdef USE_ICONV |
| 1473 | || iconv_fd != (iconv_t)-1 |
| 1474 | # endif |
| 1475 | )) |
| 1476 | { |
| 1477 | while (conv_restlen > 0) |
| 1478 | { |
| 1479 | *(--ptr) = bad_char_behavior; |
| 1480 | --conv_restlen; |
| 1481 | } |
| 1482 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1483 | fio_flags = 0; /* don't convert this */ |
Bram Moolenaar | b21e584 | 2006-04-16 18:30:08 +0000 | [diff] [blame] | 1484 | # ifdef USE_ICONV |
| 1485 | if (iconv_fd != (iconv_t)-1) |
| 1486 | { |
| 1487 | iconv_close(iconv_fd); |
| 1488 | iconv_fd = (iconv_t)-1; |
| 1489 | } |
| 1490 | # endif |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1491 | } |
| 1492 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1493 | #endif |
| 1494 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1495 | } |
| 1496 | skip_read = FALSE; |
| 1497 | |
| 1498 | #ifdef FEAT_MBYTE |
| 1499 | /* |
| 1500 | * At start of file (or after crypt magic number): Check for BOM. |
| 1501 | * Also check for a BOM for other Unicode encodings, but not after |
| 1502 | * converting with 'charconvert' or when a BOM has already been |
| 1503 | * found. |
| 1504 | */ |
| 1505 | if ((filesize == 0 |
| 1506 | # ifdef FEAT_CRYPT |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 1507 | || (cryptkey != NULL |
| 1508 | && filesize == crypt_get_header_len( |
| 1509 | crypt_get_method_nr(curbuf))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1510 | # endif |
| 1511 | ) |
| 1512 | && (fio_flags == FIO_UCSBOM |
| 1513 | || (!curbuf->b_p_bomb |
| 1514 | && tmpname == NULL |
| 1515 | && (*fenc == 'u' || (*fenc == NUL && enc_utf8))))) |
| 1516 | { |
| 1517 | char_u *ccname; |
| 1518 | int blen; |
| 1519 | |
| 1520 | /* no BOM detection in a short file or in binary mode */ |
| 1521 | if (size < 2 || curbuf->b_p_bin) |
| 1522 | ccname = NULL; |
| 1523 | else |
| 1524 | ccname = check_for_bom(ptr, size, &blen, |
| 1525 | fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc)); |
| 1526 | if (ccname != NULL) |
| 1527 | { |
| 1528 | /* Remove BOM from the text */ |
| 1529 | filesize += blen; |
| 1530 | size -= blen; |
| 1531 | mch_memmove(ptr, ptr + blen, (size_t)size); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 1532 | if (set_options) |
Bram Moolenaar | 83eb885 | 2007-08-12 13:51:26 +0000 | [diff] [blame] | 1533 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1534 | curbuf->b_p_bomb = TRUE; |
Bram Moolenaar | 83eb885 | 2007-08-12 13:51:26 +0000 | [diff] [blame] | 1535 | curbuf->b_start_bomb = TRUE; |
| 1536 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | if (fio_flags == FIO_UCSBOM) |
| 1540 | { |
| 1541 | if (ccname == NULL) |
| 1542 | { |
| 1543 | /* No BOM detected: retry with next encoding. */ |
| 1544 | advance_fenc = TRUE; |
| 1545 | } |
| 1546 | else |
| 1547 | { |
| 1548 | /* BOM detected: set "fenc" and jump back */ |
| 1549 | if (fenc_alloced) |
| 1550 | vim_free(fenc); |
| 1551 | fenc = ccname; |
| 1552 | fenc_alloced = FALSE; |
| 1553 | } |
| 1554 | /* retry reading without getting new bytes or rewinding */ |
| 1555 | skip_read = TRUE; |
| 1556 | goto retry; |
| 1557 | } |
| 1558 | } |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1559 | |
| 1560 | /* Include not converted bytes. */ |
| 1561 | ptr -= conv_restlen; |
| 1562 | size += conv_restlen; |
| 1563 | conv_restlen = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1564 | #endif |
| 1565 | /* |
| 1566 | * Break here for a read error or end-of-file. |
| 1567 | */ |
| 1568 | if (size <= 0) |
| 1569 | break; |
| 1570 | |
| 1571 | #ifdef FEAT_MBYTE |
| 1572 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1573 | # ifdef USE_ICONV |
| 1574 | if (iconv_fd != (iconv_t)-1) |
| 1575 | { |
| 1576 | /* |
| 1577 | * Attempt conversion of the read bytes to 'encoding' using |
| 1578 | * iconv(). |
| 1579 | */ |
| 1580 | const char *fromp; |
| 1581 | char *top; |
| 1582 | size_t from_size; |
| 1583 | size_t to_size; |
| 1584 | |
| 1585 | fromp = (char *)ptr; |
| 1586 | from_size = size; |
| 1587 | ptr += size; |
| 1588 | top = (char *)ptr; |
| 1589 | to_size = real_size - size; |
| 1590 | |
| 1591 | /* |
| 1592 | * If there is conversion error or not enough room try using |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 1593 | * another conversion. Except for when there is no |
| 1594 | * alternative (help files). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1595 | */ |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 1596 | while ((iconv(iconv_fd, (void *)&fromp, &from_size, |
| 1597 | &top, &to_size) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1598 | == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) |
| 1599 | || from_size > CONV_RESTLEN) |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 1600 | { |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1601 | if (can_retry) |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 1602 | goto rewind_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1603 | if (conv_error == 0) |
| 1604 | conv_error = readfile_linenr(linecnt, |
| 1605 | ptr, (char_u *)top); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1606 | |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1607 | /* Deal with a bad byte and continue with the next. */ |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 1608 | ++fromp; |
| 1609 | --from_size; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1610 | if (bad_char_behavior == BAD_KEEP) |
| 1611 | { |
| 1612 | *top++ = *(fromp - 1); |
| 1613 | --to_size; |
| 1614 | } |
| 1615 | else if (bad_char_behavior != BAD_DROP) |
| 1616 | { |
| 1617 | *top++ = bad_char_behavior; |
| 1618 | --to_size; |
| 1619 | } |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 1620 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1621 | |
| 1622 | if (from_size > 0) |
| 1623 | { |
| 1624 | /* Some remaining characters, keep them for the next |
| 1625 | * round. */ |
| 1626 | mch_memmove(conv_rest, (char_u *)fromp, from_size); |
| 1627 | conv_restlen = (int)from_size; |
| 1628 | } |
| 1629 | |
| 1630 | /* move the linerest to before the converted characters */ |
| 1631 | line_start = ptr - linerest; |
| 1632 | mch_memmove(line_start, buffer, (size_t)linerest); |
| 1633 | size = (long)((char_u *)top - ptr); |
| 1634 | } |
| 1635 | # endif |
| 1636 | |
| 1637 | # ifdef WIN3264 |
| 1638 | if (fio_flags & FIO_CODEPAGE) |
| 1639 | { |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1640 | char_u *src, *dst; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1641 | WCHAR ucs2buf[3]; |
| 1642 | int ucs2len; |
| 1643 | int codepage = FIO_GET_CP(fio_flags); |
| 1644 | int bytelen; |
| 1645 | int found_bad; |
| 1646 | char replstr[2]; |
| 1647 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1648 | /* |
| 1649 | * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1650 | * a codepage, using standard MS-Windows functions. This |
| 1651 | * requires two steps: |
| 1652 | * 1. convert from 'fileencoding' to ucs-2 |
| 1653 | * 2. convert from ucs-2 to 'encoding' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1654 | * |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1655 | * Because there may be illegal bytes AND an incomplete byte |
| 1656 | * sequence at the end, we may have to do the conversion one |
| 1657 | * character at a time to get it right. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1658 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1659 | |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1660 | /* Replacement string for WideCharToMultiByte(). */ |
| 1661 | if (bad_char_behavior > 0) |
| 1662 | replstr[0] = bad_char_behavior; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1663 | else |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1664 | replstr[0] = '?'; |
| 1665 | replstr[1] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1666 | |
| 1667 | /* |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1668 | * Move the bytes to the end of the buffer, so that we have |
| 1669 | * room to put the result at the start. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1670 | */ |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1671 | src = ptr + real_size - size; |
| 1672 | mch_memmove(src, ptr, size); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1673 | |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1674 | /* |
| 1675 | * Do the conversion. |
| 1676 | */ |
| 1677 | dst = ptr; |
| 1678 | size = size; |
| 1679 | while (size > 0) |
| 1680 | { |
| 1681 | found_bad = FALSE; |
| 1682 | |
| 1683 | # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ |
| 1684 | if (codepage == CP_UTF8) |
| 1685 | { |
| 1686 | /* Handle CP_UTF8 input ourselves to be able to handle |
| 1687 | * trailing bytes properly. |
| 1688 | * Get one UTF-8 character from src. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1689 | bytelen = (int)utf_ptr2len_len(src, size); |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1690 | if (bytelen > size) |
| 1691 | { |
| 1692 | /* Only got some bytes of a character. Normally |
| 1693 | * it's put in "conv_rest", but if it's too long |
| 1694 | * deal with it as if they were illegal bytes. */ |
| 1695 | if (bytelen <= CONV_RESTLEN) |
| 1696 | break; |
| 1697 | |
| 1698 | /* weird overlong byte sequence */ |
| 1699 | bytelen = size; |
| 1700 | found_bad = TRUE; |
| 1701 | } |
| 1702 | else |
| 1703 | { |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 1704 | int u8c = utf_ptr2char(src); |
| 1705 | |
Bram Moolenaar | 86e0108 | 2005-12-29 22:45:34 +0000 | [diff] [blame] | 1706 | if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1)) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1707 | found_bad = TRUE; |
| 1708 | ucs2buf[0] = u8c; |
| 1709 | ucs2len = 1; |
| 1710 | } |
| 1711 | } |
| 1712 | else |
| 1713 | # endif |
| 1714 | { |
| 1715 | /* We don't know how long the byte sequence is, try |
| 1716 | * from one to three bytes. */ |
| 1717 | for (bytelen = 1; bytelen <= size && bytelen <= 3; |
| 1718 | ++bytelen) |
| 1719 | { |
| 1720 | ucs2len = MultiByteToWideChar(codepage, |
| 1721 | MB_ERR_INVALID_CHARS, |
| 1722 | (LPCSTR)src, bytelen, |
| 1723 | ucs2buf, 3); |
| 1724 | if (ucs2len > 0) |
| 1725 | break; |
| 1726 | } |
| 1727 | if (ucs2len == 0) |
| 1728 | { |
| 1729 | /* If we have only one byte then it's probably an |
| 1730 | * incomplete byte sequence. Otherwise discard |
| 1731 | * one byte as a bad character. */ |
| 1732 | if (size == 1) |
| 1733 | break; |
| 1734 | found_bad = TRUE; |
| 1735 | bytelen = 1; |
| 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | if (!found_bad) |
| 1740 | { |
| 1741 | int i; |
| 1742 | |
| 1743 | /* Convert "ucs2buf[ucs2len]" to 'enc' in "dst". */ |
| 1744 | if (enc_utf8) |
| 1745 | { |
| 1746 | /* From UCS-2 to UTF-8. Cannot fail. */ |
| 1747 | for (i = 0; i < ucs2len; ++i) |
| 1748 | dst += utf_char2bytes(ucs2buf[i], dst); |
| 1749 | } |
| 1750 | else |
| 1751 | { |
| 1752 | BOOL bad = FALSE; |
| 1753 | int dstlen; |
| 1754 | |
| 1755 | /* From UCS-2 to "enc_codepage". If the |
| 1756 | * conversion uses the default character "?", |
| 1757 | * the data doesn't fit in this encoding. */ |
| 1758 | dstlen = WideCharToMultiByte(enc_codepage, 0, |
| 1759 | (LPCWSTR)ucs2buf, ucs2len, |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1760 | (LPSTR)dst, (int)(src - dst), |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1761 | replstr, &bad); |
| 1762 | if (bad) |
| 1763 | found_bad = TRUE; |
| 1764 | else |
| 1765 | dst += dstlen; |
| 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | if (found_bad) |
| 1770 | { |
| 1771 | /* Deal with bytes we can't convert. */ |
| 1772 | if (can_retry) |
| 1773 | goto rewind_retry; |
| 1774 | if (conv_error == 0) |
| 1775 | conv_error = readfile_linenr(linecnt, ptr, dst); |
| 1776 | if (bad_char_behavior != BAD_DROP) |
| 1777 | { |
| 1778 | if (bad_char_behavior == BAD_KEEP) |
| 1779 | { |
| 1780 | mch_memmove(dst, src, bytelen); |
| 1781 | dst += bytelen; |
| 1782 | } |
| 1783 | else |
| 1784 | *dst++ = bad_char_behavior; |
| 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | src += bytelen; |
| 1789 | size -= bytelen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1790 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1791 | |
| 1792 | if (size > 0) |
| 1793 | { |
| 1794 | /* An incomplete byte sequence remaining. */ |
| 1795 | mch_memmove(conv_rest, src, size); |
| 1796 | conv_restlen = size; |
| 1797 | } |
| 1798 | |
| 1799 | /* The new size is equal to how much "dst" was advanced. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1800 | size = (long)(dst - ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1801 | } |
| 1802 | else |
| 1803 | # endif |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 1804 | # ifdef MACOS_CONVERT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1805 | if (fio_flags & FIO_MACROMAN) |
| 1806 | { |
| 1807 | /* |
| 1808 | * Conversion from Apple MacRoman char encoding to UTF-8 or |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 1809 | * latin1. This is in os_mac_conv.c. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1810 | */ |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 1811 | if (macroman2enc(ptr, &size, real_size) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1812 | goto rewind_retry; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1813 | } |
| 1814 | else |
| 1815 | # endif |
| 1816 | if (fio_flags != 0) |
| 1817 | { |
| 1818 | int u8c; |
| 1819 | char_u *dest; |
| 1820 | char_u *tail = NULL; |
| 1821 | |
| 1822 | /* |
| 1823 | * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8. |
| 1824 | * "enc_utf8" not set: Convert Unicode to Latin1. |
| 1825 | * Go from end to start through the buffer, because the number |
| 1826 | * of bytes may increase. |
| 1827 | * "dest" points to after where the UTF-8 bytes go, "p" points |
| 1828 | * to after the next character to convert. |
| 1829 | */ |
| 1830 | dest = ptr + real_size; |
| 1831 | if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8) |
| 1832 | { |
| 1833 | p = ptr + size; |
| 1834 | if (fio_flags == FIO_UTF8) |
| 1835 | { |
| 1836 | /* Check for a trailing incomplete UTF-8 sequence */ |
| 1837 | tail = ptr + size - 1; |
| 1838 | while (tail > ptr && (*tail & 0xc0) == 0x80) |
| 1839 | --tail; |
| 1840 | if (tail + utf_byte2len(*tail) <= ptr + size) |
| 1841 | tail = NULL; |
| 1842 | else |
| 1843 | p = tail; |
| 1844 | } |
| 1845 | } |
| 1846 | else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) |
| 1847 | { |
| 1848 | /* Check for a trailing byte */ |
| 1849 | p = ptr + (size & ~1); |
| 1850 | if (size & 1) |
| 1851 | tail = p; |
| 1852 | if ((fio_flags & FIO_UTF16) && p > ptr) |
| 1853 | { |
| 1854 | /* Check for a trailing leading word */ |
| 1855 | if (fio_flags & FIO_ENDIAN_L) |
| 1856 | { |
| 1857 | u8c = (*--p << 8); |
| 1858 | u8c += *--p; |
| 1859 | } |
| 1860 | else |
| 1861 | { |
| 1862 | u8c = *--p; |
| 1863 | u8c += (*--p << 8); |
| 1864 | } |
| 1865 | if (u8c >= 0xd800 && u8c <= 0xdbff) |
| 1866 | tail = p; |
| 1867 | else |
| 1868 | p += 2; |
| 1869 | } |
| 1870 | } |
| 1871 | else /* FIO_UCS4 */ |
| 1872 | { |
| 1873 | /* Check for trailing 1, 2 or 3 bytes */ |
| 1874 | p = ptr + (size & ~3); |
| 1875 | if (size & 3) |
| 1876 | tail = p; |
| 1877 | } |
| 1878 | |
| 1879 | /* If there is a trailing incomplete sequence move it to |
| 1880 | * conv_rest[]. */ |
| 1881 | if (tail != NULL) |
| 1882 | { |
| 1883 | conv_restlen = (int)((ptr + size) - tail); |
| 1884 | mch_memmove(conv_rest, (char_u *)tail, conv_restlen); |
| 1885 | size -= conv_restlen; |
| 1886 | } |
| 1887 | |
| 1888 | |
| 1889 | while (p > ptr) |
| 1890 | { |
| 1891 | if (fio_flags & FIO_LATIN1) |
| 1892 | u8c = *--p; |
| 1893 | else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) |
| 1894 | { |
| 1895 | if (fio_flags & FIO_ENDIAN_L) |
| 1896 | { |
| 1897 | u8c = (*--p << 8); |
| 1898 | u8c += *--p; |
| 1899 | } |
| 1900 | else |
| 1901 | { |
| 1902 | u8c = *--p; |
| 1903 | u8c += (*--p << 8); |
| 1904 | } |
| 1905 | if ((fio_flags & FIO_UTF16) |
| 1906 | && u8c >= 0xdc00 && u8c <= 0xdfff) |
| 1907 | { |
| 1908 | int u16c; |
| 1909 | |
| 1910 | if (p == ptr) |
| 1911 | { |
| 1912 | /* Missing leading word. */ |
| 1913 | if (can_retry) |
| 1914 | goto rewind_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1915 | if (conv_error == 0) |
| 1916 | conv_error = readfile_linenr(linecnt, |
| 1917 | ptr, p); |
| 1918 | if (bad_char_behavior == BAD_DROP) |
| 1919 | continue; |
| 1920 | if (bad_char_behavior != BAD_KEEP) |
| 1921 | u8c = bad_char_behavior; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | /* found second word of double-word, get the first |
| 1925 | * word and compute the resulting character */ |
| 1926 | if (fio_flags & FIO_ENDIAN_L) |
| 1927 | { |
| 1928 | u16c = (*--p << 8); |
| 1929 | u16c += *--p; |
| 1930 | } |
| 1931 | else |
| 1932 | { |
| 1933 | u16c = *--p; |
| 1934 | u16c += (*--p << 8); |
| 1935 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1936 | u8c = 0x10000 + ((u16c & 0x3ff) << 10) |
| 1937 | + (u8c & 0x3ff); |
| 1938 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1939 | /* Check if the word is indeed a leading word. */ |
| 1940 | if (u16c < 0xd800 || u16c > 0xdbff) |
| 1941 | { |
| 1942 | if (can_retry) |
| 1943 | goto rewind_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1944 | if (conv_error == 0) |
| 1945 | conv_error = readfile_linenr(linecnt, |
| 1946 | ptr, p); |
| 1947 | if (bad_char_behavior == BAD_DROP) |
| 1948 | continue; |
| 1949 | if (bad_char_behavior != BAD_KEEP) |
| 1950 | u8c = bad_char_behavior; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1951 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1952 | } |
| 1953 | } |
| 1954 | else if (fio_flags & FIO_UCS4) |
| 1955 | { |
| 1956 | if (fio_flags & FIO_ENDIAN_L) |
| 1957 | { |
| 1958 | u8c = (*--p << 24); |
| 1959 | u8c += (*--p << 16); |
| 1960 | u8c += (*--p << 8); |
| 1961 | u8c += *--p; |
| 1962 | } |
| 1963 | else /* big endian */ |
| 1964 | { |
| 1965 | u8c = *--p; |
| 1966 | u8c += (*--p << 8); |
| 1967 | u8c += (*--p << 16); |
| 1968 | u8c += (*--p << 24); |
| 1969 | } |
| 1970 | } |
| 1971 | else /* UTF-8 */ |
| 1972 | { |
| 1973 | if (*--p < 0x80) |
| 1974 | u8c = *p; |
| 1975 | else |
| 1976 | { |
| 1977 | len = utf_head_off(ptr, p); |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1978 | p -= len; |
| 1979 | u8c = utf_ptr2char(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1980 | if (len == 0) |
| 1981 | { |
| 1982 | /* Not a valid UTF-8 character, retry with |
| 1983 | * another fenc when possible, otherwise just |
| 1984 | * report the error. */ |
| 1985 | if (can_retry) |
| 1986 | goto rewind_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1987 | if (conv_error == 0) |
| 1988 | conv_error = readfile_linenr(linecnt, |
| 1989 | ptr, p); |
| 1990 | if (bad_char_behavior == BAD_DROP) |
| 1991 | continue; |
| 1992 | if (bad_char_behavior != BAD_KEEP) |
| 1993 | u8c = bad_char_behavior; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1994 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1995 | } |
| 1996 | } |
| 1997 | if (enc_utf8) /* produce UTF-8 */ |
| 1998 | { |
| 1999 | dest -= utf_char2len(u8c); |
| 2000 | (void)utf_char2bytes(u8c, dest); |
| 2001 | } |
| 2002 | else /* produce Latin1 */ |
| 2003 | { |
| 2004 | --dest; |
| 2005 | if (u8c >= 0x100) |
| 2006 | { |
| 2007 | /* character doesn't fit in latin1, retry with |
| 2008 | * another fenc when possible, otherwise just |
| 2009 | * report the error. */ |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2010 | if (can_retry) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2011 | goto rewind_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2012 | if (conv_error == 0) |
| 2013 | conv_error = readfile_linenr(linecnt, ptr, p); |
| 2014 | if (bad_char_behavior == BAD_DROP) |
| 2015 | ++dest; |
| 2016 | else if (bad_char_behavior == BAD_KEEP) |
| 2017 | *dest = u8c; |
| 2018 | else if (eap != NULL && eap->bad_char != 0) |
| 2019 | *dest = bad_char_behavior; |
| 2020 | else |
| 2021 | *dest = 0xBF; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2022 | } |
| 2023 | else |
| 2024 | *dest = u8c; |
| 2025 | } |
| 2026 | } |
| 2027 | |
| 2028 | /* move the linerest to before the converted characters */ |
| 2029 | line_start = dest - linerest; |
| 2030 | mch_memmove(line_start, buffer, (size_t)linerest); |
| 2031 | size = (long)((ptr + real_size) - dest); |
| 2032 | ptr = dest; |
| 2033 | } |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2034 | else if (enc_utf8 && !curbuf->b_p_bin) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2035 | { |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2036 | int incomplete_tail = FALSE; |
| 2037 | |
| 2038 | /* Reading UTF-8: Check if the bytes are valid UTF-8. */ |
| 2039 | for (p = ptr; ; ++p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2040 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2041 | int todo = (int)((ptr + size) - p); |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2042 | int l; |
| 2043 | |
| 2044 | if (todo <= 0) |
| 2045 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2046 | if (*p >= 0x80) |
| 2047 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2048 | /* A length of 1 means it's an illegal byte. Accept |
| 2049 | * an incomplete character at the end though, the next |
| 2050 | * read() will get the next bytes, we'll check it |
| 2051 | * then. */ |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2052 | l = utf_ptr2len_len(p, todo); |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2053 | if (l > todo && !incomplete_tail) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2054 | { |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2055 | /* Avoid retrying with a different encoding when |
| 2056 | * a truncated file is more likely, or attempting |
| 2057 | * to read the rest of an incomplete sequence when |
| 2058 | * we have already done so. */ |
| 2059 | if (p > ptr || filesize > 0) |
| 2060 | incomplete_tail = TRUE; |
| 2061 | /* Incomplete byte sequence, move it to conv_rest[] |
| 2062 | * and try to read the rest of it, unless we've |
| 2063 | * already done so. */ |
| 2064 | if (p > ptr) |
| 2065 | { |
| 2066 | conv_restlen = todo; |
| 2067 | mch_memmove(conv_rest, p, conv_restlen); |
| 2068 | size -= conv_restlen; |
| 2069 | break; |
| 2070 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2071 | } |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2072 | if (l == 1 || l > todo) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2073 | { |
| 2074 | /* Illegal byte. If we can try another encoding |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2075 | * do that, unless at EOF where a truncated |
| 2076 | * file is more likely than a conversion error. */ |
| 2077 | if (can_retry && !incomplete_tail) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2078 | break; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2079 | # ifdef USE_ICONV |
| 2080 | /* When we did a conversion report an error. */ |
| 2081 | if (iconv_fd != (iconv_t)-1 && conv_error == 0) |
| 2082 | conv_error = readfile_linenr(linecnt, ptr, p); |
| 2083 | # endif |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2084 | /* Remember the first linenr with an illegal byte */ |
| 2085 | if (conv_error == 0 && illegal_byte == 0) |
| 2086 | illegal_byte = readfile_linenr(linecnt, ptr, p); |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2087 | |
| 2088 | /* Drop, keep or replace the bad byte. */ |
| 2089 | if (bad_char_behavior == BAD_DROP) |
| 2090 | { |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2091 | mch_memmove(p, p + 1, todo - 1); |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2092 | --p; |
| 2093 | --size; |
| 2094 | } |
| 2095 | else if (bad_char_behavior != BAD_KEEP) |
| 2096 | *p = bad_char_behavior; |
| 2097 | } |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2098 | else |
| 2099 | p += l - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2100 | } |
| 2101 | } |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2102 | if (p < ptr + size && !incomplete_tail) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2103 | { |
| 2104 | /* Detected a UTF-8 error. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2105 | rewind_retry: |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2106 | /* Retry reading with another conversion. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2107 | # if defined(FEAT_EVAL) && defined(USE_ICONV) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2108 | if (*p_ccv != NUL && iconv_fd != (iconv_t)-1) |
| 2109 | /* iconv() failed, try 'charconvert' */ |
| 2110 | did_iconv = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2111 | else |
| 2112 | # endif |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2113 | /* use next item from 'fileencodings' */ |
| 2114 | advance_fenc = TRUE; |
| 2115 | file_rewind = TRUE; |
| 2116 | goto retry; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2117 | } |
| 2118 | } |
| 2119 | #endif |
| 2120 | |
| 2121 | /* count the number of characters (after conversion!) */ |
| 2122 | filesize += size; |
| 2123 | |
| 2124 | /* |
| 2125 | * when reading the first part of a file: guess EOL type |
| 2126 | */ |
| 2127 | if (fileformat == EOL_UNKNOWN) |
| 2128 | { |
| 2129 | /* First try finding a NL, for Dos and Unix */ |
| 2130 | if (try_dos || try_unix) |
| 2131 | { |
Bram Moolenaar | c6b7217 | 2015-02-27 17:48:09 +0100 | [diff] [blame] | 2132 | /* Reset the carriage return counter. */ |
| 2133 | if (try_mac) |
| 2134 | try_mac = 1; |
| 2135 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2136 | for (p = ptr; p < ptr + size; ++p) |
| 2137 | { |
| 2138 | if (*p == NL) |
| 2139 | { |
| 2140 | if (!try_unix |
| 2141 | || (try_dos && p > ptr && p[-1] == CAR)) |
| 2142 | fileformat = EOL_DOS; |
| 2143 | else |
| 2144 | fileformat = EOL_UNIX; |
| 2145 | break; |
| 2146 | } |
Bram Moolenaar | 05eb612 | 2015-02-17 14:15:19 +0100 | [diff] [blame] | 2147 | else if (*p == CAR && try_mac) |
| 2148 | try_mac++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | /* Don't give in to EOL_UNIX if EOL_MAC is more likely */ |
| 2152 | if (fileformat == EOL_UNIX && try_mac) |
| 2153 | { |
| 2154 | /* Need to reset the counters when retrying fenc. */ |
| 2155 | try_mac = 1; |
| 2156 | try_unix = 1; |
| 2157 | for (; p >= ptr && *p != CAR; p--) |
| 2158 | ; |
| 2159 | if (p >= ptr) |
| 2160 | { |
| 2161 | for (p = ptr; p < ptr + size; ++p) |
| 2162 | { |
| 2163 | if (*p == NL) |
| 2164 | try_unix++; |
| 2165 | else if (*p == CAR) |
| 2166 | try_mac++; |
| 2167 | } |
| 2168 | if (try_mac > try_unix) |
| 2169 | fileformat = EOL_MAC; |
| 2170 | } |
| 2171 | } |
Bram Moolenaar | 05eb612 | 2015-02-17 14:15:19 +0100 | [diff] [blame] | 2172 | else if (fileformat == EOL_UNKNOWN && try_mac == 1) |
| 2173 | /* Looking for CR but found no end-of-line markers at |
| 2174 | * all: use the default format. */ |
| 2175 | fileformat = default_fileformat(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2176 | } |
| 2177 | |
| 2178 | /* No NL found: may use Mac format */ |
| 2179 | if (fileformat == EOL_UNKNOWN && try_mac) |
| 2180 | fileformat = EOL_MAC; |
| 2181 | |
| 2182 | /* Still nothing found? Use first format in 'ffs' */ |
| 2183 | if (fileformat == EOL_UNKNOWN) |
| 2184 | fileformat = default_fileformat(); |
| 2185 | |
| 2186 | /* if editing a new file: may set p_tx and p_ff */ |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2187 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2188 | set_fileformat(fileformat, OPT_LOCAL); |
| 2189 | } |
| 2190 | } |
| 2191 | |
| 2192 | /* |
| 2193 | * This loop is executed once for every character read. |
| 2194 | * Keep it fast! |
| 2195 | */ |
| 2196 | if (fileformat == EOL_MAC) |
| 2197 | { |
| 2198 | --ptr; |
| 2199 | while (++ptr, --size >= 0) |
| 2200 | { |
| 2201 | /* catch most common case first */ |
| 2202 | if ((c = *ptr) != NUL && c != CAR && c != NL) |
| 2203 | continue; |
| 2204 | if (c == NUL) |
| 2205 | *ptr = NL; /* NULs are replaced by newlines! */ |
| 2206 | else if (c == NL) |
| 2207 | *ptr = CAR; /* NLs are replaced by CRs! */ |
| 2208 | else |
| 2209 | { |
| 2210 | if (skip_count == 0) |
| 2211 | { |
| 2212 | *ptr = NUL; /* end of line */ |
| 2213 | len = (colnr_T) (ptr - line_start + 1); |
| 2214 | if (ml_append(lnum, line_start, len, newfile) == FAIL) |
| 2215 | { |
| 2216 | error = TRUE; |
| 2217 | break; |
| 2218 | } |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2219 | #ifdef FEAT_PERSISTENT_UNDO |
| 2220 | if (read_undo_file) |
| 2221 | sha256_update(&sha_ctx, line_start, len); |
| 2222 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2223 | ++lnum; |
| 2224 | if (--read_count == 0) |
| 2225 | { |
| 2226 | error = TRUE; /* break loop */ |
| 2227 | line_start = ptr; /* nothing left to write */ |
| 2228 | break; |
| 2229 | } |
| 2230 | } |
| 2231 | else |
| 2232 | --skip_count; |
| 2233 | line_start = ptr + 1; |
| 2234 | } |
| 2235 | } |
| 2236 | } |
| 2237 | else |
| 2238 | { |
| 2239 | --ptr; |
| 2240 | while (++ptr, --size >= 0) |
| 2241 | { |
| 2242 | if ((c = *ptr) != NUL && c != NL) /* catch most common case */ |
| 2243 | continue; |
| 2244 | if (c == NUL) |
| 2245 | *ptr = NL; /* NULs are replaced by newlines! */ |
| 2246 | else |
| 2247 | { |
| 2248 | if (skip_count == 0) |
| 2249 | { |
| 2250 | *ptr = NUL; /* end of line */ |
| 2251 | len = (colnr_T)(ptr - line_start + 1); |
| 2252 | if (fileformat == EOL_DOS) |
| 2253 | { |
Bram Moolenaar | 2aa5f69 | 2017-01-24 15:46:48 +0100 | [diff] [blame] | 2254 | if (ptr > line_start && ptr[-1] == CAR) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2255 | { |
Bram Moolenaar | 2aa5f69 | 2017-01-24 15:46:48 +0100 | [diff] [blame] | 2256 | /* remove CR before NL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2257 | ptr[-1] = NUL; |
| 2258 | --len; |
| 2259 | } |
| 2260 | /* |
| 2261 | * Reading in Dos format, but no CR-LF found! |
| 2262 | * When 'fileformats' includes "unix", delete all |
| 2263 | * the lines read so far and start all over again. |
| 2264 | * Otherwise give an error message later. |
| 2265 | */ |
| 2266 | else if (ff_error != EOL_DOS) |
| 2267 | { |
| 2268 | if ( try_unix |
| 2269 | && !read_stdin |
| 2270 | && (read_buffer |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 2271 | || vim_lseek(fd, (off_T)0L, SEEK_SET) |
| 2272 | == 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2273 | { |
| 2274 | fileformat = EOL_UNIX; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2275 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2276 | set_fileformat(EOL_UNIX, OPT_LOCAL); |
| 2277 | file_rewind = TRUE; |
| 2278 | keep_fileformat = TRUE; |
| 2279 | goto retry; |
| 2280 | } |
| 2281 | ff_error = EOL_DOS; |
| 2282 | } |
| 2283 | } |
| 2284 | if (ml_append(lnum, line_start, len, newfile) == FAIL) |
| 2285 | { |
| 2286 | error = TRUE; |
| 2287 | break; |
| 2288 | } |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2289 | #ifdef FEAT_PERSISTENT_UNDO |
| 2290 | if (read_undo_file) |
| 2291 | sha256_update(&sha_ctx, line_start, len); |
| 2292 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2293 | ++lnum; |
| 2294 | if (--read_count == 0) |
| 2295 | { |
| 2296 | error = TRUE; /* break loop */ |
| 2297 | line_start = ptr; /* nothing left to write */ |
| 2298 | break; |
| 2299 | } |
| 2300 | } |
| 2301 | else |
| 2302 | --skip_count; |
| 2303 | line_start = ptr + 1; |
| 2304 | } |
| 2305 | } |
| 2306 | } |
| 2307 | linerest = (long)(ptr - line_start); |
| 2308 | ui_breakcheck(); |
| 2309 | } |
| 2310 | |
| 2311 | failed: |
| 2312 | /* not an error, max. number of lines reached */ |
| 2313 | if (error && read_count == 0) |
| 2314 | error = FALSE; |
| 2315 | |
| 2316 | /* |
| 2317 | * If we get EOF in the middle of a line, note the fact and |
| 2318 | * complete the line ourselves. |
| 2319 | * In Dos format ignore a trailing CTRL-Z, unless 'binary' set. |
| 2320 | */ |
| 2321 | if (!error |
| 2322 | && !got_int |
| 2323 | && linerest != 0 |
| 2324 | && !(!curbuf->b_p_bin |
| 2325 | && fileformat == EOL_DOS |
| 2326 | && *line_start == Ctrl_Z |
| 2327 | && ptr == line_start + 1)) |
| 2328 | { |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2329 | /* remember for when writing */ |
| 2330 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2331 | curbuf->b_p_eol = FALSE; |
| 2332 | *ptr = NUL; |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2333 | len = (colnr_T)(ptr - line_start + 1); |
| 2334 | if (ml_append(lnum, line_start, len, newfile) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2335 | error = TRUE; |
| 2336 | else |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2337 | { |
| 2338 | #ifdef FEAT_PERSISTENT_UNDO |
| 2339 | if (read_undo_file) |
| 2340 | sha256_update(&sha_ctx, line_start, len); |
| 2341 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2342 | read_no_eol_lnum = ++lnum; |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2343 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2344 | } |
| 2345 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2346 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2347 | save_file_ff(curbuf); /* remember the current file format */ |
| 2348 | |
| 2349 | #ifdef FEAT_CRYPT |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 2350 | if (curbuf->b_cryptstate != NULL) |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 2351 | { |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 2352 | crypt_free_state(curbuf->b_cryptstate); |
| 2353 | curbuf->b_cryptstate = NULL; |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 2354 | } |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 2355 | if (cryptkey != NULL && cryptkey != curbuf->b_p_key) |
| 2356 | crypt_free_key(cryptkey); |
| 2357 | /* Don't set cryptkey to NULL, it's used below as a flag that |
| 2358 | * encryption was used. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2359 | #endif |
| 2360 | |
| 2361 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2362 | /* If editing a new file: set 'fenc' for the current buffer. |
| 2363 | * Also for ":read ++edit file". */ |
| 2364 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2365 | set_string_option_direct((char_u *)"fenc", -1, fenc, |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 2366 | OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2367 | if (fenc_alloced) |
| 2368 | vim_free(fenc); |
| 2369 | # ifdef USE_ICONV |
| 2370 | if (iconv_fd != (iconv_t)-1) |
| 2371 | { |
| 2372 | iconv_close(iconv_fd); |
| 2373 | iconv_fd = (iconv_t)-1; |
| 2374 | } |
| 2375 | # endif |
| 2376 | #endif |
| 2377 | |
| 2378 | if (!read_buffer && !read_stdin) |
| 2379 | close(fd); /* errors are ignored */ |
Bram Moolenaar | f05da21 | 2009-11-17 16:13:15 +0000 | [diff] [blame] | 2380 | #ifdef HAVE_FD_CLOEXEC |
| 2381 | else |
| 2382 | { |
| 2383 | int fdflags = fcntl(fd, F_GETFD); |
| 2384 | if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0) |
Bram Moolenaar | fbc4b4d | 2016-02-07 15:14:01 +0100 | [diff] [blame] | 2385 | (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC); |
Bram Moolenaar | f05da21 | 2009-11-17 16:13:15 +0000 | [diff] [blame] | 2386 | } |
| 2387 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2388 | vim_free(buffer); |
| 2389 | |
| 2390 | #ifdef HAVE_DUP |
| 2391 | if (read_stdin) |
| 2392 | { |
| 2393 | /* Use stderr for stdin, makes shell commands work. */ |
| 2394 | close(0); |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 2395 | ignored = dup(2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2396 | } |
| 2397 | #endif |
| 2398 | |
| 2399 | #ifdef FEAT_MBYTE |
| 2400 | if (tmpname != NULL) |
| 2401 | { |
| 2402 | mch_remove(tmpname); /* delete converted file */ |
| 2403 | vim_free(tmpname); |
| 2404 | } |
| 2405 | #endif |
| 2406 | --no_wait_return; /* may wait for return now */ |
| 2407 | |
| 2408 | /* |
| 2409 | * In recovery mode everything but autocommands is skipped. |
| 2410 | */ |
| 2411 | if (!recoverymode) |
| 2412 | { |
| 2413 | /* need to delete the last line, which comes from the empty buffer */ |
| 2414 | if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY)) |
| 2415 | { |
| 2416 | #ifdef FEAT_NETBEANS_INTG |
| 2417 | netbeansFireChanges = 0; |
| 2418 | #endif |
| 2419 | ml_delete(curbuf->b_ml.ml_line_count, FALSE); |
| 2420 | #ifdef FEAT_NETBEANS_INTG |
| 2421 | netbeansFireChanges = 1; |
| 2422 | #endif |
| 2423 | --linecnt; |
| 2424 | } |
| 2425 | linecnt = curbuf->b_ml.ml_line_count - linecnt; |
| 2426 | if (filesize == 0) |
| 2427 | linecnt = 0; |
| 2428 | if (newfile || read_buffer) |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 2429 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2430 | redraw_curbuf_later(NOT_VALID); |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 2431 | #ifdef FEAT_DIFF |
| 2432 | /* After reading the text into the buffer the diff info needs to |
| 2433 | * be updated. */ |
| 2434 | diff_invalidate(curbuf); |
| 2435 | #endif |
| 2436 | #ifdef FEAT_FOLDING |
| 2437 | /* All folds in the window are invalid now. Mark them for update |
| 2438 | * before triggering autocommands. */ |
| 2439 | foldUpdateAll(curwin); |
| 2440 | #endif |
| 2441 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2442 | else if (linecnt) /* appended at least one line */ |
| 2443 | appended_lines_mark(from, linecnt); |
| 2444 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2445 | #ifndef ALWAYS_USE_GUI |
| 2446 | /* |
| 2447 | * If we were reading from the same terminal as where messages go, |
| 2448 | * the screen will have been messed up. |
| 2449 | * Switch on raw mode now and clear the screen. |
| 2450 | */ |
| 2451 | if (read_stdin) |
| 2452 | { |
| 2453 | settmode(TMODE_RAW); /* set to raw mode */ |
| 2454 | starttermcap(); |
| 2455 | screenclear(); |
| 2456 | } |
| 2457 | #endif |
| 2458 | |
| 2459 | if (got_int) |
| 2460 | { |
| 2461 | if (!(flags & READ_DUMMY)) |
| 2462 | { |
| 2463 | filemess(curbuf, sfname, (char_u *)_(e_interr), 0); |
| 2464 | if (newfile) |
| 2465 | curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
| 2466 | } |
| 2467 | msg_scroll = msg_save; |
| 2468 | #ifdef FEAT_VIMINFO |
| 2469 | check_marks_read(); |
| 2470 | #endif |
| 2471 | return OK; /* an interrupt isn't really an error */ |
| 2472 | } |
| 2473 | |
| 2474 | if (!filtering && !(flags & READ_DUMMY)) |
| 2475 | { |
| 2476 | msg_add_fname(curbuf, sfname); /* fname in IObuff with quotes */ |
| 2477 | c = FALSE; |
| 2478 | |
| 2479 | #ifdef UNIX |
| 2480 | # ifdef S_ISFIFO |
| 2481 | if (S_ISFIFO(perm)) /* fifo or socket */ |
| 2482 | { |
| 2483 | STRCAT(IObuff, _("[fifo/socket]")); |
| 2484 | c = TRUE; |
| 2485 | } |
| 2486 | # else |
| 2487 | # ifdef S_IFIFO |
| 2488 | if ((perm & S_IFMT) == S_IFIFO) /* fifo */ |
| 2489 | { |
| 2490 | STRCAT(IObuff, _("[fifo]")); |
| 2491 | c = TRUE; |
| 2492 | } |
| 2493 | # endif |
| 2494 | # ifdef S_IFSOCK |
| 2495 | if ((perm & S_IFMT) == S_IFSOCK) /* or socket */ |
| 2496 | { |
| 2497 | STRCAT(IObuff, _("[socket]")); |
| 2498 | c = TRUE; |
| 2499 | } |
| 2500 | # endif |
| 2501 | # endif |
Bram Moolenaar | fe1c56d | 2007-07-10 15:10:54 +0000 | [diff] [blame] | 2502 | # ifdef OPEN_CHR_FILES |
| 2503 | if (S_ISCHR(perm)) /* or character special */ |
| 2504 | { |
| 2505 | STRCAT(IObuff, _("[character special]")); |
| 2506 | c = TRUE; |
| 2507 | } |
| 2508 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2509 | #endif |
| 2510 | if (curbuf->b_p_ro) |
| 2511 | { |
| 2512 | STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")); |
| 2513 | c = TRUE; |
| 2514 | } |
| 2515 | if (read_no_eol_lnum) |
| 2516 | { |
| 2517 | msg_add_eol(); |
| 2518 | c = TRUE; |
| 2519 | } |
| 2520 | if (ff_error == EOL_DOS) |
| 2521 | { |
| 2522 | STRCAT(IObuff, _("[CR missing]")); |
| 2523 | c = TRUE; |
| 2524 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2525 | if (split) |
| 2526 | { |
| 2527 | STRCAT(IObuff, _("[long lines split]")); |
| 2528 | c = TRUE; |
| 2529 | } |
| 2530 | #ifdef FEAT_MBYTE |
| 2531 | if (notconverted) |
| 2532 | { |
| 2533 | STRCAT(IObuff, _("[NOT converted]")); |
| 2534 | c = TRUE; |
| 2535 | } |
| 2536 | else if (converted) |
| 2537 | { |
| 2538 | STRCAT(IObuff, _("[converted]")); |
| 2539 | c = TRUE; |
| 2540 | } |
| 2541 | #endif |
| 2542 | #ifdef FEAT_CRYPT |
| 2543 | if (cryptkey != NULL) |
| 2544 | { |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 2545 | crypt_append_msg(curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2546 | c = TRUE; |
| 2547 | } |
| 2548 | #endif |
| 2549 | #ifdef FEAT_MBYTE |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2550 | if (conv_error != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2551 | { |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2552 | sprintf((char *)IObuff + STRLEN(IObuff), |
| 2553 | _("[CONVERSION ERROR in line %ld]"), (long)conv_error); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2554 | c = TRUE; |
| 2555 | } |
| 2556 | else if (illegal_byte > 0) |
| 2557 | { |
| 2558 | sprintf((char *)IObuff + STRLEN(IObuff), |
| 2559 | _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte); |
| 2560 | c = TRUE; |
| 2561 | } |
| 2562 | else |
| 2563 | #endif |
| 2564 | if (error) |
| 2565 | { |
| 2566 | STRCAT(IObuff, _("[READ ERRORS]")); |
| 2567 | c = TRUE; |
| 2568 | } |
| 2569 | if (msg_add_fileformat(fileformat)) |
| 2570 | c = TRUE; |
| 2571 | #ifdef FEAT_CRYPT |
| 2572 | if (cryptkey != NULL) |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2573 | msg_add_lines(c, (long)linecnt, filesize |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 2574 | - crypt_get_header_len(crypt_get_method_nr(curbuf))); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2575 | else |
| 2576 | #endif |
| 2577 | msg_add_lines(c, (long)linecnt, filesize); |
| 2578 | |
| 2579 | vim_free(keep_msg); |
| 2580 | keep_msg = NULL; |
| 2581 | msg_scrolled_ign = TRUE; |
| 2582 | #ifdef ALWAYS_USE_GUI |
| 2583 | /* Don't show the message when reading stdin, it would end up in a |
| 2584 | * message box (which might be shown when exiting!) */ |
| 2585 | if (read_stdin || read_buffer) |
| 2586 | p = msg_may_trunc(FALSE, IObuff); |
| 2587 | else |
| 2588 | #endif |
| 2589 | p = msg_trunc_attr(IObuff, FALSE, 0); |
| 2590 | if (read_stdin || read_buffer || restart_edit != 0 |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2591 | || (msg_scrolled != 0 && !need_wait_return)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2592 | /* Need to repeat the message after redrawing when: |
| 2593 | * - When reading from stdin (the screen will be cleared next). |
| 2594 | * - When restart_edit is set (otherwise there will be a delay |
| 2595 | * before redrawing). |
| 2596 | * - When the screen was scrolled but there is no wait-return |
| 2597 | * prompt. */ |
Bram Moolenaar | 8f7fd65 | 2006-02-21 22:04:51 +0000 | [diff] [blame] | 2598 | set_keep_msg(p, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2599 | msg_scrolled_ign = FALSE; |
| 2600 | } |
| 2601 | |
| 2602 | /* with errors writing the file requires ":w!" */ |
| 2603 | if (newfile && (error |
| 2604 | #ifdef FEAT_MBYTE |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2605 | || conv_error != 0 |
Bram Moolenaar | 8f7fd65 | 2006-02-21 22:04:51 +0000 | [diff] [blame] | 2606 | || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2607 | #endif |
| 2608 | )) |
| 2609 | curbuf->b_p_ro = TRUE; |
| 2610 | |
| 2611 | u_clearline(); /* cannot use "U" command after adding lines */ |
| 2612 | |
| 2613 | /* |
| 2614 | * In Ex mode: cursor at last new line. |
| 2615 | * Otherwise: cursor at first new line. |
| 2616 | */ |
| 2617 | if (exmode_active) |
| 2618 | curwin->w_cursor.lnum = from + linecnt; |
| 2619 | else |
| 2620 | curwin->w_cursor.lnum = from + 1; |
| 2621 | check_cursor_lnum(); |
| 2622 | beginline(BL_WHITE | BL_FIX); /* on first non-blank */ |
| 2623 | |
| 2624 | /* |
| 2625 | * Set '[ and '] marks to the newly read lines. |
| 2626 | */ |
| 2627 | curbuf->b_op_start.lnum = from + 1; |
| 2628 | curbuf->b_op_start.col = 0; |
| 2629 | curbuf->b_op_end.lnum = from + linecnt; |
| 2630 | curbuf->b_op_end.col = 0; |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 2631 | |
| 2632 | #ifdef WIN32 |
| 2633 | /* |
| 2634 | * Work around a weird problem: When a file has two links (only |
| 2635 | * possible on NTFS) and we write through one link, then stat() it |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 2636 | * through the other link, the timestamp information may be wrong. |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 2637 | * It's correct again after reading the file, thus reset the timestamp |
| 2638 | * here. |
| 2639 | */ |
| 2640 | if (newfile && !read_stdin && !read_buffer |
| 2641 | && mch_stat((char *)fname, &st) >= 0) |
| 2642 | { |
| 2643 | buf_store_time(curbuf, &st, fname); |
| 2644 | curbuf->b_mtime_read = curbuf->b_mtime; |
| 2645 | } |
| 2646 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2647 | } |
| 2648 | msg_scroll = msg_save; |
| 2649 | |
| 2650 | #ifdef FEAT_VIMINFO |
| 2651 | /* |
| 2652 | * Get the marks before executing autocommands, so they can be used there. |
| 2653 | */ |
| 2654 | check_marks_read(); |
| 2655 | #endif |
| 2656 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2657 | /* |
Bram Moolenaar | 34d72d4 | 2015-07-17 14:18:08 +0200 | [diff] [blame] | 2658 | * We remember if the last line of the read didn't have |
| 2659 | * an eol even when 'binary' is off, to support turning 'fixeol' off, |
| 2660 | * or writing the read again with 'binary' on. The latter is required |
| 2661 | * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2662 | */ |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 2663 | curbuf->b_no_eol_lnum = read_no_eol_lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2664 | |
Bram Moolenaar | f9bb734 | 2010-08-04 14:29:54 +0200 | [diff] [blame] | 2665 | /* When reloading a buffer put the cursor at the first line that is |
| 2666 | * different. */ |
| 2667 | if (flags & READ_KEEP_UNDO) |
| 2668 | u_find_first_changed(); |
| 2669 | |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2670 | #ifdef FEAT_PERSISTENT_UNDO |
| 2671 | /* |
| 2672 | * When opening a new file locate undo info and read it. |
| 2673 | */ |
| 2674 | if (read_undo_file) |
| 2675 | { |
| 2676 | char_u hash[UNDO_HASH_SIZE]; |
| 2677 | |
| 2678 | sha256_finish(&sha_ctx, hash); |
Bram Moolenaar | 6ed8ed8 | 2010-05-30 20:40:11 +0200 | [diff] [blame] | 2679 | u_read_undo(NULL, hash, fname); |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2680 | } |
| 2681 | #endif |
| 2682 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2683 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | f71d7b9 | 2016-08-09 22:14:05 +0200 | [diff] [blame] | 2684 | if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2685 | { |
| 2686 | int m = msg_scroll; |
| 2687 | int n = msg_scrolled; |
| 2688 | |
| 2689 | /* Save the fileformat now, otherwise the buffer will be considered |
| 2690 | * modified if the format/encoding was automatically detected. */ |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2691 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2692 | save_file_ff(curbuf); |
| 2693 | |
| 2694 | /* |
| 2695 | * The output from the autocommands should not overwrite anything and |
| 2696 | * should not be overwritten: Set msg_scroll, restore its value if no |
| 2697 | * output was done. |
| 2698 | */ |
| 2699 | msg_scroll = TRUE; |
| 2700 | if (filtering) |
| 2701 | apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname, |
| 2702 | FALSE, curbuf, eap); |
Bram Moolenaar | f71d7b9 | 2016-08-09 22:14:05 +0200 | [diff] [blame] | 2703 | else if (newfile || (read_buffer && sfname != NULL)) |
Bram Moolenaar | c369133 | 2016-04-20 12:49:49 +0200 | [diff] [blame] | 2704 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2705 | apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname, |
| 2706 | FALSE, curbuf, eap); |
Bram Moolenaar | c369133 | 2016-04-20 12:49:49 +0200 | [diff] [blame] | 2707 | if (!au_did_filetype && *curbuf->b_p_ft != NUL) |
| 2708 | /* |
| 2709 | * EVENT_FILETYPE was not triggered but the buffer already has a |
| 2710 | * filetype. Trigger EVENT_FILETYPE using the existing filetype. |
| 2711 | */ |
| 2712 | apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname, |
| 2713 | TRUE, curbuf); |
| 2714 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2715 | else |
| 2716 | apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname, |
| 2717 | FALSE, NULL, eap); |
| 2718 | if (msg_scrolled == n) |
| 2719 | msg_scroll = m; |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 2720 | # ifdef FEAT_EVAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2721 | if (aborting()) /* autocmds may abort script processing */ |
| 2722 | return FAIL; |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 2723 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2724 | } |
| 2725 | #endif |
| 2726 | |
| 2727 | if (recoverymode && error) |
| 2728 | return FAIL; |
| 2729 | return OK; |
| 2730 | } |
| 2731 | |
Bram Moolenaar | f04507d | 2016-08-20 15:05:39 +0200 | [diff] [blame] | 2732 | #if defined(OPEN_CHR_FILES) || defined(PROTO) |
Bram Moolenaar | fe1c56d | 2007-07-10 15:10:54 +0000 | [diff] [blame] | 2733 | /* |
| 2734 | * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+", |
| 2735 | * which is the name of files used for process substitution output by |
| 2736 | * some shells on some operating systems, e.g., bash on SunOS. |
| 2737 | * Do not accept "/dev/fd/[012]", opening these may hang Vim. |
| 2738 | */ |
Bram Moolenaar | f04507d | 2016-08-20 15:05:39 +0200 | [diff] [blame] | 2739 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2740 | is_dev_fd_file(char_u *fname) |
Bram Moolenaar | fe1c56d | 2007-07-10 15:10:54 +0000 | [diff] [blame] | 2741 | { |
| 2742 | return (STRNCMP(fname, "/dev/fd/", 8) == 0 |
| 2743 | && VIM_ISDIGIT(fname[8]) |
| 2744 | && *skipdigits(fname + 9) == NUL |
| 2745 | && (fname[9] != NUL |
| 2746 | || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2'))); |
| 2747 | } |
| 2748 | #endif |
| 2749 | |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2750 | #ifdef FEAT_MBYTE |
| 2751 | |
| 2752 | /* |
| 2753 | * From the current line count and characters read after that, estimate the |
| 2754 | * line number where we are now. |
| 2755 | * Used for error messages that include a line number. |
| 2756 | */ |
| 2757 | static linenr_T |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2758 | readfile_linenr( |
| 2759 | linenr_T linecnt, /* line count before reading more bytes */ |
| 2760 | char_u *p, /* start of more bytes read */ |
| 2761 | char_u *endp) /* end of more bytes read */ |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2762 | { |
| 2763 | char_u *s; |
| 2764 | linenr_T lnum; |
| 2765 | |
| 2766 | lnum = curbuf->b_ml.ml_line_count - linecnt + 1; |
| 2767 | for (s = p; s < endp; ++s) |
| 2768 | if (*s == '\n') |
| 2769 | ++lnum; |
| 2770 | return lnum; |
| 2771 | } |
| 2772 | #endif |
| 2773 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2774 | /* |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 2775 | * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be |
| 2776 | * equal to the buffer "buf". Used for calling readfile(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2777 | * Returns OK or FAIL. |
| 2778 | */ |
| 2779 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2780 | prep_exarg(exarg_T *eap, buf_T *buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2781 | { |
| 2782 | eap->cmd = alloc((unsigned)(STRLEN(buf->b_p_ff) |
| 2783 | #ifdef FEAT_MBYTE |
| 2784 | + STRLEN(buf->b_p_fenc) |
| 2785 | #endif |
| 2786 | + 15)); |
| 2787 | if (eap->cmd == NULL) |
| 2788 | return FAIL; |
| 2789 | |
| 2790 | #ifdef FEAT_MBYTE |
| 2791 | sprintf((char *)eap->cmd, "e ++ff=%s ++enc=%s", buf->b_p_ff, buf->b_p_fenc); |
| 2792 | eap->force_enc = 14 + (int)STRLEN(buf->b_p_ff); |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 2793 | eap->bad_char = buf->b_bad_char; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2794 | #else |
| 2795 | sprintf((char *)eap->cmd, "e ++ff=%s", buf->b_p_ff); |
| 2796 | #endif |
| 2797 | eap->force_ff = 7; |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 2798 | |
| 2799 | eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2800 | eap->read_edit = FALSE; |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 2801 | eap->forceit = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2802 | return OK; |
| 2803 | } |
| 2804 | |
Bram Moolenaar | ad875fb | 2013-07-24 15:02:03 +0200 | [diff] [blame] | 2805 | /* |
| 2806 | * Set default or forced 'fileformat' and 'binary'. |
| 2807 | */ |
| 2808 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2809 | set_file_options(int set_options, exarg_T *eap) |
Bram Moolenaar | ad875fb | 2013-07-24 15:02:03 +0200 | [diff] [blame] | 2810 | { |
| 2811 | /* set default 'fileformat' */ |
| 2812 | if (set_options) |
| 2813 | { |
| 2814 | if (eap != NULL && eap->force_ff != 0) |
| 2815 | set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL); |
| 2816 | else if (*p_ffs != NUL) |
| 2817 | set_fileformat(default_fileformat(), OPT_LOCAL); |
| 2818 | } |
| 2819 | |
| 2820 | /* set or reset 'binary' */ |
| 2821 | if (eap != NULL && eap->force_bin != 0) |
| 2822 | { |
| 2823 | int oldval = curbuf->b_p_bin; |
| 2824 | |
| 2825 | curbuf->b_p_bin = (eap->force_bin == FORCE_BIN); |
| 2826 | set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL); |
| 2827 | } |
| 2828 | } |
| 2829 | |
| 2830 | #if defined(FEAT_MBYTE) || defined(PROTO) |
| 2831 | /* |
| 2832 | * Set forced 'fileencoding'. |
| 2833 | */ |
| 2834 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2835 | set_forced_fenc(exarg_T *eap) |
Bram Moolenaar | ad875fb | 2013-07-24 15:02:03 +0200 | [diff] [blame] | 2836 | { |
| 2837 | if (eap->force_enc != 0) |
| 2838 | { |
| 2839 | char_u *fenc = enc_canonize(eap->cmd + eap->force_enc); |
| 2840 | |
| 2841 | if (fenc != NULL) |
| 2842 | set_string_option_direct((char_u *)"fenc", -1, |
| 2843 | fenc, OPT_FREE|OPT_LOCAL, 0); |
| 2844 | vim_free(fenc); |
| 2845 | } |
| 2846 | } |
| 2847 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2848 | /* |
| 2849 | * Find next fileencoding to use from 'fileencodings'. |
| 2850 | * "pp" points to fenc_next. It's advanced to the next item. |
| 2851 | * When there are no more items, an empty string is returned and *pp is set to |
| 2852 | * NULL. |
| 2853 | * When *pp is not set to NULL, the result is in allocated memory. |
| 2854 | */ |
| 2855 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2856 | next_fenc(char_u **pp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2857 | { |
| 2858 | char_u *p; |
| 2859 | char_u *r; |
| 2860 | |
| 2861 | if (**pp == NUL) |
| 2862 | { |
| 2863 | *pp = NULL; |
| 2864 | return (char_u *)""; |
| 2865 | } |
| 2866 | p = vim_strchr(*pp, ','); |
| 2867 | if (p == NULL) |
| 2868 | { |
| 2869 | r = enc_canonize(*pp); |
| 2870 | *pp += STRLEN(*pp); |
| 2871 | } |
| 2872 | else |
| 2873 | { |
| 2874 | r = vim_strnsave(*pp, (int)(p - *pp)); |
| 2875 | *pp = p + 1; |
| 2876 | if (r != NULL) |
| 2877 | { |
| 2878 | p = enc_canonize(r); |
| 2879 | vim_free(r); |
| 2880 | r = p; |
| 2881 | } |
| 2882 | } |
| 2883 | if (r == NULL) /* out of memory */ |
| 2884 | { |
| 2885 | r = (char_u *)""; |
| 2886 | *pp = NULL; |
| 2887 | } |
| 2888 | return r; |
| 2889 | } |
| 2890 | |
| 2891 | # ifdef FEAT_EVAL |
| 2892 | /* |
| 2893 | * Convert a file with the 'charconvert' expression. |
| 2894 | * This closes the file which is to be read, converts it and opens the |
| 2895 | * resulting file for reading. |
| 2896 | * Returns name of the resulting converted file (the caller should delete it |
| 2897 | * after reading it). |
| 2898 | * Returns NULL if the conversion failed ("*fdp" is not set) . |
| 2899 | */ |
| 2900 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2901 | readfile_charconvert( |
| 2902 | char_u *fname, /* name of input file */ |
| 2903 | char_u *fenc, /* converted from */ |
| 2904 | int *fdp) /* in/out: file descriptor of file */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2905 | { |
| 2906 | char_u *tmpname; |
| 2907 | char_u *errmsg = NULL; |
| 2908 | |
Bram Moolenaar | e5c421c | 2015-03-31 13:33:08 +0200 | [diff] [blame] | 2909 | tmpname = vim_tempname('r', FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2910 | if (tmpname == NULL) |
| 2911 | errmsg = (char_u *)_("Can't find temp file for conversion"); |
| 2912 | else |
| 2913 | { |
| 2914 | close(*fdp); /* close the input file, ignore errors */ |
| 2915 | *fdp = -1; |
| 2916 | if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc, |
| 2917 | fname, tmpname) == FAIL) |
| 2918 | errmsg = (char_u *)_("Conversion with 'charconvert' failed"); |
| 2919 | if (errmsg == NULL && (*fdp = mch_open((char *)tmpname, |
| 2920 | O_RDONLY | O_EXTRA, 0)) < 0) |
| 2921 | errmsg = (char_u *)_("can't read output of 'charconvert'"); |
| 2922 | } |
| 2923 | |
| 2924 | if (errmsg != NULL) |
| 2925 | { |
| 2926 | /* Don't use emsg(), it breaks mappings, the retry with |
| 2927 | * another type of conversion might still work. */ |
| 2928 | MSG(errmsg); |
| 2929 | if (tmpname != NULL) |
| 2930 | { |
| 2931 | mch_remove(tmpname); /* delete converted file */ |
| 2932 | vim_free(tmpname); |
| 2933 | tmpname = NULL; |
| 2934 | } |
| 2935 | } |
| 2936 | |
| 2937 | /* If the input file is closed, open it (caller should check for error). */ |
| 2938 | if (*fdp < 0) |
| 2939 | *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); |
| 2940 | |
| 2941 | return tmpname; |
| 2942 | } |
| 2943 | # endif |
| 2944 | |
| 2945 | #endif |
| 2946 | |
| 2947 | #ifdef FEAT_VIMINFO |
| 2948 | /* |
| 2949 | * Read marks for the current buffer from the viminfo file, when we support |
| 2950 | * buffer marks and the buffer has a name. |
| 2951 | */ |
| 2952 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2953 | check_marks_read(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2954 | { |
| 2955 | if (!curbuf->b_marks_read && get_viminfo_parameter('\'') > 0 |
| 2956 | && curbuf->b_ffname != NULL) |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 2957 | read_viminfo(NULL, VIF_WANT_MARKS); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2958 | |
| 2959 | /* Always set b_marks_read; needed when 'viminfo' is changed to include |
| 2960 | * the ' parameter after opening a buffer. */ |
| 2961 | curbuf->b_marks_read = TRUE; |
| 2962 | } |
| 2963 | #endif |
| 2964 | |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 2965 | #if defined(FEAT_CRYPT) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2966 | /* |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2967 | * Check for magic number used for encryption. Applies to the current buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2968 | * If found, the magic number is removed from ptr[*sizep] and *sizep and |
| 2969 | * *filesizep are updated. |
| 2970 | * Return the (new) encryption key, NULL for no encryption. |
| 2971 | */ |
| 2972 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2973 | check_for_cryptkey( |
| 2974 | char_u *cryptkey, /* previous encryption key or NULL */ |
| 2975 | char_u *ptr, /* pointer to read bytes */ |
| 2976 | long *sizep, /* length of read bytes */ |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 2977 | off_T *filesizep, /* nr of bytes used from file */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2978 | int newfile, /* editing a new buffer */ |
| 2979 | char_u *fname, /* file name to display */ |
| 2980 | int *did_ask) /* flag: whether already asked for key */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2981 | { |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 2982 | int method = crypt_method_nr_from_magic((char *)ptr, *sizep); |
Bram Moolenaar | cf81aef | 2013-08-25 17:46:08 +0200 | [diff] [blame] | 2983 | int b_p_ro = curbuf->b_p_ro; |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2984 | |
| 2985 | if (method >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2986 | { |
Bram Moolenaar | cf81aef | 2013-08-25 17:46:08 +0200 | [diff] [blame] | 2987 | /* Mark the buffer as read-only until the decryption has taken place. |
| 2988 | * Avoids accidentally overwriting the file with garbage. */ |
| 2989 | curbuf->b_p_ro = TRUE; |
| 2990 | |
Bram Moolenaar | 2be7950 | 2014-08-13 21:58:28 +0200 | [diff] [blame] | 2991 | /* Set the cryptmethod local to the buffer. */ |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 2992 | crypt_set_cm_option(curbuf, method); |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 2993 | if (cryptkey == NULL && !*did_ask) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2994 | { |
| 2995 | if (*curbuf->b_p_key) |
| 2996 | cryptkey = curbuf->b_p_key; |
| 2997 | else |
| 2998 | { |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2999 | /* When newfile is TRUE, store the typed key in the 'key' |
| 3000 | * option and don't free it. bf needs hash of the key saved. |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 3001 | * Don't ask for the key again when first time Enter was hit. |
| 3002 | * Happens when retrying to detect encoding. */ |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 3003 | smsg((char_u *)_(need_key_msg), fname); |
| 3004 | msg_scroll = TRUE; |
Bram Moolenaar | 3a0c908 | 2014-11-12 15:15:42 +0100 | [diff] [blame] | 3005 | crypt_check_method(method); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 3006 | cryptkey = crypt_get_key(newfile, FALSE); |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 3007 | *did_ask = TRUE; |
| 3008 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3009 | /* check if empty key entered */ |
| 3010 | if (cryptkey != NULL && *cryptkey == NUL) |
| 3011 | { |
| 3012 | if (cryptkey != curbuf->b_p_key) |
| 3013 | vim_free(cryptkey); |
| 3014 | cryptkey = NULL; |
| 3015 | } |
| 3016 | } |
| 3017 | } |
| 3018 | |
| 3019 | if (cryptkey != NULL) |
| 3020 | { |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 3021 | int header_len; |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 3022 | |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 3023 | curbuf->b_cryptstate = crypt_create_from_header( |
| 3024 | method, cryptkey, ptr); |
| 3025 | crypt_set_cm_option(curbuf, method); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3026 | |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 3027 | /* Remove cryptmethod specific header from the text. */ |
| 3028 | header_len = crypt_get_header_len(method); |
Bram Moolenaar | 680e015 | 2016-09-25 20:54:11 +0200 | [diff] [blame] | 3029 | if (*sizep <= header_len) |
| 3030 | /* invalid header, buffer can't be encrypted */ |
| 3031 | return NULL; |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 3032 | *filesizep += header_len; |
| 3033 | *sizep -= header_len; |
| 3034 | mch_memmove(ptr, ptr + header_len, (size_t)*sizep); |
| 3035 | |
Bram Moolenaar | cf81aef | 2013-08-25 17:46:08 +0200 | [diff] [blame] | 3036 | /* Restore the read-only flag. */ |
| 3037 | curbuf->b_p_ro = b_p_ro; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3038 | } |
| 3039 | } |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 3040 | /* When starting to edit a new file which does not have encryption, clear |
| 3041 | * the 'key' option, except when starting up (called with -x argument) */ |
Bram Moolenaar | fa0ff9a | 2010-07-25 16:05:19 +0200 | [diff] [blame] | 3042 | else if (newfile && *curbuf->b_p_key != NUL && !starting) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3043 | set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL); |
| 3044 | |
| 3045 | return cryptkey; |
| 3046 | } |
Bram Moolenaar | 80794b1 | 2010-06-13 05:20:42 +0200 | [diff] [blame] | 3047 | #endif /* FEAT_CRYPT */ |
| 3048 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3049 | #ifdef UNIX |
| 3050 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3051 | set_file_time( |
| 3052 | char_u *fname, |
| 3053 | time_t atime, /* access time */ |
| 3054 | time_t mtime) /* modification time */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3055 | { |
| 3056 | # if defined(HAVE_UTIME) && defined(HAVE_UTIME_H) |
| 3057 | struct utimbuf buf; |
| 3058 | |
| 3059 | buf.actime = atime; |
| 3060 | buf.modtime = mtime; |
| 3061 | (void)utime((char *)fname, &buf); |
| 3062 | # else |
| 3063 | # if defined(HAVE_UTIMES) |
| 3064 | struct timeval tvp[2]; |
| 3065 | |
| 3066 | tvp[0].tv_sec = atime; |
| 3067 | tvp[0].tv_usec = 0; |
| 3068 | tvp[1].tv_sec = mtime; |
| 3069 | tvp[1].tv_usec = 0; |
| 3070 | # ifdef NeXT |
| 3071 | (void)utimes((char *)fname, tvp); |
| 3072 | # else |
| 3073 | (void)utimes((char *)fname, (const struct timeval *)&tvp); |
| 3074 | # endif |
| 3075 | # endif |
| 3076 | # endif |
| 3077 | } |
| 3078 | #endif /* UNIX */ |
| 3079 | |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 3080 | #if defined(VMS) && !defined(MIN) |
| 3081 | /* Older DECC compiler for VAX doesn't define MIN() */ |
| 3082 | # define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 3083 | #endif |
| 3084 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3085 | /* |
Bram Moolenaar | 5386a12 | 2007-06-28 20:02:32 +0000 | [diff] [blame] | 3086 | * Return TRUE if a file appears to be read-only from the file permissions. |
| 3087 | */ |
| 3088 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3089 | check_file_readonly( |
| 3090 | char_u *fname, /* full path to file */ |
| 3091 | int perm) /* known permissions on file */ |
Bram Moolenaar | 5386a12 | 2007-06-28 20:02:32 +0000 | [diff] [blame] | 3092 | { |
| 3093 | #ifndef USE_MCH_ACCESS |
| 3094 | int fd = 0; |
| 3095 | #endif |
| 3096 | |
| 3097 | return ( |
| 3098 | #ifdef USE_MCH_ACCESS |
| 3099 | # ifdef UNIX |
| 3100 | (perm & 0222) == 0 || |
| 3101 | # endif |
| 3102 | mch_access((char *)fname, W_OK) |
| 3103 | #else |
| 3104 | (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0 |
| 3105 | ? TRUE : (close(fd), FALSE) |
| 3106 | #endif |
| 3107 | ); |
| 3108 | } |
| 3109 | |
| 3110 | |
| 3111 | /* |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3112 | * buf_write() - write to file "fname" lines "start" through "end" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3113 | * |
| 3114 | * We do our own buffering here because fwrite() is so slow. |
| 3115 | * |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3116 | * If "forceit" is true, we don't care for errors when attempting backups. |
| 3117 | * In case of an error everything possible is done to restore the original |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 3118 | * file. But when "forceit" is TRUE, we risk losing it. |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3119 | * |
| 3120 | * When "reset_changed" is TRUE and "append" == FALSE and "start" == 1 and |
| 3121 | * "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3122 | * |
| 3123 | * This function must NOT use NameBuff (because it's called by autowrite()). |
| 3124 | * |
| 3125 | * return FAIL for failure, OK otherwise |
| 3126 | */ |
| 3127 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3128 | buf_write( |
| 3129 | buf_T *buf, |
| 3130 | char_u *fname, |
| 3131 | char_u *sfname, |
| 3132 | linenr_T start, |
| 3133 | linenr_T end, |
| 3134 | exarg_T *eap, /* for forced 'ff' and 'fenc', can be |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3135 | NULL! */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3136 | int append, /* append to the file */ |
| 3137 | int forceit, |
| 3138 | int reset_changed, |
| 3139 | int filtering) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3140 | { |
| 3141 | int fd; |
| 3142 | char_u *backup = NULL; |
| 3143 | int backup_copy = FALSE; /* copy the original file? */ |
| 3144 | int dobackup; |
| 3145 | char_u *ffname; |
| 3146 | char_u *wfname = NULL; /* name of file to write to */ |
| 3147 | char_u *s; |
| 3148 | char_u *ptr; |
| 3149 | char_u c; |
| 3150 | int len; |
| 3151 | linenr_T lnum; |
| 3152 | long nchars; |
| 3153 | char_u *errmsg = NULL; |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 3154 | int errmsg_allocated = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3155 | char_u *errnum = NULL; |
| 3156 | char_u *buffer; |
| 3157 | char_u smallbuf[SMBUFSIZE]; |
| 3158 | char_u *backup_ext; |
| 3159 | int bufsize; |
| 3160 | long perm; /* file permissions */ |
| 3161 | int retval = OK; |
| 3162 | int newfile = FALSE; /* TRUE if file doesn't exist yet */ |
| 3163 | int msg_save = msg_scroll; |
| 3164 | int overwriting; /* TRUE if writing over original */ |
| 3165 | int no_eol = FALSE; /* no end-of-line written */ |
| 3166 | int device = FALSE; /* writing to a device */ |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 3167 | stat_T st_old; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3168 | int prev_got_int = got_int; |
| 3169 | int file_readonly = FALSE; /* overwritten file is read-only */ |
| 3170 | static char *err_readonly = "is read-only (cannot override: \"W\" in 'cpoptions')"; |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 3171 | #if defined(UNIX) /*XXX fix me sometime? */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3172 | int made_writable = FALSE; /* 'w' bit has been set */ |
| 3173 | #endif |
| 3174 | /* writing everything */ |
| 3175 | int whole = (start == 1 && end == buf->b_ml.ml_line_count); |
| 3176 | #ifdef FEAT_AUTOCMD |
| 3177 | linenr_T old_line_count = buf->b_ml.ml_line_count; |
| 3178 | #endif |
| 3179 | int attr; |
| 3180 | int fileformat; |
| 3181 | int write_bin; |
| 3182 | struct bw_info write_info; /* info for buf_write_bytes() */ |
| 3183 | #ifdef FEAT_MBYTE |
| 3184 | int converted = FALSE; |
| 3185 | int notconverted = FALSE; |
| 3186 | char_u *fenc; /* effective 'fileencoding' */ |
| 3187 | char_u *fenc_tofree = NULL; /* allocated "fenc" */ |
| 3188 | #endif |
| 3189 | #ifdef HAS_BW_FLAGS |
| 3190 | int wb_flags = 0; |
| 3191 | #endif |
| 3192 | #ifdef HAVE_ACL |
| 3193 | vim_acl_T acl = NULL; /* ACL copied from original file to |
| 3194 | backup or new file */ |
| 3195 | #endif |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 3196 | #ifdef FEAT_PERSISTENT_UNDO |
| 3197 | int write_undo_file = FALSE; |
| 3198 | context_sha256_T sha_ctx; |
| 3199 | #endif |
Bram Moolenaar | b8ee25a | 2014-09-23 15:45:08 +0200 | [diff] [blame] | 3200 | unsigned int bkc = get_bkc_value(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3201 | |
| 3202 | if (fname == NULL || *fname == NUL) /* safety check */ |
| 3203 | return FAIL; |
Bram Moolenaar | 70d60e9 | 2009-12-31 13:53:33 +0000 | [diff] [blame] | 3204 | if (buf->b_ml.ml_mfp == NULL) |
| 3205 | { |
| 3206 | /* This can happen during startup when there is a stray "w" in the |
| 3207 | * vimrc file. */ |
| 3208 | EMSG(_(e_emptybuf)); |
| 3209 | return FAIL; |
| 3210 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3211 | |
| 3212 | /* |
| 3213 | * Disallow writing from .exrc and .vimrc in current directory for |
| 3214 | * security reasons. |
| 3215 | */ |
| 3216 | if (check_secure()) |
| 3217 | return FAIL; |
| 3218 | |
| 3219 | /* Avoid a crash for a long name. */ |
| 3220 | if (STRLEN(fname) >= MAXPATHL) |
| 3221 | { |
| 3222 | EMSG(_(e_longname)); |
| 3223 | return FAIL; |
| 3224 | } |
| 3225 | |
| 3226 | #ifdef FEAT_MBYTE |
| 3227 | /* must init bw_conv_buf and bw_iconv_fd before jumping to "fail" */ |
| 3228 | write_info.bw_conv_buf = NULL; |
| 3229 | write_info.bw_conv_error = FALSE; |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 3230 | write_info.bw_conv_error_lnum = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3231 | write_info.bw_restlen = 0; |
| 3232 | # ifdef USE_ICONV |
| 3233 | write_info.bw_iconv_fd = (iconv_t)-1; |
| 3234 | # endif |
| 3235 | #endif |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 3236 | #ifdef FEAT_CRYPT |
| 3237 | write_info.bw_buffer = buf; |
| 3238 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3239 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3240 | /* After writing a file changedtick changes but we don't want to display |
| 3241 | * the line. */ |
| 3242 | ex_no_reprint = TRUE; |
| 3243 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3244 | /* |
| 3245 | * If there is no file name yet, use the one for the written file. |
| 3246 | * BF_NOTEDITED is set to reflect this (in case the write fails). |
| 3247 | * Don't do this when the write is for a filter command. |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3248 | * Don't do this when appending. |
| 3249 | * Only do this when 'cpoptions' contains the 'F' flag. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3250 | */ |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 3251 | if (buf->b_ffname == NULL |
| 3252 | && reset_changed |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3253 | && whole |
| 3254 | && buf == curbuf |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3255 | #ifdef FEAT_QUICKFIX |
| 3256 | && !bt_nofile(buf) |
| 3257 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3258 | && !filtering |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3259 | && (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3260 | && vim_strchr(p_cpo, CPO_FNAMEW) != NULL) |
| 3261 | { |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 3262 | if (set_rw_fname(fname, sfname) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3263 | return FAIL; |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 3264 | buf = curbuf; /* just in case autocmds made "buf" invalid */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3265 | } |
| 3266 | |
| 3267 | if (sfname == NULL) |
| 3268 | sfname = fname; |
| 3269 | /* |
| 3270 | * For Unix: Use the short file name whenever possible. |
| 3271 | * Avoids problems with networks and when directory names are changed. |
| 3272 | * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to |
| 3273 | * another directory, which we don't detect |
| 3274 | */ |
| 3275 | ffname = fname; /* remember full fname */ |
| 3276 | #ifdef UNIX |
| 3277 | fname = sfname; |
| 3278 | #endif |
| 3279 | |
| 3280 | if (buf->b_ffname != NULL && fnamecmp(ffname, buf->b_ffname) == 0) |
| 3281 | overwriting = TRUE; |
| 3282 | else |
| 3283 | overwriting = FALSE; |
| 3284 | |
| 3285 | if (exiting) |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 3286 | settmode(TMODE_COOK); /* when exiting allow typeahead now */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3287 | |
| 3288 | ++no_wait_return; /* don't wait for return yet */ |
| 3289 | |
| 3290 | /* |
| 3291 | * Set '[ and '] marks to the lines to be written. |
| 3292 | */ |
| 3293 | buf->b_op_start.lnum = start; |
| 3294 | buf->b_op_start.col = 0; |
| 3295 | buf->b_op_end.lnum = end; |
| 3296 | buf->b_op_end.col = 0; |
| 3297 | |
| 3298 | #ifdef FEAT_AUTOCMD |
| 3299 | { |
| 3300 | aco_save_T aco; |
| 3301 | int buf_ffname = FALSE; |
| 3302 | int buf_sfname = FALSE; |
| 3303 | int buf_fname_f = FALSE; |
| 3304 | int buf_fname_s = FALSE; |
| 3305 | int did_cmd = FALSE; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3306 | int nofile_err = FALSE; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 3307 | int empty_memline = (buf->b_ml.ml_mfp == NULL); |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 3308 | bufref_T bufref; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3309 | |
| 3310 | /* |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 3311 | * Apply PRE autocommands. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3312 | * Set curbuf to the buffer to be written. |
| 3313 | * Careful: The autocommands may call buf_write() recursively! |
| 3314 | */ |
| 3315 | if (ffname == buf->b_ffname) |
| 3316 | buf_ffname = TRUE; |
| 3317 | if (sfname == buf->b_sfname) |
| 3318 | buf_sfname = TRUE; |
| 3319 | if (fname == buf->b_ffname) |
| 3320 | buf_fname_f = TRUE; |
| 3321 | if (fname == buf->b_sfname) |
| 3322 | buf_fname_s = TRUE; |
| 3323 | |
| 3324 | /* set curwin/curbuf to buf and save a few things */ |
| 3325 | aucmd_prepbuf(&aco, buf); |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 3326 | set_bufref(&bufref, buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3327 | |
| 3328 | if (append) |
| 3329 | { |
| 3330 | if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD, |
| 3331 | sfname, sfname, FALSE, curbuf, eap))) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3332 | { |
Bram Moolenaar | b1b715d | 2006-01-21 22:09:43 +0000 | [diff] [blame] | 3333 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 3334 | if (overwriting && bt_nofile(curbuf)) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3335 | nofile_err = TRUE; |
| 3336 | else |
Bram Moolenaar | b1b715d | 2006-01-21 22:09:43 +0000 | [diff] [blame] | 3337 | #endif |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3338 | apply_autocmds_exarg(EVENT_FILEAPPENDPRE, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3339 | sfname, sfname, FALSE, curbuf, eap); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3340 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3341 | } |
| 3342 | else if (filtering) |
| 3343 | { |
| 3344 | apply_autocmds_exarg(EVENT_FILTERWRITEPRE, |
| 3345 | NULL, sfname, FALSE, curbuf, eap); |
| 3346 | } |
| 3347 | else if (reset_changed && whole) |
| 3348 | { |
Bram Moolenaar | 39fc42e | 2011-09-02 11:56:20 +0200 | [diff] [blame] | 3349 | int was_changed = curbufIsChanged(); |
| 3350 | |
| 3351 | did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD, |
| 3352 | sfname, sfname, FALSE, curbuf, eap); |
| 3353 | if (did_cmd) |
| 3354 | { |
| 3355 | if (was_changed && !curbufIsChanged()) |
| 3356 | { |
| 3357 | /* Written everything correctly and BufWriteCmd has reset |
| 3358 | * 'modified': Correct the undo information so that an |
| 3359 | * undo now sets 'modified'. */ |
| 3360 | u_unchanged(curbuf); |
| 3361 | u_update_save_nr(curbuf); |
| 3362 | } |
| 3363 | } |
| 3364 | else |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3365 | { |
Bram Moolenaar | b1b715d | 2006-01-21 22:09:43 +0000 | [diff] [blame] | 3366 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 3367 | if (overwriting && bt_nofile(curbuf)) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3368 | nofile_err = TRUE; |
| 3369 | else |
Bram Moolenaar | b1b715d | 2006-01-21 22:09:43 +0000 | [diff] [blame] | 3370 | #endif |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3371 | apply_autocmds_exarg(EVENT_BUFWRITEPRE, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3372 | sfname, sfname, FALSE, curbuf, eap); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3373 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3374 | } |
| 3375 | else |
| 3376 | { |
| 3377 | if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD, |
| 3378 | sfname, sfname, FALSE, curbuf, eap))) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3379 | { |
Bram Moolenaar | b1b715d | 2006-01-21 22:09:43 +0000 | [diff] [blame] | 3380 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 3381 | if (overwriting && bt_nofile(curbuf)) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3382 | nofile_err = TRUE; |
| 3383 | else |
Bram Moolenaar | b1b715d | 2006-01-21 22:09:43 +0000 | [diff] [blame] | 3384 | #endif |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3385 | apply_autocmds_exarg(EVENT_FILEWRITEPRE, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3386 | sfname, sfname, FALSE, curbuf, eap); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3387 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
| 3390 | /* restore curwin/curbuf and a few other things */ |
| 3391 | aucmd_restbuf(&aco); |
| 3392 | |
| 3393 | /* |
| 3394 | * In three situations we return here and don't write the file: |
| 3395 | * 1. the autocommands deleted or unloaded the buffer. |
| 3396 | * 2. The autocommands abort script processing. |
| 3397 | * 3. If one of the "Cmd" autocommands was executed. |
| 3398 | */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 3399 | if (!bufref_valid(&bufref)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3400 | buf = NULL; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 3401 | if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline) |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 3402 | || did_cmd || nofile_err |
| 3403 | #ifdef FEAT_EVAL |
| 3404 | || aborting() |
| 3405 | #endif |
| 3406 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3407 | { |
| 3408 | --no_wait_return; |
| 3409 | msg_scroll = msg_save; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3410 | if (nofile_err) |
| 3411 | EMSG(_("E676: No matching autocommands for acwrite buffer")); |
| 3412 | |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 3413 | if (nofile_err |
| 3414 | #ifdef FEAT_EVAL |
| 3415 | || aborting() |
| 3416 | #endif |
| 3417 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3418 | /* An aborting error, interrupt or exception in the |
| 3419 | * autocommands. */ |
| 3420 | return FAIL; |
| 3421 | if (did_cmd) |
| 3422 | { |
| 3423 | if (buf == NULL) |
| 3424 | /* The buffer was deleted. We assume it was written |
| 3425 | * (can't retry anyway). */ |
| 3426 | return OK; |
| 3427 | if (overwriting) |
| 3428 | { |
| 3429 | /* Assume the buffer was written, update the timestamp. */ |
| 3430 | ml_timestamp(buf); |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3431 | if (append) |
| 3432 | buf->b_flags &= ~BF_NEW; |
| 3433 | else |
| 3434 | buf->b_flags &= ~BF_WRITE_MASK; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3435 | } |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3436 | if (reset_changed && buf->b_changed && !append |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 3437 | && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3438 | /* Buffer still changed, the autocommands didn't work |
| 3439 | * properly. */ |
| 3440 | return FAIL; |
| 3441 | return OK; |
| 3442 | } |
| 3443 | #ifdef FEAT_EVAL |
| 3444 | if (!aborting()) |
| 3445 | #endif |
| 3446 | EMSG(_("E203: Autocommands deleted or unloaded buffer to be written")); |
| 3447 | return FAIL; |
| 3448 | } |
| 3449 | |
| 3450 | /* |
| 3451 | * The autocommands may have changed the number of lines in the file. |
| 3452 | * When writing the whole file, adjust the end. |
| 3453 | * When writing part of the file, assume that the autocommands only |
| 3454 | * changed the number of lines that are to be written (tricky!). |
| 3455 | */ |
| 3456 | if (buf->b_ml.ml_line_count != old_line_count) |
| 3457 | { |
| 3458 | if (whole) /* write all */ |
| 3459 | end = buf->b_ml.ml_line_count; |
| 3460 | else if (buf->b_ml.ml_line_count > old_line_count) /* more lines */ |
| 3461 | end += buf->b_ml.ml_line_count - old_line_count; |
| 3462 | else /* less lines */ |
| 3463 | { |
| 3464 | end -= old_line_count - buf->b_ml.ml_line_count; |
| 3465 | if (end < start) |
| 3466 | { |
| 3467 | --no_wait_return; |
| 3468 | msg_scroll = msg_save; |
| 3469 | EMSG(_("E204: Autocommand changed number of lines in unexpected way")); |
| 3470 | return FAIL; |
| 3471 | } |
| 3472 | } |
| 3473 | } |
| 3474 | |
| 3475 | /* |
| 3476 | * The autocommands may have changed the name of the buffer, which may |
| 3477 | * be kept in fname, ffname and sfname. |
| 3478 | */ |
| 3479 | if (buf_ffname) |
| 3480 | ffname = buf->b_ffname; |
| 3481 | if (buf_sfname) |
| 3482 | sfname = buf->b_sfname; |
| 3483 | if (buf_fname_f) |
| 3484 | fname = buf->b_ffname; |
| 3485 | if (buf_fname_s) |
| 3486 | fname = buf->b_sfname; |
| 3487 | } |
| 3488 | #endif |
| 3489 | |
| 3490 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 3491 | if (netbeans_active() && isNetbeansBuffer(buf)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3492 | { |
| 3493 | if (whole) |
| 3494 | { |
| 3495 | /* |
| 3496 | * b_changed can be 0 after an undo, but we still need to write |
| 3497 | * the buffer to NetBeans. |
| 3498 | */ |
| 3499 | if (buf->b_changed || isNetbeansModified(buf)) |
| 3500 | { |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3501 | --no_wait_return; /* may wait for return now */ |
| 3502 | msg_scroll = msg_save; |
| 3503 | netbeans_save_buffer(buf); /* no error checking... */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3504 | return retval; |
| 3505 | } |
| 3506 | else |
| 3507 | { |
| 3508 | errnum = (char_u *)"E656: "; |
Bram Moolenaar | ed0e745 | 2008-06-27 19:17:34 +0000 | [diff] [blame] | 3509 | errmsg = (char_u *)_("NetBeans disallows writes of unmodified buffers"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3510 | buffer = NULL; |
| 3511 | goto fail; |
| 3512 | } |
| 3513 | } |
| 3514 | else |
| 3515 | { |
| 3516 | errnum = (char_u *)"E657: "; |
| 3517 | errmsg = (char_u *)_("Partial writes disallowed for NetBeans buffers"); |
| 3518 | buffer = NULL; |
| 3519 | goto fail; |
| 3520 | } |
| 3521 | } |
| 3522 | #endif |
| 3523 | |
| 3524 | if (shortmess(SHM_OVER) && !exiting) |
| 3525 | msg_scroll = FALSE; /* overwrite previous file message */ |
| 3526 | else |
| 3527 | msg_scroll = TRUE; /* don't overwrite previous file message */ |
| 3528 | if (!filtering) |
| 3529 | filemess(buf, |
| 3530 | #ifndef UNIX |
| 3531 | sfname, |
| 3532 | #else |
| 3533 | fname, |
| 3534 | #endif |
| 3535 | (char_u *)"", 0); /* show that we are busy */ |
| 3536 | msg_scroll = FALSE; /* always overwrite the file message now */ |
| 3537 | |
| 3538 | buffer = alloc(BUFSIZE); |
| 3539 | if (buffer == NULL) /* can't allocate big buffer, use small |
| 3540 | * one (to be able to write when out of |
| 3541 | * memory) */ |
| 3542 | { |
| 3543 | buffer = smallbuf; |
| 3544 | bufsize = SMBUFSIZE; |
| 3545 | } |
| 3546 | else |
| 3547 | bufsize = BUFSIZE; |
| 3548 | |
| 3549 | /* |
| 3550 | * Get information about original file (if there is one). |
| 3551 | */ |
Bram Moolenaar | 5307683 | 2015-12-31 19:53:21 +0100 | [diff] [blame] | 3552 | #if defined(UNIX) |
Bram Moolenaar | 6f19245 | 2007-11-08 19:49:02 +0000 | [diff] [blame] | 3553 | st_old.st_dev = 0; |
| 3554 | st_old.st_ino = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3555 | perm = -1; |
| 3556 | if (mch_stat((char *)fname, &st_old) < 0) |
| 3557 | newfile = TRUE; |
| 3558 | else |
| 3559 | { |
| 3560 | perm = st_old.st_mode; |
| 3561 | if (!S_ISREG(st_old.st_mode)) /* not a file */ |
| 3562 | { |
| 3563 | if (S_ISDIR(st_old.st_mode)) |
| 3564 | { |
| 3565 | errnum = (char_u *)"E502: "; |
| 3566 | errmsg = (char_u *)_("is a directory"); |
| 3567 | goto fail; |
| 3568 | } |
| 3569 | if (mch_nodetype(fname) != NODE_WRITABLE) |
| 3570 | { |
| 3571 | errnum = (char_u *)"E503: "; |
| 3572 | errmsg = (char_u *)_("is not a file or writable device"); |
| 3573 | goto fail; |
| 3574 | } |
| 3575 | /* It's a device of some kind (or a fifo) which we can write to |
| 3576 | * but for which we can't make a backup. */ |
| 3577 | device = TRUE; |
| 3578 | newfile = TRUE; |
| 3579 | perm = -1; |
| 3580 | } |
| 3581 | } |
| 3582 | #else /* !UNIX */ |
| 3583 | /* |
| 3584 | * Check for a writable device name. |
| 3585 | */ |
| 3586 | c = mch_nodetype(fname); |
| 3587 | if (c == NODE_OTHER) |
| 3588 | { |
| 3589 | errnum = (char_u *)"E503: "; |
| 3590 | errmsg = (char_u *)_("is not a file or writable device"); |
| 3591 | goto fail; |
| 3592 | } |
| 3593 | if (c == NODE_WRITABLE) |
| 3594 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 3595 | # if defined(MSWIN) |
Bram Moolenaar | 043545e | 2006-10-10 16:44:07 +0000 | [diff] [blame] | 3596 | /* MS-Windows allows opening a device, but we will probably get stuck |
| 3597 | * trying to write to it. */ |
| 3598 | if (!p_odev) |
| 3599 | { |
| 3600 | errnum = (char_u *)"E796: "; |
| 3601 | errmsg = (char_u *)_("writing to device disabled with 'opendevice' option"); |
| 3602 | goto fail; |
| 3603 | } |
| 3604 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3605 | device = TRUE; |
| 3606 | newfile = TRUE; |
| 3607 | perm = -1; |
| 3608 | } |
| 3609 | else |
| 3610 | { |
| 3611 | perm = mch_getperm(fname); |
| 3612 | if (perm < 0) |
| 3613 | newfile = TRUE; |
| 3614 | else if (mch_isdir(fname)) |
| 3615 | { |
| 3616 | errnum = (char_u *)"E502: "; |
| 3617 | errmsg = (char_u *)_("is a directory"); |
| 3618 | goto fail; |
| 3619 | } |
| 3620 | if (overwriting) |
| 3621 | (void)mch_stat((char *)fname, &st_old); |
| 3622 | } |
| 3623 | #endif /* !UNIX */ |
| 3624 | |
| 3625 | if (!device && !newfile) |
| 3626 | { |
| 3627 | /* |
| 3628 | * Check if the file is really writable (when renaming the file to |
| 3629 | * make a backup we won't discover it later). |
| 3630 | */ |
Bram Moolenaar | 5386a12 | 2007-06-28 20:02:32 +0000 | [diff] [blame] | 3631 | file_readonly = check_file_readonly(fname, (int)perm); |
| 3632 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3633 | if (!forceit && file_readonly) |
| 3634 | { |
| 3635 | if (vim_strchr(p_cpo, CPO_FWRITE) != NULL) |
| 3636 | { |
| 3637 | errnum = (char_u *)"E504: "; |
| 3638 | errmsg = (char_u *)_(err_readonly); |
| 3639 | } |
| 3640 | else |
| 3641 | { |
| 3642 | errnum = (char_u *)"E505: "; |
| 3643 | errmsg = (char_u *)_("is read-only (add ! to override)"); |
| 3644 | } |
| 3645 | goto fail; |
| 3646 | } |
| 3647 | |
| 3648 | /* |
| 3649 | * Check if the timestamp hasn't changed since reading the file. |
| 3650 | */ |
| 3651 | if (overwriting) |
| 3652 | { |
| 3653 | retval = check_mtime(buf, &st_old); |
| 3654 | if (retval == FAIL) |
| 3655 | goto fail; |
| 3656 | } |
| 3657 | } |
| 3658 | |
| 3659 | #ifdef HAVE_ACL |
| 3660 | /* |
| 3661 | * For systems that support ACL: get the ACL from the original file. |
| 3662 | */ |
| 3663 | if (!newfile) |
| 3664 | acl = mch_get_acl(fname); |
| 3665 | #endif |
| 3666 | |
| 3667 | /* |
| 3668 | * If 'backupskip' is not empty, don't make a backup for some files. |
| 3669 | */ |
| 3670 | dobackup = (p_wb || p_bk || *p_pm != NUL); |
| 3671 | #ifdef FEAT_WILDIGN |
| 3672 | if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname)) |
| 3673 | dobackup = FALSE; |
| 3674 | #endif |
| 3675 | |
| 3676 | /* |
| 3677 | * Save the value of got_int and reset it. We don't want a previous |
| 3678 | * interruption cancel writing, only hitting CTRL-C while writing should |
| 3679 | * abort it. |
| 3680 | */ |
| 3681 | prev_got_int = got_int; |
| 3682 | got_int = FALSE; |
| 3683 | |
| 3684 | /* Mark the buffer as 'being saved' to prevent changed buffer warnings */ |
| 3685 | buf->b_saving = TRUE; |
| 3686 | |
| 3687 | /* |
| 3688 | * If we are not appending or filtering, the file exists, and the |
| 3689 | * 'writebackup', 'backup' or 'patchmode' option is set, need a backup. |
| 3690 | * When 'patchmode' is set also make a backup when appending. |
| 3691 | * |
| 3692 | * Do not make any backup, if 'writebackup' and 'backup' are both switched |
| 3693 | * off. This helps when editing large files on almost-full disks. |
| 3694 | */ |
| 3695 | if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup) |
| 3696 | { |
| 3697 | #if defined(UNIX) || defined(WIN32) |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 3698 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3699 | #endif |
| 3700 | |
Bram Moolenaar | b8ee25a | 2014-09-23 15:45:08 +0200 | [diff] [blame] | 3701 | if ((bkc & BKC_YES) || append) /* "yes" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3702 | backup_copy = TRUE; |
| 3703 | #if defined(UNIX) || defined(WIN32) |
Bram Moolenaar | b8ee25a | 2014-09-23 15:45:08 +0200 | [diff] [blame] | 3704 | else if ((bkc & BKC_AUTO)) /* "auto" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3705 | { |
| 3706 | int i; |
| 3707 | |
| 3708 | # ifdef UNIX |
| 3709 | /* |
| 3710 | * Don't rename the file when: |
| 3711 | * - it's a hard link |
| 3712 | * - it's a symbolic link |
| 3713 | * - we don't have write permission in the directory |
| 3714 | * - we can't set the owner/group of the new file |
| 3715 | */ |
| 3716 | if (st_old.st_nlink > 1 |
| 3717 | || mch_lstat((char *)fname, &st) < 0 |
| 3718 | || st.st_dev != st_old.st_dev |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3719 | || st.st_ino != st_old.st_ino |
| 3720 | # ifndef HAVE_FCHOWN |
| 3721 | || st.st_uid != st_old.st_uid |
| 3722 | || st.st_gid != st_old.st_gid |
| 3723 | # endif |
| 3724 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3725 | backup_copy = TRUE; |
| 3726 | else |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 3727 | # else |
| 3728 | # ifdef WIN32 |
| 3729 | /* On NTFS file systems hard links are possible. */ |
| 3730 | if (mch_is_linked(fname)) |
| 3731 | backup_copy = TRUE; |
| 3732 | else |
| 3733 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3734 | # endif |
| 3735 | { |
| 3736 | /* |
| 3737 | * Check if we can create a file and set the owner/group to |
| 3738 | * the ones from the original file. |
| 3739 | * First find a file name that doesn't exist yet (use some |
| 3740 | * arbitrary numbers). |
| 3741 | */ |
| 3742 | STRCPY(IObuff, fname); |
| 3743 | for (i = 4913; ; i += 123) |
| 3744 | { |
| 3745 | sprintf((char *)gettail(IObuff), "%d", i); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3746 | if (mch_lstat((char *)IObuff, &st) < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3747 | break; |
| 3748 | } |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3749 | fd = mch_open((char *)IObuff, |
| 3750 | O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3751 | if (fd < 0) /* can't write in directory */ |
| 3752 | backup_copy = TRUE; |
| 3753 | else |
| 3754 | { |
| 3755 | # ifdef UNIX |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3756 | # ifdef HAVE_FCHOWN |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 3757 | ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3758 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3759 | if (mch_stat((char *)IObuff, &st) < 0 |
| 3760 | || st.st_uid != st_old.st_uid |
| 3761 | || st.st_gid != st_old.st_gid |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 3762 | || (long)st.st_mode != perm) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3763 | backup_copy = TRUE; |
| 3764 | # endif |
Bram Moolenaar | 9835862 | 2005-11-28 22:58:23 +0000 | [diff] [blame] | 3765 | /* Close the file before removing it, on MS-Windows we |
| 3766 | * can't delete an open file. */ |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3767 | close(fd); |
Bram Moolenaar | 9835862 | 2005-11-28 22:58:23 +0000 | [diff] [blame] | 3768 | mch_remove(IObuff); |
Bram Moolenaar | 3479c5d | 2010-08-08 18:46:06 +0200 | [diff] [blame] | 3769 | # ifdef MSWIN |
| 3770 | /* MS-Windows may trigger a virus scanner to open the |
| 3771 | * file, we can't delete it then. Keep trying for half a |
| 3772 | * second. */ |
| 3773 | { |
| 3774 | int try; |
| 3775 | |
| 3776 | for (try = 0; try < 10; ++try) |
| 3777 | { |
| 3778 | if (mch_lstat((char *)IObuff, &st) < 0) |
| 3779 | break; |
| 3780 | ui_delay(50L, TRUE); /* wait 50 msec */ |
| 3781 | mch_remove(IObuff); |
| 3782 | } |
| 3783 | } |
| 3784 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3785 | } |
| 3786 | } |
| 3787 | } |
| 3788 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3789 | /* |
| 3790 | * Break symlinks and/or hardlinks if we've been asked to. |
| 3791 | */ |
Bram Moolenaar | b8ee25a | 2014-09-23 15:45:08 +0200 | [diff] [blame] | 3792 | if ((bkc & BKC_BREAKSYMLINK) || (bkc & BKC_BREAKHARDLINK)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3793 | { |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3794 | # ifdef UNIX |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3795 | int lstat_res; |
| 3796 | |
| 3797 | lstat_res = mch_lstat((char *)fname, &st); |
| 3798 | |
| 3799 | /* Symlinks. */ |
Bram Moolenaar | b8ee25a | 2014-09-23 15:45:08 +0200 | [diff] [blame] | 3800 | if ((bkc & BKC_BREAKSYMLINK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3801 | && lstat_res == 0 |
| 3802 | && st.st_ino != st_old.st_ino) |
| 3803 | backup_copy = FALSE; |
| 3804 | |
| 3805 | /* Hardlinks. */ |
Bram Moolenaar | b8ee25a | 2014-09-23 15:45:08 +0200 | [diff] [blame] | 3806 | if ((bkc & BKC_BREAKHARDLINK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3807 | && st_old.st_nlink > 1 |
| 3808 | && (lstat_res != 0 || st.st_ino == st_old.st_ino)) |
| 3809 | backup_copy = FALSE; |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3810 | # else |
| 3811 | # if defined(WIN32) |
| 3812 | /* Symlinks. */ |
Bram Moolenaar | b8ee25a | 2014-09-23 15:45:08 +0200 | [diff] [blame] | 3813 | if ((bkc & BKC_BREAKSYMLINK) && mch_is_symbolic_link(fname)) |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3814 | backup_copy = FALSE; |
| 3815 | |
| 3816 | /* Hardlinks. */ |
Bram Moolenaar | b8ee25a | 2014-09-23 15:45:08 +0200 | [diff] [blame] | 3817 | if ((bkc & BKC_BREAKHARDLINK) && mch_is_hard_link(fname)) |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3818 | backup_copy = FALSE; |
| 3819 | # endif |
| 3820 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3821 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3822 | |
| 3823 | #endif |
| 3824 | |
| 3825 | /* make sure we have a valid backup extension to use */ |
| 3826 | if (*p_bex == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3827 | backup_ext = (char_u *)".bak"; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3828 | else |
| 3829 | backup_ext = p_bex; |
| 3830 | |
| 3831 | if (backup_copy |
| 3832 | && (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) >= 0) |
| 3833 | { |
| 3834 | int bfd; |
| 3835 | char_u *copybuf, *wp; |
| 3836 | int some_error = FALSE; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 3837 | stat_T st_new; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3838 | char_u *dirp; |
| 3839 | char_u *rootname; |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 3840 | #if defined(UNIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3841 | int did_set_shortname; |
| 3842 | #endif |
| 3843 | |
| 3844 | copybuf = alloc(BUFSIZE + 1); |
| 3845 | if (copybuf == NULL) |
| 3846 | { |
| 3847 | some_error = TRUE; /* out of memory */ |
| 3848 | goto nobackup; |
| 3849 | } |
| 3850 | |
| 3851 | /* |
| 3852 | * Try to make the backup in each directory in the 'bdir' option. |
| 3853 | * |
| 3854 | * Unix semantics has it, that we may have a writable file, |
| 3855 | * that cannot be recreated with a simple open(..., O_CREAT, ) e.g: |
| 3856 | * - the directory is not writable, |
| 3857 | * - the file may be a symbolic link, |
| 3858 | * - the file may belong to another user/group, etc. |
| 3859 | * |
| 3860 | * For these reasons, the existing writable file must be truncated |
| 3861 | * and reused. Creation of a backup COPY will be attempted. |
| 3862 | */ |
| 3863 | dirp = p_bdir; |
| 3864 | while (*dirp) |
| 3865 | { |
| 3866 | #ifdef UNIX |
| 3867 | st_new.st_ino = 0; |
| 3868 | st_new.st_dev = 0; |
| 3869 | st_new.st_gid = 0; |
| 3870 | #endif |
| 3871 | |
| 3872 | /* |
| 3873 | * Isolate one directory name, using an entry in 'bdir'. |
| 3874 | */ |
| 3875 | (void)copy_option_part(&dirp, copybuf, BUFSIZE, ","); |
| 3876 | rootname = get_file_in_dir(fname, copybuf); |
| 3877 | if (rootname == NULL) |
| 3878 | { |
| 3879 | some_error = TRUE; /* out of memory */ |
| 3880 | goto nobackup; |
| 3881 | } |
| 3882 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 3883 | #if defined(UNIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3884 | did_set_shortname = FALSE; |
| 3885 | #endif |
| 3886 | |
| 3887 | /* |
| 3888 | * May try twice if 'shortname' not set. |
| 3889 | */ |
| 3890 | for (;;) |
| 3891 | { |
| 3892 | /* |
| 3893 | * Make backup file name. |
| 3894 | */ |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 3895 | backup = buf_modname((buf->b_p_sn || buf->b_shortname), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3896 | rootname, backup_ext, FALSE); |
| 3897 | if (backup == NULL) |
| 3898 | { |
| 3899 | vim_free(rootname); |
| 3900 | some_error = TRUE; /* out of memory */ |
| 3901 | goto nobackup; |
| 3902 | } |
| 3903 | |
| 3904 | /* |
| 3905 | * Check if backup file already exists. |
| 3906 | */ |
| 3907 | if (mch_stat((char *)backup, &st_new) >= 0) |
| 3908 | { |
| 3909 | #ifdef UNIX |
| 3910 | /* |
| 3911 | * Check if backup file is same as original file. |
| 3912 | * May happen when modname() gave the same file back. |
| 3913 | * E.g. silly link, or file name-length reached. |
| 3914 | * If we don't check here, we either ruin the file |
| 3915 | * when copying or erase it after writing. jw. |
| 3916 | */ |
| 3917 | if (st_new.st_dev == st_old.st_dev |
| 3918 | && st_new.st_ino == st_old.st_ino) |
| 3919 | { |
| 3920 | vim_free(backup); |
| 3921 | backup = NULL; /* no backup file to delete */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3922 | /* |
| 3923 | * may try again with 'shortname' set |
| 3924 | */ |
| 3925 | if (!(buf->b_shortname || buf->b_p_sn)) |
| 3926 | { |
| 3927 | buf->b_shortname = TRUE; |
| 3928 | did_set_shortname = TRUE; |
| 3929 | continue; |
| 3930 | } |
| 3931 | /* setting shortname didn't help */ |
| 3932 | if (did_set_shortname) |
| 3933 | buf->b_shortname = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3934 | break; |
| 3935 | } |
| 3936 | #endif |
| 3937 | |
| 3938 | /* |
| 3939 | * If we are not going to keep the backup file, don't |
| 3940 | * delete an existing one, try to use another name. |
| 3941 | * Change one character, just before the extension. |
| 3942 | */ |
| 3943 | if (!p_bk) |
| 3944 | { |
| 3945 | wp = backup + STRLEN(backup) - 1 |
| 3946 | - STRLEN(backup_ext); |
| 3947 | if (wp < backup) /* empty file name ??? */ |
| 3948 | wp = backup; |
| 3949 | *wp = 'z'; |
| 3950 | while (*wp > 'a' |
| 3951 | && mch_stat((char *)backup, &st_new) >= 0) |
| 3952 | --*wp; |
| 3953 | /* They all exist??? Must be something wrong. */ |
| 3954 | if (*wp == 'a') |
| 3955 | { |
| 3956 | vim_free(backup); |
| 3957 | backup = NULL; |
| 3958 | } |
| 3959 | } |
| 3960 | } |
| 3961 | break; |
| 3962 | } |
| 3963 | vim_free(rootname); |
| 3964 | |
| 3965 | /* |
| 3966 | * Try to create the backup file |
| 3967 | */ |
| 3968 | if (backup != NULL) |
| 3969 | { |
| 3970 | /* remove old backup, if present */ |
| 3971 | mch_remove(backup); |
| 3972 | /* Open with O_EXCL to avoid the file being created while |
| 3973 | * we were sleeping (symlink hacker attack?) */ |
| 3974 | bfd = mch_open((char *)backup, |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3975 | O_WRONLY|O_CREAT|O_EXTRA|O_EXCL|O_NOFOLLOW, |
| 3976 | perm & 0777); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3977 | if (bfd < 0) |
| 3978 | { |
| 3979 | vim_free(backup); |
| 3980 | backup = NULL; |
| 3981 | } |
| 3982 | else |
| 3983 | { |
| 3984 | /* set file protection same as original file, but |
| 3985 | * strip s-bit */ |
| 3986 | (void)mch_setperm(backup, perm & 0777); |
| 3987 | |
| 3988 | #ifdef UNIX |
| 3989 | /* |
| 3990 | * Try to set the group of the backup same as the |
| 3991 | * original file. If this fails, set the protection |
| 3992 | * bits for the group same as the protection bits for |
| 3993 | * others. |
| 3994 | */ |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3995 | if (st_new.st_gid != st_old.st_gid |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3996 | # ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */ |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3997 | && fchown(bfd, (uid_t)-1, st_old.st_gid) != 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3998 | # endif |
| 3999 | ) |
| 4000 | mch_setperm(backup, |
| 4001 | (perm & 0707) | ((perm & 07) << 3)); |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 4002 | # if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 4003 | mch_copy_sec(fname, backup); |
| 4004 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4005 | #endif |
| 4006 | |
| 4007 | /* |
| 4008 | * copy the file. |
| 4009 | */ |
| 4010 | write_info.bw_fd = bfd; |
| 4011 | write_info.bw_buf = copybuf; |
| 4012 | #ifdef HAS_BW_FLAGS |
| 4013 | write_info.bw_flags = FIO_NOCONVERT; |
| 4014 | #endif |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 4015 | while ((write_info.bw_len = read_eintr(fd, copybuf, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4016 | BUFSIZE)) > 0) |
| 4017 | { |
| 4018 | if (buf_write_bytes(&write_info) == FAIL) |
| 4019 | { |
| 4020 | errmsg = (char_u *)_("E506: Can't write to backup file (add ! to override)"); |
| 4021 | break; |
| 4022 | } |
| 4023 | ui_breakcheck(); |
| 4024 | if (got_int) |
| 4025 | { |
| 4026 | errmsg = (char_u *)_(e_interr); |
| 4027 | break; |
| 4028 | } |
| 4029 | } |
| 4030 | |
| 4031 | if (close(bfd) < 0 && errmsg == NULL) |
| 4032 | errmsg = (char_u *)_("E507: Close error for backup file (add ! to override)"); |
| 4033 | if (write_info.bw_len < 0) |
| 4034 | errmsg = (char_u *)_("E508: Can't read file for backup (add ! to override)"); |
| 4035 | #ifdef UNIX |
| 4036 | set_file_time(backup, st_old.st_atime, st_old.st_mtime); |
| 4037 | #endif |
| 4038 | #ifdef HAVE_ACL |
| 4039 | mch_set_acl(backup, acl); |
| 4040 | #endif |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 4041 | #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 4042 | mch_copy_sec(fname, backup); |
| 4043 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4044 | break; |
| 4045 | } |
| 4046 | } |
| 4047 | } |
| 4048 | nobackup: |
| 4049 | close(fd); /* ignore errors for closing read file */ |
| 4050 | vim_free(copybuf); |
| 4051 | |
| 4052 | if (backup == NULL && errmsg == NULL) |
| 4053 | errmsg = (char_u *)_("E509: Cannot create backup file (add ! to override)"); |
| 4054 | /* ignore errors when forceit is TRUE */ |
| 4055 | if ((some_error || errmsg != NULL) && !forceit) |
| 4056 | { |
| 4057 | retval = FAIL; |
| 4058 | goto fail; |
| 4059 | } |
| 4060 | errmsg = NULL; |
| 4061 | } |
| 4062 | else |
| 4063 | { |
| 4064 | char_u *dirp; |
| 4065 | char_u *p; |
| 4066 | char_u *rootname; |
| 4067 | |
| 4068 | /* |
| 4069 | * Make a backup by renaming the original file. |
| 4070 | */ |
| 4071 | /* |
| 4072 | * If 'cpoptions' includes the "W" flag, we don't want to |
| 4073 | * overwrite a read-only file. But rename may be possible |
| 4074 | * anyway, thus we need an extra check here. |
| 4075 | */ |
| 4076 | if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL) |
| 4077 | { |
| 4078 | errnum = (char_u *)"E504: "; |
| 4079 | errmsg = (char_u *)_(err_readonly); |
| 4080 | goto fail; |
| 4081 | } |
| 4082 | |
| 4083 | /* |
| 4084 | * |
| 4085 | * Form the backup file name - change path/fo.o.h to |
| 4086 | * path/fo.o.h.bak Try all directories in 'backupdir', first one |
| 4087 | * that works is used. |
| 4088 | */ |
| 4089 | dirp = p_bdir; |
| 4090 | while (*dirp) |
| 4091 | { |
| 4092 | /* |
| 4093 | * Isolate one directory name and make the backup file name. |
| 4094 | */ |
| 4095 | (void)copy_option_part(&dirp, IObuff, IOSIZE, ","); |
| 4096 | rootname = get_file_in_dir(fname, IObuff); |
| 4097 | if (rootname == NULL) |
| 4098 | backup = NULL; |
| 4099 | else |
| 4100 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4101 | backup = buf_modname((buf->b_p_sn || buf->b_shortname), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4102 | rootname, backup_ext, FALSE); |
| 4103 | vim_free(rootname); |
| 4104 | } |
| 4105 | |
| 4106 | if (backup != NULL) |
| 4107 | { |
| 4108 | /* |
| 4109 | * If we are not going to keep the backup file, don't |
| 4110 | * delete an existing one, try to use another name. |
| 4111 | * Change one character, just before the extension. |
| 4112 | */ |
| 4113 | if (!p_bk && mch_getperm(backup) >= 0) |
| 4114 | { |
| 4115 | p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext); |
| 4116 | if (p < backup) /* empty file name ??? */ |
| 4117 | p = backup; |
| 4118 | *p = 'z'; |
| 4119 | while (*p > 'a' && mch_getperm(backup) >= 0) |
| 4120 | --*p; |
| 4121 | /* They all exist??? Must be something wrong! */ |
| 4122 | if (*p == 'a') |
| 4123 | { |
| 4124 | vim_free(backup); |
| 4125 | backup = NULL; |
| 4126 | } |
| 4127 | } |
| 4128 | } |
| 4129 | if (backup != NULL) |
| 4130 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4131 | /* |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4132 | * Delete any existing backup and move the current version |
| 4133 | * to the backup. For safety, we don't remove the backup |
| 4134 | * until the write has finished successfully. And if the |
| 4135 | * 'backup' option is set, leave it around. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4136 | */ |
| 4137 | /* |
| 4138 | * If the renaming of the original file to the backup file |
| 4139 | * works, quit here. |
| 4140 | */ |
| 4141 | if (vim_rename(fname, backup) == 0) |
| 4142 | break; |
| 4143 | |
| 4144 | vim_free(backup); /* don't do the rename below */ |
| 4145 | backup = NULL; |
| 4146 | } |
| 4147 | } |
| 4148 | if (backup == NULL && !forceit) |
| 4149 | { |
| 4150 | errmsg = (char_u *)_("E510: Can't make backup file (add ! to override)"); |
| 4151 | goto fail; |
| 4152 | } |
| 4153 | } |
| 4154 | } |
| 4155 | |
Bram Moolenaar | 5307683 | 2015-12-31 19:53:21 +0100 | [diff] [blame] | 4156 | #if defined(UNIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4157 | /* When using ":w!" and the file was read-only: make it writable */ |
| 4158 | if (forceit && perm >= 0 && !(perm & 0200) && st_old.st_uid == getuid() |
| 4159 | && vim_strchr(p_cpo, CPO_FWRITE) == NULL) |
| 4160 | { |
| 4161 | perm |= 0200; |
| 4162 | (void)mch_setperm(fname, perm); |
| 4163 | made_writable = TRUE; |
| 4164 | } |
| 4165 | #endif |
| 4166 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 4167 | /* When using ":w!" and writing to the current file, 'readonly' makes no |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 4168 | * sense, reset it, unless 'Z' appears in 'cpoptions'. */ |
| 4169 | if (forceit && overwriting && vim_strchr(p_cpo, CPO_KEEPRO) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4170 | { |
| 4171 | buf->b_p_ro = FALSE; |
| 4172 | #ifdef FEAT_TITLE |
| 4173 | need_maketitle = TRUE; /* set window title later */ |
| 4174 | #endif |
| 4175 | #ifdef FEAT_WINDOWS |
| 4176 | status_redraw_all(); /* redraw status lines later */ |
| 4177 | #endif |
| 4178 | } |
| 4179 | |
| 4180 | if (end > buf->b_ml.ml_line_count) |
| 4181 | end = buf->b_ml.ml_line_count; |
| 4182 | if (buf->b_ml.ml_flags & ML_EMPTY) |
| 4183 | start = end + 1; |
| 4184 | |
| 4185 | /* |
| 4186 | * If the original file is being overwritten, there is a small chance that |
| 4187 | * we crash in the middle of writing. Therefore the file is preserved now. |
| 4188 | * This makes all block numbers positive so that recovery does not need |
| 4189 | * the original file. |
| 4190 | * Don't do this if there is a backup file and we are exiting. |
| 4191 | */ |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4192 | if (reset_changed && !newfile && overwriting |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4193 | && !(exiting && backup != NULL)) |
| 4194 | { |
| 4195 | ml_preserve(buf, FALSE); |
| 4196 | if (got_int) |
| 4197 | { |
| 4198 | errmsg = (char_u *)_(e_interr); |
| 4199 | goto restore_backup; |
| 4200 | } |
| 4201 | } |
| 4202 | |
| 4203 | #ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */ |
| 4204 | /* |
| 4205 | * Before risking to lose the original file verify if there's |
| 4206 | * a resource fork to preserve, and if cannot be done warn |
| 4207 | * the users. This happens when overwriting without backups. |
| 4208 | */ |
| 4209 | if (backup == NULL && overwriting && !append) |
| 4210 | if (mch_has_resource_fork(fname)) |
| 4211 | { |
| 4212 | errmsg = (char_u *)_("E460: The resource fork would be lost (add ! to override)"); |
| 4213 | goto restore_backup; |
| 4214 | } |
| 4215 | #endif |
| 4216 | |
| 4217 | #ifdef VMS |
| 4218 | vms_remove_version(fname); /* remove version */ |
| 4219 | #endif |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 4220 | /* Default: write the file directly. May write to a temp file for |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4221 | * multi-byte conversion. */ |
| 4222 | wfname = fname; |
| 4223 | |
| 4224 | #ifdef FEAT_MBYTE |
| 4225 | /* Check for forced 'fileencoding' from "++opt=val" argument. */ |
| 4226 | if (eap != NULL && eap->force_enc != 0) |
| 4227 | { |
| 4228 | fenc = eap->cmd + eap->force_enc; |
| 4229 | fenc = enc_canonize(fenc); |
| 4230 | fenc_tofree = fenc; |
| 4231 | } |
| 4232 | else |
| 4233 | fenc = buf->b_p_fenc; |
| 4234 | |
| 4235 | /* |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 4236 | * Check if the file needs to be converted. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4237 | */ |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 4238 | converted = need_conversion(fenc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4239 | |
| 4240 | /* |
| 4241 | * Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done. Or |
| 4242 | * Latin1 to Unicode conversion. This is handled in buf_write_bytes(). |
| 4243 | * Prepare the flags for it and allocate bw_conv_buf when needed. |
| 4244 | */ |
| 4245 | if (converted && (enc_utf8 || STRCMP(p_enc, "latin1") == 0)) |
| 4246 | { |
| 4247 | wb_flags = get_fio_flags(fenc); |
| 4248 | if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8)) |
| 4249 | { |
| 4250 | /* Need to allocate a buffer to translate into. */ |
| 4251 | if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8)) |
| 4252 | write_info.bw_conv_buflen = bufsize * 2; |
| 4253 | else /* FIO_UCS4 */ |
| 4254 | write_info.bw_conv_buflen = bufsize * 4; |
| 4255 | write_info.bw_conv_buf |
| 4256 | = lalloc((long_u)write_info.bw_conv_buflen, TRUE); |
| 4257 | if (write_info.bw_conv_buf == NULL) |
| 4258 | end = 0; |
| 4259 | } |
| 4260 | } |
| 4261 | |
| 4262 | # ifdef WIN3264 |
| 4263 | if (converted && wb_flags == 0 && (wb_flags = get_win_fio_flags(fenc)) != 0) |
| 4264 | { |
| 4265 | /* Convert UTF-8 -> UCS-2 and UCS-2 -> DBCS. Worst-case * 4: */ |
| 4266 | write_info.bw_conv_buflen = bufsize * 4; |
| 4267 | write_info.bw_conv_buf |
| 4268 | = lalloc((long_u)write_info.bw_conv_buflen, TRUE); |
| 4269 | if (write_info.bw_conv_buf == NULL) |
| 4270 | end = 0; |
| 4271 | } |
| 4272 | # endif |
| 4273 | |
| 4274 | # ifdef MACOS_X |
| 4275 | if (converted && wb_flags == 0 && (wb_flags = get_mac_fio_flags(fenc)) != 0) |
| 4276 | { |
| 4277 | write_info.bw_conv_buflen = bufsize * 3; |
| 4278 | write_info.bw_conv_buf |
| 4279 | = lalloc((long_u)write_info.bw_conv_buflen, TRUE); |
| 4280 | if (write_info.bw_conv_buf == NULL) |
| 4281 | end = 0; |
| 4282 | } |
| 4283 | # endif |
| 4284 | |
| 4285 | # if defined(FEAT_EVAL) || defined(USE_ICONV) |
| 4286 | if (converted && wb_flags == 0) |
| 4287 | { |
| 4288 | # ifdef USE_ICONV |
| 4289 | /* |
| 4290 | * Use iconv() conversion when conversion is needed and it's not done |
| 4291 | * internally. |
| 4292 | */ |
| 4293 | write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc, |
| 4294 | enc_utf8 ? (char_u *)"utf-8" : p_enc); |
| 4295 | if (write_info.bw_iconv_fd != (iconv_t)-1) |
| 4296 | { |
| 4297 | /* We're going to use iconv(), allocate a buffer to convert in. */ |
| 4298 | write_info.bw_conv_buflen = bufsize * ICONV_MULT; |
| 4299 | write_info.bw_conv_buf |
| 4300 | = lalloc((long_u)write_info.bw_conv_buflen, TRUE); |
| 4301 | if (write_info.bw_conv_buf == NULL) |
| 4302 | end = 0; |
| 4303 | write_info.bw_first = TRUE; |
| 4304 | } |
| 4305 | # ifdef FEAT_EVAL |
| 4306 | else |
| 4307 | # endif |
| 4308 | # endif |
| 4309 | |
| 4310 | # ifdef FEAT_EVAL |
| 4311 | /* |
| 4312 | * When the file needs to be converted with 'charconvert' after |
| 4313 | * writing, write to a temp file instead and let the conversion |
| 4314 | * overwrite the original file. |
| 4315 | */ |
| 4316 | if (*p_ccv != NUL) |
| 4317 | { |
Bram Moolenaar | e5c421c | 2015-03-31 13:33:08 +0200 | [diff] [blame] | 4318 | wfname = vim_tempname('w', FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4319 | if (wfname == NULL) /* Can't write without a tempfile! */ |
| 4320 | { |
| 4321 | errmsg = (char_u *)_("E214: Can't find temp file for writing"); |
| 4322 | goto restore_backup; |
| 4323 | } |
| 4324 | } |
| 4325 | # endif |
| 4326 | } |
| 4327 | # endif |
| 4328 | if (converted && wb_flags == 0 |
| 4329 | # ifdef USE_ICONV |
| 4330 | && write_info.bw_iconv_fd == (iconv_t)-1 |
| 4331 | # endif |
| 4332 | # ifdef FEAT_EVAL |
| 4333 | && wfname == fname |
| 4334 | # endif |
| 4335 | ) |
| 4336 | { |
| 4337 | if (!forceit) |
| 4338 | { |
| 4339 | errmsg = (char_u *)_("E213: Cannot convert (add ! to write without conversion)"); |
| 4340 | goto restore_backup; |
| 4341 | } |
| 4342 | notconverted = TRUE; |
| 4343 | } |
| 4344 | #endif |
| 4345 | |
| 4346 | /* |
| 4347 | * Open the file "wfname" for writing. |
| 4348 | * We may try to open the file twice: If we can't write to the |
| 4349 | * file and forceit is TRUE we delete the existing file and try to create |
| 4350 | * a new one. If this still fails we may have lost the original file! |
| 4351 | * (this may happen when the user reached his quotum for number of files). |
| 4352 | * Appending will fail if the file does not exist and forceit is FALSE. |
| 4353 | */ |
| 4354 | while ((fd = mch_open((char *)wfname, O_WRONLY | O_EXTRA | (append |
| 4355 | ? (forceit ? (O_APPEND | O_CREAT) : O_APPEND) |
| 4356 | : (O_CREAT | O_TRUNC)) |
Bram Moolenaar | 4317d9b | 2005-03-18 20:25:31 +0000 | [diff] [blame] | 4357 | , perm < 0 ? 0666 : (perm & 0777))) < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4358 | { |
| 4359 | /* |
| 4360 | * A forced write will try to create a new file if the old one is |
| 4361 | * still readonly. This may also happen when the directory is |
| 4362 | * read-only. In that case the mch_remove() will fail. |
| 4363 | */ |
| 4364 | if (errmsg == NULL) |
| 4365 | { |
| 4366 | #ifdef UNIX |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 4367 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4368 | |
| 4369 | /* Don't delete the file when it's a hard or symbolic link. */ |
| 4370 | if ((!newfile && st_old.st_nlink > 1) |
| 4371 | || (mch_lstat((char *)fname, &st) == 0 |
| 4372 | && (st.st_dev != st_old.st_dev |
| 4373 | || st.st_ino != st_old.st_ino))) |
| 4374 | errmsg = (char_u *)_("E166: Can't open linked file for writing"); |
| 4375 | else |
| 4376 | #endif |
| 4377 | { |
| 4378 | errmsg = (char_u *)_("E212: Can't open file for writing"); |
| 4379 | if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL |
| 4380 | && perm >= 0) |
| 4381 | { |
| 4382 | #ifdef UNIX |
| 4383 | /* we write to the file, thus it should be marked |
| 4384 | writable after all */ |
| 4385 | if (!(perm & 0200)) |
| 4386 | made_writable = TRUE; |
| 4387 | perm |= 0200; |
| 4388 | if (st_old.st_uid != getuid() || st_old.st_gid != getgid()) |
| 4389 | perm &= 0777; |
| 4390 | #endif |
| 4391 | if (!append) /* don't remove when appending */ |
| 4392 | mch_remove(wfname); |
| 4393 | continue; |
| 4394 | } |
| 4395 | } |
| 4396 | } |
| 4397 | |
| 4398 | restore_backup: |
| 4399 | { |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 4400 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4401 | |
| 4402 | /* |
| 4403 | * If we failed to open the file, we don't need a backup. Throw it |
| 4404 | * away. If we moved or removed the original file try to put the |
| 4405 | * backup in its place. |
| 4406 | */ |
| 4407 | if (backup != NULL && wfname == fname) |
| 4408 | { |
| 4409 | if (backup_copy) |
| 4410 | { |
| 4411 | /* |
| 4412 | * There is a small chance that we removed the original, |
| 4413 | * try to move the copy in its place. |
| 4414 | * This may not work if the vim_rename() fails. |
| 4415 | * In that case we leave the copy around. |
| 4416 | */ |
| 4417 | /* If file does not exist, put the copy in its place */ |
| 4418 | if (mch_stat((char *)fname, &st) < 0) |
| 4419 | vim_rename(backup, fname); |
| 4420 | /* if original file does exist throw away the copy */ |
| 4421 | if (mch_stat((char *)fname, &st) >= 0) |
| 4422 | mch_remove(backup); |
| 4423 | } |
| 4424 | else |
| 4425 | { |
| 4426 | /* try to put the original file back */ |
| 4427 | vim_rename(backup, fname); |
| 4428 | } |
| 4429 | } |
| 4430 | |
| 4431 | /* if original file no longer exists give an extra warning */ |
| 4432 | if (!newfile && mch_stat((char *)fname, &st) < 0) |
| 4433 | end = 0; |
| 4434 | } |
| 4435 | |
| 4436 | #ifdef FEAT_MBYTE |
| 4437 | if (wfname != fname) |
| 4438 | vim_free(wfname); |
| 4439 | #endif |
| 4440 | goto fail; |
| 4441 | } |
| 4442 | errmsg = NULL; |
| 4443 | |
| 4444 | #if defined(MACOS_CLASSIC) || defined(WIN3264) |
| 4445 | /* TODO: Is it need for MACOS_X? (Dany) */ |
| 4446 | /* |
| 4447 | * On macintosh copy the original files attributes (i.e. the backup) |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 4448 | * This is done in order to preserve the resource fork and the |
| 4449 | * Finder attribute (label, comments, custom icons, file creator) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4450 | */ |
| 4451 | if (backup != NULL && overwriting && !append) |
| 4452 | { |
| 4453 | if (backup_copy) |
| 4454 | (void)mch_copy_file_attribute(wfname, backup); |
| 4455 | else |
| 4456 | (void)mch_copy_file_attribute(backup, wfname); |
| 4457 | } |
| 4458 | |
| 4459 | if (!overwriting && !append) |
| 4460 | { |
| 4461 | if (buf->b_ffname != NULL) |
| 4462 | (void)mch_copy_file_attribute(buf->b_ffname, wfname); |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 4463 | /* Should copy resource fork */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4464 | } |
| 4465 | #endif |
| 4466 | |
| 4467 | write_info.bw_fd = fd; |
| 4468 | |
| 4469 | #ifdef FEAT_CRYPT |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 4470 | if (*buf->b_p_key != NUL && !filtering) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4471 | { |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 4472 | char_u *header; |
| 4473 | int header_len; |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 4474 | |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 4475 | buf->b_cryptstate = crypt_create_for_writing(crypt_get_method_nr(buf), |
| 4476 | buf->b_p_key, &header, &header_len); |
| 4477 | if (buf->b_cryptstate == NULL || header == NULL) |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 4478 | end = 0; |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 4479 | else |
| 4480 | { |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 4481 | /* Write magic number, so that Vim knows how this file is |
| 4482 | * encrypted when reading it back. */ |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 4483 | write_info.bw_buf = header; |
| 4484 | write_info.bw_len = header_len; |
| 4485 | write_info.bw_flags = FIO_NOCONVERT; |
| 4486 | if (buf_write_bytes(&write_info) == FAIL) |
| 4487 | end = 0; |
| 4488 | wb_flags |= FIO_ENCRYPTED; |
| 4489 | vim_free(header); |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 4490 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4491 | } |
| 4492 | #endif |
| 4493 | |
| 4494 | write_info.bw_buf = buffer; |
| 4495 | nchars = 0; |
| 4496 | |
| 4497 | /* use "++bin", "++nobin" or 'binary' */ |
| 4498 | if (eap != NULL && eap->force_bin != 0) |
| 4499 | write_bin = (eap->force_bin == FORCE_BIN); |
| 4500 | else |
| 4501 | write_bin = buf->b_p_bin; |
| 4502 | |
| 4503 | #ifdef FEAT_MBYTE |
| 4504 | /* |
| 4505 | * The BOM is written just after the encryption magic number. |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 4506 | * Skip it when appending and the file already existed, the BOM only makes |
| 4507 | * sense at the start of the file. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4508 | */ |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 4509 | if (buf->b_p_bomb && !write_bin && (!append || perm < 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4510 | { |
| 4511 | write_info.bw_len = make_bom(buffer, fenc); |
| 4512 | if (write_info.bw_len > 0) |
| 4513 | { |
| 4514 | /* don't convert, do encryption */ |
| 4515 | write_info.bw_flags = FIO_NOCONVERT | wb_flags; |
| 4516 | if (buf_write_bytes(&write_info) == FAIL) |
| 4517 | end = 0; |
| 4518 | else |
| 4519 | nchars += write_info.bw_len; |
| 4520 | } |
| 4521 | } |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 4522 | write_info.bw_start_lnum = start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4523 | #endif |
| 4524 | |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 4525 | #ifdef FEAT_PERSISTENT_UNDO |
| 4526 | write_undo_file = (buf->b_p_udf && overwriting && !append |
| 4527 | && !filtering && reset_changed); |
| 4528 | if (write_undo_file) |
| 4529 | /* Prepare for computing the hash value of the text. */ |
| 4530 | sha256_start(&sha_ctx); |
| 4531 | #endif |
| 4532 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4533 | write_info.bw_len = bufsize; |
| 4534 | #ifdef HAS_BW_FLAGS |
| 4535 | write_info.bw_flags = wb_flags; |
| 4536 | #endif |
| 4537 | fileformat = get_fileformat_force(buf, eap); |
| 4538 | s = buffer; |
| 4539 | len = 0; |
| 4540 | for (lnum = start; lnum <= end; ++lnum) |
| 4541 | { |
| 4542 | /* |
| 4543 | * The next while loop is done once for each character written. |
| 4544 | * Keep it fast! |
| 4545 | */ |
| 4546 | ptr = ml_get_buf(buf, lnum, FALSE) - 1; |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 4547 | #ifdef FEAT_PERSISTENT_UNDO |
| 4548 | if (write_undo_file) |
Bram Moolenaar | 442b422 | 2010-05-24 21:34:22 +0200 | [diff] [blame] | 4549 | sha256_update(&sha_ctx, ptr + 1, (UINT32_T)(STRLEN(ptr + 1) + 1)); |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 4550 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4551 | while ((c = *++ptr) != NUL) |
| 4552 | { |
| 4553 | if (c == NL) |
| 4554 | *s = NUL; /* replace newlines with NULs */ |
| 4555 | else if (c == CAR && fileformat == EOL_MAC) |
| 4556 | *s = NL; /* Mac: replace CRs with NLs */ |
| 4557 | else |
| 4558 | *s = c; |
| 4559 | ++s; |
| 4560 | if (++len != bufsize) |
| 4561 | continue; |
| 4562 | if (buf_write_bytes(&write_info) == FAIL) |
| 4563 | { |
| 4564 | end = 0; /* write error: break loop */ |
| 4565 | break; |
| 4566 | } |
| 4567 | nchars += bufsize; |
| 4568 | s = buffer; |
| 4569 | len = 0; |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 4570 | #ifdef FEAT_MBYTE |
| 4571 | write_info.bw_start_lnum = lnum; |
| 4572 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4573 | } |
| 4574 | /* write failed or last line has no EOL: stop here */ |
| 4575 | if (end == 0 |
| 4576 | || (lnum == end |
Bram Moolenaar | 34d72d4 | 2015-07-17 14:18:08 +0200 | [diff] [blame] | 4577 | && (write_bin || !buf->b_p_fixeol) |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 4578 | && (lnum == buf->b_no_eol_lnum |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4579 | || (lnum == buf->b_ml.ml_line_count && !buf->b_p_eol)))) |
| 4580 | { |
| 4581 | ++lnum; /* written the line, count it */ |
| 4582 | no_eol = TRUE; |
| 4583 | break; |
| 4584 | } |
| 4585 | if (fileformat == EOL_UNIX) |
| 4586 | *s++ = NL; |
| 4587 | else |
| 4588 | { |
| 4589 | *s++ = CAR; /* EOL_MAC or EOL_DOS: write CR */ |
| 4590 | if (fileformat == EOL_DOS) /* write CR-NL */ |
| 4591 | { |
| 4592 | if (++len == bufsize) |
| 4593 | { |
| 4594 | if (buf_write_bytes(&write_info) == FAIL) |
| 4595 | { |
| 4596 | end = 0; /* write error: break loop */ |
| 4597 | break; |
| 4598 | } |
| 4599 | nchars += bufsize; |
| 4600 | s = buffer; |
| 4601 | len = 0; |
| 4602 | } |
| 4603 | *s++ = NL; |
| 4604 | } |
| 4605 | } |
| 4606 | if (++len == bufsize && end) |
| 4607 | { |
| 4608 | if (buf_write_bytes(&write_info) == FAIL) |
| 4609 | { |
| 4610 | end = 0; /* write error: break loop */ |
| 4611 | break; |
| 4612 | } |
| 4613 | nchars += bufsize; |
| 4614 | s = buffer; |
| 4615 | len = 0; |
| 4616 | |
| 4617 | ui_breakcheck(); |
| 4618 | if (got_int) |
| 4619 | { |
| 4620 | end = 0; /* Interrupted, break loop */ |
| 4621 | break; |
| 4622 | } |
| 4623 | } |
| 4624 | #ifdef VMS |
| 4625 | /* |
| 4626 | * On VMS there is a problem: newlines get added when writing blocks |
| 4627 | * at a time. Fix it by writing a line at a time. |
| 4628 | * This is much slower! |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 4629 | * Explanation: VAX/DECC RTL insists that records in some RMS |
| 4630 | * structures end with a newline (carriage return) character, and if |
| 4631 | * they don't it adds one. |
| 4632 | * With other RMS structures it works perfect without this fix. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4633 | */ |
Bram Moolenaar | b52e260 | 2007-10-29 21:38:54 +0000 | [diff] [blame] | 4634 | if (buf->b_fab_rfm == FAB$C_VFC |
| 4635 | || ((buf->b_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4636 | { |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 4637 | int b2write; |
| 4638 | |
| 4639 | buf->b_fab_mrs = (buf->b_fab_mrs == 0 |
| 4640 | ? MIN(4096, bufsize) |
| 4641 | : MIN(buf->b_fab_mrs, bufsize)); |
| 4642 | |
| 4643 | b2write = len; |
| 4644 | while (b2write > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4645 | { |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 4646 | write_info.bw_len = MIN(b2write, buf->b_fab_mrs); |
| 4647 | if (buf_write_bytes(&write_info) == FAIL) |
| 4648 | { |
| 4649 | end = 0; |
| 4650 | break; |
| 4651 | } |
| 4652 | b2write -= MIN(b2write, buf->b_fab_mrs); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4653 | } |
| 4654 | write_info.bw_len = bufsize; |
| 4655 | nchars += len; |
| 4656 | s = buffer; |
| 4657 | len = 0; |
| 4658 | } |
| 4659 | #endif |
| 4660 | } |
| 4661 | if (len > 0 && end > 0) |
| 4662 | { |
| 4663 | write_info.bw_len = len; |
| 4664 | if (buf_write_bytes(&write_info) == FAIL) |
| 4665 | end = 0; /* write error */ |
| 4666 | nchars += len; |
| 4667 | } |
| 4668 | |
| 4669 | #if defined(UNIX) && defined(HAVE_FSYNC) |
| 4670 | /* On many journalling file systems there is a bug that causes both the |
| 4671 | * original and the backup file to be lost when halting the system right |
| 4672 | * after writing the file. That's because only the meta-data is |
| 4673 | * journalled. Syncing the file slows down the system, but assures it has |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 4674 | * been written to disk and we don't lose it. |
| 4675 | * For a device do try the fsync() but don't complain if it does not work |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 4676 | * (could be a pipe). |
| 4677 | * If the 'fsync' option is FALSE, don't fsync(). Useful for laptops. */ |
| 4678 | if (p_fs && fsync(fd) != 0 && !device) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4679 | { |
| 4680 | errmsg = (char_u *)_("E667: Fsync failed"); |
| 4681 | end = 0; |
| 4682 | } |
| 4683 | #endif |
| 4684 | |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 4685 | #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 4686 | /* Probably need to set the security context. */ |
| 4687 | if (!backup_copy) |
| 4688 | mch_copy_sec(backup, wfname); |
| 4689 | #endif |
| 4690 | |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4691 | #ifdef UNIX |
| 4692 | /* When creating a new file, set its owner/group to that of the original |
| 4693 | * file. Get the new device and inode number. */ |
| 4694 | if (backup != NULL && !backup_copy) |
| 4695 | { |
| 4696 | # ifdef HAVE_FCHOWN |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 4697 | stat_T st; |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4698 | |
| 4699 | /* don't change the owner when it's already OK, some systems remove |
| 4700 | * permission or ACL stuff */ |
| 4701 | if (mch_stat((char *)wfname, &st) < 0 |
| 4702 | || st.st_uid != st_old.st_uid |
| 4703 | || st.st_gid != st_old.st_gid) |
| 4704 | { |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4705 | ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4706 | if (perm >= 0) /* set permission again, may have changed */ |
| 4707 | (void)mch_setperm(wfname, perm); |
| 4708 | } |
| 4709 | # endif |
| 4710 | buf_setino(buf); |
| 4711 | } |
Bram Moolenaar | f1726cc | 2009-05-13 18:48:16 +0000 | [diff] [blame] | 4712 | else if (!buf->b_dev_valid) |
Bram Moolenaar | 8fa0445 | 2005-12-23 22:13:51 +0000 | [diff] [blame] | 4713 | /* Set the inode when creating a new file. */ |
| 4714 | buf_setino(buf); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4715 | #endif |
| 4716 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4717 | if (close(fd) != 0) |
| 4718 | { |
| 4719 | errmsg = (char_u *)_("E512: Close failed"); |
| 4720 | end = 0; |
| 4721 | } |
| 4722 | |
| 4723 | #ifdef UNIX |
| 4724 | if (made_writable) |
| 4725 | perm &= ~0200; /* reset 'w' bit for security reasons */ |
| 4726 | #endif |
| 4727 | if (perm >= 0) /* set perm. of new file same as old file */ |
| 4728 | (void)mch_setperm(wfname, perm); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4729 | #ifdef HAVE_ACL |
Bram Moolenaar | da41277 | 2016-07-14 20:37:07 +0200 | [diff] [blame] | 4730 | /* |
| 4731 | * Probably need to set the ACL before changing the user (can't set the |
| 4732 | * ACL on a file the user doesn't own). |
| 4733 | * On Solaris, with ZFS and the aclmode property set to "discard" (the |
| 4734 | * default), chmod() discards all part of a file's ACL that don't represent |
| 4735 | * the mode of the file. It's non-trivial for us to discover whether we're |
| 4736 | * in that situation, so we simply always re-set the ACL. |
| 4737 | */ |
| 4738 | # ifndef HAVE_SOLARIS_ZFS_ACL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4739 | if (!backup_copy) |
Bram Moolenaar | da41277 | 2016-07-14 20:37:07 +0200 | [diff] [blame] | 4740 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4741 | mch_set_acl(wfname, acl); |
| 4742 | #endif |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 4743 | #ifdef FEAT_CRYPT |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 4744 | if (buf->b_cryptstate != NULL) |
| 4745 | { |
| 4746 | crypt_free_state(buf->b_cryptstate); |
| 4747 | buf->b_cryptstate = NULL; |
| 4748 | } |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 4749 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4750 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4751 | #if defined(FEAT_MBYTE) && defined(FEAT_EVAL) |
| 4752 | if (wfname != fname) |
| 4753 | { |
| 4754 | /* |
| 4755 | * The file was written to a temp file, now it needs to be converted |
| 4756 | * with 'charconvert' to (overwrite) the output file. |
| 4757 | */ |
| 4758 | if (end != 0) |
| 4759 | { |
| 4760 | if (eval_charconvert(enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc, |
| 4761 | wfname, fname) == FAIL) |
| 4762 | { |
| 4763 | write_info.bw_conv_error = TRUE; |
| 4764 | end = 0; |
| 4765 | } |
| 4766 | } |
| 4767 | mch_remove(wfname); |
| 4768 | vim_free(wfname); |
| 4769 | } |
| 4770 | #endif |
| 4771 | |
| 4772 | if (end == 0) |
| 4773 | { |
| 4774 | if (errmsg == NULL) |
| 4775 | { |
| 4776 | #ifdef FEAT_MBYTE |
| 4777 | if (write_info.bw_conv_error) |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 4778 | { |
| 4779 | if (write_info.bw_conv_error_lnum == 0) |
| 4780 | errmsg = (char_u *)_("E513: write error, conversion failed (make 'fenc' empty to override)"); |
| 4781 | else |
| 4782 | { |
| 4783 | errmsg_allocated = TRUE; |
| 4784 | errmsg = alloc(300); |
| 4785 | vim_snprintf((char *)errmsg, 300, _("E513: write error, conversion failed in line %ld (make 'fenc' empty to override)"), |
| 4786 | (long)write_info.bw_conv_error_lnum); |
| 4787 | } |
| 4788 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4789 | else |
| 4790 | #endif |
| 4791 | if (got_int) |
| 4792 | errmsg = (char_u *)_(e_interr); |
| 4793 | else |
| 4794 | errmsg = (char_u *)_("E514: write error (file system full?)"); |
| 4795 | } |
| 4796 | |
| 4797 | /* |
| 4798 | * If we have a backup file, try to put it in place of the new file, |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 4799 | * because the new file is probably corrupt. This avoids losing the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4800 | * original file when trying to make a backup when writing the file a |
| 4801 | * second time. |
| 4802 | * When "backup_copy" is set we need to copy the backup over the new |
| 4803 | * file. Otherwise rename the backup file. |
| 4804 | * If this is OK, don't give the extra warning message. |
| 4805 | */ |
| 4806 | if (backup != NULL) |
| 4807 | { |
| 4808 | if (backup_copy) |
| 4809 | { |
| 4810 | /* This may take a while, if we were interrupted let the user |
| 4811 | * know we got the message. */ |
| 4812 | if (got_int) |
| 4813 | { |
| 4814 | MSG(_(e_interr)); |
| 4815 | out_flush(); |
| 4816 | } |
| 4817 | if ((fd = mch_open((char *)backup, O_RDONLY | O_EXTRA, 0)) >= 0) |
| 4818 | { |
| 4819 | if ((write_info.bw_fd = mch_open((char *)fname, |
Bram Moolenaar | 9be038d | 2005-03-08 22:34:32 +0000 | [diff] [blame] | 4820 | O_WRONLY | O_CREAT | O_TRUNC | O_EXTRA, |
| 4821 | perm & 0777)) >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4822 | { |
| 4823 | /* copy the file. */ |
| 4824 | write_info.bw_buf = smallbuf; |
| 4825 | #ifdef HAS_BW_FLAGS |
| 4826 | write_info.bw_flags = FIO_NOCONVERT; |
| 4827 | #endif |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 4828 | while ((write_info.bw_len = read_eintr(fd, smallbuf, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4829 | SMBUFSIZE)) > 0) |
| 4830 | if (buf_write_bytes(&write_info) == FAIL) |
| 4831 | break; |
| 4832 | |
| 4833 | if (close(write_info.bw_fd) >= 0 |
| 4834 | && write_info.bw_len == 0) |
| 4835 | end = 1; /* success */ |
| 4836 | } |
| 4837 | close(fd); /* ignore errors for closing read file */ |
| 4838 | } |
| 4839 | } |
| 4840 | else |
| 4841 | { |
| 4842 | if (vim_rename(backup, fname) == 0) |
| 4843 | end = 1; |
| 4844 | } |
| 4845 | } |
| 4846 | goto fail; |
| 4847 | } |
| 4848 | |
| 4849 | lnum -= start; /* compute number of written lines */ |
| 4850 | --no_wait_return; /* may wait for return now */ |
| 4851 | |
| 4852 | #if !(defined(UNIX) || defined(VMS)) |
| 4853 | fname = sfname; /* use shortname now, for the messages */ |
| 4854 | #endif |
| 4855 | if (!filtering) |
| 4856 | { |
| 4857 | msg_add_fname(buf, fname); /* put fname in IObuff with quotes */ |
| 4858 | c = FALSE; |
| 4859 | #ifdef FEAT_MBYTE |
| 4860 | if (write_info.bw_conv_error) |
| 4861 | { |
| 4862 | STRCAT(IObuff, _(" CONVERSION ERROR")); |
| 4863 | c = TRUE; |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 4864 | if (write_info.bw_conv_error_lnum != 0) |
Bram Moolenaar | a800b42 | 2010-06-27 01:15:55 +0200 | [diff] [blame] | 4865 | vim_snprintf_add((char *)IObuff, IOSIZE, _(" in line %ld;"), |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 4866 | (long)write_info.bw_conv_error_lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4867 | } |
| 4868 | else if (notconverted) |
| 4869 | { |
| 4870 | STRCAT(IObuff, _("[NOT converted]")); |
| 4871 | c = TRUE; |
| 4872 | } |
| 4873 | else if (converted) |
| 4874 | { |
| 4875 | STRCAT(IObuff, _("[converted]")); |
| 4876 | c = TRUE; |
| 4877 | } |
| 4878 | #endif |
| 4879 | if (device) |
| 4880 | { |
| 4881 | STRCAT(IObuff, _("[Device]")); |
| 4882 | c = TRUE; |
| 4883 | } |
| 4884 | else if (newfile) |
| 4885 | { |
| 4886 | STRCAT(IObuff, shortmess(SHM_NEW) ? _("[New]") : _("[New File]")); |
| 4887 | c = TRUE; |
| 4888 | } |
| 4889 | if (no_eol) |
| 4890 | { |
| 4891 | msg_add_eol(); |
| 4892 | c = TRUE; |
| 4893 | } |
| 4894 | /* may add [unix/dos/mac] */ |
| 4895 | if (msg_add_fileformat(fileformat)) |
| 4896 | c = TRUE; |
| 4897 | #ifdef FEAT_CRYPT |
| 4898 | if (wb_flags & FIO_ENCRYPTED) |
| 4899 | { |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 4900 | crypt_append_msg(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4901 | c = TRUE; |
| 4902 | } |
| 4903 | #endif |
| 4904 | msg_add_lines(c, (long)lnum, nchars); /* add line/char count */ |
| 4905 | if (!shortmess(SHM_WRITE)) |
| 4906 | { |
| 4907 | if (append) |
| 4908 | STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [a]") : _(" appended")); |
| 4909 | else |
| 4910 | STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [w]") : _(" written")); |
| 4911 | } |
| 4912 | |
Bram Moolenaar | 8f7fd65 | 2006-02-21 22:04:51 +0000 | [diff] [blame] | 4913 | set_keep_msg(msg_trunc_attr(IObuff, FALSE, 0), 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4916 | /* When written everything correctly: reset 'modified'. Unless not |
| 4917 | * writing to the original file and '+' is not in 'cpoptions'. */ |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 4918 | if (reset_changed && whole && !append |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4919 | #ifdef FEAT_MBYTE |
| 4920 | && !write_info.bw_conv_error |
| 4921 | #endif |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4922 | && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL) |
| 4923 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4924 | { |
| 4925 | unchanged(buf, TRUE); |
Bram Moolenaar | 086329d | 2014-10-31 19:51:36 +0100 | [diff] [blame] | 4926 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 4927 | /* b:changedtick is always incremented in unchanged() but that |
Bram Moolenaar | 086329d | 2014-10-31 19:51:36 +0100 | [diff] [blame] | 4928 | * should not trigger a TextChanged event. */ |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 4929 | if (last_changedtick + 1 == CHANGEDTICK(buf) |
Bram Moolenaar | 086329d | 2014-10-31 19:51:36 +0100 | [diff] [blame] | 4930 | && last_changedtick_buf == buf) |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 4931 | last_changedtick = CHANGEDTICK(buf); |
Bram Moolenaar | 086329d | 2014-10-31 19:51:36 +0100 | [diff] [blame] | 4932 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4933 | u_unchanged(buf); |
Bram Moolenaar | 730cde9 | 2010-06-27 05:18:54 +0200 | [diff] [blame] | 4934 | u_update_save_nr(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4935 | } |
| 4936 | |
| 4937 | /* |
| 4938 | * If written to the current file, update the timestamp of the swap file |
| 4939 | * and reset the BF_WRITE_MASK flags. Also sets buf->b_mtime. |
| 4940 | */ |
| 4941 | if (overwriting) |
| 4942 | { |
| 4943 | ml_timestamp(buf); |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 4944 | if (append) |
| 4945 | buf->b_flags &= ~BF_NEW; |
| 4946 | else |
| 4947 | buf->b_flags &= ~BF_WRITE_MASK; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4948 | } |
| 4949 | |
| 4950 | /* |
| 4951 | * If we kept a backup until now, and we are in patch mode, then we make |
| 4952 | * the backup file our 'original' file. |
| 4953 | */ |
| 4954 | if (*p_pm && dobackup) |
| 4955 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4956 | char *org = (char *)buf_modname((buf->b_p_sn || buf->b_shortname), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4957 | fname, p_pm, FALSE); |
| 4958 | |
| 4959 | if (backup != NULL) |
| 4960 | { |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 4961 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4962 | |
| 4963 | /* |
| 4964 | * If the original file does not exist yet |
| 4965 | * the current backup file becomes the original file |
| 4966 | */ |
| 4967 | if (org == NULL) |
| 4968 | EMSG(_("E205: Patchmode: can't save original file")); |
| 4969 | else if (mch_stat(org, &st) < 0) |
| 4970 | { |
| 4971 | vim_rename(backup, (char_u *)org); |
| 4972 | vim_free(backup); /* don't delete the file */ |
| 4973 | backup = NULL; |
| 4974 | #ifdef UNIX |
| 4975 | set_file_time((char_u *)org, st_old.st_atime, st_old.st_mtime); |
| 4976 | #endif |
| 4977 | } |
| 4978 | } |
| 4979 | /* |
| 4980 | * If there is no backup file, remember that a (new) file was |
| 4981 | * created. |
| 4982 | */ |
| 4983 | else |
| 4984 | { |
| 4985 | int empty_fd; |
| 4986 | |
| 4987 | if (org == NULL |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4988 | || (empty_fd = mch_open(org, |
| 4989 | O_CREAT | O_EXTRA | O_EXCL | O_NOFOLLOW, |
Bram Moolenaar | 4317d9b | 2005-03-18 20:25:31 +0000 | [diff] [blame] | 4990 | perm < 0 ? 0666 : (perm & 0777))) < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4991 | EMSG(_("E206: patchmode: can't touch empty original file")); |
| 4992 | else |
| 4993 | close(empty_fd); |
| 4994 | } |
| 4995 | if (org != NULL) |
| 4996 | { |
| 4997 | mch_setperm((char_u *)org, mch_getperm(fname) & 0777); |
| 4998 | vim_free(org); |
| 4999 | } |
| 5000 | } |
| 5001 | |
| 5002 | /* |
| 5003 | * Remove the backup unless 'backup' option is set |
| 5004 | */ |
| 5005 | if (!p_bk && backup != NULL && mch_remove(backup) != 0) |
| 5006 | EMSG(_("E207: Can't delete backup file")); |
| 5007 | |
| 5008 | #ifdef FEAT_SUN_WORKSHOP |
| 5009 | if (usingSunWorkShop) |
| 5010 | workshop_file_saved((char *) ffname); |
| 5011 | #endif |
| 5012 | |
| 5013 | goto nofail; |
| 5014 | |
| 5015 | /* |
| 5016 | * Finish up. We get here either after failure or success. |
| 5017 | */ |
| 5018 | fail: |
| 5019 | --no_wait_return; /* may wait for return now */ |
| 5020 | nofail: |
| 5021 | |
| 5022 | /* Done saving, we accept changed buffer warnings again */ |
| 5023 | buf->b_saving = FALSE; |
| 5024 | |
| 5025 | vim_free(backup); |
| 5026 | if (buffer != smallbuf) |
| 5027 | vim_free(buffer); |
| 5028 | #ifdef FEAT_MBYTE |
| 5029 | vim_free(fenc_tofree); |
| 5030 | vim_free(write_info.bw_conv_buf); |
| 5031 | # ifdef USE_ICONV |
| 5032 | if (write_info.bw_iconv_fd != (iconv_t)-1) |
| 5033 | { |
| 5034 | iconv_close(write_info.bw_iconv_fd); |
| 5035 | write_info.bw_iconv_fd = (iconv_t)-1; |
| 5036 | } |
| 5037 | # endif |
| 5038 | #endif |
| 5039 | #ifdef HAVE_ACL |
| 5040 | mch_free_acl(acl); |
| 5041 | #endif |
| 5042 | |
| 5043 | if (errmsg != NULL) |
| 5044 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5045 | int numlen = errnum != NULL ? (int)STRLEN(errnum) : 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5046 | |
| 5047 | attr = hl_attr(HLF_E); /* set highlight for error messages */ |
| 5048 | msg_add_fname(buf, |
| 5049 | #ifndef UNIX |
| 5050 | sfname |
| 5051 | #else |
| 5052 | fname |
| 5053 | #endif |
| 5054 | ); /* put file name in IObuff with quotes */ |
| 5055 | if (STRLEN(IObuff) + STRLEN(errmsg) + numlen >= IOSIZE) |
| 5056 | IObuff[IOSIZE - STRLEN(errmsg) - numlen - 1] = NUL; |
| 5057 | /* If the error message has the form "is ...", put the error number in |
| 5058 | * front of the file name. */ |
| 5059 | if (errnum != NULL) |
| 5060 | { |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 5061 | STRMOVE(IObuff + numlen, IObuff); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5062 | mch_memmove(IObuff, errnum, (size_t)numlen); |
| 5063 | } |
| 5064 | STRCAT(IObuff, errmsg); |
| 5065 | emsg(IObuff); |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 5066 | if (errmsg_allocated) |
| 5067 | vim_free(errmsg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5068 | |
| 5069 | retval = FAIL; |
| 5070 | if (end == 0) |
| 5071 | { |
| 5072 | MSG_PUTS_ATTR(_("\nWARNING: Original file may be lost or damaged\n"), |
| 5073 | attr | MSG_HIST); |
| 5074 | MSG_PUTS_ATTR(_("don't quit the editor until the file is successfully written!"), |
| 5075 | attr | MSG_HIST); |
| 5076 | |
| 5077 | /* Update the timestamp to avoid an "overwrite changed file" |
| 5078 | * prompt when writing again. */ |
| 5079 | if (mch_stat((char *)fname, &st_old) >= 0) |
| 5080 | { |
| 5081 | buf_store_time(buf, &st_old, fname); |
| 5082 | buf->b_mtime_read = buf->b_mtime; |
| 5083 | } |
| 5084 | } |
| 5085 | } |
| 5086 | msg_scroll = msg_save; |
| 5087 | |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 5088 | #ifdef FEAT_PERSISTENT_UNDO |
| 5089 | /* |
| 5090 | * When writing the whole file and 'undofile' is set, also write the undo |
| 5091 | * file. |
| 5092 | */ |
| 5093 | if (retval == OK && write_undo_file) |
| 5094 | { |
| 5095 | char_u hash[UNDO_HASH_SIZE]; |
| 5096 | |
| 5097 | sha256_finish(&sha_ctx, hash); |
| 5098 | u_write_undo(NULL, FALSE, buf, hash); |
| 5099 | } |
| 5100 | #endif |
| 5101 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5102 | #ifdef FEAT_AUTOCMD |
| 5103 | #ifdef FEAT_EVAL |
| 5104 | if (!should_abort(retval)) |
| 5105 | #else |
| 5106 | if (!got_int) |
| 5107 | #endif |
| 5108 | { |
| 5109 | aco_save_T aco; |
| 5110 | |
Bram Moolenaar | 68a33fc | 2012-04-25 16:50:48 +0200 | [diff] [blame] | 5111 | curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */ |
| 5112 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5113 | /* |
| 5114 | * Apply POST autocommands. |
| 5115 | * Careful: The autocommands may call buf_write() recursively! |
| 5116 | */ |
| 5117 | aucmd_prepbuf(&aco, buf); |
| 5118 | |
| 5119 | if (append) |
| 5120 | apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname, |
| 5121 | FALSE, curbuf, eap); |
| 5122 | else if (filtering) |
| 5123 | apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname, |
| 5124 | FALSE, curbuf, eap); |
| 5125 | else if (reset_changed && whole) |
| 5126 | apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname, |
| 5127 | FALSE, curbuf, eap); |
| 5128 | else |
| 5129 | apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname, |
| 5130 | FALSE, curbuf, eap); |
| 5131 | |
| 5132 | /* restore curwin/curbuf and a few other things */ |
| 5133 | aucmd_restbuf(&aco); |
| 5134 | |
| 5135 | #ifdef FEAT_EVAL |
| 5136 | if (aborting()) /* autocmds may abort script processing */ |
| 5137 | retval = FALSE; |
| 5138 | #endif |
| 5139 | } |
| 5140 | #endif |
| 5141 | |
| 5142 | got_int |= prev_got_int; |
| 5143 | |
| 5144 | #ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */ |
| 5145 | /* Update machine specific information. */ |
| 5146 | mch_post_buffer_write(buf); |
| 5147 | #endif |
| 5148 | return retval; |
| 5149 | } |
| 5150 | |
| 5151 | /* |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 5152 | * Set the name of the current buffer. Use when the buffer doesn't have a |
| 5153 | * name and a ":r" or ":w" command with a file name is used. |
| 5154 | */ |
| 5155 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5156 | set_rw_fname(char_u *fname, char_u *sfname) |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 5157 | { |
| 5158 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 8b38e24 | 2009-06-16 13:35:20 +0000 | [diff] [blame] | 5159 | buf_T *buf = curbuf; |
| 5160 | |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 5161 | /* It's like the unnamed buffer is deleted.... */ |
| 5162 | if (curbuf->b_p_bl) |
| 5163 | apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); |
| 5164 | apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf); |
| 5165 | # ifdef FEAT_EVAL |
| 5166 | if (aborting()) /* autocmds may abort script processing */ |
| 5167 | return FAIL; |
| 5168 | # endif |
Bram Moolenaar | 8b38e24 | 2009-06-16 13:35:20 +0000 | [diff] [blame] | 5169 | if (curbuf != buf) |
| 5170 | { |
| 5171 | /* We are in another buffer now, don't do the renaming. */ |
| 5172 | EMSG(_(e_auchangedbuf)); |
| 5173 | return FAIL; |
| 5174 | } |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 5175 | #endif |
| 5176 | |
| 5177 | if (setfname(curbuf, fname, sfname, FALSE) == OK) |
| 5178 | curbuf->b_flags |= BF_NOTEDITED; |
| 5179 | |
| 5180 | #ifdef FEAT_AUTOCMD |
| 5181 | /* ....and a new named one is created */ |
| 5182 | apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf); |
| 5183 | if (curbuf->b_p_bl) |
| 5184 | apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf); |
| 5185 | # ifdef FEAT_EVAL |
| 5186 | if (aborting()) /* autocmds may abort script processing */ |
| 5187 | return FAIL; |
| 5188 | # endif |
| 5189 | |
| 5190 | /* Do filetype detection now if 'filetype' is empty. */ |
| 5191 | if (*curbuf->b_p_ft == NUL) |
| 5192 | { |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5193 | if (au_has_group((char_u *)"filetypedetect")) |
Bram Moolenaar | 1610d05 | 2016-06-09 22:53:01 +0200 | [diff] [blame] | 5194 | (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL); |
Bram Moolenaar | a3227e2 | 2006-03-08 21:32:40 +0000 | [diff] [blame] | 5195 | do_modelines(0); |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 5196 | } |
| 5197 | #endif |
| 5198 | |
| 5199 | return OK; |
| 5200 | } |
| 5201 | |
| 5202 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5203 | * Put file name into IObuff with quotes. |
| 5204 | */ |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 5205 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5206 | msg_add_fname(buf_T *buf, char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5207 | { |
| 5208 | if (fname == NULL) |
| 5209 | fname = (char_u *)"-stdin-"; |
| 5210 | home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE); |
| 5211 | IObuff[0] = '"'; |
| 5212 | STRCAT(IObuff, "\" "); |
| 5213 | } |
| 5214 | |
| 5215 | /* |
| 5216 | * Append message for text mode to IObuff. |
| 5217 | * Return TRUE if something appended. |
| 5218 | */ |
| 5219 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5220 | msg_add_fileformat(int eol_type) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5221 | { |
| 5222 | #ifndef USE_CRNL |
| 5223 | if (eol_type == EOL_DOS) |
| 5224 | { |
| 5225 | STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]")); |
| 5226 | return TRUE; |
| 5227 | } |
| 5228 | #endif |
| 5229 | #ifndef USE_CR |
| 5230 | if (eol_type == EOL_MAC) |
| 5231 | { |
| 5232 | STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]")); |
| 5233 | return TRUE; |
| 5234 | } |
| 5235 | #endif |
| 5236 | #if defined(USE_CRNL) || defined(USE_CR) |
| 5237 | if (eol_type == EOL_UNIX) |
| 5238 | { |
| 5239 | STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]")); |
| 5240 | return TRUE; |
| 5241 | } |
| 5242 | #endif |
| 5243 | return FALSE; |
| 5244 | } |
| 5245 | |
| 5246 | /* |
| 5247 | * Append line and character count to IObuff. |
| 5248 | */ |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 5249 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5250 | msg_add_lines( |
| 5251 | int insert_space, |
| 5252 | long lnum, |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 5253 | off_T nchars) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5254 | { |
| 5255 | char_u *p; |
| 5256 | |
| 5257 | p = IObuff + STRLEN(IObuff); |
| 5258 | |
| 5259 | if (insert_space) |
| 5260 | *p++ = ' '; |
| 5261 | if (shortmess(SHM_LINES)) |
Bram Moolenaar | bde9810 | 2016-07-01 20:03:42 +0200 | [diff] [blame] | 5262 | vim_snprintf((char *)p, IOSIZE - (p - IObuff), |
| 5263 | "%ldL, %lldC", lnum, (varnumber_T)nchars); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5264 | else |
| 5265 | { |
| 5266 | if (lnum == 1) |
| 5267 | STRCPY(p, _("1 line, ")); |
| 5268 | else |
| 5269 | sprintf((char *)p, _("%ld lines, "), lnum); |
| 5270 | p += STRLEN(p); |
| 5271 | if (nchars == 1) |
| 5272 | STRCPY(p, _("1 character")); |
| 5273 | else |
Bram Moolenaar | bde9810 | 2016-07-01 20:03:42 +0200 | [diff] [blame] | 5274 | vim_snprintf((char *)p, IOSIZE - (p - IObuff), |
| 5275 | _("%lld characters"), (varnumber_T)nchars); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5276 | } |
| 5277 | } |
| 5278 | |
| 5279 | /* |
| 5280 | * Append message for missing line separator to IObuff. |
| 5281 | */ |
| 5282 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5283 | msg_add_eol(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5284 | { |
| 5285 | STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]")); |
| 5286 | } |
| 5287 | |
| 5288 | /* |
| 5289 | * Check modification time of file, before writing to it. |
| 5290 | * The size isn't checked, because using a tool like "gzip" takes care of |
| 5291 | * using the same timestamp but can't set the size. |
| 5292 | */ |
| 5293 | static int |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 5294 | check_mtime(buf_T *buf, stat_T *st) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5295 | { |
| 5296 | if (buf->b_mtime_read != 0 |
| 5297 | && time_differs((long)st->st_mtime, buf->b_mtime_read)) |
| 5298 | { |
| 5299 | msg_scroll = TRUE; /* don't overwrite messages here */ |
| 5300 | msg_silent = 0; /* must give this prompt */ |
| 5301 | /* don't use emsg() here, don't want to flush the buffers */ |
| 5302 | MSG_ATTR(_("WARNING: The file has been changed since reading it!!!"), |
| 5303 | hl_attr(HLF_E)); |
| 5304 | if (ask_yesno((char_u *)_("Do you really want to write to it"), |
| 5305 | TRUE) == 'n') |
| 5306 | return FAIL; |
| 5307 | msg_scroll = FALSE; /* always overwrite the file message now */ |
| 5308 | } |
| 5309 | return OK; |
| 5310 | } |
| 5311 | |
| 5312 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5313 | time_differs(long t1, long t2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5314 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5315 | #if defined(__linux__) || defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5316 | /* On a FAT filesystem, esp. under Linux, there are only 5 bits to store |
| 5317 | * the seconds. Since the roundoff is done when flushing the inode, the |
| 5318 | * time may change unexpectedly by one second!!! */ |
| 5319 | return (t1 - t2 > 1 || t2 - t1 > 1); |
| 5320 | #else |
| 5321 | return (t1 != t2); |
| 5322 | #endif |
| 5323 | } |
| 5324 | |
| 5325 | /* |
| 5326 | * Call write() to write a number of bytes to the file. |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 5327 | * Handles encryption and 'encoding' conversion. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5328 | * |
| 5329 | * Return FAIL for failure, OK otherwise. |
| 5330 | */ |
| 5331 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5332 | buf_write_bytes(struct bw_info *ip) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5333 | { |
| 5334 | int wlen; |
| 5335 | char_u *buf = ip->bw_buf; /* data to write */ |
| 5336 | int len = ip->bw_len; /* length of data */ |
| 5337 | #ifdef HAS_BW_FLAGS |
| 5338 | int flags = ip->bw_flags; /* extra flags */ |
| 5339 | #endif |
| 5340 | |
| 5341 | #ifdef FEAT_MBYTE |
| 5342 | /* |
| 5343 | * Skip conversion when writing the crypt magic number or the BOM. |
| 5344 | */ |
| 5345 | if (!(flags & FIO_NOCONVERT)) |
| 5346 | { |
| 5347 | char_u *p; |
| 5348 | unsigned c; |
| 5349 | int n; |
| 5350 | |
| 5351 | if (flags & FIO_UTF8) |
| 5352 | { |
| 5353 | /* |
| 5354 | * Convert latin1 in the buffer to UTF-8 in the file. |
| 5355 | */ |
| 5356 | p = ip->bw_conv_buf; /* translate to buffer */ |
| 5357 | for (wlen = 0; wlen < len; ++wlen) |
| 5358 | p += utf_char2bytes(buf[wlen], p); |
| 5359 | buf = ip->bw_conv_buf; |
| 5360 | len = (int)(p - ip->bw_conv_buf); |
| 5361 | } |
| 5362 | else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1)) |
| 5363 | { |
| 5364 | /* |
| 5365 | * Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or |
| 5366 | * Latin1 chars in the file. |
| 5367 | */ |
| 5368 | if (flags & FIO_LATIN1) |
| 5369 | p = buf; /* translate in-place (can only get shorter) */ |
| 5370 | else |
| 5371 | p = ip->bw_conv_buf; /* translate to buffer */ |
| 5372 | for (wlen = 0; wlen < len; wlen += n) |
| 5373 | { |
| 5374 | if (wlen == 0 && ip->bw_restlen != 0) |
| 5375 | { |
| 5376 | int l; |
| 5377 | |
| 5378 | /* Use remainder of previous call. Append the start of |
| 5379 | * buf[] to get a full sequence. Might still be too |
| 5380 | * short! */ |
| 5381 | l = CONV_RESTLEN - ip->bw_restlen; |
| 5382 | if (l > len) |
| 5383 | l = len; |
| 5384 | mch_memmove(ip->bw_rest + ip->bw_restlen, buf, (size_t)l); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5385 | n = utf_ptr2len_len(ip->bw_rest, ip->bw_restlen + l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5386 | if (n > ip->bw_restlen + len) |
| 5387 | { |
| 5388 | /* We have an incomplete byte sequence at the end to |
| 5389 | * be written. We can't convert it without the |
| 5390 | * remaining bytes. Keep them for the next call. */ |
| 5391 | if (ip->bw_restlen + len > CONV_RESTLEN) |
| 5392 | return FAIL; |
| 5393 | ip->bw_restlen += len; |
| 5394 | break; |
| 5395 | } |
| 5396 | if (n > 1) |
| 5397 | c = utf_ptr2char(ip->bw_rest); |
| 5398 | else |
| 5399 | c = ip->bw_rest[0]; |
| 5400 | if (n >= ip->bw_restlen) |
| 5401 | { |
| 5402 | n -= ip->bw_restlen; |
| 5403 | ip->bw_restlen = 0; |
| 5404 | } |
| 5405 | else |
| 5406 | { |
| 5407 | ip->bw_restlen -= n; |
| 5408 | mch_memmove(ip->bw_rest, ip->bw_rest + n, |
| 5409 | (size_t)ip->bw_restlen); |
| 5410 | n = 0; |
| 5411 | } |
| 5412 | } |
| 5413 | else |
| 5414 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5415 | n = utf_ptr2len_len(buf + wlen, len - wlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5416 | if (n > len - wlen) |
| 5417 | { |
| 5418 | /* We have an incomplete byte sequence at the end to |
| 5419 | * be written. We can't convert it without the |
| 5420 | * remaining bytes. Keep them for the next call. */ |
| 5421 | if (len - wlen > CONV_RESTLEN) |
| 5422 | return FAIL; |
| 5423 | ip->bw_restlen = len - wlen; |
| 5424 | mch_memmove(ip->bw_rest, buf + wlen, |
| 5425 | (size_t)ip->bw_restlen); |
| 5426 | break; |
| 5427 | } |
| 5428 | if (n > 1) |
| 5429 | c = utf_ptr2char(buf + wlen); |
| 5430 | else |
| 5431 | c = buf[wlen]; |
| 5432 | } |
| 5433 | |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 5434 | if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error) |
| 5435 | { |
| 5436 | ip->bw_conv_error = TRUE; |
| 5437 | ip->bw_conv_error_lnum = ip->bw_start_lnum; |
| 5438 | } |
| 5439 | if (c == NL) |
| 5440 | ++ip->bw_start_lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5441 | } |
| 5442 | if (flags & FIO_LATIN1) |
| 5443 | len = (int)(p - buf); |
| 5444 | else |
| 5445 | { |
| 5446 | buf = ip->bw_conv_buf; |
| 5447 | len = (int)(p - ip->bw_conv_buf); |
| 5448 | } |
| 5449 | } |
| 5450 | |
| 5451 | # ifdef WIN3264 |
| 5452 | else if (flags & FIO_CODEPAGE) |
| 5453 | { |
| 5454 | /* |
| 5455 | * Convert UTF-8 or codepage to UCS-2 and then to MS-Windows |
| 5456 | * codepage. |
| 5457 | */ |
| 5458 | char_u *from; |
| 5459 | size_t fromlen; |
| 5460 | char_u *to; |
| 5461 | int u8c; |
| 5462 | BOOL bad = FALSE; |
| 5463 | int needed; |
| 5464 | |
| 5465 | if (ip->bw_restlen > 0) |
| 5466 | { |
| 5467 | /* Need to concatenate the remainder of the previous call and |
| 5468 | * the bytes of the current call. Use the end of the |
| 5469 | * conversion buffer for this. */ |
| 5470 | fromlen = len + ip->bw_restlen; |
| 5471 | from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; |
| 5472 | mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); |
| 5473 | mch_memmove(from + ip->bw_restlen, buf, (size_t)len); |
| 5474 | } |
| 5475 | else |
| 5476 | { |
| 5477 | from = buf; |
| 5478 | fromlen = len; |
| 5479 | } |
| 5480 | |
| 5481 | to = ip->bw_conv_buf; |
| 5482 | if (enc_utf8) |
| 5483 | { |
| 5484 | /* Convert from UTF-8 to UCS-2, to the start of the buffer. |
| 5485 | * The buffer has been allocated to be big enough. */ |
| 5486 | while (fromlen > 0) |
| 5487 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5488 | n = (int)utf_ptr2len_len(from, (int)fromlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5489 | if (n > (int)fromlen) /* incomplete byte sequence */ |
| 5490 | break; |
| 5491 | u8c = utf_ptr2char(from); |
| 5492 | *to++ = (u8c & 0xff); |
| 5493 | *to++ = (u8c >> 8); |
| 5494 | fromlen -= n; |
| 5495 | from += n; |
| 5496 | } |
| 5497 | |
| 5498 | /* Copy remainder to ip->bw_rest[] to be used for the next |
| 5499 | * call. */ |
| 5500 | if (fromlen > CONV_RESTLEN) |
| 5501 | { |
| 5502 | /* weird overlong sequence */ |
| 5503 | ip->bw_conv_error = TRUE; |
| 5504 | return FAIL; |
| 5505 | } |
| 5506 | mch_memmove(ip->bw_rest, from, fromlen); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5507 | ip->bw_restlen = (int)fromlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5508 | } |
| 5509 | else |
| 5510 | { |
| 5511 | /* Convert from enc_codepage to UCS-2, to the start of the |
| 5512 | * buffer. The buffer has been allocated to be big enough. */ |
| 5513 | ip->bw_restlen = 0; |
| 5514 | needed = MultiByteToWideChar(enc_codepage, |
Bram Moolenaar | 36f5ac0 | 2007-05-06 12:40:34 +0000 | [diff] [blame] | 5515 | MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5516 | NULL, 0); |
| 5517 | if (needed == 0) |
| 5518 | { |
| 5519 | /* When conversion fails there may be a trailing byte. */ |
| 5520 | needed = MultiByteToWideChar(enc_codepage, |
Bram Moolenaar | 36f5ac0 | 2007-05-06 12:40:34 +0000 | [diff] [blame] | 5521 | MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen - 1, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5522 | NULL, 0); |
| 5523 | if (needed == 0) |
| 5524 | { |
| 5525 | /* Conversion doesn't work. */ |
| 5526 | ip->bw_conv_error = TRUE; |
| 5527 | return FAIL; |
| 5528 | } |
| 5529 | /* Save the trailing byte for the next call. */ |
| 5530 | ip->bw_rest[0] = from[fromlen - 1]; |
| 5531 | ip->bw_restlen = 1; |
| 5532 | } |
| 5533 | needed = MultiByteToWideChar(enc_codepage, MB_ERR_INVALID_CHARS, |
Bram Moolenaar | 36f5ac0 | 2007-05-06 12:40:34 +0000 | [diff] [blame] | 5534 | (LPCSTR)from, (int)(fromlen - ip->bw_restlen), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5535 | (LPWSTR)to, needed); |
| 5536 | if (needed == 0) |
| 5537 | { |
| 5538 | /* Safety check: Conversion doesn't work. */ |
| 5539 | ip->bw_conv_error = TRUE; |
| 5540 | return FAIL; |
| 5541 | } |
| 5542 | to += needed * 2; |
| 5543 | } |
| 5544 | |
| 5545 | fromlen = to - ip->bw_conv_buf; |
| 5546 | buf = to; |
| 5547 | # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ |
| 5548 | if (FIO_GET_CP(flags) == CP_UTF8) |
| 5549 | { |
| 5550 | /* Convert from UCS-2 to UTF-8, using the remainder of the |
| 5551 | * conversion buffer. Fails when out of space. */ |
| 5552 | for (from = ip->bw_conv_buf; fromlen > 1; fromlen -= 2) |
| 5553 | { |
| 5554 | u8c = *from++; |
| 5555 | u8c += (*from++ << 8); |
| 5556 | to += utf_char2bytes(u8c, to); |
| 5557 | if (to + 6 >= ip->bw_conv_buf + ip->bw_conv_buflen) |
| 5558 | { |
| 5559 | ip->bw_conv_error = TRUE; |
| 5560 | return FAIL; |
| 5561 | } |
| 5562 | } |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5563 | len = (int)(to - buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5564 | } |
| 5565 | else |
| 5566 | #endif |
| 5567 | { |
| 5568 | /* Convert from UCS-2 to the codepage, using the remainder of |
| 5569 | * the conversion buffer. If the conversion uses the default |
| 5570 | * character "0", the data doesn't fit in this encoding, so |
| 5571 | * fail. */ |
| 5572 | len = WideCharToMultiByte(FIO_GET_CP(flags), 0, |
| 5573 | (LPCWSTR)ip->bw_conv_buf, (int)fromlen / sizeof(WCHAR), |
Bram Moolenaar | 36f5ac0 | 2007-05-06 12:40:34 +0000 | [diff] [blame] | 5574 | (LPSTR)to, (int)(ip->bw_conv_buflen - fromlen), 0, |
| 5575 | &bad); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5576 | if (bad) |
| 5577 | { |
| 5578 | ip->bw_conv_error = TRUE; |
| 5579 | return FAIL; |
| 5580 | } |
| 5581 | } |
| 5582 | } |
| 5583 | # endif |
| 5584 | |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 5585 | # ifdef MACOS_CONVERT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5586 | else if (flags & FIO_MACROMAN) |
| 5587 | { |
| 5588 | /* |
| 5589 | * Convert UTF-8 or latin1 to Apple MacRoman. |
| 5590 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5591 | char_u *from; |
| 5592 | size_t fromlen; |
| 5593 | |
| 5594 | if (ip->bw_restlen > 0) |
| 5595 | { |
| 5596 | /* Need to concatenate the remainder of the previous call and |
| 5597 | * the bytes of the current call. Use the end of the |
| 5598 | * conversion buffer for this. */ |
| 5599 | fromlen = len + ip->bw_restlen; |
| 5600 | from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; |
| 5601 | mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); |
| 5602 | mch_memmove(from + ip->bw_restlen, buf, (size_t)len); |
| 5603 | } |
| 5604 | else |
| 5605 | { |
| 5606 | from = buf; |
| 5607 | fromlen = len; |
| 5608 | } |
| 5609 | |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 5610 | if (enc2macroman(from, fromlen, |
| 5611 | ip->bw_conv_buf, &len, ip->bw_conv_buflen, |
| 5612 | ip->bw_rest, &ip->bw_restlen) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5613 | { |
| 5614 | ip->bw_conv_error = TRUE; |
| 5615 | return FAIL; |
| 5616 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5617 | buf = ip->bw_conv_buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5618 | } |
| 5619 | # endif |
| 5620 | |
| 5621 | # ifdef USE_ICONV |
| 5622 | if (ip->bw_iconv_fd != (iconv_t)-1) |
| 5623 | { |
| 5624 | const char *from; |
| 5625 | size_t fromlen; |
| 5626 | char *to; |
| 5627 | size_t tolen; |
| 5628 | |
| 5629 | /* Convert with iconv(). */ |
| 5630 | if (ip->bw_restlen > 0) |
| 5631 | { |
Bram Moolenaar | 5d294d1 | 2009-03-11 12:11:02 +0000 | [diff] [blame] | 5632 | char *fp; |
| 5633 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5634 | /* Need to concatenate the remainder of the previous call and |
| 5635 | * the bytes of the current call. Use the end of the |
| 5636 | * conversion buffer for this. */ |
| 5637 | fromlen = len + ip->bw_restlen; |
Bram Moolenaar | 5d294d1 | 2009-03-11 12:11:02 +0000 | [diff] [blame] | 5638 | fp = (char *)ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; |
| 5639 | mch_memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen); |
| 5640 | mch_memmove(fp + ip->bw_restlen, buf, (size_t)len); |
| 5641 | from = fp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5642 | tolen = ip->bw_conv_buflen - fromlen; |
| 5643 | } |
| 5644 | else |
| 5645 | { |
| 5646 | from = (const char *)buf; |
| 5647 | fromlen = len; |
| 5648 | tolen = ip->bw_conv_buflen; |
| 5649 | } |
| 5650 | to = (char *)ip->bw_conv_buf; |
| 5651 | |
| 5652 | if (ip->bw_first) |
| 5653 | { |
| 5654 | size_t save_len = tolen; |
| 5655 | |
| 5656 | /* output the initial shift state sequence */ |
| 5657 | (void)iconv(ip->bw_iconv_fd, NULL, NULL, &to, &tolen); |
| 5658 | |
| 5659 | /* There is a bug in iconv() on Linux (which appears to be |
| 5660 | * wide-spread) which sets "to" to NULL and messes up "tolen". |
| 5661 | */ |
| 5662 | if (to == NULL) |
| 5663 | { |
| 5664 | to = (char *)ip->bw_conv_buf; |
| 5665 | tolen = save_len; |
| 5666 | } |
| 5667 | ip->bw_first = FALSE; |
| 5668 | } |
| 5669 | |
| 5670 | /* |
| 5671 | * If iconv() has an error or there is not enough room, fail. |
| 5672 | */ |
| 5673 | if ((iconv(ip->bw_iconv_fd, (void *)&from, &fromlen, &to, &tolen) |
| 5674 | == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) |
| 5675 | || fromlen > CONV_RESTLEN) |
| 5676 | { |
| 5677 | ip->bw_conv_error = TRUE; |
| 5678 | return FAIL; |
| 5679 | } |
| 5680 | |
| 5681 | /* copy remainder to ip->bw_rest[] to be used for the next call. */ |
| 5682 | if (fromlen > 0) |
| 5683 | mch_memmove(ip->bw_rest, (void *)from, fromlen); |
| 5684 | ip->bw_restlen = (int)fromlen; |
| 5685 | |
| 5686 | buf = ip->bw_conv_buf; |
| 5687 | len = (int)((char_u *)to - ip->bw_conv_buf); |
| 5688 | } |
| 5689 | # endif |
| 5690 | } |
| 5691 | #endif /* FEAT_MBYTE */ |
| 5692 | |
| 5693 | #ifdef FEAT_CRYPT |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 5694 | if (flags & FIO_ENCRYPTED) |
| 5695 | { |
| 5696 | /* Encrypt the data. Do it in-place if possible, otherwise use an |
| 5697 | * allocated buffer. */ |
| 5698 | if (crypt_works_inplace(ip->bw_buffer->b_cryptstate)) |
| 5699 | { |
| 5700 | crypt_encode_inplace(ip->bw_buffer->b_cryptstate, buf, len); |
| 5701 | } |
| 5702 | else |
| 5703 | { |
| 5704 | char_u *outbuf; |
| 5705 | |
| 5706 | len = crypt_encode_alloc(curbuf->b_cryptstate, buf, len, &outbuf); |
| 5707 | if (len == 0) |
| 5708 | return OK; /* Crypt layer is buffering, will flush later. */ |
| 5709 | wlen = write_eintr(ip->bw_fd, outbuf, len); |
| 5710 | vim_free(outbuf); |
| 5711 | return (wlen < len) ? FAIL : OK; |
| 5712 | } |
| 5713 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5714 | #endif |
| 5715 | |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 5716 | wlen = write_eintr(ip->bw_fd, buf, len); |
| 5717 | return (wlen < len) ? FAIL : OK; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5718 | } |
| 5719 | |
| 5720 | #ifdef FEAT_MBYTE |
| 5721 | /* |
| 5722 | * Convert a Unicode character to bytes. |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 5723 | * Return TRUE for an error, FALSE when it's OK. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5724 | */ |
| 5725 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5726 | ucs2bytes( |
| 5727 | unsigned c, /* in: character */ |
| 5728 | char_u **pp, /* in/out: pointer to result */ |
| 5729 | int flags) /* FIO_ flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5730 | { |
| 5731 | char_u *p = *pp; |
| 5732 | int error = FALSE; |
| 5733 | int cc; |
| 5734 | |
| 5735 | |
| 5736 | if (flags & FIO_UCS4) |
| 5737 | { |
| 5738 | if (flags & FIO_ENDIAN_L) |
| 5739 | { |
| 5740 | *p++ = c; |
| 5741 | *p++ = (c >> 8); |
| 5742 | *p++ = (c >> 16); |
| 5743 | *p++ = (c >> 24); |
| 5744 | } |
| 5745 | else |
| 5746 | { |
| 5747 | *p++ = (c >> 24); |
| 5748 | *p++ = (c >> 16); |
| 5749 | *p++ = (c >> 8); |
| 5750 | *p++ = c; |
| 5751 | } |
| 5752 | } |
| 5753 | else if (flags & (FIO_UCS2 | FIO_UTF16)) |
| 5754 | { |
| 5755 | if (c >= 0x10000) |
| 5756 | { |
| 5757 | if (flags & FIO_UTF16) |
| 5758 | { |
| 5759 | /* Make two words, ten bits of the character in each. First |
| 5760 | * word is 0xd800 - 0xdbff, second one 0xdc00 - 0xdfff */ |
| 5761 | c -= 0x10000; |
| 5762 | if (c >= 0x100000) |
| 5763 | error = TRUE; |
| 5764 | cc = ((c >> 10) & 0x3ff) + 0xd800; |
| 5765 | if (flags & FIO_ENDIAN_L) |
| 5766 | { |
| 5767 | *p++ = cc; |
| 5768 | *p++ = ((unsigned)cc >> 8); |
| 5769 | } |
| 5770 | else |
| 5771 | { |
| 5772 | *p++ = ((unsigned)cc >> 8); |
| 5773 | *p++ = cc; |
| 5774 | } |
| 5775 | c = (c & 0x3ff) + 0xdc00; |
| 5776 | } |
| 5777 | else |
| 5778 | error = TRUE; |
| 5779 | } |
| 5780 | if (flags & FIO_ENDIAN_L) |
| 5781 | { |
| 5782 | *p++ = c; |
| 5783 | *p++ = (c >> 8); |
| 5784 | } |
| 5785 | else |
| 5786 | { |
| 5787 | *p++ = (c >> 8); |
| 5788 | *p++ = c; |
| 5789 | } |
| 5790 | } |
| 5791 | else /* Latin1 */ |
| 5792 | { |
| 5793 | if (c >= 0x100) |
| 5794 | { |
| 5795 | error = TRUE; |
| 5796 | *p++ = 0xBF; |
| 5797 | } |
| 5798 | else |
| 5799 | *p++ = c; |
| 5800 | } |
| 5801 | |
| 5802 | *pp = p; |
| 5803 | return error; |
| 5804 | } |
| 5805 | |
| 5806 | /* |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 5807 | * Return TRUE if file encoding "fenc" requires conversion from or to |
| 5808 | * 'encoding'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5809 | */ |
| 5810 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5811 | need_conversion(char_u *fenc) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5812 | { |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 5813 | int same_encoding; |
| 5814 | int enc_flags; |
| 5815 | int fenc_flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5816 | |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 5817 | if (*fenc == NUL || STRCMP(p_enc, fenc) == 0) |
Bram Moolenaar | 442b422 | 2010-05-24 21:34:22 +0200 | [diff] [blame] | 5818 | { |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 5819 | same_encoding = TRUE; |
Bram Moolenaar | 442b422 | 2010-05-24 21:34:22 +0200 | [diff] [blame] | 5820 | fenc_flags = 0; |
| 5821 | } |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 5822 | else |
| 5823 | { |
| 5824 | /* Ignore difference between "ansi" and "latin1", "ucs-4" and |
| 5825 | * "ucs-4be", etc. */ |
| 5826 | enc_flags = get_fio_flags(p_enc); |
| 5827 | fenc_flags = get_fio_flags(fenc); |
| 5828 | same_encoding = (enc_flags != 0 && fenc_flags == enc_flags); |
| 5829 | } |
| 5830 | if (same_encoding) |
| 5831 | { |
| 5832 | /* Specified encoding matches with 'encoding'. This requires |
| 5833 | * conversion when 'encoding' is Unicode but not UTF-8. */ |
| 5834 | return enc_unicode != 0; |
| 5835 | } |
| 5836 | |
| 5837 | /* Encodings differ. However, conversion is not needed when 'enc' is any |
| 5838 | * Unicode encoding and the file is UTF-8. */ |
| 5839 | return !(enc_utf8 && fenc_flags == FIO_UTF8); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5840 | } |
| 5841 | |
| 5842 | /* |
| 5843 | * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the |
| 5844 | * internal conversion. |
| 5845 | * if "ptr" is an empty string, use 'encoding'. |
| 5846 | */ |
| 5847 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5848 | get_fio_flags(char_u *ptr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5849 | { |
| 5850 | int prop; |
| 5851 | |
| 5852 | if (*ptr == NUL) |
| 5853 | ptr = p_enc; |
| 5854 | |
| 5855 | prop = enc_canon_props(ptr); |
| 5856 | if (prop & ENC_UNICODE) |
| 5857 | { |
| 5858 | if (prop & ENC_2BYTE) |
| 5859 | { |
| 5860 | if (prop & ENC_ENDIAN_L) |
| 5861 | return FIO_UCS2 | FIO_ENDIAN_L; |
| 5862 | return FIO_UCS2; |
| 5863 | } |
| 5864 | if (prop & ENC_4BYTE) |
| 5865 | { |
| 5866 | if (prop & ENC_ENDIAN_L) |
| 5867 | return FIO_UCS4 | FIO_ENDIAN_L; |
| 5868 | return FIO_UCS4; |
| 5869 | } |
| 5870 | if (prop & ENC_2WORD) |
| 5871 | { |
| 5872 | if (prop & ENC_ENDIAN_L) |
| 5873 | return FIO_UTF16 | FIO_ENDIAN_L; |
| 5874 | return FIO_UTF16; |
| 5875 | } |
| 5876 | return FIO_UTF8; |
| 5877 | } |
| 5878 | if (prop & ENC_LATIN1) |
| 5879 | return FIO_LATIN1; |
| 5880 | /* must be ENC_DBCS, requires iconv() */ |
| 5881 | return 0; |
| 5882 | } |
| 5883 | |
| 5884 | #ifdef WIN3264 |
| 5885 | /* |
| 5886 | * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed |
| 5887 | * for the conversion MS-Windows can do for us. Also accept "utf-8". |
| 5888 | * Used for conversion between 'encoding' and 'fileencoding'. |
| 5889 | */ |
| 5890 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5891 | get_win_fio_flags(char_u *ptr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5892 | { |
| 5893 | int cp; |
| 5894 | |
| 5895 | /* Cannot do this when 'encoding' is not utf-8 and not a codepage. */ |
| 5896 | if (!enc_utf8 && enc_codepage <= 0) |
| 5897 | return 0; |
| 5898 | |
| 5899 | cp = encname2codepage(ptr); |
| 5900 | if (cp == 0) |
| 5901 | { |
| 5902 | # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ |
| 5903 | if (STRCMP(ptr, "utf-8") == 0) |
| 5904 | cp = CP_UTF8; |
| 5905 | else |
| 5906 | # endif |
| 5907 | return 0; |
| 5908 | } |
| 5909 | return FIO_PUT_CP(cp) | FIO_CODEPAGE; |
| 5910 | } |
| 5911 | #endif |
| 5912 | |
| 5913 | #ifdef MACOS_X |
| 5914 | /* |
| 5915 | * Check "ptr" for a Carbon supported encoding and return the FIO_ flags |
| 5916 | * needed for the internal conversion to/from utf-8 or latin1. |
| 5917 | */ |
| 5918 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5919 | get_mac_fio_flags(char_u *ptr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5920 | { |
| 5921 | if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0) |
| 5922 | && (enc_canon_props(ptr) & ENC_MACROMAN)) |
| 5923 | return FIO_MACROMAN; |
| 5924 | return 0; |
| 5925 | } |
| 5926 | #endif |
| 5927 | |
| 5928 | /* |
| 5929 | * Check for a Unicode BOM (Byte Order Mark) at the start of p[size]. |
| 5930 | * "size" must be at least 2. |
| 5931 | * Return the name of the encoding and set "*lenp" to the length. |
| 5932 | * Returns NULL when no BOM found. |
| 5933 | */ |
| 5934 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5935 | check_for_bom( |
| 5936 | char_u *p, |
| 5937 | long size, |
| 5938 | int *lenp, |
| 5939 | int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5940 | { |
| 5941 | char *name = NULL; |
| 5942 | int len = 2; |
| 5943 | |
| 5944 | if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf |
Bram Moolenaar | ee0f5a6 | 2008-07-24 20:09:16 +0000 | [diff] [blame] | 5945 | && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5946 | { |
| 5947 | name = "utf-8"; /* EF BB BF */ |
| 5948 | len = 3; |
| 5949 | } |
| 5950 | else if (p[0] == 0xff && p[1] == 0xfe) |
| 5951 | { |
| 5952 | if (size >= 4 && p[2] == 0 && p[3] == 0 |
| 5953 | && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L))) |
| 5954 | { |
| 5955 | name = "ucs-4le"; /* FF FE 00 00 */ |
| 5956 | len = 4; |
| 5957 | } |
Bram Moolenaar | 223a189 | 2008-11-11 20:57:11 +0000 | [diff] [blame] | 5958 | else if (flags == (FIO_UCS2 | FIO_ENDIAN_L)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5959 | name = "ucs-2le"; /* FF FE */ |
Bram Moolenaar | 223a189 | 2008-11-11 20:57:11 +0000 | [diff] [blame] | 5960 | else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L)) |
| 5961 | /* utf-16le is preferred, it also works for ucs-2le text */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5962 | name = "utf-16le"; /* FF FE */ |
| 5963 | } |
| 5964 | else if (p[0] == 0xfe && p[1] == 0xff |
| 5965 | && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16)) |
| 5966 | { |
Bram Moolenaar | ffd82c5 | 2008-02-20 17:15:26 +0000 | [diff] [blame] | 5967 | /* Default to utf-16, it works also for ucs-2 text. */ |
| 5968 | if (flags == FIO_UCS2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5969 | name = "ucs-2"; /* FE FF */ |
Bram Moolenaar | ffd82c5 | 2008-02-20 17:15:26 +0000 | [diff] [blame] | 5970 | else |
| 5971 | name = "utf-16"; /* FE FF */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5972 | } |
| 5973 | else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe |
| 5974 | && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4)) |
| 5975 | { |
| 5976 | name = "ucs-4"; /* 00 00 FE FF */ |
| 5977 | len = 4; |
| 5978 | } |
| 5979 | |
| 5980 | *lenp = len; |
| 5981 | return (char_u *)name; |
| 5982 | } |
| 5983 | |
| 5984 | /* |
| 5985 | * Generate a BOM in "buf[4]" for encoding "name". |
| 5986 | * Return the length of the BOM (zero when no BOM). |
| 5987 | */ |
| 5988 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5989 | make_bom(char_u *buf, char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5990 | { |
| 5991 | int flags; |
| 5992 | char_u *p; |
| 5993 | |
| 5994 | flags = get_fio_flags(name); |
| 5995 | |
| 5996 | /* Can't put a BOM in a non-Unicode file. */ |
| 5997 | if (flags == FIO_LATIN1 || flags == 0) |
| 5998 | return 0; |
| 5999 | |
| 6000 | if (flags == FIO_UTF8) /* UTF-8 */ |
| 6001 | { |
| 6002 | buf[0] = 0xef; |
| 6003 | buf[1] = 0xbb; |
| 6004 | buf[2] = 0xbf; |
| 6005 | return 3; |
| 6006 | } |
| 6007 | p = buf; |
| 6008 | (void)ucs2bytes(0xfeff, &p, flags); |
| 6009 | return (int)(p - buf); |
| 6010 | } |
| 6011 | #endif |
| 6012 | |
Bram Moolenaar | d4cacdf | 2007-10-03 10:50:10 +0000 | [diff] [blame] | 6013 | #if defined(FEAT_VIMINFO) || defined(FEAT_BROWSE) || \ |
Bram Moolenaar | a0174af | 2008-01-02 20:08:25 +0000 | [diff] [blame] | 6014 | defined(FEAT_QUICKFIX) || defined(FEAT_AUTOCMD) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6015 | /* |
| 6016 | * Try to find a shortname by comparing the fullname with the current |
| 6017 | * directory. |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 6018 | * Returns "full_path" or pointer into "full_path" if shortened. |
| 6019 | */ |
| 6020 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6021 | shorten_fname1(char_u *full_path) |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 6022 | { |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 6023 | char_u *dirname; |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 6024 | char_u *p = full_path; |
| 6025 | |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 6026 | dirname = alloc(MAXPATHL); |
| 6027 | if (dirname == NULL) |
| 6028 | return full_path; |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 6029 | if (mch_dirname(dirname, MAXPATHL) == OK) |
| 6030 | { |
| 6031 | p = shorten_fname(full_path, dirname); |
| 6032 | if (p == NULL || *p == NUL) |
| 6033 | p = full_path; |
| 6034 | } |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 6035 | vim_free(dirname); |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 6036 | return p; |
| 6037 | } |
Bram Moolenaar | d4cacdf | 2007-10-03 10:50:10 +0000 | [diff] [blame] | 6038 | #endif |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 6039 | |
| 6040 | /* |
| 6041 | * Try to find a shortname by comparing the fullname with the current |
| 6042 | * directory. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6043 | * Returns NULL if not shorter name possible, pointer into "full_path" |
| 6044 | * otherwise. |
| 6045 | */ |
| 6046 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6047 | shorten_fname(char_u *full_path, char_u *dir_name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6048 | { |
| 6049 | int len; |
| 6050 | char_u *p; |
| 6051 | |
| 6052 | if (full_path == NULL) |
| 6053 | return NULL; |
| 6054 | len = (int)STRLEN(dir_name); |
| 6055 | if (fnamencmp(dir_name, full_path, len) == 0) |
| 6056 | { |
| 6057 | p = full_path + len; |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 6058 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6059 | /* |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 6060 | * MSWIN: when a file is in the root directory, dir_name will end in a |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6061 | * slash, since C: by itself does not define a specific dir. In this |
| 6062 | * case p may already be correct. <negri> |
| 6063 | */ |
| 6064 | if (!((len > 2) && (*(p - 2) == ':'))) |
| 6065 | #endif |
| 6066 | { |
| 6067 | if (vim_ispathsep(*p)) |
| 6068 | ++p; |
| 6069 | #ifndef VMS /* the path separator is always part of the path */ |
| 6070 | else |
| 6071 | p = NULL; |
| 6072 | #endif |
| 6073 | } |
| 6074 | } |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 6075 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6076 | /* |
| 6077 | * When using a file in the current drive, remove the drive name: |
| 6078 | * "A:\dir\file" -> "\dir\file". This helps when moving a session file on |
| 6079 | * a floppy from "A:\dir" to "B:\dir". |
| 6080 | */ |
| 6081 | else if (len > 3 |
| 6082 | && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0]) |
| 6083 | && full_path[1] == ':' |
| 6084 | && vim_ispathsep(full_path[2])) |
| 6085 | p = full_path + 2; |
| 6086 | #endif |
| 6087 | else |
| 6088 | p = NULL; |
| 6089 | return p; |
| 6090 | } |
| 6091 | |
| 6092 | /* |
| 6093 | * Shorten filenames for all buffers. |
| 6094 | * When "force" is TRUE: Use full path from now on for files currently being |
| 6095 | * edited, both for file name and swap file name. Try to shorten the file |
| 6096 | * names a bit, if safe to do so. |
| 6097 | * When "force" is FALSE: Only try to shorten absolute file names. |
| 6098 | * For buffers that have buftype "nofile" or "scratch": never change the file |
| 6099 | * name. |
| 6100 | */ |
| 6101 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6102 | shorten_fnames(int force) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6103 | { |
| 6104 | char_u dirname[MAXPATHL]; |
| 6105 | buf_T *buf; |
| 6106 | char_u *p; |
| 6107 | |
| 6108 | mch_dirname(dirname, MAXPATHL); |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 6109 | FOR_ALL_BUFFERS(buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6110 | { |
| 6111 | if (buf->b_fname != NULL |
| 6112 | #ifdef FEAT_QUICKFIX |
| 6113 | && !bt_nofile(buf) |
| 6114 | #endif |
| 6115 | && !path_with_url(buf->b_fname) |
| 6116 | && (force |
| 6117 | || buf->b_sfname == NULL |
| 6118 | || mch_isFullName(buf->b_sfname))) |
| 6119 | { |
| 6120 | vim_free(buf->b_sfname); |
| 6121 | buf->b_sfname = NULL; |
| 6122 | p = shorten_fname(buf->b_ffname, dirname); |
| 6123 | if (p != NULL) |
| 6124 | { |
| 6125 | buf->b_sfname = vim_strsave(p); |
| 6126 | buf->b_fname = buf->b_sfname; |
| 6127 | } |
| 6128 | if (p == NULL || buf->b_fname == NULL) |
| 6129 | buf->b_fname = buf->b_ffname; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6130 | } |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 6131 | |
| 6132 | /* Always make the swap file name a full path, a "nofile" buffer may |
| 6133 | * also have a swap file. */ |
| 6134 | mf_fullname(buf->b_ml.ml_mfp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6135 | } |
| 6136 | #ifdef FEAT_WINDOWS |
| 6137 | status_redraw_all(); |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 6138 | redraw_tabline = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6139 | #endif |
| 6140 | } |
| 6141 | |
| 6142 | #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \ |
| 6143 | || defined(FEAT_GUI_MSWIN) \ |
| 6144 | || defined(FEAT_GUI_MAC) \ |
| 6145 | || defined(PROTO) |
| 6146 | /* |
| 6147 | * Shorten all filenames in "fnames[count]" by current directory. |
| 6148 | */ |
| 6149 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6150 | shorten_filenames(char_u **fnames, int count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6151 | { |
| 6152 | int i; |
| 6153 | char_u dirname[MAXPATHL]; |
| 6154 | char_u *p; |
| 6155 | |
| 6156 | if (fnames == NULL || count < 1) |
| 6157 | return; |
| 6158 | mch_dirname(dirname, sizeof(dirname)); |
| 6159 | for (i = 0; i < count; ++i) |
| 6160 | { |
| 6161 | if ((p = shorten_fname(fnames[i], dirname)) != NULL) |
| 6162 | { |
| 6163 | /* shorten_fname() returns pointer in given "fnames[i]". If free |
| 6164 | * "fnames[i]" first, "p" becomes invalid. So we need to copy |
| 6165 | * "p" first then free fnames[i]. */ |
| 6166 | p = vim_strsave(p); |
| 6167 | vim_free(fnames[i]); |
| 6168 | fnames[i] = p; |
| 6169 | } |
| 6170 | } |
| 6171 | } |
| 6172 | #endif |
| 6173 | |
| 6174 | /* |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 6175 | * add extension to file name - change path/fo.o.h to path/fo.o.h.ext or |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6176 | * fo_o_h.ext for MSDOS or when shortname option set. |
| 6177 | * |
| 6178 | * Assumed that fname is a valid name found in the filesystem we assure that |
| 6179 | * the return value is a different name and ends in 'ext'. |
| 6180 | * "ext" MUST be at most 4 characters long if it starts with a dot, 3 |
| 6181 | * characters otherwise. |
| 6182 | * Space for the returned name is allocated, must be freed later. |
| 6183 | * Returns NULL when out of memory. |
| 6184 | */ |
| 6185 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6186 | modname( |
| 6187 | char_u *fname, |
| 6188 | char_u *ext, |
| 6189 | int prepend_dot) /* may prepend a '.' to file name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6190 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 6191 | return buf_modname((curbuf->b_p_sn || curbuf->b_shortname), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6192 | fname, ext, prepend_dot); |
| 6193 | } |
| 6194 | |
| 6195 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6196 | buf_modname( |
| 6197 | int shortname, /* use 8.3 file name */ |
| 6198 | char_u *fname, |
| 6199 | char_u *ext, |
| 6200 | int prepend_dot) /* may prepend a '.' to file name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6201 | { |
| 6202 | char_u *retval; |
| 6203 | char_u *s; |
| 6204 | char_u *e; |
| 6205 | char_u *ptr; |
| 6206 | int fnamelen, extlen; |
| 6207 | |
| 6208 | extlen = (int)STRLEN(ext); |
| 6209 | |
| 6210 | /* |
| 6211 | * If there is no file name we must get the name of the current directory |
| 6212 | * (we need the full path in case :cd is used). |
| 6213 | */ |
| 6214 | if (fname == NULL || *fname == NUL) |
| 6215 | { |
| 6216 | retval = alloc((unsigned)(MAXPATHL + extlen + 3)); |
| 6217 | if (retval == NULL) |
| 6218 | return NULL; |
| 6219 | if (mch_dirname(retval, MAXPATHL) == FAIL || |
| 6220 | (fnamelen = (int)STRLEN(retval)) == 0) |
| 6221 | { |
| 6222 | vim_free(retval); |
| 6223 | return NULL; |
| 6224 | } |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 6225 | if (!after_pathsep(retval, retval + fnamelen)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6226 | { |
| 6227 | retval[fnamelen++] = PATHSEP; |
| 6228 | retval[fnamelen] = NUL; |
| 6229 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6230 | prepend_dot = FALSE; /* nothing to prepend a dot to */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6231 | } |
| 6232 | else |
| 6233 | { |
| 6234 | fnamelen = (int)STRLEN(fname); |
| 6235 | retval = alloc((unsigned)(fnamelen + extlen + 3)); |
| 6236 | if (retval == NULL) |
| 6237 | return NULL; |
| 6238 | STRCPY(retval, fname); |
| 6239 | #ifdef VMS |
| 6240 | vms_remove_version(retval); /* we do not need versions here */ |
| 6241 | #endif |
| 6242 | } |
| 6243 | |
| 6244 | /* |
| 6245 | * search backwards until we hit a '/', '\' or ':' replacing all '.' |
| 6246 | * by '_' for MSDOS or when shortname option set and ext starts with a dot. |
| 6247 | * Then truncate what is after the '/', '\' or ':' to 8 characters for |
| 6248 | * MSDOS and 26 characters for AMIGA, a lot more for UNIX. |
| 6249 | */ |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame^] | 6250 | for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6251 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6252 | if (*ext == '.' |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 6253 | #ifdef USE_LONG_FNAME |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6254 | && (!USE_LONG_FNAME || shortname) |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 6255 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6256 | && shortname |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 6257 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6258 | ) |
| 6259 | if (*ptr == '.') /* replace '.' by '_' */ |
| 6260 | *ptr = '_'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6261 | if (vim_ispathsep(*ptr)) |
Bram Moolenaar | 53180ce | 2005-07-05 21:48:14 +0000 | [diff] [blame] | 6262 | { |
| 6263 | ++ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6264 | break; |
Bram Moolenaar | 53180ce | 2005-07-05 21:48:14 +0000 | [diff] [blame] | 6265 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6266 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6267 | |
| 6268 | /* the file name has at most BASENAMELEN characters. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6269 | if (STRLEN(ptr) > (unsigned)BASENAMELEN) |
| 6270 | ptr[BASENAMELEN] = '\0'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6271 | |
| 6272 | s = ptr + STRLEN(ptr); |
| 6273 | |
| 6274 | /* |
| 6275 | * For 8.3 file names we may have to reduce the length. |
| 6276 | */ |
| 6277 | #ifdef USE_LONG_FNAME |
| 6278 | if (!USE_LONG_FNAME || shortname) |
| 6279 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6280 | if (shortname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6281 | #endif |
| 6282 | { |
| 6283 | /* |
| 6284 | * If there is no file name, or the file name ends in '/', and the |
| 6285 | * extension starts with '.', put a '_' before the dot, because just |
| 6286 | * ".ext" is invalid. |
| 6287 | */ |
| 6288 | if (fname == NULL || *fname == NUL |
| 6289 | || vim_ispathsep(fname[STRLEN(fname) - 1])) |
| 6290 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6291 | if (*ext == '.') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6292 | *s++ = '_'; |
| 6293 | } |
| 6294 | /* |
| 6295 | * If the extension starts with '.', truncate the base name at 8 |
| 6296 | * characters |
| 6297 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6298 | else if (*ext == '.') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6299 | { |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 6300 | if ((size_t)(s - ptr) > (size_t)8) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6301 | { |
| 6302 | s = ptr + 8; |
| 6303 | *s = '\0'; |
| 6304 | } |
| 6305 | } |
| 6306 | /* |
| 6307 | * If the extension doesn't start with '.', and the file name |
| 6308 | * doesn't have an extension yet, append a '.' |
| 6309 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6310 | else if ((e = vim_strchr(ptr, '.')) == NULL) |
| 6311 | *s++ = '.'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6312 | /* |
| 6313 | * If the extension doesn't start with '.', and there already is an |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 6314 | * extension, it may need to be truncated |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6315 | */ |
| 6316 | else if ((int)STRLEN(e) + extlen > 4) |
| 6317 | s = e + 4 - extlen; |
| 6318 | } |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 6319 | #if defined(USE_LONG_FNAME) || defined(WIN3264) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6320 | /* |
| 6321 | * If there is no file name, and the extension starts with '.', put a |
| 6322 | * '_' before the dot, because just ".ext" may be invalid if it's on a |
| 6323 | * FAT partition, and on HPFS it doesn't matter. |
| 6324 | */ |
| 6325 | else if ((fname == NULL || *fname == NUL) && *ext == '.') |
| 6326 | *s++ = '_'; |
| 6327 | #endif |
| 6328 | |
| 6329 | /* |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 6330 | * Append the extension. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6331 | * ext can start with '.' and cannot exceed 3 more characters. |
| 6332 | */ |
| 6333 | STRCPY(s, ext); |
| 6334 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6335 | /* |
| 6336 | * Prepend the dot. |
| 6337 | */ |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 6338 | if (prepend_dot && !shortname && *(e = gettail(retval)) != '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6339 | #ifdef USE_LONG_FNAME |
| 6340 | && USE_LONG_FNAME |
| 6341 | #endif |
| 6342 | ) |
| 6343 | { |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 6344 | STRMOVE(e + 1, e); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6345 | *e = '.'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6346 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6347 | |
| 6348 | /* |
| 6349 | * Check that, after appending the extension, the file name is really |
| 6350 | * different. |
| 6351 | */ |
| 6352 | if (fname != NULL && STRCMP(fname, retval) == 0) |
| 6353 | { |
| 6354 | /* we search for a character that can be replaced by '_' */ |
| 6355 | while (--s >= ptr) |
| 6356 | { |
| 6357 | if (*s != '_') |
| 6358 | { |
| 6359 | *s = '_'; |
| 6360 | break; |
| 6361 | } |
| 6362 | } |
| 6363 | if (s < ptr) /* fname was "________.<ext>", how tricky! */ |
| 6364 | *ptr = 'v'; |
| 6365 | } |
| 6366 | return retval; |
| 6367 | } |
| 6368 | |
| 6369 | /* |
| 6370 | * Like fgets(), but if the file line is too long, it is truncated and the |
| 6371 | * rest of the line is thrown away. Returns TRUE for end-of-file. |
| 6372 | */ |
| 6373 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6374 | vim_fgets(char_u *buf, int size, FILE *fp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6375 | { |
| 6376 | char *eof; |
| 6377 | #define FGETS_SIZE 200 |
| 6378 | char tbuf[FGETS_SIZE]; |
| 6379 | |
| 6380 | buf[size - 2] = NUL; |
| 6381 | #ifdef USE_CR |
| 6382 | eof = fgets_cr((char *)buf, size, fp); |
| 6383 | #else |
| 6384 | eof = fgets((char *)buf, size, fp); |
| 6385 | #endif |
| 6386 | if (buf[size - 2] != NUL && buf[size - 2] != '\n') |
| 6387 | { |
| 6388 | buf[size - 1] = NUL; /* Truncate the line */ |
| 6389 | |
| 6390 | /* Now throw away the rest of the line: */ |
| 6391 | do |
| 6392 | { |
| 6393 | tbuf[FGETS_SIZE - 2] = NUL; |
| 6394 | #ifdef USE_CR |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 6395 | ignoredp = fgets_cr((char *)tbuf, FGETS_SIZE, fp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6396 | #else |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 6397 | ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6398 | #endif |
| 6399 | } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n'); |
| 6400 | } |
| 6401 | return (eof == NULL); |
| 6402 | } |
| 6403 | |
| 6404 | #if defined(USE_CR) || defined(PROTO) |
| 6405 | /* |
| 6406 | * Like vim_fgets(), but accept any line terminator: CR, CR-LF or LF. |
| 6407 | * Returns TRUE for end-of-file. |
| 6408 | * Only used for the Mac, because it's much slower than vim_fgets(). |
| 6409 | */ |
| 6410 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6411 | tag_fgets(char_u *buf, int size, FILE *fp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6412 | { |
| 6413 | int i = 0; |
| 6414 | int c; |
| 6415 | int eof = FALSE; |
| 6416 | |
| 6417 | for (;;) |
| 6418 | { |
| 6419 | c = fgetc(fp); |
| 6420 | if (c == EOF) |
| 6421 | { |
| 6422 | eof = TRUE; |
| 6423 | break; |
| 6424 | } |
| 6425 | if (c == '\r') |
| 6426 | { |
| 6427 | /* Always store a NL for end-of-line. */ |
| 6428 | if (i < size - 1) |
| 6429 | buf[i++] = '\n'; |
| 6430 | c = fgetc(fp); |
| 6431 | if (c != '\n') /* Macintosh format: single CR. */ |
| 6432 | ungetc(c, fp); |
| 6433 | break; |
| 6434 | } |
| 6435 | if (i < size - 1) |
| 6436 | buf[i++] = c; |
| 6437 | if (c == '\n') |
| 6438 | break; |
| 6439 | } |
| 6440 | buf[i] = NUL; |
| 6441 | return eof; |
| 6442 | } |
| 6443 | #endif |
| 6444 | |
| 6445 | /* |
| 6446 | * rename() only works if both files are on the same file system, this |
| 6447 | * function will (attempts to?) copy the file across if rename fails -- webb |
| 6448 | * Return -1 for failure, 0 for success. |
| 6449 | */ |
| 6450 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6451 | vim_rename(char_u *from, char_u *to) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6452 | { |
| 6453 | int fd_in; |
| 6454 | int fd_out; |
| 6455 | int n; |
| 6456 | char *errmsg = NULL; |
| 6457 | char *buffer; |
| 6458 | #ifdef AMIGA |
| 6459 | BPTR flock; |
| 6460 | #endif |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 6461 | stat_T st; |
Bram Moolenaar | 9be038d | 2005-03-08 22:34:32 +0000 | [diff] [blame] | 6462 | long perm; |
Bram Moolenaar | cd71fa3 | 2005-03-11 22:46:48 +0000 | [diff] [blame] | 6463 | #ifdef HAVE_ACL |
| 6464 | vim_acl_T acl; /* ACL from original file */ |
| 6465 | #endif |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6466 | int use_tmp_file = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6467 | |
| 6468 | /* |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6469 | * When the names are identical, there is nothing to do. When they refer |
| 6470 | * to the same file (ignoring case and slash/backslash differences) but |
| 6471 | * the file name differs we need to go through a temp file. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6472 | */ |
| 6473 | if (fnamecmp(from, to) == 0) |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6474 | { |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 6475 | if (p_fic && STRCMP(gettail(from), gettail(to)) != 0) |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6476 | use_tmp_file = TRUE; |
| 6477 | else |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6478 | return 0; |
| 6479 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6480 | |
| 6481 | /* |
| 6482 | * Fail if the "from" file doesn't exist. Avoids that "to" is deleted. |
| 6483 | */ |
| 6484 | if (mch_stat((char *)from, &st) < 0) |
| 6485 | return -1; |
| 6486 | |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6487 | #ifdef UNIX |
| 6488 | { |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 6489 | stat_T st_to; |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6490 | |
| 6491 | /* It's possible for the source and destination to be the same file. |
| 6492 | * This happens when "from" and "to" differ in case and are on a FAT32 |
| 6493 | * filesystem. In that case go through a temp file name. */ |
| 6494 | if (mch_stat((char *)to, &st_to) >= 0 |
| 6495 | && st.st_dev == st_to.st_dev |
| 6496 | && st.st_ino == st_to.st_ino) |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6497 | use_tmp_file = TRUE; |
| 6498 | } |
| 6499 | #endif |
Bram Moolenaar | 1c32dff | 2011-05-05 16:41:24 +0200 | [diff] [blame] | 6500 | #ifdef WIN3264 |
| 6501 | { |
| 6502 | BY_HANDLE_FILE_INFORMATION info1, info2; |
| 6503 | |
| 6504 | /* It's possible for the source and destination to be the same file. |
| 6505 | * In that case go through a temp file name. This makes rename("foo", |
| 6506 | * "./foo") a no-op (in a complicated way). */ |
| 6507 | if (win32_fileinfo(from, &info1) == FILEINFO_OK |
| 6508 | && win32_fileinfo(to, &info2) == FILEINFO_OK |
| 6509 | && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber |
| 6510 | && info1.nFileIndexHigh == info2.nFileIndexHigh |
| 6511 | && info1.nFileIndexLow == info2.nFileIndexLow) |
| 6512 | use_tmp_file = TRUE; |
| 6513 | } |
| 6514 | #endif |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6515 | |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6516 | if (use_tmp_file) |
| 6517 | { |
| 6518 | char tempname[MAXPATHL + 1]; |
| 6519 | |
| 6520 | /* |
| 6521 | * Find a name that doesn't exist and is in the same directory. |
| 6522 | * Rename "from" to "tempname" and then rename "tempname" to "to". |
| 6523 | */ |
| 6524 | if (STRLEN(from) >= MAXPATHL - 5) |
| 6525 | return -1; |
| 6526 | STRCPY(tempname, from); |
| 6527 | for (n = 123; n < 99999; ++n) |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6528 | { |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6529 | sprintf((char *)gettail((char_u *)tempname), "%d", n); |
| 6530 | if (mch_stat(tempname, &st) < 0) |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6531 | { |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6532 | if (mch_rename((char *)from, tempname) == 0) |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6533 | { |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6534 | if (mch_rename(tempname, (char *)to) == 0) |
| 6535 | return 0; |
| 6536 | /* Strange, the second step failed. Try moving the |
| 6537 | * file back and return failure. */ |
| 6538 | mch_rename(tempname, (char *)from); |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6539 | return -1; |
| 6540 | } |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6541 | /* If it fails for one temp name it will most likely fail |
| 6542 | * for any temp name, give up. */ |
| 6543 | return -1; |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6544 | } |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6545 | } |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6546 | return -1; |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6547 | } |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6548 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6549 | /* |
| 6550 | * Delete the "to" file, this is required on some systems to make the |
| 6551 | * mch_rename() work, on other systems it makes sure that we don't have |
| 6552 | * two files when the mch_rename() fails. |
| 6553 | */ |
| 6554 | |
| 6555 | #ifdef AMIGA |
| 6556 | /* |
| 6557 | * With MSDOS-compatible filesystems (crossdos, messydos) it is possible |
| 6558 | * that the name of the "to" file is the same as the "from" file, even |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 6559 | * though the names are different. To avoid the chance of accidentally |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6560 | * deleting the "from" file (horror!) we lock it during the remove. |
| 6561 | * |
| 6562 | * When used for making a backup before writing the file: This should not |
| 6563 | * happen with ":w", because startscript() should detect this problem and |
| 6564 | * set buf->b_shortname, causing modname() to return a correct ".bak" file |
| 6565 | * name. This problem does exist with ":w filename", but then the |
| 6566 | * original file will be somewhere else so the backup isn't really |
| 6567 | * important. If autoscripting is off the rename may fail. |
| 6568 | */ |
| 6569 | flock = Lock((UBYTE *)from, (long)ACCESS_READ); |
| 6570 | #endif |
| 6571 | mch_remove(to); |
| 6572 | #ifdef AMIGA |
| 6573 | if (flock) |
| 6574 | UnLock(flock); |
| 6575 | #endif |
| 6576 | |
| 6577 | /* |
| 6578 | * First try a normal rename, return if it works. |
| 6579 | */ |
| 6580 | if (mch_rename((char *)from, (char *)to) == 0) |
| 6581 | return 0; |
| 6582 | |
| 6583 | /* |
| 6584 | * Rename() failed, try copying the file. |
| 6585 | */ |
Bram Moolenaar | 9be038d | 2005-03-08 22:34:32 +0000 | [diff] [blame] | 6586 | perm = mch_getperm(from); |
Bram Moolenaar | cd71fa3 | 2005-03-11 22:46:48 +0000 | [diff] [blame] | 6587 | #ifdef HAVE_ACL |
| 6588 | /* For systems that support ACL: get the ACL from the original file. */ |
| 6589 | acl = mch_get_acl(from); |
| 6590 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6591 | fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0); |
| 6592 | if (fd_in == -1) |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 6593 | { |
| 6594 | #ifdef HAVE_ACL |
| 6595 | mch_free_acl(acl); |
| 6596 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6597 | return -1; |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 6598 | } |
Bram Moolenaar | 9be038d | 2005-03-08 22:34:32 +0000 | [diff] [blame] | 6599 | |
| 6600 | /* Create the new file with same permissions as the original. */ |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 6601 | fd_out = mch_open((char *)to, |
| 6602 | O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6603 | if (fd_out == -1) |
| 6604 | { |
| 6605 | close(fd_in); |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 6606 | #ifdef HAVE_ACL |
| 6607 | mch_free_acl(acl); |
| 6608 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6609 | return -1; |
| 6610 | } |
| 6611 | |
| 6612 | buffer = (char *)alloc(BUFSIZE); |
| 6613 | if (buffer == NULL) |
| 6614 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6615 | close(fd_out); |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 6616 | close(fd_in); |
| 6617 | #ifdef HAVE_ACL |
| 6618 | mch_free_acl(acl); |
| 6619 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6620 | return -1; |
| 6621 | } |
| 6622 | |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 6623 | while ((n = read_eintr(fd_in, buffer, BUFSIZE)) > 0) |
| 6624 | if (write_eintr(fd_out, buffer, n) != n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6625 | { |
| 6626 | errmsg = _("E208: Error writing to \"%s\""); |
| 6627 | break; |
| 6628 | } |
| 6629 | |
| 6630 | vim_free(buffer); |
| 6631 | close(fd_in); |
| 6632 | if (close(fd_out) < 0) |
| 6633 | errmsg = _("E209: Error closing \"%s\""); |
| 6634 | if (n < 0) |
| 6635 | { |
| 6636 | errmsg = _("E210: Error reading \"%s\""); |
| 6637 | to = from; |
| 6638 | } |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 6639 | #ifndef UNIX /* for Unix mch_open() already set the permission */ |
Bram Moolenaar | 9be038d | 2005-03-08 22:34:32 +0000 | [diff] [blame] | 6640 | mch_setperm(to, perm); |
Bram Moolenaar | c6039d8 | 2005-12-02 00:44:04 +0000 | [diff] [blame] | 6641 | #endif |
Bram Moolenaar | cd71fa3 | 2005-03-11 22:46:48 +0000 | [diff] [blame] | 6642 | #ifdef HAVE_ACL |
| 6643 | mch_set_acl(to, acl); |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 6644 | mch_free_acl(acl); |
Bram Moolenaar | cd71fa3 | 2005-03-11 22:46:48 +0000 | [diff] [blame] | 6645 | #endif |
Bram Moolenaar | 5bd32f4 | 2014-04-02 14:05:38 +0200 | [diff] [blame] | 6646 | #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
Bram Moolenaar | e874744 | 2013-11-12 18:09:29 +0100 | [diff] [blame] | 6647 | mch_copy_sec(from, to); |
Bram Moolenaar | 0671de3 | 2013-11-12 05:12:03 +0100 | [diff] [blame] | 6648 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6649 | if (errmsg != NULL) |
| 6650 | { |
| 6651 | EMSG2(errmsg, to); |
| 6652 | return -1; |
| 6653 | } |
| 6654 | mch_remove(from); |
| 6655 | return 0; |
| 6656 | } |
| 6657 | |
| 6658 | static int already_warned = FALSE; |
| 6659 | |
| 6660 | /* |
| 6661 | * Check if any not hidden buffer has been changed. |
| 6662 | * Postpone the check if there are characters in the stuff buffer, a global |
| 6663 | * command is being executed, a mapping is being executed or an autocommand is |
| 6664 | * busy. |
| 6665 | * Returns TRUE if some message was written (screen should be redrawn and |
| 6666 | * cursor positioned). |
| 6667 | */ |
| 6668 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6669 | check_timestamps( |
| 6670 | int focus) /* called for GUI focus event */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6671 | { |
| 6672 | buf_T *buf; |
| 6673 | int didit = 0; |
| 6674 | int n; |
| 6675 | |
| 6676 | /* Don't check timestamps while system() or another low-level function may |
| 6677 | * cause us to lose and gain focus. */ |
| 6678 | if (no_check_timestamps > 0) |
| 6679 | return FALSE; |
| 6680 | |
| 6681 | /* Avoid doing a check twice. The OK/Reload dialog can cause a focus |
| 6682 | * event and we would keep on checking if the file is steadily growing. |
| 6683 | * Do check again after typing something. */ |
| 6684 | if (focus && did_check_timestamps) |
| 6685 | { |
| 6686 | need_check_timestamps = TRUE; |
| 6687 | return FALSE; |
| 6688 | } |
| 6689 | |
| 6690 | if (!stuff_empty() || global_busy || !typebuf_typed() |
| 6691 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 6692 | || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6693 | #endif |
| 6694 | ) |
| 6695 | need_check_timestamps = TRUE; /* check later */ |
| 6696 | else |
| 6697 | { |
| 6698 | ++no_wait_return; |
| 6699 | did_check_timestamps = TRUE; |
| 6700 | already_warned = FALSE; |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 6701 | FOR_ALL_BUFFERS(buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6702 | { |
| 6703 | /* Only check buffers in a window. */ |
| 6704 | if (buf->b_nwindows > 0) |
| 6705 | { |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6706 | bufref_T bufref; |
| 6707 | |
| 6708 | set_bufref(&bufref, buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6709 | n = buf_check_timestamp(buf, focus); |
| 6710 | if (didit < n) |
| 6711 | didit = n; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6712 | if (n > 0 && !bufref_valid(&bufref)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6713 | { |
| 6714 | /* Autocommands have removed the buffer, start at the |
| 6715 | * first one again. */ |
| 6716 | buf = firstbuf; |
| 6717 | continue; |
| 6718 | } |
| 6719 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6720 | } |
| 6721 | --no_wait_return; |
| 6722 | need_check_timestamps = FALSE; |
| 6723 | if (need_wait_return && didit == 2) |
| 6724 | { |
| 6725 | /* make sure msg isn't overwritten */ |
| 6726 | msg_puts((char_u *)"\n"); |
| 6727 | out_flush(); |
| 6728 | } |
| 6729 | } |
| 6730 | return didit; |
| 6731 | } |
| 6732 | |
| 6733 | /* |
| 6734 | * Move all the lines from buffer "frombuf" to buffer "tobuf". |
| 6735 | * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not |
| 6736 | * empty. |
| 6737 | */ |
| 6738 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6739 | move_lines(buf_T *frombuf, buf_T *tobuf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6740 | { |
| 6741 | buf_T *tbuf = curbuf; |
| 6742 | int retval = OK; |
| 6743 | linenr_T lnum; |
| 6744 | char_u *p; |
| 6745 | |
| 6746 | /* Copy the lines in "frombuf" to "tobuf". */ |
| 6747 | curbuf = tobuf; |
| 6748 | for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum) |
| 6749 | { |
| 6750 | p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE)); |
| 6751 | if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL) |
| 6752 | { |
| 6753 | vim_free(p); |
| 6754 | retval = FAIL; |
| 6755 | break; |
| 6756 | } |
| 6757 | vim_free(p); |
| 6758 | } |
| 6759 | |
| 6760 | /* Delete all the lines in "frombuf". */ |
| 6761 | if (retval != FAIL) |
| 6762 | { |
| 6763 | curbuf = frombuf; |
Bram Moolenaar | 9460b9d | 2007-01-09 14:37:01 +0000 | [diff] [blame] | 6764 | for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum) |
| 6765 | if (ml_delete(lnum, FALSE) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6766 | { |
| 6767 | /* Oops! We could try putting back the saved lines, but that |
| 6768 | * might fail again... */ |
| 6769 | retval = FAIL; |
| 6770 | break; |
| 6771 | } |
| 6772 | } |
| 6773 | |
| 6774 | curbuf = tbuf; |
| 6775 | return retval; |
| 6776 | } |
| 6777 | |
| 6778 | /* |
| 6779 | * Check if buffer "buf" has been changed. |
| 6780 | * Also check if the file for a new buffer unexpectedly appeared. |
| 6781 | * return 1 if a changed buffer was found. |
| 6782 | * return 2 if a message has been displayed. |
| 6783 | * return 0 otherwise. |
| 6784 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6785 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6786 | buf_check_timestamp( |
| 6787 | buf_T *buf, |
| 6788 | int focus UNUSED) /* called for GUI focus event */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6789 | { |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 6790 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6791 | int stat_res; |
| 6792 | int retval = 0; |
| 6793 | char_u *path; |
| 6794 | char_u *tbuf; |
| 6795 | char *mesg = NULL; |
Bram Moolenaar | 44ecf65 | 2005-03-07 23:09:59 +0000 | [diff] [blame] | 6796 | char *mesg2 = ""; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6797 | int helpmesg = FALSE; |
| 6798 | int reload = FALSE; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6799 | char *reason; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6800 | #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
| 6801 | int can_reload = FALSE; |
| 6802 | #endif |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 6803 | off_T orig_size = buf->b_orig_size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6804 | int orig_mode = buf->b_orig_mode; |
| 6805 | #ifdef FEAT_GUI |
| 6806 | int save_mouse_correct = need_mouse_correct; |
| 6807 | #endif |
| 6808 | #ifdef FEAT_AUTOCMD |
| 6809 | static int busy = FALSE; |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6810 | int n; |
| 6811 | char_u *s; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6812 | bufref_T bufref; |
| 6813 | |
| 6814 | set_bufref(&bufref, buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6815 | #endif |
| 6816 | |
| 6817 | /* If there is no file name, the buffer is not loaded, 'buftype' is |
| 6818 | * set, we are in the middle of a save or being called recursively: ignore |
| 6819 | * this buffer. */ |
| 6820 | if (buf->b_ffname == NULL |
| 6821 | || buf->b_ml.ml_mfp == NULL |
| 6822 | #if defined(FEAT_QUICKFIX) |
| 6823 | || *buf->b_p_bt != NUL |
| 6824 | #endif |
| 6825 | || buf->b_saving |
| 6826 | #ifdef FEAT_AUTOCMD |
| 6827 | || busy |
| 6828 | #endif |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 6829 | #ifdef FEAT_NETBEANS_INTG |
| 6830 | || isNetbeansBuffer(buf) |
| 6831 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6832 | ) |
| 6833 | return 0; |
| 6834 | |
| 6835 | if ( !(buf->b_flags & BF_NOTEDITED) |
| 6836 | && buf->b_mtime != 0 |
| 6837 | && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0 |
| 6838 | || time_differs((long)st.st_mtime, buf->b_mtime) |
Bram Moolenaar | a7611f6 | 2014-05-02 15:46:14 +0200 | [diff] [blame] | 6839 | || st.st_size != buf->b_orig_size |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6840 | #ifdef HAVE_ST_MODE |
| 6841 | || (int)st.st_mode != buf->b_orig_mode |
| 6842 | #else |
| 6843 | || mch_getperm(buf->b_ffname) != buf->b_orig_mode |
| 6844 | #endif |
| 6845 | )) |
| 6846 | { |
| 6847 | retval = 1; |
| 6848 | |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6849 | /* set b_mtime to stop further warnings (e.g., when executing |
| 6850 | * FileChangedShell autocmd) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6851 | if (stat_res < 0) |
| 6852 | { |
| 6853 | buf->b_mtime = 0; |
| 6854 | buf->b_orig_size = 0; |
| 6855 | buf->b_orig_mode = 0; |
| 6856 | } |
| 6857 | else |
| 6858 | buf_store_time(buf, &st, buf->b_ffname); |
| 6859 | |
| 6860 | /* Don't do anything for a directory. Might contain the file |
| 6861 | * explorer. */ |
| 6862 | if (mch_isdir(buf->b_fname)) |
| 6863 | ; |
| 6864 | |
| 6865 | /* |
| 6866 | * If 'autoread' is set, the buffer has no changes and the file still |
| 6867 | * exists, reload the buffer. Use the buffer-local option value if it |
| 6868 | * was set, the global option value otherwise. |
| 6869 | */ |
| 6870 | else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar) |
| 6871 | && !bufIsChanged(buf) && stat_res >= 0) |
| 6872 | reload = TRUE; |
| 6873 | else |
| 6874 | { |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6875 | if (stat_res < 0) |
| 6876 | reason = "deleted"; |
| 6877 | else if (bufIsChanged(buf)) |
| 6878 | reason = "conflict"; |
| 6879 | else if (orig_size != buf->b_orig_size || buf_contents_changed(buf)) |
| 6880 | reason = "changed"; |
| 6881 | else if (orig_mode != buf->b_orig_mode) |
| 6882 | reason = "mode"; |
| 6883 | else |
| 6884 | reason = "time"; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6885 | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6886 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6887 | /* |
| 6888 | * Only give the warning if there are no FileChangedShell |
| 6889 | * autocommands. |
| 6890 | * Avoid being called recursively by setting "busy". |
| 6891 | */ |
| 6892 | busy = TRUE; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 6893 | # ifdef FEAT_EVAL |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6894 | set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1); |
| 6895 | set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 6896 | # endif |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 6897 | ++allbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6898 | n = apply_autocmds(EVENT_FILECHANGEDSHELL, |
| 6899 | buf->b_fname, buf->b_fname, FALSE, buf); |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 6900 | --allbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6901 | busy = FALSE; |
| 6902 | if (n) |
| 6903 | { |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6904 | if (!bufref_valid(&bufref)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6905 | EMSG(_("E246: FileChangedShell autocommand deleted buffer")); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 6906 | # ifdef FEAT_EVAL |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6907 | s = get_vim_var_str(VV_FCS_CHOICE); |
| 6908 | if (STRCMP(s, "reload") == 0 && *reason != 'd') |
| 6909 | reload = TRUE; |
| 6910 | else if (STRCMP(s, "ask") == 0) |
| 6911 | n = FALSE; |
| 6912 | else |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 6913 | # endif |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6914 | return 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6915 | } |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6916 | if (!n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6917 | #endif |
| 6918 | { |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6919 | if (*reason == 'd') |
| 6920 | mesg = _("E211: File \"%s\" no longer available"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6921 | else |
| 6922 | { |
| 6923 | helpmesg = TRUE; |
| 6924 | #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
| 6925 | can_reload = TRUE; |
| 6926 | #endif |
| 6927 | /* |
| 6928 | * Check if the file contents really changed to avoid |
| 6929 | * giving a warning when only the timestamp was set (e.g., |
| 6930 | * checked out of CVS). Always warn when the buffer was |
| 6931 | * changed. |
| 6932 | */ |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6933 | if (reason[2] == 'n') |
| 6934 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6935 | mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well"); |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6936 | mesg2 = _("See \":help W12\" for more info."); |
| 6937 | } |
| 6938 | else if (reason[1] == 'h') |
| 6939 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6940 | mesg = _("W11: Warning: File \"%s\" has changed since editing started"); |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6941 | mesg2 = _("See \":help W11\" for more info."); |
| 6942 | } |
| 6943 | else if (*reason == 'm') |
| 6944 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6945 | mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started"); |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6946 | mesg2 = _("See \":help W16\" for more info."); |
| 6947 | } |
Bram Moolenaar | 85388b5 | 2009-06-24 09:58:32 +0000 | [diff] [blame] | 6948 | else |
| 6949 | /* Only timestamp changed, store it to avoid a warning |
| 6950 | * in check_mtime() later. */ |
| 6951 | buf->b_mtime_read = buf->b_mtime; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6952 | } |
| 6953 | } |
| 6954 | } |
| 6955 | |
| 6956 | } |
| 6957 | else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W) |
| 6958 | && vim_fexists(buf->b_ffname)) |
| 6959 | { |
| 6960 | retval = 1; |
| 6961 | mesg = _("W13: Warning: File \"%s\" has been created after editing started"); |
| 6962 | buf->b_flags |= BF_NEW_W; |
| 6963 | #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
| 6964 | can_reload = TRUE; |
| 6965 | #endif |
| 6966 | } |
| 6967 | |
| 6968 | if (mesg != NULL) |
| 6969 | { |
| 6970 | path = home_replace_save(buf, buf->b_fname); |
| 6971 | if (path != NULL) |
| 6972 | { |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6973 | if (!helpmesg) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6974 | mesg2 = ""; |
| 6975 | tbuf = alloc((unsigned)(STRLEN(path) + STRLEN(mesg) |
| 6976 | + STRLEN(mesg2) + 2)); |
| 6977 | sprintf((char *)tbuf, mesg, path); |
Bram Moolenaar | 496c526 | 2009-03-18 14:42:00 +0000 | [diff] [blame] | 6978 | #ifdef FEAT_EVAL |
| 6979 | /* Set warningmsg here, before the unimportant and output-specific |
| 6980 | * mesg2 has been appended. */ |
| 6981 | set_vim_var_string(VV_WARNINGMSG, tbuf, -1); |
| 6982 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6983 | #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
| 6984 | if (can_reload) |
| 6985 | { |
| 6986 | if (*mesg2 != NUL) |
| 6987 | { |
| 6988 | STRCAT(tbuf, "\n"); |
| 6989 | STRCAT(tbuf, mesg2); |
| 6990 | } |
| 6991 | if (do_dialog(VIM_WARNING, (char_u *)_("Warning"), tbuf, |
Bram Moolenaar | d2c340a | 2011-01-17 20:08:11 +0100 | [diff] [blame] | 6992 | (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6993 | reload = TRUE; |
| 6994 | } |
| 6995 | else |
| 6996 | #endif |
| 6997 | if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned) |
| 6998 | { |
| 6999 | if (*mesg2 != NUL) |
| 7000 | { |
| 7001 | STRCAT(tbuf, "; "); |
| 7002 | STRCAT(tbuf, mesg2); |
| 7003 | } |
| 7004 | EMSG(tbuf); |
| 7005 | retval = 2; |
| 7006 | } |
| 7007 | else |
| 7008 | { |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 7009 | # ifdef FEAT_AUTOCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7010 | if (!autocmd_busy) |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 7011 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7012 | { |
| 7013 | msg_start(); |
| 7014 | msg_puts_attr(tbuf, hl_attr(HLF_E) + MSG_HIST); |
| 7015 | if (*mesg2 != NUL) |
| 7016 | msg_puts_attr((char_u *)mesg2, |
| 7017 | hl_attr(HLF_W) + MSG_HIST); |
| 7018 | msg_clr_eos(); |
| 7019 | (void)msg_end(); |
| 7020 | if (emsg_silent == 0) |
| 7021 | { |
| 7022 | out_flush(); |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 7023 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7024 | if (!focus) |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 7025 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7026 | /* give the user some time to think about it */ |
| 7027 | ui_delay(1000L, TRUE); |
| 7028 | |
| 7029 | /* don't redraw and erase the message */ |
| 7030 | redraw_cmdline = FALSE; |
| 7031 | } |
| 7032 | } |
| 7033 | already_warned = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7034 | } |
| 7035 | |
| 7036 | vim_free(path); |
| 7037 | vim_free(tbuf); |
| 7038 | } |
| 7039 | } |
| 7040 | |
| 7041 | if (reload) |
Bram Moolenaar | 465748e | 2012-08-29 18:50:54 +0200 | [diff] [blame] | 7042 | { |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7043 | /* Reload the buffer. */ |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 7044 | buf_reload(buf, orig_mode); |
Bram Moolenaar | 465748e | 2012-08-29 18:50:54 +0200 | [diff] [blame] | 7045 | #ifdef FEAT_PERSISTENT_UNDO |
| 7046 | if (buf->b_p_udf && buf->b_ffname != NULL) |
| 7047 | { |
| 7048 | char_u hash[UNDO_HASH_SIZE]; |
| 7049 | buf_T *save_curbuf = curbuf; |
| 7050 | |
| 7051 | /* Any existing undo file is unusable, write it now. */ |
| 7052 | curbuf = buf; |
| 7053 | u_compute_hash(hash); |
| 7054 | u_write_undo(NULL, FALSE, buf, hash); |
| 7055 | curbuf = save_curbuf; |
| 7056 | } |
| 7057 | #endif |
| 7058 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7059 | |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 7060 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 7061 | /* Trigger FileChangedShell when the file was changed in any way. */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7062 | if (bufref_valid(&bufref) && retval != 0) |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 7063 | (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, |
| 7064 | buf->b_fname, buf->b_fname, FALSE, buf); |
| 7065 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7066 | #ifdef FEAT_GUI |
| 7067 | /* restore this in case an autocommand has set it; it would break |
| 7068 | * 'mousefocus' */ |
| 7069 | need_mouse_correct = save_mouse_correct; |
| 7070 | #endif |
| 7071 | |
| 7072 | return retval; |
| 7073 | } |
| 7074 | |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7075 | /* |
| 7076 | * Reload a buffer that is already loaded. |
| 7077 | * Used when the file was changed outside of Vim. |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 7078 | * "orig_mode" is buf->b_orig_mode before the need for reloading was detected. |
| 7079 | * buf->b_orig_mode may have been reset already. |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7080 | */ |
| 7081 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7082 | buf_reload(buf_T *buf, int orig_mode) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7083 | { |
| 7084 | exarg_T ea; |
| 7085 | pos_T old_cursor; |
| 7086 | linenr_T old_topline; |
| 7087 | int old_ro = buf->b_p_ro; |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7088 | buf_T *savebuf; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7089 | bufref_T bufref; |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7090 | int saved = OK; |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7091 | aco_save_T aco; |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 7092 | int flags = READ_NEW; |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7093 | |
| 7094 | /* set curwin/curbuf for "buf" and save some things */ |
| 7095 | aucmd_prepbuf(&aco, buf); |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7096 | |
| 7097 | /* We only want to read the text from the file, not reset the syntax |
| 7098 | * highlighting, clear marks, diff status, etc. Force the fileformat |
| 7099 | * and encoding to be the same. */ |
| 7100 | if (prep_exarg(&ea, buf) == OK) |
| 7101 | { |
| 7102 | old_cursor = curwin->w_cursor; |
| 7103 | old_topline = curwin->w_topline; |
| 7104 | |
Bram Moolenaar | f9bb734 | 2010-08-04 14:29:54 +0200 | [diff] [blame] | 7105 | if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur) |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 7106 | { |
| 7107 | /* Save all the text, so that the reload can be undone. |
| 7108 | * Sync first so that this is a separate undo-able action. */ |
| 7109 | u_sync(FALSE); |
| 7110 | saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE); |
| 7111 | flags |= READ_KEEP_UNDO; |
| 7112 | } |
| 7113 | |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7114 | /* |
| 7115 | * To behave like when a new file is edited (matters for |
| 7116 | * BufReadPost autocommands) we first need to delete the current |
| 7117 | * buffer contents. But if reading the file fails we should keep |
| 7118 | * the old contents. Can't use memory only, the file might be |
| 7119 | * too big. Use a hidden buffer to move the buffer contents to. |
| 7120 | */ |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 7121 | if (BUFEMPTY() || saved == FAIL) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7122 | savebuf = NULL; |
| 7123 | else |
| 7124 | { |
| 7125 | /* Allocate a buffer without putting it in the buffer list. */ |
| 7126 | savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7127 | set_bufref(&bufref, savebuf); |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 7128 | if (savebuf != NULL && buf == curbuf) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7129 | { |
| 7130 | /* Open the memline. */ |
| 7131 | curbuf = savebuf; |
| 7132 | curwin->w_buffer = savebuf; |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7133 | saved = ml_open(curbuf); |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7134 | curbuf = buf; |
| 7135 | curwin->w_buffer = buf; |
| 7136 | } |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 7137 | if (savebuf == NULL || saved == FAIL || buf != curbuf |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7138 | || move_lines(buf, savebuf) == FAIL) |
| 7139 | { |
| 7140 | EMSG2(_("E462: Could not prepare for reloading \"%s\""), |
| 7141 | buf->b_fname); |
| 7142 | saved = FAIL; |
| 7143 | } |
| 7144 | } |
| 7145 | |
| 7146 | if (saved == OK) |
| 7147 | { |
| 7148 | curbuf->b_flags |= BF_CHECK_RO; /* check for RO again */ |
| 7149 | #ifdef FEAT_AUTOCMD |
| 7150 | keep_filetype = TRUE; /* don't detect 'filetype' */ |
| 7151 | #endif |
| 7152 | if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, |
| 7153 | (linenr_T)0, |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 7154 | (linenr_T)MAXLNUM, &ea, flags) != OK) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7155 | { |
| 7156 | #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
| 7157 | if (!aborting()) |
| 7158 | #endif |
| 7159 | EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname); |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7160 | if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7161 | { |
| 7162 | /* Put the text back from the save buffer. First |
| 7163 | * delete any lines that readfile() added. */ |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 7164 | while (!BUFEMPTY()) |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 7165 | if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7166 | break; |
| 7167 | (void)move_lines(savebuf, buf); |
| 7168 | } |
| 7169 | } |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 7170 | else if (buf == curbuf) /* "buf" still valid */ |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7171 | { |
| 7172 | /* Mark the buffer as unmodified and free undo info. */ |
| 7173 | unchanged(buf, TRUE); |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 7174 | if ((flags & READ_KEEP_UNDO) == 0) |
| 7175 | { |
| 7176 | u_blockfree(buf); |
| 7177 | u_clearall(buf); |
| 7178 | } |
| 7179 | else |
Bram Moolenaar | f9bb734 | 2010-08-04 14:29:54 +0200 | [diff] [blame] | 7180 | { |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 7181 | /* Mark all undo states as changed. */ |
| 7182 | u_unchanged(curbuf); |
Bram Moolenaar | f9bb734 | 2010-08-04 14:29:54 +0200 | [diff] [blame] | 7183 | } |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7184 | } |
| 7185 | } |
| 7186 | vim_free(ea.cmd); |
| 7187 | |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7188 | if (savebuf != NULL && bufref_valid(&bufref)) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7189 | wipe_buffer(savebuf, FALSE); |
| 7190 | |
| 7191 | #ifdef FEAT_DIFF |
| 7192 | /* Invalidate diff info if necessary. */ |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 7193 | diff_invalidate(curbuf); |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7194 | #endif |
| 7195 | |
| 7196 | /* Restore the topline and cursor position and check it (lines may |
| 7197 | * have been removed). */ |
| 7198 | if (old_topline > curbuf->b_ml.ml_line_count) |
| 7199 | curwin->w_topline = curbuf->b_ml.ml_line_count; |
| 7200 | else |
| 7201 | curwin->w_topline = old_topline; |
| 7202 | curwin->w_cursor = old_cursor; |
| 7203 | check_cursor(); |
| 7204 | update_topline(); |
| 7205 | #ifdef FEAT_AUTOCMD |
| 7206 | keep_filetype = FALSE; |
| 7207 | #endif |
| 7208 | #ifdef FEAT_FOLDING |
| 7209 | { |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 7210 | win_T *wp; |
| 7211 | tabpage_T *tp; |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7212 | |
| 7213 | /* Update folds unless they are defined manually. */ |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 7214 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7215 | if (wp->w_buffer == curwin->w_buffer |
| 7216 | && !foldmethodIsManual(wp)) |
| 7217 | foldUpdateAll(wp); |
| 7218 | } |
| 7219 | #endif |
| 7220 | /* If the mode didn't change and 'readonly' was set, keep the old |
| 7221 | * value; the user probably used the ":view" command. But don't |
| 7222 | * reset it, might have had a read error. */ |
| 7223 | if (orig_mode == curbuf->b_orig_mode) |
| 7224 | curbuf->b_p_ro |= old_ro; |
Bram Moolenaar | 52f85b7 | 2013-01-30 14:13:56 +0100 | [diff] [blame] | 7225 | |
| 7226 | /* Modelines must override settings done by autocommands. */ |
| 7227 | do_modelines(0); |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7228 | } |
| 7229 | |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7230 | /* restore curwin/curbuf and a few other things */ |
| 7231 | aucmd_restbuf(&aco); |
| 7232 | /* Careful: autocommands may have made "buf" invalid! */ |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7233 | } |
| 7234 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7235 | void |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 7236 | buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7237 | { |
| 7238 | buf->b_mtime = (long)st->st_mtime; |
Bram Moolenaar | 914703b | 2010-05-31 21:59:46 +0200 | [diff] [blame] | 7239 | buf->b_orig_size = st->st_size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7240 | #ifdef HAVE_ST_MODE |
| 7241 | buf->b_orig_mode = (int)st->st_mode; |
| 7242 | #else |
| 7243 | buf->b_orig_mode = mch_getperm(fname); |
| 7244 | #endif |
| 7245 | } |
| 7246 | |
| 7247 | /* |
| 7248 | * Adjust the line with missing eol, used for the next write. |
| 7249 | * Used for do_filter(), when the input lines for the filter are deleted. |
| 7250 | */ |
| 7251 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7252 | write_lnum_adjust(linenr_T offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7253 | { |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 7254 | if (curbuf->b_no_eol_lnum != 0) /* only if there is a missing eol */ |
| 7255 | curbuf->b_no_eol_lnum += offset; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7256 | } |
| 7257 | |
Bram Moolenaar | da440d2 | 2016-01-16 21:27:23 +0100 | [diff] [blame] | 7258 | #if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO) |
| 7259 | /* |
| 7260 | * Delete "name" and everything in it, recursively. |
| 7261 | * return 0 for succes, -1 if some file was not deleted. |
| 7262 | */ |
| 7263 | int |
| 7264 | delete_recursive(char_u *name) |
| 7265 | { |
| 7266 | int result = 0; |
| 7267 | char_u **files; |
| 7268 | int file_count; |
| 7269 | int i; |
| 7270 | char_u *exp; |
| 7271 | |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 7272 | /* A symbolic link to a directory itself is deleted, not the directory it |
| 7273 | * points to. */ |
| 7274 | if ( |
Bram Moolenaar | 203258c | 2016-01-17 22:15:16 +0100 | [diff] [blame] | 7275 | # if defined(UNIX) || defined(WIN32) |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 7276 | mch_isrealdir(name) |
Bram Moolenaar | 203258c | 2016-01-17 22:15:16 +0100 | [diff] [blame] | 7277 | # else |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 7278 | mch_isdir(name) |
Bram Moolenaar | 43a34f9 | 2016-01-17 15:56:34 +0100 | [diff] [blame] | 7279 | # endif |
| 7280 | ) |
Bram Moolenaar | da440d2 | 2016-01-16 21:27:23 +0100 | [diff] [blame] | 7281 | { |
| 7282 | vim_snprintf((char *)NameBuff, MAXPATHL, "%s/*", name); |
| 7283 | exp = vim_strsave(NameBuff); |
| 7284 | if (exp == NULL) |
| 7285 | return -1; |
| 7286 | if (gen_expand_wildcards(1, &exp, &file_count, &files, |
Bram Moolenaar | 336bd62 | 2016-01-17 18:23:58 +0100 | [diff] [blame] | 7287 | EW_DIR|EW_FILE|EW_SILENT|EW_ALLLINKS|EW_DODOT|EW_EMPTYOK) == OK) |
Bram Moolenaar | da440d2 | 2016-01-16 21:27:23 +0100 | [diff] [blame] | 7288 | { |
| 7289 | for (i = 0; i < file_count; ++i) |
| 7290 | if (delete_recursive(files[i]) != 0) |
| 7291 | result = -1; |
| 7292 | FreeWild(file_count, files); |
| 7293 | } |
| 7294 | else |
| 7295 | result = -1; |
| 7296 | vim_free(exp); |
| 7297 | (void)mch_rmdir(name); |
| 7298 | } |
| 7299 | else |
| 7300 | result = mch_remove(name) == 0 ? 0 : -1; |
| 7301 | |
| 7302 | return result; |
| 7303 | } |
| 7304 | #endif |
| 7305 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7306 | #if defined(TEMPDIRNAMES) || defined(PROTO) |
| 7307 | static long temp_count = 0; /* Temp filename counter. */ |
| 7308 | |
| 7309 | /* |
| 7310 | * Delete the temp directory and all files it contains. |
| 7311 | */ |
| 7312 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7313 | vim_deltempdir(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7314 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7315 | if (vim_tempdir != NULL) |
| 7316 | { |
Bram Moolenaar | da440d2 | 2016-01-16 21:27:23 +0100 | [diff] [blame] | 7317 | /* remove the trailing path separator */ |
| 7318 | gettail(vim_tempdir)[-1] = NUL; |
| 7319 | delete_recursive(vim_tempdir); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7320 | vim_free(vim_tempdir); |
| 7321 | vim_tempdir = NULL; |
| 7322 | } |
| 7323 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7324 | |
| 7325 | /* |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7326 | * Directory "tempdir" was created. Expand this name to a full path and put |
| 7327 | * it in "vim_tempdir". This avoids that using ":cd" would confuse us. |
| 7328 | * "tempdir" must be no longer than MAXPATHL. |
| 7329 | */ |
| 7330 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7331 | vim_settempdir(char_u *tempdir) |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7332 | { |
| 7333 | char_u *buf; |
| 7334 | |
| 7335 | buf = alloc((unsigned)MAXPATHL + 2); |
| 7336 | if (buf != NULL) |
| 7337 | { |
| 7338 | if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL) |
| 7339 | STRCPY(buf, tempdir); |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 7340 | add_pathsep(buf); |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7341 | vim_tempdir = vim_strsave(buf); |
| 7342 | vim_free(buf); |
| 7343 | } |
| 7344 | } |
Bram Moolenaar | 4592dee | 2009-11-18 19:11:58 +0000 | [diff] [blame] | 7345 | #endif |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7346 | |
| 7347 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7348 | * vim_tempname(): Return a unique name that can be used for a temp file. |
| 7349 | * |
Bram Moolenaar | 76ae22f | 2016-06-13 20:00:29 +0200 | [diff] [blame] | 7350 | * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is |
| 7351 | * guaranteed to NOT be created. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7352 | * |
| 7353 | * The returned pointer is to allocated memory. |
| 7354 | * The returned pointer is NULL if no valid name was found. |
| 7355 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7356 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7357 | vim_tempname( |
| 7358 | int extra_char UNUSED, /* char to use in the name instead of '?' */ |
| 7359 | int keep UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7360 | { |
| 7361 | #ifdef USE_TMPNAM |
| 7362 | char_u itmp[L_tmpnam]; /* use tmpnam() */ |
| 7363 | #else |
| 7364 | char_u itmp[TEMPNAMELEN]; |
| 7365 | #endif |
| 7366 | |
| 7367 | #ifdef TEMPDIRNAMES |
| 7368 | static char *(tempdirs[]) = {TEMPDIRNAMES}; |
| 7369 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7370 | # ifndef EEXIST |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 7371 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7372 | # endif |
| 7373 | |
| 7374 | /* |
| 7375 | * This will create a directory for private use by this instance of Vim. |
| 7376 | * This is done once, and the same directory is used for all temp files. |
| 7377 | * This method avoids security problems because of symlink attacks et al. |
| 7378 | * It's also a bit faster, because we only need to check for an existing |
| 7379 | * file when creating the directory and not for each temp file. |
| 7380 | */ |
| 7381 | if (vim_tempdir == NULL) |
| 7382 | { |
| 7383 | /* |
| 7384 | * Try the entries in TEMPDIRNAMES to create the temp directory. |
| 7385 | */ |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 7386 | for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7387 | { |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7388 | # ifndef HAVE_MKDTEMP |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 7389 | size_t itmplen; |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7390 | long nr; |
| 7391 | long off; |
| 7392 | # endif |
| 7393 | |
Bram Moolenaar | e1a6199 | 2015-12-03 21:02:27 +0100 | [diff] [blame] | 7394 | /* Expand $TMP, leave room for "/v1100000/999999999". |
| 7395 | * Skip the directory check if the expansion fails. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7396 | expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20); |
Bram Moolenaar | e1a6199 | 2015-12-03 21:02:27 +0100 | [diff] [blame] | 7397 | if (itmp[0] != '$' && mch_isdir(itmp)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7398 | { |
Bram Moolenaar | e1a6199 | 2015-12-03 21:02:27 +0100 | [diff] [blame] | 7399 | /* directory exists */ |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 7400 | add_pathsep(itmp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7401 | |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7402 | # ifdef HAVE_MKDTEMP |
Bram Moolenaar | 35d88f4 | 2016-06-04 14:52:00 +0200 | [diff] [blame] | 7403 | { |
| 7404 | # if defined(UNIX) || defined(VMS) |
| 7405 | /* Make sure the umask doesn't remove the executable bit. |
| 7406 | * "repl" has been reported to use "177". */ |
| 7407 | mode_t umask_save = umask(077); |
| 7408 | # endif |
| 7409 | /* Leave room for filename */ |
| 7410 | STRCAT(itmp, "vXXXXXX"); |
| 7411 | if (mkdtemp((char *)itmp) != NULL) |
| 7412 | vim_settempdir(itmp); |
| 7413 | # if defined(UNIX) || defined(VMS) |
| 7414 | (void)umask(umask_save); |
| 7415 | # endif |
| 7416 | } |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7417 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7418 | /* Get an arbitrary number of up to 6 digits. When it's |
| 7419 | * unlikely that it already exists it will be faster, |
| 7420 | * otherwise it doesn't matter. The use of mkdir() avoids any |
| 7421 | * security problems because of the predictable number. */ |
| 7422 | nr = (mch_get_pid() + (long)time(NULL)) % 1000000L; |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 7423 | itmplen = STRLEN(itmp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7424 | |
| 7425 | /* Try up to 10000 different values until we find a name that |
| 7426 | * doesn't exist. */ |
| 7427 | for (off = 0; off < 10000L; ++off) |
| 7428 | { |
| 7429 | int r; |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7430 | # if defined(UNIX) || defined(VMS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7431 | mode_t umask_save; |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7432 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7433 | |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7434 | sprintf((char *)itmp + itmplen, "v%ld", nr + off); |
| 7435 | # ifndef EEXIST |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7436 | /* If mkdir() does not set errno to EEXIST, check for |
| 7437 | * existing file here. There is a race condition then, |
| 7438 | * although it's fail-safe. */ |
| 7439 | if (mch_stat((char *)itmp, &st) >= 0) |
| 7440 | continue; |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7441 | # endif |
| 7442 | # if defined(UNIX) || defined(VMS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7443 | /* Make sure the umask doesn't remove the executable bit. |
| 7444 | * "repl" has been reported to use "177". */ |
| 7445 | umask_save = umask(077); |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7446 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7447 | r = vim_mkdir(itmp, 0700); |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7448 | # if defined(UNIX) || defined(VMS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7449 | (void)umask(umask_save); |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7450 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7451 | if (r == 0) |
| 7452 | { |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7453 | vim_settempdir(itmp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7454 | break; |
| 7455 | } |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7456 | # ifdef EEXIST |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7457 | /* If the mkdir() didn't fail because the file/dir exists, |
| 7458 | * we probably can't create any dir here, try another |
| 7459 | * place. */ |
| 7460 | if (errno != EEXIST) |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7461 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7462 | break; |
| 7463 | } |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7464 | # endif /* HAVE_MKDTEMP */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7465 | if (vim_tempdir != NULL) |
| 7466 | break; |
| 7467 | } |
| 7468 | } |
| 7469 | } |
| 7470 | |
| 7471 | if (vim_tempdir != NULL) |
| 7472 | { |
| 7473 | /* There is no need to check if the file exists, because we own the |
| 7474 | * directory and nobody else creates a file in it. */ |
| 7475 | sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++); |
| 7476 | return vim_strsave(itmp); |
| 7477 | } |
| 7478 | |
| 7479 | return NULL; |
| 7480 | |
| 7481 | #else /* TEMPDIRNAMES */ |
| 7482 | |
| 7483 | # ifdef WIN3264 |
| 7484 | char szTempFile[_MAX_PATH + 1]; |
| 7485 | char buf4[4]; |
| 7486 | char_u *retval; |
| 7487 | char_u *p; |
| 7488 | |
| 7489 | STRCPY(itmp, ""); |
| 7490 | if (GetTempPath(_MAX_PATH, szTempFile) == 0) |
Bram Moolenaar | b189191 | 2011-02-09 14:47:03 +0100 | [diff] [blame] | 7491 | { |
| 7492 | szTempFile[0] = '.'; /* GetTempPath() failed, use current dir */ |
| 7493 | szTempFile[1] = NUL; |
| 7494 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7495 | strcpy(buf4, "VIM"); |
| 7496 | buf4[2] = extra_char; /* make it "VIa", "VIb", etc. */ |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 7497 | if (GetTempFileName(szTempFile, buf4, 0, (LPSTR)itmp) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7498 | return NULL; |
Bram Moolenaar | e5c421c | 2015-03-31 13:33:08 +0200 | [diff] [blame] | 7499 | if (!keep) |
| 7500 | /* GetTempFileName() will create the file, we don't want that */ |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 7501 | (void)DeleteFile((LPSTR)itmp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7502 | |
| 7503 | /* Backslashes in a temp file name cause problems when filtering with |
| 7504 | * "sh". NOTE: This also checks 'shellcmdflag' to help those people who |
| 7505 | * didn't set 'shellslash'. */ |
| 7506 | retval = vim_strsave(itmp); |
| 7507 | if (*p_shcf == '-' || p_ssl) |
| 7508 | for (p = retval; *p; ++p) |
| 7509 | if (*p == '\\') |
| 7510 | *p = '/'; |
| 7511 | return retval; |
| 7512 | |
| 7513 | # else /* WIN3264 */ |
| 7514 | |
| 7515 | # ifdef USE_TMPNAM |
Bram Moolenaar | 95474ca | 2011-02-09 16:44:51 +0100 | [diff] [blame] | 7516 | char_u *p; |
| 7517 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7518 | /* tmpnam() will make its own name */ |
Bram Moolenaar | 95474ca | 2011-02-09 16:44:51 +0100 | [diff] [blame] | 7519 | p = tmpnam((char *)itmp); |
| 7520 | if (p == NULL || *p == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7521 | return NULL; |
| 7522 | # else |
| 7523 | char_u *p; |
| 7524 | |
| 7525 | # ifdef VMS_TEMPNAM |
| 7526 | /* mktemp() is not working on VMS. It seems to be |
| 7527 | * a do-nothing function. Therefore we use tempnam(). |
| 7528 | */ |
| 7529 | sprintf((char *)itmp, "VIM%c", extra_char); |
| 7530 | p = (char_u *)tempnam("tmp:", (char *)itmp); |
| 7531 | if (p != NULL) |
| 7532 | { |
Bram Moolenaar | 206f011 | 2014-03-12 16:51:55 +0100 | [diff] [blame] | 7533 | /* VMS will use '.LIS' if we don't explicitly specify an extension, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7534 | * and VIM will then be unable to find the file later */ |
| 7535 | STRCPY(itmp, p); |
| 7536 | STRCAT(itmp, ".txt"); |
| 7537 | free(p); |
| 7538 | } |
| 7539 | else |
| 7540 | return NULL; |
| 7541 | # else |
| 7542 | STRCPY(itmp, TEMPNAME); |
| 7543 | if ((p = vim_strchr(itmp, '?')) != NULL) |
| 7544 | *p = extra_char; |
| 7545 | if (mktemp((char *)itmp) == NULL) |
| 7546 | return NULL; |
| 7547 | # endif |
| 7548 | # endif |
| 7549 | |
| 7550 | return vim_strsave(itmp); |
| 7551 | # endif /* WIN3264 */ |
| 7552 | #endif /* TEMPDIRNAMES */ |
| 7553 | } |
| 7554 | |
| 7555 | #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) |
| 7556 | /* |
Bram Moolenaar | b4f6a46 | 2015-10-13 19:43:17 +0200 | [diff] [blame] | 7557 | * Convert all backslashes in fname to forward slashes in-place, unless when |
| 7558 | * it looks like a URL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7559 | */ |
| 7560 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7561 | forward_slash(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7562 | { |
| 7563 | char_u *p; |
| 7564 | |
Bram Moolenaar | b4f6a46 | 2015-10-13 19:43:17 +0200 | [diff] [blame] | 7565 | if (path_with_url(fname)) |
| 7566 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7567 | for (p = fname; *p != NUL; ++p) |
| 7568 | # ifdef FEAT_MBYTE |
| 7569 | /* The Big5 encoding can have '\' in the trail byte. */ |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7570 | if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7571 | ++p; |
| 7572 | else |
| 7573 | # endif |
| 7574 | if (*p == '\\') |
| 7575 | *p = '/'; |
| 7576 | } |
| 7577 | #endif |
| 7578 | |
| 7579 | |
| 7580 | /* |
| 7581 | * Code for automatic commands. |
| 7582 | * |
| 7583 | * Only included when "FEAT_AUTOCMD" has been defined. |
| 7584 | */ |
| 7585 | |
| 7586 | #if defined(FEAT_AUTOCMD) || defined(PROTO) |
| 7587 | |
| 7588 | /* |
| 7589 | * The autocommands are stored in a list for each event. |
| 7590 | * Autocommands for the same pattern, that are consecutive, are joined |
| 7591 | * together, to avoid having to match the pattern too often. |
| 7592 | * The result is an array of Autopat lists, which point to AutoCmd lists: |
| 7593 | * |
| 7594 | * first_autopat[0] --> Autopat.next --> Autopat.next --> NULL |
| 7595 | * Autopat.cmds Autopat.cmds |
| 7596 | * | | |
| 7597 | * V V |
| 7598 | * AutoCmd.next AutoCmd.next |
| 7599 | * | | |
| 7600 | * V V |
| 7601 | * AutoCmd.next NULL |
| 7602 | * | |
| 7603 | * V |
| 7604 | * NULL |
| 7605 | * |
| 7606 | * first_autopat[1] --> Autopat.next --> NULL |
| 7607 | * Autopat.cmds |
| 7608 | * | |
| 7609 | * V |
| 7610 | * AutoCmd.next |
| 7611 | * | |
| 7612 | * V |
| 7613 | * NULL |
| 7614 | * etc. |
| 7615 | * |
| 7616 | * The order of AutoCmds is important, this is the order in which they were |
| 7617 | * defined and will have to be executed. |
| 7618 | */ |
| 7619 | typedef struct AutoCmd |
| 7620 | { |
| 7621 | char_u *cmd; /* The command to be executed (NULL |
| 7622 | when command has been removed) */ |
| 7623 | char nested; /* If autocommands nest here */ |
| 7624 | char last; /* last command in list */ |
| 7625 | #ifdef FEAT_EVAL |
| 7626 | scid_T scriptID; /* script ID where defined */ |
| 7627 | #endif |
| 7628 | struct AutoCmd *next; /* Next AutoCmd in list */ |
| 7629 | } AutoCmd; |
| 7630 | |
| 7631 | typedef struct AutoPat |
| 7632 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7633 | char_u *pat; /* pattern as typed (NULL when pattern |
| 7634 | has been removed) */ |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 7635 | regprog_T *reg_prog; /* compiled regprog for pattern */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7636 | AutoCmd *cmds; /* list of commands to do */ |
| 7637 | struct AutoPat *next; /* next AutoPat in AutoPat list */ |
Bram Moolenaar | 6395af8 | 2013-06-12 19:52:15 +0200 | [diff] [blame] | 7638 | int group; /* group ID */ |
| 7639 | int patlen; /* strlen() of pat */ |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7640 | int buflocal_nr; /* !=0 for buffer-local AutoPat */ |
Bram Moolenaar | 6395af8 | 2013-06-12 19:52:15 +0200 | [diff] [blame] | 7641 | char allow_dirs; /* Pattern may match whole path */ |
| 7642 | char last; /* last pattern for apply_autocmds() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7643 | } AutoPat; |
| 7644 | |
| 7645 | static struct event_name |
| 7646 | { |
| 7647 | char *name; /* event name */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7648 | event_T event; /* event number */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7649 | } event_names[] = |
| 7650 | { |
| 7651 | {"BufAdd", EVENT_BUFADD}, |
| 7652 | {"BufCreate", EVENT_BUFADD}, |
| 7653 | {"BufDelete", EVENT_BUFDELETE}, |
| 7654 | {"BufEnter", EVENT_BUFENTER}, |
| 7655 | {"BufFilePost", EVENT_BUFFILEPOST}, |
| 7656 | {"BufFilePre", EVENT_BUFFILEPRE}, |
| 7657 | {"BufHidden", EVENT_BUFHIDDEN}, |
| 7658 | {"BufLeave", EVENT_BUFLEAVE}, |
| 7659 | {"BufNew", EVENT_BUFNEW}, |
| 7660 | {"BufNewFile", EVENT_BUFNEWFILE}, |
| 7661 | {"BufRead", EVENT_BUFREADPOST}, |
| 7662 | {"BufReadCmd", EVENT_BUFREADCMD}, |
| 7663 | {"BufReadPost", EVENT_BUFREADPOST}, |
| 7664 | {"BufReadPre", EVENT_BUFREADPRE}, |
| 7665 | {"BufUnload", EVENT_BUFUNLOAD}, |
| 7666 | {"BufWinEnter", EVENT_BUFWINENTER}, |
| 7667 | {"BufWinLeave", EVENT_BUFWINLEAVE}, |
| 7668 | {"BufWipeout", EVENT_BUFWIPEOUT}, |
| 7669 | {"BufWrite", EVENT_BUFWRITEPRE}, |
| 7670 | {"BufWritePost", EVENT_BUFWRITEPOST}, |
| 7671 | {"BufWritePre", EVENT_BUFWRITEPRE}, |
| 7672 | {"BufWriteCmd", EVENT_BUFWRITECMD}, |
| 7673 | {"CmdwinEnter", EVENT_CMDWINENTER}, |
| 7674 | {"CmdwinLeave", EVENT_CMDWINLEAVE}, |
Bram Moolenaar | d500516 | 2014-08-22 23:05:54 +0200 | [diff] [blame] | 7675 | {"CmdUndefined", EVENT_CMDUNDEFINED}, |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7676 | {"ColorScheme", EVENT_COLORSCHEME}, |
Bram Moolenaar | cfa3cae | 2012-07-10 17:14:56 +0200 | [diff] [blame] | 7677 | {"CompleteDone", EVENT_COMPLETEDONE}, |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7678 | {"CursorHold", EVENT_CURSORHOLD}, |
| 7679 | {"CursorHoldI", EVENT_CURSORHOLDI}, |
| 7680 | {"CursorMoved", EVENT_CURSORMOVED}, |
| 7681 | {"CursorMovedI", EVENT_CURSORMOVEDI}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7682 | {"EncodingChanged", EVENT_ENCODINGCHANGED}, |
| 7683 | {"FileEncoding", EVENT_ENCODINGCHANGED}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7684 | {"FileAppendPost", EVENT_FILEAPPENDPOST}, |
| 7685 | {"FileAppendPre", EVENT_FILEAPPENDPRE}, |
| 7686 | {"FileAppendCmd", EVENT_FILEAPPENDCMD}, |
| 7687 | {"FileChangedShell",EVENT_FILECHANGEDSHELL}, |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 7688 | {"FileChangedShellPost",EVENT_FILECHANGEDSHELLPOST}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7689 | {"FileChangedRO", EVENT_FILECHANGEDRO}, |
| 7690 | {"FileReadPost", EVENT_FILEREADPOST}, |
| 7691 | {"FileReadPre", EVENT_FILEREADPRE}, |
| 7692 | {"FileReadCmd", EVENT_FILEREADCMD}, |
| 7693 | {"FileType", EVENT_FILETYPE}, |
| 7694 | {"FileWritePost", EVENT_FILEWRITEPOST}, |
| 7695 | {"FileWritePre", EVENT_FILEWRITEPRE}, |
| 7696 | {"FileWriteCmd", EVENT_FILEWRITECMD}, |
| 7697 | {"FilterReadPost", EVENT_FILTERREADPOST}, |
| 7698 | {"FilterReadPre", EVENT_FILTERREADPRE}, |
| 7699 | {"FilterWritePost", EVENT_FILTERWRITEPOST}, |
| 7700 | {"FilterWritePre", EVENT_FILTERWRITEPRE}, |
| 7701 | {"FocusGained", EVENT_FOCUSGAINED}, |
| 7702 | {"FocusLost", EVENT_FOCUSLOST}, |
| 7703 | {"FuncUndefined", EVENT_FUNCUNDEFINED}, |
| 7704 | {"GUIEnter", EVENT_GUIENTER}, |
Bram Moolenaar | 265e507 | 2006-08-29 16:13:22 +0000 | [diff] [blame] | 7705 | {"GUIFailed", EVENT_GUIFAILED}, |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 7706 | {"InsertChange", EVENT_INSERTCHANGE}, |
| 7707 | {"InsertEnter", EVENT_INSERTENTER}, |
| 7708 | {"InsertLeave", EVENT_INSERTLEAVE}, |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 7709 | {"InsertCharPre", EVENT_INSERTCHARPRE}, |
Bram Moolenaar | a3ffd9c | 2005-07-21 21:03:15 +0000 | [diff] [blame] | 7710 | {"MenuPopup", EVENT_MENUPOPUP}, |
Bram Moolenaar | 5374430 | 2015-07-17 17:38:22 +0200 | [diff] [blame] | 7711 | {"OptionSet", EVENT_OPTIONSET}, |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7712 | {"QuickFixCmdPost", EVENT_QUICKFIXCMDPOST}, |
| 7713 | {"QuickFixCmdPre", EVENT_QUICKFIXCMDPRE}, |
Bram Moolenaar | 3b53dfb | 2012-06-06 18:03:07 +0200 | [diff] [blame] | 7714 | {"QuitPre", EVENT_QUITPRE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7715 | {"RemoteReply", EVENT_REMOTEREPLY}, |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 7716 | {"SessionLoadPost", EVENT_SESSIONLOADPOST}, |
Bram Moolenaar | 5c4bab0 | 2006-03-10 21:37:46 +0000 | [diff] [blame] | 7717 | {"ShellCmdPost", EVENT_SHELLCMDPOST}, |
| 7718 | {"ShellFilterPost", EVENT_SHELLFILTERPOST}, |
Bram Moolenaar | a203182 | 2006-03-07 22:29:51 +0000 | [diff] [blame] | 7719 | {"SourcePre", EVENT_SOURCEPRE}, |
Bram Moolenaar | 8dd1aa5 | 2007-01-16 20:33:19 +0000 | [diff] [blame] | 7720 | {"SourceCmd", EVENT_SOURCECMD}, |
Bram Moolenaar | b8a7b56 | 2006-02-01 21:47:16 +0000 | [diff] [blame] | 7721 | {"SpellFileMissing",EVENT_SPELLFILEMISSING}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7722 | {"StdinReadPost", EVENT_STDINREADPOST}, |
| 7723 | {"StdinReadPre", EVENT_STDINREADPRE}, |
Bram Moolenaar | b815dac | 2005-12-07 20:59:24 +0000 | [diff] [blame] | 7724 | {"SwapExists", EVENT_SWAPEXISTS}, |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 7725 | {"Syntax", EVENT_SYNTAX}, |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 7726 | {"TabNew", EVENT_TABNEW}, |
Bram Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 7727 | {"TabClosed", EVENT_TABCLOSED}, |
Bram Moolenaar | 70836c8 | 2006-02-20 21:28:49 +0000 | [diff] [blame] | 7728 | {"TabEnter", EVENT_TABENTER}, |
| 7729 | {"TabLeave", EVENT_TABLEAVE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7730 | {"TermChanged", EVENT_TERMCHANGED}, |
| 7731 | {"TermResponse", EVENT_TERMRESPONSE}, |
Bram Moolenaar | 186628f | 2013-03-19 13:33:23 +0100 | [diff] [blame] | 7732 | {"TextChanged", EVENT_TEXTCHANGED}, |
| 7733 | {"TextChangedI", EVENT_TEXTCHANGEDI}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7734 | {"User", EVENT_USER}, |
| 7735 | {"VimEnter", EVENT_VIMENTER}, |
| 7736 | {"VimLeave", EVENT_VIMLEAVE}, |
| 7737 | {"VimLeavePre", EVENT_VIMLEAVEPRE}, |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 7738 | {"WinNew", EVENT_WINNEW}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7739 | {"WinEnter", EVENT_WINENTER}, |
| 7740 | {"WinLeave", EVENT_WINLEAVE}, |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 7741 | {"VimResized", EVENT_VIMRESIZED}, |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7742 | {NULL, (event_T)0} |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7743 | }; |
| 7744 | |
| 7745 | static AutoPat *first_autopat[NUM_EVENTS] = |
| 7746 | { |
| 7747 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 7748 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 7749 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 7750 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 7751 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
Bram Moolenaar | 5374430 | 2015-07-17 17:38:22 +0200 | [diff] [blame] | 7752 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7753 | }; |
| 7754 | |
| 7755 | /* |
| 7756 | * struct used to keep status while executing autocommands for an event. |
| 7757 | */ |
| 7758 | typedef struct AutoPatCmd |
| 7759 | { |
| 7760 | AutoPat *curpat; /* next AutoPat to examine */ |
| 7761 | AutoCmd *nextcmd; /* next AutoCmd to execute */ |
| 7762 | int group; /* group being used */ |
| 7763 | char_u *fname; /* fname to match with */ |
| 7764 | char_u *sfname; /* sfname to match with */ |
| 7765 | char_u *tail; /* tail of fname */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7766 | event_T event; /* current event */ |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7767 | int arg_bufnr; /* initially equal to <abuf>, set to zero when |
| 7768 | buf is deleted */ |
| 7769 | struct AutoPatCmd *next; /* chain of active apc-s for auto-invalidation*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7770 | } AutoPatCmd; |
| 7771 | |
Bram Moolenaar | d6f676d | 2005-06-01 21:51:55 +0000 | [diff] [blame] | 7772 | static AutoPatCmd *active_apc_list = NULL; /* stack of active autocommands */ |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7773 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7774 | /* |
| 7775 | * augroups stores a list of autocmd group names. |
| 7776 | */ |
Bram Moolenaar | d6f676d | 2005-06-01 21:51:55 +0000 | [diff] [blame] | 7777 | static garray_T augroups = {0, 0, sizeof(char_u *), 10, NULL}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7778 | #define AUGROUP_NAME(i) (((char_u **)augroups.ga_data)[i]) |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 7779 | /* use get_deleted_augroup() to get this */ |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 7780 | static char_u *deleted_augroup = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7781 | |
| 7782 | /* |
| 7783 | * The ID of the current group. Group 0 is the default one. |
| 7784 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7785 | static int current_augroup = AUGROUP_DEFAULT; |
| 7786 | |
| 7787 | static int au_need_clean = FALSE; /* need to delete marked patterns */ |
| 7788 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 7789 | static void show_autocmd(AutoPat *ap, event_T event); |
| 7790 | static void au_remove_pat(AutoPat *ap); |
| 7791 | static void au_remove_cmds(AutoPat *ap); |
| 7792 | static void au_cleanup(void); |
| 7793 | static int au_new_group(char_u *name); |
| 7794 | static void au_del_group(char_u *name); |
| 7795 | static event_T event_name2nr(char_u *start, char_u **end); |
| 7796 | static char_u *event_nr2name(event_T event); |
| 7797 | static char_u *find_end_event(char_u *arg, int have_group); |
| 7798 | static int event_ignored(event_T event); |
| 7799 | static int au_get_grouparg(char_u **argp); |
| 7800 | static int do_autocmd_event(event_T event, char_u *pat, int nested, char_u *cmd, int forceit, int group); |
| 7801 | static int apply_autocmds_group(event_T event, char_u *fname, char_u *fname_io, int force, int group, buf_T *buf, exarg_T *eap); |
| 7802 | static void auto_next_pat(AutoPatCmd *apc, int stop_at_last); |
Bram Moolenaar | dffa5b8 | 2014-11-19 16:38:07 +0100 | [diff] [blame] | 7803 | #if defined(FEAT_AUTOCMD) || defined(FEAT_WILDIGN) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 7804 | static int match_file_pat(char_u *pattern, regprog_T **prog, char_u *fname, char_u *sfname, char_u *tail, int allow_dirs); |
Bram Moolenaar | dffa5b8 | 2014-11-19 16:38:07 +0100 | [diff] [blame] | 7805 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7806 | |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7807 | |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7808 | static event_T last_event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7809 | static int last_group; |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7810 | static int autocmd_blocked = 0; /* block all autocmds */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7811 | |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 7812 | static char_u * |
| 7813 | get_deleted_augroup(void) |
| 7814 | { |
| 7815 | if (deleted_augroup == NULL) |
| 7816 | deleted_augroup = (char_u *)_("--Deleted--"); |
| 7817 | return deleted_augroup; |
| 7818 | } |
| 7819 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7820 | /* |
| 7821 | * Show the autocommands for one AutoPat. |
| 7822 | */ |
| 7823 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7824 | show_autocmd(AutoPat *ap, event_T event) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7825 | { |
| 7826 | AutoCmd *ac; |
| 7827 | |
| 7828 | /* Check for "got_int" (here and at various places below), which is set |
| 7829 | * when "q" has been hit for the "--more--" prompt */ |
| 7830 | if (got_int) |
| 7831 | return; |
| 7832 | if (ap->pat == NULL) /* pattern has been removed */ |
| 7833 | return; |
| 7834 | |
| 7835 | msg_putchar('\n'); |
| 7836 | if (got_int) |
| 7837 | return; |
| 7838 | if (event != last_event || ap->group != last_group) |
| 7839 | { |
| 7840 | if (ap->group != AUGROUP_DEFAULT) |
| 7841 | { |
| 7842 | if (AUGROUP_NAME(ap->group) == NULL) |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 7843 | msg_puts_attr(get_deleted_augroup(), hl_attr(HLF_E)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7844 | else |
| 7845 | msg_puts_attr(AUGROUP_NAME(ap->group), hl_attr(HLF_T)); |
| 7846 | msg_puts((char_u *)" "); |
| 7847 | } |
| 7848 | msg_puts_attr(event_nr2name(event), hl_attr(HLF_T)); |
| 7849 | last_event = event; |
| 7850 | last_group = ap->group; |
| 7851 | msg_putchar('\n'); |
| 7852 | if (got_int) |
| 7853 | return; |
| 7854 | } |
| 7855 | msg_col = 4; |
| 7856 | msg_outtrans(ap->pat); |
| 7857 | |
| 7858 | for (ac = ap->cmds; ac != NULL; ac = ac->next) |
| 7859 | { |
| 7860 | if (ac->cmd != NULL) /* skip removed commands */ |
| 7861 | { |
| 7862 | if (msg_col >= 14) |
| 7863 | msg_putchar('\n'); |
| 7864 | msg_col = 14; |
| 7865 | if (got_int) |
| 7866 | return; |
| 7867 | msg_outtrans(ac->cmd); |
Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 7868 | #ifdef FEAT_EVAL |
| 7869 | if (p_verbose > 0) |
| 7870 | last_set_msg(ac->scriptID); |
| 7871 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7872 | if (got_int) |
| 7873 | return; |
| 7874 | if (ac->next != NULL) |
| 7875 | { |
| 7876 | msg_putchar('\n'); |
| 7877 | if (got_int) |
| 7878 | return; |
| 7879 | } |
| 7880 | } |
| 7881 | } |
| 7882 | } |
| 7883 | |
| 7884 | /* |
| 7885 | * Mark an autocommand pattern for deletion. |
| 7886 | */ |
| 7887 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7888 | au_remove_pat(AutoPat *ap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7889 | { |
| 7890 | vim_free(ap->pat); |
| 7891 | ap->pat = NULL; |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7892 | ap->buflocal_nr = -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7893 | au_need_clean = TRUE; |
| 7894 | } |
| 7895 | |
| 7896 | /* |
| 7897 | * Mark all commands for a pattern for deletion. |
| 7898 | */ |
| 7899 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7900 | au_remove_cmds(AutoPat *ap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7901 | { |
| 7902 | AutoCmd *ac; |
| 7903 | |
| 7904 | for (ac = ap->cmds; ac != NULL; ac = ac->next) |
| 7905 | { |
| 7906 | vim_free(ac->cmd); |
| 7907 | ac->cmd = NULL; |
| 7908 | } |
| 7909 | au_need_clean = TRUE; |
| 7910 | } |
| 7911 | |
| 7912 | /* |
| 7913 | * Cleanup autocommands and patterns that have been deleted. |
| 7914 | * This is only done when not executing autocommands. |
| 7915 | */ |
| 7916 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7917 | au_cleanup(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7918 | { |
| 7919 | AutoPat *ap, **prev_ap; |
| 7920 | AutoCmd *ac, **prev_ac; |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7921 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7922 | |
| 7923 | if (autocmd_busy || !au_need_clean) |
| 7924 | return; |
| 7925 | |
| 7926 | /* loop over all events */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7927 | for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
| 7928 | event = (event_T)((int)event + 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7929 | { |
| 7930 | /* loop over all autocommand patterns */ |
| 7931 | prev_ap = &(first_autopat[(int)event]); |
| 7932 | for (ap = *prev_ap; ap != NULL; ap = *prev_ap) |
| 7933 | { |
| 7934 | /* loop over all commands for this pattern */ |
| 7935 | prev_ac = &(ap->cmds); |
| 7936 | for (ac = *prev_ac; ac != NULL; ac = *prev_ac) |
| 7937 | { |
| 7938 | /* remove the command if the pattern is to be deleted or when |
| 7939 | * the command has been marked for deletion */ |
| 7940 | if (ap->pat == NULL || ac->cmd == NULL) |
| 7941 | { |
| 7942 | *prev_ac = ac->next; |
| 7943 | vim_free(ac->cmd); |
| 7944 | vim_free(ac); |
| 7945 | } |
| 7946 | else |
| 7947 | prev_ac = &(ac->next); |
| 7948 | } |
| 7949 | |
| 7950 | /* remove the pattern if it has been marked for deletion */ |
| 7951 | if (ap->pat == NULL) |
| 7952 | { |
| 7953 | *prev_ap = ap->next; |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7954 | vim_regfree(ap->reg_prog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7955 | vim_free(ap); |
| 7956 | } |
| 7957 | else |
| 7958 | prev_ap = &(ap->next); |
| 7959 | } |
| 7960 | } |
| 7961 | |
| 7962 | au_need_clean = FALSE; |
| 7963 | } |
| 7964 | |
| 7965 | /* |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7966 | * Called when buffer is freed, to remove/invalidate related buffer-local |
| 7967 | * autocmds. |
| 7968 | */ |
| 7969 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7970 | aubuflocal_remove(buf_T *buf) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7971 | { |
| 7972 | AutoPat *ap; |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7973 | event_T event; |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7974 | AutoPatCmd *apc; |
| 7975 | |
| 7976 | /* invalidate currently executing autocommands */ |
| 7977 | for (apc = active_apc_list; apc; apc = apc->next) |
| 7978 | if (buf->b_fnum == apc->arg_bufnr) |
| 7979 | apc->arg_bufnr = 0; |
| 7980 | |
| 7981 | /* invalidate buflocals looping through events */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7982 | for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
| 7983 | event = (event_T)((int)event + 1)) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7984 | /* loop over all autocommand patterns */ |
| 7985 | for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) |
| 7986 | if (ap->buflocal_nr == buf->b_fnum) |
| 7987 | { |
| 7988 | au_remove_pat(ap); |
| 7989 | if (p_verbose >= 6) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 7990 | { |
| 7991 | verbose_enter(); |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7992 | smsg((char_u *) |
| 7993 | _("auto-removing autocommand: %s <buffer=%d>"), |
| 7994 | event_nr2name(event), buf->b_fnum); |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 7995 | verbose_leave(); |
| 7996 | } |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7997 | } |
| 7998 | au_cleanup(); |
| 7999 | } |
| 8000 | |
| 8001 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8002 | * Add an autocmd group name. |
| 8003 | * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. |
| 8004 | */ |
| 8005 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8006 | au_new_group(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8007 | { |
| 8008 | int i; |
| 8009 | |
| 8010 | i = au_find_group(name); |
| 8011 | if (i == AUGROUP_ERROR) /* the group doesn't exist yet, add it */ |
| 8012 | { |
| 8013 | /* First try using a free entry. */ |
| 8014 | for (i = 0; i < augroups.ga_len; ++i) |
| 8015 | if (AUGROUP_NAME(i) == NULL) |
| 8016 | break; |
| 8017 | if (i == augroups.ga_len && ga_grow(&augroups, 1) == FAIL) |
| 8018 | return AUGROUP_ERROR; |
| 8019 | |
| 8020 | AUGROUP_NAME(i) = vim_strsave(name); |
| 8021 | if (AUGROUP_NAME(i) == NULL) |
| 8022 | return AUGROUP_ERROR; |
| 8023 | if (i == augroups.ga_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8024 | ++augroups.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8025 | } |
| 8026 | |
| 8027 | return i; |
| 8028 | } |
| 8029 | |
| 8030 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8031 | au_del_group(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8032 | { |
| 8033 | int i; |
| 8034 | |
| 8035 | i = au_find_group(name); |
| 8036 | if (i == AUGROUP_ERROR) /* the group doesn't exist */ |
| 8037 | EMSG2(_("E367: No such group: \"%s\""), name); |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 8038 | else if (i == current_augroup) |
| 8039 | EMSG(_("E936: Cannot delete the current group")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8040 | else |
| 8041 | { |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 8042 | event_T event; |
| 8043 | AutoPat *ap; |
| 8044 | int in_use = FALSE; |
| 8045 | |
| 8046 | for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
| 8047 | event = (event_T)((int)event + 1)) |
| 8048 | { |
| 8049 | for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) |
Bram Moolenaar | 5c80908 | 2016-09-01 16:21:48 +0200 | [diff] [blame] | 8050 | if (ap->group == i && ap->pat != NULL) |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 8051 | { |
| 8052 | give_warning((char_u *)_("W19: Deleting augroup that is still in use"), TRUE); |
| 8053 | in_use = TRUE; |
| 8054 | event = NUM_EVENTS; |
| 8055 | break; |
| 8056 | } |
| 8057 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8058 | vim_free(AUGROUP_NAME(i)); |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 8059 | if (in_use) |
| 8060 | { |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 8061 | AUGROUP_NAME(i) = get_deleted_augroup(); |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 8062 | } |
| 8063 | else |
| 8064 | AUGROUP_NAME(i) = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8065 | } |
| 8066 | } |
| 8067 | |
| 8068 | /* |
| 8069 | * Find the ID of an autocmd group name. |
| 8070 | * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. |
| 8071 | */ |
| 8072 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8073 | au_find_group(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8074 | { |
| 8075 | int i; |
| 8076 | |
| 8077 | for (i = 0; i < augroups.ga_len; ++i) |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 8078 | if (AUGROUP_NAME(i) != NULL && AUGROUP_NAME(i) != get_deleted_augroup() |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 8079 | && STRCMP(AUGROUP_NAME(i), name) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8080 | return i; |
| 8081 | return AUGROUP_ERROR; |
| 8082 | } |
| 8083 | |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 8084 | /* |
| 8085 | * Return TRUE if augroup "name" exists. |
| 8086 | */ |
| 8087 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8088 | au_has_group(char_u *name) |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 8089 | { |
| 8090 | return au_find_group(name) != AUGROUP_ERROR; |
| 8091 | } |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 8092 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8093 | /* |
| 8094 | * ":augroup {name}". |
| 8095 | */ |
| 8096 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8097 | do_augroup(char_u *arg, int del_group) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8098 | { |
| 8099 | int i; |
| 8100 | |
| 8101 | if (del_group) |
| 8102 | { |
| 8103 | if (*arg == NUL) |
| 8104 | EMSG(_(e_argreq)); |
| 8105 | else |
| 8106 | au_del_group(arg); |
| 8107 | } |
| 8108 | else if (STRICMP(arg, "end") == 0) /* ":aug end": back to group 0 */ |
| 8109 | current_augroup = AUGROUP_DEFAULT; |
| 8110 | else if (*arg) /* ":aug xxx": switch to group xxx */ |
| 8111 | { |
| 8112 | i = au_new_group(arg); |
| 8113 | if (i != AUGROUP_ERROR) |
| 8114 | current_augroup = i; |
| 8115 | } |
| 8116 | else /* ":aug": list the group names */ |
| 8117 | { |
| 8118 | msg_start(); |
| 8119 | for (i = 0; i < augroups.ga_len; ++i) |
| 8120 | { |
| 8121 | if (AUGROUP_NAME(i) != NULL) |
| 8122 | { |
| 8123 | msg_puts(AUGROUP_NAME(i)); |
| 8124 | msg_puts((char_u *)" "); |
| 8125 | } |
| 8126 | } |
| 8127 | msg_clr_eos(); |
| 8128 | msg_end(); |
| 8129 | } |
| 8130 | } |
| 8131 | |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 8132 | #if defined(EXITFREE) || defined(PROTO) |
| 8133 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8134 | free_all_autocmds(void) |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 8135 | { |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 8136 | int i; |
| 8137 | char_u *s; |
| 8138 | |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 8139 | for (current_augroup = -1; current_augroup < augroups.ga_len; |
| 8140 | ++current_augroup) |
| 8141 | do_autocmd((char_u *)"", TRUE); |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 8142 | |
| 8143 | for (i = 0; i < augroups.ga_len; ++i) |
| 8144 | { |
| 8145 | s = ((char_u **)(augroups.ga_data))[i]; |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 8146 | if (s != get_deleted_augroup()) |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 8147 | vim_free(s); |
| 8148 | } |
| 8149 | ga_clear(&augroups); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 8150 | } |
| 8151 | #endif |
| 8152 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8153 | /* |
| 8154 | * Return the event number for event name "start". |
| 8155 | * Return NUM_EVENTS if the event name was not found. |
| 8156 | * Return a pointer to the next event name in "end". |
| 8157 | */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8158 | static event_T |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8159 | event_name2nr(char_u *start, char_u **end) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8160 | { |
| 8161 | char_u *p; |
| 8162 | int i; |
| 8163 | int len; |
| 8164 | |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 8165 | /* the event name ends with end of line, '|', a blank or a comma */ |
| 8166 | for (p = start; *p && !vim_iswhite(*p) && *p != ',' && *p != '|'; ++p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8167 | ; |
| 8168 | for (i = 0; event_names[i].name != NULL; ++i) |
| 8169 | { |
| 8170 | len = (int)STRLEN(event_names[i].name); |
| 8171 | if (len == p - start && STRNICMP(event_names[i].name, start, len) == 0) |
| 8172 | break; |
| 8173 | } |
| 8174 | if (*p == ',') |
| 8175 | ++p; |
| 8176 | *end = p; |
| 8177 | if (event_names[i].name == NULL) |
| 8178 | return NUM_EVENTS; |
| 8179 | return event_names[i].event; |
| 8180 | } |
| 8181 | |
| 8182 | /* |
| 8183 | * Return the name for event "event". |
| 8184 | */ |
| 8185 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8186 | event_nr2name(event_T event) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8187 | { |
| 8188 | int i; |
| 8189 | |
| 8190 | for (i = 0; event_names[i].name != NULL; ++i) |
| 8191 | if (event_names[i].event == event) |
| 8192 | return (char_u *)event_names[i].name; |
| 8193 | return (char_u *)"Unknown"; |
| 8194 | } |
| 8195 | |
| 8196 | /* |
| 8197 | * Scan over the events. "*" stands for all events. |
| 8198 | */ |
| 8199 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8200 | find_end_event( |
| 8201 | char_u *arg, |
| 8202 | int have_group) /* TRUE when group name was found */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8203 | { |
| 8204 | char_u *pat; |
| 8205 | char_u *p; |
| 8206 | |
| 8207 | if (*arg == '*') |
| 8208 | { |
| 8209 | if (arg[1] && !vim_iswhite(arg[1])) |
| 8210 | { |
| 8211 | EMSG2(_("E215: Illegal character after *: %s"), arg); |
| 8212 | return NULL; |
| 8213 | } |
| 8214 | pat = arg + 1; |
| 8215 | } |
| 8216 | else |
| 8217 | { |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 8218 | for (pat = arg; *pat && *pat != '|' && !vim_iswhite(*pat); pat = p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8219 | { |
| 8220 | if ((int)event_name2nr(pat, &p) >= (int)NUM_EVENTS) |
| 8221 | { |
| 8222 | if (have_group) |
| 8223 | EMSG2(_("E216: No such event: %s"), pat); |
| 8224 | else |
| 8225 | EMSG2(_("E216: No such group or event: %s"), pat); |
| 8226 | return NULL; |
| 8227 | } |
| 8228 | } |
| 8229 | } |
| 8230 | return pat; |
| 8231 | } |
| 8232 | |
| 8233 | /* |
| 8234 | * Return TRUE if "event" is included in 'eventignore'. |
| 8235 | */ |
| 8236 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8237 | event_ignored(event_T event) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8238 | { |
| 8239 | char_u *p = p_ei; |
| 8240 | |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 8241 | while (*p != NUL) |
| 8242 | { |
| 8243 | if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) |
| 8244 | return TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8245 | if (event_name2nr(p, &p) == event) |
| 8246 | return TRUE; |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 8247 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8248 | |
| 8249 | return FALSE; |
| 8250 | } |
| 8251 | |
| 8252 | /* |
| 8253 | * Return OK when the contents of p_ei is valid, FAIL otherwise. |
| 8254 | */ |
| 8255 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8256 | check_ei(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8257 | { |
| 8258 | char_u *p = p_ei; |
| 8259 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8260 | while (*p) |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 8261 | { |
| 8262 | if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) |
| 8263 | { |
| 8264 | p += 3; |
| 8265 | if (*p == ',') |
| 8266 | ++p; |
| 8267 | } |
| 8268 | else if (event_name2nr(p, &p) == NUM_EVENTS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8269 | return FAIL; |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 8270 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8271 | |
| 8272 | return OK; |
| 8273 | } |
| 8274 | |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 8275 | # if defined(FEAT_SYN_HL) || defined(PROTO) |
| 8276 | |
| 8277 | /* |
| 8278 | * Add "what" to 'eventignore' to skip loading syntax highlighting for every |
| 8279 | * buffer loaded into the window. "what" must start with a comma. |
| 8280 | * Returns the old value of 'eventignore' in allocated memory. |
| 8281 | */ |
| 8282 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8283 | au_event_disable(char *what) |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 8284 | { |
| 8285 | char_u *new_ei; |
| 8286 | char_u *save_ei; |
| 8287 | |
| 8288 | save_ei = vim_strsave(p_ei); |
| 8289 | if (save_ei != NULL) |
| 8290 | { |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 8291 | new_ei = vim_strnsave(p_ei, (int)(STRLEN(p_ei) + STRLEN(what))); |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 8292 | if (new_ei != NULL) |
| 8293 | { |
Bram Moolenaar | 8cac9fd | 2010-03-02 12:48:05 +0100 | [diff] [blame] | 8294 | if (*what == ',' && *p_ei == NUL) |
| 8295 | STRCPY(new_ei, what + 1); |
| 8296 | else |
| 8297 | STRCAT(new_ei, what); |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 8298 | set_string_option_direct((char_u *)"ei", -1, new_ei, |
| 8299 | OPT_FREE, SID_NONE); |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 8300 | vim_free(new_ei); |
| 8301 | } |
| 8302 | } |
| 8303 | return save_ei; |
| 8304 | } |
| 8305 | |
| 8306 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8307 | au_event_restore(char_u *old_ei) |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 8308 | { |
| 8309 | if (old_ei != NULL) |
| 8310 | { |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 8311 | set_string_option_direct((char_u *)"ei", -1, old_ei, |
| 8312 | OPT_FREE, SID_NONE); |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 8313 | vim_free(old_ei); |
| 8314 | } |
| 8315 | } |
| 8316 | # endif /* FEAT_SYN_HL */ |
| 8317 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8318 | /* |
| 8319 | * do_autocmd() -- implements the :autocmd command. Can be used in the |
| 8320 | * following ways: |
| 8321 | * |
| 8322 | * :autocmd <event> <pat> <cmd> Add <cmd> to the list of commands that |
| 8323 | * will be automatically executed for <event> |
| 8324 | * when editing a file matching <pat>, in |
| 8325 | * the current group. |
| 8326 | * :autocmd <event> <pat> Show the auto-commands associated with |
| 8327 | * <event> and <pat>. |
| 8328 | * :autocmd <event> Show the auto-commands associated with |
| 8329 | * <event>. |
| 8330 | * :autocmd Show all auto-commands. |
| 8331 | * :autocmd! <event> <pat> <cmd> Remove all auto-commands associated with |
| 8332 | * <event> and <pat>, and add the command |
| 8333 | * <cmd>, for the current group. |
| 8334 | * :autocmd! <event> <pat> Remove all auto-commands associated with |
| 8335 | * <event> and <pat> for the current group. |
| 8336 | * :autocmd! <event> Remove all auto-commands associated with |
| 8337 | * <event> for the current group. |
| 8338 | * :autocmd! Remove ALL auto-commands for the current |
| 8339 | * group. |
| 8340 | * |
| 8341 | * Multiple events and patterns may be given separated by commas. Here are |
| 8342 | * some examples: |
| 8343 | * :autocmd bufread,bufenter *.c,*.h set tw=0 smartindent noic |
| 8344 | * :autocmd bufleave * set tw=79 nosmartindent ic infercase |
| 8345 | * |
| 8346 | * :autocmd * *.c show all autocommands for *.c files. |
Bram Moolenaar | d35f971 | 2005-12-18 22:02:33 +0000 | [diff] [blame] | 8347 | * |
| 8348 | * Mostly a {group} argument can optionally appear before <event>. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8349 | */ |
| 8350 | void |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 8351 | do_autocmd(char_u *arg_in, int forceit) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8352 | { |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 8353 | char_u *arg = arg_in; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8354 | char_u *pat; |
| 8355 | char_u *envpat = NULL; |
| 8356 | char_u *cmd; |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8357 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8358 | int need_free = FALSE; |
| 8359 | int nested = FALSE; |
| 8360 | int group; |
| 8361 | |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 8362 | if (*arg == '|') |
| 8363 | { |
| 8364 | arg = (char_u *)""; |
| 8365 | group = AUGROUP_ALL; /* no argument, use all groups */ |
| 8366 | } |
| 8367 | else |
| 8368 | { |
| 8369 | /* |
| 8370 | * Check for a legal group name. If not, use AUGROUP_ALL. |
| 8371 | */ |
| 8372 | group = au_get_grouparg(&arg); |
| 8373 | if (arg == NULL) /* out of memory */ |
| 8374 | return; |
| 8375 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8376 | |
| 8377 | /* |
| 8378 | * Scan over the events. |
| 8379 | * If we find an illegal name, return here, don't do anything. |
| 8380 | */ |
| 8381 | pat = find_end_event(arg, group != AUGROUP_ALL); |
| 8382 | if (pat == NULL) |
| 8383 | return; |
| 8384 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8385 | pat = skipwhite(pat); |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 8386 | if (*pat == '|') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8387 | { |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 8388 | pat = (char_u *)""; |
| 8389 | cmd = (char_u *)""; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8390 | } |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 8391 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8392 | { |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 8393 | /* |
| 8394 | * Scan over the pattern. Put a NUL at the end. |
| 8395 | */ |
| 8396 | cmd = pat; |
| 8397 | while (*cmd && (!vim_iswhite(*cmd) || cmd[-1] == '\\')) |
| 8398 | cmd++; |
| 8399 | if (*cmd) |
| 8400 | *cmd++ = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8401 | |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 8402 | /* Expand environment variables in the pattern. Set 'shellslash', we want |
| 8403 | * forward slashes here. */ |
| 8404 | if (vim_strchr(pat, '$') != NULL || vim_strchr(pat, '~') != NULL) |
| 8405 | { |
| 8406 | #ifdef BACKSLASH_IN_FILENAME |
| 8407 | int p_ssl_save = p_ssl; |
| 8408 | |
| 8409 | p_ssl = TRUE; |
| 8410 | #endif |
| 8411 | envpat = expand_env_save(pat); |
| 8412 | #ifdef BACKSLASH_IN_FILENAME |
| 8413 | p_ssl = p_ssl_save; |
| 8414 | #endif |
| 8415 | if (envpat != NULL) |
| 8416 | pat = envpat; |
| 8417 | } |
| 8418 | |
| 8419 | /* |
| 8420 | * Check for "nested" flag. |
| 8421 | */ |
| 8422 | cmd = skipwhite(cmd); |
| 8423 | if (*cmd != NUL && STRNCMP(cmd, "nested", 6) == 0 && vim_iswhite(cmd[6])) |
| 8424 | { |
| 8425 | nested = TRUE; |
| 8426 | cmd = skipwhite(cmd + 6); |
| 8427 | } |
| 8428 | |
| 8429 | /* |
| 8430 | * Find the start of the commands. |
| 8431 | * Expand <sfile> in it. |
| 8432 | */ |
| 8433 | if (*cmd != NUL) |
| 8434 | { |
| 8435 | cmd = expand_sfile(cmd); |
| 8436 | if (cmd == NULL) /* some error */ |
| 8437 | return; |
| 8438 | need_free = TRUE; |
| 8439 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8440 | } |
| 8441 | |
| 8442 | /* |
| 8443 | * Print header when showing autocommands. |
| 8444 | */ |
| 8445 | if (!forceit && *cmd == NUL) |
| 8446 | { |
| 8447 | /* Highlight title */ |
| 8448 | MSG_PUTS_TITLE(_("\n--- Auto-Commands ---")); |
| 8449 | } |
| 8450 | |
| 8451 | /* |
| 8452 | * Loop over the events. |
| 8453 | */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8454 | last_event = (event_T)-1; /* for listing the event name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8455 | last_group = AUGROUP_ERROR; /* for listing the group name */ |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 8456 | if (*arg == '*' || *arg == NUL || *arg == '|') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8457 | { |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8458 | for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
| 8459 | event = (event_T)((int)event + 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8460 | if (do_autocmd_event(event, pat, |
| 8461 | nested, cmd, forceit, group) == FAIL) |
| 8462 | break; |
| 8463 | } |
| 8464 | else |
| 8465 | { |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 8466 | while (*arg && *arg != '|' && !vim_iswhite(*arg)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8467 | if (do_autocmd_event(event_name2nr(arg, &arg), pat, |
| 8468 | nested, cmd, forceit, group) == FAIL) |
| 8469 | break; |
| 8470 | } |
| 8471 | |
| 8472 | if (need_free) |
| 8473 | vim_free(cmd); |
| 8474 | vim_free(envpat); |
| 8475 | } |
| 8476 | |
| 8477 | /* |
| 8478 | * Find the group ID in a ":autocmd" or ":doautocmd" argument. |
| 8479 | * The "argp" argument is advanced to the following argument. |
| 8480 | * |
| 8481 | * Returns the group ID, AUGROUP_ERROR for error (out of memory). |
| 8482 | */ |
| 8483 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8484 | au_get_grouparg(char_u **argp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8485 | { |
| 8486 | char_u *group_name; |
| 8487 | char_u *p; |
| 8488 | char_u *arg = *argp; |
| 8489 | int group = AUGROUP_ALL; |
| 8490 | |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 8491 | for (p = arg; *p && !vim_iswhite(*p) && *p != '|'; ++p) |
| 8492 | ; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8493 | if (p > arg) |
| 8494 | { |
| 8495 | group_name = vim_strnsave(arg, (int)(p - arg)); |
| 8496 | if (group_name == NULL) /* out of memory */ |
| 8497 | return AUGROUP_ERROR; |
| 8498 | group = au_find_group(group_name); |
| 8499 | if (group == AUGROUP_ERROR) |
| 8500 | group = AUGROUP_ALL; /* no match, use all groups */ |
| 8501 | else |
| 8502 | *argp = skipwhite(p); /* match, skip over group name */ |
| 8503 | vim_free(group_name); |
| 8504 | } |
| 8505 | return group; |
| 8506 | } |
| 8507 | |
| 8508 | /* |
| 8509 | * do_autocmd() for one event. |
| 8510 | * If *pat == NUL do for all patterns. |
| 8511 | * If *cmd == NUL show entries. |
| 8512 | * If forceit == TRUE delete entries. |
| 8513 | * If group is not AUGROUP_ALL, only use this group. |
| 8514 | */ |
| 8515 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8516 | do_autocmd_event( |
| 8517 | event_T event, |
| 8518 | char_u *pat, |
| 8519 | int nested, |
| 8520 | char_u *cmd, |
| 8521 | int forceit, |
| 8522 | int group) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8523 | { |
| 8524 | AutoPat *ap; |
| 8525 | AutoPat **prev_ap; |
| 8526 | AutoCmd *ac; |
| 8527 | AutoCmd **prev_ac; |
| 8528 | int brace_level; |
| 8529 | char_u *endpat; |
| 8530 | int findgroup; |
| 8531 | int allgroups; |
| 8532 | int patlen; |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 8533 | int is_buflocal; |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8534 | int buflocal_nr; |
| 8535 | char_u buflocal_pat[25]; /* for "<buffer=X>" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8536 | |
| 8537 | if (group == AUGROUP_ALL) |
| 8538 | findgroup = current_augroup; |
| 8539 | else |
| 8540 | findgroup = group; |
| 8541 | allgroups = (group == AUGROUP_ALL && !forceit && *cmd == NUL); |
| 8542 | |
| 8543 | /* |
| 8544 | * Show or delete all patterns for an event. |
| 8545 | */ |
| 8546 | if (*pat == NUL) |
| 8547 | { |
| 8548 | for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) |
| 8549 | { |
| 8550 | if (forceit) /* delete the AutoPat, if it's in the current group */ |
| 8551 | { |
| 8552 | if (ap->group == findgroup) |
| 8553 | au_remove_pat(ap); |
| 8554 | } |
| 8555 | else if (group == AUGROUP_ALL || ap->group == group) |
| 8556 | show_autocmd(ap, event); |
| 8557 | } |
| 8558 | } |
| 8559 | |
| 8560 | /* |
| 8561 | * Loop through all the specified patterns. |
| 8562 | */ |
| 8563 | for ( ; *pat; pat = (*endpat == ',' ? endpat + 1 : endpat)) |
| 8564 | { |
| 8565 | /* |
| 8566 | * Find end of the pattern. |
| 8567 | * Watch out for a comma in braces, like "*.\{obj,o\}". |
| 8568 | */ |
| 8569 | brace_level = 0; |
| 8570 | for (endpat = pat; *endpat && (*endpat != ',' || brace_level |
Bram Moolenaar | 6b9be1b | 2015-07-28 13:33:45 +0200 | [diff] [blame] | 8571 | || (endpat > pat && endpat[-1] == '\\')); ++endpat) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8572 | { |
| 8573 | if (*endpat == '{') |
| 8574 | brace_level++; |
| 8575 | else if (*endpat == '}') |
| 8576 | brace_level--; |
| 8577 | } |
| 8578 | if (pat == endpat) /* ignore single comma */ |
| 8579 | continue; |
| 8580 | patlen = (int)(endpat - pat); |
| 8581 | |
| 8582 | /* |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8583 | * detect special <buflocal[=X]> buffer-local patterns |
| 8584 | */ |
| 8585 | is_buflocal = FALSE; |
| 8586 | buflocal_nr = 0; |
| 8587 | |
Bram Moolenaar | 1e99782 | 2015-02-17 16:04:57 +0100 | [diff] [blame] | 8588 | if (patlen >= 8 && STRNCMP(pat, "<buffer", 7) == 0 |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8589 | && pat[patlen - 1] == '>') |
| 8590 | { |
Bram Moolenaar | 1e99782 | 2015-02-17 16:04:57 +0100 | [diff] [blame] | 8591 | /* "<buffer...>": Error will be printed only for addition. |
| 8592 | * printing and removing will proceed silently. */ |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8593 | is_buflocal = TRUE; |
| 8594 | if (patlen == 8) |
Bram Moolenaar | 1e99782 | 2015-02-17 16:04:57 +0100 | [diff] [blame] | 8595 | /* "<buffer>" */ |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8596 | buflocal_nr = curbuf->b_fnum; |
| 8597 | else if (patlen > 9 && pat[7] == '=') |
| 8598 | { |
Bram Moolenaar | 1e99782 | 2015-02-17 16:04:57 +0100 | [diff] [blame] | 8599 | if (patlen == 13 && STRNICMP(pat, "<buffer=abuf>", 13) == 0) |
| 8600 | /* "<buffer=abuf>" */ |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8601 | buflocal_nr = autocmd_bufnr; |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8602 | else if (skipdigits(pat + 8) == pat + patlen - 1) |
Bram Moolenaar | 1e99782 | 2015-02-17 16:04:57 +0100 | [diff] [blame] | 8603 | /* "<buffer=123>" */ |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8604 | buflocal_nr = atoi((char *)pat + 8); |
| 8605 | } |
| 8606 | } |
| 8607 | |
| 8608 | if (is_buflocal) |
| 8609 | { |
| 8610 | /* normalize pat into standard "<buffer>#N" form */ |
| 8611 | sprintf((char *)buflocal_pat, "<buffer=%d>", buflocal_nr); |
| 8612 | pat = buflocal_pat; /* can modify pat and patlen */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 8613 | patlen = (int)STRLEN(buflocal_pat); /* but not endpat */ |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8614 | } |
| 8615 | |
| 8616 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8617 | * Find AutoPat entries with this pattern. |
| 8618 | */ |
| 8619 | prev_ap = &first_autopat[(int)event]; |
| 8620 | while ((ap = *prev_ap) != NULL) |
| 8621 | { |
| 8622 | if (ap->pat != NULL) |
| 8623 | { |
| 8624 | /* Accept a pattern when: |
| 8625 | * - a group was specified and it's that group, or a group was |
| 8626 | * not specified and it's the current group, or a group was |
| 8627 | * not specified and we are listing |
| 8628 | * - the length of the pattern matches |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8629 | * - the pattern matches. |
| 8630 | * For <buffer[=X]>, this condition works because we normalize |
| 8631 | * all buffer-local patterns. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8632 | */ |
| 8633 | if ((allgroups || ap->group == findgroup) |
| 8634 | && ap->patlen == patlen |
| 8635 | && STRNCMP(pat, ap->pat, patlen) == 0) |
| 8636 | { |
| 8637 | /* |
| 8638 | * Remove existing autocommands. |
| 8639 | * If adding any new autocmd's for this AutoPat, don't |
| 8640 | * delete the pattern from the autopat list, append to |
| 8641 | * this list. |
| 8642 | */ |
| 8643 | if (forceit) |
| 8644 | { |
| 8645 | if (*cmd != NUL && ap->next == NULL) |
| 8646 | { |
| 8647 | au_remove_cmds(ap); |
| 8648 | break; |
| 8649 | } |
| 8650 | au_remove_pat(ap); |
| 8651 | } |
| 8652 | |
| 8653 | /* |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8654 | * Show autocmd's for this autopat, or buflocals <buffer=X> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8655 | */ |
| 8656 | else if (*cmd == NUL) |
| 8657 | show_autocmd(ap, event); |
| 8658 | |
| 8659 | /* |
| 8660 | * Add autocmd to this autopat, if it's the last one. |
| 8661 | */ |
| 8662 | else if (ap->next == NULL) |
| 8663 | break; |
| 8664 | } |
| 8665 | } |
| 8666 | prev_ap = &ap->next; |
| 8667 | } |
| 8668 | |
| 8669 | /* |
| 8670 | * Add a new command. |
| 8671 | */ |
| 8672 | if (*cmd != NUL) |
| 8673 | { |
| 8674 | /* |
| 8675 | * If the pattern we want to add a command to does appear at the |
| 8676 | * end of the list (or not is not in the list at all), add the |
| 8677 | * pattern at the end of the list. |
| 8678 | */ |
| 8679 | if (ap == NULL) |
| 8680 | { |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8681 | /* refuse to add buffer-local ap if buffer number is invalid */ |
| 8682 | if (is_buflocal && (buflocal_nr == 0 |
| 8683 | || buflist_findnr(buflocal_nr) == NULL)) |
| 8684 | { |
| 8685 | EMSGN(_("E680: <buffer=%d>: invalid buffer number "), |
| 8686 | buflocal_nr); |
| 8687 | return FAIL; |
| 8688 | } |
| 8689 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8690 | ap = (AutoPat *)alloc((unsigned)sizeof(AutoPat)); |
| 8691 | if (ap == NULL) |
| 8692 | return FAIL; |
| 8693 | ap->pat = vim_strnsave(pat, patlen); |
| 8694 | ap->patlen = patlen; |
| 8695 | if (ap->pat == NULL) |
| 8696 | { |
| 8697 | vim_free(ap); |
| 8698 | return FAIL; |
| 8699 | } |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8700 | |
| 8701 | if (is_buflocal) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8702 | { |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8703 | ap->buflocal_nr = buflocal_nr; |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 8704 | ap->reg_prog = NULL; |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8705 | } |
| 8706 | else |
| 8707 | { |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 8708 | char_u *reg_pat; |
| 8709 | |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8710 | ap->buflocal_nr = 0; |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 8711 | reg_pat = file_pat_to_reg_pat(pat, endpat, |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8712 | &ap->allow_dirs, TRUE); |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 8713 | if (reg_pat != NULL) |
| 8714 | ap->reg_prog = vim_regcomp(reg_pat, RE_MAGIC); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 8715 | vim_free(reg_pat); |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 8716 | if (reg_pat == NULL || ap->reg_prog == NULL) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8717 | { |
| 8718 | vim_free(ap->pat); |
| 8719 | vim_free(ap); |
| 8720 | return FAIL; |
| 8721 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8722 | } |
| 8723 | ap->cmds = NULL; |
| 8724 | *prev_ap = ap; |
| 8725 | ap->next = NULL; |
| 8726 | if (group == AUGROUP_ALL) |
| 8727 | ap->group = current_augroup; |
| 8728 | else |
| 8729 | ap->group = group; |
| 8730 | } |
| 8731 | |
| 8732 | /* |
| 8733 | * Add the autocmd at the end of the AutoCmd list. |
| 8734 | */ |
| 8735 | prev_ac = &(ap->cmds); |
| 8736 | while ((ac = *prev_ac) != NULL) |
| 8737 | prev_ac = &ac->next; |
| 8738 | ac = (AutoCmd *)alloc((unsigned)sizeof(AutoCmd)); |
| 8739 | if (ac == NULL) |
| 8740 | return FAIL; |
| 8741 | ac->cmd = vim_strsave(cmd); |
| 8742 | #ifdef FEAT_EVAL |
| 8743 | ac->scriptID = current_SID; |
| 8744 | #endif |
| 8745 | if (ac->cmd == NULL) |
| 8746 | { |
| 8747 | vim_free(ac); |
| 8748 | return FAIL; |
| 8749 | } |
| 8750 | ac->next = NULL; |
| 8751 | *prev_ac = ac; |
| 8752 | ac->nested = nested; |
| 8753 | } |
| 8754 | } |
| 8755 | |
| 8756 | au_cleanup(); /* may really delete removed patterns/commands now */ |
| 8757 | return OK; |
| 8758 | } |
| 8759 | |
| 8760 | /* |
| 8761 | * Implementation of ":doautocmd [group] event [fname]". |
| 8762 | * Return OK for success, FAIL for failure; |
| 8763 | */ |
| 8764 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8765 | do_doautocmd( |
| 8766 | char_u *arg, |
Bram Moolenaar | 1610d05 | 2016-06-09 22:53:01 +0200 | [diff] [blame] | 8767 | int do_msg, /* give message for no matching autocmds? */ |
| 8768 | int *did_something) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8769 | { |
| 8770 | char_u *fname; |
| 8771 | int nothing_done = TRUE; |
| 8772 | int group; |
| 8773 | |
Bram Moolenaar | 1610d05 | 2016-06-09 22:53:01 +0200 | [diff] [blame] | 8774 | if (did_something != NULL) |
Bram Moolenaar | 76ae22f | 2016-06-13 20:00:29 +0200 | [diff] [blame] | 8775 | *did_something = FALSE; |
Bram Moolenaar | 1610d05 | 2016-06-09 22:53:01 +0200 | [diff] [blame] | 8776 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8777 | /* |
| 8778 | * Check for a legal group name. If not, use AUGROUP_ALL. |
| 8779 | */ |
| 8780 | group = au_get_grouparg(&arg); |
| 8781 | if (arg == NULL) /* out of memory */ |
| 8782 | return FAIL; |
| 8783 | |
| 8784 | if (*arg == '*') |
| 8785 | { |
| 8786 | EMSG(_("E217: Can't execute autocommands for ALL events")); |
| 8787 | return FAIL; |
| 8788 | } |
| 8789 | |
| 8790 | /* |
| 8791 | * Scan over the events. |
| 8792 | * If we find an illegal name, return here, don't do anything. |
| 8793 | */ |
| 8794 | fname = find_end_event(arg, group != AUGROUP_ALL); |
| 8795 | if (fname == NULL) |
| 8796 | return FAIL; |
| 8797 | |
| 8798 | fname = skipwhite(fname); |
| 8799 | |
| 8800 | /* |
| 8801 | * Loop over the events. |
| 8802 | */ |
| 8803 | while (*arg && !vim_iswhite(*arg)) |
| 8804 | if (apply_autocmds_group(event_name2nr(arg, &arg), |
| 8805 | fname, NULL, TRUE, group, curbuf, NULL)) |
| 8806 | nothing_done = FALSE; |
| 8807 | |
| 8808 | if (nothing_done && do_msg) |
| 8809 | MSG(_("No matching autocommands")); |
Bram Moolenaar | 1610d05 | 2016-06-09 22:53:01 +0200 | [diff] [blame] | 8810 | if (did_something != NULL) |
| 8811 | *did_something = !nothing_done; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8812 | |
| 8813 | #ifdef FEAT_EVAL |
| 8814 | return aborting() ? FAIL : OK; |
| 8815 | #else |
| 8816 | return OK; |
| 8817 | #endif |
| 8818 | } |
| 8819 | |
| 8820 | /* |
| 8821 | * ":doautoall": execute autocommands for each loaded buffer. |
| 8822 | */ |
| 8823 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8824 | ex_doautoall(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8825 | { |
| 8826 | int retval; |
| 8827 | aco_save_T aco; |
| 8828 | buf_T *buf; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 8829 | bufref_T bufref; |
Bram Moolenaar | a61d5fb | 2012-02-12 00:18:58 +0100 | [diff] [blame] | 8830 | char_u *arg = eap->arg; |
Bram Moolenaar | 60542ac | 2012-02-12 20:14:01 +0100 | [diff] [blame] | 8831 | int call_do_modelines = check_nomodeline(&arg); |
Bram Moolenaar | 1610d05 | 2016-06-09 22:53:01 +0200 | [diff] [blame] | 8832 | int did_aucmd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8833 | |
| 8834 | /* |
| 8835 | * This is a bit tricky: For some commands curwin->w_buffer needs to be |
| 8836 | * equal to curbuf, but for some buffers there may not be a window. |
| 8837 | * So we change the buffer for the current window for a moment. This |
| 8838 | * gives problems when the autocommands make changes to the list of |
| 8839 | * buffers or windows... |
| 8840 | */ |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 8841 | FOR_ALL_BUFFERS(buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8842 | { |
Bram Moolenaar | 3a84797 | 2008-07-08 09:36:58 +0000 | [diff] [blame] | 8843 | if (buf->b_ml.ml_mfp != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8844 | { |
| 8845 | /* find a window for this buffer and save some values */ |
| 8846 | aucmd_prepbuf(&aco, buf); |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 8847 | set_bufref(&bufref, buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8848 | |
| 8849 | /* execute the autocommands for this buffer */ |
Bram Moolenaar | 1610d05 | 2016-06-09 22:53:01 +0200 | [diff] [blame] | 8850 | retval = do_doautocmd(arg, FALSE, &did_aucmd); |
Bram Moolenaar | eeefcc7 | 2007-05-01 21:21:21 +0000 | [diff] [blame] | 8851 | |
Bram Moolenaar | 1610d05 | 2016-06-09 22:53:01 +0200 | [diff] [blame] | 8852 | if (call_do_modelines && did_aucmd) |
Bram Moolenaar | a61d5fb | 2012-02-12 00:18:58 +0100 | [diff] [blame] | 8853 | { |
| 8854 | /* Execute the modeline settings, but don't set window-local |
| 8855 | * options if we are using the current window for another |
| 8856 | * buffer. */ |
| 8857 | do_modelines(curwin == aucmd_win ? OPT_NOWIN : 0); |
| 8858 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8859 | |
| 8860 | /* restore the current window */ |
| 8861 | aucmd_restbuf(&aco); |
| 8862 | |
| 8863 | /* stop if there is some error or buffer was deleted */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 8864 | if (retval == FAIL || !bufref_valid(&bufref)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8865 | break; |
| 8866 | } |
| 8867 | } |
| 8868 | |
| 8869 | check_cursor(); /* just in case lines got deleted */ |
| 8870 | } |
| 8871 | |
| 8872 | /* |
Bram Moolenaar | 60542ac | 2012-02-12 20:14:01 +0100 | [diff] [blame] | 8873 | * Check *argp for <nomodeline>. When it is present return FALSE, otherwise |
| 8874 | * return TRUE and advance *argp to after it. |
| 8875 | * Thus return TRUE when do_modelines() should be called. |
| 8876 | */ |
| 8877 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8878 | check_nomodeline(char_u **argp) |
Bram Moolenaar | 60542ac | 2012-02-12 20:14:01 +0100 | [diff] [blame] | 8879 | { |
| 8880 | if (STRNCMP(*argp, "<nomodeline>", 12) == 0) |
| 8881 | { |
| 8882 | *argp = skipwhite(*argp + 12); |
| 8883 | return FALSE; |
| 8884 | } |
| 8885 | return TRUE; |
| 8886 | } |
| 8887 | |
| 8888 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8889 | * Prepare for executing autocommands for (hidden) buffer "buf". |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8890 | * Search for a visible window containing the current buffer. If there isn't |
| 8891 | * one then use "aucmd_win". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8892 | * Set "curbuf" and "curwin" to match "buf". |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 8893 | * When FEAT_AUTOCMD is not defined another version is used, see below. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8894 | */ |
| 8895 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8896 | aucmd_prepbuf( |
| 8897 | aco_save_T *aco, /* structure to save values in */ |
| 8898 | buf_T *buf) /* new curbuf */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8899 | { |
| 8900 | win_T *win; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8901 | #ifdef FEAT_WINDOWS |
| 8902 | int save_ea; |
| 8903 | #endif |
Bram Moolenaar | 01c458e | 2013-08-05 22:02:20 +0200 | [diff] [blame] | 8904 | #ifdef FEAT_AUTOCHDIR |
Bram Moolenaar | 4008f4f | 2013-08-02 17:08:13 +0200 | [diff] [blame] | 8905 | int save_acd; |
Bram Moolenaar | 01c458e | 2013-08-05 22:02:20 +0200 | [diff] [blame] | 8906 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8907 | |
| 8908 | /* Find a window that is for the new buffer */ |
| 8909 | if (buf == curbuf) /* be quick when buf is curbuf */ |
| 8910 | win = curwin; |
| 8911 | else |
| 8912 | #ifdef FEAT_WINDOWS |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 8913 | FOR_ALL_WINDOWS(win) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8914 | if (win->w_buffer == buf) |
| 8915 | break; |
| 8916 | #else |
| 8917 | win = NULL; |
| 8918 | #endif |
| 8919 | |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8920 | /* Allocate "aucmd_win" when needed. If this fails (out of memory) fall |
| 8921 | * back to using the current window. */ |
| 8922 | if (win == NULL && aucmd_win == NULL) |
| 8923 | { |
| 8924 | win_alloc_aucmd_win(); |
| 8925 | if (aucmd_win == NULL) |
| 8926 | win = curwin; |
| 8927 | } |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 8928 | if (win == NULL && aucmd_win_used) |
| 8929 | /* Strange recursive autocommand, fall back to using the current |
| 8930 | * window. Expect a few side effects... */ |
| 8931 | win = curwin; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8932 | |
| 8933 | aco->save_curwin = curwin; |
| 8934 | aco->save_curbuf = curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8935 | if (win != NULL) |
| 8936 | { |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8937 | /* There is a window for "buf" in the current tab page, make it the |
| 8938 | * curwin. This is preferred, it has the least side effects (esp. if |
| 8939 | * "buf" is curbuf). */ |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 8940 | aco->use_aucmd_win = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8941 | curwin = win; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8942 | } |
| 8943 | else |
| 8944 | { |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8945 | /* There is no window for "buf", use "aucmd_win". To minimize the side |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 8946 | * effects, insert it in the current tab page. |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8947 | * Anything related to a window (e.g., setting folds) may have |
| 8948 | * unexpected results. */ |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 8949 | aco->use_aucmd_win = TRUE; |
| 8950 | aucmd_win_used = TRUE; |
Bram Moolenaar | f061e0b | 2009-06-24 15:32:01 +0000 | [diff] [blame] | 8951 | aucmd_win->w_buffer = buf; |
Bram Moolenaar | cdddaa4 | 2010-06-07 23:07:44 +0200 | [diff] [blame] | 8952 | aucmd_win->w_s = &buf->b_s; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8953 | ++buf->b_nwindows; |
Bram Moolenaar | f061e0b | 2009-06-24 15:32:01 +0000 | [diff] [blame] | 8954 | win_init_empty(aucmd_win); /* set cursor and topline to safe values */ |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 8955 | |
| 8956 | /* Make sure w_localdir and globaldir are NULL to avoid a chdir() in |
| 8957 | * win_enter_ext(). */ |
Bram Moolenaar | 4008f4f | 2013-08-02 17:08:13 +0200 | [diff] [blame] | 8958 | vim_free(aucmd_win->w_localdir); |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 8959 | aucmd_win->w_localdir = NULL; |
| 8960 | aco->globaldir = globaldir; |
| 8961 | globaldir = NULL; |
| 8962 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8963 | |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8964 | #ifdef FEAT_WINDOWS |
Bram Moolenaar | 2bc76e6 | 2009-07-01 15:13:56 +0000 | [diff] [blame] | 8965 | /* Split the current window, put the aucmd_win in the upper half. |
| 8966 | * We don't want the BufEnter or WinEnter autocommands. */ |
| 8967 | block_autocmds(); |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8968 | make_snapshot(SNAP_AUCMD_IDX); |
| 8969 | save_ea = p_ea; |
| 8970 | p_ea = FALSE; |
Bram Moolenaar | 4008f4f | 2013-08-02 17:08:13 +0200 | [diff] [blame] | 8971 | |
Bram Moolenaar | 01c458e | 2013-08-05 22:02:20 +0200 | [diff] [blame] | 8972 | # ifdef FEAT_AUTOCHDIR |
Bram Moolenaar | 4008f4f | 2013-08-02 17:08:13 +0200 | [diff] [blame] | 8973 | /* Prevent chdir() call in win_enter_ext(), through do_autochdir(). */ |
| 8974 | save_acd = p_acd; |
| 8975 | p_acd = FALSE; |
Bram Moolenaar | 01c458e | 2013-08-05 22:02:20 +0200 | [diff] [blame] | 8976 | # endif |
Bram Moolenaar | 4008f4f | 2013-08-02 17:08:13 +0200 | [diff] [blame] | 8977 | |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8978 | (void)win_split_ins(0, WSP_TOP, aucmd_win, 0); |
| 8979 | (void)win_comp_pos(); /* recompute window positions */ |
| 8980 | p_ea = save_ea; |
Bram Moolenaar | 01c458e | 2013-08-05 22:02:20 +0200 | [diff] [blame] | 8981 | # ifdef FEAT_AUTOCHDIR |
Bram Moolenaar | 4008f4f | 2013-08-02 17:08:13 +0200 | [diff] [blame] | 8982 | p_acd = save_acd; |
Bram Moolenaar | 01c458e | 2013-08-05 22:02:20 +0200 | [diff] [blame] | 8983 | # endif |
Bram Moolenaar | 2bc76e6 | 2009-07-01 15:13:56 +0000 | [diff] [blame] | 8984 | unblock_autocmds(); |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8985 | #endif |
Bram Moolenaar | f061e0b | 2009-06-24 15:32:01 +0000 | [diff] [blame] | 8986 | curwin = aucmd_win; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8987 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8988 | curbuf = buf; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8989 | aco->new_curwin = curwin; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 8990 | set_bufref(&aco->new_curbuf, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8991 | } |
| 8992 | |
| 8993 | /* |
| 8994 | * Cleanup after executing autocommands for a (hidden) buffer. |
| 8995 | * Restore the window as it was (if possible). |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 8996 | * When FEAT_AUTOCMD is not defined another version is used, see below. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8997 | */ |
| 8998 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8999 | aucmd_restbuf( |
| 9000 | aco_save_T *aco) /* structure holding saved values */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9001 | { |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 9002 | #ifdef FEAT_WINDOWS |
| 9003 | int dummy; |
| 9004 | #endif |
| 9005 | |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 9006 | if (aco->use_aucmd_win) |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 9007 | { |
| 9008 | --curbuf->b_nwindows; |
| 9009 | #ifdef FEAT_WINDOWS |
| 9010 | /* Find "aucmd_win", it can't be closed, but it may be in another tab |
Bram Moolenaar | 2bc76e6 | 2009-07-01 15:13:56 +0000 | [diff] [blame] | 9011 | * page. Do not trigger autocommands here. */ |
| 9012 | block_autocmds(); |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 9013 | if (curwin != aucmd_win) |
| 9014 | { |
| 9015 | tabpage_T *tp; |
| 9016 | win_T *wp; |
| 9017 | |
| 9018 | FOR_ALL_TAB_WINDOWS(tp, wp) |
| 9019 | { |
| 9020 | if (wp == aucmd_win) |
| 9021 | { |
| 9022 | if (tp != curtab) |
Bram Moolenaar | 49e649f | 2013-05-06 04:50:35 +0200 | [diff] [blame] | 9023 | goto_tabpage_tp(tp, TRUE, TRUE); |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 9024 | win_goto(aucmd_win); |
Bram Moolenaar | 28f2908 | 2012-02-11 23:45:37 +0100 | [diff] [blame] | 9025 | goto win_found; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 9026 | } |
| 9027 | } |
| 9028 | } |
Bram Moolenaar | 28f2908 | 2012-02-11 23:45:37 +0100 | [diff] [blame] | 9029 | win_found: |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 9030 | |
| 9031 | /* Remove the window and frame from the tree of frames. */ |
| 9032 | (void)winframe_remove(curwin, &dummy, NULL); |
| 9033 | win_remove(curwin, NULL); |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 9034 | aucmd_win_used = FALSE; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 9035 | last_status(FALSE); /* may need to remove last status line */ |
| 9036 | restore_snapshot(SNAP_AUCMD_IDX, FALSE); |
| 9037 | (void)win_comp_pos(); /* recompute window positions */ |
Bram Moolenaar | 2bc76e6 | 2009-07-01 15:13:56 +0000 | [diff] [blame] | 9038 | unblock_autocmds(); |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 9039 | |
| 9040 | if (win_valid(aco->save_curwin)) |
| 9041 | curwin = aco->save_curwin; |
| 9042 | else |
| 9043 | /* Hmm, original window disappeared. Just use the first one. */ |
| 9044 | curwin = firstwin; |
| 9045 | # ifdef FEAT_EVAL |
Bram Moolenaar | 429fa85 | 2013-04-15 12:27:36 +0200 | [diff] [blame] | 9046 | vars_clear(&aucmd_win->w_vars->dv_hashtab); /* free all w: variables */ |
| 9047 | hash_init(&aucmd_win->w_vars->dv_hashtab); /* re-use the hashtab */ |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 9048 | # endif |
| 9049 | #else |
| 9050 | curwin = aco->save_curwin; |
| 9051 | #endif |
| 9052 | curbuf = curwin->w_buffer; |
| 9053 | |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 9054 | vim_free(globaldir); |
| 9055 | globaldir = aco->globaldir; |
| 9056 | |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 9057 | /* the buffer contents may have changed */ |
| 9058 | check_cursor(); |
| 9059 | if (curwin->w_topline > curbuf->b_ml.ml_line_count) |
| 9060 | { |
| 9061 | curwin->w_topline = curbuf->b_ml.ml_line_count; |
| 9062 | #ifdef FEAT_DIFF |
| 9063 | curwin->w_topfill = 0; |
| 9064 | #endif |
| 9065 | } |
| 9066 | #if defined(FEAT_GUI) |
| 9067 | /* Hide the scrollbars from the aucmd_win and update. */ |
| 9068 | gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_LEFT], FALSE); |
| 9069 | gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_RIGHT], FALSE); |
| 9070 | gui_may_update_scrollbars(); |
| 9071 | #endif |
| 9072 | } |
| 9073 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9074 | { |
| 9075 | /* restore curwin */ |
| 9076 | #ifdef FEAT_WINDOWS |
| 9077 | if (win_valid(aco->save_curwin)) |
| 9078 | #endif |
| 9079 | { |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 9080 | /* Restore the buffer which was previously edited by curwin, if |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 9081 | * it was changed, we are still the same window and the buffer is |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 9082 | * valid. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9083 | if (curwin == aco->new_curwin |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 9084 | && curbuf != aco->new_curbuf.br_buf |
| 9085 | && bufref_valid(&aco->new_curbuf) |
| 9086 | && aco->new_curbuf.br_buf->b_ml.ml_mfp != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9087 | { |
Bram Moolenaar | 7da9c37 | 2012-04-30 17:04:52 +0200 | [diff] [blame] | 9088 | # if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) |
| 9089 | if (curwin->w_s == &curbuf->b_s) |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 9090 | curwin->w_s = &aco->new_curbuf.br_buf->b_s; |
Bram Moolenaar | 7da9c37 | 2012-04-30 17:04:52 +0200 | [diff] [blame] | 9091 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9092 | --curbuf->b_nwindows; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 9093 | curbuf = aco->new_curbuf.br_buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9094 | curwin->w_buffer = curbuf; |
| 9095 | ++curbuf->b_nwindows; |
| 9096 | } |
| 9097 | |
| 9098 | curwin = aco->save_curwin; |
| 9099 | curbuf = curwin->w_buffer; |
Bram Moolenaar | 371932a | 2014-09-09 16:59:38 +0200 | [diff] [blame] | 9100 | /* In case the autocommand move the cursor to a position that that |
| 9101 | * not exist in curbuf. */ |
| 9102 | check_cursor(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9103 | } |
| 9104 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9105 | } |
| 9106 | |
| 9107 | static int autocmd_nested = FALSE; |
| 9108 | |
| 9109 | /* |
| 9110 | * Execute autocommands for "event" and file name "fname". |
| 9111 | * Return TRUE if some commands were executed. |
| 9112 | */ |
| 9113 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9114 | apply_autocmds( |
| 9115 | event_T event, |
| 9116 | char_u *fname, /* NULL or empty means use actual file name */ |
| 9117 | char_u *fname_io, /* fname to use for <afile> on cmdline */ |
| 9118 | int force, /* when TRUE, ignore autocmd_busy */ |
| 9119 | buf_T *buf) /* buffer for <abuf> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9120 | { |
| 9121 | return apply_autocmds_group(event, fname, fname_io, force, |
| 9122 | AUGROUP_ALL, buf, NULL); |
| 9123 | } |
| 9124 | |
| 9125 | /* |
| 9126 | * Like apply_autocmds(), but with extra "eap" argument. This takes care of |
| 9127 | * setting v:filearg. |
| 9128 | */ |
| 9129 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9130 | apply_autocmds_exarg( |
| 9131 | event_T event, |
| 9132 | char_u *fname, |
| 9133 | char_u *fname_io, |
| 9134 | int force, |
| 9135 | buf_T *buf, |
| 9136 | exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9137 | { |
| 9138 | return apply_autocmds_group(event, fname, fname_io, force, |
| 9139 | AUGROUP_ALL, buf, eap); |
| 9140 | } |
| 9141 | |
| 9142 | /* |
| 9143 | * Like apply_autocmds(), but handles the caller's retval. If the script |
| 9144 | * processing is being aborted or if retval is FAIL when inside a try |
| 9145 | * conditional, no autocommands are executed. If otherwise the autocommands |
| 9146 | * cause the script to be aborted, retval is set to FAIL. |
| 9147 | */ |
| 9148 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9149 | apply_autocmds_retval( |
| 9150 | event_T event, |
| 9151 | char_u *fname, /* NULL or empty means use actual file name */ |
| 9152 | char_u *fname_io, /* fname to use for <afile> on cmdline */ |
| 9153 | int force, /* when TRUE, ignore autocmd_busy */ |
| 9154 | buf_T *buf, /* buffer for <abuf> */ |
| 9155 | int *retval) /* pointer to caller's retval */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9156 | { |
| 9157 | int did_cmd; |
| 9158 | |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 9159 | #ifdef FEAT_EVAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9160 | if (should_abort(*retval)) |
| 9161 | return FALSE; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 9162 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9163 | |
| 9164 | did_cmd = apply_autocmds_group(event, fname, fname_io, force, |
| 9165 | AUGROUP_ALL, buf, NULL); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 9166 | if (did_cmd |
| 9167 | #ifdef FEAT_EVAL |
| 9168 | && aborting() |
| 9169 | #endif |
| 9170 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9171 | *retval = FAIL; |
| 9172 | return did_cmd; |
| 9173 | } |
| 9174 | |
Bram Moolenaar | d35f971 | 2005-12-18 22:02:33 +0000 | [diff] [blame] | 9175 | /* |
| 9176 | * Return TRUE when there is a CursorHold autocommand defined. |
| 9177 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9178 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9179 | has_cursorhold(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9180 | { |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9181 | return (first_autopat[(int)(get_real_state() == NORMAL_BUSY |
| 9182 | ? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)] != NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9183 | } |
Bram Moolenaar | d35f971 | 2005-12-18 22:02:33 +0000 | [diff] [blame] | 9184 | |
| 9185 | /* |
| 9186 | * Return TRUE if the CursorHold event can be triggered. |
| 9187 | */ |
| 9188 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9189 | trigger_cursorhold(void) |
Bram Moolenaar | d35f971 | 2005-12-18 22:02:33 +0000 | [diff] [blame] | 9190 | { |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9191 | int state; |
| 9192 | |
Bram Moolenaar | 0533443 | 2011-07-20 18:29:39 +0200 | [diff] [blame] | 9193 | if (!did_cursorhold |
| 9194 | && has_cursorhold() |
| 9195 | && !Recording |
| 9196 | && typebuf.tb_len == 0 |
Bram Moolenaar | d29a9ee | 2006-09-14 09:07:34 +0000 | [diff] [blame] | 9197 | #ifdef FEAT_INS_EXPAND |
| 9198 | && !ins_compl_active() |
| 9199 | #endif |
| 9200 | ) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9201 | { |
| 9202 | state = get_real_state(); |
| 9203 | if (state == NORMAL_BUSY || (state & INSERT) != 0) |
| 9204 | return TRUE; |
| 9205 | } |
| 9206 | return FALSE; |
Bram Moolenaar | d35f971 | 2005-12-18 22:02:33 +0000 | [diff] [blame] | 9207 | } |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9208 | |
| 9209 | /* |
| 9210 | * Return TRUE when there is a CursorMoved autocommand defined. |
| 9211 | */ |
| 9212 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9213 | has_cursormoved(void) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9214 | { |
| 9215 | return (first_autopat[(int)EVENT_CURSORMOVED] != NULL); |
| 9216 | } |
| 9217 | |
| 9218 | /* |
| 9219 | * Return TRUE when there is a CursorMovedI autocommand defined. |
| 9220 | */ |
| 9221 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9222 | has_cursormovedI(void) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9223 | { |
| 9224 | return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL); |
| 9225 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9226 | |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 9227 | /* |
Bram Moolenaar | 186628f | 2013-03-19 13:33:23 +0100 | [diff] [blame] | 9228 | * Return TRUE when there is a TextChanged autocommand defined. |
| 9229 | */ |
| 9230 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9231 | has_textchanged(void) |
Bram Moolenaar | 186628f | 2013-03-19 13:33:23 +0100 | [diff] [blame] | 9232 | { |
| 9233 | return (first_autopat[(int)EVENT_TEXTCHANGED] != NULL); |
| 9234 | } |
| 9235 | |
| 9236 | /* |
| 9237 | * Return TRUE when there is a TextChangedI autocommand defined. |
| 9238 | */ |
| 9239 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9240 | has_textchangedI(void) |
Bram Moolenaar | 186628f | 2013-03-19 13:33:23 +0100 | [diff] [blame] | 9241 | { |
| 9242 | return (first_autopat[(int)EVENT_TEXTCHANGEDI] != NULL); |
| 9243 | } |
| 9244 | |
| 9245 | /* |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 9246 | * Return TRUE when there is an InsertCharPre autocommand defined. |
| 9247 | */ |
| 9248 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9249 | has_insertcharpre(void) |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 9250 | { |
| 9251 | return (first_autopat[(int)EVENT_INSERTCHARPRE] != NULL); |
| 9252 | } |
| 9253 | |
Bram Moolenaar | d500516 | 2014-08-22 23:05:54 +0200 | [diff] [blame] | 9254 | /* |
| 9255 | * Return TRUE when there is an CmdUndefined autocommand defined. |
| 9256 | */ |
| 9257 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9258 | has_cmdundefined(void) |
Bram Moolenaar | d500516 | 2014-08-22 23:05:54 +0200 | [diff] [blame] | 9259 | { |
| 9260 | return (first_autopat[(int)EVENT_CMDUNDEFINED] != NULL); |
| 9261 | } |
| 9262 | |
| 9263 | /* |
| 9264 | * Return TRUE when there is an FuncUndefined autocommand defined. |
| 9265 | */ |
| 9266 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9267 | has_funcundefined(void) |
Bram Moolenaar | d500516 | 2014-08-22 23:05:54 +0200 | [diff] [blame] | 9268 | { |
| 9269 | return (first_autopat[(int)EVENT_FUNCUNDEFINED] != NULL); |
| 9270 | } |
| 9271 | |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 9272 | /* |
| 9273 | * Execute autocommands for "event" and file name "fname". |
| 9274 | * Return TRUE if some commands were executed. |
| 9275 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9276 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9277 | apply_autocmds_group( |
| 9278 | event_T event, |
| 9279 | char_u *fname, /* NULL or empty means use actual file name */ |
| 9280 | char_u *fname_io, /* fname to use for <afile> on cmdline, NULL means |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9281 | use fname */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9282 | int force, /* when TRUE, ignore autocmd_busy */ |
| 9283 | int group, /* group ID, or AUGROUP_ALL */ |
| 9284 | buf_T *buf, /* buffer for <abuf> */ |
| 9285 | exarg_T *eap) /* command arguments */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9286 | { |
| 9287 | char_u *sfname = NULL; /* short file name */ |
| 9288 | char_u *tail; |
| 9289 | int save_changed; |
| 9290 | buf_T *old_curbuf; |
| 9291 | int retval = FALSE; |
| 9292 | char_u *save_sourcing_name; |
| 9293 | linenr_T save_sourcing_lnum; |
| 9294 | char_u *save_autocmd_fname; |
Bram Moolenaar | f6dad43 | 2008-09-18 19:29:58 +0000 | [diff] [blame] | 9295 | int save_autocmd_fname_full; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9296 | int save_autocmd_bufnr; |
| 9297 | char_u *save_autocmd_match; |
| 9298 | int save_autocmd_busy; |
| 9299 | int save_autocmd_nested; |
| 9300 | static int nesting = 0; |
| 9301 | AutoPatCmd patcmd; |
| 9302 | AutoPat *ap; |
| 9303 | #ifdef FEAT_EVAL |
| 9304 | scid_T save_current_SID; |
| 9305 | void *save_funccalp; |
| 9306 | char_u *save_cmdarg; |
| 9307 | long save_cmdbang; |
| 9308 | #endif |
| 9309 | static int filechangeshell_busy = FALSE; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 9310 | #ifdef FEAT_PROFILE |
| 9311 | proftime_T wait_time; |
| 9312 | #endif |
Bram Moolenaar | c51b02d | 2015-02-17 10:58:25 +0100 | [diff] [blame] | 9313 | int did_save_redobuff = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9314 | |
| 9315 | /* |
| 9316 | * Quickly return if there are no autocommands for this event or |
| 9317 | * autocommands are blocked. |
| 9318 | */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 9319 | if (first_autopat[(int)event] == NULL || autocmd_blocked > 0) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9320 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9321 | |
| 9322 | /* |
| 9323 | * When autocommands are busy, new autocommands are only executed when |
| 9324 | * explicitly enabled with the "nested" flag. |
| 9325 | */ |
| 9326 | if (autocmd_busy && !(force || autocmd_nested)) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9327 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9328 | |
| 9329 | #ifdef FEAT_EVAL |
| 9330 | /* |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 9331 | * Quickly return when immediately aborting on error, or when an interrupt |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9332 | * occurred or an exception was thrown but not caught. |
| 9333 | */ |
| 9334 | if (aborting()) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9335 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9336 | #endif |
| 9337 | |
| 9338 | /* |
| 9339 | * FileChangedShell never nests, because it can create an endless loop. |
| 9340 | */ |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 9341 | if (filechangeshell_busy && (event == EVENT_FILECHANGEDSHELL |
| 9342 | || event == EVENT_FILECHANGEDSHELLPOST)) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9343 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9344 | |
| 9345 | /* |
| 9346 | * Ignore events in 'eventignore'. |
| 9347 | */ |
| 9348 | if (event_ignored(event)) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9349 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9350 | |
| 9351 | /* |
| 9352 | * Allow nesting of autocommands, but restrict the depth, because it's |
| 9353 | * possible to create an endless loop. |
| 9354 | */ |
| 9355 | if (nesting == 10) |
| 9356 | { |
| 9357 | EMSG(_("E218: autocommand nesting too deep")); |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9358 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9359 | } |
| 9360 | |
| 9361 | /* |
| 9362 | * Check if these autocommands are disabled. Used when doing ":all" or |
| 9363 | * ":ball". |
| 9364 | */ |
| 9365 | if ( (autocmd_no_enter |
| 9366 | && (event == EVENT_WINENTER || event == EVENT_BUFENTER)) |
| 9367 | || (autocmd_no_leave |
| 9368 | && (event == EVENT_WINLEAVE || event == EVENT_BUFLEAVE))) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9369 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9370 | |
| 9371 | /* |
| 9372 | * Save the autocmd_* variables and info about the current buffer. |
| 9373 | */ |
| 9374 | save_autocmd_fname = autocmd_fname; |
Bram Moolenaar | f6dad43 | 2008-09-18 19:29:58 +0000 | [diff] [blame] | 9375 | save_autocmd_fname_full = autocmd_fname_full; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9376 | save_autocmd_bufnr = autocmd_bufnr; |
| 9377 | save_autocmd_match = autocmd_match; |
| 9378 | save_autocmd_busy = autocmd_busy; |
| 9379 | save_autocmd_nested = autocmd_nested; |
| 9380 | save_changed = curbuf->b_changed; |
| 9381 | old_curbuf = curbuf; |
| 9382 | |
| 9383 | /* |
| 9384 | * Set the file name to be used for <afile>. |
Bram Moolenaar | a0174af | 2008-01-02 20:08:25 +0000 | [diff] [blame] | 9385 | * Make a copy to avoid that changing a buffer name or directory makes it |
| 9386 | * invalid. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9387 | */ |
| 9388 | if (fname_io == NULL) |
| 9389 | { |
Bram Moolenaar | 5374430 | 2015-07-17 17:38:22 +0200 | [diff] [blame] | 9390 | if (event == EVENT_COLORSCHEME || event == EVENT_OPTIONSET) |
Bram Moolenaar | b95186f | 2013-11-28 18:53:52 +0100 | [diff] [blame] | 9391 | autocmd_fname = NULL; |
| 9392 | else if (fname != NULL && *fname != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9393 | autocmd_fname = fname; |
| 9394 | else if (buf != NULL) |
Bram Moolenaar | f6dad43 | 2008-09-18 19:29:58 +0000 | [diff] [blame] | 9395 | autocmd_fname = buf->b_ffname; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9396 | else |
| 9397 | autocmd_fname = NULL; |
| 9398 | } |
| 9399 | else |
| 9400 | autocmd_fname = fname_io; |
Bram Moolenaar | a0174af | 2008-01-02 20:08:25 +0000 | [diff] [blame] | 9401 | if (autocmd_fname != NULL) |
Bram Moolenaar | f6dad43 | 2008-09-18 19:29:58 +0000 | [diff] [blame] | 9402 | autocmd_fname = vim_strsave(autocmd_fname); |
| 9403 | autocmd_fname_full = FALSE; /* call FullName_save() later */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9404 | |
| 9405 | /* |
| 9406 | * Set the buffer number to be used for <abuf>. |
| 9407 | */ |
| 9408 | if (buf == NULL) |
| 9409 | autocmd_bufnr = 0; |
| 9410 | else |
| 9411 | autocmd_bufnr = buf->b_fnum; |
| 9412 | |
| 9413 | /* |
| 9414 | * When the file name is NULL or empty, use the file name of buffer "buf". |
| 9415 | * Always use the full path of the file name to match with, in case |
| 9416 | * "allow_dirs" is set. |
| 9417 | */ |
| 9418 | if (fname == NULL || *fname == NUL) |
| 9419 | { |
| 9420 | if (buf == NULL) |
| 9421 | fname = NULL; |
| 9422 | else |
| 9423 | { |
| 9424 | #ifdef FEAT_SYN_HL |
| 9425 | if (event == EVENT_SYNTAX) |
| 9426 | fname = buf->b_p_syn; |
| 9427 | else |
| 9428 | #endif |
| 9429 | if (event == EVENT_FILETYPE) |
| 9430 | fname = buf->b_p_ft; |
| 9431 | else |
| 9432 | { |
| 9433 | if (buf->b_sfname != NULL) |
| 9434 | sfname = vim_strsave(buf->b_sfname); |
| 9435 | fname = buf->b_ffname; |
| 9436 | } |
| 9437 | } |
| 9438 | if (fname == NULL) |
| 9439 | fname = (char_u *)""; |
| 9440 | fname = vim_strsave(fname); /* make a copy, so we can change it */ |
| 9441 | } |
| 9442 | else |
| 9443 | { |
| 9444 | sfname = vim_strsave(fname); |
Bram Moolenaar | b95186f | 2013-11-28 18:53:52 +0100 | [diff] [blame] | 9445 | /* Don't try expanding FileType, Syntax, FuncUndefined, WindowID, |
| 9446 | * ColorScheme or QuickFixCmd* */ |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 9447 | if (event == EVENT_FILETYPE |
| 9448 | || event == EVENT_SYNTAX |
Bram Moolenaar | 5135d46 | 2009-04-29 16:03:38 +0000 | [diff] [blame] | 9449 | || event == EVENT_FUNCUNDEFINED |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 9450 | || event == EVENT_REMOTEREPLY |
Bram Moolenaar | b8a7b56 | 2006-02-01 21:47:16 +0000 | [diff] [blame] | 9451 | || event == EVENT_SPELLFILEMISSING |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 9452 | || event == EVENT_QUICKFIXCMDPRE |
Bram Moolenaar | b95186f | 2013-11-28 18:53:52 +0100 | [diff] [blame] | 9453 | || event == EVENT_COLORSCHEME |
Bram Moolenaar | 5374430 | 2015-07-17 17:38:22 +0200 | [diff] [blame] | 9454 | || event == EVENT_OPTIONSET |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 9455 | || event == EVENT_QUICKFIXCMDPOST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9456 | fname = vim_strsave(fname); |
| 9457 | else |
| 9458 | fname = FullName_save(fname, FALSE); |
| 9459 | } |
| 9460 | if (fname == NULL) /* out of memory */ |
| 9461 | { |
| 9462 | vim_free(sfname); |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9463 | retval = FALSE; |
| 9464 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9465 | } |
| 9466 | |
| 9467 | #ifdef BACKSLASH_IN_FILENAME |
| 9468 | /* |
| 9469 | * Replace all backslashes with forward slashes. This makes the |
| 9470 | * autocommand patterns portable between Unix and MS-DOS. |
| 9471 | */ |
| 9472 | if (sfname != NULL) |
| 9473 | forward_slash(sfname); |
| 9474 | forward_slash(fname); |
| 9475 | #endif |
| 9476 | |
| 9477 | #ifdef VMS |
| 9478 | /* remove version for correct match */ |
| 9479 | if (sfname != NULL) |
| 9480 | vms_remove_version(sfname); |
| 9481 | vms_remove_version(fname); |
| 9482 | #endif |
| 9483 | |
| 9484 | /* |
| 9485 | * Set the name to be used for <amatch>. |
| 9486 | */ |
| 9487 | autocmd_match = fname; |
| 9488 | |
| 9489 | |
| 9490 | /* Don't redraw while doing auto commands. */ |
| 9491 | ++RedrawingDisabled; |
| 9492 | save_sourcing_name = sourcing_name; |
| 9493 | sourcing_name = NULL; /* don't free this one */ |
| 9494 | save_sourcing_lnum = sourcing_lnum; |
| 9495 | sourcing_lnum = 0; /* no line number here */ |
| 9496 | |
| 9497 | #ifdef FEAT_EVAL |
| 9498 | save_current_SID = current_SID; |
| 9499 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 9500 | # ifdef FEAT_PROFILE |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 9501 | if (do_profiling == PROF_YES) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 9502 | prof_child_enter(&wait_time); /* doesn't count for the caller itself */ |
| 9503 | # endif |
| 9504 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9505 | /* Don't use local function variables, if called from a function */ |
| 9506 | save_funccalp = save_funccal(); |
| 9507 | #endif |
| 9508 | |
| 9509 | /* |
| 9510 | * When starting to execute autocommands, save the search patterns. |
| 9511 | */ |
| 9512 | if (!autocmd_busy) |
| 9513 | { |
| 9514 | save_search_patterns(); |
Bram Moolenaar | 20ad69c | 2015-12-03 13:52:52 +0100 | [diff] [blame] | 9515 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | c51b02d | 2015-02-17 10:58:25 +0100 | [diff] [blame] | 9516 | if (!ins_compl_active()) |
Bram Moolenaar | 20ad69c | 2015-12-03 13:52:52 +0100 | [diff] [blame] | 9517 | #endif |
Bram Moolenaar | c51b02d | 2015-02-17 10:58:25 +0100 | [diff] [blame] | 9518 | { |
| 9519 | saveRedobuff(); |
| 9520 | did_save_redobuff = TRUE; |
| 9521 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9522 | did_filetype = keep_filetype; |
| 9523 | } |
| 9524 | |
| 9525 | /* |
| 9526 | * Note that we are applying autocmds. Some commands need to know. |
| 9527 | */ |
| 9528 | autocmd_busy = TRUE; |
| 9529 | filechangeshell_busy = (event == EVENT_FILECHANGEDSHELL); |
| 9530 | ++nesting; /* see matching decrement below */ |
| 9531 | |
| 9532 | /* Remember that FileType was triggered. Used for did_filetype(). */ |
| 9533 | if (event == EVENT_FILETYPE) |
| 9534 | did_filetype = TRUE; |
| 9535 | |
| 9536 | tail = gettail(fname); |
| 9537 | |
| 9538 | /* Find first autocommand that matches */ |
| 9539 | patcmd.curpat = first_autopat[(int)event]; |
| 9540 | patcmd.nextcmd = NULL; |
| 9541 | patcmd.group = group; |
| 9542 | patcmd.fname = fname; |
| 9543 | patcmd.sfname = sfname; |
| 9544 | patcmd.tail = tail; |
| 9545 | patcmd.event = event; |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9546 | patcmd.arg_bufnr = autocmd_bufnr; |
| 9547 | patcmd.next = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9548 | auto_next_pat(&patcmd, FALSE); |
| 9549 | |
| 9550 | /* found one, start executing the autocommands */ |
| 9551 | if (patcmd.curpat != NULL) |
| 9552 | { |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9553 | /* add to active_apc_list */ |
| 9554 | patcmd.next = active_apc_list; |
| 9555 | active_apc_list = &patcmd; |
| 9556 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9557 | #ifdef FEAT_EVAL |
| 9558 | /* set v:cmdarg (only when there is a matching pattern) */ |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 9559 | save_cmdbang = (long)get_vim_var_nr(VV_CMDBANG); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9560 | if (eap != NULL) |
| 9561 | { |
| 9562 | save_cmdarg = set_cmdarg(eap, NULL); |
| 9563 | set_vim_var_nr(VV_CMDBANG, (long)eap->forceit); |
| 9564 | } |
| 9565 | else |
| 9566 | save_cmdarg = NULL; /* avoid gcc warning */ |
| 9567 | #endif |
| 9568 | retval = TRUE; |
| 9569 | /* mark the last pattern, to avoid an endless loop when more patterns |
| 9570 | * are added when executing autocommands */ |
| 9571 | for (ap = patcmd.curpat; ap->next != NULL; ap = ap->next) |
| 9572 | ap->last = FALSE; |
| 9573 | ap->last = TRUE; |
| 9574 | check_lnums(TRUE); /* make sure cursor and topline are valid */ |
| 9575 | do_cmdline(NULL, getnextac, (void *)&patcmd, |
| 9576 | DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT); |
| 9577 | #ifdef FEAT_EVAL |
| 9578 | if (eap != NULL) |
| 9579 | { |
| 9580 | (void)set_cmdarg(NULL, save_cmdarg); |
| 9581 | set_vim_var_nr(VV_CMDBANG, save_cmdbang); |
| 9582 | } |
| 9583 | #endif |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9584 | /* delete from active_apc_list */ |
| 9585 | if (active_apc_list == &patcmd) /* just in case */ |
| 9586 | active_apc_list = patcmd.next; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9587 | } |
| 9588 | |
| 9589 | --RedrawingDisabled; |
| 9590 | autocmd_busy = save_autocmd_busy; |
| 9591 | filechangeshell_busy = FALSE; |
| 9592 | autocmd_nested = save_autocmd_nested; |
| 9593 | vim_free(sourcing_name); |
| 9594 | sourcing_name = save_sourcing_name; |
| 9595 | sourcing_lnum = save_sourcing_lnum; |
Bram Moolenaar | a0174af | 2008-01-02 20:08:25 +0000 | [diff] [blame] | 9596 | vim_free(autocmd_fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9597 | autocmd_fname = save_autocmd_fname; |
Bram Moolenaar | f6dad43 | 2008-09-18 19:29:58 +0000 | [diff] [blame] | 9598 | autocmd_fname_full = save_autocmd_fname_full; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9599 | autocmd_bufnr = save_autocmd_bufnr; |
| 9600 | autocmd_match = save_autocmd_match; |
| 9601 | #ifdef FEAT_EVAL |
| 9602 | current_SID = save_current_SID; |
| 9603 | restore_funccal(save_funccalp); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 9604 | # ifdef FEAT_PROFILE |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 9605 | if (do_profiling == PROF_YES) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 9606 | prof_child_exit(&wait_time); |
| 9607 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9608 | #endif |
| 9609 | vim_free(fname); |
| 9610 | vim_free(sfname); |
| 9611 | --nesting; /* see matching increment above */ |
| 9612 | |
| 9613 | /* |
| 9614 | * When stopping to execute autocommands, restore the search patterns and |
Bram Moolenaar | 3be8585 | 2014-06-12 14:01:31 +0200 | [diff] [blame] | 9615 | * the redo buffer. Free any buffers in the au_pending_free_buf list and |
| 9616 | * free any windows in the au_pending_free_win list. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9617 | */ |
| 9618 | if (!autocmd_busy) |
| 9619 | { |
| 9620 | restore_search_patterns(); |
Bram Moolenaar | c51b02d | 2015-02-17 10:58:25 +0100 | [diff] [blame] | 9621 | if (did_save_redobuff) |
| 9622 | restoreRedobuff(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9623 | did_filetype = FALSE; |
Bram Moolenaar | 4c7ab1b | 2014-04-06 20:45:43 +0200 | [diff] [blame] | 9624 | while (au_pending_free_buf != NULL) |
| 9625 | { |
| 9626 | buf_T *b = au_pending_free_buf->b_next; |
| 9627 | vim_free(au_pending_free_buf); |
| 9628 | au_pending_free_buf = b; |
| 9629 | } |
Bram Moolenaar | 3be8585 | 2014-06-12 14:01:31 +0200 | [diff] [blame] | 9630 | while (au_pending_free_win != NULL) |
| 9631 | { |
| 9632 | win_T *w = au_pending_free_win->w_next; |
| 9633 | vim_free(au_pending_free_win); |
| 9634 | au_pending_free_win = w; |
| 9635 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9636 | } |
| 9637 | |
| 9638 | /* |
| 9639 | * Some events don't set or reset the Changed flag. |
| 9640 | * Check if still in the same buffer! |
| 9641 | */ |
| 9642 | if (curbuf == old_curbuf |
| 9643 | && (event == EVENT_BUFREADPOST |
| 9644 | || event == EVENT_BUFWRITEPOST |
| 9645 | || event == EVENT_FILEAPPENDPOST |
| 9646 | || event == EVENT_VIMLEAVE |
| 9647 | || event == EVENT_VIMLEAVEPRE)) |
| 9648 | { |
| 9649 | #ifdef FEAT_TITLE |
| 9650 | if (curbuf->b_changed != save_changed) |
| 9651 | need_maketitle = TRUE; |
| 9652 | #endif |
| 9653 | curbuf->b_changed = save_changed; |
| 9654 | } |
| 9655 | |
| 9656 | au_cleanup(); /* may really delete removed patterns/commands now */ |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9657 | |
| 9658 | BYPASS_AU: |
| 9659 | /* When wiping out a buffer make sure all its buffer-local autocommands |
| 9660 | * are deleted. */ |
| 9661 | if (event == EVENT_BUFWIPEOUT && buf != NULL) |
| 9662 | aubuflocal_remove(buf); |
| 9663 | |
Bram Moolenaar | c369133 | 2016-04-20 12:49:49 +0200 | [diff] [blame] | 9664 | if (retval == OK && event == EVENT_FILETYPE) |
| 9665 | au_did_filetype = TRUE; |
| 9666 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9667 | return retval; |
| 9668 | } |
| 9669 | |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 9670 | # ifdef FEAT_EVAL |
| 9671 | static char_u *old_termresponse = NULL; |
| 9672 | # endif |
| 9673 | |
| 9674 | /* |
| 9675 | * Block triggering autocommands until unblock_autocmd() is called. |
| 9676 | * Can be used recursively, so long as it's symmetric. |
| 9677 | */ |
| 9678 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9679 | block_autocmds(void) |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 9680 | { |
| 9681 | # ifdef FEAT_EVAL |
| 9682 | /* Remember the value of v:termresponse. */ |
| 9683 | if (autocmd_blocked == 0) |
| 9684 | old_termresponse = get_vim_var_str(VV_TERMRESPONSE); |
| 9685 | # endif |
| 9686 | ++autocmd_blocked; |
| 9687 | } |
| 9688 | |
| 9689 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9690 | unblock_autocmds(void) |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 9691 | { |
| 9692 | --autocmd_blocked; |
| 9693 | |
| 9694 | # ifdef FEAT_EVAL |
| 9695 | /* When v:termresponse was set while autocommands were blocked, trigger |
| 9696 | * the autocommands now. Esp. useful when executing a shell command |
| 9697 | * during startup (vimdiff). */ |
| 9698 | if (autocmd_blocked == 0 |
| 9699 | && get_vim_var_str(VV_TERMRESPONSE) != old_termresponse) |
| 9700 | apply_autocmds(EVENT_TERMRESPONSE, NULL, NULL, FALSE, curbuf); |
| 9701 | # endif |
| 9702 | } |
| 9703 | |
Bram Moolenaar | abab85a | 2013-06-26 19:18:05 +0200 | [diff] [blame] | 9704 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9705 | is_autocmd_blocked(void) |
Bram Moolenaar | abab85a | 2013-06-26 19:18:05 +0200 | [diff] [blame] | 9706 | { |
| 9707 | return autocmd_blocked != 0; |
| 9708 | } |
| 9709 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9710 | /* |
| 9711 | * Find next autocommand pattern that matches. |
| 9712 | */ |
| 9713 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9714 | auto_next_pat( |
| 9715 | AutoPatCmd *apc, |
| 9716 | int stop_at_last) /* stop when 'last' flag is set */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9717 | { |
| 9718 | AutoPat *ap; |
| 9719 | AutoCmd *cp; |
| 9720 | char_u *name; |
| 9721 | char *s; |
| 9722 | |
| 9723 | vim_free(sourcing_name); |
| 9724 | sourcing_name = NULL; |
| 9725 | |
| 9726 | for (ap = apc->curpat; ap != NULL && !got_int; ap = ap->next) |
| 9727 | { |
| 9728 | apc->curpat = NULL; |
| 9729 | |
Bram Moolenaar | f6dad43 | 2008-09-18 19:29:58 +0000 | [diff] [blame] | 9730 | /* Only use a pattern when it has not been removed, has commands and |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9731 | * the group matches. For buffer-local autocommands only check the |
| 9732 | * buffer number. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9733 | if (ap->pat != NULL && ap->cmds != NULL |
| 9734 | && (apc->group == AUGROUP_ALL || apc->group == ap->group)) |
| 9735 | { |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9736 | /* execution-condition */ |
| 9737 | if (ap->buflocal_nr == 0 |
Bram Moolenaar | dffa5b8 | 2014-11-19 16:38:07 +0100 | [diff] [blame] | 9738 | ? (match_file_pat(NULL, &ap->reg_prog, apc->fname, |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 9739 | apc->sfname, apc->tail, ap->allow_dirs)) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9740 | : ap->buflocal_nr == apc->arg_bufnr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9741 | { |
| 9742 | name = event_nr2name(apc->event); |
| 9743 | s = _("%s Auto commands for \"%s\""); |
| 9744 | sourcing_name = alloc((unsigned)(STRLEN(s) |
| 9745 | + STRLEN(name) + ap->patlen + 1)); |
| 9746 | if (sourcing_name != NULL) |
| 9747 | { |
| 9748 | sprintf((char *)sourcing_name, s, |
| 9749 | (char *)name, (char *)ap->pat); |
| 9750 | if (p_verbose >= 8) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 9751 | { |
| 9752 | verbose_enter(); |
Bram Moolenaar | 051b782 | 2005-05-19 21:00:46 +0000 | [diff] [blame] | 9753 | smsg((char_u *)_("Executing %s"), sourcing_name); |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 9754 | verbose_leave(); |
| 9755 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9756 | } |
| 9757 | |
| 9758 | apc->curpat = ap; |
| 9759 | apc->nextcmd = ap->cmds; |
| 9760 | /* mark last command */ |
| 9761 | for (cp = ap->cmds; cp->next != NULL; cp = cp->next) |
| 9762 | cp->last = FALSE; |
| 9763 | cp->last = TRUE; |
| 9764 | } |
| 9765 | line_breakcheck(); |
| 9766 | if (apc->curpat != NULL) /* found a match */ |
| 9767 | break; |
| 9768 | } |
| 9769 | if (stop_at_last && ap->last) |
| 9770 | break; |
| 9771 | } |
| 9772 | } |
| 9773 | |
| 9774 | /* |
| 9775 | * Get next autocommand command. |
| 9776 | * Called by do_cmdline() to get the next line for ":if". |
| 9777 | * Returns allocated string, or NULL for end of autocommands. |
| 9778 | */ |
Bram Moolenaar | 21691f8 | 2012-12-05 19:13:18 +0100 | [diff] [blame] | 9779 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9780 | getnextac(int c UNUSED, void *cookie, int indent UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9781 | { |
| 9782 | AutoPatCmd *acp = (AutoPatCmd *)cookie; |
| 9783 | char_u *retval; |
| 9784 | AutoCmd *ac; |
| 9785 | |
| 9786 | /* Can be called again after returning the last line. */ |
| 9787 | if (acp->curpat == NULL) |
| 9788 | return NULL; |
| 9789 | |
| 9790 | /* repeat until we find an autocommand to execute */ |
| 9791 | for (;;) |
| 9792 | { |
| 9793 | /* skip removed commands */ |
| 9794 | while (acp->nextcmd != NULL && acp->nextcmd->cmd == NULL) |
| 9795 | if (acp->nextcmd->last) |
| 9796 | acp->nextcmd = NULL; |
| 9797 | else |
| 9798 | acp->nextcmd = acp->nextcmd->next; |
| 9799 | |
| 9800 | if (acp->nextcmd != NULL) |
| 9801 | break; |
| 9802 | |
| 9803 | /* at end of commands, find next pattern that matches */ |
| 9804 | if (acp->curpat->last) |
| 9805 | acp->curpat = NULL; |
| 9806 | else |
| 9807 | acp->curpat = acp->curpat->next; |
| 9808 | if (acp->curpat != NULL) |
| 9809 | auto_next_pat(acp, TRUE); |
| 9810 | if (acp->curpat == NULL) |
| 9811 | return NULL; |
| 9812 | } |
| 9813 | |
| 9814 | ac = acp->nextcmd; |
| 9815 | |
| 9816 | if (p_verbose >= 9) |
| 9817 | { |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 9818 | verbose_enter_scroll(); |
Bram Moolenaar | 051b782 | 2005-05-19 21:00:46 +0000 | [diff] [blame] | 9819 | smsg((char_u *)_("autocommand %s"), ac->cmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9820 | msg_puts((char_u *)"\n"); /* don't overwrite this either */ |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 9821 | verbose_leave_scroll(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9822 | } |
| 9823 | retval = vim_strsave(ac->cmd); |
| 9824 | autocmd_nested = ac->nested; |
| 9825 | #ifdef FEAT_EVAL |
| 9826 | current_SID = ac->scriptID; |
| 9827 | #endif |
| 9828 | if (ac->last) |
| 9829 | acp->nextcmd = NULL; |
| 9830 | else |
| 9831 | acp->nextcmd = ac->next; |
| 9832 | return retval; |
| 9833 | } |
| 9834 | |
| 9835 | /* |
| 9836 | * Return TRUE if there is a matching autocommand for "fname". |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9837 | * To account for buffer-local autocommands, function needs to know |
| 9838 | * in which buffer the file will be opened. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9839 | */ |
| 9840 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9841 | has_autocmd(event_T event, char_u *sfname, buf_T *buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9842 | { |
| 9843 | AutoPat *ap; |
| 9844 | char_u *fname; |
| 9845 | char_u *tail = gettail(sfname); |
| 9846 | int retval = FALSE; |
| 9847 | |
| 9848 | fname = FullName_save(sfname, FALSE); |
| 9849 | if (fname == NULL) |
| 9850 | return FALSE; |
| 9851 | |
| 9852 | #ifdef BACKSLASH_IN_FILENAME |
| 9853 | /* |
| 9854 | * Replace all backslashes with forward slashes. This makes the |
| 9855 | * autocommand patterns portable between Unix and MS-DOS. |
| 9856 | */ |
| 9857 | sfname = vim_strsave(sfname); |
| 9858 | if (sfname != NULL) |
| 9859 | forward_slash(sfname); |
| 9860 | forward_slash(fname); |
| 9861 | #endif |
| 9862 | |
| 9863 | for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) |
| 9864 | if (ap->pat != NULL && ap->cmds != NULL |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 9865 | && (ap->buflocal_nr == 0 |
Bram Moolenaar | dffa5b8 | 2014-11-19 16:38:07 +0100 | [diff] [blame] | 9866 | ? match_file_pat(NULL, &ap->reg_prog, |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 9867 | fname, sfname, tail, ap->allow_dirs) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 9868 | : buf != NULL && ap->buflocal_nr == buf->b_fnum |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9869 | )) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9870 | { |
| 9871 | retval = TRUE; |
| 9872 | break; |
| 9873 | } |
| 9874 | |
| 9875 | vim_free(fname); |
| 9876 | #ifdef BACKSLASH_IN_FILENAME |
| 9877 | vim_free(sfname); |
| 9878 | #endif |
| 9879 | |
| 9880 | return retval; |
| 9881 | } |
| 9882 | |
| 9883 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 9884 | /* |
| 9885 | * Function given to ExpandGeneric() to obtain the list of autocommand group |
| 9886 | * names. |
| 9887 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9888 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9889 | get_augroup_name(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9890 | { |
| 9891 | if (idx == augroups.ga_len) /* add "END" add the end */ |
| 9892 | return (char_u *)"END"; |
| 9893 | if (idx >= augroups.ga_len) /* end of list */ |
| 9894 | return NULL; |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 9895 | if (AUGROUP_NAME(idx) == NULL || AUGROUP_NAME(idx) == get_deleted_augroup()) |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 9896 | /* skip deleted entries */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9897 | return (char_u *)""; |
| 9898 | return AUGROUP_NAME(idx); /* return a name */ |
| 9899 | } |
| 9900 | |
| 9901 | static int include_groups = FALSE; |
| 9902 | |
| 9903 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9904 | set_context_in_autocmd( |
| 9905 | expand_T *xp, |
| 9906 | char_u *arg, |
| 9907 | int doautocmd) /* TRUE for :doauto*, FALSE for :autocmd */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9908 | { |
| 9909 | char_u *p; |
| 9910 | int group; |
| 9911 | |
| 9912 | /* check for a group name, skip it if present */ |
| 9913 | include_groups = FALSE; |
| 9914 | p = arg; |
| 9915 | group = au_get_grouparg(&arg); |
| 9916 | if (group == AUGROUP_ERROR) |
| 9917 | return NULL; |
| 9918 | /* If there only is a group name that's what we expand. */ |
| 9919 | if (*arg == NUL && group != AUGROUP_ALL && !vim_iswhite(arg[-1])) |
| 9920 | { |
| 9921 | arg = p; |
| 9922 | group = AUGROUP_ALL; |
| 9923 | } |
| 9924 | |
| 9925 | /* skip over event name */ |
| 9926 | for (p = arg; *p != NUL && !vim_iswhite(*p); ++p) |
| 9927 | if (*p == ',') |
| 9928 | arg = p + 1; |
| 9929 | if (*p == NUL) |
| 9930 | { |
| 9931 | if (group == AUGROUP_ALL) |
| 9932 | include_groups = TRUE; |
| 9933 | xp->xp_context = EXPAND_EVENTS; /* expand event name */ |
| 9934 | xp->xp_pattern = arg; |
| 9935 | return NULL; |
| 9936 | } |
| 9937 | |
| 9938 | /* skip over pattern */ |
| 9939 | arg = skipwhite(p); |
| 9940 | while (*arg && (!vim_iswhite(*arg) || arg[-1] == '\\')) |
| 9941 | arg++; |
| 9942 | if (*arg) |
| 9943 | return arg; /* expand (next) command */ |
| 9944 | |
| 9945 | if (doautocmd) |
| 9946 | xp->xp_context = EXPAND_FILES; /* expand file names */ |
| 9947 | else |
| 9948 | xp->xp_context = EXPAND_NOTHING; /* pattern is not expanded */ |
| 9949 | return NULL; |
| 9950 | } |
| 9951 | |
| 9952 | /* |
| 9953 | * Function given to ExpandGeneric() to obtain the list of event names. |
| 9954 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9955 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9956 | get_event_name(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9957 | { |
| 9958 | if (idx < augroups.ga_len) /* First list group names, if wanted */ |
| 9959 | { |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 9960 | if (!include_groups || AUGROUP_NAME(idx) == NULL |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 9961 | || AUGROUP_NAME(idx) == get_deleted_augroup()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9962 | return (char_u *)""; /* skip deleted entries */ |
| 9963 | return AUGROUP_NAME(idx); /* return a name */ |
| 9964 | } |
| 9965 | return (char_u *)event_names[idx - augroups.ga_len].name; |
| 9966 | } |
| 9967 | |
| 9968 | #endif /* FEAT_CMDL_COMPL */ |
| 9969 | |
| 9970 | /* |
Bram Moolenaar | f4cd3e8 | 2005-12-22 22:47:02 +0000 | [diff] [blame] | 9971 | * Return TRUE if autocmd is supported. |
| 9972 | */ |
| 9973 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9974 | autocmd_supported(char_u *name) |
Bram Moolenaar | f4cd3e8 | 2005-12-22 22:47:02 +0000 | [diff] [blame] | 9975 | { |
| 9976 | char_u *p; |
| 9977 | |
| 9978 | return (event_name2nr(name, &p) != NUM_EVENTS); |
| 9979 | } |
| 9980 | |
| 9981 | /* |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9982 | * Return TRUE if an autocommand is defined for a group, event and |
| 9983 | * pattern: The group can be omitted to accept any group. "event" and "pattern" |
| 9984 | * can be NULL to accept any event and pattern. "pattern" can be NULL to accept |
| 9985 | * any pattern. Buffer-local patterns <buffer> or <buffer=N> are accepted. |
| 9986 | * Used for: |
| 9987 | * exists("#Group") or |
| 9988 | * exists("#Group#Event") or |
| 9989 | * exists("#Group#Event#pat") or |
| 9990 | * exists("#Event") or |
| 9991 | * exists("#Event#pat") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9992 | */ |
| 9993 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 9994 | au_exists(char_u *arg) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9995 | { |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9996 | char_u *arg_save; |
| 9997 | char_u *pattern = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9998 | char_u *event_name; |
| 9999 | char_u *p; |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 10000 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10001 | AutoPat *ap; |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 10002 | buf_T *buflocal_buf = NULL; |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 10003 | int group; |
| 10004 | int retval = FALSE; |
| 10005 | |
Bram Moolenaar | f4cd3e8 | 2005-12-22 22:47:02 +0000 | [diff] [blame] | 10006 | /* Make a copy so that we can change the '#' chars to a NUL. */ |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 10007 | arg_save = vim_strsave(arg); |
| 10008 | if (arg_save == NULL) |
| 10009 | return FALSE; |
Bram Moolenaar | f4cd3e8 | 2005-12-22 22:47:02 +0000 | [diff] [blame] | 10010 | p = vim_strchr(arg_save, '#'); |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 10011 | if (p != NULL) |
| 10012 | *p++ = NUL; |
| 10013 | |
| 10014 | /* First, look for an autocmd group name */ |
| 10015 | group = au_find_group(arg_save); |
| 10016 | if (group == AUGROUP_ERROR) |
| 10017 | { |
| 10018 | /* Didn't match a group name, assume the first argument is an event. */ |
| 10019 | group = AUGROUP_ALL; |
| 10020 | event_name = arg_save; |
| 10021 | } |
| 10022 | else |
| 10023 | { |
| 10024 | if (p == NULL) |
| 10025 | { |
| 10026 | /* "Group": group name is present and it's recognized */ |
| 10027 | retval = TRUE; |
| 10028 | goto theend; |
| 10029 | } |
| 10030 | |
| 10031 | /* Must be "Group#Event" or "Group#Event#pat". */ |
| 10032 | event_name = p; |
| 10033 | p = vim_strchr(event_name, '#'); |
| 10034 | if (p != NULL) |
| 10035 | *p++ = NUL; /* "Group#Event#pat" */ |
| 10036 | } |
| 10037 | |
| 10038 | pattern = p; /* "pattern" is NULL when there is no pattern */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10039 | |
| 10040 | /* find the index (enum) for the event name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10041 | event = event_name2nr(event_name, &p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10042 | |
| 10043 | /* return FALSE if the event name is not recognized */ |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 10044 | if (event == NUM_EVENTS) |
| 10045 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10046 | |
| 10047 | /* Find the first autocommand for this event. |
| 10048 | * If there isn't any, return FALSE; |
| 10049 | * If there is one and no pattern given, return TRUE; */ |
| 10050 | ap = first_autopat[(int)event]; |
| 10051 | if (ap == NULL) |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 10052 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10053 | |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 10054 | /* if pattern is "<buffer>", special handling is needed which uses curbuf */ |
| 10055 | /* for pattern "<buffer=N>, fnamecmp() will work fine */ |
Bram Moolenaar | 5b7880d | 2009-09-11 15:24:31 +0000 | [diff] [blame] | 10056 | if (pattern != NULL && STRICMP(pattern, "<buffer>") == 0) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 10057 | buflocal_buf = curbuf; |
| 10058 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10059 | /* Check if there is an autocommand with the given pattern. */ |
| 10060 | for ( ; ap != NULL; ap = ap->next) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 10061 | /* only use a pattern when it has not been removed and has commands. */ |
| 10062 | /* For buffer-local autocommands, fnamecmp() works fine. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10063 | if (ap->pat != NULL && ap->cmds != NULL |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 10064 | && (group == AUGROUP_ALL || ap->group == group) |
Bram Moolenaar | 5b7880d | 2009-09-11 15:24:31 +0000 | [diff] [blame] | 10065 | && (pattern == NULL |
| 10066 | || (buflocal_buf == NULL |
| 10067 | ? fnamecmp(ap->pat, pattern) == 0 |
| 10068 | : ap->buflocal_nr == buflocal_buf->b_fnum))) |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 10069 | { |
| 10070 | retval = TRUE; |
| 10071 | break; |
| 10072 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10073 | |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 10074 | theend: |
| 10075 | vim_free(arg_save); |
| 10076 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10077 | } |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 10078 | |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 10079 | #else /* FEAT_AUTOCMD */ |
| 10080 | |
| 10081 | /* |
| 10082 | * Prepare for executing commands for (hidden) buffer "buf". |
| 10083 | * This is the non-autocommand version, it simply saves "curbuf" and sets |
| 10084 | * "curbuf" and "curwin" to match "buf". |
| 10085 | */ |
| 10086 | void |
Bram Moolenaar | d14e00e | 2016-01-31 17:30:51 +0100 | [diff] [blame] | 10087 | aucmd_prepbuf( |
| 10088 | aco_save_T *aco, /* structure to save values in */ |
| 10089 | buf_T *buf) /* new curbuf */ |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 10090 | { |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 10091 | aco->save_curbuf = curbuf; |
| 10092 | --curbuf->b_nwindows; |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 10093 | curbuf = buf; |
| 10094 | curwin->w_buffer = buf; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 10095 | ++curbuf->b_nwindows; |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 10096 | } |
| 10097 | |
| 10098 | /* |
| 10099 | * Restore after executing commands for a (hidden) buffer. |
| 10100 | * This is the non-autocommand version. |
| 10101 | */ |
| 10102 | void |
Bram Moolenaar | d14e00e | 2016-01-31 17:30:51 +0100 | [diff] [blame] | 10103 | aucmd_restbuf( |
| 10104 | aco_save_T *aco) /* structure holding saved values */ |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 10105 | { |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 10106 | --curbuf->b_nwindows; |
| 10107 | curbuf = aco->save_curbuf; |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 10108 | curwin->w_buffer = curbuf; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 10109 | ++curbuf->b_nwindows; |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 10110 | } |
| 10111 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10112 | #endif /* FEAT_AUTOCMD */ |
| 10113 | |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 10114 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10115 | #if defined(FEAT_AUTOCMD) || defined(FEAT_WILDIGN) || defined(PROTO) |
| 10116 | /* |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 10117 | * Try matching a filename with a "pattern" ("prog" is NULL), or use the |
| 10118 | * precompiled regprog "prog" ("pattern" is NULL). That avoids calling |
| 10119 | * vim_regcomp() often. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10120 | * Used for autocommands and 'wildignore'. |
| 10121 | * Returns TRUE if there is a match, FALSE otherwise. |
| 10122 | */ |
Bram Moolenaar | dffa5b8 | 2014-11-19 16:38:07 +0100 | [diff] [blame] | 10123 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 10124 | match_file_pat( |
| 10125 | char_u *pattern, /* pattern to match with */ |
| 10126 | regprog_T **prog, /* pre-compiled regprog or NULL */ |
| 10127 | char_u *fname, /* full path of file name */ |
| 10128 | char_u *sfname, /* short file name or NULL */ |
| 10129 | char_u *tail, /* tail of path */ |
| 10130 | int allow_dirs) /* allow matching with dir */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10131 | { |
| 10132 | regmatch_T regmatch; |
| 10133 | int result = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10134 | |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 10135 | regmatch.rm_ic = p_fic; /* ignore case if 'fileignorecase' is set */ |
Bram Moolenaar | 49a6ed8 | 2015-01-07 14:43:39 +0100 | [diff] [blame] | 10136 | if (prog != NULL) |
| 10137 | regmatch.regprog = *prog; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10138 | else |
Bram Moolenaar | 49a6ed8 | 2015-01-07 14:43:39 +0100 | [diff] [blame] | 10139 | regmatch.regprog = vim_regcomp(pattern, RE_MAGIC); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10140 | |
| 10141 | /* |
| 10142 | * Try for a match with the pattern with: |
| 10143 | * 1. the full file name, when the pattern has a '/'. |
| 10144 | * 2. the short file name, when the pattern has a '/'. |
| 10145 | * 3. the tail of the file name, when the pattern has no '/'. |
| 10146 | */ |
Bram Moolenaar | 49a6ed8 | 2015-01-07 14:43:39 +0100 | [diff] [blame] | 10147 | if (regmatch.regprog != NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10148 | && ((allow_dirs |
| 10149 | && (vim_regexec(®match, fname, (colnr_T)0) |
| 10150 | || (sfname != NULL |
| 10151 | && vim_regexec(®match, sfname, (colnr_T)0)))) |
Bram Moolenaar | 49a6ed8 | 2015-01-07 14:43:39 +0100 | [diff] [blame] | 10152 | || (!allow_dirs && vim_regexec(®match, tail, (colnr_T)0)))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10153 | result = TRUE; |
| 10154 | |
Bram Moolenaar | dffa5b8 | 2014-11-19 16:38:07 +0100 | [diff] [blame] | 10155 | if (prog != NULL) |
| 10156 | *prog = regmatch.regprog; |
| 10157 | else |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 10158 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10159 | return result; |
| 10160 | } |
| 10161 | #endif |
| 10162 | |
| 10163 | #if defined(FEAT_WILDIGN) || defined(PROTO) |
| 10164 | /* |
| 10165 | * Return TRUE if a file matches with a pattern in "list". |
| 10166 | * "list" is a comma-separated list of patterns, like 'wildignore'. |
| 10167 | * "sfname" is the short file name or NULL, "ffname" the long file name. |
| 10168 | */ |
| 10169 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 10170 | match_file_list(char_u *list, char_u *sfname, char_u *ffname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10171 | { |
| 10172 | char_u buf[100]; |
| 10173 | char_u *tail; |
| 10174 | char_u *regpat; |
| 10175 | char allow_dirs; |
| 10176 | int match; |
| 10177 | char_u *p; |
| 10178 | |
| 10179 | tail = gettail(sfname); |
| 10180 | |
| 10181 | /* try all patterns in 'wildignore' */ |
| 10182 | p = list; |
| 10183 | while (*p) |
| 10184 | { |
| 10185 | copy_option_part(&p, buf, 100, ","); |
| 10186 | regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE); |
| 10187 | if (regpat == NULL) |
| 10188 | break; |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 10189 | match = match_file_pat(regpat, NULL, ffname, sfname, |
| 10190 | tail, (int)allow_dirs); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10191 | vim_free(regpat); |
| 10192 | if (match) |
| 10193 | return TRUE; |
| 10194 | } |
| 10195 | return FALSE; |
| 10196 | } |
| 10197 | #endif |
| 10198 | |
| 10199 | /* |
| 10200 | * Convert the given pattern "pat" which has shell style wildcards in it, into |
| 10201 | * a regular expression, and return the result in allocated memory. If there |
| 10202 | * is a directory path separator to be matched, then TRUE is put in |
| 10203 | * allow_dirs, otherwise FALSE is put there -- webb. |
| 10204 | * Handle backslashes before special characters, like "\*" and "\ ". |
| 10205 | * |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10206 | * Returns NULL when out of memory. |
| 10207 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10208 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 10209 | file_pat_to_reg_pat( |
| 10210 | char_u *pat, |
| 10211 | char_u *pat_end, /* first char after pattern or NULL */ |
| 10212 | char *allow_dirs, /* Result passed back out in here */ |
| 10213 | int no_bslash UNUSED) /* Don't use a backward slash as pathsep */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10214 | { |
Bram Moolenaar | 49a6ed8 | 2015-01-07 14:43:39 +0100 | [diff] [blame] | 10215 | int size = 2; /* '^' at start, '$' at end */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10216 | char_u *endp; |
| 10217 | char_u *reg_pat; |
| 10218 | char_u *p; |
| 10219 | int i; |
| 10220 | int nested = 0; |
| 10221 | int add_dollar = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10222 | |
| 10223 | if (allow_dirs != NULL) |
| 10224 | *allow_dirs = FALSE; |
| 10225 | if (pat_end == NULL) |
| 10226 | pat_end = pat + STRLEN(pat); |
| 10227 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10228 | for (p = pat; p < pat_end; p++) |
| 10229 | { |
| 10230 | switch (*p) |
| 10231 | { |
| 10232 | case '*': |
| 10233 | case '.': |
| 10234 | case ',': |
| 10235 | case '{': |
| 10236 | case '}': |
| 10237 | case '~': |
| 10238 | size += 2; /* extra backslash */ |
| 10239 | break; |
| 10240 | #ifdef BACKSLASH_IN_FILENAME |
| 10241 | case '\\': |
| 10242 | case '/': |
| 10243 | size += 4; /* could become "[\/]" */ |
| 10244 | break; |
| 10245 | #endif |
| 10246 | default: |
| 10247 | size++; |
Bram Moolenaar | 2288afe | 2015-08-11 16:20:05 +0200 | [diff] [blame] | 10248 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10249 | if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10250 | { |
| 10251 | ++p; |
| 10252 | ++size; |
| 10253 | } |
| 10254 | # endif |
| 10255 | break; |
| 10256 | } |
| 10257 | } |
| 10258 | reg_pat = alloc(size + 1); |
| 10259 | if (reg_pat == NULL) |
| 10260 | return NULL; |
| 10261 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10262 | i = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10263 | |
| 10264 | if (pat[0] == '*') |
| 10265 | while (pat[0] == '*' && pat < pat_end - 1) |
| 10266 | pat++; |
| 10267 | else |
| 10268 | reg_pat[i++] = '^'; |
| 10269 | endp = pat_end - 1; |
Bram Moolenaar | 8fee878 | 2015-08-11 18:45:48 +0200 | [diff] [blame] | 10270 | if (endp >= pat && *endp == '*') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10271 | { |
| 10272 | while (endp - pat > 0 && *endp == '*') |
| 10273 | endp--; |
| 10274 | add_dollar = FALSE; |
| 10275 | } |
| 10276 | for (p = pat; *p && nested >= 0 && p <= endp; p++) |
| 10277 | { |
| 10278 | switch (*p) |
| 10279 | { |
| 10280 | case '*': |
| 10281 | reg_pat[i++] = '.'; |
| 10282 | reg_pat[i++] = '*'; |
Bram Moolenaar | 0274363 | 2005-07-25 20:42:36 +0000 | [diff] [blame] | 10283 | while (p[1] == '*') /* "**" matches like "*" */ |
| 10284 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10285 | break; |
| 10286 | case '.': |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10287 | case '~': |
| 10288 | reg_pat[i++] = '\\'; |
| 10289 | reg_pat[i++] = *p; |
| 10290 | break; |
| 10291 | case '?': |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10292 | reg_pat[i++] = '.'; |
| 10293 | break; |
| 10294 | case '\\': |
| 10295 | if (p[1] == NUL) |
| 10296 | break; |
| 10297 | #ifdef BACKSLASH_IN_FILENAME |
| 10298 | if (!no_bslash) |
| 10299 | { |
| 10300 | /* translate: |
| 10301 | * "\x" to "\\x" e.g., "dir\file" |
| 10302 | * "\*" to "\\.*" e.g., "dir\*.c" |
| 10303 | * "\?" to "\\." e.g., "dir\??.c" |
| 10304 | * "\+" to "\+" e.g., "fileX\+.c" |
| 10305 | */ |
| 10306 | if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?') |
| 10307 | && p[1] != '+') |
| 10308 | { |
| 10309 | reg_pat[i++] = '['; |
| 10310 | reg_pat[i++] = '\\'; |
| 10311 | reg_pat[i++] = '/'; |
| 10312 | reg_pat[i++] = ']'; |
| 10313 | if (allow_dirs != NULL) |
| 10314 | *allow_dirs = TRUE; |
| 10315 | break; |
| 10316 | } |
| 10317 | } |
| 10318 | #endif |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 10319 | /* Undo escaping from ExpandEscape(): |
| 10320 | * foo\?bar -> foo?bar |
| 10321 | * foo\%bar -> foo%bar |
| 10322 | * foo\,bar -> foo,bar |
| 10323 | * foo\ bar -> foo bar |
| 10324 | * Don't unescape \, * and others that are also special in a |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10325 | * regexp. |
| 10326 | * An escaped { must be unescaped since we use magic not |
Bram Moolenaar | a946afe | 2013-08-02 15:22:39 +0200 | [diff] [blame] | 10327 | * verymagic. Use "\\\{n,m\}"" to get "\{n,m}". |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10328 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10329 | if (*++p == '?' |
| 10330 | #ifdef BACKSLASH_IN_FILENAME |
| 10331 | && no_bslash |
| 10332 | #endif |
| 10333 | ) |
| 10334 | reg_pat[i++] = '?'; |
| 10335 | else |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10336 | if (*p == ',' || *p == '%' || *p == '#' |
Bram Moolenaar | 2288afe | 2015-08-11 16:20:05 +0200 | [diff] [blame] | 10337 | || vim_isspace(*p) || *p == '{' || *p == '}') |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 10338 | reg_pat[i++] = *p; |
Bram Moolenaar | a946afe | 2013-08-02 15:22:39 +0200 | [diff] [blame] | 10339 | else if (*p == '\\' && p[1] == '\\' && p[2] == '{') |
| 10340 | { |
| 10341 | reg_pat[i++] = '\\'; |
| 10342 | reg_pat[i++] = '{'; |
| 10343 | p += 2; |
| 10344 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10345 | else |
| 10346 | { |
| 10347 | if (allow_dirs != NULL && vim_ispathsep(*p) |
| 10348 | #ifdef BACKSLASH_IN_FILENAME |
| 10349 | && (!no_bslash || *p != '\\') |
| 10350 | #endif |
| 10351 | ) |
| 10352 | *allow_dirs = TRUE; |
| 10353 | reg_pat[i++] = '\\'; |
| 10354 | reg_pat[i++] = *p; |
| 10355 | } |
| 10356 | break; |
| 10357 | #ifdef BACKSLASH_IN_FILENAME |
| 10358 | case '/': |
| 10359 | reg_pat[i++] = '['; |
| 10360 | reg_pat[i++] = '\\'; |
| 10361 | reg_pat[i++] = '/'; |
| 10362 | reg_pat[i++] = ']'; |
| 10363 | if (allow_dirs != NULL) |
| 10364 | *allow_dirs = TRUE; |
| 10365 | break; |
| 10366 | #endif |
| 10367 | case '{': |
| 10368 | reg_pat[i++] = '\\'; |
| 10369 | reg_pat[i++] = '('; |
| 10370 | nested++; |
| 10371 | break; |
| 10372 | case '}': |
| 10373 | reg_pat[i++] = '\\'; |
| 10374 | reg_pat[i++] = ')'; |
| 10375 | --nested; |
| 10376 | break; |
| 10377 | case ',': |
| 10378 | if (nested) |
| 10379 | { |
| 10380 | reg_pat[i++] = '\\'; |
| 10381 | reg_pat[i++] = '|'; |
| 10382 | } |
| 10383 | else |
| 10384 | reg_pat[i++] = ','; |
| 10385 | break; |
| 10386 | default: |
| 10387 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10388 | if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10389 | reg_pat[i++] = *p++; |
| 10390 | else |
| 10391 | # endif |
| 10392 | if (allow_dirs != NULL && vim_ispathsep(*p)) |
| 10393 | *allow_dirs = TRUE; |
| 10394 | reg_pat[i++] = *p; |
| 10395 | break; |
| 10396 | } |
| 10397 | } |
| 10398 | if (add_dollar) |
| 10399 | reg_pat[i++] = '$'; |
| 10400 | reg_pat[i] = NUL; |
| 10401 | if (nested != 0) |
| 10402 | { |
| 10403 | if (nested < 0) |
| 10404 | EMSG(_("E219: Missing {.")); |
| 10405 | else |
| 10406 | EMSG(_("E220: Missing }.")); |
| 10407 | vim_free(reg_pat); |
| 10408 | reg_pat = NULL; |
| 10409 | } |
| 10410 | return reg_pat; |
| 10411 | } |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 10412 | |
| 10413 | #if defined(EINTR) || defined(PROTO) |
| 10414 | /* |
| 10415 | * Version of read() that retries when interrupted by EINTR (possibly |
| 10416 | * by a SIGWINCH). |
| 10417 | */ |
| 10418 | long |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 10419 | read_eintr(int fd, void *buf, size_t bufsize) |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 10420 | { |
| 10421 | long ret; |
| 10422 | |
| 10423 | for (;;) |
| 10424 | { |
| 10425 | ret = vim_read(fd, buf, bufsize); |
| 10426 | if (ret >= 0 || errno != EINTR) |
| 10427 | break; |
| 10428 | } |
| 10429 | return ret; |
| 10430 | } |
| 10431 | |
| 10432 | /* |
| 10433 | * Version of write() that retries when interrupted by EINTR (possibly |
| 10434 | * by a SIGWINCH). |
| 10435 | */ |
| 10436 | long |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 10437 | write_eintr(int fd, void *buf, size_t bufsize) |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 10438 | { |
| 10439 | long ret = 0; |
| 10440 | long wlen; |
| 10441 | |
| 10442 | /* Repeat the write() so long it didn't fail, other than being interrupted |
| 10443 | * by a signal. */ |
| 10444 | while (ret < (long)bufsize) |
| 10445 | { |
Bram Moolenaar | 9c26303 | 2010-12-17 18:06:06 +0100 | [diff] [blame] | 10446 | wlen = vim_write(fd, (char *)buf + ret, bufsize - ret); |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 10447 | if (wlen < 0) |
| 10448 | { |
| 10449 | if (errno != EINTR) |
| 10450 | break; |
| 10451 | } |
| 10452 | else |
| 10453 | ret += wlen; |
| 10454 | } |
| 10455 | return ret; |
| 10456 | } |
| 10457 | #endif |