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 | |
| 14 | #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64) |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 15 | # include "vimio.h" /* for lseek(), must be before vim.h */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 16 | #endif |
| 17 | |
| 18 | #if defined __EMX__ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 19 | # include "vimio.h" /* for mktemp(), CJW 1997-12-03 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 20 | #endif |
| 21 | |
| 22 | #include "vim.h" |
| 23 | |
Bram Moolenaar | f4888d0 | 2009-12-02 12:31:27 +0000 | [diff] [blame] | 24 | #if defined(__TANDEM) || defined(__MINT__) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 25 | # include <limits.h> /* for SSIZE_MAX */ |
| 26 | #endif |
| 27 | |
| 28 | #if defined(HAVE_UTIME) && defined(HAVE_UTIME_H) |
| 29 | # include <utime.h> /* for struct utimbuf */ |
| 30 | #endif |
| 31 | |
| 32 | #define BUFSIZE 8192 /* size of normal write buffer */ |
| 33 | #define SMBUFSIZE 256 /* size of emergency write buffer */ |
| 34 | |
| 35 | #ifdef FEAT_CRYPT |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 36 | /* crypt_magic[0] is pkzip crypt, crypt_magic[1] is sha2+blowfish */ |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 37 | static char *crypt_magic[] = {"VimCrypt~01!", "VimCrypt~02!"}; |
| 38 | static char crypt_magic_head[] = "VimCrypt~"; |
| 39 | # define CRYPT_MAGIC_LEN 12 /* must be multiple of 4! */ |
| 40 | static int crypt_seed_len[] = {0, 8}; |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 41 | #define CRYPT_SEED_LEN_MAX 8 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 42 | #endif |
| 43 | |
| 44 | /* Is there any system that doesn't have access()? */ |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 45 | #define USE_MCH_ACCESS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 46 | |
Bram Moolenaar | fe1c56d | 2007-07-10 15:10:54 +0000 | [diff] [blame] | 47 | #if defined(sun) && defined(S_ISCHR) |
| 48 | # define OPEN_CHR_FILES |
| 49 | static int is_dev_fd_file(char_u *fname); |
| 50 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 51 | #ifdef FEAT_MBYTE |
| 52 | static char_u *next_fenc __ARGS((char_u **pp)); |
| 53 | # ifdef FEAT_EVAL |
| 54 | static char_u *readfile_charconvert __ARGS((char_u *fname, char_u *fenc, int *fdp)); |
| 55 | # endif |
| 56 | #endif |
| 57 | #ifdef FEAT_VIMINFO |
| 58 | static void check_marks_read __ARGS((void)); |
| 59 | #endif |
| 60 | #ifdef FEAT_CRYPT |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 61 | static int get_crypt_method __ARGS((char *ptr, int len)); |
Bram Moolenaar | 914703b | 2010-05-31 21:59:46 +0200 | [diff] [blame] | 62 | static char_u *check_for_cryptkey __ARGS((char_u *cryptkey, char_u *ptr, long *sizep, off_t *filesizep, int newfile, int *did_ask)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 63 | #endif |
| 64 | #ifdef UNIX |
| 65 | static void set_file_time __ARGS((char_u *fname, time_t atime, time_t mtime)); |
| 66 | #endif |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 67 | static int set_rw_fname __ARGS((char_u *fname, char_u *sfname)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 68 | static int msg_add_fileformat __ARGS((int eol_type)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 69 | static void msg_add_eol __ARGS((void)); |
| 70 | static int check_mtime __ARGS((buf_T *buf, struct stat *s)); |
| 71 | static int time_differs __ARGS((long t1, long t2)); |
| 72 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 73 | 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] | 74 | static int au_find_group __ARGS((char_u *name)); |
| 75 | |
| 76 | # define AUGROUP_DEFAULT -1 /* default autocmd group */ |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 77 | # define AUGROUP_ERROR -2 /* erroneous autocmd group */ |
Bram Moolenaar | 70836c8 | 2006-02-20 21:28:49 +0000 | [diff] [blame] | 78 | # define AUGROUP_ALL -3 /* all autocmd groups */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 79 | #endif |
| 80 | |
| 81 | #if defined(FEAT_CRYPT) || defined(FEAT_MBYTE) |
| 82 | # define HAS_BW_FLAGS |
| 83 | # define FIO_LATIN1 0x01 /* convert Latin1 */ |
| 84 | # define FIO_UTF8 0x02 /* convert UTF-8 */ |
| 85 | # define FIO_UCS2 0x04 /* convert UCS-2 */ |
| 86 | # define FIO_UCS4 0x08 /* convert UCS-4 */ |
| 87 | # define FIO_UTF16 0x10 /* convert UTF-16 */ |
| 88 | # ifdef WIN3264 |
| 89 | # define FIO_CODEPAGE 0x20 /* convert MS-Windows codepage */ |
| 90 | # define FIO_PUT_CP(x) (((x) & 0xffff) << 16) /* put codepage in top word */ |
| 91 | # define FIO_GET_CP(x) (((x)>>16) & 0xffff) /* get codepage from top word */ |
| 92 | # endif |
| 93 | # ifdef MACOS_X |
| 94 | # define FIO_MACROMAN 0x20 /* convert MacRoman */ |
| 95 | # endif |
| 96 | # define FIO_ENDIAN_L 0x80 /* little endian */ |
| 97 | # define FIO_ENCRYPTED 0x1000 /* encrypt written bytes */ |
| 98 | # define FIO_NOCONVERT 0x2000 /* skip encoding conversion */ |
| 99 | # define FIO_UCSBOM 0x4000 /* check for BOM at start of file */ |
| 100 | # define FIO_ALL -1 /* allow all formats */ |
| 101 | #endif |
| 102 | |
| 103 | /* When converting, a read() or write() may leave some bytes to be converted |
| 104 | * for the next call. The value is guessed... */ |
| 105 | #define CONV_RESTLEN 30 |
| 106 | |
| 107 | /* We have to guess how much a sequence of bytes may expand when converting |
| 108 | * with iconv() to be able to allocate a buffer. */ |
| 109 | #define ICONV_MULT 8 |
| 110 | |
| 111 | /* |
| 112 | * Structure to pass arguments from buf_write() to buf_write_bytes(). |
| 113 | */ |
| 114 | struct bw_info |
| 115 | { |
| 116 | int bw_fd; /* file descriptor */ |
| 117 | char_u *bw_buf; /* buffer with data to be written */ |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 118 | int bw_len; /* length of data */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 119 | #ifdef HAS_BW_FLAGS |
| 120 | int bw_flags; /* FIO_ flags */ |
| 121 | #endif |
| 122 | #ifdef FEAT_MBYTE |
| 123 | char_u bw_rest[CONV_RESTLEN]; /* not converted bytes */ |
| 124 | int bw_restlen; /* nr of bytes in bw_rest[] */ |
| 125 | int bw_first; /* first write call */ |
| 126 | char_u *bw_conv_buf; /* buffer for writing converted chars */ |
| 127 | int bw_conv_buflen; /* size of bw_conv_buf */ |
| 128 | int bw_conv_error; /* set for conversion error */ |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 129 | linenr_T bw_conv_error_lnum; /* first line with error or zero */ |
| 130 | linenr_T bw_start_lnum; /* line number at start of buffer */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 131 | # ifdef USE_ICONV |
| 132 | iconv_t bw_iconv_fd; /* descriptor for iconv() or -1 */ |
| 133 | # endif |
| 134 | #endif |
| 135 | }; |
| 136 | |
| 137 | static int buf_write_bytes __ARGS((struct bw_info *ip)); |
| 138 | |
| 139 | #ifdef FEAT_MBYTE |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 140 | 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] | 141 | static int ucs2bytes __ARGS((unsigned c, char_u **pp, int flags)); |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 142 | static int need_conversion __ARGS((char_u *fenc)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 143 | static int get_fio_flags __ARGS((char_u *ptr)); |
| 144 | static char_u *check_for_bom __ARGS((char_u *p, long size, int *lenp, int flags)); |
| 145 | static int make_bom __ARGS((char_u *buf, char_u *name)); |
| 146 | # ifdef WIN3264 |
| 147 | static int get_win_fio_flags __ARGS((char_u *ptr)); |
| 148 | # endif |
| 149 | # ifdef MACOS_X |
| 150 | static int get_mac_fio_flags __ARGS((char_u *ptr)); |
| 151 | # endif |
| 152 | #endif |
| 153 | static int move_lines __ARGS((buf_T *frombuf, buf_T *tobuf)); |
Bram Moolenaar | 4592dee | 2009-11-18 19:11:58 +0000 | [diff] [blame] | 154 | #ifdef TEMPDIRNAMES |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 155 | static void vim_settempdir __ARGS((char_u *tempdir)); |
Bram Moolenaar | 4592dee | 2009-11-18 19:11:58 +0000 | [diff] [blame] | 156 | #endif |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 157 | #ifdef FEAT_AUTOCMD |
| 158 | static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name"); |
| 159 | #endif |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 160 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 161 | void |
| 162 | filemess(buf, name, s, attr) |
| 163 | buf_T *buf; |
| 164 | char_u *name; |
| 165 | char_u *s; |
| 166 | int attr; |
| 167 | { |
| 168 | int msg_scroll_save; |
| 169 | |
| 170 | if (msg_silent != 0) |
| 171 | return; |
| 172 | msg_add_fname(buf, name); /* put file name in IObuff with quotes */ |
| 173 | /* If it's extremely long, truncate it. */ |
| 174 | if (STRLEN(IObuff) > IOSIZE - 80) |
| 175 | IObuff[IOSIZE - 80] = NUL; |
| 176 | STRCAT(IObuff, s); |
| 177 | /* |
| 178 | * For the first message may have to start a new line. |
| 179 | * For further ones overwrite the previous one, reset msg_scroll before |
| 180 | * calling filemess(). |
| 181 | */ |
| 182 | msg_scroll_save = msg_scroll; |
| 183 | if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) |
| 184 | msg_scroll = FALSE; |
| 185 | if (!msg_scroll) /* wait a bit when overwriting an error msg */ |
| 186 | check_for_delay(FALSE); |
| 187 | msg_start(); |
| 188 | msg_scroll = msg_scroll_save; |
| 189 | msg_scrolled_ign = TRUE; |
| 190 | /* may truncate the message to avoid a hit-return prompt */ |
| 191 | msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr); |
| 192 | msg_clr_eos(); |
| 193 | out_flush(); |
| 194 | msg_scrolled_ign = FALSE; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Read lines from file "fname" into the buffer after line "from". |
| 199 | * |
| 200 | * 1. We allocate blocks with lalloc, as big as possible. |
| 201 | * 2. Each block is filled with characters from the file with a single read(). |
| 202 | * 3. The lines are inserted in the buffer with ml_append(). |
| 203 | * |
| 204 | * (caller must check that fname != NULL, unless READ_STDIN is used) |
| 205 | * |
| 206 | * "lines_to_skip" is the number of lines that must be skipped |
| 207 | * "lines_to_read" is the number of lines that are appended |
| 208 | * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM. |
| 209 | * |
| 210 | * flags: |
| 211 | * READ_NEW starting to edit a new buffer |
| 212 | * READ_FILTER reading filter output |
| 213 | * READ_STDIN read from stdin instead of a file |
| 214 | * READ_BUFFER read from curbuf instead of a file (converting after reading |
| 215 | * stdin) |
| 216 | * READ_DUMMY read into a dummy buffer (to check if file contents changed) |
| 217 | * |
| 218 | * return FAIL for failure, OK otherwise |
| 219 | */ |
| 220 | int |
| 221 | readfile(fname, sfname, from, lines_to_skip, lines_to_read, eap, flags) |
| 222 | char_u *fname; |
| 223 | char_u *sfname; |
| 224 | linenr_T from; |
| 225 | linenr_T lines_to_skip; |
| 226 | linenr_T lines_to_read; |
| 227 | exarg_T *eap; /* can be NULL! */ |
| 228 | int flags; |
| 229 | { |
| 230 | int fd = 0; |
| 231 | int newfile = (flags & READ_NEW); |
| 232 | int check_readonly; |
| 233 | int filtering = (flags & READ_FILTER); |
| 234 | int read_stdin = (flags & READ_STDIN); |
| 235 | int read_buffer = (flags & READ_BUFFER); |
Bram Moolenaar | 690ffc0 | 2008-01-04 15:31:21 +0000 | [diff] [blame] | 236 | int set_options = newfile || read_buffer |
| 237 | || (eap != NULL && eap->read_edit); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 238 | linenr_T read_buf_lnum = 1; /* next line to read from curbuf */ |
| 239 | colnr_T read_buf_col = 0; /* next char to read from this line */ |
| 240 | char_u c; |
| 241 | linenr_T lnum = from; |
| 242 | char_u *ptr = NULL; /* pointer into read buffer */ |
| 243 | char_u *buffer = NULL; /* read buffer */ |
| 244 | char_u *new_buffer = NULL; /* init to shut up gcc */ |
| 245 | char_u *line_start = NULL; /* init to shut up gcc */ |
| 246 | int wasempty; /* buffer was empty before reading */ |
| 247 | colnr_T len; |
| 248 | long size = 0; |
| 249 | char_u *p; |
Bram Moolenaar | 914703b | 2010-05-31 21:59:46 +0200 | [diff] [blame] | 250 | off_t filesize = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 251 | int skip_read = FALSE; |
| 252 | #ifdef FEAT_CRYPT |
| 253 | char_u *cryptkey = NULL; |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 254 | int did_ask_for_key = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 255 | #endif |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 256 | #ifdef FEAT_PERSISTENT_UNDO |
| 257 | context_sha256_T sha_ctx; |
| 258 | int read_undo_file = FALSE; |
| 259 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 260 | int split = 0; /* number of split lines */ |
| 261 | #define UNKNOWN 0x0fffffff /* file size is unknown */ |
| 262 | linenr_T linecnt; |
| 263 | int error = FALSE; /* errors encountered */ |
| 264 | int ff_error = EOL_UNKNOWN; /* file format with errors */ |
| 265 | long linerest = 0; /* remaining chars in line */ |
| 266 | #ifdef UNIX |
| 267 | int perm = 0; |
| 268 | int swap_mode = -1; /* protection bits for swap file */ |
| 269 | #else |
| 270 | int perm; |
| 271 | #endif |
| 272 | int fileformat = 0; /* end-of-line format */ |
| 273 | int keep_fileformat = FALSE; |
| 274 | struct stat st; |
| 275 | int file_readonly; |
| 276 | linenr_T skip_count = 0; |
| 277 | linenr_T read_count = 0; |
| 278 | int msg_save = msg_scroll; |
| 279 | linenr_T read_no_eol_lnum = 0; /* non-zero lnum when last line of |
| 280 | * last read was missing the eol */ |
| 281 | int try_mac = (vim_strchr(p_ffs, 'm') != NULL); |
| 282 | int try_dos = (vim_strchr(p_ffs, 'd') != NULL); |
| 283 | int try_unix = (vim_strchr(p_ffs, 'x') != NULL); |
| 284 | int file_rewind = FALSE; |
| 285 | #ifdef FEAT_MBYTE |
| 286 | int can_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 287 | linenr_T conv_error = 0; /* line nr with conversion error */ |
| 288 | linenr_T illegal_byte = 0; /* line nr with illegal byte */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 289 | int keep_dest_enc = FALSE; /* don't retry when char doesn't fit |
| 290 | in destination encoding */ |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 291 | int bad_char_behavior = BAD_REPLACE; |
| 292 | /* BAD_KEEP, BAD_DROP or character to |
| 293 | * replace with */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 294 | char_u *tmpname = NULL; /* name of 'charconvert' output file */ |
| 295 | int fio_flags = 0; |
| 296 | char_u *fenc; /* fileencoding to use */ |
| 297 | int fenc_alloced; /* fenc_next is in allocated memory */ |
| 298 | char_u *fenc_next = NULL; /* next item in 'fencs' or NULL */ |
| 299 | int advance_fenc = FALSE; |
| 300 | long real_size = 0; |
| 301 | # ifdef USE_ICONV |
| 302 | iconv_t iconv_fd = (iconv_t)-1; /* descriptor for iconv() or -1 */ |
| 303 | # ifdef FEAT_EVAL |
| 304 | int did_iconv = FALSE; /* TRUE when iconv() failed and trying |
| 305 | 'charconvert' next */ |
| 306 | # endif |
| 307 | # endif |
| 308 | int converted = FALSE; /* TRUE if conversion done */ |
| 309 | int notconverted = FALSE; /* TRUE if conversion wanted but it |
| 310 | wasn't possible */ |
| 311 | char_u conv_rest[CONV_RESTLEN]; |
| 312 | int conv_restlen = 0; /* nr of bytes in conv_rest[] */ |
| 313 | #endif |
| 314 | |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 315 | #ifdef FEAT_AUTOCMD |
| 316 | /* Remember the initial values of curbuf, curbuf->b_ffname and |
| 317 | * curbuf->b_fname to detect whether they are altered as a result of |
| 318 | * executing nasty autocommands. Also check if "fname" and "sfname" |
| 319 | * point to one of these values. */ |
| 320 | buf_T *old_curbuf = curbuf; |
| 321 | char_u *old_b_ffname = curbuf->b_ffname; |
| 322 | char_u *old_b_fname = curbuf->b_fname; |
| 323 | int using_b_ffname = (fname == curbuf->b_ffname) |
| 324 | || (sfname == curbuf->b_ffname); |
| 325 | int using_b_fname = (fname == curbuf->b_fname) |
| 326 | || (sfname == curbuf->b_fname); |
| 327 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 328 | write_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] | 329 | |
| 330 | /* |
| 331 | * If there is no file name yet, use the one for the read file. |
| 332 | * BF_NOTEDITED is set to reflect this. |
| 333 | * Don't do this for a read from a filter. |
| 334 | * Only do this when 'cpoptions' contains the 'f' flag. |
| 335 | */ |
| 336 | if (curbuf->b_ffname == NULL |
| 337 | && !filtering |
| 338 | && fname != NULL |
| 339 | && vim_strchr(p_cpo, CPO_FNAMER) != NULL |
| 340 | && !(flags & READ_DUMMY)) |
| 341 | { |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 342 | if (set_rw_fname(fname, sfname) == FAIL) |
| 343 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 346 | /* After reading a file the cursor line changes but we don't want to |
| 347 | * display the line. */ |
| 348 | ex_no_reprint = TRUE; |
| 349 | |
Bram Moolenaar | 55b7cf8 | 2006-09-09 12:52:42 +0000 | [diff] [blame] | 350 | /* don't display the file info for another buffer now */ |
| 351 | need_fileinfo = FALSE; |
| 352 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 353 | /* |
| 354 | * For Unix: Use the short file name whenever possible. |
| 355 | * Avoids problems with networks and when directory names are changed. |
| 356 | * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to |
| 357 | * another directory, which we don't detect. |
| 358 | */ |
| 359 | if (sfname == NULL) |
| 360 | sfname = fname; |
| 361 | #if defined(UNIX) || defined(__EMX__) |
| 362 | fname = sfname; |
| 363 | #endif |
| 364 | |
| 365 | #ifdef FEAT_AUTOCMD |
| 366 | /* |
| 367 | * The BufReadCmd and FileReadCmd events intercept the reading process by |
| 368 | * executing the associated commands instead. |
| 369 | */ |
| 370 | if (!filtering && !read_stdin && !read_buffer) |
| 371 | { |
| 372 | pos_T pos; |
| 373 | |
| 374 | pos = curbuf->b_op_start; |
| 375 | |
| 376 | /* Set '[ mark to the line above where the lines go (line 1 if zero). */ |
| 377 | curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); |
| 378 | curbuf->b_op_start.col = 0; |
| 379 | |
| 380 | if (newfile) |
| 381 | { |
| 382 | if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname, |
| 383 | FALSE, curbuf, eap)) |
| 384 | #ifdef FEAT_EVAL |
| 385 | return aborting() ? FAIL : OK; |
| 386 | #else |
| 387 | return OK; |
| 388 | #endif |
| 389 | } |
| 390 | else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname, |
| 391 | FALSE, NULL, eap)) |
| 392 | #ifdef FEAT_EVAL |
| 393 | return aborting() ? FAIL : OK; |
| 394 | #else |
| 395 | return OK; |
| 396 | #endif |
| 397 | |
| 398 | curbuf->b_op_start = pos; |
| 399 | } |
| 400 | #endif |
| 401 | |
| 402 | if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0) |
| 403 | msg_scroll = FALSE; /* overwrite previous file message */ |
| 404 | else |
| 405 | msg_scroll = TRUE; /* don't overwrite previous file message */ |
| 406 | |
| 407 | /* |
| 408 | * If the name ends in a path separator, we can't open it. Check here, |
| 409 | * because reading the file may actually work, but then creating the swap |
| 410 | * file may destroy it! Reported on MS-DOS and Win 95. |
| 411 | * If the name is too long we might crash further on, quit here. |
| 412 | */ |
Bram Moolenaar | 15d0a8c | 2004-09-06 17:44:46 +0000 | [diff] [blame] | 413 | if (fname != NULL && *fname != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 414 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 415 | p = fname + STRLEN(fname); |
| 416 | if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL) |
Bram Moolenaar | 15d0a8c | 2004-09-06 17:44:46 +0000 | [diff] [blame] | 417 | { |
| 418 | filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0); |
| 419 | msg_end(); |
| 420 | msg_scroll = msg_save; |
| 421 | return FAIL; |
| 422 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | #ifdef UNIX |
| 426 | /* |
| 427 | * On Unix it is possible to read a directory, so we have to |
| 428 | * check for it before the mch_open(). |
| 429 | */ |
| 430 | if (!read_stdin && !read_buffer) |
| 431 | { |
| 432 | perm = mch_getperm(fname); |
| 433 | if (perm >= 0 && !S_ISREG(perm) /* not a regular file ... */ |
| 434 | # ifdef S_ISFIFO |
| 435 | && !S_ISFIFO(perm) /* ... or fifo */ |
| 436 | # endif |
| 437 | # ifdef S_ISSOCK |
| 438 | && !S_ISSOCK(perm) /* ... or socket */ |
| 439 | # endif |
Bram Moolenaar | fe1c56d | 2007-07-10 15:10:54 +0000 | [diff] [blame] | 440 | # ifdef OPEN_CHR_FILES |
| 441 | && !(S_ISCHR(perm) && is_dev_fd_file(fname)) |
| 442 | /* ... or a character special file named /dev/fd/<n> */ |
| 443 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 444 | ) |
| 445 | { |
| 446 | if (S_ISDIR(perm)) |
| 447 | filemess(curbuf, fname, (char_u *)_("is a directory"), 0); |
| 448 | else |
| 449 | filemess(curbuf, fname, (char_u *)_("is not a file"), 0); |
| 450 | msg_end(); |
| 451 | msg_scroll = msg_save; |
| 452 | return FAIL; |
| 453 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 454 | |
Bram Moolenaar | c67764a | 2006-10-12 19:14:26 +0000 | [diff] [blame] | 455 | # if defined(MSDOS) || defined(MSWIN) || defined(OS2) |
| 456 | /* |
| 457 | * MS-Windows allows opening a device, but we will probably get stuck |
| 458 | * trying to read it. |
| 459 | */ |
| 460 | if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE) |
| 461 | { |
Bram Moolenaar | 5386a12 | 2007-06-28 20:02:32 +0000 | [diff] [blame] | 462 | 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] | 463 | msg_end(); |
| 464 | msg_scroll = msg_save; |
| 465 | return FAIL; |
| 466 | } |
| 467 | # endif |
Bram Moolenaar | 043545e | 2006-10-10 16:44:07 +0000 | [diff] [blame] | 468 | } |
| 469 | #endif |
| 470 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 471 | /* set default 'fileformat' */ |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 472 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 473 | { |
| 474 | if (eap != NULL && eap->force_ff != 0) |
| 475 | set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL); |
| 476 | else if (*p_ffs != NUL) |
| 477 | set_fileformat(default_fileformat(), OPT_LOCAL); |
| 478 | } |
| 479 | |
| 480 | /* set or reset 'binary' */ |
| 481 | if (eap != NULL && eap->force_bin != 0) |
| 482 | { |
| 483 | int oldval = curbuf->b_p_bin; |
| 484 | |
| 485 | curbuf->b_p_bin = (eap->force_bin == FORCE_BIN); |
| 486 | set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL); |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * When opening a new file we take the readonly flag from the file. |
| 491 | * Default is r/w, can be set to r/o below. |
| 492 | * Don't reset it when in readonly mode |
| 493 | * Only set/reset b_p_ro when BF_CHECK_RO is set. |
| 494 | */ |
| 495 | check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO)); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 496 | if (check_readonly && !readonlymode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 497 | curbuf->b_p_ro = FALSE; |
| 498 | |
| 499 | if (newfile && !read_stdin && !read_buffer) |
| 500 | { |
| 501 | /* Remember time of file. |
| 502 | * For RISCOS, also remember the filetype. |
| 503 | */ |
| 504 | if (mch_stat((char *)fname, &st) >= 0) |
| 505 | { |
| 506 | buf_store_time(curbuf, &st, fname); |
| 507 | curbuf->b_mtime_read = curbuf->b_mtime; |
| 508 | |
| 509 | #if defined(RISCOS) && defined(FEAT_OSFILETYPE) |
| 510 | /* Read the filetype into the buffer local filetype option. */ |
| 511 | mch_read_filetype(fname); |
| 512 | #endif |
| 513 | #ifdef UNIX |
| 514 | /* |
| 515 | * Use the protection bits of the original file for the swap file. |
| 516 | * This makes it possible for others to read the name of the |
| 517 | * edited file from the swapfile, but only if they can read the |
| 518 | * edited file. |
| 519 | * Remove the "write" and "execute" bits for group and others |
| 520 | * (they must not write the swapfile). |
| 521 | * Add the "read" and "write" bits for the user, otherwise we may |
| 522 | * not be able to write to the file ourselves. |
| 523 | * Setting the bits is done below, after creating the swap file. |
| 524 | */ |
| 525 | swap_mode = (st.st_mode & 0644) | 0600; |
| 526 | #endif |
| 527 | #ifdef FEAT_CW_EDITOR |
| 528 | /* Get the FSSpec on MacOS |
| 529 | * TODO: Update it properly when the buffer name changes |
| 530 | */ |
| 531 | (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec); |
| 532 | #endif |
| 533 | #ifdef VMS |
| 534 | curbuf->b_fab_rfm = st.st_fab_rfm; |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 535 | curbuf->b_fab_rat = st.st_fab_rat; |
| 536 | curbuf->b_fab_mrs = st.st_fab_mrs; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 537 | #endif |
| 538 | } |
| 539 | else |
| 540 | { |
| 541 | curbuf->b_mtime = 0; |
| 542 | curbuf->b_mtime_read = 0; |
| 543 | curbuf->b_orig_size = 0; |
| 544 | curbuf->b_orig_mode = 0; |
| 545 | } |
| 546 | |
| 547 | /* Reset the "new file" flag. It will be set again below when the |
| 548 | * file doesn't exist. */ |
| 549 | curbuf->b_flags &= ~(BF_NEW | BF_NEW_W); |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * for UNIX: check readonly with perm and mch_access() |
| 554 | * for RISCOS: same as Unix, otherwise file gets re-datestamped! |
| 555 | * for MSDOS and Amiga: check readonly by trying to open the file for writing |
| 556 | */ |
| 557 | file_readonly = FALSE; |
| 558 | if (read_stdin) |
| 559 | { |
| 560 | #if defined(MSDOS) || defined(MSWIN) || defined(OS2) |
| 561 | /* Force binary I/O on stdin to avoid CR-LF -> LF conversion. */ |
| 562 | setmode(0, O_BINARY); |
| 563 | #endif |
| 564 | } |
| 565 | else if (!read_buffer) |
| 566 | { |
| 567 | #ifdef USE_MCH_ACCESS |
| 568 | if ( |
| 569 | # ifdef UNIX |
| 570 | !(perm & 0222) || |
| 571 | # endif |
| 572 | mch_access((char *)fname, W_OK)) |
| 573 | file_readonly = TRUE; |
| 574 | fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); |
| 575 | #else |
| 576 | if (!newfile |
| 577 | || readonlymode |
| 578 | || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0) |
| 579 | { |
| 580 | file_readonly = TRUE; |
| 581 | /* try to open ro */ |
| 582 | fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); |
| 583 | } |
| 584 | #endif |
| 585 | } |
| 586 | |
| 587 | if (fd < 0) /* cannot open at all */ |
| 588 | { |
| 589 | #ifndef UNIX |
| 590 | int isdir_f; |
| 591 | #endif |
| 592 | msg_scroll = msg_save; |
| 593 | #ifndef UNIX |
| 594 | /* |
| 595 | * On MSDOS and Amiga we can't open a directory, check here. |
| 596 | */ |
| 597 | isdir_f = (mch_isdir(fname)); |
| 598 | perm = mch_getperm(fname); /* check if the file exists */ |
| 599 | if (isdir_f) |
| 600 | { |
| 601 | filemess(curbuf, sfname, (char_u *)_("is a directory"), 0); |
| 602 | curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
| 603 | } |
| 604 | else |
| 605 | #endif |
| 606 | if (newfile) |
| 607 | { |
Bram Moolenaar | 2efbc66 | 2010-05-14 18:56:38 +0200 | [diff] [blame] | 608 | if (perm < 0 |
| 609 | #ifdef ENOENT |
| 610 | && errno == ENOENT |
| 611 | #endif |
| 612 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 613 | { |
| 614 | /* |
| 615 | * Set the 'new-file' flag, so that when the file has |
| 616 | * been created by someone else, a ":w" will complain. |
| 617 | */ |
| 618 | curbuf->b_flags |= BF_NEW; |
| 619 | |
| 620 | /* Create a swap file now, so that other Vims are warned |
| 621 | * that we are editing this file. Don't do this for a |
| 622 | * "nofile" or "nowrite" buffer type. */ |
| 623 | #ifdef FEAT_QUICKFIX |
| 624 | if (!bt_dontwrite(curbuf)) |
| 625 | #endif |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 626 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 627 | check_need_swap(newfile); |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 628 | #ifdef FEAT_AUTOCMD |
| 629 | /* SwapExists autocommand may mess things up */ |
| 630 | if (curbuf != old_curbuf |
| 631 | || (using_b_ffname |
| 632 | && (old_b_ffname != curbuf->b_ffname)) |
| 633 | || (using_b_fname |
| 634 | && (old_b_fname != curbuf->b_fname))) |
| 635 | { |
| 636 | EMSG(_(e_auchangedbuf)); |
| 637 | return FAIL; |
| 638 | } |
| 639 | #endif |
| 640 | } |
Bram Moolenaar | 5b962cf | 2005-12-12 21:58:40 +0000 | [diff] [blame] | 641 | if (dir_of_file_exists(fname)) |
| 642 | filemess(curbuf, sfname, (char_u *)_("[New File]"), 0); |
| 643 | else |
| 644 | filemess(curbuf, sfname, |
| 645 | (char_u *)_("[New DIRECTORY]"), 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 646 | #ifdef FEAT_VIMINFO |
| 647 | /* Even though this is a new file, it might have been |
| 648 | * edited before and deleted. Get the old marks. */ |
| 649 | check_marks_read(); |
| 650 | #endif |
| 651 | #ifdef FEAT_MBYTE |
| 652 | if (eap != NULL && eap->force_enc != 0) |
| 653 | { |
| 654 | /* set forced 'fileencoding' */ |
| 655 | fenc = enc_canonize(eap->cmd + eap->force_enc); |
| 656 | if (fenc != NULL) |
| 657 | set_string_option_direct((char_u *)"fenc", -1, |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 658 | fenc, OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 659 | vim_free(fenc); |
| 660 | } |
| 661 | #endif |
| 662 | #ifdef FEAT_AUTOCMD |
| 663 | apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname, |
| 664 | FALSE, curbuf, eap); |
| 665 | #endif |
| 666 | /* remember the current fileformat */ |
| 667 | save_file_ff(curbuf); |
| 668 | |
| 669 | #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
| 670 | if (aborting()) /* autocmds may abort script processing */ |
| 671 | return FAIL; |
| 672 | #endif |
| 673 | return OK; /* a new file is not an error */ |
| 674 | } |
| 675 | else |
| 676 | { |
Bram Moolenaar | 202795b | 2005-10-11 20:29:39 +0000 | [diff] [blame] | 677 | filemess(curbuf, sfname, (char_u *)( |
| 678 | # ifdef EFBIG |
| 679 | (errno == EFBIG) ? _("[File too big]") : |
| 680 | # endif |
Bram Moolenaar | 2efbc66 | 2010-05-14 18:56:38 +0200 | [diff] [blame] | 681 | # ifdef EOVERFLOW |
| 682 | (errno == EOVERFLOW) ? _("[File too big]") : |
| 683 | # endif |
Bram Moolenaar | 202795b | 2005-10-11 20:29:39 +0000 | [diff] [blame] | 684 | _("[Permission Denied]")), 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 685 | curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | return FAIL; |
| 690 | } |
| 691 | |
| 692 | /* |
| 693 | * Only set the 'ro' flag for readonly files the first time they are |
| 694 | * loaded. Help files always get readonly mode |
| 695 | */ |
| 696 | if ((check_readonly && file_readonly) || curbuf->b_help) |
| 697 | curbuf->b_p_ro = TRUE; |
| 698 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 699 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 700 | { |
Bram Moolenaar | 690ffc0 | 2008-01-04 15:31:21 +0000 | [diff] [blame] | 701 | /* Don't change 'eol' if reading from buffer as it will already be |
| 702 | * correctly set when reading stdin. */ |
| 703 | if (!read_buffer) |
| 704 | { |
| 705 | curbuf->b_p_eol = TRUE; |
| 706 | curbuf->b_start_eol = TRUE; |
| 707 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 708 | #ifdef FEAT_MBYTE |
| 709 | curbuf->b_p_bomb = FALSE; |
Bram Moolenaar | 83eb885 | 2007-08-12 13:51:26 +0000 | [diff] [blame] | 710 | curbuf->b_start_bomb = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 711 | #endif |
| 712 | } |
| 713 | |
| 714 | /* Create a swap file now, so that other Vims are warned that we are |
| 715 | * editing this file. |
| 716 | * Don't do this for a "nofile" or "nowrite" buffer type. */ |
| 717 | #ifdef FEAT_QUICKFIX |
| 718 | if (!bt_dontwrite(curbuf)) |
| 719 | #endif |
| 720 | { |
| 721 | check_need_swap(newfile); |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 722 | #ifdef FEAT_AUTOCMD |
| 723 | if (!read_stdin && (curbuf != old_curbuf |
| 724 | || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) |
| 725 | || (using_b_fname && (old_b_fname != curbuf->b_fname)))) |
| 726 | { |
| 727 | EMSG(_(e_auchangedbuf)); |
| 728 | if (!read_buffer) |
| 729 | close(fd); |
| 730 | return FAIL; |
| 731 | } |
| 732 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 733 | #ifdef UNIX |
| 734 | /* Set swap file protection bits after creating it. */ |
Bram Moolenaar | f061e0b | 2009-06-24 15:32:01 +0000 | [diff] [blame] | 735 | if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL |
| 736 | && curbuf->b_ml.ml_mfp->mf_fname != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 737 | (void)mch_setperm(curbuf->b_ml.ml_mfp->mf_fname, (long)swap_mode); |
| 738 | #endif |
| 739 | } |
| 740 | |
Bram Moolenaar | b815dac | 2005-12-07 20:59:24 +0000 | [diff] [blame] | 741 | #if defined(HAS_SWAP_EXISTS_ACTION) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 742 | /* If "Quit" selected at ATTENTION dialog, don't load the file */ |
| 743 | if (swap_exists_action == SEA_QUIT) |
| 744 | { |
| 745 | if (!read_buffer && !read_stdin) |
| 746 | close(fd); |
| 747 | return FAIL; |
| 748 | } |
| 749 | #endif |
| 750 | |
| 751 | ++no_wait_return; /* don't wait for return yet */ |
| 752 | |
| 753 | /* |
| 754 | * Set '[ mark to the line above where the lines go (line 1 if zero). |
| 755 | */ |
| 756 | curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); |
| 757 | curbuf->b_op_start.col = 0; |
| 758 | |
| 759 | #ifdef FEAT_AUTOCMD |
| 760 | if (!read_buffer) |
| 761 | { |
| 762 | int m = msg_scroll; |
| 763 | int n = msg_scrolled; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 764 | |
| 765 | /* |
| 766 | * The file must be closed again, the autocommands may want to change |
| 767 | * the file before reading it. |
| 768 | */ |
| 769 | if (!read_stdin) |
| 770 | close(fd); /* ignore errors */ |
| 771 | |
| 772 | /* |
| 773 | * The output from the autocommands should not overwrite anything and |
| 774 | * should not be overwritten: Set msg_scroll, restore its value if no |
| 775 | * output was done. |
| 776 | */ |
| 777 | msg_scroll = TRUE; |
| 778 | if (filtering) |
| 779 | apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname, |
| 780 | FALSE, curbuf, eap); |
| 781 | else if (read_stdin) |
| 782 | apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname, |
| 783 | FALSE, curbuf, eap); |
| 784 | else if (newfile) |
| 785 | apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname, |
| 786 | FALSE, curbuf, eap); |
| 787 | else |
| 788 | apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname, |
| 789 | FALSE, NULL, eap); |
| 790 | if (msg_scrolled == n) |
| 791 | msg_scroll = m; |
| 792 | |
| 793 | #ifdef FEAT_EVAL |
| 794 | if (aborting()) /* autocmds may abort script processing */ |
| 795 | { |
| 796 | --no_wait_return; |
| 797 | msg_scroll = msg_save; |
| 798 | curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
| 799 | return FAIL; |
| 800 | } |
| 801 | #endif |
| 802 | /* |
| 803 | * Don't allow the autocommands to change the current buffer. |
| 804 | * Try to re-open the file. |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 805 | * |
| 806 | * Don't allow the autocommands to change the buffer name either |
| 807 | * (cd for example) if it invalidates fname or sfname. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 808 | */ |
| 809 | if (!read_stdin && (curbuf != old_curbuf |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 810 | || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) |
| 811 | || (using_b_fname && (old_b_fname != curbuf->b_fname)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 812 | || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0)) |
| 813 | { |
| 814 | --no_wait_return; |
| 815 | msg_scroll = msg_save; |
| 816 | if (fd < 0) |
| 817 | EMSG(_("E200: *ReadPre autocommands made the file unreadable")); |
| 818 | else |
| 819 | EMSG(_("E201: *ReadPre autocommands must not change current buffer")); |
| 820 | curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
| 821 | return FAIL; |
| 822 | } |
| 823 | } |
| 824 | #endif /* FEAT_AUTOCMD */ |
| 825 | |
| 826 | /* Autocommands may add lines to the file, need to check if it is empty */ |
| 827 | wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY); |
| 828 | |
| 829 | if (!recoverymode && !filtering && !(flags & READ_DUMMY)) |
| 830 | { |
| 831 | /* |
| 832 | * Show the user that we are busy reading the input. Sometimes this |
| 833 | * may take a while. When reading from stdin another program may |
| 834 | * still be running, don't move the cursor to the last line, unless |
| 835 | * always using the GUI. |
| 836 | */ |
| 837 | if (read_stdin) |
| 838 | { |
| 839 | #ifndef ALWAYS_USE_GUI |
| 840 | mch_msg(_("Vim: Reading from stdin...\n")); |
| 841 | #endif |
| 842 | #ifdef FEAT_GUI |
| 843 | /* Also write a message in the GUI window, if there is one. */ |
| 844 | if (gui.in_use && !gui.dying && !gui.starting) |
| 845 | { |
| 846 | p = (char_u *)_("Reading from stdin..."); |
| 847 | gui_write(p, (int)STRLEN(p)); |
| 848 | } |
| 849 | #endif |
| 850 | } |
| 851 | else if (!read_buffer) |
| 852 | filemess(curbuf, sfname, (char_u *)"", 0); |
| 853 | } |
| 854 | |
| 855 | msg_scroll = FALSE; /* overwrite the file message */ |
| 856 | |
| 857 | /* |
| 858 | * Set linecnt now, before the "retry" caused by a wrong guess for |
| 859 | * fileformat, and after the autocommands, which may change them. |
| 860 | */ |
| 861 | linecnt = curbuf->b_ml.ml_line_count; |
| 862 | |
| 863 | #ifdef FEAT_MBYTE |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 864 | /* "++bad=" argument. */ |
| 865 | if (eap != NULL && eap->bad_char != 0) |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 866 | { |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 867 | bad_char_behavior = eap->bad_char; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 868 | if (set_options) |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 869 | curbuf->b_bad_char = eap->bad_char; |
| 870 | } |
| 871 | else |
| 872 | curbuf->b_bad_char = 0; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 873 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 874 | /* |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 875 | * Decide which 'encoding' to use or use first. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 876 | */ |
| 877 | if (eap != NULL && eap->force_enc != 0) |
| 878 | { |
| 879 | fenc = enc_canonize(eap->cmd + eap->force_enc); |
| 880 | fenc_alloced = TRUE; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 881 | keep_dest_enc = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 882 | } |
| 883 | else if (curbuf->b_p_bin) |
| 884 | { |
| 885 | fenc = (char_u *)""; /* binary: don't convert */ |
| 886 | fenc_alloced = FALSE; |
| 887 | } |
| 888 | else if (curbuf->b_help) |
| 889 | { |
| 890 | char_u firstline[80]; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 891 | int fc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 892 | |
| 893 | /* Help files are either utf-8 or latin1. Try utf-8 first, if this |
| 894 | * fails it must be latin1. |
| 895 | * Always do this when 'encoding' is "utf-8". Otherwise only do |
| 896 | * this when needed to avoid [converted] remarks all the time. |
| 897 | * It is needed when the first line contains non-ASCII characters. |
| 898 | * That is only in *.??x files. */ |
| 899 | fenc = (char_u *)"latin1"; |
| 900 | c = enc_utf8; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 901 | if (!c && !read_stdin) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 902 | { |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 903 | fc = fname[STRLEN(fname) - 1]; |
| 904 | if (TOLOWER_ASC(fc) == 'x') |
| 905 | { |
| 906 | /* Read the first line (and a bit more). Immediately rewind to |
| 907 | * the start of the file. If the read() fails "len" is -1. */ |
| 908 | len = vim_read(fd, firstline, 80); |
| 909 | lseek(fd, (off_t)0L, SEEK_SET); |
| 910 | for (p = firstline; p < firstline + len; ++p) |
| 911 | if (*p >= 0x80) |
| 912 | { |
| 913 | c = TRUE; |
| 914 | break; |
| 915 | } |
| 916 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | if (c) |
| 920 | { |
| 921 | fenc_next = fenc; |
| 922 | fenc = (char_u *)"utf-8"; |
| 923 | |
| 924 | /* When the file is utf-8 but a character doesn't fit in |
| 925 | * 'encoding' don't retry. In help text editing utf-8 bytes |
| 926 | * doesn't make sense. */ |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 927 | if (!enc_utf8) |
| 928 | keep_dest_enc = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 929 | } |
| 930 | fenc_alloced = FALSE; |
| 931 | } |
| 932 | else if (*p_fencs == NUL) |
| 933 | { |
| 934 | fenc = curbuf->b_p_fenc; /* use format from buffer */ |
| 935 | fenc_alloced = FALSE; |
| 936 | } |
| 937 | else |
| 938 | { |
| 939 | fenc_next = p_fencs; /* try items in 'fileencodings' */ |
| 940 | fenc = next_fenc(&fenc_next); |
| 941 | fenc_alloced = TRUE; |
| 942 | } |
| 943 | #endif |
| 944 | |
| 945 | /* |
| 946 | * Jump back here to retry reading the file in different ways. |
| 947 | * Reasons to retry: |
| 948 | * - encoding conversion failed: try another one from "fenc_next" |
| 949 | * - BOM detected and fenc was set, need to setup conversion |
| 950 | * - "fileformat" check failed: try another |
| 951 | * |
| 952 | * Variables set for special retry actions: |
| 953 | * "file_rewind" Rewind the file to start reading it again. |
| 954 | * "advance_fenc" Advance "fenc" using "fenc_next". |
| 955 | * "skip_read" Re-use already read bytes (BOM detected). |
| 956 | * "did_iconv" iconv() conversion failed, try 'charconvert'. |
| 957 | * "keep_fileformat" Don't reset "fileformat". |
| 958 | * |
| 959 | * Other status indicators: |
| 960 | * "tmpname" When != NULL did conversion with 'charconvert'. |
| 961 | * Output file has to be deleted afterwards. |
| 962 | * "iconv_fd" When != -1 did conversion with iconv(). |
| 963 | */ |
| 964 | retry: |
| 965 | |
| 966 | if (file_rewind) |
| 967 | { |
| 968 | if (read_buffer) |
| 969 | { |
| 970 | read_buf_lnum = 1; |
| 971 | read_buf_col = 0; |
| 972 | } |
| 973 | else if (read_stdin || lseek(fd, (off_t)0L, SEEK_SET) != 0) |
| 974 | { |
| 975 | /* Can't rewind the file, give up. */ |
| 976 | error = TRUE; |
| 977 | goto failed; |
| 978 | } |
| 979 | /* Delete the previously read lines. */ |
| 980 | while (lnum > from) |
| 981 | ml_delete(lnum--, FALSE); |
| 982 | file_rewind = FALSE; |
| 983 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 984 | if (set_options) |
Bram Moolenaar | 83eb885 | 2007-08-12 13:51:26 +0000 | [diff] [blame] | 985 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 986 | curbuf->b_p_bomb = FALSE; |
Bram Moolenaar | 83eb885 | 2007-08-12 13:51:26 +0000 | [diff] [blame] | 987 | curbuf->b_start_bomb = FALSE; |
| 988 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 989 | conv_error = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 990 | #endif |
| 991 | } |
| 992 | |
| 993 | /* |
| 994 | * When retrying with another "fenc" and the first time "fileformat" |
| 995 | * will be reset. |
| 996 | */ |
| 997 | if (keep_fileformat) |
| 998 | keep_fileformat = FALSE; |
| 999 | else |
| 1000 | { |
| 1001 | if (eap != NULL && eap->force_ff != 0) |
Bram Moolenaar | 1c86036 | 2008-11-12 15:05:21 +0000 | [diff] [blame] | 1002 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1003 | fileformat = get_fileformat_force(curbuf, eap); |
Bram Moolenaar | 1c86036 | 2008-11-12 15:05:21 +0000 | [diff] [blame] | 1004 | try_unix = try_dos = try_mac = FALSE; |
| 1005 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1006 | else if (curbuf->b_p_bin) |
| 1007 | fileformat = EOL_UNIX; /* binary: use Unix format */ |
| 1008 | else if (*p_ffs == NUL) |
| 1009 | fileformat = get_fileformat(curbuf);/* use format from buffer */ |
| 1010 | else |
| 1011 | fileformat = EOL_UNKNOWN; /* detect from file */ |
| 1012 | } |
| 1013 | |
| 1014 | #ifdef FEAT_MBYTE |
| 1015 | # ifdef USE_ICONV |
| 1016 | if (iconv_fd != (iconv_t)-1) |
| 1017 | { |
| 1018 | /* aborted conversion with iconv(), close the descriptor */ |
| 1019 | iconv_close(iconv_fd); |
| 1020 | iconv_fd = (iconv_t)-1; |
| 1021 | } |
| 1022 | # endif |
| 1023 | |
| 1024 | if (advance_fenc) |
| 1025 | { |
| 1026 | /* |
| 1027 | * Try the next entry in 'fileencodings'. |
| 1028 | */ |
| 1029 | advance_fenc = FALSE; |
| 1030 | |
| 1031 | if (eap != NULL && eap->force_enc != 0) |
| 1032 | { |
| 1033 | /* Conversion given with "++cc=" wasn't possible, read |
| 1034 | * without conversion. */ |
| 1035 | notconverted = TRUE; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1036 | conv_error = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1037 | if (fenc_alloced) |
| 1038 | vim_free(fenc); |
| 1039 | fenc = (char_u *)""; |
| 1040 | fenc_alloced = FALSE; |
| 1041 | } |
| 1042 | else |
| 1043 | { |
| 1044 | if (fenc_alloced) |
| 1045 | vim_free(fenc); |
| 1046 | if (fenc_next != NULL) |
| 1047 | { |
| 1048 | fenc = next_fenc(&fenc_next); |
| 1049 | fenc_alloced = (fenc_next != NULL); |
| 1050 | } |
| 1051 | else |
| 1052 | { |
| 1053 | fenc = (char_u *)""; |
| 1054 | fenc_alloced = FALSE; |
| 1055 | } |
| 1056 | } |
| 1057 | if (tmpname != NULL) |
| 1058 | { |
| 1059 | mch_remove(tmpname); /* delete converted file */ |
| 1060 | vim_free(tmpname); |
| 1061 | tmpname = NULL; |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | /* |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 1066 | * Conversion may be required when the encoding of the file is different |
| 1067 | * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1068 | */ |
| 1069 | fio_flags = 0; |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 1070 | converted = need_conversion(fenc); |
| 1071 | if (converted) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1072 | { |
| 1073 | |
| 1074 | /* "ucs-bom" means we need to check the first bytes of the file |
| 1075 | * for a BOM. */ |
| 1076 | if (STRCMP(fenc, ENC_UCSBOM) == 0) |
| 1077 | fio_flags = FIO_UCSBOM; |
| 1078 | |
| 1079 | /* |
| 1080 | * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be |
| 1081 | * done. This is handled below after read(). Prepare the |
| 1082 | * fio_flags to avoid having to parse the string each time. |
| 1083 | * Also check for Unicode to Latin1 conversion, because iconv() |
| 1084 | * appears not to handle this correctly. This works just like |
| 1085 | * conversion to UTF-8 except how the resulting character is put in |
| 1086 | * the buffer. |
| 1087 | */ |
| 1088 | else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0) |
| 1089 | fio_flags = get_fio_flags(fenc); |
| 1090 | |
| 1091 | # ifdef WIN3264 |
| 1092 | /* |
| 1093 | * Conversion from an MS-Windows codepage to UTF-8 or another codepage |
| 1094 | * is handled with MultiByteToWideChar(). |
| 1095 | */ |
| 1096 | if (fio_flags == 0) |
| 1097 | fio_flags = get_win_fio_flags(fenc); |
| 1098 | # endif |
| 1099 | |
| 1100 | # ifdef MACOS_X |
| 1101 | /* Conversion from Apple MacRoman to latin1 or UTF-8 */ |
| 1102 | if (fio_flags == 0) |
| 1103 | fio_flags = get_mac_fio_flags(fenc); |
| 1104 | # endif |
| 1105 | |
| 1106 | # ifdef USE_ICONV |
| 1107 | /* |
| 1108 | * Try using iconv() if we can't convert internally. |
| 1109 | */ |
| 1110 | if (fio_flags == 0 |
| 1111 | # ifdef FEAT_EVAL |
| 1112 | && !did_iconv |
| 1113 | # endif |
| 1114 | ) |
| 1115 | iconv_fd = (iconv_t)my_iconv_open( |
| 1116 | enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc); |
| 1117 | # endif |
| 1118 | |
| 1119 | # ifdef FEAT_EVAL |
| 1120 | /* |
| 1121 | * Use the 'charconvert' expression when conversion is required |
| 1122 | * and we can't do it internally or with iconv(). |
| 1123 | */ |
| 1124 | if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL |
| 1125 | # ifdef USE_ICONV |
| 1126 | && iconv_fd == (iconv_t)-1 |
| 1127 | # endif |
| 1128 | ) |
| 1129 | { |
| 1130 | # ifdef USE_ICONV |
| 1131 | did_iconv = FALSE; |
| 1132 | # endif |
| 1133 | /* Skip conversion when it's already done (retry for wrong |
| 1134 | * "fileformat"). */ |
| 1135 | if (tmpname == NULL) |
| 1136 | { |
| 1137 | tmpname = readfile_charconvert(fname, fenc, &fd); |
| 1138 | if (tmpname == NULL) |
| 1139 | { |
| 1140 | /* Conversion failed. Try another one. */ |
| 1141 | advance_fenc = TRUE; |
| 1142 | if (fd < 0) |
| 1143 | { |
| 1144 | /* Re-opening the original file failed! */ |
| 1145 | EMSG(_("E202: Conversion made file unreadable!")); |
| 1146 | error = TRUE; |
| 1147 | goto failed; |
| 1148 | } |
| 1149 | goto retry; |
| 1150 | } |
| 1151 | } |
| 1152 | } |
| 1153 | else |
| 1154 | # endif |
| 1155 | { |
| 1156 | if (fio_flags == 0 |
| 1157 | # ifdef USE_ICONV |
| 1158 | && iconv_fd == (iconv_t)-1 |
| 1159 | # endif |
| 1160 | ) |
| 1161 | { |
| 1162 | /* Conversion wanted but we can't. |
| 1163 | * Try the next conversion in 'fileencodings' */ |
| 1164 | advance_fenc = TRUE; |
| 1165 | goto retry; |
| 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 | |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1170 | /* 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] | 1171 | * 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] | 1172 | * stdin or fixed at a specific encoding. */ |
| 1173 | can_retry = (*fenc != NUL && !read_stdin && !keep_dest_enc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1174 | #endif |
| 1175 | |
| 1176 | if (!skip_read) |
| 1177 | { |
| 1178 | linerest = 0; |
| 1179 | filesize = 0; |
| 1180 | skip_count = lines_to_skip; |
| 1181 | read_count = lines_to_read; |
| 1182 | #ifdef FEAT_MBYTE |
| 1183 | conv_restlen = 0; |
| 1184 | #endif |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 1185 | #ifdef FEAT_PERSISTENT_UNDO |
| 1186 | read_undo_file = (newfile && curbuf->b_ffname != NULL && curbuf->b_p_udf |
| 1187 | && !filtering && !read_stdin && !read_buffer); |
| 1188 | if (read_undo_file) |
| 1189 | sha256_start(&sha_ctx); |
| 1190 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | while (!error && !got_int) |
| 1194 | { |
| 1195 | /* |
| 1196 | * We allocate as much space for the file as we can get, plus |
| 1197 | * space for the old line plus room for one terminating NUL. |
| 1198 | * The amount is limited by the fact that read() only can read |
| 1199 | * upto max_unsigned characters (and other things). |
| 1200 | */ |
| 1201 | #if SIZEOF_INT <= 2 |
| 1202 | if (linerest >= 0x7ff0) |
| 1203 | { |
| 1204 | ++split; |
| 1205 | *ptr = NL; /* split line by inserting a NL */ |
| 1206 | size = 1; |
| 1207 | } |
| 1208 | else |
| 1209 | #endif |
| 1210 | { |
| 1211 | if (!skip_read) |
| 1212 | { |
| 1213 | #if SIZEOF_INT > 2 |
Bram Moolenaar | 311d982 | 2007-02-27 15:48:28 +0000 | [diff] [blame] | 1214 | # if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1215 | size = SSIZE_MAX; /* use max I/O size, 52K */ |
| 1216 | # else |
| 1217 | size = 0x10000L; /* use buffer >= 64K */ |
| 1218 | # endif |
| 1219 | #else |
| 1220 | size = 0x7ff0L - linerest; /* limit buffer to 32K */ |
| 1221 | #endif |
| 1222 | |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 1223 | for ( ; size >= 10; size = (long)((long_u)size >> 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1224 | { |
| 1225 | if ((new_buffer = lalloc((long_u)(size + linerest + 1), |
| 1226 | FALSE)) != NULL) |
| 1227 | break; |
| 1228 | } |
| 1229 | if (new_buffer == NULL) |
| 1230 | { |
| 1231 | do_outofmem_msg((long_u)(size * 2 + linerest + 1)); |
| 1232 | error = TRUE; |
| 1233 | break; |
| 1234 | } |
| 1235 | if (linerest) /* copy characters from the previous buffer */ |
| 1236 | mch_memmove(new_buffer, ptr - linerest, (size_t)linerest); |
| 1237 | vim_free(buffer); |
| 1238 | buffer = new_buffer; |
| 1239 | ptr = buffer + linerest; |
| 1240 | line_start = buffer; |
| 1241 | |
| 1242 | #ifdef FEAT_MBYTE |
| 1243 | /* May need room to translate into. |
| 1244 | * For iconv() we don't really know the required space, use a |
| 1245 | * factor ICONV_MULT. |
| 1246 | * latin1 to utf-8: 1 byte becomes up to 2 bytes |
| 1247 | * utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes |
| 1248 | * become up to 4 bytes, size must be multiple of 2 |
| 1249 | * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be |
| 1250 | * multiple of 2 |
| 1251 | * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be |
| 1252 | * multiple of 4 */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1253 | real_size = (int)size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1254 | # ifdef USE_ICONV |
| 1255 | if (iconv_fd != (iconv_t)-1) |
| 1256 | size = size / ICONV_MULT; |
| 1257 | else |
| 1258 | # endif |
| 1259 | if (fio_flags & FIO_LATIN1) |
| 1260 | size = size / 2; |
| 1261 | else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) |
| 1262 | size = (size * 2 / 3) & ~1; |
| 1263 | else if (fio_flags & FIO_UCS4) |
| 1264 | size = (size * 2 / 3) & ~3; |
| 1265 | else if (fio_flags == FIO_UCSBOM) |
| 1266 | size = size / ICONV_MULT; /* worst case */ |
| 1267 | # ifdef WIN3264 |
| 1268 | else if (fio_flags & FIO_CODEPAGE) |
| 1269 | size = size / ICONV_MULT; /* also worst case */ |
| 1270 | # endif |
| 1271 | # ifdef MACOS_X |
| 1272 | else if (fio_flags & FIO_MACROMAN) |
| 1273 | size = size / ICONV_MULT; /* also worst case */ |
| 1274 | # endif |
| 1275 | #endif |
| 1276 | |
| 1277 | #ifdef FEAT_MBYTE |
| 1278 | if (conv_restlen > 0) |
| 1279 | { |
| 1280 | /* Insert unconverted bytes from previous line. */ |
| 1281 | mch_memmove(ptr, conv_rest, conv_restlen); |
| 1282 | ptr += conv_restlen; |
| 1283 | size -= conv_restlen; |
| 1284 | } |
| 1285 | #endif |
| 1286 | |
| 1287 | if (read_buffer) |
| 1288 | { |
| 1289 | /* |
| 1290 | * Read bytes from curbuf. Used for converting text read |
| 1291 | * from stdin. |
| 1292 | */ |
| 1293 | if (read_buf_lnum > from) |
| 1294 | size = 0; |
| 1295 | else |
| 1296 | { |
| 1297 | int n, ni; |
| 1298 | long tlen; |
| 1299 | |
| 1300 | tlen = 0; |
| 1301 | for (;;) |
| 1302 | { |
| 1303 | p = ml_get(read_buf_lnum) + read_buf_col; |
| 1304 | n = (int)STRLEN(p); |
| 1305 | if ((int)tlen + n + 1 > size) |
| 1306 | { |
| 1307 | /* Filled up to "size", append partial line. |
| 1308 | * Change NL to NUL to reverse the effect done |
| 1309 | * below. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1310 | n = (int)(size - tlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1311 | for (ni = 0; ni < n; ++ni) |
| 1312 | { |
| 1313 | if (p[ni] == NL) |
| 1314 | ptr[tlen++] = NUL; |
| 1315 | else |
| 1316 | ptr[tlen++] = p[ni]; |
| 1317 | } |
| 1318 | read_buf_col += n; |
| 1319 | break; |
| 1320 | } |
| 1321 | else |
| 1322 | { |
| 1323 | /* Append whole line and new-line. Change NL |
| 1324 | * to NUL to reverse the effect done below. */ |
| 1325 | for (ni = 0; ni < n; ++ni) |
| 1326 | { |
| 1327 | if (p[ni] == NL) |
| 1328 | ptr[tlen++] = NUL; |
| 1329 | else |
| 1330 | ptr[tlen++] = p[ni]; |
| 1331 | } |
| 1332 | ptr[tlen++] = NL; |
| 1333 | read_buf_col = 0; |
| 1334 | if (++read_buf_lnum > from) |
| 1335 | { |
| 1336 | /* When the last line didn't have an |
| 1337 | * end-of-line don't add it now either. */ |
| 1338 | if (!curbuf->b_p_eol) |
| 1339 | --tlen; |
| 1340 | size = tlen; |
| 1341 | break; |
| 1342 | } |
| 1343 | } |
| 1344 | } |
| 1345 | } |
| 1346 | } |
| 1347 | else |
| 1348 | { |
| 1349 | /* |
| 1350 | * Read bytes from the file. |
| 1351 | */ |
| 1352 | size = vim_read(fd, ptr, size); |
| 1353 | } |
| 1354 | |
| 1355 | if (size <= 0) |
| 1356 | { |
| 1357 | if (size < 0) /* read error */ |
| 1358 | error = TRUE; |
| 1359 | #ifdef FEAT_MBYTE |
| 1360 | else if (conv_restlen > 0) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1361 | { |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1362 | /* |
| 1363 | * Reached end-of-file but some trailing bytes could |
| 1364 | * not be converted. Truncated file? |
| 1365 | */ |
| 1366 | |
| 1367 | /* When we did a conversion report an error. */ |
| 1368 | if (fio_flags != 0 |
| 1369 | # ifdef USE_ICONV |
| 1370 | || iconv_fd != (iconv_t)-1 |
| 1371 | # endif |
| 1372 | ) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1373 | { |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1374 | if (conv_error == 0) |
| 1375 | conv_error = curbuf->b_ml.ml_line_count |
| 1376 | - linecnt + 1; |
| 1377 | } |
| 1378 | /* Remember the first linenr with an illegal byte */ |
| 1379 | else if (illegal_byte == 0) |
| 1380 | illegal_byte = curbuf->b_ml.ml_line_count |
| 1381 | - linecnt + 1; |
| 1382 | if (bad_char_behavior == BAD_DROP) |
| 1383 | { |
| 1384 | *(ptr - conv_restlen) = NUL; |
| 1385 | conv_restlen = 0; |
| 1386 | } |
| 1387 | else |
| 1388 | { |
| 1389 | /* Replace the trailing bytes with the replacement |
| 1390 | * character if we were converting; if we weren't, |
| 1391 | * leave the UTF8 checking code to do it, as it |
| 1392 | * works slightly differently. */ |
| 1393 | if (bad_char_behavior != BAD_KEEP && (fio_flags != 0 |
| 1394 | # ifdef USE_ICONV |
| 1395 | || iconv_fd != (iconv_t)-1 |
| 1396 | # endif |
| 1397 | )) |
| 1398 | { |
| 1399 | while (conv_restlen > 0) |
| 1400 | { |
| 1401 | *(--ptr) = bad_char_behavior; |
| 1402 | --conv_restlen; |
| 1403 | } |
| 1404 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1405 | fio_flags = 0; /* don't convert this */ |
Bram Moolenaar | b21e584 | 2006-04-16 18:30:08 +0000 | [diff] [blame] | 1406 | # ifdef USE_ICONV |
| 1407 | if (iconv_fd != (iconv_t)-1) |
| 1408 | { |
| 1409 | iconv_close(iconv_fd); |
| 1410 | iconv_fd = (iconv_t)-1; |
| 1411 | } |
| 1412 | # endif |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1413 | } |
| 1414 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1415 | #endif |
| 1416 | } |
| 1417 | |
| 1418 | #ifdef FEAT_CRYPT |
| 1419 | /* |
| 1420 | * At start of file: Check for magic number of encryption. |
| 1421 | */ |
| 1422 | if (filesize == 0) |
| 1423 | cryptkey = check_for_cryptkey(cryptkey, ptr, &size, |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 1424 | &filesize, newfile, &did_ask_for_key); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1425 | /* |
| 1426 | * Decrypt the read bytes. |
| 1427 | */ |
| 1428 | if (cryptkey != NULL && size > 0) |
| 1429 | for (p = ptr; p < ptr + size; ++p) |
| 1430 | ZDECODE(*p); |
| 1431 | #endif |
| 1432 | } |
| 1433 | skip_read = FALSE; |
| 1434 | |
| 1435 | #ifdef FEAT_MBYTE |
| 1436 | /* |
| 1437 | * At start of file (or after crypt magic number): Check for BOM. |
| 1438 | * Also check for a BOM for other Unicode encodings, but not after |
| 1439 | * converting with 'charconvert' or when a BOM has already been |
| 1440 | * found. |
| 1441 | */ |
| 1442 | if ((filesize == 0 |
| 1443 | # ifdef FEAT_CRYPT |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 1444 | || (filesize == (CRYPT_MAGIC_LEN |
| 1445 | + crypt_seed_len[use_crypt_method]) |
| 1446 | && cryptkey != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1447 | # endif |
| 1448 | ) |
| 1449 | && (fio_flags == FIO_UCSBOM |
| 1450 | || (!curbuf->b_p_bomb |
| 1451 | && tmpname == NULL |
| 1452 | && (*fenc == 'u' || (*fenc == NUL && enc_utf8))))) |
| 1453 | { |
| 1454 | char_u *ccname; |
| 1455 | int blen; |
| 1456 | |
| 1457 | /* no BOM detection in a short file or in binary mode */ |
| 1458 | if (size < 2 || curbuf->b_p_bin) |
| 1459 | ccname = NULL; |
| 1460 | else |
| 1461 | ccname = check_for_bom(ptr, size, &blen, |
| 1462 | fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc)); |
| 1463 | if (ccname != NULL) |
| 1464 | { |
| 1465 | /* Remove BOM from the text */ |
| 1466 | filesize += blen; |
| 1467 | size -= blen; |
| 1468 | mch_memmove(ptr, ptr + blen, (size_t)size); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 1469 | if (set_options) |
Bram Moolenaar | 83eb885 | 2007-08-12 13:51:26 +0000 | [diff] [blame] | 1470 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1471 | curbuf->b_p_bomb = TRUE; |
Bram Moolenaar | 83eb885 | 2007-08-12 13:51:26 +0000 | [diff] [blame] | 1472 | curbuf->b_start_bomb = TRUE; |
| 1473 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | if (fio_flags == FIO_UCSBOM) |
| 1477 | { |
| 1478 | if (ccname == NULL) |
| 1479 | { |
| 1480 | /* No BOM detected: retry with next encoding. */ |
| 1481 | advance_fenc = TRUE; |
| 1482 | } |
| 1483 | else |
| 1484 | { |
| 1485 | /* BOM detected: set "fenc" and jump back */ |
| 1486 | if (fenc_alloced) |
| 1487 | vim_free(fenc); |
| 1488 | fenc = ccname; |
| 1489 | fenc_alloced = FALSE; |
| 1490 | } |
| 1491 | /* retry reading without getting new bytes or rewinding */ |
| 1492 | skip_read = TRUE; |
| 1493 | goto retry; |
| 1494 | } |
| 1495 | } |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1496 | |
| 1497 | /* Include not converted bytes. */ |
| 1498 | ptr -= conv_restlen; |
| 1499 | size += conv_restlen; |
| 1500 | conv_restlen = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1501 | #endif |
| 1502 | /* |
| 1503 | * Break here for a read error or end-of-file. |
| 1504 | */ |
| 1505 | if (size <= 0) |
| 1506 | break; |
| 1507 | |
| 1508 | #ifdef FEAT_MBYTE |
| 1509 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1510 | # ifdef USE_ICONV |
| 1511 | if (iconv_fd != (iconv_t)-1) |
| 1512 | { |
| 1513 | /* |
| 1514 | * Attempt conversion of the read bytes to 'encoding' using |
| 1515 | * iconv(). |
| 1516 | */ |
| 1517 | const char *fromp; |
| 1518 | char *top; |
| 1519 | size_t from_size; |
| 1520 | size_t to_size; |
| 1521 | |
| 1522 | fromp = (char *)ptr; |
| 1523 | from_size = size; |
| 1524 | ptr += size; |
| 1525 | top = (char *)ptr; |
| 1526 | to_size = real_size - size; |
| 1527 | |
| 1528 | /* |
| 1529 | * If there is conversion error or not enough room try using |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 1530 | * another conversion. Except for when there is no |
| 1531 | * alternative (help files). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1532 | */ |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 1533 | while ((iconv(iconv_fd, (void *)&fromp, &from_size, |
| 1534 | &top, &to_size) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1535 | == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) |
| 1536 | || from_size > CONV_RESTLEN) |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 1537 | { |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1538 | if (can_retry) |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 1539 | goto rewind_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1540 | if (conv_error == 0) |
| 1541 | conv_error = readfile_linenr(linecnt, |
| 1542 | ptr, (char_u *)top); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1543 | |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1544 | /* Deal with a bad byte and continue with the next. */ |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 1545 | ++fromp; |
| 1546 | --from_size; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1547 | if (bad_char_behavior == BAD_KEEP) |
| 1548 | { |
| 1549 | *top++ = *(fromp - 1); |
| 1550 | --to_size; |
| 1551 | } |
| 1552 | else if (bad_char_behavior != BAD_DROP) |
| 1553 | { |
| 1554 | *top++ = bad_char_behavior; |
| 1555 | --to_size; |
| 1556 | } |
Bram Moolenaar | 8cd06ca | 2005-02-28 22:44:58 +0000 | [diff] [blame] | 1557 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1558 | |
| 1559 | if (from_size > 0) |
| 1560 | { |
| 1561 | /* Some remaining characters, keep them for the next |
| 1562 | * round. */ |
| 1563 | mch_memmove(conv_rest, (char_u *)fromp, from_size); |
| 1564 | conv_restlen = (int)from_size; |
| 1565 | } |
| 1566 | |
| 1567 | /* move the linerest to before the converted characters */ |
| 1568 | line_start = ptr - linerest; |
| 1569 | mch_memmove(line_start, buffer, (size_t)linerest); |
| 1570 | size = (long)((char_u *)top - ptr); |
| 1571 | } |
| 1572 | # endif |
| 1573 | |
| 1574 | # ifdef WIN3264 |
| 1575 | if (fio_flags & FIO_CODEPAGE) |
| 1576 | { |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1577 | char_u *src, *dst; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1578 | WCHAR ucs2buf[3]; |
| 1579 | int ucs2len; |
| 1580 | int codepage = FIO_GET_CP(fio_flags); |
| 1581 | int bytelen; |
| 1582 | int found_bad; |
| 1583 | char replstr[2]; |
| 1584 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1585 | /* |
| 1586 | * 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] | 1587 | * a codepage, using standard MS-Windows functions. This |
| 1588 | * requires two steps: |
| 1589 | * 1. convert from 'fileencoding' to ucs-2 |
| 1590 | * 2. convert from ucs-2 to 'encoding' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1591 | * |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1592 | * Because there may be illegal bytes AND an incomplete byte |
| 1593 | * sequence at the end, we may have to do the conversion one |
| 1594 | * character at a time to get it right. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1595 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1596 | |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1597 | /* Replacement string for WideCharToMultiByte(). */ |
| 1598 | if (bad_char_behavior > 0) |
| 1599 | replstr[0] = bad_char_behavior; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1600 | else |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1601 | replstr[0] = '?'; |
| 1602 | replstr[1] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1603 | |
| 1604 | /* |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1605 | * Move the bytes to the end of the buffer, so that we have |
| 1606 | * room to put the result at the start. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1607 | */ |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1608 | src = ptr + real_size - size; |
| 1609 | mch_memmove(src, ptr, size); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1610 | |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1611 | /* |
| 1612 | * Do the conversion. |
| 1613 | */ |
| 1614 | dst = ptr; |
| 1615 | size = size; |
| 1616 | while (size > 0) |
| 1617 | { |
| 1618 | found_bad = FALSE; |
| 1619 | |
| 1620 | # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ |
| 1621 | if (codepage == CP_UTF8) |
| 1622 | { |
| 1623 | /* Handle CP_UTF8 input ourselves to be able to handle |
| 1624 | * trailing bytes properly. |
| 1625 | * Get one UTF-8 character from src. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1626 | bytelen = (int)utf_ptr2len_len(src, size); |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1627 | if (bytelen > size) |
| 1628 | { |
| 1629 | /* Only got some bytes of a character. Normally |
| 1630 | * it's put in "conv_rest", but if it's too long |
| 1631 | * deal with it as if they were illegal bytes. */ |
| 1632 | if (bytelen <= CONV_RESTLEN) |
| 1633 | break; |
| 1634 | |
| 1635 | /* weird overlong byte sequence */ |
| 1636 | bytelen = size; |
| 1637 | found_bad = TRUE; |
| 1638 | } |
| 1639 | else |
| 1640 | { |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 1641 | int u8c = utf_ptr2char(src); |
| 1642 | |
Bram Moolenaar | 86e0108 | 2005-12-29 22:45:34 +0000 | [diff] [blame] | 1643 | if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1)) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1644 | found_bad = TRUE; |
| 1645 | ucs2buf[0] = u8c; |
| 1646 | ucs2len = 1; |
| 1647 | } |
| 1648 | } |
| 1649 | else |
| 1650 | # endif |
| 1651 | { |
| 1652 | /* We don't know how long the byte sequence is, try |
| 1653 | * from one to three bytes. */ |
| 1654 | for (bytelen = 1; bytelen <= size && bytelen <= 3; |
| 1655 | ++bytelen) |
| 1656 | { |
| 1657 | ucs2len = MultiByteToWideChar(codepage, |
| 1658 | MB_ERR_INVALID_CHARS, |
| 1659 | (LPCSTR)src, bytelen, |
| 1660 | ucs2buf, 3); |
| 1661 | if (ucs2len > 0) |
| 1662 | break; |
| 1663 | } |
| 1664 | if (ucs2len == 0) |
| 1665 | { |
| 1666 | /* If we have only one byte then it's probably an |
| 1667 | * incomplete byte sequence. Otherwise discard |
| 1668 | * one byte as a bad character. */ |
| 1669 | if (size == 1) |
| 1670 | break; |
| 1671 | found_bad = TRUE; |
| 1672 | bytelen = 1; |
| 1673 | } |
| 1674 | } |
| 1675 | |
| 1676 | if (!found_bad) |
| 1677 | { |
| 1678 | int i; |
| 1679 | |
| 1680 | /* Convert "ucs2buf[ucs2len]" to 'enc' in "dst". */ |
| 1681 | if (enc_utf8) |
| 1682 | { |
| 1683 | /* From UCS-2 to UTF-8. Cannot fail. */ |
| 1684 | for (i = 0; i < ucs2len; ++i) |
| 1685 | dst += utf_char2bytes(ucs2buf[i], dst); |
| 1686 | } |
| 1687 | else |
| 1688 | { |
| 1689 | BOOL bad = FALSE; |
| 1690 | int dstlen; |
| 1691 | |
| 1692 | /* From UCS-2 to "enc_codepage". If the |
| 1693 | * conversion uses the default character "?", |
| 1694 | * the data doesn't fit in this encoding. */ |
| 1695 | dstlen = WideCharToMultiByte(enc_codepage, 0, |
| 1696 | (LPCWSTR)ucs2buf, ucs2len, |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1697 | (LPSTR)dst, (int)(src - dst), |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1698 | replstr, &bad); |
| 1699 | if (bad) |
| 1700 | found_bad = TRUE; |
| 1701 | else |
| 1702 | dst += dstlen; |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | if (found_bad) |
| 1707 | { |
| 1708 | /* Deal with bytes we can't convert. */ |
| 1709 | if (can_retry) |
| 1710 | goto rewind_retry; |
| 1711 | if (conv_error == 0) |
| 1712 | conv_error = readfile_linenr(linecnt, ptr, dst); |
| 1713 | if (bad_char_behavior != BAD_DROP) |
| 1714 | { |
| 1715 | if (bad_char_behavior == BAD_KEEP) |
| 1716 | { |
| 1717 | mch_memmove(dst, src, bytelen); |
| 1718 | dst += bytelen; |
| 1719 | } |
| 1720 | else |
| 1721 | *dst++ = bad_char_behavior; |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | src += bytelen; |
| 1726 | size -= bytelen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1727 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1728 | |
| 1729 | if (size > 0) |
| 1730 | { |
| 1731 | /* An incomplete byte sequence remaining. */ |
| 1732 | mch_memmove(conv_rest, src, size); |
| 1733 | conv_restlen = size; |
| 1734 | } |
| 1735 | |
| 1736 | /* The new size is equal to how much "dst" was advanced. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1737 | size = (long)(dst - ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1738 | } |
| 1739 | else |
| 1740 | # endif |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 1741 | # ifdef MACOS_CONVERT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1742 | if (fio_flags & FIO_MACROMAN) |
| 1743 | { |
| 1744 | /* |
| 1745 | * Conversion from Apple MacRoman char encoding to UTF-8 or |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 1746 | * latin1. This is in os_mac_conv.c. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1747 | */ |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 1748 | if (macroman2enc(ptr, &size, real_size) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1749 | goto rewind_retry; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1750 | } |
| 1751 | else |
| 1752 | # endif |
| 1753 | if (fio_flags != 0) |
| 1754 | { |
| 1755 | int u8c; |
| 1756 | char_u *dest; |
| 1757 | char_u *tail = NULL; |
| 1758 | |
| 1759 | /* |
| 1760 | * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8. |
| 1761 | * "enc_utf8" not set: Convert Unicode to Latin1. |
| 1762 | * Go from end to start through the buffer, because the number |
| 1763 | * of bytes may increase. |
| 1764 | * "dest" points to after where the UTF-8 bytes go, "p" points |
| 1765 | * to after the next character to convert. |
| 1766 | */ |
| 1767 | dest = ptr + real_size; |
| 1768 | if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8) |
| 1769 | { |
| 1770 | p = ptr + size; |
| 1771 | if (fio_flags == FIO_UTF8) |
| 1772 | { |
| 1773 | /* Check for a trailing incomplete UTF-8 sequence */ |
| 1774 | tail = ptr + size - 1; |
| 1775 | while (tail > ptr && (*tail & 0xc0) == 0x80) |
| 1776 | --tail; |
| 1777 | if (tail + utf_byte2len(*tail) <= ptr + size) |
| 1778 | tail = NULL; |
| 1779 | else |
| 1780 | p = tail; |
| 1781 | } |
| 1782 | } |
| 1783 | else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) |
| 1784 | { |
| 1785 | /* Check for a trailing byte */ |
| 1786 | p = ptr + (size & ~1); |
| 1787 | if (size & 1) |
| 1788 | tail = p; |
| 1789 | if ((fio_flags & FIO_UTF16) && p > ptr) |
| 1790 | { |
| 1791 | /* Check for a trailing leading word */ |
| 1792 | if (fio_flags & FIO_ENDIAN_L) |
| 1793 | { |
| 1794 | u8c = (*--p << 8); |
| 1795 | u8c += *--p; |
| 1796 | } |
| 1797 | else |
| 1798 | { |
| 1799 | u8c = *--p; |
| 1800 | u8c += (*--p << 8); |
| 1801 | } |
| 1802 | if (u8c >= 0xd800 && u8c <= 0xdbff) |
| 1803 | tail = p; |
| 1804 | else |
| 1805 | p += 2; |
| 1806 | } |
| 1807 | } |
| 1808 | else /* FIO_UCS4 */ |
| 1809 | { |
| 1810 | /* Check for trailing 1, 2 or 3 bytes */ |
| 1811 | p = ptr + (size & ~3); |
| 1812 | if (size & 3) |
| 1813 | tail = p; |
| 1814 | } |
| 1815 | |
| 1816 | /* If there is a trailing incomplete sequence move it to |
| 1817 | * conv_rest[]. */ |
| 1818 | if (tail != NULL) |
| 1819 | { |
| 1820 | conv_restlen = (int)((ptr + size) - tail); |
| 1821 | mch_memmove(conv_rest, (char_u *)tail, conv_restlen); |
| 1822 | size -= conv_restlen; |
| 1823 | } |
| 1824 | |
| 1825 | |
| 1826 | while (p > ptr) |
| 1827 | { |
| 1828 | if (fio_flags & FIO_LATIN1) |
| 1829 | u8c = *--p; |
| 1830 | else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) |
| 1831 | { |
| 1832 | if (fio_flags & FIO_ENDIAN_L) |
| 1833 | { |
| 1834 | u8c = (*--p << 8); |
| 1835 | u8c += *--p; |
| 1836 | } |
| 1837 | else |
| 1838 | { |
| 1839 | u8c = *--p; |
| 1840 | u8c += (*--p << 8); |
| 1841 | } |
| 1842 | if ((fio_flags & FIO_UTF16) |
| 1843 | && u8c >= 0xdc00 && u8c <= 0xdfff) |
| 1844 | { |
| 1845 | int u16c; |
| 1846 | |
| 1847 | if (p == ptr) |
| 1848 | { |
| 1849 | /* Missing leading word. */ |
| 1850 | if (can_retry) |
| 1851 | goto rewind_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1852 | if (conv_error == 0) |
| 1853 | conv_error = readfile_linenr(linecnt, |
| 1854 | ptr, p); |
| 1855 | if (bad_char_behavior == BAD_DROP) |
| 1856 | continue; |
| 1857 | if (bad_char_behavior != BAD_KEEP) |
| 1858 | u8c = bad_char_behavior; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | /* found second word of double-word, get the first |
| 1862 | * word and compute the resulting character */ |
| 1863 | if (fio_flags & FIO_ENDIAN_L) |
| 1864 | { |
| 1865 | u16c = (*--p << 8); |
| 1866 | u16c += *--p; |
| 1867 | } |
| 1868 | else |
| 1869 | { |
| 1870 | u16c = *--p; |
| 1871 | u16c += (*--p << 8); |
| 1872 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1873 | u8c = 0x10000 + ((u16c & 0x3ff) << 10) |
| 1874 | + (u8c & 0x3ff); |
| 1875 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1876 | /* Check if the word is indeed a leading word. */ |
| 1877 | if (u16c < 0xd800 || u16c > 0xdbff) |
| 1878 | { |
| 1879 | if (can_retry) |
| 1880 | goto rewind_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1881 | if (conv_error == 0) |
| 1882 | conv_error = readfile_linenr(linecnt, |
| 1883 | ptr, p); |
| 1884 | if (bad_char_behavior == BAD_DROP) |
| 1885 | continue; |
| 1886 | if (bad_char_behavior != BAD_KEEP) |
| 1887 | u8c = bad_char_behavior; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1888 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1889 | } |
| 1890 | } |
| 1891 | else if (fio_flags & FIO_UCS4) |
| 1892 | { |
| 1893 | if (fio_flags & FIO_ENDIAN_L) |
| 1894 | { |
| 1895 | u8c = (*--p << 24); |
| 1896 | u8c += (*--p << 16); |
| 1897 | u8c += (*--p << 8); |
| 1898 | u8c += *--p; |
| 1899 | } |
| 1900 | else /* big endian */ |
| 1901 | { |
| 1902 | u8c = *--p; |
| 1903 | u8c += (*--p << 8); |
| 1904 | u8c += (*--p << 16); |
| 1905 | u8c += (*--p << 24); |
| 1906 | } |
| 1907 | } |
| 1908 | else /* UTF-8 */ |
| 1909 | { |
| 1910 | if (*--p < 0x80) |
| 1911 | u8c = *p; |
| 1912 | else |
| 1913 | { |
| 1914 | len = utf_head_off(ptr, p); |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1915 | p -= len; |
| 1916 | u8c = utf_ptr2char(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1917 | if (len == 0) |
| 1918 | { |
| 1919 | /* Not a valid UTF-8 character, retry with |
| 1920 | * another fenc when possible, otherwise just |
| 1921 | * report the error. */ |
| 1922 | if (can_retry) |
| 1923 | goto rewind_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1924 | if (conv_error == 0) |
| 1925 | conv_error = readfile_linenr(linecnt, |
| 1926 | ptr, p); |
| 1927 | if (bad_char_behavior == BAD_DROP) |
| 1928 | continue; |
| 1929 | if (bad_char_behavior != BAD_KEEP) |
| 1930 | u8c = bad_char_behavior; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1931 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1932 | } |
| 1933 | } |
| 1934 | if (enc_utf8) /* produce UTF-8 */ |
| 1935 | { |
| 1936 | dest -= utf_char2len(u8c); |
| 1937 | (void)utf_char2bytes(u8c, dest); |
| 1938 | } |
| 1939 | else /* produce Latin1 */ |
| 1940 | { |
| 1941 | --dest; |
| 1942 | if (u8c >= 0x100) |
| 1943 | { |
| 1944 | /* character doesn't fit in latin1, retry with |
| 1945 | * another fenc when possible, otherwise just |
| 1946 | * report the error. */ |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1947 | if (can_retry) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1948 | goto rewind_retry; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1949 | if (conv_error == 0) |
| 1950 | conv_error = readfile_linenr(linecnt, ptr, p); |
| 1951 | if (bad_char_behavior == BAD_DROP) |
| 1952 | ++dest; |
| 1953 | else if (bad_char_behavior == BAD_KEEP) |
| 1954 | *dest = u8c; |
| 1955 | else if (eap != NULL && eap->bad_char != 0) |
| 1956 | *dest = bad_char_behavior; |
| 1957 | else |
| 1958 | *dest = 0xBF; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1959 | } |
| 1960 | else |
| 1961 | *dest = u8c; |
| 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | /* move the linerest to before the converted characters */ |
| 1966 | line_start = dest - linerest; |
| 1967 | mch_memmove(line_start, buffer, (size_t)linerest); |
| 1968 | size = (long)((ptr + real_size) - dest); |
| 1969 | ptr = dest; |
| 1970 | } |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1971 | else if (enc_utf8 && !curbuf->b_p_bin) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1972 | { |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1973 | int incomplete_tail = FALSE; |
| 1974 | |
| 1975 | /* Reading UTF-8: Check if the bytes are valid UTF-8. */ |
| 1976 | for (p = ptr; ; ++p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1977 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1978 | int todo = (int)((ptr + size) - p); |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1979 | int l; |
| 1980 | |
| 1981 | if (todo <= 0) |
| 1982 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1983 | if (*p >= 0x80) |
| 1984 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1985 | /* A length of 1 means it's an illegal byte. Accept |
| 1986 | * an incomplete character at the end though, the next |
| 1987 | * read() will get the next bytes, we'll check it |
| 1988 | * then. */ |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 1989 | l = utf_ptr2len_len(p, todo); |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1990 | if (l > todo && !incomplete_tail) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1991 | { |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 1992 | /* Avoid retrying with a different encoding when |
| 1993 | * a truncated file is more likely, or attempting |
| 1994 | * to read the rest of an incomplete sequence when |
| 1995 | * we have already done so. */ |
| 1996 | if (p > ptr || filesize > 0) |
| 1997 | incomplete_tail = TRUE; |
| 1998 | /* Incomplete byte sequence, move it to conv_rest[] |
| 1999 | * and try to read the rest of it, unless we've |
| 2000 | * already done so. */ |
| 2001 | if (p > ptr) |
| 2002 | { |
| 2003 | conv_restlen = todo; |
| 2004 | mch_memmove(conv_rest, p, conv_restlen); |
| 2005 | size -= conv_restlen; |
| 2006 | break; |
| 2007 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2008 | } |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2009 | if (l == 1 || l > todo) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2010 | { |
| 2011 | /* Illegal byte. If we can try another encoding |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2012 | * do that, unless at EOF where a truncated |
| 2013 | * file is more likely than a conversion error. */ |
| 2014 | if (can_retry && !incomplete_tail) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2015 | break; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2016 | # ifdef USE_ICONV |
| 2017 | /* When we did a conversion report an error. */ |
| 2018 | if (iconv_fd != (iconv_t)-1 && conv_error == 0) |
| 2019 | conv_error = readfile_linenr(linecnt, ptr, p); |
| 2020 | # endif |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2021 | /* Remember the first linenr with an illegal byte */ |
| 2022 | if (conv_error == 0 && illegal_byte == 0) |
| 2023 | illegal_byte = readfile_linenr(linecnt, ptr, p); |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2024 | |
| 2025 | /* Drop, keep or replace the bad byte. */ |
| 2026 | if (bad_char_behavior == BAD_DROP) |
| 2027 | { |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2028 | mch_memmove(p, p + 1, todo - 1); |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2029 | --p; |
| 2030 | --size; |
| 2031 | } |
| 2032 | else if (bad_char_behavior != BAD_KEEP) |
| 2033 | *p = bad_char_behavior; |
| 2034 | } |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2035 | else |
| 2036 | p += l - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2037 | } |
| 2038 | } |
Bram Moolenaar | f453d35 | 2008-06-04 17:37:34 +0000 | [diff] [blame] | 2039 | if (p < ptr + size && !incomplete_tail) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2040 | { |
| 2041 | /* Detected a UTF-8 error. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2042 | rewind_retry: |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2043 | /* Retry reading with another conversion. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2044 | # if defined(FEAT_EVAL) && defined(USE_ICONV) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2045 | if (*p_ccv != NUL && iconv_fd != (iconv_t)-1) |
| 2046 | /* iconv() failed, try 'charconvert' */ |
| 2047 | did_iconv = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2048 | else |
| 2049 | # endif |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2050 | /* use next item from 'fileencodings' */ |
| 2051 | advance_fenc = TRUE; |
| 2052 | file_rewind = TRUE; |
| 2053 | goto retry; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2054 | } |
| 2055 | } |
| 2056 | #endif |
| 2057 | |
| 2058 | /* count the number of characters (after conversion!) */ |
| 2059 | filesize += size; |
| 2060 | |
| 2061 | /* |
| 2062 | * when reading the first part of a file: guess EOL type |
| 2063 | */ |
| 2064 | if (fileformat == EOL_UNKNOWN) |
| 2065 | { |
| 2066 | /* First try finding a NL, for Dos and Unix */ |
| 2067 | if (try_dos || try_unix) |
| 2068 | { |
| 2069 | for (p = ptr; p < ptr + size; ++p) |
| 2070 | { |
| 2071 | if (*p == NL) |
| 2072 | { |
| 2073 | if (!try_unix |
| 2074 | || (try_dos && p > ptr && p[-1] == CAR)) |
| 2075 | fileformat = EOL_DOS; |
| 2076 | else |
| 2077 | fileformat = EOL_UNIX; |
| 2078 | break; |
| 2079 | } |
| 2080 | } |
| 2081 | |
| 2082 | /* Don't give in to EOL_UNIX if EOL_MAC is more likely */ |
| 2083 | if (fileformat == EOL_UNIX && try_mac) |
| 2084 | { |
| 2085 | /* Need to reset the counters when retrying fenc. */ |
| 2086 | try_mac = 1; |
| 2087 | try_unix = 1; |
| 2088 | for (; p >= ptr && *p != CAR; p--) |
| 2089 | ; |
| 2090 | if (p >= ptr) |
| 2091 | { |
| 2092 | for (p = ptr; p < ptr + size; ++p) |
| 2093 | { |
| 2094 | if (*p == NL) |
| 2095 | try_unix++; |
| 2096 | else if (*p == CAR) |
| 2097 | try_mac++; |
| 2098 | } |
| 2099 | if (try_mac > try_unix) |
| 2100 | fileformat = EOL_MAC; |
| 2101 | } |
| 2102 | } |
| 2103 | } |
| 2104 | |
| 2105 | /* No NL found: may use Mac format */ |
| 2106 | if (fileformat == EOL_UNKNOWN && try_mac) |
| 2107 | fileformat = EOL_MAC; |
| 2108 | |
| 2109 | /* Still nothing found? Use first format in 'ffs' */ |
| 2110 | if (fileformat == EOL_UNKNOWN) |
| 2111 | fileformat = default_fileformat(); |
| 2112 | |
| 2113 | /* if editing a new file: may set p_tx and p_ff */ |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2114 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2115 | set_fileformat(fileformat, OPT_LOCAL); |
| 2116 | } |
| 2117 | } |
| 2118 | |
| 2119 | /* |
| 2120 | * This loop is executed once for every character read. |
| 2121 | * Keep it fast! |
| 2122 | */ |
| 2123 | if (fileformat == EOL_MAC) |
| 2124 | { |
| 2125 | --ptr; |
| 2126 | while (++ptr, --size >= 0) |
| 2127 | { |
| 2128 | /* catch most common case first */ |
| 2129 | if ((c = *ptr) != NUL && c != CAR && c != NL) |
| 2130 | continue; |
| 2131 | if (c == NUL) |
| 2132 | *ptr = NL; /* NULs are replaced by newlines! */ |
| 2133 | else if (c == NL) |
| 2134 | *ptr = CAR; /* NLs are replaced by CRs! */ |
| 2135 | else |
| 2136 | { |
| 2137 | if (skip_count == 0) |
| 2138 | { |
| 2139 | *ptr = NUL; /* end of line */ |
| 2140 | len = (colnr_T) (ptr - line_start + 1); |
| 2141 | if (ml_append(lnum, line_start, len, newfile) == FAIL) |
| 2142 | { |
| 2143 | error = TRUE; |
| 2144 | break; |
| 2145 | } |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2146 | #ifdef FEAT_PERSISTENT_UNDO |
| 2147 | if (read_undo_file) |
| 2148 | sha256_update(&sha_ctx, line_start, len); |
| 2149 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2150 | ++lnum; |
| 2151 | if (--read_count == 0) |
| 2152 | { |
| 2153 | error = TRUE; /* break loop */ |
| 2154 | line_start = ptr; /* nothing left to write */ |
| 2155 | break; |
| 2156 | } |
| 2157 | } |
| 2158 | else |
| 2159 | --skip_count; |
| 2160 | line_start = ptr + 1; |
| 2161 | } |
| 2162 | } |
| 2163 | } |
| 2164 | else |
| 2165 | { |
| 2166 | --ptr; |
| 2167 | while (++ptr, --size >= 0) |
| 2168 | { |
| 2169 | if ((c = *ptr) != NUL && c != NL) /* catch most common case */ |
| 2170 | continue; |
| 2171 | if (c == NUL) |
| 2172 | *ptr = NL; /* NULs are replaced by newlines! */ |
| 2173 | else |
| 2174 | { |
| 2175 | if (skip_count == 0) |
| 2176 | { |
| 2177 | *ptr = NUL; /* end of line */ |
| 2178 | len = (colnr_T)(ptr - line_start + 1); |
| 2179 | if (fileformat == EOL_DOS) |
| 2180 | { |
| 2181 | if (ptr[-1] == CAR) /* remove CR */ |
| 2182 | { |
| 2183 | ptr[-1] = NUL; |
| 2184 | --len; |
| 2185 | } |
| 2186 | /* |
| 2187 | * Reading in Dos format, but no CR-LF found! |
| 2188 | * When 'fileformats' includes "unix", delete all |
| 2189 | * the lines read so far and start all over again. |
| 2190 | * Otherwise give an error message later. |
| 2191 | */ |
| 2192 | else if (ff_error != EOL_DOS) |
| 2193 | { |
| 2194 | if ( try_unix |
| 2195 | && !read_stdin |
| 2196 | && (read_buffer |
| 2197 | || lseek(fd, (off_t)0L, SEEK_SET) == 0)) |
| 2198 | { |
| 2199 | fileformat = EOL_UNIX; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2200 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2201 | set_fileformat(EOL_UNIX, OPT_LOCAL); |
| 2202 | file_rewind = TRUE; |
| 2203 | keep_fileformat = TRUE; |
| 2204 | goto retry; |
| 2205 | } |
| 2206 | ff_error = EOL_DOS; |
| 2207 | } |
| 2208 | } |
| 2209 | if (ml_append(lnum, line_start, len, newfile) == FAIL) |
| 2210 | { |
| 2211 | error = TRUE; |
| 2212 | break; |
| 2213 | } |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2214 | #ifdef FEAT_PERSISTENT_UNDO |
| 2215 | if (read_undo_file) |
| 2216 | sha256_update(&sha_ctx, line_start, len); |
| 2217 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2218 | ++lnum; |
| 2219 | if (--read_count == 0) |
| 2220 | { |
| 2221 | error = TRUE; /* break loop */ |
| 2222 | line_start = ptr; /* nothing left to write */ |
| 2223 | break; |
| 2224 | } |
| 2225 | } |
| 2226 | else |
| 2227 | --skip_count; |
| 2228 | line_start = ptr + 1; |
| 2229 | } |
| 2230 | } |
| 2231 | } |
| 2232 | linerest = (long)(ptr - line_start); |
| 2233 | ui_breakcheck(); |
| 2234 | } |
| 2235 | |
| 2236 | failed: |
| 2237 | /* not an error, max. number of lines reached */ |
| 2238 | if (error && read_count == 0) |
| 2239 | error = FALSE; |
| 2240 | |
| 2241 | /* |
| 2242 | * If we get EOF in the middle of a line, note the fact and |
| 2243 | * complete the line ourselves. |
| 2244 | * In Dos format ignore a trailing CTRL-Z, unless 'binary' set. |
| 2245 | */ |
| 2246 | if (!error |
| 2247 | && !got_int |
| 2248 | && linerest != 0 |
| 2249 | && !(!curbuf->b_p_bin |
| 2250 | && fileformat == EOL_DOS |
| 2251 | && *line_start == Ctrl_Z |
| 2252 | && ptr == line_start + 1)) |
| 2253 | { |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2254 | /* remember for when writing */ |
| 2255 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2256 | curbuf->b_p_eol = FALSE; |
| 2257 | *ptr = NUL; |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2258 | len = (colnr_T)(ptr - line_start + 1); |
| 2259 | if (ml_append(lnum, line_start, len, newfile) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2260 | error = TRUE; |
| 2261 | else |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2262 | { |
| 2263 | #ifdef FEAT_PERSISTENT_UNDO |
| 2264 | if (read_undo_file) |
| 2265 | sha256_update(&sha_ctx, line_start, len); |
| 2266 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2267 | read_no_eol_lnum = ++lnum; |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2268 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2269 | } |
| 2270 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2271 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2272 | save_file_ff(curbuf); /* remember the current file format */ |
| 2273 | |
| 2274 | #ifdef FEAT_CRYPT |
| 2275 | if (cryptkey != curbuf->b_p_key) |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2276 | free_crypt_key(cryptkey); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2277 | #endif |
| 2278 | |
| 2279 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2280 | /* If editing a new file: set 'fenc' for the current buffer. |
| 2281 | * Also for ":read ++edit file". */ |
| 2282 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2283 | set_string_option_direct((char_u *)"fenc", -1, fenc, |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 2284 | OPT_FREE|OPT_LOCAL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2285 | if (fenc_alloced) |
| 2286 | vim_free(fenc); |
| 2287 | # ifdef USE_ICONV |
| 2288 | if (iconv_fd != (iconv_t)-1) |
| 2289 | { |
| 2290 | iconv_close(iconv_fd); |
| 2291 | iconv_fd = (iconv_t)-1; |
| 2292 | } |
| 2293 | # endif |
| 2294 | #endif |
| 2295 | |
| 2296 | if (!read_buffer && !read_stdin) |
| 2297 | close(fd); /* errors are ignored */ |
Bram Moolenaar | f05da21 | 2009-11-17 16:13:15 +0000 | [diff] [blame] | 2298 | #ifdef HAVE_FD_CLOEXEC |
| 2299 | else |
| 2300 | { |
| 2301 | int fdflags = fcntl(fd, F_GETFD); |
| 2302 | if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0) |
| 2303 | fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC); |
| 2304 | } |
| 2305 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2306 | vim_free(buffer); |
| 2307 | |
| 2308 | #ifdef HAVE_DUP |
| 2309 | if (read_stdin) |
| 2310 | { |
| 2311 | /* Use stderr for stdin, makes shell commands work. */ |
| 2312 | close(0); |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 2313 | ignored = dup(2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2314 | } |
| 2315 | #endif |
| 2316 | |
| 2317 | #ifdef FEAT_MBYTE |
| 2318 | if (tmpname != NULL) |
| 2319 | { |
| 2320 | mch_remove(tmpname); /* delete converted file */ |
| 2321 | vim_free(tmpname); |
| 2322 | } |
| 2323 | #endif |
| 2324 | --no_wait_return; /* may wait for return now */ |
| 2325 | |
| 2326 | /* |
| 2327 | * In recovery mode everything but autocommands is skipped. |
| 2328 | */ |
| 2329 | if (!recoverymode) |
| 2330 | { |
| 2331 | /* need to delete the last line, which comes from the empty buffer */ |
| 2332 | if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY)) |
| 2333 | { |
| 2334 | #ifdef FEAT_NETBEANS_INTG |
| 2335 | netbeansFireChanges = 0; |
| 2336 | #endif |
| 2337 | ml_delete(curbuf->b_ml.ml_line_count, FALSE); |
| 2338 | #ifdef FEAT_NETBEANS_INTG |
| 2339 | netbeansFireChanges = 1; |
| 2340 | #endif |
| 2341 | --linecnt; |
| 2342 | } |
| 2343 | linecnt = curbuf->b_ml.ml_line_count - linecnt; |
| 2344 | if (filesize == 0) |
| 2345 | linecnt = 0; |
| 2346 | if (newfile || read_buffer) |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 2347 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2348 | redraw_curbuf_later(NOT_VALID); |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 2349 | #ifdef FEAT_DIFF |
| 2350 | /* After reading the text into the buffer the diff info needs to |
| 2351 | * be updated. */ |
| 2352 | diff_invalidate(curbuf); |
| 2353 | #endif |
| 2354 | #ifdef FEAT_FOLDING |
| 2355 | /* All folds in the window are invalid now. Mark them for update |
| 2356 | * before triggering autocommands. */ |
| 2357 | foldUpdateAll(curwin); |
| 2358 | #endif |
| 2359 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2360 | else if (linecnt) /* appended at least one line */ |
| 2361 | appended_lines_mark(from, linecnt); |
| 2362 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2363 | #ifndef ALWAYS_USE_GUI |
| 2364 | /* |
| 2365 | * If we were reading from the same terminal as where messages go, |
| 2366 | * the screen will have been messed up. |
| 2367 | * Switch on raw mode now and clear the screen. |
| 2368 | */ |
| 2369 | if (read_stdin) |
| 2370 | { |
| 2371 | settmode(TMODE_RAW); /* set to raw mode */ |
| 2372 | starttermcap(); |
| 2373 | screenclear(); |
| 2374 | } |
| 2375 | #endif |
| 2376 | |
| 2377 | if (got_int) |
| 2378 | { |
| 2379 | if (!(flags & READ_DUMMY)) |
| 2380 | { |
| 2381 | filemess(curbuf, sfname, (char_u *)_(e_interr), 0); |
| 2382 | if (newfile) |
| 2383 | curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
| 2384 | } |
| 2385 | msg_scroll = msg_save; |
| 2386 | #ifdef FEAT_VIMINFO |
| 2387 | check_marks_read(); |
| 2388 | #endif |
| 2389 | return OK; /* an interrupt isn't really an error */ |
| 2390 | } |
| 2391 | |
| 2392 | if (!filtering && !(flags & READ_DUMMY)) |
| 2393 | { |
| 2394 | msg_add_fname(curbuf, sfname); /* fname in IObuff with quotes */ |
| 2395 | c = FALSE; |
| 2396 | |
| 2397 | #ifdef UNIX |
| 2398 | # ifdef S_ISFIFO |
| 2399 | if (S_ISFIFO(perm)) /* fifo or socket */ |
| 2400 | { |
| 2401 | STRCAT(IObuff, _("[fifo/socket]")); |
| 2402 | c = TRUE; |
| 2403 | } |
| 2404 | # else |
| 2405 | # ifdef S_IFIFO |
| 2406 | if ((perm & S_IFMT) == S_IFIFO) /* fifo */ |
| 2407 | { |
| 2408 | STRCAT(IObuff, _("[fifo]")); |
| 2409 | c = TRUE; |
| 2410 | } |
| 2411 | # endif |
| 2412 | # ifdef S_IFSOCK |
| 2413 | if ((perm & S_IFMT) == S_IFSOCK) /* or socket */ |
| 2414 | { |
| 2415 | STRCAT(IObuff, _("[socket]")); |
| 2416 | c = TRUE; |
| 2417 | } |
| 2418 | # endif |
| 2419 | # endif |
Bram Moolenaar | fe1c56d | 2007-07-10 15:10:54 +0000 | [diff] [blame] | 2420 | # ifdef OPEN_CHR_FILES |
| 2421 | if (S_ISCHR(perm)) /* or character special */ |
| 2422 | { |
| 2423 | STRCAT(IObuff, _("[character special]")); |
| 2424 | c = TRUE; |
| 2425 | } |
| 2426 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2427 | #endif |
| 2428 | if (curbuf->b_p_ro) |
| 2429 | { |
| 2430 | STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")); |
| 2431 | c = TRUE; |
| 2432 | } |
| 2433 | if (read_no_eol_lnum) |
| 2434 | { |
| 2435 | msg_add_eol(); |
| 2436 | c = TRUE; |
| 2437 | } |
| 2438 | if (ff_error == EOL_DOS) |
| 2439 | { |
| 2440 | STRCAT(IObuff, _("[CR missing]")); |
| 2441 | c = TRUE; |
| 2442 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2443 | if (split) |
| 2444 | { |
| 2445 | STRCAT(IObuff, _("[long lines split]")); |
| 2446 | c = TRUE; |
| 2447 | } |
| 2448 | #ifdef FEAT_MBYTE |
| 2449 | if (notconverted) |
| 2450 | { |
| 2451 | STRCAT(IObuff, _("[NOT converted]")); |
| 2452 | c = TRUE; |
| 2453 | } |
| 2454 | else if (converted) |
| 2455 | { |
| 2456 | STRCAT(IObuff, _("[converted]")); |
| 2457 | c = TRUE; |
| 2458 | } |
| 2459 | #endif |
| 2460 | #ifdef FEAT_CRYPT |
| 2461 | if (cryptkey != NULL) |
| 2462 | { |
| 2463 | STRCAT(IObuff, _("[crypted]")); |
| 2464 | c = TRUE; |
| 2465 | } |
| 2466 | #endif |
| 2467 | #ifdef FEAT_MBYTE |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2468 | if (conv_error != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2469 | { |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2470 | sprintf((char *)IObuff + STRLEN(IObuff), |
| 2471 | _("[CONVERSION ERROR in line %ld]"), (long)conv_error); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2472 | c = TRUE; |
| 2473 | } |
| 2474 | else if (illegal_byte > 0) |
| 2475 | { |
| 2476 | sprintf((char *)IObuff + STRLEN(IObuff), |
| 2477 | _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte); |
| 2478 | c = TRUE; |
| 2479 | } |
| 2480 | else |
| 2481 | #endif |
| 2482 | if (error) |
| 2483 | { |
| 2484 | STRCAT(IObuff, _("[READ ERRORS]")); |
| 2485 | c = TRUE; |
| 2486 | } |
| 2487 | if (msg_add_fileformat(fileformat)) |
| 2488 | c = TRUE; |
| 2489 | #ifdef FEAT_CRYPT |
| 2490 | if (cryptkey != NULL) |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2491 | msg_add_lines(c, (long)linecnt, filesize |
| 2492 | - CRYPT_MAGIC_LEN - crypt_seed_len[use_crypt_method]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2493 | else |
| 2494 | #endif |
| 2495 | msg_add_lines(c, (long)linecnt, filesize); |
| 2496 | |
| 2497 | vim_free(keep_msg); |
| 2498 | keep_msg = NULL; |
| 2499 | msg_scrolled_ign = TRUE; |
| 2500 | #ifdef ALWAYS_USE_GUI |
| 2501 | /* Don't show the message when reading stdin, it would end up in a |
| 2502 | * message box (which might be shown when exiting!) */ |
| 2503 | if (read_stdin || read_buffer) |
| 2504 | p = msg_may_trunc(FALSE, IObuff); |
| 2505 | else |
| 2506 | #endif |
| 2507 | p = msg_trunc_attr(IObuff, FALSE, 0); |
| 2508 | if (read_stdin || read_buffer || restart_edit != 0 |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2509 | || (msg_scrolled != 0 && !need_wait_return)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2510 | /* Need to repeat the message after redrawing when: |
| 2511 | * - When reading from stdin (the screen will be cleared next). |
| 2512 | * - When restart_edit is set (otherwise there will be a delay |
| 2513 | * before redrawing). |
| 2514 | * - When the screen was scrolled but there is no wait-return |
| 2515 | * prompt. */ |
Bram Moolenaar | 8f7fd65 | 2006-02-21 22:04:51 +0000 | [diff] [blame] | 2516 | set_keep_msg(p, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2517 | msg_scrolled_ign = FALSE; |
| 2518 | } |
| 2519 | |
| 2520 | /* with errors writing the file requires ":w!" */ |
| 2521 | if (newfile && (error |
| 2522 | #ifdef FEAT_MBYTE |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2523 | || conv_error != 0 |
Bram Moolenaar | 8f7fd65 | 2006-02-21 22:04:51 +0000 | [diff] [blame] | 2524 | || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2525 | #endif |
| 2526 | )) |
| 2527 | curbuf->b_p_ro = TRUE; |
| 2528 | |
| 2529 | u_clearline(); /* cannot use "U" command after adding lines */ |
| 2530 | |
| 2531 | /* |
| 2532 | * In Ex mode: cursor at last new line. |
| 2533 | * Otherwise: cursor at first new line. |
| 2534 | */ |
| 2535 | if (exmode_active) |
| 2536 | curwin->w_cursor.lnum = from + linecnt; |
| 2537 | else |
| 2538 | curwin->w_cursor.lnum = from + 1; |
| 2539 | check_cursor_lnum(); |
| 2540 | beginline(BL_WHITE | BL_FIX); /* on first non-blank */ |
| 2541 | |
| 2542 | /* |
| 2543 | * Set '[ and '] marks to the newly read lines. |
| 2544 | */ |
| 2545 | curbuf->b_op_start.lnum = from + 1; |
| 2546 | curbuf->b_op_start.col = 0; |
| 2547 | curbuf->b_op_end.lnum = from + linecnt; |
| 2548 | curbuf->b_op_end.col = 0; |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 2549 | |
| 2550 | #ifdef WIN32 |
| 2551 | /* |
| 2552 | * Work around a weird problem: When a file has two links (only |
| 2553 | * possible on NTFS) and we write through one link, then stat() it |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 2554 | * through the other link, the timestamp information may be wrong. |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 2555 | * It's correct again after reading the file, thus reset the timestamp |
| 2556 | * here. |
| 2557 | */ |
| 2558 | if (newfile && !read_stdin && !read_buffer |
| 2559 | && mch_stat((char *)fname, &st) >= 0) |
| 2560 | { |
| 2561 | buf_store_time(curbuf, &st, fname); |
| 2562 | curbuf->b_mtime_read = curbuf->b_mtime; |
| 2563 | } |
| 2564 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2565 | } |
| 2566 | msg_scroll = msg_save; |
| 2567 | |
| 2568 | #ifdef FEAT_VIMINFO |
| 2569 | /* |
| 2570 | * Get the marks before executing autocommands, so they can be used there. |
| 2571 | */ |
| 2572 | check_marks_read(); |
| 2573 | #endif |
| 2574 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2575 | /* |
| 2576 | * Trick: We remember if the last line of the read didn't have |
| 2577 | * an eol for when writing it again. This is required for |
| 2578 | * ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work. |
| 2579 | */ |
| 2580 | write_no_eol_lnum = read_no_eol_lnum; |
| 2581 | |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2582 | #ifdef FEAT_PERSISTENT_UNDO |
| 2583 | /* |
| 2584 | * When opening a new file locate undo info and read it. |
| 2585 | */ |
| 2586 | if (read_undo_file) |
| 2587 | { |
| 2588 | char_u hash[UNDO_HASH_SIZE]; |
| 2589 | |
| 2590 | sha256_finish(&sha_ctx, hash); |
Bram Moolenaar | 6ed8ed8 | 2010-05-30 20:40:11 +0200 | [diff] [blame] | 2591 | u_read_undo(NULL, hash, fname); |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 2592 | } |
| 2593 | #endif |
| 2594 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2595 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2596 | if (!read_stdin && !read_buffer) |
| 2597 | { |
| 2598 | int m = msg_scroll; |
| 2599 | int n = msg_scrolled; |
| 2600 | |
| 2601 | /* Save the fileformat now, otherwise the buffer will be considered |
| 2602 | * modified if the format/encoding was automatically detected. */ |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2603 | if (set_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2604 | save_file_ff(curbuf); |
| 2605 | |
| 2606 | /* |
| 2607 | * The output from the autocommands should not overwrite anything and |
| 2608 | * should not be overwritten: Set msg_scroll, restore its value if no |
| 2609 | * output was done. |
| 2610 | */ |
| 2611 | msg_scroll = TRUE; |
| 2612 | if (filtering) |
| 2613 | apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname, |
| 2614 | FALSE, curbuf, eap); |
| 2615 | else if (newfile) |
| 2616 | apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname, |
| 2617 | FALSE, curbuf, eap); |
| 2618 | else |
| 2619 | apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname, |
| 2620 | FALSE, NULL, eap); |
| 2621 | if (msg_scrolled == n) |
| 2622 | msg_scroll = m; |
| 2623 | #ifdef FEAT_EVAL |
| 2624 | if (aborting()) /* autocmds may abort script processing */ |
| 2625 | return FAIL; |
| 2626 | #endif |
| 2627 | } |
| 2628 | #endif |
| 2629 | |
| 2630 | if (recoverymode && error) |
| 2631 | return FAIL; |
| 2632 | return OK; |
| 2633 | } |
| 2634 | |
Bram Moolenaar | fe1c56d | 2007-07-10 15:10:54 +0000 | [diff] [blame] | 2635 | #ifdef OPEN_CHR_FILES |
| 2636 | /* |
| 2637 | * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+", |
| 2638 | * which is the name of files used for process substitution output by |
| 2639 | * some shells on some operating systems, e.g., bash on SunOS. |
| 2640 | * Do not accept "/dev/fd/[012]", opening these may hang Vim. |
| 2641 | */ |
| 2642 | static int |
| 2643 | is_dev_fd_file(fname) |
| 2644 | char_u *fname; |
| 2645 | { |
| 2646 | return (STRNCMP(fname, "/dev/fd/", 8) == 0 |
| 2647 | && VIM_ISDIGIT(fname[8]) |
| 2648 | && *skipdigits(fname + 9) == NUL |
| 2649 | && (fname[9] != NUL |
| 2650 | || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2'))); |
| 2651 | } |
| 2652 | #endif |
| 2653 | |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 2654 | #ifdef FEAT_MBYTE |
| 2655 | |
| 2656 | /* |
| 2657 | * From the current line count and characters read after that, estimate the |
| 2658 | * line number where we are now. |
| 2659 | * Used for error messages that include a line number. |
| 2660 | */ |
| 2661 | static linenr_T |
| 2662 | readfile_linenr(linecnt, p, endp) |
| 2663 | linenr_T linecnt; /* line count before reading more bytes */ |
| 2664 | char_u *p; /* start of more bytes read */ |
| 2665 | char_u *endp; /* end of more bytes read */ |
| 2666 | { |
| 2667 | char_u *s; |
| 2668 | linenr_T lnum; |
| 2669 | |
| 2670 | lnum = curbuf->b_ml.ml_line_count - linecnt + 1; |
| 2671 | for (s = p; s < endp; ++s) |
| 2672 | if (*s == '\n') |
| 2673 | ++lnum; |
| 2674 | return lnum; |
| 2675 | } |
| 2676 | #endif |
| 2677 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2678 | /* |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 2679 | * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be |
| 2680 | * equal to the buffer "buf". Used for calling readfile(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2681 | * Returns OK or FAIL. |
| 2682 | */ |
| 2683 | int |
| 2684 | prep_exarg(eap, buf) |
| 2685 | exarg_T *eap; |
| 2686 | buf_T *buf; |
| 2687 | { |
| 2688 | eap->cmd = alloc((unsigned)(STRLEN(buf->b_p_ff) |
| 2689 | #ifdef FEAT_MBYTE |
| 2690 | + STRLEN(buf->b_p_fenc) |
| 2691 | #endif |
| 2692 | + 15)); |
| 2693 | if (eap->cmd == NULL) |
| 2694 | return FAIL; |
| 2695 | |
| 2696 | #ifdef FEAT_MBYTE |
| 2697 | sprintf((char *)eap->cmd, "e ++ff=%s ++enc=%s", buf->b_p_ff, buf->b_p_fenc); |
| 2698 | eap->force_enc = 14 + (int)STRLEN(buf->b_p_ff); |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 2699 | eap->bad_char = buf->b_bad_char; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2700 | #else |
| 2701 | sprintf((char *)eap->cmd, "e ++ff=%s", buf->b_p_ff); |
| 2702 | #endif |
| 2703 | eap->force_ff = 7; |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 2704 | |
| 2705 | eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2706 | eap->read_edit = FALSE; |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 2707 | eap->forceit = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2708 | return OK; |
| 2709 | } |
| 2710 | |
| 2711 | #ifdef FEAT_MBYTE |
| 2712 | /* |
| 2713 | * Find next fileencoding to use from 'fileencodings'. |
| 2714 | * "pp" points to fenc_next. It's advanced to the next item. |
| 2715 | * When there are no more items, an empty string is returned and *pp is set to |
| 2716 | * NULL. |
| 2717 | * When *pp is not set to NULL, the result is in allocated memory. |
| 2718 | */ |
| 2719 | static char_u * |
| 2720 | next_fenc(pp) |
| 2721 | char_u **pp; |
| 2722 | { |
| 2723 | char_u *p; |
| 2724 | char_u *r; |
| 2725 | |
| 2726 | if (**pp == NUL) |
| 2727 | { |
| 2728 | *pp = NULL; |
| 2729 | return (char_u *)""; |
| 2730 | } |
| 2731 | p = vim_strchr(*pp, ','); |
| 2732 | if (p == NULL) |
| 2733 | { |
| 2734 | r = enc_canonize(*pp); |
| 2735 | *pp += STRLEN(*pp); |
| 2736 | } |
| 2737 | else |
| 2738 | { |
| 2739 | r = vim_strnsave(*pp, (int)(p - *pp)); |
| 2740 | *pp = p + 1; |
| 2741 | if (r != NULL) |
| 2742 | { |
| 2743 | p = enc_canonize(r); |
| 2744 | vim_free(r); |
| 2745 | r = p; |
| 2746 | } |
| 2747 | } |
| 2748 | if (r == NULL) /* out of memory */ |
| 2749 | { |
| 2750 | r = (char_u *)""; |
| 2751 | *pp = NULL; |
| 2752 | } |
| 2753 | return r; |
| 2754 | } |
| 2755 | |
| 2756 | # ifdef FEAT_EVAL |
| 2757 | /* |
| 2758 | * Convert a file with the 'charconvert' expression. |
| 2759 | * This closes the file which is to be read, converts it and opens the |
| 2760 | * resulting file for reading. |
| 2761 | * Returns name of the resulting converted file (the caller should delete it |
| 2762 | * after reading it). |
| 2763 | * Returns NULL if the conversion failed ("*fdp" is not set) . |
| 2764 | */ |
| 2765 | static char_u * |
| 2766 | readfile_charconvert(fname, fenc, fdp) |
| 2767 | char_u *fname; /* name of input file */ |
| 2768 | char_u *fenc; /* converted from */ |
| 2769 | int *fdp; /* in/out: file descriptor of file */ |
| 2770 | { |
| 2771 | char_u *tmpname; |
| 2772 | char_u *errmsg = NULL; |
| 2773 | |
| 2774 | tmpname = vim_tempname('r'); |
| 2775 | if (tmpname == NULL) |
| 2776 | errmsg = (char_u *)_("Can't find temp file for conversion"); |
| 2777 | else |
| 2778 | { |
| 2779 | close(*fdp); /* close the input file, ignore errors */ |
| 2780 | *fdp = -1; |
| 2781 | if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc, |
| 2782 | fname, tmpname) == FAIL) |
| 2783 | errmsg = (char_u *)_("Conversion with 'charconvert' failed"); |
| 2784 | if (errmsg == NULL && (*fdp = mch_open((char *)tmpname, |
| 2785 | O_RDONLY | O_EXTRA, 0)) < 0) |
| 2786 | errmsg = (char_u *)_("can't read output of 'charconvert'"); |
| 2787 | } |
| 2788 | |
| 2789 | if (errmsg != NULL) |
| 2790 | { |
| 2791 | /* Don't use emsg(), it breaks mappings, the retry with |
| 2792 | * another type of conversion might still work. */ |
| 2793 | MSG(errmsg); |
| 2794 | if (tmpname != NULL) |
| 2795 | { |
| 2796 | mch_remove(tmpname); /* delete converted file */ |
| 2797 | vim_free(tmpname); |
| 2798 | tmpname = NULL; |
| 2799 | } |
| 2800 | } |
| 2801 | |
| 2802 | /* If the input file is closed, open it (caller should check for error). */ |
| 2803 | if (*fdp < 0) |
| 2804 | *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); |
| 2805 | |
| 2806 | return tmpname; |
| 2807 | } |
| 2808 | # endif |
| 2809 | |
| 2810 | #endif |
| 2811 | |
| 2812 | #ifdef FEAT_VIMINFO |
| 2813 | /* |
| 2814 | * Read marks for the current buffer from the viminfo file, when we support |
| 2815 | * buffer marks and the buffer has a name. |
| 2816 | */ |
| 2817 | static void |
| 2818 | check_marks_read() |
| 2819 | { |
| 2820 | if (!curbuf->b_marks_read && get_viminfo_parameter('\'') > 0 |
| 2821 | && curbuf->b_ffname != NULL) |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 2822 | read_viminfo(NULL, VIF_WANT_MARKS); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2823 | |
| 2824 | /* Always set b_marks_read; needed when 'viminfo' is changed to include |
| 2825 | * the ' parameter after opening a buffer. */ |
| 2826 | curbuf->b_marks_read = TRUE; |
| 2827 | } |
| 2828 | #endif |
| 2829 | |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 2830 | #if defined(FEAT_CRYPT) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2831 | /* |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2832 | * Get the crypt method used for a file from "ptr[len]", the magic text at the |
| 2833 | * start of the file. |
| 2834 | * Returns -1 when no encryption used. |
| 2835 | */ |
| 2836 | static int |
| 2837 | get_crypt_method(ptr, len) |
| 2838 | char *ptr; |
| 2839 | int len; |
| 2840 | { |
| 2841 | int i; |
| 2842 | |
| 2843 | for (i = 0; i < (int)(sizeof(crypt_magic) / sizeof(crypt_magic[0])); i++) |
| 2844 | { |
| 2845 | if (len < (CRYPT_MAGIC_LEN + crypt_seed_len[i])) |
| 2846 | continue; |
| 2847 | if (memcmp(ptr, crypt_magic[i], CRYPT_MAGIC_LEN) == 0) |
| 2848 | return i; |
| 2849 | } |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 2850 | |
Bram Moolenaar | 442b422 | 2010-05-24 21:34:22 +0200 | [diff] [blame] | 2851 | i = (int)STRLEN(crypt_magic_head); |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 2852 | if (len >= i && memcmp(ptr, crypt_magic_head, i) == 0) |
| 2853 | EMSG(_("E821: File is encrypted with unknown method")); |
| 2854 | |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2855 | return -1; |
| 2856 | } |
| 2857 | |
| 2858 | /* |
| 2859 | * Check for magic number used for encryption. Applies to the current buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2860 | * If found, the magic number is removed from ptr[*sizep] and *sizep and |
| 2861 | * *filesizep are updated. |
| 2862 | * Return the (new) encryption key, NULL for no encryption. |
| 2863 | */ |
| 2864 | static char_u * |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 2865 | check_for_cryptkey(cryptkey, ptr, sizep, filesizep, newfile, did_ask) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2866 | char_u *cryptkey; /* previous encryption key or NULL */ |
| 2867 | char_u *ptr; /* pointer to read bytes */ |
| 2868 | long *sizep; /* length of read bytes */ |
Bram Moolenaar | 914703b | 2010-05-31 21:59:46 +0200 | [diff] [blame] | 2869 | off_t *filesizep; /* nr of bytes used from file */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2870 | int newfile; /* editing a new buffer */ |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 2871 | int *did_ask; /* flag: whether already asked for key */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2872 | { |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2873 | int method = get_crypt_method((char *)ptr, *sizep); |
| 2874 | |
| 2875 | if (method >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2876 | { |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2877 | curbuf->b_p_cm = method; |
| 2878 | use_crypt_method = method; |
| 2879 | if (method > 0) |
| 2880 | (void)blowfish_self_test(); |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 2881 | if (cryptkey == NULL && !*did_ask) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2882 | { |
| 2883 | if (*curbuf->b_p_key) |
| 2884 | cryptkey = curbuf->b_p_key; |
| 2885 | else |
| 2886 | { |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2887 | /* When newfile is TRUE, store the typed key in the 'key' |
| 2888 | * 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] | 2889 | * Don't ask for the key again when first time Enter was hit. |
| 2890 | * Happens when retrying to detect encoding. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2891 | cryptkey = get_crypt_key(newfile, FALSE); |
Bram Moolenaar | f50a253 | 2010-05-21 15:36:08 +0200 | [diff] [blame] | 2892 | *did_ask = TRUE; |
| 2893 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2894 | /* check if empty key entered */ |
| 2895 | if (cryptkey != NULL && *cryptkey == NUL) |
| 2896 | { |
| 2897 | if (cryptkey != curbuf->b_p_key) |
| 2898 | vim_free(cryptkey); |
| 2899 | cryptkey = NULL; |
| 2900 | } |
| 2901 | } |
| 2902 | } |
| 2903 | |
| 2904 | if (cryptkey != NULL) |
| 2905 | { |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2906 | int seed_len = crypt_seed_len[method]; |
| 2907 | |
| 2908 | if (method == 0) |
| 2909 | crypt_init_keys(cryptkey); |
| 2910 | else |
| 2911 | { |
| 2912 | bf_key_init(cryptkey); |
| 2913 | bf_ofb_init(ptr + CRYPT_MAGIC_LEN, seed_len); |
| 2914 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2915 | |
| 2916 | /* Remove magic number from the text */ |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2917 | *filesizep += CRYPT_MAGIC_LEN + seed_len; |
| 2918 | *sizep -= CRYPT_MAGIC_LEN + seed_len; |
| 2919 | mch_memmove(ptr, ptr + CRYPT_MAGIC_LEN + seed_len, (size_t)*sizep); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2920 | } |
| 2921 | } |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 2922 | /* When starting to edit a new file which does not have encryption, clear |
| 2923 | * the 'key' option, except when starting up (called with -x argument) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2924 | else if (newfile && *curbuf->b_p_key && !starting) |
| 2925 | set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL); |
| 2926 | |
| 2927 | return cryptkey; |
| 2928 | } |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 2929 | |
| 2930 | /* |
| 2931 | * Check for magic number used for encryption. Applies to the current buffer. |
| 2932 | * If found and decryption is possible returns OK; |
| 2933 | */ |
| 2934 | int |
| 2935 | prepare_crypt_read(fp) |
| 2936 | FILE *fp; |
| 2937 | { |
| 2938 | int method; |
| 2939 | char_u buffer[CRYPT_MAGIC_LEN + CRYPT_SEED_LEN_MAX + 2]; |
| 2940 | |
| 2941 | if (fread(buffer, CRYPT_MAGIC_LEN, 1, fp) != 1) |
| 2942 | return FAIL; |
| 2943 | method = get_crypt_method((char *)buffer, |
| 2944 | CRYPT_MAGIC_LEN + CRYPT_SEED_LEN_MAX); |
| 2945 | if (method < 0 || method != curbuf->b_p_cm) |
| 2946 | return FAIL; |
| 2947 | |
| 2948 | if (method == 0) |
| 2949 | crypt_init_keys(curbuf->b_p_key); |
| 2950 | else |
| 2951 | { |
| 2952 | int seed_len = crypt_seed_len[method]; |
| 2953 | |
| 2954 | if (fread(buffer, seed_len, 1, fp) != 1) |
| 2955 | return FAIL; |
| 2956 | bf_key_init(curbuf->b_p_key); |
| 2957 | bf_ofb_init(buffer, seed_len); |
| 2958 | } |
| 2959 | return OK; |
| 2960 | } |
| 2961 | |
| 2962 | /* |
| 2963 | * Prepare for writing encrypted bytes for buffer "buf". |
| 2964 | * Returns a pointer to an allocated header of length "*lenp". |
| 2965 | */ |
| 2966 | char_u * |
| 2967 | prepare_crypt_write(buf, lenp) |
| 2968 | buf_T *buf; |
| 2969 | int *lenp; |
| 2970 | { |
| 2971 | char_u *header; |
| 2972 | int seed_len = crypt_seed_len[buf->b_p_cm]; |
| 2973 | |
| 2974 | header = alloc_clear(CRYPT_MAGIC_LEN + CRYPT_SEED_LEN_MAX + 2); |
| 2975 | if (header != NULL) |
| 2976 | { |
| 2977 | use_crypt_method = buf->b_p_cm; /* select pkzip or blowfish */ |
| 2978 | vim_strncpy(header, (char_u *)crypt_magic[use_crypt_method], |
| 2979 | CRYPT_MAGIC_LEN); |
| 2980 | if (buf->b_p_cm == 0) |
| 2981 | crypt_init_keys(buf->b_p_key); |
| 2982 | else |
| 2983 | { |
| 2984 | /* Using blowfish, add seed. */ |
| 2985 | sha2_seed(header + CRYPT_MAGIC_LEN, seed_len); /* create iv */ |
| 2986 | bf_ofb_init(header + CRYPT_MAGIC_LEN, seed_len); |
| 2987 | bf_key_init(buf->b_p_key); |
| 2988 | } |
| 2989 | } |
| 2990 | *lenp = CRYPT_MAGIC_LEN + seed_len; |
| 2991 | return header; |
| 2992 | } |
| 2993 | |
| 2994 | /* |
| 2995 | * Like fwrite() but crypt the bytes when 'key' is set. |
| 2996 | * Returns 1 if successful. |
| 2997 | */ |
| 2998 | size_t |
| 2999 | fwrite_crypt(buf, ptr, len, fp) |
| 3000 | buf_T *buf; |
| 3001 | char_u *ptr; |
| 3002 | size_t len; |
| 3003 | FILE *fp; |
| 3004 | { |
| 3005 | char_u *copy; |
| 3006 | char_u small_buf[100]; |
| 3007 | int ztemp, t; |
| 3008 | size_t i; |
| 3009 | |
| 3010 | if (*buf->b_p_key == NUL) |
| 3011 | return fwrite(ptr, len, (size_t)1, fp); |
| 3012 | if (len < 100) |
| 3013 | copy = small_buf; /* no malloc()/free() for short strings */ |
| 3014 | else |
| 3015 | { |
| 3016 | copy = lalloc(len, FALSE); |
| 3017 | if (copy == NULL) |
| 3018 | return 0; |
| 3019 | } |
| 3020 | for (i = 0; i < len; ++i) |
| 3021 | { |
| 3022 | ztemp = ptr[i]; |
| 3023 | copy[i] = ZENCODE(ztemp, t); |
| 3024 | } |
| 3025 | i = fwrite(copy, len, (size_t)1, fp); |
| 3026 | if (copy != small_buf) |
| 3027 | vim_free(copy); |
| 3028 | return i; |
| 3029 | } |
| 3030 | |
| 3031 | /* |
| 3032 | * Read a string of length "len" from "fd". |
| 3033 | * When 'key' is set decrypt the bytes. |
| 3034 | */ |
| 3035 | char_u * |
| 3036 | read_string_decrypt(buf, fd, len) |
| 3037 | buf_T *buf; |
| 3038 | FILE *fd; |
| 3039 | int len; |
| 3040 | { |
| 3041 | char_u *ptr; |
| 3042 | char_u *p; |
| 3043 | |
| 3044 | ptr = read_string(fd, len); |
| 3045 | if (ptr != NULL || *buf->b_p_key != NUL) |
| 3046 | for (p = ptr; p < ptr + len; ++p) |
| 3047 | ZDECODE(*p); |
| 3048 | return ptr; |
| 3049 | } |
| 3050 | |
| 3051 | #endif /* FEAT_CRYPT */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3052 | |
| 3053 | #ifdef UNIX |
| 3054 | static void |
| 3055 | set_file_time(fname, atime, mtime) |
| 3056 | char_u *fname; |
| 3057 | time_t atime; /* access time */ |
| 3058 | time_t mtime; /* modification time */ |
| 3059 | { |
| 3060 | # if defined(HAVE_UTIME) && defined(HAVE_UTIME_H) |
| 3061 | struct utimbuf buf; |
| 3062 | |
| 3063 | buf.actime = atime; |
| 3064 | buf.modtime = mtime; |
| 3065 | (void)utime((char *)fname, &buf); |
| 3066 | # else |
| 3067 | # if defined(HAVE_UTIMES) |
| 3068 | struct timeval tvp[2]; |
| 3069 | |
| 3070 | tvp[0].tv_sec = atime; |
| 3071 | tvp[0].tv_usec = 0; |
| 3072 | tvp[1].tv_sec = mtime; |
| 3073 | tvp[1].tv_usec = 0; |
| 3074 | # ifdef NeXT |
| 3075 | (void)utimes((char *)fname, tvp); |
| 3076 | # else |
| 3077 | (void)utimes((char *)fname, (const struct timeval *)&tvp); |
| 3078 | # endif |
| 3079 | # endif |
| 3080 | # endif |
| 3081 | } |
| 3082 | #endif /* UNIX */ |
| 3083 | |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 3084 | #if defined(VMS) && !defined(MIN) |
| 3085 | /* Older DECC compiler for VAX doesn't define MIN() */ |
| 3086 | # define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 3087 | #endif |
| 3088 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3089 | /* |
Bram Moolenaar | 5386a12 | 2007-06-28 20:02:32 +0000 | [diff] [blame] | 3090 | * Return TRUE if a file appears to be read-only from the file permissions. |
| 3091 | */ |
| 3092 | int |
| 3093 | check_file_readonly(fname, perm) |
| 3094 | char_u *fname; /* full path to file */ |
| 3095 | int perm; /* known permissions on file */ |
| 3096 | { |
| 3097 | #ifndef USE_MCH_ACCESS |
| 3098 | int fd = 0; |
| 3099 | #endif |
| 3100 | |
| 3101 | return ( |
| 3102 | #ifdef USE_MCH_ACCESS |
| 3103 | # ifdef UNIX |
| 3104 | (perm & 0222) == 0 || |
| 3105 | # endif |
| 3106 | mch_access((char *)fname, W_OK) |
| 3107 | #else |
| 3108 | (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0 |
| 3109 | ? TRUE : (close(fd), FALSE) |
| 3110 | #endif |
| 3111 | ); |
| 3112 | } |
| 3113 | |
| 3114 | |
| 3115 | /* |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3116 | * buf_write() - write to file "fname" lines "start" through "end" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3117 | * |
| 3118 | * We do our own buffering here because fwrite() is so slow. |
| 3119 | * |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3120 | * If "forceit" is true, we don't care for errors when attempting backups. |
| 3121 | * 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] | 3122 | * file. But when "forceit" is TRUE, we risk losing it. |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3123 | * |
| 3124 | * When "reset_changed" is TRUE and "append" == FALSE and "start" == 1 and |
| 3125 | * "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3126 | * |
| 3127 | * This function must NOT use NameBuff (because it's called by autowrite()). |
| 3128 | * |
| 3129 | * return FAIL for failure, OK otherwise |
| 3130 | */ |
| 3131 | int |
| 3132 | buf_write(buf, fname, sfname, start, end, eap, append, forceit, |
| 3133 | reset_changed, filtering) |
| 3134 | buf_T *buf; |
| 3135 | char_u *fname; |
| 3136 | char_u *sfname; |
| 3137 | linenr_T start, end; |
| 3138 | exarg_T *eap; /* for forced 'ff' and 'fenc', can be |
| 3139 | NULL! */ |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3140 | int append; /* append to the file */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3141 | int forceit; |
| 3142 | int reset_changed; |
| 3143 | int filtering; |
| 3144 | { |
| 3145 | int fd; |
| 3146 | char_u *backup = NULL; |
| 3147 | int backup_copy = FALSE; /* copy the original file? */ |
| 3148 | int dobackup; |
| 3149 | char_u *ffname; |
| 3150 | char_u *wfname = NULL; /* name of file to write to */ |
| 3151 | char_u *s; |
| 3152 | char_u *ptr; |
| 3153 | char_u c; |
| 3154 | int len; |
| 3155 | linenr_T lnum; |
| 3156 | long nchars; |
| 3157 | char_u *errmsg = NULL; |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 3158 | int errmsg_allocated = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3159 | char_u *errnum = NULL; |
| 3160 | char_u *buffer; |
| 3161 | char_u smallbuf[SMBUFSIZE]; |
| 3162 | char_u *backup_ext; |
| 3163 | int bufsize; |
| 3164 | long perm; /* file permissions */ |
| 3165 | int retval = OK; |
| 3166 | int newfile = FALSE; /* TRUE if file doesn't exist yet */ |
| 3167 | int msg_save = msg_scroll; |
| 3168 | int overwriting; /* TRUE if writing over original */ |
| 3169 | int no_eol = FALSE; /* no end-of-line written */ |
| 3170 | int device = FALSE; /* writing to a device */ |
| 3171 | struct stat st_old; |
| 3172 | int prev_got_int = got_int; |
| 3173 | int file_readonly = FALSE; /* overwritten file is read-only */ |
| 3174 | static char *err_readonly = "is read-only (cannot override: \"W\" in 'cpoptions')"; |
| 3175 | #if defined(UNIX) || defined(__EMX__XX) /*XXX fix me sometime? */ |
| 3176 | int made_writable = FALSE; /* 'w' bit has been set */ |
| 3177 | #endif |
| 3178 | /* writing everything */ |
| 3179 | int whole = (start == 1 && end == buf->b_ml.ml_line_count); |
| 3180 | #ifdef FEAT_AUTOCMD |
| 3181 | linenr_T old_line_count = buf->b_ml.ml_line_count; |
| 3182 | #endif |
| 3183 | int attr; |
| 3184 | int fileformat; |
| 3185 | int write_bin; |
| 3186 | struct bw_info write_info; /* info for buf_write_bytes() */ |
| 3187 | #ifdef FEAT_MBYTE |
| 3188 | int converted = FALSE; |
| 3189 | int notconverted = FALSE; |
| 3190 | char_u *fenc; /* effective 'fileencoding' */ |
| 3191 | char_u *fenc_tofree = NULL; /* allocated "fenc" */ |
| 3192 | #endif |
| 3193 | #ifdef HAS_BW_FLAGS |
| 3194 | int wb_flags = 0; |
| 3195 | #endif |
| 3196 | #ifdef HAVE_ACL |
| 3197 | vim_acl_T acl = NULL; /* ACL copied from original file to |
| 3198 | backup or new file */ |
| 3199 | #endif |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 3200 | #ifdef FEAT_PERSISTENT_UNDO |
| 3201 | int write_undo_file = FALSE; |
| 3202 | context_sha256_T sha_ctx; |
| 3203 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3204 | |
| 3205 | if (fname == NULL || *fname == NUL) /* safety check */ |
| 3206 | return FAIL; |
Bram Moolenaar | 70d60e9 | 2009-12-31 13:53:33 +0000 | [diff] [blame] | 3207 | if (buf->b_ml.ml_mfp == NULL) |
| 3208 | { |
| 3209 | /* This can happen during startup when there is a stray "w" in the |
| 3210 | * vimrc file. */ |
| 3211 | EMSG(_(e_emptybuf)); |
| 3212 | return FAIL; |
| 3213 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3214 | |
| 3215 | /* |
| 3216 | * Disallow writing from .exrc and .vimrc in current directory for |
| 3217 | * security reasons. |
| 3218 | */ |
| 3219 | if (check_secure()) |
| 3220 | return FAIL; |
| 3221 | |
| 3222 | /* Avoid a crash for a long name. */ |
| 3223 | if (STRLEN(fname) >= MAXPATHL) |
| 3224 | { |
| 3225 | EMSG(_(e_longname)); |
| 3226 | return FAIL; |
| 3227 | } |
| 3228 | |
| 3229 | #ifdef FEAT_MBYTE |
| 3230 | /* must init bw_conv_buf and bw_iconv_fd before jumping to "fail" */ |
| 3231 | write_info.bw_conv_buf = NULL; |
| 3232 | write_info.bw_conv_error = FALSE; |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 3233 | write_info.bw_conv_error_lnum = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3234 | write_info.bw_restlen = 0; |
| 3235 | # ifdef USE_ICONV |
| 3236 | write_info.bw_iconv_fd = (iconv_t)-1; |
| 3237 | # endif |
| 3238 | #endif |
| 3239 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3240 | /* After writing a file changedtick changes but we don't want to display |
| 3241 | * the line. */ |
| 3242 | ex_no_reprint = TRUE; |
| 3243 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3244 | /* |
| 3245 | * If there is no file name yet, use the one for the written file. |
| 3246 | * BF_NOTEDITED is set to reflect this (in case the write fails). |
| 3247 | * Don't do this when the write is for a filter command. |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3248 | * Don't do this when appending. |
| 3249 | * Only do this when 'cpoptions' contains the 'F' flag. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3250 | */ |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 3251 | if (buf->b_ffname == NULL |
| 3252 | && reset_changed |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3253 | && whole |
| 3254 | && buf == curbuf |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3255 | #ifdef FEAT_QUICKFIX |
| 3256 | && !bt_nofile(buf) |
| 3257 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3258 | && !filtering |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3259 | && (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3260 | && vim_strchr(p_cpo, CPO_FNAMEW) != NULL) |
| 3261 | { |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 3262 | if (set_rw_fname(fname, sfname) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3263 | return FAIL; |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 3264 | buf = curbuf; /* just in case autocmds made "buf" invalid */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3265 | } |
| 3266 | |
| 3267 | if (sfname == NULL) |
| 3268 | sfname = fname; |
| 3269 | /* |
| 3270 | * For Unix: Use the short file name whenever possible. |
| 3271 | * Avoids problems with networks and when directory names are changed. |
| 3272 | * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to |
| 3273 | * another directory, which we don't detect |
| 3274 | */ |
| 3275 | ffname = fname; /* remember full fname */ |
| 3276 | #ifdef UNIX |
| 3277 | fname = sfname; |
| 3278 | #endif |
| 3279 | |
| 3280 | if (buf->b_ffname != NULL && fnamecmp(ffname, buf->b_ffname) == 0) |
| 3281 | overwriting = TRUE; |
| 3282 | else |
| 3283 | overwriting = FALSE; |
| 3284 | |
| 3285 | if (exiting) |
| 3286 | settmode(TMODE_COOK); /* when exiting allow typahead now */ |
| 3287 | |
| 3288 | ++no_wait_return; /* don't wait for return yet */ |
| 3289 | |
| 3290 | /* |
| 3291 | * Set '[ and '] marks to the lines to be written. |
| 3292 | */ |
| 3293 | buf->b_op_start.lnum = start; |
| 3294 | buf->b_op_start.col = 0; |
| 3295 | buf->b_op_end.lnum = end; |
| 3296 | buf->b_op_end.col = 0; |
| 3297 | |
| 3298 | #ifdef FEAT_AUTOCMD |
| 3299 | { |
| 3300 | aco_save_T aco; |
| 3301 | int buf_ffname = FALSE; |
| 3302 | int buf_sfname = FALSE; |
| 3303 | int buf_fname_f = FALSE; |
| 3304 | int buf_fname_s = FALSE; |
| 3305 | int did_cmd = FALSE; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3306 | int nofile_err = FALSE; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 3307 | int empty_memline = (buf->b_ml.ml_mfp == NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3308 | |
| 3309 | /* |
| 3310 | * Apply PRE aucocommands. |
| 3311 | * Set curbuf to the buffer to be written. |
| 3312 | * Careful: The autocommands may call buf_write() recursively! |
| 3313 | */ |
| 3314 | if (ffname == buf->b_ffname) |
| 3315 | buf_ffname = TRUE; |
| 3316 | if (sfname == buf->b_sfname) |
| 3317 | buf_sfname = TRUE; |
| 3318 | if (fname == buf->b_ffname) |
| 3319 | buf_fname_f = TRUE; |
| 3320 | if (fname == buf->b_sfname) |
| 3321 | buf_fname_s = TRUE; |
| 3322 | |
| 3323 | /* set curwin/curbuf to buf and save a few things */ |
| 3324 | aucmd_prepbuf(&aco, buf); |
| 3325 | |
| 3326 | if (append) |
| 3327 | { |
| 3328 | if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD, |
| 3329 | sfname, sfname, FALSE, curbuf, eap))) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3330 | { |
Bram Moolenaar | b1b715d | 2006-01-21 22:09:43 +0000 | [diff] [blame] | 3331 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 3332 | if (overwriting && bt_nofile(curbuf)) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3333 | nofile_err = TRUE; |
| 3334 | else |
Bram Moolenaar | b1b715d | 2006-01-21 22:09:43 +0000 | [diff] [blame] | 3335 | #endif |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3336 | apply_autocmds_exarg(EVENT_FILEAPPENDPRE, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3337 | sfname, sfname, FALSE, curbuf, eap); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3338 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3339 | } |
| 3340 | else if (filtering) |
| 3341 | { |
| 3342 | apply_autocmds_exarg(EVENT_FILTERWRITEPRE, |
| 3343 | NULL, sfname, FALSE, curbuf, eap); |
| 3344 | } |
| 3345 | else if (reset_changed && whole) |
| 3346 | { |
| 3347 | if (!(did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD, |
| 3348 | sfname, sfname, FALSE, curbuf, eap))) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3349 | { |
Bram Moolenaar | b1b715d | 2006-01-21 22:09:43 +0000 | [diff] [blame] | 3350 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 3351 | if (overwriting && bt_nofile(curbuf)) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3352 | nofile_err = TRUE; |
| 3353 | else |
Bram Moolenaar | b1b715d | 2006-01-21 22:09:43 +0000 | [diff] [blame] | 3354 | #endif |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3355 | apply_autocmds_exarg(EVENT_BUFWRITEPRE, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3356 | sfname, sfname, FALSE, curbuf, eap); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3357 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3358 | } |
| 3359 | else |
| 3360 | { |
| 3361 | if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD, |
| 3362 | sfname, sfname, FALSE, curbuf, eap))) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3363 | { |
Bram Moolenaar | b1b715d | 2006-01-21 22:09:43 +0000 | [diff] [blame] | 3364 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 3365 | if (overwriting && bt_nofile(curbuf)) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3366 | nofile_err = TRUE; |
| 3367 | else |
Bram Moolenaar | b1b715d | 2006-01-21 22:09:43 +0000 | [diff] [blame] | 3368 | #endif |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3369 | apply_autocmds_exarg(EVENT_FILEWRITEPRE, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3370 | sfname, sfname, FALSE, curbuf, eap); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3371 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3372 | } |
| 3373 | |
| 3374 | /* restore curwin/curbuf and a few other things */ |
| 3375 | aucmd_restbuf(&aco); |
| 3376 | |
| 3377 | /* |
| 3378 | * In three situations we return here and don't write the file: |
| 3379 | * 1. the autocommands deleted or unloaded the buffer. |
| 3380 | * 2. The autocommands abort script processing. |
| 3381 | * 3. If one of the "Cmd" autocommands was executed. |
| 3382 | */ |
| 3383 | if (!buf_valid(buf)) |
| 3384 | buf = NULL; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 3385 | if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline) |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 3386 | || did_cmd || nofile_err |
| 3387 | #ifdef FEAT_EVAL |
| 3388 | || aborting() |
| 3389 | #endif |
| 3390 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3391 | { |
| 3392 | --no_wait_return; |
| 3393 | msg_scroll = msg_save; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3394 | if (nofile_err) |
| 3395 | EMSG(_("E676: No matching autocommands for acwrite buffer")); |
| 3396 | |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 3397 | if (nofile_err |
| 3398 | #ifdef FEAT_EVAL |
| 3399 | || aborting() |
| 3400 | #endif |
| 3401 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3402 | /* An aborting error, interrupt or exception in the |
| 3403 | * autocommands. */ |
| 3404 | return FAIL; |
| 3405 | if (did_cmd) |
| 3406 | { |
| 3407 | if (buf == NULL) |
| 3408 | /* The buffer was deleted. We assume it was written |
| 3409 | * (can't retry anyway). */ |
| 3410 | return OK; |
| 3411 | if (overwriting) |
| 3412 | { |
| 3413 | /* Assume the buffer was written, update the timestamp. */ |
| 3414 | ml_timestamp(buf); |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3415 | if (append) |
| 3416 | buf->b_flags &= ~BF_NEW; |
| 3417 | else |
| 3418 | buf->b_flags &= ~BF_WRITE_MASK; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3419 | } |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 3420 | if (reset_changed && buf->b_changed && !append |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 3421 | && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3422 | /* Buffer still changed, the autocommands didn't work |
| 3423 | * properly. */ |
| 3424 | return FAIL; |
| 3425 | return OK; |
| 3426 | } |
| 3427 | #ifdef FEAT_EVAL |
| 3428 | if (!aborting()) |
| 3429 | #endif |
| 3430 | EMSG(_("E203: Autocommands deleted or unloaded buffer to be written")); |
| 3431 | return FAIL; |
| 3432 | } |
| 3433 | |
| 3434 | /* |
| 3435 | * The autocommands may have changed the number of lines in the file. |
| 3436 | * When writing the whole file, adjust the end. |
| 3437 | * When writing part of the file, assume that the autocommands only |
| 3438 | * changed the number of lines that are to be written (tricky!). |
| 3439 | */ |
| 3440 | if (buf->b_ml.ml_line_count != old_line_count) |
| 3441 | { |
| 3442 | if (whole) /* write all */ |
| 3443 | end = buf->b_ml.ml_line_count; |
| 3444 | else if (buf->b_ml.ml_line_count > old_line_count) /* more lines */ |
| 3445 | end += buf->b_ml.ml_line_count - old_line_count; |
| 3446 | else /* less lines */ |
| 3447 | { |
| 3448 | end -= old_line_count - buf->b_ml.ml_line_count; |
| 3449 | if (end < start) |
| 3450 | { |
| 3451 | --no_wait_return; |
| 3452 | msg_scroll = msg_save; |
| 3453 | EMSG(_("E204: Autocommand changed number of lines in unexpected way")); |
| 3454 | return FAIL; |
| 3455 | } |
| 3456 | } |
| 3457 | } |
| 3458 | |
| 3459 | /* |
| 3460 | * The autocommands may have changed the name of the buffer, which may |
| 3461 | * be kept in fname, ffname and sfname. |
| 3462 | */ |
| 3463 | if (buf_ffname) |
| 3464 | ffname = buf->b_ffname; |
| 3465 | if (buf_sfname) |
| 3466 | sfname = buf->b_sfname; |
| 3467 | if (buf_fname_f) |
| 3468 | fname = buf->b_ffname; |
| 3469 | if (buf_fname_s) |
| 3470 | fname = buf->b_sfname; |
| 3471 | } |
| 3472 | #endif |
| 3473 | |
| 3474 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 3475 | if (netbeans_active() && isNetbeansBuffer(buf)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3476 | { |
| 3477 | if (whole) |
| 3478 | { |
| 3479 | /* |
| 3480 | * b_changed can be 0 after an undo, but we still need to write |
| 3481 | * the buffer to NetBeans. |
| 3482 | */ |
| 3483 | if (buf->b_changed || isNetbeansModified(buf)) |
| 3484 | { |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3485 | --no_wait_return; /* may wait for return now */ |
| 3486 | msg_scroll = msg_save; |
| 3487 | netbeans_save_buffer(buf); /* no error checking... */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3488 | return retval; |
| 3489 | } |
| 3490 | else |
| 3491 | { |
| 3492 | errnum = (char_u *)"E656: "; |
Bram Moolenaar | ed0e745 | 2008-06-27 19:17:34 +0000 | [diff] [blame] | 3493 | errmsg = (char_u *)_("NetBeans disallows writes of unmodified buffers"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3494 | buffer = NULL; |
| 3495 | goto fail; |
| 3496 | } |
| 3497 | } |
| 3498 | else |
| 3499 | { |
| 3500 | errnum = (char_u *)"E657: "; |
| 3501 | errmsg = (char_u *)_("Partial writes disallowed for NetBeans buffers"); |
| 3502 | buffer = NULL; |
| 3503 | goto fail; |
| 3504 | } |
| 3505 | } |
| 3506 | #endif |
| 3507 | |
| 3508 | if (shortmess(SHM_OVER) && !exiting) |
| 3509 | msg_scroll = FALSE; /* overwrite previous file message */ |
| 3510 | else |
| 3511 | msg_scroll = TRUE; /* don't overwrite previous file message */ |
| 3512 | if (!filtering) |
| 3513 | filemess(buf, |
| 3514 | #ifndef UNIX |
| 3515 | sfname, |
| 3516 | #else |
| 3517 | fname, |
| 3518 | #endif |
| 3519 | (char_u *)"", 0); /* show that we are busy */ |
| 3520 | msg_scroll = FALSE; /* always overwrite the file message now */ |
| 3521 | |
| 3522 | buffer = alloc(BUFSIZE); |
| 3523 | if (buffer == NULL) /* can't allocate big buffer, use small |
| 3524 | * one (to be able to write when out of |
| 3525 | * memory) */ |
| 3526 | { |
| 3527 | buffer = smallbuf; |
| 3528 | bufsize = SMBUFSIZE; |
| 3529 | } |
| 3530 | else |
| 3531 | bufsize = BUFSIZE; |
| 3532 | |
| 3533 | /* |
| 3534 | * Get information about original file (if there is one). |
| 3535 | */ |
| 3536 | #if defined(UNIX) && !defined(ARCHIE) |
Bram Moolenaar | 6f19245 | 2007-11-08 19:49:02 +0000 | [diff] [blame] | 3537 | st_old.st_dev = 0; |
| 3538 | st_old.st_ino = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3539 | perm = -1; |
| 3540 | if (mch_stat((char *)fname, &st_old) < 0) |
| 3541 | newfile = TRUE; |
| 3542 | else |
| 3543 | { |
| 3544 | perm = st_old.st_mode; |
| 3545 | if (!S_ISREG(st_old.st_mode)) /* not a file */ |
| 3546 | { |
| 3547 | if (S_ISDIR(st_old.st_mode)) |
| 3548 | { |
| 3549 | errnum = (char_u *)"E502: "; |
| 3550 | errmsg = (char_u *)_("is a directory"); |
| 3551 | goto fail; |
| 3552 | } |
| 3553 | if (mch_nodetype(fname) != NODE_WRITABLE) |
| 3554 | { |
| 3555 | errnum = (char_u *)"E503: "; |
| 3556 | errmsg = (char_u *)_("is not a file or writable device"); |
| 3557 | goto fail; |
| 3558 | } |
| 3559 | /* It's a device of some kind (or a fifo) which we can write to |
| 3560 | * but for which we can't make a backup. */ |
| 3561 | device = TRUE; |
| 3562 | newfile = TRUE; |
| 3563 | perm = -1; |
| 3564 | } |
| 3565 | } |
| 3566 | #else /* !UNIX */ |
| 3567 | /* |
| 3568 | * Check for a writable device name. |
| 3569 | */ |
| 3570 | c = mch_nodetype(fname); |
| 3571 | if (c == NODE_OTHER) |
| 3572 | { |
| 3573 | errnum = (char_u *)"E503: "; |
| 3574 | errmsg = (char_u *)_("is not a file or writable device"); |
| 3575 | goto fail; |
| 3576 | } |
| 3577 | if (c == NODE_WRITABLE) |
| 3578 | { |
Bram Moolenaar | 043545e | 2006-10-10 16:44:07 +0000 | [diff] [blame] | 3579 | # if defined(MSDOS) || defined(MSWIN) || defined(OS2) |
| 3580 | /* MS-Windows allows opening a device, but we will probably get stuck |
| 3581 | * trying to write to it. */ |
| 3582 | if (!p_odev) |
| 3583 | { |
| 3584 | errnum = (char_u *)"E796: "; |
| 3585 | errmsg = (char_u *)_("writing to device disabled with 'opendevice' option"); |
| 3586 | goto fail; |
| 3587 | } |
| 3588 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3589 | device = TRUE; |
| 3590 | newfile = TRUE; |
| 3591 | perm = -1; |
| 3592 | } |
| 3593 | else |
| 3594 | { |
| 3595 | perm = mch_getperm(fname); |
| 3596 | if (perm < 0) |
| 3597 | newfile = TRUE; |
| 3598 | else if (mch_isdir(fname)) |
| 3599 | { |
| 3600 | errnum = (char_u *)"E502: "; |
| 3601 | errmsg = (char_u *)_("is a directory"); |
| 3602 | goto fail; |
| 3603 | } |
| 3604 | if (overwriting) |
| 3605 | (void)mch_stat((char *)fname, &st_old); |
| 3606 | } |
| 3607 | #endif /* !UNIX */ |
| 3608 | |
| 3609 | if (!device && !newfile) |
| 3610 | { |
| 3611 | /* |
| 3612 | * Check if the file is really writable (when renaming the file to |
| 3613 | * make a backup we won't discover it later). |
| 3614 | */ |
Bram Moolenaar | 5386a12 | 2007-06-28 20:02:32 +0000 | [diff] [blame] | 3615 | file_readonly = check_file_readonly(fname, (int)perm); |
| 3616 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3617 | if (!forceit && file_readonly) |
| 3618 | { |
| 3619 | if (vim_strchr(p_cpo, CPO_FWRITE) != NULL) |
| 3620 | { |
| 3621 | errnum = (char_u *)"E504: "; |
| 3622 | errmsg = (char_u *)_(err_readonly); |
| 3623 | } |
| 3624 | else |
| 3625 | { |
| 3626 | errnum = (char_u *)"E505: "; |
| 3627 | errmsg = (char_u *)_("is read-only (add ! to override)"); |
| 3628 | } |
| 3629 | goto fail; |
| 3630 | } |
| 3631 | |
| 3632 | /* |
| 3633 | * Check if the timestamp hasn't changed since reading the file. |
| 3634 | */ |
| 3635 | if (overwriting) |
| 3636 | { |
| 3637 | retval = check_mtime(buf, &st_old); |
| 3638 | if (retval == FAIL) |
| 3639 | goto fail; |
| 3640 | } |
| 3641 | } |
| 3642 | |
| 3643 | #ifdef HAVE_ACL |
| 3644 | /* |
| 3645 | * For systems that support ACL: get the ACL from the original file. |
| 3646 | */ |
| 3647 | if (!newfile) |
| 3648 | acl = mch_get_acl(fname); |
| 3649 | #endif |
| 3650 | |
| 3651 | /* |
| 3652 | * If 'backupskip' is not empty, don't make a backup for some files. |
| 3653 | */ |
| 3654 | dobackup = (p_wb || p_bk || *p_pm != NUL); |
| 3655 | #ifdef FEAT_WILDIGN |
| 3656 | if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname)) |
| 3657 | dobackup = FALSE; |
| 3658 | #endif |
| 3659 | |
| 3660 | /* |
| 3661 | * Save the value of got_int and reset it. We don't want a previous |
| 3662 | * interruption cancel writing, only hitting CTRL-C while writing should |
| 3663 | * abort it. |
| 3664 | */ |
| 3665 | prev_got_int = got_int; |
| 3666 | got_int = FALSE; |
| 3667 | |
| 3668 | /* Mark the buffer as 'being saved' to prevent changed buffer warnings */ |
| 3669 | buf->b_saving = TRUE; |
| 3670 | |
| 3671 | /* |
| 3672 | * If we are not appending or filtering, the file exists, and the |
| 3673 | * 'writebackup', 'backup' or 'patchmode' option is set, need a backup. |
| 3674 | * When 'patchmode' is set also make a backup when appending. |
| 3675 | * |
| 3676 | * Do not make any backup, if 'writebackup' and 'backup' are both switched |
| 3677 | * off. This helps when editing large files on almost-full disks. |
| 3678 | */ |
| 3679 | if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup) |
| 3680 | { |
| 3681 | #if defined(UNIX) || defined(WIN32) |
| 3682 | struct stat st; |
| 3683 | #endif |
| 3684 | |
| 3685 | if ((bkc_flags & BKC_YES) || append) /* "yes" */ |
| 3686 | backup_copy = TRUE; |
| 3687 | #if defined(UNIX) || defined(WIN32) |
| 3688 | else if ((bkc_flags & BKC_AUTO)) /* "auto" */ |
| 3689 | { |
| 3690 | int i; |
| 3691 | |
| 3692 | # ifdef UNIX |
| 3693 | /* |
| 3694 | * Don't rename the file when: |
| 3695 | * - it's a hard link |
| 3696 | * - it's a symbolic link |
| 3697 | * - we don't have write permission in the directory |
| 3698 | * - we can't set the owner/group of the new file |
| 3699 | */ |
| 3700 | if (st_old.st_nlink > 1 |
| 3701 | || mch_lstat((char *)fname, &st) < 0 |
| 3702 | || st.st_dev != st_old.st_dev |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3703 | || st.st_ino != st_old.st_ino |
| 3704 | # ifndef HAVE_FCHOWN |
| 3705 | || st.st_uid != st_old.st_uid |
| 3706 | || st.st_gid != st_old.st_gid |
| 3707 | # endif |
| 3708 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3709 | backup_copy = TRUE; |
| 3710 | else |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 3711 | # else |
| 3712 | # ifdef WIN32 |
| 3713 | /* On NTFS file systems hard links are possible. */ |
| 3714 | if (mch_is_linked(fname)) |
| 3715 | backup_copy = TRUE; |
| 3716 | else |
| 3717 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3718 | # endif |
| 3719 | { |
| 3720 | /* |
| 3721 | * Check if we can create a file and set the owner/group to |
| 3722 | * the ones from the original file. |
| 3723 | * First find a file name that doesn't exist yet (use some |
| 3724 | * arbitrary numbers). |
| 3725 | */ |
| 3726 | STRCPY(IObuff, fname); |
| 3727 | for (i = 4913; ; i += 123) |
| 3728 | { |
| 3729 | sprintf((char *)gettail(IObuff), "%d", i); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3730 | if (mch_lstat((char *)IObuff, &st) < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3731 | break; |
| 3732 | } |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3733 | fd = mch_open((char *)IObuff, |
| 3734 | O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3735 | if (fd < 0) /* can't write in directory */ |
| 3736 | backup_copy = TRUE; |
| 3737 | else |
| 3738 | { |
| 3739 | # ifdef UNIX |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3740 | # ifdef HAVE_FCHOWN |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 3741 | ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3742 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3743 | if (mch_stat((char *)IObuff, &st) < 0 |
| 3744 | || st.st_uid != st_old.st_uid |
| 3745 | || st.st_gid != st_old.st_gid |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 3746 | || (long)st.st_mode != perm) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3747 | backup_copy = TRUE; |
| 3748 | # endif |
Bram Moolenaar | 9835862 | 2005-11-28 22:58:23 +0000 | [diff] [blame] | 3749 | /* Close the file before removing it, on MS-Windows we |
| 3750 | * can't delete an open file. */ |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3751 | close(fd); |
Bram Moolenaar | 9835862 | 2005-11-28 22:58:23 +0000 | [diff] [blame] | 3752 | mch_remove(IObuff); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3753 | } |
| 3754 | } |
| 3755 | } |
| 3756 | |
| 3757 | # ifdef UNIX |
| 3758 | /* |
| 3759 | * Break symlinks and/or hardlinks if we've been asked to. |
| 3760 | */ |
| 3761 | if ((bkc_flags & BKC_BREAKSYMLINK) || (bkc_flags & BKC_BREAKHARDLINK)) |
| 3762 | { |
| 3763 | int lstat_res; |
| 3764 | |
| 3765 | lstat_res = mch_lstat((char *)fname, &st); |
| 3766 | |
| 3767 | /* Symlinks. */ |
| 3768 | if ((bkc_flags & BKC_BREAKSYMLINK) |
| 3769 | && lstat_res == 0 |
| 3770 | && st.st_ino != st_old.st_ino) |
| 3771 | backup_copy = FALSE; |
| 3772 | |
| 3773 | /* Hardlinks. */ |
| 3774 | if ((bkc_flags & BKC_BREAKHARDLINK) |
| 3775 | && st_old.st_nlink > 1 |
| 3776 | && (lstat_res != 0 || st.st_ino == st_old.st_ino)) |
| 3777 | backup_copy = FALSE; |
| 3778 | } |
| 3779 | #endif |
| 3780 | |
| 3781 | #endif |
| 3782 | |
| 3783 | /* make sure we have a valid backup extension to use */ |
| 3784 | if (*p_bex == NUL) |
| 3785 | { |
| 3786 | #ifdef RISCOS |
| 3787 | backup_ext = (char_u *)"/bak"; |
| 3788 | #else |
| 3789 | backup_ext = (char_u *)".bak"; |
| 3790 | #endif |
| 3791 | } |
| 3792 | else |
| 3793 | backup_ext = p_bex; |
| 3794 | |
| 3795 | if (backup_copy |
| 3796 | && (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) >= 0) |
| 3797 | { |
| 3798 | int bfd; |
| 3799 | char_u *copybuf, *wp; |
| 3800 | int some_error = FALSE; |
| 3801 | struct stat st_new; |
| 3802 | char_u *dirp; |
| 3803 | char_u *rootname; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3804 | #if defined(UNIX) && !defined(SHORT_FNAME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3805 | int did_set_shortname; |
| 3806 | #endif |
| 3807 | |
| 3808 | copybuf = alloc(BUFSIZE + 1); |
| 3809 | if (copybuf == NULL) |
| 3810 | { |
| 3811 | some_error = TRUE; /* out of memory */ |
| 3812 | goto nobackup; |
| 3813 | } |
| 3814 | |
| 3815 | /* |
| 3816 | * Try to make the backup in each directory in the 'bdir' option. |
| 3817 | * |
| 3818 | * Unix semantics has it, that we may have a writable file, |
| 3819 | * that cannot be recreated with a simple open(..., O_CREAT, ) e.g: |
| 3820 | * - the directory is not writable, |
| 3821 | * - the file may be a symbolic link, |
| 3822 | * - the file may belong to another user/group, etc. |
| 3823 | * |
| 3824 | * For these reasons, the existing writable file must be truncated |
| 3825 | * and reused. Creation of a backup COPY will be attempted. |
| 3826 | */ |
| 3827 | dirp = p_bdir; |
| 3828 | while (*dirp) |
| 3829 | { |
| 3830 | #ifdef UNIX |
| 3831 | st_new.st_ino = 0; |
| 3832 | st_new.st_dev = 0; |
| 3833 | st_new.st_gid = 0; |
| 3834 | #endif |
| 3835 | |
| 3836 | /* |
| 3837 | * Isolate one directory name, using an entry in 'bdir'. |
| 3838 | */ |
| 3839 | (void)copy_option_part(&dirp, copybuf, BUFSIZE, ","); |
| 3840 | rootname = get_file_in_dir(fname, copybuf); |
| 3841 | if (rootname == NULL) |
| 3842 | { |
| 3843 | some_error = TRUE; /* out of memory */ |
| 3844 | goto nobackup; |
| 3845 | } |
| 3846 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3847 | #if defined(UNIX) && !defined(SHORT_FNAME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3848 | did_set_shortname = FALSE; |
| 3849 | #endif |
| 3850 | |
| 3851 | /* |
| 3852 | * May try twice if 'shortname' not set. |
| 3853 | */ |
| 3854 | for (;;) |
| 3855 | { |
| 3856 | /* |
| 3857 | * Make backup file name. |
| 3858 | */ |
| 3859 | backup = buf_modname( |
| 3860 | #ifdef SHORT_FNAME |
| 3861 | TRUE, |
| 3862 | #else |
| 3863 | (buf->b_p_sn || buf->b_shortname), |
| 3864 | #endif |
| 3865 | rootname, backup_ext, FALSE); |
| 3866 | if (backup == NULL) |
| 3867 | { |
| 3868 | vim_free(rootname); |
| 3869 | some_error = TRUE; /* out of memory */ |
| 3870 | goto nobackup; |
| 3871 | } |
| 3872 | |
| 3873 | /* |
| 3874 | * Check if backup file already exists. |
| 3875 | */ |
| 3876 | if (mch_stat((char *)backup, &st_new) >= 0) |
| 3877 | { |
| 3878 | #ifdef UNIX |
| 3879 | /* |
| 3880 | * Check if backup file is same as original file. |
| 3881 | * May happen when modname() gave the same file back. |
| 3882 | * E.g. silly link, or file name-length reached. |
| 3883 | * If we don't check here, we either ruin the file |
| 3884 | * when copying or erase it after writing. jw. |
| 3885 | */ |
| 3886 | if (st_new.st_dev == st_old.st_dev |
| 3887 | && st_new.st_ino == st_old.st_ino) |
| 3888 | { |
| 3889 | vim_free(backup); |
| 3890 | backup = NULL; /* no backup file to delete */ |
| 3891 | # ifndef SHORT_FNAME |
| 3892 | /* |
| 3893 | * may try again with 'shortname' set |
| 3894 | */ |
| 3895 | if (!(buf->b_shortname || buf->b_p_sn)) |
| 3896 | { |
| 3897 | buf->b_shortname = TRUE; |
| 3898 | did_set_shortname = TRUE; |
| 3899 | continue; |
| 3900 | } |
| 3901 | /* setting shortname didn't help */ |
| 3902 | if (did_set_shortname) |
| 3903 | buf->b_shortname = FALSE; |
| 3904 | # endif |
| 3905 | break; |
| 3906 | } |
| 3907 | #endif |
| 3908 | |
| 3909 | /* |
| 3910 | * If we are not going to keep the backup file, don't |
| 3911 | * delete an existing one, try to use another name. |
| 3912 | * Change one character, just before the extension. |
| 3913 | */ |
| 3914 | if (!p_bk) |
| 3915 | { |
| 3916 | wp = backup + STRLEN(backup) - 1 |
| 3917 | - STRLEN(backup_ext); |
| 3918 | if (wp < backup) /* empty file name ??? */ |
| 3919 | wp = backup; |
| 3920 | *wp = 'z'; |
| 3921 | while (*wp > 'a' |
| 3922 | && mch_stat((char *)backup, &st_new) >= 0) |
| 3923 | --*wp; |
| 3924 | /* They all exist??? Must be something wrong. */ |
| 3925 | if (*wp == 'a') |
| 3926 | { |
| 3927 | vim_free(backup); |
| 3928 | backup = NULL; |
| 3929 | } |
| 3930 | } |
| 3931 | } |
| 3932 | break; |
| 3933 | } |
| 3934 | vim_free(rootname); |
| 3935 | |
| 3936 | /* |
| 3937 | * Try to create the backup file |
| 3938 | */ |
| 3939 | if (backup != NULL) |
| 3940 | { |
| 3941 | /* remove old backup, if present */ |
| 3942 | mch_remove(backup); |
| 3943 | /* Open with O_EXCL to avoid the file being created while |
| 3944 | * we were sleeping (symlink hacker attack?) */ |
| 3945 | bfd = mch_open((char *)backup, |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3946 | O_WRONLY|O_CREAT|O_EXTRA|O_EXCL|O_NOFOLLOW, |
| 3947 | perm & 0777); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3948 | if (bfd < 0) |
| 3949 | { |
| 3950 | vim_free(backup); |
| 3951 | backup = NULL; |
| 3952 | } |
| 3953 | else |
| 3954 | { |
| 3955 | /* set file protection same as original file, but |
| 3956 | * strip s-bit */ |
| 3957 | (void)mch_setperm(backup, perm & 0777); |
| 3958 | |
| 3959 | #ifdef UNIX |
| 3960 | /* |
| 3961 | * Try to set the group of the backup same as the |
| 3962 | * original file. If this fails, set the protection |
| 3963 | * bits for the group same as the protection bits for |
| 3964 | * others. |
| 3965 | */ |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3966 | if (st_new.st_gid != st_old.st_gid |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3967 | # ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */ |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3968 | && fchown(bfd, (uid_t)-1, st_old.st_gid) != 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3969 | # endif |
| 3970 | ) |
| 3971 | mch_setperm(backup, |
| 3972 | (perm & 0707) | ((perm & 07) << 3)); |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 3973 | # ifdef HAVE_SELINUX |
| 3974 | mch_copy_sec(fname, backup); |
| 3975 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3976 | #endif |
| 3977 | |
| 3978 | /* |
| 3979 | * copy the file. |
| 3980 | */ |
| 3981 | write_info.bw_fd = bfd; |
| 3982 | write_info.bw_buf = copybuf; |
| 3983 | #ifdef HAS_BW_FLAGS |
| 3984 | write_info.bw_flags = FIO_NOCONVERT; |
| 3985 | #endif |
| 3986 | while ((write_info.bw_len = vim_read(fd, copybuf, |
| 3987 | BUFSIZE)) > 0) |
| 3988 | { |
| 3989 | if (buf_write_bytes(&write_info) == FAIL) |
| 3990 | { |
| 3991 | errmsg = (char_u *)_("E506: Can't write to backup file (add ! to override)"); |
| 3992 | break; |
| 3993 | } |
| 3994 | ui_breakcheck(); |
| 3995 | if (got_int) |
| 3996 | { |
| 3997 | errmsg = (char_u *)_(e_interr); |
| 3998 | break; |
| 3999 | } |
| 4000 | } |
| 4001 | |
| 4002 | if (close(bfd) < 0 && errmsg == NULL) |
| 4003 | errmsg = (char_u *)_("E507: Close error for backup file (add ! to override)"); |
| 4004 | if (write_info.bw_len < 0) |
| 4005 | errmsg = (char_u *)_("E508: Can't read file for backup (add ! to override)"); |
| 4006 | #ifdef UNIX |
| 4007 | set_file_time(backup, st_old.st_atime, st_old.st_mtime); |
| 4008 | #endif |
| 4009 | #ifdef HAVE_ACL |
| 4010 | mch_set_acl(backup, acl); |
| 4011 | #endif |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 4012 | #ifdef HAVE_SELINUX |
| 4013 | mch_copy_sec(fname, backup); |
| 4014 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4015 | break; |
| 4016 | } |
| 4017 | } |
| 4018 | } |
| 4019 | nobackup: |
| 4020 | close(fd); /* ignore errors for closing read file */ |
| 4021 | vim_free(copybuf); |
| 4022 | |
| 4023 | if (backup == NULL && errmsg == NULL) |
| 4024 | errmsg = (char_u *)_("E509: Cannot create backup file (add ! to override)"); |
| 4025 | /* ignore errors when forceit is TRUE */ |
| 4026 | if ((some_error || errmsg != NULL) && !forceit) |
| 4027 | { |
| 4028 | retval = FAIL; |
| 4029 | goto fail; |
| 4030 | } |
| 4031 | errmsg = NULL; |
| 4032 | } |
| 4033 | else |
| 4034 | { |
| 4035 | char_u *dirp; |
| 4036 | char_u *p; |
| 4037 | char_u *rootname; |
| 4038 | |
| 4039 | /* |
| 4040 | * Make a backup by renaming the original file. |
| 4041 | */ |
| 4042 | /* |
| 4043 | * If 'cpoptions' includes the "W" flag, we don't want to |
| 4044 | * overwrite a read-only file. But rename may be possible |
| 4045 | * anyway, thus we need an extra check here. |
| 4046 | */ |
| 4047 | if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL) |
| 4048 | { |
| 4049 | errnum = (char_u *)"E504: "; |
| 4050 | errmsg = (char_u *)_(err_readonly); |
| 4051 | goto fail; |
| 4052 | } |
| 4053 | |
| 4054 | /* |
| 4055 | * |
| 4056 | * Form the backup file name - change path/fo.o.h to |
| 4057 | * path/fo.o.h.bak Try all directories in 'backupdir', first one |
| 4058 | * that works is used. |
| 4059 | */ |
| 4060 | dirp = p_bdir; |
| 4061 | while (*dirp) |
| 4062 | { |
| 4063 | /* |
| 4064 | * Isolate one directory name and make the backup file name. |
| 4065 | */ |
| 4066 | (void)copy_option_part(&dirp, IObuff, IOSIZE, ","); |
| 4067 | rootname = get_file_in_dir(fname, IObuff); |
| 4068 | if (rootname == NULL) |
| 4069 | backup = NULL; |
| 4070 | else |
| 4071 | { |
| 4072 | backup = buf_modname( |
| 4073 | #ifdef SHORT_FNAME |
| 4074 | TRUE, |
| 4075 | #else |
| 4076 | (buf->b_p_sn || buf->b_shortname), |
| 4077 | #endif |
| 4078 | rootname, backup_ext, FALSE); |
| 4079 | vim_free(rootname); |
| 4080 | } |
| 4081 | |
| 4082 | if (backup != NULL) |
| 4083 | { |
| 4084 | /* |
| 4085 | * If we are not going to keep the backup file, don't |
| 4086 | * delete an existing one, try to use another name. |
| 4087 | * Change one character, just before the extension. |
| 4088 | */ |
| 4089 | if (!p_bk && mch_getperm(backup) >= 0) |
| 4090 | { |
| 4091 | p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext); |
| 4092 | if (p < backup) /* empty file name ??? */ |
| 4093 | p = backup; |
| 4094 | *p = 'z'; |
| 4095 | while (*p > 'a' && mch_getperm(backup) >= 0) |
| 4096 | --*p; |
| 4097 | /* They all exist??? Must be something wrong! */ |
| 4098 | if (*p == 'a') |
| 4099 | { |
| 4100 | vim_free(backup); |
| 4101 | backup = NULL; |
| 4102 | } |
| 4103 | } |
| 4104 | } |
| 4105 | if (backup != NULL) |
| 4106 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4107 | /* |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4108 | * Delete any existing backup and move the current version |
| 4109 | * to the backup. For safety, we don't remove the backup |
| 4110 | * until the write has finished successfully. And if the |
| 4111 | * 'backup' option is set, leave it around. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4112 | */ |
| 4113 | /* |
| 4114 | * If the renaming of the original file to the backup file |
| 4115 | * works, quit here. |
| 4116 | */ |
| 4117 | if (vim_rename(fname, backup) == 0) |
| 4118 | break; |
| 4119 | |
| 4120 | vim_free(backup); /* don't do the rename below */ |
| 4121 | backup = NULL; |
| 4122 | } |
| 4123 | } |
| 4124 | if (backup == NULL && !forceit) |
| 4125 | { |
| 4126 | errmsg = (char_u *)_("E510: Can't make backup file (add ! to override)"); |
| 4127 | goto fail; |
| 4128 | } |
| 4129 | } |
| 4130 | } |
| 4131 | |
| 4132 | #if defined(UNIX) && !defined(ARCHIE) |
| 4133 | /* When using ":w!" and the file was read-only: make it writable */ |
| 4134 | if (forceit && perm >= 0 && !(perm & 0200) && st_old.st_uid == getuid() |
| 4135 | && vim_strchr(p_cpo, CPO_FWRITE) == NULL) |
| 4136 | { |
| 4137 | perm |= 0200; |
| 4138 | (void)mch_setperm(fname, perm); |
| 4139 | made_writable = TRUE; |
| 4140 | } |
| 4141 | #endif |
| 4142 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 4143 | /* When using ":w!" and writing to the current file, 'readonly' makes no |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 4144 | * sense, reset it, unless 'Z' appears in 'cpoptions'. */ |
| 4145 | if (forceit && overwriting && vim_strchr(p_cpo, CPO_KEEPRO) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4146 | { |
| 4147 | buf->b_p_ro = FALSE; |
| 4148 | #ifdef FEAT_TITLE |
| 4149 | need_maketitle = TRUE; /* set window title later */ |
| 4150 | #endif |
| 4151 | #ifdef FEAT_WINDOWS |
| 4152 | status_redraw_all(); /* redraw status lines later */ |
| 4153 | #endif |
| 4154 | } |
| 4155 | |
| 4156 | if (end > buf->b_ml.ml_line_count) |
| 4157 | end = buf->b_ml.ml_line_count; |
| 4158 | if (buf->b_ml.ml_flags & ML_EMPTY) |
| 4159 | start = end + 1; |
| 4160 | |
| 4161 | /* |
| 4162 | * If the original file is being overwritten, there is a small chance that |
| 4163 | * we crash in the middle of writing. Therefore the file is preserved now. |
| 4164 | * This makes all block numbers positive so that recovery does not need |
| 4165 | * the original file. |
| 4166 | * Don't do this if there is a backup file and we are exiting. |
| 4167 | */ |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4168 | if (reset_changed && !newfile && overwriting |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4169 | && !(exiting && backup != NULL)) |
| 4170 | { |
| 4171 | ml_preserve(buf, FALSE); |
| 4172 | if (got_int) |
| 4173 | { |
| 4174 | errmsg = (char_u *)_(e_interr); |
| 4175 | goto restore_backup; |
| 4176 | } |
| 4177 | } |
| 4178 | |
| 4179 | #ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */ |
| 4180 | /* |
| 4181 | * Before risking to lose the original file verify if there's |
| 4182 | * a resource fork to preserve, and if cannot be done warn |
| 4183 | * the users. This happens when overwriting without backups. |
| 4184 | */ |
| 4185 | if (backup == NULL && overwriting && !append) |
| 4186 | if (mch_has_resource_fork(fname)) |
| 4187 | { |
| 4188 | errmsg = (char_u *)_("E460: The resource fork would be lost (add ! to override)"); |
| 4189 | goto restore_backup; |
| 4190 | } |
| 4191 | #endif |
| 4192 | |
| 4193 | #ifdef VMS |
| 4194 | vms_remove_version(fname); /* remove version */ |
| 4195 | #endif |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 4196 | /* Default: write the file directly. May write to a temp file for |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4197 | * multi-byte conversion. */ |
| 4198 | wfname = fname; |
| 4199 | |
| 4200 | #ifdef FEAT_MBYTE |
| 4201 | /* Check for forced 'fileencoding' from "++opt=val" argument. */ |
| 4202 | if (eap != NULL && eap->force_enc != 0) |
| 4203 | { |
| 4204 | fenc = eap->cmd + eap->force_enc; |
| 4205 | fenc = enc_canonize(fenc); |
| 4206 | fenc_tofree = fenc; |
| 4207 | } |
| 4208 | else |
| 4209 | fenc = buf->b_p_fenc; |
| 4210 | |
| 4211 | /* |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 4212 | * Check if the file needs to be converted. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4213 | */ |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 4214 | converted = need_conversion(fenc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4215 | |
| 4216 | /* |
| 4217 | * Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done. Or |
| 4218 | * Latin1 to Unicode conversion. This is handled in buf_write_bytes(). |
| 4219 | * Prepare the flags for it and allocate bw_conv_buf when needed. |
| 4220 | */ |
| 4221 | if (converted && (enc_utf8 || STRCMP(p_enc, "latin1") == 0)) |
| 4222 | { |
| 4223 | wb_flags = get_fio_flags(fenc); |
| 4224 | if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8)) |
| 4225 | { |
| 4226 | /* Need to allocate a buffer to translate into. */ |
| 4227 | if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8)) |
| 4228 | write_info.bw_conv_buflen = bufsize * 2; |
| 4229 | else /* FIO_UCS4 */ |
| 4230 | write_info.bw_conv_buflen = bufsize * 4; |
| 4231 | write_info.bw_conv_buf |
| 4232 | = lalloc((long_u)write_info.bw_conv_buflen, TRUE); |
| 4233 | if (write_info.bw_conv_buf == NULL) |
| 4234 | end = 0; |
| 4235 | } |
| 4236 | } |
| 4237 | |
| 4238 | # ifdef WIN3264 |
| 4239 | if (converted && wb_flags == 0 && (wb_flags = get_win_fio_flags(fenc)) != 0) |
| 4240 | { |
| 4241 | /* Convert UTF-8 -> UCS-2 and UCS-2 -> DBCS. Worst-case * 4: */ |
| 4242 | write_info.bw_conv_buflen = bufsize * 4; |
| 4243 | write_info.bw_conv_buf |
| 4244 | = lalloc((long_u)write_info.bw_conv_buflen, TRUE); |
| 4245 | if (write_info.bw_conv_buf == NULL) |
| 4246 | end = 0; |
| 4247 | } |
| 4248 | # endif |
| 4249 | |
| 4250 | # ifdef MACOS_X |
| 4251 | if (converted && wb_flags == 0 && (wb_flags = get_mac_fio_flags(fenc)) != 0) |
| 4252 | { |
| 4253 | write_info.bw_conv_buflen = bufsize * 3; |
| 4254 | write_info.bw_conv_buf |
| 4255 | = lalloc((long_u)write_info.bw_conv_buflen, TRUE); |
| 4256 | if (write_info.bw_conv_buf == NULL) |
| 4257 | end = 0; |
| 4258 | } |
| 4259 | # endif |
| 4260 | |
| 4261 | # if defined(FEAT_EVAL) || defined(USE_ICONV) |
| 4262 | if (converted && wb_flags == 0) |
| 4263 | { |
| 4264 | # ifdef USE_ICONV |
| 4265 | /* |
| 4266 | * Use iconv() conversion when conversion is needed and it's not done |
| 4267 | * internally. |
| 4268 | */ |
| 4269 | write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc, |
| 4270 | enc_utf8 ? (char_u *)"utf-8" : p_enc); |
| 4271 | if (write_info.bw_iconv_fd != (iconv_t)-1) |
| 4272 | { |
| 4273 | /* We're going to use iconv(), allocate a buffer to convert in. */ |
| 4274 | write_info.bw_conv_buflen = bufsize * ICONV_MULT; |
| 4275 | write_info.bw_conv_buf |
| 4276 | = lalloc((long_u)write_info.bw_conv_buflen, TRUE); |
| 4277 | if (write_info.bw_conv_buf == NULL) |
| 4278 | end = 0; |
| 4279 | write_info.bw_first = TRUE; |
| 4280 | } |
| 4281 | # ifdef FEAT_EVAL |
| 4282 | else |
| 4283 | # endif |
| 4284 | # endif |
| 4285 | |
| 4286 | # ifdef FEAT_EVAL |
| 4287 | /* |
| 4288 | * When the file needs to be converted with 'charconvert' after |
| 4289 | * writing, write to a temp file instead and let the conversion |
| 4290 | * overwrite the original file. |
| 4291 | */ |
| 4292 | if (*p_ccv != NUL) |
| 4293 | { |
| 4294 | wfname = vim_tempname('w'); |
| 4295 | if (wfname == NULL) /* Can't write without a tempfile! */ |
| 4296 | { |
| 4297 | errmsg = (char_u *)_("E214: Can't find temp file for writing"); |
| 4298 | goto restore_backup; |
| 4299 | } |
| 4300 | } |
| 4301 | # endif |
| 4302 | } |
| 4303 | # endif |
| 4304 | if (converted && wb_flags == 0 |
| 4305 | # ifdef USE_ICONV |
| 4306 | && write_info.bw_iconv_fd == (iconv_t)-1 |
| 4307 | # endif |
| 4308 | # ifdef FEAT_EVAL |
| 4309 | && wfname == fname |
| 4310 | # endif |
| 4311 | ) |
| 4312 | { |
| 4313 | if (!forceit) |
| 4314 | { |
| 4315 | errmsg = (char_u *)_("E213: Cannot convert (add ! to write without conversion)"); |
| 4316 | goto restore_backup; |
| 4317 | } |
| 4318 | notconverted = TRUE; |
| 4319 | } |
| 4320 | #endif |
| 4321 | |
| 4322 | /* |
| 4323 | * Open the file "wfname" for writing. |
| 4324 | * We may try to open the file twice: If we can't write to the |
| 4325 | * file and forceit is TRUE we delete the existing file and try to create |
| 4326 | * a new one. If this still fails we may have lost the original file! |
| 4327 | * (this may happen when the user reached his quotum for number of files). |
| 4328 | * Appending will fail if the file does not exist and forceit is FALSE. |
| 4329 | */ |
| 4330 | while ((fd = mch_open((char *)wfname, O_WRONLY | O_EXTRA | (append |
| 4331 | ? (forceit ? (O_APPEND | O_CREAT) : O_APPEND) |
| 4332 | : (O_CREAT | O_TRUNC)) |
Bram Moolenaar | 4317d9b | 2005-03-18 20:25:31 +0000 | [diff] [blame] | 4333 | , perm < 0 ? 0666 : (perm & 0777))) < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4334 | { |
| 4335 | /* |
| 4336 | * A forced write will try to create a new file if the old one is |
| 4337 | * still readonly. This may also happen when the directory is |
| 4338 | * read-only. In that case the mch_remove() will fail. |
| 4339 | */ |
| 4340 | if (errmsg == NULL) |
| 4341 | { |
| 4342 | #ifdef UNIX |
| 4343 | struct stat st; |
| 4344 | |
| 4345 | /* Don't delete the file when it's a hard or symbolic link. */ |
| 4346 | if ((!newfile && st_old.st_nlink > 1) |
| 4347 | || (mch_lstat((char *)fname, &st) == 0 |
| 4348 | && (st.st_dev != st_old.st_dev |
| 4349 | || st.st_ino != st_old.st_ino))) |
| 4350 | errmsg = (char_u *)_("E166: Can't open linked file for writing"); |
| 4351 | else |
| 4352 | #endif |
| 4353 | { |
| 4354 | errmsg = (char_u *)_("E212: Can't open file for writing"); |
| 4355 | if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL |
| 4356 | && perm >= 0) |
| 4357 | { |
| 4358 | #ifdef UNIX |
| 4359 | /* we write to the file, thus it should be marked |
| 4360 | writable after all */ |
| 4361 | if (!(perm & 0200)) |
| 4362 | made_writable = TRUE; |
| 4363 | perm |= 0200; |
| 4364 | if (st_old.st_uid != getuid() || st_old.st_gid != getgid()) |
| 4365 | perm &= 0777; |
| 4366 | #endif |
| 4367 | if (!append) /* don't remove when appending */ |
| 4368 | mch_remove(wfname); |
| 4369 | continue; |
| 4370 | } |
| 4371 | } |
| 4372 | } |
| 4373 | |
| 4374 | restore_backup: |
| 4375 | { |
| 4376 | struct stat st; |
| 4377 | |
| 4378 | /* |
| 4379 | * If we failed to open the file, we don't need a backup. Throw it |
| 4380 | * away. If we moved or removed the original file try to put the |
| 4381 | * backup in its place. |
| 4382 | */ |
| 4383 | if (backup != NULL && wfname == fname) |
| 4384 | { |
| 4385 | if (backup_copy) |
| 4386 | { |
| 4387 | /* |
| 4388 | * There is a small chance that we removed the original, |
| 4389 | * try to move the copy in its place. |
| 4390 | * This may not work if the vim_rename() fails. |
| 4391 | * In that case we leave the copy around. |
| 4392 | */ |
| 4393 | /* If file does not exist, put the copy in its place */ |
| 4394 | if (mch_stat((char *)fname, &st) < 0) |
| 4395 | vim_rename(backup, fname); |
| 4396 | /* if original file does exist throw away the copy */ |
| 4397 | if (mch_stat((char *)fname, &st) >= 0) |
| 4398 | mch_remove(backup); |
| 4399 | } |
| 4400 | else |
| 4401 | { |
| 4402 | /* try to put the original file back */ |
| 4403 | vim_rename(backup, fname); |
| 4404 | } |
| 4405 | } |
| 4406 | |
| 4407 | /* if original file no longer exists give an extra warning */ |
| 4408 | if (!newfile && mch_stat((char *)fname, &st) < 0) |
| 4409 | end = 0; |
| 4410 | } |
| 4411 | |
| 4412 | #ifdef FEAT_MBYTE |
| 4413 | if (wfname != fname) |
| 4414 | vim_free(wfname); |
| 4415 | #endif |
| 4416 | goto fail; |
| 4417 | } |
| 4418 | errmsg = NULL; |
| 4419 | |
| 4420 | #if defined(MACOS_CLASSIC) || defined(WIN3264) |
| 4421 | /* TODO: Is it need for MACOS_X? (Dany) */ |
| 4422 | /* |
| 4423 | * On macintosh copy the original files attributes (i.e. the backup) |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 4424 | * This is done in order to preserve the resource fork and the |
| 4425 | * Finder attribute (label, comments, custom icons, file creator) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4426 | */ |
| 4427 | if (backup != NULL && overwriting && !append) |
| 4428 | { |
| 4429 | if (backup_copy) |
| 4430 | (void)mch_copy_file_attribute(wfname, backup); |
| 4431 | else |
| 4432 | (void)mch_copy_file_attribute(backup, wfname); |
| 4433 | } |
| 4434 | |
| 4435 | if (!overwriting && !append) |
| 4436 | { |
| 4437 | if (buf->b_ffname != NULL) |
| 4438 | (void)mch_copy_file_attribute(buf->b_ffname, wfname); |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 4439 | /* Should copy resource fork */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4440 | } |
| 4441 | #endif |
| 4442 | |
| 4443 | write_info.bw_fd = fd; |
| 4444 | |
| 4445 | #ifdef FEAT_CRYPT |
| 4446 | if (*buf->b_p_key && !filtering) |
| 4447 | { |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 4448 | char_u *header; |
| 4449 | int header_len; |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 4450 | |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 4451 | header = prepare_crypt_write(buf, &header_len); |
| 4452 | if (header == NULL) |
| 4453 | end = 0; |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 4454 | else |
| 4455 | { |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 4456 | /* Write magic number, so that Vim knows that this file is |
| 4457 | * encrypted when reading it again. This also undergoes utf-8 to |
| 4458 | * ucs-2/4 conversion when needed. */ |
| 4459 | write_info.bw_buf = header; |
| 4460 | write_info.bw_len = header_len; |
| 4461 | write_info.bw_flags = FIO_NOCONVERT; |
| 4462 | if (buf_write_bytes(&write_info) == FAIL) |
| 4463 | end = 0; |
| 4464 | wb_flags |= FIO_ENCRYPTED; |
| 4465 | vim_free(header); |
Bram Moolenaar | 40e6a71 | 2010-05-16 22:32:54 +0200 | [diff] [blame] | 4466 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4467 | } |
| 4468 | #endif |
| 4469 | |
| 4470 | write_info.bw_buf = buffer; |
| 4471 | nchars = 0; |
| 4472 | |
| 4473 | /* use "++bin", "++nobin" or 'binary' */ |
| 4474 | if (eap != NULL && eap->force_bin != 0) |
| 4475 | write_bin = (eap->force_bin == FORCE_BIN); |
| 4476 | else |
| 4477 | write_bin = buf->b_p_bin; |
| 4478 | |
| 4479 | #ifdef FEAT_MBYTE |
| 4480 | /* |
| 4481 | * The BOM is written just after the encryption magic number. |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 4482 | * Skip it when appending and the file already existed, the BOM only makes |
| 4483 | * sense at the start of the file. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4484 | */ |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 4485 | if (buf->b_p_bomb && !write_bin && (!append || perm < 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4486 | { |
| 4487 | write_info.bw_len = make_bom(buffer, fenc); |
| 4488 | if (write_info.bw_len > 0) |
| 4489 | { |
| 4490 | /* don't convert, do encryption */ |
| 4491 | write_info.bw_flags = FIO_NOCONVERT | wb_flags; |
| 4492 | if (buf_write_bytes(&write_info) == FAIL) |
| 4493 | end = 0; |
| 4494 | else |
| 4495 | nchars += write_info.bw_len; |
| 4496 | } |
| 4497 | } |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 4498 | write_info.bw_start_lnum = start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4499 | #endif |
| 4500 | |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 4501 | #ifdef FEAT_PERSISTENT_UNDO |
| 4502 | write_undo_file = (buf->b_p_udf && overwriting && !append |
| 4503 | && !filtering && reset_changed); |
| 4504 | if (write_undo_file) |
| 4505 | /* Prepare for computing the hash value of the text. */ |
| 4506 | sha256_start(&sha_ctx); |
| 4507 | #endif |
| 4508 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4509 | write_info.bw_len = bufsize; |
| 4510 | #ifdef HAS_BW_FLAGS |
| 4511 | write_info.bw_flags = wb_flags; |
| 4512 | #endif |
| 4513 | fileformat = get_fileformat_force(buf, eap); |
| 4514 | s = buffer; |
| 4515 | len = 0; |
| 4516 | for (lnum = start; lnum <= end; ++lnum) |
| 4517 | { |
| 4518 | /* |
| 4519 | * The next while loop is done once for each character written. |
| 4520 | * Keep it fast! |
| 4521 | */ |
| 4522 | ptr = ml_get_buf(buf, lnum, FALSE) - 1; |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 4523 | #ifdef FEAT_PERSISTENT_UNDO |
| 4524 | if (write_undo_file) |
Bram Moolenaar | 442b422 | 2010-05-24 21:34:22 +0200 | [diff] [blame] | 4525 | sha256_update(&sha_ctx, ptr + 1, (UINT32_T)(STRLEN(ptr + 1) + 1)); |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 4526 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4527 | while ((c = *++ptr) != NUL) |
| 4528 | { |
| 4529 | if (c == NL) |
| 4530 | *s = NUL; /* replace newlines with NULs */ |
| 4531 | else if (c == CAR && fileformat == EOL_MAC) |
| 4532 | *s = NL; /* Mac: replace CRs with NLs */ |
| 4533 | else |
| 4534 | *s = c; |
| 4535 | ++s; |
| 4536 | if (++len != bufsize) |
| 4537 | continue; |
| 4538 | if (buf_write_bytes(&write_info) == FAIL) |
| 4539 | { |
| 4540 | end = 0; /* write error: break loop */ |
| 4541 | break; |
| 4542 | } |
| 4543 | nchars += bufsize; |
| 4544 | s = buffer; |
| 4545 | len = 0; |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 4546 | #ifdef FEAT_MBYTE |
| 4547 | write_info.bw_start_lnum = lnum; |
| 4548 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4549 | } |
| 4550 | /* write failed or last line has no EOL: stop here */ |
| 4551 | if (end == 0 |
| 4552 | || (lnum == end |
| 4553 | && write_bin |
| 4554 | && (lnum == write_no_eol_lnum |
| 4555 | || (lnum == buf->b_ml.ml_line_count && !buf->b_p_eol)))) |
| 4556 | { |
| 4557 | ++lnum; /* written the line, count it */ |
| 4558 | no_eol = TRUE; |
| 4559 | break; |
| 4560 | } |
| 4561 | if (fileformat == EOL_UNIX) |
| 4562 | *s++ = NL; |
| 4563 | else |
| 4564 | { |
| 4565 | *s++ = CAR; /* EOL_MAC or EOL_DOS: write CR */ |
| 4566 | if (fileformat == EOL_DOS) /* write CR-NL */ |
| 4567 | { |
| 4568 | if (++len == bufsize) |
| 4569 | { |
| 4570 | if (buf_write_bytes(&write_info) == FAIL) |
| 4571 | { |
| 4572 | end = 0; /* write error: break loop */ |
| 4573 | break; |
| 4574 | } |
| 4575 | nchars += bufsize; |
| 4576 | s = buffer; |
| 4577 | len = 0; |
| 4578 | } |
| 4579 | *s++ = NL; |
| 4580 | } |
| 4581 | } |
| 4582 | if (++len == bufsize && end) |
| 4583 | { |
| 4584 | if (buf_write_bytes(&write_info) == FAIL) |
| 4585 | { |
| 4586 | end = 0; /* write error: break loop */ |
| 4587 | break; |
| 4588 | } |
| 4589 | nchars += bufsize; |
| 4590 | s = buffer; |
| 4591 | len = 0; |
| 4592 | |
| 4593 | ui_breakcheck(); |
| 4594 | if (got_int) |
| 4595 | { |
| 4596 | end = 0; /* Interrupted, break loop */ |
| 4597 | break; |
| 4598 | } |
| 4599 | } |
| 4600 | #ifdef VMS |
| 4601 | /* |
| 4602 | * On VMS there is a problem: newlines get added when writing blocks |
| 4603 | * at a time. Fix it by writing a line at a time. |
| 4604 | * This is much slower! |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 4605 | * Explanation: VAX/DECC RTL insists that records in some RMS |
| 4606 | * structures end with a newline (carriage return) character, and if |
| 4607 | * they don't it adds one. |
| 4608 | * With other RMS structures it works perfect without this fix. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4609 | */ |
Bram Moolenaar | b52e260 | 2007-10-29 21:38:54 +0000 | [diff] [blame] | 4610 | if (buf->b_fab_rfm == FAB$C_VFC |
| 4611 | || ((buf->b_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4612 | { |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 4613 | int b2write; |
| 4614 | |
| 4615 | buf->b_fab_mrs = (buf->b_fab_mrs == 0 |
| 4616 | ? MIN(4096, bufsize) |
| 4617 | : MIN(buf->b_fab_mrs, bufsize)); |
| 4618 | |
| 4619 | b2write = len; |
| 4620 | while (b2write > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4621 | { |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 4622 | write_info.bw_len = MIN(b2write, buf->b_fab_mrs); |
| 4623 | if (buf_write_bytes(&write_info) == FAIL) |
| 4624 | { |
| 4625 | end = 0; |
| 4626 | break; |
| 4627 | } |
| 4628 | b2write -= MIN(b2write, buf->b_fab_mrs); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4629 | } |
| 4630 | write_info.bw_len = bufsize; |
| 4631 | nchars += len; |
| 4632 | s = buffer; |
| 4633 | len = 0; |
| 4634 | } |
| 4635 | #endif |
| 4636 | } |
| 4637 | if (len > 0 && end > 0) |
| 4638 | { |
| 4639 | write_info.bw_len = len; |
| 4640 | if (buf_write_bytes(&write_info) == FAIL) |
| 4641 | end = 0; /* write error */ |
| 4642 | nchars += len; |
| 4643 | } |
| 4644 | |
| 4645 | #if defined(UNIX) && defined(HAVE_FSYNC) |
| 4646 | /* On many journalling file systems there is a bug that causes both the |
| 4647 | * original and the backup file to be lost when halting the system right |
| 4648 | * after writing the file. That's because only the meta-data is |
| 4649 | * journalled. Syncing the file slows down the system, but assures it has |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 4650 | * been written to disk and we don't lose it. |
| 4651 | * 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] | 4652 | * (could be a pipe). |
| 4653 | * If the 'fsync' option is FALSE, don't fsync(). Useful for laptops. */ |
| 4654 | if (p_fs && fsync(fd) != 0 && !device) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4655 | { |
| 4656 | errmsg = (char_u *)_("E667: Fsync failed"); |
| 4657 | end = 0; |
| 4658 | } |
| 4659 | #endif |
| 4660 | |
Bram Moolenaar | 588ebeb | 2008-05-07 17:09:24 +0000 | [diff] [blame] | 4661 | #ifdef HAVE_SELINUX |
| 4662 | /* Probably need to set the security context. */ |
| 4663 | if (!backup_copy) |
| 4664 | mch_copy_sec(backup, wfname); |
| 4665 | #endif |
| 4666 | |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4667 | #ifdef UNIX |
| 4668 | /* When creating a new file, set its owner/group to that of the original |
| 4669 | * file. Get the new device and inode number. */ |
| 4670 | if (backup != NULL && !backup_copy) |
| 4671 | { |
| 4672 | # ifdef HAVE_FCHOWN |
| 4673 | struct stat st; |
| 4674 | |
| 4675 | /* don't change the owner when it's already OK, some systems remove |
| 4676 | * permission or ACL stuff */ |
| 4677 | if (mch_stat((char *)wfname, &st) < 0 |
| 4678 | || st.st_uid != st_old.st_uid |
| 4679 | || st.st_gid != st_old.st_gid) |
| 4680 | { |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4681 | ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4682 | if (perm >= 0) /* set permission again, may have changed */ |
| 4683 | (void)mch_setperm(wfname, perm); |
| 4684 | } |
| 4685 | # endif |
| 4686 | buf_setino(buf); |
| 4687 | } |
Bram Moolenaar | f1726cc | 2009-05-13 18:48:16 +0000 | [diff] [blame] | 4688 | else if (!buf->b_dev_valid) |
Bram Moolenaar | 8fa0445 | 2005-12-23 22:13:51 +0000 | [diff] [blame] | 4689 | /* Set the inode when creating a new file. */ |
| 4690 | buf_setino(buf); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4691 | #endif |
| 4692 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4693 | if (close(fd) != 0) |
| 4694 | { |
| 4695 | errmsg = (char_u *)_("E512: Close failed"); |
| 4696 | end = 0; |
| 4697 | } |
| 4698 | |
| 4699 | #ifdef UNIX |
| 4700 | if (made_writable) |
| 4701 | perm &= ~0200; /* reset 'w' bit for security reasons */ |
| 4702 | #endif |
| 4703 | if (perm >= 0) /* set perm. of new file same as old file */ |
| 4704 | (void)mch_setperm(wfname, perm); |
| 4705 | #ifdef RISCOS |
| 4706 | if (!append && !filtering) |
| 4707 | /* Set the filetype after writing the file. */ |
| 4708 | mch_set_filetype(wfname, buf->b_p_oft); |
| 4709 | #endif |
| 4710 | #ifdef HAVE_ACL |
| 4711 | /* Probably need to set the ACL before changing the user (can't set the |
| 4712 | * ACL on a file the user doesn't own). */ |
| 4713 | if (!backup_copy) |
| 4714 | mch_set_acl(wfname, acl); |
| 4715 | #endif |
| 4716 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4717 | |
| 4718 | #if defined(FEAT_MBYTE) && defined(FEAT_EVAL) |
| 4719 | if (wfname != fname) |
| 4720 | { |
| 4721 | /* |
| 4722 | * The file was written to a temp file, now it needs to be converted |
| 4723 | * with 'charconvert' to (overwrite) the output file. |
| 4724 | */ |
| 4725 | if (end != 0) |
| 4726 | { |
| 4727 | if (eval_charconvert(enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc, |
| 4728 | wfname, fname) == FAIL) |
| 4729 | { |
| 4730 | write_info.bw_conv_error = TRUE; |
| 4731 | end = 0; |
| 4732 | } |
| 4733 | } |
| 4734 | mch_remove(wfname); |
| 4735 | vim_free(wfname); |
| 4736 | } |
| 4737 | #endif |
| 4738 | |
| 4739 | if (end == 0) |
| 4740 | { |
| 4741 | if (errmsg == NULL) |
| 4742 | { |
| 4743 | #ifdef FEAT_MBYTE |
| 4744 | if (write_info.bw_conv_error) |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 4745 | { |
| 4746 | if (write_info.bw_conv_error_lnum == 0) |
| 4747 | errmsg = (char_u *)_("E513: write error, conversion failed (make 'fenc' empty to override)"); |
| 4748 | else |
| 4749 | { |
| 4750 | errmsg_allocated = TRUE; |
| 4751 | errmsg = alloc(300); |
| 4752 | vim_snprintf((char *)errmsg, 300, _("E513: write error, conversion failed in line %ld (make 'fenc' empty to override)"), |
| 4753 | (long)write_info.bw_conv_error_lnum); |
| 4754 | } |
| 4755 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4756 | else |
| 4757 | #endif |
| 4758 | if (got_int) |
| 4759 | errmsg = (char_u *)_(e_interr); |
| 4760 | else |
| 4761 | errmsg = (char_u *)_("E514: write error (file system full?)"); |
| 4762 | } |
| 4763 | |
| 4764 | /* |
| 4765 | * 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] | 4766 | * because the new file is probably corrupt. This avoids losing the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4767 | * original file when trying to make a backup when writing the file a |
| 4768 | * second time. |
| 4769 | * When "backup_copy" is set we need to copy the backup over the new |
| 4770 | * file. Otherwise rename the backup file. |
| 4771 | * If this is OK, don't give the extra warning message. |
| 4772 | */ |
| 4773 | if (backup != NULL) |
| 4774 | { |
| 4775 | if (backup_copy) |
| 4776 | { |
| 4777 | /* This may take a while, if we were interrupted let the user |
| 4778 | * know we got the message. */ |
| 4779 | if (got_int) |
| 4780 | { |
| 4781 | MSG(_(e_interr)); |
| 4782 | out_flush(); |
| 4783 | } |
| 4784 | if ((fd = mch_open((char *)backup, O_RDONLY | O_EXTRA, 0)) >= 0) |
| 4785 | { |
| 4786 | if ((write_info.bw_fd = mch_open((char *)fname, |
Bram Moolenaar | 9be038d | 2005-03-08 22:34:32 +0000 | [diff] [blame] | 4787 | O_WRONLY | O_CREAT | O_TRUNC | O_EXTRA, |
| 4788 | perm & 0777)) >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4789 | { |
| 4790 | /* copy the file. */ |
| 4791 | write_info.bw_buf = smallbuf; |
| 4792 | #ifdef HAS_BW_FLAGS |
| 4793 | write_info.bw_flags = FIO_NOCONVERT; |
| 4794 | #endif |
| 4795 | while ((write_info.bw_len = vim_read(fd, smallbuf, |
| 4796 | SMBUFSIZE)) > 0) |
| 4797 | if (buf_write_bytes(&write_info) == FAIL) |
| 4798 | break; |
| 4799 | |
| 4800 | if (close(write_info.bw_fd) >= 0 |
| 4801 | && write_info.bw_len == 0) |
| 4802 | end = 1; /* success */ |
| 4803 | } |
| 4804 | close(fd); /* ignore errors for closing read file */ |
| 4805 | } |
| 4806 | } |
| 4807 | else |
| 4808 | { |
| 4809 | if (vim_rename(backup, fname) == 0) |
| 4810 | end = 1; |
| 4811 | } |
| 4812 | } |
| 4813 | goto fail; |
| 4814 | } |
| 4815 | |
| 4816 | lnum -= start; /* compute number of written lines */ |
| 4817 | --no_wait_return; /* may wait for return now */ |
| 4818 | |
| 4819 | #if !(defined(UNIX) || defined(VMS)) |
| 4820 | fname = sfname; /* use shortname now, for the messages */ |
| 4821 | #endif |
| 4822 | if (!filtering) |
| 4823 | { |
| 4824 | msg_add_fname(buf, fname); /* put fname in IObuff with quotes */ |
| 4825 | c = FALSE; |
| 4826 | #ifdef FEAT_MBYTE |
| 4827 | if (write_info.bw_conv_error) |
| 4828 | { |
| 4829 | STRCAT(IObuff, _(" CONVERSION ERROR")); |
| 4830 | c = TRUE; |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 4831 | if (write_info.bw_conv_error_lnum != 0) |
| 4832 | { |
Bram Moolenaar | c066202 | 2009-09-11 13:04:24 +0000 | [diff] [blame] | 4833 | size_t l = STRLEN(IObuff); |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 4834 | vim_snprintf((char *)IObuff + l, IOSIZE - l, _(" in line %ld;"), |
| 4835 | (long)write_info.bw_conv_error_lnum); |
| 4836 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4837 | } |
| 4838 | else if (notconverted) |
| 4839 | { |
| 4840 | STRCAT(IObuff, _("[NOT converted]")); |
| 4841 | c = TRUE; |
| 4842 | } |
| 4843 | else if (converted) |
| 4844 | { |
| 4845 | STRCAT(IObuff, _("[converted]")); |
| 4846 | c = TRUE; |
| 4847 | } |
| 4848 | #endif |
| 4849 | if (device) |
| 4850 | { |
| 4851 | STRCAT(IObuff, _("[Device]")); |
| 4852 | c = TRUE; |
| 4853 | } |
| 4854 | else if (newfile) |
| 4855 | { |
| 4856 | STRCAT(IObuff, shortmess(SHM_NEW) ? _("[New]") : _("[New File]")); |
| 4857 | c = TRUE; |
| 4858 | } |
| 4859 | if (no_eol) |
| 4860 | { |
| 4861 | msg_add_eol(); |
| 4862 | c = TRUE; |
| 4863 | } |
| 4864 | /* may add [unix/dos/mac] */ |
| 4865 | if (msg_add_fileformat(fileformat)) |
| 4866 | c = TRUE; |
| 4867 | #ifdef FEAT_CRYPT |
| 4868 | if (wb_flags & FIO_ENCRYPTED) |
| 4869 | { |
| 4870 | STRCAT(IObuff, _("[crypted]")); |
| 4871 | c = TRUE; |
| 4872 | } |
| 4873 | #endif |
| 4874 | msg_add_lines(c, (long)lnum, nchars); /* add line/char count */ |
| 4875 | if (!shortmess(SHM_WRITE)) |
| 4876 | { |
| 4877 | if (append) |
| 4878 | STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [a]") : _(" appended")); |
| 4879 | else |
| 4880 | STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [w]") : _(" written")); |
| 4881 | } |
| 4882 | |
Bram Moolenaar | 8f7fd65 | 2006-02-21 22:04:51 +0000 | [diff] [blame] | 4883 | set_keep_msg(msg_trunc_attr(IObuff, FALSE, 0), 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4884 | } |
| 4885 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4886 | /* When written everything correctly: reset 'modified'. Unless not |
| 4887 | * writing to the original file and '+' is not in 'cpoptions'. */ |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 4888 | if (reset_changed && whole && !append |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4889 | #ifdef FEAT_MBYTE |
| 4890 | && !write_info.bw_conv_error |
| 4891 | #endif |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4892 | && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL) |
| 4893 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4894 | { |
| 4895 | unchanged(buf, TRUE); |
| 4896 | u_unchanged(buf); |
| 4897 | } |
| 4898 | |
| 4899 | /* |
| 4900 | * If written to the current file, update the timestamp of the swap file |
| 4901 | * and reset the BF_WRITE_MASK flags. Also sets buf->b_mtime. |
| 4902 | */ |
| 4903 | if (overwriting) |
| 4904 | { |
| 4905 | ml_timestamp(buf); |
Bram Moolenaar | 292ad19 | 2005-12-11 21:29:51 +0000 | [diff] [blame] | 4906 | if (append) |
| 4907 | buf->b_flags &= ~BF_NEW; |
| 4908 | else |
| 4909 | buf->b_flags &= ~BF_WRITE_MASK; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4910 | } |
| 4911 | |
| 4912 | /* |
| 4913 | * If we kept a backup until now, and we are in patch mode, then we make |
| 4914 | * the backup file our 'original' file. |
| 4915 | */ |
| 4916 | if (*p_pm && dobackup) |
| 4917 | { |
| 4918 | char *org = (char *)buf_modname( |
| 4919 | #ifdef SHORT_FNAME |
| 4920 | TRUE, |
| 4921 | #else |
| 4922 | (buf->b_p_sn || buf->b_shortname), |
| 4923 | #endif |
| 4924 | fname, p_pm, FALSE); |
| 4925 | |
| 4926 | if (backup != NULL) |
| 4927 | { |
| 4928 | struct stat st; |
| 4929 | |
| 4930 | /* |
| 4931 | * If the original file does not exist yet |
| 4932 | * the current backup file becomes the original file |
| 4933 | */ |
| 4934 | if (org == NULL) |
| 4935 | EMSG(_("E205: Patchmode: can't save original file")); |
| 4936 | else if (mch_stat(org, &st) < 0) |
| 4937 | { |
| 4938 | vim_rename(backup, (char_u *)org); |
| 4939 | vim_free(backup); /* don't delete the file */ |
| 4940 | backup = NULL; |
| 4941 | #ifdef UNIX |
| 4942 | set_file_time((char_u *)org, st_old.st_atime, st_old.st_mtime); |
| 4943 | #endif |
| 4944 | } |
| 4945 | } |
| 4946 | /* |
| 4947 | * If there is no backup file, remember that a (new) file was |
| 4948 | * created. |
| 4949 | */ |
| 4950 | else |
| 4951 | { |
| 4952 | int empty_fd; |
| 4953 | |
| 4954 | if (org == NULL |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 4955 | || (empty_fd = mch_open(org, |
| 4956 | O_CREAT | O_EXTRA | O_EXCL | O_NOFOLLOW, |
Bram Moolenaar | 4317d9b | 2005-03-18 20:25:31 +0000 | [diff] [blame] | 4957 | perm < 0 ? 0666 : (perm & 0777))) < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4958 | EMSG(_("E206: patchmode: can't touch empty original file")); |
| 4959 | else |
| 4960 | close(empty_fd); |
| 4961 | } |
| 4962 | if (org != NULL) |
| 4963 | { |
| 4964 | mch_setperm((char_u *)org, mch_getperm(fname) & 0777); |
| 4965 | vim_free(org); |
| 4966 | } |
| 4967 | } |
| 4968 | |
| 4969 | /* |
| 4970 | * Remove the backup unless 'backup' option is set |
| 4971 | */ |
| 4972 | if (!p_bk && backup != NULL && mch_remove(backup) != 0) |
| 4973 | EMSG(_("E207: Can't delete backup file")); |
| 4974 | |
| 4975 | #ifdef FEAT_SUN_WORKSHOP |
| 4976 | if (usingSunWorkShop) |
| 4977 | workshop_file_saved((char *) ffname); |
| 4978 | #endif |
| 4979 | |
| 4980 | goto nofail; |
| 4981 | |
| 4982 | /* |
| 4983 | * Finish up. We get here either after failure or success. |
| 4984 | */ |
| 4985 | fail: |
| 4986 | --no_wait_return; /* may wait for return now */ |
| 4987 | nofail: |
| 4988 | |
| 4989 | /* Done saving, we accept changed buffer warnings again */ |
| 4990 | buf->b_saving = FALSE; |
| 4991 | |
| 4992 | vim_free(backup); |
| 4993 | if (buffer != smallbuf) |
| 4994 | vim_free(buffer); |
| 4995 | #ifdef FEAT_MBYTE |
| 4996 | vim_free(fenc_tofree); |
| 4997 | vim_free(write_info.bw_conv_buf); |
| 4998 | # ifdef USE_ICONV |
| 4999 | if (write_info.bw_iconv_fd != (iconv_t)-1) |
| 5000 | { |
| 5001 | iconv_close(write_info.bw_iconv_fd); |
| 5002 | write_info.bw_iconv_fd = (iconv_t)-1; |
| 5003 | } |
| 5004 | # endif |
| 5005 | #endif |
| 5006 | #ifdef HAVE_ACL |
| 5007 | mch_free_acl(acl); |
| 5008 | #endif |
| 5009 | |
| 5010 | if (errmsg != NULL) |
| 5011 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5012 | int numlen = errnum != NULL ? (int)STRLEN(errnum) : 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5013 | |
| 5014 | attr = hl_attr(HLF_E); /* set highlight for error messages */ |
| 5015 | msg_add_fname(buf, |
| 5016 | #ifndef UNIX |
| 5017 | sfname |
| 5018 | #else |
| 5019 | fname |
| 5020 | #endif |
| 5021 | ); /* put file name in IObuff with quotes */ |
| 5022 | if (STRLEN(IObuff) + STRLEN(errmsg) + numlen >= IOSIZE) |
| 5023 | IObuff[IOSIZE - STRLEN(errmsg) - numlen - 1] = NUL; |
| 5024 | /* If the error message has the form "is ...", put the error number in |
| 5025 | * front of the file name. */ |
| 5026 | if (errnum != NULL) |
| 5027 | { |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 5028 | STRMOVE(IObuff + numlen, IObuff); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5029 | mch_memmove(IObuff, errnum, (size_t)numlen); |
| 5030 | } |
| 5031 | STRCAT(IObuff, errmsg); |
| 5032 | emsg(IObuff); |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 5033 | if (errmsg_allocated) |
| 5034 | vim_free(errmsg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5035 | |
| 5036 | retval = FAIL; |
| 5037 | if (end == 0) |
| 5038 | { |
| 5039 | MSG_PUTS_ATTR(_("\nWARNING: Original file may be lost or damaged\n"), |
| 5040 | attr | MSG_HIST); |
| 5041 | MSG_PUTS_ATTR(_("don't quit the editor until the file is successfully written!"), |
| 5042 | attr | MSG_HIST); |
| 5043 | |
| 5044 | /* Update the timestamp to avoid an "overwrite changed file" |
| 5045 | * prompt when writing again. */ |
| 5046 | if (mch_stat((char *)fname, &st_old) >= 0) |
| 5047 | { |
| 5048 | buf_store_time(buf, &st_old, fname); |
| 5049 | buf->b_mtime_read = buf->b_mtime; |
| 5050 | } |
| 5051 | } |
| 5052 | } |
| 5053 | msg_scroll = msg_save; |
| 5054 | |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 5055 | #ifdef FEAT_PERSISTENT_UNDO |
| 5056 | /* |
| 5057 | * When writing the whole file and 'undofile' is set, also write the undo |
| 5058 | * file. |
| 5059 | */ |
| 5060 | if (retval == OK && write_undo_file) |
| 5061 | { |
| 5062 | char_u hash[UNDO_HASH_SIZE]; |
| 5063 | |
| 5064 | sha256_finish(&sha_ctx, hash); |
| 5065 | u_write_undo(NULL, FALSE, buf, hash); |
| 5066 | } |
| 5067 | #endif |
| 5068 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5069 | #ifdef FEAT_AUTOCMD |
| 5070 | #ifdef FEAT_EVAL |
| 5071 | if (!should_abort(retval)) |
| 5072 | #else |
| 5073 | if (!got_int) |
| 5074 | #endif |
| 5075 | { |
| 5076 | aco_save_T aco; |
| 5077 | |
| 5078 | write_no_eol_lnum = 0; /* in case it was set by the previous read */ |
| 5079 | |
| 5080 | /* |
| 5081 | * Apply POST autocommands. |
| 5082 | * Careful: The autocommands may call buf_write() recursively! |
| 5083 | */ |
| 5084 | aucmd_prepbuf(&aco, buf); |
| 5085 | |
| 5086 | if (append) |
| 5087 | apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname, |
| 5088 | FALSE, curbuf, eap); |
| 5089 | else if (filtering) |
| 5090 | apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname, |
| 5091 | FALSE, curbuf, eap); |
| 5092 | else if (reset_changed && whole) |
| 5093 | apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname, |
| 5094 | FALSE, curbuf, eap); |
| 5095 | else |
| 5096 | apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname, |
| 5097 | FALSE, curbuf, eap); |
| 5098 | |
| 5099 | /* restore curwin/curbuf and a few other things */ |
| 5100 | aucmd_restbuf(&aco); |
| 5101 | |
| 5102 | #ifdef FEAT_EVAL |
| 5103 | if (aborting()) /* autocmds may abort script processing */ |
| 5104 | retval = FALSE; |
| 5105 | #endif |
| 5106 | } |
| 5107 | #endif |
| 5108 | |
| 5109 | got_int |= prev_got_int; |
| 5110 | |
| 5111 | #ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */ |
| 5112 | /* Update machine specific information. */ |
| 5113 | mch_post_buffer_write(buf); |
| 5114 | #endif |
| 5115 | return retval; |
| 5116 | } |
| 5117 | |
| 5118 | /* |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 5119 | * Set the name of the current buffer. Use when the buffer doesn't have a |
| 5120 | * name and a ":r" or ":w" command with a file name is used. |
| 5121 | */ |
| 5122 | static int |
| 5123 | set_rw_fname(fname, sfname) |
| 5124 | char_u *fname; |
| 5125 | char_u *sfname; |
| 5126 | { |
| 5127 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 8b38e24 | 2009-06-16 13:35:20 +0000 | [diff] [blame] | 5128 | buf_T *buf = curbuf; |
| 5129 | |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 5130 | /* It's like the unnamed buffer is deleted.... */ |
| 5131 | if (curbuf->b_p_bl) |
| 5132 | apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); |
| 5133 | apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf); |
| 5134 | # ifdef FEAT_EVAL |
| 5135 | if (aborting()) /* autocmds may abort script processing */ |
| 5136 | return FAIL; |
| 5137 | # endif |
Bram Moolenaar | 8b38e24 | 2009-06-16 13:35:20 +0000 | [diff] [blame] | 5138 | if (curbuf != buf) |
| 5139 | { |
| 5140 | /* We are in another buffer now, don't do the renaming. */ |
| 5141 | EMSG(_(e_auchangedbuf)); |
| 5142 | return FAIL; |
| 5143 | } |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 5144 | #endif |
| 5145 | |
| 5146 | if (setfname(curbuf, fname, sfname, FALSE) == OK) |
| 5147 | curbuf->b_flags |= BF_NOTEDITED; |
| 5148 | |
| 5149 | #ifdef FEAT_AUTOCMD |
| 5150 | /* ....and a new named one is created */ |
| 5151 | apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf); |
| 5152 | if (curbuf->b_p_bl) |
| 5153 | apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf); |
| 5154 | # ifdef FEAT_EVAL |
| 5155 | if (aborting()) /* autocmds may abort script processing */ |
| 5156 | return FAIL; |
| 5157 | # endif |
| 5158 | |
| 5159 | /* Do filetype detection now if 'filetype' is empty. */ |
| 5160 | if (*curbuf->b_p_ft == NUL) |
| 5161 | { |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5162 | if (au_has_group((char_u *)"filetypedetect")) |
Bram Moolenaar | 70836c8 | 2006-02-20 21:28:49 +0000 | [diff] [blame] | 5163 | (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE); |
Bram Moolenaar | a3227e2 | 2006-03-08 21:32:40 +0000 | [diff] [blame] | 5164 | do_modelines(0); |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 5165 | } |
| 5166 | #endif |
| 5167 | |
| 5168 | return OK; |
| 5169 | } |
| 5170 | |
| 5171 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5172 | * Put file name into IObuff with quotes. |
| 5173 | */ |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 5174 | void |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5175 | msg_add_fname(buf, fname) |
| 5176 | buf_T *buf; |
| 5177 | char_u *fname; |
| 5178 | { |
| 5179 | if (fname == NULL) |
| 5180 | fname = (char_u *)"-stdin-"; |
| 5181 | home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE); |
| 5182 | IObuff[0] = '"'; |
| 5183 | STRCAT(IObuff, "\" "); |
| 5184 | } |
| 5185 | |
| 5186 | /* |
| 5187 | * Append message for text mode to IObuff. |
| 5188 | * Return TRUE if something appended. |
| 5189 | */ |
| 5190 | static int |
| 5191 | msg_add_fileformat(eol_type) |
| 5192 | int eol_type; |
| 5193 | { |
| 5194 | #ifndef USE_CRNL |
| 5195 | if (eol_type == EOL_DOS) |
| 5196 | { |
| 5197 | STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]")); |
| 5198 | return TRUE; |
| 5199 | } |
| 5200 | #endif |
| 5201 | #ifndef USE_CR |
| 5202 | if (eol_type == EOL_MAC) |
| 5203 | { |
| 5204 | STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]")); |
| 5205 | return TRUE; |
| 5206 | } |
| 5207 | #endif |
| 5208 | #if defined(USE_CRNL) || defined(USE_CR) |
| 5209 | if (eol_type == EOL_UNIX) |
| 5210 | { |
| 5211 | STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]")); |
| 5212 | return TRUE; |
| 5213 | } |
| 5214 | #endif |
| 5215 | return FALSE; |
| 5216 | } |
| 5217 | |
| 5218 | /* |
| 5219 | * Append line and character count to IObuff. |
| 5220 | */ |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 5221 | void |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5222 | msg_add_lines(insert_space, lnum, nchars) |
| 5223 | int insert_space; |
| 5224 | long lnum; |
Bram Moolenaar | 914703b | 2010-05-31 21:59:46 +0200 | [diff] [blame] | 5225 | off_t nchars; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5226 | { |
| 5227 | char_u *p; |
| 5228 | |
| 5229 | p = IObuff + STRLEN(IObuff); |
| 5230 | |
| 5231 | if (insert_space) |
| 5232 | *p++ = ' '; |
| 5233 | if (shortmess(SHM_LINES)) |
Bram Moolenaar | 914703b | 2010-05-31 21:59:46 +0200 | [diff] [blame] | 5234 | sprintf((char *)p, |
| 5235 | #ifdef LONG_LONG_OFF_T |
| 5236 | "%ldL, %lldC", |
| 5237 | #else |
| 5238 | "%ldL, %ldC", |
| 5239 | #endif |
| 5240 | lnum, nchars); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5241 | else |
| 5242 | { |
| 5243 | if (lnum == 1) |
| 5244 | STRCPY(p, _("1 line, ")); |
| 5245 | else |
| 5246 | sprintf((char *)p, _("%ld lines, "), lnum); |
| 5247 | p += STRLEN(p); |
| 5248 | if (nchars == 1) |
| 5249 | STRCPY(p, _("1 character")); |
| 5250 | else |
Bram Moolenaar | 914703b | 2010-05-31 21:59:46 +0200 | [diff] [blame] | 5251 | sprintf((char *)p, |
| 5252 | #ifdef LONG_LONG_OFF_T |
| 5253 | _("%lld characters"), |
| 5254 | #else |
| 5255 | _("%ld characters"), |
| 5256 | #endif |
| 5257 | nchars); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5258 | } |
| 5259 | } |
| 5260 | |
| 5261 | /* |
| 5262 | * Append message for missing line separator to IObuff. |
| 5263 | */ |
| 5264 | static void |
| 5265 | msg_add_eol() |
| 5266 | { |
| 5267 | STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]")); |
| 5268 | } |
| 5269 | |
| 5270 | /* |
| 5271 | * Check modification time of file, before writing to it. |
| 5272 | * The size isn't checked, because using a tool like "gzip" takes care of |
| 5273 | * using the same timestamp but can't set the size. |
| 5274 | */ |
| 5275 | static int |
| 5276 | check_mtime(buf, st) |
| 5277 | buf_T *buf; |
| 5278 | struct stat *st; |
| 5279 | { |
| 5280 | if (buf->b_mtime_read != 0 |
| 5281 | && time_differs((long)st->st_mtime, buf->b_mtime_read)) |
| 5282 | { |
| 5283 | msg_scroll = TRUE; /* don't overwrite messages here */ |
| 5284 | msg_silent = 0; /* must give this prompt */ |
| 5285 | /* don't use emsg() here, don't want to flush the buffers */ |
| 5286 | MSG_ATTR(_("WARNING: The file has been changed since reading it!!!"), |
| 5287 | hl_attr(HLF_E)); |
| 5288 | if (ask_yesno((char_u *)_("Do you really want to write to it"), |
| 5289 | TRUE) == 'n') |
| 5290 | return FAIL; |
| 5291 | msg_scroll = FALSE; /* always overwrite the file message now */ |
| 5292 | } |
| 5293 | return OK; |
| 5294 | } |
| 5295 | |
| 5296 | static int |
| 5297 | time_differs(t1, t2) |
| 5298 | long t1, t2; |
| 5299 | { |
| 5300 | #if defined(__linux__) || defined(MSDOS) || defined(MSWIN) |
| 5301 | /* On a FAT filesystem, esp. under Linux, there are only 5 bits to store |
| 5302 | * the seconds. Since the roundoff is done when flushing the inode, the |
| 5303 | * time may change unexpectedly by one second!!! */ |
| 5304 | return (t1 - t2 > 1 || t2 - t1 > 1); |
| 5305 | #else |
| 5306 | return (t1 != t2); |
| 5307 | #endif |
| 5308 | } |
| 5309 | |
| 5310 | /* |
| 5311 | * Call write() to write a number of bytes to the file. |
| 5312 | * Also handles encryption and 'encoding' conversion. |
| 5313 | * |
| 5314 | * Return FAIL for failure, OK otherwise. |
| 5315 | */ |
| 5316 | static int |
| 5317 | buf_write_bytes(ip) |
| 5318 | struct bw_info *ip; |
| 5319 | { |
| 5320 | int wlen; |
| 5321 | char_u *buf = ip->bw_buf; /* data to write */ |
| 5322 | int len = ip->bw_len; /* length of data */ |
| 5323 | #ifdef HAS_BW_FLAGS |
| 5324 | int flags = ip->bw_flags; /* extra flags */ |
| 5325 | #endif |
| 5326 | |
| 5327 | #ifdef FEAT_MBYTE |
| 5328 | /* |
| 5329 | * Skip conversion when writing the crypt magic number or the BOM. |
| 5330 | */ |
| 5331 | if (!(flags & FIO_NOCONVERT)) |
| 5332 | { |
| 5333 | char_u *p; |
| 5334 | unsigned c; |
| 5335 | int n; |
| 5336 | |
| 5337 | if (flags & FIO_UTF8) |
| 5338 | { |
| 5339 | /* |
| 5340 | * Convert latin1 in the buffer to UTF-8 in the file. |
| 5341 | */ |
| 5342 | p = ip->bw_conv_buf; /* translate to buffer */ |
| 5343 | for (wlen = 0; wlen < len; ++wlen) |
| 5344 | p += utf_char2bytes(buf[wlen], p); |
| 5345 | buf = ip->bw_conv_buf; |
| 5346 | len = (int)(p - ip->bw_conv_buf); |
| 5347 | } |
| 5348 | else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1)) |
| 5349 | { |
| 5350 | /* |
| 5351 | * Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or |
| 5352 | * Latin1 chars in the file. |
| 5353 | */ |
| 5354 | if (flags & FIO_LATIN1) |
| 5355 | p = buf; /* translate in-place (can only get shorter) */ |
| 5356 | else |
| 5357 | p = ip->bw_conv_buf; /* translate to buffer */ |
| 5358 | for (wlen = 0; wlen < len; wlen += n) |
| 5359 | { |
| 5360 | if (wlen == 0 && ip->bw_restlen != 0) |
| 5361 | { |
| 5362 | int l; |
| 5363 | |
| 5364 | /* Use remainder of previous call. Append the start of |
| 5365 | * buf[] to get a full sequence. Might still be too |
| 5366 | * short! */ |
| 5367 | l = CONV_RESTLEN - ip->bw_restlen; |
| 5368 | if (l > len) |
| 5369 | l = len; |
| 5370 | mch_memmove(ip->bw_rest + ip->bw_restlen, buf, (size_t)l); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5371 | n = utf_ptr2len_len(ip->bw_rest, ip->bw_restlen + l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5372 | if (n > ip->bw_restlen + len) |
| 5373 | { |
| 5374 | /* We have an incomplete byte sequence at the end to |
| 5375 | * be written. We can't convert it without the |
| 5376 | * remaining bytes. Keep them for the next call. */ |
| 5377 | if (ip->bw_restlen + len > CONV_RESTLEN) |
| 5378 | return FAIL; |
| 5379 | ip->bw_restlen += len; |
| 5380 | break; |
| 5381 | } |
| 5382 | if (n > 1) |
| 5383 | c = utf_ptr2char(ip->bw_rest); |
| 5384 | else |
| 5385 | c = ip->bw_rest[0]; |
| 5386 | if (n >= ip->bw_restlen) |
| 5387 | { |
| 5388 | n -= ip->bw_restlen; |
| 5389 | ip->bw_restlen = 0; |
| 5390 | } |
| 5391 | else |
| 5392 | { |
| 5393 | ip->bw_restlen -= n; |
| 5394 | mch_memmove(ip->bw_rest, ip->bw_rest + n, |
| 5395 | (size_t)ip->bw_restlen); |
| 5396 | n = 0; |
| 5397 | } |
| 5398 | } |
| 5399 | else |
| 5400 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5401 | n = utf_ptr2len_len(buf + wlen, len - wlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5402 | if (n > len - wlen) |
| 5403 | { |
| 5404 | /* We have an incomplete byte sequence at the end to |
| 5405 | * be written. We can't convert it without the |
| 5406 | * remaining bytes. Keep them for the next call. */ |
| 5407 | if (len - wlen > CONV_RESTLEN) |
| 5408 | return FAIL; |
| 5409 | ip->bw_restlen = len - wlen; |
| 5410 | mch_memmove(ip->bw_rest, buf + wlen, |
| 5411 | (size_t)ip->bw_restlen); |
| 5412 | break; |
| 5413 | } |
| 5414 | if (n > 1) |
| 5415 | c = utf_ptr2char(buf + wlen); |
| 5416 | else |
| 5417 | c = buf[wlen]; |
| 5418 | } |
| 5419 | |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 5420 | if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error) |
| 5421 | { |
| 5422 | ip->bw_conv_error = TRUE; |
| 5423 | ip->bw_conv_error_lnum = ip->bw_start_lnum; |
| 5424 | } |
| 5425 | if (c == NL) |
| 5426 | ++ip->bw_start_lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5427 | } |
| 5428 | if (flags & FIO_LATIN1) |
| 5429 | len = (int)(p - buf); |
| 5430 | else |
| 5431 | { |
| 5432 | buf = ip->bw_conv_buf; |
| 5433 | len = (int)(p - ip->bw_conv_buf); |
| 5434 | } |
| 5435 | } |
| 5436 | |
| 5437 | # ifdef WIN3264 |
| 5438 | else if (flags & FIO_CODEPAGE) |
| 5439 | { |
| 5440 | /* |
| 5441 | * Convert UTF-8 or codepage to UCS-2 and then to MS-Windows |
| 5442 | * codepage. |
| 5443 | */ |
| 5444 | char_u *from; |
| 5445 | size_t fromlen; |
| 5446 | char_u *to; |
| 5447 | int u8c; |
| 5448 | BOOL bad = FALSE; |
| 5449 | int needed; |
| 5450 | |
| 5451 | if (ip->bw_restlen > 0) |
| 5452 | { |
| 5453 | /* Need to concatenate the remainder of the previous call and |
| 5454 | * the bytes of the current call. Use the end of the |
| 5455 | * conversion buffer for this. */ |
| 5456 | fromlen = len + ip->bw_restlen; |
| 5457 | from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; |
| 5458 | mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); |
| 5459 | mch_memmove(from + ip->bw_restlen, buf, (size_t)len); |
| 5460 | } |
| 5461 | else |
| 5462 | { |
| 5463 | from = buf; |
| 5464 | fromlen = len; |
| 5465 | } |
| 5466 | |
| 5467 | to = ip->bw_conv_buf; |
| 5468 | if (enc_utf8) |
| 5469 | { |
| 5470 | /* Convert from UTF-8 to UCS-2, to the start of the buffer. |
| 5471 | * The buffer has been allocated to be big enough. */ |
| 5472 | while (fromlen > 0) |
| 5473 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5474 | n = (int)utf_ptr2len_len(from, (int)fromlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5475 | if (n > (int)fromlen) /* incomplete byte sequence */ |
| 5476 | break; |
| 5477 | u8c = utf_ptr2char(from); |
| 5478 | *to++ = (u8c & 0xff); |
| 5479 | *to++ = (u8c >> 8); |
| 5480 | fromlen -= n; |
| 5481 | from += n; |
| 5482 | } |
| 5483 | |
| 5484 | /* Copy remainder to ip->bw_rest[] to be used for the next |
| 5485 | * call. */ |
| 5486 | if (fromlen > CONV_RESTLEN) |
| 5487 | { |
| 5488 | /* weird overlong sequence */ |
| 5489 | ip->bw_conv_error = TRUE; |
| 5490 | return FAIL; |
| 5491 | } |
| 5492 | mch_memmove(ip->bw_rest, from, fromlen); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5493 | ip->bw_restlen = (int)fromlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5494 | } |
| 5495 | else |
| 5496 | { |
| 5497 | /* Convert from enc_codepage to UCS-2, to the start of the |
| 5498 | * buffer. The buffer has been allocated to be big enough. */ |
| 5499 | ip->bw_restlen = 0; |
| 5500 | needed = MultiByteToWideChar(enc_codepage, |
Bram Moolenaar | 36f5ac0 | 2007-05-06 12:40:34 +0000 | [diff] [blame] | 5501 | MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5502 | NULL, 0); |
| 5503 | if (needed == 0) |
| 5504 | { |
| 5505 | /* When conversion fails there may be a trailing byte. */ |
| 5506 | needed = MultiByteToWideChar(enc_codepage, |
Bram Moolenaar | 36f5ac0 | 2007-05-06 12:40:34 +0000 | [diff] [blame] | 5507 | MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen - 1, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5508 | NULL, 0); |
| 5509 | if (needed == 0) |
| 5510 | { |
| 5511 | /* Conversion doesn't work. */ |
| 5512 | ip->bw_conv_error = TRUE; |
| 5513 | return FAIL; |
| 5514 | } |
| 5515 | /* Save the trailing byte for the next call. */ |
| 5516 | ip->bw_rest[0] = from[fromlen - 1]; |
| 5517 | ip->bw_restlen = 1; |
| 5518 | } |
| 5519 | needed = MultiByteToWideChar(enc_codepage, MB_ERR_INVALID_CHARS, |
Bram Moolenaar | 36f5ac0 | 2007-05-06 12:40:34 +0000 | [diff] [blame] | 5520 | (LPCSTR)from, (int)(fromlen - ip->bw_restlen), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5521 | (LPWSTR)to, needed); |
| 5522 | if (needed == 0) |
| 5523 | { |
| 5524 | /* Safety check: Conversion doesn't work. */ |
| 5525 | ip->bw_conv_error = TRUE; |
| 5526 | return FAIL; |
| 5527 | } |
| 5528 | to += needed * 2; |
| 5529 | } |
| 5530 | |
| 5531 | fromlen = to - ip->bw_conv_buf; |
| 5532 | buf = to; |
| 5533 | # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ |
| 5534 | if (FIO_GET_CP(flags) == CP_UTF8) |
| 5535 | { |
| 5536 | /* Convert from UCS-2 to UTF-8, using the remainder of the |
| 5537 | * conversion buffer. Fails when out of space. */ |
| 5538 | for (from = ip->bw_conv_buf; fromlen > 1; fromlen -= 2) |
| 5539 | { |
| 5540 | u8c = *from++; |
| 5541 | u8c += (*from++ << 8); |
| 5542 | to += utf_char2bytes(u8c, to); |
| 5543 | if (to + 6 >= ip->bw_conv_buf + ip->bw_conv_buflen) |
| 5544 | { |
| 5545 | ip->bw_conv_error = TRUE; |
| 5546 | return FAIL; |
| 5547 | } |
| 5548 | } |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5549 | len = (int)(to - buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5550 | } |
| 5551 | else |
| 5552 | #endif |
| 5553 | { |
| 5554 | /* Convert from UCS-2 to the codepage, using the remainder of |
| 5555 | * the conversion buffer. If the conversion uses the default |
| 5556 | * character "0", the data doesn't fit in this encoding, so |
| 5557 | * fail. */ |
| 5558 | len = WideCharToMultiByte(FIO_GET_CP(flags), 0, |
| 5559 | (LPCWSTR)ip->bw_conv_buf, (int)fromlen / sizeof(WCHAR), |
Bram Moolenaar | 36f5ac0 | 2007-05-06 12:40:34 +0000 | [diff] [blame] | 5560 | (LPSTR)to, (int)(ip->bw_conv_buflen - fromlen), 0, |
| 5561 | &bad); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5562 | if (bad) |
| 5563 | { |
| 5564 | ip->bw_conv_error = TRUE; |
| 5565 | return FAIL; |
| 5566 | } |
| 5567 | } |
| 5568 | } |
| 5569 | # endif |
| 5570 | |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 5571 | # ifdef MACOS_CONVERT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5572 | else if (flags & FIO_MACROMAN) |
| 5573 | { |
| 5574 | /* |
| 5575 | * Convert UTF-8 or latin1 to Apple MacRoman. |
| 5576 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5577 | char_u *from; |
| 5578 | size_t fromlen; |
| 5579 | |
| 5580 | if (ip->bw_restlen > 0) |
| 5581 | { |
| 5582 | /* Need to concatenate the remainder of the previous call and |
| 5583 | * the bytes of the current call. Use the end of the |
| 5584 | * conversion buffer for this. */ |
| 5585 | fromlen = len + ip->bw_restlen; |
| 5586 | from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; |
| 5587 | mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); |
| 5588 | mch_memmove(from + ip->bw_restlen, buf, (size_t)len); |
| 5589 | } |
| 5590 | else |
| 5591 | { |
| 5592 | from = buf; |
| 5593 | fromlen = len; |
| 5594 | } |
| 5595 | |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 5596 | if (enc2macroman(from, fromlen, |
| 5597 | ip->bw_conv_buf, &len, ip->bw_conv_buflen, |
| 5598 | ip->bw_rest, &ip->bw_restlen) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5599 | { |
| 5600 | ip->bw_conv_error = TRUE; |
| 5601 | return FAIL; |
| 5602 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5603 | buf = ip->bw_conv_buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5604 | } |
| 5605 | # endif |
| 5606 | |
| 5607 | # ifdef USE_ICONV |
| 5608 | if (ip->bw_iconv_fd != (iconv_t)-1) |
| 5609 | { |
| 5610 | const char *from; |
| 5611 | size_t fromlen; |
| 5612 | char *to; |
| 5613 | size_t tolen; |
| 5614 | |
| 5615 | /* Convert with iconv(). */ |
| 5616 | if (ip->bw_restlen > 0) |
| 5617 | { |
Bram Moolenaar | 5d294d1 | 2009-03-11 12:11:02 +0000 | [diff] [blame] | 5618 | char *fp; |
| 5619 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5620 | /* Need to concatenate the remainder of the previous call and |
| 5621 | * the bytes of the current call. Use the end of the |
| 5622 | * conversion buffer for this. */ |
| 5623 | fromlen = len + ip->bw_restlen; |
Bram Moolenaar | 5d294d1 | 2009-03-11 12:11:02 +0000 | [diff] [blame] | 5624 | fp = (char *)ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; |
| 5625 | mch_memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen); |
| 5626 | mch_memmove(fp + ip->bw_restlen, buf, (size_t)len); |
| 5627 | from = fp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5628 | tolen = ip->bw_conv_buflen - fromlen; |
| 5629 | } |
| 5630 | else |
| 5631 | { |
| 5632 | from = (const char *)buf; |
| 5633 | fromlen = len; |
| 5634 | tolen = ip->bw_conv_buflen; |
| 5635 | } |
| 5636 | to = (char *)ip->bw_conv_buf; |
| 5637 | |
| 5638 | if (ip->bw_first) |
| 5639 | { |
| 5640 | size_t save_len = tolen; |
| 5641 | |
| 5642 | /* output the initial shift state sequence */ |
| 5643 | (void)iconv(ip->bw_iconv_fd, NULL, NULL, &to, &tolen); |
| 5644 | |
| 5645 | /* There is a bug in iconv() on Linux (which appears to be |
| 5646 | * wide-spread) which sets "to" to NULL and messes up "tolen". |
| 5647 | */ |
| 5648 | if (to == NULL) |
| 5649 | { |
| 5650 | to = (char *)ip->bw_conv_buf; |
| 5651 | tolen = save_len; |
| 5652 | } |
| 5653 | ip->bw_first = FALSE; |
| 5654 | } |
| 5655 | |
| 5656 | /* |
| 5657 | * If iconv() has an error or there is not enough room, fail. |
| 5658 | */ |
| 5659 | if ((iconv(ip->bw_iconv_fd, (void *)&from, &fromlen, &to, &tolen) |
| 5660 | == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) |
| 5661 | || fromlen > CONV_RESTLEN) |
| 5662 | { |
| 5663 | ip->bw_conv_error = TRUE; |
| 5664 | return FAIL; |
| 5665 | } |
| 5666 | |
| 5667 | /* copy remainder to ip->bw_rest[] to be used for the next call. */ |
| 5668 | if (fromlen > 0) |
| 5669 | mch_memmove(ip->bw_rest, (void *)from, fromlen); |
| 5670 | ip->bw_restlen = (int)fromlen; |
| 5671 | |
| 5672 | buf = ip->bw_conv_buf; |
| 5673 | len = (int)((char_u *)to - ip->bw_conv_buf); |
| 5674 | } |
| 5675 | # endif |
| 5676 | } |
| 5677 | #endif /* FEAT_MBYTE */ |
| 5678 | |
| 5679 | #ifdef FEAT_CRYPT |
| 5680 | if (flags & FIO_ENCRYPTED) /* encrypt the data */ |
| 5681 | { |
| 5682 | int ztemp, t, i; |
| 5683 | |
| 5684 | for (i = 0; i < len; i++) |
| 5685 | { |
Bram Moolenaar | a3ff49f | 2010-05-30 22:48:02 +0200 | [diff] [blame] | 5686 | ztemp = buf[i]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5687 | buf[i] = ZENCODE(ztemp, t); |
| 5688 | } |
| 5689 | } |
| 5690 | #endif |
| 5691 | |
| 5692 | /* Repeat the write(), it may be interrupted by a signal. */ |
Bram Moolenaar | 36f5ac0 | 2007-05-06 12:40:34 +0000 | [diff] [blame] | 5693 | while (len > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5694 | { |
| 5695 | wlen = vim_write(ip->bw_fd, buf, len); |
| 5696 | if (wlen <= 0) /* error! */ |
| 5697 | return FAIL; |
| 5698 | len -= wlen; |
| 5699 | buf += wlen; |
| 5700 | } |
| 5701 | return OK; |
| 5702 | } |
| 5703 | |
| 5704 | #ifdef FEAT_MBYTE |
| 5705 | /* |
| 5706 | * Convert a Unicode character to bytes. |
Bram Moolenaar | 32b485f | 2009-07-29 16:06:27 +0000 | [diff] [blame] | 5707 | * Return TRUE for an error, FALSE when it's OK. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5708 | */ |
| 5709 | static int |
| 5710 | ucs2bytes(c, pp, flags) |
| 5711 | unsigned c; /* in: character */ |
| 5712 | char_u **pp; /* in/out: pointer to result */ |
| 5713 | int flags; /* FIO_ flags */ |
| 5714 | { |
| 5715 | char_u *p = *pp; |
| 5716 | int error = FALSE; |
| 5717 | int cc; |
| 5718 | |
| 5719 | |
| 5720 | if (flags & FIO_UCS4) |
| 5721 | { |
| 5722 | if (flags & FIO_ENDIAN_L) |
| 5723 | { |
| 5724 | *p++ = c; |
| 5725 | *p++ = (c >> 8); |
| 5726 | *p++ = (c >> 16); |
| 5727 | *p++ = (c >> 24); |
| 5728 | } |
| 5729 | else |
| 5730 | { |
| 5731 | *p++ = (c >> 24); |
| 5732 | *p++ = (c >> 16); |
| 5733 | *p++ = (c >> 8); |
| 5734 | *p++ = c; |
| 5735 | } |
| 5736 | } |
| 5737 | else if (flags & (FIO_UCS2 | FIO_UTF16)) |
| 5738 | { |
| 5739 | if (c >= 0x10000) |
| 5740 | { |
| 5741 | if (flags & FIO_UTF16) |
| 5742 | { |
| 5743 | /* Make two words, ten bits of the character in each. First |
| 5744 | * word is 0xd800 - 0xdbff, second one 0xdc00 - 0xdfff */ |
| 5745 | c -= 0x10000; |
| 5746 | if (c >= 0x100000) |
| 5747 | error = TRUE; |
| 5748 | cc = ((c >> 10) & 0x3ff) + 0xd800; |
| 5749 | if (flags & FIO_ENDIAN_L) |
| 5750 | { |
| 5751 | *p++ = cc; |
| 5752 | *p++ = ((unsigned)cc >> 8); |
| 5753 | } |
| 5754 | else |
| 5755 | { |
| 5756 | *p++ = ((unsigned)cc >> 8); |
| 5757 | *p++ = cc; |
| 5758 | } |
| 5759 | c = (c & 0x3ff) + 0xdc00; |
| 5760 | } |
| 5761 | else |
| 5762 | error = TRUE; |
| 5763 | } |
| 5764 | if (flags & FIO_ENDIAN_L) |
| 5765 | { |
| 5766 | *p++ = c; |
| 5767 | *p++ = (c >> 8); |
| 5768 | } |
| 5769 | else |
| 5770 | { |
| 5771 | *p++ = (c >> 8); |
| 5772 | *p++ = c; |
| 5773 | } |
| 5774 | } |
| 5775 | else /* Latin1 */ |
| 5776 | { |
| 5777 | if (c >= 0x100) |
| 5778 | { |
| 5779 | error = TRUE; |
| 5780 | *p++ = 0xBF; |
| 5781 | } |
| 5782 | else |
| 5783 | *p++ = c; |
| 5784 | } |
| 5785 | |
| 5786 | *pp = p; |
| 5787 | return error; |
| 5788 | } |
| 5789 | |
| 5790 | /* |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 5791 | * Return TRUE if file encoding "fenc" requires conversion from or to |
| 5792 | * 'encoding'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5793 | */ |
| 5794 | static int |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 5795 | need_conversion(fenc) |
| 5796 | char_u *fenc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5797 | { |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 5798 | int same_encoding; |
| 5799 | int enc_flags; |
| 5800 | int fenc_flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5801 | |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 5802 | if (*fenc == NUL || STRCMP(p_enc, fenc) == 0) |
Bram Moolenaar | 442b422 | 2010-05-24 21:34:22 +0200 | [diff] [blame] | 5803 | { |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 5804 | same_encoding = TRUE; |
Bram Moolenaar | 442b422 | 2010-05-24 21:34:22 +0200 | [diff] [blame] | 5805 | fenc_flags = 0; |
| 5806 | } |
Bram Moolenaar | b5cdf2e | 2009-07-29 16:25:31 +0000 | [diff] [blame] | 5807 | else |
| 5808 | { |
| 5809 | /* Ignore difference between "ansi" and "latin1", "ucs-4" and |
| 5810 | * "ucs-4be", etc. */ |
| 5811 | enc_flags = get_fio_flags(p_enc); |
| 5812 | fenc_flags = get_fio_flags(fenc); |
| 5813 | same_encoding = (enc_flags != 0 && fenc_flags == enc_flags); |
| 5814 | } |
| 5815 | if (same_encoding) |
| 5816 | { |
| 5817 | /* Specified encoding matches with 'encoding'. This requires |
| 5818 | * conversion when 'encoding' is Unicode but not UTF-8. */ |
| 5819 | return enc_unicode != 0; |
| 5820 | } |
| 5821 | |
| 5822 | /* Encodings differ. However, conversion is not needed when 'enc' is any |
| 5823 | * Unicode encoding and the file is UTF-8. */ |
| 5824 | return !(enc_utf8 && fenc_flags == FIO_UTF8); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5825 | } |
| 5826 | |
| 5827 | /* |
| 5828 | * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the |
| 5829 | * internal conversion. |
| 5830 | * if "ptr" is an empty string, use 'encoding'. |
| 5831 | */ |
| 5832 | static int |
| 5833 | get_fio_flags(ptr) |
| 5834 | char_u *ptr; |
| 5835 | { |
| 5836 | int prop; |
| 5837 | |
| 5838 | if (*ptr == NUL) |
| 5839 | ptr = p_enc; |
| 5840 | |
| 5841 | prop = enc_canon_props(ptr); |
| 5842 | if (prop & ENC_UNICODE) |
| 5843 | { |
| 5844 | if (prop & ENC_2BYTE) |
| 5845 | { |
| 5846 | if (prop & ENC_ENDIAN_L) |
| 5847 | return FIO_UCS2 | FIO_ENDIAN_L; |
| 5848 | return FIO_UCS2; |
| 5849 | } |
| 5850 | if (prop & ENC_4BYTE) |
| 5851 | { |
| 5852 | if (prop & ENC_ENDIAN_L) |
| 5853 | return FIO_UCS4 | FIO_ENDIAN_L; |
| 5854 | return FIO_UCS4; |
| 5855 | } |
| 5856 | if (prop & ENC_2WORD) |
| 5857 | { |
| 5858 | if (prop & ENC_ENDIAN_L) |
| 5859 | return FIO_UTF16 | FIO_ENDIAN_L; |
| 5860 | return FIO_UTF16; |
| 5861 | } |
| 5862 | return FIO_UTF8; |
| 5863 | } |
| 5864 | if (prop & ENC_LATIN1) |
| 5865 | return FIO_LATIN1; |
| 5866 | /* must be ENC_DBCS, requires iconv() */ |
| 5867 | return 0; |
| 5868 | } |
| 5869 | |
| 5870 | #ifdef WIN3264 |
| 5871 | /* |
| 5872 | * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed |
| 5873 | * for the conversion MS-Windows can do for us. Also accept "utf-8". |
| 5874 | * Used for conversion between 'encoding' and 'fileencoding'. |
| 5875 | */ |
| 5876 | static int |
| 5877 | get_win_fio_flags(ptr) |
| 5878 | char_u *ptr; |
| 5879 | { |
| 5880 | int cp; |
| 5881 | |
| 5882 | /* Cannot do this when 'encoding' is not utf-8 and not a codepage. */ |
| 5883 | if (!enc_utf8 && enc_codepage <= 0) |
| 5884 | return 0; |
| 5885 | |
| 5886 | cp = encname2codepage(ptr); |
| 5887 | if (cp == 0) |
| 5888 | { |
| 5889 | # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ |
| 5890 | if (STRCMP(ptr, "utf-8") == 0) |
| 5891 | cp = CP_UTF8; |
| 5892 | else |
| 5893 | # endif |
| 5894 | return 0; |
| 5895 | } |
| 5896 | return FIO_PUT_CP(cp) | FIO_CODEPAGE; |
| 5897 | } |
| 5898 | #endif |
| 5899 | |
| 5900 | #ifdef MACOS_X |
| 5901 | /* |
| 5902 | * Check "ptr" for a Carbon supported encoding and return the FIO_ flags |
| 5903 | * needed for the internal conversion to/from utf-8 or latin1. |
| 5904 | */ |
| 5905 | static int |
| 5906 | get_mac_fio_flags(ptr) |
| 5907 | char_u *ptr; |
| 5908 | { |
| 5909 | if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0) |
| 5910 | && (enc_canon_props(ptr) & ENC_MACROMAN)) |
| 5911 | return FIO_MACROMAN; |
| 5912 | return 0; |
| 5913 | } |
| 5914 | #endif |
| 5915 | |
| 5916 | /* |
| 5917 | * Check for a Unicode BOM (Byte Order Mark) at the start of p[size]. |
| 5918 | * "size" must be at least 2. |
| 5919 | * Return the name of the encoding and set "*lenp" to the length. |
| 5920 | * Returns NULL when no BOM found. |
| 5921 | */ |
| 5922 | static char_u * |
| 5923 | check_for_bom(p, size, lenp, flags) |
| 5924 | char_u *p; |
| 5925 | long size; |
| 5926 | int *lenp; |
| 5927 | int flags; |
| 5928 | { |
| 5929 | char *name = NULL; |
| 5930 | int len = 2; |
| 5931 | |
| 5932 | if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf |
Bram Moolenaar | ee0f5a6 | 2008-07-24 20:09:16 +0000 | [diff] [blame] | 5933 | && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5934 | { |
| 5935 | name = "utf-8"; /* EF BB BF */ |
| 5936 | len = 3; |
| 5937 | } |
| 5938 | else if (p[0] == 0xff && p[1] == 0xfe) |
| 5939 | { |
| 5940 | if (size >= 4 && p[2] == 0 && p[3] == 0 |
| 5941 | && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L))) |
| 5942 | { |
| 5943 | name = "ucs-4le"; /* FF FE 00 00 */ |
| 5944 | len = 4; |
| 5945 | } |
Bram Moolenaar | 223a189 | 2008-11-11 20:57:11 +0000 | [diff] [blame] | 5946 | else if (flags == (FIO_UCS2 | FIO_ENDIAN_L)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5947 | name = "ucs-2le"; /* FF FE */ |
Bram Moolenaar | 223a189 | 2008-11-11 20:57:11 +0000 | [diff] [blame] | 5948 | else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L)) |
| 5949 | /* utf-16le is preferred, it also works for ucs-2le text */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5950 | name = "utf-16le"; /* FF FE */ |
| 5951 | } |
| 5952 | else if (p[0] == 0xfe && p[1] == 0xff |
| 5953 | && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16)) |
| 5954 | { |
Bram Moolenaar | ffd82c5 | 2008-02-20 17:15:26 +0000 | [diff] [blame] | 5955 | /* Default to utf-16, it works also for ucs-2 text. */ |
| 5956 | if (flags == FIO_UCS2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5957 | name = "ucs-2"; /* FE FF */ |
Bram Moolenaar | ffd82c5 | 2008-02-20 17:15:26 +0000 | [diff] [blame] | 5958 | else |
| 5959 | name = "utf-16"; /* FE FF */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5960 | } |
| 5961 | else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe |
| 5962 | && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4)) |
| 5963 | { |
| 5964 | name = "ucs-4"; /* 00 00 FE FF */ |
| 5965 | len = 4; |
| 5966 | } |
| 5967 | |
| 5968 | *lenp = len; |
| 5969 | return (char_u *)name; |
| 5970 | } |
| 5971 | |
| 5972 | /* |
| 5973 | * Generate a BOM in "buf[4]" for encoding "name". |
| 5974 | * Return the length of the BOM (zero when no BOM). |
| 5975 | */ |
| 5976 | static int |
| 5977 | make_bom(buf, name) |
| 5978 | char_u *buf; |
| 5979 | char_u *name; |
| 5980 | { |
| 5981 | int flags; |
| 5982 | char_u *p; |
| 5983 | |
| 5984 | flags = get_fio_flags(name); |
| 5985 | |
| 5986 | /* Can't put a BOM in a non-Unicode file. */ |
| 5987 | if (flags == FIO_LATIN1 || flags == 0) |
| 5988 | return 0; |
| 5989 | |
| 5990 | if (flags == FIO_UTF8) /* UTF-8 */ |
| 5991 | { |
| 5992 | buf[0] = 0xef; |
| 5993 | buf[1] = 0xbb; |
| 5994 | buf[2] = 0xbf; |
| 5995 | return 3; |
| 5996 | } |
| 5997 | p = buf; |
| 5998 | (void)ucs2bytes(0xfeff, &p, flags); |
| 5999 | return (int)(p - buf); |
| 6000 | } |
| 6001 | #endif |
| 6002 | |
Bram Moolenaar | d4cacdf | 2007-10-03 10:50:10 +0000 | [diff] [blame] | 6003 | #if defined(FEAT_VIMINFO) || defined(FEAT_BROWSE) || \ |
Bram Moolenaar | a0174af | 2008-01-02 20:08:25 +0000 | [diff] [blame] | 6004 | defined(FEAT_QUICKFIX) || defined(FEAT_AUTOCMD) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6005 | /* |
| 6006 | * Try to find a shortname by comparing the fullname with the current |
| 6007 | * directory. |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 6008 | * Returns "full_path" or pointer into "full_path" if shortened. |
| 6009 | */ |
| 6010 | char_u * |
| 6011 | shorten_fname1(full_path) |
| 6012 | char_u *full_path; |
| 6013 | { |
| 6014 | char_u dirname[MAXPATHL]; |
| 6015 | char_u *p = full_path; |
| 6016 | |
| 6017 | if (mch_dirname(dirname, MAXPATHL) == OK) |
| 6018 | { |
| 6019 | p = shorten_fname(full_path, dirname); |
| 6020 | if (p == NULL || *p == NUL) |
| 6021 | p = full_path; |
| 6022 | } |
| 6023 | return p; |
| 6024 | } |
Bram Moolenaar | d4cacdf | 2007-10-03 10:50:10 +0000 | [diff] [blame] | 6025 | #endif |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 6026 | |
| 6027 | /* |
| 6028 | * Try to find a shortname by comparing the fullname with the current |
| 6029 | * directory. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6030 | * Returns NULL if not shorter name possible, pointer into "full_path" |
| 6031 | * otherwise. |
| 6032 | */ |
| 6033 | char_u * |
| 6034 | shorten_fname(full_path, dir_name) |
| 6035 | char_u *full_path; |
| 6036 | char_u *dir_name; |
| 6037 | { |
| 6038 | int len; |
| 6039 | char_u *p; |
| 6040 | |
| 6041 | if (full_path == NULL) |
| 6042 | return NULL; |
| 6043 | len = (int)STRLEN(dir_name); |
| 6044 | if (fnamencmp(dir_name, full_path, len) == 0) |
| 6045 | { |
| 6046 | p = full_path + len; |
| 6047 | #if defined(MSDOS) || defined(MSWIN) || defined(OS2) |
| 6048 | /* |
| 6049 | * MSDOS: when a file is in the root directory, dir_name will end in a |
| 6050 | * slash, since C: by itself does not define a specific dir. In this |
| 6051 | * case p may already be correct. <negri> |
| 6052 | */ |
| 6053 | if (!((len > 2) && (*(p - 2) == ':'))) |
| 6054 | #endif |
| 6055 | { |
| 6056 | if (vim_ispathsep(*p)) |
| 6057 | ++p; |
| 6058 | #ifndef VMS /* the path separator is always part of the path */ |
| 6059 | else |
| 6060 | p = NULL; |
| 6061 | #endif |
| 6062 | } |
| 6063 | } |
| 6064 | #if defined(MSDOS) || defined(MSWIN) || defined(OS2) |
| 6065 | /* |
| 6066 | * When using a file in the current drive, remove the drive name: |
| 6067 | * "A:\dir\file" -> "\dir\file". This helps when moving a session file on |
| 6068 | * a floppy from "A:\dir" to "B:\dir". |
| 6069 | */ |
| 6070 | else if (len > 3 |
| 6071 | && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0]) |
| 6072 | && full_path[1] == ':' |
| 6073 | && vim_ispathsep(full_path[2])) |
| 6074 | p = full_path + 2; |
| 6075 | #endif |
| 6076 | else |
| 6077 | p = NULL; |
| 6078 | return p; |
| 6079 | } |
| 6080 | |
| 6081 | /* |
| 6082 | * Shorten filenames for all buffers. |
| 6083 | * When "force" is TRUE: Use full path from now on for files currently being |
| 6084 | * edited, both for file name and swap file name. Try to shorten the file |
| 6085 | * names a bit, if safe to do so. |
| 6086 | * When "force" is FALSE: Only try to shorten absolute file names. |
| 6087 | * For buffers that have buftype "nofile" or "scratch": never change the file |
| 6088 | * name. |
| 6089 | */ |
| 6090 | void |
| 6091 | shorten_fnames(force) |
| 6092 | int force; |
| 6093 | { |
| 6094 | char_u dirname[MAXPATHL]; |
| 6095 | buf_T *buf; |
| 6096 | char_u *p; |
| 6097 | |
| 6098 | mch_dirname(dirname, MAXPATHL); |
| 6099 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 6100 | { |
| 6101 | if (buf->b_fname != NULL |
| 6102 | #ifdef FEAT_QUICKFIX |
| 6103 | && !bt_nofile(buf) |
| 6104 | #endif |
| 6105 | && !path_with_url(buf->b_fname) |
| 6106 | && (force |
| 6107 | || buf->b_sfname == NULL |
| 6108 | || mch_isFullName(buf->b_sfname))) |
| 6109 | { |
| 6110 | vim_free(buf->b_sfname); |
| 6111 | buf->b_sfname = NULL; |
| 6112 | p = shorten_fname(buf->b_ffname, dirname); |
| 6113 | if (p != NULL) |
| 6114 | { |
| 6115 | buf->b_sfname = vim_strsave(p); |
| 6116 | buf->b_fname = buf->b_sfname; |
| 6117 | } |
| 6118 | if (p == NULL || buf->b_fname == NULL) |
| 6119 | buf->b_fname = buf->b_ffname; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6120 | } |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 6121 | |
| 6122 | /* Always make the swap file name a full path, a "nofile" buffer may |
| 6123 | * also have a swap file. */ |
| 6124 | mf_fullname(buf->b_ml.ml_mfp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6125 | } |
| 6126 | #ifdef FEAT_WINDOWS |
| 6127 | status_redraw_all(); |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 6128 | redraw_tabline = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6129 | #endif |
| 6130 | } |
| 6131 | |
| 6132 | #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \ |
| 6133 | || defined(FEAT_GUI_MSWIN) \ |
| 6134 | || defined(FEAT_GUI_MAC) \ |
| 6135 | || defined(PROTO) |
| 6136 | /* |
| 6137 | * Shorten all filenames in "fnames[count]" by current directory. |
| 6138 | */ |
| 6139 | void |
| 6140 | shorten_filenames(fnames, count) |
| 6141 | char_u **fnames; |
| 6142 | int count; |
| 6143 | { |
| 6144 | int i; |
| 6145 | char_u dirname[MAXPATHL]; |
| 6146 | char_u *p; |
| 6147 | |
| 6148 | if (fnames == NULL || count < 1) |
| 6149 | return; |
| 6150 | mch_dirname(dirname, sizeof(dirname)); |
| 6151 | for (i = 0; i < count; ++i) |
| 6152 | { |
| 6153 | if ((p = shorten_fname(fnames[i], dirname)) != NULL) |
| 6154 | { |
| 6155 | /* shorten_fname() returns pointer in given "fnames[i]". If free |
| 6156 | * "fnames[i]" first, "p" becomes invalid. So we need to copy |
| 6157 | * "p" first then free fnames[i]. */ |
| 6158 | p = vim_strsave(p); |
| 6159 | vim_free(fnames[i]); |
| 6160 | fnames[i] = p; |
| 6161 | } |
| 6162 | } |
| 6163 | } |
| 6164 | #endif |
| 6165 | |
| 6166 | /* |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 6167 | * 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] | 6168 | * fo_o_h.ext for MSDOS or when shortname option set. |
| 6169 | * |
| 6170 | * Assumed that fname is a valid name found in the filesystem we assure that |
| 6171 | * the return value is a different name and ends in 'ext'. |
| 6172 | * "ext" MUST be at most 4 characters long if it starts with a dot, 3 |
| 6173 | * characters otherwise. |
| 6174 | * Space for the returned name is allocated, must be freed later. |
| 6175 | * Returns NULL when out of memory. |
| 6176 | */ |
| 6177 | char_u * |
| 6178 | modname(fname, ext, prepend_dot) |
| 6179 | char_u *fname, *ext; |
| 6180 | int prepend_dot; /* may prepend a '.' to file name */ |
| 6181 | { |
| 6182 | return buf_modname( |
| 6183 | #ifdef SHORT_FNAME |
| 6184 | TRUE, |
| 6185 | #else |
| 6186 | (curbuf->b_p_sn || curbuf->b_shortname), |
| 6187 | #endif |
| 6188 | fname, ext, prepend_dot); |
| 6189 | } |
| 6190 | |
| 6191 | char_u * |
| 6192 | buf_modname(shortname, fname, ext, prepend_dot) |
| 6193 | int shortname; /* use 8.3 file name */ |
| 6194 | char_u *fname, *ext; |
| 6195 | int prepend_dot; /* may prepend a '.' to file name */ |
| 6196 | { |
| 6197 | char_u *retval; |
| 6198 | char_u *s; |
| 6199 | char_u *e; |
| 6200 | char_u *ptr; |
| 6201 | int fnamelen, extlen; |
| 6202 | |
| 6203 | extlen = (int)STRLEN(ext); |
| 6204 | |
| 6205 | /* |
| 6206 | * If there is no file name we must get the name of the current directory |
| 6207 | * (we need the full path in case :cd is used). |
| 6208 | */ |
| 6209 | if (fname == NULL || *fname == NUL) |
| 6210 | { |
| 6211 | retval = alloc((unsigned)(MAXPATHL + extlen + 3)); |
| 6212 | if (retval == NULL) |
| 6213 | return NULL; |
| 6214 | if (mch_dirname(retval, MAXPATHL) == FAIL || |
| 6215 | (fnamelen = (int)STRLEN(retval)) == 0) |
| 6216 | { |
| 6217 | vim_free(retval); |
| 6218 | return NULL; |
| 6219 | } |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 6220 | if (!after_pathsep(retval, retval + fnamelen)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6221 | { |
| 6222 | retval[fnamelen++] = PATHSEP; |
| 6223 | retval[fnamelen] = NUL; |
| 6224 | } |
| 6225 | #ifndef SHORT_FNAME |
| 6226 | prepend_dot = FALSE; /* nothing to prepend a dot to */ |
| 6227 | #endif |
| 6228 | } |
| 6229 | else |
| 6230 | { |
| 6231 | fnamelen = (int)STRLEN(fname); |
| 6232 | retval = alloc((unsigned)(fnamelen + extlen + 3)); |
| 6233 | if (retval == NULL) |
| 6234 | return NULL; |
| 6235 | STRCPY(retval, fname); |
| 6236 | #ifdef VMS |
| 6237 | vms_remove_version(retval); /* we do not need versions here */ |
| 6238 | #endif |
| 6239 | } |
| 6240 | |
| 6241 | /* |
| 6242 | * search backwards until we hit a '/', '\' or ':' replacing all '.' |
| 6243 | * by '_' for MSDOS or when shortname option set and ext starts with a dot. |
| 6244 | * Then truncate what is after the '/', '\' or ':' to 8 characters for |
| 6245 | * MSDOS and 26 characters for AMIGA, a lot more for UNIX. |
| 6246 | */ |
Bram Moolenaar | 53180ce | 2005-07-05 21:48:14 +0000 | [diff] [blame] | 6247 | for (ptr = retval + fnamelen; ptr > retval; mb_ptr_back(retval, ptr)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6248 | { |
| 6249 | #ifndef RISCOS |
| 6250 | if (*ext == '.' |
Bram Moolenaar | 53180ce | 2005-07-05 21:48:14 +0000 | [diff] [blame] | 6251 | # ifdef USE_LONG_FNAME |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6252 | && (!USE_LONG_FNAME || shortname) |
Bram Moolenaar | 53180ce | 2005-07-05 21:48:14 +0000 | [diff] [blame] | 6253 | # else |
| 6254 | # ifndef SHORT_FNAME |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6255 | && shortname |
Bram Moolenaar | 53180ce | 2005-07-05 21:48:14 +0000 | [diff] [blame] | 6256 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6257 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6258 | ) |
| 6259 | if (*ptr == '.') /* replace '.' by '_' */ |
| 6260 | *ptr = '_'; |
Bram Moolenaar | 53180ce | 2005-07-05 21:48:14 +0000 | [diff] [blame] | 6261 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6262 | if (vim_ispathsep(*ptr)) |
Bram Moolenaar | 53180ce | 2005-07-05 21:48:14 +0000 | [diff] [blame] | 6263 | { |
| 6264 | ++ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6265 | break; |
Bram Moolenaar | 53180ce | 2005-07-05 21:48:14 +0000 | [diff] [blame] | 6266 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6267 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6268 | |
| 6269 | /* the file name has at most BASENAMELEN characters. */ |
| 6270 | #ifndef SHORT_FNAME |
| 6271 | if (STRLEN(ptr) > (unsigned)BASENAMELEN) |
| 6272 | ptr[BASENAMELEN] = '\0'; |
| 6273 | #endif |
| 6274 | |
| 6275 | s = ptr + STRLEN(ptr); |
| 6276 | |
| 6277 | /* |
| 6278 | * For 8.3 file names we may have to reduce the length. |
| 6279 | */ |
| 6280 | #ifdef USE_LONG_FNAME |
| 6281 | if (!USE_LONG_FNAME || shortname) |
| 6282 | #else |
| 6283 | # ifndef SHORT_FNAME |
| 6284 | if (shortname) |
| 6285 | # endif |
| 6286 | #endif |
| 6287 | { |
| 6288 | /* |
| 6289 | * If there is no file name, or the file name ends in '/', and the |
| 6290 | * extension starts with '.', put a '_' before the dot, because just |
| 6291 | * ".ext" is invalid. |
| 6292 | */ |
| 6293 | if (fname == NULL || *fname == NUL |
| 6294 | || vim_ispathsep(fname[STRLEN(fname) - 1])) |
| 6295 | { |
| 6296 | #ifdef RISCOS |
| 6297 | if (*ext == '/') |
| 6298 | #else |
| 6299 | if (*ext == '.') |
| 6300 | #endif |
| 6301 | *s++ = '_'; |
| 6302 | } |
| 6303 | /* |
| 6304 | * If the extension starts with '.', truncate the base name at 8 |
| 6305 | * characters |
| 6306 | */ |
| 6307 | #ifdef RISCOS |
| 6308 | /* We normally use '/', but swap files are '_' */ |
| 6309 | else if (*ext == '/' || *ext == '_') |
| 6310 | #else |
| 6311 | else if (*ext == '.') |
| 6312 | #endif |
| 6313 | { |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 6314 | if ((size_t)(s - ptr) > (size_t)8) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6315 | { |
| 6316 | s = ptr + 8; |
| 6317 | *s = '\0'; |
| 6318 | } |
| 6319 | } |
| 6320 | /* |
| 6321 | * If the extension doesn't start with '.', and the file name |
| 6322 | * doesn't have an extension yet, append a '.' |
| 6323 | */ |
| 6324 | #ifdef RISCOS |
| 6325 | else if ((e = vim_strchr(ptr, '/')) == NULL) |
| 6326 | *s++ = '/'; |
| 6327 | #else |
| 6328 | else if ((e = vim_strchr(ptr, '.')) == NULL) |
| 6329 | *s++ = '.'; |
| 6330 | #endif |
| 6331 | /* |
| 6332 | * If the extension doesn't start with '.', and there already is an |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 6333 | * extension, it may need to be truncated |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6334 | */ |
| 6335 | else if ((int)STRLEN(e) + extlen > 4) |
| 6336 | s = e + 4 - extlen; |
| 6337 | } |
| 6338 | #if defined(OS2) || defined(USE_LONG_FNAME) || defined(WIN3264) |
| 6339 | /* |
| 6340 | * If there is no file name, and the extension starts with '.', put a |
| 6341 | * '_' before the dot, because just ".ext" may be invalid if it's on a |
| 6342 | * FAT partition, and on HPFS it doesn't matter. |
| 6343 | */ |
| 6344 | else if ((fname == NULL || *fname == NUL) && *ext == '.') |
| 6345 | *s++ = '_'; |
| 6346 | #endif |
| 6347 | |
| 6348 | /* |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 6349 | * Append the extension. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6350 | * ext can start with '.' and cannot exceed 3 more characters. |
| 6351 | */ |
| 6352 | STRCPY(s, ext); |
| 6353 | |
| 6354 | #ifndef SHORT_FNAME |
| 6355 | /* |
| 6356 | * Prepend the dot. |
| 6357 | */ |
| 6358 | if (prepend_dot && !shortname && *(e = gettail(retval)) != |
| 6359 | #ifdef RISCOS |
| 6360 | '/' |
| 6361 | #else |
| 6362 | '.' |
| 6363 | #endif |
| 6364 | #ifdef USE_LONG_FNAME |
| 6365 | && USE_LONG_FNAME |
| 6366 | #endif |
| 6367 | ) |
| 6368 | { |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 6369 | STRMOVE(e + 1, e); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6370 | #ifdef RISCOS |
| 6371 | *e = '/'; |
| 6372 | #else |
| 6373 | *e = '.'; |
| 6374 | #endif |
| 6375 | } |
| 6376 | #endif |
| 6377 | |
| 6378 | /* |
| 6379 | * Check that, after appending the extension, the file name is really |
| 6380 | * different. |
| 6381 | */ |
| 6382 | if (fname != NULL && STRCMP(fname, retval) == 0) |
| 6383 | { |
| 6384 | /* we search for a character that can be replaced by '_' */ |
| 6385 | while (--s >= ptr) |
| 6386 | { |
| 6387 | if (*s != '_') |
| 6388 | { |
| 6389 | *s = '_'; |
| 6390 | break; |
| 6391 | } |
| 6392 | } |
| 6393 | if (s < ptr) /* fname was "________.<ext>", how tricky! */ |
| 6394 | *ptr = 'v'; |
| 6395 | } |
| 6396 | return retval; |
| 6397 | } |
| 6398 | |
| 6399 | /* |
| 6400 | * Like fgets(), but if the file line is too long, it is truncated and the |
| 6401 | * rest of the line is thrown away. Returns TRUE for end-of-file. |
| 6402 | */ |
| 6403 | int |
| 6404 | vim_fgets(buf, size, fp) |
| 6405 | char_u *buf; |
| 6406 | int size; |
| 6407 | FILE *fp; |
| 6408 | { |
| 6409 | char *eof; |
| 6410 | #define FGETS_SIZE 200 |
| 6411 | char tbuf[FGETS_SIZE]; |
| 6412 | |
| 6413 | buf[size - 2] = NUL; |
| 6414 | #ifdef USE_CR |
| 6415 | eof = fgets_cr((char *)buf, size, fp); |
| 6416 | #else |
| 6417 | eof = fgets((char *)buf, size, fp); |
| 6418 | #endif |
| 6419 | if (buf[size - 2] != NUL && buf[size - 2] != '\n') |
| 6420 | { |
| 6421 | buf[size - 1] = NUL; /* Truncate the line */ |
| 6422 | |
| 6423 | /* Now throw away the rest of the line: */ |
| 6424 | do |
| 6425 | { |
| 6426 | tbuf[FGETS_SIZE - 2] = NUL; |
| 6427 | #ifdef USE_CR |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 6428 | ignoredp = fgets_cr((char *)tbuf, FGETS_SIZE, fp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6429 | #else |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 6430 | ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6431 | #endif |
| 6432 | } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n'); |
| 6433 | } |
| 6434 | return (eof == NULL); |
| 6435 | } |
| 6436 | |
| 6437 | #if defined(USE_CR) || defined(PROTO) |
| 6438 | /* |
| 6439 | * Like vim_fgets(), but accept any line terminator: CR, CR-LF or LF. |
| 6440 | * Returns TRUE for end-of-file. |
| 6441 | * Only used for the Mac, because it's much slower than vim_fgets(). |
| 6442 | */ |
| 6443 | int |
| 6444 | tag_fgets(buf, size, fp) |
| 6445 | char_u *buf; |
| 6446 | int size; |
| 6447 | FILE *fp; |
| 6448 | { |
| 6449 | int i = 0; |
| 6450 | int c; |
| 6451 | int eof = FALSE; |
| 6452 | |
| 6453 | for (;;) |
| 6454 | { |
| 6455 | c = fgetc(fp); |
| 6456 | if (c == EOF) |
| 6457 | { |
| 6458 | eof = TRUE; |
| 6459 | break; |
| 6460 | } |
| 6461 | if (c == '\r') |
| 6462 | { |
| 6463 | /* Always store a NL for end-of-line. */ |
| 6464 | if (i < size - 1) |
| 6465 | buf[i++] = '\n'; |
| 6466 | c = fgetc(fp); |
| 6467 | if (c != '\n') /* Macintosh format: single CR. */ |
| 6468 | ungetc(c, fp); |
| 6469 | break; |
| 6470 | } |
| 6471 | if (i < size - 1) |
| 6472 | buf[i++] = c; |
| 6473 | if (c == '\n') |
| 6474 | break; |
| 6475 | } |
| 6476 | buf[i] = NUL; |
| 6477 | return eof; |
| 6478 | } |
| 6479 | #endif |
| 6480 | |
| 6481 | /* |
| 6482 | * rename() only works if both files are on the same file system, this |
| 6483 | * function will (attempts to?) copy the file across if rename fails -- webb |
| 6484 | * Return -1 for failure, 0 for success. |
| 6485 | */ |
| 6486 | int |
| 6487 | vim_rename(from, to) |
| 6488 | char_u *from; |
| 6489 | char_u *to; |
| 6490 | { |
| 6491 | int fd_in; |
| 6492 | int fd_out; |
| 6493 | int n; |
| 6494 | char *errmsg = NULL; |
| 6495 | char *buffer; |
| 6496 | #ifdef AMIGA |
| 6497 | BPTR flock; |
| 6498 | #endif |
| 6499 | struct stat st; |
Bram Moolenaar | 9be038d | 2005-03-08 22:34:32 +0000 | [diff] [blame] | 6500 | long perm; |
Bram Moolenaar | cd71fa3 | 2005-03-11 22:46:48 +0000 | [diff] [blame] | 6501 | #ifdef HAVE_ACL |
| 6502 | vim_acl_T acl; /* ACL from original file */ |
| 6503 | #endif |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6504 | #if defined(UNIX) || defined(CASE_INSENSITIVE_FILENAME) |
| 6505 | int use_tmp_file = FALSE; |
| 6506 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6507 | |
| 6508 | /* |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6509 | * When the names are identical, there is nothing to do. When they refer |
| 6510 | * to the same file (ignoring case and slash/backslash differences) but |
| 6511 | * the file name differs we need to go through a temp file. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6512 | */ |
| 6513 | if (fnamecmp(from, to) == 0) |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6514 | { |
| 6515 | #ifdef CASE_INSENSITIVE_FILENAME |
| 6516 | if (STRCMP(gettail(from), gettail(to)) != 0) |
| 6517 | use_tmp_file = TRUE; |
| 6518 | else |
| 6519 | #endif |
| 6520 | return 0; |
| 6521 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6522 | |
| 6523 | /* |
| 6524 | * Fail if the "from" file doesn't exist. Avoids that "to" is deleted. |
| 6525 | */ |
| 6526 | if (mch_stat((char *)from, &st) < 0) |
| 6527 | return -1; |
| 6528 | |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6529 | #ifdef UNIX |
| 6530 | { |
| 6531 | struct stat st_to; |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6532 | |
| 6533 | /* It's possible for the source and destination to be the same file. |
| 6534 | * This happens when "from" and "to" differ in case and are on a FAT32 |
| 6535 | * filesystem. In that case go through a temp file name. */ |
| 6536 | if (mch_stat((char *)to, &st_to) >= 0 |
| 6537 | && st.st_dev == st_to.st_dev |
| 6538 | && st.st_ino == st_to.st_ino) |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6539 | use_tmp_file = TRUE; |
| 6540 | } |
| 6541 | #endif |
| 6542 | |
| 6543 | #if defined(UNIX) || defined(CASE_INSENSITIVE_FILENAME) |
| 6544 | if (use_tmp_file) |
| 6545 | { |
| 6546 | char tempname[MAXPATHL + 1]; |
| 6547 | |
| 6548 | /* |
| 6549 | * Find a name that doesn't exist and is in the same directory. |
| 6550 | * Rename "from" to "tempname" and then rename "tempname" to "to". |
| 6551 | */ |
| 6552 | if (STRLEN(from) >= MAXPATHL - 5) |
| 6553 | return -1; |
| 6554 | STRCPY(tempname, from); |
| 6555 | for (n = 123; n < 99999; ++n) |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6556 | { |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6557 | sprintf((char *)gettail((char_u *)tempname), "%d", n); |
| 6558 | if (mch_stat(tempname, &st) < 0) |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6559 | { |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6560 | if (mch_rename((char *)from, tempname) == 0) |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6561 | { |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6562 | if (mch_rename(tempname, (char *)to) == 0) |
| 6563 | return 0; |
| 6564 | /* Strange, the second step failed. Try moving the |
| 6565 | * file back and return failure. */ |
| 6566 | mch_rename(tempname, (char *)from); |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6567 | return -1; |
| 6568 | } |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6569 | /* If it fails for one temp name it will most likely fail |
| 6570 | * for any temp name, give up. */ |
| 6571 | return -1; |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6572 | } |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6573 | } |
Bram Moolenaar | e0e6f99 | 2008-12-31 15:21:32 +0000 | [diff] [blame] | 6574 | return -1; |
Bram Moolenaar | 3576da7 | 2008-12-30 15:15:57 +0000 | [diff] [blame] | 6575 | } |
| 6576 | #endif |
| 6577 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6578 | /* |
| 6579 | * Delete the "to" file, this is required on some systems to make the |
| 6580 | * mch_rename() work, on other systems it makes sure that we don't have |
| 6581 | * two files when the mch_rename() fails. |
| 6582 | */ |
| 6583 | |
| 6584 | #ifdef AMIGA |
| 6585 | /* |
| 6586 | * With MSDOS-compatible filesystems (crossdos, messydos) it is possible |
| 6587 | * 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] | 6588 | * though the names are different. To avoid the chance of accidentally |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6589 | * deleting the "from" file (horror!) we lock it during the remove. |
| 6590 | * |
| 6591 | * When used for making a backup before writing the file: This should not |
| 6592 | * happen with ":w", because startscript() should detect this problem and |
| 6593 | * set buf->b_shortname, causing modname() to return a correct ".bak" file |
| 6594 | * name. This problem does exist with ":w filename", but then the |
| 6595 | * original file will be somewhere else so the backup isn't really |
| 6596 | * important. If autoscripting is off the rename may fail. |
| 6597 | */ |
| 6598 | flock = Lock((UBYTE *)from, (long)ACCESS_READ); |
| 6599 | #endif |
| 6600 | mch_remove(to); |
| 6601 | #ifdef AMIGA |
| 6602 | if (flock) |
| 6603 | UnLock(flock); |
| 6604 | #endif |
| 6605 | |
| 6606 | /* |
| 6607 | * First try a normal rename, return if it works. |
| 6608 | */ |
| 6609 | if (mch_rename((char *)from, (char *)to) == 0) |
| 6610 | return 0; |
| 6611 | |
| 6612 | /* |
| 6613 | * Rename() failed, try copying the file. |
| 6614 | */ |
Bram Moolenaar | 9be038d | 2005-03-08 22:34:32 +0000 | [diff] [blame] | 6615 | perm = mch_getperm(from); |
Bram Moolenaar | cd71fa3 | 2005-03-11 22:46:48 +0000 | [diff] [blame] | 6616 | #ifdef HAVE_ACL |
| 6617 | /* For systems that support ACL: get the ACL from the original file. */ |
| 6618 | acl = mch_get_acl(from); |
| 6619 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6620 | fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0); |
| 6621 | if (fd_in == -1) |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 6622 | { |
| 6623 | #ifdef HAVE_ACL |
| 6624 | mch_free_acl(acl); |
| 6625 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6626 | return -1; |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 6627 | } |
Bram Moolenaar | 9be038d | 2005-03-08 22:34:32 +0000 | [diff] [blame] | 6628 | |
| 6629 | /* Create the new file with same permissions as the original. */ |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 6630 | fd_out = mch_open((char *)to, |
| 6631 | O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6632 | if (fd_out == -1) |
| 6633 | { |
| 6634 | close(fd_in); |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 6635 | #ifdef HAVE_ACL |
| 6636 | mch_free_acl(acl); |
| 6637 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6638 | return -1; |
| 6639 | } |
| 6640 | |
| 6641 | buffer = (char *)alloc(BUFSIZE); |
| 6642 | if (buffer == NULL) |
| 6643 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6644 | close(fd_out); |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 6645 | close(fd_in); |
| 6646 | #ifdef HAVE_ACL |
| 6647 | mch_free_acl(acl); |
| 6648 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6649 | return -1; |
| 6650 | } |
| 6651 | |
| 6652 | while ((n = vim_read(fd_in, buffer, BUFSIZE)) > 0) |
| 6653 | if (vim_write(fd_out, buffer, n) != n) |
| 6654 | { |
| 6655 | errmsg = _("E208: Error writing to \"%s\""); |
| 6656 | break; |
| 6657 | } |
| 6658 | |
| 6659 | vim_free(buffer); |
| 6660 | close(fd_in); |
| 6661 | if (close(fd_out) < 0) |
| 6662 | errmsg = _("E209: Error closing \"%s\""); |
| 6663 | if (n < 0) |
| 6664 | { |
| 6665 | errmsg = _("E210: Error reading \"%s\""); |
| 6666 | to = from; |
| 6667 | } |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 6668 | #ifndef UNIX /* for Unix mch_open() already set the permission */ |
Bram Moolenaar | 9be038d | 2005-03-08 22:34:32 +0000 | [diff] [blame] | 6669 | mch_setperm(to, perm); |
Bram Moolenaar | c6039d8 | 2005-12-02 00:44:04 +0000 | [diff] [blame] | 6670 | #endif |
Bram Moolenaar | cd71fa3 | 2005-03-11 22:46:48 +0000 | [diff] [blame] | 6671 | #ifdef HAVE_ACL |
| 6672 | mch_set_acl(to, acl); |
Bram Moolenaar | b23a7e8 | 2008-06-27 18:42:32 +0000 | [diff] [blame] | 6673 | mch_free_acl(acl); |
Bram Moolenaar | cd71fa3 | 2005-03-11 22:46:48 +0000 | [diff] [blame] | 6674 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6675 | if (errmsg != NULL) |
| 6676 | { |
| 6677 | EMSG2(errmsg, to); |
| 6678 | return -1; |
| 6679 | } |
| 6680 | mch_remove(from); |
| 6681 | return 0; |
| 6682 | } |
| 6683 | |
| 6684 | static int already_warned = FALSE; |
| 6685 | |
| 6686 | /* |
| 6687 | * Check if any not hidden buffer has been changed. |
| 6688 | * Postpone the check if there are characters in the stuff buffer, a global |
| 6689 | * command is being executed, a mapping is being executed or an autocommand is |
| 6690 | * busy. |
| 6691 | * Returns TRUE if some message was written (screen should be redrawn and |
| 6692 | * cursor positioned). |
| 6693 | */ |
| 6694 | int |
| 6695 | check_timestamps(focus) |
| 6696 | int focus; /* called for GUI focus event */ |
| 6697 | { |
| 6698 | buf_T *buf; |
| 6699 | int didit = 0; |
| 6700 | int n; |
| 6701 | |
| 6702 | /* Don't check timestamps while system() or another low-level function may |
| 6703 | * cause us to lose and gain focus. */ |
| 6704 | if (no_check_timestamps > 0) |
| 6705 | return FALSE; |
| 6706 | |
| 6707 | /* Avoid doing a check twice. The OK/Reload dialog can cause a focus |
| 6708 | * event and we would keep on checking if the file is steadily growing. |
| 6709 | * Do check again after typing something. */ |
| 6710 | if (focus && did_check_timestamps) |
| 6711 | { |
| 6712 | need_check_timestamps = TRUE; |
| 6713 | return FALSE; |
| 6714 | } |
| 6715 | |
| 6716 | if (!stuff_empty() || global_busy || !typebuf_typed() |
| 6717 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 6718 | || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6719 | #endif |
| 6720 | ) |
| 6721 | need_check_timestamps = TRUE; /* check later */ |
| 6722 | else |
| 6723 | { |
| 6724 | ++no_wait_return; |
| 6725 | did_check_timestamps = TRUE; |
| 6726 | already_warned = FALSE; |
| 6727 | for (buf = firstbuf; buf != NULL; ) |
| 6728 | { |
| 6729 | /* Only check buffers in a window. */ |
| 6730 | if (buf->b_nwindows > 0) |
| 6731 | { |
| 6732 | n = buf_check_timestamp(buf, focus); |
| 6733 | if (didit < n) |
| 6734 | didit = n; |
| 6735 | if (n > 0 && !buf_valid(buf)) |
| 6736 | { |
| 6737 | /* Autocommands have removed the buffer, start at the |
| 6738 | * first one again. */ |
| 6739 | buf = firstbuf; |
| 6740 | continue; |
| 6741 | } |
| 6742 | } |
| 6743 | buf = buf->b_next; |
| 6744 | } |
| 6745 | --no_wait_return; |
| 6746 | need_check_timestamps = FALSE; |
| 6747 | if (need_wait_return && didit == 2) |
| 6748 | { |
| 6749 | /* make sure msg isn't overwritten */ |
| 6750 | msg_puts((char_u *)"\n"); |
| 6751 | out_flush(); |
| 6752 | } |
| 6753 | } |
| 6754 | return didit; |
| 6755 | } |
| 6756 | |
| 6757 | /* |
| 6758 | * Move all the lines from buffer "frombuf" to buffer "tobuf". |
| 6759 | * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not |
| 6760 | * empty. |
| 6761 | */ |
| 6762 | static int |
| 6763 | move_lines(frombuf, tobuf) |
| 6764 | buf_T *frombuf; |
| 6765 | buf_T *tobuf; |
| 6766 | { |
| 6767 | buf_T *tbuf = curbuf; |
| 6768 | int retval = OK; |
| 6769 | linenr_T lnum; |
| 6770 | char_u *p; |
| 6771 | |
| 6772 | /* Copy the lines in "frombuf" to "tobuf". */ |
| 6773 | curbuf = tobuf; |
| 6774 | for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum) |
| 6775 | { |
| 6776 | p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE)); |
| 6777 | if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL) |
| 6778 | { |
| 6779 | vim_free(p); |
| 6780 | retval = FAIL; |
| 6781 | break; |
| 6782 | } |
| 6783 | vim_free(p); |
| 6784 | } |
| 6785 | |
| 6786 | /* Delete all the lines in "frombuf". */ |
| 6787 | if (retval != FAIL) |
| 6788 | { |
| 6789 | curbuf = frombuf; |
Bram Moolenaar | 9460b9d | 2007-01-09 14:37:01 +0000 | [diff] [blame] | 6790 | for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum) |
| 6791 | if (ml_delete(lnum, FALSE) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6792 | { |
| 6793 | /* Oops! We could try putting back the saved lines, but that |
| 6794 | * might fail again... */ |
| 6795 | retval = FAIL; |
| 6796 | break; |
| 6797 | } |
| 6798 | } |
| 6799 | |
| 6800 | curbuf = tbuf; |
| 6801 | return retval; |
| 6802 | } |
| 6803 | |
| 6804 | /* |
| 6805 | * Check if buffer "buf" has been changed. |
| 6806 | * Also check if the file for a new buffer unexpectedly appeared. |
| 6807 | * return 1 if a changed buffer was found. |
| 6808 | * return 2 if a message has been displayed. |
| 6809 | * return 0 otherwise. |
| 6810 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6811 | int |
| 6812 | buf_check_timestamp(buf, focus) |
| 6813 | buf_T *buf; |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 6814 | int focus UNUSED; /* called for GUI focus event */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6815 | { |
| 6816 | struct stat st; |
| 6817 | int stat_res; |
| 6818 | int retval = 0; |
| 6819 | char_u *path; |
| 6820 | char_u *tbuf; |
| 6821 | char *mesg = NULL; |
Bram Moolenaar | 44ecf65 | 2005-03-07 23:09:59 +0000 | [diff] [blame] | 6822 | char *mesg2 = ""; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6823 | int helpmesg = FALSE; |
| 6824 | int reload = FALSE; |
| 6825 | #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
| 6826 | int can_reload = FALSE; |
| 6827 | #endif |
Bram Moolenaar | 914703b | 2010-05-31 21:59:46 +0200 | [diff] [blame] | 6828 | off_t orig_size = buf->b_orig_size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6829 | int orig_mode = buf->b_orig_mode; |
| 6830 | #ifdef FEAT_GUI |
| 6831 | int save_mouse_correct = need_mouse_correct; |
| 6832 | #endif |
| 6833 | #ifdef FEAT_AUTOCMD |
| 6834 | static int busy = FALSE; |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6835 | int n; |
| 6836 | char_u *s; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6837 | #endif |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6838 | char *reason; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6839 | |
| 6840 | /* If there is no file name, the buffer is not loaded, 'buftype' is |
| 6841 | * set, we are in the middle of a save or being called recursively: ignore |
| 6842 | * this buffer. */ |
| 6843 | if (buf->b_ffname == NULL |
| 6844 | || buf->b_ml.ml_mfp == NULL |
| 6845 | #if defined(FEAT_QUICKFIX) |
| 6846 | || *buf->b_p_bt != NUL |
| 6847 | #endif |
| 6848 | || buf->b_saving |
| 6849 | #ifdef FEAT_AUTOCMD |
| 6850 | || busy |
| 6851 | #endif |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 6852 | #ifdef FEAT_NETBEANS_INTG |
| 6853 | || isNetbeansBuffer(buf) |
| 6854 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6855 | ) |
| 6856 | return 0; |
| 6857 | |
| 6858 | if ( !(buf->b_flags & BF_NOTEDITED) |
| 6859 | && buf->b_mtime != 0 |
| 6860 | && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0 |
| 6861 | || time_differs((long)st.st_mtime, buf->b_mtime) |
| 6862 | #ifdef HAVE_ST_MODE |
| 6863 | || (int)st.st_mode != buf->b_orig_mode |
| 6864 | #else |
| 6865 | || mch_getperm(buf->b_ffname) != buf->b_orig_mode |
| 6866 | #endif |
| 6867 | )) |
| 6868 | { |
| 6869 | retval = 1; |
| 6870 | |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 6871 | /* set b_mtime to stop further warnings (e.g., when executing |
| 6872 | * FileChangedShell autocmd) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6873 | if (stat_res < 0) |
| 6874 | { |
| 6875 | buf->b_mtime = 0; |
| 6876 | buf->b_orig_size = 0; |
| 6877 | buf->b_orig_mode = 0; |
| 6878 | } |
| 6879 | else |
| 6880 | buf_store_time(buf, &st, buf->b_ffname); |
| 6881 | |
| 6882 | /* Don't do anything for a directory. Might contain the file |
| 6883 | * explorer. */ |
| 6884 | if (mch_isdir(buf->b_fname)) |
| 6885 | ; |
| 6886 | |
| 6887 | /* |
| 6888 | * If 'autoread' is set, the buffer has no changes and the file still |
| 6889 | * exists, reload the buffer. Use the buffer-local option value if it |
| 6890 | * was set, the global option value otherwise. |
| 6891 | */ |
| 6892 | else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar) |
| 6893 | && !bufIsChanged(buf) && stat_res >= 0) |
| 6894 | reload = TRUE; |
| 6895 | else |
| 6896 | { |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6897 | if (stat_res < 0) |
| 6898 | reason = "deleted"; |
| 6899 | else if (bufIsChanged(buf)) |
| 6900 | reason = "conflict"; |
| 6901 | else if (orig_size != buf->b_orig_size || buf_contents_changed(buf)) |
| 6902 | reason = "changed"; |
| 6903 | else if (orig_mode != buf->b_orig_mode) |
| 6904 | reason = "mode"; |
| 6905 | else |
| 6906 | reason = "time"; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6907 | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6908 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6909 | /* |
| 6910 | * Only give the warning if there are no FileChangedShell |
| 6911 | * autocommands. |
| 6912 | * Avoid being called recursively by setting "busy". |
| 6913 | */ |
| 6914 | busy = TRUE; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 6915 | # ifdef FEAT_EVAL |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6916 | set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1); |
| 6917 | set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 6918 | # endif |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 6919 | ++allbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6920 | n = apply_autocmds(EVENT_FILECHANGEDSHELL, |
| 6921 | buf->b_fname, buf->b_fname, FALSE, buf); |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 6922 | --allbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6923 | busy = FALSE; |
| 6924 | if (n) |
| 6925 | { |
| 6926 | if (!buf_valid(buf)) |
| 6927 | EMSG(_("E246: FileChangedShell autocommand deleted buffer")); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 6928 | # ifdef FEAT_EVAL |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6929 | s = get_vim_var_str(VV_FCS_CHOICE); |
| 6930 | if (STRCMP(s, "reload") == 0 && *reason != 'd') |
| 6931 | reload = TRUE; |
| 6932 | else if (STRCMP(s, "ask") == 0) |
| 6933 | n = FALSE; |
| 6934 | else |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 6935 | # endif |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6936 | return 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6937 | } |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6938 | if (!n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6939 | #endif |
| 6940 | { |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6941 | if (*reason == 'd') |
| 6942 | mesg = _("E211: File \"%s\" no longer available"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6943 | else |
| 6944 | { |
| 6945 | helpmesg = TRUE; |
| 6946 | #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
| 6947 | can_reload = TRUE; |
| 6948 | #endif |
| 6949 | /* |
| 6950 | * Check if the file contents really changed to avoid |
| 6951 | * giving a warning when only the timestamp was set (e.g., |
| 6952 | * checked out of CVS). Always warn when the buffer was |
| 6953 | * changed. |
| 6954 | */ |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6955 | if (reason[2] == 'n') |
| 6956 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6957 | 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] | 6958 | mesg2 = _("See \":help W12\" for more info."); |
| 6959 | } |
| 6960 | else if (reason[1] == 'h') |
| 6961 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6962 | mesg = _("W11: Warning: File \"%s\" has changed since editing started"); |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6963 | mesg2 = _("See \":help W11\" for more info."); |
| 6964 | } |
| 6965 | else if (*reason == 'm') |
| 6966 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6967 | mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started"); |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6968 | mesg2 = _("See \":help W16\" for more info."); |
| 6969 | } |
Bram Moolenaar | 85388b5 | 2009-06-24 09:58:32 +0000 | [diff] [blame] | 6970 | else |
| 6971 | /* Only timestamp changed, store it to avoid a warning |
| 6972 | * in check_mtime() later. */ |
| 6973 | buf->b_mtime_read = buf->b_mtime; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6974 | } |
| 6975 | } |
| 6976 | } |
| 6977 | |
| 6978 | } |
| 6979 | else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W) |
| 6980 | && vim_fexists(buf->b_ffname)) |
| 6981 | { |
| 6982 | retval = 1; |
| 6983 | mesg = _("W13: Warning: File \"%s\" has been created after editing started"); |
| 6984 | buf->b_flags |= BF_NEW_W; |
| 6985 | #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
| 6986 | can_reload = TRUE; |
| 6987 | #endif |
| 6988 | } |
| 6989 | |
| 6990 | if (mesg != NULL) |
| 6991 | { |
| 6992 | path = home_replace_save(buf, buf->b_fname); |
| 6993 | if (path != NULL) |
| 6994 | { |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 6995 | if (!helpmesg) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6996 | mesg2 = ""; |
| 6997 | tbuf = alloc((unsigned)(STRLEN(path) + STRLEN(mesg) |
| 6998 | + STRLEN(mesg2) + 2)); |
| 6999 | sprintf((char *)tbuf, mesg, path); |
Bram Moolenaar | 496c526 | 2009-03-18 14:42:00 +0000 | [diff] [blame] | 7000 | #ifdef FEAT_EVAL |
| 7001 | /* Set warningmsg here, before the unimportant and output-specific |
| 7002 | * mesg2 has been appended. */ |
| 7003 | set_vim_var_string(VV_WARNINGMSG, tbuf, -1); |
| 7004 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7005 | #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
| 7006 | if (can_reload) |
| 7007 | { |
| 7008 | if (*mesg2 != NUL) |
| 7009 | { |
| 7010 | STRCAT(tbuf, "\n"); |
| 7011 | STRCAT(tbuf, mesg2); |
| 7012 | } |
| 7013 | if (do_dialog(VIM_WARNING, (char_u *)_("Warning"), tbuf, |
| 7014 | (char_u *)_("&OK\n&Load File"), 1, NULL) == 2) |
| 7015 | reload = TRUE; |
| 7016 | } |
| 7017 | else |
| 7018 | #endif |
| 7019 | if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned) |
| 7020 | { |
| 7021 | if (*mesg2 != NUL) |
| 7022 | { |
| 7023 | STRCAT(tbuf, "; "); |
| 7024 | STRCAT(tbuf, mesg2); |
| 7025 | } |
| 7026 | EMSG(tbuf); |
| 7027 | retval = 2; |
| 7028 | } |
| 7029 | else |
| 7030 | { |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 7031 | # ifdef FEAT_AUTOCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7032 | if (!autocmd_busy) |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 7033 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7034 | { |
| 7035 | msg_start(); |
| 7036 | msg_puts_attr(tbuf, hl_attr(HLF_E) + MSG_HIST); |
| 7037 | if (*mesg2 != NUL) |
| 7038 | msg_puts_attr((char_u *)mesg2, |
| 7039 | hl_attr(HLF_W) + MSG_HIST); |
| 7040 | msg_clr_eos(); |
| 7041 | (void)msg_end(); |
| 7042 | if (emsg_silent == 0) |
| 7043 | { |
| 7044 | out_flush(); |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 7045 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7046 | if (!focus) |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 7047 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7048 | /* give the user some time to think about it */ |
| 7049 | ui_delay(1000L, TRUE); |
| 7050 | |
| 7051 | /* don't redraw and erase the message */ |
| 7052 | redraw_cmdline = FALSE; |
| 7053 | } |
| 7054 | } |
| 7055 | already_warned = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7056 | } |
| 7057 | |
| 7058 | vim_free(path); |
| 7059 | vim_free(tbuf); |
| 7060 | } |
| 7061 | } |
| 7062 | |
| 7063 | if (reload) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7064 | /* Reload the buffer. */ |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 7065 | buf_reload(buf, orig_mode); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7066 | |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 7067 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 7068 | /* Trigger FileChangedShell when the file was changed in any way. */ |
| 7069 | if (buf_valid(buf) && retval != 0) |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 7070 | (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, |
| 7071 | buf->b_fname, buf->b_fname, FALSE, buf); |
| 7072 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7073 | #ifdef FEAT_GUI |
| 7074 | /* restore this in case an autocommand has set it; it would break |
| 7075 | * 'mousefocus' */ |
| 7076 | need_mouse_correct = save_mouse_correct; |
| 7077 | #endif |
| 7078 | |
| 7079 | return retval; |
| 7080 | } |
| 7081 | |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7082 | /* |
| 7083 | * Reload a buffer that is already loaded. |
| 7084 | * Used when the file was changed outside of Vim. |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 7085 | * "orig_mode" is buf->b_orig_mode before the need for reloading was detected. |
| 7086 | * buf->b_orig_mode may have been reset already. |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7087 | */ |
| 7088 | void |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 7089 | buf_reload(buf, orig_mode) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7090 | buf_T *buf; |
Bram Moolenaar | 316059c | 2006-01-14 21:18:42 +0000 | [diff] [blame] | 7091 | int orig_mode; |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7092 | { |
| 7093 | exarg_T ea; |
| 7094 | pos_T old_cursor; |
| 7095 | linenr_T old_topline; |
| 7096 | int old_ro = buf->b_p_ro; |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7097 | buf_T *savebuf; |
| 7098 | int saved = OK; |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7099 | aco_save_T aco; |
| 7100 | |
| 7101 | /* set curwin/curbuf for "buf" and save some things */ |
| 7102 | aucmd_prepbuf(&aco, buf); |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7103 | |
| 7104 | /* We only want to read the text from the file, not reset the syntax |
| 7105 | * highlighting, clear marks, diff status, etc. Force the fileformat |
| 7106 | * and encoding to be the same. */ |
| 7107 | if (prep_exarg(&ea, buf) == OK) |
| 7108 | { |
| 7109 | old_cursor = curwin->w_cursor; |
| 7110 | old_topline = curwin->w_topline; |
| 7111 | |
| 7112 | /* |
| 7113 | * To behave like when a new file is edited (matters for |
| 7114 | * BufReadPost autocommands) we first need to delete the current |
| 7115 | * buffer contents. But if reading the file fails we should keep |
| 7116 | * the old contents. Can't use memory only, the file might be |
| 7117 | * too big. Use a hidden buffer to move the buffer contents to. |
| 7118 | */ |
| 7119 | if (bufempty()) |
| 7120 | savebuf = NULL; |
| 7121 | else |
| 7122 | { |
| 7123 | /* Allocate a buffer without putting it in the buffer list. */ |
| 7124 | savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 7125 | if (savebuf != NULL && buf == curbuf) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7126 | { |
| 7127 | /* Open the memline. */ |
| 7128 | curbuf = savebuf; |
| 7129 | curwin->w_buffer = savebuf; |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7130 | saved = ml_open(curbuf); |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7131 | curbuf = buf; |
| 7132 | curwin->w_buffer = buf; |
| 7133 | } |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 7134 | if (savebuf == NULL || saved == FAIL || buf != curbuf |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7135 | || move_lines(buf, savebuf) == FAIL) |
| 7136 | { |
| 7137 | EMSG2(_("E462: Could not prepare for reloading \"%s\""), |
| 7138 | buf->b_fname); |
| 7139 | saved = FAIL; |
| 7140 | } |
| 7141 | } |
| 7142 | |
| 7143 | if (saved == OK) |
| 7144 | { |
| 7145 | curbuf->b_flags |= BF_CHECK_RO; /* check for RO again */ |
| 7146 | #ifdef FEAT_AUTOCMD |
| 7147 | keep_filetype = TRUE; /* don't detect 'filetype' */ |
| 7148 | #endif |
| 7149 | if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, |
| 7150 | (linenr_T)0, |
| 7151 | (linenr_T)MAXLNUM, &ea, READ_NEW) == FAIL) |
| 7152 | { |
| 7153 | #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
| 7154 | if (!aborting()) |
| 7155 | #endif |
| 7156 | EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname); |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 7157 | if (savebuf != NULL && buf_valid(savebuf) && buf == curbuf) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7158 | { |
| 7159 | /* Put the text back from the save buffer. First |
| 7160 | * delete any lines that readfile() added. */ |
| 7161 | while (!bufempty()) |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 7162 | if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7163 | break; |
| 7164 | (void)move_lines(savebuf, buf); |
| 7165 | } |
| 7166 | } |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 7167 | else if (buf == curbuf) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7168 | { |
| 7169 | /* Mark the buffer as unmodified and free undo info. */ |
| 7170 | unchanged(buf, TRUE); |
| 7171 | u_blockfree(buf); |
| 7172 | u_clearall(buf); |
| 7173 | } |
| 7174 | } |
| 7175 | vim_free(ea.cmd); |
| 7176 | |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 7177 | if (savebuf != NULL && buf_valid(savebuf)) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7178 | wipe_buffer(savebuf, FALSE); |
| 7179 | |
| 7180 | #ifdef FEAT_DIFF |
| 7181 | /* Invalidate diff info if necessary. */ |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 7182 | diff_invalidate(curbuf); |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7183 | #endif |
| 7184 | |
| 7185 | /* Restore the topline and cursor position and check it (lines may |
| 7186 | * have been removed). */ |
| 7187 | if (old_topline > curbuf->b_ml.ml_line_count) |
| 7188 | curwin->w_topline = curbuf->b_ml.ml_line_count; |
| 7189 | else |
| 7190 | curwin->w_topline = old_topline; |
| 7191 | curwin->w_cursor = old_cursor; |
| 7192 | check_cursor(); |
| 7193 | update_topline(); |
| 7194 | #ifdef FEAT_AUTOCMD |
| 7195 | keep_filetype = FALSE; |
| 7196 | #endif |
| 7197 | #ifdef FEAT_FOLDING |
| 7198 | { |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 7199 | win_T *wp; |
| 7200 | tabpage_T *tp; |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7201 | |
| 7202 | /* Update folds unless they are defined manually. */ |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 7203 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7204 | if (wp->w_buffer == curwin->w_buffer |
| 7205 | && !foldmethodIsManual(wp)) |
| 7206 | foldUpdateAll(wp); |
| 7207 | } |
| 7208 | #endif |
| 7209 | /* If the mode didn't change and 'readonly' was set, keep the old |
| 7210 | * value; the user probably used the ":view" command. But don't |
| 7211 | * reset it, might have had a read error. */ |
| 7212 | if (orig_mode == curbuf->b_orig_mode) |
| 7213 | curbuf->b_p_ro |= old_ro; |
| 7214 | } |
| 7215 | |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7216 | /* restore curwin/curbuf and a few other things */ |
| 7217 | aucmd_restbuf(&aco); |
| 7218 | /* Careful: autocommands may have made "buf" invalid! */ |
Bram Moolenaar | 631d6f6 | 2005-06-07 21:02:10 +0000 | [diff] [blame] | 7219 | } |
| 7220 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7221 | void |
| 7222 | buf_store_time(buf, st, fname) |
| 7223 | buf_T *buf; |
| 7224 | struct stat *st; |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 7225 | char_u *fname UNUSED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7226 | { |
| 7227 | buf->b_mtime = (long)st->st_mtime; |
Bram Moolenaar | 914703b | 2010-05-31 21:59:46 +0200 | [diff] [blame] | 7228 | buf->b_orig_size = st->st_size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7229 | #ifdef HAVE_ST_MODE |
| 7230 | buf->b_orig_mode = (int)st->st_mode; |
| 7231 | #else |
| 7232 | buf->b_orig_mode = mch_getperm(fname); |
| 7233 | #endif |
| 7234 | } |
| 7235 | |
| 7236 | /* |
| 7237 | * Adjust the line with missing eol, used for the next write. |
| 7238 | * Used for do_filter(), when the input lines for the filter are deleted. |
| 7239 | */ |
| 7240 | void |
| 7241 | write_lnum_adjust(offset) |
| 7242 | linenr_T offset; |
| 7243 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7244 | if (write_no_eol_lnum != 0) /* only if there is a missing eol */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7245 | write_no_eol_lnum += offset; |
| 7246 | } |
| 7247 | |
| 7248 | #if defined(TEMPDIRNAMES) || defined(PROTO) |
| 7249 | static long temp_count = 0; /* Temp filename counter. */ |
| 7250 | |
| 7251 | /* |
| 7252 | * Delete the temp directory and all files it contains. |
| 7253 | */ |
| 7254 | void |
| 7255 | vim_deltempdir() |
| 7256 | { |
| 7257 | char_u **files; |
| 7258 | int file_count; |
| 7259 | int i; |
| 7260 | |
| 7261 | if (vim_tempdir != NULL) |
| 7262 | { |
| 7263 | sprintf((char *)NameBuff, "%s*", vim_tempdir); |
| 7264 | if (gen_expand_wildcards(1, &NameBuff, &file_count, &files, |
| 7265 | EW_DIR|EW_FILE|EW_SILENT) == OK) |
| 7266 | { |
| 7267 | for (i = 0; i < file_count; ++i) |
| 7268 | mch_remove(files[i]); |
| 7269 | FreeWild(file_count, files); |
| 7270 | } |
| 7271 | gettail(NameBuff)[-1] = NUL; |
| 7272 | (void)mch_rmdir(NameBuff); |
| 7273 | |
| 7274 | vim_free(vim_tempdir); |
| 7275 | vim_tempdir = NULL; |
| 7276 | } |
| 7277 | } |
| 7278 | #endif |
| 7279 | |
Bram Moolenaar | 4592dee | 2009-11-18 19:11:58 +0000 | [diff] [blame] | 7280 | #ifdef TEMPDIRNAMES |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7281 | /* |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7282 | * Directory "tempdir" was created. Expand this name to a full path and put |
| 7283 | * it in "vim_tempdir". This avoids that using ":cd" would confuse us. |
| 7284 | * "tempdir" must be no longer than MAXPATHL. |
| 7285 | */ |
| 7286 | static void |
| 7287 | vim_settempdir(tempdir) |
| 7288 | char_u *tempdir; |
| 7289 | { |
| 7290 | char_u *buf; |
| 7291 | |
| 7292 | buf = alloc((unsigned)MAXPATHL + 2); |
| 7293 | if (buf != NULL) |
| 7294 | { |
| 7295 | if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL) |
| 7296 | STRCPY(buf, tempdir); |
| 7297 | # ifdef __EMX__ |
| 7298 | if (vim_strchr(buf, '/') != NULL) |
| 7299 | STRCAT(buf, "/"); |
| 7300 | else |
| 7301 | # endif |
| 7302 | add_pathsep(buf); |
| 7303 | vim_tempdir = vim_strsave(buf); |
| 7304 | vim_free(buf); |
| 7305 | } |
| 7306 | } |
Bram Moolenaar | 4592dee | 2009-11-18 19:11:58 +0000 | [diff] [blame] | 7307 | #endif |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7308 | |
| 7309 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7310 | * vim_tempname(): Return a unique name that can be used for a temp file. |
| 7311 | * |
| 7312 | * The temp file is NOT created. |
| 7313 | * |
| 7314 | * The returned pointer is to allocated memory. |
| 7315 | * The returned pointer is NULL if no valid name was found. |
| 7316 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7317 | char_u * |
| 7318 | vim_tempname(extra_char) |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 7319 | int extra_char UNUSED; /* char to use in the name instead of '?' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7320 | { |
| 7321 | #ifdef USE_TMPNAM |
| 7322 | char_u itmp[L_tmpnam]; /* use tmpnam() */ |
| 7323 | #else |
| 7324 | char_u itmp[TEMPNAMELEN]; |
| 7325 | #endif |
| 7326 | |
| 7327 | #ifdef TEMPDIRNAMES |
| 7328 | static char *(tempdirs[]) = {TEMPDIRNAMES}; |
| 7329 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7330 | # ifndef EEXIST |
| 7331 | struct stat st; |
| 7332 | # endif |
| 7333 | |
| 7334 | /* |
| 7335 | * This will create a directory for private use by this instance of Vim. |
| 7336 | * This is done once, and the same directory is used for all temp files. |
| 7337 | * This method avoids security problems because of symlink attacks et al. |
| 7338 | * It's also a bit faster, because we only need to check for an existing |
| 7339 | * file when creating the directory and not for each temp file. |
| 7340 | */ |
| 7341 | if (vim_tempdir == NULL) |
| 7342 | { |
| 7343 | /* |
| 7344 | * Try the entries in TEMPDIRNAMES to create the temp directory. |
| 7345 | */ |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 7346 | for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7347 | { |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7348 | # ifndef HAVE_MKDTEMP |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 7349 | size_t itmplen; |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7350 | long nr; |
| 7351 | long off; |
| 7352 | # endif |
| 7353 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7354 | /* expand $TMP, leave room for "/v1100000/999999999" */ |
| 7355 | expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20); |
| 7356 | if (mch_isdir(itmp)) /* directory exists */ |
| 7357 | { |
| 7358 | # ifdef __EMX__ |
| 7359 | /* If $TMP contains a forward slash (perhaps using bash or |
| 7360 | * tcsh), don't add a backslash, use a forward slash! |
| 7361 | * Adding 2 backslashes didn't work. */ |
| 7362 | if (vim_strchr(itmp, '/') != NULL) |
| 7363 | STRCAT(itmp, "/"); |
| 7364 | else |
| 7365 | # endif |
| 7366 | add_pathsep(itmp); |
| 7367 | |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7368 | # ifdef HAVE_MKDTEMP |
| 7369 | /* Leave room for filename */ |
| 7370 | STRCAT(itmp, "vXXXXXX"); |
| 7371 | if (mkdtemp((char *)itmp) != NULL) |
| 7372 | vim_settempdir(itmp); |
| 7373 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7374 | /* Get an arbitrary number of up to 6 digits. When it's |
| 7375 | * unlikely that it already exists it will be faster, |
| 7376 | * otherwise it doesn't matter. The use of mkdir() avoids any |
| 7377 | * security problems because of the predictable number. */ |
| 7378 | nr = (mch_get_pid() + (long)time(NULL)) % 1000000L; |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 7379 | itmplen = STRLEN(itmp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7380 | |
| 7381 | /* Try up to 10000 different values until we find a name that |
| 7382 | * doesn't exist. */ |
| 7383 | for (off = 0; off < 10000L; ++off) |
| 7384 | { |
| 7385 | int r; |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7386 | # if defined(UNIX) || defined(VMS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7387 | mode_t umask_save; |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7388 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7389 | |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7390 | sprintf((char *)itmp + itmplen, "v%ld", nr + off); |
| 7391 | # ifndef EEXIST |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7392 | /* If mkdir() does not set errno to EEXIST, check for |
| 7393 | * existing file here. There is a race condition then, |
| 7394 | * although it's fail-safe. */ |
| 7395 | if (mch_stat((char *)itmp, &st) >= 0) |
| 7396 | continue; |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7397 | # endif |
| 7398 | # if defined(UNIX) || defined(VMS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7399 | /* Make sure the umask doesn't remove the executable bit. |
| 7400 | * "repl" has been reported to use "177". */ |
| 7401 | umask_save = umask(077); |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7402 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7403 | r = vim_mkdir(itmp, 0700); |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7404 | # if defined(UNIX) || defined(VMS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7405 | (void)umask(umask_save); |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7406 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7407 | if (r == 0) |
| 7408 | { |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7409 | vim_settempdir(itmp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7410 | break; |
| 7411 | } |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7412 | # ifdef EEXIST |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7413 | /* If the mkdir() didn't fail because the file/dir exists, |
| 7414 | * we probably can't create any dir here, try another |
| 7415 | * place. */ |
| 7416 | if (errno != EEXIST) |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7417 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7418 | break; |
| 7419 | } |
Bram Moolenaar | eaf0339 | 2009-11-17 11:08:52 +0000 | [diff] [blame] | 7420 | # endif /* HAVE_MKDTEMP */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7421 | if (vim_tempdir != NULL) |
| 7422 | break; |
| 7423 | } |
| 7424 | } |
| 7425 | } |
| 7426 | |
| 7427 | if (vim_tempdir != NULL) |
| 7428 | { |
| 7429 | /* There is no need to check if the file exists, because we own the |
| 7430 | * directory and nobody else creates a file in it. */ |
| 7431 | sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++); |
| 7432 | return vim_strsave(itmp); |
| 7433 | } |
| 7434 | |
| 7435 | return NULL; |
| 7436 | |
| 7437 | #else /* TEMPDIRNAMES */ |
| 7438 | |
| 7439 | # ifdef WIN3264 |
| 7440 | char szTempFile[_MAX_PATH + 1]; |
| 7441 | char buf4[4]; |
| 7442 | char_u *retval; |
| 7443 | char_u *p; |
| 7444 | |
| 7445 | STRCPY(itmp, ""); |
| 7446 | if (GetTempPath(_MAX_PATH, szTempFile) == 0) |
| 7447 | szTempFile[0] = NUL; /* GetTempPath() failed, use current dir */ |
| 7448 | strcpy(buf4, "VIM"); |
| 7449 | buf4[2] = extra_char; /* make it "VIa", "VIb", etc. */ |
| 7450 | if (GetTempFileName(szTempFile, buf4, 0, itmp) == 0) |
| 7451 | return NULL; |
| 7452 | /* GetTempFileName() will create the file, we don't want that */ |
| 7453 | (void)DeleteFile(itmp); |
| 7454 | |
| 7455 | /* Backslashes in a temp file name cause problems when filtering with |
| 7456 | * "sh". NOTE: This also checks 'shellcmdflag' to help those people who |
| 7457 | * didn't set 'shellslash'. */ |
| 7458 | retval = vim_strsave(itmp); |
| 7459 | if (*p_shcf == '-' || p_ssl) |
| 7460 | for (p = retval; *p; ++p) |
| 7461 | if (*p == '\\') |
| 7462 | *p = '/'; |
| 7463 | return retval; |
| 7464 | |
| 7465 | # else /* WIN3264 */ |
| 7466 | |
| 7467 | # ifdef USE_TMPNAM |
| 7468 | /* tmpnam() will make its own name */ |
| 7469 | if (*tmpnam((char *)itmp) == NUL) |
| 7470 | return NULL; |
| 7471 | # else |
| 7472 | char_u *p; |
| 7473 | |
| 7474 | # ifdef VMS_TEMPNAM |
| 7475 | /* mktemp() is not working on VMS. It seems to be |
| 7476 | * a do-nothing function. Therefore we use tempnam(). |
| 7477 | */ |
| 7478 | sprintf((char *)itmp, "VIM%c", extra_char); |
| 7479 | p = (char_u *)tempnam("tmp:", (char *)itmp); |
| 7480 | if (p != NULL) |
| 7481 | { |
| 7482 | /* VMS will use '.LOG' if we don't explicitly specify an extension, |
| 7483 | * and VIM will then be unable to find the file later */ |
| 7484 | STRCPY(itmp, p); |
| 7485 | STRCAT(itmp, ".txt"); |
| 7486 | free(p); |
| 7487 | } |
| 7488 | else |
| 7489 | return NULL; |
| 7490 | # else |
| 7491 | STRCPY(itmp, TEMPNAME); |
| 7492 | if ((p = vim_strchr(itmp, '?')) != NULL) |
| 7493 | *p = extra_char; |
| 7494 | if (mktemp((char *)itmp) == NULL) |
| 7495 | return NULL; |
| 7496 | # endif |
| 7497 | # endif |
| 7498 | |
| 7499 | return vim_strsave(itmp); |
| 7500 | # endif /* WIN3264 */ |
| 7501 | #endif /* TEMPDIRNAMES */ |
| 7502 | } |
| 7503 | |
| 7504 | #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) |
| 7505 | /* |
| 7506 | * Convert all backslashes in fname to forward slashes in-place. |
| 7507 | */ |
| 7508 | void |
| 7509 | forward_slash(fname) |
| 7510 | char_u *fname; |
| 7511 | { |
| 7512 | char_u *p; |
| 7513 | |
| 7514 | for (p = fname; *p != NUL; ++p) |
| 7515 | # ifdef FEAT_MBYTE |
| 7516 | /* The Big5 encoding can have '\' in the trail byte. */ |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7517 | if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7518 | ++p; |
| 7519 | else |
| 7520 | # endif |
| 7521 | if (*p == '\\') |
| 7522 | *p = '/'; |
| 7523 | } |
| 7524 | #endif |
| 7525 | |
| 7526 | |
| 7527 | /* |
| 7528 | * Code for automatic commands. |
| 7529 | * |
| 7530 | * Only included when "FEAT_AUTOCMD" has been defined. |
| 7531 | */ |
| 7532 | |
| 7533 | #if defined(FEAT_AUTOCMD) || defined(PROTO) |
| 7534 | |
| 7535 | /* |
| 7536 | * The autocommands are stored in a list for each event. |
| 7537 | * Autocommands for the same pattern, that are consecutive, are joined |
| 7538 | * together, to avoid having to match the pattern too often. |
| 7539 | * The result is an array of Autopat lists, which point to AutoCmd lists: |
| 7540 | * |
| 7541 | * first_autopat[0] --> Autopat.next --> Autopat.next --> NULL |
| 7542 | * Autopat.cmds Autopat.cmds |
| 7543 | * | | |
| 7544 | * V V |
| 7545 | * AutoCmd.next AutoCmd.next |
| 7546 | * | | |
| 7547 | * V V |
| 7548 | * AutoCmd.next NULL |
| 7549 | * | |
| 7550 | * V |
| 7551 | * NULL |
| 7552 | * |
| 7553 | * first_autopat[1] --> Autopat.next --> NULL |
| 7554 | * Autopat.cmds |
| 7555 | * | |
| 7556 | * V |
| 7557 | * AutoCmd.next |
| 7558 | * | |
| 7559 | * V |
| 7560 | * NULL |
| 7561 | * etc. |
| 7562 | * |
| 7563 | * The order of AutoCmds is important, this is the order in which they were |
| 7564 | * defined and will have to be executed. |
| 7565 | */ |
| 7566 | typedef struct AutoCmd |
| 7567 | { |
| 7568 | char_u *cmd; /* The command to be executed (NULL |
| 7569 | when command has been removed) */ |
| 7570 | char nested; /* If autocommands nest here */ |
| 7571 | char last; /* last command in list */ |
| 7572 | #ifdef FEAT_EVAL |
| 7573 | scid_T scriptID; /* script ID where defined */ |
| 7574 | #endif |
| 7575 | struct AutoCmd *next; /* Next AutoCmd in list */ |
| 7576 | } AutoCmd; |
| 7577 | |
| 7578 | typedef struct AutoPat |
| 7579 | { |
| 7580 | int group; /* group ID */ |
| 7581 | char_u *pat; /* pattern as typed (NULL when pattern |
| 7582 | has been removed) */ |
| 7583 | int patlen; /* strlen() of pat */ |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 7584 | regprog_T *reg_prog; /* compiled regprog for pattern */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7585 | char allow_dirs; /* Pattern may match whole path */ |
| 7586 | char last; /* last pattern for apply_autocmds() */ |
| 7587 | AutoCmd *cmds; /* list of commands to do */ |
| 7588 | struct AutoPat *next; /* next AutoPat in AutoPat list */ |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7589 | int buflocal_nr; /* !=0 for buffer-local AutoPat */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7590 | } AutoPat; |
| 7591 | |
| 7592 | static struct event_name |
| 7593 | { |
| 7594 | char *name; /* event name */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7595 | event_T event; /* event number */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7596 | } event_names[] = |
| 7597 | { |
| 7598 | {"BufAdd", EVENT_BUFADD}, |
| 7599 | {"BufCreate", EVENT_BUFADD}, |
| 7600 | {"BufDelete", EVENT_BUFDELETE}, |
| 7601 | {"BufEnter", EVENT_BUFENTER}, |
| 7602 | {"BufFilePost", EVENT_BUFFILEPOST}, |
| 7603 | {"BufFilePre", EVENT_BUFFILEPRE}, |
| 7604 | {"BufHidden", EVENT_BUFHIDDEN}, |
| 7605 | {"BufLeave", EVENT_BUFLEAVE}, |
| 7606 | {"BufNew", EVENT_BUFNEW}, |
| 7607 | {"BufNewFile", EVENT_BUFNEWFILE}, |
| 7608 | {"BufRead", EVENT_BUFREADPOST}, |
| 7609 | {"BufReadCmd", EVENT_BUFREADCMD}, |
| 7610 | {"BufReadPost", EVENT_BUFREADPOST}, |
| 7611 | {"BufReadPre", EVENT_BUFREADPRE}, |
| 7612 | {"BufUnload", EVENT_BUFUNLOAD}, |
| 7613 | {"BufWinEnter", EVENT_BUFWINENTER}, |
| 7614 | {"BufWinLeave", EVENT_BUFWINLEAVE}, |
| 7615 | {"BufWipeout", EVENT_BUFWIPEOUT}, |
| 7616 | {"BufWrite", EVENT_BUFWRITEPRE}, |
| 7617 | {"BufWritePost", EVENT_BUFWRITEPOST}, |
| 7618 | {"BufWritePre", EVENT_BUFWRITEPRE}, |
| 7619 | {"BufWriteCmd", EVENT_BUFWRITECMD}, |
| 7620 | {"CmdwinEnter", EVENT_CMDWINENTER}, |
| 7621 | {"CmdwinLeave", EVENT_CMDWINLEAVE}, |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 7622 | {"ColorScheme", EVENT_COLORSCHEME}, |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7623 | {"CursorHold", EVENT_CURSORHOLD}, |
| 7624 | {"CursorHoldI", EVENT_CURSORHOLDI}, |
| 7625 | {"CursorMoved", EVENT_CURSORMOVED}, |
| 7626 | {"CursorMovedI", EVENT_CURSORMOVEDI}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7627 | {"EncodingChanged", EVENT_ENCODINGCHANGED}, |
| 7628 | {"FileEncoding", EVENT_ENCODINGCHANGED}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7629 | {"FileAppendPost", EVENT_FILEAPPENDPOST}, |
| 7630 | {"FileAppendPre", EVENT_FILEAPPENDPRE}, |
| 7631 | {"FileAppendCmd", EVENT_FILEAPPENDCMD}, |
| 7632 | {"FileChangedShell",EVENT_FILECHANGEDSHELL}, |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 7633 | {"FileChangedShellPost",EVENT_FILECHANGEDSHELLPOST}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7634 | {"FileChangedRO", EVENT_FILECHANGEDRO}, |
| 7635 | {"FileReadPost", EVENT_FILEREADPOST}, |
| 7636 | {"FileReadPre", EVENT_FILEREADPRE}, |
| 7637 | {"FileReadCmd", EVENT_FILEREADCMD}, |
| 7638 | {"FileType", EVENT_FILETYPE}, |
| 7639 | {"FileWritePost", EVENT_FILEWRITEPOST}, |
| 7640 | {"FileWritePre", EVENT_FILEWRITEPRE}, |
| 7641 | {"FileWriteCmd", EVENT_FILEWRITECMD}, |
| 7642 | {"FilterReadPost", EVENT_FILTERREADPOST}, |
| 7643 | {"FilterReadPre", EVENT_FILTERREADPRE}, |
| 7644 | {"FilterWritePost", EVENT_FILTERWRITEPOST}, |
| 7645 | {"FilterWritePre", EVENT_FILTERWRITEPRE}, |
| 7646 | {"FocusGained", EVENT_FOCUSGAINED}, |
| 7647 | {"FocusLost", EVENT_FOCUSLOST}, |
| 7648 | {"FuncUndefined", EVENT_FUNCUNDEFINED}, |
| 7649 | {"GUIEnter", EVENT_GUIENTER}, |
Bram Moolenaar | 265e507 | 2006-08-29 16:13:22 +0000 | [diff] [blame] | 7650 | {"GUIFailed", EVENT_GUIFAILED}, |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 7651 | {"InsertChange", EVENT_INSERTCHANGE}, |
| 7652 | {"InsertEnter", EVENT_INSERTENTER}, |
| 7653 | {"InsertLeave", EVENT_INSERTLEAVE}, |
Bram Moolenaar | a3ffd9c | 2005-07-21 21:03:15 +0000 | [diff] [blame] | 7654 | {"MenuPopup", EVENT_MENUPOPUP}, |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7655 | {"QuickFixCmdPost", EVENT_QUICKFIXCMDPOST}, |
| 7656 | {"QuickFixCmdPre", EVENT_QUICKFIXCMDPRE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7657 | {"RemoteReply", EVENT_REMOTEREPLY}, |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 7658 | {"SessionLoadPost", EVENT_SESSIONLOADPOST}, |
Bram Moolenaar | 5c4bab0 | 2006-03-10 21:37:46 +0000 | [diff] [blame] | 7659 | {"ShellCmdPost", EVENT_SHELLCMDPOST}, |
| 7660 | {"ShellFilterPost", EVENT_SHELLFILTERPOST}, |
Bram Moolenaar | a203182 | 2006-03-07 22:29:51 +0000 | [diff] [blame] | 7661 | {"SourcePre", EVENT_SOURCEPRE}, |
Bram Moolenaar | 8dd1aa5 | 2007-01-16 20:33:19 +0000 | [diff] [blame] | 7662 | {"SourceCmd", EVENT_SOURCECMD}, |
Bram Moolenaar | b8a7b56 | 2006-02-01 21:47:16 +0000 | [diff] [blame] | 7663 | {"SpellFileMissing",EVENT_SPELLFILEMISSING}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7664 | {"StdinReadPost", EVENT_STDINREADPOST}, |
| 7665 | {"StdinReadPre", EVENT_STDINREADPRE}, |
Bram Moolenaar | b815dac | 2005-12-07 20:59:24 +0000 | [diff] [blame] | 7666 | {"SwapExists", EVENT_SWAPEXISTS}, |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 7667 | {"Syntax", EVENT_SYNTAX}, |
Bram Moolenaar | 70836c8 | 2006-02-20 21:28:49 +0000 | [diff] [blame] | 7668 | {"TabEnter", EVENT_TABENTER}, |
| 7669 | {"TabLeave", EVENT_TABLEAVE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7670 | {"TermChanged", EVENT_TERMCHANGED}, |
| 7671 | {"TermResponse", EVENT_TERMRESPONSE}, |
| 7672 | {"User", EVENT_USER}, |
| 7673 | {"VimEnter", EVENT_VIMENTER}, |
| 7674 | {"VimLeave", EVENT_VIMLEAVE}, |
| 7675 | {"VimLeavePre", EVENT_VIMLEAVEPRE}, |
| 7676 | {"WinEnter", EVENT_WINENTER}, |
| 7677 | {"WinLeave", EVENT_WINLEAVE}, |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 7678 | {"VimResized", EVENT_VIMRESIZED}, |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7679 | {NULL, (event_T)0} |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7680 | }; |
| 7681 | |
| 7682 | static AutoPat *first_autopat[NUM_EVENTS] = |
| 7683 | { |
| 7684 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 7685 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 7686 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 7687 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
Bram Moolenaar | ab79bcb | 2004-07-18 21:34:53 +0000 | [diff] [blame] | 7688 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 7689 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7690 | }; |
| 7691 | |
| 7692 | /* |
| 7693 | * struct used to keep status while executing autocommands for an event. |
| 7694 | */ |
| 7695 | typedef struct AutoPatCmd |
| 7696 | { |
| 7697 | AutoPat *curpat; /* next AutoPat to examine */ |
| 7698 | AutoCmd *nextcmd; /* next AutoCmd to execute */ |
| 7699 | int group; /* group being used */ |
| 7700 | char_u *fname; /* fname to match with */ |
| 7701 | char_u *sfname; /* sfname to match with */ |
| 7702 | char_u *tail; /* tail of fname */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7703 | event_T event; /* current event */ |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7704 | int arg_bufnr; /* initially equal to <abuf>, set to zero when |
| 7705 | buf is deleted */ |
| 7706 | struct AutoPatCmd *next; /* chain of active apc-s for auto-invalidation*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7707 | } AutoPatCmd; |
| 7708 | |
Bram Moolenaar | d6f676d | 2005-06-01 21:51:55 +0000 | [diff] [blame] | 7709 | static AutoPatCmd *active_apc_list = NULL; /* stack of active autocommands */ |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7710 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7711 | /* |
| 7712 | * augroups stores a list of autocmd group names. |
| 7713 | */ |
Bram Moolenaar | d6f676d | 2005-06-01 21:51:55 +0000 | [diff] [blame] | 7714 | static garray_T augroups = {0, 0, sizeof(char_u *), 10, NULL}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7715 | #define AUGROUP_NAME(i) (((char_u **)augroups.ga_data)[i]) |
| 7716 | |
| 7717 | /* |
| 7718 | * The ID of the current group. Group 0 is the default one. |
| 7719 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7720 | static int current_augroup = AUGROUP_DEFAULT; |
| 7721 | |
| 7722 | static int au_need_clean = FALSE; /* need to delete marked patterns */ |
| 7723 | |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7724 | static void show_autocmd __ARGS((AutoPat *ap, event_T event)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7725 | static void au_remove_pat __ARGS((AutoPat *ap)); |
| 7726 | static void au_remove_cmds __ARGS((AutoPat *ap)); |
| 7727 | static void au_cleanup __ARGS((void)); |
| 7728 | static int au_new_group __ARGS((char_u *name)); |
| 7729 | static void au_del_group __ARGS((char_u *name)); |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7730 | static event_T event_name2nr __ARGS((char_u *start, char_u **end)); |
| 7731 | static char_u *event_nr2name __ARGS((event_T event)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7732 | 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] | 7733 | static int event_ignored __ARGS((event_T event)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7734 | static int au_get_grouparg __ARGS((char_u **argp)); |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7735 | static int do_autocmd_event __ARGS((event_T event, char_u *pat, int nested, char_u *cmd, int forceit, int group)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7736 | static char_u *getnextac __ARGS((int c, void *cookie, int indent)); |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7737 | 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] | 7738 | static void auto_next_pat __ARGS((AutoPatCmd *apc, int stop_at_last)); |
| 7739 | |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7740 | |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7741 | static event_T last_event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7742 | static int last_group; |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7743 | static int autocmd_blocked = 0; /* block all autocmds */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7744 | |
| 7745 | /* |
| 7746 | * Show the autocommands for one AutoPat. |
| 7747 | */ |
| 7748 | static void |
| 7749 | show_autocmd(ap, event) |
| 7750 | AutoPat *ap; |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7751 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7752 | { |
| 7753 | AutoCmd *ac; |
| 7754 | |
| 7755 | /* Check for "got_int" (here and at various places below), which is set |
| 7756 | * when "q" has been hit for the "--more--" prompt */ |
| 7757 | if (got_int) |
| 7758 | return; |
| 7759 | if (ap->pat == NULL) /* pattern has been removed */ |
| 7760 | return; |
| 7761 | |
| 7762 | msg_putchar('\n'); |
| 7763 | if (got_int) |
| 7764 | return; |
| 7765 | if (event != last_event || ap->group != last_group) |
| 7766 | { |
| 7767 | if (ap->group != AUGROUP_DEFAULT) |
| 7768 | { |
| 7769 | if (AUGROUP_NAME(ap->group) == NULL) |
| 7770 | msg_puts_attr((char_u *)_("--Deleted--"), hl_attr(HLF_E)); |
| 7771 | else |
| 7772 | msg_puts_attr(AUGROUP_NAME(ap->group), hl_attr(HLF_T)); |
| 7773 | msg_puts((char_u *)" "); |
| 7774 | } |
| 7775 | msg_puts_attr(event_nr2name(event), hl_attr(HLF_T)); |
| 7776 | last_event = event; |
| 7777 | last_group = ap->group; |
| 7778 | msg_putchar('\n'); |
| 7779 | if (got_int) |
| 7780 | return; |
| 7781 | } |
| 7782 | msg_col = 4; |
| 7783 | msg_outtrans(ap->pat); |
| 7784 | |
| 7785 | for (ac = ap->cmds; ac != NULL; ac = ac->next) |
| 7786 | { |
| 7787 | if (ac->cmd != NULL) /* skip removed commands */ |
| 7788 | { |
| 7789 | if (msg_col >= 14) |
| 7790 | msg_putchar('\n'); |
| 7791 | msg_col = 14; |
| 7792 | if (got_int) |
| 7793 | return; |
| 7794 | msg_outtrans(ac->cmd); |
Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 7795 | #ifdef FEAT_EVAL |
| 7796 | if (p_verbose > 0) |
| 7797 | last_set_msg(ac->scriptID); |
| 7798 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7799 | if (got_int) |
| 7800 | return; |
| 7801 | if (ac->next != NULL) |
| 7802 | { |
| 7803 | msg_putchar('\n'); |
| 7804 | if (got_int) |
| 7805 | return; |
| 7806 | } |
| 7807 | } |
| 7808 | } |
| 7809 | } |
| 7810 | |
| 7811 | /* |
| 7812 | * Mark an autocommand pattern for deletion. |
| 7813 | */ |
| 7814 | static void |
| 7815 | au_remove_pat(ap) |
| 7816 | AutoPat *ap; |
| 7817 | { |
| 7818 | vim_free(ap->pat); |
| 7819 | ap->pat = NULL; |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7820 | ap->buflocal_nr = -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7821 | au_need_clean = TRUE; |
| 7822 | } |
| 7823 | |
| 7824 | /* |
| 7825 | * Mark all commands for a pattern for deletion. |
| 7826 | */ |
| 7827 | static void |
| 7828 | au_remove_cmds(ap) |
| 7829 | AutoPat *ap; |
| 7830 | { |
| 7831 | AutoCmd *ac; |
| 7832 | |
| 7833 | for (ac = ap->cmds; ac != NULL; ac = ac->next) |
| 7834 | { |
| 7835 | vim_free(ac->cmd); |
| 7836 | ac->cmd = NULL; |
| 7837 | } |
| 7838 | au_need_clean = TRUE; |
| 7839 | } |
| 7840 | |
| 7841 | /* |
| 7842 | * Cleanup autocommands and patterns that have been deleted. |
| 7843 | * This is only done when not executing autocommands. |
| 7844 | */ |
| 7845 | static void |
| 7846 | au_cleanup() |
| 7847 | { |
| 7848 | AutoPat *ap, **prev_ap; |
| 7849 | AutoCmd *ac, **prev_ac; |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7850 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7851 | |
| 7852 | if (autocmd_busy || !au_need_clean) |
| 7853 | return; |
| 7854 | |
| 7855 | /* loop over all events */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7856 | for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
| 7857 | event = (event_T)((int)event + 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7858 | { |
| 7859 | /* loop over all autocommand patterns */ |
| 7860 | prev_ap = &(first_autopat[(int)event]); |
| 7861 | for (ap = *prev_ap; ap != NULL; ap = *prev_ap) |
| 7862 | { |
| 7863 | /* loop over all commands for this pattern */ |
| 7864 | prev_ac = &(ap->cmds); |
| 7865 | for (ac = *prev_ac; ac != NULL; ac = *prev_ac) |
| 7866 | { |
| 7867 | /* remove the command if the pattern is to be deleted or when |
| 7868 | * the command has been marked for deletion */ |
| 7869 | if (ap->pat == NULL || ac->cmd == NULL) |
| 7870 | { |
| 7871 | *prev_ac = ac->next; |
| 7872 | vim_free(ac->cmd); |
| 7873 | vim_free(ac); |
| 7874 | } |
| 7875 | else |
| 7876 | prev_ac = &(ac->next); |
| 7877 | } |
| 7878 | |
| 7879 | /* remove the pattern if it has been marked for deletion */ |
| 7880 | if (ap->pat == NULL) |
| 7881 | { |
| 7882 | *prev_ap = ap->next; |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 7883 | vim_free(ap->reg_prog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7884 | vim_free(ap); |
| 7885 | } |
| 7886 | else |
| 7887 | prev_ap = &(ap->next); |
| 7888 | } |
| 7889 | } |
| 7890 | |
| 7891 | au_need_clean = FALSE; |
| 7892 | } |
| 7893 | |
| 7894 | /* |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7895 | * Called when buffer is freed, to remove/invalidate related buffer-local |
| 7896 | * autocmds. |
| 7897 | */ |
| 7898 | void |
| 7899 | aubuflocal_remove(buf) |
| 7900 | buf_T *buf; |
| 7901 | { |
| 7902 | AutoPat *ap; |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7903 | event_T event; |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7904 | AutoPatCmd *apc; |
| 7905 | |
| 7906 | /* invalidate currently executing autocommands */ |
| 7907 | for (apc = active_apc_list; apc; apc = apc->next) |
| 7908 | if (buf->b_fnum == apc->arg_bufnr) |
| 7909 | apc->arg_bufnr = 0; |
| 7910 | |
| 7911 | /* invalidate buflocals looping through events */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7912 | for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
| 7913 | event = (event_T)((int)event + 1)) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7914 | /* loop over all autocommand patterns */ |
| 7915 | for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) |
| 7916 | if (ap->buflocal_nr == buf->b_fnum) |
| 7917 | { |
| 7918 | au_remove_pat(ap); |
| 7919 | if (p_verbose >= 6) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 7920 | { |
| 7921 | verbose_enter(); |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7922 | smsg((char_u *) |
| 7923 | _("auto-removing autocommand: %s <buffer=%d>"), |
| 7924 | event_nr2name(event), buf->b_fnum); |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 7925 | verbose_leave(); |
| 7926 | } |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 7927 | } |
| 7928 | au_cleanup(); |
| 7929 | } |
| 7930 | |
| 7931 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7932 | * Add an autocmd group name. |
| 7933 | * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. |
| 7934 | */ |
| 7935 | static int |
| 7936 | au_new_group(name) |
| 7937 | char_u *name; |
| 7938 | { |
| 7939 | int i; |
| 7940 | |
| 7941 | i = au_find_group(name); |
| 7942 | if (i == AUGROUP_ERROR) /* the group doesn't exist yet, add it */ |
| 7943 | { |
| 7944 | /* First try using a free entry. */ |
| 7945 | for (i = 0; i < augroups.ga_len; ++i) |
| 7946 | if (AUGROUP_NAME(i) == NULL) |
| 7947 | break; |
| 7948 | if (i == augroups.ga_len && ga_grow(&augroups, 1) == FAIL) |
| 7949 | return AUGROUP_ERROR; |
| 7950 | |
| 7951 | AUGROUP_NAME(i) = vim_strsave(name); |
| 7952 | if (AUGROUP_NAME(i) == NULL) |
| 7953 | return AUGROUP_ERROR; |
| 7954 | if (i == augroups.ga_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7955 | ++augroups.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7956 | } |
| 7957 | |
| 7958 | return i; |
| 7959 | } |
| 7960 | |
| 7961 | static void |
| 7962 | au_del_group(name) |
| 7963 | char_u *name; |
| 7964 | { |
| 7965 | int i; |
| 7966 | |
| 7967 | i = au_find_group(name); |
| 7968 | if (i == AUGROUP_ERROR) /* the group doesn't exist */ |
| 7969 | EMSG2(_("E367: No such group: \"%s\""), name); |
| 7970 | else |
| 7971 | { |
| 7972 | vim_free(AUGROUP_NAME(i)); |
| 7973 | AUGROUP_NAME(i) = NULL; |
| 7974 | } |
| 7975 | } |
| 7976 | |
| 7977 | /* |
| 7978 | * Find the ID of an autocmd group name. |
| 7979 | * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. |
| 7980 | */ |
| 7981 | static int |
| 7982 | au_find_group(name) |
| 7983 | char_u *name; |
| 7984 | { |
| 7985 | int i; |
| 7986 | |
| 7987 | for (i = 0; i < augroups.ga_len; ++i) |
| 7988 | if (AUGROUP_NAME(i) != NULL && STRCMP(AUGROUP_NAME(i), name) == 0) |
| 7989 | return i; |
| 7990 | return AUGROUP_ERROR; |
| 7991 | } |
| 7992 | |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 7993 | /* |
| 7994 | * Return TRUE if augroup "name" exists. |
| 7995 | */ |
| 7996 | int |
| 7997 | au_has_group(name) |
| 7998 | char_u *name; |
| 7999 | { |
| 8000 | return au_find_group(name) != AUGROUP_ERROR; |
| 8001 | } |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 8002 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8003 | /* |
| 8004 | * ":augroup {name}". |
| 8005 | */ |
| 8006 | void |
| 8007 | do_augroup(arg, del_group) |
| 8008 | char_u *arg; |
| 8009 | int del_group; |
| 8010 | { |
| 8011 | int i; |
| 8012 | |
| 8013 | if (del_group) |
| 8014 | { |
| 8015 | if (*arg == NUL) |
| 8016 | EMSG(_(e_argreq)); |
| 8017 | else |
| 8018 | au_del_group(arg); |
| 8019 | } |
| 8020 | else if (STRICMP(arg, "end") == 0) /* ":aug end": back to group 0 */ |
| 8021 | current_augroup = AUGROUP_DEFAULT; |
| 8022 | else if (*arg) /* ":aug xxx": switch to group xxx */ |
| 8023 | { |
| 8024 | i = au_new_group(arg); |
| 8025 | if (i != AUGROUP_ERROR) |
| 8026 | current_augroup = i; |
| 8027 | } |
| 8028 | else /* ":aug": list the group names */ |
| 8029 | { |
| 8030 | msg_start(); |
| 8031 | for (i = 0; i < augroups.ga_len; ++i) |
| 8032 | { |
| 8033 | if (AUGROUP_NAME(i) != NULL) |
| 8034 | { |
| 8035 | msg_puts(AUGROUP_NAME(i)); |
| 8036 | msg_puts((char_u *)" "); |
| 8037 | } |
| 8038 | } |
| 8039 | msg_clr_eos(); |
| 8040 | msg_end(); |
| 8041 | } |
| 8042 | } |
| 8043 | |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 8044 | #if defined(EXITFREE) || defined(PROTO) |
| 8045 | void |
| 8046 | free_all_autocmds() |
| 8047 | { |
| 8048 | for (current_augroup = -1; current_augroup < augroups.ga_len; |
| 8049 | ++current_augroup) |
| 8050 | do_autocmd((char_u *)"", TRUE); |
| 8051 | ga_clear_strings(&augroups); |
| 8052 | } |
| 8053 | #endif |
| 8054 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8055 | /* |
| 8056 | * Return the event number for event name "start". |
| 8057 | * Return NUM_EVENTS if the event name was not found. |
| 8058 | * Return a pointer to the next event name in "end". |
| 8059 | */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8060 | static event_T |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8061 | event_name2nr(start, end) |
| 8062 | char_u *start; |
| 8063 | char_u **end; |
| 8064 | { |
| 8065 | char_u *p; |
| 8066 | int i; |
| 8067 | int len; |
| 8068 | |
| 8069 | /* the event name ends with end of line, a blank or a comma */ |
| 8070 | for (p = start; *p && !vim_iswhite(*p) && *p != ','; ++p) |
| 8071 | ; |
| 8072 | for (i = 0; event_names[i].name != NULL; ++i) |
| 8073 | { |
| 8074 | len = (int)STRLEN(event_names[i].name); |
| 8075 | if (len == p - start && STRNICMP(event_names[i].name, start, len) == 0) |
| 8076 | break; |
| 8077 | } |
| 8078 | if (*p == ',') |
| 8079 | ++p; |
| 8080 | *end = p; |
| 8081 | if (event_names[i].name == NULL) |
| 8082 | return NUM_EVENTS; |
| 8083 | return event_names[i].event; |
| 8084 | } |
| 8085 | |
| 8086 | /* |
| 8087 | * Return the name for event "event". |
| 8088 | */ |
| 8089 | static char_u * |
| 8090 | event_nr2name(event) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8091 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8092 | { |
| 8093 | int i; |
| 8094 | |
| 8095 | for (i = 0; event_names[i].name != NULL; ++i) |
| 8096 | if (event_names[i].event == event) |
| 8097 | return (char_u *)event_names[i].name; |
| 8098 | return (char_u *)"Unknown"; |
| 8099 | } |
| 8100 | |
| 8101 | /* |
| 8102 | * Scan over the events. "*" stands for all events. |
| 8103 | */ |
| 8104 | static char_u * |
| 8105 | find_end_event(arg, have_group) |
| 8106 | char_u *arg; |
| 8107 | int have_group; /* TRUE when group name was found */ |
| 8108 | { |
| 8109 | char_u *pat; |
| 8110 | char_u *p; |
| 8111 | |
| 8112 | if (*arg == '*') |
| 8113 | { |
| 8114 | if (arg[1] && !vim_iswhite(arg[1])) |
| 8115 | { |
| 8116 | EMSG2(_("E215: Illegal character after *: %s"), arg); |
| 8117 | return NULL; |
| 8118 | } |
| 8119 | pat = arg + 1; |
| 8120 | } |
| 8121 | else |
| 8122 | { |
| 8123 | for (pat = arg; *pat && !vim_iswhite(*pat); pat = p) |
| 8124 | { |
| 8125 | if ((int)event_name2nr(pat, &p) >= (int)NUM_EVENTS) |
| 8126 | { |
| 8127 | if (have_group) |
| 8128 | EMSG2(_("E216: No such event: %s"), pat); |
| 8129 | else |
| 8130 | EMSG2(_("E216: No such group or event: %s"), pat); |
| 8131 | return NULL; |
| 8132 | } |
| 8133 | } |
| 8134 | } |
| 8135 | return pat; |
| 8136 | } |
| 8137 | |
| 8138 | /* |
| 8139 | * Return TRUE if "event" is included in 'eventignore'. |
| 8140 | */ |
| 8141 | static int |
| 8142 | event_ignored(event) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8143 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8144 | { |
| 8145 | char_u *p = p_ei; |
| 8146 | |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 8147 | while (*p != NUL) |
| 8148 | { |
| 8149 | if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) |
| 8150 | return TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8151 | if (event_name2nr(p, &p) == event) |
| 8152 | return TRUE; |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 8153 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8154 | |
| 8155 | return FALSE; |
| 8156 | } |
| 8157 | |
| 8158 | /* |
| 8159 | * Return OK when the contents of p_ei is valid, FAIL otherwise. |
| 8160 | */ |
| 8161 | int |
| 8162 | check_ei() |
| 8163 | { |
| 8164 | char_u *p = p_ei; |
| 8165 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8166 | while (*p) |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 8167 | { |
| 8168 | if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) |
| 8169 | { |
| 8170 | p += 3; |
| 8171 | if (*p == ',') |
| 8172 | ++p; |
| 8173 | } |
| 8174 | else if (event_name2nr(p, &p) == NUM_EVENTS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8175 | return FAIL; |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 8176 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8177 | |
| 8178 | return OK; |
| 8179 | } |
| 8180 | |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 8181 | # if defined(FEAT_SYN_HL) || defined(PROTO) |
| 8182 | |
| 8183 | /* |
| 8184 | * Add "what" to 'eventignore' to skip loading syntax highlighting for every |
| 8185 | * buffer loaded into the window. "what" must start with a comma. |
| 8186 | * Returns the old value of 'eventignore' in allocated memory. |
| 8187 | */ |
| 8188 | char_u * |
| 8189 | au_event_disable(what) |
| 8190 | char *what; |
| 8191 | { |
| 8192 | char_u *new_ei; |
| 8193 | char_u *save_ei; |
| 8194 | |
| 8195 | save_ei = vim_strsave(p_ei); |
| 8196 | if (save_ei != NULL) |
| 8197 | { |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 8198 | new_ei = vim_strnsave(p_ei, (int)(STRLEN(p_ei) + STRLEN(what))); |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 8199 | if (new_ei != NULL) |
| 8200 | { |
Bram Moolenaar | 8cac9fd | 2010-03-02 12:48:05 +0100 | [diff] [blame] | 8201 | if (*what == ',' && *p_ei == NUL) |
| 8202 | STRCPY(new_ei, what + 1); |
| 8203 | else |
| 8204 | STRCAT(new_ei, what); |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 8205 | set_string_option_direct((char_u *)"ei", -1, new_ei, |
| 8206 | OPT_FREE, SID_NONE); |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 8207 | vim_free(new_ei); |
| 8208 | } |
| 8209 | } |
| 8210 | return save_ei; |
| 8211 | } |
| 8212 | |
| 8213 | void |
| 8214 | au_event_restore(old_ei) |
| 8215 | char_u *old_ei; |
| 8216 | { |
| 8217 | if (old_ei != NULL) |
| 8218 | { |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 8219 | set_string_option_direct((char_u *)"ei", -1, old_ei, |
| 8220 | OPT_FREE, SID_NONE); |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 8221 | vim_free(old_ei); |
| 8222 | } |
| 8223 | } |
| 8224 | # endif /* FEAT_SYN_HL */ |
| 8225 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8226 | /* |
| 8227 | * do_autocmd() -- implements the :autocmd command. Can be used in the |
| 8228 | * following ways: |
| 8229 | * |
| 8230 | * :autocmd <event> <pat> <cmd> Add <cmd> to the list of commands that |
| 8231 | * will be automatically executed for <event> |
| 8232 | * when editing a file matching <pat>, in |
| 8233 | * the current group. |
| 8234 | * :autocmd <event> <pat> Show the auto-commands associated with |
| 8235 | * <event> and <pat>. |
| 8236 | * :autocmd <event> Show the auto-commands associated with |
| 8237 | * <event>. |
| 8238 | * :autocmd Show all auto-commands. |
| 8239 | * :autocmd! <event> <pat> <cmd> Remove all auto-commands associated with |
| 8240 | * <event> and <pat>, and add the command |
| 8241 | * <cmd>, for the current group. |
| 8242 | * :autocmd! <event> <pat> Remove all auto-commands associated with |
| 8243 | * <event> and <pat> for the current group. |
| 8244 | * :autocmd! <event> Remove all auto-commands associated with |
| 8245 | * <event> for the current group. |
| 8246 | * :autocmd! Remove ALL auto-commands for the current |
| 8247 | * group. |
| 8248 | * |
| 8249 | * Multiple events and patterns may be given separated by commas. Here are |
| 8250 | * some examples: |
| 8251 | * :autocmd bufread,bufenter *.c,*.h set tw=0 smartindent noic |
| 8252 | * :autocmd bufleave * set tw=79 nosmartindent ic infercase |
| 8253 | * |
| 8254 | * :autocmd * *.c show all autocommands for *.c files. |
Bram Moolenaar | d35f971 | 2005-12-18 22:02:33 +0000 | [diff] [blame] | 8255 | * |
| 8256 | * Mostly a {group} argument can optionally appear before <event>. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8257 | */ |
| 8258 | void |
| 8259 | do_autocmd(arg, forceit) |
| 8260 | char_u *arg; |
| 8261 | int forceit; |
| 8262 | { |
| 8263 | char_u *pat; |
| 8264 | char_u *envpat = NULL; |
| 8265 | char_u *cmd; |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8266 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8267 | int need_free = FALSE; |
| 8268 | int nested = FALSE; |
| 8269 | int group; |
| 8270 | |
| 8271 | /* |
| 8272 | * Check for a legal group name. If not, use AUGROUP_ALL. |
| 8273 | */ |
| 8274 | group = au_get_grouparg(&arg); |
| 8275 | if (arg == NULL) /* out of memory */ |
| 8276 | return; |
| 8277 | |
| 8278 | /* |
| 8279 | * Scan over the events. |
| 8280 | * If we find an illegal name, return here, don't do anything. |
| 8281 | */ |
| 8282 | pat = find_end_event(arg, group != AUGROUP_ALL); |
| 8283 | if (pat == NULL) |
| 8284 | return; |
| 8285 | |
| 8286 | /* |
| 8287 | * Scan over the pattern. Put a NUL at the end. |
| 8288 | */ |
| 8289 | pat = skipwhite(pat); |
| 8290 | cmd = pat; |
| 8291 | while (*cmd && (!vim_iswhite(*cmd) || cmd[-1] == '\\')) |
| 8292 | cmd++; |
| 8293 | if (*cmd) |
| 8294 | *cmd++ = NUL; |
| 8295 | |
| 8296 | /* Expand environment variables in the pattern. Set 'shellslash', we want |
| 8297 | * forward slashes here. */ |
| 8298 | if (vim_strchr(pat, '$') != NULL || vim_strchr(pat, '~') != NULL) |
| 8299 | { |
| 8300 | #ifdef BACKSLASH_IN_FILENAME |
| 8301 | int p_ssl_save = p_ssl; |
| 8302 | |
| 8303 | p_ssl = TRUE; |
| 8304 | #endif |
| 8305 | envpat = expand_env_save(pat); |
| 8306 | #ifdef BACKSLASH_IN_FILENAME |
| 8307 | p_ssl = p_ssl_save; |
| 8308 | #endif |
| 8309 | if (envpat != NULL) |
| 8310 | pat = envpat; |
| 8311 | } |
| 8312 | |
| 8313 | /* |
| 8314 | * Check for "nested" flag. |
| 8315 | */ |
| 8316 | cmd = skipwhite(cmd); |
| 8317 | if (*cmd != NUL && STRNCMP(cmd, "nested", 6) == 0 && vim_iswhite(cmd[6])) |
| 8318 | { |
| 8319 | nested = TRUE; |
| 8320 | cmd = skipwhite(cmd + 6); |
| 8321 | } |
| 8322 | |
| 8323 | /* |
| 8324 | * Find the start of the commands. |
| 8325 | * Expand <sfile> in it. |
| 8326 | */ |
| 8327 | if (*cmd != NUL) |
| 8328 | { |
| 8329 | cmd = expand_sfile(cmd); |
| 8330 | if (cmd == NULL) /* some error */ |
| 8331 | return; |
| 8332 | need_free = TRUE; |
| 8333 | } |
| 8334 | |
| 8335 | /* |
| 8336 | * Print header when showing autocommands. |
| 8337 | */ |
| 8338 | if (!forceit && *cmd == NUL) |
| 8339 | { |
| 8340 | /* Highlight title */ |
| 8341 | MSG_PUTS_TITLE(_("\n--- Auto-Commands ---")); |
| 8342 | } |
| 8343 | |
| 8344 | /* |
| 8345 | * Loop over the events. |
| 8346 | */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8347 | last_event = (event_T)-1; /* for listing the event name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8348 | last_group = AUGROUP_ERROR; /* for listing the group name */ |
| 8349 | if (*arg == '*' || *arg == NUL) |
| 8350 | { |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8351 | for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
| 8352 | event = (event_T)((int)event + 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8353 | if (do_autocmd_event(event, pat, |
| 8354 | nested, cmd, forceit, group) == FAIL) |
| 8355 | break; |
| 8356 | } |
| 8357 | else |
| 8358 | { |
| 8359 | while (*arg && !vim_iswhite(*arg)) |
| 8360 | if (do_autocmd_event(event_name2nr(arg, &arg), pat, |
| 8361 | nested, cmd, forceit, group) == FAIL) |
| 8362 | break; |
| 8363 | } |
| 8364 | |
| 8365 | if (need_free) |
| 8366 | vim_free(cmd); |
| 8367 | vim_free(envpat); |
| 8368 | } |
| 8369 | |
| 8370 | /* |
| 8371 | * Find the group ID in a ":autocmd" or ":doautocmd" argument. |
| 8372 | * The "argp" argument is advanced to the following argument. |
| 8373 | * |
| 8374 | * Returns the group ID, AUGROUP_ERROR for error (out of memory). |
| 8375 | */ |
| 8376 | static int |
| 8377 | au_get_grouparg(argp) |
| 8378 | char_u **argp; |
| 8379 | { |
| 8380 | char_u *group_name; |
| 8381 | char_u *p; |
| 8382 | char_u *arg = *argp; |
| 8383 | int group = AUGROUP_ALL; |
| 8384 | |
| 8385 | p = skiptowhite(arg); |
| 8386 | if (p > arg) |
| 8387 | { |
| 8388 | group_name = vim_strnsave(arg, (int)(p - arg)); |
| 8389 | if (group_name == NULL) /* out of memory */ |
| 8390 | return AUGROUP_ERROR; |
| 8391 | group = au_find_group(group_name); |
| 8392 | if (group == AUGROUP_ERROR) |
| 8393 | group = AUGROUP_ALL; /* no match, use all groups */ |
| 8394 | else |
| 8395 | *argp = skipwhite(p); /* match, skip over group name */ |
| 8396 | vim_free(group_name); |
| 8397 | } |
| 8398 | return group; |
| 8399 | } |
| 8400 | |
| 8401 | /* |
| 8402 | * do_autocmd() for one event. |
| 8403 | * If *pat == NUL do for all patterns. |
| 8404 | * If *cmd == NUL show entries. |
| 8405 | * If forceit == TRUE delete entries. |
| 8406 | * If group is not AUGROUP_ALL, only use this group. |
| 8407 | */ |
| 8408 | static int |
| 8409 | do_autocmd_event(event, pat, nested, cmd, forceit, group) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8410 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8411 | char_u *pat; |
| 8412 | int nested; |
| 8413 | char_u *cmd; |
| 8414 | int forceit; |
| 8415 | int group; |
| 8416 | { |
| 8417 | AutoPat *ap; |
| 8418 | AutoPat **prev_ap; |
| 8419 | AutoCmd *ac; |
| 8420 | AutoCmd **prev_ac; |
| 8421 | int brace_level; |
| 8422 | char_u *endpat; |
| 8423 | int findgroup; |
| 8424 | int allgroups; |
| 8425 | int patlen; |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 8426 | int is_buflocal; |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8427 | int buflocal_nr; |
| 8428 | char_u buflocal_pat[25]; /* for "<buffer=X>" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8429 | |
| 8430 | if (group == AUGROUP_ALL) |
| 8431 | findgroup = current_augroup; |
| 8432 | else |
| 8433 | findgroup = group; |
| 8434 | allgroups = (group == AUGROUP_ALL && !forceit && *cmd == NUL); |
| 8435 | |
| 8436 | /* |
| 8437 | * Show or delete all patterns for an event. |
| 8438 | */ |
| 8439 | if (*pat == NUL) |
| 8440 | { |
| 8441 | for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) |
| 8442 | { |
| 8443 | if (forceit) /* delete the AutoPat, if it's in the current group */ |
| 8444 | { |
| 8445 | if (ap->group == findgroup) |
| 8446 | au_remove_pat(ap); |
| 8447 | } |
| 8448 | else if (group == AUGROUP_ALL || ap->group == group) |
| 8449 | show_autocmd(ap, event); |
| 8450 | } |
| 8451 | } |
| 8452 | |
| 8453 | /* |
| 8454 | * Loop through all the specified patterns. |
| 8455 | */ |
| 8456 | for ( ; *pat; pat = (*endpat == ',' ? endpat + 1 : endpat)) |
| 8457 | { |
| 8458 | /* |
| 8459 | * Find end of the pattern. |
| 8460 | * Watch out for a comma in braces, like "*.\{obj,o\}". |
| 8461 | */ |
| 8462 | brace_level = 0; |
| 8463 | for (endpat = pat; *endpat && (*endpat != ',' || brace_level |
| 8464 | || endpat[-1] == '\\'); ++endpat) |
| 8465 | { |
| 8466 | if (*endpat == '{') |
| 8467 | brace_level++; |
| 8468 | else if (*endpat == '}') |
| 8469 | brace_level--; |
| 8470 | } |
| 8471 | if (pat == endpat) /* ignore single comma */ |
| 8472 | continue; |
| 8473 | patlen = (int)(endpat - pat); |
| 8474 | |
| 8475 | /* |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8476 | * detect special <buflocal[=X]> buffer-local patterns |
| 8477 | */ |
| 8478 | is_buflocal = FALSE; |
| 8479 | buflocal_nr = 0; |
| 8480 | |
| 8481 | if (patlen >= 7 && STRNCMP(pat, "<buffer", 7) == 0 |
| 8482 | && pat[patlen - 1] == '>') |
| 8483 | { |
| 8484 | /* Error will be printed only for addition. printing and removing |
| 8485 | * will proceed silently. */ |
| 8486 | is_buflocal = TRUE; |
| 8487 | if (patlen == 8) |
| 8488 | buflocal_nr = curbuf->b_fnum; |
| 8489 | else if (patlen > 9 && pat[7] == '=') |
| 8490 | { |
| 8491 | /* <buffer=abuf> */ |
| 8492 | if (patlen == 13 && STRNICMP(pat, "<buffer=abuf>", 13)) |
| 8493 | buflocal_nr = autocmd_bufnr; |
| 8494 | /* <buffer=123> */ |
| 8495 | else if (skipdigits(pat + 8) == pat + patlen - 1) |
| 8496 | buflocal_nr = atoi((char *)pat + 8); |
| 8497 | } |
| 8498 | } |
| 8499 | |
| 8500 | if (is_buflocal) |
| 8501 | { |
| 8502 | /* normalize pat into standard "<buffer>#N" form */ |
| 8503 | sprintf((char *)buflocal_pat, "<buffer=%d>", buflocal_nr); |
| 8504 | pat = buflocal_pat; /* can modify pat and patlen */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 8505 | patlen = (int)STRLEN(buflocal_pat); /* but not endpat */ |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8506 | } |
| 8507 | |
| 8508 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8509 | * Find AutoPat entries with this pattern. |
| 8510 | */ |
| 8511 | prev_ap = &first_autopat[(int)event]; |
| 8512 | while ((ap = *prev_ap) != NULL) |
| 8513 | { |
| 8514 | if (ap->pat != NULL) |
| 8515 | { |
| 8516 | /* Accept a pattern when: |
| 8517 | * - a group was specified and it's that group, or a group was |
| 8518 | * not specified and it's the current group, or a group was |
| 8519 | * not specified and we are listing |
| 8520 | * - the length of the pattern matches |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8521 | * - the pattern matches. |
| 8522 | * For <buffer[=X]>, this condition works because we normalize |
| 8523 | * all buffer-local patterns. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8524 | */ |
| 8525 | if ((allgroups || ap->group == findgroup) |
| 8526 | && ap->patlen == patlen |
| 8527 | && STRNCMP(pat, ap->pat, patlen) == 0) |
| 8528 | { |
| 8529 | /* |
| 8530 | * Remove existing autocommands. |
| 8531 | * If adding any new autocmd's for this AutoPat, don't |
| 8532 | * delete the pattern from the autopat list, append to |
| 8533 | * this list. |
| 8534 | */ |
| 8535 | if (forceit) |
| 8536 | { |
| 8537 | if (*cmd != NUL && ap->next == NULL) |
| 8538 | { |
| 8539 | au_remove_cmds(ap); |
| 8540 | break; |
| 8541 | } |
| 8542 | au_remove_pat(ap); |
| 8543 | } |
| 8544 | |
| 8545 | /* |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8546 | * Show autocmd's for this autopat, or buflocals <buffer=X> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8547 | */ |
| 8548 | else if (*cmd == NUL) |
| 8549 | show_autocmd(ap, event); |
| 8550 | |
| 8551 | /* |
| 8552 | * Add autocmd to this autopat, if it's the last one. |
| 8553 | */ |
| 8554 | else if (ap->next == NULL) |
| 8555 | break; |
| 8556 | } |
| 8557 | } |
| 8558 | prev_ap = &ap->next; |
| 8559 | } |
| 8560 | |
| 8561 | /* |
| 8562 | * Add a new command. |
| 8563 | */ |
| 8564 | if (*cmd != NUL) |
| 8565 | { |
| 8566 | /* |
| 8567 | * If the pattern we want to add a command to does appear at the |
| 8568 | * end of the list (or not is not in the list at all), add the |
| 8569 | * pattern at the end of the list. |
| 8570 | */ |
| 8571 | if (ap == NULL) |
| 8572 | { |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8573 | /* refuse to add buffer-local ap if buffer number is invalid */ |
| 8574 | if (is_buflocal && (buflocal_nr == 0 |
| 8575 | || buflist_findnr(buflocal_nr) == NULL)) |
| 8576 | { |
| 8577 | EMSGN(_("E680: <buffer=%d>: invalid buffer number "), |
| 8578 | buflocal_nr); |
| 8579 | return FAIL; |
| 8580 | } |
| 8581 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8582 | ap = (AutoPat *)alloc((unsigned)sizeof(AutoPat)); |
| 8583 | if (ap == NULL) |
| 8584 | return FAIL; |
| 8585 | ap->pat = vim_strnsave(pat, patlen); |
| 8586 | ap->patlen = patlen; |
| 8587 | if (ap->pat == NULL) |
| 8588 | { |
| 8589 | vim_free(ap); |
| 8590 | return FAIL; |
| 8591 | } |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8592 | |
| 8593 | if (is_buflocal) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8594 | { |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8595 | ap->buflocal_nr = buflocal_nr; |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 8596 | ap->reg_prog = NULL; |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8597 | } |
| 8598 | else |
| 8599 | { |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 8600 | char_u *reg_pat; |
| 8601 | |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8602 | ap->buflocal_nr = 0; |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 8603 | reg_pat = file_pat_to_reg_pat(pat, endpat, |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8604 | &ap->allow_dirs, TRUE); |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 8605 | if (reg_pat != NULL) |
| 8606 | ap->reg_prog = vim_regcomp(reg_pat, RE_MAGIC); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 8607 | vim_free(reg_pat); |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 8608 | if (reg_pat == NULL || ap->reg_prog == NULL) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 8609 | { |
| 8610 | vim_free(ap->pat); |
| 8611 | vim_free(ap); |
| 8612 | return FAIL; |
| 8613 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8614 | } |
| 8615 | ap->cmds = NULL; |
| 8616 | *prev_ap = ap; |
| 8617 | ap->next = NULL; |
| 8618 | if (group == AUGROUP_ALL) |
| 8619 | ap->group = current_augroup; |
| 8620 | else |
| 8621 | ap->group = group; |
| 8622 | } |
| 8623 | |
| 8624 | /* |
| 8625 | * Add the autocmd at the end of the AutoCmd list. |
| 8626 | */ |
| 8627 | prev_ac = &(ap->cmds); |
| 8628 | while ((ac = *prev_ac) != NULL) |
| 8629 | prev_ac = &ac->next; |
| 8630 | ac = (AutoCmd *)alloc((unsigned)sizeof(AutoCmd)); |
| 8631 | if (ac == NULL) |
| 8632 | return FAIL; |
| 8633 | ac->cmd = vim_strsave(cmd); |
| 8634 | #ifdef FEAT_EVAL |
| 8635 | ac->scriptID = current_SID; |
| 8636 | #endif |
| 8637 | if (ac->cmd == NULL) |
| 8638 | { |
| 8639 | vim_free(ac); |
| 8640 | return FAIL; |
| 8641 | } |
| 8642 | ac->next = NULL; |
| 8643 | *prev_ac = ac; |
| 8644 | ac->nested = nested; |
| 8645 | } |
| 8646 | } |
| 8647 | |
| 8648 | au_cleanup(); /* may really delete removed patterns/commands now */ |
| 8649 | return OK; |
| 8650 | } |
| 8651 | |
| 8652 | /* |
| 8653 | * Implementation of ":doautocmd [group] event [fname]". |
| 8654 | * Return OK for success, FAIL for failure; |
| 8655 | */ |
| 8656 | int |
| 8657 | do_doautocmd(arg, do_msg) |
| 8658 | char_u *arg; |
| 8659 | int do_msg; /* give message for no matching autocmds? */ |
| 8660 | { |
| 8661 | char_u *fname; |
| 8662 | int nothing_done = TRUE; |
| 8663 | int group; |
| 8664 | |
| 8665 | /* |
| 8666 | * Check for a legal group name. If not, use AUGROUP_ALL. |
| 8667 | */ |
| 8668 | group = au_get_grouparg(&arg); |
| 8669 | if (arg == NULL) /* out of memory */ |
| 8670 | return FAIL; |
| 8671 | |
| 8672 | if (*arg == '*') |
| 8673 | { |
| 8674 | EMSG(_("E217: Can't execute autocommands for ALL events")); |
| 8675 | return FAIL; |
| 8676 | } |
| 8677 | |
| 8678 | /* |
| 8679 | * Scan over the events. |
| 8680 | * If we find an illegal name, return here, don't do anything. |
| 8681 | */ |
| 8682 | fname = find_end_event(arg, group != AUGROUP_ALL); |
| 8683 | if (fname == NULL) |
| 8684 | return FAIL; |
| 8685 | |
| 8686 | fname = skipwhite(fname); |
| 8687 | |
| 8688 | /* |
| 8689 | * Loop over the events. |
| 8690 | */ |
| 8691 | while (*arg && !vim_iswhite(*arg)) |
| 8692 | if (apply_autocmds_group(event_name2nr(arg, &arg), |
| 8693 | fname, NULL, TRUE, group, curbuf, NULL)) |
| 8694 | nothing_done = FALSE; |
| 8695 | |
| 8696 | if (nothing_done && do_msg) |
| 8697 | MSG(_("No matching autocommands")); |
| 8698 | |
| 8699 | #ifdef FEAT_EVAL |
| 8700 | return aborting() ? FAIL : OK; |
| 8701 | #else |
| 8702 | return OK; |
| 8703 | #endif |
| 8704 | } |
| 8705 | |
| 8706 | /* |
| 8707 | * ":doautoall": execute autocommands for each loaded buffer. |
| 8708 | */ |
| 8709 | void |
| 8710 | ex_doautoall(eap) |
| 8711 | exarg_T *eap; |
| 8712 | { |
| 8713 | int retval; |
| 8714 | aco_save_T aco; |
| 8715 | buf_T *buf; |
| 8716 | |
| 8717 | /* |
| 8718 | * This is a bit tricky: For some commands curwin->w_buffer needs to be |
| 8719 | * equal to curbuf, but for some buffers there may not be a window. |
| 8720 | * So we change the buffer for the current window for a moment. This |
| 8721 | * gives problems when the autocommands make changes to the list of |
| 8722 | * buffers or windows... |
| 8723 | */ |
| 8724 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 8725 | { |
Bram Moolenaar | 3a84797 | 2008-07-08 09:36:58 +0000 | [diff] [blame] | 8726 | if (buf->b_ml.ml_mfp != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8727 | { |
| 8728 | /* find a window for this buffer and save some values */ |
| 8729 | aucmd_prepbuf(&aco, buf); |
| 8730 | |
| 8731 | /* execute the autocommands for this buffer */ |
| 8732 | retval = do_doautocmd(eap->arg, FALSE); |
Bram Moolenaar | eeefcc7 | 2007-05-01 21:21:21 +0000 | [diff] [blame] | 8733 | |
| 8734 | /* Execute the modeline settings, but don't set window-local |
| 8735 | * options if we are using the current window for another buffer. */ |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8736 | do_modelines(curwin == aucmd_win ? OPT_NOWIN : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8737 | |
| 8738 | /* restore the current window */ |
| 8739 | aucmd_restbuf(&aco); |
| 8740 | |
| 8741 | /* stop if there is some error or buffer was deleted */ |
| 8742 | if (retval == FAIL || !buf_valid(buf)) |
| 8743 | break; |
| 8744 | } |
| 8745 | } |
| 8746 | |
| 8747 | check_cursor(); /* just in case lines got deleted */ |
| 8748 | } |
| 8749 | |
| 8750 | /* |
| 8751 | * Prepare for executing autocommands for (hidden) buffer "buf". |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8752 | * Search for a visible window containing the current buffer. If there isn't |
| 8753 | * one then use "aucmd_win". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8754 | * Set "curbuf" and "curwin" to match "buf". |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 8755 | * When FEAT_AUTOCMD is not defined another version is used, see below. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8756 | */ |
| 8757 | void |
| 8758 | aucmd_prepbuf(aco, buf) |
| 8759 | aco_save_T *aco; /* structure to save values in */ |
| 8760 | buf_T *buf; /* new curbuf */ |
| 8761 | { |
| 8762 | win_T *win; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8763 | #ifdef FEAT_WINDOWS |
| 8764 | int save_ea; |
| 8765 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8766 | |
| 8767 | /* Find a window that is for the new buffer */ |
| 8768 | if (buf == curbuf) /* be quick when buf is curbuf */ |
| 8769 | win = curwin; |
| 8770 | else |
| 8771 | #ifdef FEAT_WINDOWS |
| 8772 | for (win = firstwin; win != NULL; win = win->w_next) |
| 8773 | if (win->w_buffer == buf) |
| 8774 | break; |
| 8775 | #else |
| 8776 | win = NULL; |
| 8777 | #endif |
| 8778 | |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8779 | /* Allocate "aucmd_win" when needed. If this fails (out of memory) fall |
| 8780 | * back to using the current window. */ |
| 8781 | if (win == NULL && aucmd_win == NULL) |
| 8782 | { |
| 8783 | win_alloc_aucmd_win(); |
| 8784 | if (aucmd_win == NULL) |
| 8785 | win = curwin; |
| 8786 | } |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 8787 | if (win == NULL && aucmd_win_used) |
| 8788 | /* Strange recursive autocommand, fall back to using the current |
| 8789 | * window. Expect a few side effects... */ |
| 8790 | win = curwin; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8791 | |
| 8792 | aco->save_curwin = curwin; |
| 8793 | aco->save_curbuf = curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8794 | if (win != NULL) |
| 8795 | { |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8796 | /* There is a window for "buf" in the current tab page, make it the |
| 8797 | * curwin. This is preferred, it has the least side effects (esp. if |
| 8798 | * "buf" is curbuf). */ |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 8799 | aco->use_aucmd_win = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8800 | curwin = win; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8801 | } |
| 8802 | else |
| 8803 | { |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8804 | /* There is no window for "buf", use "aucmd_win". To minimize the side |
| 8805 | * effects, insert it in a the current tab page. |
| 8806 | * Anything related to a window (e.g., setting folds) may have |
| 8807 | * unexpected results. */ |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 8808 | aco->use_aucmd_win = TRUE; |
| 8809 | aucmd_win_used = TRUE; |
Bram Moolenaar | f061e0b | 2009-06-24 15:32:01 +0000 | [diff] [blame] | 8810 | aucmd_win->w_buffer = buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8811 | ++buf->b_nwindows; |
Bram Moolenaar | f061e0b | 2009-06-24 15:32:01 +0000 | [diff] [blame] | 8812 | win_init_empty(aucmd_win); /* set cursor and topline to safe values */ |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 8813 | vim_free(aucmd_win->w_localdir); |
| 8814 | aucmd_win->w_localdir = NULL; |
| 8815 | |
| 8816 | /* Make sure w_localdir and globaldir are NULL to avoid a chdir() in |
| 8817 | * win_enter_ext(). */ |
| 8818 | aucmd_win->w_localdir = NULL; |
| 8819 | aco->globaldir = globaldir; |
| 8820 | globaldir = NULL; |
| 8821 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8822 | |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8823 | #ifdef FEAT_WINDOWS |
Bram Moolenaar | 2bc76e6 | 2009-07-01 15:13:56 +0000 | [diff] [blame] | 8824 | /* Split the current window, put the aucmd_win in the upper half. |
| 8825 | * We don't want the BufEnter or WinEnter autocommands. */ |
| 8826 | block_autocmds(); |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8827 | make_snapshot(SNAP_AUCMD_IDX); |
| 8828 | save_ea = p_ea; |
| 8829 | p_ea = FALSE; |
| 8830 | (void)win_split_ins(0, WSP_TOP, aucmd_win, 0); |
| 8831 | (void)win_comp_pos(); /* recompute window positions */ |
| 8832 | p_ea = save_ea; |
Bram Moolenaar | 2bc76e6 | 2009-07-01 15:13:56 +0000 | [diff] [blame] | 8833 | unblock_autocmds(); |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8834 | #endif |
Bram Moolenaar | f061e0b | 2009-06-24 15:32:01 +0000 | [diff] [blame] | 8835 | curwin = aucmd_win; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8836 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8837 | curbuf = buf; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8838 | aco->new_curwin = curwin; |
| 8839 | aco->new_curbuf = curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8840 | } |
| 8841 | |
| 8842 | /* |
| 8843 | * Cleanup after executing autocommands for a (hidden) buffer. |
| 8844 | * Restore the window as it was (if possible). |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 8845 | * When FEAT_AUTOCMD is not defined another version is used, see below. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8846 | */ |
| 8847 | void |
| 8848 | aucmd_restbuf(aco) |
| 8849 | aco_save_T *aco; /* structure holding saved values */ |
| 8850 | { |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8851 | #ifdef FEAT_WINDOWS |
| 8852 | int dummy; |
| 8853 | #endif |
| 8854 | |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 8855 | if (aco->use_aucmd_win) |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8856 | { |
| 8857 | --curbuf->b_nwindows; |
| 8858 | #ifdef FEAT_WINDOWS |
| 8859 | /* 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] | 8860 | * page. Do not trigger autocommands here. */ |
| 8861 | block_autocmds(); |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8862 | if (curwin != aucmd_win) |
| 8863 | { |
| 8864 | tabpage_T *tp; |
| 8865 | win_T *wp; |
| 8866 | |
| 8867 | FOR_ALL_TAB_WINDOWS(tp, wp) |
| 8868 | { |
| 8869 | if (wp == aucmd_win) |
| 8870 | { |
| 8871 | if (tp != curtab) |
| 8872 | goto_tabpage_tp(tp); |
| 8873 | win_goto(aucmd_win); |
| 8874 | break; |
| 8875 | } |
| 8876 | } |
| 8877 | } |
| 8878 | |
| 8879 | /* Remove the window and frame from the tree of frames. */ |
| 8880 | (void)winframe_remove(curwin, &dummy, NULL); |
| 8881 | win_remove(curwin, NULL); |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 8882 | aucmd_win_used = FALSE; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8883 | last_status(FALSE); /* may need to remove last status line */ |
| 8884 | restore_snapshot(SNAP_AUCMD_IDX, FALSE); |
| 8885 | (void)win_comp_pos(); /* recompute window positions */ |
Bram Moolenaar | 2bc76e6 | 2009-07-01 15:13:56 +0000 | [diff] [blame] | 8886 | unblock_autocmds(); |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8887 | |
| 8888 | if (win_valid(aco->save_curwin)) |
| 8889 | curwin = aco->save_curwin; |
| 8890 | else |
| 8891 | /* Hmm, original window disappeared. Just use the first one. */ |
| 8892 | curwin = firstwin; |
| 8893 | # ifdef FEAT_EVAL |
| 8894 | vars_clear(&aucmd_win->w_vars.dv_hashtab); /* free all w: variables */ |
Bram Moolenaar | 50daf40 | 2009-11-17 13:57:22 +0000 | [diff] [blame] | 8895 | hash_init(&aucmd_win->w_vars.dv_hashtab); /* re-use the hashtab */ |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8896 | # endif |
| 8897 | #else |
| 8898 | curwin = aco->save_curwin; |
| 8899 | #endif |
| 8900 | curbuf = curwin->w_buffer; |
| 8901 | |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 8902 | vim_free(globaldir); |
| 8903 | globaldir = aco->globaldir; |
| 8904 | |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8905 | /* the buffer contents may have changed */ |
| 8906 | check_cursor(); |
| 8907 | if (curwin->w_topline > curbuf->b_ml.ml_line_count) |
| 8908 | { |
| 8909 | curwin->w_topline = curbuf->b_ml.ml_line_count; |
| 8910 | #ifdef FEAT_DIFF |
| 8911 | curwin->w_topfill = 0; |
| 8912 | #endif |
| 8913 | } |
| 8914 | #if defined(FEAT_GUI) |
| 8915 | /* Hide the scrollbars from the aucmd_win and update. */ |
| 8916 | gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_LEFT], FALSE); |
| 8917 | gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_RIGHT], FALSE); |
| 8918 | gui_may_update_scrollbars(); |
| 8919 | #endif |
| 8920 | } |
| 8921 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8922 | { |
| 8923 | /* restore curwin */ |
| 8924 | #ifdef FEAT_WINDOWS |
| 8925 | if (win_valid(aco->save_curwin)) |
| 8926 | #endif |
| 8927 | { |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8928 | /* Restore the buffer which was previously edited by curwin, if |
Bram Moolenaar | 6bef63c | 2009-07-29 10:10:29 +0000 | [diff] [blame] | 8929 | * 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] | 8930 | * valid. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8931 | if (curwin == aco->new_curwin |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8932 | && curbuf != aco->new_curbuf |
| 8933 | && buf_valid(aco->new_curbuf) |
| 8934 | && aco->new_curbuf->b_ml.ml_mfp != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8935 | { |
| 8936 | --curbuf->b_nwindows; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 8937 | curbuf = aco->new_curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8938 | curwin->w_buffer = curbuf; |
| 8939 | ++curbuf->b_nwindows; |
| 8940 | } |
| 8941 | |
| 8942 | curwin = aco->save_curwin; |
| 8943 | curbuf = curwin->w_buffer; |
| 8944 | } |
| 8945 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8946 | } |
| 8947 | |
| 8948 | static int autocmd_nested = FALSE; |
| 8949 | |
| 8950 | /* |
| 8951 | * Execute autocommands for "event" and file name "fname". |
| 8952 | * Return TRUE if some commands were executed. |
| 8953 | */ |
| 8954 | int |
| 8955 | apply_autocmds(event, fname, fname_io, force, buf) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8956 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8957 | char_u *fname; /* NULL or empty means use actual file name */ |
| 8958 | char_u *fname_io; /* fname to use for <afile> on cmdline */ |
| 8959 | int force; /* when TRUE, ignore autocmd_busy */ |
| 8960 | buf_T *buf; /* buffer for <abuf> */ |
| 8961 | { |
| 8962 | return apply_autocmds_group(event, fname, fname_io, force, |
| 8963 | AUGROUP_ALL, buf, NULL); |
| 8964 | } |
| 8965 | |
| 8966 | /* |
| 8967 | * Like apply_autocmds(), but with extra "eap" argument. This takes care of |
| 8968 | * setting v:filearg. |
| 8969 | */ |
| 8970 | static int |
| 8971 | apply_autocmds_exarg(event, fname, fname_io, force, buf, eap) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8972 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8973 | char_u *fname; |
| 8974 | char_u *fname_io; |
| 8975 | int force; |
| 8976 | buf_T *buf; |
| 8977 | exarg_T *eap; |
| 8978 | { |
| 8979 | return apply_autocmds_group(event, fname, fname_io, force, |
| 8980 | AUGROUP_ALL, buf, eap); |
| 8981 | } |
| 8982 | |
| 8983 | /* |
| 8984 | * Like apply_autocmds(), but handles the caller's retval. If the script |
| 8985 | * processing is being aborted or if retval is FAIL when inside a try |
| 8986 | * conditional, no autocommands are executed. If otherwise the autocommands |
| 8987 | * cause the script to be aborted, retval is set to FAIL. |
| 8988 | */ |
| 8989 | int |
| 8990 | apply_autocmds_retval(event, fname, fname_io, force, buf, retval) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8991 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8992 | char_u *fname; /* NULL or empty means use actual file name */ |
| 8993 | char_u *fname_io; /* fname to use for <afile> on cmdline */ |
| 8994 | int force; /* when TRUE, ignore autocmd_busy */ |
| 8995 | buf_T *buf; /* buffer for <abuf> */ |
| 8996 | int *retval; /* pointer to caller's retval */ |
| 8997 | { |
| 8998 | int did_cmd; |
| 8999 | |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 9000 | #ifdef FEAT_EVAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9001 | if (should_abort(*retval)) |
| 9002 | return FALSE; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 9003 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9004 | |
| 9005 | did_cmd = apply_autocmds_group(event, fname, fname_io, force, |
| 9006 | AUGROUP_ALL, buf, NULL); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 9007 | if (did_cmd |
| 9008 | #ifdef FEAT_EVAL |
| 9009 | && aborting() |
| 9010 | #endif |
| 9011 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9012 | *retval = FAIL; |
| 9013 | return did_cmd; |
| 9014 | } |
| 9015 | |
Bram Moolenaar | d35f971 | 2005-12-18 22:02:33 +0000 | [diff] [blame] | 9016 | /* |
| 9017 | * Return TRUE when there is a CursorHold autocommand defined. |
| 9018 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9019 | int |
| 9020 | has_cursorhold() |
| 9021 | { |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9022 | return (first_autopat[(int)(get_real_state() == NORMAL_BUSY |
| 9023 | ? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)] != NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9024 | } |
Bram Moolenaar | d35f971 | 2005-12-18 22:02:33 +0000 | [diff] [blame] | 9025 | |
| 9026 | /* |
| 9027 | * Return TRUE if the CursorHold event can be triggered. |
| 9028 | */ |
| 9029 | int |
| 9030 | trigger_cursorhold() |
| 9031 | { |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9032 | int state; |
| 9033 | |
Bram Moolenaar | d29a9ee | 2006-09-14 09:07:34 +0000 | [diff] [blame] | 9034 | if (!did_cursorhold && has_cursorhold() && !Recording |
| 9035 | #ifdef FEAT_INS_EXPAND |
| 9036 | && !ins_compl_active() |
| 9037 | #endif |
| 9038 | ) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9039 | { |
| 9040 | state = get_real_state(); |
| 9041 | if (state == NORMAL_BUSY || (state & INSERT) != 0) |
| 9042 | return TRUE; |
| 9043 | } |
| 9044 | return FALSE; |
Bram Moolenaar | d35f971 | 2005-12-18 22:02:33 +0000 | [diff] [blame] | 9045 | } |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9046 | |
| 9047 | /* |
| 9048 | * Return TRUE when there is a CursorMoved autocommand defined. |
| 9049 | */ |
| 9050 | int |
| 9051 | has_cursormoved() |
| 9052 | { |
| 9053 | return (first_autopat[(int)EVENT_CURSORMOVED] != NULL); |
| 9054 | } |
| 9055 | |
| 9056 | /* |
| 9057 | * Return TRUE when there is a CursorMovedI autocommand defined. |
| 9058 | */ |
| 9059 | int |
| 9060 | has_cursormovedI() |
| 9061 | { |
| 9062 | return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL); |
| 9063 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9064 | |
| 9065 | static int |
| 9066 | apply_autocmds_group(event, fname, fname_io, force, group, buf, eap) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9067 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9068 | char_u *fname; /* NULL or empty means use actual file name */ |
| 9069 | char_u *fname_io; /* fname to use for <afile> on cmdline, NULL means |
| 9070 | use fname */ |
| 9071 | int force; /* when TRUE, ignore autocmd_busy */ |
| 9072 | int group; /* group ID, or AUGROUP_ALL */ |
| 9073 | buf_T *buf; /* buffer for <abuf> */ |
| 9074 | exarg_T *eap; /* command arguments */ |
| 9075 | { |
| 9076 | char_u *sfname = NULL; /* short file name */ |
| 9077 | char_u *tail; |
| 9078 | int save_changed; |
| 9079 | buf_T *old_curbuf; |
| 9080 | int retval = FALSE; |
| 9081 | char_u *save_sourcing_name; |
| 9082 | linenr_T save_sourcing_lnum; |
| 9083 | char_u *save_autocmd_fname; |
Bram Moolenaar | f6dad43 | 2008-09-18 19:29:58 +0000 | [diff] [blame] | 9084 | int save_autocmd_fname_full; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9085 | int save_autocmd_bufnr; |
| 9086 | char_u *save_autocmd_match; |
| 9087 | int save_autocmd_busy; |
| 9088 | int save_autocmd_nested; |
| 9089 | static int nesting = 0; |
| 9090 | AutoPatCmd patcmd; |
| 9091 | AutoPat *ap; |
| 9092 | #ifdef FEAT_EVAL |
| 9093 | scid_T save_current_SID; |
| 9094 | void *save_funccalp; |
| 9095 | char_u *save_cmdarg; |
| 9096 | long save_cmdbang; |
| 9097 | #endif |
| 9098 | static int filechangeshell_busy = FALSE; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 9099 | #ifdef FEAT_PROFILE |
| 9100 | proftime_T wait_time; |
| 9101 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9102 | |
| 9103 | /* |
| 9104 | * Quickly return if there are no autocommands for this event or |
| 9105 | * autocommands are blocked. |
| 9106 | */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 9107 | if (first_autopat[(int)event] == NULL || autocmd_blocked > 0) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9108 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9109 | |
| 9110 | /* |
| 9111 | * When autocommands are busy, new autocommands are only executed when |
| 9112 | * explicitly enabled with the "nested" flag. |
| 9113 | */ |
| 9114 | if (autocmd_busy && !(force || autocmd_nested)) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9115 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9116 | |
| 9117 | #ifdef FEAT_EVAL |
| 9118 | /* |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 9119 | * Quickly return when immediately aborting on error, or when an interrupt |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9120 | * occurred or an exception was thrown but not caught. |
| 9121 | */ |
| 9122 | if (aborting()) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9123 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9124 | #endif |
| 9125 | |
| 9126 | /* |
| 9127 | * FileChangedShell never nests, because it can create an endless loop. |
| 9128 | */ |
Bram Moolenaar | 5671873 | 2006-03-15 22:53:57 +0000 | [diff] [blame] | 9129 | if (filechangeshell_busy && (event == EVENT_FILECHANGEDSHELL |
| 9130 | || event == EVENT_FILECHANGEDSHELLPOST)) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9131 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9132 | |
| 9133 | /* |
| 9134 | * Ignore events in 'eventignore'. |
| 9135 | */ |
| 9136 | if (event_ignored(event)) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9137 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9138 | |
| 9139 | /* |
| 9140 | * Allow nesting of autocommands, but restrict the depth, because it's |
| 9141 | * possible to create an endless loop. |
| 9142 | */ |
| 9143 | if (nesting == 10) |
| 9144 | { |
| 9145 | EMSG(_("E218: autocommand nesting too deep")); |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9146 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9147 | } |
| 9148 | |
| 9149 | /* |
| 9150 | * Check if these autocommands are disabled. Used when doing ":all" or |
| 9151 | * ":ball". |
| 9152 | */ |
| 9153 | if ( (autocmd_no_enter |
| 9154 | && (event == EVENT_WINENTER || event == EVENT_BUFENTER)) |
| 9155 | || (autocmd_no_leave |
| 9156 | && (event == EVENT_WINLEAVE || event == EVENT_BUFLEAVE))) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9157 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9158 | |
| 9159 | /* |
| 9160 | * Save the autocmd_* variables and info about the current buffer. |
| 9161 | */ |
| 9162 | save_autocmd_fname = autocmd_fname; |
Bram Moolenaar | f6dad43 | 2008-09-18 19:29:58 +0000 | [diff] [blame] | 9163 | save_autocmd_fname_full = autocmd_fname_full; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9164 | save_autocmd_bufnr = autocmd_bufnr; |
| 9165 | save_autocmd_match = autocmd_match; |
| 9166 | save_autocmd_busy = autocmd_busy; |
| 9167 | save_autocmd_nested = autocmd_nested; |
| 9168 | save_changed = curbuf->b_changed; |
| 9169 | old_curbuf = curbuf; |
| 9170 | |
| 9171 | /* |
| 9172 | * Set the file name to be used for <afile>. |
Bram Moolenaar | a0174af | 2008-01-02 20:08:25 +0000 | [diff] [blame] | 9173 | * Make a copy to avoid that changing a buffer name or directory makes it |
| 9174 | * invalid. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9175 | */ |
| 9176 | if (fname_io == NULL) |
| 9177 | { |
| 9178 | if (fname != NULL && *fname != NUL) |
| 9179 | autocmd_fname = fname; |
| 9180 | else if (buf != NULL) |
Bram Moolenaar | f6dad43 | 2008-09-18 19:29:58 +0000 | [diff] [blame] | 9181 | autocmd_fname = buf->b_ffname; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9182 | else |
| 9183 | autocmd_fname = NULL; |
| 9184 | } |
| 9185 | else |
| 9186 | autocmd_fname = fname_io; |
Bram Moolenaar | a0174af | 2008-01-02 20:08:25 +0000 | [diff] [blame] | 9187 | if (autocmd_fname != NULL) |
Bram Moolenaar | f6dad43 | 2008-09-18 19:29:58 +0000 | [diff] [blame] | 9188 | autocmd_fname = vim_strsave(autocmd_fname); |
| 9189 | autocmd_fname_full = FALSE; /* call FullName_save() later */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9190 | |
| 9191 | /* |
| 9192 | * Set the buffer number to be used for <abuf>. |
| 9193 | */ |
| 9194 | if (buf == NULL) |
| 9195 | autocmd_bufnr = 0; |
| 9196 | else |
| 9197 | autocmd_bufnr = buf->b_fnum; |
| 9198 | |
| 9199 | /* |
| 9200 | * When the file name is NULL or empty, use the file name of buffer "buf". |
| 9201 | * Always use the full path of the file name to match with, in case |
| 9202 | * "allow_dirs" is set. |
| 9203 | */ |
| 9204 | if (fname == NULL || *fname == NUL) |
| 9205 | { |
| 9206 | if (buf == NULL) |
| 9207 | fname = NULL; |
| 9208 | else |
| 9209 | { |
| 9210 | #ifdef FEAT_SYN_HL |
| 9211 | if (event == EVENT_SYNTAX) |
| 9212 | fname = buf->b_p_syn; |
| 9213 | else |
| 9214 | #endif |
| 9215 | if (event == EVENT_FILETYPE) |
| 9216 | fname = buf->b_p_ft; |
| 9217 | else |
| 9218 | { |
| 9219 | if (buf->b_sfname != NULL) |
| 9220 | sfname = vim_strsave(buf->b_sfname); |
| 9221 | fname = buf->b_ffname; |
| 9222 | } |
| 9223 | } |
| 9224 | if (fname == NULL) |
| 9225 | fname = (char_u *)""; |
| 9226 | fname = vim_strsave(fname); /* make a copy, so we can change it */ |
| 9227 | } |
| 9228 | else |
| 9229 | { |
| 9230 | sfname = vim_strsave(fname); |
Bram Moolenaar | 5135d46 | 2009-04-29 16:03:38 +0000 | [diff] [blame] | 9231 | /* Don't try expanding FileType, Syntax, FuncUndefined, WindowID or |
| 9232 | * QuickFixCmd* */ |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 9233 | if (event == EVENT_FILETYPE |
| 9234 | || event == EVENT_SYNTAX |
Bram Moolenaar | 5135d46 | 2009-04-29 16:03:38 +0000 | [diff] [blame] | 9235 | || event == EVENT_FUNCUNDEFINED |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 9236 | || event == EVENT_REMOTEREPLY |
Bram Moolenaar | b8a7b56 | 2006-02-01 21:47:16 +0000 | [diff] [blame] | 9237 | || event == EVENT_SPELLFILEMISSING |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 9238 | || event == EVENT_QUICKFIXCMDPRE |
| 9239 | || event == EVENT_QUICKFIXCMDPOST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9240 | fname = vim_strsave(fname); |
| 9241 | else |
| 9242 | fname = FullName_save(fname, FALSE); |
| 9243 | } |
| 9244 | if (fname == NULL) /* out of memory */ |
| 9245 | { |
| 9246 | vim_free(sfname); |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9247 | retval = FALSE; |
| 9248 | goto BYPASS_AU; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9249 | } |
| 9250 | |
| 9251 | #ifdef BACKSLASH_IN_FILENAME |
| 9252 | /* |
| 9253 | * Replace all backslashes with forward slashes. This makes the |
| 9254 | * autocommand patterns portable between Unix and MS-DOS. |
| 9255 | */ |
| 9256 | if (sfname != NULL) |
| 9257 | forward_slash(sfname); |
| 9258 | forward_slash(fname); |
| 9259 | #endif |
| 9260 | |
| 9261 | #ifdef VMS |
| 9262 | /* remove version for correct match */ |
| 9263 | if (sfname != NULL) |
| 9264 | vms_remove_version(sfname); |
| 9265 | vms_remove_version(fname); |
| 9266 | #endif |
| 9267 | |
| 9268 | /* |
| 9269 | * Set the name to be used for <amatch>. |
| 9270 | */ |
| 9271 | autocmd_match = fname; |
| 9272 | |
| 9273 | |
| 9274 | /* Don't redraw while doing auto commands. */ |
| 9275 | ++RedrawingDisabled; |
| 9276 | save_sourcing_name = sourcing_name; |
| 9277 | sourcing_name = NULL; /* don't free this one */ |
| 9278 | save_sourcing_lnum = sourcing_lnum; |
| 9279 | sourcing_lnum = 0; /* no line number here */ |
| 9280 | |
| 9281 | #ifdef FEAT_EVAL |
| 9282 | save_current_SID = current_SID; |
| 9283 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 9284 | # ifdef FEAT_PROFILE |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 9285 | if (do_profiling == PROF_YES) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 9286 | prof_child_enter(&wait_time); /* doesn't count for the caller itself */ |
| 9287 | # endif |
| 9288 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9289 | /* Don't use local function variables, if called from a function */ |
| 9290 | save_funccalp = save_funccal(); |
| 9291 | #endif |
| 9292 | |
| 9293 | /* |
| 9294 | * When starting to execute autocommands, save the search patterns. |
| 9295 | */ |
| 9296 | if (!autocmd_busy) |
| 9297 | { |
| 9298 | save_search_patterns(); |
| 9299 | saveRedobuff(); |
| 9300 | did_filetype = keep_filetype; |
| 9301 | } |
| 9302 | |
| 9303 | /* |
| 9304 | * Note that we are applying autocmds. Some commands need to know. |
| 9305 | */ |
| 9306 | autocmd_busy = TRUE; |
| 9307 | filechangeshell_busy = (event == EVENT_FILECHANGEDSHELL); |
| 9308 | ++nesting; /* see matching decrement below */ |
| 9309 | |
| 9310 | /* Remember that FileType was triggered. Used for did_filetype(). */ |
| 9311 | if (event == EVENT_FILETYPE) |
| 9312 | did_filetype = TRUE; |
| 9313 | |
| 9314 | tail = gettail(fname); |
| 9315 | |
| 9316 | /* Find first autocommand that matches */ |
| 9317 | patcmd.curpat = first_autopat[(int)event]; |
| 9318 | patcmd.nextcmd = NULL; |
| 9319 | patcmd.group = group; |
| 9320 | patcmd.fname = fname; |
| 9321 | patcmd.sfname = sfname; |
| 9322 | patcmd.tail = tail; |
| 9323 | patcmd.event = event; |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9324 | patcmd.arg_bufnr = autocmd_bufnr; |
| 9325 | patcmd.next = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9326 | auto_next_pat(&patcmd, FALSE); |
| 9327 | |
| 9328 | /* found one, start executing the autocommands */ |
| 9329 | if (patcmd.curpat != NULL) |
| 9330 | { |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9331 | /* add to active_apc_list */ |
| 9332 | patcmd.next = active_apc_list; |
| 9333 | active_apc_list = &patcmd; |
| 9334 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9335 | #ifdef FEAT_EVAL |
| 9336 | /* set v:cmdarg (only when there is a matching pattern) */ |
| 9337 | save_cmdbang = get_vim_var_nr(VV_CMDBANG); |
| 9338 | if (eap != NULL) |
| 9339 | { |
| 9340 | save_cmdarg = set_cmdarg(eap, NULL); |
| 9341 | set_vim_var_nr(VV_CMDBANG, (long)eap->forceit); |
| 9342 | } |
| 9343 | else |
| 9344 | save_cmdarg = NULL; /* avoid gcc warning */ |
| 9345 | #endif |
| 9346 | retval = TRUE; |
| 9347 | /* mark the last pattern, to avoid an endless loop when more patterns |
| 9348 | * are added when executing autocommands */ |
| 9349 | for (ap = patcmd.curpat; ap->next != NULL; ap = ap->next) |
| 9350 | ap->last = FALSE; |
| 9351 | ap->last = TRUE; |
| 9352 | check_lnums(TRUE); /* make sure cursor and topline are valid */ |
| 9353 | do_cmdline(NULL, getnextac, (void *)&patcmd, |
| 9354 | DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT); |
| 9355 | #ifdef FEAT_EVAL |
| 9356 | if (eap != NULL) |
| 9357 | { |
| 9358 | (void)set_cmdarg(NULL, save_cmdarg); |
| 9359 | set_vim_var_nr(VV_CMDBANG, save_cmdbang); |
| 9360 | } |
| 9361 | #endif |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9362 | /* delete from active_apc_list */ |
| 9363 | if (active_apc_list == &patcmd) /* just in case */ |
| 9364 | active_apc_list = patcmd.next; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9365 | } |
| 9366 | |
| 9367 | --RedrawingDisabled; |
| 9368 | autocmd_busy = save_autocmd_busy; |
| 9369 | filechangeshell_busy = FALSE; |
| 9370 | autocmd_nested = save_autocmd_nested; |
| 9371 | vim_free(sourcing_name); |
| 9372 | sourcing_name = save_sourcing_name; |
| 9373 | sourcing_lnum = save_sourcing_lnum; |
Bram Moolenaar | a0174af | 2008-01-02 20:08:25 +0000 | [diff] [blame] | 9374 | vim_free(autocmd_fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9375 | autocmd_fname = save_autocmd_fname; |
Bram Moolenaar | f6dad43 | 2008-09-18 19:29:58 +0000 | [diff] [blame] | 9376 | autocmd_fname_full = save_autocmd_fname_full; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9377 | autocmd_bufnr = save_autocmd_bufnr; |
| 9378 | autocmd_match = save_autocmd_match; |
| 9379 | #ifdef FEAT_EVAL |
| 9380 | current_SID = save_current_SID; |
| 9381 | restore_funccal(save_funccalp); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 9382 | # ifdef FEAT_PROFILE |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 9383 | if (do_profiling == PROF_YES) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 9384 | prof_child_exit(&wait_time); |
| 9385 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9386 | #endif |
| 9387 | vim_free(fname); |
| 9388 | vim_free(sfname); |
| 9389 | --nesting; /* see matching increment above */ |
| 9390 | |
| 9391 | /* |
| 9392 | * When stopping to execute autocommands, restore the search patterns and |
| 9393 | * the redo buffer. |
| 9394 | */ |
| 9395 | if (!autocmd_busy) |
| 9396 | { |
| 9397 | restore_search_patterns(); |
| 9398 | restoreRedobuff(); |
| 9399 | did_filetype = FALSE; |
| 9400 | } |
| 9401 | |
| 9402 | /* |
| 9403 | * Some events don't set or reset the Changed flag. |
| 9404 | * Check if still in the same buffer! |
| 9405 | */ |
| 9406 | if (curbuf == old_curbuf |
| 9407 | && (event == EVENT_BUFREADPOST |
| 9408 | || event == EVENT_BUFWRITEPOST |
| 9409 | || event == EVENT_FILEAPPENDPOST |
| 9410 | || event == EVENT_VIMLEAVE |
| 9411 | || event == EVENT_VIMLEAVEPRE)) |
| 9412 | { |
| 9413 | #ifdef FEAT_TITLE |
| 9414 | if (curbuf->b_changed != save_changed) |
| 9415 | need_maketitle = TRUE; |
| 9416 | #endif |
| 9417 | curbuf->b_changed = save_changed; |
| 9418 | } |
| 9419 | |
| 9420 | au_cleanup(); /* may really delete removed patterns/commands now */ |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9421 | |
| 9422 | BYPASS_AU: |
| 9423 | /* When wiping out a buffer make sure all its buffer-local autocommands |
| 9424 | * are deleted. */ |
| 9425 | if (event == EVENT_BUFWIPEOUT && buf != NULL) |
| 9426 | aubuflocal_remove(buf); |
| 9427 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9428 | return retval; |
| 9429 | } |
| 9430 | |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 9431 | # ifdef FEAT_EVAL |
| 9432 | static char_u *old_termresponse = NULL; |
| 9433 | # endif |
| 9434 | |
| 9435 | /* |
| 9436 | * Block triggering autocommands until unblock_autocmd() is called. |
| 9437 | * Can be used recursively, so long as it's symmetric. |
| 9438 | */ |
| 9439 | void |
| 9440 | block_autocmds() |
| 9441 | { |
| 9442 | # ifdef FEAT_EVAL |
| 9443 | /* Remember the value of v:termresponse. */ |
| 9444 | if (autocmd_blocked == 0) |
| 9445 | old_termresponse = get_vim_var_str(VV_TERMRESPONSE); |
| 9446 | # endif |
| 9447 | ++autocmd_blocked; |
| 9448 | } |
| 9449 | |
| 9450 | void |
| 9451 | unblock_autocmds() |
| 9452 | { |
| 9453 | --autocmd_blocked; |
| 9454 | |
| 9455 | # ifdef FEAT_EVAL |
| 9456 | /* When v:termresponse was set while autocommands were blocked, trigger |
| 9457 | * the autocommands now. Esp. useful when executing a shell command |
| 9458 | * during startup (vimdiff). */ |
| 9459 | if (autocmd_blocked == 0 |
| 9460 | && get_vim_var_str(VV_TERMRESPONSE) != old_termresponse) |
| 9461 | apply_autocmds(EVENT_TERMRESPONSE, NULL, NULL, FALSE, curbuf); |
| 9462 | # endif |
| 9463 | } |
| 9464 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9465 | /* |
| 9466 | * Find next autocommand pattern that matches. |
| 9467 | */ |
| 9468 | static void |
| 9469 | auto_next_pat(apc, stop_at_last) |
| 9470 | AutoPatCmd *apc; |
| 9471 | int stop_at_last; /* stop when 'last' flag is set */ |
| 9472 | { |
| 9473 | AutoPat *ap; |
| 9474 | AutoCmd *cp; |
| 9475 | char_u *name; |
| 9476 | char *s; |
| 9477 | |
| 9478 | vim_free(sourcing_name); |
| 9479 | sourcing_name = NULL; |
| 9480 | |
| 9481 | for (ap = apc->curpat; ap != NULL && !got_int; ap = ap->next) |
| 9482 | { |
| 9483 | apc->curpat = NULL; |
| 9484 | |
Bram Moolenaar | f6dad43 | 2008-09-18 19:29:58 +0000 | [diff] [blame] | 9485 | /* 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] | 9486 | * the group matches. For buffer-local autocommands only check the |
| 9487 | * buffer number. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9488 | if (ap->pat != NULL && ap->cmds != NULL |
| 9489 | && (apc->group == AUGROUP_ALL || apc->group == ap->group)) |
| 9490 | { |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9491 | /* execution-condition */ |
| 9492 | if (ap->buflocal_nr == 0 |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 9493 | ? (match_file_pat(NULL, ap->reg_prog, apc->fname, |
| 9494 | apc->sfname, apc->tail, ap->allow_dirs)) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9495 | : ap->buflocal_nr == apc->arg_bufnr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9496 | { |
| 9497 | name = event_nr2name(apc->event); |
| 9498 | s = _("%s Auto commands for \"%s\""); |
| 9499 | sourcing_name = alloc((unsigned)(STRLEN(s) |
| 9500 | + STRLEN(name) + ap->patlen + 1)); |
| 9501 | if (sourcing_name != NULL) |
| 9502 | { |
| 9503 | sprintf((char *)sourcing_name, s, |
| 9504 | (char *)name, (char *)ap->pat); |
| 9505 | if (p_verbose >= 8) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 9506 | { |
| 9507 | verbose_enter(); |
Bram Moolenaar | 051b782 | 2005-05-19 21:00:46 +0000 | [diff] [blame] | 9508 | smsg((char_u *)_("Executing %s"), sourcing_name); |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 9509 | verbose_leave(); |
| 9510 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9511 | } |
| 9512 | |
| 9513 | apc->curpat = ap; |
| 9514 | apc->nextcmd = ap->cmds; |
| 9515 | /* mark last command */ |
| 9516 | for (cp = ap->cmds; cp->next != NULL; cp = cp->next) |
| 9517 | cp->last = FALSE; |
| 9518 | cp->last = TRUE; |
| 9519 | } |
| 9520 | line_breakcheck(); |
| 9521 | if (apc->curpat != NULL) /* found a match */ |
| 9522 | break; |
| 9523 | } |
| 9524 | if (stop_at_last && ap->last) |
| 9525 | break; |
| 9526 | } |
| 9527 | } |
| 9528 | |
| 9529 | /* |
| 9530 | * Get next autocommand command. |
| 9531 | * Called by do_cmdline() to get the next line for ":if". |
| 9532 | * Returns allocated string, or NULL for end of autocommands. |
| 9533 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9534 | static char_u * |
| 9535 | getnextac(c, cookie, indent) |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 9536 | int c UNUSED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9537 | void *cookie; |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 9538 | int indent UNUSED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9539 | { |
| 9540 | AutoPatCmd *acp = (AutoPatCmd *)cookie; |
| 9541 | char_u *retval; |
| 9542 | AutoCmd *ac; |
| 9543 | |
| 9544 | /* Can be called again after returning the last line. */ |
| 9545 | if (acp->curpat == NULL) |
| 9546 | return NULL; |
| 9547 | |
| 9548 | /* repeat until we find an autocommand to execute */ |
| 9549 | for (;;) |
| 9550 | { |
| 9551 | /* skip removed commands */ |
| 9552 | while (acp->nextcmd != NULL && acp->nextcmd->cmd == NULL) |
| 9553 | if (acp->nextcmd->last) |
| 9554 | acp->nextcmd = NULL; |
| 9555 | else |
| 9556 | acp->nextcmd = acp->nextcmd->next; |
| 9557 | |
| 9558 | if (acp->nextcmd != NULL) |
| 9559 | break; |
| 9560 | |
| 9561 | /* at end of commands, find next pattern that matches */ |
| 9562 | if (acp->curpat->last) |
| 9563 | acp->curpat = NULL; |
| 9564 | else |
| 9565 | acp->curpat = acp->curpat->next; |
| 9566 | if (acp->curpat != NULL) |
| 9567 | auto_next_pat(acp, TRUE); |
| 9568 | if (acp->curpat == NULL) |
| 9569 | return NULL; |
| 9570 | } |
| 9571 | |
| 9572 | ac = acp->nextcmd; |
| 9573 | |
| 9574 | if (p_verbose >= 9) |
| 9575 | { |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 9576 | verbose_enter_scroll(); |
Bram Moolenaar | 051b782 | 2005-05-19 21:00:46 +0000 | [diff] [blame] | 9577 | smsg((char_u *)_("autocommand %s"), ac->cmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9578 | msg_puts((char_u *)"\n"); /* don't overwrite this either */ |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 9579 | verbose_leave_scroll(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9580 | } |
| 9581 | retval = vim_strsave(ac->cmd); |
| 9582 | autocmd_nested = ac->nested; |
| 9583 | #ifdef FEAT_EVAL |
| 9584 | current_SID = ac->scriptID; |
| 9585 | #endif |
| 9586 | if (ac->last) |
| 9587 | acp->nextcmd = NULL; |
| 9588 | else |
| 9589 | acp->nextcmd = ac->next; |
| 9590 | return retval; |
| 9591 | } |
| 9592 | |
| 9593 | /* |
| 9594 | * Return TRUE if there is a matching autocommand for "fname". |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9595 | * To account for buffer-local autocommands, function needs to know |
| 9596 | * in which buffer the file will be opened. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9597 | */ |
| 9598 | int |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9599 | has_autocmd(event, sfname, buf) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9600 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9601 | char_u *sfname; |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9602 | buf_T *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9603 | { |
| 9604 | AutoPat *ap; |
| 9605 | char_u *fname; |
| 9606 | char_u *tail = gettail(sfname); |
| 9607 | int retval = FALSE; |
| 9608 | |
| 9609 | fname = FullName_save(sfname, FALSE); |
| 9610 | if (fname == NULL) |
| 9611 | return FALSE; |
| 9612 | |
| 9613 | #ifdef BACKSLASH_IN_FILENAME |
| 9614 | /* |
| 9615 | * Replace all backslashes with forward slashes. This makes the |
| 9616 | * autocommand patterns portable between Unix and MS-DOS. |
| 9617 | */ |
| 9618 | sfname = vim_strsave(sfname); |
| 9619 | if (sfname != NULL) |
| 9620 | forward_slash(sfname); |
| 9621 | forward_slash(fname); |
| 9622 | #endif |
| 9623 | |
| 9624 | for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) |
| 9625 | if (ap->pat != NULL && ap->cmds != NULL |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 9626 | && (ap->buflocal_nr == 0 |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 9627 | ? match_file_pat(NULL, ap->reg_prog, |
| 9628 | fname, sfname, tail, ap->allow_dirs) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 9629 | : buf != NULL && ap->buflocal_nr == buf->b_fnum |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9630 | )) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9631 | { |
| 9632 | retval = TRUE; |
| 9633 | break; |
| 9634 | } |
| 9635 | |
| 9636 | vim_free(fname); |
| 9637 | #ifdef BACKSLASH_IN_FILENAME |
| 9638 | vim_free(sfname); |
| 9639 | #endif |
| 9640 | |
| 9641 | return retval; |
| 9642 | } |
| 9643 | |
| 9644 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 9645 | /* |
| 9646 | * Function given to ExpandGeneric() to obtain the list of autocommand group |
| 9647 | * names. |
| 9648 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9649 | char_u * |
| 9650 | get_augroup_name(xp, idx) |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 9651 | expand_T *xp UNUSED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9652 | int idx; |
| 9653 | { |
| 9654 | if (idx == augroups.ga_len) /* add "END" add the end */ |
| 9655 | return (char_u *)"END"; |
| 9656 | if (idx >= augroups.ga_len) /* end of list */ |
| 9657 | return NULL; |
| 9658 | if (AUGROUP_NAME(idx) == NULL) /* skip deleted entries */ |
| 9659 | return (char_u *)""; |
| 9660 | return AUGROUP_NAME(idx); /* return a name */ |
| 9661 | } |
| 9662 | |
| 9663 | static int include_groups = FALSE; |
| 9664 | |
| 9665 | char_u * |
| 9666 | set_context_in_autocmd(xp, arg, doautocmd) |
| 9667 | expand_T *xp; |
| 9668 | char_u *arg; |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 9669 | int doautocmd; /* TRUE for :doauto*, FALSE for :autocmd */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9670 | { |
| 9671 | char_u *p; |
| 9672 | int group; |
| 9673 | |
| 9674 | /* check for a group name, skip it if present */ |
| 9675 | include_groups = FALSE; |
| 9676 | p = arg; |
| 9677 | group = au_get_grouparg(&arg); |
| 9678 | if (group == AUGROUP_ERROR) |
| 9679 | return NULL; |
| 9680 | /* If there only is a group name that's what we expand. */ |
| 9681 | if (*arg == NUL && group != AUGROUP_ALL && !vim_iswhite(arg[-1])) |
| 9682 | { |
| 9683 | arg = p; |
| 9684 | group = AUGROUP_ALL; |
| 9685 | } |
| 9686 | |
| 9687 | /* skip over event name */ |
| 9688 | for (p = arg; *p != NUL && !vim_iswhite(*p); ++p) |
| 9689 | if (*p == ',') |
| 9690 | arg = p + 1; |
| 9691 | if (*p == NUL) |
| 9692 | { |
| 9693 | if (group == AUGROUP_ALL) |
| 9694 | include_groups = TRUE; |
| 9695 | xp->xp_context = EXPAND_EVENTS; /* expand event name */ |
| 9696 | xp->xp_pattern = arg; |
| 9697 | return NULL; |
| 9698 | } |
| 9699 | |
| 9700 | /* skip over pattern */ |
| 9701 | arg = skipwhite(p); |
| 9702 | while (*arg && (!vim_iswhite(*arg) || arg[-1] == '\\')) |
| 9703 | arg++; |
| 9704 | if (*arg) |
| 9705 | return arg; /* expand (next) command */ |
| 9706 | |
| 9707 | if (doautocmd) |
| 9708 | xp->xp_context = EXPAND_FILES; /* expand file names */ |
| 9709 | else |
| 9710 | xp->xp_context = EXPAND_NOTHING; /* pattern is not expanded */ |
| 9711 | return NULL; |
| 9712 | } |
| 9713 | |
| 9714 | /* |
| 9715 | * Function given to ExpandGeneric() to obtain the list of event names. |
| 9716 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9717 | char_u * |
| 9718 | get_event_name(xp, idx) |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 9719 | expand_T *xp UNUSED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9720 | int idx; |
| 9721 | { |
| 9722 | if (idx < augroups.ga_len) /* First list group names, if wanted */ |
| 9723 | { |
| 9724 | if (!include_groups || AUGROUP_NAME(idx) == NULL) |
| 9725 | return (char_u *)""; /* skip deleted entries */ |
| 9726 | return AUGROUP_NAME(idx); /* return a name */ |
| 9727 | } |
| 9728 | return (char_u *)event_names[idx - augroups.ga_len].name; |
| 9729 | } |
| 9730 | |
| 9731 | #endif /* FEAT_CMDL_COMPL */ |
| 9732 | |
| 9733 | /* |
Bram Moolenaar | f4cd3e8 | 2005-12-22 22:47:02 +0000 | [diff] [blame] | 9734 | * Return TRUE if autocmd is supported. |
| 9735 | */ |
| 9736 | int |
| 9737 | autocmd_supported(name) |
| 9738 | char_u *name; |
| 9739 | { |
| 9740 | char_u *p; |
| 9741 | |
| 9742 | return (event_name2nr(name, &p) != NUM_EVENTS); |
| 9743 | } |
| 9744 | |
| 9745 | /* |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9746 | * Return TRUE if an autocommand is defined for a group, event and |
| 9747 | * pattern: The group can be omitted to accept any group. "event" and "pattern" |
| 9748 | * can be NULL to accept any event and pattern. "pattern" can be NULL to accept |
| 9749 | * any pattern. Buffer-local patterns <buffer> or <buffer=N> are accepted. |
| 9750 | * Used for: |
| 9751 | * exists("#Group") or |
| 9752 | * exists("#Group#Event") or |
| 9753 | * exists("#Group#Event#pat") or |
| 9754 | * exists("#Event") or |
| 9755 | * exists("#Event#pat") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9756 | */ |
| 9757 | int |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9758 | au_exists(arg) |
| 9759 | char_u *arg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9760 | { |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9761 | char_u *arg_save; |
| 9762 | char_u *pattern = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9763 | char_u *event_name; |
| 9764 | char_u *p; |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9765 | event_T event; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9766 | AutoPat *ap; |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9767 | buf_T *buflocal_buf = NULL; |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9768 | int group; |
| 9769 | int retval = FALSE; |
| 9770 | |
Bram Moolenaar | f4cd3e8 | 2005-12-22 22:47:02 +0000 | [diff] [blame] | 9771 | /* 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] | 9772 | arg_save = vim_strsave(arg); |
| 9773 | if (arg_save == NULL) |
| 9774 | return FALSE; |
Bram Moolenaar | f4cd3e8 | 2005-12-22 22:47:02 +0000 | [diff] [blame] | 9775 | p = vim_strchr(arg_save, '#'); |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9776 | if (p != NULL) |
| 9777 | *p++ = NUL; |
| 9778 | |
| 9779 | /* First, look for an autocmd group name */ |
| 9780 | group = au_find_group(arg_save); |
| 9781 | if (group == AUGROUP_ERROR) |
| 9782 | { |
| 9783 | /* Didn't match a group name, assume the first argument is an event. */ |
| 9784 | group = AUGROUP_ALL; |
| 9785 | event_name = arg_save; |
| 9786 | } |
| 9787 | else |
| 9788 | { |
| 9789 | if (p == NULL) |
| 9790 | { |
| 9791 | /* "Group": group name is present and it's recognized */ |
| 9792 | retval = TRUE; |
| 9793 | goto theend; |
| 9794 | } |
| 9795 | |
| 9796 | /* Must be "Group#Event" or "Group#Event#pat". */ |
| 9797 | event_name = p; |
| 9798 | p = vim_strchr(event_name, '#'); |
| 9799 | if (p != NULL) |
| 9800 | *p++ = NUL; /* "Group#Event#pat" */ |
| 9801 | } |
| 9802 | |
| 9803 | pattern = p; /* "pattern" is NULL when there is no pattern */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9804 | |
| 9805 | /* find the index (enum) for the event name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9806 | event = event_name2nr(event_name, &p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9807 | |
| 9808 | /* return FALSE if the event name is not recognized */ |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9809 | if (event == NUM_EVENTS) |
| 9810 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9811 | |
| 9812 | /* Find the first autocommand for this event. |
| 9813 | * If there isn't any, return FALSE; |
| 9814 | * If there is one and no pattern given, return TRUE; */ |
| 9815 | ap = first_autopat[(int)event]; |
| 9816 | if (ap == NULL) |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9817 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9818 | |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9819 | /* if pattern is "<buffer>", special handling is needed which uses curbuf */ |
| 9820 | /* for pattern "<buffer=N>, fnamecmp() will work fine */ |
Bram Moolenaar | 5b7880d | 2009-09-11 15:24:31 +0000 | [diff] [blame] | 9821 | if (pattern != NULL && STRICMP(pattern, "<buffer>") == 0) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9822 | buflocal_buf = curbuf; |
| 9823 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9824 | /* Check if there is an autocommand with the given pattern. */ |
| 9825 | for ( ; ap != NULL; ap = ap->next) |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9826 | /* only use a pattern when it has not been removed and has commands. */ |
| 9827 | /* For buffer-local autocommands, fnamecmp() works fine. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9828 | if (ap->pat != NULL && ap->cmds != NULL |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9829 | && (group == AUGROUP_ALL || ap->group == group) |
Bram Moolenaar | 5b7880d | 2009-09-11 15:24:31 +0000 | [diff] [blame] | 9830 | && (pattern == NULL |
| 9831 | || (buflocal_buf == NULL |
| 9832 | ? fnamecmp(ap->pat, pattern) == 0 |
| 9833 | : ap->buflocal_nr == buflocal_buf->b_fnum))) |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9834 | { |
| 9835 | retval = TRUE; |
| 9836 | break; |
| 9837 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9838 | |
Bram Moolenaar | 195d635 | 2005-12-19 22:08:24 +0000 | [diff] [blame] | 9839 | theend: |
| 9840 | vim_free(arg_save); |
| 9841 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9842 | } |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 9843 | |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 9844 | #else /* FEAT_AUTOCMD */ |
| 9845 | |
| 9846 | /* |
| 9847 | * Prepare for executing commands for (hidden) buffer "buf". |
| 9848 | * This is the non-autocommand version, it simply saves "curbuf" and sets |
| 9849 | * "curbuf" and "curwin" to match "buf". |
| 9850 | */ |
| 9851 | void |
| 9852 | aucmd_prepbuf(aco, buf) |
| 9853 | aco_save_T *aco; /* structure to save values in */ |
| 9854 | buf_T *buf; /* new curbuf */ |
| 9855 | { |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 9856 | aco->save_curbuf = curbuf; |
| 9857 | --curbuf->b_nwindows; |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 9858 | curbuf = buf; |
| 9859 | curwin->w_buffer = buf; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 9860 | ++curbuf->b_nwindows; |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 9861 | } |
| 9862 | |
| 9863 | /* |
| 9864 | * Restore after executing commands for a (hidden) buffer. |
| 9865 | * This is the non-autocommand version. |
| 9866 | */ |
| 9867 | void |
| 9868 | aucmd_restbuf(aco) |
| 9869 | aco_save_T *aco; /* structure holding saved values */ |
| 9870 | { |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 9871 | --curbuf->b_nwindows; |
| 9872 | curbuf = aco->save_curbuf; |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 9873 | curwin->w_buffer = curbuf; |
Bram Moolenaar | 746ebd3 | 2009-06-16 14:01:43 +0000 | [diff] [blame] | 9874 | ++curbuf->b_nwindows; |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 9875 | } |
| 9876 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9877 | #endif /* FEAT_AUTOCMD */ |
| 9878 | |
Bram Moolenaar | f30e74c | 2006-08-16 17:35:00 +0000 | [diff] [blame] | 9879 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9880 | #if defined(FEAT_AUTOCMD) || defined(FEAT_WILDIGN) || defined(PROTO) |
| 9881 | /* |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 9882 | * Try matching a filename with a "pattern" ("prog" is NULL), or use the |
| 9883 | * precompiled regprog "prog" ("pattern" is NULL). That avoids calling |
| 9884 | * vim_regcomp() often. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9885 | * Used for autocommands and 'wildignore'. |
| 9886 | * Returns TRUE if there is a match, FALSE otherwise. |
| 9887 | */ |
| 9888 | int |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 9889 | match_file_pat(pattern, prog, fname, sfname, tail, allow_dirs) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9890 | char_u *pattern; /* pattern to match with */ |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 9891 | regprog_T *prog; /* pre-compiled regprog or NULL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9892 | char_u *fname; /* full path of file name */ |
| 9893 | char_u *sfname; /* short file name or NULL */ |
| 9894 | char_u *tail; /* tail of path */ |
| 9895 | int allow_dirs; /* allow matching with dir */ |
| 9896 | { |
| 9897 | regmatch_T regmatch; |
| 9898 | int result = FALSE; |
| 9899 | #ifdef FEAT_OSFILETYPE |
| 9900 | int no_pattern = FALSE; /* TRUE if check is filetype only */ |
| 9901 | char_u *type_start; |
| 9902 | char_u c; |
| 9903 | int match = FALSE; |
| 9904 | #endif |
| 9905 | |
| 9906 | #ifdef CASE_INSENSITIVE_FILENAME |
| 9907 | regmatch.rm_ic = TRUE; /* Always ignore case */ |
| 9908 | #else |
| 9909 | regmatch.rm_ic = FALSE; /* Don't ever ignore case */ |
| 9910 | #endif |
| 9911 | #ifdef FEAT_OSFILETYPE |
| 9912 | if (*pattern == '<') |
| 9913 | { |
| 9914 | /* There is a filetype condition specified with this pattern. |
| 9915 | * Check the filetype matches first. If not, don't bother with the |
| 9916 | * pattern (set regprog to NULL). |
| 9917 | * Always use magic for the regexp. |
| 9918 | */ |
| 9919 | |
| 9920 | for (type_start = pattern + 1; (c = *pattern); pattern++) |
| 9921 | { |
| 9922 | if ((c == ';' || c == '>') && match == FALSE) |
| 9923 | { |
| 9924 | *pattern = NUL; /* Terminate the string */ |
| 9925 | match = mch_check_filetype(fname, type_start); |
| 9926 | *pattern = c; /* Restore the terminator */ |
| 9927 | type_start = pattern + 1; |
| 9928 | } |
| 9929 | if (c == '>') |
| 9930 | break; |
| 9931 | } |
| 9932 | |
| 9933 | /* (c should never be NUL, but check anyway) */ |
| 9934 | if (match == FALSE || c == NUL) |
| 9935 | regmatch.regprog = NULL; /* Doesn't match - don't check pat. */ |
| 9936 | else if (*pattern == NUL) |
| 9937 | { |
| 9938 | regmatch.regprog = NULL; /* Vim will try to free regprog later */ |
| 9939 | no_pattern = TRUE; /* Always matches - don't check pat. */ |
| 9940 | } |
| 9941 | else |
| 9942 | regmatch.regprog = vim_regcomp(pattern + 1, RE_MAGIC); |
| 9943 | } |
| 9944 | else |
| 9945 | #endif |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 9946 | { |
| 9947 | if (prog != NULL) |
| 9948 | regmatch.regprog = prog; |
| 9949 | else |
| 9950 | regmatch.regprog = vim_regcomp(pattern, RE_MAGIC); |
| 9951 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9952 | |
| 9953 | /* |
| 9954 | * Try for a match with the pattern with: |
| 9955 | * 1. the full file name, when the pattern has a '/'. |
| 9956 | * 2. the short file name, when the pattern has a '/'. |
| 9957 | * 3. the tail of the file name, when the pattern has no '/'. |
| 9958 | */ |
| 9959 | if ( |
| 9960 | #ifdef FEAT_OSFILETYPE |
| 9961 | /* If the check is for a filetype only and we don't care |
| 9962 | * about the path then skip all the regexp stuff. |
| 9963 | */ |
| 9964 | no_pattern || |
| 9965 | #endif |
| 9966 | (regmatch.regprog != NULL |
| 9967 | && ((allow_dirs |
| 9968 | && (vim_regexec(®match, fname, (colnr_T)0) |
| 9969 | || (sfname != NULL |
| 9970 | && vim_regexec(®match, sfname, (colnr_T)0)))) |
| 9971 | || (!allow_dirs && vim_regexec(®match, tail, (colnr_T)0))))) |
| 9972 | result = TRUE; |
| 9973 | |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 9974 | if (prog == NULL) |
| 9975 | vim_free(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9976 | return result; |
| 9977 | } |
| 9978 | #endif |
| 9979 | |
| 9980 | #if defined(FEAT_WILDIGN) || defined(PROTO) |
| 9981 | /* |
| 9982 | * Return TRUE if a file matches with a pattern in "list". |
| 9983 | * "list" is a comma-separated list of patterns, like 'wildignore'. |
| 9984 | * "sfname" is the short file name or NULL, "ffname" the long file name. |
| 9985 | */ |
| 9986 | int |
| 9987 | match_file_list(list, sfname, ffname) |
| 9988 | char_u *list; |
| 9989 | char_u *sfname; |
| 9990 | char_u *ffname; |
| 9991 | { |
| 9992 | char_u buf[100]; |
| 9993 | char_u *tail; |
| 9994 | char_u *regpat; |
| 9995 | char allow_dirs; |
| 9996 | int match; |
| 9997 | char_u *p; |
| 9998 | |
| 9999 | tail = gettail(sfname); |
| 10000 | |
| 10001 | /* try all patterns in 'wildignore' */ |
| 10002 | p = list; |
| 10003 | while (*p) |
| 10004 | { |
| 10005 | copy_option_part(&p, buf, 100, ","); |
| 10006 | regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE); |
| 10007 | if (regpat == NULL) |
| 10008 | break; |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 10009 | match = match_file_pat(regpat, NULL, ffname, sfname, |
| 10010 | tail, (int)allow_dirs); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10011 | vim_free(regpat); |
| 10012 | if (match) |
| 10013 | return TRUE; |
| 10014 | } |
| 10015 | return FALSE; |
| 10016 | } |
| 10017 | #endif |
| 10018 | |
| 10019 | /* |
| 10020 | * Convert the given pattern "pat" which has shell style wildcards in it, into |
| 10021 | * a regular expression, and return the result in allocated memory. If there |
| 10022 | * is a directory path separator to be matched, then TRUE is put in |
| 10023 | * allow_dirs, otherwise FALSE is put there -- webb. |
| 10024 | * Handle backslashes before special characters, like "\*" and "\ ". |
| 10025 | * |
| 10026 | * If FEAT_OSFILETYPE defined then pass initial <type> through unchanged. Eg: |
| 10027 | * '<html>myfile' becomes '<html>^myfile$' -- leonard. |
| 10028 | * |
| 10029 | * Returns NULL when out of memory. |
| 10030 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10031 | char_u * |
| 10032 | file_pat_to_reg_pat(pat, pat_end, allow_dirs, no_bslash) |
| 10033 | char_u *pat; |
| 10034 | char_u *pat_end; /* first char after pattern or NULL */ |
| 10035 | char *allow_dirs; /* Result passed back out in here */ |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 10036 | int no_bslash UNUSED; /* Don't use a backward slash as pathsep */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10037 | { |
| 10038 | int size; |
| 10039 | char_u *endp; |
| 10040 | char_u *reg_pat; |
| 10041 | char_u *p; |
| 10042 | int i; |
| 10043 | int nested = 0; |
| 10044 | int add_dollar = TRUE; |
| 10045 | #ifdef FEAT_OSFILETYPE |
| 10046 | int check_length = 0; |
| 10047 | #endif |
| 10048 | |
| 10049 | if (allow_dirs != NULL) |
| 10050 | *allow_dirs = FALSE; |
| 10051 | if (pat_end == NULL) |
| 10052 | pat_end = pat + STRLEN(pat); |
| 10053 | |
| 10054 | #ifdef FEAT_OSFILETYPE |
| 10055 | /* Find out how much of the string is the filetype check */ |
| 10056 | if (*pat == '<') |
| 10057 | { |
| 10058 | /* Count chars until the next '>' */ |
| 10059 | for (p = pat + 1; p < pat_end && *p != '>'; p++) |
| 10060 | ; |
| 10061 | if (p < pat_end) |
| 10062 | { |
| 10063 | /* Pattern is of the form <.*>.* */ |
| 10064 | check_length = p - pat + 1; |
| 10065 | if (p + 1 >= pat_end) |
| 10066 | { |
| 10067 | /* The 'pattern' is a filetype check ONLY */ |
| 10068 | reg_pat = (char_u *)alloc(check_length + 1); |
| 10069 | if (reg_pat != NULL) |
| 10070 | { |
| 10071 | mch_memmove(reg_pat, pat, (size_t)check_length); |
| 10072 | reg_pat[check_length] = NUL; |
| 10073 | } |
| 10074 | return reg_pat; |
| 10075 | } |
| 10076 | } |
| 10077 | /* else: there was no closing '>' - assume it was a normal pattern */ |
| 10078 | |
| 10079 | } |
| 10080 | pat += check_length; |
| 10081 | size = 2 + check_length; |
| 10082 | #else |
| 10083 | size = 2; /* '^' at start, '$' at end */ |
| 10084 | #endif |
| 10085 | |
| 10086 | for (p = pat; p < pat_end; p++) |
| 10087 | { |
| 10088 | switch (*p) |
| 10089 | { |
| 10090 | case '*': |
| 10091 | case '.': |
| 10092 | case ',': |
| 10093 | case '{': |
| 10094 | case '}': |
| 10095 | case '~': |
| 10096 | size += 2; /* extra backslash */ |
| 10097 | break; |
| 10098 | #ifdef BACKSLASH_IN_FILENAME |
| 10099 | case '\\': |
| 10100 | case '/': |
| 10101 | size += 4; /* could become "[\/]" */ |
| 10102 | break; |
| 10103 | #endif |
| 10104 | default: |
| 10105 | size++; |
| 10106 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10107 | if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10108 | { |
| 10109 | ++p; |
| 10110 | ++size; |
| 10111 | } |
| 10112 | # endif |
| 10113 | break; |
| 10114 | } |
| 10115 | } |
| 10116 | reg_pat = alloc(size + 1); |
| 10117 | if (reg_pat == NULL) |
| 10118 | return NULL; |
| 10119 | |
| 10120 | #ifdef FEAT_OSFILETYPE |
| 10121 | /* Copy the type check in to the start. */ |
| 10122 | if (check_length) |
| 10123 | mch_memmove(reg_pat, pat - check_length, (size_t)check_length); |
| 10124 | i = check_length; |
| 10125 | #else |
| 10126 | i = 0; |
| 10127 | #endif |
| 10128 | |
| 10129 | if (pat[0] == '*') |
| 10130 | while (pat[0] == '*' && pat < pat_end - 1) |
| 10131 | pat++; |
| 10132 | else |
| 10133 | reg_pat[i++] = '^'; |
| 10134 | endp = pat_end - 1; |
| 10135 | if (*endp == '*') |
| 10136 | { |
| 10137 | while (endp - pat > 0 && *endp == '*') |
| 10138 | endp--; |
| 10139 | add_dollar = FALSE; |
| 10140 | } |
| 10141 | for (p = pat; *p && nested >= 0 && p <= endp; p++) |
| 10142 | { |
| 10143 | switch (*p) |
| 10144 | { |
| 10145 | case '*': |
| 10146 | reg_pat[i++] = '.'; |
| 10147 | reg_pat[i++] = '*'; |
Bram Moolenaar | 0274363 | 2005-07-25 20:42:36 +0000 | [diff] [blame] | 10148 | while (p[1] == '*') /* "**" matches like "*" */ |
| 10149 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10150 | break; |
| 10151 | case '.': |
| 10152 | #ifdef RISCOS |
| 10153 | if (allow_dirs != NULL) |
| 10154 | *allow_dirs = TRUE; |
| 10155 | /* FALLTHROUGH */ |
| 10156 | #endif |
| 10157 | case '~': |
| 10158 | reg_pat[i++] = '\\'; |
| 10159 | reg_pat[i++] = *p; |
| 10160 | break; |
| 10161 | case '?': |
| 10162 | #ifdef RISCOS |
| 10163 | case '#': |
| 10164 | #endif |
| 10165 | reg_pat[i++] = '.'; |
| 10166 | break; |
| 10167 | case '\\': |
| 10168 | if (p[1] == NUL) |
| 10169 | break; |
| 10170 | #ifdef BACKSLASH_IN_FILENAME |
| 10171 | if (!no_bslash) |
| 10172 | { |
| 10173 | /* translate: |
| 10174 | * "\x" to "\\x" e.g., "dir\file" |
| 10175 | * "\*" to "\\.*" e.g., "dir\*.c" |
| 10176 | * "\?" to "\\." e.g., "dir\??.c" |
| 10177 | * "\+" to "\+" e.g., "fileX\+.c" |
| 10178 | */ |
| 10179 | if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?') |
| 10180 | && p[1] != '+') |
| 10181 | { |
| 10182 | reg_pat[i++] = '['; |
| 10183 | reg_pat[i++] = '\\'; |
| 10184 | reg_pat[i++] = '/'; |
| 10185 | reg_pat[i++] = ']'; |
| 10186 | if (allow_dirs != NULL) |
| 10187 | *allow_dirs = TRUE; |
| 10188 | break; |
| 10189 | } |
| 10190 | } |
| 10191 | #endif |
| 10192 | if (*++p == '?' |
| 10193 | #ifdef BACKSLASH_IN_FILENAME |
| 10194 | && no_bslash |
| 10195 | #endif |
| 10196 | ) |
| 10197 | reg_pat[i++] = '?'; |
| 10198 | else |
| 10199 | if (*p == ',') |
| 10200 | reg_pat[i++] = ','; |
| 10201 | else |
| 10202 | { |
| 10203 | if (allow_dirs != NULL && vim_ispathsep(*p) |
| 10204 | #ifdef BACKSLASH_IN_FILENAME |
| 10205 | && (!no_bslash || *p != '\\') |
| 10206 | #endif |
| 10207 | ) |
| 10208 | *allow_dirs = TRUE; |
| 10209 | reg_pat[i++] = '\\'; |
| 10210 | reg_pat[i++] = *p; |
| 10211 | } |
| 10212 | break; |
| 10213 | #ifdef BACKSLASH_IN_FILENAME |
| 10214 | case '/': |
| 10215 | reg_pat[i++] = '['; |
| 10216 | reg_pat[i++] = '\\'; |
| 10217 | reg_pat[i++] = '/'; |
| 10218 | reg_pat[i++] = ']'; |
| 10219 | if (allow_dirs != NULL) |
| 10220 | *allow_dirs = TRUE; |
| 10221 | break; |
| 10222 | #endif |
| 10223 | case '{': |
| 10224 | reg_pat[i++] = '\\'; |
| 10225 | reg_pat[i++] = '('; |
| 10226 | nested++; |
| 10227 | break; |
| 10228 | case '}': |
| 10229 | reg_pat[i++] = '\\'; |
| 10230 | reg_pat[i++] = ')'; |
| 10231 | --nested; |
| 10232 | break; |
| 10233 | case ',': |
| 10234 | if (nested) |
| 10235 | { |
| 10236 | reg_pat[i++] = '\\'; |
| 10237 | reg_pat[i++] = '|'; |
| 10238 | } |
| 10239 | else |
| 10240 | reg_pat[i++] = ','; |
| 10241 | break; |
| 10242 | default: |
| 10243 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10244 | if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10245 | reg_pat[i++] = *p++; |
| 10246 | else |
| 10247 | # endif |
| 10248 | if (allow_dirs != NULL && vim_ispathsep(*p)) |
| 10249 | *allow_dirs = TRUE; |
| 10250 | reg_pat[i++] = *p; |
| 10251 | break; |
| 10252 | } |
| 10253 | } |
| 10254 | if (add_dollar) |
| 10255 | reg_pat[i++] = '$'; |
| 10256 | reg_pat[i] = NUL; |
| 10257 | if (nested != 0) |
| 10258 | { |
| 10259 | if (nested < 0) |
| 10260 | EMSG(_("E219: Missing {.")); |
| 10261 | else |
| 10262 | EMSG(_("E220: Missing }.")); |
| 10263 | vim_free(reg_pat); |
| 10264 | reg_pat = NULL; |
| 10265 | } |
| 10266 | return reg_pat; |
| 10267 | } |