Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * fileio.c: read from and write to a file |
| 12 | */ |
| 13 | |
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 | |
| 27 | #ifdef FEAT_CRYPT |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 28 | /* crypt_magic[0] is pkzip crypt, crypt_magic[1] is sha2+blowfish */ |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 29 | static char *crypt_magic[] = {"VimCrypt~01!", "VimCrypt~02!"}; |
| 30 | static char crypt_magic_head[] = "VimCrypt~"; |
| 31 | # define CRYPT_MAGIC_LEN 12 /* must be multiple of 4! */ |
Bram Moolenaar | 80794b1 | 2010-06-13 05:20:42 +0200 | [diff] [blame] | 32 | |
| 33 | /* For blowfish, after the magic header, we store 8 bytes of salt and then 8 |
| 34 | * bytes of seed (initialisation vector). */ |
| 35 | static int crypt_salt_len[] = {0, 8}; |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 36 | static int crypt_seed_len[] = {0, 8}; |
Bram Moolenaar | 80794b1 | 2010-06-13 05:20:42 +0200 | [diff] [blame] | 37 | #define CRYPT_SALT_LEN_MAX 8 |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 38 | #define CRYPT_SEED_LEN_MAX 8 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
| 41 | /* Is there any system that doesn't have access()? */ |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 42 | #define USE_MCH_ACCESS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 43 | |
Bram Moolenaar | fe1c56d | 2007-07-10 15:10:54 +0000 | [diff] [blame] | 44 | #if defined(sun) && defined(S_ISCHR) |
| 45 | # define OPEN_CHR_FILES |
| 46 | static int is_dev_fd_file(char_u *fname); |
| 47 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 48 | #ifdef FEAT_MBYTE |
| 49 | static char_u *next_fenc __ARGS((char_u **pp)); |
| 50 | # ifdef FEAT_EVAL |
| 51 | static char_u *readfile_charconvert __ARGS((char_u *fname, char_u *fenc, int *fdp)); |
| 52 | # endif |
| 53 | #endif |
| 54 | #ifdef FEAT_VIMINFO |
| 55 | static void check_marks_read __ARGS((void)); |
| 56 | #endif |
| 57 | #ifdef FEAT_CRYPT |
Bram Moolenaar | 49771f4 | 2010-07-20 17:32:38 +0200 | [diff] [blame] | 58 | static int crypt_method_from_magic __ARGS((char *ptr, int len)); |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 59 | static char_u *check_for_cryptkey __ARGS((char_u *cryptkey, char_u *ptr, long *sizep, off_t *filesizep, int newfile, char_u *fname, int *did_ask)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 60 | #endif |
| 61 | #ifdef UNIX |
| 62 | static void set_file_time __ARGS((char_u *fname, time_t atime, time_t mtime)); |
| 63 | #endif |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 64 | static int set_rw_fname __ARGS((char_u *fname, char_u *sfname)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 65 | static int msg_add_fileformat __ARGS((int eol_type)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 66 | static void msg_add_eol __ARGS((void)); |
| 67 | static int check_mtime __ARGS((buf_T *buf, struct stat *s)); |
| 68 | static int time_differs __ARGS((long t1, long t2)); |
| 69 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 70 | static int apply_autocmds_exarg __ARGS((event_T event, char_u *fname, char_u *fname_io, int force, buf_T *buf, exarg_T *eap)); |
Bram Moolenaar | 70836c8 | 2006-02-20 21:28:49 +0000 | [diff] [blame] | 71 | static int au_find_group __ARGS((char_u *name)); |
| 72 | |
| 73 | # define AUGROUP_DEFAULT -1 /* default autocmd group */ |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 74 | # define AUGROUP_ERROR -2 /* erroneous autocmd group */ |
Bram Moolenaar | 70836c8 | 2006-02-20 21:28:49 +0000 | [diff] [blame] | 75 | # define AUGROUP_ALL -3 /* all autocmd groups */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 76 | #endif |
| 77 | |
| 78 | #if defined(FEAT_CRYPT) || defined(FEAT_MBYTE) |
| 79 | # define HAS_BW_FLAGS |
| 80 | # define FIO_LATIN1 0x01 /* convert Latin1 */ |
| 81 | # define FIO_UTF8 0x02 /* convert UTF-8 */ |
| 82 | # define FIO_UCS2 0x04 /* convert UCS-2 */ |
| 83 | # define FIO_UCS4 0x08 /* convert UCS-4 */ |
| 84 | # define FIO_UTF16 0x10 /* convert UTF-16 */ |
| 85 | # ifdef WIN3264 |
| 86 | # define FIO_CODEPAGE 0x20 /* convert MS-Windows codepage */ |
| 87 | # define FIO_PUT_CP(x) (((x) & 0xffff) << 16) /* put codepage in top word */ |
| 88 | # define FIO_GET_CP(x) (((x)>>16) & 0xffff) /* get codepage from top word */ |
| 89 | # endif |
| 90 | # ifdef MACOS_X |
| 91 | # define FIO_MACROMAN 0x20 /* convert MacRoman */ |
| 92 | # endif |
| 93 | # define FIO_ENDIAN_L 0x80 /* little endian */ |
| 94 | # define FIO_ENCRYPTED 0x1000 /* encrypt written bytes */ |
| 95 | # define FIO_NOCONVERT 0x2000 /* skip encoding conversion */ |
| 96 | # define FIO_UCSBOM 0x4000 /* check for BOM at start of file */ |
| 97 | # define FIO_ALL -1 /* allow all formats */ |
| 98 | #endif |
| 99 | |
| 100 | /* When converting, a read() or write() may leave some bytes to be converted |
| 101 | * for the next call. The value is guessed... */ |
| 102 | #define CONV_RESTLEN 30 |
| 103 | |
| 104 | /* We have to guess how much a sequence of bytes may expand when converting |
| 105 | * with iconv() to be able to allocate a buffer. */ |
| 106 | #define ICONV_MULT 8 |
| 107 | |
| 108 | /* |
| 109 | * Structure to pass arguments from buf_write() to buf_write_bytes(). |
| 110 | */ |
| 111 | struct bw_info |
| 112 | { |
| 113 | int bw_fd; /* file descriptor */ |
| 114 | char_u *bw_buf; /* buffer with data to be written */ |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 115 | int bw_len; /* length of data */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 116 | #ifdef HAS_BW_FLAGS |
| 117 | int bw_flags; /* FIO_ flags */ |
| 118 | #endif |
| 119 | #ifdef FEAT_MBYTE |
| 120 | char_u bw_rest[CONV_RESTLEN]; /* not converted bytes */ |
| 121 | int bw_restlen; /* nr of bytes in bw_rest[] */ |
| 122 | int bw_first; /* first write call */ |
| 123 | char_u *bw_conv_buf; /* buffer for writing converted chars */ |
| 124 | int bw_conv_buflen; /* size of bw_conv_buf */ |
| 125 | int bw_conv_error; /* set for conversion error */ |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 126 | linenr_T bw_conv_error_lnum; /* first line with error or zero */ |
| 127 | linenr_T bw_start_lnum; /* line number at start of buffer */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 128 | # ifdef USE_ICONV |
| 129 | iconv_t bw_iconv_fd; /* descriptor for iconv() or -1 */ |
| 130 | # endif |
| 131 | #endif |
| 132 | }; |
| 133 | |
| 134 | static int buf_write_bytes __ARGS((struct bw_info *ip)); |
| 135 | |
| 136 | #ifdef FEAT_MBYTE |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 137 | static linenr_T readfile_linenr __ARGS((linenr_T linecnt, char_u *p, char_u *endp)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 138 | static int ucs2bytes __ARGS((unsigned c, char_u **pp, int flags)); |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 139 | static int need_conversion __ARGS((char_u *fenc)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 140 | static int get_fio_flags __ARGS((char_u *ptr)); |
| 141 | static char_u *check_for_bom __ARGS((char_u *p, long size, int *lenp, int flags)); |
| 142 | static int make_bom __ARGS((char_u *buf, char_u *name)); |
| 143 | # ifdef WIN3264 |
| 144 | static int get_win_fio_flags __ARGS((char_u *ptr)); |
| 145 | # endif |
| 146 | # ifdef MACOS_X |
| 147 | static int get_mac_fio_flags __ARGS((char_u *ptr)); |
| 148 | # endif |
| 149 | #endif |
| 150 | static int move_lines __ARGS((buf_T *frombuf, buf_T *tobuf)); |
Bram Moolenaar | 4592dee | 2009-11-18 19:11:58 +0000 | [diff] [blame] | 151 | #ifdef TEMPDIRNAMES |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 152 | static void vim_settempdir __ARGS((char_u *tempdir)); |
Bram Moolenaar | 4592dee | 2009-11-18 19:11:58 +0000 | [diff] [blame] | 153 | #endif |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 154 | #ifdef FEAT_AUTOCMD |
| 155 | static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name"); |
| 156 | #endif |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 157 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 158 | void |
| 159 | filemess(buf, name, s, attr) |
| 160 | buf_T *buf; |
| 161 | char_u *name; |
| 162 | char_u *s; |
| 163 | int attr; |
| 164 | { |
| 165 | int msg_scroll_save; |
| 166 | |
| 167 | if (msg_silent != 0) |
| 168 | return; |
| 169 | msg_add_fname(buf, name); /* put file name in IObuff with quotes */ |
| 170 | /* If it's extremely long, truncate it. */ |
| 171 | if (STRLEN(IObuff) > IOSIZE - 80) |
| 172 | IObuff[IOSIZE - 80] = NUL; |
| 173 | STRCAT(IObuff, s); |
| 174 | /* |
| 175 | * For the first message may have to start a new line. |
| 176 | * For further ones overwrite the previous one, reset msg_scroll before |
| 177 | * calling filemess(). |
| 178 | */ |
| 179 | msg_scroll_save = msg_scroll; |
| 180 | if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) |
| 181 | msg_scroll = FALSE; |
| 182 | if (!msg_scroll) /* wait a bit when overwriting an error msg */ |
| 183 | check_for_delay(FALSE); |
| 184 | msg_start(); |
| 185 | msg_scroll = msg_scroll_save; |
| 186 | msg_scrolled_ign = TRUE; |
| 187 | /* may truncate the message to avoid a hit-return prompt */ |
| 188 | msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr); |
| 189 | msg_clr_eos(); |
| 190 | out_flush(); |
| 191 | msg_scrolled_ign = FALSE; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Read lines from file "fname" into the buffer after line "from". |
| 196 | * |
| 197 | * 1. We allocate blocks with lalloc, as big as possible. |
| 198 | * 2. Each block is filled with characters from the file with a single read(). |
| 199 | * 3. The lines are inserted in the buffer with ml_append(). |
| 200 | * |
| 201 | * (caller must check that fname != NULL, unless READ_STDIN is used) |
| 202 | * |
| 203 | * "lines_to_skip" is the number of lines that must be skipped |
| 204 | * "lines_to_read" is the number of lines that are appended |
| 205 | * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM. |
| 206 | * |
| 207 | * flags: |
| 208 | * READ_NEW starting to edit a new buffer |
| 209 | * READ_FILTER reading filter output |
| 210 | * READ_STDIN read from stdin instead of a file |
| 211 | * READ_BUFFER read from curbuf instead of a file (converting after reading |
| 212 | * stdin) |
| 213 | * READ_DUMMY read into a dummy buffer (to check if file contents changed) |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 214 | * READ_KEEP_UNDO don't clear undo info or read it from a file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 215 | * |
| 216 | * return FAIL for failure, OK otherwise |
| 217 | */ |
| 218 | int |
| 219 | readfile(fname, sfname, from, lines_to_skip, lines_to_read, eap, flags) |
| 220 | char_u *fname; |
| 221 | char_u *sfname; |
| 222 | linenr_T from; |
| 223 | linenr_T lines_to_skip; |
| 224 | linenr_T lines_to_read; |
| 225 | exarg_T *eap; /* can be NULL! */ |
| 226 | int flags; |
| 227 | { |
| 228 | int fd = 0; |
| 229 | int newfile = (flags & READ_NEW); |
| 230 | int check_readonly; |
| 231 | int filtering = (flags & READ_FILTER); |
| 232 | int read_stdin = (flags & READ_STDIN); |
| 233 | int read_buffer = (flags & READ_BUFFER); |
Bram Moolenaar | 690ffc0 | 2008-01-04 15:31:21 +0000 | [diff] [blame] | 234 | int set_options = newfile || read_buffer |
| 235 | || (eap != NULL && eap->read_edit); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 236 | linenr_T read_buf_lnum = 1; /* next line to read from curbuf */ |
| 237 | colnr_T read_buf_col = 0; /* next char to read from this line */ |
| 238 | char_u c; |
| 239 | linenr_T lnum = from; |
| 240 | char_u *ptr = NULL; /* pointer into read buffer */ |
| 241 | char_u *buffer = NULL; /* read buffer */ |
| 242 | char_u *new_buffer = NULL; /* init to shut up gcc */ |
| 243 | char_u *line_start = NULL; /* init to shut up gcc */ |
| 244 | int wasempty; /* buffer was empty before reading */ |
| 245 | colnr_T len; |
| 246 | long size = 0; |
| 247 | char_u *p; |
Bram Moolenaar | 914703b | 2010-05-31 21:59:46 +0200 | [diff] [blame] | 248 | off_t filesize = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 249 | int skip_read = FALSE; |
| 250 | #ifdef FEAT_CRYPT |
| 251 | char_u *cryptkey = NULL; |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 252 | int did_ask_for_key = FALSE; |
Bram Moolenaar | 4cf35c2 | 2011-02-25 16:52:17 +0100 | [diff] [blame] | 253 | int crypt_method_used; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 254 | #endif |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 255 | #ifdef FEAT_PERSISTENT_UNDO |
| 256 | context_sha256_T sha_ctx; |
| 257 | int read_undo_file = FALSE; |
| 258 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 259 | int split = 0; /* number of split lines */ |
| 260 | #define UNKNOWN 0x0fffffff /* file size is unknown */ |
| 261 | linenr_T linecnt; |
| 262 | int error = FALSE; /* errors encountered */ |
| 263 | int ff_error = EOL_UNKNOWN; /* file format with errors */ |
| 264 | long linerest = 0; /* remaining chars in line */ |
| 265 | #ifdef UNIX |
| 266 | int perm = 0; |
| 267 | int swap_mode = -1; /* protection bits for swap file */ |
| 268 | #else |
| 269 | int perm; |
| 270 | #endif |
| 271 | int fileformat = 0; /* end-of-line format */ |
| 272 | int keep_fileformat = FALSE; |
| 273 | struct stat st; |
| 274 | int file_readonly; |
| 275 | linenr_T skip_count = 0; |
| 276 | linenr_T read_count = 0; |
| 277 | int msg_save = msg_scroll; |
| 278 | linenr_T read_no_eol_lnum = 0; /* non-zero lnum when last line of |
| 279 | * last read was missing the eol */ |
| 280 | int try_mac = (vim_strchr(p_ffs, 'm') != NULL); |
| 281 | int try_dos = (vim_strchr(p_ffs, 'd') != NULL); |
| 282 | int try_unix = (vim_strchr(p_ffs, 'x') != NULL); |
| 283 | int file_rewind = FALSE; |
| 284 | #ifdef FEAT_MBYTE |
| 285 | int can_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 286 | linenr_T conv_error = 0; /* line nr with conversion error */ |
| 287 | linenr_T illegal_byte = 0; /* line nr with illegal byte */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 288 | int keep_dest_enc = FALSE; /* don't retry when char doesn't fit |
| 289 | in destination encoding */ |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 290 | int bad_char_behavior = BAD_REPLACE; |
| 291 | /* BAD_KEEP, BAD_DROP or character to |
| 292 | * replace with */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 293 | char_u *tmpname = NULL; /* name of 'charconvert' output file */ |
| 294 | int fio_flags = 0; |
| 295 | char_u *fenc; /* fileencoding to use */ |
| 296 | int fenc_alloced; /* fenc_next is in allocated memory */ |
| 297 | char_u *fenc_next = NULL; /* next item in 'fencs' or NULL */ |
| 298 | int advance_fenc = FALSE; |
| 299 | long real_size = 0; |
| 300 | # ifdef USE_ICONV |
| 301 | iconv_t iconv_fd = (iconv_t)-1; /* descriptor for iconv() or -1 */ |
| 302 | # ifdef FEAT_EVAL |
| 303 | int did_iconv = FALSE; /* TRUE when iconv() failed and trying |
| 304 | 'charconvert' next */ |
| 305 | # endif |
| 306 | # endif |
| 307 | int converted = FALSE; /* TRUE if conversion done */ |
| 308 | int notconverted = FALSE; /* TRUE if conversion wanted but it |
| 309 | wasn't possible */ |
| 310 | char_u conv_rest[CONV_RESTLEN]; |
| 311 | int conv_restlen = 0; /* nr of bytes in conv_rest[] */ |
| 312 | #endif |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 313 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | bb3d5dc | 2010-08-14 14:32:54 +0200 | [diff] [blame] | 314 | buf_T *old_curbuf; |
| 315 | char_u *old_b_ffname; |
| 316 | char_u *old_b_fname; |
| 317 | int using_b_ffname; |
| 318 | int using_b_fname; |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 319 | #endif |
Bram Moolenaar | bb3d5dc | 2010-08-14 14:32:54 +0200 | [diff] [blame] | 320 | |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 321 | 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] | 322 | |
| 323 | /* |
| 324 | * If there is no file name yet, use the one for the read file. |
| 325 | * BF_NOTEDITED is set to reflect this. |
| 326 | * Don't do this for a read from a filter. |
| 327 | * Only do this when 'cpoptions' contains the 'f' flag. |
| 328 | */ |
| 329 | if (curbuf->b_ffname == NULL |
| 330 | && !filtering |
| 331 | && fname != NULL |
| 332 | && vim_strchr(p_cpo, CPO_FNAMER) != NULL |
| 333 | && !(flags & READ_DUMMY)) |
| 334 | { |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 335 | if (set_rw_fname(fname, sfname) == FAIL) |
| 336 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Bram Moolenaar | bb3d5dc | 2010-08-14 14:32:54 +0200 | [diff] [blame] | 339 | #ifdef FEAT_AUTOCMD |
| 340 | /* Remember the initial values of curbuf, curbuf->b_ffname and |
| 341 | * curbuf->b_fname to detect whether they are altered as a result of |
| 342 | * executing nasty autocommands. Also check if "fname" and "sfname" |
| 343 | * point to one of these values. */ |
| 344 | old_curbuf = curbuf; |
| 345 | old_b_ffname = curbuf->b_ffname; |
| 346 | old_b_fname = curbuf->b_fname; |
| 347 | using_b_ffname = (fname == curbuf->b_ffname) |
| 348 | || (sfname == curbuf->b_ffname); |
| 349 | using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname); |
| 350 | #endif |
| 351 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 352 | /* After reading a file the cursor line changes but we don't want to |
| 353 | * display the line. */ |
| 354 | ex_no_reprint = TRUE; |
| 355 | |
Bram Moolenaar | 55b7cf8 | 2006-09-09 12:52:42 +0000 | [diff] [blame] | 356 | /* don't display the file info for another buffer now */ |
| 357 | need_fileinfo = FALSE; |
| 358 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 359 | /* |
| 360 | * For Unix: Use the short file name whenever possible. |
| 361 | * Avoids problems with networks and when directory names are changed. |
| 362 | * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to |
| 363 | * another directory, which we don't detect. |
| 364 | */ |
| 365 | if (sfname == NULL) |
| 366 | sfname = fname; |
| 367 | #if defined(UNIX) || defined(__EMX__) |
| 368 | fname = sfname; |
| 369 | #endif |
| 370 | |
| 371 | #ifdef FEAT_AUTOCMD |
| 372 | /* |
| 373 | * The BufReadCmd and FileReadCmd events intercept the reading process by |
| 374 | * executing the associated commands instead. |
| 375 | */ |
| 376 | if (!filtering && !read_stdin && !read_buffer) |
| 377 | { |
| 378 | pos_T pos; |
| 379 | |
| 380 | pos = curbuf->b_op_start; |
| 381 | |
| 382 | /* Set '[ mark to the line above where the lines go (line 1 if zero). */ |
| 383 | curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); |
| 384 | curbuf->b_op_start.col = 0; |
| 385 | |
| 386 | if (newfile) |
| 387 | { |
| 388 | if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname, |
| 389 | FALSE, curbuf, eap)) |
| 390 | #ifdef FEAT_EVAL |
| 391 | return aborting() ? FAIL : OK; |
| 392 | #else |
| 393 | return OK; |
| 394 | #endif |
| 395 | } |
| 396 | else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname, |
| 397 | FALSE, NULL, eap)) |
| 398 | #ifdef FEAT_EVAL |
| 399 | return aborting() ? FAIL : OK; |
| 400 | #else |
| 401 | return OK; |
| 402 | #endif |
| 403 | |
| 404 | curbuf->b_op_start = pos; |
| 405 | } |
| 406 | #endif |
| 407 | |
| 408 | if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0) |
| 409 | msg_scroll = FALSE; /* overwrite previous file message */ |
| 410 | else |
| 411 | msg_scroll = TRUE; /* don't overwrite previous file message */ |
| 412 | |
| 413 | /* |
| 414 | * If the name ends in a path separator, we can't open it. Check here, |
| 415 | * because reading the file may actually work, but then creating the swap |
| 416 | * file may destroy it! Reported on MS-DOS and Win 95. |
| 417 | * If the name is too long we might crash further on, quit here. |
| 418 | */ |
Bram Moolenaar | 15d0a8c | 2004-09-06 17:44:46 +0000 | [diff] [blame] | 419 | if (fname != NULL && *fname != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 420 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 421 | p = fname + STRLEN(fname); |
| 422 | if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL) |
Bram Moolenaar | 15d0a8c | 2004-09-06 17:44:46 +0000 | [diff] [blame] | 423 | { |
| 424 | filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0); |
| 425 | msg_end(); |
| 426 | msg_scroll = msg_save; |
| 427 | return FAIL; |
| 428 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | #ifdef UNIX |
| 432 | /* |
| 433 | * On Unix it is possible to read a directory, so we have to |
| 434 | * check for it before the mch_open(). |
| 435 | */ |
| 436 | if (!read_stdin && !read_buffer) |
| 437 | { |
| 438 | perm = mch_getperm(fname); |
| 439 | if (perm >= 0 && !S_ISREG(perm) /* not a regular file ... */ |
| 440 | # ifdef S_ISFIFO |
| 441 | && !S_ISFIFO(perm) /* ... or fifo */ |
| 442 | # endif |
| 443 | # ifdef S_ISSOCK |
| 444 | && !S_ISSOCK(perm) /* ... or socket */ |
| 445 | # endif |
Bram Moolenaar | fe1c56d | 2007-07-10 15:10:54 +0000 | [diff] [blame] | 446 | # ifdef OPEN_CHR_FILES |
| 447 | && !(S_ISCHR(perm) && is_dev_fd_file(fname)) |
| 448 | /* ... or a character special file named /dev/fd/<n> */ |
| 449 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 450 | ) |
| 451 | { |
| 452 | if (S_ISDIR(perm)) |
| 453 | filemess(curbuf, fname, (char_u *)_("is a directory"), 0); |
| 454 | else |
| 455 | filemess(curbuf, fname, (char_u *)_("is not a file"), 0); |
| 456 | msg_end(); |
| 457 | msg_scroll = msg_save; |
| 458 | return FAIL; |
| 459 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 460 | |
Bram Moolenaar | c67764a | 2006-10-12 19:14:26 +0000 | [diff] [blame] | 461 | # if defined(MSDOS) || defined(MSWIN) || defined(OS2) |
| 462 | /* |
| 463 | * MS-Windows allows opening a device, but we will probably get stuck |
| 464 | * trying to read it. |
| 465 | */ |
| 466 | if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE) |
| 467 | { |
Bram Moolenaar | 5386a12 | 2007-06-28 20:02:32 +0000 | [diff] [blame] | 468 | 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] | 469 | msg_end(); |
| 470 | msg_scroll = msg_save; |
| 471 | return FAIL; |
| 472 | } |
| 473 | # endif |
Bram Moolenaar | 043545e | 2006-10-10 16:44:07 +0000 | [diff] [blame] | 474 | } |
| 475 | #endif |
| 476 | |
Bram Moolenaar | ad875fb | 2013-07-24 15:02:03 +0200 | [diff] [blame] | 477 | /* Set default or forced 'fileformat' and 'binary'. */ |
| 478 | set_file_options(set_options, eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 479 | |
| 480 | /* |
| 481 | * When opening a new file we take the readonly flag from the file. |
| 482 | * Default is r/w, can be set to r/o below. |
| 483 | * Don't reset it when in readonly mode |
| 484 | * Only set/reset b_p_ro when BF_CHECK_RO is set. |
| 485 | */ |
| 486 | check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO)); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 487 | if (check_readonly && !readonlymode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 488 | curbuf->b_p_ro = FALSE; |
| 489 | |
| 490 | if (newfile && !read_stdin && !read_buffer) |
| 491 | { |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 492 | /* Remember time of file. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 493 | if (mch_stat((char *)fname, &st) >= 0) |
| 494 | { |
| 495 | buf_store_time(curbuf, &st, fname); |
| 496 | curbuf->b_mtime_read = curbuf->b_mtime; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 497 | #ifdef UNIX |
| 498 | /* |
| 499 | * Use the protection bits of the original file for the swap file. |
| 500 | * This makes it possible for others to read the name of the |
| 501 | * edited file from the swapfile, but only if they can read the |
| 502 | * edited file. |
| 503 | * Remove the "write" and "execute" bits for group and others |
| 504 | * (they must not write the swapfile). |
| 505 | * Add the "read" and "write" bits for the user, otherwise we may |
| 506 | * not be able to write to the file ourselves. |
| 507 | * Setting the bits is done below, after creating the swap file. |
| 508 | */ |
| 509 | swap_mode = (st.st_mode & 0644) | 0600; |
| 510 | #endif |
| 511 | #ifdef FEAT_CW_EDITOR |
| 512 | /* Get the FSSpec on MacOS |
| 513 | * TODO: Update it properly when the buffer name changes |
| 514 | */ |
| 515 | (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec); |
| 516 | #endif |
| 517 | #ifdef VMS |
| 518 | curbuf->b_fab_rfm = st.st_fab_rfm; |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 519 | curbuf->b_fab_rat = st.st_fab_rat; |
| 520 | curbuf->b_fab_mrs = st.st_fab_mrs; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 521 | #endif |
| 522 | } |
| 523 | else |
| 524 | { |
| 525 | curbuf->b_mtime = 0; |
| 526 | curbuf->b_mtime_read = 0; |
| 527 | curbuf->b_orig_size = 0; |
| 528 | curbuf->b_orig_mode = 0; |
| 529 | } |
| 530 | |
| 531 | /* Reset the "new file" flag. It will be set again below when the |
| 532 | * file doesn't exist. */ |
| 533 | curbuf->b_flags &= ~(BF_NEW | BF_NEW_W); |
| 534 | } |
| 535 | |
| 536 | /* |
| 537 | * for UNIX: check readonly with perm and mch_access() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 538 | * for MSDOS and Amiga: check readonly by trying to open the file for writing |
| 539 | */ |
| 540 | file_readonly = FALSE; |
| 541 | if (read_stdin) |
| 542 | { |
| 543 | #if defined(MSDOS) || defined(MSWIN) || defined(OS2) |
| 544 | /* Force binary I/O on stdin to avoid CR-LF -> LF conversion. */ |
| 545 | setmode(0, O_BINARY); |
| 546 | #endif |
| 547 | } |
| 548 | else if (!read_buffer) |
| 549 | { |
| 550 | #ifdef USE_MCH_ACCESS |
| 551 | if ( |
| 552 | # ifdef UNIX |
| 553 | !(perm & 0222) || |
| 554 | # endif |
| 555 | mch_access((char *)fname, W_OK)) |
| 556 | file_readonly = TRUE; |
| 557 | fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); |
| 558 | #else |
| 559 | if (!newfile |
| 560 | || readonlymode |
| 561 | || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0) |
| 562 | { |
| 563 | file_readonly = TRUE; |
| 564 | /* try to open ro */ |
| 565 | fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); |
| 566 | } |
| 567 | #endif |
| 568 | } |
| 569 | |
| 570 | if (fd < 0) /* cannot open at all */ |
| 571 | { |
| 572 | #ifndef UNIX |
| 573 | int isdir_f; |
| 574 | #endif |
| 575 | msg_scroll = msg_save; |
| 576 | #ifndef UNIX |
| 577 | /* |
| 578 | * On MSDOS and Amiga we can't open a directory, check here. |
| 579 | */ |
| 580 | isdir_f = (mch_isdir(fname)); |
| 581 | perm = mch_getperm(fname); /* check if the file exists */ |
| 582 | if (isdir_f) |
| 583 | { |
| 584 | filemess(curbuf, sfname, (char_u *)_("is a directory"), 0); |
| 585 | curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
| 586 | } |
| 587 | else |
| 588 | #endif |
| 589 | if (newfile) |
| 590 | { |
Bram Moolenaar | 2efbc66 | 2010-05-14 18:56:38 +0200 | [diff] [blame] | 591 | if (perm < 0 |
| 592 | #ifdef ENOENT |
| 593 | && errno == ENOENT |
| 594 | #endif |
| 595 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 596 | { |
| 597 | /* |
| 598 | * Set the 'new-file' flag, so that when the file has |
| 599 | * been created by someone else, a ":w" will complain. |
| 600 | */ |
| 601 | curbuf->b_flags |= BF_NEW; |
| 602 | |
| 603 | /* Create a swap file now, so that other Vims are warned |
| 604 | * that we are editing this file. Don't do this for a |
| 605 | * "nofile" or "nowrite" buffer type. */ |
| 606 | #ifdef FEAT_QUICKFIX |
| 607 | if (!bt_dontwrite(curbuf)) |
| 608 | #endif |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 609 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 610 | check_need_swap(newfile); |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 611 | #ifdef FEAT_AUTOCMD |
| 612 | /* SwapExists autocommand may mess things up */ |
| 613 | if (curbuf != old_curbuf |
| 614 | || (using_b_ffname |
| 615 | && (old_b_ffname != curbuf->b_ffname)) |
| 616 | || (using_b_fname |
| 617 | && (old_b_fname != curbuf->b_fname))) |
| 618 | { |
| 619 | EMSG(_(e_auchangedbuf)); |
| 620 | return FAIL; |
| 621 | } |
| 622 | #endif |
| 623 | } |
Bram Moolenaar | 5b962cf | 2005-12-12 21:58:40 +0000 | [diff] [blame] | 624 | if (dir_of_file_exists(fname)) |
| 625 | filemess(curbuf, sfname, (char_u *)_("[New File]"), 0); |
| 626 | else |
| 627 | filemess(curbuf, sfname, |
| 628 | (char_u *)_("[New DIRECTORY]"), 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 629 | #ifdef FEAT_VIMINFO |
| 630 | /* Even though this is a new file, it might have been |
| 631 | * edited before and deleted. Get the old marks. */ |
| 632 | check_marks_read(); |
| 633 | #endif |
| 634 | #ifdef FEAT_MBYTE |
Bram Moolenaar | ad875fb | 2013-07-24 15:02:03 +0200 | [diff] [blame] | 635 | /* Set forced 'fileencoding'. */ |
| 636 | if (eap != NULL) |
| 637 | set_forced_fenc(eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 638 | #endif |
| 639 | #ifdef FEAT_AUTOCMD |
| 640 | apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname, |
| 641 | FALSE, curbuf, eap); |
| 642 | #endif |
| 643 | /* remember the current fileformat */ |
| 644 | save_file_ff(curbuf); |
| 645 | |
| 646 | #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
| 647 | if (aborting()) /* autocmds may abort script processing */ |
| 648 | return FAIL; |
| 649 | #endif |
| 650 | return OK; /* a new file is not an error */ |
| 651 | } |
| 652 | else |
| 653 | { |
Bram Moolenaar | 202795b | 2005-10-11 20:29:39 +0000 | [diff] [blame] | 654 | filemess(curbuf, sfname, (char_u *)( |
| 655 | # ifdef EFBIG |
| 656 | (errno == EFBIG) ? _("[File too big]") : |
| 657 | # endif |
Bram Moolenaar | 2efbc66 | 2010-05-14 18:56:38 +0200 | [diff] [blame] | 658 | # ifdef EOVERFLOW |
| 659 | (errno == EOVERFLOW) ? _("[File too big]") : |
| 660 | # endif |
Bram Moolenaar | 202795b | 2005-10-11 20:29:39 +0000 | [diff] [blame] | 661 | _("[Permission Denied]")), 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 662 | curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | return FAIL; |
| 667 | } |
| 668 | |
| 669 | /* |
| 670 | * Only set the 'ro' flag for readonly files the first time they are |
| 671 | * loaded. Help files always get readonly mode |
| 672 | */ |
| 673 | if ((check_readonly && file_readonly) || curbuf->b_help) |
| 674 | curbuf->b_p_ro = TRUE; |
| 675 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 676 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 677 | { |
Bram Moolenaar | 690ffc0 | 2008-01-04 15:31:21 +0000 | [diff] [blame] | 678 | /* Don't change 'eol' if reading from buffer as it will already be |
| 679 | * correctly set when reading stdin. */ |
| 680 | if (!read_buffer) |
| 681 | { |
| 682 | curbuf->b_p_eol = TRUE; |
| 683 | curbuf->b_start_eol = TRUE; |
| 684 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 685 | #ifdef FEAT_MBYTE |
| 686 | curbuf->b_p_bomb = FALSE; |
Bram Moolenaar | 83eb885 | 2007-08-12 13:51:26 +0000 | [diff] [blame] | 687 | curbuf->b_start_bomb = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 688 | #endif |
| 689 | } |
| 690 | |
| 691 | /* Create a swap file now, so that other Vims are warned that we are |
| 692 | * editing this file. |
| 693 | * Don't do this for a "nofile" or "nowrite" buffer type. */ |
| 694 | #ifdef FEAT_QUICKFIX |
| 695 | if (!bt_dontwrite(curbuf)) |
| 696 | #endif |
| 697 | { |
| 698 | check_need_swap(newfile); |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 699 | #ifdef FEAT_AUTOCMD |
| 700 | if (!read_stdin && (curbuf != old_curbuf |
| 701 | || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) |
| 702 | || (using_b_fname && (old_b_fname != curbuf->b_fname)))) |
| 703 | { |
| 704 | EMSG(_(e_auchangedbuf)); |
| 705 | if (!read_buffer) |
| 706 | close(fd); |
| 707 | return FAIL; |
| 708 | } |
| 709 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 710 | #ifdef UNIX |
| 711 | /* Set swap file protection bits after creating it. */ |
Bram Moolenaar | f061e0b | 2009-06-24 15:32:01 +0000 | [diff] [blame] | 712 | if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL |
| 713 | && curbuf->b_ml.ml_mfp->mf_fname != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 714 | (void)mch_setperm(curbuf->b_ml.ml_mfp->mf_fname, (long)swap_mode); |
| 715 | #endif |
| 716 | } |
| 717 | |
Bram Moolenaar | b815dac | 2005-12-07 20:59:24 +0000 | [diff] [blame] | 718 | #if defined(HAS_SWAP_EXISTS_ACTION) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 719 | /* If "Quit" selected at ATTENTION dialog, don't load the file */ |
| 720 | if (swap_exists_action == SEA_QUIT) |
| 721 | { |
| 722 | if (!read_buffer && !read_stdin) |
| 723 | close(fd); |
| 724 | return FAIL; |
| 725 | } |
| 726 | #endif |
| 727 | |
| 728 | ++no_wait_return; /* don't wait for return yet */ |
| 729 | |
| 730 | /* |
| 731 | * Set '[ mark to the line above where the lines go (line 1 if zero). |
| 732 | */ |
| 733 | curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); |
| 734 | curbuf->b_op_start.col = 0; |
| 735 | |
| 736 | #ifdef FEAT_AUTOCMD |
| 737 | if (!read_buffer) |
| 738 | { |
| 739 | int m = msg_scroll; |
| 740 | int n = msg_scrolled; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 741 | |
| 742 | /* |
| 743 | * The file must be closed again, the autocommands may want to change |
| 744 | * the file before reading it. |
| 745 | */ |
| 746 | if (!read_stdin) |
| 747 | close(fd); /* ignore errors */ |
| 748 | |
| 749 | /* |
| 750 | * The output from the autocommands should not overwrite anything and |
| 751 | * should not be overwritten: Set msg_scroll, restore its value if no |
| 752 | * output was done. |
| 753 | */ |
| 754 | msg_scroll = TRUE; |
| 755 | if (filtering) |
| 756 | apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname, |
| 757 | FALSE, curbuf, eap); |
| 758 | else if (read_stdin) |
| 759 | apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname, |
| 760 | FALSE, curbuf, eap); |
| 761 | else if (newfile) |
| 762 | apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname, |
| 763 | FALSE, curbuf, eap); |
| 764 | else |
| 765 | apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname, |
| 766 | FALSE, NULL, eap); |
| 767 | if (msg_scrolled == n) |
| 768 | msg_scroll = m; |
| 769 | |
| 770 | #ifdef FEAT_EVAL |
| 771 | if (aborting()) /* autocmds may abort script processing */ |
| 772 | { |
| 773 | --no_wait_return; |
| 774 | msg_scroll = msg_save; |
| 775 | curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
| 776 | return FAIL; |
| 777 | } |
| 778 | #endif |
| 779 | /* |
| 780 | * Don't allow the autocommands to change the current buffer. |
| 781 | * Try to re-open the file. |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 782 | * |
| 783 | * Don't allow the autocommands to change the buffer name either |
| 784 | * (cd for example) if it invalidates fname or sfname. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 785 | */ |
| 786 | if (!read_stdin && (curbuf != old_curbuf |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 787 | || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) |
| 788 | || (using_b_fname && (old_b_fname != curbuf->b_fname)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 789 | || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0)) |
| 790 | { |
| 791 | --no_wait_return; |
| 792 | msg_scroll = msg_save; |
| 793 | if (fd < 0) |
| 794 | EMSG(_("E200: *ReadPre autocommands made the file unreadable")); |
| 795 | else |
| 796 | EMSG(_("E201: *ReadPre autocommands must not change current buffer")); |
| 797 | curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
| 798 | return FAIL; |
| 799 | } |
| 800 | } |
| 801 | #endif /* FEAT_AUTOCMD */ |
| 802 | |
| 803 | /* Autocommands may add lines to the file, need to check if it is empty */ |
| 804 | wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY); |
| 805 | |
| 806 | if (!recoverymode && !filtering && !(flags & READ_DUMMY)) |
| 807 | { |
| 808 | /* |
| 809 | * Show the user that we are busy reading the input. Sometimes this |
| 810 | * may take a while. When reading from stdin another program may |
| 811 | * still be running, don't move the cursor to the last line, unless |
| 812 | * always using the GUI. |
| 813 | */ |
| 814 | if (read_stdin) |
| 815 | { |
| 816 | #ifndef ALWAYS_USE_GUI |
| 817 | mch_msg(_("Vim: Reading from stdin...\n")); |
| 818 | #endif |
| 819 | #ifdef FEAT_GUI |
| 820 | /* Also write a message in the GUI window, if there is one. */ |
| 821 | if (gui.in_use && !gui.dying && !gui.starting) |
| 822 | { |
| 823 | p = (char_u *)_("Reading from stdin..."); |
| 824 | gui_write(p, (int)STRLEN(p)); |
| 825 | } |
| 826 | #endif |
| 827 | } |
| 828 | else if (!read_buffer) |
| 829 | filemess(curbuf, sfname, (char_u *)"", 0); |
| 830 | } |
| 831 | |
| 832 | msg_scroll = FALSE; /* overwrite the file message */ |
| 833 | |
| 834 | /* |
| 835 | * Set linecnt now, before the "retry" caused by a wrong guess for |
| 836 | * fileformat, and after the autocommands, which may change them. |
| 837 | */ |
| 838 | linecnt = curbuf->b_ml.ml_line_count; |
| 839 | |
| 840 | #ifdef FEAT_MBYTE |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 841 | /* "++bad=" argument. */ |
| 842 | if (eap != NULL && eap->bad_char != 0) |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 843 | { |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 844 | bad_char_behavior = eap->bad_char; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 845 | if (set_options) |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 846 | curbuf->b_bad_char = eap->bad_char; |
| 847 | } |
| 848 | else |
| 849 | curbuf->b_bad_char = 0; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 850 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 851 | /* |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 852 | * Decide which 'encoding' to use or use first. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 853 | */ |
| 854 | if (eap != NULL && eap->force_enc != 0) |
| 855 | { |
| 856 | fenc = enc_canonize(eap->cmd + eap->force_enc); |
| 857 | fenc_alloced = TRUE; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 858 | keep_dest_enc = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 859 | } |
| 860 | else if (curbuf->b_p_bin) |
| 861 | { |
| 862 | fenc = (char_u *)""; /* binary: don't convert */ |
| 863 | fenc_alloced = FALSE; |
| 864 | } |
| 865 | else if (curbuf->b_help) |
| 866 | { |
| 867 | char_u firstline[80]; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 868 | int fc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 869 | |
| 870 | /* Help files are either utf-8 or latin1. Try utf-8 first, if this |
| 871 | * fails it must be latin1. |
| 872 | * Always do this when 'encoding' is "utf-8". Otherwise only do |
| 873 | * this when needed to avoid [converted] remarks all the time. |
| 874 | * It is needed when the first line contains non-ASCII characters. |
| 875 | * That is only in *.??x files. */ |
| 876 | fenc = (char_u *)"latin1"; |
| 877 | c = enc_utf8; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 878 | if (!c && !read_stdin) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 879 | { |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 880 | fc = fname[STRLEN(fname) - 1]; |
| 881 | if (TOLOWER_ASC(fc) == 'x') |
| 882 | { |
| 883 | /* Read the first line (and a bit more). Immediately rewind to |
| 884 | * the start of the file. If the read() fails "len" is -1. */ |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 885 | len = read_eintr(fd, firstline, 80); |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 886 | lseek(fd, (off_t)0L, SEEK_SET); |
| 887 | for (p = firstline; p < firstline + len; ++p) |
| 888 | if (*p >= 0x80) |
| 889 | { |
| 890 | c = TRUE; |
| 891 | break; |
| 892 | } |
| 893 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | if (c) |
| 897 | { |
| 898 | fenc_next = fenc; |
| 899 | fenc = (char_u *)"utf-8"; |
| 900 | |
| 901 | /* When the file is utf-8 but a character doesn't fit in |
| 902 | * 'encoding' don't retry. In help text editing utf-8 bytes |
| 903 | * doesn't make sense. */ |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 904 | if (!enc_utf8) |
| 905 | keep_dest_enc = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 906 | } |
| 907 | fenc_alloced = FALSE; |
| 908 | } |
| 909 | else if (*p_fencs == NUL) |
| 910 | { |
| 911 | fenc = curbuf->b_p_fenc; /* use format from buffer */ |
| 912 | fenc_alloced = FALSE; |
| 913 | } |
| 914 | else |
| 915 | { |
| 916 | fenc_next = p_fencs; /* try items in 'fileencodings' */ |
| 917 | fenc = next_fenc(&fenc_next); |
| 918 | fenc_alloced = TRUE; |
| 919 | } |
| 920 | #endif |
| 921 | |
| 922 | /* |
| 923 | * Jump back here to retry reading the file in different ways. |
| 924 | * Reasons to retry: |
| 925 | * - encoding conversion failed: try another one from "fenc_next" |
| 926 | * - BOM detected and fenc was set, need to setup conversion |
| 927 | * - "fileformat" check failed: try another |
| 928 | * |
| 929 | * Variables set for special retry actions: |
| 930 | * "file_rewind" Rewind the file to start reading it again. |
| 931 | * "advance_fenc" Advance "fenc" using "fenc_next". |
| 932 | * "skip_read" Re-use already read bytes (BOM detected). |
| 933 | * "did_iconv" iconv() conversion failed, try 'charconvert'. |
| 934 | * "keep_fileformat" Don't reset "fileformat". |
| 935 | * |
| 936 | * Other status indicators: |
| 937 | * "tmpname" When != NULL did conversion with 'charconvert'. |
| 938 | * Output file has to be deleted afterwards. |
| 939 | * "iconv_fd" When != -1 did conversion with iconv(). |
| 940 | */ |
| 941 | retry: |
| 942 | |
| 943 | if (file_rewind) |
| 944 | { |
| 945 | if (read_buffer) |
| 946 | { |
| 947 | read_buf_lnum = 1; |
| 948 | read_buf_col = 0; |
| 949 | } |
| 950 | else if (read_stdin || lseek(fd, (off_t)0L, SEEK_SET) != 0) |
| 951 | { |
| 952 | /* Can't rewind the file, give up. */ |
| 953 | error = TRUE; |
| 954 | goto failed; |
| 955 | } |
| 956 | /* Delete the previously read lines. */ |
| 957 | while (lnum > from) |
| 958 | ml_delete(lnum--, FALSE); |
| 959 | file_rewind = FALSE; |
| 960 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 961 | if (set_options) |
Bram Moolenaar | 83eb885 | 2007-08-12 13:51:26 +0000 | [diff] [blame] | 962 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 963 | curbuf->b_p_bomb = FALSE; |
Bram Moolenaar | 83eb885 | 2007-08-12 13:51:26 +0000 | [diff] [blame] | 964 | curbuf->b_start_bomb = FALSE; |
| 965 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 966 | conv_error = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 967 | #endif |
| 968 | } |
| 969 | |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 970 | #ifdef FEAT_CRYPT |
| 971 | if (cryptkey != NULL) |
| 972 | /* Need to reset the state, but keep the key, don't want to ask for it |
| 973 | * again. */ |
| 974 | crypt_pop_state(); |
| 975 | #endif |
| 976 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 977 | /* |
| 978 | * When retrying with another "fenc" and the first time "fileformat" |
| 979 | * will be reset. |
| 980 | */ |
| 981 | if (keep_fileformat) |
| 982 | keep_fileformat = FALSE; |
| 983 | else |
| 984 | { |
| 985 | if (eap != NULL && eap->force_ff != 0) |
Bram Moolenaar | 1c86036 | 2008-11-12 15:05:21 +0000 | [diff] [blame] | 986 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 987 | fileformat = get_fileformat_force(curbuf, eap); |
Bram Moolenaar | 1c86036 | 2008-11-12 15:05:21 +0000 | [diff] [blame] | 988 | try_unix = try_dos = try_mac = FALSE; |
| 989 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 990 | else if (curbuf->b_p_bin) |
| 991 | fileformat = EOL_UNIX; /* binary: use Unix format */ |
| 992 | else if (*p_ffs == NUL) |
| 993 | fileformat = get_fileformat(curbuf);/* use format from buffer */ |
| 994 | else |
| 995 | fileformat = EOL_UNKNOWN; /* detect from file */ |
| 996 | } |
| 997 | |
| 998 | #ifdef FEAT_MBYTE |
| 999 | # ifdef USE_ICONV |
| 1000 | if (iconv_fd != (iconv_t)-1) |
| 1001 | { |
| 1002 | /* aborted conversion with iconv(), close the descriptor */ |
| 1003 | iconv_close(iconv_fd); |
| 1004 | iconv_fd = (iconv_t)-1; |
| 1005 | } |
| 1006 | # endif |
| 1007 | |
| 1008 | if (advance_fenc) |
| 1009 | { |
| 1010 | /* |
| 1011 | * Try the next entry in 'fileencodings'. |
| 1012 | */ |
| 1013 | advance_fenc = FALSE; |
| 1014 | |
| 1015 | if (eap != NULL && eap->force_enc != 0) |
| 1016 | { |
| 1017 | /* Conversion given with "++cc=" wasn't possible, read |
| 1018 | * without conversion. */ |
| 1019 | notconverted = TRUE; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1020 | conv_error = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1021 | if (fenc_alloced) |
| 1022 | vim_free(fenc); |
| 1023 | fenc = (char_u *)""; |
| 1024 | fenc_alloced = FALSE; |
| 1025 | } |
| 1026 | else |
| 1027 | { |
| 1028 | if (fenc_alloced) |
| 1029 | vim_free(fenc); |
| 1030 | if (fenc_next != NULL) |
| 1031 | { |
| 1032 | fenc = next_fenc(&fenc_next); |
| 1033 | fenc_alloced = (fenc_next != NULL); |
| 1034 | } |
| 1035 | else |
| 1036 | { |
| 1037 | fenc = (char_u *)""; |
| 1038 | fenc_alloced = FALSE; |
| 1039 | } |
| 1040 | } |
| 1041 | if (tmpname != NULL) |
| 1042 | { |
| 1043 | mch_remove(tmpname); /* delete converted file */ |
| 1044 | vim_free(tmpname); |
| 1045 | tmpname = NULL; |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | /* |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 1050 | * Conversion may be required when the encoding of the file is different |
| 1051 | * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1052 | */ |
| 1053 | fio_flags = 0; |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 1054 | converted = need_conversion(fenc); |
| 1055 | if (converted) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1056 | { |
| 1057 | |
| 1058 | /* "ucs-bom" means we need to check the first bytes of the file |
| 1059 | * for a BOM. */ |
| 1060 | if (STRCMP(fenc, ENC_UCSBOM) == 0) |
| 1061 | fio_flags = FIO_UCSBOM; |
| 1062 | |
| 1063 | /* |
| 1064 | * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be |
| 1065 | * done. This is handled below after read(). Prepare the |
| 1066 | * fio_flags to avoid having to parse the string each time. |
| 1067 | * Also check for Unicode to Latin1 conversion, because iconv() |
| 1068 | * appears not to handle this correctly. This works just like |
| 1069 | * conversion to UTF-8 except how the resulting character is put in |
| 1070 | * the buffer. |
| 1071 | */ |
| 1072 | else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0) |
| 1073 | fio_flags = get_fio_flags(fenc); |
| 1074 | |
| 1075 | # ifdef WIN3264 |
| 1076 | /* |
| 1077 | * Conversion from an MS-Windows codepage to UTF-8 or another codepage |
| 1078 | * is handled with MultiByteToWideChar(). |
| 1079 | */ |
| 1080 | if (fio_flags == 0) |
| 1081 | fio_flags = get_win_fio_flags(fenc); |
| 1082 | # endif |
| 1083 | |
| 1084 | # ifdef MACOS_X |
| 1085 | /* Conversion from Apple MacRoman to latin1 or UTF-8 */ |
| 1086 | if (fio_flags == 0) |
| 1087 | fio_flags = get_mac_fio_flags(fenc); |
| 1088 | # endif |
| 1089 | |
| 1090 | # ifdef USE_ICONV |
| 1091 | /* |
| 1092 | * Try using iconv() if we can't convert internally. |
| 1093 | */ |
| 1094 | if (fio_flags == 0 |
| 1095 | # ifdef FEAT_EVAL |
| 1096 | && !did_iconv |
| 1097 | # endif |
| 1098 | ) |
| 1099 | iconv_fd = (iconv_t)my_iconv_open( |
| 1100 | enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc); |
| 1101 | # endif |
| 1102 | |
| 1103 | # ifdef FEAT_EVAL |
| 1104 | /* |
| 1105 | * Use the 'charconvert' expression when conversion is required |
| 1106 | * and we can't do it internally or with iconv(). |
| 1107 | */ |
| 1108 | if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL |
| 1109 | # ifdef USE_ICONV |
| 1110 | && iconv_fd == (iconv_t)-1 |
| 1111 | # endif |
| 1112 | ) |
| 1113 | { |
| 1114 | # ifdef USE_ICONV |
| 1115 | did_iconv = FALSE; |
| 1116 | # endif |
| 1117 | /* Skip conversion when it's already done (retry for wrong |
| 1118 | * "fileformat"). */ |
| 1119 | if (tmpname == NULL) |
| 1120 | { |
| 1121 | tmpname = readfile_charconvert(fname, fenc, &fd); |
| 1122 | if (tmpname == NULL) |
| 1123 | { |
| 1124 | /* Conversion failed. Try another one. */ |
| 1125 | advance_fenc = TRUE; |
| 1126 | if (fd < 0) |
| 1127 | { |
| 1128 | /* Re-opening the original file failed! */ |
| 1129 | EMSG(_("E202: Conversion made file unreadable!")); |
| 1130 | error = TRUE; |
| 1131 | goto failed; |
| 1132 | } |
| 1133 | goto retry; |
| 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | else |
| 1138 | # endif |
| 1139 | { |
| 1140 | if (fio_flags == 0 |
| 1141 | # ifdef USE_ICONV |
| 1142 | && iconv_fd == (iconv_t)-1 |
| 1143 | # endif |
| 1144 | ) |
| 1145 | { |
| 1146 | /* Conversion wanted but we can't. |
| 1147 | * Try the next conversion in 'fileencodings' */ |
| 1148 | advance_fenc = TRUE; |
| 1149 | goto retry; |
| 1150 | } |
| 1151 | } |
| 1152 | } |
| 1153 | |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1154 | /* 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] | 1155 | * 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] | 1156 | * stdin or fixed at a specific encoding. */ |
| 1157 | can_retry = (*fenc != NUL && !read_stdin && !keep_dest_enc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1158 | #endif |
| 1159 | |
| 1160 | if (!skip_read) |
| 1161 | { |
| 1162 | linerest = 0; |
| 1163 | filesize = 0; |
| 1164 | skip_count = lines_to_skip; |
| 1165 | read_count = lines_to_read; |
| 1166 | #ifdef FEAT_MBYTE |
| 1167 | conv_restlen = 0; |
| 1168 | #endif |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 1169 | #ifdef FEAT_PERSISTENT_UNDO |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 1170 | read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0 |
| 1171 | && curbuf->b_ffname != NULL |
| 1172 | && curbuf->b_p_udf |
| 1173 | && !filtering |
| 1174 | && !read_stdin |
| 1175 | && !read_buffer); |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 1176 | if (read_undo_file) |
| 1177 | sha256_start(&sha_ctx); |
| 1178 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | while (!error && !got_int) |
| 1182 | { |
| 1183 | /* |
| 1184 | * We allocate as much space for the file as we can get, plus |
| 1185 | * space for the old line plus room for one terminating NUL. |
| 1186 | * The amount is limited by the fact that read() only can read |
| 1187 | * upto max_unsigned characters (and other things). |
| 1188 | */ |
| 1189 | #if SIZEOF_INT <= 2 |
| 1190 | if (linerest >= 0x7ff0) |
| 1191 | { |
| 1192 | ++split; |
| 1193 | *ptr = NL; /* split line by inserting a NL */ |
| 1194 | size = 1; |
| 1195 | } |
| 1196 | else |
| 1197 | #endif |
| 1198 | { |
| 1199 | if (!skip_read) |
| 1200 | { |
| 1201 | #if SIZEOF_INT > 2 |
Bram Moolenaar | 311d982 | 2007-02-27 15:48:28 +0000 | [diff] [blame] | 1202 | # if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1203 | size = SSIZE_MAX; /* use max I/O size, 52K */ |
| 1204 | # else |
| 1205 | size = 0x10000L; /* use buffer >= 64K */ |
| 1206 | # endif |
| 1207 | #else |
| 1208 | size = 0x7ff0L - linerest; /* limit buffer to 32K */ |
| 1209 | #endif |
| 1210 | |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 1211 | for ( ; size >= 10; size = (long)((long_u)size >> 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1212 | { |
| 1213 | if ((new_buffer = lalloc((long_u)(size + linerest + 1), |
| 1214 | FALSE)) != NULL) |
| 1215 | break; |
| 1216 | } |
| 1217 | if (new_buffer == NULL) |
| 1218 | { |
| 1219 | do_outofmem_msg((long_u)(size * 2 + linerest + 1)); |
| 1220 | error = TRUE; |
| 1221 | break; |
| 1222 | } |
| 1223 | if (linerest) /* copy characters from the previous buffer */ |
| 1224 | mch_memmove(new_buffer, ptr - linerest, (size_t)linerest); |
| 1225 | vim_free(buffer); |
| 1226 | buffer = new_buffer; |
| 1227 | ptr = buffer + linerest; |
| 1228 | line_start = buffer; |
| 1229 | |
| 1230 | #ifdef FEAT_MBYTE |
| 1231 | /* May need room to translate into. |
| 1232 | * For iconv() we don't really know the required space, use a |
| 1233 | * factor ICONV_MULT. |
| 1234 | * latin1 to utf-8: 1 byte becomes up to 2 bytes |
| 1235 | * utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes |
| 1236 | * become up to 4 bytes, size must be multiple of 2 |
| 1237 | * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be |
| 1238 | * multiple of 2 |
| 1239 | * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be |
| 1240 | * multiple of 4 */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1241 | real_size = (int)size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1242 | # ifdef USE_ICONV |
| 1243 | if (iconv_fd != (iconv_t)-1) |
| 1244 | size = size / ICONV_MULT; |
| 1245 | else |
| 1246 | # endif |
| 1247 | if (fio_flags & FIO_LATIN1) |
| 1248 | size = size / 2; |
| 1249 | else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) |
| 1250 | size = (size * 2 / 3) & ~1; |
| 1251 | else if (fio_flags & FIO_UCS4) |
| 1252 | size = (size * 2 / 3) & ~3; |
| 1253 | else if (fio_flags == FIO_UCSBOM) |
| 1254 | size = size / ICONV_MULT; /* worst case */ |
| 1255 | # ifdef WIN3264 |
| 1256 | else if (fio_flags & FIO_CODEPAGE) |
| 1257 | size = size / ICONV_MULT; /* also worst case */ |
| 1258 | # endif |
| 1259 | # ifdef MACOS_X |
| 1260 | else if (fio_flags & FIO_MACROMAN) |
| 1261 | size = size / ICONV_MULT; /* also worst case */ |
| 1262 | # endif |
| 1263 | #endif |
| 1264 | |
| 1265 | #ifdef FEAT_MBYTE |
| 1266 | if (conv_restlen > 0) |
| 1267 | { |
| 1268 | /* Insert unconverted bytes from previous line. */ |
| 1269 | mch_memmove(ptr, conv_rest, conv_restlen); |
| 1270 | ptr += conv_restlen; |
| 1271 | size -= conv_restlen; |
| 1272 | } |
| 1273 | #endif |
| 1274 | |
| 1275 | if (read_buffer) |
| 1276 | { |
| 1277 | /* |
| 1278 | * Read bytes from curbuf. Used for converting text read |
| 1279 | * from stdin. |
| 1280 | */ |
| 1281 | if (read_buf_lnum > from) |
| 1282 | size = 0; |
| 1283 | else |
| 1284 | { |
| 1285 | int n, ni; |
| 1286 | long tlen; |
| 1287 | |
| 1288 | tlen = 0; |
| 1289 | for (;;) |
| 1290 | { |
| 1291 | p = ml_get(read_buf_lnum) + read_buf_col; |
| 1292 | n = (int)STRLEN(p); |
| 1293 | if ((int)tlen + n + 1 > size) |
| 1294 | { |
| 1295 | /* Filled up to "size", append partial line. |
| 1296 | * Change NL to NUL to reverse the effect done |
| 1297 | * below. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1298 | n = (int)(size - tlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1299 | for (ni = 0; ni < n; ++ni) |
| 1300 | { |
| 1301 | if (p[ni] == NL) |
| 1302 | ptr[tlen++] = NUL; |
| 1303 | else |
| 1304 | ptr[tlen++] = p[ni]; |
| 1305 | } |
| 1306 | read_buf_col += n; |
| 1307 | break; |
| 1308 | } |
| 1309 | else |
| 1310 | { |
| 1311 | /* Append whole line and new-line. Change NL |
| 1312 | * to NUL to reverse the effect done below. */ |
| 1313 | for (ni = 0; ni < n; ++ni) |
| 1314 | { |
| 1315 | if (p[ni] == NL) |
| 1316 | ptr[tlen++] = NUL; |
| 1317 | else |
| 1318 | ptr[tlen++] = p[ni]; |
| 1319 | } |
| 1320 | ptr[tlen++] = NL; |
| 1321 | read_buf_col = 0; |
| 1322 | if (++read_buf_lnum > from) |
| 1323 | { |
| 1324 | /* When the last line didn't have an |
| 1325 | * end-of-line don't add it now either. */ |
| 1326 | if (!curbuf->b_p_eol) |
| 1327 | --tlen; |
| 1328 | size = tlen; |
| 1329 | break; |
| 1330 | } |
| 1331 | } |
| 1332 | } |
| 1333 | } |
| 1334 | } |
| 1335 | else |
| 1336 | { |
| 1337 | /* |
| 1338 | * Read bytes from the file. |
| 1339 | */ |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 1340 | size = read_eintr(fd, ptr, size); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | if (size <= 0) |
| 1344 | { |
| 1345 | if (size < 0) /* read error */ |
| 1346 | error = TRUE; |
| 1347 | #ifdef FEAT_MBYTE |
| 1348 | else if (conv_restlen > 0) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1349 | { |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1350 | /* |
| 1351 | * Reached end-of-file but some trailing bytes could |
| 1352 | * not be converted. Truncated file? |
| 1353 | */ |
| 1354 | |
| 1355 | /* When we did a conversion report an error. */ |
| 1356 | if (fio_flags != 0 |
| 1357 | # ifdef USE_ICONV |
| 1358 | || iconv_fd != (iconv_t)-1 |
| 1359 | # endif |
| 1360 | ) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1361 | { |
Bram Moolenaar | e8d9530 | 2013-04-24 16:34:02 +0200 | [diff] [blame] | 1362 | if (can_retry) |
| 1363 | goto rewind_retry; |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1364 | if (conv_error == 0) |
| 1365 | conv_error = curbuf->b_ml.ml_line_count |
| 1366 | - linecnt + 1; |
| 1367 | } |
| 1368 | /* Remember the first linenr with an illegal byte */ |
| 1369 | else if (illegal_byte == 0) |
| 1370 | illegal_byte = curbuf->b_ml.ml_line_count |
| 1371 | - linecnt + 1; |
| 1372 | if (bad_char_behavior == BAD_DROP) |
| 1373 | { |
| 1374 | *(ptr - conv_restlen) = NUL; |
| 1375 | conv_restlen = 0; |
| 1376 | } |
| 1377 | else |
| 1378 | { |
| 1379 | /* Replace the trailing bytes with the replacement |
| 1380 | * character if we were converting; if we weren't, |
| 1381 | * leave the UTF8 checking code to do it, as it |
| 1382 | * works slightly differently. */ |
| 1383 | if (bad_char_behavior != BAD_KEEP && (fio_flags != 0 |
| 1384 | # ifdef USE_ICONV |
| 1385 | || iconv_fd != (iconv_t)-1 |
| 1386 | # endif |
| 1387 | )) |
| 1388 | { |
| 1389 | while (conv_restlen > 0) |
| 1390 | { |
| 1391 | *(--ptr) = bad_char_behavior; |
| 1392 | --conv_restlen; |
| 1393 | } |
| 1394 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1395 | fio_flags = 0; /* don't convert this */ |
Bram Moolenaar | b21e584 | 2006-04-16 18:30:08 +0000 | [diff] [blame] | 1396 | # ifdef USE_ICONV |
| 1397 | if (iconv_fd != (iconv_t)-1) |
| 1398 | { |
| 1399 | iconv_close(iconv_fd); |
| 1400 | iconv_fd = (iconv_t)-1; |
| 1401 | } |
| 1402 | # endif |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1403 | } |
| 1404 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1405 | #endif |
| 1406 | } |
| 1407 | |
| 1408 | #ifdef FEAT_CRYPT |
| 1409 | /* |
| 1410 | * At start of file: Check for magic number of encryption. |
| 1411 | */ |
| 1412 | if (filesize == 0) |
| 1413 | cryptkey = check_for_cryptkey(cryptkey, ptr, &size, |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 1414 | &filesize, newfile, sfname, |
| 1415 | &did_ask_for_key); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1416 | /* |
| 1417 | * Decrypt the read bytes. |
| 1418 | */ |
| 1419 | if (cryptkey != NULL && size > 0) |
Bram Moolenaar | 04c9baf | 2010-06-01 23:37:39 +0200 | [diff] [blame] | 1420 | crypt_decode(ptr, size); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1421 | #endif |
| 1422 | } |
| 1423 | skip_read = FALSE; |
| 1424 | |
| 1425 | #ifdef FEAT_MBYTE |
| 1426 | /* |
| 1427 | * At start of file (or after crypt magic number): Check for BOM. |
| 1428 | * Also check for a BOM for other Unicode encodings, but not after |
| 1429 | * converting with 'charconvert' or when a BOM has already been |
| 1430 | * found. |
| 1431 | */ |
| 1432 | if ((filesize == 0 |
| 1433 | # ifdef FEAT_CRYPT |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 1434 | || (filesize == (CRYPT_MAGIC_LEN |
Bram Moolenaar | 80794b1 | 2010-06-13 05:20:42 +0200 | [diff] [blame] | 1435 | + crypt_salt_len[use_crypt_method] |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 1436 | + crypt_seed_len[use_crypt_method]) |
| 1437 | && cryptkey != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1438 | # endif |
| 1439 | ) |
| 1440 | && (fio_flags == FIO_UCSBOM |
| 1441 | || (!curbuf->b_p_bomb |
| 1442 | && tmpname == NULL |
| 1443 | && (*fenc == 'u' || (*fenc == NUL && enc_utf8))))) |
| 1444 | { |
| 1445 | char_u *ccname; |
| 1446 | int blen; |
| 1447 | |
| 1448 | /* no BOM detection in a short file or in binary mode */ |
| 1449 | if (size < 2 || curbuf->b_p_bin) |
| 1450 | ccname = NULL; |
| 1451 | else |
| 1452 | ccname = check_for_bom(ptr, size, &blen, |
| 1453 | fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc)); |
| 1454 | if (ccname != NULL) |
| 1455 | { |
| 1456 | /* Remove BOM from the text */ |
| 1457 | filesize += blen; |
| 1458 | size -= blen; |
| 1459 | mch_memmove(ptr, ptr + blen, (size_t)size); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 1460 | if (set_options) |
Bram Moolenaar | 83eb885 | 2007-08-12 13:51:26 +0000 | [diff] [blame] | 1461 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1462 | curbuf->b_p_bomb = TRUE; |
Bram Moolenaar | 83eb885 | 2007-08-12 13:51:26 +0000 | [diff] [blame] | 1463 | curbuf->b_start_bomb = TRUE; |
| 1464 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | if (fio_flags == FIO_UCSBOM) |
| 1468 | { |
| 1469 | if (ccname == NULL) |
| 1470 | { |
| 1471 | /* No BOM detected: retry with next encoding. */ |
| 1472 | advance_fenc = TRUE; |
| 1473 | } |
| 1474 | else |
| 1475 | { |
| 1476 | /* BOM detected: set "fenc" and jump back */ |
| 1477 | if (fenc_alloced) |
| 1478 | vim_free(fenc); |
| 1479 | fenc = ccname; |
| 1480 | fenc_alloced = FALSE; |
| 1481 | } |
| 1482 | /* retry reading without getting new bytes or rewinding */ |
| 1483 | skip_read = TRUE; |
| 1484 | goto retry; |
| 1485 | } |
| 1486 | } |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1487 | |
| 1488 | /* Include not converted bytes. */ |
| 1489 | ptr -= conv_restlen; |
| 1490 | size += conv_restlen; |
| 1491 | conv_restlen = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1492 | #endif |
| 1493 | /* |
| 1494 | * Break here for a read error or end-of-file. |
| 1495 | */ |
| 1496 | if (size <= 0) |
| 1497 | break; |
| 1498 | |
| 1499 | #ifdef FEAT_MBYTE |
| 1500 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1501 | # ifdef USE_ICONV |
| 1502 | if (iconv_fd != (iconv_t)-1) |
| 1503 | { |
| 1504 | /* |
| 1505 | * Attempt conversion of the read bytes to 'encoding' using |
| 1506 | * iconv(). |
| 1507 | */ |
| 1508 | const char *fromp; |
| 1509 | char *top; |
| 1510 | size_t from_size; |
| 1511 | size_t to_size; |
| 1512 | |
| 1513 | fromp = (char *)ptr; |
| 1514 | from_size = size; |
| 1515 | ptr += size; |
| 1516 | top = (char *)ptr; |
| 1517 | to_size = real_size - size; |
| 1518 | |
| 1519 | /* |
| 1520 | * If there is conversion error or not enough room try using |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 1521 | * another conversion. Except for when there is no |
| 1522 | * alternative (help files). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1523 | */ |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 1524 | while ((iconv(iconv_fd, (void *)&fromp, &from_size, |
| 1525 | &top, &to_size) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1526 | == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) |
| 1527 | || from_size > CONV_RESTLEN) |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 1528 | { |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1529 | if (can_retry) |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 1530 | goto rewind_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1531 | if (conv_error == 0) |
| 1532 | conv_error = readfile_linenr(linecnt, |
| 1533 | ptr, (char_u *)top); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1534 | |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1535 | /* Deal with a bad byte and continue with the next. */ |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 1536 | ++fromp; |
| 1537 | --from_size; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1538 | if (bad_char_behavior == BAD_KEEP) |
| 1539 | { |
| 1540 | *top++ = *(fromp - 1); |
| 1541 | --to_size; |
| 1542 | } |
| 1543 | else if (bad_char_behavior != BAD_DROP) |
| 1544 | { |
| 1545 | *top++ = bad_char_behavior; |
| 1546 | --to_size; |
| 1547 | } |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 1548 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1549 | |
| 1550 | if (from_size > 0) |
| 1551 | { |
| 1552 | /* Some remaining characters, keep them for the next |
| 1553 | * round. */ |
| 1554 | mch_memmove(conv_rest, (char_u *)fromp, from_size); |
| 1555 | conv_restlen = (int)from_size; |
| 1556 | } |
| 1557 | |
| 1558 | /* move the linerest to before the converted characters */ |
| 1559 | line_start = ptr - linerest; |
| 1560 | mch_memmove(line_start, buffer, (size_t)linerest); |
| 1561 | size = (long)((char_u *)top - ptr); |
| 1562 | } |
| 1563 | # endif |
| 1564 | |
| 1565 | # ifdef WIN3264 |
| 1566 | if (fio_flags & FIO_CODEPAGE) |
| 1567 | { |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1568 | char_u *src, *dst; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1569 | WCHAR ucs2buf[3]; |
| 1570 | int ucs2len; |
| 1571 | int codepage = FIO_GET_CP(fio_flags); |
| 1572 | int bytelen; |
| 1573 | int found_bad; |
| 1574 | char replstr[2]; |
| 1575 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1576 | /* |
| 1577 | * 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] | 1578 | * a codepage, using standard MS-Windows functions. This |
| 1579 | * requires two steps: |
| 1580 | * 1. convert from 'fileencoding' to ucs-2 |
| 1581 | * 2. convert from ucs-2 to 'encoding' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1582 | * |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1583 | * Because there may be illegal bytes AND an incomplete byte |
| 1584 | * sequence at the end, we may have to do the conversion one |
| 1585 | * character at a time to get it right. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1586 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1587 | |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1588 | /* Replacement string for WideCharToMultiByte(). */ |
| 1589 | if (bad_char_behavior > 0) |
| 1590 | replstr[0] = bad_char_behavior; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1591 | else |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1592 | replstr[0] = '?'; |
| 1593 | replstr[1] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1594 | |
| 1595 | /* |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1596 | * Move the bytes to the end of the buffer, so that we have |
| 1597 | * room to put the result at the start. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1598 | */ |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1599 | src = ptr + real_size - size; |
| 1600 | mch_memmove(src, ptr, size); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1601 | |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1602 | /* |
| 1603 | * Do the conversion. |
| 1604 | */ |
| 1605 | dst = ptr; |
| 1606 | size = size; |
| 1607 | while (size > 0) |
| 1608 | { |
| 1609 | found_bad = FALSE; |
| 1610 | |
| 1611 | # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ |
| 1612 | if (codepage == CP_UTF8) |
| 1613 | { |
| 1614 | /* Handle CP_UTF8 input ourselves to be able to handle |
| 1615 | * trailing bytes properly. |
| 1616 | * Get one UTF-8 character from src. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1617 | bytelen = (int)utf_ptr2len_len(src, size); |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1618 | if (bytelen > size) |
| 1619 | { |
| 1620 | /* Only got some bytes of a character. Normally |
| 1621 | * it's put in "conv_rest", but if it's too long |
| 1622 | * deal with it as if they were illegal bytes. */ |
| 1623 | if (bytelen <= CONV_RESTLEN) |
| 1624 | break; |
| 1625 | |
| 1626 | /* weird overlong byte sequence */ |
| 1627 | bytelen = size; |
| 1628 | found_bad = TRUE; |
| 1629 | } |
| 1630 | else |
| 1631 | { |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 1632 | int u8c = utf_ptr2char(src); |
| 1633 | |
Bram Moolenaar | 86e0108 | 2005-12-29 22:45:34 +0000 | [diff] [blame] | 1634 | if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1)) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1635 | found_bad = TRUE; |
| 1636 | ucs2buf[0] = u8c; |
| 1637 | ucs2len = 1; |
| 1638 | } |
| 1639 | } |
| 1640 | else |
| 1641 | # endif |
| 1642 | { |
| 1643 | /* We don't know how long the byte sequence is, try |
| 1644 | * from one to three bytes. */ |
| 1645 | for (bytelen = 1; bytelen <= size && bytelen <= 3; |
| 1646 | ++bytelen) |
| 1647 | { |
| 1648 | ucs2len = MultiByteToWideChar(codepage, |
| 1649 | MB_ERR_INVALID_CHARS, |
| 1650 | (LPCSTR)src, bytelen, |
| 1651 | ucs2buf, 3); |
| 1652 | if (ucs2len > 0) |
| 1653 | break; |
| 1654 | } |
| 1655 | if (ucs2len == 0) |
| 1656 | { |
| 1657 | /* If we have only one byte then it's probably an |
| 1658 | * incomplete byte sequence. Otherwise discard |
| 1659 | * one byte as a bad character. */ |
| 1660 | if (size == 1) |
| 1661 | break; |
| 1662 | found_bad = TRUE; |
| 1663 | bytelen = 1; |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | if (!found_bad) |
| 1668 | { |
| 1669 | int i; |
| 1670 | |
| 1671 | /* Convert "ucs2buf[ucs2len]" to 'enc' in "dst". */ |
| 1672 | if (enc_utf8) |
| 1673 | { |
| 1674 | /* From UCS-2 to UTF-8. Cannot fail. */ |
| 1675 | for (i = 0; i < ucs2len; ++i) |
| 1676 | dst += utf_char2bytes(ucs2buf[i], dst); |
| 1677 | } |
| 1678 | else |
| 1679 | { |
| 1680 | BOOL bad = FALSE; |
| 1681 | int dstlen; |
| 1682 | |
| 1683 | /* From UCS-2 to "enc_codepage". If the |
| 1684 | * conversion uses the default character "?", |
| 1685 | * the data doesn't fit in this encoding. */ |
| 1686 | dstlen = WideCharToMultiByte(enc_codepage, 0, |
| 1687 | (LPCWSTR)ucs2buf, ucs2len, |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1688 | (LPSTR)dst, (int)(src - dst), |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1689 | replstr, &bad); |
| 1690 | if (bad) |
| 1691 | found_bad = TRUE; |
| 1692 | else |
| 1693 | dst += dstlen; |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | if (found_bad) |
| 1698 | { |
| 1699 | /* Deal with bytes we can't convert. */ |
| 1700 | if (can_retry) |
| 1701 | goto rewind_retry; |
| 1702 | if (conv_error == 0) |
| 1703 | conv_error = readfile_linenr(linecnt, ptr, dst); |
| 1704 | if (bad_char_behavior != BAD_DROP) |
| 1705 | { |
| 1706 | if (bad_char_behavior == BAD_KEEP) |
| 1707 | { |
| 1708 | mch_memmove(dst, src, bytelen); |
| 1709 | dst += bytelen; |
| 1710 | } |
| 1711 | else |
| 1712 | *dst++ = bad_char_behavior; |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | src += bytelen; |
| 1717 | size -= bytelen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1718 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1719 | |
| 1720 | if (size > 0) |
| 1721 | { |
| 1722 | /* An incomplete byte sequence remaining. */ |
| 1723 | mch_memmove(conv_rest, src, size); |
| 1724 | conv_restlen = size; |
| 1725 | } |
| 1726 | |
| 1727 | /* The new size is equal to how much "dst" was advanced. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1728 | size = (long)(dst - ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1729 | } |
| 1730 | else |
| 1731 | # endif |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 1732 | # ifdef MACOS_CONVERT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1733 | if (fio_flags & FIO_MACROMAN) |
| 1734 | { |
| 1735 | /* |
| 1736 | * Conversion from Apple MacRoman char encoding to UTF-8 or |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 1737 | * latin1. This is in os_mac_conv.c. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1738 | */ |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 1739 | if (macroman2enc(ptr, &size, real_size) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1740 | goto rewind_retry; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1741 | } |
| 1742 | else |
| 1743 | # endif |
| 1744 | if (fio_flags != 0) |
| 1745 | { |
| 1746 | int u8c; |
| 1747 | char_u *dest; |
| 1748 | char_u *tail = NULL; |
| 1749 | |
| 1750 | /* |
| 1751 | * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8. |
| 1752 | * "enc_utf8" not set: Convert Unicode to Latin1. |
| 1753 | * Go from end to start through the buffer, because the number |
| 1754 | * of bytes may increase. |
| 1755 | * "dest" points to after where the UTF-8 bytes go, "p" points |
| 1756 | * to after the next character to convert. |
| 1757 | */ |
| 1758 | dest = ptr + real_size; |
| 1759 | if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8) |
| 1760 | { |
| 1761 | p = ptr + size; |
| 1762 | if (fio_flags == FIO_UTF8) |
| 1763 | { |
| 1764 | /* Check for a trailing incomplete UTF-8 sequence */ |
| 1765 | tail = ptr + size - 1; |
| 1766 | while (tail > ptr && (*tail & 0xc0) == 0x80) |
| 1767 | --tail; |
| 1768 | if (tail + utf_byte2len(*tail) <= ptr + size) |
| 1769 | tail = NULL; |
| 1770 | else |
| 1771 | p = tail; |
| 1772 | } |
| 1773 | } |
| 1774 | else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) |
| 1775 | { |
| 1776 | /* Check for a trailing byte */ |
| 1777 | p = ptr + (size & ~1); |
| 1778 | if (size & 1) |
| 1779 | tail = p; |
| 1780 | if ((fio_flags & FIO_UTF16) && p > ptr) |
| 1781 | { |
| 1782 | /* Check for a trailing leading word */ |
| 1783 | if (fio_flags & FIO_ENDIAN_L) |
| 1784 | { |
| 1785 | u8c = (*--p << 8); |
| 1786 | u8c += *--p; |
| 1787 | } |
| 1788 | else |
| 1789 | { |
| 1790 | u8c = *--p; |
| 1791 | u8c += (*--p << 8); |
| 1792 | } |
| 1793 | if (u8c >= 0xd800 && u8c <= 0xdbff) |
| 1794 | tail = p; |
| 1795 | else |
| 1796 | p += 2; |
| 1797 | } |
| 1798 | } |
| 1799 | else /* FIO_UCS4 */ |
| 1800 | { |
| 1801 | /* Check for trailing 1, 2 or 3 bytes */ |
| 1802 | p = ptr + (size & ~3); |
| 1803 | if (size & 3) |
| 1804 | tail = p; |
| 1805 | } |
| 1806 | |
| 1807 | /* If there is a trailing incomplete sequence move it to |
| 1808 | * conv_rest[]. */ |
| 1809 | if (tail != NULL) |
| 1810 | { |
| 1811 | conv_restlen = (int)((ptr + size) - tail); |
| 1812 | mch_memmove(conv_rest, (char_u *)tail, conv_restlen); |
| 1813 | size -= conv_restlen; |
| 1814 | } |
| 1815 | |
| 1816 | |
| 1817 | while (p > ptr) |
| 1818 | { |
| 1819 | if (fio_flags & FIO_LATIN1) |
| 1820 | u8c = *--p; |
| 1821 | else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) |
| 1822 | { |
| 1823 | if (fio_flags & FIO_ENDIAN_L) |
| 1824 | { |
| 1825 | u8c = (*--p << 8); |
| 1826 | u8c += *--p; |
| 1827 | } |
| 1828 | else |
| 1829 | { |
| 1830 | u8c = *--p; |
| 1831 | u8c += (*--p << 8); |
| 1832 | } |
| 1833 | if ((fio_flags & FIO_UTF16) |
| 1834 | && u8c >= 0xdc00 && u8c <= 0xdfff) |
| 1835 | { |
| 1836 | int u16c; |
| 1837 | |
| 1838 | if (p == ptr) |
| 1839 | { |
| 1840 | /* Missing leading word. */ |
| 1841 | if (can_retry) |
| 1842 | goto rewind_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1843 | if (conv_error == 0) |
| 1844 | conv_error = readfile_linenr(linecnt, |
| 1845 | ptr, p); |
| 1846 | if (bad_char_behavior == BAD_DROP) |
| 1847 | continue; |
| 1848 | if (bad_char_behavior != BAD_KEEP) |
| 1849 | u8c = bad_char_behavior; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | /* found second word of double-word, get the first |
| 1853 | * word and compute the resulting character */ |
| 1854 | if (fio_flags & FIO_ENDIAN_L) |
| 1855 | { |
| 1856 | u16c = (*--p << 8); |
| 1857 | u16c += *--p; |
| 1858 | } |
| 1859 | else |
| 1860 | { |
| 1861 | u16c = *--p; |
| 1862 | u16c += (*--p << 8); |
| 1863 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1864 | u8c = 0x10000 + ((u16c & 0x3ff) << 10) |
| 1865 | + (u8c & 0x3ff); |
| 1866 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1867 | /* Check if the word is indeed a leading word. */ |
| 1868 | if (u16c < 0xd800 || u16c > 0xdbff) |
| 1869 | { |
| 1870 | if (can_retry) |
| 1871 | goto rewind_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1872 | if (conv_error == 0) |
| 1873 | conv_error = readfile_linenr(linecnt, |
| 1874 | ptr, p); |
| 1875 | if (bad_char_behavior == BAD_DROP) |
| 1876 | continue; |
| 1877 | if (bad_char_behavior != BAD_KEEP) |
| 1878 | u8c = bad_char_behavior; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1879 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1880 | } |
| 1881 | } |
| 1882 | else if (fio_flags & FIO_UCS4) |
| 1883 | { |
| 1884 | if (fio_flags & FIO_ENDIAN_L) |
| 1885 | { |
| 1886 | u8c = (*--p << 24); |
| 1887 | u8c += (*--p << 16); |
| 1888 | u8c += (*--p << 8); |
| 1889 | u8c += *--p; |
| 1890 | } |
| 1891 | else /* big endian */ |
| 1892 | { |
| 1893 | u8c = *--p; |
| 1894 | u8c += (*--p << 8); |
| 1895 | u8c += (*--p << 16); |
| 1896 | u8c += (*--p << 24); |
| 1897 | } |
| 1898 | } |
| 1899 | else /* UTF-8 */ |
| 1900 | { |
| 1901 | if (*--p < 0x80) |
| 1902 | u8c = *p; |
| 1903 | else |
| 1904 | { |
| 1905 | len = utf_head_off(ptr, p); |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1906 | p -= len; |
| 1907 | u8c = utf_ptr2char(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1908 | if (len == 0) |
| 1909 | { |
| 1910 | /* Not a valid UTF-8 character, retry with |
| 1911 | * another fenc when possible, otherwise just |
| 1912 | * report the error. */ |
| 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 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1923 | } |
| 1924 | } |
| 1925 | if (enc_utf8) /* produce UTF-8 */ |
| 1926 | { |
| 1927 | dest -= utf_char2len(u8c); |
| 1928 | (void)utf_char2bytes(u8c, dest); |
| 1929 | } |
| 1930 | else /* produce Latin1 */ |
| 1931 | { |
| 1932 | --dest; |
| 1933 | if (u8c >= 0x100) |
| 1934 | { |
| 1935 | /* character doesn't fit in latin1, retry with |
| 1936 | * another fenc when possible, otherwise just |
| 1937 | * report the error. */ |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1938 | if (can_retry) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1939 | goto rewind_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1940 | if (conv_error == 0) |
| 1941 | conv_error = readfile_linenr(linecnt, ptr, p); |
| 1942 | if (bad_char_behavior == BAD_DROP) |
| 1943 | ++dest; |
| 1944 | else if (bad_char_behavior == BAD_KEEP) |
| 1945 | *dest = u8c; |
| 1946 | else if (eap != NULL && eap->bad_char != 0) |
| 1947 | *dest = bad_char_behavior; |
| 1948 | else |
| 1949 | *dest = 0xBF; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1950 | } |
| 1951 | else |
| 1952 | *dest = u8c; |
| 1953 | } |
| 1954 | } |
| 1955 | |
| 1956 | /* move the linerest to before the converted characters */ |
| 1957 | line_start = dest - linerest; |
| 1958 | mch_memmove(line_start, buffer, (size_t)linerest); |
| 1959 | size = (long)((ptr + real_size) - dest); |
| 1960 | ptr = dest; |
| 1961 | } |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1962 | else if (enc_utf8 && !curbuf->b_p_bin) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1963 | { |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1964 | int incomplete_tail = FALSE; |
| 1965 | |
| 1966 | /* Reading UTF-8: Check if the bytes are valid UTF-8. */ |
| 1967 | for (p = ptr; ; ++p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1968 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1969 | int todo = (int)((ptr + size) - p); |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1970 | int l; |
| 1971 | |
| 1972 | if (todo <= 0) |
| 1973 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1974 | if (*p >= 0x80) |
| 1975 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1976 | /* A length of 1 means it's an illegal byte. Accept |
| 1977 | * an incomplete character at the end though, the next |
| 1978 | * read() will get the next bytes, we'll check it |
| 1979 | * then. */ |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1980 | l = utf_ptr2len_len(p, todo); |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1981 | if (l > todo && !incomplete_tail) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1982 | { |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1983 | /* Avoid retrying with a different encoding when |
| 1984 | * a truncated file is more likely, or attempting |
| 1985 | * to read the rest of an incomplete sequence when |
| 1986 | * we have already done so. */ |
| 1987 | if (p > ptr || filesize > 0) |
| 1988 | incomplete_tail = TRUE; |
| 1989 | /* Incomplete byte sequence, move it to conv_rest[] |
| 1990 | * and try to read the rest of it, unless we've |
| 1991 | * already done so. */ |
| 1992 | if (p > ptr) |
| 1993 | { |
| 1994 | conv_restlen = todo; |
| 1995 | mch_memmove(conv_rest, p, conv_restlen); |
| 1996 | size -= conv_restlen; |
| 1997 | break; |
| 1998 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1999 | } |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2000 | if (l == 1 || l > todo) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2001 | { |
| 2002 | /* Illegal byte. If we can try another encoding |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2003 | * do that, unless at EOF where a truncated |
| 2004 | * file is more likely than a conversion error. */ |
| 2005 | if (can_retry && !incomplete_tail) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2006 | break; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2007 | # ifdef USE_ICONV |
| 2008 | /* When we did a conversion report an error. */ |
| 2009 | if (iconv_fd != (iconv_t)-1 && conv_error == 0) |
| 2010 | conv_error = readfile_linenr(linecnt, ptr, p); |
| 2011 | # endif |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2012 | /* Remember the first linenr with an illegal byte */ |
| 2013 | if (conv_error == 0 && illegal_byte == 0) |
| 2014 | illegal_byte = readfile_linenr(linecnt, ptr, p); |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2015 | |
| 2016 | /* Drop, keep or replace the bad byte. */ |
| 2017 | if (bad_char_behavior == BAD_DROP) |
| 2018 | { |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2019 | mch_memmove(p, p + 1, todo - 1); |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2020 | --p; |
| 2021 | --size; |
| 2022 | } |
| 2023 | else if (bad_char_behavior != BAD_KEEP) |
| 2024 | *p = bad_char_behavior; |
| 2025 | } |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2026 | else |
| 2027 | p += l - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2028 | } |
| 2029 | } |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2030 | if (p < ptr + size && !incomplete_tail) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2031 | { |
| 2032 | /* Detected a UTF-8 error. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2033 | rewind_retry: |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2034 | /* Retry reading with another conversion. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2035 | # if defined(FEAT_EVAL) && defined(USE_ICONV) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2036 | if (*p_ccv != NUL && iconv_fd != (iconv_t)-1) |
| 2037 | /* iconv() failed, try 'charconvert' */ |
| 2038 | did_iconv = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2039 | else |
| 2040 | # endif |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2041 | /* use next item from 'fileencodings' */ |
| 2042 | advance_fenc = TRUE; |
| 2043 | file_rewind = TRUE; |
| 2044 | goto retry; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2045 | } |
| 2046 | } |
| 2047 | #endif |
| 2048 | |
| 2049 | /* count the number of characters (after conversion!) */ |
| 2050 | filesize += size; |
| 2051 | |
| 2052 | /* |
| 2053 | * when reading the first part of a file: guess EOL type |
| 2054 | */ |
| 2055 | if (fileformat == EOL_UNKNOWN) |
| 2056 | { |
| 2057 | /* First try finding a NL, for Dos and Unix */ |
| 2058 | if (try_dos || try_unix) |
| 2059 | { |
| 2060 | for (p = ptr; p < ptr + size; ++p) |
| 2061 | { |
| 2062 | if (*p == NL) |
| 2063 | { |
| 2064 | if (!try_unix |
| 2065 | || (try_dos && p > ptr && p[-1] == CAR)) |
| 2066 | fileformat = EOL_DOS; |
| 2067 | else |
| 2068 | fileformat = EOL_UNIX; |
| 2069 | break; |
| 2070 | } |
| 2071 | } |
| 2072 | |
| 2073 | /* Don't give in to EOL_UNIX if EOL_MAC is more likely */ |
| 2074 | if (fileformat == EOL_UNIX && try_mac) |
| 2075 | { |
| 2076 | /* Need to reset the counters when retrying fenc. */ |
| 2077 | try_mac = 1; |
| 2078 | try_unix = 1; |
| 2079 | for (; p >= ptr && *p != CAR; p--) |
| 2080 | ; |
| 2081 | if (p >= ptr) |
| 2082 | { |
| 2083 | for (p = ptr; p < ptr + size; ++p) |
| 2084 | { |
| 2085 | if (*p == NL) |
| 2086 | try_unix++; |
| 2087 | else if (*p == CAR) |
| 2088 | try_mac++; |
| 2089 | } |
| 2090 | if (try_mac > try_unix) |
| 2091 | fileformat = EOL_MAC; |
| 2092 | } |
| 2093 | } |
| 2094 | } |
| 2095 | |
| 2096 | /* No NL found: may use Mac format */ |
| 2097 | if (fileformat == EOL_UNKNOWN && try_mac) |
| 2098 | fileformat = EOL_MAC; |
| 2099 | |
| 2100 | /* Still nothing found? Use first format in 'ffs' */ |
| 2101 | if (fileformat == EOL_UNKNOWN) |
| 2102 | fileformat = default_fileformat(); |
| 2103 | |
| 2104 | /* if editing a new file: may set p_tx and p_ff */ |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2105 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2106 | set_fileformat(fileformat, OPT_LOCAL); |
| 2107 | } |
| 2108 | } |
| 2109 | |
| 2110 | /* |
| 2111 | * This loop is executed once for every character read. |
| 2112 | * Keep it fast! |
| 2113 | */ |
| 2114 | if (fileformat == EOL_MAC) |
| 2115 | { |
| 2116 | --ptr; |
| 2117 | while (++ptr, --size >= 0) |
| 2118 | { |
| 2119 | /* catch most common case first */ |
| 2120 | if ((c = *ptr) != NUL && c != CAR && c != NL) |
| 2121 | continue; |
| 2122 | if (c == NUL) |
| 2123 | *ptr = NL; /* NULs are replaced by newlines! */ |
| 2124 | else if (c == NL) |
| 2125 | *ptr = CAR; /* NLs are replaced by CRs! */ |
| 2126 | else |
| 2127 | { |
| 2128 | if (skip_count == 0) |
| 2129 | { |
| 2130 | *ptr = NUL; /* end of line */ |
| 2131 | len = (colnr_T) (ptr - line_start + 1); |
| 2132 | if (ml_append(lnum, line_start, len, newfile) == FAIL) |
| 2133 | { |
| 2134 | error = TRUE; |
| 2135 | break; |
| 2136 | } |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2137 | #ifdef FEAT_PERSISTENT_UNDO |
| 2138 | if (read_undo_file) |
| 2139 | sha256_update(&sha_ctx, line_start, len); |
| 2140 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2141 | ++lnum; |
| 2142 | if (--read_count == 0) |
| 2143 | { |
| 2144 | error = TRUE; /* break loop */ |
| 2145 | line_start = ptr; /* nothing left to write */ |
| 2146 | break; |
| 2147 | } |
| 2148 | } |
| 2149 | else |
| 2150 | --skip_count; |
| 2151 | line_start = ptr + 1; |
| 2152 | } |
| 2153 | } |
| 2154 | } |
| 2155 | else |
| 2156 | { |
| 2157 | --ptr; |
| 2158 | while (++ptr, --size >= 0) |
| 2159 | { |
| 2160 | if ((c = *ptr) != NUL && c != NL) /* catch most common case */ |
| 2161 | continue; |
| 2162 | if (c == NUL) |
| 2163 | *ptr = NL; /* NULs are replaced by newlines! */ |
| 2164 | else |
| 2165 | { |
| 2166 | if (skip_count == 0) |
| 2167 | { |
| 2168 | *ptr = NUL; /* end of line */ |
| 2169 | len = (colnr_T)(ptr - line_start + 1); |
| 2170 | if (fileformat == EOL_DOS) |
| 2171 | { |
| 2172 | if (ptr[-1] == CAR) /* remove CR */ |
| 2173 | { |
| 2174 | ptr[-1] = NUL; |
| 2175 | --len; |
| 2176 | } |
| 2177 | /* |
| 2178 | * Reading in Dos format, but no CR-LF found! |
| 2179 | * When 'fileformats' includes "unix", delete all |
| 2180 | * the lines read so far and start all over again. |
| 2181 | * Otherwise give an error message later. |
| 2182 | */ |
| 2183 | else if (ff_error != EOL_DOS) |
| 2184 | { |
| 2185 | if ( try_unix |
| 2186 | && !read_stdin |
| 2187 | && (read_buffer |
| 2188 | || lseek(fd, (off_t)0L, SEEK_SET) == 0)) |
| 2189 | { |
| 2190 | fileformat = EOL_UNIX; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2191 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2192 | set_fileformat(EOL_UNIX, OPT_LOCAL); |
| 2193 | file_rewind = TRUE; |
| 2194 | keep_fileformat = TRUE; |
| 2195 | goto retry; |
| 2196 | } |
| 2197 | ff_error = EOL_DOS; |
| 2198 | } |
| 2199 | } |
| 2200 | if (ml_append(lnum, line_start, len, newfile) == FAIL) |
| 2201 | { |
| 2202 | error = TRUE; |
| 2203 | break; |
| 2204 | } |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2205 | #ifdef FEAT_PERSISTENT_UNDO |
| 2206 | if (read_undo_file) |
| 2207 | sha256_update(&sha_ctx, line_start, len); |
| 2208 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2209 | ++lnum; |
| 2210 | if (--read_count == 0) |
| 2211 | { |
| 2212 | error = TRUE; /* break loop */ |
| 2213 | line_start = ptr; /* nothing left to write */ |
| 2214 | break; |
| 2215 | } |
| 2216 | } |
| 2217 | else |
| 2218 | --skip_count; |
| 2219 | line_start = ptr + 1; |
| 2220 | } |
| 2221 | } |
| 2222 | } |
| 2223 | linerest = (long)(ptr - line_start); |
| 2224 | ui_breakcheck(); |
| 2225 | } |
| 2226 | |
| 2227 | failed: |
| 2228 | /* not an error, max. number of lines reached */ |
| 2229 | if (error && read_count == 0) |
| 2230 | error = FALSE; |
| 2231 | |
| 2232 | /* |
| 2233 | * If we get EOF in the middle of a line, note the fact and |
| 2234 | * complete the line ourselves. |
| 2235 | * In Dos format ignore a trailing CTRL-Z, unless 'binary' set. |
| 2236 | */ |
| 2237 | if (!error |
| 2238 | && !got_int |
| 2239 | && linerest != 0 |
| 2240 | && !(!curbuf->b_p_bin |
| 2241 | && fileformat == EOL_DOS |
| 2242 | && *line_start == Ctrl_Z |
| 2243 | && ptr == line_start + 1)) |
| 2244 | { |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2245 | /* remember for when writing */ |
| 2246 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2247 | curbuf->b_p_eol = FALSE; |
| 2248 | *ptr = NUL; |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2249 | len = (colnr_T)(ptr - line_start + 1); |
| 2250 | if (ml_append(lnum, line_start, len, newfile) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2251 | error = TRUE; |
| 2252 | else |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2253 | { |
| 2254 | #ifdef FEAT_PERSISTENT_UNDO |
| 2255 | if (read_undo_file) |
| 2256 | sha256_update(&sha_ctx, line_start, len); |
| 2257 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2258 | read_no_eol_lnum = ++lnum; |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2259 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2262 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2263 | save_file_ff(curbuf); /* remember the current file format */ |
| 2264 | |
| 2265 | #ifdef FEAT_CRYPT |
Bram Moolenaar | 4cf35c2 | 2011-02-25 16:52:17 +0100 | [diff] [blame] | 2266 | crypt_method_used = use_crypt_method; |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 2267 | if (cryptkey != NULL) |
| 2268 | { |
| 2269 | crypt_pop_state(); |
| 2270 | if (cryptkey != curbuf->b_p_key) |
| 2271 | free_crypt_key(cryptkey); |
| 2272 | /* don't set cryptkey to NULL, it's used below as a flag that |
| 2273 | * encryption was used */ |
| 2274 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2275 | #endif |
| 2276 | |
| 2277 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2278 | /* If editing a new file: set 'fenc' for the current buffer. |
| 2279 | * Also for ":read ++edit file". */ |
| 2280 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2281 | set_string_option_direct((char_u *)"fenc", -1, fenc, |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 2282 | OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2283 | if (fenc_alloced) |
| 2284 | vim_free(fenc); |
| 2285 | # ifdef USE_ICONV |
| 2286 | if (iconv_fd != (iconv_t)-1) |
| 2287 | { |
| 2288 | iconv_close(iconv_fd); |
| 2289 | iconv_fd = (iconv_t)-1; |
| 2290 | } |
| 2291 | # endif |
| 2292 | #endif |
| 2293 | |
| 2294 | if (!read_buffer && !read_stdin) |
| 2295 | close(fd); /* errors are ignored */ |
Bram Moolenaar | f05da21 | 2009-11-17 16:13:15 +0000 | [diff] [blame] | 2296 | #ifdef HAVE_FD_CLOEXEC |
| 2297 | else |
| 2298 | { |
| 2299 | int fdflags = fcntl(fd, F_GETFD); |
| 2300 | if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0) |
| 2301 | fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC); |
| 2302 | } |
| 2303 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2304 | vim_free(buffer); |
| 2305 | |
| 2306 | #ifdef HAVE_DUP |
| 2307 | if (read_stdin) |
| 2308 | { |
| 2309 | /* Use stderr for stdin, makes shell commands work. */ |
| 2310 | close(0); |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 2311 | ignored = dup(2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2312 | } |
| 2313 | #endif |
| 2314 | |
| 2315 | #ifdef FEAT_MBYTE |
| 2316 | if (tmpname != NULL) |
| 2317 | { |
| 2318 | mch_remove(tmpname); /* delete converted file */ |
| 2319 | vim_free(tmpname); |
| 2320 | } |
| 2321 | #endif |
| 2322 | --no_wait_return; /* may wait for return now */ |
| 2323 | |
| 2324 | /* |
| 2325 | * In recovery mode everything but autocommands is skipped. |
| 2326 | */ |
| 2327 | if (!recoverymode) |
| 2328 | { |
| 2329 | /* need to delete the last line, which comes from the empty buffer */ |
| 2330 | if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY)) |
| 2331 | { |
| 2332 | #ifdef FEAT_NETBEANS_INTG |
| 2333 | netbeansFireChanges = 0; |
| 2334 | #endif |
| 2335 | ml_delete(curbuf->b_ml.ml_line_count, FALSE); |
| 2336 | #ifdef FEAT_NETBEANS_INTG |
| 2337 | netbeansFireChanges = 1; |
| 2338 | #endif |
| 2339 | --linecnt; |
| 2340 | } |
| 2341 | linecnt = curbuf->b_ml.ml_line_count - linecnt; |
| 2342 | if (filesize == 0) |
| 2343 | linecnt = 0; |
| 2344 | if (newfile || read_buffer) |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 2345 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2346 | redraw_curbuf_later(NOT_VALID); |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 2347 | #ifdef FEAT_DIFF |
| 2348 | /* After reading the text into the buffer the diff info needs to |
| 2349 | * be updated. */ |
| 2350 | diff_invalidate(curbuf); |
| 2351 | #endif |
| 2352 | #ifdef FEAT_FOLDING |
| 2353 | /* All folds in the window are invalid now. Mark them for update |
| 2354 | * before triggering autocommands. */ |
| 2355 | foldUpdateAll(curwin); |
| 2356 | #endif |
| 2357 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2358 | else if (linecnt) /* appended at least one line */ |
| 2359 | appended_lines_mark(from, linecnt); |
| 2360 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2361 | #ifndef ALWAYS_USE_GUI |
| 2362 | /* |
| 2363 | * If we were reading from the same terminal as where messages go, |
| 2364 | * the screen will have been messed up. |
| 2365 | * Switch on raw mode now and clear the screen. |
| 2366 | */ |
| 2367 | if (read_stdin) |
| 2368 | { |
| 2369 | settmode(TMODE_RAW); /* set to raw mode */ |
| 2370 | starttermcap(); |
| 2371 | screenclear(); |
| 2372 | } |
| 2373 | #endif |
| 2374 | |
| 2375 | if (got_int) |
| 2376 | { |
| 2377 | if (!(flags & READ_DUMMY)) |
| 2378 | { |
| 2379 | filemess(curbuf, sfname, (char_u *)_(e_interr), 0); |
| 2380 | if (newfile) |
| 2381 | curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
| 2382 | } |
| 2383 | msg_scroll = msg_save; |
| 2384 | #ifdef FEAT_VIMINFO |
| 2385 | check_marks_read(); |
| 2386 | #endif |
| 2387 | return OK; /* an interrupt isn't really an error */ |
| 2388 | } |
| 2389 | |
| 2390 | if (!filtering && !(flags & READ_DUMMY)) |
| 2391 | { |
| 2392 | msg_add_fname(curbuf, sfname); /* fname in IObuff with quotes */ |
| 2393 | c = FALSE; |
| 2394 | |
| 2395 | #ifdef UNIX |
| 2396 | # ifdef S_ISFIFO |
| 2397 | if (S_ISFIFO(perm)) /* fifo or socket */ |
| 2398 | { |
| 2399 | STRCAT(IObuff, _("[fifo/socket]")); |
| 2400 | c = TRUE; |
| 2401 | } |
| 2402 | # else |
| 2403 | # ifdef S_IFIFO |
| 2404 | if ((perm & S_IFMT) == S_IFIFO) /* fifo */ |
| 2405 | { |
| 2406 | STRCAT(IObuff, _("[fifo]")); |
| 2407 | c = TRUE; |
| 2408 | } |
| 2409 | # endif |
| 2410 | # ifdef S_IFSOCK |
| 2411 | if ((perm & S_IFMT) == S_IFSOCK) /* or socket */ |
| 2412 | { |
| 2413 | STRCAT(IObuff, _("[socket]")); |
| 2414 | c = TRUE; |
| 2415 | } |
| 2416 | # endif |
| 2417 | # endif |
Bram Moolenaar | fe1c56d | 2007-07-10 15:10:54 +0000 | [diff] [blame] | 2418 | # ifdef OPEN_CHR_FILES |
| 2419 | if (S_ISCHR(perm)) /* or character special */ |
| 2420 | { |
| 2421 | STRCAT(IObuff, _("[character special]")); |
| 2422 | c = TRUE; |
| 2423 | } |
| 2424 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2425 | #endif |
| 2426 | if (curbuf->b_p_ro) |
| 2427 | { |
| 2428 | STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")); |
| 2429 | c = TRUE; |
| 2430 | } |
| 2431 | if (read_no_eol_lnum) |
| 2432 | { |
| 2433 | msg_add_eol(); |
| 2434 | c = TRUE; |
| 2435 | } |
| 2436 | if (ff_error == EOL_DOS) |
| 2437 | { |
| 2438 | STRCAT(IObuff, _("[CR missing]")); |
| 2439 | c = TRUE; |
| 2440 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2441 | if (split) |
| 2442 | { |
| 2443 | STRCAT(IObuff, _("[long lines split]")); |
| 2444 | c = TRUE; |
| 2445 | } |
| 2446 | #ifdef FEAT_MBYTE |
| 2447 | if (notconverted) |
| 2448 | { |
| 2449 | STRCAT(IObuff, _("[NOT converted]")); |
| 2450 | c = TRUE; |
| 2451 | } |
| 2452 | else if (converted) |
| 2453 | { |
| 2454 | STRCAT(IObuff, _("[converted]")); |
| 2455 | c = TRUE; |
| 2456 | } |
| 2457 | #endif |
| 2458 | #ifdef FEAT_CRYPT |
| 2459 | if (cryptkey != NULL) |
| 2460 | { |
Bram Moolenaar | 4cf35c2 | 2011-02-25 16:52:17 +0100 | [diff] [blame] | 2461 | if (crypt_method_used == 1) |
| 2462 | STRCAT(IObuff, _("[blowfish]")); |
| 2463 | else |
| 2464 | STRCAT(IObuff, _("[crypted]")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2465 | c = TRUE; |
| 2466 | } |
| 2467 | #endif |
| 2468 | #ifdef FEAT_MBYTE |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2469 | if (conv_error != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2470 | { |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2471 | sprintf((char *)IObuff + STRLEN(IObuff), |
| 2472 | _("[CONVERSION ERROR in line %ld]"), (long)conv_error); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2473 | c = TRUE; |
| 2474 | } |
| 2475 | else if (illegal_byte > 0) |
| 2476 | { |
| 2477 | sprintf((char *)IObuff + STRLEN(IObuff), |
| 2478 | _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte); |
| 2479 | c = TRUE; |
| 2480 | } |
| 2481 | else |
| 2482 | #endif |
| 2483 | if (error) |
| 2484 | { |
| 2485 | STRCAT(IObuff, _("[READ ERRORS]")); |
| 2486 | c = TRUE; |
| 2487 | } |
| 2488 | if (msg_add_fileformat(fileformat)) |
| 2489 | c = TRUE; |
| 2490 | #ifdef FEAT_CRYPT |
| 2491 | if (cryptkey != NULL) |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2492 | msg_add_lines(c, (long)linecnt, filesize |
Bram Moolenaar | 80794b1 | 2010-06-13 05:20:42 +0200 | [diff] [blame] | 2493 | - CRYPT_MAGIC_LEN |
| 2494 | - crypt_salt_len[use_crypt_method] |
| 2495 | - crypt_seed_len[use_crypt_method]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2496 | else |
| 2497 | #endif |
| 2498 | msg_add_lines(c, (long)linecnt, filesize); |
| 2499 | |
| 2500 | vim_free(keep_msg); |
| 2501 | keep_msg = NULL; |
| 2502 | msg_scrolled_ign = TRUE; |
| 2503 | #ifdef ALWAYS_USE_GUI |
| 2504 | /* Don't show the message when reading stdin, it would end up in a |
| 2505 | * message box (which might be shown when exiting!) */ |
| 2506 | if (read_stdin || read_buffer) |
| 2507 | p = msg_may_trunc(FALSE, IObuff); |
| 2508 | else |
| 2509 | #endif |
| 2510 | p = msg_trunc_attr(IObuff, FALSE, 0); |
| 2511 | if (read_stdin || read_buffer || restart_edit != 0 |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2512 | || (msg_scrolled != 0 && !need_wait_return)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2513 | /* Need to repeat the message after redrawing when: |
| 2514 | * - When reading from stdin (the screen will be cleared next). |
| 2515 | * - When restart_edit is set (otherwise there will be a delay |
| 2516 | * before redrawing). |
| 2517 | * - When the screen was scrolled but there is no wait-return |
| 2518 | * prompt. */ |
Bram Moolenaar | 8f7fd65 | 2006-02-21 22:04:51 +0000 | [diff] [blame] | 2519 | set_keep_msg(p, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2520 | msg_scrolled_ign = FALSE; |
| 2521 | } |
| 2522 | |
| 2523 | /* with errors writing the file requires ":w!" */ |
| 2524 | if (newfile && (error |
| 2525 | #ifdef FEAT_MBYTE |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2526 | || conv_error != 0 |
Bram Moolenaar | 8f7fd65 | 2006-02-21 22:04:51 +0000 | [diff] [blame] | 2527 | || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2528 | #endif |
| 2529 | )) |
| 2530 | curbuf->b_p_ro = TRUE; |
| 2531 | |
| 2532 | u_clearline(); /* cannot use "U" command after adding lines */ |
| 2533 | |
| 2534 | /* |
| 2535 | * In Ex mode: cursor at last new line. |
| 2536 | * Otherwise: cursor at first new line. |
| 2537 | */ |
| 2538 | if (exmode_active) |
| 2539 | curwin->w_cursor.lnum = from + linecnt; |
| 2540 | else |
| 2541 | curwin->w_cursor.lnum = from + 1; |
| 2542 | check_cursor_lnum(); |
| 2543 | beginline(BL_WHITE | BL_FIX); /* on first non-blank */ |
| 2544 | |
| 2545 | /* |
| 2546 | * Set '[ and '] marks to the newly read lines. |
| 2547 | */ |
| 2548 | curbuf->b_op_start.lnum = from + 1; |
| 2549 | curbuf->b_op_start.col = 0; |
| 2550 | curbuf->b_op_end.lnum = from + linecnt; |
| 2551 | curbuf->b_op_end.col = 0; |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 2552 | |
| 2553 | #ifdef WIN32 |
| 2554 | /* |
| 2555 | * Work around a weird problem: When a file has two links (only |
| 2556 | * possible on NTFS) and we write through one link, then stat() it |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 2557 | * through the other link, the timestamp information may be wrong. |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 2558 | * It's correct again after reading the file, thus reset the timestamp |
| 2559 | * here. |
| 2560 | */ |
| 2561 | if (newfile && !read_stdin && !read_buffer |
| 2562 | && mch_stat((char *)fname, &st) >= 0) |
| 2563 | { |
| 2564 | buf_store_time(curbuf, &st, fname); |
| 2565 | curbuf->b_mtime_read = curbuf->b_mtime; |
| 2566 | } |
| 2567 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2568 | } |
| 2569 | msg_scroll = msg_save; |
| 2570 | |
| 2571 | #ifdef FEAT_VIMINFO |
| 2572 | /* |
| 2573 | * Get the marks before executing autocommands, so they can be used there. |
| 2574 | */ |
| 2575 | check_marks_read(); |
| 2576 | #endif |
| 2577 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2578 | /* |
| 2579 | * Trick: We remember if the last line of the read didn't have |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 2580 | * an eol even when 'binary' is off, for when writing it again with |
| 2581 | * 'binary' on. This is required for |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2582 | * ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work. |
| 2583 | */ |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 2584 | curbuf->b_no_eol_lnum = read_no_eol_lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2585 | |
Bram Moolenaar | f9bb734 | 2010-08-04 14:29:54 +0200 | [diff] [blame] | 2586 | /* When reloading a buffer put the cursor at the first line that is |
| 2587 | * different. */ |
| 2588 | if (flags & READ_KEEP_UNDO) |
| 2589 | u_find_first_changed(); |
| 2590 | |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2591 | #ifdef FEAT_PERSISTENT_UNDO |
| 2592 | /* |
| 2593 | * When opening a new file locate undo info and read it. |
| 2594 | */ |
| 2595 | if (read_undo_file) |
| 2596 | { |
| 2597 | char_u hash[UNDO_HASH_SIZE]; |
| 2598 | |
| 2599 | sha256_finish(&sha_ctx, hash); |
Bram Moolenaar | 6ed8ed8 | 2010-05-30 20:40:11 +0200 | [diff] [blame] | 2600 | u_read_undo(NULL, hash, fname); |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2601 | } |
| 2602 | #endif |
| 2603 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2604 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2605 | if (!read_stdin && !read_buffer) |
| 2606 | { |
| 2607 | int m = msg_scroll; |
| 2608 | int n = msg_scrolled; |
| 2609 | |
| 2610 | /* Save the fileformat now, otherwise the buffer will be considered |
| 2611 | * modified if the format/encoding was automatically detected. */ |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2612 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2613 | save_file_ff(curbuf); |
| 2614 | |
| 2615 | /* |
| 2616 | * The output from the autocommands should not overwrite anything and |
| 2617 | * should not be overwritten: Set msg_scroll, restore its value if no |
| 2618 | * output was done. |
| 2619 | */ |
| 2620 | msg_scroll = TRUE; |
| 2621 | if (filtering) |
| 2622 | apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname, |
| 2623 | FALSE, curbuf, eap); |
| 2624 | else if (newfile) |
| 2625 | apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname, |
| 2626 | FALSE, curbuf, eap); |
| 2627 | else |
| 2628 | apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname, |
| 2629 | FALSE, NULL, eap); |
| 2630 | if (msg_scrolled == n) |
| 2631 | msg_scroll = m; |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 2632 | # ifdef FEAT_EVAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2633 | if (aborting()) /* autocmds may abort script processing */ |
| 2634 | return FAIL; |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 2635 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2636 | } |
| 2637 | #endif |
| 2638 | |
| 2639 | if (recoverymode && error) |
| 2640 | return FAIL; |
| 2641 | return OK; |
| 2642 | } |
| 2643 | |
Bram Moolenaar | fe1c56d | 2007-07-10 15:10:54 +0000 | [diff] [blame] | 2644 | #ifdef OPEN_CHR_FILES |
| 2645 | /* |
| 2646 | * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+", |
| 2647 | * which is the name of files used for process substitution output by |
| 2648 | * some shells on some operating systems, e.g., bash on SunOS. |
| 2649 | * Do not accept "/dev/fd/[012]", opening these may hang Vim. |
| 2650 | */ |
| 2651 | static int |
| 2652 | is_dev_fd_file(fname) |
| 2653 | char_u *fname; |
| 2654 | { |
| 2655 | return (STRNCMP(fname, "/dev/fd/", 8) == 0 |
| 2656 | && VIM_ISDIGIT(fname[8]) |
| 2657 | && *skipdigits(fname + 9) == NUL |
| 2658 | && (fname[9] != NUL |
| 2659 | || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2'))); |
| 2660 | } |
| 2661 | #endif |
| 2662 | |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2663 | #ifdef FEAT_MBYTE |
| 2664 | |
| 2665 | /* |
| 2666 | * From the current line count and characters read after that, estimate the |
| 2667 | * line number where we are now. |
| 2668 | * Used for error messages that include a line number. |
| 2669 | */ |
| 2670 | static linenr_T |
| 2671 | readfile_linenr(linecnt, p, endp) |
| 2672 | linenr_T linecnt; /* line count before reading more bytes */ |
| 2673 | char_u *p; /* start of more bytes read */ |
| 2674 | char_u *endp; /* end of more bytes read */ |
| 2675 | { |
| 2676 | char_u *s; |
| 2677 | linenr_T lnum; |
| 2678 | |
| 2679 | lnum = curbuf->b_ml.ml_line_count - linecnt + 1; |
| 2680 | for (s = p; s < endp; ++s) |
| 2681 | if (*s == '\n') |
| 2682 | ++lnum; |
| 2683 | return lnum; |
| 2684 | } |
| 2685 | #endif |
| 2686 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2687 | /* |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 2688 | * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be |
| 2689 | * equal to the buffer "buf". Used for calling readfile(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2690 | * Returns OK or FAIL. |
| 2691 | */ |
| 2692 | int |
| 2693 | prep_exarg(eap, buf) |
| 2694 | exarg_T *eap; |
| 2695 | buf_T *buf; |
| 2696 | { |
| 2697 | eap->cmd = alloc((unsigned)(STRLEN(buf->b_p_ff) |
| 2698 | #ifdef FEAT_MBYTE |
| 2699 | + STRLEN(buf->b_p_fenc) |
| 2700 | #endif |
| 2701 | + 15)); |
| 2702 | if (eap->cmd == NULL) |
| 2703 | return FAIL; |
| 2704 | |
| 2705 | #ifdef FEAT_MBYTE |
| 2706 | sprintf((char *)eap->cmd, "e ++ff=%s ++enc=%s", buf->b_p_ff, buf->b_p_fenc); |
| 2707 | eap->force_enc = 14 + (int)STRLEN(buf->b_p_ff); |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 2708 | eap->bad_char = buf->b_bad_char; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2709 | #else |
| 2710 | sprintf((char *)eap->cmd, "e ++ff=%s", buf->b_p_ff); |
| 2711 | #endif |
| 2712 | eap->force_ff = 7; |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 2713 | |
| 2714 | eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2715 | eap->read_edit = FALSE; |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 2716 | eap->forceit = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2717 | return OK; |
| 2718 | } |
| 2719 | |
Bram Moolenaar | ad875fb | 2013-07-24 15:02:03 +0200 | [diff] [blame] | 2720 | /* |
| 2721 | * Set default or forced 'fileformat' and 'binary'. |
| 2722 | */ |
| 2723 | void |
| 2724 | set_file_options(set_options, eap) |
| 2725 | int set_options; |
| 2726 | exarg_T *eap; |
| 2727 | { |
| 2728 | /* set default 'fileformat' */ |
| 2729 | if (set_options) |
| 2730 | { |
| 2731 | if (eap != NULL && eap->force_ff != 0) |
| 2732 | set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL); |
| 2733 | else if (*p_ffs != NUL) |
| 2734 | set_fileformat(default_fileformat(), OPT_LOCAL); |
| 2735 | } |
| 2736 | |
| 2737 | /* set or reset 'binary' */ |
| 2738 | if (eap != NULL && eap->force_bin != 0) |
| 2739 | { |
| 2740 | int oldval = curbuf->b_p_bin; |
| 2741 | |
| 2742 | curbuf->b_p_bin = (eap->force_bin == FORCE_BIN); |
| 2743 | set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL); |
| 2744 | } |
| 2745 | } |
| 2746 | |
| 2747 | #if defined(FEAT_MBYTE) || defined(PROTO) |
| 2748 | /* |
| 2749 | * Set forced 'fileencoding'. |
| 2750 | */ |
| 2751 | void |
| 2752 | set_forced_fenc(eap) |
| 2753 | exarg_T *eap; |
| 2754 | { |
| 2755 | if (eap->force_enc != 0) |
| 2756 | { |
| 2757 | char_u *fenc = enc_canonize(eap->cmd + eap->force_enc); |
| 2758 | |
| 2759 | if (fenc != NULL) |
| 2760 | set_string_option_direct((char_u *)"fenc", -1, |
| 2761 | fenc, OPT_FREE|OPT_LOCAL, 0); |
| 2762 | vim_free(fenc); |
| 2763 | } |
| 2764 | } |
| 2765 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2766 | /* |
| 2767 | * Find next fileencoding to use from 'fileencodings'. |
| 2768 | * "pp" points to fenc_next. It's advanced to the next item. |
| 2769 | * When there are no more items, an empty string is returned and *pp is set to |
| 2770 | * NULL. |
| 2771 | * When *pp is not set to NULL, the result is in allocated memory. |
| 2772 | */ |
| 2773 | static char_u * |
| 2774 | next_fenc(pp) |
| 2775 | char_u **pp; |
| 2776 | { |
| 2777 | char_u *p; |
| 2778 | char_u *r; |
| 2779 | |
| 2780 | if (**pp == NUL) |
| 2781 | { |
| 2782 | *pp = NULL; |
| 2783 | return (char_u *)""; |
| 2784 | } |
| 2785 | p = vim_strchr(*pp, ','); |
| 2786 | if (p == NULL) |
| 2787 | { |
| 2788 | r = enc_canonize(*pp); |
| 2789 | *pp += STRLEN(*pp); |
| 2790 | } |
| 2791 | else |
| 2792 | { |
| 2793 | r = vim_strnsave(*pp, (int)(p - *pp)); |
| 2794 | *pp = p + 1; |
| 2795 | if (r != NULL) |
| 2796 | { |
| 2797 | p = enc_canonize(r); |
| 2798 | vim_free(r); |
| 2799 | r = p; |
| 2800 | } |
| 2801 | } |
| 2802 | if (r == NULL) /* out of memory */ |
| 2803 | { |
| 2804 | r = (char_u *)""; |
| 2805 | *pp = NULL; |
| 2806 | } |
| 2807 | return r; |
| 2808 | } |
| 2809 | |
| 2810 | # ifdef FEAT_EVAL |
| 2811 | /* |
| 2812 | * Convert a file with the 'charconvert' expression. |
| 2813 | * This closes the file which is to be read, converts it and opens the |
| 2814 | * resulting file for reading. |
| 2815 | * Returns name of the resulting converted file (the caller should delete it |
| 2816 | * after reading it). |
| 2817 | * Returns NULL if the conversion failed ("*fdp" is not set) . |
| 2818 | */ |
| 2819 | static char_u * |
| 2820 | readfile_charconvert(fname, fenc, fdp) |
| 2821 | char_u *fname; /* name of input file */ |
| 2822 | char_u *fenc; /* converted from */ |
| 2823 | int *fdp; /* in/out: file descriptor of file */ |
| 2824 | { |
| 2825 | char_u *tmpname; |
| 2826 | char_u *errmsg = NULL; |
| 2827 | |
| 2828 | tmpname = vim_tempname('r'); |
| 2829 | if (tmpname == NULL) |
| 2830 | errmsg = (char_u *)_("Can't find temp file for conversion"); |
| 2831 | else |
| 2832 | { |
| 2833 | close(*fdp); /* close the input file, ignore errors */ |
| 2834 | *fdp = -1; |
| 2835 | if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc, |
| 2836 | fname, tmpname) == FAIL) |
| 2837 | errmsg = (char_u *)_("Conversion with 'charconvert' failed"); |
| 2838 | if (errmsg == NULL && (*fdp = mch_open((char *)tmpname, |
| 2839 | O_RDONLY | O_EXTRA, 0)) < 0) |
| 2840 | errmsg = (char_u *)_("can't read output of 'charconvert'"); |
| 2841 | } |
| 2842 | |
| 2843 | if (errmsg != NULL) |
| 2844 | { |
| 2845 | /* Don't use emsg(), it breaks mappings, the retry with |
| 2846 | * another type of conversion might still work. */ |
| 2847 | MSG(errmsg); |
| 2848 | if (tmpname != NULL) |
| 2849 | { |
| 2850 | mch_remove(tmpname); /* delete converted file */ |
| 2851 | vim_free(tmpname); |
| 2852 | tmpname = NULL; |
| 2853 | } |
| 2854 | } |
| 2855 | |
| 2856 | /* If the input file is closed, open it (caller should check for error). */ |
| 2857 | if (*fdp < 0) |
| 2858 | *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); |
| 2859 | |
| 2860 | return tmpname; |
| 2861 | } |
| 2862 | # endif |
| 2863 | |
| 2864 | #endif |
| 2865 | |
| 2866 | #ifdef FEAT_VIMINFO |
| 2867 | /* |
| 2868 | * Read marks for the current buffer from the viminfo file, when we support |
| 2869 | * buffer marks and the buffer has a name. |
| 2870 | */ |
| 2871 | static void |
| 2872 | check_marks_read() |
| 2873 | { |
| 2874 | if (!curbuf->b_marks_read && get_viminfo_parameter('\'') > 0 |
| 2875 | && curbuf->b_ffname != NULL) |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 2876 | read_viminfo(NULL, VIF_WANT_MARKS); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2877 | |
| 2878 | /* Always set b_marks_read; needed when 'viminfo' is changed to include |
| 2879 | * the ' parameter after opening a buffer. */ |
| 2880 | curbuf->b_marks_read = TRUE; |
| 2881 | } |
| 2882 | #endif |
| 2883 | |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 2884 | #if defined(FEAT_CRYPT) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2885 | /* |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2886 | * Get the crypt method used for a file from "ptr[len]", the magic text at the |
| 2887 | * start of the file. |
| 2888 | * Returns -1 when no encryption used. |
| 2889 | */ |
| 2890 | static int |
Bram Moolenaar | 49771f4 | 2010-07-20 17:32:38 +0200 | [diff] [blame] | 2891 | crypt_method_from_magic(ptr, len) |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2892 | char *ptr; |
| 2893 | int len; |
| 2894 | { |
| 2895 | int i; |
| 2896 | |
| 2897 | for (i = 0; i < (int)(sizeof(crypt_magic) / sizeof(crypt_magic[0])); i++) |
| 2898 | { |
Bram Moolenaar | 80794b1 | 2010-06-13 05:20:42 +0200 | [diff] [blame] | 2899 | if (len < (CRYPT_MAGIC_LEN + crypt_salt_len[i] + crypt_seed_len[i])) |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2900 | continue; |
| 2901 | if (memcmp(ptr, crypt_magic[i], CRYPT_MAGIC_LEN) == 0) |
| 2902 | return i; |
| 2903 | } |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 2904 | |
Bram Moolenaar | 442b422 | 2010-05-24 21:34:22 +0200 | [diff] [blame] | 2905 | i = (int)STRLEN(crypt_magic_head); |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 2906 | if (len >= i && memcmp(ptr, crypt_magic_head, i) == 0) |
| 2907 | EMSG(_("E821: File is encrypted with unknown method")); |
| 2908 | |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2909 | return -1; |
| 2910 | } |
| 2911 | |
| 2912 | /* |
| 2913 | * Check for magic number used for encryption. Applies to the current buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2914 | * If found, the magic number is removed from ptr[*sizep] and *sizep and |
| 2915 | * *filesizep are updated. |
| 2916 | * Return the (new) encryption key, NULL for no encryption. |
| 2917 | */ |
| 2918 | static char_u * |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 2919 | check_for_cryptkey(cryptkey, ptr, sizep, filesizep, newfile, fname, did_ask) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2920 | char_u *cryptkey; /* previous encryption key or NULL */ |
| 2921 | char_u *ptr; /* pointer to read bytes */ |
| 2922 | long *sizep; /* length of read bytes */ |
Bram Moolenaar | 914703b | 2010-05-31 21:59:46 +0200 | [diff] [blame] | 2923 | off_t *filesizep; /* nr of bytes used from file */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2924 | int newfile; /* editing a new buffer */ |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 2925 | char_u *fname; /* file name to display */ |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 2926 | int *did_ask; /* flag: whether already asked for key */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2927 | { |
Bram Moolenaar | 49771f4 | 2010-07-20 17:32:38 +0200 | [diff] [blame] | 2928 | int method = crypt_method_from_magic((char *)ptr, *sizep); |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2929 | |
| 2930 | if (method >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2931 | { |
Bram Moolenaar | 49771f4 | 2010-07-20 17:32:38 +0200 | [diff] [blame] | 2932 | set_crypt_method(curbuf, method); |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2933 | if (method > 0) |
| 2934 | (void)blowfish_self_test(); |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 2935 | if (cryptkey == NULL && !*did_ask) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2936 | { |
| 2937 | if (*curbuf->b_p_key) |
| 2938 | cryptkey = curbuf->b_p_key; |
| 2939 | else |
| 2940 | { |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2941 | /* When newfile is TRUE, store the typed key in the 'key' |
| 2942 | * 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] | 2943 | * Don't ask for the key again when first time Enter was hit. |
| 2944 | * Happens when retrying to detect encoding. */ |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 2945 | smsg((char_u *)_(need_key_msg), fname); |
| 2946 | msg_scroll = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2947 | cryptkey = get_crypt_key(newfile, FALSE); |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 2948 | *did_ask = TRUE; |
| 2949 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2950 | /* check if empty key entered */ |
| 2951 | if (cryptkey != NULL && *cryptkey == NUL) |
| 2952 | { |
| 2953 | if (cryptkey != curbuf->b_p_key) |
| 2954 | vim_free(cryptkey); |
| 2955 | cryptkey = NULL; |
| 2956 | } |
| 2957 | } |
| 2958 | } |
| 2959 | |
| 2960 | if (cryptkey != NULL) |
| 2961 | { |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2962 | int seed_len = crypt_seed_len[method]; |
Bram Moolenaar | 80794b1 | 2010-06-13 05:20:42 +0200 | [diff] [blame] | 2963 | int salt_len = crypt_salt_len[method]; |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2964 | |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 2965 | crypt_push_state(); |
| 2966 | use_crypt_method = method; |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2967 | if (method == 0) |
| 2968 | crypt_init_keys(cryptkey); |
| 2969 | else |
| 2970 | { |
Bram Moolenaar | 80794b1 | 2010-06-13 05:20:42 +0200 | [diff] [blame] | 2971 | bf_key_init(cryptkey, ptr + CRYPT_MAGIC_LEN, salt_len); |
| 2972 | bf_ofb_init(ptr + CRYPT_MAGIC_LEN + salt_len, seed_len); |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2973 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2974 | |
| 2975 | /* Remove magic number from the text */ |
Bram Moolenaar | 80794b1 | 2010-06-13 05:20:42 +0200 | [diff] [blame] | 2976 | *filesizep += CRYPT_MAGIC_LEN + salt_len + seed_len; |
| 2977 | *sizep -= CRYPT_MAGIC_LEN + salt_len + seed_len; |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 2978 | mch_memmove(ptr, ptr + CRYPT_MAGIC_LEN + salt_len + seed_len, |
| 2979 | (size_t)*sizep); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2980 | } |
| 2981 | } |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2982 | /* When starting to edit a new file which does not have encryption, clear |
| 2983 | * the 'key' option, except when starting up (called with -x argument) */ |
Bram Moolenaar | fa0ff9a | 2010-07-25 16:05:19 +0200 | [diff] [blame] | 2984 | else if (newfile && *curbuf->b_p_key != NUL && !starting) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2985 | set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL); |
| 2986 | |
| 2987 | return cryptkey; |
| 2988 | } |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 2989 | |
| 2990 | /* |
| 2991 | * Check for magic number used for encryption. Applies to the current buffer. |
| 2992 | * If found and decryption is possible returns OK; |
| 2993 | */ |
| 2994 | int |
| 2995 | prepare_crypt_read(fp) |
| 2996 | FILE *fp; |
| 2997 | { |
| 2998 | int method; |
Bram Moolenaar | 80794b1 | 2010-06-13 05:20:42 +0200 | [diff] [blame] | 2999 | char_u buffer[CRYPT_MAGIC_LEN + CRYPT_SALT_LEN_MAX |
| 3000 | + CRYPT_SEED_LEN_MAX + 2]; |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 3001 | |
| 3002 | if (fread(buffer, CRYPT_MAGIC_LEN, 1, fp) != 1) |
| 3003 | return FAIL; |
Bram Moolenaar | 49771f4 | 2010-07-20 17:32:38 +0200 | [diff] [blame] | 3004 | method = crypt_method_from_magic((char *)buffer, |
Bram Moolenaar | 80794b1 | 2010-06-13 05:20:42 +0200 | [diff] [blame] | 3005 | CRYPT_MAGIC_LEN + |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 3006 | CRYPT_SEED_LEN_MAX + |
| 3007 | CRYPT_SALT_LEN_MAX); |
Bram Moolenaar | 49771f4 | 2010-07-20 17:32:38 +0200 | [diff] [blame] | 3008 | if (method < 0 || method != get_crypt_method(curbuf)) |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 3009 | return FAIL; |
| 3010 | |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 3011 | crypt_push_state(); |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 3012 | if (method == 0) |
| 3013 | crypt_init_keys(curbuf->b_p_key); |
| 3014 | else |
| 3015 | { |
Bram Moolenaar | 80794b1 | 2010-06-13 05:20:42 +0200 | [diff] [blame] | 3016 | int salt_len = crypt_salt_len[method]; |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 3017 | int seed_len = crypt_seed_len[method]; |
| 3018 | |
Bram Moolenaar | 80794b1 | 2010-06-13 05:20:42 +0200 | [diff] [blame] | 3019 | if (fread(buffer, salt_len + seed_len, 1, fp) != 1) |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 3020 | return FAIL; |
Bram Moolenaar | 80794b1 | 2010-06-13 05:20:42 +0200 | [diff] [blame] | 3021 | bf_key_init(curbuf->b_p_key, buffer, salt_len); |
| 3022 | bf_ofb_init(buffer + salt_len, seed_len); |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 3023 | } |
| 3024 | return OK; |
| 3025 | } |
| 3026 | |
| 3027 | /* |
| 3028 | * Prepare for writing encrypted bytes for buffer "buf". |
| 3029 | * Returns a pointer to an allocated header of length "*lenp". |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 3030 | * When out of memory returns NULL. |
| 3031 | * Otherwise calls crypt_push_state(), call crypt_pop_state() later. |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 3032 | */ |
| 3033 | char_u * |
| 3034 | prepare_crypt_write(buf, lenp) |
| 3035 | buf_T *buf; |
| 3036 | int *lenp; |
| 3037 | { |
| 3038 | char_u *header; |
Bram Moolenaar | 49771f4 | 2010-07-20 17:32:38 +0200 | [diff] [blame] | 3039 | int seed_len = crypt_seed_len[get_crypt_method(buf)]; |
| 3040 | int salt_len = crypt_salt_len[get_crypt_method(buf)]; |
Bram Moolenaar | 80794b1 | 2010-06-13 05:20:42 +0200 | [diff] [blame] | 3041 | char_u *salt; |
| 3042 | char_u *seed; |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 3043 | |
Bram Moolenaar | 80794b1 | 2010-06-13 05:20:42 +0200 | [diff] [blame] | 3044 | header = alloc_clear(CRYPT_MAGIC_LEN + CRYPT_SALT_LEN_MAX |
| 3045 | + CRYPT_SEED_LEN_MAX + 2); |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 3046 | if (header != NULL) |
| 3047 | { |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 3048 | crypt_push_state(); |
Bram Moolenaar | 49771f4 | 2010-07-20 17:32:38 +0200 | [diff] [blame] | 3049 | use_crypt_method = get_crypt_method(buf); /* select zip or blowfish */ |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 3050 | vim_strncpy(header, (char_u *)crypt_magic[use_crypt_method], |
| 3051 | CRYPT_MAGIC_LEN); |
Bram Moolenaar | 49771f4 | 2010-07-20 17:32:38 +0200 | [diff] [blame] | 3052 | if (use_crypt_method == 0) |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 3053 | crypt_init_keys(buf->b_p_key); |
| 3054 | else |
| 3055 | { |
Bram Moolenaar | 80794b1 | 2010-06-13 05:20:42 +0200 | [diff] [blame] | 3056 | /* Using blowfish, add salt and seed. */ |
| 3057 | salt = header + CRYPT_MAGIC_LEN; |
| 3058 | seed = salt + salt_len; |
| 3059 | sha2_seed(salt, salt_len, seed, seed_len); |
| 3060 | bf_key_init(buf->b_p_key, salt, salt_len); |
| 3061 | bf_ofb_init(seed, seed_len); |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 3062 | } |
| 3063 | } |
Bram Moolenaar | 80794b1 | 2010-06-13 05:20:42 +0200 | [diff] [blame] | 3064 | *lenp = CRYPT_MAGIC_LEN + salt_len + seed_len; |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 3065 | return header; |
| 3066 | } |
| 3067 | |
Bram Moolenaar | 80794b1 | 2010-06-13 05:20:42 +0200 | [diff] [blame] | 3068 | #endif /* FEAT_CRYPT */ |
| 3069 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3070 | #ifdef UNIX |
| 3071 | static void |
| 3072 | set_file_time(fname, atime, mtime) |
| 3073 | char_u *fname; |
| 3074 | time_t atime; /* access time */ |
| 3075 | time_t mtime; /* modification time */ |
| 3076 | { |
| 3077 | # if defined(HAVE_UTIME) && defined(HAVE_UTIME_H) |
| 3078 | struct utimbuf buf; |
| 3079 | |
| 3080 | buf.actime = atime; |
| 3081 | buf.modtime = mtime; |
| 3082 | (void)utime((char *)fname, &buf); |
| 3083 | # else |
| 3084 | # if defined(HAVE_UTIMES) |
| 3085 | struct timeval tvp[2]; |
| 3086 | |
| 3087 | tvp[0].tv_sec = atime; |
| 3088 | tvp[0].tv_usec = 0; |
| 3089 | tvp[1].tv_sec = mtime; |
| 3090 | tvp[1].tv_usec = 0; |
| 3091 | # ifdef NeXT |
| 3092 | (void)utimes((char *)fname, tvp); |
| 3093 | # else |
| 3094 | (void)utimes((char *)fname, (const struct timeval *)&tvp); |
| 3095 | # endif |
| 3096 | # endif |
| 3097 | # endif |
| 3098 | } |
| 3099 | #endif /* UNIX */ |
| 3100 | |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 3101 | #if defined(VMS) && !defined(MIN) |
| 3102 | /* Older DECC compiler for VAX doesn't define MIN() */ |
| 3103 | # define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 3104 | #endif |
| 3105 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3106 | /* |
Bram Moolenaar | 5386a12 | 2007-06-28 20:02:32 +0000 | [diff] [blame] | 3107 | * Return TRUE if a file appears to be read-only from the file permissions. |
| 3108 | */ |
| 3109 | int |
| 3110 | check_file_readonly(fname, perm) |
| 3111 | char_u *fname; /* full path to file */ |
| 3112 | int perm; /* known permissions on file */ |
| 3113 | { |
| 3114 | #ifndef USE_MCH_ACCESS |
| 3115 | int fd = 0; |
| 3116 | #endif |
| 3117 | |
| 3118 | return ( |
| 3119 | #ifdef USE_MCH_ACCESS |
| 3120 | # ifdef UNIX |
| 3121 | (perm & 0222) == 0 || |
| 3122 | # endif |
| 3123 | mch_access((char *)fname, W_OK) |
| 3124 | #else |
| 3125 | (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0 |
| 3126 | ? TRUE : (close(fd), FALSE) |
| 3127 | #endif |
| 3128 | ); |
| 3129 | } |
| 3130 | |
| 3131 | |
| 3132 | /* |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3133 | * buf_write() - write to file "fname" lines "start" through "end" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3134 | * |
| 3135 | * We do our own buffering here because fwrite() is so slow. |
| 3136 | * |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3137 | * If "forceit" is true, we don't care for errors when attempting backups. |
| 3138 | * 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] | 3139 | * file. But when "forceit" is TRUE, we risk losing it. |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3140 | * |
| 3141 | * When "reset_changed" is TRUE and "append" == FALSE and "start" == 1 and |
| 3142 | * "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3143 | * |
| 3144 | * This function must NOT use NameBuff (because it's called by autowrite()). |
| 3145 | * |
| 3146 | * return FAIL for failure, OK otherwise |
| 3147 | */ |
| 3148 | int |
| 3149 | buf_write(buf, fname, sfname, start, end, eap, append, forceit, |
| 3150 | reset_changed, filtering) |
| 3151 | buf_T *buf; |
| 3152 | char_u *fname; |
| 3153 | char_u *sfname; |
| 3154 | linenr_T start, end; |
| 3155 | exarg_T *eap; /* for forced 'ff' and 'fenc', can be |
| 3156 | NULL! */ |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3157 | int append; /* append to the file */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3158 | int forceit; |
| 3159 | int reset_changed; |
| 3160 | int filtering; |
| 3161 | { |
| 3162 | int fd; |
| 3163 | char_u *backup = NULL; |
| 3164 | int backup_copy = FALSE; /* copy the original file? */ |
| 3165 | int dobackup; |
| 3166 | char_u *ffname; |
| 3167 | char_u *wfname = NULL; /* name of file to write to */ |
| 3168 | char_u *s; |
| 3169 | char_u *ptr; |
| 3170 | char_u c; |
| 3171 | int len; |
| 3172 | linenr_T lnum; |
| 3173 | long nchars; |
| 3174 | char_u *errmsg = NULL; |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 3175 | int errmsg_allocated = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3176 | char_u *errnum = NULL; |
| 3177 | char_u *buffer; |
| 3178 | char_u smallbuf[SMBUFSIZE]; |
| 3179 | char_u *backup_ext; |
| 3180 | int bufsize; |
| 3181 | long perm; /* file permissions */ |
| 3182 | int retval = OK; |
| 3183 | int newfile = FALSE; /* TRUE if file doesn't exist yet */ |
| 3184 | int msg_save = msg_scroll; |
| 3185 | int overwriting; /* TRUE if writing over original */ |
| 3186 | int no_eol = FALSE; /* no end-of-line written */ |
| 3187 | int device = FALSE; /* writing to a device */ |
| 3188 | struct stat st_old; |
| 3189 | int prev_got_int = got_int; |
| 3190 | int file_readonly = FALSE; /* overwritten file is read-only */ |
| 3191 | static char *err_readonly = "is read-only (cannot override: \"W\" in 'cpoptions')"; |
| 3192 | #if defined(UNIX) || defined(__EMX__XX) /*XXX fix me sometime? */ |
| 3193 | int made_writable = FALSE; /* 'w' bit has been set */ |
| 3194 | #endif |
| 3195 | /* writing everything */ |
| 3196 | int whole = (start == 1 && end == buf->b_ml.ml_line_count); |
| 3197 | #ifdef FEAT_AUTOCMD |
| 3198 | linenr_T old_line_count = buf->b_ml.ml_line_count; |
| 3199 | #endif |
| 3200 | int attr; |
| 3201 | int fileformat; |
| 3202 | int write_bin; |
| 3203 | struct bw_info write_info; /* info for buf_write_bytes() */ |
| 3204 | #ifdef FEAT_MBYTE |
| 3205 | int converted = FALSE; |
| 3206 | int notconverted = FALSE; |
| 3207 | char_u *fenc; /* effective 'fileencoding' */ |
| 3208 | char_u *fenc_tofree = NULL; /* allocated "fenc" */ |
| 3209 | #endif |
| 3210 | #ifdef HAS_BW_FLAGS |
| 3211 | int wb_flags = 0; |
| 3212 | #endif |
| 3213 | #ifdef HAVE_ACL |
| 3214 | vim_acl_T acl = NULL; /* ACL copied from original file to |
| 3215 | backup or new file */ |
| 3216 | #endif |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 3217 | #ifdef FEAT_PERSISTENT_UNDO |
| 3218 | int write_undo_file = FALSE; |
| 3219 | context_sha256_T sha_ctx; |
| 3220 | #endif |
Bram Moolenaar | 4cf35c2 | 2011-02-25 16:52:17 +0100 | [diff] [blame] | 3221 | #ifdef FEAT_CRYPT |
| 3222 | int crypt_method_used; |
| 3223 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3224 | |
| 3225 | if (fname == NULL || *fname == NUL) /* safety check */ |
| 3226 | return FAIL; |
Bram Moolenaar | 70d60e9 | 2009-12-31 13:53:33 +0000 | [diff] [blame] | 3227 | if (buf->b_ml.ml_mfp == NULL) |
| 3228 | { |
| 3229 | /* This can happen during startup when there is a stray "w" in the |
| 3230 | * vimrc file. */ |
| 3231 | EMSG(_(e_emptybuf)); |
| 3232 | return FAIL; |
| 3233 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3234 | |
| 3235 | /* |
| 3236 | * Disallow writing from .exrc and .vimrc in current directory for |
| 3237 | * security reasons. |
| 3238 | */ |
| 3239 | if (check_secure()) |
| 3240 | return FAIL; |
| 3241 | |
| 3242 | /* Avoid a crash for a long name. */ |
| 3243 | if (STRLEN(fname) >= MAXPATHL) |
| 3244 | { |
| 3245 | EMSG(_(e_longname)); |
| 3246 | return FAIL; |
| 3247 | } |
| 3248 | |
| 3249 | #ifdef FEAT_MBYTE |
| 3250 | /* must init bw_conv_buf and bw_iconv_fd before jumping to "fail" */ |
| 3251 | write_info.bw_conv_buf = NULL; |
| 3252 | write_info.bw_conv_error = FALSE; |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 3253 | write_info.bw_conv_error_lnum = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3254 | write_info.bw_restlen = 0; |
| 3255 | # ifdef USE_ICONV |
| 3256 | write_info.bw_iconv_fd = (iconv_t)-1; |
| 3257 | # endif |
| 3258 | #endif |
| 3259 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3260 | /* After writing a file changedtick changes but we don't want to display |
| 3261 | * the line. */ |
| 3262 | ex_no_reprint = TRUE; |
| 3263 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3264 | /* |
| 3265 | * If there is no file name yet, use the one for the written file. |
| 3266 | * BF_NOTEDITED is set to reflect this (in case the write fails). |
| 3267 | * Don't do this when the write is for a filter command. |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3268 | * Don't do this when appending. |
| 3269 | * Only do this when 'cpoptions' contains the 'F' flag. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3270 | */ |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 3271 | if (buf->b_ffname == NULL |
| 3272 | && reset_changed |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3273 | && whole |
| 3274 | && buf == curbuf |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3275 | #ifdef FEAT_QUICKFIX |
| 3276 | && !bt_nofile(buf) |
| 3277 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3278 | && !filtering |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3279 | && (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3280 | && vim_strchr(p_cpo, CPO_FNAMEW) != NULL) |
| 3281 | { |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 3282 | if (set_rw_fname(fname, sfname) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3283 | return FAIL; |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 3284 | buf = curbuf; /* just in case autocmds made "buf" invalid */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3285 | } |
| 3286 | |
| 3287 | if (sfname == NULL) |
| 3288 | sfname = fname; |
| 3289 | /* |
| 3290 | * For Unix: Use the short file name whenever possible. |
| 3291 | * Avoids problems with networks and when directory names are changed. |
| 3292 | * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to |
| 3293 | * another directory, which we don't detect |
| 3294 | */ |
| 3295 | ffname = fname; /* remember full fname */ |
| 3296 | #ifdef UNIX |
| 3297 | fname = sfname; |
| 3298 | #endif |
| 3299 | |
| 3300 | if (buf->b_ffname != NULL && fnamecmp(ffname, buf->b_ffname) == 0) |
| 3301 | overwriting = TRUE; |
| 3302 | else |
| 3303 | overwriting = FALSE; |
| 3304 | |
| 3305 | if (exiting) |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 3306 | settmode(TMODE_COOK); /* when exiting allow typeahead now */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3307 | |
| 3308 | ++no_wait_return; /* don't wait for return yet */ |
| 3309 | |
| 3310 | /* |
| 3311 | * Set '[ and '] marks to the lines to be written. |
| 3312 | */ |
| 3313 | buf->b_op_start.lnum = start; |
| 3314 | buf->b_op_start.col = 0; |
| 3315 | buf->b_op_end.lnum = end; |
| 3316 | buf->b_op_end.col = 0; |
| 3317 | |
| 3318 | #ifdef FEAT_AUTOCMD |
| 3319 | { |
| 3320 | aco_save_T aco; |
| 3321 | int buf_ffname = FALSE; |
| 3322 | int buf_sfname = FALSE; |
| 3323 | int buf_fname_f = FALSE; |
| 3324 | int buf_fname_s = FALSE; |
| 3325 | int did_cmd = FALSE; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3326 | int nofile_err = FALSE; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 3327 | int empty_memline = (buf->b_ml.ml_mfp == NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3328 | |
| 3329 | /* |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 3330 | * Apply PRE autocommands. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3331 | * Set curbuf to the buffer to be written. |
| 3332 | * Careful: The autocommands may call buf_write() recursively! |
| 3333 | */ |
| 3334 | if (ffname == buf->b_ffname) |
| 3335 | buf_ffname = TRUE; |
| 3336 | if (sfname == buf->b_sfname) |
| 3337 | buf_sfname = TRUE; |
| 3338 | if (fname == buf->b_ffname) |
| 3339 | buf_fname_f = TRUE; |
| 3340 | if (fname == buf->b_sfname) |
| 3341 | buf_fname_s = TRUE; |
| 3342 | |
| 3343 | /* set curwin/curbuf to buf and save a few things */ |
| 3344 | aucmd_prepbuf(&aco, buf); |
| 3345 | |
| 3346 | if (append) |
| 3347 | { |
| 3348 | if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD, |
| 3349 | sfname, sfname, FALSE, curbuf, eap))) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3350 | { |
Bram Moolenaar | b1b715d | 2006-01-21 22:09:43 +0000 | [diff] [blame] | 3351 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 3352 | if (overwriting && bt_nofile(curbuf)) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3353 | nofile_err = TRUE; |
| 3354 | else |
Bram Moolenaar | b1b715d | 2006-01-21 22:09:43 +0000 | [diff] [blame] | 3355 | #endif |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3356 | apply_autocmds_exarg(EVENT_FILEAPPENDPRE, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3357 | sfname, sfname, FALSE, curbuf, eap); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3358 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3359 | } |
| 3360 | else if (filtering) |
| 3361 | { |
| 3362 | apply_autocmds_exarg(EVENT_FILTERWRITEPRE, |
| 3363 | NULL, sfname, FALSE, curbuf, eap); |
| 3364 | } |
| 3365 | else if (reset_changed && whole) |
| 3366 | { |
Bram Moolenaar | 39fc42e | 2011-09-02 11:56:20 +0200 | [diff] [blame] | 3367 | int was_changed = curbufIsChanged(); |
| 3368 | |
| 3369 | did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD, |
| 3370 | sfname, sfname, FALSE, curbuf, eap); |
| 3371 | if (did_cmd) |
| 3372 | { |
| 3373 | if (was_changed && !curbufIsChanged()) |
| 3374 | { |
| 3375 | /* Written everything correctly and BufWriteCmd has reset |
| 3376 | * 'modified': Correct the undo information so that an |
| 3377 | * undo now sets 'modified'. */ |
| 3378 | u_unchanged(curbuf); |
| 3379 | u_update_save_nr(curbuf); |
| 3380 | } |
| 3381 | } |
| 3382 | else |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3383 | { |
Bram Moolenaar | b1b715d | 2006-01-21 22:09:43 +0000 | [diff] [blame] | 3384 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 3385 | if (overwriting && bt_nofile(curbuf)) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3386 | nofile_err = TRUE; |
| 3387 | else |
Bram Moolenaar | b1b715d | 2006-01-21 22:09:43 +0000 | [diff] [blame] | 3388 | #endif |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3389 | apply_autocmds_exarg(EVENT_BUFWRITEPRE, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3390 | sfname, sfname, FALSE, curbuf, eap); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3391 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3392 | } |
| 3393 | else |
| 3394 | { |
| 3395 | if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD, |
| 3396 | sfname, sfname, FALSE, curbuf, eap))) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3397 | { |
Bram Moolenaar | b1b715d | 2006-01-21 22:09:43 +0000 | [diff] [blame] | 3398 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 3399 | if (overwriting && bt_nofile(curbuf)) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3400 | nofile_err = TRUE; |
| 3401 | else |
Bram Moolenaar | b1b715d | 2006-01-21 22:09:43 +0000 | [diff] [blame] | 3402 | #endif |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3403 | apply_autocmds_exarg(EVENT_FILEWRITEPRE, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3404 | sfname, sfname, FALSE, curbuf, eap); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3405 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3406 | } |
| 3407 | |
| 3408 | /* restore curwin/curbuf and a few other things */ |
| 3409 | aucmd_restbuf(&aco); |
| 3410 | |
| 3411 | /* |
| 3412 | * In three situations we return here and don't write the file: |
| 3413 | * 1. the autocommands deleted or unloaded the buffer. |
| 3414 | * 2. The autocommands abort script processing. |
| 3415 | * 3. If one of the "Cmd" autocommands was executed. |
| 3416 | */ |
| 3417 | if (!buf_valid(buf)) |
| 3418 | buf = NULL; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 3419 | if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline) |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 3420 | || did_cmd || nofile_err |
| 3421 | #ifdef FEAT_EVAL |
| 3422 | || aborting() |
| 3423 | #endif |
| 3424 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3425 | { |
| 3426 | --no_wait_return; |
| 3427 | msg_scroll = msg_save; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3428 | if (nofile_err) |
| 3429 | EMSG(_("E676: No matching autocommands for acwrite buffer")); |
| 3430 | |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 3431 | if (nofile_err |
| 3432 | #ifdef FEAT_EVAL |
| 3433 | || aborting() |
| 3434 | #endif |
| 3435 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3436 | /* An aborting error, interrupt or exception in the |
| 3437 | * autocommands. */ |
| 3438 | return FAIL; |
| 3439 | if (did_cmd) |
| 3440 | { |
| 3441 | if (buf == NULL) |
| 3442 | /* The buffer was deleted. We assume it was written |
| 3443 | * (can't retry anyway). */ |
| 3444 | return OK; |
| 3445 | if (overwriting) |
| 3446 | { |
| 3447 | /* Assume the buffer was written, update the timestamp. */ |
| 3448 | ml_timestamp(buf); |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3449 | if (append) |
| 3450 | buf->b_flags &= ~BF_NEW; |
| 3451 | else |
| 3452 | buf->b_flags &= ~BF_WRITE_MASK; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3453 | } |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3454 | if (reset_changed && buf->b_changed && !append |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 3455 | && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3456 | /* Buffer still changed, the autocommands didn't work |
| 3457 | * properly. */ |
| 3458 | return FAIL; |
| 3459 | return OK; |
| 3460 | } |
| 3461 | #ifdef FEAT_EVAL |
| 3462 | if (!aborting()) |
| 3463 | #endif |
| 3464 | EMSG(_("E203: Autocommands deleted or unloaded buffer to be written")); |
| 3465 | return FAIL; |
| 3466 | } |
| 3467 | |
| 3468 | /* |
| 3469 | * The autocommands may have changed the number of lines in the file. |
| 3470 | * When writing the whole file, adjust the end. |
| 3471 | * When writing part of the file, assume that the autocommands only |
| 3472 | * changed the number of lines that are to be written (tricky!). |
| 3473 | */ |
| 3474 | if (buf->b_ml.ml_line_count != old_line_count) |
| 3475 | { |
| 3476 | if (whole) /* write all */ |
| 3477 | end = buf->b_ml.ml_line_count; |
| 3478 | else if (buf->b_ml.ml_line_count > old_line_count) /* more lines */ |
| 3479 | end += buf->b_ml.ml_line_count - old_line_count; |
| 3480 | else /* less lines */ |
| 3481 | { |
| 3482 | end -= old_line_count - buf->b_ml.ml_line_count; |
| 3483 | if (end < start) |
| 3484 | { |
| 3485 | --no_wait_return; |
| 3486 | msg_scroll = msg_save; |
| 3487 | EMSG(_("E204: Autocommand changed number of lines in unexpected way")); |
| 3488 | return FAIL; |
| 3489 | } |
| 3490 | } |
| 3491 | } |
| 3492 | |
| 3493 | /* |
| 3494 | * The autocommands may have changed the name of the buffer, which may |
| 3495 | * be kept in fname, ffname and sfname. |
| 3496 | */ |
| 3497 | if (buf_ffname) |
| 3498 | ffname = buf->b_ffname; |
| 3499 | if (buf_sfname) |
| 3500 | sfname = buf->b_sfname; |
| 3501 | if (buf_fname_f) |
| 3502 | fname = buf->b_ffname; |
| 3503 | if (buf_fname_s) |
| 3504 | fname = buf->b_sfname; |
| 3505 | } |
| 3506 | #endif |
| 3507 | |
| 3508 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 3509 | if (netbeans_active() && isNetbeansBuffer(buf)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3510 | { |
| 3511 | if (whole) |
| 3512 | { |
| 3513 | /* |
| 3514 | * b_changed can be 0 after an undo, but we still need to write |
| 3515 | * the buffer to NetBeans. |
| 3516 | */ |
| 3517 | if (buf->b_changed || isNetbeansModified(buf)) |
| 3518 | { |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3519 | --no_wait_return; /* may wait for return now */ |
| 3520 | msg_scroll = msg_save; |
| 3521 | netbeans_save_buffer(buf); /* no error checking... */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3522 | return retval; |
| 3523 | } |
| 3524 | else |
| 3525 | { |
| 3526 | errnum = (char_u *)"E656: "; |
Bram Moolenaar | ed0e745 | 2008-06-27 19:17:34 +0000 | [diff] [blame] | 3527 | errmsg = (char_u *)_("NetBeans disallows writes of unmodified buffers"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3528 | buffer = NULL; |
| 3529 | goto fail; |
| 3530 | } |
| 3531 | } |
| 3532 | else |
| 3533 | { |
| 3534 | errnum = (char_u *)"E657: "; |
| 3535 | errmsg = (char_u *)_("Partial writes disallowed for NetBeans buffers"); |
| 3536 | buffer = NULL; |
| 3537 | goto fail; |
| 3538 | } |
| 3539 | } |
| 3540 | #endif |
| 3541 | |
| 3542 | if (shortmess(SHM_OVER) && !exiting) |
| 3543 | msg_scroll = FALSE; /* overwrite previous file message */ |
| 3544 | else |
| 3545 | msg_scroll = TRUE; /* don't overwrite previous file message */ |
| 3546 | if (!filtering) |
| 3547 | filemess(buf, |
| 3548 | #ifndef UNIX |
| 3549 | sfname, |
| 3550 | #else |
| 3551 | fname, |
| 3552 | #endif |
| 3553 | (char_u *)"", 0); /* show that we are busy */ |
| 3554 | msg_scroll = FALSE; /* always overwrite the file message now */ |
| 3555 | |
| 3556 | buffer = alloc(BUFSIZE); |
| 3557 | if (buffer == NULL) /* can't allocate big buffer, use small |
| 3558 | * one (to be able to write when out of |
| 3559 | * memory) */ |
| 3560 | { |
| 3561 | buffer = smallbuf; |
| 3562 | bufsize = SMBUFSIZE; |
| 3563 | } |
| 3564 | else |
| 3565 | bufsize = BUFSIZE; |
| 3566 | |
| 3567 | /* |
| 3568 | * Get information about original file (if there is one). |
| 3569 | */ |
| 3570 | #if defined(UNIX) && !defined(ARCHIE) |
Bram Moolenaar | 6f19245 | 2007-11-08 19:49:02 +0000 | [diff] [blame] | 3571 | st_old.st_dev = 0; |
| 3572 | st_old.st_ino = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3573 | perm = -1; |
| 3574 | if (mch_stat((char *)fname, &st_old) < 0) |
| 3575 | newfile = TRUE; |
| 3576 | else |
| 3577 | { |
| 3578 | perm = st_old.st_mode; |
| 3579 | if (!S_ISREG(st_old.st_mode)) /* not a file */ |
| 3580 | { |
| 3581 | if (S_ISDIR(st_old.st_mode)) |
| 3582 | { |
| 3583 | errnum = (char_u *)"E502: "; |
| 3584 | errmsg = (char_u *)_("is a directory"); |
| 3585 | goto fail; |
| 3586 | } |
| 3587 | if (mch_nodetype(fname) != NODE_WRITABLE) |
| 3588 | { |
| 3589 | errnum = (char_u *)"E503: "; |
| 3590 | errmsg = (char_u *)_("is not a file or writable device"); |
| 3591 | goto fail; |
| 3592 | } |
| 3593 | /* It's a device of some kind (or a fifo) which we can write to |
| 3594 | * but for which we can't make a backup. */ |
| 3595 | device = TRUE; |
| 3596 | newfile = TRUE; |
| 3597 | perm = -1; |
| 3598 | } |
| 3599 | } |
| 3600 | #else /* !UNIX */ |
| 3601 | /* |
| 3602 | * Check for a writable device name. |
| 3603 | */ |
| 3604 | c = mch_nodetype(fname); |
| 3605 | if (c == NODE_OTHER) |
| 3606 | { |
| 3607 | errnum = (char_u *)"E503: "; |
| 3608 | errmsg = (char_u *)_("is not a file or writable device"); |
| 3609 | goto fail; |
| 3610 | } |
| 3611 | if (c == NODE_WRITABLE) |
| 3612 | { |
Bram Moolenaar | 043545e | 2006-10-10 16:44:07 +0000 | [diff] [blame] | 3613 | # if defined(MSDOS) || defined(MSWIN) || defined(OS2) |
| 3614 | /* MS-Windows allows opening a device, but we will probably get stuck |
| 3615 | * trying to write to it. */ |
| 3616 | if (!p_odev) |
| 3617 | { |
| 3618 | errnum = (char_u *)"E796: "; |
| 3619 | errmsg = (char_u *)_("writing to device disabled with 'opendevice' option"); |
| 3620 | goto fail; |
| 3621 | } |
| 3622 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3623 | device = TRUE; |
| 3624 | newfile = TRUE; |
| 3625 | perm = -1; |
| 3626 | } |
| 3627 | else |
| 3628 | { |
| 3629 | perm = mch_getperm(fname); |
| 3630 | if (perm < 0) |
| 3631 | newfile = TRUE; |
| 3632 | else if (mch_isdir(fname)) |
| 3633 | { |
| 3634 | errnum = (char_u *)"E502: "; |
| 3635 | errmsg = (char_u *)_("is a directory"); |
| 3636 | goto fail; |
| 3637 | } |
| 3638 | if (overwriting) |
| 3639 | (void)mch_stat((char *)fname, &st_old); |
| 3640 | } |
| 3641 | #endif /* !UNIX */ |
| 3642 | |
| 3643 | if (!device && !newfile) |
| 3644 | { |
| 3645 | /* |
| 3646 | * Check if the file is really writable (when renaming the file to |
| 3647 | * make a backup we won't discover it later). |
| 3648 | */ |
Bram Moolenaar | 5386a12 | 2007-06-28 20:02:32 +0000 | [diff] [blame] | 3649 | file_readonly = check_file_readonly(fname, (int)perm); |
| 3650 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3651 | if (!forceit && file_readonly) |
| 3652 | { |
| 3653 | if (vim_strchr(p_cpo, CPO_FWRITE) != NULL) |
| 3654 | { |
| 3655 | errnum = (char_u *)"E504: "; |
| 3656 | errmsg = (char_u *)_(err_readonly); |
| 3657 | } |
| 3658 | else |
| 3659 | { |
| 3660 | errnum = (char_u *)"E505: "; |
| 3661 | errmsg = (char_u *)_("is read-only (add ! to override)"); |
| 3662 | } |
| 3663 | goto fail; |
| 3664 | } |
| 3665 | |
| 3666 | /* |
| 3667 | * Check if the timestamp hasn't changed since reading the file. |
| 3668 | */ |
| 3669 | if (overwriting) |
| 3670 | { |
| 3671 | retval = check_mtime(buf, &st_old); |
| 3672 | if (retval == FAIL) |
| 3673 | goto fail; |
| 3674 | } |
| 3675 | } |
| 3676 | |
| 3677 | #ifdef HAVE_ACL |
| 3678 | /* |
| 3679 | * For systems that support ACL: get the ACL from the original file. |
| 3680 | */ |
| 3681 | if (!newfile) |
| 3682 | acl = mch_get_acl(fname); |
| 3683 | #endif |
| 3684 | |
| 3685 | /* |
| 3686 | * If 'backupskip' is not empty, don't make a backup for some files. |
| 3687 | */ |
| 3688 | dobackup = (p_wb || p_bk || *p_pm != NUL); |
| 3689 | #ifdef FEAT_WILDIGN |
| 3690 | if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname)) |
| 3691 | dobackup = FALSE; |
| 3692 | #endif |
| 3693 | |
| 3694 | /* |
| 3695 | * Save the value of got_int and reset it. We don't want a previous |
| 3696 | * interruption cancel writing, only hitting CTRL-C while writing should |
| 3697 | * abort it. |
| 3698 | */ |
| 3699 | prev_got_int = got_int; |
| 3700 | got_int = FALSE; |
| 3701 | |
| 3702 | /* Mark the buffer as 'being saved' to prevent changed buffer warnings */ |
| 3703 | buf->b_saving = TRUE; |
| 3704 | |
| 3705 | /* |
| 3706 | * If we are not appending or filtering, the file exists, and the |
| 3707 | * 'writebackup', 'backup' or 'patchmode' option is set, need a backup. |
| 3708 | * When 'patchmode' is set also make a backup when appending. |
| 3709 | * |
| 3710 | * Do not make any backup, if 'writebackup' and 'backup' are both switched |
| 3711 | * off. This helps when editing large files on almost-full disks. |
| 3712 | */ |
| 3713 | if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup) |
| 3714 | { |
| 3715 | #if defined(UNIX) || defined(WIN32) |
| 3716 | struct stat st; |
| 3717 | #endif |
| 3718 | |
| 3719 | if ((bkc_flags & BKC_YES) || append) /* "yes" */ |
| 3720 | backup_copy = TRUE; |
| 3721 | #if defined(UNIX) || defined(WIN32) |
| 3722 | else if ((bkc_flags & BKC_AUTO)) /* "auto" */ |
| 3723 | { |
| 3724 | int i; |
| 3725 | |
| 3726 | # ifdef UNIX |
| 3727 | /* |
| 3728 | * Don't rename the file when: |
| 3729 | * - it's a hard link |
| 3730 | * - it's a symbolic link |
| 3731 | * - we don't have write permission in the directory |
| 3732 | * - we can't set the owner/group of the new file |
| 3733 | */ |
| 3734 | if (st_old.st_nlink > 1 |
| 3735 | || mch_lstat((char *)fname, &st) < 0 |
| 3736 | || st.st_dev != st_old.st_dev |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3737 | || st.st_ino != st_old.st_ino |
| 3738 | # ifndef HAVE_FCHOWN |
| 3739 | || st.st_uid != st_old.st_uid |
| 3740 | || st.st_gid != st_old.st_gid |
| 3741 | # endif |
| 3742 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3743 | backup_copy = TRUE; |
| 3744 | else |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 3745 | # else |
| 3746 | # ifdef WIN32 |
| 3747 | /* On NTFS file systems hard links are possible. */ |
| 3748 | if (mch_is_linked(fname)) |
| 3749 | backup_copy = TRUE; |
| 3750 | else |
| 3751 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3752 | # endif |
| 3753 | { |
| 3754 | /* |
| 3755 | * Check if we can create a file and set the owner/group to |
| 3756 | * the ones from the original file. |
| 3757 | * First find a file name that doesn't exist yet (use some |
| 3758 | * arbitrary numbers). |
| 3759 | */ |
| 3760 | STRCPY(IObuff, fname); |
| 3761 | for (i = 4913; ; i += 123) |
| 3762 | { |
| 3763 | sprintf((char *)gettail(IObuff), "%d", i); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3764 | if (mch_lstat((char *)IObuff, &st) < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3765 | break; |
| 3766 | } |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3767 | fd = mch_open((char *)IObuff, |
| 3768 | O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3769 | if (fd < 0) /* can't write in directory */ |
| 3770 | backup_copy = TRUE; |
| 3771 | else |
| 3772 | { |
| 3773 | # ifdef UNIX |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3774 | # ifdef HAVE_FCHOWN |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 3775 | ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3776 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3777 | if (mch_stat((char *)IObuff, &st) < 0 |
| 3778 | || st.st_uid != st_old.st_uid |
| 3779 | || st.st_gid != st_old.st_gid |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 3780 | || (long)st.st_mode != perm) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3781 | backup_copy = TRUE; |
| 3782 | # endif |
Bram Moolenaar | 9835862 | 2005-11-28 22:58:23 +0000 | [diff] [blame] | 3783 | /* Close the file before removing it, on MS-Windows we |
| 3784 | * can't delete an open file. */ |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3785 | close(fd); |
Bram Moolenaar | 9835862 | 2005-11-28 22:58:23 +0000 | [diff] [blame] | 3786 | mch_remove(IObuff); |
Bram Moolenaar | 3479c5d | 2010-08-08 18:46:06 +0200 | [diff] [blame] | 3787 | # ifdef MSWIN |
| 3788 | /* MS-Windows may trigger a virus scanner to open the |
| 3789 | * file, we can't delete it then. Keep trying for half a |
| 3790 | * second. */ |
| 3791 | { |
| 3792 | int try; |
| 3793 | |
| 3794 | for (try = 0; try < 10; ++try) |
| 3795 | { |
| 3796 | if (mch_lstat((char *)IObuff, &st) < 0) |
| 3797 | break; |
| 3798 | ui_delay(50L, TRUE); /* wait 50 msec */ |
| 3799 | mch_remove(IObuff); |
| 3800 | } |
| 3801 | } |
| 3802 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3803 | } |
| 3804 | } |
| 3805 | } |
| 3806 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3807 | /* |
| 3808 | * Break symlinks and/or hardlinks if we've been asked to. |
| 3809 | */ |
| 3810 | if ((bkc_flags & BKC_BREAKSYMLINK) || (bkc_flags & BKC_BREAKHARDLINK)) |
| 3811 | { |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3812 | # ifdef UNIX |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3813 | int lstat_res; |
| 3814 | |
| 3815 | lstat_res = mch_lstat((char *)fname, &st); |
| 3816 | |
| 3817 | /* Symlinks. */ |
| 3818 | if ((bkc_flags & BKC_BREAKSYMLINK) |
| 3819 | && lstat_res == 0 |
| 3820 | && st.st_ino != st_old.st_ino) |
| 3821 | backup_copy = FALSE; |
| 3822 | |
| 3823 | /* Hardlinks. */ |
| 3824 | if ((bkc_flags & BKC_BREAKHARDLINK) |
| 3825 | && st_old.st_nlink > 1 |
| 3826 | && (lstat_res != 0 || st.st_ino == st_old.st_ino)) |
| 3827 | backup_copy = FALSE; |
Bram Moolenaar | 12b559e | 2013-06-12 22:41:37 +0200 | [diff] [blame] | 3828 | # else |
| 3829 | # if defined(WIN32) |
| 3830 | /* Symlinks. */ |
| 3831 | if ((bkc_flags & BKC_BREAKSYMLINK) && mch_is_symbolic_link(fname)) |
| 3832 | backup_copy = FALSE; |
| 3833 | |
| 3834 | /* Hardlinks. */ |
| 3835 | if ((bkc_flags & BKC_BREAKHARDLINK) && mch_is_hard_link(fname)) |
| 3836 | backup_copy = FALSE; |
| 3837 | # endif |
| 3838 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3839 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3840 | |
| 3841 | #endif |
| 3842 | |
| 3843 | /* make sure we have a valid backup extension to use */ |
| 3844 | if (*p_bex == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3845 | backup_ext = (char_u *)".bak"; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3846 | else |
| 3847 | backup_ext = p_bex; |
| 3848 | |
| 3849 | if (backup_copy |
| 3850 | && (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) >= 0) |
| 3851 | { |
| 3852 | int bfd; |
| 3853 | char_u *copybuf, *wp; |
| 3854 | int some_error = FALSE; |
| 3855 | struct stat st_new; |
| 3856 | char_u *dirp; |
| 3857 | char_u *rootname; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3858 | #if defined(UNIX) && !defined(SHORT_FNAME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3859 | int did_set_shortname; |
| 3860 | #endif |
| 3861 | |
| 3862 | copybuf = alloc(BUFSIZE + 1); |
| 3863 | if (copybuf == NULL) |
| 3864 | { |
| 3865 | some_error = TRUE; /* out of memory */ |
| 3866 | goto nobackup; |
| 3867 | } |
| 3868 | |
| 3869 | /* |
| 3870 | * Try to make the backup in each directory in the 'bdir' option. |
| 3871 | * |
| 3872 | * Unix semantics has it, that we may have a writable file, |
| 3873 | * that cannot be recreated with a simple open(..., O_CREAT, ) e.g: |
| 3874 | * - the directory is not writable, |
| 3875 | * - the file may be a symbolic link, |
| 3876 | * - the file may belong to another user/group, etc. |
| 3877 | * |
| 3878 | * For these reasons, the existing writable file must be truncated |
| 3879 | * and reused. Creation of a backup COPY will be attempted. |
| 3880 | */ |
| 3881 | dirp = p_bdir; |
| 3882 | while (*dirp) |
| 3883 | { |
| 3884 | #ifdef UNIX |
| 3885 | st_new.st_ino = 0; |
| 3886 | st_new.st_dev = 0; |
| 3887 | st_new.st_gid = 0; |
| 3888 | #endif |
| 3889 | |
| 3890 | /* |
| 3891 | * Isolate one directory name, using an entry in 'bdir'. |
| 3892 | */ |
| 3893 | (void)copy_option_part(&dirp, copybuf, BUFSIZE, ","); |
| 3894 | rootname = get_file_in_dir(fname, copybuf); |
| 3895 | if (rootname == NULL) |
| 3896 | { |
| 3897 | some_error = TRUE; /* out of memory */ |
| 3898 | goto nobackup; |
| 3899 | } |
| 3900 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3901 | #if defined(UNIX) && !defined(SHORT_FNAME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3902 | did_set_shortname = FALSE; |
| 3903 | #endif |
| 3904 | |
| 3905 | /* |
| 3906 | * May try twice if 'shortname' not set. |
| 3907 | */ |
| 3908 | for (;;) |
| 3909 | { |
| 3910 | /* |
| 3911 | * Make backup file name. |
| 3912 | */ |
| 3913 | backup = buf_modname( |
| 3914 | #ifdef SHORT_FNAME |
| 3915 | TRUE, |
| 3916 | #else |
| 3917 | (buf->b_p_sn || buf->b_shortname), |
| 3918 | #endif |
| 3919 | rootname, backup_ext, FALSE); |
| 3920 | if (backup == NULL) |
| 3921 | { |
| 3922 | vim_free(rootname); |
| 3923 | some_error = TRUE; /* out of memory */ |
| 3924 | goto nobackup; |
| 3925 | } |
| 3926 | |
| 3927 | /* |
| 3928 | * Check if backup file already exists. |
| 3929 | */ |
| 3930 | if (mch_stat((char *)backup, &st_new) >= 0) |
| 3931 | { |
| 3932 | #ifdef UNIX |
| 3933 | /* |
| 3934 | * Check if backup file is same as original file. |
| 3935 | * May happen when modname() gave the same file back. |
| 3936 | * E.g. silly link, or file name-length reached. |
| 3937 | * If we don't check here, we either ruin the file |
| 3938 | * when copying or erase it after writing. jw. |
| 3939 | */ |
| 3940 | if (st_new.st_dev == st_old.st_dev |
| 3941 | && st_new.st_ino == st_old.st_ino) |
| 3942 | { |
| 3943 | vim_free(backup); |
| 3944 | backup = NULL; /* no backup file to delete */ |
| 3945 | # ifndef SHORT_FNAME |
| 3946 | /* |
| 3947 | * may try again with 'shortname' set |
| 3948 | */ |
| 3949 | if (!(buf->b_shortname || buf->b_p_sn)) |
| 3950 | { |
| 3951 | buf->b_shortname = TRUE; |
| 3952 | did_set_shortname = TRUE; |
| 3953 | continue; |
| 3954 | } |
| 3955 | /* setting shortname didn't help */ |
| 3956 | if (did_set_shortname) |
| 3957 | buf->b_shortname = FALSE; |
| 3958 | # endif |
| 3959 | break; |
| 3960 | } |
| 3961 | #endif |
| 3962 | |
| 3963 | /* |
| 3964 | * If we are not going to keep the backup file, don't |
| 3965 | * delete an existing one, try to use another name. |
| 3966 | * Change one character, just before the extension. |
| 3967 | */ |
| 3968 | if (!p_bk) |
| 3969 | { |
| 3970 | wp = backup + STRLEN(backup) - 1 |
| 3971 | - STRLEN(backup_ext); |
| 3972 | if (wp < backup) /* empty file name ??? */ |
| 3973 | wp = backup; |
| 3974 | *wp = 'z'; |
| 3975 | while (*wp > 'a' |
| 3976 | && mch_stat((char *)backup, &st_new) >= 0) |
| 3977 | --*wp; |
| 3978 | /* They all exist??? Must be something wrong. */ |
| 3979 | if (*wp == 'a') |
| 3980 | { |
| 3981 | vim_free(backup); |
| 3982 | backup = NULL; |
| 3983 | } |
| 3984 | } |
| 3985 | } |
| 3986 | break; |
| 3987 | } |
| 3988 | vim_free(rootname); |
| 3989 | |
| 3990 | /* |
| 3991 | * Try to create the backup file |
| 3992 | */ |
| 3993 | if (backup != NULL) |
| 3994 | { |
| 3995 | /* remove old backup, if present */ |
| 3996 | mch_remove(backup); |
| 3997 | /* Open with O_EXCL to avoid the file being created while |
| 3998 | * we were sleeping (symlink hacker attack?) */ |
| 3999 | bfd = mch_open((char *)backup, |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4000 | O_WRONLY|O_CREAT|O_EXTRA|O_EXCL|O_NOFOLLOW, |
| 4001 | perm & 0777); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4002 | if (bfd < 0) |
| 4003 | { |
| 4004 | vim_free(backup); |
| 4005 | backup = NULL; |
| 4006 | } |
| 4007 | else |
| 4008 | { |
| 4009 | /* set file protection same as original file, but |
| 4010 | * strip s-bit */ |
| 4011 | (void)mch_setperm(backup, perm & 0777); |
| 4012 | |
| 4013 | #ifdef UNIX |
| 4014 | /* |
| 4015 | * Try to set the group of the backup same as the |
| 4016 | * original file. If this fails, set the protection |
| 4017 | * bits for the group same as the protection bits for |
| 4018 | * others. |
| 4019 | */ |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4020 | if (st_new.st_gid != st_old.st_gid |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4021 | # ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */ |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4022 | && fchown(bfd, (uid_t)-1, st_old.st_gid) != 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4023 | # endif |
| 4024 | ) |
| 4025 | mch_setperm(backup, |
| 4026 | (perm & 0707) | ((perm & 07) << 3)); |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 4027 | # ifdef HAVE_SELINUX |
| 4028 | mch_copy_sec(fname, backup); |
| 4029 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4030 | #endif |
| 4031 | |
| 4032 | /* |
| 4033 | * copy the file. |
| 4034 | */ |
| 4035 | write_info.bw_fd = bfd; |
| 4036 | write_info.bw_buf = copybuf; |
| 4037 | #ifdef HAS_BW_FLAGS |
| 4038 | write_info.bw_flags = FIO_NOCONVERT; |
| 4039 | #endif |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 4040 | while ((write_info.bw_len = read_eintr(fd, copybuf, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4041 | BUFSIZE)) > 0) |
| 4042 | { |
| 4043 | if (buf_write_bytes(&write_info) == FAIL) |
| 4044 | { |
| 4045 | errmsg = (char_u *)_("E506: Can't write to backup file (add ! to override)"); |
| 4046 | break; |
| 4047 | } |
| 4048 | ui_breakcheck(); |
| 4049 | if (got_int) |
| 4050 | { |
| 4051 | errmsg = (char_u *)_(e_interr); |
| 4052 | break; |
| 4053 | } |
| 4054 | } |
| 4055 | |
| 4056 | if (close(bfd) < 0 && errmsg == NULL) |
| 4057 | errmsg = (char_u *)_("E507: Close error for backup file (add ! to override)"); |
| 4058 | if (write_info.bw_len < 0) |
| 4059 | errmsg = (char_u *)_("E508: Can't read file for backup (add ! to override)"); |
| 4060 | #ifdef UNIX |
| 4061 | set_file_time(backup, st_old.st_atime, st_old.st_mtime); |
| 4062 | #endif |
| 4063 | #ifdef HAVE_ACL |
| 4064 | mch_set_acl(backup, acl); |
| 4065 | #endif |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 4066 | #ifdef HAVE_SELINUX |
| 4067 | mch_copy_sec(fname, backup); |
| 4068 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4069 | break; |
| 4070 | } |
| 4071 | } |
| 4072 | } |
| 4073 | nobackup: |
| 4074 | close(fd); /* ignore errors for closing read file */ |
| 4075 | vim_free(copybuf); |
| 4076 | |
| 4077 | if (backup == NULL && errmsg == NULL) |
| 4078 | errmsg = (char_u *)_("E509: Cannot create backup file (add ! to override)"); |
| 4079 | /* ignore errors when forceit is TRUE */ |
| 4080 | if ((some_error || errmsg != NULL) && !forceit) |
| 4081 | { |
| 4082 | retval = FAIL; |
| 4083 | goto fail; |
| 4084 | } |
| 4085 | errmsg = NULL; |
| 4086 | } |
| 4087 | else |
| 4088 | { |
| 4089 | char_u *dirp; |
| 4090 | char_u *p; |
| 4091 | char_u *rootname; |
| 4092 | |
| 4093 | /* |
| 4094 | * Make a backup by renaming the original file. |
| 4095 | */ |
| 4096 | /* |
| 4097 | * If 'cpoptions' includes the "W" flag, we don't want to |
| 4098 | * overwrite a read-only file. But rename may be possible |
| 4099 | * anyway, thus we need an extra check here. |
| 4100 | */ |
| 4101 | if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL) |
| 4102 | { |
| 4103 | errnum = (char_u *)"E504: "; |
| 4104 | errmsg = (char_u *)_(err_readonly); |
| 4105 | goto fail; |
| 4106 | } |
| 4107 | |
| 4108 | /* |
| 4109 | * |
| 4110 | * Form the backup file name - change path/fo.o.h to |
| 4111 | * path/fo.o.h.bak Try all directories in 'backupdir', first one |
| 4112 | * that works is used. |
| 4113 | */ |
| 4114 | dirp = p_bdir; |
| 4115 | while (*dirp) |
| 4116 | { |
| 4117 | /* |
| 4118 | * Isolate one directory name and make the backup file name. |
| 4119 | */ |
| 4120 | (void)copy_option_part(&dirp, IObuff, IOSIZE, ","); |
| 4121 | rootname = get_file_in_dir(fname, IObuff); |
| 4122 | if (rootname == NULL) |
| 4123 | backup = NULL; |
| 4124 | else |
| 4125 | { |
| 4126 | backup = buf_modname( |
| 4127 | #ifdef SHORT_FNAME |
| 4128 | TRUE, |
| 4129 | #else |
| 4130 | (buf->b_p_sn || buf->b_shortname), |
| 4131 | #endif |
| 4132 | rootname, backup_ext, FALSE); |
| 4133 | vim_free(rootname); |
| 4134 | } |
| 4135 | |
| 4136 | if (backup != NULL) |
| 4137 | { |
| 4138 | /* |
| 4139 | * If we are not going to keep the backup file, don't |
| 4140 | * delete an existing one, try to use another name. |
| 4141 | * Change one character, just before the extension. |
| 4142 | */ |
| 4143 | if (!p_bk && mch_getperm(backup) >= 0) |
| 4144 | { |
| 4145 | p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext); |
| 4146 | if (p < backup) /* empty file name ??? */ |
| 4147 | p = backup; |
| 4148 | *p = 'z'; |
| 4149 | while (*p > 'a' && mch_getperm(backup) >= 0) |
| 4150 | --*p; |
| 4151 | /* They all exist??? Must be something wrong! */ |
| 4152 | if (*p == 'a') |
| 4153 | { |
| 4154 | vim_free(backup); |
| 4155 | backup = NULL; |
| 4156 | } |
| 4157 | } |
| 4158 | } |
| 4159 | if (backup != NULL) |
| 4160 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4161 | /* |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4162 | * Delete any existing backup and move the current version |
| 4163 | * to the backup. For safety, we don't remove the backup |
| 4164 | * until the write has finished successfully. And if the |
| 4165 | * 'backup' option is set, leave it around. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4166 | */ |
| 4167 | /* |
| 4168 | * If the renaming of the original file to the backup file |
| 4169 | * works, quit here. |
| 4170 | */ |
| 4171 | if (vim_rename(fname, backup) == 0) |
| 4172 | break; |
| 4173 | |
| 4174 | vim_free(backup); /* don't do the rename below */ |
| 4175 | backup = NULL; |
| 4176 | } |
| 4177 | } |
| 4178 | if (backup == NULL && !forceit) |
| 4179 | { |
| 4180 | errmsg = (char_u *)_("E510: Can't make backup file (add ! to override)"); |
| 4181 | goto fail; |
| 4182 | } |
| 4183 | } |
| 4184 | } |
| 4185 | |
| 4186 | #if defined(UNIX) && !defined(ARCHIE) |
| 4187 | /* When using ":w!" and the file was read-only: make it writable */ |
| 4188 | if (forceit && perm >= 0 && !(perm & 0200) && st_old.st_uid == getuid() |
| 4189 | && vim_strchr(p_cpo, CPO_FWRITE) == NULL) |
| 4190 | { |
| 4191 | perm |= 0200; |
| 4192 | (void)mch_setperm(fname, perm); |
| 4193 | made_writable = TRUE; |
| 4194 | } |
| 4195 | #endif |
| 4196 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 4197 | /* When using ":w!" and writing to the current file, 'readonly' makes no |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 4198 | * sense, reset it, unless 'Z' appears in 'cpoptions'. */ |
| 4199 | if (forceit && overwriting && vim_strchr(p_cpo, CPO_KEEPRO) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4200 | { |
| 4201 | buf->b_p_ro = FALSE; |
| 4202 | #ifdef FEAT_TITLE |
| 4203 | need_maketitle = TRUE; /* set window title later */ |
| 4204 | #endif |
| 4205 | #ifdef FEAT_WINDOWS |
| 4206 | status_redraw_all(); /* redraw status lines later */ |
| 4207 | #endif |
| 4208 | } |
| 4209 | |
| 4210 | if (end > buf->b_ml.ml_line_count) |
| 4211 | end = buf->b_ml.ml_line_count; |
| 4212 | if (buf->b_ml.ml_flags & ML_EMPTY) |
| 4213 | start = end + 1; |
| 4214 | |
| 4215 | /* |
| 4216 | * If the original file is being overwritten, there is a small chance that |
| 4217 | * we crash in the middle of writing. Therefore the file is preserved now. |
| 4218 | * This makes all block numbers positive so that recovery does not need |
| 4219 | * the original file. |
| 4220 | * Don't do this if there is a backup file and we are exiting. |
| 4221 | */ |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4222 | if (reset_changed && !newfile && overwriting |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4223 | && !(exiting && backup != NULL)) |
| 4224 | { |
| 4225 | ml_preserve(buf, FALSE); |
| 4226 | if (got_int) |
| 4227 | { |
| 4228 | errmsg = (char_u *)_(e_interr); |
| 4229 | goto restore_backup; |
| 4230 | } |
| 4231 | } |
| 4232 | |
| 4233 | #ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */ |
| 4234 | /* |
| 4235 | * Before risking to lose the original file verify if there's |
| 4236 | * a resource fork to preserve, and if cannot be done warn |
| 4237 | * the users. This happens when overwriting without backups. |
| 4238 | */ |
| 4239 | if (backup == NULL && overwriting && !append) |
| 4240 | if (mch_has_resource_fork(fname)) |
| 4241 | { |
| 4242 | errmsg = (char_u *)_("E460: The resource fork would be lost (add ! to override)"); |
| 4243 | goto restore_backup; |
| 4244 | } |
| 4245 | #endif |
| 4246 | |
| 4247 | #ifdef VMS |
| 4248 | vms_remove_version(fname); /* remove version */ |
| 4249 | #endif |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 4250 | /* Default: write the file directly. May write to a temp file for |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4251 | * multi-byte conversion. */ |
| 4252 | wfname = fname; |
| 4253 | |
| 4254 | #ifdef FEAT_MBYTE |
| 4255 | /* Check for forced 'fileencoding' from "++opt=val" argument. */ |
| 4256 | if (eap != NULL && eap->force_enc != 0) |
| 4257 | { |
| 4258 | fenc = eap->cmd + eap->force_enc; |
| 4259 | fenc = enc_canonize(fenc); |
| 4260 | fenc_tofree = fenc; |
| 4261 | } |
| 4262 | else |
| 4263 | fenc = buf->b_p_fenc; |
| 4264 | |
| 4265 | /* |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 4266 | * Check if the file needs to be converted. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4267 | */ |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 4268 | converted = need_conversion(fenc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4269 | |
| 4270 | /* |
| 4271 | * Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done. Or |
| 4272 | * Latin1 to Unicode conversion. This is handled in buf_write_bytes(). |
| 4273 | * Prepare the flags for it and allocate bw_conv_buf when needed. |
| 4274 | */ |
| 4275 | if (converted && (enc_utf8 || STRCMP(p_enc, "latin1") == 0)) |
| 4276 | { |
| 4277 | wb_flags = get_fio_flags(fenc); |
| 4278 | if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8)) |
| 4279 | { |
| 4280 | /* Need to allocate a buffer to translate into. */ |
| 4281 | if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8)) |
| 4282 | write_info.bw_conv_buflen = bufsize * 2; |
| 4283 | else /* FIO_UCS4 */ |
| 4284 | write_info.bw_conv_buflen = bufsize * 4; |
| 4285 | write_info.bw_conv_buf |
| 4286 | = lalloc((long_u)write_info.bw_conv_buflen, TRUE); |
| 4287 | if (write_info.bw_conv_buf == NULL) |
| 4288 | end = 0; |
| 4289 | } |
| 4290 | } |
| 4291 | |
| 4292 | # ifdef WIN3264 |
| 4293 | if (converted && wb_flags == 0 && (wb_flags = get_win_fio_flags(fenc)) != 0) |
| 4294 | { |
| 4295 | /* Convert UTF-8 -> UCS-2 and UCS-2 -> DBCS. Worst-case * 4: */ |
| 4296 | write_info.bw_conv_buflen = bufsize * 4; |
| 4297 | write_info.bw_conv_buf |
| 4298 | = lalloc((long_u)write_info.bw_conv_buflen, TRUE); |
| 4299 | if (write_info.bw_conv_buf == NULL) |
| 4300 | end = 0; |
| 4301 | } |
| 4302 | # endif |
| 4303 | |
| 4304 | # ifdef MACOS_X |
| 4305 | if (converted && wb_flags == 0 && (wb_flags = get_mac_fio_flags(fenc)) != 0) |
| 4306 | { |
| 4307 | write_info.bw_conv_buflen = bufsize * 3; |
| 4308 | write_info.bw_conv_buf |
| 4309 | = lalloc((long_u)write_info.bw_conv_buflen, TRUE); |
| 4310 | if (write_info.bw_conv_buf == NULL) |
| 4311 | end = 0; |
| 4312 | } |
| 4313 | # endif |
| 4314 | |
| 4315 | # if defined(FEAT_EVAL) || defined(USE_ICONV) |
| 4316 | if (converted && wb_flags == 0) |
| 4317 | { |
| 4318 | # ifdef USE_ICONV |
| 4319 | /* |
| 4320 | * Use iconv() conversion when conversion is needed and it's not done |
| 4321 | * internally. |
| 4322 | */ |
| 4323 | write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc, |
| 4324 | enc_utf8 ? (char_u *)"utf-8" : p_enc); |
| 4325 | if (write_info.bw_iconv_fd != (iconv_t)-1) |
| 4326 | { |
| 4327 | /* We're going to use iconv(), allocate a buffer to convert in. */ |
| 4328 | write_info.bw_conv_buflen = bufsize * ICONV_MULT; |
| 4329 | write_info.bw_conv_buf |
| 4330 | = lalloc((long_u)write_info.bw_conv_buflen, TRUE); |
| 4331 | if (write_info.bw_conv_buf == NULL) |
| 4332 | end = 0; |
| 4333 | write_info.bw_first = TRUE; |
| 4334 | } |
| 4335 | # ifdef FEAT_EVAL |
| 4336 | else |
| 4337 | # endif |
| 4338 | # endif |
| 4339 | |
| 4340 | # ifdef FEAT_EVAL |
| 4341 | /* |
| 4342 | * When the file needs to be converted with 'charconvert' after |
| 4343 | * writing, write to a temp file instead and let the conversion |
| 4344 | * overwrite the original file. |
| 4345 | */ |
| 4346 | if (*p_ccv != NUL) |
| 4347 | { |
| 4348 | wfname = vim_tempname('w'); |
| 4349 | if (wfname == NULL) /* Can't write without a tempfile! */ |
| 4350 | { |
| 4351 | errmsg = (char_u *)_("E214: Can't find temp file for writing"); |
| 4352 | goto restore_backup; |
| 4353 | } |
| 4354 | } |
| 4355 | # endif |
| 4356 | } |
| 4357 | # endif |
| 4358 | if (converted && wb_flags == 0 |
| 4359 | # ifdef USE_ICONV |
| 4360 | && write_info.bw_iconv_fd == (iconv_t)-1 |
| 4361 | # endif |
| 4362 | # ifdef FEAT_EVAL |
| 4363 | && wfname == fname |
| 4364 | # endif |
| 4365 | ) |
| 4366 | { |
| 4367 | if (!forceit) |
| 4368 | { |
| 4369 | errmsg = (char_u *)_("E213: Cannot convert (add ! to write without conversion)"); |
| 4370 | goto restore_backup; |
| 4371 | } |
| 4372 | notconverted = TRUE; |
| 4373 | } |
| 4374 | #endif |
| 4375 | |
| 4376 | /* |
| 4377 | * Open the file "wfname" for writing. |
| 4378 | * We may try to open the file twice: If we can't write to the |
| 4379 | * file and forceit is TRUE we delete the existing file and try to create |
| 4380 | * a new one. If this still fails we may have lost the original file! |
| 4381 | * (this may happen when the user reached his quotum for number of files). |
| 4382 | * Appending will fail if the file does not exist and forceit is FALSE. |
| 4383 | */ |
| 4384 | while ((fd = mch_open((char *)wfname, O_WRONLY | O_EXTRA | (append |
| 4385 | ? (forceit ? (O_APPEND | O_CREAT) : O_APPEND) |
| 4386 | : (O_CREAT | O_TRUNC)) |
Bram Moolenaar | 4317d9b | 2005-03-18 20:25:31 +0000 | [diff] [blame] | 4387 | , perm < 0 ? 0666 : (perm & 0777))) < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4388 | { |
| 4389 | /* |
| 4390 | * A forced write will try to create a new file if the old one is |
| 4391 | * still readonly. This may also happen when the directory is |
| 4392 | * read-only. In that case the mch_remove() will fail. |
| 4393 | */ |
| 4394 | if (errmsg == NULL) |
| 4395 | { |
| 4396 | #ifdef UNIX |
| 4397 | struct stat st; |
| 4398 | |
| 4399 | /* Don't delete the file when it's a hard or symbolic link. */ |
| 4400 | if ((!newfile && st_old.st_nlink > 1) |
| 4401 | || (mch_lstat((char *)fname, &st) == 0 |
| 4402 | && (st.st_dev != st_old.st_dev |
| 4403 | || st.st_ino != st_old.st_ino))) |
| 4404 | errmsg = (char_u *)_("E166: Can't open linked file for writing"); |
| 4405 | else |
| 4406 | #endif |
| 4407 | { |
| 4408 | errmsg = (char_u *)_("E212: Can't open file for writing"); |
| 4409 | if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL |
| 4410 | && perm >= 0) |
| 4411 | { |
| 4412 | #ifdef UNIX |
| 4413 | /* we write to the file, thus it should be marked |
| 4414 | writable after all */ |
| 4415 | if (!(perm & 0200)) |
| 4416 | made_writable = TRUE; |
| 4417 | perm |= 0200; |
| 4418 | if (st_old.st_uid != getuid() || st_old.st_gid != getgid()) |
| 4419 | perm &= 0777; |
| 4420 | #endif |
| 4421 | if (!append) /* don't remove when appending */ |
| 4422 | mch_remove(wfname); |
| 4423 | continue; |
| 4424 | } |
| 4425 | } |
| 4426 | } |
| 4427 | |
| 4428 | restore_backup: |
| 4429 | { |
| 4430 | struct stat st; |
| 4431 | |
| 4432 | /* |
| 4433 | * If we failed to open the file, we don't need a backup. Throw it |
| 4434 | * away. If we moved or removed the original file try to put the |
| 4435 | * backup in its place. |
| 4436 | */ |
| 4437 | if (backup != NULL && wfname == fname) |
| 4438 | { |
| 4439 | if (backup_copy) |
| 4440 | { |
| 4441 | /* |
| 4442 | * There is a small chance that we removed the original, |
| 4443 | * try to move the copy in its place. |
| 4444 | * This may not work if the vim_rename() fails. |
| 4445 | * In that case we leave the copy around. |
| 4446 | */ |
| 4447 | /* If file does not exist, put the copy in its place */ |
| 4448 | if (mch_stat((char *)fname, &st) < 0) |
| 4449 | vim_rename(backup, fname); |
| 4450 | /* if original file does exist throw away the copy */ |
| 4451 | if (mch_stat((char *)fname, &st) >= 0) |
| 4452 | mch_remove(backup); |
| 4453 | } |
| 4454 | else |
| 4455 | { |
| 4456 | /* try to put the original file back */ |
| 4457 | vim_rename(backup, fname); |
| 4458 | } |
| 4459 | } |
| 4460 | |
| 4461 | /* if original file no longer exists give an extra warning */ |
| 4462 | if (!newfile && mch_stat((char *)fname, &st) < 0) |
| 4463 | end = 0; |
| 4464 | } |
| 4465 | |
| 4466 | #ifdef FEAT_MBYTE |
| 4467 | if (wfname != fname) |
| 4468 | vim_free(wfname); |
| 4469 | #endif |
| 4470 | goto fail; |
| 4471 | } |
| 4472 | errmsg = NULL; |
| 4473 | |
| 4474 | #if defined(MACOS_CLASSIC) || defined(WIN3264) |
| 4475 | /* TODO: Is it need for MACOS_X? (Dany) */ |
| 4476 | /* |
| 4477 | * On macintosh copy the original files attributes (i.e. the backup) |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 4478 | * This is done in order to preserve the resource fork and the |
| 4479 | * Finder attribute (label, comments, custom icons, file creator) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4480 | */ |
| 4481 | if (backup != NULL && overwriting && !append) |
| 4482 | { |
| 4483 | if (backup_copy) |
| 4484 | (void)mch_copy_file_attribute(wfname, backup); |
| 4485 | else |
| 4486 | (void)mch_copy_file_attribute(backup, wfname); |
| 4487 | } |
| 4488 | |
| 4489 | if (!overwriting && !append) |
| 4490 | { |
| 4491 | if (buf->b_ffname != NULL) |
| 4492 | (void)mch_copy_file_attribute(buf->b_ffname, wfname); |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 4493 | /* Should copy resource fork */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4494 | } |
| 4495 | #endif |
| 4496 | |
| 4497 | write_info.bw_fd = fd; |
| 4498 | |
| 4499 | #ifdef FEAT_CRYPT |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 4500 | if (*buf->b_p_key != NUL && !filtering) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4501 | { |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 4502 | char_u *header; |
| 4503 | int header_len; |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 4504 | |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 4505 | header = prepare_crypt_write(buf, &header_len); |
| 4506 | if (header == NULL) |
| 4507 | end = 0; |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 4508 | else |
| 4509 | { |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 4510 | /* Write magic number, so that Vim knows that this file is |
| 4511 | * encrypted when reading it again. This also undergoes utf-8 to |
| 4512 | * ucs-2/4 conversion when needed. */ |
| 4513 | write_info.bw_buf = header; |
| 4514 | write_info.bw_len = header_len; |
| 4515 | write_info.bw_flags = FIO_NOCONVERT; |
| 4516 | if (buf_write_bytes(&write_info) == FAIL) |
| 4517 | end = 0; |
| 4518 | wb_flags |= FIO_ENCRYPTED; |
| 4519 | vim_free(header); |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 4520 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4521 | } |
| 4522 | #endif |
| 4523 | |
| 4524 | write_info.bw_buf = buffer; |
| 4525 | nchars = 0; |
| 4526 | |
| 4527 | /* use "++bin", "++nobin" or 'binary' */ |
| 4528 | if (eap != NULL && eap->force_bin != 0) |
| 4529 | write_bin = (eap->force_bin == FORCE_BIN); |
| 4530 | else |
| 4531 | write_bin = buf->b_p_bin; |
| 4532 | |
| 4533 | #ifdef FEAT_MBYTE |
| 4534 | /* |
| 4535 | * The BOM is written just after the encryption magic number. |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 4536 | * Skip it when appending and the file already existed, the BOM only makes |
| 4537 | * sense at the start of the file. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4538 | */ |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 4539 | if (buf->b_p_bomb && !write_bin && (!append || perm < 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4540 | { |
| 4541 | write_info.bw_len = make_bom(buffer, fenc); |
| 4542 | if (write_info.bw_len > 0) |
| 4543 | { |
| 4544 | /* don't convert, do encryption */ |
| 4545 | write_info.bw_flags = FIO_NOCONVERT | wb_flags; |
| 4546 | if (buf_write_bytes(&write_info) == FAIL) |
| 4547 | end = 0; |
| 4548 | else |
| 4549 | nchars += write_info.bw_len; |
| 4550 | } |
| 4551 | } |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 4552 | write_info.bw_start_lnum = start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4553 | #endif |
| 4554 | |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 4555 | #ifdef FEAT_PERSISTENT_UNDO |
| 4556 | write_undo_file = (buf->b_p_udf && overwriting && !append |
| 4557 | && !filtering && reset_changed); |
| 4558 | if (write_undo_file) |
| 4559 | /* Prepare for computing the hash value of the text. */ |
| 4560 | sha256_start(&sha_ctx); |
| 4561 | #endif |
| 4562 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4563 | write_info.bw_len = bufsize; |
| 4564 | #ifdef HAS_BW_FLAGS |
| 4565 | write_info.bw_flags = wb_flags; |
| 4566 | #endif |
| 4567 | fileformat = get_fileformat_force(buf, eap); |
| 4568 | s = buffer; |
| 4569 | len = 0; |
| 4570 | for (lnum = start; lnum <= end; ++lnum) |
| 4571 | { |
| 4572 | /* |
| 4573 | * The next while loop is done once for each character written. |
| 4574 | * Keep it fast! |
| 4575 | */ |
| 4576 | ptr = ml_get_buf(buf, lnum, FALSE) - 1; |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 4577 | #ifdef FEAT_PERSISTENT_UNDO |
| 4578 | if (write_undo_file) |
Bram Moolenaar | 442b422 | 2010-05-24 21:34:22 +0200 | [diff] [blame] | 4579 | sha256_update(&sha_ctx, ptr + 1, (UINT32_T)(STRLEN(ptr + 1) + 1)); |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 4580 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4581 | while ((c = *++ptr) != NUL) |
| 4582 | { |
| 4583 | if (c == NL) |
| 4584 | *s = NUL; /* replace newlines with NULs */ |
| 4585 | else if (c == CAR && fileformat == EOL_MAC) |
| 4586 | *s = NL; /* Mac: replace CRs with NLs */ |
| 4587 | else |
| 4588 | *s = c; |
| 4589 | ++s; |
| 4590 | if (++len != bufsize) |
| 4591 | continue; |
| 4592 | if (buf_write_bytes(&write_info) == FAIL) |
| 4593 | { |
| 4594 | end = 0; /* write error: break loop */ |
| 4595 | break; |
| 4596 | } |
| 4597 | nchars += bufsize; |
| 4598 | s = buffer; |
| 4599 | len = 0; |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 4600 | #ifdef FEAT_MBYTE |
| 4601 | write_info.bw_start_lnum = lnum; |
| 4602 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4603 | } |
| 4604 | /* write failed or last line has no EOL: stop here */ |
| 4605 | if (end == 0 |
| 4606 | || (lnum == end |
| 4607 | && write_bin |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 4608 | && (lnum == buf->b_no_eol_lnum |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4609 | || (lnum == buf->b_ml.ml_line_count && !buf->b_p_eol)))) |
| 4610 | { |
| 4611 | ++lnum; /* written the line, count it */ |
| 4612 | no_eol = TRUE; |
| 4613 | break; |
| 4614 | } |
| 4615 | if (fileformat == EOL_UNIX) |
| 4616 | *s++ = NL; |
| 4617 | else |
| 4618 | { |
| 4619 | *s++ = CAR; /* EOL_MAC or EOL_DOS: write CR */ |
| 4620 | if (fileformat == EOL_DOS) /* write CR-NL */ |
| 4621 | { |
| 4622 | if (++len == bufsize) |
| 4623 | { |
| 4624 | if (buf_write_bytes(&write_info) == FAIL) |
| 4625 | { |
| 4626 | end = 0; /* write error: break loop */ |
| 4627 | break; |
| 4628 | } |
| 4629 | nchars += bufsize; |
| 4630 | s = buffer; |
| 4631 | len = 0; |
| 4632 | } |
| 4633 | *s++ = NL; |
| 4634 | } |
| 4635 | } |
| 4636 | if (++len == bufsize && end) |
| 4637 | { |
| 4638 | if (buf_write_bytes(&write_info) == FAIL) |
| 4639 | { |
| 4640 | end = 0; /* write error: break loop */ |
| 4641 | break; |
| 4642 | } |
| 4643 | nchars += bufsize; |
| 4644 | s = buffer; |
| 4645 | len = 0; |
| 4646 | |
| 4647 | ui_breakcheck(); |
| 4648 | if (got_int) |
| 4649 | { |
| 4650 | end = 0; /* Interrupted, break loop */ |
| 4651 | break; |
| 4652 | } |
| 4653 | } |
| 4654 | #ifdef VMS |
| 4655 | /* |
| 4656 | * On VMS there is a problem: newlines get added when writing blocks |
| 4657 | * at a time. Fix it by writing a line at a time. |
| 4658 | * This is much slower! |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 4659 | * Explanation: VAX/DECC RTL insists that records in some RMS |
| 4660 | * structures end with a newline (carriage return) character, and if |
| 4661 | * they don't it adds one. |
| 4662 | * With other RMS structures it works perfect without this fix. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4663 | */ |
Bram Moolenaar | b52e260 | 2007-10-29 21:38:54 +0000 | [diff] [blame] | 4664 | if (buf->b_fab_rfm == FAB$C_VFC |
| 4665 | || ((buf->b_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4666 | { |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 4667 | int b2write; |
| 4668 | |
| 4669 | buf->b_fab_mrs = (buf->b_fab_mrs == 0 |
| 4670 | ? MIN(4096, bufsize) |
| 4671 | : MIN(buf->b_fab_mrs, bufsize)); |
| 4672 | |
| 4673 | b2write = len; |
| 4674 | while (b2write > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4675 | { |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 4676 | write_info.bw_len = MIN(b2write, buf->b_fab_mrs); |
| 4677 | if (buf_write_bytes(&write_info) == FAIL) |
| 4678 | { |
| 4679 | end = 0; |
| 4680 | break; |
| 4681 | } |
| 4682 | b2write -= MIN(b2write, buf->b_fab_mrs); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4683 | } |
| 4684 | write_info.bw_len = bufsize; |
| 4685 | nchars += len; |
| 4686 | s = buffer; |
| 4687 | len = 0; |
| 4688 | } |
| 4689 | #endif |
| 4690 | } |
| 4691 | if (len > 0 && end > 0) |
| 4692 | { |
| 4693 | write_info.bw_len = len; |
| 4694 | if (buf_write_bytes(&write_info) == FAIL) |
| 4695 | end = 0; /* write error */ |
| 4696 | nchars += len; |
| 4697 | } |
| 4698 | |
| 4699 | #if defined(UNIX) && defined(HAVE_FSYNC) |
| 4700 | /* On many journalling file systems there is a bug that causes both the |
| 4701 | * original and the backup file to be lost when halting the system right |
| 4702 | * after writing the file. That's because only the meta-data is |
| 4703 | * journalled. Syncing the file slows down the system, but assures it has |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 4704 | * been written to disk and we don't lose it. |
| 4705 | * 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] | 4706 | * (could be a pipe). |
| 4707 | * If the 'fsync' option is FALSE, don't fsync(). Useful for laptops. */ |
| 4708 | if (p_fs && fsync(fd) != 0 && !device) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4709 | { |
| 4710 | errmsg = (char_u *)_("E667: Fsync failed"); |
| 4711 | end = 0; |
| 4712 | } |
| 4713 | #endif |
| 4714 | |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 4715 | #ifdef HAVE_SELINUX |
| 4716 | /* Probably need to set the security context. */ |
| 4717 | if (!backup_copy) |
| 4718 | mch_copy_sec(backup, wfname); |
| 4719 | #endif |
| 4720 | |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4721 | #ifdef UNIX |
| 4722 | /* When creating a new file, set its owner/group to that of the original |
| 4723 | * file. Get the new device and inode number. */ |
| 4724 | if (backup != NULL && !backup_copy) |
| 4725 | { |
| 4726 | # ifdef HAVE_FCHOWN |
| 4727 | struct stat st; |
| 4728 | |
| 4729 | /* don't change the owner when it's already OK, some systems remove |
| 4730 | * permission or ACL stuff */ |
| 4731 | if (mch_stat((char *)wfname, &st) < 0 |
| 4732 | || st.st_uid != st_old.st_uid |
| 4733 | || st.st_gid != st_old.st_gid) |
| 4734 | { |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4735 | ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4736 | if (perm >= 0) /* set permission again, may have changed */ |
| 4737 | (void)mch_setperm(wfname, perm); |
| 4738 | } |
| 4739 | # endif |
| 4740 | buf_setino(buf); |
| 4741 | } |
Bram Moolenaar | f1726cc | 2009-05-13 18:48:16 +0000 | [diff] [blame] | 4742 | else if (!buf->b_dev_valid) |
Bram Moolenaar | 8fa0445 | 2005-12-23 22:13:51 +0000 | [diff] [blame] | 4743 | /* Set the inode when creating a new file. */ |
| 4744 | buf_setino(buf); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4745 | #endif |
| 4746 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4747 | if (close(fd) != 0) |
| 4748 | { |
| 4749 | errmsg = (char_u *)_("E512: Close failed"); |
| 4750 | end = 0; |
| 4751 | } |
| 4752 | |
| 4753 | #ifdef UNIX |
| 4754 | if (made_writable) |
| 4755 | perm &= ~0200; /* reset 'w' bit for security reasons */ |
| 4756 | #endif |
| 4757 | if (perm >= 0) /* set perm. of new file same as old file */ |
| 4758 | (void)mch_setperm(wfname, perm); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4759 | #ifdef HAVE_ACL |
| 4760 | /* Probably need to set the ACL before changing the user (can't set the |
| 4761 | * ACL on a file the user doesn't own). */ |
| 4762 | if (!backup_copy) |
| 4763 | mch_set_acl(wfname, acl); |
| 4764 | #endif |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 4765 | #ifdef FEAT_CRYPT |
Bram Moolenaar | 4cf35c2 | 2011-02-25 16:52:17 +0100 | [diff] [blame] | 4766 | crypt_method_used = use_crypt_method; |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 4767 | if (wb_flags & FIO_ENCRYPTED) |
| 4768 | crypt_pop_state(); |
| 4769 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4770 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4771 | |
| 4772 | #if defined(FEAT_MBYTE) && defined(FEAT_EVAL) |
| 4773 | if (wfname != fname) |
| 4774 | { |
| 4775 | /* |
| 4776 | * The file was written to a temp file, now it needs to be converted |
| 4777 | * with 'charconvert' to (overwrite) the output file. |
| 4778 | */ |
| 4779 | if (end != 0) |
| 4780 | { |
| 4781 | if (eval_charconvert(enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc, |
| 4782 | wfname, fname) == FAIL) |
| 4783 | { |
| 4784 | write_info.bw_conv_error = TRUE; |
| 4785 | end = 0; |
| 4786 | } |
| 4787 | } |
| 4788 | mch_remove(wfname); |
| 4789 | vim_free(wfname); |
| 4790 | } |
| 4791 | #endif |
| 4792 | |
| 4793 | if (end == 0) |
| 4794 | { |
| 4795 | if (errmsg == NULL) |
| 4796 | { |
| 4797 | #ifdef FEAT_MBYTE |
| 4798 | if (write_info.bw_conv_error) |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 4799 | { |
| 4800 | if (write_info.bw_conv_error_lnum == 0) |
| 4801 | errmsg = (char_u *)_("E513: write error, conversion failed (make 'fenc' empty to override)"); |
| 4802 | else |
| 4803 | { |
| 4804 | errmsg_allocated = TRUE; |
| 4805 | errmsg = alloc(300); |
| 4806 | vim_snprintf((char *)errmsg, 300, _("E513: write error, conversion failed in line %ld (make 'fenc' empty to override)"), |
| 4807 | (long)write_info.bw_conv_error_lnum); |
| 4808 | } |
| 4809 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4810 | else |
| 4811 | #endif |
| 4812 | if (got_int) |
| 4813 | errmsg = (char_u *)_(e_interr); |
| 4814 | else |
| 4815 | errmsg = (char_u *)_("E514: write error (file system full?)"); |
| 4816 | } |
| 4817 | |
| 4818 | /* |
| 4819 | * 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] | 4820 | * because the new file is probably corrupt. This avoids losing the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4821 | * original file when trying to make a backup when writing the file a |
| 4822 | * second time. |
| 4823 | * When "backup_copy" is set we need to copy the backup over the new |
| 4824 | * file. Otherwise rename the backup file. |
| 4825 | * If this is OK, don't give the extra warning message. |
| 4826 | */ |
| 4827 | if (backup != NULL) |
| 4828 | { |
| 4829 | if (backup_copy) |
| 4830 | { |
| 4831 | /* This may take a while, if we were interrupted let the user |
| 4832 | * know we got the message. */ |
| 4833 | if (got_int) |
| 4834 | { |
| 4835 | MSG(_(e_interr)); |
| 4836 | out_flush(); |
| 4837 | } |
| 4838 | if ((fd = mch_open((char *)backup, O_RDONLY | O_EXTRA, 0)) >= 0) |
| 4839 | { |
| 4840 | if ((write_info.bw_fd = mch_open((char *)fname, |
Bram Moolenaar | 9be038d | 2005-03-08 22:34:32 +0000 | [diff] [blame] | 4841 | O_WRONLY | O_CREAT | O_TRUNC | O_EXTRA, |
| 4842 | perm & 0777)) >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4843 | { |
| 4844 | /* copy the file. */ |
| 4845 | write_info.bw_buf = smallbuf; |
| 4846 | #ifdef HAS_BW_FLAGS |
| 4847 | write_info.bw_flags = FIO_NOCONVERT; |
| 4848 | #endif |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 4849 | while ((write_info.bw_len = read_eintr(fd, smallbuf, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4850 | SMBUFSIZE)) > 0) |
| 4851 | if (buf_write_bytes(&write_info) == FAIL) |
| 4852 | break; |
| 4853 | |
| 4854 | if (close(write_info.bw_fd) >= 0 |
| 4855 | && write_info.bw_len == 0) |
| 4856 | end = 1; /* success */ |
| 4857 | } |
| 4858 | close(fd); /* ignore errors for closing read file */ |
| 4859 | } |
| 4860 | } |
| 4861 | else |
| 4862 | { |
| 4863 | if (vim_rename(backup, fname) == 0) |
| 4864 | end = 1; |
| 4865 | } |
| 4866 | } |
| 4867 | goto fail; |
| 4868 | } |
| 4869 | |
| 4870 | lnum -= start; /* compute number of written lines */ |
| 4871 | --no_wait_return; /* may wait for return now */ |
| 4872 | |
| 4873 | #if !(defined(UNIX) || defined(VMS)) |
| 4874 | fname = sfname; /* use shortname now, for the messages */ |
| 4875 | #endif |
| 4876 | if (!filtering) |
| 4877 | { |
| 4878 | msg_add_fname(buf, fname); /* put fname in IObuff with quotes */ |
| 4879 | c = FALSE; |
| 4880 | #ifdef FEAT_MBYTE |
| 4881 | if (write_info.bw_conv_error) |
| 4882 | { |
| 4883 | STRCAT(IObuff, _(" CONVERSION ERROR")); |
| 4884 | c = TRUE; |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 4885 | if (write_info.bw_conv_error_lnum != 0) |
Bram Moolenaar | a800b42 | 2010-06-27 01:15:55 +0200 | [diff] [blame] | 4886 | vim_snprintf_add((char *)IObuff, IOSIZE, _(" in line %ld;"), |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 4887 | (long)write_info.bw_conv_error_lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4888 | } |
| 4889 | else if (notconverted) |
| 4890 | { |
| 4891 | STRCAT(IObuff, _("[NOT converted]")); |
| 4892 | c = TRUE; |
| 4893 | } |
| 4894 | else if (converted) |
| 4895 | { |
| 4896 | STRCAT(IObuff, _("[converted]")); |
| 4897 | c = TRUE; |
| 4898 | } |
| 4899 | #endif |
| 4900 | if (device) |
| 4901 | { |
| 4902 | STRCAT(IObuff, _("[Device]")); |
| 4903 | c = TRUE; |
| 4904 | } |
| 4905 | else if (newfile) |
| 4906 | { |
| 4907 | STRCAT(IObuff, shortmess(SHM_NEW) ? _("[New]") : _("[New File]")); |
| 4908 | c = TRUE; |
| 4909 | } |
| 4910 | if (no_eol) |
| 4911 | { |
| 4912 | msg_add_eol(); |
| 4913 | c = TRUE; |
| 4914 | } |
| 4915 | /* may add [unix/dos/mac] */ |
| 4916 | if (msg_add_fileformat(fileformat)) |
| 4917 | c = TRUE; |
| 4918 | #ifdef FEAT_CRYPT |
| 4919 | if (wb_flags & FIO_ENCRYPTED) |
| 4920 | { |
Bram Moolenaar | 4cf35c2 | 2011-02-25 16:52:17 +0100 | [diff] [blame] | 4921 | if (crypt_method_used == 1) |
| 4922 | STRCAT(IObuff, _("[blowfish]")); |
| 4923 | else |
| 4924 | STRCAT(IObuff, _("[crypted]")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4925 | c = TRUE; |
| 4926 | } |
| 4927 | #endif |
| 4928 | msg_add_lines(c, (long)lnum, nchars); /* add line/char count */ |
| 4929 | if (!shortmess(SHM_WRITE)) |
| 4930 | { |
| 4931 | if (append) |
| 4932 | STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [a]") : _(" appended")); |
| 4933 | else |
| 4934 | STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [w]") : _(" written")); |
| 4935 | } |
| 4936 | |
Bram Moolenaar | 8f7fd65 | 2006-02-21 22:04:51 +0000 | [diff] [blame] | 4937 | set_keep_msg(msg_trunc_attr(IObuff, FALSE, 0), 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4938 | } |
| 4939 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4940 | /* When written everything correctly: reset 'modified'. Unless not |
| 4941 | * writing to the original file and '+' is not in 'cpoptions'. */ |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 4942 | if (reset_changed && whole && !append |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4943 | #ifdef FEAT_MBYTE |
| 4944 | && !write_info.bw_conv_error |
| 4945 | #endif |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4946 | && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL) |
| 4947 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4948 | { |
| 4949 | unchanged(buf, TRUE); |
| 4950 | u_unchanged(buf); |
Bram Moolenaar | 730cde9 | 2010-06-27 05:18:54 +0200 | [diff] [blame] | 4951 | u_update_save_nr(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
| 4954 | /* |
| 4955 | * If written to the current file, update the timestamp of the swap file |
| 4956 | * and reset the BF_WRITE_MASK flags. Also sets buf->b_mtime. |
| 4957 | */ |
| 4958 | if (overwriting) |
| 4959 | { |
| 4960 | ml_timestamp(buf); |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 4961 | if (append) |
| 4962 | buf->b_flags &= ~BF_NEW; |
| 4963 | else |
| 4964 | buf->b_flags &= ~BF_WRITE_MASK; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4965 | } |
| 4966 | |
| 4967 | /* |
| 4968 | * If we kept a backup until now, and we are in patch mode, then we make |
| 4969 | * the backup file our 'original' file. |
| 4970 | */ |
| 4971 | if (*p_pm && dobackup) |
| 4972 | { |
| 4973 | char *org = (char *)buf_modname( |
| 4974 | #ifdef SHORT_FNAME |
| 4975 | TRUE, |
| 4976 | #else |
| 4977 | (buf->b_p_sn || buf->b_shortname), |
| 4978 | #endif |
| 4979 | fname, p_pm, FALSE); |
| 4980 | |
| 4981 | if (backup != NULL) |
| 4982 | { |
| 4983 | struct stat st; |
| 4984 | |
| 4985 | /* |
| 4986 | * If the original file does not exist yet |
| 4987 | * the current backup file becomes the original file |
| 4988 | */ |
| 4989 | if (org == NULL) |
| 4990 | EMSG(_("E205: Patchmode: can't save original file")); |
| 4991 | else if (mch_stat(org, &st) < 0) |
| 4992 | { |
| 4993 | vim_rename(backup, (char_u *)org); |
| 4994 | vim_free(backup); /* don't delete the file */ |
| 4995 | backup = NULL; |
| 4996 | #ifdef UNIX |
| 4997 | set_file_time((char_u *)org, st_old.st_atime, st_old.st_mtime); |
| 4998 | #endif |
| 4999 | } |
| 5000 | } |
| 5001 | /* |
| 5002 | * If there is no backup file, remember that a (new) file was |
| 5003 | * created. |
| 5004 | */ |
| 5005 | else |
| 5006 | { |
| 5007 | int empty_fd; |
| 5008 | |
| 5009 | if (org == NULL |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 5010 | || (empty_fd = mch_open(org, |
| 5011 | O_CREAT | O_EXTRA | O_EXCL | O_NOFOLLOW, |
Bram Moolenaar | 4317d9b | 2005-03-18 20:25:31 +0000 | [diff] [blame] | 5012 | perm < 0 ? 0666 : (perm & 0777))) < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5013 | EMSG(_("E206: patchmode: can't touch empty original file")); |
| 5014 | else |
| 5015 | close(empty_fd); |
| 5016 | } |
| 5017 | if (org != NULL) |
| 5018 | { |
| 5019 | mch_setperm((char_u *)org, mch_getperm(fname) & 0777); |
| 5020 | vim_free(org); |
| 5021 | } |
| 5022 | } |
| 5023 | |
| 5024 | /* |
| 5025 | * Remove the backup unless 'backup' option is set |
| 5026 | */ |
| 5027 | if (!p_bk && backup != NULL && mch_remove(backup) != 0) |
| 5028 | EMSG(_("E207: Can't delete backup file")); |
| 5029 | |
| 5030 | #ifdef FEAT_SUN_WORKSHOP |
| 5031 | if (usingSunWorkShop) |
| 5032 | workshop_file_saved((char *) ffname); |
| 5033 | #endif |
| 5034 | |
| 5035 | goto nofail; |
| 5036 | |
| 5037 | /* |
| 5038 | * Finish up. We get here either after failure or success. |
| 5039 | */ |
| 5040 | fail: |
| 5041 | --no_wait_return; /* may wait for return now */ |
| 5042 | nofail: |
| 5043 | |
| 5044 | /* Done saving, we accept changed buffer warnings again */ |
| 5045 | buf->b_saving = FALSE; |
| 5046 | |
| 5047 | vim_free(backup); |
| 5048 | if (buffer != smallbuf) |
| 5049 | vim_free(buffer); |
| 5050 | #ifdef FEAT_MBYTE |
| 5051 | vim_free(fenc_tofree); |
| 5052 | vim_free(write_info.bw_conv_buf); |
| 5053 | # ifdef USE_ICONV |
| 5054 | if (write_info.bw_iconv_fd != (iconv_t)-1) |
| 5055 | { |
| 5056 | iconv_close(write_info.bw_iconv_fd); |
| 5057 | write_info.bw_iconv_fd = (iconv_t)-1; |
| 5058 | } |
| 5059 | # endif |
| 5060 | #endif |
| 5061 | #ifdef HAVE_ACL |
| 5062 | mch_free_acl(acl); |
| 5063 | #endif |
| 5064 | |
| 5065 | if (errmsg != NULL) |
| 5066 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5067 | int numlen = errnum != NULL ? (int)STRLEN(errnum) : 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5068 | |
| 5069 | attr = hl_attr(HLF_E); /* set highlight for error messages */ |
| 5070 | msg_add_fname(buf, |
| 5071 | #ifndef UNIX |
| 5072 | sfname |
| 5073 | #else |
| 5074 | fname |
| 5075 | #endif |
| 5076 | ); /* put file name in IObuff with quotes */ |
| 5077 | if (STRLEN(IObuff) + STRLEN(errmsg) + numlen >= IOSIZE) |
| 5078 | IObuff[IOSIZE - STRLEN(errmsg) - numlen - 1] = NUL; |
| 5079 | /* If the error message has the form "is ...", put the error number in |
| 5080 | * front of the file name. */ |
| 5081 | if (errnum != NULL) |
| 5082 | { |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 5083 | STRMOVE(IObuff + numlen, IObuff); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5084 | mch_memmove(IObuff, errnum, (size_t)numlen); |
| 5085 | } |
| 5086 | STRCAT(IObuff, errmsg); |
| 5087 | emsg(IObuff); |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 5088 | if (errmsg_allocated) |
| 5089 | vim_free(errmsg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5090 | |
| 5091 | retval = FAIL; |
| 5092 | if (end == 0) |
| 5093 | { |
| 5094 | MSG_PUTS_ATTR(_("\nWARNING: Original file may be lost or damaged\n"), |
| 5095 | attr | MSG_HIST); |
| 5096 | MSG_PUTS_ATTR(_("don't quit the editor until the file is successfully written!"), |
| 5097 | attr | MSG_HIST); |
| 5098 | |
| 5099 | /* Update the timestamp to avoid an "overwrite changed file" |
| 5100 | * prompt when writing again. */ |
| 5101 | if (mch_stat((char *)fname, &st_old) >= 0) |
| 5102 | { |
| 5103 | buf_store_time(buf, &st_old, fname); |
| 5104 | buf->b_mtime_read = buf->b_mtime; |
| 5105 | } |
| 5106 | } |
| 5107 | } |
| 5108 | msg_scroll = msg_save; |
| 5109 | |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 5110 | #ifdef FEAT_PERSISTENT_UNDO |
| 5111 | /* |
| 5112 | * When writing the whole file and 'undofile' is set, also write the undo |
| 5113 | * file. |
| 5114 | */ |
| 5115 | if (retval == OK && write_undo_file) |
| 5116 | { |
| 5117 | char_u hash[UNDO_HASH_SIZE]; |
| 5118 | |
| 5119 | sha256_finish(&sha_ctx, hash); |
| 5120 | u_write_undo(NULL, FALSE, buf, hash); |
| 5121 | } |
| 5122 | #endif |
| 5123 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5124 | #ifdef FEAT_AUTOCMD |
| 5125 | #ifdef FEAT_EVAL |
| 5126 | if (!should_abort(retval)) |
| 5127 | #else |
| 5128 | if (!got_int) |
| 5129 | #endif |
| 5130 | { |
| 5131 | aco_save_T aco; |
| 5132 | |
Bram Moolenaar | 68a33fc | 2012-04-25 16:50:48 +0200 | [diff] [blame] | 5133 | curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */ |
| 5134 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5135 | /* |
| 5136 | * Apply POST autocommands. |
| 5137 | * Careful: The autocommands may call buf_write() recursively! |
| 5138 | */ |
| 5139 | aucmd_prepbuf(&aco, buf); |
| 5140 | |
| 5141 | if (append) |
| 5142 | apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname, |
| 5143 | FALSE, curbuf, eap); |
| 5144 | else if (filtering) |
| 5145 | apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname, |
| 5146 | FALSE, curbuf, eap); |
| 5147 | else if (reset_changed && whole) |
| 5148 | apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname, |
| 5149 | FALSE, curbuf, eap); |
| 5150 | else |
| 5151 | apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname, |
| 5152 | FALSE, curbuf, eap); |
| 5153 | |
| 5154 | /* restore curwin/curbuf and a few other things */ |
| 5155 | aucmd_restbuf(&aco); |
| 5156 | |
| 5157 | #ifdef FEAT_EVAL |
| 5158 | if (aborting()) /* autocmds may abort script processing */ |
| 5159 | retval = FALSE; |
| 5160 | #endif |
| 5161 | } |
| 5162 | #endif |
| 5163 | |
| 5164 | got_int |= prev_got_int; |
| 5165 | |
| 5166 | #ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */ |
| 5167 | /* Update machine specific information. */ |
| 5168 | mch_post_buffer_write(buf); |
| 5169 | #endif |
| 5170 | return retval; |
| 5171 | } |
| 5172 | |
| 5173 | /* |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 5174 | * Set the name of the current buffer. Use when the buffer doesn't have a |
| 5175 | * name and a ":r" or ":w" command with a file name is used. |
| 5176 | */ |
| 5177 | static int |
| 5178 | set_rw_fname(fname, sfname) |
| 5179 | char_u *fname; |
| 5180 | char_u *sfname; |
| 5181 | { |
| 5182 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 8b38e24 | 2009-06-16 13:35:20 +0000 | [diff] [blame] | 5183 | buf_T *buf = curbuf; |
| 5184 | |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 5185 | /* It's like the unnamed buffer is deleted.... */ |
| 5186 | if (curbuf->b_p_bl) |
| 5187 | apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); |
| 5188 | apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf); |
| 5189 | # ifdef FEAT_EVAL |
| 5190 | if (aborting()) /* autocmds may abort script processing */ |
| 5191 | return FAIL; |
| 5192 | # endif |
Bram Moolenaar | 8b38e24 | 2009-06-16 13:35:20 +0000 | [diff] [blame] | 5193 | if (curbuf != buf) |
| 5194 | { |
| 5195 | /* We are in another buffer now, don't do the renaming. */ |
| 5196 | EMSG(_(e_auchangedbuf)); |
| 5197 | return FAIL; |
| 5198 | } |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 5199 | #endif |
| 5200 | |
| 5201 | if (setfname(curbuf, fname, sfname, FALSE) == OK) |
| 5202 | curbuf->b_flags |= BF_NOTEDITED; |
| 5203 | |
| 5204 | #ifdef FEAT_AUTOCMD |
| 5205 | /* ....and a new named one is created */ |
| 5206 | apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf); |
| 5207 | if (curbuf->b_p_bl) |
| 5208 | apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf); |
| 5209 | # ifdef FEAT_EVAL |
| 5210 | if (aborting()) /* autocmds may abort script processing */ |
| 5211 | return FAIL; |
| 5212 | # endif |
| 5213 | |
| 5214 | /* Do filetype detection now if 'filetype' is empty. */ |
| 5215 | if (*curbuf->b_p_ft == NUL) |
| 5216 | { |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5217 | if (au_has_group((char_u *)"filetypedetect")) |
Bram Moolenaar | 70836c8 | 2006-02-20 21:28:49 +0000 | [diff] [blame] | 5218 | (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE); |
Bram Moolenaar | a3227e2 | 2006-03-08 21:32:40 +0000 | [diff] [blame] | 5219 | do_modelines(0); |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 5220 | } |
| 5221 | #endif |
| 5222 | |
| 5223 | return OK; |
| 5224 | } |
| 5225 | |
| 5226 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5227 | * Put file name into IObuff with quotes. |
| 5228 | */ |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 5229 | void |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5230 | msg_add_fname(buf, fname) |
| 5231 | buf_T *buf; |
| 5232 | char_u *fname; |
| 5233 | { |
| 5234 | if (fname == NULL) |
| 5235 | fname = (char_u *)"-stdin-"; |
| 5236 | home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE); |
| 5237 | IObuff[0] = '"'; |
| 5238 | STRCAT(IObuff, "\" "); |
| 5239 | } |
| 5240 | |
| 5241 | /* |
| 5242 | * Append message for text mode to IObuff. |
| 5243 | * Return TRUE if something appended. |
| 5244 | */ |
| 5245 | static int |
| 5246 | msg_add_fileformat(eol_type) |
| 5247 | int eol_type; |
| 5248 | { |
| 5249 | #ifndef USE_CRNL |
| 5250 | if (eol_type == EOL_DOS) |
| 5251 | { |
| 5252 | STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]")); |
| 5253 | return TRUE; |
| 5254 | } |
| 5255 | #endif |
| 5256 | #ifndef USE_CR |
| 5257 | if (eol_type == EOL_MAC) |
| 5258 | { |
| 5259 | STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]")); |
| 5260 | return TRUE; |
| 5261 | } |
| 5262 | #endif |
| 5263 | #if defined(USE_CRNL) || defined(USE_CR) |
| 5264 | if (eol_type == EOL_UNIX) |
| 5265 | { |
| 5266 | STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]")); |
| 5267 | return TRUE; |
| 5268 | } |
| 5269 | #endif |
| 5270 | return FALSE; |
| 5271 | } |
| 5272 | |
| 5273 | /* |
| 5274 | * Append line and character count to IObuff. |
| 5275 | */ |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 5276 | void |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5277 | msg_add_lines(insert_space, lnum, nchars) |
| 5278 | int insert_space; |
| 5279 | long lnum; |
Bram Moolenaar | 914703b | 2010-05-31 21:59:46 +0200 | [diff] [blame] | 5280 | off_t nchars; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5281 | { |
| 5282 | char_u *p; |
| 5283 | |
| 5284 | p = IObuff + STRLEN(IObuff); |
| 5285 | |
| 5286 | if (insert_space) |
| 5287 | *p++ = ' '; |
| 5288 | if (shortmess(SHM_LINES)) |
Bram Moolenaar | 914703b | 2010-05-31 21:59:46 +0200 | [diff] [blame] | 5289 | sprintf((char *)p, |
| 5290 | #ifdef LONG_LONG_OFF_T |
Bram Moolenaar | f9b0129 | 2010-06-12 06:45:20 +0200 | [diff] [blame] | 5291 | "%ldL, %lldC", lnum, nchars |
Bram Moolenaar | 914703b | 2010-05-31 21:59:46 +0200 | [diff] [blame] | 5292 | #else |
Bram Moolenaar | f9b0129 | 2010-06-12 06:45:20 +0200 | [diff] [blame] | 5293 | /* Explicit typecast avoids warning on Mac OS X 10.6 */ |
| 5294 | "%ldL, %ldC", lnum, (long)nchars |
Bram Moolenaar | 914703b | 2010-05-31 21:59:46 +0200 | [diff] [blame] | 5295 | #endif |
Bram Moolenaar | f9b0129 | 2010-06-12 06:45:20 +0200 | [diff] [blame] | 5296 | ); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5297 | else |
| 5298 | { |
| 5299 | if (lnum == 1) |
| 5300 | STRCPY(p, _("1 line, ")); |
| 5301 | else |
| 5302 | sprintf((char *)p, _("%ld lines, "), lnum); |
| 5303 | p += STRLEN(p); |
| 5304 | if (nchars == 1) |
| 5305 | STRCPY(p, _("1 character")); |
| 5306 | else |
Bram Moolenaar | 914703b | 2010-05-31 21:59:46 +0200 | [diff] [blame] | 5307 | sprintf((char *)p, |
| 5308 | #ifdef LONG_LONG_OFF_T |
Bram Moolenaar | f9b0129 | 2010-06-12 06:45:20 +0200 | [diff] [blame] | 5309 | _("%lld characters"), nchars |
Bram Moolenaar | 914703b | 2010-05-31 21:59:46 +0200 | [diff] [blame] | 5310 | #else |
Bram Moolenaar | f9b0129 | 2010-06-12 06:45:20 +0200 | [diff] [blame] | 5311 | /* Explicit typecast avoids warning on Mac OS X 10.6 */ |
| 5312 | _("%ld characters"), (long)nchars |
Bram Moolenaar | 914703b | 2010-05-31 21:59:46 +0200 | [diff] [blame] | 5313 | #endif |
Bram Moolenaar | f9b0129 | 2010-06-12 06:45:20 +0200 | [diff] [blame] | 5314 | ); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5315 | } |
| 5316 | } |
| 5317 | |
| 5318 | /* |
| 5319 | * Append message for missing line separator to IObuff. |
| 5320 | */ |
| 5321 | static void |
| 5322 | msg_add_eol() |
| 5323 | { |
| 5324 | STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]")); |
| 5325 | } |
| 5326 | |
| 5327 | /* |
| 5328 | * Check modification time of file, before writing to it. |
| 5329 | * The size isn't checked, because using a tool like "gzip" takes care of |
| 5330 | * using the same timestamp but can't set the size. |
| 5331 | */ |
| 5332 | static int |
| 5333 | check_mtime(buf, st) |
| 5334 | buf_T *buf; |
| 5335 | struct stat *st; |
| 5336 | { |
| 5337 | if (buf->b_mtime_read != 0 |
| 5338 | && time_differs((long)st->st_mtime, buf->b_mtime_read)) |
| 5339 | { |
| 5340 | msg_scroll = TRUE; /* don't overwrite messages here */ |
| 5341 | msg_silent = 0; /* must give this prompt */ |
| 5342 | /* don't use emsg() here, don't want to flush the buffers */ |
| 5343 | MSG_ATTR(_("WARNING: The file has been changed since reading it!!!"), |
| 5344 | hl_attr(HLF_E)); |
| 5345 | if (ask_yesno((char_u *)_("Do you really want to write to it"), |
| 5346 | TRUE) == 'n') |
| 5347 | return FAIL; |
| 5348 | msg_scroll = FALSE; /* always overwrite the file message now */ |
| 5349 | } |
| 5350 | return OK; |
| 5351 | } |
| 5352 | |
| 5353 | static int |
| 5354 | time_differs(t1, t2) |
| 5355 | long t1, t2; |
| 5356 | { |
| 5357 | #if defined(__linux__) || defined(MSDOS) || defined(MSWIN) |
| 5358 | /* On a FAT filesystem, esp. under Linux, there are only 5 bits to store |
| 5359 | * the seconds. Since the roundoff is done when flushing the inode, the |
| 5360 | * time may change unexpectedly by one second!!! */ |
| 5361 | return (t1 - t2 > 1 || t2 - t1 > 1); |
| 5362 | #else |
| 5363 | return (t1 != t2); |
| 5364 | #endif |
| 5365 | } |
| 5366 | |
| 5367 | /* |
| 5368 | * Call write() to write a number of bytes to the file. |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 5369 | * Handles encryption and 'encoding' conversion. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5370 | * |
| 5371 | * Return FAIL for failure, OK otherwise. |
| 5372 | */ |
| 5373 | static int |
| 5374 | buf_write_bytes(ip) |
| 5375 | struct bw_info *ip; |
| 5376 | { |
| 5377 | int wlen; |
| 5378 | char_u *buf = ip->bw_buf; /* data to write */ |
| 5379 | int len = ip->bw_len; /* length of data */ |
| 5380 | #ifdef HAS_BW_FLAGS |
| 5381 | int flags = ip->bw_flags; /* extra flags */ |
| 5382 | #endif |
| 5383 | |
| 5384 | #ifdef FEAT_MBYTE |
| 5385 | /* |
| 5386 | * Skip conversion when writing the crypt magic number or the BOM. |
| 5387 | */ |
| 5388 | if (!(flags & FIO_NOCONVERT)) |
| 5389 | { |
| 5390 | char_u *p; |
| 5391 | unsigned c; |
| 5392 | int n; |
| 5393 | |
| 5394 | if (flags & FIO_UTF8) |
| 5395 | { |
| 5396 | /* |
| 5397 | * Convert latin1 in the buffer to UTF-8 in the file. |
| 5398 | */ |
| 5399 | p = ip->bw_conv_buf; /* translate to buffer */ |
| 5400 | for (wlen = 0; wlen < len; ++wlen) |
| 5401 | p += utf_char2bytes(buf[wlen], p); |
| 5402 | buf = ip->bw_conv_buf; |
| 5403 | len = (int)(p - ip->bw_conv_buf); |
| 5404 | } |
| 5405 | else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1)) |
| 5406 | { |
| 5407 | /* |
| 5408 | * Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or |
| 5409 | * Latin1 chars in the file. |
| 5410 | */ |
| 5411 | if (flags & FIO_LATIN1) |
| 5412 | p = buf; /* translate in-place (can only get shorter) */ |
| 5413 | else |
| 5414 | p = ip->bw_conv_buf; /* translate to buffer */ |
| 5415 | for (wlen = 0; wlen < len; wlen += n) |
| 5416 | { |
| 5417 | if (wlen == 0 && ip->bw_restlen != 0) |
| 5418 | { |
| 5419 | int l; |
| 5420 | |
| 5421 | /* Use remainder of previous call. Append the start of |
| 5422 | * buf[] to get a full sequence. Might still be too |
| 5423 | * short! */ |
| 5424 | l = CONV_RESTLEN - ip->bw_restlen; |
| 5425 | if (l > len) |
| 5426 | l = len; |
| 5427 | mch_memmove(ip->bw_rest + ip->bw_restlen, buf, (size_t)l); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5428 | n = utf_ptr2len_len(ip->bw_rest, ip->bw_restlen + l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5429 | if (n > ip->bw_restlen + len) |
| 5430 | { |
| 5431 | /* We have an incomplete byte sequence at the end to |
| 5432 | * be written. We can't convert it without the |
| 5433 | * remaining bytes. Keep them for the next call. */ |
| 5434 | if (ip->bw_restlen + len > CONV_RESTLEN) |
| 5435 | return FAIL; |
| 5436 | ip->bw_restlen += len; |
| 5437 | break; |
| 5438 | } |
| 5439 | if (n > 1) |
| 5440 | c = utf_ptr2char(ip->bw_rest); |
| 5441 | else |
| 5442 | c = ip->bw_rest[0]; |
| 5443 | if (n >= ip->bw_restlen) |
| 5444 | { |
| 5445 | n -= ip->bw_restlen; |
| 5446 | ip->bw_restlen = 0; |
| 5447 | } |
| 5448 | else |
| 5449 | { |
| 5450 | ip->bw_restlen -= n; |
| 5451 | mch_memmove(ip->bw_rest, ip->bw_rest + n, |
| 5452 | (size_t)ip->bw_restlen); |
| 5453 | n = 0; |
| 5454 | } |
| 5455 | } |
| 5456 | else |
| 5457 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5458 | n = utf_ptr2len_len(buf + wlen, len - wlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5459 | if (n > len - wlen) |
| 5460 | { |
| 5461 | /* We have an incomplete byte sequence at the end to |
| 5462 | * be written. We can't convert it without the |
| 5463 | * remaining bytes. Keep them for the next call. */ |
| 5464 | if (len - wlen > CONV_RESTLEN) |
| 5465 | return FAIL; |
| 5466 | ip->bw_restlen = len - wlen; |
| 5467 | mch_memmove(ip->bw_rest, buf + wlen, |
| 5468 | (size_t)ip->bw_restlen); |
| 5469 | break; |
| 5470 | } |
| 5471 | if (n > 1) |
| 5472 | c = utf_ptr2char(buf + wlen); |
| 5473 | else |
| 5474 | c = buf[wlen]; |
| 5475 | } |
| 5476 | |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 5477 | if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error) |
| 5478 | { |
| 5479 | ip->bw_conv_error = TRUE; |
| 5480 | ip->bw_conv_error_lnum = ip->bw_start_lnum; |
| 5481 | } |
| 5482 | if (c == NL) |
| 5483 | ++ip->bw_start_lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5484 | } |
| 5485 | if (flags & FIO_LATIN1) |
| 5486 | len = (int)(p - buf); |
| 5487 | else |
| 5488 | { |
| 5489 | buf = ip->bw_conv_buf; |
| 5490 | len = (int)(p - ip->bw_conv_buf); |
| 5491 | } |
| 5492 | } |
| 5493 | |
| 5494 | # ifdef WIN3264 |
| 5495 | else if (flags & FIO_CODEPAGE) |
| 5496 | { |
| 5497 | /* |
| 5498 | * Convert UTF-8 or codepage to UCS-2 and then to MS-Windows |
| 5499 | * codepage. |
| 5500 | */ |
| 5501 | char_u *from; |
| 5502 | size_t fromlen; |
| 5503 | char_u *to; |
| 5504 | int u8c; |
| 5505 | BOOL bad = FALSE; |
| 5506 | int needed; |
| 5507 | |
| 5508 | if (ip->bw_restlen > 0) |
| 5509 | { |
| 5510 | /* Need to concatenate the remainder of the previous call and |
| 5511 | * the bytes of the current call. Use the end of the |
| 5512 | * conversion buffer for this. */ |
| 5513 | fromlen = len + ip->bw_restlen; |
| 5514 | from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; |
| 5515 | mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); |
| 5516 | mch_memmove(from + ip->bw_restlen, buf, (size_t)len); |
| 5517 | } |
| 5518 | else |
| 5519 | { |
| 5520 | from = buf; |
| 5521 | fromlen = len; |
| 5522 | } |
| 5523 | |
| 5524 | to = ip->bw_conv_buf; |
| 5525 | if (enc_utf8) |
| 5526 | { |
| 5527 | /* Convert from UTF-8 to UCS-2, to the start of the buffer. |
| 5528 | * The buffer has been allocated to be big enough. */ |
| 5529 | while (fromlen > 0) |
| 5530 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5531 | n = (int)utf_ptr2len_len(from, (int)fromlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5532 | if (n > (int)fromlen) /* incomplete byte sequence */ |
| 5533 | break; |
| 5534 | u8c = utf_ptr2char(from); |
| 5535 | *to++ = (u8c & 0xff); |
| 5536 | *to++ = (u8c >> 8); |
| 5537 | fromlen -= n; |
| 5538 | from += n; |
| 5539 | } |
| 5540 | |
| 5541 | /* Copy remainder to ip->bw_rest[] to be used for the next |
| 5542 | * call. */ |
| 5543 | if (fromlen > CONV_RESTLEN) |
| 5544 | { |
| 5545 | /* weird overlong sequence */ |
| 5546 | ip->bw_conv_error = TRUE; |
| 5547 | return FAIL; |
| 5548 | } |
| 5549 | mch_memmove(ip->bw_rest, from, fromlen); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5550 | ip->bw_restlen = (int)fromlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5551 | } |
| 5552 | else |
| 5553 | { |
| 5554 | /* Convert from enc_codepage to UCS-2, to the start of the |
| 5555 | * buffer. The buffer has been allocated to be big enough. */ |
| 5556 | ip->bw_restlen = 0; |
| 5557 | needed = MultiByteToWideChar(enc_codepage, |
Bram Moolenaar | 36f5ac0 | 2007-05-06 12:40:34 +0000 | [diff] [blame] | 5558 | MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5559 | NULL, 0); |
| 5560 | if (needed == 0) |
| 5561 | { |
| 5562 | /* When conversion fails there may be a trailing byte. */ |
| 5563 | needed = MultiByteToWideChar(enc_codepage, |
Bram Moolenaar | 36f5ac0 | 2007-05-06 12:40:34 +0000 | [diff] [blame] | 5564 | MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen - 1, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5565 | NULL, 0); |
| 5566 | if (needed == 0) |
| 5567 | { |
| 5568 | /* Conversion doesn't work. */ |
| 5569 | ip->bw_conv_error = TRUE; |
| 5570 | return FAIL; |
| 5571 | } |
| 5572 | /* Save the trailing byte for the next call. */ |
| 5573 | ip->bw_rest[0] = from[fromlen - 1]; |
| 5574 | ip->bw_restlen = 1; |
| 5575 | } |
| 5576 | needed = MultiByteToWideChar(enc_codepage, MB_ERR_INVALID_CHARS, |
Bram Moolenaar | 36f5ac0 | 2007-05-06 12:40:34 +0000 | [diff] [blame] | 5577 | (LPCSTR)from, (int)(fromlen - ip->bw_restlen), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5578 | (LPWSTR)to, needed); |
| 5579 | if (needed == 0) |
| 5580 | { |
| 5581 | /* Safety check: Conversion doesn't work. */ |
| 5582 | ip->bw_conv_error = TRUE; |
| 5583 | return FAIL; |
| 5584 | } |
| 5585 | to += needed * 2; |
| 5586 | } |
| 5587 | |
| 5588 | fromlen = to - ip->bw_conv_buf; |
| 5589 | buf = to; |
| 5590 | # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ |
| 5591 | if (FIO_GET_CP(flags) == CP_UTF8) |
| 5592 | { |
| 5593 | /* Convert from UCS-2 to UTF-8, using the remainder of the |
| 5594 | * conversion buffer. Fails when out of space. */ |
| 5595 | for (from = ip->bw_conv_buf; fromlen > 1; fromlen -= 2) |
| 5596 | { |
| 5597 | u8c = *from++; |
| 5598 | u8c += (*from++ << 8); |
| 5599 | to += utf_char2bytes(u8c, to); |
| 5600 | if (to + 6 >= ip->bw_conv_buf + ip->bw_conv_buflen) |
| 5601 | { |
| 5602 | ip->bw_conv_error = TRUE; |
| 5603 | return FAIL; |
| 5604 | } |
| 5605 | } |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5606 | len = (int)(to - buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5607 | } |
| 5608 | else |
| 5609 | #endif |
| 5610 | { |
| 5611 | /* Convert from UCS-2 to the codepage, using the remainder of |
| 5612 | * the conversion buffer. If the conversion uses the default |
| 5613 | * character "0", the data doesn't fit in this encoding, so |
| 5614 | * fail. */ |
| 5615 | len = WideCharToMultiByte(FIO_GET_CP(flags), 0, |
| 5616 | (LPCWSTR)ip->bw_conv_buf, (int)fromlen / sizeof(WCHAR), |
Bram Moolenaar | 36f5ac0 | 2007-05-06 12:40:34 +0000 | [diff] [blame] | 5617 | (LPSTR)to, (int)(ip->bw_conv_buflen - fromlen), 0, |
| 5618 | &bad); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5619 | if (bad) |
| 5620 | { |
| 5621 | ip->bw_conv_error = TRUE; |
| 5622 | return FAIL; |
| 5623 | } |
| 5624 | } |
| 5625 | } |
| 5626 | # endif |
| 5627 | |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 5628 | # ifdef MACOS_CONVERT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5629 | else if (flags & FIO_MACROMAN) |
| 5630 | { |
| 5631 | /* |
| 5632 | * Convert UTF-8 or latin1 to Apple MacRoman. |
| 5633 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5634 | char_u *from; |
| 5635 | size_t fromlen; |
| 5636 | |
| 5637 | if (ip->bw_restlen > 0) |
| 5638 | { |
| 5639 | /* Need to concatenate the remainder of the previous call and |
| 5640 | * the bytes of the current call. Use the end of the |
| 5641 | * conversion buffer for this. */ |
| 5642 | fromlen = len + ip->bw_restlen; |
| 5643 | from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; |
| 5644 | mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); |
| 5645 | mch_memmove(from + ip->bw_restlen, buf, (size_t)len); |
| 5646 | } |
| 5647 | else |
| 5648 | { |
| 5649 | from = buf; |
| 5650 | fromlen = len; |
| 5651 | } |
| 5652 | |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 5653 | if (enc2macroman(from, fromlen, |
| 5654 | ip->bw_conv_buf, &len, ip->bw_conv_buflen, |
| 5655 | ip->bw_rest, &ip->bw_restlen) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5656 | { |
| 5657 | ip->bw_conv_error = TRUE; |
| 5658 | return FAIL; |
| 5659 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5660 | buf = ip->bw_conv_buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5661 | } |
| 5662 | # endif |
| 5663 | |
| 5664 | # ifdef USE_ICONV |
| 5665 | if (ip->bw_iconv_fd != (iconv_t)-1) |
| 5666 | { |
| 5667 | const char *from; |
| 5668 | size_t fromlen; |
| 5669 | char *to; |
| 5670 | size_t tolen; |
| 5671 | |
| 5672 | /* Convert with iconv(). */ |
| 5673 | if (ip->bw_restlen > 0) |
| 5674 | { |
Bram Moolenaar | 5d294d1 | 2009-03-11 12:11:02 +0000 | [diff] [blame] | 5675 | char *fp; |
| 5676 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5677 | /* Need to concatenate the remainder of the previous call and |
| 5678 | * the bytes of the current call. Use the end of the |
| 5679 | * conversion buffer for this. */ |
| 5680 | fromlen = len + ip->bw_restlen; |
Bram Moolenaar | 5d294d1 | 2009-03-11 12:11:02 +0000 | [diff] [blame] | 5681 | fp = (char *)ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; |
| 5682 | mch_memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen); |
| 5683 | mch_memmove(fp + ip->bw_restlen, buf, (size_t)len); |
| 5684 | from = fp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5685 | tolen = ip->bw_conv_buflen - fromlen; |
| 5686 | } |
| 5687 | else |
| 5688 | { |
| 5689 | from = (const char *)buf; |
| 5690 | fromlen = len; |
| 5691 | tolen = ip->bw_conv_buflen; |
| 5692 | } |
| 5693 | to = (char *)ip->bw_conv_buf; |
| 5694 | |
| 5695 | if (ip->bw_first) |
| 5696 | { |
| 5697 | size_t save_len = tolen; |
| 5698 | |
| 5699 | /* output the initial shift state sequence */ |
| 5700 | (void)iconv(ip->bw_iconv_fd, NULL, NULL, &to, &tolen); |
| 5701 | |
| 5702 | /* There is a bug in iconv() on Linux (which appears to be |
| 5703 | * wide-spread) which sets "to" to NULL and messes up "tolen". |
| 5704 | */ |
| 5705 | if (to == NULL) |
| 5706 | { |
| 5707 | to = (char *)ip->bw_conv_buf; |
| 5708 | tolen = save_len; |
| 5709 | } |
| 5710 | ip->bw_first = FALSE; |
| 5711 | } |
| 5712 | |
| 5713 | /* |
| 5714 | * If iconv() has an error or there is not enough room, fail. |
| 5715 | */ |
| 5716 | if ((iconv(ip->bw_iconv_fd, (void *)&from, &fromlen, &to, &tolen) |
| 5717 | == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) |
| 5718 | || fromlen > CONV_RESTLEN) |
| 5719 | { |
| 5720 | ip->bw_conv_error = TRUE; |
| 5721 | return FAIL; |
| 5722 | } |
| 5723 | |
| 5724 | /* copy remainder to ip->bw_rest[] to be used for the next call. */ |
| 5725 | if (fromlen > 0) |
| 5726 | mch_memmove(ip->bw_rest, (void *)from, fromlen); |
| 5727 | ip->bw_restlen = (int)fromlen; |
| 5728 | |
| 5729 | buf = ip->bw_conv_buf; |
| 5730 | len = (int)((char_u *)to - ip->bw_conv_buf); |
| 5731 | } |
| 5732 | # endif |
| 5733 | } |
| 5734 | #endif /* FEAT_MBYTE */ |
| 5735 | |
| 5736 | #ifdef FEAT_CRYPT |
| 5737 | if (flags & FIO_ENCRYPTED) /* encrypt the data */ |
Bram Moolenaar | 04c9baf | 2010-06-01 23:37:39 +0200 | [diff] [blame] | 5738 | crypt_encode(buf, len, buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5739 | #endif |
| 5740 | |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 5741 | wlen = write_eintr(ip->bw_fd, buf, len); |
| 5742 | return (wlen < len) ? FAIL : OK; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5743 | } |
| 5744 | |
| 5745 | #ifdef FEAT_MBYTE |
| 5746 | /* |
| 5747 | * Convert a Unicode character to bytes. |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 5748 | * Return TRUE for an error, FALSE when it's OK. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5749 | */ |
| 5750 | static int |
| 5751 | ucs2bytes(c, pp, flags) |
| 5752 | unsigned c; /* in: character */ |
| 5753 | char_u **pp; /* in/out: pointer to result */ |
| 5754 | int flags; /* FIO_ flags */ |
| 5755 | { |
| 5756 | char_u *p = *pp; |
| 5757 | int error = FALSE; |
| 5758 | int cc; |
| 5759 | |
| 5760 | |
| 5761 | if (flags & FIO_UCS4) |
| 5762 | { |
| 5763 | if (flags & FIO_ENDIAN_L) |
| 5764 | { |
| 5765 | *p++ = c; |
| 5766 | *p++ = (c >> 8); |
| 5767 | *p++ = (c >> 16); |
| 5768 | *p++ = (c >> 24); |
| 5769 | } |
| 5770 | else |
| 5771 | { |
| 5772 | *p++ = (c >> 24); |
| 5773 | *p++ = (c >> 16); |
| 5774 | *p++ = (c >> 8); |
| 5775 | *p++ = c; |
| 5776 | } |
| 5777 | } |
| 5778 | else if (flags & (FIO_UCS2 | FIO_UTF16)) |
| 5779 | { |
| 5780 | if (c >= 0x10000) |
| 5781 | { |
| 5782 | if (flags & FIO_UTF16) |
| 5783 | { |
| 5784 | /* Make two words, ten bits of the character in each. First |
| 5785 | * word is 0xd800 - 0xdbff, second one 0xdc00 - 0xdfff */ |
| 5786 | c -= 0x10000; |
| 5787 | if (c >= 0x100000) |
| 5788 | error = TRUE; |
| 5789 | cc = ((c >> 10) & 0x3ff) + 0xd800; |
| 5790 | if (flags & FIO_ENDIAN_L) |
| 5791 | { |
| 5792 | *p++ = cc; |
| 5793 | *p++ = ((unsigned)cc >> 8); |
| 5794 | } |
| 5795 | else |
| 5796 | { |
| 5797 | *p++ = ((unsigned)cc >> 8); |
| 5798 | *p++ = cc; |
| 5799 | } |
| 5800 | c = (c & 0x3ff) + 0xdc00; |
| 5801 | } |
| 5802 | else |
| 5803 | error = TRUE; |
| 5804 | } |
| 5805 | if (flags & FIO_ENDIAN_L) |
| 5806 | { |
| 5807 | *p++ = c; |
| 5808 | *p++ = (c >> 8); |
| 5809 | } |
| 5810 | else |
| 5811 | { |
| 5812 | *p++ = (c >> 8); |
| 5813 | *p++ = c; |
| 5814 | } |
| 5815 | } |
| 5816 | else /* Latin1 */ |
| 5817 | { |
| 5818 | if (c >= 0x100) |
| 5819 | { |
| 5820 | error = TRUE; |
| 5821 | *p++ = 0xBF; |
| 5822 | } |
| 5823 | else |
| 5824 | *p++ = c; |
| 5825 | } |
| 5826 | |
| 5827 | *pp = p; |
| 5828 | return error; |
| 5829 | } |
| 5830 | |
| 5831 | /* |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 5832 | * Return TRUE if file encoding "fenc" requires conversion from or to |
| 5833 | * 'encoding'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5834 | */ |
| 5835 | static int |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 5836 | need_conversion(fenc) |
| 5837 | char_u *fenc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5838 | { |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 5839 | int same_encoding; |
| 5840 | int enc_flags; |
| 5841 | int fenc_flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5842 | |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 5843 | if (*fenc == NUL || STRCMP(p_enc, fenc) == 0) |
Bram Moolenaar | 442b422 | 2010-05-24 21:34:22 +0200 | [diff] [blame] | 5844 | { |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 5845 | same_encoding = TRUE; |
Bram Moolenaar | 442b422 | 2010-05-24 21:34:22 +0200 | [diff] [blame] | 5846 | fenc_flags = 0; |
| 5847 | } |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 5848 | else |
| 5849 | { |
| 5850 | /* Ignore difference between "ansi" and "latin1", "ucs-4" and |
| 5851 | * "ucs-4be", etc. */ |
| 5852 | enc_flags = get_fio_flags(p_enc); |
| 5853 | fenc_flags = get_fio_flags(fenc); |
| 5854 | same_encoding = (enc_flags != 0 && fenc_flags == enc_flags); |
| 5855 | } |
| 5856 | if (same_encoding) |
| 5857 | { |
| 5858 | /* Specified encoding matches with 'encoding'. This requires |
| 5859 | * conversion when 'encoding' is Unicode but not UTF-8. */ |
| 5860 | return enc_unicode != 0; |
| 5861 | } |
| 5862 | |
| 5863 | /* Encodings differ. However, conversion is not needed when 'enc' is any |
| 5864 | * Unicode encoding and the file is UTF-8. */ |
| 5865 | return !(enc_utf8 && fenc_flags == FIO_UTF8); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5866 | } |
| 5867 | |
| 5868 | /* |
| 5869 | * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the |
| 5870 | * internal conversion. |
| 5871 | * if "ptr" is an empty string, use 'encoding'. |
| 5872 | */ |
| 5873 | static int |
| 5874 | get_fio_flags(ptr) |
| 5875 | char_u *ptr; |
| 5876 | { |
| 5877 | int prop; |
| 5878 | |
| 5879 | if (*ptr == NUL) |
| 5880 | ptr = p_enc; |
| 5881 | |
| 5882 | prop = enc_canon_props(ptr); |
| 5883 | if (prop & ENC_UNICODE) |
| 5884 | { |
| 5885 | if (prop & ENC_2BYTE) |
| 5886 | { |
| 5887 | if (prop & ENC_ENDIAN_L) |
| 5888 | return FIO_UCS2 | FIO_ENDIAN_L; |
| 5889 | return FIO_UCS2; |
| 5890 | } |
| 5891 | if (prop & ENC_4BYTE) |
| 5892 | { |
| 5893 | if (prop & ENC_ENDIAN_L) |
| 5894 | return FIO_UCS4 | FIO_ENDIAN_L; |
| 5895 | return FIO_UCS4; |
| 5896 | } |
| 5897 | if (prop & ENC_2WORD) |
| 5898 | { |
| 5899 | if (prop & ENC_ENDIAN_L) |
| 5900 | return FIO_UTF16 | FIO_ENDIAN_L; |
| 5901 | return FIO_UTF16; |
| 5902 | } |
| 5903 | return FIO_UTF8; |
| 5904 | } |
| 5905 | if (prop & ENC_LATIN1) |
| 5906 | return FIO_LATIN1; |
| 5907 | /* must be ENC_DBCS, requires iconv() */ |
| 5908 | return 0; |
| 5909 | } |
| 5910 | |
| 5911 | #ifdef WIN3264 |
| 5912 | /* |
| 5913 | * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed |
| 5914 | * for the conversion MS-Windows can do for us. Also accept "utf-8". |
| 5915 | * Used for conversion between 'encoding' and 'fileencoding'. |
| 5916 | */ |
| 5917 | static int |
| 5918 | get_win_fio_flags(ptr) |
| 5919 | char_u *ptr; |
| 5920 | { |
| 5921 | int cp; |
| 5922 | |
| 5923 | /* Cannot do this when 'encoding' is not utf-8 and not a codepage. */ |
| 5924 | if (!enc_utf8 && enc_codepage <= 0) |
| 5925 | return 0; |
| 5926 | |
| 5927 | cp = encname2codepage(ptr); |
| 5928 | if (cp == 0) |
| 5929 | { |
| 5930 | # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ |
| 5931 | if (STRCMP(ptr, "utf-8") == 0) |
| 5932 | cp = CP_UTF8; |
| 5933 | else |
| 5934 | # endif |
| 5935 | return 0; |
| 5936 | } |
| 5937 | return FIO_PUT_CP(cp) | FIO_CODEPAGE; |
| 5938 | } |
| 5939 | #endif |
| 5940 | |
| 5941 | #ifdef MACOS_X |
| 5942 | /* |
| 5943 | * Check "ptr" for a Carbon supported encoding and return the FIO_ flags |
| 5944 | * needed for the internal conversion to/from utf-8 or latin1. |
| 5945 | */ |
| 5946 | static int |
| 5947 | get_mac_fio_flags(ptr) |
| 5948 | char_u *ptr; |
| 5949 | { |
| 5950 | if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0) |
| 5951 | && (enc_canon_props(ptr) & ENC_MACROMAN)) |
| 5952 | return FIO_MACROMAN; |
| 5953 | return 0; |
| 5954 | } |
| 5955 | #endif |
| 5956 | |
| 5957 | /* |
| 5958 | * Check for a Unicode BOM (Byte Order Mark) at the start of p[size]. |
| 5959 | * "size" must be at least 2. |
| 5960 | * Return the name of the encoding and set "*lenp" to the length. |
| 5961 | * Returns NULL when no BOM found. |
| 5962 | */ |
| 5963 | static char_u * |
| 5964 | check_for_bom(p, size, lenp, flags) |
| 5965 | char_u *p; |
| 5966 | long size; |
| 5967 | int *lenp; |
| 5968 | int flags; |
| 5969 | { |
| 5970 | char *name = NULL; |
| 5971 | int len = 2; |
| 5972 | |
| 5973 | if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf |
Bram Moolenaar | ee0f5a6 | 2008-07-24 20:09:16 +0000 | [diff] [blame] | 5974 | && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5975 | { |
| 5976 | name = "utf-8"; /* EF BB BF */ |
| 5977 | len = 3; |
| 5978 | } |
| 5979 | else if (p[0] == 0xff && p[1] == 0xfe) |
| 5980 | { |
| 5981 | if (size >= 4 && p[2] == 0 && p[3] == 0 |
| 5982 | && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L))) |
| 5983 | { |
| 5984 | name = "ucs-4le"; /* FF FE 00 00 */ |
| 5985 | len = 4; |
| 5986 | } |
Bram Moolenaar | 223a189 | 2008-11-11 20:57:11 +0000 | [diff] [blame] | 5987 | else if (flags == (FIO_UCS2 | FIO_ENDIAN_L)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5988 | name = "ucs-2le"; /* FF FE */ |
Bram Moolenaar | 223a189 | 2008-11-11 20:57:11 +0000 | [diff] [blame] | 5989 | else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L)) |
| 5990 | /* utf-16le is preferred, it also works for ucs-2le text */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5991 | name = "utf-16le"; /* FF FE */ |
| 5992 | } |
| 5993 | else if (p[0] == 0xfe && p[1] == 0xff |
| 5994 | && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16)) |
| 5995 | { |
Bram Moolenaar | ffd82c5 | 2008-02-20 17:15:26 +0000 | [diff] [blame] | 5996 | /* Default to utf-16, it works also for ucs-2 text. */ |
| 5997 | if (flags == FIO_UCS2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5998 | name = "ucs-2"; /* FE FF */ |
Bram Moolenaar | ffd82c5 | 2008-02-20 17:15:26 +0000 | [diff] [blame] | 5999 | else |
| 6000 | name = "utf-16"; /* FE FF */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6001 | } |
| 6002 | else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe |
| 6003 | && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4)) |
| 6004 | { |
| 6005 | name = "ucs-4"; /* 00 00 FE FF */ |
| 6006 | len = 4; |
| 6007 | } |
| 6008 | |
| 6009 | *lenp = len; |
| 6010 | return (char_u *)name; |
| 6011 | } |
| 6012 | |
| 6013 | /* |
| 6014 | * Generate a BOM in "buf[4]" for encoding "name". |
| 6015 | * Return the length of the BOM (zero when no BOM). |
| 6016 | */ |
| 6017 | static int |
| 6018 | make_bom(buf, name) |
| 6019 | char_u *buf; |
| 6020 | char_u *name; |
| 6021 | { |
| 6022 | int flags; |
| 6023 | char_u *p; |
| 6024 | |
| 6025 | flags = get_fio_flags(name); |
| 6026 | |
| 6027 | /* Can't put a BOM in a non-Unicode file. */ |
| 6028 | if (flags == FIO_LATIN1 || flags == 0) |
| 6029 | return 0; |
| 6030 | |
| 6031 | if (flags == FIO_UTF8) /* UTF-8 */ |
| 6032 | { |
| 6033 | buf[0] = 0xef; |
| 6034 | buf[1] = 0xbb; |
| 6035 | buf[2] = 0xbf; |
| 6036 | return 3; |
| 6037 | } |
| 6038 | p = buf; |
| 6039 | (void)ucs2bytes(0xfeff, &p, flags); |
| 6040 | return (int)(p - buf); |
| 6041 | } |
| 6042 | #endif |
| 6043 | |
Bram Moolenaar | d4cacdf | 2007-10-03 10:50:10 +0000 | [diff] [blame] | 6044 | #if defined(FEAT_VIMINFO) || defined(FEAT_BROWSE) || \ |
Bram Moolenaar | a0174af | 2008-01-02 20:08:25 +0000 | [diff] [blame] | 6045 | defined(FEAT_QUICKFIX) || defined(FEAT_AUTOCMD) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6046 | /* |
| 6047 | * Try to find a shortname by comparing the fullname with the current |
| 6048 | * directory. |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 6049 | * Returns "full_path" or pointer into "full_path" if shortened. |
| 6050 | */ |
| 6051 | char_u * |
| 6052 | shorten_fname1(full_path) |
| 6053 | char_u *full_path; |
| 6054 | { |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 6055 | char_u *dirname; |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 6056 | char_u *p = full_path; |
| 6057 | |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 6058 | dirname = alloc(MAXPATHL); |
| 6059 | if (dirname == NULL) |
| 6060 | return full_path; |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 6061 | if (mch_dirname(dirname, MAXPATHL) == OK) |
| 6062 | { |
| 6063 | p = shorten_fname(full_path, dirname); |
| 6064 | if (p == NULL || *p == NUL) |
| 6065 | p = full_path; |
| 6066 | } |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 6067 | vim_free(dirname); |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 6068 | return p; |
| 6069 | } |
Bram Moolenaar | d4cacdf | 2007-10-03 10:50:10 +0000 | [diff] [blame] | 6070 | #endif |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 6071 | |
| 6072 | /* |
| 6073 | * Try to find a shortname by comparing the fullname with the current |
| 6074 | * directory. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6075 | * Returns NULL if not shorter name possible, pointer into "full_path" |
| 6076 | * otherwise. |
| 6077 | */ |
| 6078 | char_u * |
| 6079 | shorten_fname(full_path, dir_name) |
| 6080 | char_u *full_path; |
| 6081 | char_u *dir_name; |
| 6082 | { |
| 6083 | int len; |
| 6084 | char_u *p; |
| 6085 | |
| 6086 | if (full_path == NULL) |
| 6087 | return NULL; |
| 6088 | len = (int)STRLEN(dir_name); |
| 6089 | if (fnamencmp(dir_name, full_path, len) == 0) |
| 6090 | { |
| 6091 | p = full_path + len; |
| 6092 | #if defined(MSDOS) || defined(MSWIN) || defined(OS2) |
| 6093 | /* |
| 6094 | * MSDOS: when a file is in the root directory, dir_name will end in a |
| 6095 | * slash, since C: by itself does not define a specific dir. In this |
| 6096 | * case p may already be correct. <negri> |
| 6097 | */ |
| 6098 | if (!((len > 2) && (*(p - 2) == ':'))) |
| 6099 | #endif |
| 6100 | { |
| 6101 | if (vim_ispathsep(*p)) |
| 6102 | ++p; |
| 6103 | #ifndef VMS /* the path separator is always part of the path */ |
| 6104 | else |
| 6105 | p = NULL; |
| 6106 | #endif |
| 6107 | } |
| 6108 | } |
| 6109 | #if defined(MSDOS) || defined(MSWIN) || defined(OS2) |
| 6110 | /* |
| 6111 | * When using a file in the current drive, remove the drive name: |
| 6112 | * "A:\dir\file" -> "\dir\file". This helps when moving a session file on |
| 6113 | * a floppy from "A:\dir" to "B:\dir". |
| 6114 | */ |
| 6115 | else if (len > 3 |
| 6116 | && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0]) |
| 6117 | && full_path[1] == ':' |
| 6118 | && vim_ispathsep(full_path[2])) |
| 6119 | p = full_path + 2; |
| 6120 | #endif |
| 6121 | else |
| 6122 | p = NULL; |
| 6123 | return p; |
| 6124 | } |
| 6125 | |
| 6126 | /* |
| 6127 | * Shorten filenames for all buffers. |
| 6128 | * When "force" is TRUE: Use full path from now on for files currently being |
| 6129 | * edited, both for file name and swap file name. Try to shorten the file |
| 6130 | * names a bit, if safe to do so. |
| 6131 | * When "force" is FALSE: Only try to shorten absolute file names. |
| 6132 | * For buffers that have buftype "nofile" or "scratch": never change the file |
| 6133 | * name. |
| 6134 | */ |
| 6135 | void |
| 6136 | shorten_fnames(force) |
| 6137 | int force; |
| 6138 | { |
| 6139 | char_u dirname[MAXPATHL]; |
| 6140 | buf_T *buf; |
| 6141 | char_u *p; |
| 6142 | |
| 6143 | mch_dirname(dirname, MAXPATHL); |
| 6144 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 6145 | { |
| 6146 | if (buf->b_fname != NULL |
| 6147 | #ifdef FEAT_QUICKFIX |
| 6148 | && !bt_nofile(buf) |
| 6149 | #endif |
| 6150 | && !path_with_url(buf->b_fname) |
| 6151 | && (force |
| 6152 | || buf->b_sfname == NULL |
| 6153 | || mch_isFullName(buf->b_sfname))) |
| 6154 | { |
| 6155 | vim_free(buf->b_sfname); |
| 6156 | buf->b_sfname = NULL; |
| 6157 | p = shorten_fname(buf->b_ffname, dirname); |
| 6158 | if (p != NULL) |
| 6159 | { |
| 6160 | buf->b_sfname = vim_strsave(p); |
| 6161 | buf->b_fname = buf->b_sfname; |
| 6162 | } |
| 6163 | if (p == NULL || buf->b_fname == NULL) |
| 6164 | buf->b_fname = buf->b_ffname; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6165 | } |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 6166 | |
| 6167 | /* Always make the swap file name a full path, a "nofile" buffer may |
| 6168 | * also have a swap file. */ |
| 6169 | mf_fullname(buf->b_ml.ml_mfp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6170 | } |
| 6171 | #ifdef FEAT_WINDOWS |
| 6172 | status_redraw_all(); |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 6173 | redraw_tabline = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6174 | #endif |
| 6175 | } |
| 6176 | |
| 6177 | #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \ |
| 6178 | || defined(FEAT_GUI_MSWIN) \ |
| 6179 | || defined(FEAT_GUI_MAC) \ |
| 6180 | || defined(PROTO) |
| 6181 | /* |
| 6182 | * Shorten all filenames in "fnames[count]" by current directory. |
| 6183 | */ |
| 6184 | void |
| 6185 | shorten_filenames(fnames, count) |
| 6186 | char_u **fnames; |
| 6187 | int count; |
| 6188 | { |
| 6189 | int i; |
| 6190 | char_u dirname[MAXPATHL]; |
| 6191 | char_u *p; |
| 6192 | |
| 6193 | if (fnames == NULL || count < 1) |
| 6194 | return; |
| 6195 | mch_dirname(dirname, sizeof(dirname)); |
| 6196 | for (i = 0; i < count; ++i) |
| 6197 | { |
| 6198 | if ((p = shorten_fname(fnames[i], dirname)) != NULL) |
| 6199 | { |
| 6200 | /* shorten_fname() returns pointer in given "fnames[i]". If free |
| 6201 | * "fnames[i]" first, "p" becomes invalid. So we need to copy |
| 6202 | * "p" first then free fnames[i]. */ |
| 6203 | p = vim_strsave(p); |
| 6204 | vim_free(fnames[i]); |
| 6205 | fnames[i] = p; |
| 6206 | } |
| 6207 | } |
| 6208 | } |
| 6209 | #endif |
| 6210 | |
| 6211 | /* |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 6212 | * 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] | 6213 | * fo_o_h.ext for MSDOS or when shortname option set. |
| 6214 | * |
| 6215 | * Assumed that fname is a valid name found in the filesystem we assure that |
| 6216 | * the return value is a different name and ends in 'ext'. |
| 6217 | * "ext" MUST be at most 4 characters long if it starts with a dot, 3 |
| 6218 | * characters otherwise. |
| 6219 | * Space for the returned name is allocated, must be freed later. |
| 6220 | * Returns NULL when out of memory. |
| 6221 | */ |
| 6222 | char_u * |
| 6223 | modname(fname, ext, prepend_dot) |
| 6224 | char_u *fname, *ext; |
| 6225 | int prepend_dot; /* may prepend a '.' to file name */ |
| 6226 | { |
| 6227 | return buf_modname( |
| 6228 | #ifdef SHORT_FNAME |
| 6229 | TRUE, |
| 6230 | #else |
| 6231 | (curbuf->b_p_sn || curbuf->b_shortname), |
| 6232 | #endif |
| 6233 | fname, ext, prepend_dot); |
| 6234 | } |
| 6235 | |
| 6236 | char_u * |
| 6237 | buf_modname(shortname, fname, ext, prepend_dot) |
| 6238 | int shortname; /* use 8.3 file name */ |
| 6239 | char_u *fname, *ext; |
| 6240 | int prepend_dot; /* may prepend a '.' to file name */ |
| 6241 | { |
| 6242 | char_u *retval; |
| 6243 | char_u *s; |
| 6244 | char_u *e; |
| 6245 | char_u *ptr; |
| 6246 | int fnamelen, extlen; |
| 6247 | |
| 6248 | extlen = (int)STRLEN(ext); |
| 6249 | |
| 6250 | /* |
| 6251 | * If there is no file name we must get the name of the current directory |
| 6252 | * (we need the full path in case :cd is used). |
| 6253 | */ |
| 6254 | if (fname == NULL || *fname == NUL) |
| 6255 | { |
| 6256 | retval = alloc((unsigned)(MAXPATHL + extlen + 3)); |
| 6257 | if (retval == NULL) |
| 6258 | return NULL; |
| 6259 | if (mch_dirname(retval, MAXPATHL) == FAIL || |
| 6260 | (fnamelen = (int)STRLEN(retval)) == 0) |
| 6261 | { |
| 6262 | vim_free(retval); |
| 6263 | return NULL; |
| 6264 | } |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 6265 | if (!after_pathsep(retval, retval + fnamelen)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6266 | { |
| 6267 | retval[fnamelen++] = PATHSEP; |
| 6268 | retval[fnamelen] = NUL; |
| 6269 | } |
| 6270 | #ifndef SHORT_FNAME |
| 6271 | prepend_dot = FALSE; /* nothing to prepend a dot to */ |
| 6272 | #endif |
| 6273 | } |
| 6274 | else |
| 6275 | { |
| 6276 | fnamelen = (int)STRLEN(fname); |
| 6277 | retval = alloc((unsigned)(fnamelen + extlen + 3)); |
| 6278 | if (retval == NULL) |
| 6279 | return NULL; |
| 6280 | STRCPY(retval, fname); |
| 6281 | #ifdef VMS |
| 6282 | vms_remove_version(retval); /* we do not need versions here */ |
| 6283 | #endif |
| 6284 | } |
| 6285 | |
| 6286 | /* |
| 6287 | * search backwards until we hit a '/', '\' or ':' replacing all '.' |
| 6288 | * by '_' for MSDOS or when shortname option set and ext starts with a dot. |
| 6289 | * Then truncate what is after the '/', '\' or ':' to 8 characters for |
| 6290 | * MSDOS and 26 characters for AMIGA, a lot more for UNIX. |
| 6291 | */ |
Bram Moolenaar | 53180ce | 2005-07-05 21:48:14 +0000 | [diff] [blame] | 6292 | for (ptr = retval + fnamelen; ptr > retval; mb_ptr_back(retval, ptr)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6293 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6294 | if (*ext == '.' |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 6295 | #ifdef USE_LONG_FNAME |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6296 | && (!USE_LONG_FNAME || shortname) |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 6297 | #else |
| 6298 | # ifndef SHORT_FNAME |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6299 | && shortname |
| 6300 | # endif |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 6301 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6302 | ) |
| 6303 | if (*ptr == '.') /* replace '.' by '_' */ |
| 6304 | *ptr = '_'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6305 | if (vim_ispathsep(*ptr)) |
Bram Moolenaar | 53180ce | 2005-07-05 21:48:14 +0000 | [diff] [blame] | 6306 | { |
| 6307 | ++ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6308 | break; |
Bram Moolenaar | 53180ce | 2005-07-05 21:48:14 +0000 | [diff] [blame] | 6309 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6310 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6311 | |
| 6312 | /* the file name has at most BASENAMELEN characters. */ |
| 6313 | #ifndef SHORT_FNAME |
| 6314 | if (STRLEN(ptr) > (unsigned)BASENAMELEN) |
| 6315 | ptr[BASENAMELEN] = '\0'; |
| 6316 | #endif |
| 6317 | |
| 6318 | s = ptr + STRLEN(ptr); |
| 6319 | |
| 6320 | /* |
| 6321 | * For 8.3 file names we may have to reduce the length. |
| 6322 | */ |
| 6323 | #ifdef USE_LONG_FNAME |
| 6324 | if (!USE_LONG_FNAME || shortname) |
| 6325 | #else |
| 6326 | # ifndef SHORT_FNAME |
| 6327 | if (shortname) |
| 6328 | # endif |
| 6329 | #endif |
| 6330 | { |
| 6331 | /* |
| 6332 | * If there is no file name, or the file name ends in '/', and the |
| 6333 | * extension starts with '.', put a '_' before the dot, because just |
| 6334 | * ".ext" is invalid. |
| 6335 | */ |
| 6336 | if (fname == NULL || *fname == NUL |
| 6337 | || vim_ispathsep(fname[STRLEN(fname) - 1])) |
| 6338 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6339 | if (*ext == '.') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6340 | *s++ = '_'; |
| 6341 | } |
| 6342 | /* |
| 6343 | * If the extension starts with '.', truncate the base name at 8 |
| 6344 | * characters |
| 6345 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6346 | else if (*ext == '.') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6347 | { |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 6348 | if ((size_t)(s - ptr) > (size_t)8) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6349 | { |
| 6350 | s = ptr + 8; |
| 6351 | *s = '\0'; |
| 6352 | } |
| 6353 | } |
| 6354 | /* |
| 6355 | * If the extension doesn't start with '.', and the file name |
| 6356 | * doesn't have an extension yet, append a '.' |
| 6357 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6358 | else if ((e = vim_strchr(ptr, '.')) == NULL) |
| 6359 | *s++ = '.'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6360 | /* |
| 6361 | * If the extension doesn't start with '.', and there already is an |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 6362 | * extension, it may need to be truncated |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6363 | */ |
| 6364 | else if ((int)STRLEN(e) + extlen > 4) |
| 6365 | s = e + 4 - extlen; |
| 6366 | } |
| 6367 | #if defined(OS2) || defined(USE_LONG_FNAME) || defined(WIN3264) |
| 6368 | /* |
| 6369 | * If there is no file name, and the extension starts with '.', put a |
| 6370 | * '_' before the dot, because just ".ext" may be invalid if it's on a |
| 6371 | * FAT partition, and on HPFS it doesn't matter. |
| 6372 | */ |
| 6373 | else if ((fname == NULL || *fname == NUL) && *ext == '.') |
| 6374 | *s++ = '_'; |
| 6375 | #endif |
| 6376 | |
| 6377 | /* |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 6378 | * Append the extension. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6379 | * ext can start with '.' and cannot exceed 3 more characters. |
| 6380 | */ |
| 6381 | STRCPY(s, ext); |
| 6382 | |
| 6383 | #ifndef SHORT_FNAME |
| 6384 | /* |
| 6385 | * Prepend the dot. |
| 6386 | */ |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 6387 | if (prepend_dot && !shortname && *(e = gettail(retval)) != '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6388 | #ifdef USE_LONG_FNAME |
| 6389 | && USE_LONG_FNAME |
| 6390 | #endif |
| 6391 | ) |
| 6392 | { |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 6393 | STRMOVE(e + 1, e); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6394 | *e = '.'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6395 | } |
| 6396 | #endif |
| 6397 | |
| 6398 | /* |
| 6399 | * Check that, after appending the extension, the file name is really |
| 6400 | * different. |
| 6401 | */ |
| 6402 | if (fname != NULL && STRCMP(fname, retval) == 0) |
| 6403 | { |
| 6404 | /* we search for a character that can be replaced by '_' */ |
| 6405 | while (--s >= ptr) |
| 6406 | { |
| 6407 | if (*s != '_') |
| 6408 | { |
| 6409 | *s = '_'; |
| 6410 | break; |
| 6411 | } |
| 6412 | } |
| 6413 | if (s < ptr) /* fname was "________.<ext>", how tricky! */ |
| 6414 | *ptr = 'v'; |
| 6415 | } |
| 6416 | return retval; |
| 6417 | } |
| 6418 | |
| 6419 | /* |
| 6420 | * Like fgets(), but if the file line is too long, it is truncated and the |
| 6421 | * rest of the line is thrown away. Returns TRUE for end-of-file. |
| 6422 | */ |
| 6423 | int |
| 6424 | vim_fgets(buf, size, fp) |
| 6425 | char_u *buf; |
| 6426 | int size; |
| 6427 | FILE *fp; |
| 6428 | { |
| 6429 | char *eof; |
| 6430 | #define FGETS_SIZE 200 |
| 6431 | char tbuf[FGETS_SIZE]; |
| 6432 | |
| 6433 | buf[size - 2] = NUL; |
| 6434 | #ifdef USE_CR |
| 6435 | eof = fgets_cr((char *)buf, size, fp); |
| 6436 | #else |
| 6437 | eof = fgets((char *)buf, size, fp); |
| 6438 | #endif |
| 6439 | if (buf[size - 2] != NUL && buf[size - 2] != '\n') |
| 6440 | { |
| 6441 | buf[size - 1] = NUL; /* Truncate the line */ |
| 6442 | |
| 6443 | /* Now throw away the rest of the line: */ |
| 6444 | do |
| 6445 | { |
| 6446 | tbuf[FGETS_SIZE - 2] = NUL; |
| 6447 | #ifdef USE_CR |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 6448 | ignoredp = fgets_cr((char *)tbuf, FGETS_SIZE, fp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6449 | #else |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 6450 | ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6451 | #endif |
| 6452 | } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n'); |
| 6453 | } |
| 6454 | return (eof == NULL); |
| 6455 | } |
| 6456 | |
| 6457 | #if defined(USE_CR) || defined(PROTO) |
| 6458 | /* |
| 6459 | * Like vim_fgets(), but accept any line terminator: CR, CR-LF or LF. |
| 6460 | * Returns TRUE for end-of-file. |
| 6461 | * Only used for the Mac, because it's much slower than vim_fgets(). |
| 6462 | */ |
| 6463 | int |
| 6464 | tag_fgets(buf, size, fp) |
| 6465 | char_u *buf; |
| 6466 | int size; |
| 6467 | FILE *fp; |
| 6468 | { |
| 6469 | int i = 0; |
| 6470 | int c; |
| 6471 | int eof = FALSE; |
| 6472 | |
| 6473 | for (;;) |
| 6474 | { |
| 6475 | c = fgetc(fp); |
| 6476 | if (c == EOF) |
| 6477 | { |
| 6478 | eof = TRUE; |
| 6479 | break; |
| 6480 | } |
| 6481 | if (c == '\r') |
| 6482 | { |
| 6483 | /* Always store a NL for end-of-line. */ |
| 6484 | if (i < size - 1) |
| 6485 | buf[i++] = '\n'; |
| 6486 | c = fgetc(fp); |
| 6487 | if (c != '\n') /* Macintosh format: single CR. */ |
| 6488 | ungetc(c, fp); |
| 6489 | break; |
| 6490 | } |
| 6491 | if (i < size - 1) |
| 6492 | buf[i++] = c; |
| 6493 | if (c == '\n') |
| 6494 | break; |
| 6495 | } |
| 6496 | buf[i] = NUL; |
| 6497 | return eof; |
| 6498 | } |
| 6499 | #endif |
| 6500 | |
| 6501 | /* |
| 6502 | * rename() only works if both files are on the same file system, this |
| 6503 | * function will (attempts to?) copy the file across if rename fails -- webb |
| 6504 | * Return -1 for failure, 0 for success. |
| 6505 | */ |
| 6506 | int |
| 6507 | vim_rename(from, to) |
| 6508 | char_u *from; |
| 6509 | char_u *to; |
| 6510 | { |
| 6511 | int fd_in; |
| 6512 | int fd_out; |
| 6513 | int n; |
| 6514 | char *errmsg = NULL; |
| 6515 | char *buffer; |
| 6516 | #ifdef AMIGA |
| 6517 | BPTR flock; |
| 6518 | #endif |
| 6519 | struct stat st; |
Bram Moolenaar | 9be038d | 2005-03-08 22:34:32 +0000 | [diff] [blame] | 6520 | long perm; |
Bram Moolenaar | cd71fa3 | 2005-03-11 22:46:48 +0000 | [diff] [blame] | 6521 | #ifdef HAVE_ACL |
| 6522 | vim_acl_T acl; /* ACL from original file */ |
| 6523 | #endif |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6524 | int use_tmp_file = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6525 | |
| 6526 | /* |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6527 | * When the names are identical, there is nothing to do. When they refer |
| 6528 | * to the same file (ignoring case and slash/backslash differences) but |
| 6529 | * the file name differs we need to go through a temp file. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6530 | */ |
| 6531 | if (fnamecmp(from, to) == 0) |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6532 | { |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 6533 | if (p_fic && STRCMP(gettail(from), gettail(to)) != 0) |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6534 | use_tmp_file = TRUE; |
| 6535 | else |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6536 | return 0; |
| 6537 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6538 | |
| 6539 | /* |
| 6540 | * Fail if the "from" file doesn't exist. Avoids that "to" is deleted. |
| 6541 | */ |
| 6542 | if (mch_stat((char *)from, &st) < 0) |
| 6543 | return -1; |
| 6544 | |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6545 | #ifdef UNIX |
| 6546 | { |
| 6547 | struct stat st_to; |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6548 | |
| 6549 | /* It's possible for the source and destination to be the same file. |
| 6550 | * This happens when "from" and "to" differ in case and are on a FAT32 |
| 6551 | * filesystem. In that case go through a temp file name. */ |
| 6552 | if (mch_stat((char *)to, &st_to) >= 0 |
| 6553 | && st.st_dev == st_to.st_dev |
| 6554 | && st.st_ino == st_to.st_ino) |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6555 | use_tmp_file = TRUE; |
| 6556 | } |
| 6557 | #endif |
Bram Moolenaar | 1c32dff | 2011-05-05 16:41:24 +0200 | [diff] [blame] | 6558 | #ifdef WIN3264 |
| 6559 | { |
| 6560 | BY_HANDLE_FILE_INFORMATION info1, info2; |
| 6561 | |
| 6562 | /* It's possible for the source and destination to be the same file. |
| 6563 | * In that case go through a temp file name. This makes rename("foo", |
| 6564 | * "./foo") a no-op (in a complicated way). */ |
| 6565 | if (win32_fileinfo(from, &info1) == FILEINFO_OK |
| 6566 | && win32_fileinfo(to, &info2) == FILEINFO_OK |
| 6567 | && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber |
| 6568 | && info1.nFileIndexHigh == info2.nFileIndexHigh |
| 6569 | && info1.nFileIndexLow == info2.nFileIndexLow) |
| 6570 | use_tmp_file = TRUE; |
| 6571 | } |
| 6572 | #endif |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6573 | |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6574 | if (use_tmp_file) |
| 6575 | { |
| 6576 | char tempname[MAXPATHL + 1]; |
| 6577 | |
| 6578 | /* |
| 6579 | * Find a name that doesn't exist and is in the same directory. |
| 6580 | * Rename "from" to "tempname" and then rename "tempname" to "to". |
| 6581 | */ |
| 6582 | if (STRLEN(from) >= MAXPATHL - 5) |
| 6583 | return -1; |
| 6584 | STRCPY(tempname, from); |
| 6585 | for (n = 123; n < 99999; ++n) |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6586 | { |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6587 | sprintf((char *)gettail((char_u *)tempname), "%d", n); |
| 6588 | if (mch_stat(tempname, &st) < 0) |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6589 | { |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6590 | if (mch_rename((char *)from, tempname) == 0) |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6591 | { |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6592 | if (mch_rename(tempname, (char *)to) == 0) |
| 6593 | return 0; |
| 6594 | /* Strange, the second step failed. Try moving the |
| 6595 | * file back and return failure. */ |
| 6596 | mch_rename(tempname, (char *)from); |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6597 | return -1; |
| 6598 | } |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6599 | /* If it fails for one temp name it will most likely fail |
| 6600 | * for any temp name, give up. */ |
| 6601 | return -1; |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6602 | } |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6603 | } |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6604 | return -1; |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6605 | } |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6606 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6607 | /* |
| 6608 | * Delete the "to" file, this is required on some systems to make the |
| 6609 | * mch_rename() work, on other systems it makes sure that we don't have |
| 6610 | * two files when the mch_rename() fails. |
| 6611 | */ |
| 6612 | |
| 6613 | #ifdef AMIGA |
| 6614 | /* |
| 6615 | * With MSDOS-compatible filesystems (crossdos, messydos) it is possible |
| 6616 | * 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] | 6617 | * though the names are different. To avoid the chance of accidentally |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6618 | * deleting the "from" file (horror!) we lock it during the remove. |
| 6619 | * |
| 6620 | * When used for making a backup before writing the file: This should not |
| 6621 | * happen with ":w", because startscript() should detect this problem and |
| 6622 | * set buf->b_shortname, causing modname() to return a correct ".bak" file |
| 6623 | * name. This problem does exist with ":w filename", but then the |
| 6624 | * original file will be somewhere else so the backup isn't really |
| 6625 | * important. If autoscripting is off the rename may fail. |
| 6626 | */ |
| 6627 | flock = Lock((UBYTE *)from, (long)ACCESS_READ); |
| 6628 | #endif |
| 6629 | mch_remove(to); |
| 6630 | #ifdef AMIGA |
| 6631 | if (flock) |
| 6632 | UnLock(flock); |
| 6633 | #endif |
| 6634 | |
| 6635 | /* |
| 6636 | * First try a normal rename, return if it works. |
| 6637 | */ |
| 6638 | if (mch_rename((char *)from, (char *)to) == 0) |
| 6639 | return 0; |
| 6640 | |
| 6641 | /* |
| 6642 | * Rename() failed, try copying the file. |
| 6643 | */ |
Bram Moolenaar | 9be038d | 2005-03-08 22:34:32 +0000 | [diff] [blame] | 6644 | perm = mch_getperm(from); |
Bram Moolenaar | cd71fa3 | 2005-03-11 22:46:48 +0000 | [diff] [blame] | 6645 | #ifdef HAVE_ACL |
| 6646 | /* For systems that support ACL: get the ACL from the original file. */ |
| 6647 | acl = mch_get_acl(from); |
| 6648 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6649 | fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0); |
| 6650 | if (fd_in == -1) |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 6651 | { |
| 6652 | #ifdef HAVE_ACL |
| 6653 | mch_free_acl(acl); |
| 6654 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6655 | return -1; |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 6656 | } |
Bram Moolenaar | 9be038d | 2005-03-08 22:34:32 +0000 | [diff] [blame] | 6657 | |
| 6658 | /* Create the new file with same permissions as the original. */ |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 6659 | fd_out = mch_open((char *)to, |
| 6660 | O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6661 | if (fd_out == -1) |
| 6662 | { |
| 6663 | close(fd_in); |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 6664 | #ifdef HAVE_ACL |
| 6665 | mch_free_acl(acl); |
| 6666 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6667 | return -1; |
| 6668 | } |
| 6669 | |
| 6670 | buffer = (char *)alloc(BUFSIZE); |
| 6671 | if (buffer == NULL) |
| 6672 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6673 | close(fd_out); |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 6674 | close(fd_in); |
| 6675 | #ifdef HAVE_ACL |
| 6676 | mch_free_acl(acl); |
| 6677 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6678 | return -1; |
| 6679 | } |
| 6680 | |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 6681 | while ((n = read_eintr(fd_in, buffer, BUFSIZE)) > 0) |
| 6682 | if (write_eintr(fd_out, buffer, n) != n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6683 | { |
| 6684 | errmsg = _("E208: Error writing to \"%s\""); |
| 6685 | break; |
| 6686 | } |
| 6687 | |
| 6688 | vim_free(buffer); |
| 6689 | close(fd_in); |
| 6690 | if (close(fd_out) < 0) |
| 6691 | errmsg = _("E209: Error closing \"%s\""); |
| 6692 | if (n < 0) |
| 6693 | { |
| 6694 | errmsg = _("E210: Error reading \"%s\""); |
| 6695 | to = from; |
| 6696 | } |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 6697 | #ifndef UNIX /* for Unix mch_open() already set the permission */ |
Bram Moolenaar | 9be038d | 2005-03-08 22:34:32 +0000 | [diff] [blame] | 6698 | mch_setperm(to, perm); |
Bram Moolenaar | c6039d8 | 2005-12-02 00:44:04 +0000 | [diff] [blame] | 6699 | #endif |
Bram Moolenaar | cd71fa3 | 2005-03-11 22:46:48 +0000 | [diff] [blame] | 6700 | #ifdef HAVE_ACL |
| 6701 | mch_set_acl(to, acl); |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 6702 | mch_free_acl(acl); |
Bram Moolenaar | cd71fa3 | 2005-03-11 22:46:48 +0000 | [diff] [blame] | 6703 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6704 | if (errmsg != NULL) |
| 6705 | { |
| 6706 | EMSG2(errmsg, to); |
| 6707 | return -1; |
| 6708 | } |
| 6709 | mch_remove(from); |
| 6710 | return 0; |
| 6711 | } |
| 6712 | |
| 6713 | static int already_warned = FALSE; |
| 6714 | |
| 6715 | /* |
| 6716 | * Check if any not hidden buffer has been changed. |
| 6717 | * Postpone the check if there are characters in the stuff buffer, a global |
| 6718 | * command is being executed, a mapping is being executed or an autocommand is |
| 6719 | * busy. |
| 6720 | * Returns TRUE if some message was written (screen should be redrawn and |
| 6721 | * cursor positioned). |
| 6722 | */ |
| 6723 | int |
| 6724 | check_timestamps(focus) |
| 6725 | int focus; /* called for GUI focus event */ |
| 6726 | { |
| 6727 | buf_T *buf; |
| 6728 | int didit = 0; |
| 6729 | int n; |
| 6730 | |
| 6731 | /* Don't check timestamps while system() or another low-level function may |
| 6732 | * cause us to lose and gain focus. */ |
| 6733 | if (no_check_timestamps > 0) |
| 6734 | return FALSE; |
| 6735 | |
| 6736 | /* Avoid doing a check twice. The OK/Reload dialog can cause a focus |
| 6737 | * event and we would keep on checking if the file is steadily growing. |
| 6738 | * Do check again after typing something. */ |
| 6739 | if (focus && did_check_timestamps) |
| 6740 | { |
| 6741 | need_check_timestamps = TRUE; |
| 6742 | return FALSE; |
| 6743 | } |
| 6744 | |
| 6745 | if (!stuff_empty() || global_busy || !typebuf_typed() |
| 6746 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 6747 | || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6748 | #endif |
| 6749 | ) |
| 6750 | need_check_timestamps = TRUE; /* check later */ |
| 6751 | else |
| 6752 | { |
| 6753 | ++no_wait_return; |
| 6754 | did_check_timestamps = TRUE; |
| 6755 | already_warned = FALSE; |
| 6756 | for (buf = firstbuf; buf != NULL; ) |
| 6757 | { |
| 6758 | /* Only check buffers in a window. */ |
| 6759 | if (buf->b_nwindows > 0) |
| 6760 | { |
| 6761 | n = buf_check_timestamp(buf, focus); |
| 6762 | if (didit < n) |
| 6763 | didit = n; |
| 6764 | if (n > 0 && !buf_valid(buf)) |
| 6765 | { |
| 6766 | /* Autocommands have removed the buffer, start at the |
| 6767 | * first one again. */ |
| 6768 | buf = firstbuf; |
| 6769 | continue; |
| 6770 | } |
| 6771 | } |
| 6772 | buf = buf->b_next; |
| 6773 | } |
| 6774 | --no_wait_return; |
| 6775 | need_check_timestamps = FALSE; |
| 6776 | if (need_wait_return && didit == 2) |
| 6777 | { |
| 6778 | /* make sure msg isn't overwritten */ |
| 6779 | msg_puts((char_u *)"\n"); |
| 6780 | out_flush(); |
| 6781 | } |
| 6782 | } |
| 6783 | return didit; |
| 6784 | } |
| 6785 | |
| 6786 | /* |
| 6787 | * Move all the lines from buffer "frombuf" to buffer "tobuf". |
| 6788 | * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not |
| 6789 | * empty. |
| 6790 | */ |
| 6791 | static int |
| 6792 | move_lines(frombuf, tobuf) |
| 6793 | buf_T *frombuf; |
| 6794 | buf_T *tobuf; |
| 6795 | { |
| 6796 | buf_T *tbuf = curbuf; |
| 6797 | int retval = OK; |
| 6798 | linenr_T lnum; |
| 6799 | char_u *p; |
| 6800 | |
| 6801 | /* Copy the lines in "frombuf" to "tobuf". */ |
| 6802 | curbuf = tobuf; |
| 6803 | for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum) |
| 6804 | { |
| 6805 | p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE)); |
| 6806 | if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL) |
| 6807 | { |
| 6808 | vim_free(p); |
| 6809 | retval = FAIL; |
| 6810 | break; |
| 6811 | } |
| 6812 | vim_free(p); |
| 6813 | } |
| 6814 | |
| 6815 | /* Delete all the lines in "frombuf". */ |
| 6816 | if (retval != FAIL) |
| 6817 | { |
| 6818 | curbuf = frombuf; |
Bram Moolenaar | 9460b9d | 2007-01-09 14:37:01 +0000 | [diff] [blame] | 6819 | for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum) |
| 6820 | if (ml_delete(lnum, FALSE) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6821 | { |
| 6822 | /* Oops! We could try putting back the saved lines, but that |
| 6823 | * might fail again... */ |
| 6824 | retval = FAIL; |
| 6825 | break; |
| 6826 | } |
| 6827 | } |
| 6828 | |
| 6829 | curbuf = tbuf; |
| 6830 | return retval; |
| 6831 | } |
| 6832 | |
| 6833 | /* |
| 6834 | * Check if buffer "buf" has been changed. |
| 6835 | * Also check if the file for a new buffer unexpectedly appeared. |
| 6836 | * return 1 if a changed buffer was found. |
| 6837 | * return 2 if a message has been displayed. |
| 6838 | * return 0 otherwise. |
| 6839 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6840 | int |
| 6841 | buf_check_timestamp(buf, focus) |
| 6842 | buf_T *buf; |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 6843 | int focus UNUSED; /* called for GUI focus event */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6844 | { |
| 6845 | struct stat st; |
| 6846 | int stat_res; |
| 6847 | int retval = 0; |
| 6848 | char_u *path; |
| 6849 | char_u *tbuf; |
| 6850 | char *mesg = NULL; |
Bram Moolenaar | 44ecf65 | 2005-03-07 23:09:59 +0000 | [diff] [blame] | 6851 | char *mesg2 = ""; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6852 | int helpmesg = FALSE; |
| 6853 | int reload = FALSE; |
| 6854 | #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
| 6855 | int can_reload = FALSE; |
| 6856 | #endif |
Bram Moolenaar | 914703b | 2010-05-31 21:59:46 +0200 | [diff] [blame] | 6857 | off_t orig_size = buf->b_orig_size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6858 | int orig_mode = buf->b_orig_mode; |
| 6859 | #ifdef FEAT_GUI |
| 6860 | int save_mouse_correct = need_mouse_correct; |
| 6861 | #endif |
| 6862 | #ifdef FEAT_AUTOCMD |
| 6863 | static int busy = FALSE; |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6864 | int n; |
| 6865 | char_u *s; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6866 | #endif |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6867 | char *reason; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6868 | |
| 6869 | /* If there is no file name, the buffer is not loaded, 'buftype' is |
| 6870 | * set, we are in the middle of a save or being called recursively: ignore |
| 6871 | * this buffer. */ |
| 6872 | if (buf->b_ffname == NULL |
| 6873 | || buf->b_ml.ml_mfp == NULL |
| 6874 | #if defined(FEAT_QUICKFIX) |
| 6875 | || *buf->b_p_bt != NUL |
| 6876 | #endif |
| 6877 | || buf->b_saving |
| 6878 | #ifdef FEAT_AUTOCMD |
| 6879 | || busy |
| 6880 | #endif |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 6881 | #ifdef FEAT_NETBEANS_INTG |
| 6882 | || isNetbeansBuffer(buf) |
| 6883 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6884 | ) |
| 6885 | return 0; |
| 6886 | |
| 6887 | if ( !(buf->b_flags & BF_NOTEDITED) |
| 6888 | && buf->b_mtime != 0 |
| 6889 | && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0 |
| 6890 | || time_differs((long)st.st_mtime, buf->b_mtime) |
| 6891 | #ifdef HAVE_ST_MODE |
| 6892 | || (int)st.st_mode != buf->b_orig_mode |
| 6893 | #else |
| 6894 | || mch_getperm(buf->b_ffname) != buf->b_orig_mode |
| 6895 | #endif |
| 6896 | )) |
| 6897 | { |
| 6898 | retval = 1; |
| 6899 | |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6900 | /* set b_mtime to stop further warnings (e.g., when executing |
| 6901 | * FileChangedShell autocmd) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6902 | if (stat_res < 0) |
| 6903 | { |
| 6904 | buf->b_mtime = 0; |
| 6905 | buf->b_orig_size = 0; |
| 6906 | buf->b_orig_mode = 0; |
| 6907 | } |
| 6908 | else |
| 6909 | buf_store_time(buf, &st, buf->b_ffname); |
| 6910 | |
| 6911 | /* Don't do anything for a directory. Might contain the file |
| 6912 | * explorer. */ |
| 6913 | if (mch_isdir(buf->b_fname)) |
| 6914 | ; |
| 6915 | |
| 6916 | /* |
| 6917 | * If 'autoread' is set, the buffer has no changes and the file still |
| 6918 | * exists, reload the buffer. Use the buffer-local option value if it |
| 6919 | * was set, the global option value otherwise. |
| 6920 | */ |
| 6921 | else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar) |
| 6922 | && !bufIsChanged(buf) && stat_res >= 0) |
| 6923 | reload = TRUE; |
| 6924 | else |
| 6925 | { |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6926 | if (stat_res < 0) |
| 6927 | reason = "deleted"; |
| 6928 | else if (bufIsChanged(buf)) |
| 6929 | reason = "conflict"; |
| 6930 | else if (orig_size != buf->b_orig_size || buf_contents_changed(buf)) |
| 6931 | reason = "changed"; |
| 6932 | else if (orig_mode != buf->b_orig_mode) |
| 6933 | reason = "mode"; |
| 6934 | else |
| 6935 | reason = "time"; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6936 | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6937 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6938 | /* |
| 6939 | * Only give the warning if there are no FileChangedShell |
| 6940 | * autocommands. |
| 6941 | * Avoid being called recursively by setting "busy". |
| 6942 | */ |
| 6943 | busy = TRUE; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 6944 | # ifdef FEAT_EVAL |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6945 | set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1); |
| 6946 | set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 6947 | # endif |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 6948 | ++allbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6949 | n = apply_autocmds(EVENT_FILECHANGEDSHELL, |
| 6950 | buf->b_fname, buf->b_fname, FALSE, buf); |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 6951 | --allbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6952 | busy = FALSE; |
| 6953 | if (n) |
| 6954 | { |
| 6955 | if (!buf_valid(buf)) |
| 6956 | EMSG(_("E246: FileChangedShell autocommand deleted buffer")); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 6957 | # ifdef FEAT_EVAL |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6958 | s = get_vim_var_str(VV_FCS_CHOICE); |
| 6959 | if (STRCMP(s, "reload") == 0 && *reason != 'd') |
| 6960 | reload = TRUE; |
| 6961 | else if (STRCMP(s, "ask") == 0) |
| 6962 | n = FALSE; |
| 6963 | else |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 6964 | # endif |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6965 | return 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6966 | } |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6967 | if (!n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6968 | #endif |
| 6969 | { |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6970 | if (*reason == 'd') |
| 6971 | mesg = _("E211: File \"%s\" no longer available"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6972 | else |
| 6973 | { |
| 6974 | helpmesg = TRUE; |
| 6975 | #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
| 6976 | can_reload = TRUE; |
| 6977 | #endif |
| 6978 | /* |
| 6979 | * Check if the file contents really changed to avoid |
| 6980 | * giving a warning when only the timestamp was set (e.g., |
| 6981 | * checked out of CVS). Always warn when the buffer was |
| 6982 | * changed. |
| 6983 | */ |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6984 | if (reason[2] == 'n') |
| 6985 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6986 | 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] | 6987 | mesg2 = _("See \":help W12\" for more info."); |
| 6988 | } |
| 6989 | else if (reason[1] == 'h') |
| 6990 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6991 | mesg = _("W11: Warning: File \"%s\" has changed since editing started"); |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6992 | mesg2 = _("See \":help W11\" for more info."); |
| 6993 | } |
| 6994 | else if (*reason == 'm') |
| 6995 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6996 | mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started"); |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6997 | mesg2 = _("See \":help W16\" for more info."); |
| 6998 | } |
Bram Moolenaar | 85388b5 | 2009-06-24 09:58:32 +0000 | [diff] [blame] | 6999 | else |
| 7000 | /* Only timestamp changed, store it to avoid a warning |
| 7001 | * in check_mtime() later. */ |
| 7002 | buf->b_mtime_read = buf->b_mtime; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7003 | } |
| 7004 | } |
| 7005 | } |
| 7006 | |
| 7007 | } |
| 7008 | else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W) |
| 7009 | && vim_fexists(buf->b_ffname)) |
| 7010 | { |
| 7011 | retval = 1; |
| 7012 | mesg = _("W13: Warning: File \"%s\" has been created after editing started"); |
| 7013 | buf->b_flags |= BF_NEW_W; |
| 7014 | #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
| 7015 | can_reload = TRUE; |
| 7016 | #endif |
| 7017 | } |
| 7018 | |
| 7019 | if (mesg != NULL) |
| 7020 | { |
| 7021 | path = home_replace_save(buf, buf->b_fname); |
| 7022 | if (path != NULL) |
| 7023 | { |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 7024 | if (!helpmesg) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7025 | mesg2 = ""; |
| 7026 | tbuf = alloc((unsigned)(STRLEN(path) + STRLEN(mesg) |
| 7027 | + STRLEN(mesg2) + 2)); |
| 7028 | sprintf((char *)tbuf, mesg, path); |
Bram Moolenaar | 496c526 | 2009-03-18 14:42:00 +0000 | [diff] [blame] | 7029 | #ifdef FEAT_EVAL |
| 7030 | /* Set warningmsg here, before the unimportant and output-specific |
| 7031 | * mesg2 has been appended. */ |
| 7032 | set_vim_var_string(VV_WARNINGMSG, tbuf, -1); |
| 7033 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7034 | #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
| 7035 | if (can_reload) |
| 7036 | { |
| 7037 | if (*mesg2 != NUL) |
| 7038 | { |
| 7039 | STRCAT(tbuf, "\n"); |
| 7040 | STRCAT(tbuf, mesg2); |
| 7041 | } |
| 7042 | if (do_dialog(VIM_WARNING, (char_u *)_("Warning"), tbuf, |
Bram Moolenaar | d2c340a | 2011-01-17 20:08:11 +0100 | [diff] [blame] | 7043 | (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7044 | reload = TRUE; |
| 7045 | } |
| 7046 | else |
| 7047 | #endif |
| 7048 | if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned) |
| 7049 | { |
| 7050 | if (*mesg2 != NUL) |
| 7051 | { |
| 7052 | STRCAT(tbuf, "; "); |
| 7053 | STRCAT(tbuf, mesg2); |
| 7054 | } |
| 7055 | EMSG(tbuf); |
| 7056 | retval = 2; |
| 7057 | } |
| 7058 | else |
| 7059 | { |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 7060 | # ifdef FEAT_AUTOCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7061 | if (!autocmd_busy) |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 7062 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7063 | { |
| 7064 | msg_start(); |
| 7065 | msg_puts_attr(tbuf, hl_attr(HLF_E) + MSG_HIST); |
| 7066 | if (*mesg2 != NUL) |
| 7067 | msg_puts_attr((char_u *)mesg2, |
| 7068 | hl_attr(HLF_W) + MSG_HIST); |
| 7069 | msg_clr_eos(); |
| 7070 | (void)msg_end(); |
| 7071 | if (emsg_silent == 0) |
| 7072 | { |
| 7073 | out_flush(); |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 7074 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7075 | if (!focus) |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 7076 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7077 | /* give the user some time to think about it */ |
| 7078 | ui_delay(1000L, TRUE); |
| 7079 | |
| 7080 | /* don't redraw and erase the message */ |
| 7081 | redraw_cmdline = FALSE; |
| 7082 | } |
| 7083 | } |
| 7084 | already_warned = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7085 | } |
| 7086 | |
| 7087 | vim_free(path); |
| 7088 | vim_free(tbuf); |
| 7089 | } |
| 7090 | } |
| 7091 | |
| 7092 | if (reload) |
Bram Moolenaar | 465748e | 2012-08-29 18:50:54 +0200 | [diff] [blame] | 7093 | { |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7094 | /* Reload the buffer. */ |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 7095 | buf_reload(buf, orig_mode); |
Bram Moolenaar | 465748e | 2012-08-29 18:50:54 +0200 | [diff] [blame] | 7096 | #ifdef FEAT_PERSISTENT_UNDO |
| 7097 | if (buf->b_p_udf && buf->b_ffname != NULL) |
| 7098 | { |
| 7099 | char_u hash[UNDO_HASH_SIZE]; |
| 7100 | buf_T *save_curbuf = curbuf; |
| 7101 | |
| 7102 | /* Any existing undo file is unusable, write it now. */ |
| 7103 | curbuf = buf; |
| 7104 | u_compute_hash(hash); |
| 7105 | u_write_undo(NULL, FALSE, buf, hash); |
| 7106 | curbuf = save_curbuf; |
| 7107 | } |
| 7108 | #endif |
| 7109 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7110 | |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 7111 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 7112 | /* Trigger FileChangedShell when the file was changed in any way. */ |
| 7113 | if (buf_valid(buf) && retval != 0) |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 7114 | (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, |
| 7115 | buf->b_fname, buf->b_fname, FALSE, buf); |
| 7116 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7117 | #ifdef FEAT_GUI |
| 7118 | /* restore this in case an autocommand has set it; it would break |
| 7119 | * 'mousefocus' */ |
| 7120 | need_mouse_correct = save_mouse_correct; |
| 7121 | #endif |
| 7122 | |
| 7123 | return retval; |
| 7124 | } |
| 7125 | |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7126 | /* |
| 7127 | * Reload a buffer that is already loaded. |
| 7128 | * Used when the file was changed outside of Vim. |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 7129 | * "orig_mode" is buf->b_orig_mode before the need for reloading was detected. |
| 7130 | * buf->b_orig_mode may have been reset already. |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7131 | */ |
| 7132 | void |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 7133 | buf_reload(buf, orig_mode) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7134 | buf_T *buf; |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 7135 | int orig_mode; |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7136 | { |
| 7137 | exarg_T ea; |
| 7138 | pos_T old_cursor; |
| 7139 | linenr_T old_topline; |
| 7140 | int old_ro = buf->b_p_ro; |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7141 | buf_T *savebuf; |
| 7142 | int saved = OK; |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7143 | aco_save_T aco; |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 7144 | int flags = READ_NEW; |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7145 | |
| 7146 | /* set curwin/curbuf for "buf" and save some things */ |
| 7147 | aucmd_prepbuf(&aco, buf); |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7148 | |
| 7149 | /* We only want to read the text from the file, not reset the syntax |
| 7150 | * highlighting, clear marks, diff status, etc. Force the fileformat |
| 7151 | * and encoding to be the same. */ |
| 7152 | if (prep_exarg(&ea, buf) == OK) |
| 7153 | { |
| 7154 | old_cursor = curwin->w_cursor; |
| 7155 | old_topline = curwin->w_topline; |
| 7156 | |
Bram Moolenaar | f9bb734 | 2010-08-04 14:29:54 +0200 | [diff] [blame] | 7157 | if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur) |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 7158 | { |
| 7159 | /* Save all the text, so that the reload can be undone. |
| 7160 | * Sync first so that this is a separate undo-able action. */ |
| 7161 | u_sync(FALSE); |
| 7162 | saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE); |
| 7163 | flags |= READ_KEEP_UNDO; |
| 7164 | } |
| 7165 | |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7166 | /* |
| 7167 | * To behave like when a new file is edited (matters for |
| 7168 | * BufReadPost autocommands) we first need to delete the current |
| 7169 | * buffer contents. But if reading the file fails we should keep |
| 7170 | * the old contents. Can't use memory only, the file might be |
| 7171 | * too big. Use a hidden buffer to move the buffer contents to. |
| 7172 | */ |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 7173 | if (bufempty() || saved == FAIL) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7174 | savebuf = NULL; |
| 7175 | else |
| 7176 | { |
| 7177 | /* Allocate a buffer without putting it in the buffer list. */ |
| 7178 | savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 7179 | if (savebuf != NULL && buf == curbuf) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7180 | { |
| 7181 | /* Open the memline. */ |
| 7182 | curbuf = savebuf; |
| 7183 | curwin->w_buffer = savebuf; |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7184 | saved = ml_open(curbuf); |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7185 | curbuf = buf; |
| 7186 | curwin->w_buffer = buf; |
| 7187 | } |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 7188 | if (savebuf == NULL || saved == FAIL || buf != curbuf |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7189 | || move_lines(buf, savebuf) == FAIL) |
| 7190 | { |
| 7191 | EMSG2(_("E462: Could not prepare for reloading \"%s\""), |
| 7192 | buf->b_fname); |
| 7193 | saved = FAIL; |
| 7194 | } |
| 7195 | } |
| 7196 | |
| 7197 | if (saved == OK) |
| 7198 | { |
| 7199 | curbuf->b_flags |= BF_CHECK_RO; /* check for RO again */ |
| 7200 | #ifdef FEAT_AUTOCMD |
| 7201 | keep_filetype = TRUE; /* don't detect 'filetype' */ |
| 7202 | #endif |
| 7203 | if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, |
| 7204 | (linenr_T)0, |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 7205 | (linenr_T)MAXLNUM, &ea, flags) == FAIL) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7206 | { |
| 7207 | #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
| 7208 | if (!aborting()) |
| 7209 | #endif |
| 7210 | EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname); |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 7211 | if (savebuf != NULL && buf_valid(savebuf) && buf == curbuf) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7212 | { |
| 7213 | /* Put the text back from the save buffer. First |
| 7214 | * delete any lines that readfile() added. */ |
| 7215 | while (!bufempty()) |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 7216 | if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7217 | break; |
| 7218 | (void)move_lines(savebuf, buf); |
| 7219 | } |
| 7220 | } |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 7221 | else if (buf == curbuf) /* "buf" still valid */ |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7222 | { |
| 7223 | /* Mark the buffer as unmodified and free undo info. */ |
| 7224 | unchanged(buf, TRUE); |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 7225 | if ((flags & READ_KEEP_UNDO) == 0) |
| 7226 | { |
| 7227 | u_blockfree(buf); |
| 7228 | u_clearall(buf); |
| 7229 | } |
| 7230 | else |
Bram Moolenaar | f9bb734 | 2010-08-04 14:29:54 +0200 | [diff] [blame] | 7231 | { |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 7232 | /* Mark all undo states as changed. */ |
| 7233 | u_unchanged(curbuf); |
Bram Moolenaar | f9bb734 | 2010-08-04 14:29:54 +0200 | [diff] [blame] | 7234 | } |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7235 | } |
| 7236 | } |
| 7237 | vim_free(ea.cmd); |
| 7238 | |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 7239 | if (savebuf != NULL && buf_valid(savebuf)) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7240 | wipe_buffer(savebuf, FALSE); |
| 7241 | |
| 7242 | #ifdef FEAT_DIFF |
| 7243 | /* Invalidate diff info if necessary. */ |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 7244 | diff_invalidate(curbuf); |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7245 | #endif |
| 7246 | |
| 7247 | /* Restore the topline and cursor position and check it (lines may |
| 7248 | * have been removed). */ |
| 7249 | if (old_topline > curbuf->b_ml.ml_line_count) |
| 7250 | curwin->w_topline = curbuf->b_ml.ml_line_count; |
| 7251 | else |
| 7252 | curwin->w_topline = old_topline; |
| 7253 | curwin->w_cursor = old_cursor; |
| 7254 | check_cursor(); |
| 7255 | update_topline(); |
| 7256 | #ifdef FEAT_AUTOCMD |
| 7257 | keep_filetype = FALSE; |
| 7258 | #endif |
| 7259 | #ifdef FEAT_FOLDING |
| 7260 | { |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 7261 | win_T *wp; |
| 7262 | tabpage_T *tp; |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7263 | |
| 7264 | /* Update folds unless they are defined manually. */ |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 7265 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7266 | if (wp->w_buffer == curwin->w_buffer |
| 7267 | && !foldmethodIsManual(wp)) |
| 7268 | foldUpdateAll(wp); |
| 7269 | } |
| 7270 | #endif |
| 7271 | /* If the mode didn't change and 'readonly' was set, keep the old |
| 7272 | * value; the user probably used the ":view" command. But don't |
| 7273 | * reset it, might have had a read error. */ |
| 7274 | if (orig_mode == curbuf->b_orig_mode) |
| 7275 | curbuf->b_p_ro |= old_ro; |
Bram Moolenaar | 52f85b7 | 2013-01-30 14:13:56 +0100 | [diff] [blame] | 7276 | |
| 7277 | /* Modelines must override settings done by autocommands. */ |
| 7278 | do_modelines(0); |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7279 | } |
| 7280 | |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7281 | /* restore curwin/curbuf and a few other things */ |
| 7282 | aucmd_restbuf(&aco); |
| 7283 | /* Careful: autocommands may have made "buf" invalid! */ |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7284 | } |
| 7285 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7286 | void |
| 7287 | buf_store_time(buf, st, fname) |
| 7288 | buf_T *buf; |
| 7289 | struct stat *st; |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 7290 | char_u *fname UNUSED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7291 | { |
| 7292 | buf->b_mtime = (long)st->st_mtime; |
Bram Moolenaar | 914703b | 2010-05-31 21:59:46 +0200 | [diff] [blame] | 7293 | buf->b_orig_size = st->st_size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7294 | #ifdef HAVE_ST_MODE |
| 7295 | buf->b_orig_mode = (int)st->st_mode; |
| 7296 | #else |
| 7297 | buf->b_orig_mode = mch_getperm(fname); |
| 7298 | #endif |
| 7299 | } |
| 7300 | |
| 7301 | /* |
| 7302 | * Adjust the line with missing eol, used for the next write. |
| 7303 | * Used for do_filter(), when the input lines for the filter are deleted. |
| 7304 | */ |
| 7305 | void |
| 7306 | write_lnum_adjust(offset) |
| 7307 | linenr_T offset; |
| 7308 | { |
Bram Moolenaar | cab35ad | 2011-02-15 17:39:22 +0100 | [diff] [blame] | 7309 | if (curbuf->b_no_eol_lnum != 0) /* only if there is a missing eol */ |
| 7310 | curbuf->b_no_eol_lnum += offset; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7311 | } |
| 7312 | |
| 7313 | #if defined(TEMPDIRNAMES) || defined(PROTO) |
| 7314 | static long temp_count = 0; /* Temp filename counter. */ |
| 7315 | |
| 7316 | /* |
| 7317 | * Delete the temp directory and all files it contains. |
| 7318 | */ |
| 7319 | void |
| 7320 | vim_deltempdir() |
| 7321 | { |
| 7322 | char_u **files; |
| 7323 | int file_count; |
| 7324 | int i; |
| 7325 | |
| 7326 | if (vim_tempdir != NULL) |
| 7327 | { |
| 7328 | sprintf((char *)NameBuff, "%s*", vim_tempdir); |
| 7329 | if (gen_expand_wildcards(1, &NameBuff, &file_count, &files, |
| 7330 | EW_DIR|EW_FILE|EW_SILENT) == OK) |
| 7331 | { |
| 7332 | for (i = 0; i < file_count; ++i) |
| 7333 | mch_remove(files[i]); |
| 7334 | FreeWild(file_count, files); |
| 7335 | } |
| 7336 | gettail(NameBuff)[-1] = NUL; |
| 7337 | (void)mch_rmdir(NameBuff); |
| 7338 | |
| 7339 | vim_free(vim_tempdir); |
| 7340 | vim_tempdir = NULL; |
| 7341 | } |
| 7342 | } |
| 7343 | #endif |
| 7344 | |
Bram Moolenaar | 4592dee | 2009-11-18 19:11:58 +0000 | [diff] [blame] | 7345 | #ifdef TEMPDIRNAMES |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7346 | /* |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7347 | * Directory "tempdir" was created. Expand this name to a full path and put |
| 7348 | * it in "vim_tempdir". This avoids that using ":cd" would confuse us. |
| 7349 | * "tempdir" must be no longer than MAXPATHL. |
| 7350 | */ |
| 7351 | static void |
| 7352 | vim_settempdir(tempdir) |
| 7353 | char_u *tempdir; |
| 7354 | { |
| 7355 | char_u *buf; |
| 7356 | |
| 7357 | buf = alloc((unsigned)MAXPATHL + 2); |
| 7358 | if (buf != NULL) |
| 7359 | { |
| 7360 | if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL) |
| 7361 | STRCPY(buf, tempdir); |
| 7362 | # ifdef __EMX__ |
| 7363 | if (vim_strchr(buf, '/') != NULL) |
| 7364 | STRCAT(buf, "/"); |
| 7365 | else |
| 7366 | # endif |
| 7367 | add_pathsep(buf); |
| 7368 | vim_tempdir = vim_strsave(buf); |
| 7369 | vim_free(buf); |
| 7370 | } |
| 7371 | } |
Bram Moolenaar | 4592dee | 2009-11-18 19:11:58 +0000 | [diff] [blame] | 7372 | #endif |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7373 | |
| 7374 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7375 | * vim_tempname(): Return a unique name that can be used for a temp file. |
| 7376 | * |
| 7377 | * The temp file is NOT created. |
| 7378 | * |
| 7379 | * The returned pointer is to allocated memory. |
| 7380 | * The returned pointer is NULL if no valid name was found. |
| 7381 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7382 | char_u * |
| 7383 | vim_tempname(extra_char) |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 7384 | int extra_char UNUSED; /* char to use in the name instead of '?' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7385 | { |
| 7386 | #ifdef USE_TMPNAM |
| 7387 | char_u itmp[L_tmpnam]; /* use tmpnam() */ |
| 7388 | #else |
| 7389 | char_u itmp[TEMPNAMELEN]; |
| 7390 | #endif |
| 7391 | |
| 7392 | #ifdef TEMPDIRNAMES |
| 7393 | static char *(tempdirs[]) = {TEMPDIRNAMES}; |
| 7394 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7395 | # ifndef EEXIST |
| 7396 | struct stat st; |
| 7397 | # endif |
| 7398 | |
| 7399 | /* |
| 7400 | * This will create a directory for private use by this instance of Vim. |
| 7401 | * This is done once, and the same directory is used for all temp files. |
| 7402 | * This method avoids security problems because of symlink attacks et al. |
| 7403 | * It's also a bit faster, because we only need to check for an existing |
| 7404 | * file when creating the directory and not for each temp file. |
| 7405 | */ |
| 7406 | if (vim_tempdir == NULL) |
| 7407 | { |
| 7408 | /* |
| 7409 | * Try the entries in TEMPDIRNAMES to create the temp directory. |
| 7410 | */ |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 7411 | for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7412 | { |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7413 | # ifndef HAVE_MKDTEMP |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 7414 | size_t itmplen; |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7415 | long nr; |
| 7416 | long off; |
| 7417 | # endif |
| 7418 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7419 | /* expand $TMP, leave room for "/v1100000/999999999" */ |
| 7420 | expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20); |
| 7421 | if (mch_isdir(itmp)) /* directory exists */ |
| 7422 | { |
| 7423 | # ifdef __EMX__ |
| 7424 | /* If $TMP contains a forward slash (perhaps using bash or |
| 7425 | * tcsh), don't add a backslash, use a forward slash! |
| 7426 | * Adding 2 backslashes didn't work. */ |
| 7427 | if (vim_strchr(itmp, '/') != NULL) |
| 7428 | STRCAT(itmp, "/"); |
| 7429 | else |
| 7430 | # endif |
| 7431 | add_pathsep(itmp); |
| 7432 | |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7433 | # ifdef HAVE_MKDTEMP |
| 7434 | /* Leave room for filename */ |
| 7435 | STRCAT(itmp, "vXXXXXX"); |
| 7436 | if (mkdtemp((char *)itmp) != NULL) |
| 7437 | vim_settempdir(itmp); |
| 7438 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7439 | /* Get an arbitrary number of up to 6 digits. When it's |
| 7440 | * unlikely that it already exists it will be faster, |
| 7441 | * otherwise it doesn't matter. The use of mkdir() avoids any |
| 7442 | * security problems because of the predictable number. */ |
| 7443 | nr = (mch_get_pid() + (long)time(NULL)) % 1000000L; |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 7444 | itmplen = STRLEN(itmp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7445 | |
| 7446 | /* Try up to 10000 different values until we find a name that |
| 7447 | * doesn't exist. */ |
| 7448 | for (off = 0; off < 10000L; ++off) |
| 7449 | { |
| 7450 | int r; |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7451 | # if defined(UNIX) || defined(VMS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7452 | mode_t umask_save; |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7453 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7454 | |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7455 | sprintf((char *)itmp + itmplen, "v%ld", nr + off); |
| 7456 | # ifndef EEXIST |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7457 | /* If mkdir() does not set errno to EEXIST, check for |
| 7458 | * existing file here. There is a race condition then, |
| 7459 | * although it's fail-safe. */ |
| 7460 | if (mch_stat((char *)itmp, &st) >= 0) |
| 7461 | continue; |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7462 | # endif |
| 7463 | # if defined(UNIX) || defined(VMS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7464 | /* Make sure the umask doesn't remove the executable bit. |
| 7465 | * "repl" has been reported to use "177". */ |
| 7466 | umask_save = umask(077); |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7467 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7468 | r = vim_mkdir(itmp, 0700); |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7469 | # if defined(UNIX) || defined(VMS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7470 | (void)umask(umask_save); |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7471 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7472 | if (r == 0) |
| 7473 | { |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7474 | vim_settempdir(itmp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7475 | break; |
| 7476 | } |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7477 | # ifdef EEXIST |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7478 | /* If the mkdir() didn't fail because the file/dir exists, |
| 7479 | * we probably can't create any dir here, try another |
| 7480 | * place. */ |
| 7481 | if (errno != EEXIST) |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7482 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7483 | break; |
| 7484 | } |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7485 | # endif /* HAVE_MKDTEMP */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7486 | if (vim_tempdir != NULL) |
| 7487 | break; |
| 7488 | } |
| 7489 | } |
| 7490 | } |
| 7491 | |
| 7492 | if (vim_tempdir != NULL) |
| 7493 | { |
| 7494 | /* There is no need to check if the file exists, because we own the |
| 7495 | * directory and nobody else creates a file in it. */ |
| 7496 | sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++); |
| 7497 | return vim_strsave(itmp); |
| 7498 | } |
| 7499 | |
| 7500 | return NULL; |
| 7501 | |
| 7502 | #else /* TEMPDIRNAMES */ |
| 7503 | |
| 7504 | # ifdef WIN3264 |
| 7505 | char szTempFile[_MAX_PATH + 1]; |
| 7506 | char buf4[4]; |
| 7507 | char_u *retval; |
| 7508 | char_u *p; |
| 7509 | |
| 7510 | STRCPY(itmp, ""); |
| 7511 | if (GetTempPath(_MAX_PATH, szTempFile) == 0) |
Bram Moolenaar | b189191 | 2011-02-09 14:47:03 +0100 | [diff] [blame] | 7512 | { |
| 7513 | szTempFile[0] = '.'; /* GetTempPath() failed, use current dir */ |
| 7514 | szTempFile[1] = NUL; |
| 7515 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7516 | strcpy(buf4, "VIM"); |
| 7517 | buf4[2] = extra_char; /* make it "VIa", "VIb", etc. */ |
| 7518 | if (GetTempFileName(szTempFile, buf4, 0, itmp) == 0) |
| 7519 | return NULL; |
| 7520 | /* GetTempFileName() will create the file, we don't want that */ |
| 7521 | (void)DeleteFile(itmp); |
| 7522 | |
| 7523 | /* Backslashes in a temp file name cause problems when filtering with |
| 7524 | * "sh". NOTE: This also checks 'shellcmdflag' to help those people who |
| 7525 | * didn't set 'shellslash'. */ |
| 7526 | retval = vim_strsave(itmp); |
| 7527 | if (*p_shcf == '-' || p_ssl) |
| 7528 | for (p = retval; *p; ++p) |
| 7529 | if (*p == '\\') |
| 7530 | *p = '/'; |
| 7531 | return retval; |
| 7532 | |
| 7533 | # else /* WIN3264 */ |
| 7534 | |
| 7535 | # ifdef USE_TMPNAM |
Bram Moolenaar | 95474ca | 2011-02-09 16:44:51 +0100 | [diff] [blame] | 7536 | char_u *p; |
| 7537 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7538 | /* tmpnam() will make its own name */ |
Bram Moolenaar | 95474ca | 2011-02-09 16:44:51 +0100 | [diff] [blame] | 7539 | p = tmpnam((char *)itmp); |
| 7540 | if (p == NULL || *p == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7541 | return NULL; |
| 7542 | # else |
| 7543 | char_u *p; |
| 7544 | |
| 7545 | # ifdef VMS_TEMPNAM |
| 7546 | /* mktemp() is not working on VMS. It seems to be |
| 7547 | * a do-nothing function. Therefore we use tempnam(). |
| 7548 | */ |
| 7549 | sprintf((char *)itmp, "VIM%c", extra_char); |
| 7550 | p = (char_u *)tempnam("tmp:", (char *)itmp); |
| 7551 | if (p != NULL) |
| 7552 | { |
| 7553 | /* VMS will use '.LOG' if we don't explicitly specify an extension, |
| 7554 | * and VIM will then be unable to find the file later */ |
| 7555 | STRCPY(itmp, p); |
| 7556 | STRCAT(itmp, ".txt"); |
| 7557 | free(p); |
| 7558 | } |
| 7559 | else |
| 7560 | return NULL; |
| 7561 | # else |
| 7562 | STRCPY(itmp, TEMPNAME); |
| 7563 | if ((p = vim_strchr(itmp, '?')) != NULL) |
| 7564 | *p = extra_char; |
| 7565 | if (mktemp((char *)itmp) == NULL) |
| 7566 | return NULL; |
| 7567 | # endif |
| 7568 | # endif |
| 7569 | |
| 7570 | return vim_strsave(itmp); |
| 7571 | # endif /* WIN3264 */ |
| 7572 | #endif /* TEMPDIRNAMES */ |
| 7573 | } |
| 7574 | |
| 7575 | #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) |
| 7576 | /* |
| 7577 | * Convert all backslashes in fname to forward slashes in-place. |
| 7578 | */ |
| 7579 | void |
| 7580 | forward_slash(fname) |
| 7581 | char_u *fname; |
| 7582 | { |
| 7583 | char_u *p; |
| 7584 | |
| 7585 | for (p = fname; *p != NUL; ++p) |
| 7586 | # ifdef FEAT_MBYTE |
| 7587 | /* The Big5 encoding can have '\' in the trail byte. */ |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7588 | if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7589 | ++p; |
| 7590 | else |
| 7591 | # endif |
| 7592 | if (*p == '\\') |
| 7593 | *p = '/'; |
| 7594 | } |
| 7595 | #endif |
| 7596 | |
| 7597 | |
| 7598 | /* |
| 7599 | * Code for automatic commands. |
| 7600 | * |
| 7601 | * Only included when "FEAT_AUTOCMD" has been defined. |
| 7602 | */ |
| 7603 | |
| 7604 | #if defined(FEAT_AUTOCMD) || defined(PROTO) |
| 7605 | |
| 7606 | /* |
| 7607 | * The autocommands are stored in a list for each event. |
| 7608 | * Autocommands for the same pattern, that are consecutive, are joined |
| 7609 | * together, to avoid having to match the pattern too often. |
| 7610 | * The result is an array of Autopat lists, which point to AutoCmd lists: |
| 7611 | * |
| 7612 | * first_autopat[0] --> Autopat.next --> Autopat.next --> NULL |
| 7613 | * Autopat.cmds Autopat.cmds |
| 7614 | * | | |
| 7615 | * V V |
| 7616 | * AutoCmd.next AutoCmd.next |
| 7617 | * | | |
| 7618 | * V V |
| 7619 | * AutoCmd.next NULL |
| 7620 | * | |
| 7621 | * V |
| 7622 | * NULL |
| 7623 | * |
| 7624 | * first_autopat[1] --> Autopat.next --> NULL |
| 7625 | * Autopat.cmds |
| 7626 | * | |
| 7627 | * V |
| 7628 | * AutoCmd.next |
| 7629 | * | |
| 7630 | * V |
| 7631 | * NULL |
| 7632 | * etc. |
| 7633 | * |
| 7634 | * The order of AutoCmds is important, this is the order in which they were |
| 7635 | * defined and will have to be executed. |
| 7636 | */ |
| 7637 | typedef struct AutoCmd |
| 7638 | { |
| 7639 | char_u *cmd; /* The command to be executed (NULL |
| 7640 | when command has been removed) */ |
| 7641 | char nested; /* If autocommands nest here */ |
| 7642 | char last; /* last command in list */ |
| 7643 | #ifdef FEAT_EVAL |
| 7644 | scid_T scriptID; /* script ID where defined */ |
| 7645 | #endif |
| 7646 | struct AutoCmd *next; /* Next AutoCmd in list */ |
| 7647 | } AutoCmd; |
| 7648 | |
| 7649 | typedef struct AutoPat |
| 7650 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7651 | char_u *pat; /* pattern as typed (NULL when pattern |
| 7652 | has been removed) */ |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 7653 | regprog_T *reg_prog; /* compiled regprog for pattern */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7654 | AutoCmd *cmds; /* list of commands to do */ |
| 7655 | struct AutoPat *next; /* next AutoPat in AutoPat list */ |
Bram Moolenaar | 6395af8 | 2013-06-12 19:52:15 +0200 | [diff] [blame] | 7656 | int group; /* group ID */ |
| 7657 | int patlen; /* strlen() of pat */ |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7658 | int buflocal_nr; /* !=0 for buffer-local AutoPat */ |
Bram Moolenaar | 6395af8 | 2013-06-12 19:52:15 +0200 | [diff] [blame] | 7659 | char allow_dirs; /* Pattern may match whole path */ |
| 7660 | char last; /* last pattern for apply_autocmds() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7661 | } AutoPat; |
| 7662 | |
| 7663 | static struct event_name |
| 7664 | { |
| 7665 | char *name; /* event name */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7666 | event_T event; /* event number */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7667 | } event_names[] = |
| 7668 | { |
| 7669 | {"BufAdd", EVENT_BUFADD}, |
| 7670 | {"BufCreate", EVENT_BUFADD}, |
| 7671 | {"BufDelete", EVENT_BUFDELETE}, |
| 7672 | {"BufEnter", EVENT_BUFENTER}, |
| 7673 | {"BufFilePost", EVENT_BUFFILEPOST}, |
| 7674 | {"BufFilePre", EVENT_BUFFILEPRE}, |
| 7675 | {"BufHidden", EVENT_BUFHIDDEN}, |
| 7676 | {"BufLeave", EVENT_BUFLEAVE}, |
| 7677 | {"BufNew", EVENT_BUFNEW}, |
| 7678 | {"BufNewFile", EVENT_BUFNEWFILE}, |
| 7679 | {"BufRead", EVENT_BUFREADPOST}, |
| 7680 | {"BufReadCmd", EVENT_BUFREADCMD}, |
| 7681 | {"BufReadPost", EVENT_BUFREADPOST}, |
| 7682 | {"BufReadPre", EVENT_BUFREADPRE}, |
| 7683 | {"BufUnload", EVENT_BUFUNLOAD}, |
| 7684 | {"BufWinEnter", EVENT_BUFWINENTER}, |
| 7685 | {"BufWinLeave", EVENT_BUFWINLEAVE}, |
| 7686 | {"BufWipeout", EVENT_BUFWIPEOUT}, |
| 7687 | {"BufWrite", EVENT_BUFWRITEPRE}, |
| 7688 | {"BufWritePost", EVENT_BUFWRITEPOST}, |
| 7689 | {"BufWritePre", EVENT_BUFWRITEPRE}, |
| 7690 | {"BufWriteCmd", EVENT_BUFWRITECMD}, |
| 7691 | {"CmdwinEnter", EVENT_CMDWINENTER}, |
| 7692 | {"CmdwinLeave", EVENT_CMDWINLEAVE}, |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7693 | {"ColorScheme", EVENT_COLORSCHEME}, |
Bram Moolenaar | cfa3cae | 2012-07-10 17:14:56 +0200 | [diff] [blame] | 7694 | {"CompleteDone", EVENT_COMPLETEDONE}, |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7695 | {"CursorHold", EVENT_CURSORHOLD}, |
| 7696 | {"CursorHoldI", EVENT_CURSORHOLDI}, |
| 7697 | {"CursorMoved", EVENT_CURSORMOVED}, |
| 7698 | {"CursorMovedI", EVENT_CURSORMOVEDI}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7699 | {"EncodingChanged", EVENT_ENCODINGCHANGED}, |
| 7700 | {"FileEncoding", EVENT_ENCODINGCHANGED}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7701 | {"FileAppendPost", EVENT_FILEAPPENDPOST}, |
| 7702 | {"FileAppendPre", EVENT_FILEAPPENDPRE}, |
| 7703 | {"FileAppendCmd", EVENT_FILEAPPENDCMD}, |
| 7704 | {"FileChangedShell",EVENT_FILECHANGEDSHELL}, |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 7705 | {"FileChangedShellPost",EVENT_FILECHANGEDSHELLPOST}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7706 | {"FileChangedRO", EVENT_FILECHANGEDRO}, |
| 7707 | {"FileReadPost", EVENT_FILEREADPOST}, |
| 7708 | {"FileReadPre", EVENT_FILEREADPRE}, |
| 7709 | {"FileReadCmd", EVENT_FILEREADCMD}, |
| 7710 | {"FileType", EVENT_FILETYPE}, |
| 7711 | {"FileWritePost", EVENT_FILEWRITEPOST}, |
| 7712 | {"FileWritePre", EVENT_FILEWRITEPRE}, |
| 7713 | {"FileWriteCmd", EVENT_FILEWRITECMD}, |
| 7714 | {"FilterReadPost", EVENT_FILTERREADPOST}, |
| 7715 | {"FilterReadPre", EVENT_FILTERREADPRE}, |
| 7716 | {"FilterWritePost", EVENT_FILTERWRITEPOST}, |
| 7717 | {"FilterWritePre", EVENT_FILTERWRITEPRE}, |
| 7718 | {"FocusGained", EVENT_FOCUSGAINED}, |
| 7719 | {"FocusLost", EVENT_FOCUSLOST}, |
| 7720 | {"FuncUndefined", EVENT_FUNCUNDEFINED}, |
| 7721 | {"GUIEnter", EVENT_GUIENTER}, |
Bram Moolenaar | 265e507 | 2006-08-29 16:13:22 +0000 | [diff] [blame] | 7722 | {"GUIFailed", EVENT_GUIFAILED}, |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 7723 | {"InsertChange", EVENT_INSERTCHANGE}, |
| 7724 | {"InsertEnter", EVENT_INSERTENTER}, |
| 7725 | {"InsertLeave", EVENT_INSERTLEAVE}, |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 7726 | {"InsertCharPre", EVENT_INSERTCHARPRE}, |
Bram Moolenaar | a3ffd9c | 2005-07-21 21:03:15 +0000 | [diff] [blame] | 7727 | {"MenuPopup", EVENT_MENUPOPUP}, |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7728 | {"QuickFixCmdPost", EVENT_QUICKFIXCMDPOST}, |
| 7729 | {"QuickFixCmdPre", EVENT_QUICKFIXCMDPRE}, |
Bram Moolenaar | 3b53dfb | 2012-06-06 18:03:07 +0200 | [diff] [blame] | 7730 | {"QuitPre", EVENT_QUITPRE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7731 | {"RemoteReply", EVENT_REMOTEREPLY}, |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 7732 | {"SessionLoadPost", EVENT_SESSIONLOADPOST}, |
Bram Moolenaar | 5c4bab0 | 2006-03-10 21:37:46 +0000 | [diff] [blame] | 7733 | {"ShellCmdPost", EVENT_SHELLCMDPOST}, |
| 7734 | {"ShellFilterPost", EVENT_SHELLFILTERPOST}, |
Bram Moolenaar | a203182 | 2006-03-07 22:29:51 +0000 | [diff] [blame] | 7735 | {"SourcePre", EVENT_SOURCEPRE}, |
Bram Moolenaar | 8dd1aa5 | 2007-01-16 20:33:19 +0000 | [diff] [blame] | 7736 | {"SourceCmd", EVENT_SOURCECMD}, |
Bram Moolenaar | b8a7b56 | 2006-02-01 21:47:16 +0000 | [diff] [blame] | 7737 | {"SpellFileMissing",EVENT_SPELLFILEMISSING}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7738 | {"StdinReadPost", EVENT_STDINREADPOST}, |
| 7739 | {"StdinReadPre", EVENT_STDINREADPRE}, |
Bram Moolenaar | b815dac | 2005-12-07 20:59:24 +0000 | [diff] [blame] | 7740 | {"SwapExists", EVENT_SWAPEXISTS}, |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 7741 | {"Syntax", EVENT_SYNTAX}, |
Bram Moolenaar | 70836c8 | 2006-02-20 21:28:49 +0000 | [diff] [blame] | 7742 | {"TabEnter", EVENT_TABENTER}, |
| 7743 | {"TabLeave", EVENT_TABLEAVE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7744 | {"TermChanged", EVENT_TERMCHANGED}, |
| 7745 | {"TermResponse", EVENT_TERMRESPONSE}, |
Bram Moolenaar | 186628f | 2013-03-19 13:33:23 +0100 | [diff] [blame] | 7746 | {"TextChanged", EVENT_TEXTCHANGED}, |
| 7747 | {"TextChangedI", EVENT_TEXTCHANGEDI}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7748 | {"User", EVENT_USER}, |
| 7749 | {"VimEnter", EVENT_VIMENTER}, |
| 7750 | {"VimLeave", EVENT_VIMLEAVE}, |
| 7751 | {"VimLeavePre", EVENT_VIMLEAVEPRE}, |
| 7752 | {"WinEnter", EVENT_WINENTER}, |
| 7753 | {"WinLeave", EVENT_WINLEAVE}, |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 7754 | {"VimResized", EVENT_VIMRESIZED}, |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7755 | {NULL, (event_T)0} |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7756 | }; |
| 7757 | |
| 7758 | static AutoPat *first_autopat[NUM_EVENTS] = |
| 7759 | { |
| 7760 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 7761 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 7762 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 7763 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 7764 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 7765 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7766 | }; |
| 7767 | |
| 7768 | /* |
| 7769 | * struct used to keep status while executing autocommands for an event. |
| 7770 | */ |
| 7771 | typedef struct AutoPatCmd |
| 7772 | { |
| 7773 | AutoPat *curpat; /* next AutoPat to examine */ |
| 7774 | AutoCmd *nextcmd; /* next AutoCmd to execute */ |
| 7775 | int group; /* group being used */ |
| 7776 | char_u *fname; /* fname to match with */ |
| 7777 | char_u *sfname; /* sfname to match with */ |
| 7778 | char_u *tail; /* tail of fname */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7779 | event_T event; /* current event */ |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7780 | int arg_bufnr; /* initially equal to <abuf>, set to zero when |
| 7781 | buf is deleted */ |
| 7782 | struct AutoPatCmd *next; /* chain of active apc-s for auto-invalidation*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7783 | } AutoPatCmd; |
| 7784 | |
Bram Moolenaar | d6f676d | 2005-06-01 21:51:55 +0000 | [diff] [blame] | 7785 | static AutoPatCmd *active_apc_list = NULL; /* stack of active autocommands */ |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7786 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7787 | /* |
| 7788 | * augroups stores a list of autocmd group names. |
| 7789 | */ |
Bram Moolenaar | d6f676d | 2005-06-01 21:51:55 +0000 | [diff] [blame] | 7790 | static garray_T augroups = {0, 0, sizeof(char_u *), 10, NULL}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7791 | #define AUGROUP_NAME(i) (((char_u **)augroups.ga_data)[i]) |
| 7792 | |
| 7793 | /* |
| 7794 | * The ID of the current group. Group 0 is the default one. |
| 7795 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7796 | static int current_augroup = AUGROUP_DEFAULT; |
| 7797 | |
| 7798 | static int au_need_clean = FALSE; /* need to delete marked patterns */ |
| 7799 | |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7800 | static void show_autocmd __ARGS((AutoPat *ap, event_T event)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7801 | static void au_remove_pat __ARGS((AutoPat *ap)); |
| 7802 | static void au_remove_cmds __ARGS((AutoPat *ap)); |
| 7803 | static void au_cleanup __ARGS((void)); |
| 7804 | static int au_new_group __ARGS((char_u *name)); |
| 7805 | static void au_del_group __ARGS((char_u *name)); |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7806 | static event_T event_name2nr __ARGS((char_u *start, char_u **end)); |
| 7807 | static char_u *event_nr2name __ARGS((event_T event)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7808 | static char_u *find_end_event __ARGS((char_u *arg, int have_group)); |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7809 | static int event_ignored __ARGS((event_T event)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7810 | static int au_get_grouparg __ARGS((char_u **argp)); |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7811 | static int do_autocmd_event __ARGS((event_T event, char_u *pat, int nested, char_u *cmd, int forceit, int group)); |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7812 | static int apply_autocmds_group __ARGS((event_T event, char_u *fname, char_u *fname_io, int force, int group, buf_T *buf, exarg_T *eap)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7813 | static void auto_next_pat __ARGS((AutoPatCmd *apc, int stop_at_last)); |
| 7814 | |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7815 | |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7816 | static event_T last_event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7817 | static int last_group; |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7818 | static int autocmd_blocked = 0; /* block all autocmds */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7819 | |
| 7820 | /* |
| 7821 | * Show the autocommands for one AutoPat. |
| 7822 | */ |
| 7823 | static void |
| 7824 | show_autocmd(ap, event) |
| 7825 | AutoPat *ap; |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7826 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7827 | { |
| 7828 | AutoCmd *ac; |
| 7829 | |
| 7830 | /* Check for "got_int" (here and at various places below), which is set |
| 7831 | * when "q" has been hit for the "--more--" prompt */ |
| 7832 | if (got_int) |
| 7833 | return; |
| 7834 | if (ap->pat == NULL) /* pattern has been removed */ |
| 7835 | return; |
| 7836 | |
| 7837 | msg_putchar('\n'); |
| 7838 | if (got_int) |
| 7839 | return; |
| 7840 | if (event != last_event || ap->group != last_group) |
| 7841 | { |
| 7842 | if (ap->group != AUGROUP_DEFAULT) |
| 7843 | { |
| 7844 | if (AUGROUP_NAME(ap->group) == NULL) |
| 7845 | msg_puts_attr((char_u *)_("--Deleted--"), hl_attr(HLF_E)); |
| 7846 | else |
| 7847 | msg_puts_attr(AUGROUP_NAME(ap->group), hl_attr(HLF_T)); |
| 7848 | msg_puts((char_u *)" "); |
| 7849 | } |
| 7850 | msg_puts_attr(event_nr2name(event), hl_attr(HLF_T)); |
| 7851 | last_event = event; |
| 7852 | last_group = ap->group; |
| 7853 | msg_putchar('\n'); |
| 7854 | if (got_int) |
| 7855 | return; |
| 7856 | } |
| 7857 | msg_col = 4; |
| 7858 | msg_outtrans(ap->pat); |
| 7859 | |
| 7860 | for (ac = ap->cmds; ac != NULL; ac = ac->next) |
| 7861 | { |
| 7862 | if (ac->cmd != NULL) /* skip removed commands */ |
| 7863 | { |
| 7864 | if (msg_col >= 14) |
| 7865 | msg_putchar('\n'); |
| 7866 | msg_col = 14; |
| 7867 | if (got_int) |
| 7868 | return; |
| 7869 | msg_outtrans(ac->cmd); |
Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 7870 | #ifdef FEAT_EVAL |
| 7871 | if (p_verbose > 0) |
| 7872 | last_set_msg(ac->scriptID); |
| 7873 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7874 | if (got_int) |
| 7875 | return; |
| 7876 | if (ac->next != NULL) |
| 7877 | { |
| 7878 | msg_putchar('\n'); |
| 7879 | if (got_int) |
| 7880 | return; |
| 7881 | } |
| 7882 | } |
| 7883 | } |
| 7884 | } |
| 7885 | |
| 7886 | /* |
| 7887 | * Mark an autocommand pattern for deletion. |
| 7888 | */ |
| 7889 | static void |
| 7890 | au_remove_pat(ap) |
| 7891 | AutoPat *ap; |
| 7892 | { |
| 7893 | vim_free(ap->pat); |
| 7894 | ap->pat = NULL; |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7895 | ap->buflocal_nr = -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7896 | au_need_clean = TRUE; |
| 7897 | } |
| 7898 | |
| 7899 | /* |
| 7900 | * Mark all commands for a pattern for deletion. |
| 7901 | */ |
| 7902 | static void |
| 7903 | au_remove_cmds(ap) |
| 7904 | AutoPat *ap; |
| 7905 | { |
| 7906 | AutoCmd *ac; |
| 7907 | |
| 7908 | for (ac = ap->cmds; ac != NULL; ac = ac->next) |
| 7909 | { |
| 7910 | vim_free(ac->cmd); |
| 7911 | ac->cmd = NULL; |
| 7912 | } |
| 7913 | au_need_clean = TRUE; |
| 7914 | } |
| 7915 | |
| 7916 | /* |
| 7917 | * Cleanup autocommands and patterns that have been deleted. |
| 7918 | * This is only done when not executing autocommands. |
| 7919 | */ |
| 7920 | static void |
| 7921 | au_cleanup() |
| 7922 | { |
| 7923 | AutoPat *ap, **prev_ap; |
| 7924 | AutoCmd *ac, **prev_ac; |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7925 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7926 | |
| 7927 | if (autocmd_busy || !au_need_clean) |
| 7928 | return; |
| 7929 | |
| 7930 | /* loop over all events */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7931 | for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
| 7932 | event = (event_T)((int)event + 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7933 | { |
| 7934 | /* loop over all autocommand patterns */ |
| 7935 | prev_ap = &(first_autopat[(int)event]); |
| 7936 | for (ap = *prev_ap; ap != NULL; ap = *prev_ap) |
| 7937 | { |
| 7938 | /* loop over all commands for this pattern */ |
| 7939 | prev_ac = &(ap->cmds); |
| 7940 | for (ac = *prev_ac; ac != NULL; ac = *prev_ac) |
| 7941 | { |
| 7942 | /* remove the command if the pattern is to be deleted or when |
| 7943 | * the command has been marked for deletion */ |
| 7944 | if (ap->pat == NULL || ac->cmd == NULL) |
| 7945 | { |
| 7946 | *prev_ac = ac->next; |
| 7947 | vim_free(ac->cmd); |
| 7948 | vim_free(ac); |
| 7949 | } |
| 7950 | else |
| 7951 | prev_ac = &(ac->next); |
| 7952 | } |
| 7953 | |
| 7954 | /* remove the pattern if it has been marked for deletion */ |
| 7955 | if (ap->pat == NULL) |
| 7956 | { |
| 7957 | *prev_ap = ap->next; |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7958 | vim_regfree(ap->reg_prog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7959 | vim_free(ap); |
| 7960 | } |
| 7961 | else |
| 7962 | prev_ap = &(ap->next); |
| 7963 | } |
| 7964 | } |
| 7965 | |
| 7966 | au_need_clean = FALSE; |
| 7967 | } |
| 7968 | |
| 7969 | /* |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7970 | * Called when buffer is freed, to remove/invalidate related buffer-local |
| 7971 | * autocmds. |
| 7972 | */ |
| 7973 | void |
| 7974 | aubuflocal_remove(buf) |
| 7975 | buf_T *buf; |
| 7976 | { |
| 7977 | AutoPat *ap; |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7978 | event_T event; |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7979 | AutoPatCmd *apc; |
| 7980 | |
| 7981 | /* invalidate currently executing autocommands */ |
| 7982 | for (apc = active_apc_list; apc; apc = apc->next) |
| 7983 | if (buf->b_fnum == apc->arg_bufnr) |
| 7984 | apc->arg_bufnr = 0; |
| 7985 | |
| 7986 | /* invalidate buflocals looping through events */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7987 | for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
| 7988 | event = (event_T)((int)event + 1)) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7989 | /* loop over all autocommand patterns */ |
| 7990 | for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) |
| 7991 | if (ap->buflocal_nr == buf->b_fnum) |
| 7992 | { |
| 7993 | au_remove_pat(ap); |
| 7994 | if (p_verbose >= 6) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 7995 | { |
| 7996 | verbose_enter(); |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7997 | smsg((char_u *) |
| 7998 | _("auto-removing autocommand: %s <buffer=%d>"), |
| 7999 | event_nr2name(event), buf->b_fnum); |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 8000 | verbose_leave(); |
| 8001 | } |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8002 | } |
| 8003 | au_cleanup(); |
| 8004 | } |
| 8005 | |
| 8006 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8007 | * Add an autocmd group name. |
| 8008 | * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. |
| 8009 | */ |
| 8010 | static int |
| 8011 | au_new_group(name) |
| 8012 | char_u *name; |
| 8013 | { |
| 8014 | int i; |
| 8015 | |
| 8016 | i = au_find_group(name); |
| 8017 | if (i == AUGROUP_ERROR) /* the group doesn't exist yet, add it */ |
| 8018 | { |
| 8019 | /* First try using a free entry. */ |
| 8020 | for (i = 0; i < augroups.ga_len; ++i) |
| 8021 | if (AUGROUP_NAME(i) == NULL) |
| 8022 | break; |
| 8023 | if (i == augroups.ga_len && ga_grow(&augroups, 1) == FAIL) |
| 8024 | return AUGROUP_ERROR; |
| 8025 | |
| 8026 | AUGROUP_NAME(i) = vim_strsave(name); |
| 8027 | if (AUGROUP_NAME(i) == NULL) |
| 8028 | return AUGROUP_ERROR; |
| 8029 | if (i == augroups.ga_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8030 | ++augroups.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8031 | } |
| 8032 | |
| 8033 | return i; |
| 8034 | } |
| 8035 | |
| 8036 | static void |
| 8037 | au_del_group(name) |
| 8038 | char_u *name; |
| 8039 | { |
| 8040 | int i; |
| 8041 | |
| 8042 | i = au_find_group(name); |
| 8043 | if (i == AUGROUP_ERROR) /* the group doesn't exist */ |
| 8044 | EMSG2(_("E367: No such group: \"%s\""), name); |
| 8045 | else |
| 8046 | { |
| 8047 | vim_free(AUGROUP_NAME(i)); |
| 8048 | AUGROUP_NAME(i) = NULL; |
| 8049 | } |
| 8050 | } |
| 8051 | |
| 8052 | /* |
| 8053 | * Find the ID of an autocmd group name. |
| 8054 | * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. |
| 8055 | */ |
| 8056 | static int |
| 8057 | au_find_group(name) |
| 8058 | char_u *name; |
| 8059 | { |
| 8060 | int i; |
| 8061 | |
| 8062 | for (i = 0; i < augroups.ga_len; ++i) |
| 8063 | if (AUGROUP_NAME(i) != NULL && STRCMP(AUGROUP_NAME(i), name) == 0) |
| 8064 | return i; |
| 8065 | return AUGROUP_ERROR; |
| 8066 | } |
| 8067 | |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 8068 | /* |
| 8069 | * Return TRUE if augroup "name" exists. |
| 8070 | */ |
| 8071 | int |
| 8072 | au_has_group(name) |
| 8073 | char_u *name; |
| 8074 | { |
| 8075 | return au_find_group(name) != AUGROUP_ERROR; |
| 8076 | } |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 8077 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8078 | /* |
| 8079 | * ":augroup {name}". |
| 8080 | */ |
| 8081 | void |
| 8082 | do_augroup(arg, del_group) |
| 8083 | char_u *arg; |
| 8084 | int del_group; |
| 8085 | { |
| 8086 | int i; |
| 8087 | |
| 8088 | if (del_group) |
| 8089 | { |
| 8090 | if (*arg == NUL) |
| 8091 | EMSG(_(e_argreq)); |
| 8092 | else |
| 8093 | au_del_group(arg); |
| 8094 | } |
| 8095 | else if (STRICMP(arg, "end") == 0) /* ":aug end": back to group 0 */ |
| 8096 | current_augroup = AUGROUP_DEFAULT; |
| 8097 | else if (*arg) /* ":aug xxx": switch to group xxx */ |
| 8098 | { |
| 8099 | i = au_new_group(arg); |
| 8100 | if (i != AUGROUP_ERROR) |
| 8101 | current_augroup = i; |
| 8102 | } |
| 8103 | else /* ":aug": list the group names */ |
| 8104 | { |
| 8105 | msg_start(); |
| 8106 | for (i = 0; i < augroups.ga_len; ++i) |
| 8107 | { |
| 8108 | if (AUGROUP_NAME(i) != NULL) |
| 8109 | { |
| 8110 | msg_puts(AUGROUP_NAME(i)); |
| 8111 | msg_puts((char_u *)" "); |
| 8112 | } |
| 8113 | } |
| 8114 | msg_clr_eos(); |
| 8115 | msg_end(); |
| 8116 | } |
| 8117 | } |
| 8118 | |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 8119 | #if defined(EXITFREE) || defined(PROTO) |
| 8120 | void |
| 8121 | free_all_autocmds() |
| 8122 | { |
| 8123 | for (current_augroup = -1; current_augroup < augroups.ga_len; |
| 8124 | ++current_augroup) |
| 8125 | do_autocmd((char_u *)"", TRUE); |
| 8126 | ga_clear_strings(&augroups); |
| 8127 | } |
| 8128 | #endif |
| 8129 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8130 | /* |
| 8131 | * Return the event number for event name "start". |
| 8132 | * Return NUM_EVENTS if the event name was not found. |
| 8133 | * Return a pointer to the next event name in "end". |
| 8134 | */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8135 | static event_T |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8136 | event_name2nr(start, end) |
| 8137 | char_u *start; |
| 8138 | char_u **end; |
| 8139 | { |
| 8140 | char_u *p; |
| 8141 | int i; |
| 8142 | int len; |
| 8143 | |
| 8144 | /* the event name ends with end of line, a blank or a comma */ |
| 8145 | for (p = start; *p && !vim_iswhite(*p) && *p != ','; ++p) |
| 8146 | ; |
| 8147 | for (i = 0; event_names[i].name != NULL; ++i) |
| 8148 | { |
| 8149 | len = (int)STRLEN(event_names[i].name); |
| 8150 | if (len == p - start && STRNICMP(event_names[i].name, start, len) == 0) |
| 8151 | break; |
| 8152 | } |
| 8153 | if (*p == ',') |
| 8154 | ++p; |
| 8155 | *end = p; |
| 8156 | if (event_names[i].name == NULL) |
| 8157 | return NUM_EVENTS; |
| 8158 | return event_names[i].event; |
| 8159 | } |
| 8160 | |
| 8161 | /* |
| 8162 | * Return the name for event "event". |
| 8163 | */ |
| 8164 | static char_u * |
| 8165 | event_nr2name(event) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8166 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8167 | { |
| 8168 | int i; |
| 8169 | |
| 8170 | for (i = 0; event_names[i].name != NULL; ++i) |
| 8171 | if (event_names[i].event == event) |
| 8172 | return (char_u *)event_names[i].name; |
| 8173 | return (char_u *)"Unknown"; |
| 8174 | } |
| 8175 | |
| 8176 | /* |
| 8177 | * Scan over the events. "*" stands for all events. |
| 8178 | */ |
| 8179 | static char_u * |
| 8180 | find_end_event(arg, have_group) |
| 8181 | char_u *arg; |
| 8182 | int have_group; /* TRUE when group name was found */ |
| 8183 | { |
| 8184 | char_u *pat; |
| 8185 | char_u *p; |
| 8186 | |
| 8187 | if (*arg == '*') |
| 8188 | { |
| 8189 | if (arg[1] && !vim_iswhite(arg[1])) |
| 8190 | { |
| 8191 | EMSG2(_("E215: Illegal character after *: %s"), arg); |
| 8192 | return NULL; |
| 8193 | } |
| 8194 | pat = arg + 1; |
| 8195 | } |
| 8196 | else |
| 8197 | { |
| 8198 | for (pat = arg; *pat && !vim_iswhite(*pat); pat = p) |
| 8199 | { |
| 8200 | if ((int)event_name2nr(pat, &p) >= (int)NUM_EVENTS) |
| 8201 | { |
| 8202 | if (have_group) |
| 8203 | EMSG2(_("E216: No such event: %s"), pat); |
| 8204 | else |
| 8205 | EMSG2(_("E216: No such group or event: %s"), pat); |
| 8206 | return NULL; |
| 8207 | } |
| 8208 | } |
| 8209 | } |
| 8210 | return pat; |
| 8211 | } |
| 8212 | |
| 8213 | /* |
| 8214 | * Return TRUE if "event" is included in 'eventignore'. |
| 8215 | */ |
| 8216 | static int |
| 8217 | event_ignored(event) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8218 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8219 | { |
| 8220 | char_u *p = p_ei; |
| 8221 | |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 8222 | while (*p != NUL) |
| 8223 | { |
| 8224 | if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) |
| 8225 | return TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8226 | if (event_name2nr(p, &p) == event) |
| 8227 | return TRUE; |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 8228 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8229 | |
| 8230 | return FALSE; |
| 8231 | } |
| 8232 | |
| 8233 | /* |
| 8234 | * Return OK when the contents of p_ei is valid, FAIL otherwise. |
| 8235 | */ |
| 8236 | int |
| 8237 | check_ei() |
| 8238 | { |
| 8239 | char_u *p = p_ei; |
| 8240 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8241 | while (*p) |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 8242 | { |
| 8243 | if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) |
| 8244 | { |
| 8245 | p += 3; |
| 8246 | if (*p == ',') |
| 8247 | ++p; |
| 8248 | } |
| 8249 | else if (event_name2nr(p, &p) == NUM_EVENTS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8250 | return FAIL; |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 8251 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8252 | |
| 8253 | return OK; |
| 8254 | } |
| 8255 | |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 8256 | # if defined(FEAT_SYN_HL) || defined(PROTO) |
| 8257 | |
| 8258 | /* |
| 8259 | * Add "what" to 'eventignore' to skip loading syntax highlighting for every |
| 8260 | * buffer loaded into the window. "what" must start with a comma. |
| 8261 | * Returns the old value of 'eventignore' in allocated memory. |
| 8262 | */ |
| 8263 | char_u * |
| 8264 | au_event_disable(what) |
| 8265 | char *what; |
| 8266 | { |
| 8267 | char_u *new_ei; |
| 8268 | char_u *save_ei; |
| 8269 | |
| 8270 | save_ei = vim_strsave(p_ei); |
| 8271 | if (save_ei != NULL) |
| 8272 | { |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 8273 | new_ei = vim_strnsave(p_ei, (int)(STRLEN(p_ei) + STRLEN(what))); |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 8274 | if (new_ei != NULL) |
| 8275 | { |
Bram Moolenaar | 8cac9fd | 2010-03-02 12:48:05 +0100 | [diff] [blame] | 8276 | if (*what == ',' && *p_ei == NUL) |
| 8277 | STRCPY(new_ei, what + 1); |
| 8278 | else |
| 8279 | STRCAT(new_ei, what); |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 8280 | set_string_option_direct((char_u *)"ei", -1, new_ei, |
| 8281 | OPT_FREE, SID_NONE); |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 8282 | vim_free(new_ei); |
| 8283 | } |
| 8284 | } |
| 8285 | return save_ei; |
| 8286 | } |
| 8287 | |
| 8288 | void |
| 8289 | au_event_restore(old_ei) |
| 8290 | char_u *old_ei; |
| 8291 | { |
| 8292 | if (old_ei != NULL) |
| 8293 | { |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 8294 | set_string_option_direct((char_u *)"ei", -1, old_ei, |
| 8295 | OPT_FREE, SID_NONE); |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 8296 | vim_free(old_ei); |
| 8297 | } |
| 8298 | } |
| 8299 | # endif /* FEAT_SYN_HL */ |
| 8300 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8301 | /* |
| 8302 | * do_autocmd() -- implements the :autocmd command. Can be used in the |
| 8303 | * following ways: |
| 8304 | * |
| 8305 | * :autocmd <event> <pat> <cmd> Add <cmd> to the list of commands that |
| 8306 | * will be automatically executed for <event> |
| 8307 | * when editing a file matching <pat>, in |
| 8308 | * the current group. |
| 8309 | * :autocmd <event> <pat> Show the auto-commands associated with |
| 8310 | * <event> and <pat>. |
| 8311 | * :autocmd <event> Show the auto-commands associated with |
| 8312 | * <event>. |
| 8313 | * :autocmd Show all auto-commands. |
| 8314 | * :autocmd! <event> <pat> <cmd> Remove all auto-commands associated with |
| 8315 | * <event> and <pat>, and add the command |
| 8316 | * <cmd>, for the current group. |
| 8317 | * :autocmd! <event> <pat> Remove all auto-commands associated with |
| 8318 | * <event> and <pat> for the current group. |
| 8319 | * :autocmd! <event> Remove all auto-commands associated with |
| 8320 | * <event> for the current group. |
| 8321 | * :autocmd! Remove ALL auto-commands for the current |
| 8322 | * group. |
| 8323 | * |
| 8324 | * Multiple events and patterns may be given separated by commas. Here are |
| 8325 | * some examples: |
| 8326 | * :autocmd bufread,bufenter *.c,*.h set tw=0 smartindent noic |
| 8327 | * :autocmd bufleave * set tw=79 nosmartindent ic infercase |
| 8328 | * |
| 8329 | * :autocmd * *.c show all autocommands for *.c files. |
Bram Moolenaar | d35f971 | 2005-12-18 22:02:33 +0000 | [diff] [blame] | 8330 | * |
| 8331 | * Mostly a {group} argument can optionally appear before <event>. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8332 | */ |
| 8333 | void |
| 8334 | do_autocmd(arg, forceit) |
| 8335 | char_u *arg; |
| 8336 | int forceit; |
| 8337 | { |
| 8338 | char_u *pat; |
| 8339 | char_u *envpat = NULL; |
| 8340 | char_u *cmd; |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8341 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8342 | int need_free = FALSE; |
| 8343 | int nested = FALSE; |
| 8344 | int group; |
| 8345 | |
| 8346 | /* |
| 8347 | * Check for a legal group name. If not, use AUGROUP_ALL. |
| 8348 | */ |
| 8349 | group = au_get_grouparg(&arg); |
| 8350 | if (arg == NULL) /* out of memory */ |
| 8351 | return; |
| 8352 | |
| 8353 | /* |
| 8354 | * Scan over the events. |
| 8355 | * If we find an illegal name, return here, don't do anything. |
| 8356 | */ |
| 8357 | pat = find_end_event(arg, group != AUGROUP_ALL); |
| 8358 | if (pat == NULL) |
| 8359 | return; |
| 8360 | |
| 8361 | /* |
| 8362 | * Scan over the pattern. Put a NUL at the end. |
| 8363 | */ |
| 8364 | pat = skipwhite(pat); |
| 8365 | cmd = pat; |
| 8366 | while (*cmd && (!vim_iswhite(*cmd) || cmd[-1] == '\\')) |
| 8367 | cmd++; |
| 8368 | if (*cmd) |
| 8369 | *cmd++ = NUL; |
| 8370 | |
| 8371 | /* Expand environment variables in the pattern. Set 'shellslash', we want |
| 8372 | * forward slashes here. */ |
| 8373 | if (vim_strchr(pat, '$') != NULL || vim_strchr(pat, '~') != NULL) |
| 8374 | { |
| 8375 | #ifdef BACKSLASH_IN_FILENAME |
| 8376 | int p_ssl_save = p_ssl; |
| 8377 | |
| 8378 | p_ssl = TRUE; |
| 8379 | #endif |
| 8380 | envpat = expand_env_save(pat); |
| 8381 | #ifdef BACKSLASH_IN_FILENAME |
| 8382 | p_ssl = p_ssl_save; |
| 8383 | #endif |
| 8384 | if (envpat != NULL) |
| 8385 | pat = envpat; |
| 8386 | } |
| 8387 | |
| 8388 | /* |
| 8389 | * Check for "nested" flag. |
| 8390 | */ |
| 8391 | cmd = skipwhite(cmd); |
| 8392 | if (*cmd != NUL && STRNCMP(cmd, "nested", 6) == 0 && vim_iswhite(cmd[6])) |
| 8393 | { |
| 8394 | nested = TRUE; |
| 8395 | cmd = skipwhite(cmd + 6); |
| 8396 | } |
| 8397 | |
| 8398 | /* |
| 8399 | * Find the start of the commands. |
| 8400 | * Expand <sfile> in it. |
| 8401 | */ |
| 8402 | if (*cmd != NUL) |
| 8403 | { |
| 8404 | cmd = expand_sfile(cmd); |
| 8405 | if (cmd == NULL) /* some error */ |
| 8406 | return; |
| 8407 | need_free = TRUE; |
| 8408 | } |
| 8409 | |
| 8410 | /* |
| 8411 | * Print header when showing autocommands. |
| 8412 | */ |
| 8413 | if (!forceit && *cmd == NUL) |
| 8414 | { |
| 8415 | /* Highlight title */ |
| 8416 | MSG_PUTS_TITLE(_("\n--- Auto-Commands ---")); |
| 8417 | } |
| 8418 | |
| 8419 | /* |
| 8420 | * Loop over the events. |
| 8421 | */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8422 | last_event = (event_T)-1; /* for listing the event name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8423 | last_group = AUGROUP_ERROR; /* for listing the group name */ |
| 8424 | if (*arg == '*' || *arg == NUL) |
| 8425 | { |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8426 | for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
| 8427 | event = (event_T)((int)event + 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8428 | if (do_autocmd_event(event, pat, |
| 8429 | nested, cmd, forceit, group) == FAIL) |
| 8430 | break; |
| 8431 | } |
| 8432 | else |
| 8433 | { |
| 8434 | while (*arg && !vim_iswhite(*arg)) |
| 8435 | if (do_autocmd_event(event_name2nr(arg, &arg), pat, |
| 8436 | nested, cmd, forceit, group) == FAIL) |
| 8437 | break; |
| 8438 | } |
| 8439 | |
| 8440 | if (need_free) |
| 8441 | vim_free(cmd); |
| 8442 | vim_free(envpat); |
| 8443 | } |
| 8444 | |
| 8445 | /* |
| 8446 | * Find the group ID in a ":autocmd" or ":doautocmd" argument. |
| 8447 | * The "argp" argument is advanced to the following argument. |
| 8448 | * |
| 8449 | * Returns the group ID, AUGROUP_ERROR for error (out of memory). |
| 8450 | */ |
| 8451 | static int |
| 8452 | au_get_grouparg(argp) |
| 8453 | char_u **argp; |
| 8454 | { |
| 8455 | char_u *group_name; |
| 8456 | char_u *p; |
| 8457 | char_u *arg = *argp; |
| 8458 | int group = AUGROUP_ALL; |
| 8459 | |
| 8460 | p = skiptowhite(arg); |
| 8461 | if (p > arg) |
| 8462 | { |
| 8463 | group_name = vim_strnsave(arg, (int)(p - arg)); |
| 8464 | if (group_name == NULL) /* out of memory */ |
| 8465 | return AUGROUP_ERROR; |
| 8466 | group = au_find_group(group_name); |
| 8467 | if (group == AUGROUP_ERROR) |
| 8468 | group = AUGROUP_ALL; /* no match, use all groups */ |
| 8469 | else |
| 8470 | *argp = skipwhite(p); /* match, skip over group name */ |
| 8471 | vim_free(group_name); |
| 8472 | } |
| 8473 | return group; |
| 8474 | } |
| 8475 | |
| 8476 | /* |
| 8477 | * do_autocmd() for one event. |
| 8478 | * If *pat == NUL do for all patterns. |
| 8479 | * If *cmd == NUL show entries. |
| 8480 | * If forceit == TRUE delete entries. |
| 8481 | * If group is not AUGROUP_ALL, only use this group. |
| 8482 | */ |
| 8483 | static int |
| 8484 | do_autocmd_event(event, pat, nested, cmd, forceit, group) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8485 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8486 | char_u *pat; |
| 8487 | int nested; |
| 8488 | char_u *cmd; |
| 8489 | int forceit; |
| 8490 | int group; |
| 8491 | { |
| 8492 | AutoPat *ap; |
| 8493 | AutoPat **prev_ap; |
| 8494 | AutoCmd *ac; |
| 8495 | AutoCmd **prev_ac; |
| 8496 | int brace_level; |
| 8497 | char_u *endpat; |
| 8498 | int findgroup; |
| 8499 | int allgroups; |
| 8500 | int patlen; |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 8501 | int is_buflocal; |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8502 | int buflocal_nr; |
| 8503 | char_u buflocal_pat[25]; /* for "<buffer=X>" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8504 | |
| 8505 | if (group == AUGROUP_ALL) |
| 8506 | findgroup = current_augroup; |
| 8507 | else |
| 8508 | findgroup = group; |
| 8509 | allgroups = (group == AUGROUP_ALL && !forceit && *cmd == NUL); |
| 8510 | |
| 8511 | /* |
| 8512 | * Show or delete all patterns for an event. |
| 8513 | */ |
| 8514 | if (*pat == NUL) |
| 8515 | { |
| 8516 | for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) |
| 8517 | { |
| 8518 | if (forceit) /* delete the AutoPat, if it's in the current group */ |
| 8519 | { |
| 8520 | if (ap->group == findgroup) |
| 8521 | au_remove_pat(ap); |
| 8522 | } |
| 8523 | else if (group == AUGROUP_ALL || ap->group == group) |
| 8524 | show_autocmd(ap, event); |
| 8525 | } |
| 8526 | } |
| 8527 | |
| 8528 | /* |
| 8529 | * Loop through all the specified patterns. |
| 8530 | */ |
| 8531 | for ( ; *pat; pat = (*endpat == ',' ? endpat + 1 : endpat)) |
| 8532 | { |
| 8533 | /* |
| 8534 | * Find end of the pattern. |
| 8535 | * Watch out for a comma in braces, like "*.\{obj,o\}". |
| 8536 | */ |
| 8537 | brace_level = 0; |
| 8538 | for (endpat = pat; *endpat && (*endpat != ',' || brace_level |
| 8539 | || endpat[-1] == '\\'); ++endpat) |
| 8540 | { |
| 8541 | if (*endpat == '{') |
| 8542 | brace_level++; |
| 8543 | else if (*endpat == '}') |
| 8544 | brace_level--; |
| 8545 | } |
| 8546 | if (pat == endpat) /* ignore single comma */ |
| 8547 | continue; |
| 8548 | patlen = (int)(endpat - pat); |
| 8549 | |
| 8550 | /* |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8551 | * detect special <buflocal[=X]> buffer-local patterns |
| 8552 | */ |
| 8553 | is_buflocal = FALSE; |
| 8554 | buflocal_nr = 0; |
| 8555 | |
| 8556 | if (patlen >= 7 && STRNCMP(pat, "<buffer", 7) == 0 |
| 8557 | && pat[patlen - 1] == '>') |
| 8558 | { |
| 8559 | /* Error will be printed only for addition. printing and removing |
| 8560 | * will proceed silently. */ |
| 8561 | is_buflocal = TRUE; |
| 8562 | if (patlen == 8) |
| 8563 | buflocal_nr = curbuf->b_fnum; |
| 8564 | else if (patlen > 9 && pat[7] == '=') |
| 8565 | { |
| 8566 | /* <buffer=abuf> */ |
| 8567 | if (patlen == 13 && STRNICMP(pat, "<buffer=abuf>", 13)) |
| 8568 | buflocal_nr = autocmd_bufnr; |
| 8569 | /* <buffer=123> */ |
| 8570 | else if (skipdigits(pat + 8) == pat + patlen - 1) |
| 8571 | buflocal_nr = atoi((char *)pat + 8); |
| 8572 | } |
| 8573 | } |
| 8574 | |
| 8575 | if (is_buflocal) |
| 8576 | { |
| 8577 | /* normalize pat into standard "<buffer>#N" form */ |
| 8578 | sprintf((char *)buflocal_pat, "<buffer=%d>", buflocal_nr); |
| 8579 | pat = buflocal_pat; /* can modify pat and patlen */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 8580 | patlen = (int)STRLEN(buflocal_pat); /* but not endpat */ |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8581 | } |
| 8582 | |
| 8583 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8584 | * Find AutoPat entries with this pattern. |
| 8585 | */ |
| 8586 | prev_ap = &first_autopat[(int)event]; |
| 8587 | while ((ap = *prev_ap) != NULL) |
| 8588 | { |
| 8589 | if (ap->pat != NULL) |
| 8590 | { |
| 8591 | /* Accept a pattern when: |
| 8592 | * - a group was specified and it's that group, or a group was |
| 8593 | * not specified and it's the current group, or a group was |
| 8594 | * not specified and we are listing |
| 8595 | * - the length of the pattern matches |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8596 | * - the pattern matches. |
| 8597 | * For <buffer[=X]>, this condition works because we normalize |
| 8598 | * all buffer-local patterns. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8599 | */ |
| 8600 | if ((allgroups || ap->group == findgroup) |
| 8601 | && ap->patlen == patlen |
| 8602 | && STRNCMP(pat, ap->pat, patlen) == 0) |
| 8603 | { |
| 8604 | /* |
| 8605 | * Remove existing autocommands. |
| 8606 | * If adding any new autocmd's for this AutoPat, don't |
| 8607 | * delete the pattern from the autopat list, append to |
| 8608 | * this list. |
| 8609 | */ |
| 8610 | if (forceit) |
| 8611 | { |
| 8612 | if (*cmd != NUL && ap->next == NULL) |
| 8613 | { |
| 8614 | au_remove_cmds(ap); |
| 8615 | break; |
| 8616 | } |
| 8617 | au_remove_pat(ap); |
| 8618 | } |
| 8619 | |
| 8620 | /* |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8621 | * Show autocmd's for this autopat, or buflocals <buffer=X> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8622 | */ |
| 8623 | else if (*cmd == NUL) |
| 8624 | show_autocmd(ap, event); |
| 8625 | |
| 8626 | /* |
| 8627 | * Add autocmd to this autopat, if it's the last one. |
| 8628 | */ |
| 8629 | else if (ap->next == NULL) |
| 8630 | break; |
| 8631 | } |
| 8632 | } |
| 8633 | prev_ap = &ap->next; |
| 8634 | } |
| 8635 | |
| 8636 | /* |
| 8637 | * Add a new command. |
| 8638 | */ |
| 8639 | if (*cmd != NUL) |
| 8640 | { |
| 8641 | /* |
| 8642 | * If the pattern we want to add a command to does appear at the |
| 8643 | * end of the list (or not is not in the list at all), add the |
| 8644 | * pattern at the end of the list. |
| 8645 | */ |
| 8646 | if (ap == NULL) |
| 8647 | { |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8648 | /* refuse to add buffer-local ap if buffer number is invalid */ |
| 8649 | if (is_buflocal && (buflocal_nr == 0 |
| 8650 | || buflist_findnr(buflocal_nr) == NULL)) |
| 8651 | { |
| 8652 | EMSGN(_("E680: <buffer=%d>: invalid buffer number "), |
| 8653 | buflocal_nr); |
| 8654 | return FAIL; |
| 8655 | } |
| 8656 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8657 | ap = (AutoPat *)alloc((unsigned)sizeof(AutoPat)); |
| 8658 | if (ap == NULL) |
| 8659 | return FAIL; |
| 8660 | ap->pat = vim_strnsave(pat, patlen); |
| 8661 | ap->patlen = patlen; |
| 8662 | if (ap->pat == NULL) |
| 8663 | { |
| 8664 | vim_free(ap); |
| 8665 | return FAIL; |
| 8666 | } |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8667 | |
| 8668 | if (is_buflocal) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8669 | { |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8670 | ap->buflocal_nr = buflocal_nr; |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 8671 | ap->reg_prog = NULL; |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8672 | } |
| 8673 | else |
| 8674 | { |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 8675 | char_u *reg_pat; |
| 8676 | |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8677 | ap->buflocal_nr = 0; |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 8678 | reg_pat = file_pat_to_reg_pat(pat, endpat, |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8679 | &ap->allow_dirs, TRUE); |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 8680 | if (reg_pat != NULL) |
| 8681 | ap->reg_prog = vim_regcomp(reg_pat, RE_MAGIC); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 8682 | vim_free(reg_pat); |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 8683 | if (reg_pat == NULL || ap->reg_prog == NULL) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8684 | { |
| 8685 | vim_free(ap->pat); |
| 8686 | vim_free(ap); |
| 8687 | return FAIL; |
| 8688 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8689 | } |
| 8690 | ap->cmds = NULL; |
| 8691 | *prev_ap = ap; |
| 8692 | ap->next = NULL; |
| 8693 | if (group == AUGROUP_ALL) |
| 8694 | ap->group = current_augroup; |
| 8695 | else |
| 8696 | ap->group = group; |
| 8697 | } |
| 8698 | |
| 8699 | /* |
| 8700 | * Add the autocmd at the end of the AutoCmd list. |
| 8701 | */ |
| 8702 | prev_ac = &(ap->cmds); |
| 8703 | while ((ac = *prev_ac) != NULL) |
| 8704 | prev_ac = &ac->next; |
| 8705 | ac = (AutoCmd *)alloc((unsigned)sizeof(AutoCmd)); |
| 8706 | if (ac == NULL) |
| 8707 | return FAIL; |
| 8708 | ac->cmd = vim_strsave(cmd); |
| 8709 | #ifdef FEAT_EVAL |
| 8710 | ac->scriptID = current_SID; |
| 8711 | #endif |
| 8712 | if (ac->cmd == NULL) |
| 8713 | { |
| 8714 | vim_free(ac); |
| 8715 | return FAIL; |
| 8716 | } |
| 8717 | ac->next = NULL; |
| 8718 | *prev_ac = ac; |
| 8719 | ac->nested = nested; |
| 8720 | } |
| 8721 | } |
| 8722 | |
| 8723 | au_cleanup(); /* may really delete removed patterns/commands now */ |
| 8724 | return OK; |
| 8725 | } |
| 8726 | |
| 8727 | /* |
| 8728 | * Implementation of ":doautocmd [group] event [fname]". |
| 8729 | * Return OK for success, FAIL for failure; |
| 8730 | */ |
| 8731 | int |
| 8732 | do_doautocmd(arg, do_msg) |
| 8733 | char_u *arg; |
| 8734 | int do_msg; /* give message for no matching autocmds? */ |
| 8735 | { |
| 8736 | char_u *fname; |
| 8737 | int nothing_done = TRUE; |
| 8738 | int group; |
| 8739 | |
| 8740 | /* |
| 8741 | * Check for a legal group name. If not, use AUGROUP_ALL. |
| 8742 | */ |
| 8743 | group = au_get_grouparg(&arg); |
| 8744 | if (arg == NULL) /* out of memory */ |
| 8745 | return FAIL; |
| 8746 | |
| 8747 | if (*arg == '*') |
| 8748 | { |
| 8749 | EMSG(_("E217: Can't execute autocommands for ALL events")); |
| 8750 | return FAIL; |
| 8751 | } |
| 8752 | |
| 8753 | /* |
| 8754 | * Scan over the events. |
| 8755 | * If we find an illegal name, return here, don't do anything. |
| 8756 | */ |
| 8757 | fname = find_end_event(arg, group != AUGROUP_ALL); |
| 8758 | if (fname == NULL) |
| 8759 | return FAIL; |
| 8760 | |
| 8761 | fname = skipwhite(fname); |
| 8762 | |
| 8763 | /* |
| 8764 | * Loop over the events. |
| 8765 | */ |
| 8766 | while (*arg && !vim_iswhite(*arg)) |
| 8767 | if (apply_autocmds_group(event_name2nr(arg, &arg), |
| 8768 | fname, NULL, TRUE, group, curbuf, NULL)) |
| 8769 | nothing_done = FALSE; |
| 8770 | |
| 8771 | if (nothing_done && do_msg) |
| 8772 | MSG(_("No matching autocommands")); |
| 8773 | |
| 8774 | #ifdef FEAT_EVAL |
| 8775 | return aborting() ? FAIL : OK; |
| 8776 | #else |
| 8777 | return OK; |
| 8778 | #endif |
| 8779 | } |
| 8780 | |
| 8781 | /* |
| 8782 | * ":doautoall": execute autocommands for each loaded buffer. |
| 8783 | */ |
| 8784 | void |
| 8785 | ex_doautoall(eap) |
| 8786 | exarg_T *eap; |
| 8787 | { |
| 8788 | int retval; |
| 8789 | aco_save_T aco; |
| 8790 | buf_T *buf; |
Bram Moolenaar | a61d5fb | 2012-02-12 00:18:58 +0100 | [diff] [blame] | 8791 | char_u *arg = eap->arg; |
Bram Moolenaar | 60542ac | 2012-02-12 20:14:01 +0100 | [diff] [blame] | 8792 | int call_do_modelines = check_nomodeline(&arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8793 | |
| 8794 | /* |
| 8795 | * This is a bit tricky: For some commands curwin->w_buffer needs to be |
| 8796 | * equal to curbuf, but for some buffers there may not be a window. |
| 8797 | * So we change the buffer for the current window for a moment. This |
| 8798 | * gives problems when the autocommands make changes to the list of |
| 8799 | * buffers or windows... |
| 8800 | */ |
| 8801 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 8802 | { |
Bram Moolenaar | 3a84797 | 2008-07-08 09:36:58 +0000 | [diff] [blame] | 8803 | if (buf->b_ml.ml_mfp != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8804 | { |
| 8805 | /* find a window for this buffer and save some values */ |
| 8806 | aucmd_prepbuf(&aco, buf); |
| 8807 | |
| 8808 | /* execute the autocommands for this buffer */ |
Bram Moolenaar | a61d5fb | 2012-02-12 00:18:58 +0100 | [diff] [blame] | 8809 | retval = do_doautocmd(arg, FALSE); |
Bram Moolenaar | eeefcc7 | 2007-05-01 21:21:21 +0000 | [diff] [blame] | 8810 | |
Bram Moolenaar | a61d5fb | 2012-02-12 00:18:58 +0100 | [diff] [blame] | 8811 | if (call_do_modelines) |
| 8812 | { |
| 8813 | /* Execute the modeline settings, but don't set window-local |
| 8814 | * options if we are using the current window for another |
| 8815 | * buffer. */ |
| 8816 | do_modelines(curwin == aucmd_win ? OPT_NOWIN : 0); |
| 8817 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8818 | |
| 8819 | /* restore the current window */ |
| 8820 | aucmd_restbuf(&aco); |
| 8821 | |
| 8822 | /* stop if there is some error or buffer was deleted */ |
| 8823 | if (retval == FAIL || !buf_valid(buf)) |
| 8824 | break; |
| 8825 | } |
| 8826 | } |
| 8827 | |
| 8828 | check_cursor(); /* just in case lines got deleted */ |
| 8829 | } |
| 8830 | |
| 8831 | /* |
Bram Moolenaar | 60542ac | 2012-02-12 20:14:01 +0100 | [diff] [blame] | 8832 | * Check *argp for <nomodeline>. When it is present return FALSE, otherwise |
| 8833 | * return TRUE and advance *argp to after it. |
| 8834 | * Thus return TRUE when do_modelines() should be called. |
| 8835 | */ |
| 8836 | int |
| 8837 | check_nomodeline(argp) |
| 8838 | char_u **argp; |
| 8839 | { |
| 8840 | if (STRNCMP(*argp, "<nomodeline>", 12) == 0) |
| 8841 | { |
| 8842 | *argp = skipwhite(*argp + 12); |
| 8843 | return FALSE; |
| 8844 | } |
| 8845 | return TRUE; |
| 8846 | } |
| 8847 | |
| 8848 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8849 | * Prepare for executing autocommands for (hidden) buffer "buf". |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8850 | * Search for a visible window containing the current buffer. If there isn't |
| 8851 | * one then use "aucmd_win". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8852 | * Set "curbuf" and "curwin" to match "buf". |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 8853 | * When FEAT_AUTOCMD is not defined another version is used, see below. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8854 | */ |
| 8855 | void |
| 8856 | aucmd_prepbuf(aco, buf) |
| 8857 | aco_save_T *aco; /* structure to save values in */ |
| 8858 | buf_T *buf; /* new curbuf */ |
| 8859 | { |
| 8860 | win_T *win; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8861 | #ifdef FEAT_WINDOWS |
| 8862 | int save_ea; |
| 8863 | #endif |
Bram Moolenaar | 01c458e | 2013-08-05 22:02:20 +0200 | [diff] [blame] | 8864 | #ifdef FEAT_AUTOCHDIR |
Bram Moolenaar | 4008f4f | 2013-08-02 17:08:13 +0200 | [diff] [blame] | 8865 | int save_acd; |
Bram Moolenaar | 01c458e | 2013-08-05 22:02:20 +0200 | [diff] [blame] | 8866 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8867 | |
| 8868 | /* Find a window that is for the new buffer */ |
| 8869 | if (buf == curbuf) /* be quick when buf is curbuf */ |
| 8870 | win = curwin; |
| 8871 | else |
| 8872 | #ifdef FEAT_WINDOWS |
| 8873 | for (win = firstwin; win != NULL; win = win->w_next) |
| 8874 | if (win->w_buffer == buf) |
| 8875 | break; |
| 8876 | #else |
| 8877 | win = NULL; |
| 8878 | #endif |
| 8879 | |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8880 | /* Allocate "aucmd_win" when needed. If this fails (out of memory) fall |
| 8881 | * back to using the current window. */ |
| 8882 | if (win == NULL && aucmd_win == NULL) |
| 8883 | { |
| 8884 | win_alloc_aucmd_win(); |
| 8885 | if (aucmd_win == NULL) |
| 8886 | win = curwin; |
| 8887 | } |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 8888 | if (win == NULL && aucmd_win_used) |
| 8889 | /* Strange recursive autocommand, fall back to using the current |
| 8890 | * window. Expect a few side effects... */ |
| 8891 | win = curwin; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8892 | |
| 8893 | aco->save_curwin = curwin; |
| 8894 | aco->save_curbuf = curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8895 | if (win != NULL) |
| 8896 | { |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8897 | /* There is a window for "buf" in the current tab page, make it the |
| 8898 | * curwin. This is preferred, it has the least side effects (esp. if |
| 8899 | * "buf" is curbuf). */ |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 8900 | aco->use_aucmd_win = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8901 | curwin = win; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8902 | } |
| 8903 | else |
| 8904 | { |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8905 | /* 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] | 8906 | * effects, insert it in the current tab page. |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8907 | * Anything related to a window (e.g., setting folds) may have |
| 8908 | * unexpected results. */ |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 8909 | aco->use_aucmd_win = TRUE; |
| 8910 | aucmd_win_used = TRUE; |
Bram Moolenaar | f061e0b | 2009-06-24 15:32:01 +0000 | [diff] [blame] | 8911 | aucmd_win->w_buffer = buf; |
Bram Moolenaar | cdddaa4 | 2010-06-07 23:07:44 +0200 | [diff] [blame] | 8912 | aucmd_win->w_s = &buf->b_s; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8913 | ++buf->b_nwindows; |
Bram Moolenaar | f061e0b | 2009-06-24 15:32:01 +0000 | [diff] [blame] | 8914 | win_init_empty(aucmd_win); /* set cursor and topline to safe values */ |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 8915 | |
| 8916 | /* Make sure w_localdir and globaldir are NULL to avoid a chdir() in |
| 8917 | * win_enter_ext(). */ |
Bram Moolenaar | 4008f4f | 2013-08-02 17:08:13 +0200 | [diff] [blame] | 8918 | vim_free(aucmd_win->w_localdir); |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 8919 | aucmd_win->w_localdir = NULL; |
| 8920 | aco->globaldir = globaldir; |
| 8921 | globaldir = NULL; |
| 8922 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8923 | |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8924 | #ifdef FEAT_WINDOWS |
Bram Moolenaar | 2bc76e6 | 2009-07-01 15:13:56 +0000 | [diff] [blame] | 8925 | /* Split the current window, put the aucmd_win in the upper half. |
| 8926 | * We don't want the BufEnter or WinEnter autocommands. */ |
| 8927 | block_autocmds(); |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8928 | make_snapshot(SNAP_AUCMD_IDX); |
| 8929 | save_ea = p_ea; |
| 8930 | p_ea = FALSE; |
Bram Moolenaar | 4008f4f | 2013-08-02 17:08:13 +0200 | [diff] [blame] | 8931 | |
Bram Moolenaar | 01c458e | 2013-08-05 22:02:20 +0200 | [diff] [blame] | 8932 | # ifdef FEAT_AUTOCHDIR |
Bram Moolenaar | 4008f4f | 2013-08-02 17:08:13 +0200 | [diff] [blame] | 8933 | /* Prevent chdir() call in win_enter_ext(), through do_autochdir(). */ |
| 8934 | save_acd = p_acd; |
| 8935 | p_acd = FALSE; |
Bram Moolenaar | 01c458e | 2013-08-05 22:02:20 +0200 | [diff] [blame] | 8936 | # endif |
Bram Moolenaar | 4008f4f | 2013-08-02 17:08:13 +0200 | [diff] [blame] | 8937 | |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8938 | (void)win_split_ins(0, WSP_TOP, aucmd_win, 0); |
| 8939 | (void)win_comp_pos(); /* recompute window positions */ |
| 8940 | p_ea = save_ea; |
Bram Moolenaar | 01c458e | 2013-08-05 22:02:20 +0200 | [diff] [blame] | 8941 | # ifdef FEAT_AUTOCHDIR |
Bram Moolenaar | 4008f4f | 2013-08-02 17:08:13 +0200 | [diff] [blame] | 8942 | p_acd = save_acd; |
Bram Moolenaar | 01c458e | 2013-08-05 22:02:20 +0200 | [diff] [blame] | 8943 | # endif |
Bram Moolenaar | 2bc76e6 | 2009-07-01 15:13:56 +0000 | [diff] [blame] | 8944 | unblock_autocmds(); |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8945 | #endif |
Bram Moolenaar | f061e0b | 2009-06-24 15:32:01 +0000 | [diff] [blame] | 8946 | curwin = aucmd_win; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8947 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8948 | curbuf = buf; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8949 | aco->new_curwin = curwin; |
| 8950 | aco->new_curbuf = curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8951 | } |
| 8952 | |
| 8953 | /* |
| 8954 | * Cleanup after executing autocommands for a (hidden) buffer. |
| 8955 | * Restore the window as it was (if possible). |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 8956 | * When FEAT_AUTOCMD is not defined another version is used, see below. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8957 | */ |
| 8958 | void |
| 8959 | aucmd_restbuf(aco) |
| 8960 | aco_save_T *aco; /* structure holding saved values */ |
| 8961 | { |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8962 | #ifdef FEAT_WINDOWS |
| 8963 | int dummy; |
| 8964 | #endif |
| 8965 | |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 8966 | if (aco->use_aucmd_win) |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8967 | { |
| 8968 | --curbuf->b_nwindows; |
| 8969 | #ifdef FEAT_WINDOWS |
| 8970 | /* 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] | 8971 | * page. Do not trigger autocommands here. */ |
| 8972 | block_autocmds(); |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8973 | if (curwin != aucmd_win) |
| 8974 | { |
| 8975 | tabpage_T *tp; |
| 8976 | win_T *wp; |
| 8977 | |
| 8978 | FOR_ALL_TAB_WINDOWS(tp, wp) |
| 8979 | { |
| 8980 | if (wp == aucmd_win) |
| 8981 | { |
| 8982 | if (tp != curtab) |
Bram Moolenaar | 49e649f | 2013-05-06 04:50:35 +0200 | [diff] [blame] | 8983 | goto_tabpage_tp(tp, TRUE, TRUE); |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8984 | win_goto(aucmd_win); |
Bram Moolenaar | 28f2908 | 2012-02-11 23:45:37 +0100 | [diff] [blame] | 8985 | goto win_found; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8986 | } |
| 8987 | } |
| 8988 | } |
Bram Moolenaar | 28f2908 | 2012-02-11 23:45:37 +0100 | [diff] [blame] | 8989 | win_found: |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8990 | |
| 8991 | /* Remove the window and frame from the tree of frames. */ |
| 8992 | (void)winframe_remove(curwin, &dummy, NULL); |
| 8993 | win_remove(curwin, NULL); |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 8994 | aucmd_win_used = FALSE; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8995 | last_status(FALSE); /* may need to remove last status line */ |
| 8996 | restore_snapshot(SNAP_AUCMD_IDX, FALSE); |
| 8997 | (void)win_comp_pos(); /* recompute window positions */ |
Bram Moolenaar | 2bc76e6 | 2009-07-01 15:13:56 +0000 | [diff] [blame] | 8998 | unblock_autocmds(); |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8999 | |
| 9000 | if (win_valid(aco->save_curwin)) |
| 9001 | curwin = aco->save_curwin; |
| 9002 | else |
| 9003 | /* Hmm, original window disappeared. Just use the first one. */ |
| 9004 | curwin = firstwin; |
| 9005 | # ifdef FEAT_EVAL |
Bram Moolenaar | 429fa85 | 2013-04-15 12:27:36 +0200 | [diff] [blame] | 9006 | vars_clear(&aucmd_win->w_vars->dv_hashtab); /* free all w: variables */ |
| 9007 | hash_init(&aucmd_win->w_vars->dv_hashtab); /* re-use the hashtab */ |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 9008 | # endif |
| 9009 | #else |
| 9010 | curwin = aco->save_curwin; |
| 9011 | #endif |
| 9012 | curbuf = curwin->w_buffer; |
| 9013 | |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 9014 | vim_free(globaldir); |
| 9015 | globaldir = aco->globaldir; |
| 9016 | |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 9017 | /* the buffer contents may have changed */ |
| 9018 | check_cursor(); |
| 9019 | if (curwin->w_topline > curbuf->b_ml.ml_line_count) |
| 9020 | { |
| 9021 | curwin->w_topline = curbuf->b_ml.ml_line_count; |
| 9022 | #ifdef FEAT_DIFF |
| 9023 | curwin->w_topfill = 0; |
| 9024 | #endif |
| 9025 | } |
| 9026 | #if defined(FEAT_GUI) |
| 9027 | /* Hide the scrollbars from the aucmd_win and update. */ |
| 9028 | gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_LEFT], FALSE); |
| 9029 | gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_RIGHT], FALSE); |
| 9030 | gui_may_update_scrollbars(); |
| 9031 | #endif |
| 9032 | } |
| 9033 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9034 | { |
| 9035 | /* restore curwin */ |
| 9036 | #ifdef FEAT_WINDOWS |
| 9037 | if (win_valid(aco->save_curwin)) |
| 9038 | #endif |
| 9039 | { |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 9040 | /* Restore the buffer which was previously edited by curwin, if |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 9041 | * 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] | 9042 | * valid. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9043 | if (curwin == aco->new_curwin |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 9044 | && curbuf != aco->new_curbuf |
| 9045 | && buf_valid(aco->new_curbuf) |
| 9046 | && aco->new_curbuf->b_ml.ml_mfp != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9047 | { |
Bram Moolenaar | 7da9c37 | 2012-04-30 17:04:52 +0200 | [diff] [blame] | 9048 | # if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) |
| 9049 | if (curwin->w_s == &curbuf->b_s) |
| 9050 | curwin->w_s = &aco->new_curbuf->b_s; |
| 9051 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9052 | --curbuf->b_nwindows; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 9053 | curbuf = aco->new_curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9054 | curwin->w_buffer = curbuf; |
| 9055 | ++curbuf->b_nwindows; |
| 9056 | } |
| 9057 | |
| 9058 | curwin = aco->save_curwin; |
| 9059 | curbuf = curwin->w_buffer; |
| 9060 | } |
| 9061 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9062 | } |
| 9063 | |
| 9064 | static int autocmd_nested = FALSE; |
| 9065 | |
| 9066 | /* |
| 9067 | * Execute autocommands for "event" and file name "fname". |
| 9068 | * Return TRUE if some commands were executed. |
| 9069 | */ |
| 9070 | int |
| 9071 | apply_autocmds(event, fname, fname_io, force, buf) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9072 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9073 | char_u *fname; /* NULL or empty means use actual file name */ |
| 9074 | char_u *fname_io; /* fname to use for <afile> on cmdline */ |
| 9075 | int force; /* when TRUE, ignore autocmd_busy */ |
| 9076 | buf_T *buf; /* buffer for <abuf> */ |
| 9077 | { |
| 9078 | return apply_autocmds_group(event, fname, fname_io, force, |
| 9079 | AUGROUP_ALL, buf, NULL); |
| 9080 | } |
| 9081 | |
| 9082 | /* |
| 9083 | * Like apply_autocmds(), but with extra "eap" argument. This takes care of |
| 9084 | * setting v:filearg. |
| 9085 | */ |
| 9086 | static int |
| 9087 | apply_autocmds_exarg(event, fname, fname_io, force, buf, eap) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9088 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9089 | char_u *fname; |
| 9090 | char_u *fname_io; |
| 9091 | int force; |
| 9092 | buf_T *buf; |
| 9093 | exarg_T *eap; |
| 9094 | { |
| 9095 | return apply_autocmds_group(event, fname, fname_io, force, |
| 9096 | AUGROUP_ALL, buf, eap); |
| 9097 | } |
| 9098 | |
| 9099 | /* |
| 9100 | * Like apply_autocmds(), but handles the caller's retval. If the script |
| 9101 | * processing is being aborted or if retval is FAIL when inside a try |
| 9102 | * conditional, no autocommands are executed. If otherwise the autocommands |
| 9103 | * cause the script to be aborted, retval is set to FAIL. |
| 9104 | */ |
| 9105 | int |
| 9106 | apply_autocmds_retval(event, fname, fname_io, force, buf, retval) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9107 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9108 | char_u *fname; /* NULL or empty means use actual file name */ |
| 9109 | char_u *fname_io; /* fname to use for <afile> on cmdline */ |
| 9110 | int force; /* when TRUE, ignore autocmd_busy */ |
| 9111 | buf_T *buf; /* buffer for <abuf> */ |
| 9112 | int *retval; /* pointer to caller's retval */ |
| 9113 | { |
| 9114 | int did_cmd; |
| 9115 | |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 9116 | #ifdef FEAT_EVAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9117 | if (should_abort(*retval)) |
| 9118 | return FALSE; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 9119 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9120 | |
| 9121 | did_cmd = apply_autocmds_group(event, fname, fname_io, force, |
| 9122 | AUGROUP_ALL, buf, NULL); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 9123 | if (did_cmd |
| 9124 | #ifdef FEAT_EVAL |
| 9125 | && aborting() |
| 9126 | #endif |
| 9127 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9128 | *retval = FAIL; |
| 9129 | return did_cmd; |
| 9130 | } |
| 9131 | |
Bram Moolenaar | d35f971 | 2005-12-18 22:02:33 +0000 | [diff] [blame] | 9132 | /* |
| 9133 | * Return TRUE when there is a CursorHold autocommand defined. |
| 9134 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9135 | int |
| 9136 | has_cursorhold() |
| 9137 | { |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9138 | return (first_autopat[(int)(get_real_state() == NORMAL_BUSY |
| 9139 | ? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)] != NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9140 | } |
Bram Moolenaar | d35f971 | 2005-12-18 22:02:33 +0000 | [diff] [blame] | 9141 | |
| 9142 | /* |
| 9143 | * Return TRUE if the CursorHold event can be triggered. |
| 9144 | */ |
| 9145 | int |
| 9146 | trigger_cursorhold() |
| 9147 | { |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9148 | int state; |
| 9149 | |
Bram Moolenaar | 0533443 | 2011-07-20 18:29:39 +0200 | [diff] [blame] | 9150 | if (!did_cursorhold |
| 9151 | && has_cursorhold() |
| 9152 | && !Recording |
| 9153 | && typebuf.tb_len == 0 |
Bram Moolenaar | d29a9ee | 2006-09-14 09:07:34 +0000 | [diff] [blame] | 9154 | #ifdef FEAT_INS_EXPAND |
| 9155 | && !ins_compl_active() |
| 9156 | #endif |
| 9157 | ) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9158 | { |
| 9159 | state = get_real_state(); |
| 9160 | if (state == NORMAL_BUSY || (state & INSERT) != 0) |
| 9161 | return TRUE; |
| 9162 | } |
| 9163 | return FALSE; |
Bram Moolenaar | d35f971 | 2005-12-18 22:02:33 +0000 | [diff] [blame] | 9164 | } |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9165 | |
| 9166 | /* |
| 9167 | * Return TRUE when there is a CursorMoved autocommand defined. |
| 9168 | */ |
| 9169 | int |
| 9170 | has_cursormoved() |
| 9171 | { |
| 9172 | return (first_autopat[(int)EVENT_CURSORMOVED] != NULL); |
| 9173 | } |
| 9174 | |
| 9175 | /* |
| 9176 | * Return TRUE when there is a CursorMovedI autocommand defined. |
| 9177 | */ |
| 9178 | int |
| 9179 | has_cursormovedI() |
| 9180 | { |
| 9181 | return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL); |
| 9182 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9183 | |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 9184 | /* |
Bram Moolenaar | 186628f | 2013-03-19 13:33:23 +0100 | [diff] [blame] | 9185 | * Return TRUE when there is a TextChanged autocommand defined. |
| 9186 | */ |
| 9187 | int |
| 9188 | has_textchanged() |
| 9189 | { |
| 9190 | return (first_autopat[(int)EVENT_TEXTCHANGED] != NULL); |
| 9191 | } |
| 9192 | |
| 9193 | /* |
| 9194 | * Return TRUE when there is a TextChangedI autocommand defined. |
| 9195 | */ |
| 9196 | int |
| 9197 | has_textchangedI() |
| 9198 | { |
| 9199 | return (first_autopat[(int)EVENT_TEXTCHANGEDI] != NULL); |
| 9200 | } |
| 9201 | |
| 9202 | /* |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 9203 | * Return TRUE when there is an InsertCharPre autocommand defined. |
| 9204 | */ |
| 9205 | int |
| 9206 | has_insertcharpre() |
| 9207 | { |
| 9208 | return (first_autopat[(int)EVENT_INSERTCHARPRE] != NULL); |
| 9209 | } |
| 9210 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9211 | static int |
| 9212 | apply_autocmds_group(event, fname, fname_io, force, group, buf, eap) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9213 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9214 | char_u *fname; /* NULL or empty means use actual file name */ |
| 9215 | char_u *fname_io; /* fname to use for <afile> on cmdline, NULL means |
| 9216 | use fname */ |
| 9217 | int force; /* when TRUE, ignore autocmd_busy */ |
| 9218 | int group; /* group ID, or AUGROUP_ALL */ |
| 9219 | buf_T *buf; /* buffer for <abuf> */ |
| 9220 | exarg_T *eap; /* command arguments */ |
| 9221 | { |
| 9222 | char_u *sfname = NULL; /* short file name */ |
| 9223 | char_u *tail; |
| 9224 | int save_changed; |
| 9225 | buf_T *old_curbuf; |
| 9226 | int retval = FALSE; |
| 9227 | char_u *save_sourcing_name; |
| 9228 | linenr_T save_sourcing_lnum; |
| 9229 | char_u *save_autocmd_fname; |
Bram Moolenaar | f6dad43 | 2008-09-18 19:29:58 +0000 | [diff] [blame] | 9230 | int save_autocmd_fname_full; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9231 | int save_autocmd_bufnr; |
| 9232 | char_u *save_autocmd_match; |
| 9233 | int save_autocmd_busy; |
| 9234 | int save_autocmd_nested; |
| 9235 | static int nesting = 0; |
| 9236 | AutoPatCmd patcmd; |
| 9237 | AutoPat *ap; |
| 9238 | #ifdef FEAT_EVAL |
| 9239 | scid_T save_current_SID; |
| 9240 | void *save_funccalp; |
| 9241 | char_u *save_cmdarg; |
| 9242 | long save_cmdbang; |
| 9243 | #endif |
| 9244 | static int filechangeshell_busy = FALSE; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 9245 | #ifdef FEAT_PROFILE |
| 9246 | proftime_T wait_time; |
| 9247 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9248 | |
| 9249 | /* |
| 9250 | * Quickly return if there are no autocommands for this event or |
| 9251 | * autocommands are blocked. |
| 9252 | */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 9253 | if (first_autopat[(int)event] == NULL || autocmd_blocked > 0) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9254 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9255 | |
| 9256 | /* |
| 9257 | * When autocommands are busy, new autocommands are only executed when |
| 9258 | * explicitly enabled with the "nested" flag. |
| 9259 | */ |
| 9260 | if (autocmd_busy && !(force || autocmd_nested)) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9261 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9262 | |
| 9263 | #ifdef FEAT_EVAL |
| 9264 | /* |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 9265 | * Quickly return when immediately aborting on error, or when an interrupt |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9266 | * occurred or an exception was thrown but not caught. |
| 9267 | */ |
| 9268 | if (aborting()) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9269 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9270 | #endif |
| 9271 | |
| 9272 | /* |
| 9273 | * FileChangedShell never nests, because it can create an endless loop. |
| 9274 | */ |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 9275 | if (filechangeshell_busy && (event == EVENT_FILECHANGEDSHELL |
| 9276 | || event == EVENT_FILECHANGEDSHELLPOST)) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9277 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9278 | |
| 9279 | /* |
| 9280 | * Ignore events in 'eventignore'. |
| 9281 | */ |
| 9282 | if (event_ignored(event)) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9283 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9284 | |
| 9285 | /* |
| 9286 | * Allow nesting of autocommands, but restrict the depth, because it's |
| 9287 | * possible to create an endless loop. |
| 9288 | */ |
| 9289 | if (nesting == 10) |
| 9290 | { |
| 9291 | EMSG(_("E218: autocommand nesting too deep")); |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9292 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9293 | } |
| 9294 | |
| 9295 | /* |
| 9296 | * Check if these autocommands are disabled. Used when doing ":all" or |
| 9297 | * ":ball". |
| 9298 | */ |
| 9299 | if ( (autocmd_no_enter |
| 9300 | && (event == EVENT_WINENTER || event == EVENT_BUFENTER)) |
| 9301 | || (autocmd_no_leave |
| 9302 | && (event == EVENT_WINLEAVE || event == EVENT_BUFLEAVE))) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9303 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9304 | |
| 9305 | /* |
| 9306 | * Save the autocmd_* variables and info about the current buffer. |
| 9307 | */ |
| 9308 | save_autocmd_fname = autocmd_fname; |
Bram Moolenaar | f6dad43 | 2008-09-18 19:29:58 +0000 | [diff] [blame] | 9309 | save_autocmd_fname_full = autocmd_fname_full; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9310 | save_autocmd_bufnr = autocmd_bufnr; |
| 9311 | save_autocmd_match = autocmd_match; |
| 9312 | save_autocmd_busy = autocmd_busy; |
| 9313 | save_autocmd_nested = autocmd_nested; |
| 9314 | save_changed = curbuf->b_changed; |
| 9315 | old_curbuf = curbuf; |
| 9316 | |
| 9317 | /* |
| 9318 | * Set the file name to be used for <afile>. |
Bram Moolenaar | a0174af | 2008-01-02 20:08:25 +0000 | [diff] [blame] | 9319 | * Make a copy to avoid that changing a buffer name or directory makes it |
| 9320 | * invalid. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9321 | */ |
| 9322 | if (fname_io == NULL) |
| 9323 | { |
| 9324 | if (fname != NULL && *fname != NUL) |
| 9325 | autocmd_fname = fname; |
| 9326 | else if (buf != NULL) |
Bram Moolenaar | f6dad43 | 2008-09-18 19:29:58 +0000 | [diff] [blame] | 9327 | autocmd_fname = buf->b_ffname; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9328 | else |
| 9329 | autocmd_fname = NULL; |
| 9330 | } |
| 9331 | else |
| 9332 | autocmd_fname = fname_io; |
Bram Moolenaar | a0174af | 2008-01-02 20:08:25 +0000 | [diff] [blame] | 9333 | if (autocmd_fname != NULL) |
Bram Moolenaar | f6dad43 | 2008-09-18 19:29:58 +0000 | [diff] [blame] | 9334 | autocmd_fname = vim_strsave(autocmd_fname); |
| 9335 | autocmd_fname_full = FALSE; /* call FullName_save() later */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9336 | |
| 9337 | /* |
| 9338 | * Set the buffer number to be used for <abuf>. |
| 9339 | */ |
| 9340 | if (buf == NULL) |
| 9341 | autocmd_bufnr = 0; |
| 9342 | else |
| 9343 | autocmd_bufnr = buf->b_fnum; |
| 9344 | |
| 9345 | /* |
| 9346 | * When the file name is NULL or empty, use the file name of buffer "buf". |
| 9347 | * Always use the full path of the file name to match with, in case |
| 9348 | * "allow_dirs" is set. |
| 9349 | */ |
| 9350 | if (fname == NULL || *fname == NUL) |
| 9351 | { |
| 9352 | if (buf == NULL) |
| 9353 | fname = NULL; |
| 9354 | else |
| 9355 | { |
| 9356 | #ifdef FEAT_SYN_HL |
| 9357 | if (event == EVENT_SYNTAX) |
| 9358 | fname = buf->b_p_syn; |
| 9359 | else |
| 9360 | #endif |
| 9361 | if (event == EVENT_FILETYPE) |
| 9362 | fname = buf->b_p_ft; |
| 9363 | else |
| 9364 | { |
| 9365 | if (buf->b_sfname != NULL) |
| 9366 | sfname = vim_strsave(buf->b_sfname); |
| 9367 | fname = buf->b_ffname; |
| 9368 | } |
| 9369 | } |
| 9370 | if (fname == NULL) |
| 9371 | fname = (char_u *)""; |
| 9372 | fname = vim_strsave(fname); /* make a copy, so we can change it */ |
| 9373 | } |
| 9374 | else |
| 9375 | { |
| 9376 | sfname = vim_strsave(fname); |
Bram Moolenaar | 5135d46 | 2009-04-29 16:03:38 +0000 | [diff] [blame] | 9377 | /* Don't try expanding FileType, Syntax, FuncUndefined, WindowID or |
| 9378 | * QuickFixCmd* */ |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 9379 | if (event == EVENT_FILETYPE |
| 9380 | || event == EVENT_SYNTAX |
Bram Moolenaar | 5135d46 | 2009-04-29 16:03:38 +0000 | [diff] [blame] | 9381 | || event == EVENT_FUNCUNDEFINED |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 9382 | || event == EVENT_REMOTEREPLY |
Bram Moolenaar | b8a7b56 | 2006-02-01 21:47:16 +0000 | [diff] [blame] | 9383 | || event == EVENT_SPELLFILEMISSING |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 9384 | || event == EVENT_QUICKFIXCMDPRE |
| 9385 | || event == EVENT_QUICKFIXCMDPOST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9386 | fname = vim_strsave(fname); |
| 9387 | else |
| 9388 | fname = FullName_save(fname, FALSE); |
| 9389 | } |
| 9390 | if (fname == NULL) /* out of memory */ |
| 9391 | { |
| 9392 | vim_free(sfname); |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9393 | retval = FALSE; |
| 9394 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9395 | } |
| 9396 | |
| 9397 | #ifdef BACKSLASH_IN_FILENAME |
| 9398 | /* |
| 9399 | * Replace all backslashes with forward slashes. This makes the |
| 9400 | * autocommand patterns portable between Unix and MS-DOS. |
| 9401 | */ |
| 9402 | if (sfname != NULL) |
| 9403 | forward_slash(sfname); |
| 9404 | forward_slash(fname); |
| 9405 | #endif |
| 9406 | |
| 9407 | #ifdef VMS |
| 9408 | /* remove version for correct match */ |
| 9409 | if (sfname != NULL) |
| 9410 | vms_remove_version(sfname); |
| 9411 | vms_remove_version(fname); |
| 9412 | #endif |
| 9413 | |
| 9414 | /* |
| 9415 | * Set the name to be used for <amatch>. |
| 9416 | */ |
| 9417 | autocmd_match = fname; |
| 9418 | |
| 9419 | |
| 9420 | /* Don't redraw while doing auto commands. */ |
| 9421 | ++RedrawingDisabled; |
| 9422 | save_sourcing_name = sourcing_name; |
| 9423 | sourcing_name = NULL; /* don't free this one */ |
| 9424 | save_sourcing_lnum = sourcing_lnum; |
| 9425 | sourcing_lnum = 0; /* no line number here */ |
| 9426 | |
| 9427 | #ifdef FEAT_EVAL |
| 9428 | save_current_SID = current_SID; |
| 9429 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 9430 | # ifdef FEAT_PROFILE |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 9431 | if (do_profiling == PROF_YES) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 9432 | prof_child_enter(&wait_time); /* doesn't count for the caller itself */ |
| 9433 | # endif |
| 9434 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9435 | /* Don't use local function variables, if called from a function */ |
| 9436 | save_funccalp = save_funccal(); |
| 9437 | #endif |
| 9438 | |
| 9439 | /* |
| 9440 | * When starting to execute autocommands, save the search patterns. |
| 9441 | */ |
| 9442 | if (!autocmd_busy) |
| 9443 | { |
| 9444 | save_search_patterns(); |
| 9445 | saveRedobuff(); |
| 9446 | did_filetype = keep_filetype; |
| 9447 | } |
| 9448 | |
| 9449 | /* |
| 9450 | * Note that we are applying autocmds. Some commands need to know. |
| 9451 | */ |
| 9452 | autocmd_busy = TRUE; |
| 9453 | filechangeshell_busy = (event == EVENT_FILECHANGEDSHELL); |
| 9454 | ++nesting; /* see matching decrement below */ |
| 9455 | |
| 9456 | /* Remember that FileType was triggered. Used for did_filetype(). */ |
| 9457 | if (event == EVENT_FILETYPE) |
| 9458 | did_filetype = TRUE; |
| 9459 | |
| 9460 | tail = gettail(fname); |
| 9461 | |
| 9462 | /* Find first autocommand that matches */ |
| 9463 | patcmd.curpat = first_autopat[(int)event]; |
| 9464 | patcmd.nextcmd = NULL; |
| 9465 | patcmd.group = group; |
| 9466 | patcmd.fname = fname; |
| 9467 | patcmd.sfname = sfname; |
| 9468 | patcmd.tail = tail; |
| 9469 | patcmd.event = event; |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9470 | patcmd.arg_bufnr = autocmd_bufnr; |
| 9471 | patcmd.next = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9472 | auto_next_pat(&patcmd, FALSE); |
| 9473 | |
| 9474 | /* found one, start executing the autocommands */ |
| 9475 | if (patcmd.curpat != NULL) |
| 9476 | { |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9477 | /* add to active_apc_list */ |
| 9478 | patcmd.next = active_apc_list; |
| 9479 | active_apc_list = &patcmd; |
| 9480 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9481 | #ifdef FEAT_EVAL |
| 9482 | /* set v:cmdarg (only when there is a matching pattern) */ |
| 9483 | save_cmdbang = get_vim_var_nr(VV_CMDBANG); |
| 9484 | if (eap != NULL) |
| 9485 | { |
| 9486 | save_cmdarg = set_cmdarg(eap, NULL); |
| 9487 | set_vim_var_nr(VV_CMDBANG, (long)eap->forceit); |
| 9488 | } |
| 9489 | else |
| 9490 | save_cmdarg = NULL; /* avoid gcc warning */ |
| 9491 | #endif |
| 9492 | retval = TRUE; |
| 9493 | /* mark the last pattern, to avoid an endless loop when more patterns |
| 9494 | * are added when executing autocommands */ |
| 9495 | for (ap = patcmd.curpat; ap->next != NULL; ap = ap->next) |
| 9496 | ap->last = FALSE; |
| 9497 | ap->last = TRUE; |
| 9498 | check_lnums(TRUE); /* make sure cursor and topline are valid */ |
| 9499 | do_cmdline(NULL, getnextac, (void *)&patcmd, |
| 9500 | DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT); |
| 9501 | #ifdef FEAT_EVAL |
| 9502 | if (eap != NULL) |
| 9503 | { |
| 9504 | (void)set_cmdarg(NULL, save_cmdarg); |
| 9505 | set_vim_var_nr(VV_CMDBANG, save_cmdbang); |
| 9506 | } |
| 9507 | #endif |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9508 | /* delete from active_apc_list */ |
| 9509 | if (active_apc_list == &patcmd) /* just in case */ |
| 9510 | active_apc_list = patcmd.next; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9511 | } |
| 9512 | |
| 9513 | --RedrawingDisabled; |
| 9514 | autocmd_busy = save_autocmd_busy; |
| 9515 | filechangeshell_busy = FALSE; |
| 9516 | autocmd_nested = save_autocmd_nested; |
| 9517 | vim_free(sourcing_name); |
| 9518 | sourcing_name = save_sourcing_name; |
| 9519 | sourcing_lnum = save_sourcing_lnum; |
Bram Moolenaar | a0174af | 2008-01-02 20:08:25 +0000 | [diff] [blame] | 9520 | vim_free(autocmd_fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9521 | autocmd_fname = save_autocmd_fname; |
Bram Moolenaar | f6dad43 | 2008-09-18 19:29:58 +0000 | [diff] [blame] | 9522 | autocmd_fname_full = save_autocmd_fname_full; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9523 | autocmd_bufnr = save_autocmd_bufnr; |
| 9524 | autocmd_match = save_autocmd_match; |
| 9525 | #ifdef FEAT_EVAL |
| 9526 | current_SID = save_current_SID; |
| 9527 | restore_funccal(save_funccalp); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 9528 | # ifdef FEAT_PROFILE |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 9529 | if (do_profiling == PROF_YES) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 9530 | prof_child_exit(&wait_time); |
| 9531 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9532 | #endif |
| 9533 | vim_free(fname); |
| 9534 | vim_free(sfname); |
| 9535 | --nesting; /* see matching increment above */ |
| 9536 | |
| 9537 | /* |
| 9538 | * When stopping to execute autocommands, restore the search patterns and |
| 9539 | * the redo buffer. |
| 9540 | */ |
| 9541 | if (!autocmd_busy) |
| 9542 | { |
| 9543 | restore_search_patterns(); |
| 9544 | restoreRedobuff(); |
| 9545 | did_filetype = FALSE; |
| 9546 | } |
| 9547 | |
| 9548 | /* |
| 9549 | * Some events don't set or reset the Changed flag. |
| 9550 | * Check if still in the same buffer! |
| 9551 | */ |
| 9552 | if (curbuf == old_curbuf |
| 9553 | && (event == EVENT_BUFREADPOST |
| 9554 | || event == EVENT_BUFWRITEPOST |
| 9555 | || event == EVENT_FILEAPPENDPOST |
| 9556 | || event == EVENT_VIMLEAVE |
| 9557 | || event == EVENT_VIMLEAVEPRE)) |
| 9558 | { |
| 9559 | #ifdef FEAT_TITLE |
| 9560 | if (curbuf->b_changed != save_changed) |
| 9561 | need_maketitle = TRUE; |
| 9562 | #endif |
| 9563 | curbuf->b_changed = save_changed; |
| 9564 | } |
| 9565 | |
| 9566 | au_cleanup(); /* may really delete removed patterns/commands now */ |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9567 | |
| 9568 | BYPASS_AU: |
| 9569 | /* When wiping out a buffer make sure all its buffer-local autocommands |
| 9570 | * are deleted. */ |
| 9571 | if (event == EVENT_BUFWIPEOUT && buf != NULL) |
| 9572 | aubuflocal_remove(buf); |
| 9573 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9574 | return retval; |
| 9575 | } |
| 9576 | |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 9577 | # ifdef FEAT_EVAL |
| 9578 | static char_u *old_termresponse = NULL; |
| 9579 | # endif |
| 9580 | |
| 9581 | /* |
| 9582 | * Block triggering autocommands until unblock_autocmd() is called. |
| 9583 | * Can be used recursively, so long as it's symmetric. |
| 9584 | */ |
| 9585 | void |
| 9586 | block_autocmds() |
| 9587 | { |
| 9588 | # ifdef FEAT_EVAL |
| 9589 | /* Remember the value of v:termresponse. */ |
| 9590 | if (autocmd_blocked == 0) |
| 9591 | old_termresponse = get_vim_var_str(VV_TERMRESPONSE); |
| 9592 | # endif |
| 9593 | ++autocmd_blocked; |
| 9594 | } |
| 9595 | |
| 9596 | void |
| 9597 | unblock_autocmds() |
| 9598 | { |
| 9599 | --autocmd_blocked; |
| 9600 | |
| 9601 | # ifdef FEAT_EVAL |
| 9602 | /* When v:termresponse was set while autocommands were blocked, trigger |
| 9603 | * the autocommands now. Esp. useful when executing a shell command |
| 9604 | * during startup (vimdiff). */ |
| 9605 | if (autocmd_blocked == 0 |
| 9606 | && get_vim_var_str(VV_TERMRESPONSE) != old_termresponse) |
| 9607 | apply_autocmds(EVENT_TERMRESPONSE, NULL, NULL, FALSE, curbuf); |
| 9608 | # endif |
| 9609 | } |
| 9610 | |
Bram Moolenaar | abab85a | 2013-06-26 19:18:05 +0200 | [diff] [blame] | 9611 | int |
| 9612 | is_autocmd_blocked() |
| 9613 | { |
| 9614 | return autocmd_blocked != 0; |
| 9615 | } |
| 9616 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9617 | /* |
| 9618 | * Find next autocommand pattern that matches. |
| 9619 | */ |
| 9620 | static void |
| 9621 | auto_next_pat(apc, stop_at_last) |
| 9622 | AutoPatCmd *apc; |
| 9623 | int stop_at_last; /* stop when 'last' flag is set */ |
| 9624 | { |
| 9625 | AutoPat *ap; |
| 9626 | AutoCmd *cp; |
| 9627 | char_u *name; |
| 9628 | char *s; |
| 9629 | |
| 9630 | vim_free(sourcing_name); |
| 9631 | sourcing_name = NULL; |
| 9632 | |
| 9633 | for (ap = apc->curpat; ap != NULL && !got_int; ap = ap->next) |
| 9634 | { |
| 9635 | apc->curpat = NULL; |
| 9636 | |
Bram Moolenaar | f6dad43 | 2008-09-18 19:29:58 +0000 | [diff] [blame] | 9637 | /* 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] | 9638 | * the group matches. For buffer-local autocommands only check the |
| 9639 | * buffer number. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9640 | if (ap->pat != NULL && ap->cmds != NULL |
| 9641 | && (apc->group == AUGROUP_ALL || apc->group == ap->group)) |
| 9642 | { |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9643 | /* execution-condition */ |
| 9644 | if (ap->buflocal_nr == 0 |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 9645 | ? (match_file_pat(NULL, ap->reg_prog, apc->fname, |
| 9646 | apc->sfname, apc->tail, ap->allow_dirs)) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9647 | : ap->buflocal_nr == apc->arg_bufnr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9648 | { |
| 9649 | name = event_nr2name(apc->event); |
| 9650 | s = _("%s Auto commands for \"%s\""); |
| 9651 | sourcing_name = alloc((unsigned)(STRLEN(s) |
| 9652 | + STRLEN(name) + ap->patlen + 1)); |
| 9653 | if (sourcing_name != NULL) |
| 9654 | { |
| 9655 | sprintf((char *)sourcing_name, s, |
| 9656 | (char *)name, (char *)ap->pat); |
| 9657 | if (p_verbose >= 8) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 9658 | { |
| 9659 | verbose_enter(); |
Bram Moolenaar | 051b782 | 2005-05-19 21:00:46 +0000 | [diff] [blame] | 9660 | smsg((char_u *)_("Executing %s"), sourcing_name); |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 9661 | verbose_leave(); |
| 9662 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9663 | } |
| 9664 | |
| 9665 | apc->curpat = ap; |
| 9666 | apc->nextcmd = ap->cmds; |
| 9667 | /* mark last command */ |
| 9668 | for (cp = ap->cmds; cp->next != NULL; cp = cp->next) |
| 9669 | cp->last = FALSE; |
| 9670 | cp->last = TRUE; |
| 9671 | } |
| 9672 | line_breakcheck(); |
| 9673 | if (apc->curpat != NULL) /* found a match */ |
| 9674 | break; |
| 9675 | } |
| 9676 | if (stop_at_last && ap->last) |
| 9677 | break; |
| 9678 | } |
| 9679 | } |
| 9680 | |
| 9681 | /* |
| 9682 | * Get next autocommand command. |
| 9683 | * Called by do_cmdline() to get the next line for ":if". |
| 9684 | * Returns allocated string, or NULL for end of autocommands. |
| 9685 | */ |
Bram Moolenaar | 21691f8 | 2012-12-05 19:13:18 +0100 | [diff] [blame] | 9686 | char_u * |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9687 | getnextac(c, cookie, indent) |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 9688 | int c UNUSED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9689 | void *cookie; |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 9690 | int indent UNUSED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9691 | { |
| 9692 | AutoPatCmd *acp = (AutoPatCmd *)cookie; |
| 9693 | char_u *retval; |
| 9694 | AutoCmd *ac; |
| 9695 | |
| 9696 | /* Can be called again after returning the last line. */ |
| 9697 | if (acp->curpat == NULL) |
| 9698 | return NULL; |
| 9699 | |
| 9700 | /* repeat until we find an autocommand to execute */ |
| 9701 | for (;;) |
| 9702 | { |
| 9703 | /* skip removed commands */ |
| 9704 | while (acp->nextcmd != NULL && acp->nextcmd->cmd == NULL) |
| 9705 | if (acp->nextcmd->last) |
| 9706 | acp->nextcmd = NULL; |
| 9707 | else |
| 9708 | acp->nextcmd = acp->nextcmd->next; |
| 9709 | |
| 9710 | if (acp->nextcmd != NULL) |
| 9711 | break; |
| 9712 | |
| 9713 | /* at end of commands, find next pattern that matches */ |
| 9714 | if (acp->curpat->last) |
| 9715 | acp->curpat = NULL; |
| 9716 | else |
| 9717 | acp->curpat = acp->curpat->next; |
| 9718 | if (acp->curpat != NULL) |
| 9719 | auto_next_pat(acp, TRUE); |
| 9720 | if (acp->curpat == NULL) |
| 9721 | return NULL; |
| 9722 | } |
| 9723 | |
| 9724 | ac = acp->nextcmd; |
| 9725 | |
| 9726 | if (p_verbose >= 9) |
| 9727 | { |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 9728 | verbose_enter_scroll(); |
Bram Moolenaar | 051b782 | 2005-05-19 21:00:46 +0000 | [diff] [blame] | 9729 | smsg((char_u *)_("autocommand %s"), ac->cmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9730 | msg_puts((char_u *)"\n"); /* don't overwrite this either */ |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 9731 | verbose_leave_scroll(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9732 | } |
| 9733 | retval = vim_strsave(ac->cmd); |
| 9734 | autocmd_nested = ac->nested; |
| 9735 | #ifdef FEAT_EVAL |
| 9736 | current_SID = ac->scriptID; |
| 9737 | #endif |
| 9738 | if (ac->last) |
| 9739 | acp->nextcmd = NULL; |
| 9740 | else |
| 9741 | acp->nextcmd = ac->next; |
| 9742 | return retval; |
| 9743 | } |
| 9744 | |
| 9745 | /* |
| 9746 | * Return TRUE if there is a matching autocommand for "fname". |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9747 | * To account for buffer-local autocommands, function needs to know |
| 9748 | * in which buffer the file will be opened. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9749 | */ |
| 9750 | int |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9751 | has_autocmd(event, sfname, buf) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9752 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9753 | char_u *sfname; |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9754 | buf_T *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9755 | { |
| 9756 | AutoPat *ap; |
| 9757 | char_u *fname; |
| 9758 | char_u *tail = gettail(sfname); |
| 9759 | int retval = FALSE; |
| 9760 | |
| 9761 | fname = FullName_save(sfname, FALSE); |
| 9762 | if (fname == NULL) |
| 9763 | return FALSE; |
| 9764 | |
| 9765 | #ifdef BACKSLASH_IN_FILENAME |
| 9766 | /* |
| 9767 | * Replace all backslashes with forward slashes. This makes the |
| 9768 | * autocommand patterns portable between Unix and MS-DOS. |
| 9769 | */ |
| 9770 | sfname = vim_strsave(sfname); |
| 9771 | if (sfname != NULL) |
| 9772 | forward_slash(sfname); |
| 9773 | forward_slash(fname); |
| 9774 | #endif |
| 9775 | |
| 9776 | for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) |
| 9777 | if (ap->pat != NULL && ap->cmds != NULL |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 9778 | && (ap->buflocal_nr == 0 |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 9779 | ? match_file_pat(NULL, ap->reg_prog, |
| 9780 | fname, sfname, tail, ap->allow_dirs) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 9781 | : buf != NULL && ap->buflocal_nr == buf->b_fnum |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9782 | )) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9783 | { |
| 9784 | retval = TRUE; |
| 9785 | break; |
| 9786 | } |
| 9787 | |
| 9788 | vim_free(fname); |
| 9789 | #ifdef BACKSLASH_IN_FILENAME |
| 9790 | vim_free(sfname); |
| 9791 | #endif |
| 9792 | |
| 9793 | return retval; |
| 9794 | } |
| 9795 | |
| 9796 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 9797 | /* |
| 9798 | * Function given to ExpandGeneric() to obtain the list of autocommand group |
| 9799 | * names. |
| 9800 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9801 | char_u * |
| 9802 | get_augroup_name(xp, idx) |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 9803 | expand_T *xp UNUSED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9804 | int idx; |
| 9805 | { |
| 9806 | if (idx == augroups.ga_len) /* add "END" add the end */ |
| 9807 | return (char_u *)"END"; |
| 9808 | if (idx >= augroups.ga_len) /* end of list */ |
| 9809 | return NULL; |
| 9810 | if (AUGROUP_NAME(idx) == NULL) /* skip deleted entries */ |
| 9811 | return (char_u *)""; |
| 9812 | return AUGROUP_NAME(idx); /* return a name */ |
| 9813 | } |
| 9814 | |
| 9815 | static int include_groups = FALSE; |
| 9816 | |
| 9817 | char_u * |
| 9818 | set_context_in_autocmd(xp, arg, doautocmd) |
| 9819 | expand_T *xp; |
| 9820 | char_u *arg; |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 9821 | int doautocmd; /* TRUE for :doauto*, FALSE for :autocmd */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9822 | { |
| 9823 | char_u *p; |
| 9824 | int group; |
| 9825 | |
| 9826 | /* check for a group name, skip it if present */ |
| 9827 | include_groups = FALSE; |
| 9828 | p = arg; |
| 9829 | group = au_get_grouparg(&arg); |
| 9830 | if (group == AUGROUP_ERROR) |
| 9831 | return NULL; |
| 9832 | /* If there only is a group name that's what we expand. */ |
| 9833 | if (*arg == NUL && group != AUGROUP_ALL && !vim_iswhite(arg[-1])) |
| 9834 | { |
| 9835 | arg = p; |
| 9836 | group = AUGROUP_ALL; |
| 9837 | } |
| 9838 | |
| 9839 | /* skip over event name */ |
| 9840 | for (p = arg; *p != NUL && !vim_iswhite(*p); ++p) |
| 9841 | if (*p == ',') |
| 9842 | arg = p + 1; |
| 9843 | if (*p == NUL) |
| 9844 | { |
| 9845 | if (group == AUGROUP_ALL) |
| 9846 | include_groups = TRUE; |
| 9847 | xp->xp_context = EXPAND_EVENTS; /* expand event name */ |
| 9848 | xp->xp_pattern = arg; |
| 9849 | return NULL; |
| 9850 | } |
| 9851 | |
| 9852 | /* skip over pattern */ |
| 9853 | arg = skipwhite(p); |
| 9854 | while (*arg && (!vim_iswhite(*arg) || arg[-1] == '\\')) |
| 9855 | arg++; |
| 9856 | if (*arg) |
| 9857 | return arg; /* expand (next) command */ |
| 9858 | |
| 9859 | if (doautocmd) |
| 9860 | xp->xp_context = EXPAND_FILES; /* expand file names */ |
| 9861 | else |
| 9862 | xp->xp_context = EXPAND_NOTHING; /* pattern is not expanded */ |
| 9863 | return NULL; |
| 9864 | } |
| 9865 | |
| 9866 | /* |
| 9867 | * Function given to ExpandGeneric() to obtain the list of event names. |
| 9868 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9869 | char_u * |
| 9870 | get_event_name(xp, idx) |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 9871 | expand_T *xp UNUSED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9872 | int idx; |
| 9873 | { |
| 9874 | if (idx < augroups.ga_len) /* First list group names, if wanted */ |
| 9875 | { |
| 9876 | if (!include_groups || AUGROUP_NAME(idx) == NULL) |
| 9877 | return (char_u *)""; /* skip deleted entries */ |
| 9878 | return AUGROUP_NAME(idx); /* return a name */ |
| 9879 | } |
| 9880 | return (char_u *)event_names[idx - augroups.ga_len].name; |
| 9881 | } |
| 9882 | |
| 9883 | #endif /* FEAT_CMDL_COMPL */ |
| 9884 | |
| 9885 | /* |
Bram Moolenaar | f4cd3e8 | 2005-12-22 22:47:02 +0000 | [diff] [blame] | 9886 | * Return TRUE if autocmd is supported. |
| 9887 | */ |
| 9888 | int |
| 9889 | autocmd_supported(name) |
| 9890 | char_u *name; |
| 9891 | { |
| 9892 | char_u *p; |
| 9893 | |
| 9894 | return (event_name2nr(name, &p) != NUM_EVENTS); |
| 9895 | } |
| 9896 | |
| 9897 | /* |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9898 | * Return TRUE if an autocommand is defined for a group, event and |
| 9899 | * pattern: The group can be omitted to accept any group. "event" and "pattern" |
| 9900 | * can be NULL to accept any event and pattern. "pattern" can be NULL to accept |
| 9901 | * any pattern. Buffer-local patterns <buffer> or <buffer=N> are accepted. |
| 9902 | * Used for: |
| 9903 | * exists("#Group") or |
| 9904 | * exists("#Group#Event") or |
| 9905 | * exists("#Group#Event#pat") or |
| 9906 | * exists("#Event") or |
| 9907 | * exists("#Event#pat") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9908 | */ |
| 9909 | int |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9910 | au_exists(arg) |
| 9911 | char_u *arg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9912 | { |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9913 | char_u *arg_save; |
| 9914 | char_u *pattern = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9915 | char_u *event_name; |
| 9916 | char_u *p; |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9917 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9918 | AutoPat *ap; |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9919 | buf_T *buflocal_buf = NULL; |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9920 | int group; |
| 9921 | int retval = FALSE; |
| 9922 | |
Bram Moolenaar | f4cd3e8 | 2005-12-22 22:47:02 +0000 | [diff] [blame] | 9923 | /* 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] | 9924 | arg_save = vim_strsave(arg); |
| 9925 | if (arg_save == NULL) |
| 9926 | return FALSE; |
Bram Moolenaar | f4cd3e8 | 2005-12-22 22:47:02 +0000 | [diff] [blame] | 9927 | p = vim_strchr(arg_save, '#'); |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9928 | if (p != NULL) |
| 9929 | *p++ = NUL; |
| 9930 | |
| 9931 | /* First, look for an autocmd group name */ |
| 9932 | group = au_find_group(arg_save); |
| 9933 | if (group == AUGROUP_ERROR) |
| 9934 | { |
| 9935 | /* Didn't match a group name, assume the first argument is an event. */ |
| 9936 | group = AUGROUP_ALL; |
| 9937 | event_name = arg_save; |
| 9938 | } |
| 9939 | else |
| 9940 | { |
| 9941 | if (p == NULL) |
| 9942 | { |
| 9943 | /* "Group": group name is present and it's recognized */ |
| 9944 | retval = TRUE; |
| 9945 | goto theend; |
| 9946 | } |
| 9947 | |
| 9948 | /* Must be "Group#Event" or "Group#Event#pat". */ |
| 9949 | event_name = p; |
| 9950 | p = vim_strchr(event_name, '#'); |
| 9951 | if (p != NULL) |
| 9952 | *p++ = NUL; /* "Group#Event#pat" */ |
| 9953 | } |
| 9954 | |
| 9955 | pattern = p; /* "pattern" is NULL when there is no pattern */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9956 | |
| 9957 | /* find the index (enum) for the event name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9958 | event = event_name2nr(event_name, &p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9959 | |
| 9960 | /* return FALSE if the event name is not recognized */ |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9961 | if (event == NUM_EVENTS) |
| 9962 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9963 | |
| 9964 | /* Find the first autocommand for this event. |
| 9965 | * If there isn't any, return FALSE; |
| 9966 | * If there is one and no pattern given, return TRUE; */ |
| 9967 | ap = first_autopat[(int)event]; |
| 9968 | if (ap == NULL) |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9969 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9970 | |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9971 | /* if pattern is "<buffer>", special handling is needed which uses curbuf */ |
| 9972 | /* for pattern "<buffer=N>, fnamecmp() will work fine */ |
Bram Moolenaar | 5b7880d | 2009-09-11 15:24:31 +0000 | [diff] [blame] | 9973 | if (pattern != NULL && STRICMP(pattern, "<buffer>") == 0) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9974 | buflocal_buf = curbuf; |
| 9975 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9976 | /* Check if there is an autocommand with the given pattern. */ |
| 9977 | for ( ; ap != NULL; ap = ap->next) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9978 | /* only use a pattern when it has not been removed and has commands. */ |
| 9979 | /* For buffer-local autocommands, fnamecmp() works fine. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9980 | if (ap->pat != NULL && ap->cmds != NULL |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9981 | && (group == AUGROUP_ALL || ap->group == group) |
Bram Moolenaar | 5b7880d | 2009-09-11 15:24:31 +0000 | [diff] [blame] | 9982 | && (pattern == NULL |
| 9983 | || (buflocal_buf == NULL |
| 9984 | ? fnamecmp(ap->pat, pattern) == 0 |
| 9985 | : ap->buflocal_nr == buflocal_buf->b_fnum))) |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9986 | { |
| 9987 | retval = TRUE; |
| 9988 | break; |
| 9989 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9990 | |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9991 | theend: |
| 9992 | vim_free(arg_save); |
| 9993 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9994 | } |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9995 | |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 9996 | #else /* FEAT_AUTOCMD */ |
| 9997 | |
| 9998 | /* |
| 9999 | * Prepare for executing commands for (hidden) buffer "buf". |
| 10000 | * This is the non-autocommand version, it simply saves "curbuf" and sets |
| 10001 | * "curbuf" and "curwin" to match "buf". |
| 10002 | */ |
| 10003 | void |
| 10004 | aucmd_prepbuf(aco, buf) |
| 10005 | aco_save_T *aco; /* structure to save values in */ |
| 10006 | buf_T *buf; /* new curbuf */ |
| 10007 | { |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 10008 | aco->save_curbuf = curbuf; |
| 10009 | --curbuf->b_nwindows; |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 10010 | curbuf = buf; |
| 10011 | curwin->w_buffer = buf; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 10012 | ++curbuf->b_nwindows; |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 10013 | } |
| 10014 | |
| 10015 | /* |
| 10016 | * Restore after executing commands for a (hidden) buffer. |
| 10017 | * This is the non-autocommand version. |
| 10018 | */ |
| 10019 | void |
| 10020 | aucmd_restbuf(aco) |
| 10021 | aco_save_T *aco; /* structure holding saved values */ |
| 10022 | { |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 10023 | --curbuf->b_nwindows; |
| 10024 | curbuf = aco->save_curbuf; |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 10025 | curwin->w_buffer = curbuf; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 10026 | ++curbuf->b_nwindows; |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 10027 | } |
| 10028 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10029 | #endif /* FEAT_AUTOCMD */ |
| 10030 | |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 10031 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10032 | #if defined(FEAT_AUTOCMD) || defined(FEAT_WILDIGN) || defined(PROTO) |
| 10033 | /* |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 10034 | * Try matching a filename with a "pattern" ("prog" is NULL), or use the |
| 10035 | * precompiled regprog "prog" ("pattern" is NULL). That avoids calling |
| 10036 | * vim_regcomp() often. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10037 | * Used for autocommands and 'wildignore'. |
| 10038 | * Returns TRUE if there is a match, FALSE otherwise. |
| 10039 | */ |
| 10040 | int |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 10041 | match_file_pat(pattern, prog, fname, sfname, tail, allow_dirs) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10042 | char_u *pattern; /* pattern to match with */ |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 10043 | regprog_T *prog; /* pre-compiled regprog or NULL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10044 | char_u *fname; /* full path of file name */ |
| 10045 | char_u *sfname; /* short file name or NULL */ |
| 10046 | char_u *tail; /* tail of path */ |
| 10047 | int allow_dirs; /* allow matching with dir */ |
| 10048 | { |
| 10049 | regmatch_T regmatch; |
| 10050 | int result = FALSE; |
| 10051 | #ifdef FEAT_OSFILETYPE |
| 10052 | int no_pattern = FALSE; /* TRUE if check is filetype only */ |
| 10053 | char_u *type_start; |
| 10054 | char_u c; |
| 10055 | int match = FALSE; |
| 10056 | #endif |
| 10057 | |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 10058 | regmatch.rm_ic = p_fic; /* ignore case if 'fileignorecase' is set */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10059 | #ifdef FEAT_OSFILETYPE |
| 10060 | if (*pattern == '<') |
| 10061 | { |
| 10062 | /* There is a filetype condition specified with this pattern. |
| 10063 | * Check the filetype matches first. If not, don't bother with the |
| 10064 | * pattern (set regprog to NULL). |
| 10065 | * Always use magic for the regexp. |
| 10066 | */ |
| 10067 | |
| 10068 | for (type_start = pattern + 1; (c = *pattern); pattern++) |
| 10069 | { |
| 10070 | if ((c == ';' || c == '>') && match == FALSE) |
| 10071 | { |
| 10072 | *pattern = NUL; /* Terminate the string */ |
Bram Moolenaar | 0533443 | 2011-07-20 18:29:39 +0200 | [diff] [blame] | 10073 | /* TODO: match with 'filetype' of buffer that "fname" comes |
| 10074 | * from. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10075 | match = mch_check_filetype(fname, type_start); |
| 10076 | *pattern = c; /* Restore the terminator */ |
| 10077 | type_start = pattern + 1; |
| 10078 | } |
| 10079 | if (c == '>') |
| 10080 | break; |
| 10081 | } |
| 10082 | |
| 10083 | /* (c should never be NUL, but check anyway) */ |
| 10084 | if (match == FALSE || c == NUL) |
| 10085 | regmatch.regprog = NULL; /* Doesn't match - don't check pat. */ |
| 10086 | else if (*pattern == NUL) |
| 10087 | { |
| 10088 | regmatch.regprog = NULL; /* Vim will try to free regprog later */ |
| 10089 | no_pattern = TRUE; /* Always matches - don't check pat. */ |
| 10090 | } |
| 10091 | else |
| 10092 | regmatch.regprog = vim_regcomp(pattern + 1, RE_MAGIC); |
| 10093 | } |
| 10094 | else |
| 10095 | #endif |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 10096 | { |
| 10097 | if (prog != NULL) |
| 10098 | regmatch.regprog = prog; |
| 10099 | else |
| 10100 | regmatch.regprog = vim_regcomp(pattern, RE_MAGIC); |
| 10101 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10102 | |
| 10103 | /* |
| 10104 | * Try for a match with the pattern with: |
| 10105 | * 1. the full file name, when the pattern has a '/'. |
| 10106 | * 2. the short file name, when the pattern has a '/'. |
| 10107 | * 3. the tail of the file name, when the pattern has no '/'. |
| 10108 | */ |
| 10109 | if ( |
| 10110 | #ifdef FEAT_OSFILETYPE |
| 10111 | /* If the check is for a filetype only and we don't care |
| 10112 | * about the path then skip all the regexp stuff. |
| 10113 | */ |
| 10114 | no_pattern || |
| 10115 | #endif |
| 10116 | (regmatch.regprog != NULL |
| 10117 | && ((allow_dirs |
| 10118 | && (vim_regexec(®match, fname, (colnr_T)0) |
| 10119 | || (sfname != NULL |
| 10120 | && vim_regexec(®match, sfname, (colnr_T)0)))) |
| 10121 | || (!allow_dirs && vim_regexec(®match, tail, (colnr_T)0))))) |
| 10122 | result = TRUE; |
| 10123 | |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 10124 | if (prog == NULL) |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 10125 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10126 | return result; |
| 10127 | } |
| 10128 | #endif |
| 10129 | |
| 10130 | #if defined(FEAT_WILDIGN) || defined(PROTO) |
| 10131 | /* |
| 10132 | * Return TRUE if a file matches with a pattern in "list". |
| 10133 | * "list" is a comma-separated list of patterns, like 'wildignore'. |
| 10134 | * "sfname" is the short file name or NULL, "ffname" the long file name. |
| 10135 | */ |
| 10136 | int |
| 10137 | match_file_list(list, sfname, ffname) |
| 10138 | char_u *list; |
| 10139 | char_u *sfname; |
| 10140 | char_u *ffname; |
| 10141 | { |
| 10142 | char_u buf[100]; |
| 10143 | char_u *tail; |
| 10144 | char_u *regpat; |
| 10145 | char allow_dirs; |
| 10146 | int match; |
| 10147 | char_u *p; |
| 10148 | |
| 10149 | tail = gettail(sfname); |
| 10150 | |
| 10151 | /* try all patterns in 'wildignore' */ |
| 10152 | p = list; |
| 10153 | while (*p) |
| 10154 | { |
| 10155 | copy_option_part(&p, buf, 100, ","); |
| 10156 | regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE); |
| 10157 | if (regpat == NULL) |
| 10158 | break; |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 10159 | match = match_file_pat(regpat, NULL, ffname, sfname, |
| 10160 | tail, (int)allow_dirs); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10161 | vim_free(regpat); |
| 10162 | if (match) |
| 10163 | return TRUE; |
| 10164 | } |
| 10165 | return FALSE; |
| 10166 | } |
| 10167 | #endif |
| 10168 | |
| 10169 | /* |
| 10170 | * Convert the given pattern "pat" which has shell style wildcards in it, into |
| 10171 | * a regular expression, and return the result in allocated memory. If there |
| 10172 | * is a directory path separator to be matched, then TRUE is put in |
| 10173 | * allow_dirs, otherwise FALSE is put there -- webb. |
| 10174 | * Handle backslashes before special characters, like "\*" and "\ ". |
| 10175 | * |
| 10176 | * If FEAT_OSFILETYPE defined then pass initial <type> through unchanged. Eg: |
| 10177 | * '<html>myfile' becomes '<html>^myfile$' -- leonard. |
| 10178 | * |
| 10179 | * Returns NULL when out of memory. |
| 10180 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10181 | char_u * |
| 10182 | file_pat_to_reg_pat(pat, pat_end, allow_dirs, no_bslash) |
| 10183 | char_u *pat; |
| 10184 | char_u *pat_end; /* first char after pattern or NULL */ |
| 10185 | char *allow_dirs; /* Result passed back out in here */ |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 10186 | int no_bslash UNUSED; /* Don't use a backward slash as pathsep */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10187 | { |
| 10188 | int size; |
| 10189 | char_u *endp; |
| 10190 | char_u *reg_pat; |
| 10191 | char_u *p; |
| 10192 | int i; |
| 10193 | int nested = 0; |
| 10194 | int add_dollar = TRUE; |
| 10195 | #ifdef FEAT_OSFILETYPE |
| 10196 | int check_length = 0; |
| 10197 | #endif |
| 10198 | |
| 10199 | if (allow_dirs != NULL) |
| 10200 | *allow_dirs = FALSE; |
| 10201 | if (pat_end == NULL) |
| 10202 | pat_end = pat + STRLEN(pat); |
| 10203 | |
| 10204 | #ifdef FEAT_OSFILETYPE |
| 10205 | /* Find out how much of the string is the filetype check */ |
| 10206 | if (*pat == '<') |
| 10207 | { |
| 10208 | /* Count chars until the next '>' */ |
| 10209 | for (p = pat + 1; p < pat_end && *p != '>'; p++) |
| 10210 | ; |
| 10211 | if (p < pat_end) |
| 10212 | { |
| 10213 | /* Pattern is of the form <.*>.* */ |
| 10214 | check_length = p - pat + 1; |
| 10215 | if (p + 1 >= pat_end) |
| 10216 | { |
| 10217 | /* The 'pattern' is a filetype check ONLY */ |
| 10218 | reg_pat = (char_u *)alloc(check_length + 1); |
| 10219 | if (reg_pat != NULL) |
| 10220 | { |
| 10221 | mch_memmove(reg_pat, pat, (size_t)check_length); |
| 10222 | reg_pat[check_length] = NUL; |
| 10223 | } |
| 10224 | return reg_pat; |
| 10225 | } |
| 10226 | } |
| 10227 | /* else: there was no closing '>' - assume it was a normal pattern */ |
| 10228 | |
| 10229 | } |
| 10230 | pat += check_length; |
| 10231 | size = 2 + check_length; |
| 10232 | #else |
| 10233 | size = 2; /* '^' at start, '$' at end */ |
| 10234 | #endif |
| 10235 | |
| 10236 | for (p = pat; p < pat_end; p++) |
| 10237 | { |
| 10238 | switch (*p) |
| 10239 | { |
| 10240 | case '*': |
| 10241 | case '.': |
| 10242 | case ',': |
| 10243 | case '{': |
| 10244 | case '}': |
| 10245 | case '~': |
| 10246 | size += 2; /* extra backslash */ |
| 10247 | break; |
| 10248 | #ifdef BACKSLASH_IN_FILENAME |
| 10249 | case '\\': |
| 10250 | case '/': |
| 10251 | size += 4; /* could become "[\/]" */ |
| 10252 | break; |
| 10253 | #endif |
| 10254 | default: |
| 10255 | size++; |
| 10256 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10257 | if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10258 | { |
| 10259 | ++p; |
| 10260 | ++size; |
| 10261 | } |
| 10262 | # endif |
| 10263 | break; |
| 10264 | } |
| 10265 | } |
| 10266 | reg_pat = alloc(size + 1); |
| 10267 | if (reg_pat == NULL) |
| 10268 | return NULL; |
| 10269 | |
| 10270 | #ifdef FEAT_OSFILETYPE |
| 10271 | /* Copy the type check in to the start. */ |
| 10272 | if (check_length) |
| 10273 | mch_memmove(reg_pat, pat - check_length, (size_t)check_length); |
| 10274 | i = check_length; |
| 10275 | #else |
| 10276 | i = 0; |
| 10277 | #endif |
| 10278 | |
| 10279 | if (pat[0] == '*') |
| 10280 | while (pat[0] == '*' && pat < pat_end - 1) |
| 10281 | pat++; |
| 10282 | else |
| 10283 | reg_pat[i++] = '^'; |
| 10284 | endp = pat_end - 1; |
| 10285 | if (*endp == '*') |
| 10286 | { |
| 10287 | while (endp - pat > 0 && *endp == '*') |
| 10288 | endp--; |
| 10289 | add_dollar = FALSE; |
| 10290 | } |
| 10291 | for (p = pat; *p && nested >= 0 && p <= endp; p++) |
| 10292 | { |
| 10293 | switch (*p) |
| 10294 | { |
| 10295 | case '*': |
| 10296 | reg_pat[i++] = '.'; |
| 10297 | reg_pat[i++] = '*'; |
Bram Moolenaar | 0274363 | 2005-07-25 20:42:36 +0000 | [diff] [blame] | 10298 | while (p[1] == '*') /* "**" matches like "*" */ |
| 10299 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10300 | break; |
| 10301 | case '.': |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10302 | case '~': |
| 10303 | reg_pat[i++] = '\\'; |
| 10304 | reg_pat[i++] = *p; |
| 10305 | break; |
| 10306 | case '?': |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10307 | reg_pat[i++] = '.'; |
| 10308 | break; |
| 10309 | case '\\': |
| 10310 | if (p[1] == NUL) |
| 10311 | break; |
| 10312 | #ifdef BACKSLASH_IN_FILENAME |
| 10313 | if (!no_bslash) |
| 10314 | { |
| 10315 | /* translate: |
| 10316 | * "\x" to "\\x" e.g., "dir\file" |
| 10317 | * "\*" to "\\.*" e.g., "dir\*.c" |
| 10318 | * "\?" to "\\." e.g., "dir\??.c" |
| 10319 | * "\+" to "\+" e.g., "fileX\+.c" |
| 10320 | */ |
| 10321 | if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?') |
| 10322 | && p[1] != '+') |
| 10323 | { |
| 10324 | reg_pat[i++] = '['; |
| 10325 | reg_pat[i++] = '\\'; |
| 10326 | reg_pat[i++] = '/'; |
| 10327 | reg_pat[i++] = ']'; |
| 10328 | if (allow_dirs != NULL) |
| 10329 | *allow_dirs = TRUE; |
| 10330 | break; |
| 10331 | } |
| 10332 | } |
| 10333 | #endif |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 10334 | /* Undo escaping from ExpandEscape(): |
| 10335 | * foo\?bar -> foo?bar |
| 10336 | * foo\%bar -> foo%bar |
| 10337 | * foo\,bar -> foo,bar |
| 10338 | * foo\ bar -> foo bar |
| 10339 | * Don't unescape \, * and others that are also special in a |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10340 | * regexp. |
| 10341 | * An escaped { must be unescaped since we use magic not |
Bram Moolenaar | a946afe | 2013-08-02 15:22:39 +0200 | [diff] [blame] | 10342 | * verymagic. Use "\\\{n,m\}"" to get "\{n,m}". |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10343 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10344 | if (*++p == '?' |
| 10345 | #ifdef BACKSLASH_IN_FILENAME |
| 10346 | && no_bslash |
| 10347 | #endif |
| 10348 | ) |
| 10349 | reg_pat[i++] = '?'; |
| 10350 | else |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10351 | if (*p == ',' || *p == '%' || *p == '#' |
Bram Moolenaar | a946afe | 2013-08-02 15:22:39 +0200 | [diff] [blame] | 10352 | || *p == ' ' || *p == '{' || *p == '}') |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 10353 | reg_pat[i++] = *p; |
Bram Moolenaar | a946afe | 2013-08-02 15:22:39 +0200 | [diff] [blame] | 10354 | else if (*p == '\\' && p[1] == '\\' && p[2] == '{') |
| 10355 | { |
| 10356 | reg_pat[i++] = '\\'; |
| 10357 | reg_pat[i++] = '{'; |
| 10358 | p += 2; |
| 10359 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10360 | else |
| 10361 | { |
| 10362 | if (allow_dirs != NULL && vim_ispathsep(*p) |
| 10363 | #ifdef BACKSLASH_IN_FILENAME |
| 10364 | && (!no_bslash || *p != '\\') |
| 10365 | #endif |
| 10366 | ) |
| 10367 | *allow_dirs = TRUE; |
| 10368 | reg_pat[i++] = '\\'; |
| 10369 | reg_pat[i++] = *p; |
| 10370 | } |
| 10371 | break; |
| 10372 | #ifdef BACKSLASH_IN_FILENAME |
| 10373 | case '/': |
| 10374 | reg_pat[i++] = '['; |
| 10375 | reg_pat[i++] = '\\'; |
| 10376 | reg_pat[i++] = '/'; |
| 10377 | reg_pat[i++] = ']'; |
| 10378 | if (allow_dirs != NULL) |
| 10379 | *allow_dirs = TRUE; |
| 10380 | break; |
| 10381 | #endif |
| 10382 | case '{': |
| 10383 | reg_pat[i++] = '\\'; |
| 10384 | reg_pat[i++] = '('; |
| 10385 | nested++; |
| 10386 | break; |
| 10387 | case '}': |
| 10388 | reg_pat[i++] = '\\'; |
| 10389 | reg_pat[i++] = ')'; |
| 10390 | --nested; |
| 10391 | break; |
| 10392 | case ',': |
| 10393 | if (nested) |
| 10394 | { |
| 10395 | reg_pat[i++] = '\\'; |
| 10396 | reg_pat[i++] = '|'; |
| 10397 | } |
| 10398 | else |
| 10399 | reg_pat[i++] = ','; |
| 10400 | break; |
| 10401 | default: |
| 10402 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10403 | if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10404 | reg_pat[i++] = *p++; |
| 10405 | else |
| 10406 | # endif |
| 10407 | if (allow_dirs != NULL && vim_ispathsep(*p)) |
| 10408 | *allow_dirs = TRUE; |
| 10409 | reg_pat[i++] = *p; |
| 10410 | break; |
| 10411 | } |
| 10412 | } |
| 10413 | if (add_dollar) |
| 10414 | reg_pat[i++] = '$'; |
| 10415 | reg_pat[i] = NUL; |
| 10416 | if (nested != 0) |
| 10417 | { |
| 10418 | if (nested < 0) |
| 10419 | EMSG(_("E219: Missing {.")); |
| 10420 | else |
| 10421 | EMSG(_("E220: Missing }.")); |
| 10422 | vim_free(reg_pat); |
| 10423 | reg_pat = NULL; |
| 10424 | } |
| 10425 | return reg_pat; |
| 10426 | } |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 10427 | |
| 10428 | #if defined(EINTR) || defined(PROTO) |
| 10429 | /* |
| 10430 | * Version of read() that retries when interrupted by EINTR (possibly |
| 10431 | * by a SIGWINCH). |
| 10432 | */ |
| 10433 | long |
| 10434 | read_eintr(fd, buf, bufsize) |
| 10435 | int fd; |
| 10436 | void *buf; |
| 10437 | size_t bufsize; |
| 10438 | { |
| 10439 | long ret; |
| 10440 | |
| 10441 | for (;;) |
| 10442 | { |
| 10443 | ret = vim_read(fd, buf, bufsize); |
| 10444 | if (ret >= 0 || errno != EINTR) |
| 10445 | break; |
| 10446 | } |
| 10447 | return ret; |
| 10448 | } |
| 10449 | |
| 10450 | /* |
| 10451 | * Version of write() that retries when interrupted by EINTR (possibly |
| 10452 | * by a SIGWINCH). |
| 10453 | */ |
| 10454 | long |
| 10455 | write_eintr(fd, buf, bufsize) |
| 10456 | int fd; |
| 10457 | void *buf; |
| 10458 | size_t bufsize; |
| 10459 | { |
| 10460 | long ret = 0; |
| 10461 | long wlen; |
| 10462 | |
| 10463 | /* Repeat the write() so long it didn't fail, other than being interrupted |
| 10464 | * by a signal. */ |
| 10465 | while (ret < (long)bufsize) |
| 10466 | { |
Bram Moolenaar | 9c26303 | 2010-12-17 18:06:06 +0100 | [diff] [blame] | 10467 | wlen = vim_write(fd, (char *)buf + ret, bufsize - ret); |
Bram Moolenaar | 540fc6f | 2010-12-17 16:27:16 +0100 | [diff] [blame] | 10468 | if (wlen < 0) |
| 10469 | { |
| 10470 | if (errno != EINTR) |
| 10471 | break; |
| 10472 | } |
| 10473 | else |
| 10474 | ret += wlen; |
| 10475 | } |
| 10476 | return ret; |
| 10477 | } |
| 10478 | #endif |