Mercurial > vim
annotate src/fileio.c @ 14475:dddba3937532 v8.1.0251
patch 8.1.0251: using full path is not supported for 'backupdir'
commit https://github.com/vim/vim/commit/b782ba475a3f8f2b0be99dda164ba4545347f60f
Author: Bram Moolenaar <Bram@vim.org>
Date: Tue Aug 7 21:39:28 2018 +0200
patch 8.1.0251: using full path is not supported for 'backupdir'
Problem: Using a full path is supported for 'directory' but not for
'backupdir'. (Mikolaj Machowski)
Solution: Support 'backupdir' as well. (Christian Brabandt, closes #179)
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Tue, 07 Aug 2018 21:45:05 +0200 |
parents | 4a94173743d9 |
children | 80f715651c4c |
rev | line source |
---|---|
10042
4aead6a9b7a9
commit https://github.com/vim/vim/commit/edf3f97ae2af024708ebb4ac614227327033ca47
Christian Brabandt <cb@256bit.org>
parents:
9911
diff
changeset
|
1 /* vi:set ts=8 sts=4 sw=4 noet: |
7 | 2 * |
3 * VIM - Vi IMproved by Bram Moolenaar | |
4 * | |
5 * Do ":help uganda" in Vim to read copying and usage conditions. | |
6 * Do ":help credits" in Vim to see a list of people who contributed. | |
7 * See README.txt for an overview of the Vim source code. | |
8 */ | |
9 | |
10 /* | |
11 * fileio.c: read from and write to a file | |
12 */ | |
13 | |
14 #include "vim.h" | |
15 | |
2014 | 16 #if defined(__TANDEM) || defined(__MINT__) |
7 | 17 # include <limits.h> /* for SSIZE_MAX */ |
18 #endif | |
19 | |
20 #if defined(HAVE_UTIME) && defined(HAVE_UTIME_H) | |
21 # include <utime.h> /* for struct utimbuf */ | |
22 #endif | |
23 | |
24 #define BUFSIZE 8192 /* size of normal write buffer */ | |
25 #define SMBUFSIZE 256 /* size of emergency write buffer */ | |
26 | |
27 /* Is there any system that doesn't have access()? */ | |
574 | 28 #define USE_MCH_ACCESS |
7 | 29 |
30 #ifdef FEAT_MBYTE | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
31 static char_u *next_fenc(char_u **pp); |
7 | 32 # ifdef FEAT_EVAL |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
33 static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp); |
7 | 34 # endif |
35 #endif | |
36 #ifdef FEAT_VIMINFO | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
37 static void check_marks_read(void); |
7 | 38 #endif |
39 #ifdef FEAT_CRYPT | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
40 static char_u *check_for_cryptkey(char_u *cryptkey, char_u *ptr, long *sizep, off_T *filesizep, int newfile, char_u *fname, int *did_ask); |
7 | 41 #endif |
42 #ifdef UNIX | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
43 static void set_file_time(char_u *fname, time_t atime, time_t mtime); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
44 #endif |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
45 static int set_rw_fname(char_u *fname, char_u *sfname); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
46 static int msg_add_fileformat(int eol_type); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
47 static void msg_add_eol(void); |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
48 static int check_mtime(buf_T *buf, stat_T *s); |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
49 static int time_differs(long t1, long t2); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
50 static int apply_autocmds_exarg(event_T event, char_u *fname, char_u *fname_io, int force, buf_T *buf, exarg_T *eap); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
51 static int au_find_group(char_u *name); |
676 | 52 |
53 # define AUGROUP_DEFAULT -1 /* default autocmd group */ | |
1834 | 54 # define AUGROUP_ERROR -2 /* erroneous autocmd group */ |
676 | 55 # define AUGROUP_ALL -3 /* all autocmd groups */ |
7 | 56 |
57 #if defined(FEAT_CRYPT) || defined(FEAT_MBYTE) | |
58 # define HAS_BW_FLAGS | |
59 # define FIO_LATIN1 0x01 /* convert Latin1 */ | |
60 # define FIO_UTF8 0x02 /* convert UTF-8 */ | |
61 # define FIO_UCS2 0x04 /* convert UCS-2 */ | |
62 # define FIO_UCS4 0x08 /* convert UCS-4 */ | |
63 # define FIO_UTF16 0x10 /* convert UTF-16 */ | |
64 # ifdef WIN3264 | |
65 # define FIO_CODEPAGE 0x20 /* convert MS-Windows codepage */ | |
66 # define FIO_PUT_CP(x) (((x) & 0xffff) << 16) /* put codepage in top word */ | |
67 # define FIO_GET_CP(x) (((x)>>16) & 0xffff) /* get codepage from top word */ | |
68 # endif | |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
69 # ifdef MACOS_CONVERT |
7 | 70 # define FIO_MACROMAN 0x20 /* convert MacRoman */ |
71 # endif | |
72 # define FIO_ENDIAN_L 0x80 /* little endian */ | |
73 # define FIO_ENCRYPTED 0x1000 /* encrypt written bytes */ | |
74 # define FIO_NOCONVERT 0x2000 /* skip encoding conversion */ | |
75 # define FIO_UCSBOM 0x4000 /* check for BOM at start of file */ | |
76 # define FIO_ALL -1 /* allow all formats */ | |
77 #endif | |
78 | |
79 /* When converting, a read() or write() may leave some bytes to be converted | |
80 * for the next call. The value is guessed... */ | |
81 #define CONV_RESTLEN 30 | |
82 | |
83 /* We have to guess how much a sequence of bytes may expand when converting | |
84 * with iconv() to be able to allocate a buffer. */ | |
85 #define ICONV_MULT 8 | |
86 | |
87 /* | |
88 * Structure to pass arguments from buf_write() to buf_write_bytes(). | |
89 */ | |
90 struct bw_info | |
91 { | |
92 int bw_fd; /* file descriptor */ | |
93 char_u *bw_buf; /* buffer with data to be written */ | |
1411 | 94 int bw_len; /* length of data */ |
7 | 95 #ifdef HAS_BW_FLAGS |
96 int bw_flags; /* FIO_ flags */ | |
97 #endif | |
6122 | 98 #ifdef FEAT_CRYPT |
99 buf_T *bw_buffer; /* buffer being written */ | |
100 #endif | |
7 | 101 #ifdef FEAT_MBYTE |
102 char_u bw_rest[CONV_RESTLEN]; /* not converted bytes */ | |
103 int bw_restlen; /* nr of bytes in bw_rest[] */ | |
104 int bw_first; /* first write call */ | |
105 char_u *bw_conv_buf; /* buffer for writing converted chars */ | |
106 int bw_conv_buflen; /* size of bw_conv_buf */ | |
107 int bw_conv_error; /* set for conversion error */ | |
1947 | 108 linenr_T bw_conv_error_lnum; /* first line with error or zero */ |
109 linenr_T bw_start_lnum; /* line number at start of buffer */ | |
7 | 110 # ifdef USE_ICONV |
111 iconv_t bw_iconv_fd; /* descriptor for iconv() or -1 */ | |
112 # endif | |
113 #endif | |
114 }; | |
115 | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
116 static int buf_write_bytes(struct bw_info *ip); |
7 | 117 |
118 #ifdef FEAT_MBYTE | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
119 static linenr_T readfile_linenr(linenr_T linecnt, char_u *p, char_u *endp); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
120 static int ucs2bytes(unsigned c, char_u **pp, int flags); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
121 static int need_conversion(char_u *fenc); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
122 static int get_fio_flags(char_u *ptr); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
123 static char_u *check_for_bom(char_u *p, long size, int *lenp, int flags); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
124 static int make_bom(char_u *buf, char_u *name); |
7 | 125 # ifdef WIN3264 |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
126 static int get_win_fio_flags(char_u *ptr); |
7 | 127 # endif |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
128 # ifdef MACOS_CONVERT |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
129 static int get_mac_fio_flags(char_u *ptr); |
7 | 130 # endif |
131 #endif | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
132 static int move_lines(buf_T *frombuf, buf_T *tobuf); |
2006 | 133 #ifdef TEMPDIRNAMES |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
134 static void vim_settempdir(char_u *tempdir); |
2006 | 135 #endif |
1834 | 136 static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name"); |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
137 |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
138 /* |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
139 * Set by the apply_autocmds_group function if the given event is equal to |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
140 * EVENT_FILETYPE. Used by the readfile function in order to determine if |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
141 * EVENT_BUFREADPOST triggered the EVENT_FILETYPE. |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
142 * |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
143 * Relying on this value requires one to reset it prior calling |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
144 * apply_autocmds_group. |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
145 */ |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
146 static int au_did_filetype INIT(= FALSE); |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
147 |
7 | 148 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
149 filemess( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
150 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
151 char_u *name, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
152 char_u *s, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
153 int attr) |
7 | 154 { |
155 int msg_scroll_save; | |
156 | |
157 if (msg_silent != 0) | |
158 return; | |
159 msg_add_fname(buf, name); /* put file name in IObuff with quotes */ | |
160 /* If it's extremely long, truncate it. */ | |
161 if (STRLEN(IObuff) > IOSIZE - 80) | |
162 IObuff[IOSIZE - 80] = NUL; | |
163 STRCAT(IObuff, s); | |
164 /* | |
165 * For the first message may have to start a new line. | |
166 * For further ones overwrite the previous one, reset msg_scroll before | |
167 * calling filemess(). | |
168 */ | |
169 msg_scroll_save = msg_scroll; | |
170 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) | |
171 msg_scroll = FALSE; | |
172 if (!msg_scroll) /* wait a bit when overwriting an error msg */ | |
173 check_for_delay(FALSE); | |
174 msg_start(); | |
175 msg_scroll = msg_scroll_save; | |
176 msg_scrolled_ign = TRUE; | |
177 /* may truncate the message to avoid a hit-return prompt */ | |
178 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr); | |
179 msg_clr_eos(); | |
180 out_flush(); | |
181 msg_scrolled_ign = FALSE; | |
182 } | |
183 | |
184 /* | |
185 * Read lines from file "fname" into the buffer after line "from". | |
186 * | |
187 * 1. We allocate blocks with lalloc, as big as possible. | |
188 * 2. Each block is filled with characters from the file with a single read(). | |
189 * 3. The lines are inserted in the buffer with ml_append(). | |
190 * | |
191 * (caller must check that fname != NULL, unless READ_STDIN is used) | |
192 * | |
193 * "lines_to_skip" is the number of lines that must be skipped | |
194 * "lines_to_read" is the number of lines that are appended | |
195 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM. | |
196 * | |
197 * flags: | |
198 * READ_NEW starting to edit a new buffer | |
199 * READ_FILTER reading filter output | |
200 * READ_STDIN read from stdin instead of a file | |
201 * READ_BUFFER read from curbuf instead of a file (converting after reading | |
202 * stdin) | |
203 * READ_DUMMY read into a dummy buffer (to check if file contents changed) | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
204 * READ_KEEP_UNDO don't clear undo info or read it from a file |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
205 * READ_FIFO read from fifo/socket instead of a file |
7 | 206 * |
10575
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
207 * return FAIL for failure, NOTDONE for directory (failure), or OK |
7 | 208 */ |
209 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
210 readfile( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
211 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
212 char_u *sfname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
213 linenr_T from, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
214 linenr_T lines_to_skip, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
215 linenr_T lines_to_read, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
216 exarg_T *eap, /* can be NULL! */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
217 int flags) |
7 | 218 { |
219 int fd = 0; | |
220 int newfile = (flags & READ_NEW); | |
221 int check_readonly; | |
222 int filtering = (flags & READ_FILTER); | |
223 int read_stdin = (flags & READ_STDIN); | |
224 int read_buffer = (flags & READ_BUFFER); | |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
225 int read_fifo = (flags & READ_FIFO); |
1486 | 226 int set_options = newfile || read_buffer |
227 || (eap != NULL && eap->read_edit); | |
7 | 228 linenr_T read_buf_lnum = 1; /* next line to read from curbuf */ |
229 colnr_T read_buf_col = 0; /* next char to read from this line */ | |
230 char_u c; | |
231 linenr_T lnum = from; | |
232 char_u *ptr = NULL; /* pointer into read buffer */ | |
233 char_u *buffer = NULL; /* read buffer */ | |
234 char_u *new_buffer = NULL; /* init to shut up gcc */ | |
235 char_u *line_start = NULL; /* init to shut up gcc */ | |
236 int wasempty; /* buffer was empty before reading */ | |
237 colnr_T len; | |
238 long size = 0; | |
239 char_u *p; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
240 off_T filesize = 0; |
7 | 241 int skip_read = FALSE; |
242 #ifdef FEAT_CRYPT | |
243 char_u *cryptkey = NULL; | |
2204
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
244 int did_ask_for_key = FALSE; |
7 | 245 #endif |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
246 #ifdef FEAT_PERSISTENT_UNDO |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
247 context_sha256_T sha_ctx; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
248 int read_undo_file = FALSE; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
249 #endif |
7 | 250 int split = 0; /* number of split lines */ |
251 #define UNKNOWN 0x0fffffff /* file size is unknown */ | |
252 linenr_T linecnt; | |
253 int error = FALSE; /* errors encountered */ | |
254 int ff_error = EOL_UNKNOWN; /* file format with errors */ | |
255 long linerest = 0; /* remaining chars in line */ | |
256 #ifdef UNIX | |
257 int perm = 0; | |
258 int swap_mode = -1; /* protection bits for swap file */ | |
259 #else | |
260 int perm; | |
261 #endif | |
262 int fileformat = 0; /* end-of-line format */ | |
263 int keep_fileformat = FALSE; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
264 stat_T st; |
7 | 265 int file_readonly; |
266 linenr_T skip_count = 0; | |
267 linenr_T read_count = 0; | |
268 int msg_save = msg_scroll; | |
269 linenr_T read_no_eol_lnum = 0; /* non-zero lnum when last line of | |
270 * last read was missing the eol */ | |
10668
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
271 int try_mac; |
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
272 int try_dos; |
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
273 int try_unix; |
7 | 274 int file_rewind = FALSE; |
275 #ifdef FEAT_MBYTE | |
276 int can_retry; | |
595 | 277 linenr_T conv_error = 0; /* line nr with conversion error */ |
278 linenr_T illegal_byte = 0; /* line nr with illegal byte */ | |
7 | 279 int keep_dest_enc = FALSE; /* don't retry when char doesn't fit |
280 in destination encoding */ | |
595 | 281 int bad_char_behavior = BAD_REPLACE; |
282 /* BAD_KEEP, BAD_DROP or character to | |
283 * replace with */ | |
7 | 284 char_u *tmpname = NULL; /* name of 'charconvert' output file */ |
285 int fio_flags = 0; | |
286 char_u *fenc; /* fileencoding to use */ | |
287 int fenc_alloced; /* fenc_next is in allocated memory */ | |
288 char_u *fenc_next = NULL; /* next item in 'fencs' or NULL */ | |
289 int advance_fenc = FALSE; | |
290 long real_size = 0; | |
291 # ifdef USE_ICONV | |
292 iconv_t iconv_fd = (iconv_t)-1; /* descriptor for iconv() or -1 */ | |
293 # ifdef FEAT_EVAL | |
294 int did_iconv = FALSE; /* TRUE when iconv() failed and trying | |
295 'charconvert' next */ | |
296 # endif | |
297 # endif | |
298 int converted = FALSE; /* TRUE if conversion done */ | |
299 int notconverted = FALSE; /* TRUE if conversion wanted but it | |
300 wasn't possible */ | |
301 char_u conv_rest[CONV_RESTLEN]; | |
302 int conv_restlen = 0; /* nr of bytes in conv_rest[] */ | |
303 #endif | |
2563
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
304 buf_T *old_curbuf; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
305 char_u *old_b_ffname; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
306 char_u *old_b_fname; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
307 int using_b_ffname; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
308 int using_b_fname; |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
309 |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
310 au_did_filetype = FALSE; /* reset before triggering any autocommands */ |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
311 |
2707 | 312 curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */ |
7 | 313 |
314 /* | |
315 * If there is no file name yet, use the one for the read file. | |
316 * BF_NOTEDITED is set to reflect this. | |
317 * Don't do this for a read from a filter. | |
318 * Only do this when 'cpoptions' contains the 'f' flag. | |
319 */ | |
320 if (curbuf->b_ffname == NULL | |
321 && !filtering | |
322 && fname != NULL | |
323 && vim_strchr(p_cpo, CPO_FNAMER) != NULL | |
324 && !(flags & READ_DUMMY)) | |
325 { | |
633 | 326 if (set_rw_fname(fname, sfname) == FAIL) |
327 return FAIL; | |
7 | 328 } |
329 | |
2563
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
330 /* Remember the initial values of curbuf, curbuf->b_ffname and |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
331 * curbuf->b_fname to detect whether they are altered as a result of |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
332 * executing nasty autocommands. Also check if "fname" and "sfname" |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
333 * point to one of these values. */ |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
334 old_curbuf = curbuf; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
335 old_b_ffname = curbuf->b_ffname; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
336 old_b_fname = curbuf->b_fname; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
337 using_b_ffname = (fname == curbuf->b_ffname) |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
338 || (sfname == curbuf->b_ffname); |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
339 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname); |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
340 |
167 | 341 /* After reading a file the cursor line changes but we don't want to |
342 * display the line. */ | |
343 ex_no_reprint = TRUE; | |
344 | |
968 | 345 /* don't display the file info for another buffer now */ |
346 need_fileinfo = FALSE; | |
347 | |
7 | 348 /* |
349 * For Unix: Use the short file name whenever possible. | |
350 * Avoids problems with networks and when directory names are changed. | |
351 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to | |
352 * another directory, which we don't detect. | |
353 */ | |
354 if (sfname == NULL) | |
355 sfname = fname; | |
9536
b2aada04d84e
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Christian Brabandt <cb@256bit.org>
parents:
9509
diff
changeset
|
356 #if defined(UNIX) |
7 | 357 fname = sfname; |
358 #endif | |
359 | |
360 /* | |
361 * The BufReadCmd and FileReadCmd events intercept the reading process by | |
362 * executing the associated commands instead. | |
363 */ | |
364 if (!filtering && !read_stdin && !read_buffer) | |
365 { | |
366 pos_T pos; | |
367 | |
368 pos = curbuf->b_op_start; | |
369 | |
370 /* Set '[ mark to the line above where the lines go (line 1 if zero). */ | |
371 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); | |
372 curbuf->b_op_start.col = 0; | |
373 | |
374 if (newfile) | |
375 { | |
376 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname, | |
377 FALSE, curbuf, eap)) | |
378 #ifdef FEAT_EVAL | |
379 return aborting() ? FAIL : OK; | |
380 #else | |
381 return OK; | |
382 #endif | |
383 } | |
384 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname, | |
385 FALSE, NULL, eap)) | |
386 #ifdef FEAT_EVAL | |
387 return aborting() ? FAIL : OK; | |
388 #else | |
389 return OK; | |
390 #endif | |
391 | |
392 curbuf->b_op_start = pos; | |
393 } | |
394 | |
395 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0) | |
396 msg_scroll = FALSE; /* overwrite previous file message */ | |
397 else | |
398 msg_scroll = TRUE; /* don't overwrite previous file message */ | |
399 | |
400 /* | |
401 * If the name ends in a path separator, we can't open it. Check here, | |
402 * because reading the file may actually work, but then creating the swap | |
403 * file may destroy it! Reported on MS-DOS and Win 95. | |
404 * If the name is too long we might crash further on, quit here. | |
405 */ | |
23 | 406 if (fname != NULL && *fname != NUL) |
407 { | |
39 | 408 p = fname + STRLEN(fname); |
409 if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL) | |
23 | 410 { |
411 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0); | |
412 msg_end(); | |
413 msg_scroll = msg_save; | |
414 return FAIL; | |
415 } | |
7 | 416 } |
417 | |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
418 if (!read_stdin && !read_buffer && !read_fifo) |
5322 | 419 { |
7 | 420 #ifdef UNIX |
5322 | 421 /* |
422 * On Unix it is possible to read a directory, so we have to | |
423 * check for it before the mch_open(). | |
424 */ | |
7 | 425 perm = mch_getperm(fname); |
426 if (perm >= 0 && !S_ISREG(perm) /* not a regular file ... */ | |
427 # ifdef S_ISFIFO | |
428 && !S_ISFIFO(perm) /* ... or fifo */ | |
429 # endif | |
430 # ifdef S_ISSOCK | |
431 && !S_ISSOCK(perm) /* ... or socket */ | |
432 # endif | |
1313 | 433 # ifdef OPEN_CHR_FILES |
434 && !(S_ISCHR(perm) && is_dev_fd_file(fname)) | |
435 /* ... or a character special file named /dev/fd/<n> */ | |
436 # endif | |
7 | 437 ) |
438 { | |
10575
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
439 int retval = FAIL; |
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
440 |
7 | 441 if (S_ISDIR(perm)) |
10575
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
442 { |
7 | 443 filemess(curbuf, fname, (char_u *)_("is a directory"), 0); |
10575
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
444 retval = NOTDONE; |
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
445 } |
7 | 446 else |
447 filemess(curbuf, fname, (char_u *)_("is not a file"), 0); | |
448 msg_end(); | |
449 msg_scroll = msg_save; | |
10575
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
450 return retval; |
7 | 451 } |
5322 | 452 #endif |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
453 #if defined(MSWIN) |
1006 | 454 /* |
455 * MS-Windows allows opening a device, but we will probably get stuck | |
456 * trying to read it. | |
457 */ | |
458 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE) | |
459 { | |
1303 | 460 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0); |
1006 | 461 msg_end(); |
462 msg_scroll = msg_save; | |
463 return FAIL; | |
464 } | |
5322 | 465 #endif |
466 } | |
1004 | 467 |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
468 /* Set default or forced 'fileformat' and 'binary'. */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
469 set_file_options(set_options, eap); |
7 | 470 |
471 /* | |
472 * When opening a new file we take the readonly flag from the file. | |
473 * Default is r/w, can be set to r/o below. | |
474 * Don't reset it when in readonly mode | |
475 * Only set/reset b_p_ro when BF_CHECK_RO is set. | |
476 */ | |
477 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO)); | |
164 | 478 if (check_readonly && !readonlymode) |
7 | 479 curbuf->b_p_ro = FALSE; |
480 | |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
481 if (newfile && !read_stdin && !read_buffer && !read_fifo) |
7 | 482 { |
2823 | 483 /* Remember time of file. */ |
7 | 484 if (mch_stat((char *)fname, &st) >= 0) |
485 { | |
486 buf_store_time(curbuf, &st, fname); | |
487 curbuf->b_mtime_read = curbuf->b_mtime; | |
488 #ifdef UNIX | |
489 /* | |
490 * Use the protection bits of the original file for the swap file. | |
491 * This makes it possible for others to read the name of the | |
492 * edited file from the swapfile, but only if they can read the | |
493 * edited file. | |
494 * Remove the "write" and "execute" bits for group and others | |
495 * (they must not write the swapfile). | |
496 * Add the "read" and "write" bits for the user, otherwise we may | |
497 * not be able to write to the file ourselves. | |
498 * Setting the bits is done below, after creating the swap file. | |
499 */ | |
500 swap_mode = (st.st_mode & 0644) | 0600; | |
501 #endif | |
502 #ifdef FEAT_CW_EDITOR | |
503 /* Get the FSSpec on MacOS | |
504 * TODO: Update it properly when the buffer name changes | |
505 */ | |
506 (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec); | |
507 #endif | |
508 #ifdef VMS | |
509 curbuf->b_fab_rfm = st.st_fab_rfm; | |
22 | 510 curbuf->b_fab_rat = st.st_fab_rat; |
511 curbuf->b_fab_mrs = st.st_fab_mrs; | |
7 | 512 #endif |
513 } | |
514 else | |
515 { | |
516 curbuf->b_mtime = 0; | |
517 curbuf->b_mtime_read = 0; | |
518 curbuf->b_orig_size = 0; | |
519 curbuf->b_orig_mode = 0; | |
520 } | |
521 | |
522 /* Reset the "new file" flag. It will be set again below when the | |
523 * file doesn't exist. */ | |
524 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W); | |
525 } | |
526 | |
527 /* | |
528 * for UNIX: check readonly with perm and mch_access() | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
529 * for Amiga: check readonly by trying to open the file for writing |
7 | 530 */ |
531 file_readonly = FALSE; | |
532 if (read_stdin) | |
533 { | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
534 #if defined(MSWIN) |
7 | 535 /* Force binary I/O on stdin to avoid CR-LF -> LF conversion. */ |
536 setmode(0, O_BINARY); | |
537 #endif | |
538 } | |
539 else if (!read_buffer) | |
540 { | |
541 #ifdef USE_MCH_ACCESS | |
542 if ( | |
543 # ifdef UNIX | |
544 !(perm & 0222) || | |
545 # endif | |
546 mch_access((char *)fname, W_OK)) | |
547 file_readonly = TRUE; | |
548 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
549 #else | |
550 if (!newfile | |
551 || readonlymode | |
552 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0) | |
553 { | |
554 file_readonly = TRUE; | |
555 /* try to open ro */ | |
556 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
557 } | |
558 #endif | |
559 } | |
560 | |
561 if (fd < 0) /* cannot open at all */ | |
562 { | |
563 #ifndef UNIX | |
564 int isdir_f; | |
565 #endif | |
566 msg_scroll = msg_save; | |
567 #ifndef UNIX | |
568 /* | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
569 * On Amiga we can't open a directory, check here. |
7 | 570 */ |
571 isdir_f = (mch_isdir(fname)); | |
572 perm = mch_getperm(fname); /* check if the file exists */ | |
573 if (isdir_f) | |
574 { | |
575 filemess(curbuf, sfname, (char_u *)_("is a directory"), 0); | |
576 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
577 } | |
578 else | |
579 #endif | |
580 if (newfile) | |
581 { | |
2147
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
582 if (perm < 0 |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
583 #ifdef ENOENT |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
584 && errno == ENOENT |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
585 #endif |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
586 ) |
7 | 587 { |
588 /* | |
589 * Set the 'new-file' flag, so that when the file has | |
590 * been created by someone else, a ":w" will complain. | |
591 */ | |
592 curbuf->b_flags |= BF_NEW; | |
593 | |
594 /* Create a swap file now, so that other Vims are warned | |
595 * that we are editing this file. Don't do this for a | |
596 * "nofile" or "nowrite" buffer type. */ | |
597 #ifdef FEAT_QUICKFIX | |
598 if (!bt_dontwrite(curbuf)) | |
599 #endif | |
1834 | 600 { |
7 | 601 check_need_swap(newfile); |
1834 | 602 /* SwapExists autocommand may mess things up */ |
603 if (curbuf != old_curbuf | |
604 || (using_b_ffname | |
605 && (old_b_ffname != curbuf->b_ffname)) | |
606 || (using_b_fname | |
607 && (old_b_fname != curbuf->b_fname))) | |
608 { | |
609 EMSG(_(e_auchangedbuf)); | |
610 return FAIL; | |
611 } | |
612 } | |
592 | 613 if (dir_of_file_exists(fname)) |
614 filemess(curbuf, sfname, (char_u *)_("[New File]"), 0); | |
615 else | |
616 filemess(curbuf, sfname, | |
617 (char_u *)_("[New DIRECTORY]"), 0); | |
7 | 618 #ifdef FEAT_VIMINFO |
619 /* Even though this is a new file, it might have been | |
620 * edited before and deleted. Get the old marks. */ | |
621 check_marks_read(); | |
622 #endif | |
623 #ifdef FEAT_MBYTE | |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
624 /* Set forced 'fileencoding'. */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
625 if (eap != NULL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
626 set_forced_fenc(eap); |
7 | 627 #endif |
628 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname, | |
629 FALSE, curbuf, eap); | |
630 /* remember the current fileformat */ | |
631 save_file_ff(curbuf); | |
632 | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
633 #if defined(FEAT_EVAL) |
7 | 634 if (aborting()) /* autocmds may abort script processing */ |
635 return FAIL; | |
636 #endif | |
637 return OK; /* a new file is not an error */ | |
638 } | |
639 else | |
640 { | |
550 | 641 filemess(curbuf, sfname, (char_u *)( |
642 # ifdef EFBIG | |
643 (errno == EFBIG) ? _("[File too big]") : | |
644 # endif | |
2147
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
645 # ifdef EOVERFLOW |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
646 (errno == EOVERFLOW) ? _("[File too big]") : |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
647 # endif |
550 | 648 _("[Permission Denied]")), 0); |
7 | 649 curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
650 } | |
651 } | |
652 | |
653 return FAIL; | |
654 } | |
655 | |
656 /* | |
657 * Only set the 'ro' flag for readonly files the first time they are | |
658 * loaded. Help files always get readonly mode | |
659 */ | |
660 if ((check_readonly && file_readonly) || curbuf->b_help) | |
661 curbuf->b_p_ro = TRUE; | |
662 | |
819 | 663 if (set_options) |
7 | 664 { |
1486 | 665 /* Don't change 'eol' if reading from buffer as it will already be |
666 * correctly set when reading stdin. */ | |
667 if (!read_buffer) | |
668 { | |
669 curbuf->b_p_eol = TRUE; | |
670 curbuf->b_start_eol = TRUE; | |
671 } | |
7 | 672 #ifdef FEAT_MBYTE |
673 curbuf->b_p_bomb = FALSE; | |
1352 | 674 curbuf->b_start_bomb = FALSE; |
7 | 675 #endif |
676 } | |
677 | |
678 /* Create a swap file now, so that other Vims are warned that we are | |
679 * editing this file. | |
680 * Don't do this for a "nofile" or "nowrite" buffer type. */ | |
681 #ifdef FEAT_QUICKFIX | |
682 if (!bt_dontwrite(curbuf)) | |
683 #endif | |
684 { | |
685 check_need_swap(newfile); | |
1834 | 686 if (!read_stdin && (curbuf != old_curbuf |
687 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) | |
688 || (using_b_fname && (old_b_fname != curbuf->b_fname)))) | |
689 { | |
690 EMSG(_(e_auchangedbuf)); | |
691 if (!read_buffer) | |
692 close(fd); | |
693 return FAIL; | |
694 } | |
7 | 695 #ifdef UNIX |
696 /* Set swap file protection bits after creating it. */ | |
1918 | 697 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL |
698 && curbuf->b_ml.ml_mfp->mf_fname != NULL) | |
12771
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
699 { |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
700 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname; |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
701 |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
702 /* |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
703 * If the group-read bit is set but not the world-read bit, then |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
704 * the group must be equal to the group of the original file. If |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
705 * we can't make that happen then reset the group-read bit. This |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
706 * avoids making the swap file readable to more users when the |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
707 * primary group of the user is too permissive. |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
708 */ |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
709 if ((swap_mode & 044) == 040) |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
710 { |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
711 stat_T swap_st; |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
712 |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
713 if (mch_stat((char *)swap_fname, &swap_st) >= 0 |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
714 && st.st_gid != swap_st.st_gid |
13730
7b818f7bb738
patch 8.0.1737: fchown() used when it is not supported
Christian Brabandt <cb@256bit.org>
parents:
13610
diff
changeset
|
715 # ifdef HAVE_FCHOWN |
12771
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
716 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid) |
13988
68290d296dc9
patch 8.1.0012: misplaced #endif
Christian Brabandt <cb@256bit.org>
parents:
13819
diff
changeset
|
717 == -1 |
13730
7b818f7bb738
patch 8.0.1737: fchown() used when it is not supported
Christian Brabandt <cb@256bit.org>
parents:
13610
diff
changeset
|
718 # endif |
7b818f7bb738
patch 8.0.1737: fchown() used when it is not supported
Christian Brabandt <cb@256bit.org>
parents:
13610
diff
changeset
|
719 ) |
12771
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
720 swap_mode &= 0600; |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
721 } |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
722 |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
723 (void)mch_setperm(swap_fname, (long)swap_mode); |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
724 } |
7 | 725 #endif |
726 } | |
727 | |
578 | 728 #if defined(HAS_SWAP_EXISTS_ACTION) |
7 | 729 /* If "Quit" selected at ATTENTION dialog, don't load the file */ |
730 if (swap_exists_action == SEA_QUIT) | |
731 { | |
732 if (!read_buffer && !read_stdin) | |
733 close(fd); | |
734 return FAIL; | |
735 } | |
736 #endif | |
737 | |
738 ++no_wait_return; /* don't wait for return yet */ | |
739 | |
740 /* | |
741 * Set '[ mark to the line above where the lines go (line 1 if zero). | |
742 */ | |
743 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); | |
744 curbuf->b_op_start.col = 0; | |
745 | |
10668
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
746 try_mac = (vim_strchr(p_ffs, 'm') != NULL); |
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
747 try_dos = (vim_strchr(p_ffs, 'd') != NULL); |
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
748 try_unix = (vim_strchr(p_ffs, 'x') != NULL); |
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
749 |
7 | 750 if (!read_buffer) |
751 { | |
752 int m = msg_scroll; | |
753 int n = msg_scrolled; | |
754 | |
755 /* | |
756 * The file must be closed again, the autocommands may want to change | |
757 * the file before reading it. | |
758 */ | |
759 if (!read_stdin) | |
760 close(fd); /* ignore errors */ | |
761 | |
762 /* | |
763 * The output from the autocommands should not overwrite anything and | |
764 * should not be overwritten: Set msg_scroll, restore its value if no | |
765 * output was done. | |
766 */ | |
767 msg_scroll = TRUE; | |
768 if (filtering) | |
769 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname, | |
770 FALSE, curbuf, eap); | |
771 else if (read_stdin) | |
772 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname, | |
773 FALSE, curbuf, eap); | |
774 else if (newfile) | |
775 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname, | |
776 FALSE, curbuf, eap); | |
777 else | |
778 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname, | |
779 FALSE, NULL, eap); | |
10668
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
780 /* autocommands may have changed it */ |
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
781 try_mac = (vim_strchr(p_ffs, 'm') != NULL); |
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
782 try_dos = (vim_strchr(p_ffs, 'd') != NULL); |
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
783 try_unix = (vim_strchr(p_ffs, 'x') != NULL); |
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
784 |
7 | 785 if (msg_scrolled == n) |
786 msg_scroll = m; | |
787 | |
788 #ifdef FEAT_EVAL | |
789 if (aborting()) /* autocmds may abort script processing */ | |
790 { | |
791 --no_wait_return; | |
792 msg_scroll = msg_save; | |
793 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
794 return FAIL; | |
795 } | |
796 #endif | |
797 /* | |
798 * Don't allow the autocommands to change the current buffer. | |
799 * Try to re-open the file. | |
1834 | 800 * |
801 * Don't allow the autocommands to change the buffer name either | |
802 * (cd for example) if it invalidates fname or sfname. | |
7 | 803 */ |
804 if (!read_stdin && (curbuf != old_curbuf | |
1834 | 805 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) |
806 || (using_b_fname && (old_b_fname != curbuf->b_fname)) | |
7 | 807 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0)) |
808 { | |
809 --no_wait_return; | |
810 msg_scroll = msg_save; | |
811 if (fd < 0) | |
812 EMSG(_("E200: *ReadPre autocommands made the file unreadable")); | |
813 else | |
814 EMSG(_("E201: *ReadPre autocommands must not change current buffer")); | |
815 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
816 return FAIL; | |
817 } | |
818 } | |
819 | |
820 /* Autocommands may add lines to the file, need to check if it is empty */ | |
821 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY); | |
822 | |
823 if (!recoverymode && !filtering && !(flags & READ_DUMMY)) | |
824 { | |
825 /* | |
826 * Show the user that we are busy reading the input. Sometimes this | |
827 * may take a while. When reading from stdin another program may | |
828 * still be running, don't move the cursor to the last line, unless | |
829 * always using the GUI. | |
830 */ | |
831 if (read_stdin) | |
832 { | |
12863
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
833 if (!is_not_a_term()) |
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
834 { |
7 | 835 #ifndef ALWAYS_USE_GUI |
12863
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
836 mch_msg(_("Vim: Reading from stdin...\n")); |
7 | 837 #endif |
838 #ifdef FEAT_GUI | |
12863
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
839 /* Also write a message in the GUI window, if there is one. */ |
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
840 if (gui.in_use && !gui.dying && !gui.starting) |
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
841 { |
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
842 p = (char_u *)_("Reading from stdin..."); |
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
843 gui_write(p, (int)STRLEN(p)); |
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
844 } |
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
845 #endif |
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
846 } |
7 | 847 } |
848 else if (!read_buffer) | |
849 filemess(curbuf, sfname, (char_u *)"", 0); | |
850 } | |
851 | |
852 msg_scroll = FALSE; /* overwrite the file message */ | |
853 | |
854 /* | |
855 * Set linecnt now, before the "retry" caused by a wrong guess for | |
856 * fileformat, and after the autocommands, which may change them. | |
857 */ | |
858 linecnt = curbuf->b_ml.ml_line_count; | |
859 | |
860 #ifdef FEAT_MBYTE | |
595 | 861 /* "++bad=" argument. */ |
862 if (eap != NULL && eap->bad_char != 0) | |
612 | 863 { |
595 | 864 bad_char_behavior = eap->bad_char; |
819 | 865 if (set_options) |
612 | 866 curbuf->b_bad_char = eap->bad_char; |
867 } | |
868 else | |
869 curbuf->b_bad_char = 0; | |
595 | 870 |
7 | 871 /* |
595 | 872 * Decide which 'encoding' to use or use first. |
7 | 873 */ |
874 if (eap != NULL && eap->force_enc != 0) | |
875 { | |
876 fenc = enc_canonize(eap->cmd + eap->force_enc); | |
877 fenc_alloced = TRUE; | |
595 | 878 keep_dest_enc = TRUE; |
7 | 879 } |
880 else if (curbuf->b_p_bin) | |
881 { | |
882 fenc = (char_u *)""; /* binary: don't convert */ | |
883 fenc_alloced = FALSE; | |
884 } | |
885 else if (curbuf->b_help) | |
886 { | |
887 char_u firstline[80]; | |
301 | 888 int fc; |
7 | 889 |
890 /* Help files are either utf-8 or latin1. Try utf-8 first, if this | |
891 * fails it must be latin1. | |
892 * Always do this when 'encoding' is "utf-8". Otherwise only do | |
893 * this when needed to avoid [converted] remarks all the time. | |
894 * It is needed when the first line contains non-ASCII characters. | |
895 * That is only in *.??x files. */ | |
896 fenc = (char_u *)"latin1"; | |
897 c = enc_utf8; | |
301 | 898 if (!c && !read_stdin) |
899 { | |
900 fc = fname[STRLEN(fname) - 1]; | |
901 if (TOLOWER_ASC(fc) == 'x') | |
902 { | |
903 /* Read the first line (and a bit more). Immediately rewind to | |
904 * the start of the file. If the read() fails "len" is -1. */ | |
2664 | 905 len = read_eintr(fd, firstline, 80); |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
906 vim_lseek(fd, (off_T)0L, SEEK_SET); |
301 | 907 for (p = firstline; p < firstline + len; ++p) |
908 if (*p >= 0x80) | |
909 { | |
910 c = TRUE; | |
911 break; | |
912 } | |
913 } | |
7 | 914 } |
915 | |
916 if (c) | |
917 { | |
918 fenc_next = fenc; | |
919 fenc = (char_u *)"utf-8"; | |
920 | |
921 /* When the file is utf-8 but a character doesn't fit in | |
922 * 'encoding' don't retry. In help text editing utf-8 bytes | |
923 * doesn't make sense. */ | |
844 | 924 if (!enc_utf8) |
925 keep_dest_enc = TRUE; | |
7 | 926 } |
927 fenc_alloced = FALSE; | |
928 } | |
929 else if (*p_fencs == NUL) | |
930 { | |
931 fenc = curbuf->b_p_fenc; /* use format from buffer */ | |
932 fenc_alloced = FALSE; | |
933 } | |
934 else | |
935 { | |
936 fenc_next = p_fencs; /* try items in 'fileencodings' */ | |
937 fenc = next_fenc(&fenc_next); | |
938 fenc_alloced = TRUE; | |
939 } | |
940 #endif | |
941 | |
942 /* | |
943 * Jump back here to retry reading the file in different ways. | |
944 * Reasons to retry: | |
945 * - encoding conversion failed: try another one from "fenc_next" | |
946 * - BOM detected and fenc was set, need to setup conversion | |
947 * - "fileformat" check failed: try another | |
948 * | |
949 * Variables set for special retry actions: | |
950 * "file_rewind" Rewind the file to start reading it again. | |
951 * "advance_fenc" Advance "fenc" using "fenc_next". | |
952 * "skip_read" Re-use already read bytes (BOM detected). | |
953 * "did_iconv" iconv() conversion failed, try 'charconvert'. | |
954 * "keep_fileformat" Don't reset "fileformat". | |
955 * | |
956 * Other status indicators: | |
957 * "tmpname" When != NULL did conversion with 'charconvert'. | |
958 * Output file has to be deleted afterwards. | |
959 * "iconv_fd" When != -1 did conversion with iconv(). | |
960 */ | |
961 retry: | |
962 | |
963 if (file_rewind) | |
964 { | |
965 if (read_buffer) | |
966 { | |
967 read_buf_lnum = 1; | |
968 read_buf_col = 0; | |
969 } | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
970 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0) |
7 | 971 { |
972 /* Can't rewind the file, give up. */ | |
973 error = TRUE; | |
974 goto failed; | |
975 } | |
976 /* Delete the previously read lines. */ | |
977 while (lnum > from) | |
978 ml_delete(lnum--, FALSE); | |
979 file_rewind = FALSE; | |
980 #ifdef FEAT_MBYTE | |
819 | 981 if (set_options) |
1352 | 982 { |
7 | 983 curbuf->b_p_bomb = FALSE; |
1352 | 984 curbuf->b_start_bomb = FALSE; |
985 } | |
595 | 986 conv_error = 0; |
7 | 987 #endif |
988 } | |
989 | |
990 /* | |
991 * When retrying with another "fenc" and the first time "fileformat" | |
992 * will be reset. | |
993 */ | |
994 if (keep_fileformat) | |
995 keep_fileformat = FALSE; | |
996 else | |
997 { | |
998 if (eap != NULL && eap->force_ff != 0) | |
1742 | 999 { |
7 | 1000 fileformat = get_fileformat_force(curbuf, eap); |
1742 | 1001 try_unix = try_dos = try_mac = FALSE; |
1002 } | |
7 | 1003 else if (curbuf->b_p_bin) |
1004 fileformat = EOL_UNIX; /* binary: use Unix format */ | |
1005 else if (*p_ffs == NUL) | |
1006 fileformat = get_fileformat(curbuf);/* use format from buffer */ | |
1007 else | |
1008 fileformat = EOL_UNKNOWN; /* detect from file */ | |
1009 } | |
1010 | |
1011 #ifdef FEAT_MBYTE | |
1012 # ifdef USE_ICONV | |
1013 if (iconv_fd != (iconv_t)-1) | |
1014 { | |
1015 /* aborted conversion with iconv(), close the descriptor */ | |
1016 iconv_close(iconv_fd); | |
1017 iconv_fd = (iconv_t)-1; | |
1018 } | |
1019 # endif | |
1020 | |
1021 if (advance_fenc) | |
1022 { | |
1023 /* | |
1024 * Try the next entry in 'fileencodings'. | |
1025 */ | |
1026 advance_fenc = FALSE; | |
1027 | |
1028 if (eap != NULL && eap->force_enc != 0) | |
1029 { | |
1030 /* Conversion given with "++cc=" wasn't possible, read | |
1031 * without conversion. */ | |
1032 notconverted = TRUE; | |
595 | 1033 conv_error = 0; |
7 | 1034 if (fenc_alloced) |
1035 vim_free(fenc); | |
1036 fenc = (char_u *)""; | |
1037 fenc_alloced = FALSE; | |
1038 } | |
1039 else | |
1040 { | |
1041 if (fenc_alloced) | |
1042 vim_free(fenc); | |
1043 if (fenc_next != NULL) | |
1044 { | |
1045 fenc = next_fenc(&fenc_next); | |
1046 fenc_alloced = (fenc_next != NULL); | |
1047 } | |
1048 else | |
1049 { | |
1050 fenc = (char_u *)""; | |
1051 fenc_alloced = FALSE; | |
1052 } | |
1053 } | |
1054 if (tmpname != NULL) | |
1055 { | |
1056 mch_remove(tmpname); /* delete converted file */ | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
1057 VIM_CLEAR(tmpname); |
7 | 1058 } |
1059 } | |
1060 | |
1061 /* | |
1948 | 1062 * Conversion may be required when the encoding of the file is different |
1063 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4. | |
7 | 1064 */ |
1065 fio_flags = 0; | |
1948 | 1066 converted = need_conversion(fenc); |
1067 if (converted) | |
7 | 1068 { |
1069 | |
1070 /* "ucs-bom" means we need to check the first bytes of the file | |
1071 * for a BOM. */ | |
1072 if (STRCMP(fenc, ENC_UCSBOM) == 0) | |
1073 fio_flags = FIO_UCSBOM; | |
1074 | |
1075 /* | |
1076 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be | |
1077 * done. This is handled below after read(). Prepare the | |
1078 * fio_flags to avoid having to parse the string each time. | |
1079 * Also check for Unicode to Latin1 conversion, because iconv() | |
1080 * appears not to handle this correctly. This works just like | |
1081 * conversion to UTF-8 except how the resulting character is put in | |
1082 * the buffer. | |
1083 */ | |
1084 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
1085 fio_flags = get_fio_flags(fenc); | |
1086 | |
1087 # ifdef WIN3264 | |
1088 /* | |
1089 * Conversion from an MS-Windows codepage to UTF-8 or another codepage | |
1090 * is handled with MultiByteToWideChar(). | |
1091 */ | |
1092 if (fio_flags == 0) | |
1093 fio_flags = get_win_fio_flags(fenc); | |
1094 # endif | |
1095 | |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
1096 # ifdef MACOS_CONVERT |
7 | 1097 /* Conversion from Apple MacRoman to latin1 or UTF-8 */ |
1098 if (fio_flags == 0) | |
1099 fio_flags = get_mac_fio_flags(fenc); | |
1100 # endif | |
1101 | |
1102 # ifdef USE_ICONV | |
1103 /* | |
1104 * Try using iconv() if we can't convert internally. | |
1105 */ | |
1106 if (fio_flags == 0 | |
1107 # ifdef FEAT_EVAL | |
1108 && !did_iconv | |
1109 # endif | |
1110 ) | |
1111 iconv_fd = (iconv_t)my_iconv_open( | |
1112 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc); | |
1113 # endif | |
1114 | |
1115 # ifdef FEAT_EVAL | |
1116 /* | |
1117 * Use the 'charconvert' expression when conversion is required | |
1118 * and we can't do it internally or with iconv(). | |
1119 */ | |
1120 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL | |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
1121 && !read_fifo |
7 | 1122 # ifdef USE_ICONV |
1123 && iconv_fd == (iconv_t)-1 | |
1124 # endif | |
1125 ) | |
1126 { | |
1127 # ifdef USE_ICONV | |
1128 did_iconv = FALSE; | |
1129 # endif | |
1130 /* Skip conversion when it's already done (retry for wrong | |
1131 * "fileformat"). */ | |
1132 if (tmpname == NULL) | |
1133 { | |
1134 tmpname = readfile_charconvert(fname, fenc, &fd); | |
1135 if (tmpname == NULL) | |
1136 { | |
1137 /* Conversion failed. Try another one. */ | |
1138 advance_fenc = TRUE; | |
1139 if (fd < 0) | |
1140 { | |
1141 /* Re-opening the original file failed! */ | |
1142 EMSG(_("E202: Conversion made file unreadable!")); | |
1143 error = TRUE; | |
1144 goto failed; | |
1145 } | |
1146 goto retry; | |
1147 } | |
1148 } | |
1149 } | |
1150 else | |
1151 # endif | |
1152 { | |
1153 if (fio_flags == 0 | |
1154 # ifdef USE_ICONV | |
1155 && iconv_fd == (iconv_t)-1 | |
1156 # endif | |
1157 ) | |
1158 { | |
1159 /* Conversion wanted but we can't. | |
1160 * Try the next conversion in 'fileencodings' */ | |
1161 advance_fenc = TRUE; | |
1162 goto retry; | |
1163 } | |
1164 } | |
1165 } | |
1166 | |
595 | 1167 /* Set "can_retry" when it's possible to rewind the file and try with |
7 | 1168 * another "fenc" value. It's FALSE when no other "fenc" to try, reading |
595 | 1169 * stdin or fixed at a specific encoding. */ |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
1170 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc); |
7 | 1171 #endif |
1172 | |
1173 if (!skip_read) | |
1174 { | |
1175 linerest = 0; | |
1176 filesize = 0; | |
1177 skip_count = lines_to_skip; | |
1178 read_count = lines_to_read; | |
1179 #ifdef FEAT_MBYTE | |
1180 conv_restlen = 0; | |
1181 #endif | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1182 #ifdef FEAT_PERSISTENT_UNDO |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1183 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0 |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1184 && curbuf->b_ffname != NULL |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1185 && curbuf->b_p_udf |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1186 && !filtering |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
1187 && !read_fifo |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1188 && !read_stdin |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1189 && !read_buffer); |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1190 if (read_undo_file) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1191 sha256_start(&sha_ctx); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1192 #endif |
6122 | 1193 #ifdef FEAT_CRYPT |
1194 if (curbuf->b_cryptstate != NULL) | |
1195 { | |
1196 /* Need to free the state, but keep the key, don't want to ask for | |
1197 * it again. */ | |
1198 crypt_free_state(curbuf->b_cryptstate); | |
1199 curbuf->b_cryptstate = NULL; | |
1200 } | |
1201 #endif | |
7 | 1202 } |
1203 | |
1204 while (!error && !got_int) | |
1205 { | |
1206 /* | |
1207 * We allocate as much space for the file as we can get, plus | |
1208 * space for the old line plus room for one terminating NUL. | |
1209 * The amount is limited by the fact that read() only can read | |
1210 * upto max_unsigned characters (and other things). | |
1211 */ | |
13804
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1212 if (!skip_read) |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1213 { |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1214 #if VIM_SIZEOF_INT > 2 |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1215 # if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L) |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1216 size = SSIZE_MAX; /* use max I/O size, 52K */ |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1217 # else |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1218 /* Use buffer >= 64K. Add linerest to double the size if the |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1219 * line gets very long, to avoid a lot of copying. But don't |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1220 * read more than 1 Mbyte at a time, so we can be interrupted. |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1221 */ |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1222 size = 0x10000L + linerest; |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1223 if (size > 0x100000L) |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1224 size = 0x100000L; |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1225 # endif |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1226 #else |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1227 size = 0x7ff0L - linerest; /* limit buffer to 32K */ |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1228 #endif |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1229 } |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1230 |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1231 /* Protect against the argument of lalloc() going negative. */ |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1232 if ( |
5684 | 1233 #if VIM_SIZEOF_INT <= 2 |
13804
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1234 linerest >= 0x7ff0 |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1235 #else |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1236 size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1237 #endif |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1238 ) |
7 | 1239 { |
1240 ++split; | |
1241 *ptr = NL; /* split line by inserting a NL */ | |
1242 size = 1; | |
1243 } | |
1244 else | |
1245 { | |
1246 if (!skip_read) | |
1247 { | |
836 | 1248 for ( ; size >= 10; size = (long)((long_u)size >> 1)) |
7 | 1249 { |
1250 if ((new_buffer = lalloc((long_u)(size + linerest + 1), | |
1251 FALSE)) != NULL) | |
1252 break; | |
1253 } | |
1254 if (new_buffer == NULL) | |
1255 { | |
1256 do_outofmem_msg((long_u)(size * 2 + linerest + 1)); | |
1257 error = TRUE; | |
1258 break; | |
1259 } | |
1260 if (linerest) /* copy characters from the previous buffer */ | |
1261 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest); | |
1262 vim_free(buffer); | |
1263 buffer = new_buffer; | |
1264 ptr = buffer + linerest; | |
1265 line_start = buffer; | |
1266 | |
1267 #ifdef FEAT_MBYTE | |
1268 /* May need room to translate into. | |
1269 * For iconv() we don't really know the required space, use a | |
1270 * factor ICONV_MULT. | |
1271 * latin1 to utf-8: 1 byte becomes up to 2 bytes | |
1272 * utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes | |
1273 * become up to 4 bytes, size must be multiple of 2 | |
1274 * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be | |
1275 * multiple of 2 | |
1276 * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be | |
1277 * multiple of 4 */ | |
835 | 1278 real_size = (int)size; |
7 | 1279 # ifdef USE_ICONV |
1280 if (iconv_fd != (iconv_t)-1) | |
1281 size = size / ICONV_MULT; | |
1282 else | |
1283 # endif | |
1284 if (fio_flags & FIO_LATIN1) | |
1285 size = size / 2; | |
1286 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1287 size = (size * 2 / 3) & ~1; | |
1288 else if (fio_flags & FIO_UCS4) | |
1289 size = (size * 2 / 3) & ~3; | |
1290 else if (fio_flags == FIO_UCSBOM) | |
1291 size = size / ICONV_MULT; /* worst case */ | |
1292 # ifdef WIN3264 | |
1293 else if (fio_flags & FIO_CODEPAGE) | |
1294 size = size / ICONV_MULT; /* also worst case */ | |
1295 # endif | |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
1296 # ifdef MACOS_CONVERT |
7 | 1297 else if (fio_flags & FIO_MACROMAN) |
1298 size = size / ICONV_MULT; /* also worst case */ | |
1299 # endif | |
1300 #endif | |
1301 | |
1302 #ifdef FEAT_MBYTE | |
1303 if (conv_restlen > 0) | |
1304 { | |
1305 /* Insert unconverted bytes from previous line. */ | |
1306 mch_memmove(ptr, conv_rest, conv_restlen); | |
1307 ptr += conv_restlen; | |
1308 size -= conv_restlen; | |
1309 } | |
1310 #endif | |
1311 | |
1312 if (read_buffer) | |
1313 { | |
1314 /* | |
1315 * Read bytes from curbuf. Used for converting text read | |
1316 * from stdin. | |
1317 */ | |
1318 if (read_buf_lnum > from) | |
1319 size = 0; | |
1320 else | |
1321 { | |
1322 int n, ni; | |
1323 long tlen; | |
1324 | |
1325 tlen = 0; | |
1326 for (;;) | |
1327 { | |
1328 p = ml_get(read_buf_lnum) + read_buf_col; | |
1329 n = (int)STRLEN(p); | |
1330 if ((int)tlen + n + 1 > size) | |
1331 { | |
1332 /* Filled up to "size", append partial line. | |
1333 * Change NL to NUL to reverse the effect done | |
1334 * below. */ | |
835 | 1335 n = (int)(size - tlen); |
7 | 1336 for (ni = 0; ni < n; ++ni) |
1337 { | |
1338 if (p[ni] == NL) | |
1339 ptr[tlen++] = NUL; | |
1340 else | |
1341 ptr[tlen++] = p[ni]; | |
1342 } | |
1343 read_buf_col += n; | |
1344 break; | |
1345 } | |
1346 else | |
1347 { | |
1348 /* Append whole line and new-line. Change NL | |
1349 * to NUL to reverse the effect done below. */ | |
1350 for (ni = 0; ni < n; ++ni) | |
1351 { | |
1352 if (p[ni] == NL) | |
1353 ptr[tlen++] = NUL; | |
1354 else | |
1355 ptr[tlen++] = p[ni]; | |
1356 } | |
1357 ptr[tlen++] = NL; | |
1358 read_buf_col = 0; | |
1359 if (++read_buf_lnum > from) | |
1360 { | |
1361 /* When the last line didn't have an | |
1362 * end-of-line don't add it now either. */ | |
1363 if (!curbuf->b_p_eol) | |
1364 --tlen; | |
1365 size = tlen; | |
1366 break; | |
1367 } | |
1368 } | |
1369 } | |
1370 } | |
1371 } | |
1372 else | |
1373 { | |
1374 /* | |
1375 * Read bytes from the file. | |
1376 */ | |
2664 | 1377 size = read_eintr(fd, ptr, size); |
7 | 1378 } |
1379 | |
6122 | 1380 #ifdef FEAT_CRYPT |
1381 /* | |
1382 * At start of file: Check for magic number of encryption. | |
1383 */ | |
1384 if (filesize == 0 && size > 0) | |
1385 cryptkey = check_for_cryptkey(cryptkey, ptr, &size, | |
1386 &filesize, newfile, sfname, | |
1387 &did_ask_for_key); | |
1388 /* | |
1389 * Decrypt the read bytes. This is done before checking for | |
1390 * EOF because the crypt layer may be buffering. | |
1391 */ | |
12216
e971ef6c0dee
patch 8.0.0988: warning from Covscan about using NULL pointer
Christian Brabandt <cb@256bit.org>
parents:
12064
diff
changeset
|
1392 if (cryptkey != NULL && curbuf->b_cryptstate != NULL |
e971ef6c0dee
patch 8.0.0988: warning from Covscan about using NULL pointer
Christian Brabandt <cb@256bit.org>
parents:
12064
diff
changeset
|
1393 && size > 0) |
6122 | 1394 { |
1395 if (crypt_works_inplace(curbuf->b_cryptstate)) | |
1396 { | |
1397 crypt_decode_inplace(curbuf->b_cryptstate, ptr, size); | |
1398 } | |
1399 else | |
1400 { | |
1401 char_u *newptr = NULL; | |
1402 int decrypted_size; | |
1403 | |
1404 decrypted_size = crypt_decode_alloc( | |
1405 curbuf->b_cryptstate, ptr, size, &newptr); | |
1406 | |
1407 /* If the crypt layer is buffering, not producing | |
1408 * anything yet, need to read more. */ | |
13762
9de2b25932eb
patch 8.0.1753: various warnings from a static analyser
Christian Brabandt <cb@256bit.org>
parents:
13752
diff
changeset
|
1409 if (decrypted_size == 0) |
6122 | 1410 continue; |
1411 | |
1412 if (linerest == 0) | |
1413 { | |
1414 /* Simple case: reuse returned buffer (may be | |
1415 * NULL, checked later). */ | |
1416 new_buffer = newptr; | |
1417 } | |
1418 else | |
1419 { | |
1420 long_u new_size; | |
1421 | |
1422 /* Need new buffer to add bytes carried over. */ | |
1423 new_size = (long_u)(decrypted_size + linerest + 1); | |
1424 new_buffer = lalloc(new_size, FALSE); | |
1425 if (new_buffer == NULL) | |
1426 { | |
1427 do_outofmem_msg(new_size); | |
1428 error = TRUE; | |
1429 break; | |
1430 } | |
1431 | |
1432 mch_memmove(new_buffer, buffer, linerest); | |
1433 if (newptr != NULL) | |
1434 mch_memmove(new_buffer + linerest, newptr, | |
1435 decrypted_size); | |
1436 } | |
1437 | |
1438 if (new_buffer != NULL) | |
1439 { | |
1440 vim_free(buffer); | |
1441 buffer = new_buffer; | |
1442 new_buffer = NULL; | |
1443 line_start = buffer; | |
1444 ptr = buffer + linerest; | |
1445 } | |
1446 size = decrypted_size; | |
1447 } | |
1448 } | |
1449 #endif | |
1450 | |
7 | 1451 if (size <= 0) |
1452 { | |
1453 if (size < 0) /* read error */ | |
1454 error = TRUE; | |
1455 #ifdef FEAT_MBYTE | |
1456 else if (conv_restlen > 0) | |
595 | 1457 { |
1597 | 1458 /* |
1459 * Reached end-of-file but some trailing bytes could | |
1460 * not be converted. Truncated file? | |
1461 */ | |
1462 | |
1463 /* When we did a conversion report an error. */ | |
1464 if (fio_flags != 0 | |
1465 # ifdef USE_ICONV | |
1466 || iconv_fd != (iconv_t)-1 | |
1467 # endif | |
1468 ) | |
1469 { | |
4331 | 1470 if (can_retry) |
1471 goto rewind_retry; | |
1597 | 1472 if (conv_error == 0) |
1473 conv_error = curbuf->b_ml.ml_line_count | |
1474 - linecnt + 1; | |
1475 } | |
1476 /* Remember the first linenr with an illegal byte */ | |
1477 else if (illegal_byte == 0) | |
1478 illegal_byte = curbuf->b_ml.ml_line_count | |
1479 - linecnt + 1; | |
1480 if (bad_char_behavior == BAD_DROP) | |
595 | 1481 { |
1597 | 1482 *(ptr - conv_restlen) = NUL; |
1483 conv_restlen = 0; | |
1484 } | |
1485 else | |
1486 { | |
1487 /* Replace the trailing bytes with the replacement | |
1488 * character if we were converting; if we weren't, | |
1489 * leave the UTF8 checking code to do it, as it | |
1490 * works slightly differently. */ | |
1491 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0 | |
1492 # ifdef USE_ICONV | |
1493 || iconv_fd != (iconv_t)-1 | |
1494 # endif | |
1495 )) | |
1496 { | |
1497 while (conv_restlen > 0) | |
1498 { | |
1499 *(--ptr) = bad_char_behavior; | |
1500 --conv_restlen; | |
1501 } | |
1502 } | |
595 | 1503 fio_flags = 0; /* don't convert this */ |
834 | 1504 # ifdef USE_ICONV |
1505 if (iconv_fd != (iconv_t)-1) | |
1506 { | |
1507 iconv_close(iconv_fd); | |
1508 iconv_fd = (iconv_t)-1; | |
1509 } | |
1510 # endif | |
595 | 1511 } |
1512 } | |
7 | 1513 #endif |
1514 } | |
1515 } | |
1516 skip_read = FALSE; | |
1517 | |
1518 #ifdef FEAT_MBYTE | |
1519 /* | |
1520 * At start of file (or after crypt magic number): Check for BOM. | |
1521 * Also check for a BOM for other Unicode encodings, but not after | |
1522 * converting with 'charconvert' or when a BOM has already been | |
1523 * found. | |
1524 */ | |
1525 if ((filesize == 0 | |
1526 # ifdef FEAT_CRYPT | |
6122 | 1527 || (cryptkey != NULL |
1528 && filesize == crypt_get_header_len( | |
1529 crypt_get_method_nr(curbuf))) | |
7 | 1530 # endif |
1531 ) | |
1532 && (fio_flags == FIO_UCSBOM | |
1533 || (!curbuf->b_p_bomb | |
1534 && tmpname == NULL | |
1535 && (*fenc == 'u' || (*fenc == NUL && enc_utf8))))) | |
1536 { | |
1537 char_u *ccname; | |
1538 int blen; | |
1539 | |
1540 /* no BOM detection in a short file or in binary mode */ | |
1541 if (size < 2 || curbuf->b_p_bin) | |
1542 ccname = NULL; | |
1543 else | |
1544 ccname = check_for_bom(ptr, size, &blen, | |
1545 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc)); | |
1546 if (ccname != NULL) | |
1547 { | |
1548 /* Remove BOM from the text */ | |
1549 filesize += blen; | |
1550 size -= blen; | |
1551 mch_memmove(ptr, ptr + blen, (size_t)size); | |
819 | 1552 if (set_options) |
1352 | 1553 { |
7 | 1554 curbuf->b_p_bomb = TRUE; |
1352 | 1555 curbuf->b_start_bomb = TRUE; |
1556 } | |
7 | 1557 } |
1558 | |
1559 if (fio_flags == FIO_UCSBOM) | |
1560 { | |
1561 if (ccname == NULL) | |
1562 { | |
1563 /* No BOM detected: retry with next encoding. */ | |
1564 advance_fenc = TRUE; | |
1565 } | |
1566 else | |
1567 { | |
1568 /* BOM detected: set "fenc" and jump back */ | |
1569 if (fenc_alloced) | |
1570 vim_free(fenc); | |
1571 fenc = ccname; | |
1572 fenc_alloced = FALSE; | |
1573 } | |
1574 /* retry reading without getting new bytes or rewinding */ | |
1575 skip_read = TRUE; | |
1576 goto retry; | |
1577 } | |
1578 } | |
1597 | 1579 |
1580 /* Include not converted bytes. */ | |
1581 ptr -= conv_restlen; | |
1582 size += conv_restlen; | |
1583 conv_restlen = 0; | |
7 | 1584 #endif |
1585 /* | |
1586 * Break here for a read error or end-of-file. | |
1587 */ | |
1588 if (size <= 0) | |
1589 break; | |
1590 | |
1591 #ifdef FEAT_MBYTE | |
1592 | |
1593 # ifdef USE_ICONV | |
1594 if (iconv_fd != (iconv_t)-1) | |
1595 { | |
1596 /* | |
1597 * Attempt conversion of the read bytes to 'encoding' using | |
1598 * iconv(). | |
1599 */ | |
1600 const char *fromp; | |
1601 char *top; | |
1602 size_t from_size; | |
1603 size_t to_size; | |
1604 | |
1605 fromp = (char *)ptr; | |
1606 from_size = size; | |
1607 ptr += size; | |
1608 top = (char *)ptr; | |
1609 to_size = real_size - size; | |
1610 | |
1611 /* | |
1612 * If there is conversion error or not enough room try using | |
179 | 1613 * another conversion. Except for when there is no |
1614 * alternative (help files). | |
7 | 1615 */ |
177 | 1616 while ((iconv(iconv_fd, (void *)&fromp, &from_size, |
1617 &top, &to_size) | |
7 | 1618 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) |
1619 || from_size > CONV_RESTLEN) | |
177 | 1620 { |
595 | 1621 if (can_retry) |
375 | 1622 goto rewind_retry; |
595 | 1623 if (conv_error == 0) |
1624 conv_error = readfile_linenr(linecnt, | |
1625 ptr, (char_u *)top); | |
1626 | |
1627 /* Deal with a bad byte and continue with the next. */ | |
177 | 1628 ++fromp; |
1629 --from_size; | |
595 | 1630 if (bad_char_behavior == BAD_KEEP) |
1631 { | |
1632 *top++ = *(fromp - 1); | |
1633 --to_size; | |
1634 } | |
1635 else if (bad_char_behavior != BAD_DROP) | |
1636 { | |
1637 *top++ = bad_char_behavior; | |
1638 --to_size; | |
1639 } | |
177 | 1640 } |
7 | 1641 |
1642 if (from_size > 0) | |
1643 { | |
1644 /* Some remaining characters, keep them for the next | |
1645 * round. */ | |
1646 mch_memmove(conv_rest, (char_u *)fromp, from_size); | |
1647 conv_restlen = (int)from_size; | |
1648 } | |
1649 | |
1650 /* move the linerest to before the converted characters */ | |
1651 line_start = ptr - linerest; | |
1652 mch_memmove(line_start, buffer, (size_t)linerest); | |
1653 size = (long)((char_u *)top - ptr); | |
1654 } | |
1655 # endif | |
1656 | |
1657 # ifdef WIN3264 | |
1658 if (fio_flags & FIO_CODEPAGE) | |
1659 { | |
595 | 1660 char_u *src, *dst; |
1661 WCHAR ucs2buf[3]; | |
1662 int ucs2len; | |
1663 int codepage = FIO_GET_CP(fio_flags); | |
1664 int bytelen; | |
1665 int found_bad; | |
1666 char replstr[2]; | |
1667 | |
7 | 1668 /* |
1669 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or | |
595 | 1670 * a codepage, using standard MS-Windows functions. This |
1671 * requires two steps: | |
1672 * 1. convert from 'fileencoding' to ucs-2 | |
1673 * 2. convert from ucs-2 to 'encoding' | |
1674 * | |
1675 * Because there may be illegal bytes AND an incomplete byte | |
1676 * sequence at the end, we may have to do the conversion one | |
1677 * character at a time to get it right. | |
7 | 1678 */ |
595 | 1679 |
1680 /* Replacement string for WideCharToMultiByte(). */ | |
1681 if (bad_char_behavior > 0) | |
1682 replstr[0] = bad_char_behavior; | |
1683 else | |
1684 replstr[0] = '?'; | |
1685 replstr[1] = NUL; | |
1686 | |
1687 /* | |
1688 * Move the bytes to the end of the buffer, so that we have | |
1689 * room to put the result at the start. | |
1690 */ | |
1691 src = ptr + real_size - size; | |
1692 mch_memmove(src, ptr, size); | |
7 | 1693 |
1694 /* | |
595 | 1695 * Do the conversion. |
7 | 1696 */ |
595 | 1697 dst = ptr; |
1698 size = size; | |
1699 while (size > 0) | |
7 | 1700 { |
595 | 1701 found_bad = FALSE; |
7 | 1702 |
1703 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
595 | 1704 if (codepage == CP_UTF8) |
7 | 1705 { |
595 | 1706 /* Handle CP_UTF8 input ourselves to be able to handle |
1707 * trailing bytes properly. | |
1708 * Get one UTF-8 character from src. */ | |
835 | 1709 bytelen = (int)utf_ptr2len_len(src, size); |
595 | 1710 if (bytelen > size) |
1711 { | |
1712 /* Only got some bytes of a character. Normally | |
1713 * it's put in "conv_rest", but if it's too long | |
1714 * deal with it as if they were illegal bytes. */ | |
1715 if (bytelen <= CONV_RESTLEN) | |
1716 break; | |
1717 | |
1718 /* weird overlong byte sequence */ | |
1719 bytelen = size; | |
1720 found_bad = TRUE; | |
1721 } | |
1722 else | |
1723 { | |
799 | 1724 int u8c = utf_ptr2char(src); |
1725 | |
622 | 1726 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1)) |
595 | 1727 found_bad = TRUE; |
1728 ucs2buf[0] = u8c; | |
1729 ucs2len = 1; | |
1730 } | |
7 | 1731 } |
595 | 1732 else |
7 | 1733 # endif |
595 | 1734 { |
1735 /* We don't know how long the byte sequence is, try | |
1736 * from one to three bytes. */ | |
1737 for (bytelen = 1; bytelen <= size && bytelen <= 3; | |
1738 ++bytelen) | |
1739 { | |
1740 ucs2len = MultiByteToWideChar(codepage, | |
1741 MB_ERR_INVALID_CHARS, | |
1742 (LPCSTR)src, bytelen, | |
1743 ucs2buf, 3); | |
1744 if (ucs2len > 0) | |
1745 break; | |
1746 } | |
1747 if (ucs2len == 0) | |
1748 { | |
1749 /* If we have only one byte then it's probably an | |
1750 * incomplete byte sequence. Otherwise discard | |
1751 * one byte as a bad character. */ | |
1752 if (size == 1) | |
1753 break; | |
1754 found_bad = TRUE; | |
1755 bytelen = 1; | |
1756 } | |
1757 } | |
1758 | |
1759 if (!found_bad) | |
7 | 1760 { |
595 | 1761 int i; |
1762 | |
1763 /* Convert "ucs2buf[ucs2len]" to 'enc' in "dst". */ | |
1764 if (enc_utf8) | |
1765 { | |
1766 /* From UCS-2 to UTF-8. Cannot fail. */ | |
1767 for (i = 0; i < ucs2len; ++i) | |
1768 dst += utf_char2bytes(ucs2buf[i], dst); | |
1769 } | |
1770 else | |
1771 { | |
1772 BOOL bad = FALSE; | |
1773 int dstlen; | |
1774 | |
1775 /* From UCS-2 to "enc_codepage". If the | |
1776 * conversion uses the default character "?", | |
1777 * the data doesn't fit in this encoding. */ | |
1778 dstlen = WideCharToMultiByte(enc_codepage, 0, | |
1779 (LPCWSTR)ucs2buf, ucs2len, | |
835 | 1780 (LPSTR)dst, (int)(src - dst), |
595 | 1781 replstr, &bad); |
1782 if (bad) | |
1783 found_bad = TRUE; | |
1784 else | |
1785 dst += dstlen; | |
1786 } | |
7 | 1787 } |
595 | 1788 |
1789 if (found_bad) | |
1790 { | |
1791 /* Deal with bytes we can't convert. */ | |
1792 if (can_retry) | |
1793 goto rewind_retry; | |
1794 if (conv_error == 0) | |
1795 conv_error = readfile_linenr(linecnt, ptr, dst); | |
1796 if (bad_char_behavior != BAD_DROP) | |
1797 { | |
1798 if (bad_char_behavior == BAD_KEEP) | |
1799 { | |
1800 mch_memmove(dst, src, bytelen); | |
1801 dst += bytelen; | |
1802 } | |
1803 else | |
1804 *dst++ = bad_char_behavior; | |
1805 } | |
1806 } | |
1807 | |
1808 src += bytelen; | |
1809 size -= bytelen; | |
7 | 1810 } |
595 | 1811 |
1812 if (size > 0) | |
7 | 1813 { |
595 | 1814 /* An incomplete byte sequence remaining. */ |
1815 mch_memmove(conv_rest, src, size); | |
1816 conv_restlen = size; | |
7 | 1817 } |
595 | 1818 |
1819 /* The new size is equal to how much "dst" was advanced. */ | |
835 | 1820 size = (long)(dst - ptr); |
7 | 1821 } |
1822 else | |
1823 # endif | |
765 | 1824 # ifdef MACOS_CONVERT |
7 | 1825 if (fio_flags & FIO_MACROMAN) |
1826 { | |
1827 /* | |
1828 * Conversion from Apple MacRoman char encoding to UTF-8 or | |
18 | 1829 * latin1. This is in os_mac_conv.c. |
7 | 1830 */ |
18 | 1831 if (macroman2enc(ptr, &size, real_size) == FAIL) |
7 | 1832 goto rewind_retry; |
1833 } | |
1834 else | |
1835 # endif | |
1836 if (fio_flags != 0) | |
1837 { | |
1838 int u8c; | |
1839 char_u *dest; | |
1840 char_u *tail = NULL; | |
1841 | |
1842 /* | |
1843 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8. | |
1844 * "enc_utf8" not set: Convert Unicode to Latin1. | |
1845 * Go from end to start through the buffer, because the number | |
1846 * of bytes may increase. | |
1847 * "dest" points to after where the UTF-8 bytes go, "p" points | |
1848 * to after the next character to convert. | |
1849 */ | |
1850 dest = ptr + real_size; | |
1851 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8) | |
1852 { | |
1853 p = ptr + size; | |
1854 if (fio_flags == FIO_UTF8) | |
1855 { | |
1856 /* Check for a trailing incomplete UTF-8 sequence */ | |
1857 tail = ptr + size - 1; | |
1858 while (tail > ptr && (*tail & 0xc0) == 0x80) | |
1859 --tail; | |
1860 if (tail + utf_byte2len(*tail) <= ptr + size) | |
1861 tail = NULL; | |
1862 else | |
1863 p = tail; | |
1864 } | |
1865 } | |
1866 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1867 { | |
1868 /* Check for a trailing byte */ | |
1869 p = ptr + (size & ~1); | |
1870 if (size & 1) | |
1871 tail = p; | |
1872 if ((fio_flags & FIO_UTF16) && p > ptr) | |
1873 { | |
1874 /* Check for a trailing leading word */ | |
1875 if (fio_flags & FIO_ENDIAN_L) | |
1876 { | |
1877 u8c = (*--p << 8); | |
1878 u8c += *--p; | |
1879 } | |
1880 else | |
1881 { | |
1882 u8c = *--p; | |
1883 u8c += (*--p << 8); | |
1884 } | |
1885 if (u8c >= 0xd800 && u8c <= 0xdbff) | |
1886 tail = p; | |
1887 else | |
1888 p += 2; | |
1889 } | |
1890 } | |
1891 else /* FIO_UCS4 */ | |
1892 { | |
1893 /* Check for trailing 1, 2 or 3 bytes */ | |
1894 p = ptr + (size & ~3); | |
1895 if (size & 3) | |
1896 tail = p; | |
1897 } | |
1898 | |
1899 /* If there is a trailing incomplete sequence move it to | |
1900 * conv_rest[]. */ | |
1901 if (tail != NULL) | |
1902 { | |
1903 conv_restlen = (int)((ptr + size) - tail); | |
1904 mch_memmove(conv_rest, (char_u *)tail, conv_restlen); | |
1905 size -= conv_restlen; | |
1906 } | |
1907 | |
1908 | |
1909 while (p > ptr) | |
1910 { | |
1911 if (fio_flags & FIO_LATIN1) | |
1912 u8c = *--p; | |
1913 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1914 { | |
1915 if (fio_flags & FIO_ENDIAN_L) | |
1916 { | |
1917 u8c = (*--p << 8); | |
1918 u8c += *--p; | |
1919 } | |
1920 else | |
1921 { | |
1922 u8c = *--p; | |
1923 u8c += (*--p << 8); | |
1924 } | |
1925 if ((fio_flags & FIO_UTF16) | |
1926 && u8c >= 0xdc00 && u8c <= 0xdfff) | |
1927 { | |
1928 int u16c; | |
1929 | |
1930 if (p == ptr) | |
1931 { | |
1932 /* Missing leading word. */ | |
1933 if (can_retry) | |
1934 goto rewind_retry; | |
595 | 1935 if (conv_error == 0) |
1936 conv_error = readfile_linenr(linecnt, | |
1937 ptr, p); | |
1938 if (bad_char_behavior == BAD_DROP) | |
1939 continue; | |
1940 if (bad_char_behavior != BAD_KEEP) | |
1941 u8c = bad_char_behavior; | |
7 | 1942 } |
1943 | |
1944 /* found second word of double-word, get the first | |
1945 * word and compute the resulting character */ | |
1946 if (fio_flags & FIO_ENDIAN_L) | |
1947 { | |
1948 u16c = (*--p << 8); | |
1949 u16c += *--p; | |
1950 } | |
1951 else | |
1952 { | |
1953 u16c = *--p; | |
1954 u16c += (*--p << 8); | |
1955 } | |
595 | 1956 u8c = 0x10000 + ((u16c & 0x3ff) << 10) |
1957 + (u8c & 0x3ff); | |
1958 | |
7 | 1959 /* Check if the word is indeed a leading word. */ |
1960 if (u16c < 0xd800 || u16c > 0xdbff) | |
1961 { | |
1962 if (can_retry) | |
1963 goto rewind_retry; | |
595 | 1964 if (conv_error == 0) |
1965 conv_error = readfile_linenr(linecnt, | |
1966 ptr, p); | |
1967 if (bad_char_behavior == BAD_DROP) | |
1968 continue; | |
1969 if (bad_char_behavior != BAD_KEEP) | |
1970 u8c = bad_char_behavior; | |
7 | 1971 } |
1972 } | |
1973 } | |
1974 else if (fio_flags & FIO_UCS4) | |
1975 { | |
1976 if (fio_flags & FIO_ENDIAN_L) | |
1977 { | |
12698
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1978 u8c = (unsigned)*--p << 24; |
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1979 u8c += (unsigned)*--p << 16; |
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1980 u8c += (unsigned)*--p << 8; |
7 | 1981 u8c += *--p; |
1982 } | |
1983 else /* big endian */ | |
1984 { | |
1985 u8c = *--p; | |
12698
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1986 u8c += (unsigned)*--p << 8; |
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1987 u8c += (unsigned)*--p << 16; |
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1988 u8c += (unsigned)*--p << 24; |
7 | 1989 } |
1990 } | |
1991 else /* UTF-8 */ | |
1992 { | |
1993 if (*--p < 0x80) | |
1994 u8c = *p; | |
1995 else | |
1996 { | |
1997 len = utf_head_off(ptr, p); | |
595 | 1998 p -= len; |
1999 u8c = utf_ptr2char(p); | |
7 | 2000 if (len == 0) |
2001 { | |
2002 /* Not a valid UTF-8 character, retry with | |
2003 * another fenc when possible, otherwise just | |
2004 * report the error. */ | |
2005 if (can_retry) | |
2006 goto rewind_retry; | |
595 | 2007 if (conv_error == 0) |
2008 conv_error = readfile_linenr(linecnt, | |
2009 ptr, p); | |
2010 if (bad_char_behavior == BAD_DROP) | |
2011 continue; | |
2012 if (bad_char_behavior != BAD_KEEP) | |
2013 u8c = bad_char_behavior; | |
7 | 2014 } |
2015 } | |
2016 } | |
2017 if (enc_utf8) /* produce UTF-8 */ | |
2018 { | |
2019 dest -= utf_char2len(u8c); | |
2020 (void)utf_char2bytes(u8c, dest); | |
2021 } | |
2022 else /* produce Latin1 */ | |
2023 { | |
2024 --dest; | |
2025 if (u8c >= 0x100) | |
2026 { | |
2027 /* character doesn't fit in latin1, retry with | |
2028 * another fenc when possible, otherwise just | |
2029 * report the error. */ | |
595 | 2030 if (can_retry) |
7 | 2031 goto rewind_retry; |
595 | 2032 if (conv_error == 0) |
2033 conv_error = readfile_linenr(linecnt, ptr, p); | |
2034 if (bad_char_behavior == BAD_DROP) | |
2035 ++dest; | |
2036 else if (bad_char_behavior == BAD_KEEP) | |
2037 *dest = u8c; | |
2038 else if (eap != NULL && eap->bad_char != 0) | |
2039 *dest = bad_char_behavior; | |
2040 else | |
2041 *dest = 0xBF; | |
7 | 2042 } |
2043 else | |
2044 *dest = u8c; | |
2045 } | |
2046 } | |
2047 | |
2048 /* move the linerest to before the converted characters */ | |
2049 line_start = dest - linerest; | |
2050 mch_memmove(line_start, buffer, (size_t)linerest); | |
2051 size = (long)((ptr + real_size) - dest); | |
2052 ptr = dest; | |
2053 } | |
1597 | 2054 else if (enc_utf8 && !curbuf->b_p_bin) |
2055 { | |
2056 int incomplete_tail = FALSE; | |
2057 | |
2058 /* Reading UTF-8: Check if the bytes are valid UTF-8. */ | |
2059 for (p = ptr; ; ++p) | |
7 | 2060 { |
835 | 2061 int todo = (int)((ptr + size) - p); |
595 | 2062 int l; |
2063 | |
2064 if (todo <= 0) | |
2065 break; | |
7 | 2066 if (*p >= 0x80) |
2067 { | |
2068 /* A length of 1 means it's an illegal byte. Accept | |
2069 * an incomplete character at the end though, the next | |
2070 * read() will get the next bytes, we'll check it | |
2071 * then. */ | |
595 | 2072 l = utf_ptr2len_len(p, todo); |
1597 | 2073 if (l > todo && !incomplete_tail) |
7 | 2074 { |
1597 | 2075 /* Avoid retrying with a different encoding when |
2076 * a truncated file is more likely, or attempting | |
2077 * to read the rest of an incomplete sequence when | |
2078 * we have already done so. */ | |
2079 if (p > ptr || filesize > 0) | |
2080 incomplete_tail = TRUE; | |
2081 /* Incomplete byte sequence, move it to conv_rest[] | |
2082 * and try to read the rest of it, unless we've | |
2083 * already done so. */ | |
2084 if (p > ptr) | |
2085 { | |
2086 conv_restlen = todo; | |
2087 mch_memmove(conv_rest, p, conv_restlen); | |
2088 size -= conv_restlen; | |
2089 break; | |
2090 } | |
7 | 2091 } |
1597 | 2092 if (l == 1 || l > todo) |
595 | 2093 { |
2094 /* Illegal byte. If we can try another encoding | |
1597 | 2095 * do that, unless at EOF where a truncated |
2096 * file is more likely than a conversion error. */ | |
2097 if (can_retry && !incomplete_tail) | |
595 | 2098 break; |
2099 # ifdef USE_ICONV | |
2100 /* When we did a conversion report an error. */ | |
2101 if (iconv_fd != (iconv_t)-1 && conv_error == 0) | |
2102 conv_error = readfile_linenr(linecnt, ptr, p); | |
2103 # endif | |
1597 | 2104 /* Remember the first linenr with an illegal byte */ |
2105 if (conv_error == 0 && illegal_byte == 0) | |
2106 illegal_byte = readfile_linenr(linecnt, ptr, p); | |
595 | 2107 |
2108 /* Drop, keep or replace the bad byte. */ | |
2109 if (bad_char_behavior == BAD_DROP) | |
2110 { | |
1597 | 2111 mch_memmove(p, p + 1, todo - 1); |
595 | 2112 --p; |
2113 --size; | |
2114 } | |
2115 else if (bad_char_behavior != BAD_KEEP) | |
2116 *p = bad_char_behavior; | |
2117 } | |
1597 | 2118 else |
2119 p += l - 1; | |
7 | 2120 } |
2121 } | |
1597 | 2122 if (p < ptr + size && !incomplete_tail) |
7 | 2123 { |
2124 /* Detected a UTF-8 error. */ | |
2125 rewind_retry: | |
595 | 2126 /* Retry reading with another conversion. */ |
7 | 2127 # if defined(FEAT_EVAL) && defined(USE_ICONV) |
595 | 2128 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1) |
2129 /* iconv() failed, try 'charconvert' */ | |
2130 did_iconv = TRUE; | |
7 | 2131 else |
2132 # endif | |
595 | 2133 /* use next item from 'fileencodings' */ |
2134 advance_fenc = TRUE; | |
2135 file_rewind = TRUE; | |
2136 goto retry; | |
7 | 2137 } |
2138 } | |
2139 #endif | |
2140 | |
2141 /* count the number of characters (after conversion!) */ | |
2142 filesize += size; | |
2143 | |
2144 /* | |
2145 * when reading the first part of a file: guess EOL type | |
2146 */ | |
2147 if (fileformat == EOL_UNKNOWN) | |
2148 { | |
2149 /* First try finding a NL, for Dos and Unix */ | |
2150 if (try_dos || try_unix) | |
2151 { | |
6635 | 2152 /* Reset the carriage return counter. */ |
2153 if (try_mac) | |
2154 try_mac = 1; | |
2155 | |
7 | 2156 for (p = ptr; p < ptr + size; ++p) |
2157 { | |
2158 if (*p == NL) | |
2159 { | |
2160 if (!try_unix | |
2161 || (try_dos && p > ptr && p[-1] == CAR)) | |
2162 fileformat = EOL_DOS; | |
2163 else | |
2164 fileformat = EOL_UNIX; | |
2165 break; | |
2166 } | |
6618 | 2167 else if (*p == CAR && try_mac) |
2168 try_mac++; | |
7 | 2169 } |
2170 | |
2171 /* Don't give in to EOL_UNIX if EOL_MAC is more likely */ | |
2172 if (fileformat == EOL_UNIX && try_mac) | |
2173 { | |
2174 /* Need to reset the counters when retrying fenc. */ | |
2175 try_mac = 1; | |
2176 try_unix = 1; | |
2177 for (; p >= ptr && *p != CAR; p--) | |
2178 ; | |
2179 if (p >= ptr) | |
2180 { | |
2181 for (p = ptr; p < ptr + size; ++p) | |
2182 { | |
2183 if (*p == NL) | |
2184 try_unix++; | |
2185 else if (*p == CAR) | |
2186 try_mac++; | |
2187 } | |
2188 if (try_mac > try_unix) | |
2189 fileformat = EOL_MAC; | |
2190 } | |
2191 } | |
6618 | 2192 else if (fileformat == EOL_UNKNOWN && try_mac == 1) |
2193 /* Looking for CR but found no end-of-line markers at | |
2194 * all: use the default format. */ | |
2195 fileformat = default_fileformat(); | |
7 | 2196 } |
2197 | |
2198 /* No NL found: may use Mac format */ | |
2199 if (fileformat == EOL_UNKNOWN && try_mac) | |
2200 fileformat = EOL_MAC; | |
2201 | |
2202 /* Still nothing found? Use first format in 'ffs' */ | |
2203 if (fileformat == EOL_UNKNOWN) | |
2204 fileformat = default_fileformat(); | |
2205 | |
2206 /* if editing a new file: may set p_tx and p_ff */ | |
819 | 2207 if (set_options) |
7 | 2208 set_fileformat(fileformat, OPT_LOCAL); |
2209 } | |
2210 } | |
2211 | |
2212 /* | |
2213 * This loop is executed once for every character read. | |
2214 * Keep it fast! | |
2215 */ | |
2216 if (fileformat == EOL_MAC) | |
2217 { | |
2218 --ptr; | |
2219 while (++ptr, --size >= 0) | |
2220 { | |
2221 /* catch most common case first */ | |
2222 if ((c = *ptr) != NUL && c != CAR && c != NL) | |
2223 continue; | |
2224 if (c == NUL) | |
2225 *ptr = NL; /* NULs are replaced by newlines! */ | |
2226 else if (c == NL) | |
2227 *ptr = CAR; /* NLs are replaced by CRs! */ | |
2228 else | |
2229 { | |
2230 if (skip_count == 0) | |
2231 { | |
2232 *ptr = NUL; /* end of line */ | |
2233 len = (colnr_T) (ptr - line_start + 1); | |
2234 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2235 { | |
2236 error = TRUE; | |
2237 break; | |
2238 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2239 #ifdef FEAT_PERSISTENT_UNDO |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2240 if (read_undo_file) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2241 sha256_update(&sha_ctx, line_start, len); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2242 #endif |
7 | 2243 ++lnum; |
2244 if (--read_count == 0) | |
2245 { | |
2246 error = TRUE; /* break loop */ | |
2247 line_start = ptr; /* nothing left to write */ | |
2248 break; | |
2249 } | |
2250 } | |
2251 else | |
2252 --skip_count; | |
2253 line_start = ptr + 1; | |
2254 } | |
2255 } | |
2256 } | |
2257 else | |
2258 { | |
2259 --ptr; | |
2260 while (++ptr, --size >= 0) | |
2261 { | |
2262 if ((c = *ptr) != NUL && c != NL) /* catch most common case */ | |
2263 continue; | |
2264 if (c == NUL) | |
2265 *ptr = NL; /* NULs are replaced by newlines! */ | |
2266 else | |
2267 { | |
2268 if (skip_count == 0) | |
2269 { | |
2270 *ptr = NUL; /* end of line */ | |
2271 len = (colnr_T)(ptr - line_start + 1); | |
2272 if (fileformat == EOL_DOS) | |
2273 { | |
10674
d6857a8dc07e
patch 8.0.0227: crash with ff=dos when first line in file has no CR
Christian Brabandt <cb@256bit.org>
parents:
10668
diff
changeset
|
2274 if (ptr > line_start && ptr[-1] == CAR) |
7 | 2275 { |
10674
d6857a8dc07e
patch 8.0.0227: crash with ff=dos when first line in file has no CR
Christian Brabandt <cb@256bit.org>
parents:
10668
diff
changeset
|
2276 /* remove CR before NL */ |
7 | 2277 ptr[-1] = NUL; |
2278 --len; | |
2279 } | |
2280 /* | |
2281 * Reading in Dos format, but no CR-LF found! | |
2282 * When 'fileformats' includes "unix", delete all | |
2283 * the lines read so far and start all over again. | |
2284 * Otherwise give an error message later. | |
2285 */ | |
2286 else if (ff_error != EOL_DOS) | |
2287 { | |
2288 if ( try_unix | |
2289 && !read_stdin | |
2290 && (read_buffer | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
2291 || vim_lseek(fd, (off_T)0L, SEEK_SET) |
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
2292 == 0)) |
7 | 2293 { |
2294 fileformat = EOL_UNIX; | |
819 | 2295 if (set_options) |
7 | 2296 set_fileformat(EOL_UNIX, OPT_LOCAL); |
2297 file_rewind = TRUE; | |
2298 keep_fileformat = TRUE; | |
2299 goto retry; | |
2300 } | |
2301 ff_error = EOL_DOS; | |
2302 } | |
2303 } | |
2304 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2305 { | |
2306 error = TRUE; | |
2307 break; | |
2308 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2309 #ifdef FEAT_PERSISTENT_UNDO |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2310 if (read_undo_file) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2311 sha256_update(&sha_ctx, line_start, len); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2312 #endif |
7 | 2313 ++lnum; |
2314 if (--read_count == 0) | |
2315 { | |
2316 error = TRUE; /* break loop */ | |
2317 line_start = ptr; /* nothing left to write */ | |
2318 break; | |
2319 } | |
2320 } | |
2321 else | |
2322 --skip_count; | |
2323 line_start = ptr + 1; | |
2324 } | |
2325 } | |
2326 } | |
2327 linerest = (long)(ptr - line_start); | |
2328 ui_breakcheck(); | |
2329 } | |
2330 | |
2331 failed: | |
2332 /* not an error, max. number of lines reached */ | |
2333 if (error && read_count == 0) | |
2334 error = FALSE; | |
2335 | |
2336 /* | |
2337 * If we get EOF in the middle of a line, note the fact and | |
2338 * complete the line ourselves. | |
2339 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set. | |
2340 */ | |
2341 if (!error | |
2342 && !got_int | |
2343 && linerest != 0 | |
2344 && !(!curbuf->b_p_bin | |
2345 && fileformat == EOL_DOS | |
2346 && *line_start == Ctrl_Z | |
2347 && ptr == line_start + 1)) | |
2348 { | |
819 | 2349 /* remember for when writing */ |
2350 if (set_options) | |
7 | 2351 curbuf->b_p_eol = FALSE; |
2352 *ptr = NUL; | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2353 len = (colnr_T)(ptr - line_start + 1); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2354 if (ml_append(lnum, line_start, len, newfile) == FAIL) |
7 | 2355 error = TRUE; |
2356 else | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2357 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2358 #ifdef FEAT_PERSISTENT_UNDO |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2359 if (read_undo_file) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2360 sha256_update(&sha_ctx, line_start, len); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2361 #endif |
7 | 2362 read_no_eol_lnum = ++lnum; |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2363 } |
7 | 2364 } |
2365 | |
819 | 2366 if (set_options) |
7 | 2367 save_file_ff(curbuf); /* remember the current file format */ |
2368 | |
2369 #ifdef FEAT_CRYPT | |
6122 | 2370 if (curbuf->b_cryptstate != NULL) |
2371 { | |
2372 crypt_free_state(curbuf->b_cryptstate); | |
2373 curbuf->b_cryptstate = NULL; | |
2374 } | |
2375 if (cryptkey != NULL && cryptkey != curbuf->b_p_key) | |
2376 crypt_free_key(cryptkey); | |
2377 /* Don't set cryptkey to NULL, it's used below as a flag that | |
2378 * encryption was used. */ | |
7 | 2379 #endif |
2380 | |
2381 #ifdef FEAT_MBYTE | |
819 | 2382 /* If editing a new file: set 'fenc' for the current buffer. |
2383 * Also for ":read ++edit file". */ | |
2384 if (set_options) | |
7 | 2385 set_string_option_direct((char_u *)"fenc", -1, fenc, |
694 | 2386 OPT_FREE|OPT_LOCAL, 0); |
7 | 2387 if (fenc_alloced) |
2388 vim_free(fenc); | |
2389 # ifdef USE_ICONV | |
2390 if (iconv_fd != (iconv_t)-1) | |
2391 { | |
2392 iconv_close(iconv_fd); | |
2393 iconv_fd = (iconv_t)-1; | |
2394 } | |
2395 # endif | |
2396 #endif | |
2397 | |
2398 if (!read_buffer && !read_stdin) | |
2399 close(fd); /* errors are ignored */ | |
2003 | 2400 #ifdef HAVE_FD_CLOEXEC |
2401 else | |
2402 { | |
2403 int fdflags = fcntl(fd, F_GETFD); | |
2404 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0) | |
7961
a7e58c6e4e9a
commit https://github.com/vim/vim/commit/fbc4b4db3a9690906a96e16724350a6241cf32a5
Christian Brabandt <cb@256bit.org>
parents:
7856
diff
changeset
|
2405 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC); |
2003 | 2406 } |
2407 #endif | |
7 | 2408 vim_free(buffer); |
2409 | |
2410 #ifdef HAVE_DUP | |
2411 if (read_stdin) | |
2412 { | |
2413 /* Use stderr for stdin, makes shell commands work. */ | |
2414 close(0); | |
1757 | 2415 ignored = dup(2); |
7 | 2416 } |
2417 #endif | |
2418 | |
2419 #ifdef FEAT_MBYTE | |
2420 if (tmpname != NULL) | |
2421 { | |
2422 mch_remove(tmpname); /* delete converted file */ | |
2423 vim_free(tmpname); | |
2424 } | |
2425 #endif | |
2426 --no_wait_return; /* may wait for return now */ | |
2427 | |
2428 /* | |
2429 * In recovery mode everything but autocommands is skipped. | |
2430 */ | |
2431 if (!recoverymode) | |
2432 { | |
2433 /* need to delete the last line, which comes from the empty buffer */ | |
2434 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY)) | |
2435 { | |
2436 #ifdef FEAT_NETBEANS_INTG | |
2437 netbeansFireChanges = 0; | |
2438 #endif | |
2439 ml_delete(curbuf->b_ml.ml_line_count, FALSE); | |
2440 #ifdef FEAT_NETBEANS_INTG | |
2441 netbeansFireChanges = 1; | |
2442 #endif | |
2443 --linecnt; | |
2444 } | |
2445 linecnt = curbuf->b_ml.ml_line_count - linecnt; | |
2446 if (filesize == 0) | |
2447 linecnt = 0; | |
2448 if (newfile || read_buffer) | |
1201 | 2449 { |
7 | 2450 redraw_curbuf_later(NOT_VALID); |
1201 | 2451 #ifdef FEAT_DIFF |
2452 /* After reading the text into the buffer the diff info needs to | |
2453 * be updated. */ | |
2454 diff_invalidate(curbuf); | |
2455 #endif | |
2456 #ifdef FEAT_FOLDING | |
2457 /* All folds in the window are invalid now. Mark them for update | |
2458 * before triggering autocommands. */ | |
2459 foldUpdateAll(curwin); | |
2460 #endif | |
2461 } | |
7 | 2462 else if (linecnt) /* appended at least one line */ |
2463 appended_lines_mark(from, linecnt); | |
2464 | |
2465 #ifndef ALWAYS_USE_GUI | |
2466 /* | |
2467 * If we were reading from the same terminal as where messages go, | |
2468 * the screen will have been messed up. | |
2469 * Switch on raw mode now and clear the screen. | |
2470 */ | |
2471 if (read_stdin) | |
2472 { | |
2473 settmode(TMODE_RAW); /* set to raw mode */ | |
2474 starttermcap(); | |
2475 screenclear(); | |
2476 } | |
2477 #endif | |
2478 | |
2479 if (got_int) | |
2480 { | |
2481 if (!(flags & READ_DUMMY)) | |
2482 { | |
2483 filemess(curbuf, sfname, (char_u *)_(e_interr), 0); | |
2484 if (newfile) | |
2485 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
2486 } | |
2487 msg_scroll = msg_save; | |
2488 #ifdef FEAT_VIMINFO | |
2489 check_marks_read(); | |
2490 #endif | |
2491 return OK; /* an interrupt isn't really an error */ | |
2492 } | |
2493 | |
2494 if (!filtering && !(flags & READ_DUMMY)) | |
2495 { | |
2496 msg_add_fname(curbuf, sfname); /* fname in IObuff with quotes */ | |
2497 c = FALSE; | |
2498 | |
2499 #ifdef UNIX | |
2500 # ifdef S_ISFIFO | |
2501 if (S_ISFIFO(perm)) /* fifo or socket */ | |
2502 { | |
2503 STRCAT(IObuff, _("[fifo/socket]")); | |
2504 c = TRUE; | |
2505 } | |
2506 # else | |
2507 # ifdef S_IFIFO | |
2508 if ((perm & S_IFMT) == S_IFIFO) /* fifo */ | |
2509 { | |
2510 STRCAT(IObuff, _("[fifo]")); | |
2511 c = TRUE; | |
2512 } | |
2513 # endif | |
2514 # ifdef S_IFSOCK | |
2515 if ((perm & S_IFMT) == S_IFSOCK) /* or socket */ | |
2516 { | |
2517 STRCAT(IObuff, _("[socket]")); | |
2518 c = TRUE; | |
2519 } | |
2520 # endif | |
2521 # endif | |
1313 | 2522 # ifdef OPEN_CHR_FILES |
2523 if (S_ISCHR(perm)) /* or character special */ | |
2524 { | |
2525 STRCAT(IObuff, _("[character special]")); | |
2526 c = TRUE; | |
2527 } | |
2528 # endif | |
7 | 2529 #endif |
2530 if (curbuf->b_p_ro) | |
2531 { | |
2532 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")); | |
2533 c = TRUE; | |
2534 } | |
2535 if (read_no_eol_lnum) | |
2536 { | |
2537 msg_add_eol(); | |
2538 c = TRUE; | |
2539 } | |
2540 if (ff_error == EOL_DOS) | |
2541 { | |
2542 STRCAT(IObuff, _("[CR missing]")); | |
2543 c = TRUE; | |
2544 } | |
2545 if (split) | |
2546 { | |
2547 STRCAT(IObuff, _("[long lines split]")); | |
2548 c = TRUE; | |
2549 } | |
2550 #ifdef FEAT_MBYTE | |
2551 if (notconverted) | |
2552 { | |
2553 STRCAT(IObuff, _("[NOT converted]")); | |
2554 c = TRUE; | |
2555 } | |
2556 else if (converted) | |
2557 { | |
2558 STRCAT(IObuff, _("[converted]")); | |
2559 c = TRUE; | |
2560 } | |
2561 #endif | |
2562 #ifdef FEAT_CRYPT | |
2563 if (cryptkey != NULL) | |
2564 { | |
6122 | 2565 crypt_append_msg(curbuf); |
7 | 2566 c = TRUE; |
2567 } | |
2568 #endif | |
2569 #ifdef FEAT_MBYTE | |
595 | 2570 if (conv_error != 0) |
2571 { | |
2572 sprintf((char *)IObuff + STRLEN(IObuff), | |
2573 _("[CONVERSION ERROR in line %ld]"), (long)conv_error); | |
7 | 2574 c = TRUE; |
2575 } | |
2576 else if (illegal_byte > 0) | |
2577 { | |
2578 sprintf((char *)IObuff + STRLEN(IObuff), | |
2579 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte); | |
2580 c = TRUE; | |
2581 } | |
2582 else | |
2583 #endif | |
2584 if (error) | |
2585 { | |
2586 STRCAT(IObuff, _("[READ ERRORS]")); | |
2587 c = TRUE; | |
2588 } | |
2589 if (msg_add_fileformat(fileformat)) | |
2590 c = TRUE; | |
2591 #ifdef FEAT_CRYPT | |
2592 if (cryptkey != NULL) | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2593 msg_add_lines(c, (long)linecnt, filesize |
6122 | 2594 - crypt_get_header_len(crypt_get_method_nr(curbuf))); |
7 | 2595 else |
2596 #endif | |
2597 msg_add_lines(c, (long)linecnt, filesize); | |
2598 | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
2599 VIM_CLEAR(keep_msg); |
7 | 2600 msg_scrolled_ign = TRUE; |
2601 #ifdef ALWAYS_USE_GUI | |
2602 /* Don't show the message when reading stdin, it would end up in a | |
2603 * message box (which might be shown when exiting!) */ | |
2604 if (read_stdin || read_buffer) | |
2605 p = msg_may_trunc(FALSE, IObuff); | |
2606 else | |
2607 #endif | |
2608 p = msg_trunc_attr(IObuff, FALSE, 0); | |
2609 if (read_stdin || read_buffer || restart_edit != 0 | |
540 | 2610 || (msg_scrolled != 0 && !need_wait_return)) |
7 | 2611 /* Need to repeat the message after redrawing when: |
2612 * - When reading from stdin (the screen will be cleared next). | |
2613 * - When restart_edit is set (otherwise there will be a delay | |
2614 * before redrawing). | |
2615 * - When the screen was scrolled but there is no wait-return | |
2616 * prompt. */ | |
679 | 2617 set_keep_msg(p, 0); |
7 | 2618 msg_scrolled_ign = FALSE; |
2619 } | |
2620 | |
2621 /* with errors writing the file requires ":w!" */ | |
2622 if (newfile && (error | |
2623 #ifdef FEAT_MBYTE | |
595 | 2624 || conv_error != 0 |
679 | 2625 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP) |
7 | 2626 #endif |
2627 )) | |
2628 curbuf->b_p_ro = TRUE; | |
2629 | |
2630 u_clearline(); /* cannot use "U" command after adding lines */ | |
2631 | |
2632 /* | |
2633 * In Ex mode: cursor at last new line. | |
2634 * Otherwise: cursor at first new line. | |
2635 */ | |
2636 if (exmode_active) | |
2637 curwin->w_cursor.lnum = from + linecnt; | |
2638 else | |
2639 curwin->w_cursor.lnum = from + 1; | |
2640 check_cursor_lnum(); | |
2641 beginline(BL_WHITE | BL_FIX); /* on first non-blank */ | |
2642 | |
2643 /* | |
2644 * Set '[ and '] marks to the newly read lines. | |
2645 */ | |
2646 curbuf->b_op_start.lnum = from + 1; | |
2647 curbuf->b_op_start.col = 0; | |
2648 curbuf->b_op_end.lnum = from + linecnt; | |
2649 curbuf->b_op_end.col = 0; | |
696 | 2650 |
2651 #ifdef WIN32 | |
2652 /* | |
2653 * Work around a weird problem: When a file has two links (only | |
2654 * possible on NTFS) and we write through one link, then stat() it | |
1651 | 2655 * through the other link, the timestamp information may be wrong. |
696 | 2656 * It's correct again after reading the file, thus reset the timestamp |
2657 * here. | |
2658 */ | |
2659 if (newfile && !read_stdin && !read_buffer | |
2660 && mch_stat((char *)fname, &st) >= 0) | |
2661 { | |
2662 buf_store_time(curbuf, &st, fname); | |
2663 curbuf->b_mtime_read = curbuf->b_mtime; | |
2664 } | |
2665 #endif | |
7 | 2666 } |
2667 msg_scroll = msg_save; | |
2668 | |
2669 #ifdef FEAT_VIMINFO | |
2670 /* | |
2671 * Get the marks before executing autocommands, so they can be used there. | |
2672 */ | |
2673 check_marks_read(); | |
2674 #endif | |
2675 | |
2676 /* | |
6933 | 2677 * We remember if the last line of the read didn't have |
2678 * an eol even when 'binary' is off, to support turning 'fixeol' off, | |
2679 * or writing the read again with 'binary' on. The latter is required | |
2680 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work. | |
7 | 2681 */ |
2707 | 2682 curbuf->b_no_eol_lnum = read_no_eol_lnum; |
7 | 2683 |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2684 /* When reloading a buffer put the cursor at the first line that is |
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2685 * different. */ |
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2686 if (flags & READ_KEEP_UNDO) |
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2687 u_find_first_changed(); |
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2688 |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2689 #ifdef FEAT_PERSISTENT_UNDO |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2690 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2691 * When opening a new file locate undo info and read it. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2692 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2693 if (read_undo_file) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2694 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2695 char_u hash[UNDO_HASH_SIZE]; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2696 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2697 sha256_finish(&sha_ctx, hash); |
2238
3d0a7beb0d75
Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents:
2217
diff
changeset
|
2698 u_read_undo(NULL, hash, fname); |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2699 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2700 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2701 |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
2702 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL)) |
7 | 2703 { |
2704 int m = msg_scroll; | |
2705 int n = msg_scrolled; | |
2706 | |
2707 /* Save the fileformat now, otherwise the buffer will be considered | |
2708 * modified if the format/encoding was automatically detected. */ | |
819 | 2709 if (set_options) |
7 | 2710 save_file_ff(curbuf); |
2711 | |
2712 /* | |
2713 * The output from the autocommands should not overwrite anything and | |
2714 * should not be overwritten: Set msg_scroll, restore its value if no | |
2715 * output was done. | |
2716 */ | |
2717 msg_scroll = TRUE; | |
2718 if (filtering) | |
2719 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname, | |
2720 FALSE, curbuf, eap); | |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
2721 else if (newfile || (read_buffer && sfname != NULL)) |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2722 { |
7 | 2723 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname, |
2724 FALSE, curbuf, eap); | |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2725 if (!au_did_filetype && *curbuf->b_p_ft != NUL) |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2726 /* |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2727 * EVENT_FILETYPE was not triggered but the buffer already has a |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2728 * filetype. Trigger EVENT_FILETYPE using the existing filetype. |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2729 */ |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2730 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname, |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2731 TRUE, curbuf); |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2732 } |
7 | 2733 else |
2734 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname, | |
2735 FALSE, NULL, eap); | |
2736 if (msg_scrolled == n) | |
2737 msg_scroll = m; | |
2707 | 2738 # ifdef FEAT_EVAL |
7 | 2739 if (aborting()) /* autocmds may abort script processing */ |
2740 return FAIL; | |
2707 | 2741 # endif |
2742 } | |
2743 | |
7 | 2744 if (recoverymode && error) |
2745 return FAIL; | |
2746 return OK; | |
2747 } | |
2748 | |
9911
74e345d2878c
commit https://github.com/vim/vim/commit/f04507d132fbcb63999167ec006fc6e700b5af4f
Christian Brabandt <cb@256bit.org>
parents:
9828
diff
changeset
|
2749 #if defined(OPEN_CHR_FILES) || defined(PROTO) |
1313 | 2750 /* |
2751 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+", | |
2752 * which is the name of files used for process substitution output by | |
2753 * some shells on some operating systems, e.g., bash on SunOS. | |
2754 * Do not accept "/dev/fd/[012]", opening these may hang Vim. | |
2755 */ | |
9911
74e345d2878c
commit https://github.com/vim/vim/commit/f04507d132fbcb63999167ec006fc6e700b5af4f
Christian Brabandt <cb@256bit.org>
parents:
9828
diff
changeset
|
2756 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2757 is_dev_fd_file(char_u *fname) |
1313 | 2758 { |
2759 return (STRNCMP(fname, "/dev/fd/", 8) == 0 | |
2760 && VIM_ISDIGIT(fname[8]) | |
2761 && *skipdigits(fname + 9) == NUL | |
2762 && (fname[9] != NUL | |
2763 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2'))); | |
2764 } | |
2765 #endif | |
2766 | |
595 | 2767 #ifdef FEAT_MBYTE |
2768 | |
2769 /* | |
2770 * From the current line count and characters read after that, estimate the | |
2771 * line number where we are now. | |
2772 * Used for error messages that include a line number. | |
2773 */ | |
2774 static linenr_T | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2775 readfile_linenr( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2776 linenr_T linecnt, /* line count before reading more bytes */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2777 char_u *p, /* start of more bytes read */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2778 char_u *endp) /* end of more bytes read */ |
595 | 2779 { |
2780 char_u *s; | |
2781 linenr_T lnum; | |
2782 | |
2783 lnum = curbuf->b_ml.ml_line_count - linecnt + 1; | |
2784 for (s = p; s < endp; ++s) | |
2785 if (*s == '\n') | |
2786 ++lnum; | |
2787 return lnum; | |
2788 } | |
2789 #endif | |
2790 | |
7 | 2791 /* |
612 | 2792 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be |
2793 * equal to the buffer "buf". Used for calling readfile(). | |
7 | 2794 * Returns OK or FAIL. |
2795 */ | |
2796 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2797 prep_exarg(exarg_T *eap, buf_T *buf) |
7 | 2798 { |
13575
4df23d9bad47
patch 8.0.1660: the terminal API "drop" command doesn't support options
Christian Brabandt <cb@256bit.org>
parents:
13444
diff
changeset
|
2799 eap->cmd = alloc(15 |
7 | 2800 #ifdef FEAT_MBYTE |
13575
4df23d9bad47
patch 8.0.1660: the terminal API "drop" command doesn't support options
Christian Brabandt <cb@256bit.org>
parents:
13444
diff
changeset
|
2801 + (unsigned)STRLEN(buf->b_p_fenc) |
4df23d9bad47
patch 8.0.1660: the terminal API "drop" command doesn't support options
Christian Brabandt <cb@256bit.org>
parents:
13444
diff
changeset
|
2802 #endif |
4df23d9bad47
patch 8.0.1660: the terminal API "drop" command doesn't support options
Christian Brabandt <cb@256bit.org>
parents:
13444
diff
changeset
|
2803 ); |
7 | 2804 if (eap->cmd == NULL) |
2805 return FAIL; | |
2806 | |
2807 #ifdef FEAT_MBYTE | |
13575
4df23d9bad47
patch 8.0.1660: the terminal API "drop" command doesn't support options
Christian Brabandt <cb@256bit.org>
parents:
13444
diff
changeset
|
2808 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc); |
4df23d9bad47
patch 8.0.1660: the terminal API "drop" command doesn't support options
Christian Brabandt <cb@256bit.org>
parents:
13444
diff
changeset
|
2809 eap->force_enc = 8; |
612 | 2810 eap->bad_char = buf->b_bad_char; |
7 | 2811 #else |
13575
4df23d9bad47
patch 8.0.1660: the terminal API "drop" command doesn't support options
Christian Brabandt <cb@256bit.org>
parents:
13444
diff
changeset
|
2812 sprintf((char *)eap->cmd, "e"); |
4df23d9bad47
patch 8.0.1660: the terminal API "drop" command doesn't support options
Christian Brabandt <cb@256bit.org>
parents:
13444
diff
changeset
|
2813 #endif |
4df23d9bad47
patch 8.0.1660: the terminal API "drop" command doesn't support options
Christian Brabandt <cb@256bit.org>
parents:
13444
diff
changeset
|
2814 eap->force_ff = *buf->b_p_ff; |
612 | 2815 |
2816 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN; | |
819 | 2817 eap->read_edit = FALSE; |
612 | 2818 eap->forceit = FALSE; |
7 | 2819 return OK; |
2820 } | |
2821 | |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2822 /* |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2823 * Set default or forced 'fileformat' and 'binary'. |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2824 */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2825 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2826 set_file_options(int set_options, exarg_T *eap) |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2827 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2828 /* set default 'fileformat' */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2829 if (set_options) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2830 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2831 if (eap != NULL && eap->force_ff != 0) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2832 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2833 else if (*p_ffs != NUL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2834 set_fileformat(default_fileformat(), OPT_LOCAL); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2835 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2836 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2837 /* set or reset 'binary' */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2838 if (eap != NULL && eap->force_bin != 0) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2839 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2840 int oldval = curbuf->b_p_bin; |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2841 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2842 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2843 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2844 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2845 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2846 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2847 #if defined(FEAT_MBYTE) || defined(PROTO) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2848 /* |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2849 * Set forced 'fileencoding'. |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2850 */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2851 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2852 set_forced_fenc(exarg_T *eap) |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2853 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2854 if (eap->force_enc != 0) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2855 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2856 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2857 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2858 if (fenc != NULL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2859 set_string_option_direct((char_u *)"fenc", -1, |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2860 fenc, OPT_FREE|OPT_LOCAL, 0); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2861 vim_free(fenc); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2862 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2863 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2864 |
7 | 2865 /* |
2866 * Find next fileencoding to use from 'fileencodings'. | |
2867 * "pp" points to fenc_next. It's advanced to the next item. | |
2868 * When there are no more items, an empty string is returned and *pp is set to | |
2869 * NULL. | |
2870 * When *pp is not set to NULL, the result is in allocated memory. | |
2871 */ | |
2872 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2873 next_fenc(char_u **pp) |
7 | 2874 { |
2875 char_u *p; | |
2876 char_u *r; | |
2877 | |
2878 if (**pp == NUL) | |
2879 { | |
2880 *pp = NULL; | |
2881 return (char_u *)""; | |
2882 } | |
2883 p = vim_strchr(*pp, ','); | |
2884 if (p == NULL) | |
2885 { | |
2886 r = enc_canonize(*pp); | |
2887 *pp += STRLEN(*pp); | |
2888 } | |
2889 else | |
2890 { | |
2891 r = vim_strnsave(*pp, (int)(p - *pp)); | |
2892 *pp = p + 1; | |
2893 if (r != NULL) | |
2894 { | |
2895 p = enc_canonize(r); | |
2896 vim_free(r); | |
2897 r = p; | |
2898 } | |
2899 } | |
2900 if (r == NULL) /* out of memory */ | |
2901 { | |
2902 r = (char_u *)""; | |
2903 *pp = NULL; | |
2904 } | |
2905 return r; | |
2906 } | |
2907 | |
2908 # ifdef FEAT_EVAL | |
2909 /* | |
2910 * Convert a file with the 'charconvert' expression. | |
2911 * This closes the file which is to be read, converts it and opens the | |
2912 * resulting file for reading. | |
2913 * Returns name of the resulting converted file (the caller should delete it | |
2914 * after reading it). | |
2915 * Returns NULL if the conversion failed ("*fdp" is not set) . | |
2916 */ | |
2917 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2918 readfile_charconvert( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2919 char_u *fname, /* name of input file */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2920 char_u *fenc, /* converted from */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2921 int *fdp) /* in/out: file descriptor of file */ |
7 | 2922 { |
2923 char_u *tmpname; | |
2924 char_u *errmsg = NULL; | |
2925 | |
6721 | 2926 tmpname = vim_tempname('r', FALSE); |
7 | 2927 if (tmpname == NULL) |
2928 errmsg = (char_u *)_("Can't find temp file for conversion"); | |
2929 else | |
2930 { | |
2931 close(*fdp); /* close the input file, ignore errors */ | |
2932 *fdp = -1; | |
2933 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc, | |
2934 fname, tmpname) == FAIL) | |
2935 errmsg = (char_u *)_("Conversion with 'charconvert' failed"); | |
2936 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname, | |
2937 O_RDONLY | O_EXTRA, 0)) < 0) | |
2938 errmsg = (char_u *)_("can't read output of 'charconvert'"); | |
2939 } | |
2940 | |
2941 if (errmsg != NULL) | |
2942 { | |
2943 /* Don't use emsg(), it breaks mappings, the retry with | |
2944 * another type of conversion might still work. */ | |
2945 MSG(errmsg); | |
2946 if (tmpname != NULL) | |
2947 { | |
2948 mch_remove(tmpname); /* delete converted file */ | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
2949 VIM_CLEAR(tmpname); |
7 | 2950 } |
2951 } | |
2952 | |
2953 /* If the input file is closed, open it (caller should check for error). */ | |
2954 if (*fdp < 0) | |
2955 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
2956 | |
2957 return tmpname; | |
2958 } | |
2959 # endif | |
2960 | |
2961 #endif | |
2962 | |
2963 #ifdef FEAT_VIMINFO | |
2964 /* | |
2965 * Read marks for the current buffer from the viminfo file, when we support | |
2966 * buffer marks and the buffer has a name. | |
2967 */ | |
2968 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2969 check_marks_read(void) |
7 | 2970 { |
2971 if (!curbuf->b_marks_read && get_viminfo_parameter('\'') > 0 | |
2972 && curbuf->b_ffname != NULL) | |
1733 | 2973 read_viminfo(NULL, VIF_WANT_MARKS); |
7 | 2974 |
2975 /* Always set b_marks_read; needed when 'viminfo' is changed to include | |
2976 * the ' parameter after opening a buffer. */ | |
2977 curbuf->b_marks_read = TRUE; | |
2978 } | |
2979 #endif | |
2980 | |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2981 #if defined(FEAT_CRYPT) || defined(PROTO) |
7 | 2982 /* |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2983 * Check for magic number used for encryption. Applies to the current buffer. |
7 | 2984 * If found, the magic number is removed from ptr[*sizep] and *sizep and |
2985 * *filesizep are updated. | |
2986 * Return the (new) encryption key, NULL for no encryption. | |
2987 */ | |
2988 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2989 check_for_cryptkey( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2990 char_u *cryptkey, /* previous encryption key or NULL */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2991 char_u *ptr, /* pointer to read bytes */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2992 long *sizep, /* length of read bytes */ |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
2993 off_T *filesizep, /* nr of bytes used from file */ |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2994 int newfile, /* editing a new buffer */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2995 char_u *fname, /* file name to display */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2996 int *did_ask) /* flag: whether already asked for key */ |
7 | 2997 { |
6122 | 2998 int method = crypt_method_nr_from_magic((char *)ptr, *sizep); |
5312 | 2999 int b_p_ro = curbuf->b_p_ro; |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3000 |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3001 if (method >= 0) |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3002 { |
5312 | 3003 /* Mark the buffer as read-only until the decryption has taken place. |
3004 * Avoids accidentally overwriting the file with garbage. */ | |
3005 curbuf->b_p_ro = TRUE; | |
3006 | |
6130 | 3007 /* Set the cryptmethod local to the buffer. */ |
6122 | 3008 crypt_set_cm_option(curbuf, method); |
2204
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
3009 if (cryptkey == NULL && !*did_ask) |
7 | 3010 { |
3011 if (*curbuf->b_p_key) | |
3012 cryptkey = curbuf->b_p_key; | |
3013 else | |
3014 { | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3015 /* When newfile is TRUE, store the typed key in the 'key' |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3016 * option and don't free it. bf needs hash of the key saved. |
2204
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
3017 * Don't ask for the key again when first time Enter was hit. |
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
3018 * Happens when retrying to detect encoding. */ |
2267 | 3019 smsg((char_u *)_(need_key_msg), fname); |
3020 msg_scroll = TRUE; | |
6353 | 3021 crypt_check_method(method); |
6122 | 3022 cryptkey = crypt_get_key(newfile, FALSE); |
2204
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
3023 *did_ask = TRUE; |
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
3024 |
7 | 3025 /* check if empty key entered */ |
3026 if (cryptkey != NULL && *cryptkey == NUL) | |
3027 { | |
3028 if (cryptkey != curbuf->b_p_key) | |
3029 vim_free(cryptkey); | |
3030 cryptkey = NULL; | |
3031 } | |
3032 } | |
3033 } | |
3034 | |
3035 if (cryptkey != NULL) | |
3036 { | |
6122 | 3037 int header_len; |
3038 | |
3039 curbuf->b_cryptstate = crypt_create_from_header( | |
3040 method, cryptkey, ptr); | |
3041 crypt_set_cm_option(curbuf, method); | |
3042 | |
3043 /* Remove cryptmethod specific header from the text. */ | |
3044 header_len = crypt_get_header_len(method); | |
10221
fb1fde4fcff7
commit https://github.com/vim/vim/commit/680e015bfe19be6772d3bd754486fbd45c1a9d3b
Christian Brabandt <cb@256bit.org>
parents:
10086
diff
changeset
|
3045 if (*sizep <= header_len) |
fb1fde4fcff7
commit https://github.com/vim/vim/commit/680e015bfe19be6772d3bd754486fbd45c1a9d3b
Christian Brabandt <cb@256bit.org>
parents:
10086
diff
changeset
|
3046 /* invalid header, buffer can't be encrypted */ |
fb1fde4fcff7
commit https://github.com/vim/vim/commit/680e015bfe19be6772d3bd754486fbd45c1a9d3b
Christian Brabandt <cb@256bit.org>
parents:
10086
diff
changeset
|
3047 return NULL; |
6122 | 3048 *filesizep += header_len; |
3049 *sizep -= header_len; | |
3050 mch_memmove(ptr, ptr + header_len, (size_t)*sizep); | |
3051 | |
5312 | 3052 /* Restore the read-only flag. */ |
3053 curbuf->b_p_ro = b_p_ro; | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3054 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3055 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3056 /* When starting to edit a new file which does not have encryption, clear |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3057 * the 'key' option, except when starting up (called with -x argument) */ |
2410
8f6106dd3d12
Fix: editing a not encrypted file after a crypted file messed up reading the
Bram Moolenaar <bram@vim.org>
parents:
2394
diff
changeset
|
3058 else if (newfile && *curbuf->b_p_key != NUL && !starting) |
7 | 3059 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL); |
3060 | |
3061 return cryptkey; | |
3062 } | |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3063 #endif /* FEAT_CRYPT */ |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3064 |
7 | 3065 #ifdef UNIX |
3066 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3067 set_file_time( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3068 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3069 time_t atime, /* access time */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3070 time_t mtime) /* modification time */ |
7 | 3071 { |
3072 # if defined(HAVE_UTIME) && defined(HAVE_UTIME_H) | |
3073 struct utimbuf buf; | |
3074 | |
3075 buf.actime = atime; | |
3076 buf.modtime = mtime; | |
3077 (void)utime((char *)fname, &buf); | |
3078 # else | |
3079 # if defined(HAVE_UTIMES) | |
3080 struct timeval tvp[2]; | |
3081 | |
3082 tvp[0].tv_sec = atime; | |
3083 tvp[0].tv_usec = 0; | |
3084 tvp[1].tv_sec = mtime; | |
3085 tvp[1].tv_usec = 0; | |
3086 # ifdef NeXT | |
3087 (void)utimes((char *)fname, tvp); | |
3088 # else | |
3089 (void)utimes((char *)fname, (const struct timeval *)&tvp); | |
3090 # endif | |
3091 # endif | |
3092 # endif | |
3093 } | |
3094 #endif /* UNIX */ | |
3095 | |
22 | 3096 #if defined(VMS) && !defined(MIN) |
3097 /* Older DECC compiler for VAX doesn't define MIN() */ | |
3098 # define MIN(a, b) ((a) < (b) ? (a) : (b)) | |
3099 #endif | |
3100 | |
7 | 3101 /* |
1303 | 3102 * Return TRUE if a file appears to be read-only from the file permissions. |
3103 */ | |
3104 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3105 check_file_readonly( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3106 char_u *fname, /* full path to file */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3107 int perm) /* known permissions on file */ |
1303 | 3108 { |
3109 #ifndef USE_MCH_ACCESS | |
3110 int fd = 0; | |
3111 #endif | |
3112 | |
3113 return ( | |
3114 #ifdef USE_MCH_ACCESS | |
3115 # ifdef UNIX | |
3116 (perm & 0222) == 0 || | |
3117 # endif | |
3118 mch_access((char *)fname, W_OK) | |
3119 #else | |
3120 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0 | |
3121 ? TRUE : (close(fd), FALSE) | |
3122 #endif | |
3123 ); | |
3124 } | |
3125 | |
3126 | |
3127 /* | |
589 | 3128 * buf_write() - write to file "fname" lines "start" through "end" |
7 | 3129 * |
3130 * We do our own buffering here because fwrite() is so slow. | |
3131 * | |
589 | 3132 * If "forceit" is true, we don't care for errors when attempting backups. |
3133 * In case of an error everything possible is done to restore the original | |
1698 | 3134 * file. But when "forceit" is TRUE, we risk losing it. |
589 | 3135 * |
3136 * When "reset_changed" is TRUE and "append" == FALSE and "start" == 1 and | |
3137 * "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed. | |
7 | 3138 * |
3139 * This function must NOT use NameBuff (because it's called by autowrite()). | |
3140 * | |
3141 * return FAIL for failure, OK otherwise | |
3142 */ | |
3143 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3144 buf_write( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3145 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3146 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3147 char_u *sfname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3148 linenr_T start, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3149 linenr_T end, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3150 exarg_T *eap, /* for forced 'ff' and 'fenc', can be |
7 | 3151 NULL! */ |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3152 int append, /* append to the file */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3153 int forceit, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3154 int reset_changed, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3155 int filtering) |
7 | 3156 { |
3157 int fd; | |
3158 char_u *backup = NULL; | |
3159 int backup_copy = FALSE; /* copy the original file? */ | |
3160 int dobackup; | |
3161 char_u *ffname; | |
3162 char_u *wfname = NULL; /* name of file to write to */ | |
3163 char_u *s; | |
3164 char_u *ptr; | |
3165 char_u c; | |
3166 int len; | |
3167 linenr_T lnum; | |
3168 long nchars; | |
3169 char_u *errmsg = NULL; | |
1947 | 3170 int errmsg_allocated = FALSE; |
7 | 3171 char_u *errnum = NULL; |
3172 char_u *buffer; | |
3173 char_u smallbuf[SMBUFSIZE]; | |
3174 char_u *backup_ext; | |
3175 int bufsize; | |
3176 long perm; /* file permissions */ | |
3177 int retval = OK; | |
3178 int newfile = FALSE; /* TRUE if file doesn't exist yet */ | |
3179 int msg_save = msg_scroll; | |
3180 int overwriting; /* TRUE if writing over original */ | |
3181 int no_eol = FALSE; /* no end-of-line written */ | |
3182 int device = FALSE; /* writing to a device */ | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
3183 stat_T st_old; |
7 | 3184 int prev_got_int = got_int; |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
3185 int checking_conversion; |
7 | 3186 int file_readonly = FALSE; /* overwritten file is read-only */ |
3187 static char *err_readonly = "is read-only (cannot override: \"W\" in 'cpoptions')"; | |
9536
b2aada04d84e
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Christian Brabandt <cb@256bit.org>
parents:
9509
diff
changeset
|
3188 #if defined(UNIX) /*XXX fix me sometime? */ |
7 | 3189 int made_writable = FALSE; /* 'w' bit has been set */ |
3190 #endif | |
3191 /* writing everything */ | |
3192 int whole = (start == 1 && end == buf->b_ml.ml_line_count); | |
3193 linenr_T old_line_count = buf->b_ml.ml_line_count; | |
3194 int attr; | |
3195 int fileformat; | |
3196 int write_bin; | |
3197 struct bw_info write_info; /* info for buf_write_bytes() */ | |
3198 #ifdef FEAT_MBYTE | |
3199 int converted = FALSE; | |
3200 int notconverted = FALSE; | |
3201 char_u *fenc; /* effective 'fileencoding' */ | |
3202 char_u *fenc_tofree = NULL; /* allocated "fenc" */ | |
3203 #endif | |
3204 #ifdef HAS_BW_FLAGS | |
3205 int wb_flags = 0; | |
3206 #endif | |
3207 #ifdef HAVE_ACL | |
3208 vim_acl_T acl = NULL; /* ACL copied from original file to | |
3209 backup or new file */ | |
3210 #endif | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3211 #ifdef FEAT_PERSISTENT_UNDO |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3212 int write_undo_file = FALSE; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3213 context_sha256_T sha_ctx; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3214 #endif |
6243 | 3215 unsigned int bkc = get_bkc_value(buf); |
7 | 3216 |
3217 if (fname == NULL || *fname == NUL) /* safety check */ | |
3218 return FAIL; | |
2028 | 3219 if (buf->b_ml.ml_mfp == NULL) |
3220 { | |
3221 /* This can happen during startup when there is a stray "w" in the | |
3222 * vimrc file. */ | |
3223 EMSG(_(e_emptybuf)); | |
3224 return FAIL; | |
3225 } | |
7 | 3226 |
3227 /* | |
3228 * Disallow writing from .exrc and .vimrc in current directory for | |
3229 * security reasons. | |
3230 */ | |
3231 if (check_secure()) | |
3232 return FAIL; | |
3233 | |
3234 /* Avoid a crash for a long name. */ | |
3235 if (STRLEN(fname) >= MAXPATHL) | |
3236 { | |
3237 EMSG(_(e_longname)); | |
3238 return FAIL; | |
3239 } | |
3240 | |
3241 #ifdef FEAT_MBYTE | |
3242 /* must init bw_conv_buf and bw_iconv_fd before jumping to "fail" */ | |
3243 write_info.bw_conv_buf = NULL; | |
3244 write_info.bw_conv_error = FALSE; | |
1947 | 3245 write_info.bw_conv_error_lnum = 0; |
7 | 3246 write_info.bw_restlen = 0; |
3247 # ifdef USE_ICONV | |
3248 write_info.bw_iconv_fd = (iconv_t)-1; | |
3249 # endif | |
3250 #endif | |
6122 | 3251 #ifdef FEAT_CRYPT |
3252 write_info.bw_buffer = buf; | |
3253 #endif | |
7 | 3254 |
167 | 3255 /* After writing a file changedtick changes but we don't want to display |
3256 * the line. */ | |
3257 ex_no_reprint = TRUE; | |
3258 | |
7 | 3259 /* |
3260 * If there is no file name yet, use the one for the written file. | |
3261 * BF_NOTEDITED is set to reflect this (in case the write fails). | |
3262 * Don't do this when the write is for a filter command. | |
589 | 3263 * Don't do this when appending. |
3264 * Only do this when 'cpoptions' contains the 'F' flag. | |
7 | 3265 */ |
633 | 3266 if (buf->b_ffname == NULL |
3267 && reset_changed | |
7 | 3268 && whole |
3269 && buf == curbuf | |
236 | 3270 #ifdef FEAT_QUICKFIX |
3271 && !bt_nofile(buf) | |
3272 #endif | |
7 | 3273 && !filtering |
589 | 3274 && (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL) |
7 | 3275 && vim_strchr(p_cpo, CPO_FNAMEW) != NULL) |
3276 { | |
633 | 3277 if (set_rw_fname(fname, sfname) == FAIL) |
7 | 3278 return FAIL; |
633 | 3279 buf = curbuf; /* just in case autocmds made "buf" invalid */ |
7 | 3280 } |
3281 | |
3282 if (sfname == NULL) | |
3283 sfname = fname; | |
3284 /* | |
3285 * For Unix: Use the short file name whenever possible. | |
3286 * Avoids problems with networks and when directory names are changed. | |
3287 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to | |
3288 * another directory, which we don't detect | |
3289 */ | |
3290 ffname = fname; /* remember full fname */ | |
3291 #ifdef UNIX | |
3292 fname = sfname; | |
3293 #endif | |
3294 | |
3295 if (buf->b_ffname != NULL && fnamecmp(ffname, buf->b_ffname) == 0) | |
3296 overwriting = TRUE; | |
3297 else | |
3298 overwriting = FALSE; | |
3299 | |
3300 if (exiting) | |
4352 | 3301 settmode(TMODE_COOK); /* when exiting allow typeahead now */ |
7 | 3302 |
3303 ++no_wait_return; /* don't wait for return yet */ | |
3304 | |
3305 /* | |
3306 * Set '[ and '] marks to the lines to be written. | |
3307 */ | |
3308 buf->b_op_start.lnum = start; | |
3309 buf->b_op_start.col = 0; | |
3310 buf->b_op_end.lnum = end; | |
3311 buf->b_op_end.col = 0; | |
3312 | |
3313 { | |
3314 aco_save_T aco; | |
3315 int buf_ffname = FALSE; | |
3316 int buf_sfname = FALSE; | |
3317 int buf_fname_f = FALSE; | |
3318 int buf_fname_s = FALSE; | |
3319 int did_cmd = FALSE; | |
17 | 3320 int nofile_err = FALSE; |
161 | 3321 int empty_memline = (buf->b_ml.ml_mfp == NULL); |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
3322 bufref_T bufref; |
7 | 3323 |
3324 /* | |
4352 | 3325 * Apply PRE autocommands. |
7 | 3326 * Set curbuf to the buffer to be written. |
3327 * Careful: The autocommands may call buf_write() recursively! | |
3328 */ | |
3329 if (ffname == buf->b_ffname) | |
3330 buf_ffname = TRUE; | |
3331 if (sfname == buf->b_sfname) | |
3332 buf_sfname = TRUE; | |
3333 if (fname == buf->b_ffname) | |
3334 buf_fname_f = TRUE; | |
3335 if (fname == buf->b_sfname) | |
3336 buf_fname_s = TRUE; | |
3337 | |
3338 /* set curwin/curbuf to buf and save a few things */ | |
3339 aucmd_prepbuf(&aco, buf); | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
3340 set_bufref(&bufref, buf); |
7 | 3341 |
3342 if (append) | |
3343 { | |
3344 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD, | |
3345 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3346 { |
635 | 3347 #ifdef FEAT_QUICKFIX |
18 | 3348 if (overwriting && bt_nofile(curbuf)) |
17 | 3349 nofile_err = TRUE; |
3350 else | |
635 | 3351 #endif |
17 | 3352 apply_autocmds_exarg(EVENT_FILEAPPENDPRE, |
7 | 3353 sfname, sfname, FALSE, curbuf, eap); |
17 | 3354 } |
7 | 3355 } |
3356 else if (filtering) | |
3357 { | |
3358 apply_autocmds_exarg(EVENT_FILTERWRITEPRE, | |
3359 NULL, sfname, FALSE, curbuf, eap); | |
3360 } | |
3361 else if (reset_changed && whole) | |
3362 { | |
3036 | 3363 int was_changed = curbufIsChanged(); |
3364 | |
3365 did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD, | |
3366 sfname, sfname, FALSE, curbuf, eap); | |
3367 if (did_cmd) | |
3368 { | |
3369 if (was_changed && !curbufIsChanged()) | |
3370 { | |
3371 /* Written everything correctly and BufWriteCmd has reset | |
3372 * 'modified': Correct the undo information so that an | |
3373 * undo now sets 'modified'. */ | |
3374 u_unchanged(curbuf); | |
3375 u_update_save_nr(curbuf); | |
3376 } | |
3377 } | |
3378 else | |
17 | 3379 { |
635 | 3380 #ifdef FEAT_QUICKFIX |
179 | 3381 if (overwriting && bt_nofile(curbuf)) |
17 | 3382 nofile_err = TRUE; |
3383 else | |
635 | 3384 #endif |
17 | 3385 apply_autocmds_exarg(EVENT_BUFWRITEPRE, |
7 | 3386 sfname, sfname, FALSE, curbuf, eap); |
17 | 3387 } |
7 | 3388 } |
3389 else | |
3390 { | |
3391 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD, | |
3392 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3393 { |
635 | 3394 #ifdef FEAT_QUICKFIX |
179 | 3395 if (overwriting && bt_nofile(curbuf)) |
17 | 3396 nofile_err = TRUE; |
3397 else | |
635 | 3398 #endif |
17 | 3399 apply_autocmds_exarg(EVENT_FILEWRITEPRE, |
7 | 3400 sfname, sfname, FALSE, curbuf, eap); |
17 | 3401 } |
7 | 3402 } |
3403 | |
3404 /* restore curwin/curbuf and a few other things */ | |
3405 aucmd_restbuf(&aco); | |
3406 | |
3407 /* | |
3408 * In three situations we return here and don't write the file: | |
3409 * 1. the autocommands deleted or unloaded the buffer. | |
3410 * 2. The autocommands abort script processing. | |
3411 * 3. If one of the "Cmd" autocommands was executed. | |
3412 */ | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
3413 if (!bufref_valid(&bufref)) |
7 | 3414 buf = NULL; |
161 | 3415 if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline) |
532 | 3416 || did_cmd || nofile_err |
3417 #ifdef FEAT_EVAL | |
3418 || aborting() | |
3419 #endif | |
3420 ) | |
7 | 3421 { |
3422 --no_wait_return; | |
3423 msg_scroll = msg_save; | |
17 | 3424 if (nofile_err) |
3425 EMSG(_("E676: No matching autocommands for acwrite buffer")); | |
3426 | |
532 | 3427 if (nofile_err |
3428 #ifdef FEAT_EVAL | |
3429 || aborting() | |
3430 #endif | |
3431 ) | |
7 | 3432 /* An aborting error, interrupt or exception in the |
3433 * autocommands. */ | |
3434 return FAIL; | |
3435 if (did_cmd) | |
3436 { | |
3437 if (buf == NULL) | |
3438 /* The buffer was deleted. We assume it was written | |
3439 * (can't retry anyway). */ | |
3440 return OK; | |
3441 if (overwriting) | |
3442 { | |
3443 /* Assume the buffer was written, update the timestamp. */ | |
3444 ml_timestamp(buf); | |
589 | 3445 if (append) |
3446 buf->b_flags &= ~BF_NEW; | |
3447 else | |
3448 buf->b_flags &= ~BF_WRITE_MASK; | |
7 | 3449 } |
589 | 3450 if (reset_changed && buf->b_changed && !append |
39 | 3451 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)) |
7 | 3452 /* Buffer still changed, the autocommands didn't work |
3453 * properly. */ | |
3454 return FAIL; | |
3455 return OK; | |
3456 } | |
3457 #ifdef FEAT_EVAL | |
3458 if (!aborting()) | |
3459 #endif | |
3460 EMSG(_("E203: Autocommands deleted or unloaded buffer to be written")); | |
3461 return FAIL; | |
3462 } | |
3463 | |
3464 /* | |
3465 * The autocommands may have changed the number of lines in the file. | |
3466 * When writing the whole file, adjust the end. | |
3467 * When writing part of the file, assume that the autocommands only | |
3468 * changed the number of lines that are to be written (tricky!). | |
3469 */ | |
3470 if (buf->b_ml.ml_line_count != old_line_count) | |
3471 { | |
3472 if (whole) /* write all */ | |
3473 end = buf->b_ml.ml_line_count; | |
3474 else if (buf->b_ml.ml_line_count > old_line_count) /* more lines */ | |
3475 end += buf->b_ml.ml_line_count - old_line_count; | |
3476 else /* less lines */ | |
3477 { | |
3478 end -= old_line_count - buf->b_ml.ml_line_count; | |
3479 if (end < start) | |
3480 { | |
3481 --no_wait_return; | |
3482 msg_scroll = msg_save; | |
3483 EMSG(_("E204: Autocommand changed number of lines in unexpected way")); | |
3484 return FAIL; | |
3485 } | |
3486 } | |
3487 } | |
3488 | |
3489 /* | |
3490 * The autocommands may have changed the name of the buffer, which may | |
3491 * be kept in fname, ffname and sfname. | |
3492 */ | |
3493 if (buf_ffname) | |
3494 ffname = buf->b_ffname; | |
3495 if (buf_sfname) | |
3496 sfname = buf->b_sfname; | |
3497 if (buf_fname_f) | |
3498 fname = buf->b_ffname; | |
3499 if (buf_fname_s) | |
3500 fname = buf->b_sfname; | |
3501 } | |
3502 | |
3503 #ifdef FEAT_NETBEANS_INTG | |
2210 | 3504 if (netbeans_active() && isNetbeansBuffer(buf)) |
7 | 3505 { |
3506 if (whole) | |
3507 { | |
3508 /* | |
3509 * b_changed can be 0 after an undo, but we still need to write | |
3510 * the buffer to NetBeans. | |
3511 */ | |
3512 if (buf->b_changed || isNetbeansModified(buf)) | |
3513 { | |
33 | 3514 --no_wait_return; /* may wait for return now */ |
3515 msg_scroll = msg_save; | |
3516 netbeans_save_buffer(buf); /* no error checking... */ | |
7 | 3517 return retval; |
3518 } | |
3519 else | |
3520 { | |
3521 errnum = (char_u *)"E656: "; | |
1653 | 3522 errmsg = (char_u *)_("NetBeans disallows writes of unmodified buffers"); |
7 | 3523 buffer = NULL; |
3524 goto fail; | |
3525 } | |
3526 } | |
3527 else | |
3528 { | |
3529 errnum = (char_u *)"E657: "; | |
3530 errmsg = (char_u *)_("Partial writes disallowed for NetBeans buffers"); | |
3531 buffer = NULL; | |
3532 goto fail; | |
3533 } | |
3534 } | |
3535 #endif | |
3536 | |
3537 if (shortmess(SHM_OVER) && !exiting) | |
3538 msg_scroll = FALSE; /* overwrite previous file message */ | |
3539 else | |
3540 msg_scroll = TRUE; /* don't overwrite previous file message */ | |
3541 if (!filtering) | |
3542 filemess(buf, | |
3543 #ifndef UNIX | |
3544 sfname, | |
3545 #else | |
3546 fname, | |
3547 #endif | |
3548 (char_u *)"", 0); /* show that we are busy */ | |
3549 msg_scroll = FALSE; /* always overwrite the file message now */ | |
3550 | |
3551 buffer = alloc(BUFSIZE); | |
3552 if (buffer == NULL) /* can't allocate big buffer, use small | |
3553 * one (to be able to write when out of | |
3554 * memory) */ | |
3555 { | |
3556 buffer = smallbuf; | |
3557 bufsize = SMBUFSIZE; | |
3558 } | |
3559 else | |
3560 bufsize = BUFSIZE; | |
3561 | |
3562 /* | |
3563 * Get information about original file (if there is one). | |
3564 */ | |
7410
08e62c4fc17d
commit https://github.com/vim/vim/commit/53076830fea6df737455523f7e235bfe4f79864d
Christian Brabandt <cb@256bit.org>
parents:
7408
diff
changeset
|
3565 #if defined(UNIX) |
1438 | 3566 st_old.st_dev = 0; |
3567 st_old.st_ino = 0; | |
7 | 3568 perm = -1; |
3569 if (mch_stat((char *)fname, &st_old) < 0) | |
3570 newfile = TRUE; | |
3571 else | |
3572 { | |
3573 perm = st_old.st_mode; | |
3574 if (!S_ISREG(st_old.st_mode)) /* not a file */ | |
3575 { | |
3576 if (S_ISDIR(st_old.st_mode)) | |
3577 { | |
3578 errnum = (char_u *)"E502: "; | |
3579 errmsg = (char_u *)_("is a directory"); | |
3580 goto fail; | |
3581 } | |
3582 if (mch_nodetype(fname) != NODE_WRITABLE) | |
3583 { | |
3584 errnum = (char_u *)"E503: "; | |
3585 errmsg = (char_u *)_("is not a file or writable device"); | |
3586 goto fail; | |
3587 } | |
3588 /* It's a device of some kind (or a fifo) which we can write to | |
3589 * but for which we can't make a backup. */ | |
3590 device = TRUE; | |
3591 newfile = TRUE; | |
3592 perm = -1; | |
3593 } | |
3594 } | |
3595 #else /* !UNIX */ | |
3596 /* | |
3597 * Check for a writable device name. | |
3598 */ | |
3599 c = mch_nodetype(fname); | |
3600 if (c == NODE_OTHER) | |
3601 { | |
3602 errnum = (char_u *)"E503: "; | |
3603 errmsg = (char_u *)_("is not a file or writable device"); | |
3604 goto fail; | |
3605 } | |
3606 if (c == NODE_WRITABLE) | |
3607 { | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3608 # if defined(MSWIN) |
1004 | 3609 /* MS-Windows allows opening a device, but we will probably get stuck |
3610 * trying to write to it. */ | |
3611 if (!p_odev) | |
3612 { | |
3613 errnum = (char_u *)"E796: "; | |
3614 errmsg = (char_u *)_("writing to device disabled with 'opendevice' option"); | |
3615 goto fail; | |
3616 } | |
3617 # endif | |
7 | 3618 device = TRUE; |
3619 newfile = TRUE; | |
3620 perm = -1; | |
3621 } | |
3622 else | |
3623 { | |
3624 perm = mch_getperm(fname); | |
3625 if (perm < 0) | |
3626 newfile = TRUE; | |
3627 else if (mch_isdir(fname)) | |
3628 { | |
3629 errnum = (char_u *)"E502: "; | |
3630 errmsg = (char_u *)_("is a directory"); | |
3631 goto fail; | |
3632 } | |
3633 if (overwriting) | |
3634 (void)mch_stat((char *)fname, &st_old); | |
3635 } | |
3636 #endif /* !UNIX */ | |
3637 | |
3638 if (!device && !newfile) | |
3639 { | |
3640 /* | |
3641 * Check if the file is really writable (when renaming the file to | |
3642 * make a backup we won't discover it later). | |
3643 */ | |
1303 | 3644 file_readonly = check_file_readonly(fname, (int)perm); |
3645 | |
7 | 3646 if (!forceit && file_readonly) |
3647 { | |
3648 if (vim_strchr(p_cpo, CPO_FWRITE) != NULL) | |
3649 { | |
3650 errnum = (char_u *)"E504: "; | |
3651 errmsg = (char_u *)_(err_readonly); | |
3652 } | |
3653 else | |
3654 { | |
3655 errnum = (char_u *)"E505: "; | |
3656 errmsg = (char_u *)_("is read-only (add ! to override)"); | |
3657 } | |
3658 goto fail; | |
3659 } | |
3660 | |
3661 /* | |
3662 * Check if the timestamp hasn't changed since reading the file. | |
3663 */ | |
3664 if (overwriting) | |
3665 { | |
3666 retval = check_mtime(buf, &st_old); | |
3667 if (retval == FAIL) | |
3668 goto fail; | |
3669 } | |
3670 } | |
3671 | |
3672 #ifdef HAVE_ACL | |
3673 /* | |
3674 * For systems that support ACL: get the ACL from the original file. | |
3675 */ | |
3676 if (!newfile) | |
3677 acl = mch_get_acl(fname); | |
3678 #endif | |
3679 | |
3680 /* | |
3681 * If 'backupskip' is not empty, don't make a backup for some files. | |
3682 */ | |
3683 dobackup = (p_wb || p_bk || *p_pm != NUL); | |
3684 #ifdef FEAT_WILDIGN | |
3685 if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname)) | |
3686 dobackup = FALSE; | |
3687 #endif | |
3688 | |
3689 /* | |
3690 * Save the value of got_int and reset it. We don't want a previous | |
3691 * interruption cancel writing, only hitting CTRL-C while writing should | |
3692 * abort it. | |
3693 */ | |
3694 prev_got_int = got_int; | |
3695 got_int = FALSE; | |
3696 | |
3697 /* Mark the buffer as 'being saved' to prevent changed buffer warnings */ | |
3698 buf->b_saving = TRUE; | |
3699 | |
3700 /* | |
3701 * If we are not appending or filtering, the file exists, and the | |
3702 * 'writebackup', 'backup' or 'patchmode' option is set, need a backup. | |
3703 * When 'patchmode' is set also make a backup when appending. | |
3704 * | |
3705 * Do not make any backup, if 'writebackup' and 'backup' are both switched | |
3706 * off. This helps when editing large files on almost-full disks. | |
3707 */ | |
3708 if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup) | |
3709 { | |
3710 #if defined(UNIX) || defined(WIN32) | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
3711 stat_T st; |
7 | 3712 #endif |
3713 | |
6243 | 3714 if ((bkc & BKC_YES) || append) /* "yes" */ |
7 | 3715 backup_copy = TRUE; |
3716 #if defined(UNIX) || defined(WIN32) | |
6243 | 3717 else if ((bkc & BKC_AUTO)) /* "auto" */ |
7 | 3718 { |
3719 int i; | |
3720 | |
3721 # ifdef UNIX | |
3722 /* | |
3723 * Don't rename the file when: | |
3724 * - it's a hard link | |
3725 * - it's a symbolic link | |
3726 * - we don't have write permission in the directory | |
3727 * - we can't set the owner/group of the new file | |
3728 */ | |
3729 if (st_old.st_nlink > 1 | |
3730 || mch_lstat((char *)fname, &st) < 0 | |
3731 || st.st_dev != st_old.st_dev | |
557 | 3732 || st.st_ino != st_old.st_ino |
3733 # ifndef HAVE_FCHOWN | |
3734 || st.st_uid != st_old.st_uid | |
3735 || st.st_gid != st_old.st_gid | |
3736 # endif | |
3737 ) | |
7 | 3738 backup_copy = TRUE; |
3739 else | |
696 | 3740 # else |
3741 # ifdef WIN32 | |
3742 /* On NTFS file systems hard links are possible. */ | |
3743 if (mch_is_linked(fname)) | |
3744 backup_copy = TRUE; | |
3745 else | |
3746 # endif | |
7 | 3747 # endif |
3748 { | |
3749 /* | |
3750 * Check if we can create a file and set the owner/group to | |
3751 * the ones from the original file. | |
3752 * First find a file name that doesn't exist yet (use some | |
3753 * arbitrary numbers). | |
3754 */ | |
3755 STRCPY(IObuff, fname); | |
3756 for (i = 4913; ; i += 123) | |
3757 { | |
3758 sprintf((char *)gettail(IObuff), "%d", i); | |
557 | 3759 if (mch_lstat((char *)IObuff, &st) < 0) |
7 | 3760 break; |
3761 } | |
557 | 3762 fd = mch_open((char *)IObuff, |
3763 O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm); | |
7 | 3764 if (fd < 0) /* can't write in directory */ |
3765 backup_copy = TRUE; | |
3766 else | |
3767 { | |
3768 # ifdef UNIX | |
557 | 3769 # ifdef HAVE_FCHOWN |
1757 | 3770 ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
557 | 3771 # endif |
7 | 3772 if (mch_stat((char *)IObuff, &st) < 0 |
3773 || st.st_uid != st_old.st_uid | |
3774 || st.st_gid != st_old.st_gid | |
1877 | 3775 || (long)st.st_mode != perm) |
7 | 3776 backup_copy = TRUE; |
3777 # endif | |
567 | 3778 /* Close the file before removing it, on MS-Windows we |
3779 * can't delete an open file. */ | |
3780 close(fd); | |
7 | 3781 mch_remove(IObuff); |
2523
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3782 # ifdef MSWIN |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3783 /* MS-Windows may trigger a virus scanner to open the |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3784 * file, we can't delete it then. Keep trying for half a |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3785 * second. */ |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3786 { |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3787 int try; |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3788 |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3789 for (try = 0; try < 10; ++try) |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3790 { |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3791 if (mch_lstat((char *)IObuff, &st) < 0) |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3792 break; |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3793 ui_delay(50L, TRUE); /* wait 50 msec */ |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3794 mch_remove(IObuff); |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3795 } |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3796 } |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3797 # endif |
7 | 3798 } |
3799 } | |
3800 } | |
3801 | |
3802 /* | |
3803 * Break symlinks and/or hardlinks if we've been asked to. | |
3804 */ | |
6243 | 3805 if ((bkc & BKC_BREAKSYMLINK) || (bkc & BKC_BREAKHARDLINK)) |
7 | 3806 { |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3807 # ifdef UNIX |
7 | 3808 int lstat_res; |
3809 | |
3810 lstat_res = mch_lstat((char *)fname, &st); | |
3811 | |
3812 /* Symlinks. */ | |
6243 | 3813 if ((bkc & BKC_BREAKSYMLINK) |
7 | 3814 && lstat_res == 0 |
3815 && st.st_ino != st_old.st_ino) | |
3816 backup_copy = FALSE; | |
3817 | |
3818 /* Hardlinks. */ | |
6243 | 3819 if ((bkc & BKC_BREAKHARDLINK) |
7 | 3820 && st_old.st_nlink > 1 |
3821 && (lstat_res != 0 || st.st_ino == st_old.st_ino)) | |
3822 backup_copy = FALSE; | |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3823 # else |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3824 # if defined(WIN32) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3825 /* Symlinks. */ |
6243 | 3826 if ((bkc & BKC_BREAKSYMLINK) && mch_is_symbolic_link(fname)) |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3827 backup_copy = FALSE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3828 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3829 /* Hardlinks. */ |
6243 | 3830 if ((bkc & BKC_BREAKHARDLINK) && mch_is_hard_link(fname)) |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3831 backup_copy = FALSE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3832 # endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3833 # endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3834 } |
7 | 3835 |
3836 #endif | |
3837 | |
3838 /* make sure we have a valid backup extension to use */ | |
3839 if (*p_bex == NUL) | |
3840 backup_ext = (char_u *)".bak"; | |
3841 else | |
3842 backup_ext = p_bex; | |
3843 | |
3844 if (backup_copy | |
3845 && (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) >= 0) | |
3846 { | |
3847 int bfd; | |
3848 char_u *copybuf, *wp; | |
3849 int some_error = FALSE; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
3850 stat_T st_new; |
7 | 3851 char_u *dirp; |
3852 char_u *rootname; | |
14475
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3853 #if defined(UNIX) || defined(WIN3264) |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3854 char_u *p; |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3855 #endif |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3856 #if defined(UNIX) |
7 | 3857 int did_set_shortname; |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
3858 mode_t umask_save; |
7 | 3859 #endif |
3860 | |
3861 copybuf = alloc(BUFSIZE + 1); | |
3862 if (copybuf == NULL) | |
3863 { | |
3864 some_error = TRUE; /* out of memory */ | |
3865 goto nobackup; | |
3866 } | |
3867 | |
3868 /* | |
3869 * Try to make the backup in each directory in the 'bdir' option. | |
3870 * | |
3871 * Unix semantics has it, that we may have a writable file, | |
3872 * that cannot be recreated with a simple open(..., O_CREAT, ) e.g: | |
3873 * - the directory is not writable, | |
3874 * - the file may be a symbolic link, | |
3875 * - the file may belong to another user/group, etc. | |
3876 * | |
3877 * For these reasons, the existing writable file must be truncated | |
3878 * and reused. Creation of a backup COPY will be attempted. | |
3879 */ | |
3880 dirp = p_bdir; | |
3881 while (*dirp) | |
3882 { | |
3883 #ifdef UNIX | |
3884 st_new.st_ino = 0; | |
3885 st_new.st_dev = 0; | |
3886 st_new.st_gid = 0; | |
3887 #endif | |
3888 | |
3889 /* | |
3890 * Isolate one directory name, using an entry in 'bdir'. | |
3891 */ | |
3892 (void)copy_option_part(&dirp, copybuf, BUFSIZE, ","); | |
14475
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3893 |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3894 #if defined(UNIX) || defined(WIN3264) |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3895 p = copybuf + STRLEN(copybuf); |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3896 if (after_pathsep(copybuf, p) && p[-1] == p[-2]) |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3897 // Ends with '//', use full path |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3898 if ((p = make_percent_swname(copybuf, fname)) != NULL) |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3899 { |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3900 backup = modname(p, backup_ext, FALSE); |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3901 vim_free(p); |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3902 } |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3903 #endif |
7 | 3904 rootname = get_file_in_dir(fname, copybuf); |
3905 if (rootname == NULL) | |
3906 { | |
3907 some_error = TRUE; /* out of memory */ | |
3908 goto nobackup; | |
3909 } | |
3910 | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3911 #if defined(UNIX) |
7 | 3912 did_set_shortname = FALSE; |
3913 #endif | |
3914 | |
3915 /* | |
3916 * May try twice if 'shortname' not set. | |
3917 */ | |
3918 for (;;) | |
3919 { | |
3920 /* | |
14475
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3921 * Make the backup file name. |
7 | 3922 */ |
14475
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3923 if (backup == NULL) |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3924 backup = buf_modname((buf->b_p_sn || buf->b_shortname), |
7 | 3925 rootname, backup_ext, FALSE); |
3926 if (backup == NULL) | |
3927 { | |
3928 vim_free(rootname); | |
3929 some_error = TRUE; /* out of memory */ | |
3930 goto nobackup; | |
3931 } | |
3932 | |
3933 /* | |
3934 * Check if backup file already exists. | |
3935 */ | |
3936 if (mch_stat((char *)backup, &st_new) >= 0) | |
3937 { | |
3938 #ifdef UNIX | |
3939 /* | |
3940 * Check if backup file is same as original file. | |
3941 * May happen when modname() gave the same file back. | |
3942 * E.g. silly link, or file name-length reached. | |
3943 * If we don't check here, we either ruin the file | |
3944 * when copying or erase it after writing. jw. | |
3945 */ | |
3946 if (st_new.st_dev == st_old.st_dev | |
3947 && st_new.st_ino == st_old.st_ino) | |
3948 { | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
3949 VIM_CLEAR(backup); /* no backup file to delete */ |
7 | 3950 /* |
3951 * may try again with 'shortname' set | |
3952 */ | |
3953 if (!(buf->b_shortname || buf->b_p_sn)) | |
3954 { | |
3955 buf->b_shortname = TRUE; | |
3956 did_set_shortname = TRUE; | |
3957 continue; | |
3958 } | |
3959 /* setting shortname didn't help */ | |
3960 if (did_set_shortname) | |
3961 buf->b_shortname = FALSE; | |
3962 break; | |
3963 } | |
3964 #endif | |
3965 | |
3966 /* | |
3967 * If we are not going to keep the backup file, don't | |
3968 * delete an existing one, try to use another name. | |
3969 * Change one character, just before the extension. | |
3970 */ | |
3971 if (!p_bk) | |
3972 { | |
3973 wp = backup + STRLEN(backup) - 1 | |
3974 - STRLEN(backup_ext); | |
3975 if (wp < backup) /* empty file name ??? */ | |
3976 wp = backup; | |
3977 *wp = 'z'; | |
3978 while (*wp > 'a' | |
3979 && mch_stat((char *)backup, &st_new) >= 0) | |
3980 --*wp; | |
3981 /* They all exist??? Must be something wrong. */ | |
3982 if (*wp == 'a') | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
3983 VIM_CLEAR(backup); |
7 | 3984 } |
3985 } | |
3986 break; | |
3987 } | |
3988 vim_free(rootname); | |
3989 | |
3990 /* | |
3991 * Try to create the backup file | |
3992 */ | |
3993 if (backup != NULL) | |
3994 { | |
3995 /* remove old backup, if present */ | |
3996 mch_remove(backup); | |
3997 /* Open with O_EXCL to avoid the file being created while | |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
3998 * we were sleeping (symlink hacker attack?). Reset umask |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
3999 * if possible to avoid mch_setperm() below. */ |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4000 #ifdef UNIX |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4001 umask_save = umask(0); |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4002 #endif |
7 | 4003 bfd = mch_open((char *)backup, |
557 | 4004 O_WRONLY|O_CREAT|O_EXTRA|O_EXCL|O_NOFOLLOW, |
4005 perm & 0777); | |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4006 #ifdef UNIX |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4007 (void)umask(umask_save); |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4008 #endif |
7 | 4009 if (bfd < 0) |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
4010 VIM_CLEAR(backup); |
7 | 4011 else |
4012 { | |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4013 /* Set file protection same as original file, but |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4014 * strip s-bit. Only needed if umask() wasn't used |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4015 * above. */ |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4016 #ifndef UNIX |
7 | 4017 (void)mch_setperm(backup, perm & 0777); |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4018 #else |
7 | 4019 /* |
4020 * Try to set the group of the backup same as the | |
4021 * original file. If this fails, set the protection | |
4022 * bits for the group same as the protection bits for | |
4023 * others. | |
4024 */ | |
557 | 4025 if (st_new.st_gid != st_old.st_gid |
7 | 4026 # ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */ |
557 | 4027 && fchown(bfd, (uid_t)-1, st_old.st_gid) != 0 |
7 | 4028 # endif |
4029 ) | |
4030 mch_setperm(backup, | |
4031 (perm & 0707) | ((perm & 07) << 3)); | |
5788 | 4032 # if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
1583 | 4033 mch_copy_sec(fname, backup); |
4034 # endif | |
7 | 4035 #endif |
4036 | |
4037 /* | |
4038 * copy the file. | |
4039 */ | |
4040 write_info.bw_fd = bfd; | |
4041 write_info.bw_buf = copybuf; | |
4042 #ifdef HAS_BW_FLAGS | |
4043 write_info.bw_flags = FIO_NOCONVERT; | |
4044 #endif | |
2664 | 4045 while ((write_info.bw_len = read_eintr(fd, copybuf, |
7 | 4046 BUFSIZE)) > 0) |
4047 { | |
4048 if (buf_write_bytes(&write_info) == FAIL) | |
4049 { | |
4050 errmsg = (char_u *)_("E506: Can't write to backup file (add ! to override)"); | |
4051 break; | |
4052 } | |
4053 ui_breakcheck(); | |
4054 if (got_int) | |
4055 { | |
4056 errmsg = (char_u *)_(e_interr); | |
4057 break; | |
4058 } | |
4059 } | |
4060 | |
4061 if (close(bfd) < 0 && errmsg == NULL) | |
4062 errmsg = (char_u *)_("E507: Close error for backup file (add ! to override)"); | |
4063 if (write_info.bw_len < 0) | |
4064 errmsg = (char_u *)_("E508: Can't read file for backup (add ! to override)"); | |
4065 #ifdef UNIX | |
4066 set_file_time(backup, st_old.st_atime, st_old.st_mtime); | |
4067 #endif | |
4068 #ifdef HAVE_ACL | |
4069 mch_set_acl(backup, acl); | |
4070 #endif | |
5788 | 4071 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
1583 | 4072 mch_copy_sec(fname, backup); |
4073 #endif | |
7 | 4074 break; |
4075 } | |
4076 } | |
4077 } | |
4078 nobackup: | |
4079 close(fd); /* ignore errors for closing read file */ | |
4080 vim_free(copybuf); | |
4081 | |
4082 if (backup == NULL && errmsg == NULL) | |
4083 errmsg = (char_u *)_("E509: Cannot create backup file (add ! to override)"); | |
4084 /* ignore errors when forceit is TRUE */ | |
4085 if ((some_error || errmsg != NULL) && !forceit) | |
4086 { | |
4087 retval = FAIL; | |
4088 goto fail; | |
4089 } | |
4090 errmsg = NULL; | |
4091 } | |
4092 else | |
4093 { | |
4094 char_u *dirp; | |
4095 char_u *p; | |
4096 char_u *rootname; | |
4097 | |
4098 /* | |
4099 * Make a backup by renaming the original file. | |
4100 */ | |
4101 /* | |
4102 * If 'cpoptions' includes the "W" flag, we don't want to | |
4103 * overwrite a read-only file. But rename may be possible | |
4104 * anyway, thus we need an extra check here. | |
4105 */ | |
4106 if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL) | |
4107 { | |
4108 errnum = (char_u *)"E504: "; | |
4109 errmsg = (char_u *)_(err_readonly); | |
4110 goto fail; | |
4111 } | |
4112 | |
4113 /* | |
4114 * | |
4115 * Form the backup file name - change path/fo.o.h to | |
4116 * path/fo.o.h.bak Try all directories in 'backupdir', first one | |
4117 * that works is used. | |
4118 */ | |
4119 dirp = p_bdir; | |
4120 while (*dirp) | |
4121 { | |
4122 /* | |
4123 * Isolate one directory name and make the backup file name. | |
4124 */ | |
4125 (void)copy_option_part(&dirp, IObuff, IOSIZE, ","); | |
14475
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4126 |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4127 #if defined(UNIX) || defined(WIN3264) |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4128 p = IObuff + STRLEN(IObuff); |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4129 if (after_pathsep(IObuff, p) && p[-1] == p[-2]) |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4130 // path ends with '//', use full path |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4131 if ((p = make_percent_swname(IObuff, fname)) != NULL) |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4132 { |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4133 backup = modname(p, backup_ext, FALSE); |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4134 vim_free(p); |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4135 } |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4136 #endif |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4137 if (backup == NULL) |
7 | 4138 { |
14475
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4139 rootname = get_file_in_dir(fname, IObuff); |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4140 if (rootname == NULL) |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4141 backup = NULL; |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4142 else |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4143 { |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4144 backup = buf_modname( |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4145 (buf->b_p_sn || buf->b_shortname), |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4146 rootname, backup_ext, FALSE); |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4147 vim_free(rootname); |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4148 } |
7 | 4149 } |
4150 | |
4151 if (backup != NULL) | |
4152 { | |
4153 /* | |
4154 * If we are not going to keep the backup file, don't | |
4155 * delete an existing one, try to use another name. | |
4156 * Change one character, just before the extension. | |
4157 */ | |
4158 if (!p_bk && mch_getperm(backup) >= 0) | |
4159 { | |
4160 p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext); | |
4161 if (p < backup) /* empty file name ??? */ | |
4162 p = backup; | |
4163 *p = 'z'; | |
4164 while (*p > 'a' && mch_getperm(backup) >= 0) | |
4165 --*p; | |
4166 /* They all exist??? Must be something wrong! */ | |
4167 if (*p == 'a') | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
4168 VIM_CLEAR(backup); |
7 | 4169 } |
4170 } | |
4171 if (backup != NULL) | |
4172 { | |
4173 /* | |
531 | 4174 * Delete any existing backup and move the current version |
4175 * to the backup. For safety, we don't remove the backup | |
4176 * until the write has finished successfully. And if the | |
4177 * 'backup' option is set, leave it around. | |
7 | 4178 */ |
4179 /* | |
4180 * If the renaming of the original file to the backup file | |
4181 * works, quit here. | |
4182 */ | |
4183 if (vim_rename(fname, backup) == 0) | |
4184 break; | |
4185 | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
4186 VIM_CLEAR(backup); /* don't do the rename below */ |
7 | 4187 } |
4188 } | |
4189 if (backup == NULL && !forceit) | |
4190 { | |
4191 errmsg = (char_u *)_("E510: Can't make backup file (add ! to override)"); | |
4192 goto fail; | |
4193 } | |
4194 } | |
4195 } | |
4196 | |
7410
08e62c4fc17d
commit https://github.com/vim/vim/commit/53076830fea6df737455523f7e235bfe4f79864d
Christian Brabandt <cb@256bit.org>
parents:
7408
diff
changeset
|
4197 #if defined(UNIX) |
7 | 4198 /* When using ":w!" and the file was read-only: make it writable */ |
4199 if (forceit && perm >= 0 && !(perm & 0200) && st_old.st_uid == getuid() | |
4200 && vim_strchr(p_cpo, CPO_FWRITE) == NULL) | |
4201 { | |
4202 perm |= 0200; | |
4203 (void)mch_setperm(fname, perm); | |
4204 made_writable = TRUE; | |
4205 } | |
4206 #endif | |
4207 | |
819 | 4208 /* When using ":w!" and writing to the current file, 'readonly' makes no |
164 | 4209 * sense, reset it, unless 'Z' appears in 'cpoptions'. */ |
4210 if (forceit && overwriting && vim_strchr(p_cpo, CPO_KEEPRO) == NULL) | |
7 | 4211 { |
4212 buf->b_p_ro = FALSE; | |
4213 #ifdef FEAT_TITLE | |
4214 need_maketitle = TRUE; /* set window title later */ | |
4215 #endif | |
4216 status_redraw_all(); /* redraw status lines later */ | |
4217 } | |
4218 | |
4219 if (end > buf->b_ml.ml_line_count) | |
4220 end = buf->b_ml.ml_line_count; | |
4221 if (buf->b_ml.ml_flags & ML_EMPTY) | |
4222 start = end + 1; | |
4223 | |
4224 /* | |
4225 * If the original file is being overwritten, there is a small chance that | |
4226 * we crash in the middle of writing. Therefore the file is preserved now. | |
4227 * This makes all block numbers positive so that recovery does not need | |
4228 * the original file. | |
4229 * Don't do this if there is a backup file and we are exiting. | |
4230 */ | |
39 | 4231 if (reset_changed && !newfile && overwriting |
7 | 4232 && !(exiting && backup != NULL)) |
4233 { | |
4234 ml_preserve(buf, FALSE); | |
4235 if (got_int) | |
4236 { | |
4237 errmsg = (char_u *)_(e_interr); | |
4238 goto restore_backup; | |
4239 } | |
4240 } | |
4241 | |
4242 #ifdef VMS | |
4243 vms_remove_version(fname); /* remove version */ | |
4244 #endif | |
1651 | 4245 /* Default: write the file directly. May write to a temp file for |
7 | 4246 * multi-byte conversion. */ |
4247 wfname = fname; | |
4248 | |
4249 #ifdef FEAT_MBYTE | |
4250 /* Check for forced 'fileencoding' from "++opt=val" argument. */ | |
4251 if (eap != NULL && eap->force_enc != 0) | |
4252 { | |
4253 fenc = eap->cmd + eap->force_enc; | |
4254 fenc = enc_canonize(fenc); | |
4255 fenc_tofree = fenc; | |
4256 } | |
4257 else | |
4258 fenc = buf->b_p_fenc; | |
4259 | |
4260 /* | |
1948 | 4261 * Check if the file needs to be converted. |
7 | 4262 */ |
1948 | 4263 converted = need_conversion(fenc); |
7 | 4264 |
4265 /* | |
4266 * Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done. Or | |
4267 * Latin1 to Unicode conversion. This is handled in buf_write_bytes(). | |
4268 * Prepare the flags for it and allocate bw_conv_buf when needed. | |
4269 */ | |
4270 if (converted && (enc_utf8 || STRCMP(p_enc, "latin1") == 0)) | |
4271 { | |
4272 wb_flags = get_fio_flags(fenc); | |
4273 if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8)) | |
4274 { | |
4275 /* Need to allocate a buffer to translate into. */ | |
4276 if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8)) | |
4277 write_info.bw_conv_buflen = bufsize * 2; | |
4278 else /* FIO_UCS4 */ | |
4279 write_info.bw_conv_buflen = bufsize * 4; | |
4280 write_info.bw_conv_buf | |
4281 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4282 if (write_info.bw_conv_buf == NULL) | |
4283 end = 0; | |
4284 } | |
4285 } | |
4286 | |
4287 # ifdef WIN3264 | |
4288 if (converted && wb_flags == 0 && (wb_flags = get_win_fio_flags(fenc)) != 0) | |
4289 { | |
4290 /* Convert UTF-8 -> UCS-2 and UCS-2 -> DBCS. Worst-case * 4: */ | |
4291 write_info.bw_conv_buflen = bufsize * 4; | |
4292 write_info.bw_conv_buf | |
4293 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4294 if (write_info.bw_conv_buf == NULL) | |
4295 end = 0; | |
4296 } | |
4297 # endif | |
4298 | |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
4299 # ifdef MACOS_CONVERT |
7 | 4300 if (converted && wb_flags == 0 && (wb_flags = get_mac_fio_flags(fenc)) != 0) |
4301 { | |
4302 write_info.bw_conv_buflen = bufsize * 3; | |
4303 write_info.bw_conv_buf | |
4304 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4305 if (write_info.bw_conv_buf == NULL) | |
4306 end = 0; | |
4307 } | |
4308 # endif | |
4309 | |
4310 # if defined(FEAT_EVAL) || defined(USE_ICONV) | |
4311 if (converted && wb_flags == 0) | |
4312 { | |
4313 # ifdef USE_ICONV | |
4314 /* | |
4315 * Use iconv() conversion when conversion is needed and it's not done | |
4316 * internally. | |
4317 */ | |
4318 write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc, | |
4319 enc_utf8 ? (char_u *)"utf-8" : p_enc); | |
4320 if (write_info.bw_iconv_fd != (iconv_t)-1) | |
4321 { | |
4322 /* We're going to use iconv(), allocate a buffer to convert in. */ | |
4323 write_info.bw_conv_buflen = bufsize * ICONV_MULT; | |
4324 write_info.bw_conv_buf | |
4325 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4326 if (write_info.bw_conv_buf == NULL) | |
4327 end = 0; | |
4328 write_info.bw_first = TRUE; | |
4329 } | |
4330 # ifdef FEAT_EVAL | |
4331 else | |
4332 # endif | |
4333 # endif | |
4334 | |
4335 # ifdef FEAT_EVAL | |
4336 /* | |
4337 * When the file needs to be converted with 'charconvert' after | |
4338 * writing, write to a temp file instead and let the conversion | |
4339 * overwrite the original file. | |
4340 */ | |
4341 if (*p_ccv != NUL) | |
4342 { | |
6721 | 4343 wfname = vim_tempname('w', FALSE); |
7 | 4344 if (wfname == NULL) /* Can't write without a tempfile! */ |
4345 { | |
4346 errmsg = (char_u *)_("E214: Can't find temp file for writing"); | |
4347 goto restore_backup; | |
4348 } | |
4349 } | |
4350 # endif | |
4351 } | |
4352 # endif | |
4353 if (converted && wb_flags == 0 | |
4354 # ifdef USE_ICONV | |
4355 && write_info.bw_iconv_fd == (iconv_t)-1 | |
4356 # endif | |
4357 # ifdef FEAT_EVAL | |
4358 && wfname == fname | |
4359 # endif | |
4360 ) | |
4361 { | |
4362 if (!forceit) | |
4363 { | |
4364 errmsg = (char_u *)_("E213: Cannot convert (add ! to write without conversion)"); | |
4365 goto restore_backup; | |
4366 } | |
4367 notconverted = TRUE; | |
4368 } | |
4369 #endif | |
4370 | |
4371 /* | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4372 * If conversion is taking place, we may first pretend to write and check |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4373 * for conversion errors. Then loop again to write for real. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4374 * When not doing conversion this writes for real right away. |
7 | 4375 */ |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4376 for (checking_conversion = TRUE; ; checking_conversion = FALSE) |
7 | 4377 { |
4378 /* | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4379 * There is no need to check conversion when: |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4380 * - there is no conversion |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4381 * - we make a backup file, that can be restored in case of conversion |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4382 * failure. |
7 | 4383 */ |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4384 #ifdef FEAT_MBYTE |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4385 if (!converted || dobackup) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4386 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4387 checking_conversion = FALSE; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4388 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4389 if (checking_conversion) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4390 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4391 /* Make sure we don't write anything. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4392 fd = -1; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4393 write_info.bw_fd = fd; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4394 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4395 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4396 { |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4397 #ifdef HAVE_FTRUNCATE |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4398 # define TRUNC_ON_OPEN 0 |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4399 #else |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4400 # define TRUNC_ON_OPEN O_TRUNC |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4401 #endif |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4402 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4403 * Open the file "wfname" for writing. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4404 * We may try to open the file twice: If we can't write to the file |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4405 * and forceit is TRUE we delete the existing file and try to |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4406 * create a new one. If this still fails we may have lost the |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4407 * original file! (this may happen when the user reached his |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4408 * quotum for number of files). |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4409 * Appending will fail if the file does not exist and forceit is |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4410 * FALSE. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4411 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4412 while ((fd = mch_open((char *)wfname, O_WRONLY | O_EXTRA | (append |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4413 ? (forceit ? (O_APPEND | O_CREAT) : O_APPEND) |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4414 : (O_CREAT | TRUNC_ON_OPEN)) |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4415 , perm < 0 ? 0666 : (perm & 0777))) < 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4416 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4417 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4418 * A forced write will try to create a new file if the old one |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4419 * is still readonly. This may also happen when the directory |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4420 * is read-only. In that case the mch_remove() will fail. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4421 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4422 if (errmsg == NULL) |
7 | 4423 { |
4424 #ifdef UNIX | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4425 stat_T st; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4426 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4427 /* Don't delete the file when it's a hard or symbolic link. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4428 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4429 if ((!newfile && st_old.st_nlink > 1) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4430 || (mch_lstat((char *)fname, &st) == 0 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4431 && (st.st_dev != st_old.st_dev |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4432 || st.st_ino != st_old.st_ino))) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4433 errmsg = (char_u *)_("E166: Can't open linked file for writing"); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4434 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4435 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4436 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4437 errmsg = (char_u *)_("E212: Can't open file for writing"); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4438 if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4439 && perm >= 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4440 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4441 #ifdef UNIX |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4442 /* we write to the file, thus it should be marked |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4443 writable after all */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4444 if (!(perm & 0200)) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4445 made_writable = TRUE; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4446 perm |= 0200; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4447 if (st_old.st_uid != getuid() |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4448 || st_old.st_gid != getgid()) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4449 perm &= 0777; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4450 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4451 if (!append) /* don't remove when appending */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4452 mch_remove(wfname); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4453 continue; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4454 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4455 } |
7 | 4456 } |
4457 | |
4458 restore_backup: | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4459 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4460 stat_T st; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4461 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4462 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4463 * If we failed to open the file, we don't need a backup. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4464 * Throw it away. If we moved or removed the original file |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4465 * try to put the backup in its place. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4466 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4467 if (backup != NULL && wfname == fname) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4468 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4469 if (backup_copy) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4470 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4471 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4472 * There is a small chance that we removed the |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4473 * original, try to move the copy in its place. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4474 * This may not work if the vim_rename() fails. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4475 * In that case we leave the copy around. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4476 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4477 /* If file does not exist, put the copy in its |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4478 * place */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4479 if (mch_stat((char *)fname, &st) < 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4480 vim_rename(backup, fname); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4481 /* if original file does exist throw away the copy |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4482 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4483 if (mch_stat((char *)fname, &st) >= 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4484 mch_remove(backup); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4485 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4486 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4487 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4488 /* try to put the original file back */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4489 vim_rename(backup, fname); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4490 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4491 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4492 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4493 /* if original file no longer exists give an extra warning |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4494 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4495 if (!newfile && mch_stat((char *)fname, &st) < 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4496 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4497 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4498 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4499 #ifdef FEAT_MBYTE |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4500 if (wfname != fname) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4501 vim_free(wfname); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4502 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4503 goto fail; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4504 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4505 write_info.bw_fd = fd; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4506 |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4507 #if defined(UNIX) |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4508 { |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4509 stat_T st; |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4510 |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4511 /* Double check we are writing the intended file before making |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4512 * any changes. */ |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4513 if (overwriting |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4514 && (!dobackup || backup_copy) |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4515 && fname == wfname |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4516 && perm >= 0 |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4517 && mch_fstat(fd, &st) == 0 |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4518 && st.st_ino != st_old.st_ino) |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4519 { |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4520 close(fd); |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4521 errmsg = (char_u *)_("E949: File changed while writing"); |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4522 goto fail; |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4523 } |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4524 } |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4525 #endif |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4526 #ifdef HAVE_FTRUNCATE |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4527 if (!append) |
12861
d07c1817dd98
patch 8.0.1307: compiler warning for ignoring return value
Christian Brabandt <cb@256bit.org>
parents:
12857
diff
changeset
|
4528 ignored = ftruncate(fd, (off_t)0); |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4529 #endif |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4530 |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
4531 #if defined(WIN3264) |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4532 if (backup != NULL && overwriting && !append) |
7 | 4533 { |
4534 if (backup_copy) | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4535 (void)mch_copy_file_attribute(wfname, backup); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4536 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4537 (void)mch_copy_file_attribute(backup, wfname); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4538 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4539 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4540 if (!overwriting && !append) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4541 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4542 if (buf->b_ffname != NULL) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4543 (void)mch_copy_file_attribute(buf->b_ffname, wfname); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4544 /* Should copy resource fork */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4545 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4546 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4547 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4548 #ifdef FEAT_CRYPT |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4549 if (*buf->b_p_key != NUL && !filtering) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4550 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4551 char_u *header; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4552 int header_len; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4553 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4554 buf->b_cryptstate = crypt_create_for_writing( |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4555 crypt_get_method_nr(buf), |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4556 buf->b_p_key, &header, &header_len); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4557 if (buf->b_cryptstate == NULL || header == NULL) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4558 end = 0; |
7 | 4559 else |
4560 { | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4561 /* Write magic number, so that Vim knows how this file is |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4562 * encrypted when reading it back. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4563 write_info.bw_buf = header; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4564 write_info.bw_len = header_len; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4565 write_info.bw_flags = FIO_NOCONVERT; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4566 if (buf_write_bytes(&write_info) == FAIL) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4567 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4568 wb_flags |= FIO_ENCRYPTED; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4569 vim_free(header); |
7 | 4570 } |
4571 } | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4572 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4573 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4574 errmsg = NULL; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4575 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4576 write_info.bw_buf = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4577 nchars = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4578 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4579 /* use "++bin", "++nobin" or 'binary' */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4580 if (eap != NULL && eap->force_bin != 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4581 write_bin = (eap->force_bin == FORCE_BIN); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4582 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4583 write_bin = buf->b_p_bin; |
7 | 4584 |
4585 #ifdef FEAT_MBYTE | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4586 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4587 * The BOM is written just after the encryption magic number. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4588 * Skip it when appending and the file already existed, the BOM only |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4589 * makes sense at the start of the file. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4590 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4591 if (buf->b_p_bomb && !write_bin && (!append || perm < 0)) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4592 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4593 write_info.bw_len = make_bom(buffer, fenc); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4594 if (write_info.bw_len > 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4595 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4596 /* don't convert, do encryption */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4597 write_info.bw_flags = FIO_NOCONVERT | wb_flags; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4598 if (buf_write_bytes(&write_info) == FAIL) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4599 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4600 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4601 nchars += write_info.bw_len; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4602 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4603 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4604 write_info.bw_start_lnum = start; |
7 | 4605 #endif |
4606 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4607 #ifdef FEAT_PERSISTENT_UNDO |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4608 write_undo_file = (buf->b_p_udf |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4609 && overwriting |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4610 && !append |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4611 && !filtering |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4612 && reset_changed |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4613 && !checking_conversion); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4614 if (write_undo_file) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4615 /* Prepare for computing the hash value of the text. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4616 sha256_start(&sha_ctx); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4617 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4618 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4619 write_info.bw_len = bufsize; |
7 | 4620 #ifdef HAS_BW_FLAGS |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4621 write_info.bw_flags = wb_flags; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4622 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4623 fileformat = get_fileformat_force(buf, eap); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4624 s = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4625 len = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4626 for (lnum = start; lnum <= end; ++lnum) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4627 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4628 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4629 * The next while loop is done once for each character written. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4630 * Keep it fast! |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4631 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4632 ptr = ml_get_buf(buf, lnum, FALSE) - 1; |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4633 #ifdef FEAT_PERSISTENT_UNDO |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4634 if (write_undo_file) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4635 sha256_update(&sha_ctx, ptr + 1, |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4636 (UINT32_T)(STRLEN(ptr + 1) + 1)); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4637 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4638 while ((c = *++ptr) != NUL) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4639 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4640 if (c == NL) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4641 *s = NUL; /* replace newlines with NULs */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4642 else if (c == CAR && fileformat == EOL_MAC) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4643 *s = NL; /* Mac: replace CRs with NLs */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4644 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4645 *s = c; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4646 ++s; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4647 if (++len != bufsize) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4648 continue; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4649 if (buf_write_bytes(&write_info) == FAIL) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4650 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4651 end = 0; /* write error: break loop */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4652 break; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4653 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4654 nchars += bufsize; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4655 s = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4656 len = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4657 #ifdef FEAT_MBYTE |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4658 write_info.bw_start_lnum = lnum; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4659 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4660 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4661 /* write failed or last line has no EOL: stop here */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4662 if (end == 0 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4663 || (lnum == end |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4664 && (write_bin || !buf->b_p_fixeol) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4665 && (lnum == buf->b_no_eol_lnum |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4666 || (lnum == buf->b_ml.ml_line_count |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4667 && !buf->b_p_eol)))) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4668 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4669 ++lnum; /* written the line, count it */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4670 no_eol = TRUE; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4671 break; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4672 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4673 if (fileformat == EOL_UNIX) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4674 *s++ = NL; |
7 | 4675 else |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4676 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4677 *s++ = CAR; /* EOL_MAC or EOL_DOS: write CR */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4678 if (fileformat == EOL_DOS) /* write CR-NL */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4679 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4680 if (++len == bufsize) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4681 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4682 if (buf_write_bytes(&write_info) == FAIL) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4683 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4684 end = 0; /* write error: break loop */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4685 break; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4686 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4687 nchars += bufsize; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4688 s = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4689 len = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4690 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4691 *s++ = NL; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4692 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4693 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4694 if (++len == bufsize && end) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4695 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4696 if (buf_write_bytes(&write_info) == FAIL) |
7 | 4697 { |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4698 end = 0; /* write error: break loop */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4699 break; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4700 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4701 nchars += bufsize; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4702 s = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4703 len = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4704 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4705 ui_breakcheck(); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4706 if (got_int) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4707 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4708 end = 0; /* Interrupted, break loop */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4709 break; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4710 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4711 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4712 #ifdef VMS |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4713 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4714 * On VMS there is a problem: newlines get added when writing |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4715 * blocks at a time. Fix it by writing a line at a time. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4716 * This is much slower! |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4717 * Explanation: VAX/DECC RTL insists that records in some RMS |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4718 * structures end with a newline (carriage return) character, and |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4719 * if they don't it adds one. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4720 * With other RMS structures it works perfect without this fix. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4721 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4722 if (buf->b_fab_rfm == FAB$C_VFC |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4723 || ((buf->b_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0)) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4724 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4725 int b2write; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4726 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4727 buf->b_fab_mrs = (buf->b_fab_mrs == 0 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4728 ? MIN(4096, bufsize) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4729 : MIN(buf->b_fab_mrs, bufsize)); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4730 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4731 b2write = len; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4732 while (b2write > 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4733 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4734 write_info.bw_len = MIN(b2write, buf->b_fab_mrs); |
7 | 4735 if (buf_write_bytes(&write_info) == FAIL) |
4736 { | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4737 end = 0; |
7 | 4738 break; |
4739 } | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4740 b2write -= MIN(b2write, buf->b_fab_mrs); |
7 | 4741 } |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4742 write_info.bw_len = bufsize; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4743 nchars += len; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4744 s = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4745 len = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4746 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4747 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4748 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4749 if (len > 0 && end > 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4750 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4751 write_info.bw_len = len; |
7 | 4752 if (buf_write_bytes(&write_info) == FAIL) |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4753 end = 0; /* write error */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4754 nchars += len; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4755 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4756 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4757 /* Stop when writing done or an error was encountered. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4758 if (!checking_conversion || end == 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4759 break; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4760 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4761 /* If no error happened until now, writing should be ok, so loop to |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4762 * really write the buffer. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4763 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4764 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4765 /* If we started writing, finish writing. Also when an error was |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4766 * encountered. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4767 if (!checking_conversion) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4768 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4769 #if defined(UNIX) && defined(HAVE_FSYNC) |
7 | 4770 /* |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4771 * On many journalling file systems there is a bug that causes both the |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4772 * original and the backup file to be lost when halting the system |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4773 * right after writing the file. That's because only the meta-data is |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4774 * journalled. Syncing the file slows down the system, but assures it |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4775 * has been written to disk and we don't lose it. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4776 * For a device do try the fsync() but don't complain if it does not |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4777 * work (could be a pipe). |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4778 * If the 'fsync' option is FALSE, don't fsync(). Useful for laptops. |
7 | 4779 */ |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4780 if (p_fs && fsync(fd) != 0 && !device) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4781 { |
12857
ffdf2e4b5d9a
patch 8.0.1305: writefile() never calls fsync()
Christian Brabandt <cb@256bit.org>
parents:
12847
diff
changeset
|
4782 errmsg = (char_u *)_(e_fsync); |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4783 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4784 } |
7 | 4785 #endif |
4786 | |
5788 | 4787 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4788 /* Probably need to set the security context. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4789 if (!backup_copy) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4790 mch_copy_sec(backup, wfname); |
1583 | 4791 #endif |
4792 | |
557 | 4793 #ifdef UNIX |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4794 /* When creating a new file, set its owner/group to that of the |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4795 * original file. Get the new device and inode number. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4796 if (backup != NULL && !backup_copy) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4797 { |
557 | 4798 # ifdef HAVE_FCHOWN |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4799 stat_T st; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4800 |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4801 /* Don't change the owner when it's already OK, some systems remove |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4802 * permission or ACL stuff. */ |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4803 if (mch_stat((char *)wfname, &st) < 0 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4804 || st.st_uid != st_old.st_uid |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4805 || st.st_gid != st_old.st_gid) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4806 { |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4807 /* changing owner might not be possible */ |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4808 ignored = fchown(fd, st_old.st_uid, -1); |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4809 /* if changing group fails clear the group permissions */ |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4810 if (fchown(fd, -1, st_old.st_gid) == -1 && perm > 0) |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4811 perm &= ~070; |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4812 } |
557 | 4813 # endif |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4814 buf_setino(buf); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4815 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4816 else if (!buf->b_dev_valid) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4817 /* Set the inode when creating a new file. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4818 buf_setino(buf); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4819 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4820 |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4821 #ifdef UNIX |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4822 if (made_writable) |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4823 perm &= ~0200; /* reset 'w' bit for security reasons */ |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4824 #endif |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4825 #ifdef HAVE_FCHMOD |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4826 /* set permission of new file same as old file */ |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4827 if (perm >= 0) |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4828 (void)mch_fsetperm(fd, perm); |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4829 #endif |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4830 if (close(fd) != 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4831 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4832 errmsg = (char_u *)_("E512: Close failed"); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4833 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4834 } |
7 | 4835 |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4836 #ifndef HAVE_FCHMOD |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4837 /* set permission of new file same as old file */ |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4838 if (perm >= 0) |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4839 (void)mch_setperm(wfname, perm); |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4840 #endif |
7 | 4841 #ifdef HAVE_ACL |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4842 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4843 * Probably need to set the ACL before changing the user (can't set the |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4844 * ACL on a file the user doesn't own). |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4845 * On Solaris, with ZFS and the aclmode property set to "discard" (the |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4846 * default), chmod() discards all part of a file's ACL that don't |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4847 * represent the mode of the file. It's non-trivial for us to discover |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4848 * whether we're in that situation, so we simply always re-set the ACL. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4849 */ |
9509
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
4850 # ifndef HAVE_SOLARIS_ZFS_ACL |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4851 if (!backup_copy) |
9509
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
4852 # endif |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4853 mch_set_acl(wfname, acl); |
7 | 4854 #endif |
2267 | 4855 #ifdef FEAT_CRYPT |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4856 if (buf->b_cryptstate != NULL) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4857 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4858 crypt_free_state(buf->b_cryptstate); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4859 buf->b_cryptstate = NULL; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4860 } |
6122 | 4861 #endif |
7 | 4862 |
4863 #if defined(FEAT_MBYTE) && defined(FEAT_EVAL) | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4864 if (wfname != fname) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4865 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4866 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4867 * The file was written to a temp file, now it needs to be |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4868 * converted with 'charconvert' to (overwrite) the output file. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4869 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4870 if (end != 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4871 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4872 if (eval_charconvert(enc_utf8 ? (char_u *)"utf-8" : p_enc, |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4873 fenc, wfname, fname) == FAIL) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4874 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4875 write_info.bw_conv_error = TRUE; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4876 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4877 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4878 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4879 mch_remove(wfname); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4880 vim_free(wfname); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4881 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4882 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4883 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4884 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4885 if (end == 0) |
7 | 4886 { |
4887 /* | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4888 * Error encountered. |
7 | 4889 */ |
4890 if (errmsg == NULL) | |
4891 { | |
4892 #ifdef FEAT_MBYTE | |
4893 if (write_info.bw_conv_error) | |
1947 | 4894 { |
4895 if (write_info.bw_conv_error_lnum == 0) | |
4896 errmsg = (char_u *)_("E513: write error, conversion failed (make 'fenc' empty to override)"); | |
4897 else | |
4898 { | |
4899 errmsg_allocated = TRUE; | |
4900 errmsg = alloc(300); | |
4901 vim_snprintf((char *)errmsg, 300, _("E513: write error, conversion failed in line %ld (make 'fenc' empty to override)"), | |
4902 (long)write_info.bw_conv_error_lnum); | |
4903 } | |
4904 } | |
7 | 4905 else |
4906 #endif | |
4907 if (got_int) | |
4908 errmsg = (char_u *)_(e_interr); | |
4909 else | |
4910 errmsg = (char_u *)_("E514: write error (file system full?)"); | |
4911 } | |
4912 | |
4913 /* | |
4914 * If we have a backup file, try to put it in place of the new file, | |
1698 | 4915 * because the new file is probably corrupt. This avoids losing the |
7 | 4916 * original file when trying to make a backup when writing the file a |
4917 * second time. | |
4918 * When "backup_copy" is set we need to copy the backup over the new | |
4919 * file. Otherwise rename the backup file. | |
4920 * If this is OK, don't give the extra warning message. | |
4921 */ | |
4922 if (backup != NULL) | |
4923 { | |
4924 if (backup_copy) | |
4925 { | |
4926 /* This may take a while, if we were interrupted let the user | |
4927 * know we got the message. */ | |
4928 if (got_int) | |
4929 { | |
4930 MSG(_(e_interr)); | |
4931 out_flush(); | |
4932 } | |
4933 if ((fd = mch_open((char *)backup, O_RDONLY | O_EXTRA, 0)) >= 0) | |
4934 { | |
4935 if ((write_info.bw_fd = mch_open((char *)fname, | |
194 | 4936 O_WRONLY | O_CREAT | O_TRUNC | O_EXTRA, |
4937 perm & 0777)) >= 0) | |
7 | 4938 { |
4939 /* copy the file. */ | |
4940 write_info.bw_buf = smallbuf; | |
4941 #ifdef HAS_BW_FLAGS | |
4942 write_info.bw_flags = FIO_NOCONVERT; | |
4943 #endif | |
2664 | 4944 while ((write_info.bw_len = read_eintr(fd, smallbuf, |
7 | 4945 SMBUFSIZE)) > 0) |
4946 if (buf_write_bytes(&write_info) == FAIL) | |
4947 break; | |
4948 | |
4949 if (close(write_info.bw_fd) >= 0 | |
4950 && write_info.bw_len == 0) | |
4951 end = 1; /* success */ | |
4952 } | |
4953 close(fd); /* ignore errors for closing read file */ | |
4954 } | |
4955 } | |
4956 else | |
4957 { | |
4958 if (vim_rename(backup, fname) == 0) | |
4959 end = 1; | |
4960 } | |
4961 } | |
4962 goto fail; | |
4963 } | |
4964 | |
4965 lnum -= start; /* compute number of written lines */ | |
4966 --no_wait_return; /* may wait for return now */ | |
4967 | |
4968 #if !(defined(UNIX) || defined(VMS)) | |
4969 fname = sfname; /* use shortname now, for the messages */ | |
4970 #endif | |
4971 if (!filtering) | |
4972 { | |
4973 msg_add_fname(buf, fname); /* put fname in IObuff with quotes */ | |
4974 c = FALSE; | |
4975 #ifdef FEAT_MBYTE | |
4976 if (write_info.bw_conv_error) | |
4977 { | |
4978 STRCAT(IObuff, _(" CONVERSION ERROR")); | |
4979 c = TRUE; | |
1947 | 4980 if (write_info.bw_conv_error_lnum != 0) |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2267
diff
changeset
|
4981 vim_snprintf_add((char *)IObuff, IOSIZE, _(" in line %ld;"), |
1947 | 4982 (long)write_info.bw_conv_error_lnum); |
7 | 4983 } |
4984 else if (notconverted) | |
4985 { | |
4986 STRCAT(IObuff, _("[NOT converted]")); | |
4987 c = TRUE; | |
4988 } | |
4989 else if (converted) | |
4990 { | |
4991 STRCAT(IObuff, _("[converted]")); | |
4992 c = TRUE; | |
4993 } | |
4994 #endif | |
4995 if (device) | |
4996 { | |
4997 STRCAT(IObuff, _("[Device]")); | |
4998 c = TRUE; | |
4999 } | |
5000 else if (newfile) | |
5001 { | |
5002 STRCAT(IObuff, shortmess(SHM_NEW) ? _("[New]") : _("[New File]")); | |
5003 c = TRUE; | |
5004 } | |
5005 if (no_eol) | |
5006 { | |
5007 msg_add_eol(); | |
5008 c = TRUE; | |
5009 } | |
5010 /* may add [unix/dos/mac] */ | |
5011 if (msg_add_fileformat(fileformat)) | |
5012 c = TRUE; | |
5013 #ifdef FEAT_CRYPT | |
5014 if (wb_flags & FIO_ENCRYPTED) | |
5015 { | |
6122 | 5016 crypt_append_msg(buf); |
7 | 5017 c = TRUE; |
5018 } | |
5019 #endif | |
5020 msg_add_lines(c, (long)lnum, nchars); /* add line/char count */ | |
5021 if (!shortmess(SHM_WRITE)) | |
5022 { | |
5023 if (append) | |
5024 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [a]") : _(" appended")); | |
5025 else | |
5026 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [w]") : _(" written")); | |
5027 } | |
5028 | |
679 | 5029 set_keep_msg(msg_trunc_attr(IObuff, FALSE, 0), 0); |
7 | 5030 } |
5031 | |
39 | 5032 /* When written everything correctly: reset 'modified'. Unless not |
5033 * writing to the original file and '+' is not in 'cpoptions'. */ | |
589 | 5034 if (reset_changed && whole && !append |
7 | 5035 #ifdef FEAT_MBYTE |
5036 && !write_info.bw_conv_error | |
5037 #endif | |
39 | 5038 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL) |
5039 ) | |
7 | 5040 { |
5041 unchanged(buf, TRUE); | |
10952
835604f3c37a
patch 8.0.0365: might free a dict item that wasn't allocated
Christian Brabandt <cb@256bit.org>
parents:
10889
diff
changeset
|
5042 /* b:changedtick is always incremented in unchanged() but that |
6320 | 5043 * should not trigger a TextChanged event. */ |
13240
5ed6e4a25925
patch 8.0.1494: no autocmd triggered in Insert mode with visible popup menu
Christian Brabandt <cb@256bit.org>
parents:
13170
diff
changeset
|
5044 if (buf->b_last_changedtick + 1 == CHANGEDTICK(buf)) |
5ed6e4a25925
patch 8.0.1494: no autocmd triggered in Insert mode with visible popup menu
Christian Brabandt <cb@256bit.org>
parents:
13170
diff
changeset
|
5045 buf->b_last_changedtick = CHANGEDTICK(buf); |
7 | 5046 u_unchanged(buf); |
2281
e41433ea71df
Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents:
2280
diff
changeset
|
5047 u_update_save_nr(buf); |
7 | 5048 } |
5049 | |
5050 /* | |
5051 * If written to the current file, update the timestamp of the swap file | |
5052 * and reset the BF_WRITE_MASK flags. Also sets buf->b_mtime. | |
5053 */ | |
5054 if (overwriting) | |
5055 { | |
5056 ml_timestamp(buf); | |
589 | 5057 if (append) |
5058 buf->b_flags &= ~BF_NEW; | |
5059 else | |
5060 buf->b_flags &= ~BF_WRITE_MASK; | |
7 | 5061 } |
5062 | |
5063 /* | |
5064 * If we kept a backup until now, and we are in patch mode, then we make | |
5065 * the backup file our 'original' file. | |
5066 */ | |
5067 if (*p_pm && dobackup) | |
5068 { | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
5069 char *org = (char *)buf_modname((buf->b_p_sn || buf->b_shortname), |
7 | 5070 fname, p_pm, FALSE); |
5071 | |
5072 if (backup != NULL) | |
5073 { | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
5074 stat_T st; |
7 | 5075 |
5076 /* | |
5077 * If the original file does not exist yet | |
5078 * the current backup file becomes the original file | |
5079 */ | |
5080 if (org == NULL) | |
5081 EMSG(_("E205: Patchmode: can't save original file")); | |
5082 else if (mch_stat(org, &st) < 0) | |
5083 { | |
5084 vim_rename(backup, (char_u *)org); | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
5085 VIM_CLEAR(backup); /* don't delete the file */ |
7 | 5086 #ifdef UNIX |
5087 set_file_time((char_u *)org, st_old.st_atime, st_old.st_mtime); | |
5088 #endif | |
5089 } | |
5090 } | |
5091 /* | |
5092 * If there is no backup file, remember that a (new) file was | |
5093 * created. | |
5094 */ | |
5095 else | |
5096 { | |
5097 int empty_fd; | |
5098 | |
5099 if (org == NULL | |
557 | 5100 || (empty_fd = mch_open(org, |
5101 O_CREAT | O_EXTRA | O_EXCL | O_NOFOLLOW, | |
216 | 5102 perm < 0 ? 0666 : (perm & 0777))) < 0) |
7 | 5103 EMSG(_("E206: patchmode: can't touch empty original file")); |
5104 else | |
5105 close(empty_fd); | |
5106 } | |
5107 if (org != NULL) | |
5108 { | |
5109 mch_setperm((char_u *)org, mch_getperm(fname) & 0777); | |
5110 vim_free(org); | |
5111 } | |
5112 } | |
5113 | |
5114 /* | |
5115 * Remove the backup unless 'backup' option is set | |
5116 */ | |
5117 if (!p_bk && backup != NULL && mch_remove(backup) != 0) | |
5118 EMSG(_("E207: Can't delete backup file")); | |
5119 | |
5120 #ifdef FEAT_SUN_WORKSHOP | |
5121 if (usingSunWorkShop) | |
5122 workshop_file_saved((char *) ffname); | |
5123 #endif | |
5124 | |
5125 goto nofail; | |
5126 | |
5127 /* | |
5128 * Finish up. We get here either after failure or success. | |
5129 */ | |
5130 fail: | |
5131 --no_wait_return; /* may wait for return now */ | |
5132 nofail: | |
5133 | |
5134 /* Done saving, we accept changed buffer warnings again */ | |
5135 buf->b_saving = FALSE; | |
5136 | |
5137 vim_free(backup); | |
5138 if (buffer != smallbuf) | |
5139 vim_free(buffer); | |
5140 #ifdef FEAT_MBYTE | |
5141 vim_free(fenc_tofree); | |
5142 vim_free(write_info.bw_conv_buf); | |
5143 # ifdef USE_ICONV | |
5144 if (write_info.bw_iconv_fd != (iconv_t)-1) | |
5145 { | |
5146 iconv_close(write_info.bw_iconv_fd); | |
5147 write_info.bw_iconv_fd = (iconv_t)-1; | |
5148 } | |
5149 # endif | |
5150 #endif | |
5151 #ifdef HAVE_ACL | |
5152 mch_free_acl(acl); | |
5153 #endif | |
5154 | |
5155 if (errmsg != NULL) | |
5156 { | |
835 | 5157 int numlen = errnum != NULL ? (int)STRLEN(errnum) : 0; |
7 | 5158 |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
5159 attr = HL_ATTR(HLF_E); /* set highlight for error messages */ |
7 | 5160 msg_add_fname(buf, |
5161 #ifndef UNIX | |
5162 sfname | |
5163 #else | |
5164 fname | |
5165 #endif | |
5166 ); /* put file name in IObuff with quotes */ | |
5167 if (STRLEN(IObuff) + STRLEN(errmsg) + numlen >= IOSIZE) | |
5168 IObuff[IOSIZE - STRLEN(errmsg) - numlen - 1] = NUL; | |
5169 /* If the error message has the form "is ...", put the error number in | |
5170 * front of the file name. */ | |
5171 if (errnum != NULL) | |
5172 { | |
1620 | 5173 STRMOVE(IObuff + numlen, IObuff); |
7 | 5174 mch_memmove(IObuff, errnum, (size_t)numlen); |
5175 } | |
5176 STRCAT(IObuff, errmsg); | |
5177 emsg(IObuff); | |
1947 | 5178 if (errmsg_allocated) |
5179 vim_free(errmsg); | |
7 | 5180 |
5181 retval = FAIL; | |
5182 if (end == 0) | |
5183 { | |
5184 MSG_PUTS_ATTR(_("\nWARNING: Original file may be lost or damaged\n"), | |
5185 attr | MSG_HIST); | |
5186 MSG_PUTS_ATTR(_("don't quit the editor until the file is successfully written!"), | |
5187 attr | MSG_HIST); | |
5188 | |
5189 /* Update the timestamp to avoid an "overwrite changed file" | |
5190 * prompt when writing again. */ | |
5191 if (mch_stat((char *)fname, &st_old) >= 0) | |
5192 { | |
5193 buf_store_time(buf, &st_old, fname); | |
5194 buf->b_mtime_read = buf->b_mtime; | |
5195 } | |
5196 } | |
5197 } | |
5198 msg_scroll = msg_save; | |
5199 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5200 #ifdef FEAT_PERSISTENT_UNDO |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5201 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5202 * When writing the whole file and 'undofile' is set, also write the undo |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5203 * file. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5204 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5205 if (retval == OK && write_undo_file) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5206 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5207 char_u hash[UNDO_HASH_SIZE]; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5208 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5209 sha256_finish(&sha_ctx, hash); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5210 u_write_undo(NULL, FALSE, buf, hash); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5211 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5212 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5213 |
7 | 5214 #ifdef FEAT_EVAL |
5215 if (!should_abort(retval)) | |
5216 #else | |
5217 if (!got_int) | |
5218 #endif | |
5219 { | |
5220 aco_save_T aco; | |
5221 | |
3482 | 5222 curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */ |
5223 | |
7 | 5224 /* |
5225 * Apply POST autocommands. | |
5226 * Careful: The autocommands may call buf_write() recursively! | |
5227 */ | |
5228 aucmd_prepbuf(&aco, buf); | |
5229 | |
5230 if (append) | |
5231 apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname, | |
5232 FALSE, curbuf, eap); | |
5233 else if (filtering) | |
5234 apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname, | |
5235 FALSE, curbuf, eap); | |
5236 else if (reset_changed && whole) | |
5237 apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname, | |
5238 FALSE, curbuf, eap); | |
5239 else | |
5240 apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname, | |
5241 FALSE, curbuf, eap); | |
5242 | |
5243 /* restore curwin/curbuf and a few other things */ | |
5244 aucmd_restbuf(&aco); | |
5245 | |
5246 #ifdef FEAT_EVAL | |
5247 if (aborting()) /* autocmds may abort script processing */ | |
5248 retval = FALSE; | |
5249 #endif | |
5250 } | |
5251 | |
5252 got_int |= prev_got_int; | |
5253 | |
5254 return retval; | |
5255 } | |
5256 | |
5257 /* | |
633 | 5258 * Set the name of the current buffer. Use when the buffer doesn't have a |
5259 * name and a ":r" or ":w" command with a file name is used. | |
5260 */ | |
5261 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5262 set_rw_fname(char_u *fname, char_u *sfname) |
633 | 5263 { |
1905 | 5264 buf_T *buf = curbuf; |
5265 | |
633 | 5266 /* It's like the unnamed buffer is deleted.... */ |
5267 if (curbuf->b_p_bl) | |
5268 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); | |
5269 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf); | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
5270 #ifdef FEAT_EVAL |
633 | 5271 if (aborting()) /* autocmds may abort script processing */ |
5272 return FAIL; | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
5273 #endif |
1905 | 5274 if (curbuf != buf) |
5275 { | |
5276 /* We are in another buffer now, don't do the renaming. */ | |
5277 EMSG(_(e_auchangedbuf)); | |
5278 return FAIL; | |
5279 } | |
633 | 5280 |
5281 if (setfname(curbuf, fname, sfname, FALSE) == OK) | |
5282 curbuf->b_flags |= BF_NOTEDITED; | |
5283 | |
5284 /* ....and a new named one is created */ | |
5285 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf); | |
5286 if (curbuf->b_p_bl) | |
5287 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf); | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
5288 #ifdef FEAT_EVAL |
633 | 5289 if (aborting()) /* autocmds may abort script processing */ |
5290 return FAIL; | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
5291 #endif |
633 | 5292 |
5293 /* Do filetype detection now if 'filetype' is empty. */ | |
5294 if (*curbuf->b_p_ft == NUL) | |
5295 { | |
819 | 5296 if (au_has_group((char_u *)"filetypedetect")) |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
5297 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL); |
717 | 5298 do_modelines(0); |
633 | 5299 } |
5300 | |
5301 return OK; | |
5302 } | |
5303 | |
5304 /* | |
7 | 5305 * Put file name into IObuff with quotes. |
5306 */ | |
33 | 5307 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5308 msg_add_fname(buf_T *buf, char_u *fname) |
7 | 5309 { |
5310 if (fname == NULL) | |
5311 fname = (char_u *)"-stdin-"; | |
5312 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE); | |
5313 IObuff[0] = '"'; | |
5314 STRCAT(IObuff, "\" "); | |
5315 } | |
5316 | |
5317 /* | |
5318 * Append message for text mode to IObuff. | |
5319 * Return TRUE if something appended. | |
5320 */ | |
5321 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5322 msg_add_fileformat(int eol_type) |
7 | 5323 { |
5324 #ifndef USE_CRNL | |
5325 if (eol_type == EOL_DOS) | |
5326 { | |
5327 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]")); | |
5328 return TRUE; | |
5329 } | |
5330 #endif | |
5331 #ifndef USE_CR | |
5332 if (eol_type == EOL_MAC) | |
5333 { | |
5334 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]")); | |
5335 return TRUE; | |
5336 } | |
5337 #endif | |
5338 #if defined(USE_CRNL) || defined(USE_CR) | |
5339 if (eol_type == EOL_UNIX) | |
5340 { | |
5341 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]")); | |
5342 return TRUE; | |
5343 } | |
5344 #endif | |
5345 return FALSE; | |
5346 } | |
5347 | |
5348 /* | |
5349 * Append line and character count to IObuff. | |
5350 */ | |
33 | 5351 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5352 msg_add_lines( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5353 int insert_space, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5354 long lnum, |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
5355 off_T nchars) |
7 | 5356 { |
5357 char_u *p; | |
5358 | |
5359 p = IObuff + STRLEN(IObuff); | |
5360 | |
5361 if (insert_space) | |
5362 *p++ = ' '; | |
5363 if (shortmess(SHM_LINES)) | |
9391
4c40631238e4
commit https://github.com/vim/vim/commit/bde9810d6103ffe3a22a9330021cb21db1ed1792
Christian Brabandt <cb@256bit.org>
parents:
9389
diff
changeset
|
5364 vim_snprintf((char *)p, IOSIZE - (p - IObuff), |
13610
e76499e85744
patch 8.0.1677: no compiler warning for wrong format in vim_snprintf()
Christian Brabandt <cb@256bit.org>
parents:
13575
diff
changeset
|
5365 "%ldL, %lldC", lnum, (long long)nchars); |
7 | 5366 else |
5367 { | |
5368 if (lnum == 1) | |
5369 STRCPY(p, _("1 line, ")); | |
5370 else | |
5371 sprintf((char *)p, _("%ld lines, "), lnum); | |
5372 p += STRLEN(p); | |
5373 if (nchars == 1) | |
5374 STRCPY(p, _("1 character")); | |
5375 else | |
9391
4c40631238e4
commit https://github.com/vim/vim/commit/bde9810d6103ffe3a22a9330021cb21db1ed1792
Christian Brabandt <cb@256bit.org>
parents:
9389
diff
changeset
|
5376 vim_snprintf((char *)p, IOSIZE - (p - IObuff), |
13610
e76499e85744
patch 8.0.1677: no compiler warning for wrong format in vim_snprintf()
Christian Brabandt <cb@256bit.org>
parents:
13575
diff
changeset
|
5377 _("%lld characters"), (long long)nchars); |
7 | 5378 } |
5379 } | |
5380 | |
5381 /* | |
5382 * Append message for missing line separator to IObuff. | |
5383 */ | |
5384 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5385 msg_add_eol(void) |
7 | 5386 { |
5387 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]")); | |
5388 } | |
5389 | |
5390 /* | |
5391 * Check modification time of file, before writing to it. | |
5392 * The size isn't checked, because using a tool like "gzip" takes care of | |
5393 * using the same timestamp but can't set the size. | |
5394 */ | |
5395 static int | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
5396 check_mtime(buf_T *buf, stat_T *st) |
7 | 5397 { |
5398 if (buf->b_mtime_read != 0 | |
5399 && time_differs((long)st->st_mtime, buf->b_mtime_read)) | |
5400 { | |
5401 msg_scroll = TRUE; /* don't overwrite messages here */ | |
5402 msg_silent = 0; /* must give this prompt */ | |
5403 /* don't use emsg() here, don't want to flush the buffers */ | |
5404 MSG_ATTR(_("WARNING: The file has been changed since reading it!!!"), | |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
5405 HL_ATTR(HLF_E)); |
7 | 5406 if (ask_yesno((char_u *)_("Do you really want to write to it"), |
5407 TRUE) == 'n') | |
5408 return FAIL; | |
5409 msg_scroll = FALSE; /* always overwrite the file message now */ | |
5410 } | |
5411 return OK; | |
5412 } | |
5413 | |
5414 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5415 time_differs(long t1, long t2) |
7 | 5416 { |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
5417 #if defined(__linux__) || defined(MSWIN) |
7 | 5418 /* On a FAT filesystem, esp. under Linux, there are only 5 bits to store |
5419 * the seconds. Since the roundoff is done when flushing the inode, the | |
5420 * time may change unexpectedly by one second!!! */ | |
5421 return (t1 - t2 > 1 || t2 - t1 > 1); | |
5422 #else | |
5423 return (t1 != t2); | |
5424 #endif | |
5425 } | |
5426 | |
5427 /* | |
5428 * Call write() to write a number of bytes to the file. | |
2664 | 5429 * Handles encryption and 'encoding' conversion. |
7 | 5430 * |
5431 * Return FAIL for failure, OK otherwise. | |
5432 */ | |
5433 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5434 buf_write_bytes(struct bw_info *ip) |
7 | 5435 { |
5436 int wlen; | |
5437 char_u *buf = ip->bw_buf; /* data to write */ | |
5438 int len = ip->bw_len; /* length of data */ | |
5439 #ifdef HAS_BW_FLAGS | |
5440 int flags = ip->bw_flags; /* extra flags */ | |
5441 #endif | |
5442 | |
5443 #ifdef FEAT_MBYTE | |
5444 /* | |
5445 * Skip conversion when writing the crypt magic number or the BOM. | |
5446 */ | |
5447 if (!(flags & FIO_NOCONVERT)) | |
5448 { | |
5449 char_u *p; | |
5450 unsigned c; | |
5451 int n; | |
5452 | |
5453 if (flags & FIO_UTF8) | |
5454 { | |
5455 /* | |
5456 * Convert latin1 in the buffer to UTF-8 in the file. | |
5457 */ | |
5458 p = ip->bw_conv_buf; /* translate to buffer */ | |
5459 for (wlen = 0; wlen < len; ++wlen) | |
5460 p += utf_char2bytes(buf[wlen], p); | |
5461 buf = ip->bw_conv_buf; | |
5462 len = (int)(p - ip->bw_conv_buf); | |
5463 } | |
5464 else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1)) | |
5465 { | |
5466 /* | |
5467 * Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or | |
5468 * Latin1 chars in the file. | |
5469 */ | |
5470 if (flags & FIO_LATIN1) | |
5471 p = buf; /* translate in-place (can only get shorter) */ | |
5472 else | |
5473 p = ip->bw_conv_buf; /* translate to buffer */ | |
5474 for (wlen = 0; wlen < len; wlen += n) | |
5475 { | |
5476 if (wlen == 0 && ip->bw_restlen != 0) | |
5477 { | |
5478 int l; | |
5479 | |
5480 /* Use remainder of previous call. Append the start of | |
5481 * buf[] to get a full sequence. Might still be too | |
5482 * short! */ | |
5483 l = CONV_RESTLEN - ip->bw_restlen; | |
5484 if (l > len) | |
5485 l = len; | |
5486 mch_memmove(ip->bw_rest + ip->bw_restlen, buf, (size_t)l); | |
474 | 5487 n = utf_ptr2len_len(ip->bw_rest, ip->bw_restlen + l); |
7 | 5488 if (n > ip->bw_restlen + len) |
5489 { | |
5490 /* We have an incomplete byte sequence at the end to | |
5491 * be written. We can't convert it without the | |
5492 * remaining bytes. Keep them for the next call. */ | |
5493 if (ip->bw_restlen + len > CONV_RESTLEN) | |
5494 return FAIL; | |
5495 ip->bw_restlen += len; | |
5496 break; | |
5497 } | |
5498 if (n > 1) | |
5499 c = utf_ptr2char(ip->bw_rest); | |
5500 else | |
5501 c = ip->bw_rest[0]; | |
5502 if (n >= ip->bw_restlen) | |
5503 { | |
5504 n -= ip->bw_restlen; | |
5505 ip->bw_restlen = 0; | |
5506 } | |
5507 else | |
5508 { | |
5509 ip->bw_restlen -= n; | |
5510 mch_memmove(ip->bw_rest, ip->bw_rest + n, | |
5511 (size_t)ip->bw_restlen); | |
5512 n = 0; | |
5513 } | |
5514 } | |
5515 else | |
5516 { | |
474 | 5517 n = utf_ptr2len_len(buf + wlen, len - wlen); |
7 | 5518 if (n > len - wlen) |
5519 { | |
5520 /* We have an incomplete byte sequence at the end to | |
5521 * be written. We can't convert it without the | |
5522 * remaining bytes. Keep them for the next call. */ | |
5523 if (len - wlen > CONV_RESTLEN) | |
5524 return FAIL; | |
5525 ip->bw_restlen = len - wlen; | |
5526 mch_memmove(ip->bw_rest, buf + wlen, | |
5527 (size_t)ip->bw_restlen); | |
5528 break; | |
5529 } | |
5530 if (n > 1) | |
5531 c = utf_ptr2char(buf + wlen); | |
5532 else | |
5533 c = buf[wlen]; | |
5534 } | |
5535 | |
1947 | 5536 if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error) |
5537 { | |
5538 ip->bw_conv_error = TRUE; | |
5539 ip->bw_conv_error_lnum = ip->bw_start_lnum; | |
5540 } | |
5541 if (c == NL) | |
5542 ++ip->bw_start_lnum; | |
7 | 5543 } |
5544 if (flags & FIO_LATIN1) | |
5545 len = (int)(p - buf); | |
5546 else | |
5547 { | |
5548 buf = ip->bw_conv_buf; | |
5549 len = (int)(p - ip->bw_conv_buf); | |
5550 } | |
5551 } | |
5552 | |
5553 # ifdef WIN3264 | |
5554 else if (flags & FIO_CODEPAGE) | |
5555 { | |
5556 /* | |
5557 * Convert UTF-8 or codepage to UCS-2 and then to MS-Windows | |
5558 * codepage. | |
5559 */ | |
5560 char_u *from; | |
5561 size_t fromlen; | |
5562 char_u *to; | |
5563 int u8c; | |
5564 BOOL bad = FALSE; | |
5565 int needed; | |
5566 | |
5567 if (ip->bw_restlen > 0) | |
5568 { | |
5569 /* Need to concatenate the remainder of the previous call and | |
5570 * the bytes of the current call. Use the end of the | |
5571 * conversion buffer for this. */ | |
5572 fromlen = len + ip->bw_restlen; | |
5573 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; | |
5574 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); | |
5575 mch_memmove(from + ip->bw_restlen, buf, (size_t)len); | |
5576 } | |
5577 else | |
5578 { | |
5579 from = buf; | |
5580 fromlen = len; | |
5581 } | |
5582 | |
5583 to = ip->bw_conv_buf; | |
5584 if (enc_utf8) | |
5585 { | |
5586 /* Convert from UTF-8 to UCS-2, to the start of the buffer. | |
5587 * The buffer has been allocated to be big enough. */ | |
5588 while (fromlen > 0) | |
5589 { | |
835 | 5590 n = (int)utf_ptr2len_len(from, (int)fromlen); |
7 | 5591 if (n > (int)fromlen) /* incomplete byte sequence */ |
5592 break; | |
5593 u8c = utf_ptr2char(from); | |
5594 *to++ = (u8c & 0xff); | |
5595 *to++ = (u8c >> 8); | |
5596 fromlen -= n; | |
5597 from += n; | |
5598 } | |
5599 | |
5600 /* Copy remainder to ip->bw_rest[] to be used for the next | |
5601 * call. */ | |
5602 if (fromlen > CONV_RESTLEN) | |
5603 { | |
5604 /* weird overlong sequence */ | |
5605 ip->bw_conv_error = TRUE; | |
5606 return FAIL; | |
5607 } | |
5608 mch_memmove(ip->bw_rest, from, fromlen); | |
835 | 5609 ip->bw_restlen = (int)fromlen; |
7 | 5610 } |
5611 else | |
5612 { | |
5613 /* Convert from enc_codepage to UCS-2, to the start of the | |
5614 * buffer. The buffer has been allocated to be big enough. */ | |
5615 ip->bw_restlen = 0; | |
5616 needed = MultiByteToWideChar(enc_codepage, | |
1142 | 5617 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen, |
7 | 5618 NULL, 0); |
5619 if (needed == 0) | |
5620 { | |
5621 /* When conversion fails there may be a trailing byte. */ | |
5622 needed = MultiByteToWideChar(enc_codepage, | |
1142 | 5623 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen - 1, |
7 | 5624 NULL, 0); |
5625 if (needed == 0) | |
5626 { | |
5627 /* Conversion doesn't work. */ | |
5628 ip->bw_conv_error = TRUE; | |
5629 return FAIL; | |
5630 } | |
5631 /* Save the trailing byte for the next call. */ | |
5632 ip->bw_rest[0] = from[fromlen - 1]; | |
5633 ip->bw_restlen = 1; | |
5634 } | |
5635 needed = MultiByteToWideChar(enc_codepage, MB_ERR_INVALID_CHARS, | |
1142 | 5636 (LPCSTR)from, (int)(fromlen - ip->bw_restlen), |
7 | 5637 (LPWSTR)to, needed); |
5638 if (needed == 0) | |
5639 { | |
5640 /* Safety check: Conversion doesn't work. */ | |
5641 ip->bw_conv_error = TRUE; | |
5642 return FAIL; | |
5643 } | |
5644 to += needed * 2; | |
5645 } | |
5646 | |
5647 fromlen = to - ip->bw_conv_buf; | |
5648 buf = to; | |
5649 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
5650 if (FIO_GET_CP(flags) == CP_UTF8) | |
5651 { | |
5652 /* Convert from UCS-2 to UTF-8, using the remainder of the | |
5653 * conversion buffer. Fails when out of space. */ | |
5654 for (from = ip->bw_conv_buf; fromlen > 1; fromlen -= 2) | |
5655 { | |
5656 u8c = *from++; | |
5657 u8c += (*from++ << 8); | |
5658 to += utf_char2bytes(u8c, to); | |
5659 if (to + 6 >= ip->bw_conv_buf + ip->bw_conv_buflen) | |
5660 { | |
5661 ip->bw_conv_error = TRUE; | |
5662 return FAIL; | |
5663 } | |
5664 } | |
835 | 5665 len = (int)(to - buf); |
7 | 5666 } |
5667 else | |
5668 #endif | |
5669 { | |
5670 /* Convert from UCS-2 to the codepage, using the remainder of | |
5671 * the conversion buffer. If the conversion uses the default | |
5672 * character "0", the data doesn't fit in this encoding, so | |
5673 * fail. */ | |
5674 len = WideCharToMultiByte(FIO_GET_CP(flags), 0, | |
5675 (LPCWSTR)ip->bw_conv_buf, (int)fromlen / sizeof(WCHAR), | |
1142 | 5676 (LPSTR)to, (int)(ip->bw_conv_buflen - fromlen), 0, |
5677 &bad); | |
7 | 5678 if (bad) |
5679 { | |
5680 ip->bw_conv_error = TRUE; | |
5681 return FAIL; | |
5682 } | |
5683 } | |
5684 } | |
5685 # endif | |
5686 | |
765 | 5687 # ifdef MACOS_CONVERT |
7 | 5688 else if (flags & FIO_MACROMAN) |
5689 { | |
5690 /* | |
5691 * Convert UTF-8 or latin1 to Apple MacRoman. | |
5692 */ | |
5693 char_u *from; | |
5694 size_t fromlen; | |
5695 | |
5696 if (ip->bw_restlen > 0) | |
5697 { | |
5698 /* Need to concatenate the remainder of the previous call and | |
5699 * the bytes of the current call. Use the end of the | |
5700 * conversion buffer for this. */ | |
5701 fromlen = len + ip->bw_restlen; | |
5702 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; | |
5703 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); | |
5704 mch_memmove(from + ip->bw_restlen, buf, (size_t)len); | |
5705 } | |
5706 else | |
5707 { | |
5708 from = buf; | |
5709 fromlen = len; | |
5710 } | |
5711 | |
18 | 5712 if (enc2macroman(from, fromlen, |
5713 ip->bw_conv_buf, &len, ip->bw_conv_buflen, | |
5714 ip->bw_rest, &ip->bw_restlen) == FAIL) | |
7 | 5715 { |
5716 ip->bw_conv_error = TRUE; | |
5717 return FAIL; | |
5718 } | |
5719 buf = ip->bw_conv_buf; | |
5720 } | |
5721 # endif | |
5722 | |
5723 # ifdef USE_ICONV | |
5724 if (ip->bw_iconv_fd != (iconv_t)-1) | |
5725 { | |
5726 const char *from; | |
5727 size_t fromlen; | |
5728 char *to; | |
5729 size_t tolen; | |
5730 | |
5731 /* Convert with iconv(). */ | |
5732 if (ip->bw_restlen > 0) | |
5733 { | |
1836 | 5734 char *fp; |
5735 | |
7 | 5736 /* Need to concatenate the remainder of the previous call and |
5737 * the bytes of the current call. Use the end of the | |
5738 * conversion buffer for this. */ | |
5739 fromlen = len + ip->bw_restlen; | |
1836 | 5740 fp = (char *)ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; |
5741 mch_memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen); | |
5742 mch_memmove(fp + ip->bw_restlen, buf, (size_t)len); | |
5743 from = fp; | |
7 | 5744 tolen = ip->bw_conv_buflen - fromlen; |
5745 } | |
5746 else | |
5747 { | |
5748 from = (const char *)buf; | |
5749 fromlen = len; | |
5750 tolen = ip->bw_conv_buflen; | |
5751 } | |
5752 to = (char *)ip->bw_conv_buf; | |
5753 | |
5754 if (ip->bw_first) | |
5755 { | |
5756 size_t save_len = tolen; | |
5757 | |
5758 /* output the initial shift state sequence */ | |
5759 (void)iconv(ip->bw_iconv_fd, NULL, NULL, &to, &tolen); | |
5760 | |
5761 /* There is a bug in iconv() on Linux (which appears to be | |
5762 * wide-spread) which sets "to" to NULL and messes up "tolen". | |
5763 */ | |
5764 if (to == NULL) | |
5765 { | |
5766 to = (char *)ip->bw_conv_buf; | |
5767 tolen = save_len; | |
5768 } | |
5769 ip->bw_first = FALSE; | |
5770 } | |
5771 | |
5772 /* | |
5773 * If iconv() has an error or there is not enough room, fail. | |
5774 */ | |
5775 if ((iconv(ip->bw_iconv_fd, (void *)&from, &fromlen, &to, &tolen) | |
5776 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) | |
5777 || fromlen > CONV_RESTLEN) | |
5778 { | |
5779 ip->bw_conv_error = TRUE; | |
5780 return FAIL; | |
5781 } | |
5782 | |
5783 /* copy remainder to ip->bw_rest[] to be used for the next call. */ | |
5784 if (fromlen > 0) | |
5785 mch_memmove(ip->bw_rest, (void *)from, fromlen); | |
5786 ip->bw_restlen = (int)fromlen; | |
5787 | |
5788 buf = ip->bw_conv_buf; | |
5789 len = (int)((char_u *)to - ip->bw_conv_buf); | |
5790 } | |
5791 # endif | |
5792 } | |
5793 #endif /* FEAT_MBYTE */ | |
5794 | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
5795 if (ip->bw_fd < 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
5796 /* Only checking conversion, which is OK if we get here. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
5797 return OK; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
5798 |
7 | 5799 #ifdef FEAT_CRYPT |
6122 | 5800 if (flags & FIO_ENCRYPTED) |
5801 { | |
5802 /* Encrypt the data. Do it in-place if possible, otherwise use an | |
5803 * allocated buffer. */ | |
5804 if (crypt_works_inplace(ip->bw_buffer->b_cryptstate)) | |
5805 { | |
5806 crypt_encode_inplace(ip->bw_buffer->b_cryptstate, buf, len); | |
5807 } | |
5808 else | |
5809 { | |
5810 char_u *outbuf; | |
5811 | |
5812 len = crypt_encode_alloc(curbuf->b_cryptstate, buf, len, &outbuf); | |
5813 if (len == 0) | |
5814 return OK; /* Crypt layer is buffering, will flush later. */ | |
5815 wlen = write_eintr(ip->bw_fd, outbuf, len); | |
5816 vim_free(outbuf); | |
5817 return (wlen < len) ? FAIL : OK; | |
5818 } | |
5819 } | |
7 | 5820 #endif |
5821 | |
2664 | 5822 wlen = write_eintr(ip->bw_fd, buf, len); |
5823 return (wlen < len) ? FAIL : OK; | |
7 | 5824 } |
5825 | |
5826 #ifdef FEAT_MBYTE | |
5827 /* | |
5828 * Convert a Unicode character to bytes. | |
1947 | 5829 * Return TRUE for an error, FALSE when it's OK. |
7 | 5830 */ |
5831 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5832 ucs2bytes( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5833 unsigned c, /* in: character */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5834 char_u **pp, /* in/out: pointer to result */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5835 int flags) /* FIO_ flags */ |
7 | 5836 { |
5837 char_u *p = *pp; | |
5838 int error = FALSE; | |
5839 int cc; | |
5840 | |
5841 | |
5842 if (flags & FIO_UCS4) | |
5843 { | |
5844 if (flags & FIO_ENDIAN_L) | |
5845 { | |
5846 *p++ = c; | |
5847 *p++ = (c >> 8); | |
5848 *p++ = (c >> 16); | |
5849 *p++ = (c >> 24); | |
5850 } | |
5851 else | |
5852 { | |
5853 *p++ = (c >> 24); | |
5854 *p++ = (c >> 16); | |
5855 *p++ = (c >> 8); | |
5856 *p++ = c; | |
5857 } | |
5858 } | |
5859 else if (flags & (FIO_UCS2 | FIO_UTF16)) | |
5860 { | |
5861 if (c >= 0x10000) | |
5862 { | |
5863 if (flags & FIO_UTF16) | |
5864 { | |
5865 /* Make two words, ten bits of the character in each. First | |
5866 * word is 0xd800 - 0xdbff, second one 0xdc00 - 0xdfff */ | |
5867 c -= 0x10000; | |
5868 if (c >= 0x100000) | |
5869 error = TRUE; | |
5870 cc = ((c >> 10) & 0x3ff) + 0xd800; | |
5871 if (flags & FIO_ENDIAN_L) | |
5872 { | |
5873 *p++ = cc; | |
5874 *p++ = ((unsigned)cc >> 8); | |
5875 } | |
5876 else | |
5877 { | |
5878 *p++ = ((unsigned)cc >> 8); | |
5879 *p++ = cc; | |
5880 } | |
5881 c = (c & 0x3ff) + 0xdc00; | |
5882 } | |
5883 else | |
5884 error = TRUE; | |
5885 } | |
5886 if (flags & FIO_ENDIAN_L) | |
5887 { | |
5888 *p++ = c; | |
5889 *p++ = (c >> 8); | |
5890 } | |
5891 else | |
5892 { | |
5893 *p++ = (c >> 8); | |
5894 *p++ = c; | |
5895 } | |
5896 } | |
5897 else /* Latin1 */ | |
5898 { | |
5899 if (c >= 0x100) | |
5900 { | |
5901 error = TRUE; | |
5902 *p++ = 0xBF; | |
5903 } | |
5904 else | |
5905 *p++ = c; | |
5906 } | |
5907 | |
5908 *pp = p; | |
5909 return error; | |
5910 } | |
5911 | |
5912 /* | |
1948 | 5913 * Return TRUE if file encoding "fenc" requires conversion from or to |
5914 * 'encoding'. | |
7 | 5915 */ |
5916 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5917 need_conversion(char_u *fenc) |
7 | 5918 { |
1948 | 5919 int same_encoding; |
5920 int enc_flags; | |
5921 int fenc_flags; | |
5922 | |
5923 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0) | |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5924 { |
1948 | 5925 same_encoding = TRUE; |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5926 fenc_flags = 0; |
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5927 } |
1948 | 5928 else |
5929 { | |
5930 /* Ignore difference between "ansi" and "latin1", "ucs-4" and | |
5931 * "ucs-4be", etc. */ | |
5932 enc_flags = get_fio_flags(p_enc); | |
5933 fenc_flags = get_fio_flags(fenc); | |
5934 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags); | |
5935 } | |
5936 if (same_encoding) | |
5937 { | |
5938 /* Specified encoding matches with 'encoding'. This requires | |
5939 * conversion when 'encoding' is Unicode but not UTF-8. */ | |
5940 return enc_unicode != 0; | |
5941 } | |
5942 | |
5943 /* Encodings differ. However, conversion is not needed when 'enc' is any | |
5944 * Unicode encoding and the file is UTF-8. */ | |
5945 return !(enc_utf8 && fenc_flags == FIO_UTF8); | |
7 | 5946 } |
5947 | |
5948 /* | |
5949 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the | |
5950 * internal conversion. | |
5951 * if "ptr" is an empty string, use 'encoding'. | |
5952 */ | |
5953 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5954 get_fio_flags(char_u *ptr) |
7 | 5955 { |
5956 int prop; | |
5957 | |
5958 if (*ptr == NUL) | |
5959 ptr = p_enc; | |
5960 | |
5961 prop = enc_canon_props(ptr); | |
5962 if (prop & ENC_UNICODE) | |
5963 { | |
5964 if (prop & ENC_2BYTE) | |
5965 { | |
5966 if (prop & ENC_ENDIAN_L) | |
5967 return FIO_UCS2 | FIO_ENDIAN_L; | |
5968 return FIO_UCS2; | |
5969 } | |
5970 if (prop & ENC_4BYTE) | |
5971 { | |
5972 if (prop & ENC_ENDIAN_L) | |
5973 return FIO_UCS4 | FIO_ENDIAN_L; | |
5974 return FIO_UCS4; | |
5975 } | |
5976 if (prop & ENC_2WORD) | |
5977 { | |
5978 if (prop & ENC_ENDIAN_L) | |
5979 return FIO_UTF16 | FIO_ENDIAN_L; | |
5980 return FIO_UTF16; | |
5981 } | |
5982 return FIO_UTF8; | |
5983 } | |
5984 if (prop & ENC_LATIN1) | |
5985 return FIO_LATIN1; | |
5986 /* must be ENC_DBCS, requires iconv() */ | |
5987 return 0; | |
5988 } | |
5989 | |
5990 #ifdef WIN3264 | |
5991 /* | |
5992 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed | |
5993 * for the conversion MS-Windows can do for us. Also accept "utf-8". | |
5994 * Used for conversion between 'encoding' and 'fileencoding'. | |
5995 */ | |
5996 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5997 get_win_fio_flags(char_u *ptr) |
7 | 5998 { |
5999 int cp; | |
6000 | |
6001 /* Cannot do this when 'encoding' is not utf-8 and not a codepage. */ | |
6002 if (!enc_utf8 && enc_codepage <= 0) | |
6003 return 0; | |
6004 | |
6005 cp = encname2codepage(ptr); | |
6006 if (cp == 0) | |
6007 { | |
6008 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
6009 if (STRCMP(ptr, "utf-8") == 0) | |
6010 cp = CP_UTF8; | |
6011 else | |
6012 # endif | |
6013 return 0; | |
6014 } | |
6015 return FIO_PUT_CP(cp) | FIO_CODEPAGE; | |
6016 } | |
6017 #endif | |
6018 | |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
6019 #ifdef MACOS_CONVERT |
7 | 6020 /* |
6021 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags | |
6022 * needed for the internal conversion to/from utf-8 or latin1. | |
6023 */ | |
6024 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6025 get_mac_fio_flags(char_u *ptr) |
7 | 6026 { |
6027 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
6028 && (enc_canon_props(ptr) & ENC_MACROMAN)) | |
6029 return FIO_MACROMAN; | |
6030 return 0; | |
6031 } | |
6032 #endif | |
6033 | |
6034 /* | |
6035 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size]. | |
6036 * "size" must be at least 2. | |
6037 * Return the name of the encoding and set "*lenp" to the length. | |
6038 * Returns NULL when no BOM found. | |
6039 */ | |
6040 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6041 check_for_bom( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6042 char_u *p, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6043 long size, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6044 int *lenp, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6045 int flags) |
7 | 6046 { |
6047 char *name = NULL; | |
6048 int len = 2; | |
6049 | |
6050 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf | |
1688 | 6051 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0)) |
7 | 6052 { |
6053 name = "utf-8"; /* EF BB BF */ | |
6054 len = 3; | |
6055 } | |
6056 else if (p[0] == 0xff && p[1] == 0xfe) | |
6057 { | |
6058 if (size >= 4 && p[2] == 0 && p[3] == 0 | |
6059 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L))) | |
6060 { | |
6061 name = "ucs-4le"; /* FF FE 00 00 */ | |
6062 len = 4; | |
6063 } | |
1735 | 6064 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L)) |
7 | 6065 name = "ucs-2le"; /* FF FE */ |
1735 | 6066 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L)) |
6067 /* utf-16le is preferred, it also works for ucs-2le text */ | |
7 | 6068 name = "utf-16le"; /* FF FE */ |
6069 } | |
6070 else if (p[0] == 0xfe && p[1] == 0xff | |
6071 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16)) | |
6072 { | |
1547 | 6073 /* Default to utf-16, it works also for ucs-2 text. */ |
6074 if (flags == FIO_UCS2) | |
6075 name = "ucs-2"; /* FE FF */ | |
6076 else | |
7 | 6077 name = "utf-16"; /* FE FF */ |
6078 } | |
6079 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe | |
6080 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4)) | |
6081 { | |
6082 name = "ucs-4"; /* 00 00 FE FF */ | |
6083 len = 4; | |
6084 } | |
6085 | |
6086 *lenp = len; | |
6087 return (char_u *)name; | |
6088 } | |
6089 | |
6090 /* | |
6091 * Generate a BOM in "buf[4]" for encoding "name". | |
6092 * Return the length of the BOM (zero when no BOM). | |
6093 */ | |
6094 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6095 make_bom(char_u *buf, char_u *name) |
7 | 6096 { |
6097 int flags; | |
6098 char_u *p; | |
6099 | |
6100 flags = get_fio_flags(name); | |
6101 | |
6102 /* Can't put a BOM in a non-Unicode file. */ | |
6103 if (flags == FIO_LATIN1 || flags == 0) | |
6104 return 0; | |
6105 | |
6106 if (flags == FIO_UTF8) /* UTF-8 */ | |
6107 { | |
6108 buf[0] = 0xef; | |
6109 buf[1] = 0xbb; | |
6110 buf[2] = 0xbf; | |
6111 return 3; | |
6112 } | |
6113 p = buf; | |
6114 (void)ucs2bytes(0xfeff, &p, flags); | |
6115 return (int)(p - buf); | |
6116 } | |
6117 #endif | |
6118 | |
6119 /* | |
6120 * Try to find a shortname by comparing the fullname with the current | |
6121 * directory. | |
1411 | 6122 * Returns "full_path" or pointer into "full_path" if shortened. |
6123 */ | |
6124 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6125 shorten_fname1(char_u *full_path) |
1411 | 6126 { |
2770 | 6127 char_u *dirname; |
1411 | 6128 char_u *p = full_path; |
6129 | |
2770 | 6130 dirname = alloc(MAXPATHL); |
6131 if (dirname == NULL) | |
6132 return full_path; | |
1411 | 6133 if (mch_dirname(dirname, MAXPATHL) == OK) |
6134 { | |
6135 p = shorten_fname(full_path, dirname); | |
6136 if (p == NULL || *p == NUL) | |
6137 p = full_path; | |
6138 } | |
2770 | 6139 vim_free(dirname); |
1411 | 6140 return p; |
6141 } | |
6142 | |
6143 /* | |
6144 * Try to find a shortname by comparing the fullname with the current | |
6145 * directory. | |
7 | 6146 * Returns NULL if not shorter name possible, pointer into "full_path" |
6147 * otherwise. | |
6148 */ | |
6149 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6150 shorten_fname(char_u *full_path, char_u *dir_name) |
7 | 6151 { |
6152 int len; | |
6153 char_u *p; | |
6154 | |
6155 if (full_path == NULL) | |
6156 return NULL; | |
6157 len = (int)STRLEN(dir_name); | |
6158 if (fnamencmp(dir_name, full_path, len) == 0) | |
6159 { | |
6160 p = full_path + len; | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6161 #if defined(MSWIN) |
7 | 6162 /* |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6163 * MSWIN: when a file is in the root directory, dir_name will end in a |
7 | 6164 * slash, since C: by itself does not define a specific dir. In this |
6165 * case p may already be correct. <negri> | |
6166 */ | |
6167 if (!((len > 2) && (*(p - 2) == ':'))) | |
6168 #endif | |
6169 { | |
6170 if (vim_ispathsep(*p)) | |
6171 ++p; | |
6172 #ifndef VMS /* the path separator is always part of the path */ | |
6173 else | |
6174 p = NULL; | |
6175 #endif | |
6176 } | |
6177 } | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6178 #if defined(MSWIN) |
7 | 6179 /* |
6180 * When using a file in the current drive, remove the drive name: | |
6181 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on | |
6182 * a floppy from "A:\dir" to "B:\dir". | |
6183 */ | |
6184 else if (len > 3 | |
6185 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0]) | |
6186 && full_path[1] == ':' | |
6187 && vim_ispathsep(full_path[2])) | |
6188 p = full_path + 2; | |
6189 #endif | |
6190 else | |
6191 p = NULL; | |
6192 return p; | |
6193 } | |
6194 | |
6195 /* | |
13819
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6196 * Shorten filename of a buffer. |
7 | 6197 * When "force" is TRUE: Use full path from now on for files currently being |
6198 * edited, both for file name and swap file name. Try to shorten the file | |
6199 * names a bit, if safe to do so. | |
6200 * When "force" is FALSE: Only try to shorten absolute file names. | |
6201 * For buffers that have buftype "nofile" or "scratch": never change the file | |
6202 * name. | |
6203 */ | |
6204 void | |
13819
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6205 shorten_buf_fname(buf_T *buf, char_u *dirname, int force) |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6206 { |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6207 char_u *p; |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6208 |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6209 if (buf->b_fname != NULL |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6210 #ifdef FEAT_QUICKFIX |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6211 && !bt_nofile(buf) |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6212 #endif |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6213 && !path_with_url(buf->b_fname) |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6214 && (force |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6215 || buf->b_sfname == NULL |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6216 || mch_isFullName(buf->b_sfname))) |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6217 { |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6218 VIM_CLEAR(buf->b_sfname); |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6219 p = shorten_fname(buf->b_ffname, dirname); |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6220 if (p != NULL) |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6221 { |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6222 buf->b_sfname = vim_strsave(p); |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6223 buf->b_fname = buf->b_sfname; |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6224 } |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6225 if (p == NULL || buf->b_fname == NULL) |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6226 buf->b_fname = buf->b_ffname; |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6227 } |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6228 } |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6229 |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6230 /* |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6231 * Shorten filenames for all buffers. |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6232 */ |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6233 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6234 shorten_fnames(int force) |
7 | 6235 { |
6236 char_u dirname[MAXPATHL]; | |
6237 buf_T *buf; | |
6238 | |
6239 mch_dirname(dirname, MAXPATHL); | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
6240 FOR_ALL_BUFFERS(buf) |
7 | 6241 { |
13819
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6242 shorten_buf_fname(buf, dirname, force); |
9 | 6243 |
6244 /* Always make the swap file name a full path, a "nofile" buffer may | |
6245 * also have a swap file. */ | |
6246 mf_fullname(buf->b_ml.ml_mfp); | |
7 | 6247 } |
6248 status_redraw_all(); | |
672 | 6249 redraw_tabline = TRUE; |
7 | 6250 } |
6251 | |
6252 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \ | |
6253 || defined(FEAT_GUI_MSWIN) \ | |
6254 || defined(FEAT_GUI_MAC) \ | |
6255 || defined(PROTO) | |
6256 /* | |
6257 * Shorten all filenames in "fnames[count]" by current directory. | |
6258 */ | |
6259 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6260 shorten_filenames(char_u **fnames, int count) |
7 | 6261 { |
6262 int i; | |
6263 char_u dirname[MAXPATHL]; | |
6264 char_u *p; | |
6265 | |
6266 if (fnames == NULL || count < 1) | |
6267 return; | |
6268 mch_dirname(dirname, sizeof(dirname)); | |
6269 for (i = 0; i < count; ++i) | |
6270 { | |
6271 if ((p = shorten_fname(fnames[i], dirname)) != NULL) | |
6272 { | |
6273 /* shorten_fname() returns pointer in given "fnames[i]". If free | |
6274 * "fnames[i]" first, "p" becomes invalid. So we need to copy | |
6275 * "p" first then free fnames[i]. */ | |
6276 p = vim_strsave(p); | |
6277 vim_free(fnames[i]); | |
6278 fnames[i] = p; | |
6279 } | |
6280 } | |
6281 } | |
6282 #endif | |
6283 | |
6284 /* | |
14475
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
6285 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or |
7 | 6286 * fo_o_h.ext for MSDOS or when shortname option set. |
6287 * | |
6288 * Assumed that fname is a valid name found in the filesystem we assure that | |
6289 * the return value is a different name and ends in 'ext'. | |
6290 * "ext" MUST be at most 4 characters long if it starts with a dot, 3 | |
6291 * characters otherwise. | |
6292 * Space for the returned name is allocated, must be freed later. | |
6293 * Returns NULL when out of memory. | |
6294 */ | |
6295 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6296 modname( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6297 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6298 char_u *ext, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6299 int prepend_dot) /* may prepend a '.' to file name */ |
7 | 6300 { |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6301 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname), |
7 | 6302 fname, ext, prepend_dot); |
6303 } | |
6304 | |
6305 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6306 buf_modname( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6307 int shortname, /* use 8.3 file name */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6308 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6309 char_u *ext, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6310 int prepend_dot) /* may prepend a '.' to file name */ |
7 | 6311 { |
6312 char_u *retval; | |
6313 char_u *s; | |
6314 char_u *e; | |
6315 char_u *ptr; | |
6316 int fnamelen, extlen; | |
6317 | |
6318 extlen = (int)STRLEN(ext); | |
6319 | |
6320 /* | |
6321 * If there is no file name we must get the name of the current directory | |
6322 * (we need the full path in case :cd is used). | |
6323 */ | |
6324 if (fname == NULL || *fname == NUL) | |
6325 { | |
6326 retval = alloc((unsigned)(MAXPATHL + extlen + 3)); | |
6327 if (retval == NULL) | |
6328 return NULL; | |
6329 if (mch_dirname(retval, MAXPATHL) == FAIL || | |
6330 (fnamelen = (int)STRLEN(retval)) == 0) | |
6331 { | |
6332 vim_free(retval); | |
6333 return NULL; | |
6334 } | |
39 | 6335 if (!after_pathsep(retval, retval + fnamelen)) |
7 | 6336 { |
6337 retval[fnamelen++] = PATHSEP; | |
6338 retval[fnamelen] = NUL; | |
6339 } | |
6340 prepend_dot = FALSE; /* nothing to prepend a dot to */ | |
6341 } | |
6342 else | |
6343 { | |
6344 fnamelen = (int)STRLEN(fname); | |
6345 retval = alloc((unsigned)(fnamelen + extlen + 3)); | |
6346 if (retval == NULL) | |
6347 return NULL; | |
6348 STRCPY(retval, fname); | |
6349 #ifdef VMS | |
6350 vms_remove_version(retval); /* we do not need versions here */ | |
6351 #endif | |
6352 } | |
6353 | |
6354 /* | |
6355 * search backwards until we hit a '/', '\' or ':' replacing all '.' | |
6356 * by '_' for MSDOS or when shortname option set and ext starts with a dot. | |
6357 * Then truncate what is after the '/', '\' or ':' to 8 characters for | |
6358 * MSDOS and 26 characters for AMIGA, a lot more for UNIX. | |
6359 */ | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
6360 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr)) |
7 | 6361 { |
6362 if (*ext == '.' | |
2823 | 6363 #ifdef USE_LONG_FNAME |
7 | 6364 && (!USE_LONG_FNAME || shortname) |
2823 | 6365 #else |
7 | 6366 && shortname |
2823 | 6367 #endif |
7 | 6368 ) |
6369 if (*ptr == '.') /* replace '.' by '_' */ | |
6370 *ptr = '_'; | |
6371 if (vim_ispathsep(*ptr)) | |
391 | 6372 { |
6373 ++ptr; | |
7 | 6374 break; |
391 | 6375 } |
6376 } | |
7 | 6377 |
6378 /* the file name has at most BASENAMELEN characters. */ | |
6379 if (STRLEN(ptr) > (unsigned)BASENAMELEN) | |
6380 ptr[BASENAMELEN] = '\0'; | |
6381 | |
6382 s = ptr + STRLEN(ptr); | |
6383 | |
6384 /* | |
6385 * For 8.3 file names we may have to reduce the length. | |
6386 */ | |
6387 #ifdef USE_LONG_FNAME | |
6388 if (!USE_LONG_FNAME || shortname) | |
6389 #else | |
6390 if (shortname) | |
6391 #endif | |
6392 { | |
6393 /* | |
6394 * If there is no file name, or the file name ends in '/', and the | |
6395 * extension starts with '.', put a '_' before the dot, because just | |
6396 * ".ext" is invalid. | |
6397 */ | |
6398 if (fname == NULL || *fname == NUL | |
6399 || vim_ispathsep(fname[STRLEN(fname) - 1])) | |
6400 { | |
6401 if (*ext == '.') | |
6402 *s++ = '_'; | |
6403 } | |
6404 /* | |
6405 * If the extension starts with '.', truncate the base name at 8 | |
6406 * characters | |
6407 */ | |
6408 else if (*ext == '.') | |
6409 { | |
1877 | 6410 if ((size_t)(s - ptr) > (size_t)8) |
7 | 6411 { |
6412 s = ptr + 8; | |
6413 *s = '\0'; | |
6414 } | |
6415 } | |
6416 /* | |
6417 * If the extension doesn't start with '.', and the file name | |
6418 * doesn't have an extension yet, append a '.' | |
6419 */ | |
6420 else if ((e = vim_strchr(ptr, '.')) == NULL) | |
6421 *s++ = '.'; | |
6422 /* | |
6423 * If the extension doesn't start with '.', and there already is an | |
1201 | 6424 * extension, it may need to be truncated |
7 | 6425 */ |
6426 else if ((int)STRLEN(e) + extlen > 4) | |
6427 s = e + 4 - extlen; | |
6428 } | |
7408
1886f2863437
commit https://github.com/vim/vim/commit/e7fedb6ebe72d9a475aa65109b77d5ed4667067a
Christian Brabandt <cb@256bit.org>
parents:
7320
diff
changeset
|
6429 #if defined(USE_LONG_FNAME) || defined(WIN3264) |
7 | 6430 /* |
6431 * If there is no file name, and the extension starts with '.', put a | |
6432 * '_' before the dot, because just ".ext" may be invalid if it's on a | |
6433 * FAT partition, and on HPFS it doesn't matter. | |
6434 */ | |
6435 else if ((fname == NULL || *fname == NUL) && *ext == '.') | |
6436 *s++ = '_'; | |
6437 #endif | |
6438 | |
6439 /* | |
1651 | 6440 * Append the extension. |
7 | 6441 * ext can start with '.' and cannot exceed 3 more characters. |
6442 */ | |
6443 STRCPY(s, ext); | |
6444 | |
6445 /* | |
6446 * Prepend the dot. | |
6447 */ | |
2823 | 6448 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.' |
7 | 6449 #ifdef USE_LONG_FNAME |
6450 && USE_LONG_FNAME | |
6451 #endif | |
6452 ) | |
6453 { | |
1620 | 6454 STRMOVE(e + 1, e); |
7 | 6455 *e = '.'; |
6456 } | |
6457 | |
6458 /* | |
6459 * Check that, after appending the extension, the file name is really | |
6460 * different. | |
6461 */ | |
6462 if (fname != NULL && STRCMP(fname, retval) == 0) | |
6463 { | |
6464 /* we search for a character that can be replaced by '_' */ | |
6465 while (--s >= ptr) | |
6466 { | |
6467 if (*s != '_') | |
6468 { | |
6469 *s = '_'; | |
6470 break; | |
6471 } | |
6472 } | |
6473 if (s < ptr) /* fname was "________.<ext>", how tricky! */ | |
6474 *ptr = 'v'; | |
6475 } | |
6476 return retval; | |
6477 } | |
6478 | |
6479 /* | |
6480 * Like fgets(), but if the file line is too long, it is truncated and the | |
6481 * rest of the line is thrown away. Returns TRUE for end-of-file. | |
13037
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
6482 * If the line is truncated then buf[size - 2] will not be NUL. |
7 | 6483 */ |
6484 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6485 vim_fgets(char_u *buf, int size, FILE *fp) |
7 | 6486 { |
6487 char *eof; | |
6488 #define FGETS_SIZE 200 | |
6489 char tbuf[FGETS_SIZE]; | |
6490 | |
6491 buf[size - 2] = NUL; | |
6492 #ifdef USE_CR | |
6493 eof = fgets_cr((char *)buf, size, fp); | |
6494 #else | |
6495 eof = fgets((char *)buf, size, fp); | |
6496 #endif | |
6497 if (buf[size - 2] != NUL && buf[size - 2] != '\n') | |
6498 { | |
6499 buf[size - 1] = NUL; /* Truncate the line */ | |
6500 | |
6501 /* Now throw away the rest of the line: */ | |
6502 do | |
6503 { | |
6504 tbuf[FGETS_SIZE - 2] = NUL; | |
6505 #ifdef USE_CR | |
1757 | 6506 ignoredp = fgets_cr((char *)tbuf, FGETS_SIZE, fp); |
7 | 6507 #else |
1757 | 6508 ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp); |
7 | 6509 #endif |
6510 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n'); | |
6511 } | |
6512 return (eof == NULL); | |
6513 } | |
6514 | |
6515 #if defined(USE_CR) || defined(PROTO) | |
6516 /* | |
6517 * Like vim_fgets(), but accept any line terminator: CR, CR-LF or LF. | |
6518 * Returns TRUE for end-of-file. | |
6519 * Only used for the Mac, because it's much slower than vim_fgets(). | |
6520 */ | |
6521 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6522 tag_fgets(char_u *buf, int size, FILE *fp) |
7 | 6523 { |
6524 int i = 0; | |
6525 int c; | |
6526 int eof = FALSE; | |
6527 | |
6528 for (;;) | |
6529 { | |
6530 c = fgetc(fp); | |
6531 if (c == EOF) | |
6532 { | |
6533 eof = TRUE; | |
6534 break; | |
6535 } | |
6536 if (c == '\r') | |
6537 { | |
6538 /* Always store a NL for end-of-line. */ | |
6539 if (i < size - 1) | |
6540 buf[i++] = '\n'; | |
6541 c = fgetc(fp); | |
6542 if (c != '\n') /* Macintosh format: single CR. */ | |
6543 ungetc(c, fp); | |
6544 break; | |
6545 } | |
6546 if (i < size - 1) | |
6547 buf[i++] = c; | |
6548 if (c == '\n') | |
6549 break; | |
6550 } | |
6551 buf[i] = NUL; | |
6552 return eof; | |
6553 } | |
6554 #endif | |
6555 | |
6556 /* | |
6557 * rename() only works if both files are on the same file system, this | |
6558 * function will (attempts to?) copy the file across if rename fails -- webb | |
6559 * Return -1 for failure, 0 for success. | |
6560 */ | |
6561 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6562 vim_rename(char_u *from, char_u *to) |
7 | 6563 { |
6564 int fd_in; | |
6565 int fd_out; | |
6566 int n; | |
6567 char *errmsg = NULL; | |
6568 char *buffer; | |
6569 #ifdef AMIGA | |
6570 BPTR flock; | |
6571 #endif | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6572 stat_T st; |
194 | 6573 long perm; |
199 | 6574 #ifdef HAVE_ACL |
6575 vim_acl_T acl; /* ACL from original file */ | |
6576 #endif | |
1779 | 6577 int use_tmp_file = FALSE; |
7 | 6578 |
6579 /* | |
1779 | 6580 * When the names are identical, there is nothing to do. When they refer |
6581 * to the same file (ignoring case and slash/backslash differences) but | |
6582 * the file name differs we need to go through a temp file. | |
7 | 6583 */ |
6584 if (fnamecmp(from, to) == 0) | |
1779 | 6585 { |
4242 | 6586 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0) |
1779 | 6587 use_tmp_file = TRUE; |
6588 else | |
6589 return 0; | |
6590 } | |
7 | 6591 |
6592 /* | |
6593 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted. | |
6594 */ | |
6595 if (mch_stat((char *)from, &st) < 0) | |
6596 return -1; | |
6597 | |
1778 | 6598 #ifdef UNIX |
6599 { | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6600 stat_T st_to; |
1778 | 6601 |
6602 /* It's possible for the source and destination to be the same file. | |
6603 * This happens when "from" and "to" differ in case and are on a FAT32 | |
6604 * filesystem. In that case go through a temp file name. */ | |
6605 if (mch_stat((char *)to, &st_to) >= 0 | |
6606 && st.st_dev == st_to.st_dev | |
6607 && st.st_ino == st_to.st_ino) | |
1779 | 6608 use_tmp_file = TRUE; |
6609 } | |
6610 #endif | |
2793 | 6611 #ifdef WIN3264 |
6612 { | |
6613 BY_HANDLE_FILE_INFORMATION info1, info2; | |
6614 | |
6615 /* It's possible for the source and destination to be the same file. | |
6616 * In that case go through a temp file name. This makes rename("foo", | |
6617 * "./foo") a no-op (in a complicated way). */ | |
6618 if (win32_fileinfo(from, &info1) == FILEINFO_OK | |
6619 && win32_fileinfo(to, &info2) == FILEINFO_OK | |
6620 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber | |
6621 && info1.nFileIndexHigh == info2.nFileIndexHigh | |
6622 && info1.nFileIndexLow == info2.nFileIndexLow) | |
6623 use_tmp_file = TRUE; | |
6624 } | |
6625 #endif | |
1779 | 6626 |
6627 if (use_tmp_file) | |
6628 { | |
6629 char tempname[MAXPATHL + 1]; | |
6630 | |
6631 /* | |
6632 * Find a name that doesn't exist and is in the same directory. | |
6633 * Rename "from" to "tempname" and then rename "tempname" to "to". | |
6634 */ | |
6635 if (STRLEN(from) >= MAXPATHL - 5) | |
6636 return -1; | |
6637 STRCPY(tempname, from); | |
6638 for (n = 123; n < 99999; ++n) | |
6639 { | |
6640 sprintf((char *)gettail((char_u *)tempname), "%d", n); | |
6641 if (mch_stat(tempname, &st) < 0) | |
6642 { | |
6643 if (mch_rename((char *)from, tempname) == 0) | |
1778 | 6644 { |
1779 | 6645 if (mch_rename(tempname, (char *)to) == 0) |
6646 return 0; | |
6647 /* Strange, the second step failed. Try moving the | |
6648 * file back and return failure. */ | |
6649 mch_rename(tempname, (char *)from); | |
1778 | 6650 return -1; |
6651 } | |
1779 | 6652 /* If it fails for one temp name it will most likely fail |
6653 * for any temp name, give up. */ | |
6654 return -1; | |
6655 } | |
6656 } | |
6657 return -1; | |
1778 | 6658 } |
6659 | |
7 | 6660 /* |
6661 * Delete the "to" file, this is required on some systems to make the | |
6662 * mch_rename() work, on other systems it makes sure that we don't have | |
6663 * two files when the mch_rename() fails. | |
6664 */ | |
6665 | |
6666 #ifdef AMIGA | |
6667 /* | |
6668 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible | |
6669 * that the name of the "to" file is the same as the "from" file, even | |
1201 | 6670 * though the names are different. To avoid the chance of accidentally |
7 | 6671 * deleting the "from" file (horror!) we lock it during the remove. |
6672 * | |
6673 * When used for making a backup before writing the file: This should not | |
6674 * happen with ":w", because startscript() should detect this problem and | |
6675 * set buf->b_shortname, causing modname() to return a correct ".bak" file | |
6676 * name. This problem does exist with ":w filename", but then the | |
6677 * original file will be somewhere else so the backup isn't really | |
6678 * important. If autoscripting is off the rename may fail. | |
6679 */ | |
6680 flock = Lock((UBYTE *)from, (long)ACCESS_READ); | |
6681 #endif | |
6682 mch_remove(to); | |
6683 #ifdef AMIGA | |
6684 if (flock) | |
6685 UnLock(flock); | |
6686 #endif | |
6687 | |
6688 /* | |
6689 * First try a normal rename, return if it works. | |
6690 */ | |
6691 if (mch_rename((char *)from, (char *)to) == 0) | |
6692 return 0; | |
6693 | |
6694 /* | |
6695 * Rename() failed, try copying the file. | |
6696 */ | |
194 | 6697 perm = mch_getperm(from); |
199 | 6698 #ifdef HAVE_ACL |
6699 /* For systems that support ACL: get the ACL from the original file. */ | |
6700 acl = mch_get_acl(from); | |
6701 #endif | |
7 | 6702 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0); |
6703 if (fd_in == -1) | |
1651 | 6704 { |
6705 #ifdef HAVE_ACL | |
6706 mch_free_acl(acl); | |
6707 #endif | |
7 | 6708 return -1; |
1651 | 6709 } |
194 | 6710 |
6711 /* Create the new file with same permissions as the original. */ | |
557 | 6712 fd_out = mch_open((char *)to, |
6713 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm); | |
7 | 6714 if (fd_out == -1) |
6715 { | |
6716 close(fd_in); | |
1651 | 6717 #ifdef HAVE_ACL |
6718 mch_free_acl(acl); | |
6719 #endif | |
7 | 6720 return -1; |
6721 } | |
6722 | |
6723 buffer = (char *)alloc(BUFSIZE); | |
6724 if (buffer == NULL) | |
6725 { | |
1651 | 6726 close(fd_out); |
7 | 6727 close(fd_in); |
1651 | 6728 #ifdef HAVE_ACL |
6729 mch_free_acl(acl); | |
6730 #endif | |
7 | 6731 return -1; |
6732 } | |
6733 | |
2664 | 6734 while ((n = read_eintr(fd_in, buffer, BUFSIZE)) > 0) |
6735 if (write_eintr(fd_out, buffer, n) != n) | |
7 | 6736 { |
6737 errmsg = _("E208: Error writing to \"%s\""); | |
6738 break; | |
6739 } | |
6740 | |
6741 vim_free(buffer); | |
6742 close(fd_in); | |
6743 if (close(fd_out) < 0) | |
6744 errmsg = _("E209: Error closing \"%s\""); | |
6745 if (n < 0) | |
6746 { | |
6747 errmsg = _("E210: Error reading \"%s\""); | |
6748 to = from; | |
6749 } | |
1201 | 6750 #ifndef UNIX /* for Unix mch_open() already set the permission */ |
194 | 6751 mch_setperm(to, perm); |
570 | 6752 #endif |
199 | 6753 #ifdef HAVE_ACL |
6754 mch_set_acl(to, acl); | |
1651 | 6755 mch_free_acl(acl); |
199 | 6756 #endif |
5788 | 6757 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
5483 | 6758 mch_copy_sec(from, to); |
5479 | 6759 #endif |
7 | 6760 if (errmsg != NULL) |
6761 { | |
6762 EMSG2(errmsg, to); | |
6763 return -1; | |
6764 } | |
6765 mch_remove(from); | |
6766 return 0; | |
6767 } | |
6768 | |
6769 static int already_warned = FALSE; | |
6770 | |
6771 /* | |
6772 * Check if any not hidden buffer has been changed. | |
6773 * Postpone the check if there are characters in the stuff buffer, a global | |
6774 * command is being executed, a mapping is being executed or an autocommand is | |
6775 * busy. | |
6776 * Returns TRUE if some message was written (screen should be redrawn and | |
6777 * cursor positioned). | |
6778 */ | |
6779 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6780 check_timestamps( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6781 int focus) /* called for GUI focus event */ |
7 | 6782 { |
6783 buf_T *buf; | |
6784 int didit = 0; | |
6785 int n; | |
6786 | |
6787 /* Don't check timestamps while system() or another low-level function may | |
6788 * cause us to lose and gain focus. */ | |
6789 if (no_check_timestamps > 0) | |
6790 return FALSE; | |
6791 | |
6792 /* Avoid doing a check twice. The OK/Reload dialog can cause a focus | |
6793 * event and we would keep on checking if the file is steadily growing. | |
6794 * Do check again after typing something. */ | |
6795 if (focus && did_check_timestamps) | |
6796 { | |
6797 need_check_timestamps = TRUE; | |
6798 return FALSE; | |
6799 } | |
6800 | |
6801 if (!stuff_empty() || global_busy || !typebuf_typed() | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
6802 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0) |
7 | 6803 need_check_timestamps = TRUE; /* check later */ |
6804 else | |
6805 { | |
6806 ++no_wait_return; | |
6807 did_check_timestamps = TRUE; | |
6808 already_warned = FALSE; | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
6809 FOR_ALL_BUFFERS(buf) |
7 | 6810 { |
6811 /* Only check buffers in a window. */ | |
6812 if (buf->b_nwindows > 0) | |
6813 { | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6814 bufref_T bufref; |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6815 |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6816 set_bufref(&bufref, buf); |
7 | 6817 n = buf_check_timestamp(buf, focus); |
6818 if (didit < n) | |
6819 didit = n; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6820 if (n > 0 && !bufref_valid(&bufref)) |
7 | 6821 { |
6822 /* Autocommands have removed the buffer, start at the | |
6823 * first one again. */ | |
6824 buf = firstbuf; | |
6825 continue; | |
6826 } | |
6827 } | |
6828 } | |
6829 --no_wait_return; | |
6830 need_check_timestamps = FALSE; | |
6831 if (need_wait_return && didit == 2) | |
6832 { | |
6833 /* make sure msg isn't overwritten */ | |
6834 msg_puts((char_u *)"\n"); | |
6835 out_flush(); | |
6836 } | |
6837 } | |
6838 return didit; | |
6839 } | |
6840 | |
6841 /* | |
6842 * Move all the lines from buffer "frombuf" to buffer "tobuf". | |
6843 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not | |
6844 * empty. | |
6845 */ | |
6846 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6847 move_lines(buf_T *frombuf, buf_T *tobuf) |
7 | 6848 { |
6849 buf_T *tbuf = curbuf; | |
6850 int retval = OK; | |
6851 linenr_T lnum; | |
6852 char_u *p; | |
6853 | |
6854 /* Copy the lines in "frombuf" to "tobuf". */ | |
6855 curbuf = tobuf; | |
6856 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum) | |
6857 { | |
6858 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE)); | |
6859 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL) | |
6860 { | |
6861 vim_free(p); | |
6862 retval = FAIL; | |
6863 break; | |
6864 } | |
6865 vim_free(p); | |
6866 } | |
6867 | |
6868 /* Delete all the lines in "frombuf". */ | |
6869 if (retval != FAIL) | |
6870 { | |
6871 curbuf = frombuf; | |
1055 | 6872 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum) |
6873 if (ml_delete(lnum, FALSE) == FAIL) | |
7 | 6874 { |
6875 /* Oops! We could try putting back the saved lines, but that | |
6876 * might fail again... */ | |
6877 retval = FAIL; | |
6878 break; | |
6879 } | |
6880 } | |
6881 | |
6882 curbuf = tbuf; | |
6883 return retval; | |
6884 } | |
6885 | |
6886 /* | |
6887 * Check if buffer "buf" has been changed. | |
6888 * Also check if the file for a new buffer unexpectedly appeared. | |
6889 * return 1 if a changed buffer was found. | |
6890 * return 2 if a message has been displayed. | |
6891 * return 0 otherwise. | |
6892 */ | |
6893 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6894 buf_check_timestamp( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6895 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6896 int focus UNUSED) /* called for GUI focus event */ |
7 | 6897 { |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6898 stat_T st; |
7 | 6899 int stat_res; |
6900 int retval = 0; | |
6901 char_u *path; | |
6902 char_u *tbuf; | |
6903 char *mesg = NULL; | |
188 | 6904 char *mesg2 = ""; |
7 | 6905 int helpmesg = FALSE; |
6906 int reload = FALSE; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6907 char *reason; |
7 | 6908 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
6909 int can_reload = FALSE; | |
6910 #endif | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6911 off_T orig_size = buf->b_orig_size; |
7 | 6912 int orig_mode = buf->b_orig_mode; |
6913 #ifdef FEAT_GUI | |
6914 int save_mouse_correct = need_mouse_correct; | |
6915 #endif | |
6916 static int busy = FALSE; | |
179 | 6917 int n; |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
6918 #ifdef FEAT_EVAL |
179 | 6919 char_u *s; |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
6920 #endif |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6921 bufref_T bufref; |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6922 |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6923 set_bufref(&bufref, buf); |
7 | 6924 |
6925 /* If there is no file name, the buffer is not loaded, 'buftype' is | |
6926 * set, we are in the middle of a save or being called recursively: ignore | |
6927 * this buffer. */ | |
6928 if (buf->b_ffname == NULL | |
6929 || buf->b_ml.ml_mfp == NULL | |
14433
4a94173743d9
patch 8.1.0230: directly checking 'buftype' value
Christian Brabandt <cb@256bit.org>
parents:
14290
diff
changeset
|
6930 || !bt_normal(buf) |
7 | 6931 || buf->b_saving |
6932 || busy | |
33 | 6933 #ifdef FEAT_NETBEANS_INTG |
6934 || isNetbeansBuffer(buf) | |
6935 #endif | |
12064
407a475c67fd
patch 8.0.0912: cannot run a job in a hidden terminal
Christian Brabandt <cb@256bit.org>
parents:
11757
diff
changeset
|
6936 #ifdef FEAT_TERMINAL |
407a475c67fd
patch 8.0.0912: cannot run a job in a hidden terminal
Christian Brabandt <cb@256bit.org>
parents:
11757
diff
changeset
|
6937 || buf->b_term != NULL |
407a475c67fd
patch 8.0.0912: cannot run a job in a hidden terminal
Christian Brabandt <cb@256bit.org>
parents:
11757
diff
changeset
|
6938 #endif |
7 | 6939 ) |
6940 return 0; | |
6941 | |
6942 if ( !(buf->b_flags & BF_NOTEDITED) | |
6943 && buf->b_mtime != 0 | |
6944 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0 | |
6945 || time_differs((long)st.st_mtime, buf->b_mtime) | |
5863 | 6946 || st.st_size != buf->b_orig_size |
7 | 6947 #ifdef HAVE_ST_MODE |
6948 || (int)st.st_mode != buf->b_orig_mode | |
6949 #else | |
6950 || mch_getperm(buf->b_ffname) != buf->b_orig_mode | |
6951 #endif | |
6952 )) | |
6953 { | |
6954 retval = 1; | |
6955 | |
14290
977cab3d5474
patch 8.1.0161: buffer not updated with 'autoread' set if file was deleted
Christian Brabandt <cb@256bit.org>
parents:
14157
diff
changeset
|
6956 // set b_mtime to stop further warnings (e.g., when executing |
977cab3d5474
patch 8.1.0161: buffer not updated with 'autoread' set if file was deleted
Christian Brabandt <cb@256bit.org>
parents:
14157
diff
changeset
|
6957 // FileChangedShell autocmd) |
7 | 6958 if (stat_res < 0) |
6959 { | |
14290
977cab3d5474
patch 8.1.0161: buffer not updated with 'autoread' set if file was deleted
Christian Brabandt <cb@256bit.org>
parents:
14157
diff
changeset
|
6960 // When 'autoread' is set we'll check the file again to see if it |
977cab3d5474
patch 8.1.0161: buffer not updated with 'autoread' set if file was deleted
Christian Brabandt <cb@256bit.org>
parents:
14157
diff
changeset
|
6961 // re-appears. |
977cab3d5474
patch 8.1.0161: buffer not updated with 'autoread' set if file was deleted
Christian Brabandt <cb@256bit.org>
parents:
14157
diff
changeset
|
6962 buf->b_mtime = buf->b_p_ar; |
7 | 6963 buf->b_orig_size = 0; |
6964 buf->b_orig_mode = 0; | |
6965 } | |
6966 else | |
6967 buf_store_time(buf, &st, buf->b_ffname); | |
6968 | |
6969 /* Don't do anything for a directory. Might contain the file | |
6970 * explorer. */ | |
6971 if (mch_isdir(buf->b_fname)) | |
6972 ; | |
6973 | |
6974 /* | |
6975 * If 'autoread' is set, the buffer has no changes and the file still | |
6976 * exists, reload the buffer. Use the buffer-local option value if it | |
6977 * was set, the global option value otherwise. | |
6978 */ | |
6979 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar) | |
6980 && !bufIsChanged(buf) && stat_res >= 0) | |
6981 reload = TRUE; | |
6982 else | |
6983 { | |
179 | 6984 if (stat_res < 0) |
6985 reason = "deleted"; | |
6986 else if (bufIsChanged(buf)) | |
6987 reason = "conflict"; | |
6988 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf)) | |
6989 reason = "changed"; | |
6990 else if (orig_mode != buf->b_orig_mode) | |
6991 reason = "mode"; | |
6992 else | |
6993 reason = "time"; | |
6994 | |
7 | 6995 /* |
6996 * Only give the warning if there are no FileChangedShell | |
6997 * autocommands. | |
6998 * Avoid being called recursively by setting "busy". | |
6999 */ | |
7000 busy = TRUE; | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
7001 #ifdef FEAT_EVAL |
179 | 7002 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1); |
7003 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1); | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
7004 #endif |
1834 | 7005 ++allbuf_lock; |
7 | 7006 n = apply_autocmds(EVENT_FILECHANGEDSHELL, |
7007 buf->b_fname, buf->b_fname, FALSE, buf); | |
1834 | 7008 --allbuf_lock; |
7 | 7009 busy = FALSE; |
7010 if (n) | |
7011 { | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
7012 if (!bufref_valid(&bufref)) |
7 | 7013 EMSG(_("E246: FileChangedShell autocommand deleted buffer")); |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
7014 #ifdef FEAT_EVAL |
179 | 7015 s = get_vim_var_str(VV_FCS_CHOICE); |
7016 if (STRCMP(s, "reload") == 0 && *reason != 'd') | |
7017 reload = TRUE; | |
7018 else if (STRCMP(s, "ask") == 0) | |
7019 n = FALSE; | |
7020 else | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
7021 #endif |
179 | 7022 return 2; |
7 | 7023 } |
179 | 7024 if (!n) |
7 | 7025 { |
179 | 7026 if (*reason == 'd') |
7027 mesg = _("E211: File \"%s\" no longer available"); | |
7 | 7028 else |
7029 { | |
7030 helpmesg = TRUE; | |
7031 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
7032 can_reload = TRUE; | |
7033 #endif | |
7034 /* | |
7035 * Check if the file contents really changed to avoid | |
7036 * giving a warning when only the timestamp was set (e.g., | |
7037 * checked out of CVS). Always warn when the buffer was | |
7038 * changed. | |
7039 */ | |
179 | 7040 if (reason[2] == 'n') |
7041 { | |
7 | 7042 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well"); |
179 | 7043 mesg2 = _("See \":help W12\" for more info."); |
7044 } | |
7045 else if (reason[1] == 'h') | |
7046 { | |
7 | 7047 mesg = _("W11: Warning: File \"%s\" has changed since editing started"); |
179 | 7048 mesg2 = _("See \":help W11\" for more info."); |
7049 } | |
7050 else if (*reason == 'm') | |
7051 { | |
7 | 7052 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started"); |
179 | 7053 mesg2 = _("See \":help W16\" for more info."); |
7054 } | |
1913 | 7055 else |
7056 /* Only timestamp changed, store it to avoid a warning | |
7057 * in check_mtime() later. */ | |
7058 buf->b_mtime_read = buf->b_mtime; | |
7 | 7059 } |
7060 } | |
7061 } | |
7062 | |
7063 } | |
7064 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W) | |
7065 && vim_fexists(buf->b_ffname)) | |
7066 { | |
7067 retval = 1; | |
7068 mesg = _("W13: Warning: File \"%s\" has been created after editing started"); | |
7069 buf->b_flags |= BF_NEW_W; | |
7070 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
7071 can_reload = TRUE; | |
7072 #endif | |
7073 } | |
7074 | |
7075 if (mesg != NULL) | |
7076 { | |
7077 path = home_replace_save(buf, buf->b_fname); | |
7078 if (path != NULL) | |
7079 { | |
179 | 7080 if (!helpmesg) |
7 | 7081 mesg2 = ""; |
7082 tbuf = alloc((unsigned)(STRLEN(path) + STRLEN(mesg) | |
7083 + STRLEN(mesg2) + 2)); | |
7084 sprintf((char *)tbuf, mesg, path); | |
1848 | 7085 #ifdef FEAT_EVAL |
7086 /* Set warningmsg here, before the unimportant and output-specific | |
7087 * mesg2 has been appended. */ | |
7088 set_vim_var_string(VV_WARNINGMSG, tbuf, -1); | |
7089 #endif | |
7 | 7090 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
7091 if (can_reload) | |
7092 { | |
7093 if (*mesg2 != NUL) | |
7094 { | |
7095 STRCAT(tbuf, "\n"); | |
7096 STRCAT(tbuf, mesg2); | |
7097 } | |
7098 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"), tbuf, | |
2684 | 7099 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2) |
7 | 7100 reload = TRUE; |
7101 } | |
7102 else | |
7103 #endif | |
7104 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned) | |
7105 { | |
7106 if (*mesg2 != NUL) | |
7107 { | |
7108 STRCAT(tbuf, "; "); | |
7109 STRCAT(tbuf, mesg2); | |
7110 } | |
7111 EMSG(tbuf); | |
7112 retval = 2; | |
7113 } | |
7114 else | |
7115 { | |
7116 if (!autocmd_busy) | |
7117 { | |
7118 msg_start(); | |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
7119 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST); |
7 | 7120 if (*mesg2 != NUL) |
7121 msg_puts_attr((char_u *)mesg2, | |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
7122 HL_ATTR(HLF_W) + MSG_HIST); |
7 | 7123 msg_clr_eos(); |
7124 (void)msg_end(); | |
7125 if (emsg_silent == 0) | |
7126 { | |
7127 out_flush(); | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
7128 #ifdef FEAT_GUI |
7 | 7129 if (!focus) |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
7130 #endif |
7 | 7131 /* give the user some time to think about it */ |
7132 ui_delay(1000L, TRUE); | |
7133 | |
7134 /* don't redraw and erase the message */ | |
7135 redraw_cmdline = FALSE; | |
7136 } | |
7137 } | |
7138 already_warned = TRUE; | |
7139 } | |
7140 | |
7141 vim_free(path); | |
7142 vim_free(tbuf); | |
7143 } | |
7144 } | |
7145 | |
7146 if (reload) | |
3776 | 7147 { |
311 | 7148 /* Reload the buffer. */ |
628 | 7149 buf_reload(buf, orig_mode); |
3776 | 7150 #ifdef FEAT_PERSISTENT_UNDO |
7151 if (buf->b_p_udf && buf->b_ffname != NULL) | |
7152 { | |
7153 char_u hash[UNDO_HASH_SIZE]; | |
7154 buf_T *save_curbuf = curbuf; | |
7155 | |
7156 /* Any existing undo file is unusable, write it now. */ | |
7157 curbuf = buf; | |
7158 u_compute_hash(hash); | |
7159 u_write_undo(NULL, FALSE, buf, hash); | |
7160 curbuf = save_curbuf; | |
7161 } | |
7162 #endif | |
7163 } | |
7 | 7164 |
1620 | 7165 /* Trigger FileChangedShell when the file was changed in any way. */ |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
7166 if (bufref_valid(&bufref) && retval != 0) |
765 | 7167 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, |
7168 buf->b_fname, buf->b_fname, FALSE, buf); | |
7 | 7169 #ifdef FEAT_GUI |
7170 /* restore this in case an autocommand has set it; it would break | |
7171 * 'mousefocus' */ | |
7172 need_mouse_correct = save_mouse_correct; | |
7173 #endif | |
7174 | |
7175 return retval; | |
7176 } | |
7177 | |
311 | 7178 /* |
7179 * Reload a buffer that is already loaded. | |
7180 * Used when the file was changed outside of Vim. | |
628 | 7181 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected. |
7182 * buf->b_orig_mode may have been reset already. | |
311 | 7183 */ |
7184 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7185 buf_reload(buf_T *buf, int orig_mode) |
311 | 7186 { |
7187 exarg_T ea; | |
7188 pos_T old_cursor; | |
7189 linenr_T old_topline; | |
7190 int old_ro = buf->b_p_ro; | |
7191 buf_T *savebuf; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
7192 bufref_T bufref; |
311 | 7193 int saved = OK; |
7194 aco_save_T aco; | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7195 int flags = READ_NEW; |
311 | 7196 |
7197 /* set curwin/curbuf for "buf" and save some things */ | |
7198 aucmd_prepbuf(&aco, buf); | |
7199 | |
7200 /* We only want to read the text from the file, not reset the syntax | |
7201 * highlighting, clear marks, diff status, etc. Force the fileformat | |
7202 * and encoding to be the same. */ | |
7203 if (prep_exarg(&ea, buf) == OK) | |
7204 { | |
7205 old_cursor = curwin->w_cursor; | |
7206 old_topline = curwin->w_topline; | |
7207 | |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
7208 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur) |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7209 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7210 /* Save all the text, so that the reload can be undone. |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7211 * Sync first so that this is a separate undo-able action. */ |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7212 u_sync(FALSE); |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7213 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE); |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7214 flags |= READ_KEEP_UNDO; |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7215 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7216 |
311 | 7217 /* |
7218 * To behave like when a new file is edited (matters for | |
7219 * BufReadPost autocommands) we first need to delete the current | |
7220 * buffer contents. But if reading the file fails we should keep | |
7221 * the old contents. Can't use memory only, the file might be | |
7222 * too big. Use a hidden buffer to move the buffer contents to. | |
7223 */ | |
11121
778c10516955
patch 8.0.0448: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
10952
diff
changeset
|
7224 if (BUFEMPTY() || saved == FAIL) |
311 | 7225 savebuf = NULL; |
7226 else | |
7227 { | |
7228 /* Allocate a buffer without putting it in the buffer list. */ | |
7229 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
7230 set_bufref(&bufref, savebuf); |
837 | 7231 if (savebuf != NULL && buf == curbuf) |
311 | 7232 { |
7233 /* Open the memline. */ | |
7234 curbuf = savebuf; | |
7235 curwin->w_buffer = savebuf; | |
625 | 7236 saved = ml_open(curbuf); |
311 | 7237 curbuf = buf; |
7238 curwin->w_buffer = buf; | |
7239 } | |
837 | 7240 if (savebuf == NULL || saved == FAIL || buf != curbuf |
311 | 7241 || move_lines(buf, savebuf) == FAIL) |
7242 { | |
7243 EMSG2(_("E462: Could not prepare for reloading \"%s\""), | |
7244 buf->b_fname); | |
7245 saved = FAIL; | |
7246 } | |
7247 } | |
7248 | |
7249 if (saved == OK) | |
7250 { | |
7251 curbuf->b_flags |= BF_CHECK_RO; /* check for RO again */ | |
7252 keep_filetype = TRUE; /* don't detect 'filetype' */ | |
7253 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, | |
7254 (linenr_T)0, | |
10575
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
7255 (linenr_T)MAXLNUM, &ea, flags) != OK) |
311 | 7256 { |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
7257 #if defined(FEAT_EVAL) |
311 | 7258 if (!aborting()) |
7259 #endif | |
7260 EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname); | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
7261 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf) |
311 | 7262 { |
7263 /* Put the text back from the save buffer. First | |
7264 * delete any lines that readfile() added. */ | |
11121
778c10516955
patch 8.0.0448: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
10952
diff
changeset
|
7265 while (!BUFEMPTY()) |
837 | 7266 if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL) |
311 | 7267 break; |
7268 (void)move_lines(savebuf, buf); | |
7269 } | |
7270 } | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7271 else if (buf == curbuf) /* "buf" still valid */ |
311 | 7272 { |
7273 /* Mark the buffer as unmodified and free undo info. */ | |
7274 unchanged(buf, TRUE); | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7275 if ((flags & READ_KEEP_UNDO) == 0) |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7276 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7277 u_blockfree(buf); |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7278 u_clearall(buf); |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7279 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7280 else |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
7281 { |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7282 /* Mark all undo states as changed. */ |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7283 u_unchanged(curbuf); |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
7284 } |
311 | 7285 } |
7286 } | |
7287 vim_free(ea.cmd); | |
7288 | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
7289 if (savebuf != NULL && bufref_valid(&bufref)) |
311 | 7290 wipe_buffer(savebuf, FALSE); |
7291 | |
7292 #ifdef FEAT_DIFF | |
7293 /* Invalidate diff info if necessary. */ | |
837 | 7294 diff_invalidate(curbuf); |
311 | 7295 #endif |
7296 | |
7297 /* Restore the topline and cursor position and check it (lines may | |
7298 * have been removed). */ | |
7299 if (old_topline > curbuf->b_ml.ml_line_count) | |
7300 curwin->w_topline = curbuf->b_ml.ml_line_count; | |
7301 else | |
7302 curwin->w_topline = old_topline; | |
7303 curwin->w_cursor = old_cursor; | |
7304 check_cursor(); | |
7305 update_topline(); | |
7306 keep_filetype = FALSE; | |
7307 #ifdef FEAT_FOLDING | |
7308 { | |
1863 | 7309 win_T *wp; |
7310 tabpage_T *tp; | |
311 | 7311 |
7312 /* Update folds unless they are defined manually. */ | |
1863 | 7313 FOR_ALL_TAB_WINDOWS(tp, wp) |
311 | 7314 if (wp->w_buffer == curwin->w_buffer |
7315 && !foldmethodIsManual(wp)) | |
7316 foldUpdateAll(wp); | |
7317 } | |
7318 #endif | |
7319 /* If the mode didn't change and 'readonly' was set, keep the old | |
7320 * value; the user probably used the ":view" command. But don't | |
7321 * reset it, might have had a read error. */ | |
7322 if (orig_mode == curbuf->b_orig_mode) | |
7323 curbuf->b_p_ro |= old_ro; | |
4071 | 7324 |
7325 /* Modelines must override settings done by autocommands. */ | |
7326 do_modelines(0); | |
311 | 7327 } |
7328 | |
7329 /* restore curwin/curbuf and a few other things */ | |
7330 aucmd_restbuf(&aco); | |
7331 /* Careful: autocommands may have made "buf" invalid! */ | |
7332 } | |
7333 | |
7 | 7334 void |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
7335 buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED) |
7 | 7336 { |
7337 buf->b_mtime = (long)st->st_mtime; | |
2241
60da25e3aab7
Correct use of long instead of off_t for file size. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2239
diff
changeset
|
7338 buf->b_orig_size = st->st_size; |
7 | 7339 #ifdef HAVE_ST_MODE |
7340 buf->b_orig_mode = (int)st->st_mode; | |
7341 #else | |
7342 buf->b_orig_mode = mch_getperm(fname); | |
7343 #endif | |
7344 } | |
7345 | |
7346 /* | |
7347 * Adjust the line with missing eol, used for the next write. | |
7348 * Used for do_filter(), when the input lines for the filter are deleted. | |
7349 */ | |
7350 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7351 write_lnum_adjust(linenr_T offset) |
7 | 7352 { |
2707 | 7353 if (curbuf->b_no_eol_lnum != 0) /* only if there is a missing eol */ |
7354 curbuf->b_no_eol_lnum += offset; | |
7 | 7355 } |
7356 | |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7357 #if defined(TEMPDIRNAMES) || defined(FEAT_EVAL) || defined(PROTO) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7358 /* |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7359 * Delete "name" and everything in it, recursively. |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7360 * return 0 for succes, -1 if some file was not deleted. |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7361 */ |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7362 int |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7363 delete_recursive(char_u *name) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7364 { |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7365 int result = 0; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7366 char_u **files; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7367 int file_count; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7368 int i; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7369 char_u *exp; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7370 |
7629
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7371 /* A symbolic link to a directory itself is deleted, not the directory it |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7372 * points to. */ |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7373 if ( |
7657
9c5e8254ea6b
commit https://github.com/vim/vim/commit/203258c3ad2966cc9d08b3805b103333988b30b7
Christian Brabandt <cb@256bit.org>
parents:
7641
diff
changeset
|
7374 # if defined(UNIX) || defined(WIN32) |
9c5e8254ea6b
commit https://github.com/vim/vim/commit/203258c3ad2966cc9d08b3805b103333988b30b7
Christian Brabandt <cb@256bit.org>
parents:
7641
diff
changeset
|
7375 mch_isrealdir(name) |
7629
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7376 # else |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7377 mch_isdir(name) |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7378 # endif |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7379 ) |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7380 { |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7381 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/*", name); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7382 exp = vim_strsave(NameBuff); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7383 if (exp == NULL) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7384 return -1; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7385 if (gen_expand_wildcards(1, &exp, &file_count, &files, |
7641
b44fc33ef92a
commit https://github.com/vim/vim/commit/336bd622c31e1805495c034e1a8cfadcc0bbabc7
Christian Brabandt <cb@256bit.org>
parents:
7633
diff
changeset
|
7386 EW_DIR|EW_FILE|EW_SILENT|EW_ALLLINKS|EW_DODOT|EW_EMPTYOK) == OK) |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7387 { |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7388 for (i = 0; i < file_count; ++i) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7389 if (delete_recursive(files[i]) != 0) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7390 result = -1; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7391 FreeWild(file_count, files); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7392 } |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7393 else |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7394 result = -1; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7395 vim_free(exp); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7396 (void)mch_rmdir(name); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7397 } |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7398 else |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7399 result = mch_remove(name) == 0 ? 0 : -1; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7400 |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7401 return result; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7402 } |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7403 #endif |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7404 |
7 | 7405 #if defined(TEMPDIRNAMES) || defined(PROTO) |
7406 static long temp_count = 0; /* Temp filename counter. */ | |
7407 | |
7408 /* | |
7409 * Delete the temp directory and all files it contains. | |
7410 */ | |
7411 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7412 vim_deltempdir(void) |
7 | 7413 { |
7414 if (vim_tempdir != NULL) | |
7415 { | |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7416 /* remove the trailing path separator */ |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7417 gettail(vim_tempdir)[-1] = NUL; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7418 delete_recursive(vim_tempdir); |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
7419 VIM_CLEAR(vim_tempdir); |
7 | 7420 } |
7421 } | |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7422 |
7 | 7423 /* |
1997 | 7424 * Directory "tempdir" was created. Expand this name to a full path and put |
7425 * it in "vim_tempdir". This avoids that using ":cd" would confuse us. | |
7426 * "tempdir" must be no longer than MAXPATHL. | |
7427 */ | |
7428 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7429 vim_settempdir(char_u *tempdir) |
1997 | 7430 { |
7431 char_u *buf; | |
7432 | |
7433 buf = alloc((unsigned)MAXPATHL + 2); | |
7434 if (buf != NULL) | |
7435 { | |
7436 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL) | |
7437 STRCPY(buf, tempdir); | |
9536
b2aada04d84e
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Christian Brabandt <cb@256bit.org>
parents:
9509
diff
changeset
|
7438 add_pathsep(buf); |
1997 | 7439 vim_tempdir = vim_strsave(buf); |
7440 vim_free(buf); | |
7441 } | |
7442 } | |
2006 | 7443 #endif |
1997 | 7444 |
7445 /* | |
7 | 7446 * vim_tempname(): Return a unique name that can be used for a temp file. |
7447 * | |
9291
bff3cd5cf922
commit https://github.com/vim/vim/commit/76ae22fef3cb224ca7fbf97517f881e825d4d0c2
Christian Brabandt <cb@256bit.org>
parents:
9260
diff
changeset
|
7448 * The temp file is NOT guaranteed to be created. If "keep" is FALSE it is |
bff3cd5cf922
commit https://github.com/vim/vim/commit/76ae22fef3cb224ca7fbf97517f881e825d4d0c2
Christian Brabandt <cb@256bit.org>
parents:
9260
diff
changeset
|
7449 * guaranteed to NOT be created. |
7 | 7450 * |
7451 * The returned pointer is to allocated memory. | |
7452 * The returned pointer is NULL if no valid name was found. | |
7453 */ | |
7454 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7455 vim_tempname( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7456 int extra_char UNUSED, /* char to use in the name instead of '?' */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7457 int keep UNUSED) |
7 | 7458 { |
7459 #ifdef USE_TMPNAM | |
7460 char_u itmp[L_tmpnam]; /* use tmpnam() */ | |
7461 #else | |
7462 char_u itmp[TEMPNAMELEN]; | |
7463 #endif | |
7464 | |
7465 #ifdef TEMPDIRNAMES | |
7466 static char *(tempdirs[]) = {TEMPDIRNAMES}; | |
7467 int i; | |
7468 # ifndef EEXIST | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
7469 stat_T st; |
7 | 7470 # endif |
7471 | |
7472 /* | |
7473 * This will create a directory for private use by this instance of Vim. | |
7474 * This is done once, and the same directory is used for all temp files. | |
7475 * This method avoids security problems because of symlink attacks et al. | |
7476 * It's also a bit faster, because we only need to check for an existing | |
7477 * file when creating the directory and not for each temp file. | |
7478 */ | |
7479 if (vim_tempdir == NULL) | |
7480 { | |
7481 /* | |
7482 * Try the entries in TEMPDIRNAMES to create the temp directory. | |
7483 */ | |
1877 | 7484 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i) |
7 | 7485 { |
2047
85da03763130
updated for version 7.2.333
Bram Moolenaar <bram@zimbu.org>
parents:
2028
diff
changeset
|
7486 # ifndef HAVE_MKDTEMP |
1997 | 7487 size_t itmplen; |
7488 long nr; | |
7489 long off; | |
7490 # endif | |
7491 | |
7305
372c785c04b6
commit https://github.com/vim/vim/commit/e1a61991d9b6fd5f65636d17583f93118268cda5
Christian Brabandt <cb@256bit.org>
parents:
7285
diff
changeset
|
7492 /* Expand $TMP, leave room for "/v1100000/999999999". |
372c785c04b6
commit https://github.com/vim/vim/commit/e1a61991d9b6fd5f65636d17583f93118268cda5
Christian Brabandt <cb@256bit.org>
parents:
7285
diff
changeset
|
7493 * Skip the directory check if the expansion fails. */ |
7 | 7494 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20); |
7305
372c785c04b6
commit https://github.com/vim/vim/commit/e1a61991d9b6fd5f65636d17583f93118268cda5
Christian Brabandt <cb@256bit.org>
parents:
7285
diff
changeset
|
7495 if (itmp[0] != '$' && mch_isdir(itmp)) |
372c785c04b6
commit https://github.com/vim/vim/commit/e1a61991d9b6fd5f65636d17583f93118268cda5
Christian Brabandt <cb@256bit.org>
parents:
7285
diff
changeset
|
7496 { |
372c785c04b6
commit https://github.com/vim/vim/commit/e1a61991d9b6fd5f65636d17583f93118268cda5
Christian Brabandt <cb@256bit.org>
parents:
7285
diff
changeset
|
7497 /* directory exists */ |
9536
b2aada04d84e
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Christian Brabandt <cb@256bit.org>
parents:
9509
diff
changeset
|
7498 add_pathsep(itmp); |
1997 | 7499 |
7500 # ifdef HAVE_MKDTEMP | |
9211
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7501 { |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7502 # if defined(UNIX) || defined(VMS) |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7503 /* Make sure the umask doesn't remove the executable bit. |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7504 * "repl" has been reported to use "177". */ |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7505 mode_t umask_save = umask(077); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7506 # endif |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7507 /* Leave room for filename */ |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7508 STRCAT(itmp, "vXXXXXX"); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7509 if (mkdtemp((char *)itmp) != NULL) |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7510 vim_settempdir(itmp); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7511 # if defined(UNIX) || defined(VMS) |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7512 (void)umask(umask_save); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7513 # endif |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7514 } |
1997 | 7515 # else |
7 | 7516 /* Get an arbitrary number of up to 6 digits. When it's |
7517 * unlikely that it already exists it will be faster, | |
7518 * otherwise it doesn't matter. The use of mkdir() avoids any | |
7519 * security problems because of the predictable number. */ | |
7520 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L; | |
2047
85da03763130
updated for version 7.2.333
Bram Moolenaar <bram@zimbu.org>
parents:
2028
diff
changeset
|
7521 itmplen = STRLEN(itmp); |
7 | 7522 |
7523 /* Try up to 10000 different values until we find a name that | |
7524 * doesn't exist. */ | |
7525 for (off = 0; off < 10000L; ++off) | |
7526 { | |
7527 int r; | |
1997 | 7528 # if defined(UNIX) || defined(VMS) |
7 | 7529 mode_t umask_save; |
1997 | 7530 # endif |
7531 | |
7532 sprintf((char *)itmp + itmplen, "v%ld", nr + off); | |
7533 # ifndef EEXIST | |
7 | 7534 /* If mkdir() does not set errno to EEXIST, check for |
7535 * existing file here. There is a race condition then, | |
7536 * although it's fail-safe. */ | |
7537 if (mch_stat((char *)itmp, &st) >= 0) | |
7538 continue; | |
1997 | 7539 # endif |
7540 # if defined(UNIX) || defined(VMS) | |
7 | 7541 /* Make sure the umask doesn't remove the executable bit. |
7542 * "repl" has been reported to use "177". */ | |
7543 umask_save = umask(077); | |
1997 | 7544 # endif |
7 | 7545 r = vim_mkdir(itmp, 0700); |
1997 | 7546 # if defined(UNIX) || defined(VMS) |
7 | 7547 (void)umask(umask_save); |
1997 | 7548 # endif |
7 | 7549 if (r == 0) |
7550 { | |
1997 | 7551 vim_settempdir(itmp); |
7 | 7552 break; |
7553 } | |
1997 | 7554 # ifdef EEXIST |
7 | 7555 /* If the mkdir() didn't fail because the file/dir exists, |
7556 * we probably can't create any dir here, try another | |
7557 * place. */ | |
7558 if (errno != EEXIST) | |
1997 | 7559 # endif |
7 | 7560 break; |
7561 } | |
1997 | 7562 # endif /* HAVE_MKDTEMP */ |
7 | 7563 if (vim_tempdir != NULL) |
7564 break; | |
7565 } | |
7566 } | |
7567 } | |
7568 | |
7569 if (vim_tempdir != NULL) | |
7570 { | |
7571 /* There is no need to check if the file exists, because we own the | |
7572 * directory and nobody else creates a file in it. */ | |
7573 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++); | |
7574 return vim_strsave(itmp); | |
7575 } | |
7576 | |
7577 return NULL; | |
7578 | |
7579 #else /* TEMPDIRNAMES */ | |
7580 | |
7581 # ifdef WIN3264 | |
7582 char szTempFile[_MAX_PATH + 1]; | |
7583 char buf4[4]; | |
7584 char_u *retval; | |
7585 char_u *p; | |
7586 | |
7587 STRCPY(itmp, ""); | |
7588 if (GetTempPath(_MAX_PATH, szTempFile) == 0) | |
2695 | 7589 { |
7590 szTempFile[0] = '.'; /* GetTempPath() failed, use current dir */ | |
7591 szTempFile[1] = NUL; | |
7592 } | |
7 | 7593 strcpy(buf4, "VIM"); |
7594 buf4[2] = extra_char; /* make it "VIa", "VIb", etc. */ | |
8080
b6cb94ad97a4
commit https://github.com/vim/vim/commit/6aa2cd4be287f35f95f35c2cec6d5a24f53c4d3c
Christian Brabandt <cb@256bit.org>
parents:
7961
diff
changeset
|
7595 if (GetTempFileName(szTempFile, buf4, 0, (LPSTR)itmp) == 0) |
7 | 7596 return NULL; |
6721 | 7597 if (!keep) |
7598 /* GetTempFileName() will create the file, we don't want that */ | |
8080
b6cb94ad97a4
commit https://github.com/vim/vim/commit/6aa2cd4be287f35f95f35c2cec6d5a24f53c4d3c
Christian Brabandt <cb@256bit.org>
parents:
7961
diff
changeset
|
7599 (void)DeleteFile((LPSTR)itmp); |
7 | 7600 |
7601 /* Backslashes in a temp file name cause problems when filtering with | |
7602 * "sh". NOTE: This also checks 'shellcmdflag' to help those people who | |
7603 * didn't set 'shellslash'. */ | |
7604 retval = vim_strsave(itmp); | |
7605 if (*p_shcf == '-' || p_ssl) | |
7606 for (p = retval; *p; ++p) | |
7607 if (*p == '\\') | |
7608 *p = '/'; | |
7609 return retval; | |
7610 | |
7611 # else /* WIN3264 */ | |
7612 | |
7613 # ifdef USE_TMPNAM | |
2697 | 7614 char_u *p; |
7615 | |
7 | 7616 /* tmpnam() will make its own name */ |
2697 | 7617 p = tmpnam((char *)itmp); |
7618 if (p == NULL || *p == NUL) | |
7 | 7619 return NULL; |
7620 # else | |
7621 char_u *p; | |
7622 | |
7623 # ifdef VMS_TEMPNAM | |
7624 /* mktemp() is not working on VMS. It seems to be | |
7625 * a do-nothing function. Therefore we use tempnam(). | |
7626 */ | |
7627 sprintf((char *)itmp, "VIM%c", extra_char); | |
7628 p = (char_u *)tempnam("tmp:", (char *)itmp); | |
7629 if (p != NULL) | |
7630 { | |
5704 | 7631 /* VMS will use '.LIS' if we don't explicitly specify an extension, |
7 | 7632 * and VIM will then be unable to find the file later */ |
7633 STRCPY(itmp, p); | |
7634 STRCAT(itmp, ".txt"); | |
7635 free(p); | |
7636 } | |
7637 else | |
7638 return NULL; | |
7639 # else | |
7640 STRCPY(itmp, TEMPNAME); | |
7641 if ((p = vim_strchr(itmp, '?')) != NULL) | |
7642 *p = extra_char; | |
7643 if (mktemp((char *)itmp) == NULL) | |
7644 return NULL; | |
7645 # endif | |
7646 # endif | |
7647 | |
7648 return vim_strsave(itmp); | |
7649 # endif /* WIN3264 */ | |
7650 #endif /* TEMPDIRNAMES */ | |
7651 } | |
7652 | |
7653 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) | |
7654 /* | |
7170
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
7655 * Convert all backslashes in fname to forward slashes in-place, unless when |
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
7656 * it looks like a URL. |
7 | 7657 */ |
7658 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7659 forward_slash(char_u *fname) |
7 | 7660 { |
7661 char_u *p; | |
7662 | |
7170
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
7663 if (path_with_url(fname)) |
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
7664 return; |
7 | 7665 for (p = fname; *p != NUL; ++p) |
7666 # ifdef FEAT_MBYTE | |
7667 /* The Big5 encoding can have '\' in the trail byte. */ | |
474 | 7668 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 7669 ++p; |
7670 else | |
7671 # endif | |
7672 if (*p == '\\') | |
7673 *p = '/'; | |
7674 } | |
7675 #endif | |
7676 | |
7677 | |
7678 /* | |
7679 * Code for automatic commands. | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
7680 */ |
7 | 7681 |
7682 /* | |
7683 * The autocommands are stored in a list for each event. | |
7684 * Autocommands for the same pattern, that are consecutive, are joined | |
7685 * together, to avoid having to match the pattern too often. | |
7686 * The result is an array of Autopat lists, which point to AutoCmd lists: | |
7687 * | |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7688 * last_autopat[0] -----------------------------+ |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7689 * V |
7 | 7690 * first_autopat[0] --> Autopat.next --> Autopat.next --> NULL |
7691 * Autopat.cmds Autopat.cmds | |
7692 * | | | |
7693 * V V | |
7694 * AutoCmd.next AutoCmd.next | |
7695 * | | | |
7696 * V V | |
7697 * AutoCmd.next NULL | |
7698 * | | |
7699 * V | |
7700 * NULL | |
7701 * | |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7702 * last_autopat[1] --------+ |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7703 * V |
7 | 7704 * first_autopat[1] --> Autopat.next --> NULL |
7705 * Autopat.cmds | |
7706 * | | |
7707 * V | |
7708 * AutoCmd.next | |
7709 * | | |
7710 * V | |
7711 * NULL | |
7712 * etc. | |
7713 * | |
7714 * The order of AutoCmds is important, this is the order in which they were | |
7715 * defined and will have to be executed. | |
7716 */ | |
7717 typedef struct AutoCmd | |
7718 { | |
7719 char_u *cmd; /* The command to be executed (NULL | |
7720 when command has been removed) */ | |
7721 char nested; /* If autocommands nest here */ | |
7722 char last; /* last command in list */ | |
7723 #ifdef FEAT_EVAL | |
7724 scid_T scriptID; /* script ID where defined */ | |
7725 #endif | |
7726 struct AutoCmd *next; /* Next AutoCmd in list */ | |
7727 } AutoCmd; | |
7728 | |
7729 typedef struct AutoPat | |
7730 { | |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7731 struct AutoPat *next; /* next AutoPat in AutoPat list; MUST |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7732 * be the first entry */ |
7 | 7733 char_u *pat; /* pattern as typed (NULL when pattern |
7734 has been removed) */ | |
4861
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7735 regprog_T *reg_prog; /* compiled regprog for pattern */ |
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7736 AutoCmd *cmds; /* list of commands to do */ |
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7737 int group; /* group ID */ |
7 | 7738 int patlen; /* strlen() of pat */ |
4861
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7739 int buflocal_nr; /* !=0 for buffer-local AutoPat */ |
7 | 7740 char allow_dirs; /* Pattern may match whole path */ |
7741 char last; /* last pattern for apply_autocmds() */ | |
7742 } AutoPat; | |
7743 | |
7744 static struct event_name | |
7745 { | |
7746 char *name; /* event name */ | |
661 | 7747 event_T event; /* event number */ |
7 | 7748 } event_names[] = |
7749 { | |
7750 {"BufAdd", EVENT_BUFADD}, | |
7751 {"BufCreate", EVENT_BUFADD}, | |
7752 {"BufDelete", EVENT_BUFDELETE}, | |
7753 {"BufEnter", EVENT_BUFENTER}, | |
7754 {"BufFilePost", EVENT_BUFFILEPOST}, | |
7755 {"BufFilePre", EVENT_BUFFILEPRE}, | |
7756 {"BufHidden", EVENT_BUFHIDDEN}, | |
7757 {"BufLeave", EVENT_BUFLEAVE}, | |
7758 {"BufNew", EVENT_BUFNEW}, | |
7759 {"BufNewFile", EVENT_BUFNEWFILE}, | |
7760 {"BufRead", EVENT_BUFREADPOST}, | |
7761 {"BufReadCmd", EVENT_BUFREADCMD}, | |
7762 {"BufReadPost", EVENT_BUFREADPOST}, | |
7763 {"BufReadPre", EVENT_BUFREADPRE}, | |
7764 {"BufUnload", EVENT_BUFUNLOAD}, | |
7765 {"BufWinEnter", EVENT_BUFWINENTER}, | |
7766 {"BufWinLeave", EVENT_BUFWINLEAVE}, | |
7767 {"BufWipeout", EVENT_BUFWIPEOUT}, | |
7768 {"BufWrite", EVENT_BUFWRITEPRE}, | |
7769 {"BufWritePost", EVENT_BUFWRITEPOST}, | |
7770 {"BufWritePre", EVENT_BUFWRITEPRE}, | |
7771 {"BufWriteCmd", EVENT_BUFWRITECMD}, | |
13142
59a16624400a
patch 8.0.1445: cannot act on edits in the command line
Christian Brabandt <cb@256bit.org>
parents:
13037
diff
changeset
|
7772 {"CmdlineChanged", EVENT_CMDLINECHANGED}, |
12656
0a9dacb8826a
patch 8.0.1206: no autocmd for entering or leaving the command line
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
7773 {"CmdlineEnter", EVENT_CMDLINEENTER}, |
0a9dacb8826a
patch 8.0.1206: no autocmd for entering or leaving the command line
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
7774 {"CmdlineLeave", EVENT_CMDLINELEAVE}, |
7 | 7775 {"CmdwinEnter", EVENT_CMDWINENTER}, |
7776 {"CmdwinLeave", EVENT_CMDWINLEAVE}, | |
6154 | 7777 {"CmdUndefined", EVENT_CMDUNDEFINED}, |
12 | 7778 {"ColorScheme", EVENT_COLORSCHEME}, |
13810
cad480bac9e1
patch 8.0.1777: cannot cleanup before loading another colorscheme
Christian Brabandt <cb@256bit.org>
parents:
13804
diff
changeset
|
7779 {"ColorSchemePre", EVENT_COLORSCHEMEPRE}, |
3676 | 7780 {"CompleteDone", EVENT_COMPLETEDONE}, |
661 | 7781 {"CursorHold", EVENT_CURSORHOLD}, |
7782 {"CursorHoldI", EVENT_CURSORHOLDI}, | |
7783 {"CursorMoved", EVENT_CURSORMOVED}, | |
7784 {"CursorMovedI", EVENT_CURSORMOVEDI}, | |
13170
6559e98f3e74
patch 8.0.1459: cannot handle change of directory
Christian Brabandt <cb@256bit.org>
parents:
13142
diff
changeset
|
7785 {"DirChanged", EVENT_DIRCHANGED}, |
7 | 7786 {"EncodingChanged", EVENT_ENCODINGCHANGED}, |
13442
94e638936d3e
patch 8.0.1595: no autocommand triggered before exiting
Christian Brabandt <cb@256bit.org>
parents:
13380
diff
changeset
|
7787 {"ExitPre", EVENT_EXITPRE}, |
7 | 7788 {"FileEncoding", EVENT_ENCODINGCHANGED}, |
7789 {"FileAppendPost", EVENT_FILEAPPENDPOST}, | |
7790 {"FileAppendPre", EVENT_FILEAPPENDPRE}, | |
7791 {"FileAppendCmd", EVENT_FILEAPPENDCMD}, | |
7792 {"FileChangedShell",EVENT_FILECHANGEDSHELL}, | |
765 | 7793 {"FileChangedShellPost",EVENT_FILECHANGEDSHELLPOST}, |
7 | 7794 {"FileChangedRO", EVENT_FILECHANGEDRO}, |
7795 {"FileReadPost", EVENT_FILEREADPOST}, | |
7796 {"FileReadPre", EVENT_FILEREADPRE}, | |
7797 {"FileReadCmd", EVENT_FILEREADCMD}, | |
7798 {"FileType", EVENT_FILETYPE}, | |
7799 {"FileWritePost", EVENT_FILEWRITEPOST}, | |
7800 {"FileWritePre", EVENT_FILEWRITEPRE}, | |
7801 {"FileWriteCmd", EVENT_FILEWRITECMD}, | |
7802 {"FilterReadPost", EVENT_FILTERREADPOST}, | |
7803 {"FilterReadPre", EVENT_FILTERREADPRE}, | |
7804 {"FilterWritePost", EVENT_FILTERWRITEPOST}, | |
7805 {"FilterWritePre", EVENT_FILTERWRITEPRE}, | |
7806 {"FocusGained", EVENT_FOCUSGAINED}, | |
7807 {"FocusLost", EVENT_FOCUSLOST}, | |
7808 {"FuncUndefined", EVENT_FUNCUNDEFINED}, | |
7809 {"GUIEnter", EVENT_GUIENTER}, | |
946 | 7810 {"GUIFailed", EVENT_GUIFAILED}, |
11 | 7811 {"InsertChange", EVENT_INSERTCHANGE}, |
7812 {"InsertEnter", EVENT_INSERTENTER}, | |
7813 {"InsertLeave", EVENT_INSERTLEAVE}, | |
2845 | 7814 {"InsertCharPre", EVENT_INSERTCHARPRE}, |
434 | 7815 {"MenuPopup", EVENT_MENUPOPUP}, |
6935 | 7816 {"OptionSet", EVENT_OPTIONSET}, |
161 | 7817 {"QuickFixCmdPost", EVENT_QUICKFIXCMDPOST}, |
7818 {"QuickFixCmdPre", EVENT_QUICKFIXCMDPRE}, | |
3568 | 7819 {"QuitPre", EVENT_QUITPRE}, |
7 | 7820 {"RemoteReply", EVENT_REMOTEREPLY}, |
574 | 7821 {"SessionLoadPost", EVENT_SESSIONLOADPOST}, |
723 | 7822 {"ShellCmdPost", EVENT_SHELLCMDPOST}, |
7823 {"ShellFilterPost", EVENT_SHELLFILTERPOST}, | |
715 | 7824 {"SourcePre", EVENT_SOURCEPRE}, |
1061 | 7825 {"SourceCmd", EVENT_SOURCECMD}, |
649 | 7826 {"SpellFileMissing",EVENT_SPELLFILEMISSING}, |
7 | 7827 {"StdinReadPost", EVENT_STDINREADPOST}, |
7828 {"StdinReadPre", EVENT_STDINREADPRE}, | |
674 | 7829 {"SwapExists", EVENT_SWAPEXISTS}, |
7 | 7830 {"Syntax", EVENT_SYNTAX}, |
9595
0190d5de215f
commit https://github.com/vim/vim/commit/c917da4b3e8801a255dbefea8e4ed19c1c716dd8
Christian Brabandt <cb@256bit.org>
parents:
9536
diff
changeset
|
7831 {"TabNew", EVENT_TABNEW}, |
9599
42a8a81decdf
commit https://github.com/vim/vim/commit/12c11d553053f5a9eae9eb3c518279b12fa928c2
Christian Brabandt <cb@256bit.org>
parents:
9595
diff
changeset
|
7832 {"TabClosed", EVENT_TABCLOSED}, |
676 | 7833 {"TabEnter", EVENT_TABENTER}, |
7834 {"TabLeave", EVENT_TABLEAVE}, | |
7 | 7835 {"TermChanged", EVENT_TERMCHANGED}, |
13444
9f06f7aca74c
patch 8.0.1596: no autocommand specifically for opening a terminal window
Christian Brabandt <cb@256bit.org>
parents:
13442
diff
changeset
|
7836 {"TerminalOpen", EVENT_TERMINALOPEN}, |
7 | 7837 {"TermResponse", EVENT_TERMRESPONSE}, |
4232 | 7838 {"TextChanged", EVENT_TEXTCHANGED}, |
7839 {"TextChangedI", EVENT_TEXTCHANGEDI}, | |
13240
5ed6e4a25925
patch 8.0.1494: no autocmd triggered in Insert mode with visible popup menu
Christian Brabandt <cb@256bit.org>
parents:
13170
diff
changeset
|
7840 {"TextChangedP", EVENT_TEXTCHANGEDP}, |
7 | 7841 {"User", EVENT_USER}, |
7842 {"VimEnter", EVENT_VIMENTER}, | |
7843 {"VimLeave", EVENT_VIMLEAVE}, | |
7844 {"VimLeavePre", EVENT_VIMLEAVEPRE}, | |
9595
0190d5de215f
commit https://github.com/vim/vim/commit/c917da4b3e8801a255dbefea8e4ed19c1c716dd8
Christian Brabandt <cb@256bit.org>
parents:
9536
diff
changeset
|
7845 {"WinNew", EVENT_WINNEW}, |
7 | 7846 {"WinEnter", EVENT_WINENTER}, |
7847 {"WinLeave", EVENT_WINLEAVE}, | |
765 | 7848 {"VimResized", EVENT_VIMRESIZED}, |
13037
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
7849 {"TextYankPost", EVENT_TEXTYANKPOST}, |
661 | 7850 {NULL, (event_T)0} |
7 | 7851 }; |
7852 | |
7853 static AutoPat *first_autopat[NUM_EVENTS] = | |
7854 { | |
7855 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7856 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7857 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7858 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
18 | 7859 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
6935 | 7860 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
7 | 7861 }; |
7862 | |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7863 static AutoPat *last_autopat[NUM_EVENTS] = |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7864 { |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7865 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7866 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7867 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7868 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7869 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7870 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7871 }; |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7872 |
7 | 7873 /* |
7874 * struct used to keep status while executing autocommands for an event. | |
7875 */ | |
7876 typedef struct AutoPatCmd | |
7877 { | |
7878 AutoPat *curpat; /* next AutoPat to examine */ | |
7879 AutoCmd *nextcmd; /* next AutoCmd to execute */ | |
7880 int group; /* group being used */ | |
7881 char_u *fname; /* fname to match with */ | |
7882 char_u *sfname; /* sfname to match with */ | |
7883 char_u *tail; /* tail of fname */ | |
661 | 7884 event_T event; /* current event */ |
40 | 7885 int arg_bufnr; /* initially equal to <abuf>, set to zero when |
7886 buf is deleted */ | |
7887 struct AutoPatCmd *next; /* chain of active apc-s for auto-invalidation*/ | |
7 | 7888 } AutoPatCmd; |
7889 | |
297 | 7890 static AutoPatCmd *active_apc_list = NULL; /* stack of active autocommands */ |
40 | 7891 |
7 | 7892 /* |
7893 * augroups stores a list of autocmd group names. | |
7894 */ | |
297 | 7895 static garray_T augroups = {0, 0, sizeof(char_u *), 10, NULL}; |
7 | 7896 #define AUGROUP_NAME(i) (((char_u **)augroups.ga_data)[i]) |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7897 /* use get_deleted_augroup() to get this */ |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
7898 static char_u *deleted_augroup = NULL; |
7 | 7899 |
7900 /* | |
7901 * The ID of the current group. Group 0 is the default one. | |
7902 */ | |
7903 static int current_augroup = AUGROUP_DEFAULT; | |
7904 | |
7905 static int au_need_clean = FALSE; /* need to delete marked patterns */ | |
7906 | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7907 static void show_autocmd(AutoPat *ap, event_T event); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7908 static void au_remove_pat(AutoPat *ap); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7909 static void au_remove_cmds(AutoPat *ap); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7910 static void au_cleanup(void); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7911 static int au_new_group(char_u *name); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7912 static void au_del_group(char_u *name); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7913 static event_T event_name2nr(char_u *start, char_u **end); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7914 static char_u *event_nr2name(event_T event); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7915 static char_u *find_end_event(char_u *arg, int have_group); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7916 static int event_ignored(event_T event); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7917 static int au_get_grouparg(char_u **argp); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7918 static int do_autocmd_event(event_T event, char_u *pat, int nested, char_u *cmd, int forceit, int group); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7919 static int apply_autocmds_group(event_T event, char_u *fname, char_u *fname_io, int force, int group, buf_T *buf, exarg_T *eap); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7920 static void auto_next_pat(AutoPatCmd *apc, int stop_at_last); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7921 static int match_file_pat(char_u *pattern, regprog_T **prog, char_u *fname, char_u *sfname, char_u *tail, int allow_dirs); |
7 | 7922 |
40 | 7923 |
661 | 7924 static event_T last_event; |
7 | 7925 static int last_group; |
1410 | 7926 static int autocmd_blocked = 0; /* block all autocmds */ |
7 | 7927 |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7928 static char_u * |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7929 get_deleted_augroup(void) |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7930 { |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7931 if (deleted_augroup == NULL) |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7932 deleted_augroup = (char_u *)_("--Deleted--"); |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7933 return deleted_augroup; |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7934 } |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7935 |
7 | 7936 /* |
7937 * Show the autocommands for one AutoPat. | |
7938 */ | |
7939 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7940 show_autocmd(AutoPat *ap, event_T event) |
7 | 7941 { |
7942 AutoCmd *ac; | |
7943 | |
7944 /* Check for "got_int" (here and at various places below), which is set | |
7945 * when "q" has been hit for the "--more--" prompt */ | |
7946 if (got_int) | |
7947 return; | |
7948 if (ap->pat == NULL) /* pattern has been removed */ | |
7949 return; | |
7950 | |
7951 msg_putchar('\n'); | |
7952 if (got_int) | |
7953 return; | |
7954 if (event != last_event || ap->group != last_group) | |
7955 { | |
7956 if (ap->group != AUGROUP_DEFAULT) | |
7957 { | |
7958 if (AUGROUP_NAME(ap->group) == NULL) | |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
7959 msg_puts_attr(get_deleted_augroup(), HL_ATTR(HLF_E)); |
7 | 7960 else |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
7961 msg_puts_attr(AUGROUP_NAME(ap->group), HL_ATTR(HLF_T)); |
7 | 7962 msg_puts((char_u *)" "); |
7963 } | |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
7964 msg_puts_attr(event_nr2name(event), HL_ATTR(HLF_T)); |
7 | 7965 last_event = event; |
7966 last_group = ap->group; | |
7967 msg_putchar('\n'); | |
7968 if (got_int) | |
7969 return; | |
7970 } | |
7971 msg_col = 4; | |
7972 msg_outtrans(ap->pat); | |
7973 | |
7974 for (ac = ap->cmds; ac != NULL; ac = ac->next) | |
7975 { | |
7976 if (ac->cmd != NULL) /* skip removed commands */ | |
7977 { | |
7978 if (msg_col >= 14) | |
7979 msg_putchar('\n'); | |
7980 msg_col = 14; | |
7981 if (got_int) | |
7982 return; | |
7983 msg_outtrans(ac->cmd); | |
500 | 7984 #ifdef FEAT_EVAL |
7985 if (p_verbose > 0) | |
7986 last_set_msg(ac->scriptID); | |
7987 #endif | |
7 | 7988 if (got_int) |
7989 return; | |
7990 if (ac->next != NULL) | |
7991 { | |
7992 msg_putchar('\n'); | |
7993 if (got_int) | |
7994 return; | |
7995 } | |
7996 } | |
7997 } | |
7998 } | |
7999 | |
8000 /* | |
8001 * Mark an autocommand pattern for deletion. | |
8002 */ | |
8003 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8004 au_remove_pat(AutoPat *ap) |
7 | 8005 { |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
8006 VIM_CLEAR(ap->pat); |
40 | 8007 ap->buflocal_nr = -1; |
7 | 8008 au_need_clean = TRUE; |
8009 } | |
8010 | |
8011 /* | |
8012 * Mark all commands for a pattern for deletion. | |
8013 */ | |
8014 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8015 au_remove_cmds(AutoPat *ap) |
7 | 8016 { |
8017 AutoCmd *ac; | |
8018 | |
8019 for (ac = ap->cmds; ac != NULL; ac = ac->next) | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
8020 VIM_CLEAR(ac->cmd); |
7 | 8021 au_need_clean = TRUE; |
8022 } | |
8023 | |
8024 /* | |
8025 * Cleanup autocommands and patterns that have been deleted. | |
8026 * This is only done when not executing autocommands. | |
8027 */ | |
8028 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8029 au_cleanup(void) |
7 | 8030 { |
8031 AutoPat *ap, **prev_ap; | |
8032 AutoCmd *ac, **prev_ac; | |
661 | 8033 event_T event; |
7 | 8034 |
8035 if (autocmd_busy || !au_need_clean) | |
8036 return; | |
8037 | |
8038 /* loop over all events */ | |
661 | 8039 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
8040 event = (event_T)((int)event + 1)) | |
7 | 8041 { |
8042 /* loop over all autocommand patterns */ | |
8043 prev_ap = &(first_autopat[(int)event]); | |
8044 for (ap = *prev_ap; ap != NULL; ap = *prev_ap) | |
8045 { | |
8046 /* loop over all commands for this pattern */ | |
8047 prev_ac = &(ap->cmds); | |
8048 for (ac = *prev_ac; ac != NULL; ac = *prev_ac) | |
8049 { | |
8050 /* remove the command if the pattern is to be deleted or when | |
8051 * the command has been marked for deletion */ | |
8052 if (ap->pat == NULL || ac->cmd == NULL) | |
8053 { | |
8054 *prev_ac = ac->next; | |
8055 vim_free(ac->cmd); | |
8056 vim_free(ac); | |
8057 } | |
8058 else | |
8059 prev_ac = &(ac->next); | |
8060 } | |
8061 | |
8062 /* remove the pattern if it has been marked for deletion */ | |
8063 if (ap->pat == NULL) | |
8064 { | |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8065 if (ap->next == NULL) |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8066 { |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8067 if (prev_ap == &(first_autopat[(int)event])) |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8068 last_autopat[(int)event] = NULL; |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8069 else |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8070 /* this depends on the "next" field being the first in |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8071 * the struct */ |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8072 last_autopat[(int)event] = (AutoPat *)prev_ap; |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8073 } |
7 | 8074 *prev_ap = ap->next; |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4354
diff
changeset
|
8075 vim_regfree(ap->reg_prog); |
7 | 8076 vim_free(ap); |
8077 } | |
8078 else | |
8079 prev_ap = &(ap->next); | |
8080 } | |
8081 } | |
8082 | |
8083 au_need_clean = FALSE; | |
8084 } | |
8085 | |
8086 /* | |
40 | 8087 * Called when buffer is freed, to remove/invalidate related buffer-local |
8088 * autocmds. | |
8089 */ | |
8090 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8091 aubuflocal_remove(buf_T *buf) |
40 | 8092 { |
8093 AutoPat *ap; | |
661 | 8094 event_T event; |
40 | 8095 AutoPatCmd *apc; |
8096 | |
8097 /* invalidate currently executing autocommands */ | |
8098 for (apc = active_apc_list; apc; apc = apc->next) | |
8099 if (buf->b_fnum == apc->arg_bufnr) | |
8100 apc->arg_bufnr = 0; | |
8101 | |
8102 /* invalidate buflocals looping through events */ | |
661 | 8103 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
8104 event = (event_T)((int)event + 1)) | |
40 | 8105 /* loop over all autocommand patterns */ |
8106 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
8107 if (ap->buflocal_nr == buf->b_fnum) | |
8108 { | |
8109 au_remove_pat(ap); | |
8110 if (p_verbose >= 6) | |
292 | 8111 { |
8112 verbose_enter(); | |
40 | 8113 smsg((char_u *) |
8114 _("auto-removing autocommand: %s <buffer=%d>"), | |
8115 event_nr2name(event), buf->b_fnum); | |
292 | 8116 verbose_leave(); |
8117 } | |
40 | 8118 } |
8119 au_cleanup(); | |
8120 } | |
8121 | |
8122 /* | |
7 | 8123 * Add an autocmd group name. |
8124 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. | |
8125 */ | |
8126 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8127 au_new_group(char_u *name) |
7 | 8128 { |
8129 int i; | |
8130 | |
8131 i = au_find_group(name); | |
8132 if (i == AUGROUP_ERROR) /* the group doesn't exist yet, add it */ | |
8133 { | |
8134 /* First try using a free entry. */ | |
8135 for (i = 0; i < augroups.ga_len; ++i) | |
8136 if (AUGROUP_NAME(i) == NULL) | |
8137 break; | |
8138 if (i == augroups.ga_len && ga_grow(&augroups, 1) == FAIL) | |
8139 return AUGROUP_ERROR; | |
8140 | |
8141 AUGROUP_NAME(i) = vim_strsave(name); | |
8142 if (AUGROUP_NAME(i) == NULL) | |
8143 return AUGROUP_ERROR; | |
8144 if (i == augroups.ga_len) | |
8145 ++augroups.ga_len; | |
8146 } | |
8147 | |
8148 return i; | |
8149 } | |
8150 | |
8151 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8152 au_del_group(char_u *name) |
7 | 8153 { |
8154 int i; | |
8155 | |
8156 i = au_find_group(name); | |
8157 if (i == AUGROUP_ERROR) /* the group doesn't exist */ | |
8158 EMSG2(_("E367: No such group: \"%s\""), name); | |
10086
1de911ef1edf
commit https://github.com/vim/vim/commit/de653f08805dde14424d417502a0480a6ad292f8
Christian Brabandt <cb@256bit.org>
parents:
10084
diff
changeset
|
8159 else if (i == current_augroup) |
1de911ef1edf
commit https://github.com/vim/vim/commit/de653f08805dde14424d417502a0480a6ad292f8
Christian Brabandt <cb@256bit.org>
parents:
10084
diff
changeset
|
8160 EMSG(_("E936: Cannot delete the current group")); |
7 | 8161 else |
8162 { | |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8163 event_T event; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8164 AutoPat *ap; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8165 int in_use = FALSE; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8166 |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8167 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8168 event = (event_T)((int)event + 1)) |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8169 { |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8170 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) |
10058
65e43481d7de
commit https://github.com/vim/vim/commit/5c80908ced601be6db7554a147cdb0f98ac8daa1
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
8171 if (ap->group == i && ap->pat != NULL) |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8172 { |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8173 give_warning((char_u *)_("W19: Deleting augroup that is still in use"), TRUE); |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8174 in_use = TRUE; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8175 event = NUM_EVENTS; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8176 break; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8177 } |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8178 } |
7 | 8179 vim_free(AUGROUP_NAME(i)); |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8180 if (in_use) |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8181 { |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
8182 AUGROUP_NAME(i) = get_deleted_augroup(); |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8183 } |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8184 else |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8185 AUGROUP_NAME(i) = NULL; |
7 | 8186 } |
8187 } | |
8188 | |
8189 /* | |
8190 * Find the ID of an autocmd group name. | |
8191 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. | |
8192 */ | |
8193 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8194 au_find_group(char_u *name) |
7 | 8195 { |
8196 int i; | |
8197 | |
8198 for (i = 0; i < augroups.ga_len; ++i) | |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
8199 if (AUGROUP_NAME(i) != NULL && AUGROUP_NAME(i) != get_deleted_augroup() |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8200 && STRCMP(AUGROUP_NAME(i), name) == 0) |
7 | 8201 return i; |
8202 return AUGROUP_ERROR; | |
8203 } | |
8204 | |
461 | 8205 /* |
8206 * Return TRUE if augroup "name" exists. | |
8207 */ | |
8208 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8209 au_has_group(char_u *name) |
461 | 8210 { |
8211 return au_find_group(name) != AUGROUP_ERROR; | |
8212 } | |
8213 | |
7 | 8214 /* |
8215 * ":augroup {name}". | |
8216 */ | |
8217 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8218 do_augroup(char_u *arg, int del_group) |
7 | 8219 { |
8220 int i; | |
8221 | |
8222 if (del_group) | |
8223 { | |
8224 if (*arg == NUL) | |
8225 EMSG(_(e_argreq)); | |
8226 else | |
8227 au_del_group(arg); | |
8228 } | |
8229 else if (STRICMP(arg, "end") == 0) /* ":aug end": back to group 0 */ | |
8230 current_augroup = AUGROUP_DEFAULT; | |
8231 else if (*arg) /* ":aug xxx": switch to group xxx */ | |
8232 { | |
8233 i = au_new_group(arg); | |
8234 if (i != AUGROUP_ERROR) | |
8235 current_augroup = i; | |
8236 } | |
8237 else /* ":aug": list the group names */ | |
8238 { | |
8239 msg_start(); | |
8240 for (i = 0; i < augroups.ga_len; ++i) | |
8241 { | |
8242 if (AUGROUP_NAME(i) != NULL) | |
8243 { | |
8244 msg_puts(AUGROUP_NAME(i)); | |
8245 msg_puts((char_u *)" "); | |
8246 } | |
8247 } | |
8248 msg_clr_eos(); | |
8249 msg_end(); | |
8250 } | |
8251 } | |
8252 | |
355 | 8253 #if defined(EXITFREE) || defined(PROTO) |
8254 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8255 free_all_autocmds(void) |
355 | 8256 { |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8257 int i; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8258 char_u *s; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8259 |
355 | 8260 for (current_augroup = -1; current_augroup < augroups.ga_len; |
8261 ++current_augroup) | |
8262 do_autocmd((char_u *)"", TRUE); | |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8263 |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8264 for (i = 0; i < augroups.ga_len; ++i) |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8265 { |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8266 s = ((char_u **)(augroups.ga_data))[i]; |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
8267 if (s != get_deleted_augroup()) |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8268 vim_free(s); |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8269 } |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8270 ga_clear(&augroups); |
355 | 8271 } |
8272 #endif | |
8273 | |
7 | 8274 /* |
8275 * Return the event number for event name "start". | |
8276 * Return NUM_EVENTS if the event name was not found. | |
8277 * Return a pointer to the next event name in "end". | |
8278 */ | |
661 | 8279 static event_T |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8280 event_name2nr(char_u *start, char_u **end) |
7 | 8281 { |
8282 char_u *p; | |
8283 int i; | |
8284 int len; | |
8285 | |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8286 /* the event name ends with end of line, '|', a blank or a comma */ |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8287 for (p = start; *p && !VIM_ISWHITE(*p) && *p != ',' && *p != '|'; ++p) |
7 | 8288 ; |
8289 for (i = 0; event_names[i].name != NULL; ++i) | |
8290 { | |
8291 len = (int)STRLEN(event_names[i].name); | |
8292 if (len == p - start && STRNICMP(event_names[i].name, start, len) == 0) | |
8293 break; | |
8294 } | |
8295 if (*p == ',') | |
8296 ++p; | |
8297 *end = p; | |
8298 if (event_names[i].name == NULL) | |
8299 return NUM_EVENTS; | |
8300 return event_names[i].event; | |
8301 } | |
8302 | |
8303 /* | |
8304 * Return the name for event "event". | |
8305 */ | |
8306 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8307 event_nr2name(event_T event) |
7 | 8308 { |
8309 int i; | |
8310 | |
8311 for (i = 0; event_names[i].name != NULL; ++i) | |
8312 if (event_names[i].event == event) | |
8313 return (char_u *)event_names[i].name; | |
8314 return (char_u *)"Unknown"; | |
8315 } | |
8316 | |
8317 /* | |
8318 * Scan over the events. "*" stands for all events. | |
8319 */ | |
8320 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8321 find_end_event( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8322 char_u *arg, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8323 int have_group) /* TRUE when group name was found */ |
7 | 8324 { |
8325 char_u *pat; | |
8326 char_u *p; | |
8327 | |
8328 if (*arg == '*') | |
8329 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8330 if (arg[1] && !VIM_ISWHITE(arg[1])) |
7 | 8331 { |
8332 EMSG2(_("E215: Illegal character after *: %s"), arg); | |
8333 return NULL; | |
8334 } | |
8335 pat = arg + 1; | |
8336 } | |
8337 else | |
8338 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8339 for (pat = arg; *pat && *pat != '|' && !VIM_ISWHITE(*pat); pat = p) |
7 | 8340 { |
8341 if ((int)event_name2nr(pat, &p) >= (int)NUM_EVENTS) | |
8342 { | |
8343 if (have_group) | |
8344 EMSG2(_("E216: No such event: %s"), pat); | |
8345 else | |
8346 EMSG2(_("E216: No such group or event: %s"), pat); | |
8347 return NULL; | |
8348 } | |
8349 } | |
8350 } | |
8351 return pat; | |
8352 } | |
8353 | |
8354 /* | |
8355 * Return TRUE if "event" is included in 'eventignore'. | |
8356 */ | |
8357 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8358 event_ignored(event_T event) |
7 | 8359 { |
8360 char_u *p = p_ei; | |
8361 | |
844 | 8362 while (*p != NUL) |
8363 { | |
8364 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) | |
8365 return TRUE; | |
7 | 8366 if (event_name2nr(p, &p) == event) |
8367 return TRUE; | |
844 | 8368 } |
7 | 8369 |
8370 return FALSE; | |
8371 } | |
8372 | |
8373 /* | |
8374 * Return OK when the contents of p_ei is valid, FAIL otherwise. | |
8375 */ | |
8376 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8377 check_ei(void) |
7 | 8378 { |
8379 char_u *p = p_ei; | |
8380 | |
8381 while (*p) | |
844 | 8382 { |
8383 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) | |
8384 { | |
8385 p += 3; | |
8386 if (*p == ',') | |
8387 ++p; | |
8388 } | |
8389 else if (event_name2nr(p, &p) == NUM_EVENTS) | |
7 | 8390 return FAIL; |
844 | 8391 } |
7 | 8392 |
8393 return OK; | |
8394 } | |
8395 | |
123 | 8396 # if defined(FEAT_SYN_HL) || defined(PROTO) |
8397 | |
8398 /* | |
8399 * Add "what" to 'eventignore' to skip loading syntax highlighting for every | |
8400 * buffer loaded into the window. "what" must start with a comma. | |
8401 * Returns the old value of 'eventignore' in allocated memory. | |
8402 */ | |
8403 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8404 au_event_disable(char *what) |
123 | 8405 { |
8406 char_u *new_ei; | |
8407 char_u *save_ei; | |
8408 | |
8409 save_ei = vim_strsave(p_ei); | |
8410 if (save_ei != NULL) | |
8411 { | |
557 | 8412 new_ei = vim_strnsave(p_ei, (int)(STRLEN(p_ei) + STRLEN(what))); |
123 | 8413 if (new_ei != NULL) |
8414 { | |
2095
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8415 if (*what == ',' && *p_ei == NUL) |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8416 STRCPY(new_ei, what + 1); |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8417 else |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8418 STRCAT(new_ei, what); |
694 | 8419 set_string_option_direct((char_u *)"ei", -1, new_ei, |
8420 OPT_FREE, SID_NONE); | |
123 | 8421 vim_free(new_ei); |
8422 } | |
8423 } | |
8424 return save_ei; | |
8425 } | |
8426 | |
8427 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8428 au_event_restore(char_u *old_ei) |
123 | 8429 { |
8430 if (old_ei != NULL) | |
8431 { | |
694 | 8432 set_string_option_direct((char_u *)"ei", -1, old_ei, |
8433 OPT_FREE, SID_NONE); | |
123 | 8434 vim_free(old_ei); |
8435 } | |
8436 } | |
8437 # endif /* FEAT_SYN_HL */ | |
8438 | |
7 | 8439 /* |
8440 * do_autocmd() -- implements the :autocmd command. Can be used in the | |
8441 * following ways: | |
8442 * | |
8443 * :autocmd <event> <pat> <cmd> Add <cmd> to the list of commands that | |
8444 * will be automatically executed for <event> | |
8445 * when editing a file matching <pat>, in | |
8446 * the current group. | |
14157
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
8447 * :autocmd <event> <pat> Show the autocommands associated with |
7 | 8448 * <event> and <pat>. |
14157
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
8449 * :autocmd <event> Show the autocommands associated with |
7 | 8450 * <event>. |
14157
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
8451 * :autocmd Show all autocommands. |
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
8452 * :autocmd! <event> <pat> <cmd> Remove all autocommands associated with |
7 | 8453 * <event> and <pat>, and add the command |
8454 * <cmd>, for the current group. | |
14157
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
8455 * :autocmd! <event> <pat> Remove all autocommands associated with |
7 | 8456 * <event> and <pat> for the current group. |
14157
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
8457 * :autocmd! <event> Remove all autocommands associated with |
7 | 8458 * <event> for the current group. |
14157
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
8459 * :autocmd! Remove ALL autocommands for the current |
7 | 8460 * group. |
8461 * | |
8462 * Multiple events and patterns may be given separated by commas. Here are | |
8463 * some examples: | |
8464 * :autocmd bufread,bufenter *.c,*.h set tw=0 smartindent noic | |
8465 * :autocmd bufleave * set tw=79 nosmartindent ic infercase | |
8466 * | |
8467 * :autocmd * *.c show all autocommands for *.c files. | |
609 | 8468 * |
8469 * Mostly a {group} argument can optionally appear before <event>. | |
7 | 8470 */ |
8471 void | |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8472 do_autocmd(char_u *arg_in, int forceit) |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8473 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8474 char_u *arg = arg_in; |
7 | 8475 char_u *pat; |
8476 char_u *envpat = NULL; | |
8477 char_u *cmd; | |
661 | 8478 event_T event; |
7 | 8479 int need_free = FALSE; |
8480 int nested = FALSE; | |
8481 int group; | |
8482 | |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8483 if (*arg == '|') |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8484 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8485 arg = (char_u *)""; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8486 group = AUGROUP_ALL; /* no argument, use all groups */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8487 } |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8488 else |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8489 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8490 /* |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8491 * Check for a legal group name. If not, use AUGROUP_ALL. |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8492 */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8493 group = au_get_grouparg(&arg); |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8494 if (arg == NULL) /* out of memory */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8495 return; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8496 } |
7 | 8497 |
8498 /* | |
8499 * Scan over the events. | |
8500 * If we find an illegal name, return here, don't do anything. | |
8501 */ | |
8502 pat = find_end_event(arg, group != AUGROUP_ALL); | |
8503 if (pat == NULL) | |
8504 return; | |
8505 | |
8506 pat = skipwhite(pat); | |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8507 if (*pat == '|') |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8508 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8509 pat = (char_u *)""; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8510 cmd = (char_u *)""; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8511 } |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8512 else |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8513 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8514 /* |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8515 * Scan over the pattern. Put a NUL at the end. |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8516 */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8517 cmd = pat; |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8518 while (*cmd && (!VIM_ISWHITE(*cmd) || cmd[-1] == '\\')) |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8519 cmd++; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8520 if (*cmd) |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8521 *cmd++ = NUL; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8522 |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8523 /* Expand environment variables in the pattern. Set 'shellslash', we want |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8524 * forward slashes here. */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8525 if (vim_strchr(pat, '$') != NULL || vim_strchr(pat, '~') != NULL) |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8526 { |
7 | 8527 #ifdef BACKSLASH_IN_FILENAME |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8528 int p_ssl_save = p_ssl; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8529 |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8530 p_ssl = TRUE; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8531 #endif |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8532 envpat = expand_env_save(pat); |
7 | 8533 #ifdef BACKSLASH_IN_FILENAME |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8534 p_ssl = p_ssl_save; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8535 #endif |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8536 if (envpat != NULL) |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8537 pat = envpat; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8538 } |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8539 |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8540 /* |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8541 * Check for "nested" flag. |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8542 */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8543 cmd = skipwhite(cmd); |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8544 if (*cmd != NUL && STRNCMP(cmd, "nested", 6) == 0 && VIM_ISWHITE(cmd[6])) |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8545 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8546 nested = TRUE; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8547 cmd = skipwhite(cmd + 6); |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8548 } |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8549 |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8550 /* |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8551 * Find the start of the commands. |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8552 * Expand <sfile> in it. |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8553 */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8554 if (*cmd != NUL) |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8555 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8556 cmd = expand_sfile(cmd); |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8557 if (cmd == NULL) /* some error */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8558 return; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8559 need_free = TRUE; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8560 } |
7 | 8561 } |
8562 | |
8563 /* | |
8564 * Print header when showing autocommands. | |
8565 */ | |
8566 if (!forceit && *cmd == NUL) | |
8567 { | |
8568 /* Highlight title */ | |
14157
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
8569 MSG_PUTS_TITLE(_("\n--- Autocommands ---")); |
7 | 8570 } |
8571 | |
8572 /* | |
8573 * Loop over the events. | |
8574 */ | |
661 | 8575 last_event = (event_T)-1; /* for listing the event name */ |
7 | 8576 last_group = AUGROUP_ERROR; /* for listing the group name */ |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8577 if (*arg == '*' || *arg == NUL || *arg == '|') |
7 | 8578 { |
661 | 8579 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
8580 event = (event_T)((int)event + 1)) | |
7 | 8581 if (do_autocmd_event(event, pat, |
8582 nested, cmd, forceit, group) == FAIL) | |
8583 break; | |
8584 } | |
8585 else | |
8586 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8587 while (*arg && *arg != '|' && !VIM_ISWHITE(*arg)) |
7 | 8588 if (do_autocmd_event(event_name2nr(arg, &arg), pat, |
8589 nested, cmd, forceit, group) == FAIL) | |
8590 break; | |
8591 } | |
8592 | |
8593 if (need_free) | |
8594 vim_free(cmd); | |
8595 vim_free(envpat); | |
8596 } | |
8597 | |
8598 /* | |
8599 * Find the group ID in a ":autocmd" or ":doautocmd" argument. | |
8600 * The "argp" argument is advanced to the following argument. | |
8601 * | |
8602 * Returns the group ID, AUGROUP_ERROR for error (out of memory). | |
8603 */ | |
8604 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8605 au_get_grouparg(char_u **argp) |
7 | 8606 { |
8607 char_u *group_name; | |
8608 char_u *p; | |
8609 char_u *arg = *argp; | |
8610 int group = AUGROUP_ALL; | |
8611 | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8612 for (p = arg; *p && !VIM_ISWHITE(*p) && *p != '|'; ++p) |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8613 ; |
7 | 8614 if (p > arg) |
8615 { | |
8616 group_name = vim_strnsave(arg, (int)(p - arg)); | |
8617 if (group_name == NULL) /* out of memory */ | |
8618 return AUGROUP_ERROR; | |
8619 group = au_find_group(group_name); | |
8620 if (group == AUGROUP_ERROR) | |
8621 group = AUGROUP_ALL; /* no match, use all groups */ | |
8622 else | |
8623 *argp = skipwhite(p); /* match, skip over group name */ | |
8624 vim_free(group_name); | |
8625 } | |
8626 return group; | |
8627 } | |
8628 | |
8629 /* | |
8630 * do_autocmd() for one event. | |
8631 * If *pat == NUL do for all patterns. | |
8632 * If *cmd == NUL show entries. | |
8633 * If forceit == TRUE delete entries. | |
8634 * If group is not AUGROUP_ALL, only use this group. | |
8635 */ | |
8636 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8637 do_autocmd_event( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8638 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8639 char_u *pat, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8640 int nested, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8641 char_u *cmd, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8642 int forceit, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8643 int group) |
7 | 8644 { |
8645 AutoPat *ap; | |
8646 AutoPat **prev_ap; | |
8647 AutoCmd *ac; | |
8648 AutoCmd **prev_ac; | |
8649 int brace_level; | |
8650 char_u *endpat; | |
8651 int findgroup; | |
8652 int allgroups; | |
8653 int patlen; | |
856 | 8654 int is_buflocal; |
40 | 8655 int buflocal_nr; |
8656 char_u buflocal_pat[25]; /* for "<buffer=X>" */ | |
7 | 8657 |
8658 if (group == AUGROUP_ALL) | |
8659 findgroup = current_augroup; | |
8660 else | |
8661 findgroup = group; | |
8662 allgroups = (group == AUGROUP_ALL && !forceit && *cmd == NUL); | |
8663 | |
8664 /* | |
8665 * Show or delete all patterns for an event. | |
8666 */ | |
8667 if (*pat == NUL) | |
8668 { | |
8669 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
8670 { | |
8671 if (forceit) /* delete the AutoPat, if it's in the current group */ | |
8672 { | |
8673 if (ap->group == findgroup) | |
8674 au_remove_pat(ap); | |
8675 } | |
8676 else if (group == AUGROUP_ALL || ap->group == group) | |
8677 show_autocmd(ap, event); | |
8678 } | |
8679 } | |
8680 | |
8681 /* | |
8682 * Loop through all the specified patterns. | |
8683 */ | |
8684 for ( ; *pat; pat = (*endpat == ',' ? endpat + 1 : endpat)) | |
8685 { | |
8686 /* | |
8687 * Find end of the pattern. | |
8688 * Watch out for a comma in braces, like "*.\{obj,o\}". | |
8689 */ | |
8690 brace_level = 0; | |
8691 for (endpat = pat; *endpat && (*endpat != ',' || brace_level | |
6963 | 8692 || (endpat > pat && endpat[-1] == '\\')); ++endpat) |
7 | 8693 { |
8694 if (*endpat == '{') | |
8695 brace_level++; | |
8696 else if (*endpat == '}') | |
8697 brace_level--; | |
8698 } | |
8699 if (pat == endpat) /* ignore single comma */ | |
8700 continue; | |
8701 patlen = (int)(endpat - pat); | |
8702 | |
8703 /* | |
40 | 8704 * detect special <buflocal[=X]> buffer-local patterns |
8705 */ | |
8706 is_buflocal = FALSE; | |
8707 buflocal_nr = 0; | |
8708 | |
6623 | 8709 if (patlen >= 8 && STRNCMP(pat, "<buffer", 7) == 0 |
40 | 8710 && pat[patlen - 1] == '>') |
8711 { | |
6623 | 8712 /* "<buffer...>": Error will be printed only for addition. |
8713 * printing and removing will proceed silently. */ | |
40 | 8714 is_buflocal = TRUE; |
8715 if (patlen == 8) | |
6623 | 8716 /* "<buffer>" */ |
40 | 8717 buflocal_nr = curbuf->b_fnum; |
8718 else if (patlen > 9 && pat[7] == '=') | |
8719 { | |
6623 | 8720 if (patlen == 13 && STRNICMP(pat, "<buffer=abuf>", 13) == 0) |
8721 /* "<buffer=abuf>" */ | |
40 | 8722 buflocal_nr = autocmd_bufnr; |
8723 else if (skipdigits(pat + 8) == pat + patlen - 1) | |
6623 | 8724 /* "<buffer=123>" */ |
40 | 8725 buflocal_nr = atoi((char *)pat + 8); |
8726 } | |
8727 } | |
8728 | |
8729 if (is_buflocal) | |
8730 { | |
8731 /* normalize pat into standard "<buffer>#N" form */ | |
8732 sprintf((char *)buflocal_pat, "<buffer=%d>", buflocal_nr); | |
8733 pat = buflocal_pat; /* can modify pat and patlen */ | |
835 | 8734 patlen = (int)STRLEN(buflocal_pat); /* but not endpat */ |
40 | 8735 } |
8736 | |
8737 /* | |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8738 * Find AutoPat entries with this pattern. When adding a command it |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8739 * always goes at or after the last one, so start at the end. |
7 | 8740 */ |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8741 if (!forceit && *cmd != NUL && last_autopat[(int)event] != NULL) |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8742 prev_ap = &last_autopat[(int)event]; |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8743 else |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8744 prev_ap = &first_autopat[(int)event]; |
7 | 8745 while ((ap = *prev_ap) != NULL) |
8746 { | |
8747 if (ap->pat != NULL) | |
8748 { | |
8749 /* Accept a pattern when: | |
8750 * - a group was specified and it's that group, or a group was | |
8751 * not specified and it's the current group, or a group was | |
8752 * not specified and we are listing | |
8753 * - the length of the pattern matches | |
40 | 8754 * - the pattern matches. |
8755 * For <buffer[=X]>, this condition works because we normalize | |
8756 * all buffer-local patterns. | |
7 | 8757 */ |
8758 if ((allgroups || ap->group == findgroup) | |
8759 && ap->patlen == patlen | |
8760 && STRNCMP(pat, ap->pat, patlen) == 0) | |
8761 { | |
8762 /* | |
8763 * Remove existing autocommands. | |
8764 * If adding any new autocmd's for this AutoPat, don't | |
8765 * delete the pattern from the autopat list, append to | |
8766 * this list. | |
8767 */ | |
8768 if (forceit) | |
8769 { | |
8770 if (*cmd != NUL && ap->next == NULL) | |
8771 { | |
8772 au_remove_cmds(ap); | |
8773 break; | |
8774 } | |
8775 au_remove_pat(ap); | |
8776 } | |
8777 | |
8778 /* | |
40 | 8779 * Show autocmd's for this autopat, or buflocals <buffer=X> |
7 | 8780 */ |
8781 else if (*cmd == NUL) | |
8782 show_autocmd(ap, event); | |
8783 | |
8784 /* | |
8785 * Add autocmd to this autopat, if it's the last one. | |
8786 */ | |
8787 else if (ap->next == NULL) | |
8788 break; | |
8789 } | |
8790 } | |
8791 prev_ap = &ap->next; | |
8792 } | |
8793 | |
8794 /* | |
8795 * Add a new command. | |
8796 */ | |
8797 if (*cmd != NUL) | |
8798 { | |
8799 /* | |
8800 * If the pattern we want to add a command to does appear at the | |
8801 * end of the list (or not is not in the list at all), add the | |
8802 * pattern at the end of the list. | |
8803 */ | |
8804 if (ap == NULL) | |
8805 { | |
40 | 8806 /* refuse to add buffer-local ap if buffer number is invalid */ |
8807 if (is_buflocal && (buflocal_nr == 0 | |
8808 || buflist_findnr(buflocal_nr) == NULL)) | |
8809 { | |
8810 EMSGN(_("E680: <buffer=%d>: invalid buffer number "), | |
8811 buflocal_nr); | |
8812 return FAIL; | |
8813 } | |
8814 | |
7 | 8815 ap = (AutoPat *)alloc((unsigned)sizeof(AutoPat)); |
8816 if (ap == NULL) | |
8817 return FAIL; | |
8818 ap->pat = vim_strnsave(pat, patlen); | |
8819 ap->patlen = patlen; | |
8820 if (ap->pat == NULL) | |
8821 { | |
8822 vim_free(ap); | |
8823 return FAIL; | |
8824 } | |
40 | 8825 |
8826 if (is_buflocal) | |
8827 { | |
8828 ap->buflocal_nr = buflocal_nr; | |
153 | 8829 ap->reg_prog = NULL; |
40 | 8830 } |
8831 else | |
7 | 8832 { |
153 | 8833 char_u *reg_pat; |
8834 | |
40 | 8835 ap->buflocal_nr = 0; |
153 | 8836 reg_pat = file_pat_to_reg_pat(pat, endpat, |
40 | 8837 &ap->allow_dirs, TRUE); |
153 | 8838 if (reg_pat != NULL) |
8839 ap->reg_prog = vim_regcomp(reg_pat, RE_MAGIC); | |
355 | 8840 vim_free(reg_pat); |
153 | 8841 if (reg_pat == NULL || ap->reg_prog == NULL) |
40 | 8842 { |
8843 vim_free(ap->pat); | |
8844 vim_free(ap); | |
8845 return FAIL; | |
8846 } | |
7 | 8847 } |
8848 ap->cmds = NULL; | |
8849 *prev_ap = ap; | |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8850 last_autopat[(int)event] = ap; |
7 | 8851 ap->next = NULL; |
8852 if (group == AUGROUP_ALL) | |
8853 ap->group = current_augroup; | |
8854 else | |
8855 ap->group = group; | |
8856 } | |
8857 | |
8858 /* | |
8859 * Add the autocmd at the end of the AutoCmd list. | |
8860 */ | |
8861 prev_ac = &(ap->cmds); | |
8862 while ((ac = *prev_ac) != NULL) | |
8863 prev_ac = &ac->next; | |
8864 ac = (AutoCmd *)alloc((unsigned)sizeof(AutoCmd)); | |
8865 if (ac == NULL) | |
8866 return FAIL; | |
8867 ac->cmd = vim_strsave(cmd); | |
8868 #ifdef FEAT_EVAL | |
8869 ac->scriptID = current_SID; | |
8870 #endif | |
8871 if (ac->cmd == NULL) | |
8872 { | |
8873 vim_free(ac); | |
8874 return FAIL; | |
8875 } | |
8876 ac->next = NULL; | |
8877 *prev_ac = ac; | |
8878 ac->nested = nested; | |
8879 } | |
8880 } | |
8881 | |
8882 au_cleanup(); /* may really delete removed patterns/commands now */ | |
8883 return OK; | |
8884 } | |
8885 | |
8886 /* | |
8887 * Implementation of ":doautocmd [group] event [fname]". | |
8888 * Return OK for success, FAIL for failure; | |
8889 */ | |
8890 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8891 do_doautocmd( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8892 char_u *arg, |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8893 int do_msg, /* give message for no matching autocmds? */ |
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8894 int *did_something) |
7 | 8895 { |
8896 char_u *fname; | |
8897 int nothing_done = TRUE; | |
8898 int group; | |
8899 | |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8900 if (did_something != NULL) |
9291
bff3cd5cf922
commit https://github.com/vim/vim/commit/76ae22fef3cb224ca7fbf97517f881e825d4d0c2
Christian Brabandt <cb@256bit.org>
parents:
9260
diff
changeset
|
8901 *did_something = FALSE; |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8902 |
7 | 8903 /* |
8904 * Check for a legal group name. If not, use AUGROUP_ALL. | |
8905 */ | |
8906 group = au_get_grouparg(&arg); | |
8907 if (arg == NULL) /* out of memory */ | |
8908 return FAIL; | |
8909 | |
8910 if (*arg == '*') | |
8911 { | |
8912 EMSG(_("E217: Can't execute autocommands for ALL events")); | |
8913 return FAIL; | |
8914 } | |
8915 | |
8916 /* | |
8917 * Scan over the events. | |
8918 * If we find an illegal name, return here, don't do anything. | |
8919 */ | |
8920 fname = find_end_event(arg, group != AUGROUP_ALL); | |
8921 if (fname == NULL) | |
8922 return FAIL; | |
8923 | |
8924 fname = skipwhite(fname); | |
8925 | |
8926 /* | |
8927 * Loop over the events. | |
8928 */ | |
11641
dbce7e03bf55
patch 8.0.0703: illegal memory access with empty :doau command
Christian Brabandt <cb@256bit.org>
parents:
11605
diff
changeset
|
8929 while (*arg && !ends_excmd(*arg) && !VIM_ISWHITE(*arg)) |
7 | 8930 if (apply_autocmds_group(event_name2nr(arg, &arg), |
8931 fname, NULL, TRUE, group, curbuf, NULL)) | |
8932 nothing_done = FALSE; | |
8933 | |
8934 if (nothing_done && do_msg) | |
8935 MSG(_("No matching autocommands")); | |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8936 if (did_something != NULL) |
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8937 *did_something = !nothing_done; |
7 | 8938 |
8939 #ifdef FEAT_EVAL | |
8940 return aborting() ? FAIL : OK; | |
8941 #else | |
8942 return OK; | |
8943 #endif | |
8944 } | |
8945 | |
8946 /* | |
8947 * ":doautoall": execute autocommands for each loaded buffer. | |
8948 */ | |
8949 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8950 ex_doautoall(exarg_T *eap) |
7 | 8951 { |
8952 int retval; | |
8953 aco_save_T aco; | |
8954 buf_T *buf; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
8955 bufref_T bufref; |
3342 | 8956 char_u *arg = eap->arg; |
3350 | 8957 int call_do_modelines = check_nomodeline(&arg); |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8958 int did_aucmd; |
7 | 8959 |
8960 /* | |
8961 * This is a bit tricky: For some commands curwin->w_buffer needs to be | |
8962 * equal to curbuf, but for some buffers there may not be a window. | |
8963 * So we change the buffer for the current window for a moment. This | |
8964 * gives problems when the autocommands make changes to the list of | |
8965 * buffers or windows... | |
8966 */ | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
8967 FOR_ALL_BUFFERS(buf) |
7 | 8968 { |
1665 | 8969 if (buf->b_ml.ml_mfp != NULL) |
7 | 8970 { |
8971 /* find a window for this buffer and save some values */ | |
8972 aucmd_prepbuf(&aco, buf); | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
8973 set_bufref(&bufref, buf); |
7 | 8974 |
8975 /* execute the autocommands for this buffer */ | |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8976 retval = do_doautocmd(arg, FALSE, &did_aucmd); |
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8977 |
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8978 if (call_do_modelines && did_aucmd) |
3342 | 8979 { |
8980 /* Execute the modeline settings, but don't set window-local | |
8981 * options if we are using the current window for another | |
8982 * buffer. */ | |
8983 do_modelines(curwin == aucmd_win ? OPT_NOWIN : 0); | |
8984 } | |
7 | 8985 |
8986 /* restore the current window */ | |
8987 aucmd_restbuf(&aco); | |
8988 | |
8989 /* stop if there is some error or buffer was deleted */ | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
8990 if (retval == FAIL || !bufref_valid(&bufref)) |
7 | 8991 break; |
8992 } | |
8993 } | |
8994 | |
8995 check_cursor(); /* just in case lines got deleted */ | |
8996 } | |
8997 | |
8998 /* | |
3350 | 8999 * Check *argp for <nomodeline>. When it is present return FALSE, otherwise |
9000 * return TRUE and advance *argp to after it. | |
9001 * Thus return TRUE when do_modelines() should be called. | |
9002 */ | |
9003 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9004 check_nomodeline(char_u **argp) |
3350 | 9005 { |
9006 if (STRNCMP(*argp, "<nomodeline>", 12) == 0) | |
9007 { | |
9008 *argp = skipwhite(*argp + 12); | |
9009 return FALSE; | |
9010 } | |
9011 return TRUE; | |
9012 } | |
9013 | |
9014 /* | |
7 | 9015 * Prepare for executing autocommands for (hidden) buffer "buf". |
1906 | 9016 * Search for a visible window containing the current buffer. If there isn't |
9017 * one then use "aucmd_win". | |
7 | 9018 * Set "curbuf" and "curwin" to match "buf". |
9019 */ | |
9020 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9021 aucmd_prepbuf( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9022 aco_save_T *aco, /* structure to save values in */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9023 buf_T *buf) /* new curbuf */ |
7 | 9024 { |
9025 win_T *win; | |
1906 | 9026 int save_ea; |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
9027 #ifdef FEAT_AUTOCHDIR |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9028 int save_acd; |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
9029 #endif |
7 | 9030 |
9031 /* Find a window that is for the new buffer */ | |
9032 if (buf == curbuf) /* be quick when buf is curbuf */ | |
9033 win = curwin; | |
9034 else | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
9035 FOR_ALL_WINDOWS(win) |
7 | 9036 if (win->w_buffer == buf) |
9037 break; | |
9038 | |
1906 | 9039 /* Allocate "aucmd_win" when needed. If this fails (out of memory) fall |
9040 * back to using the current window. */ | |
9041 if (win == NULL && aucmd_win == NULL) | |
9042 { | |
9043 win_alloc_aucmd_win(); | |
9044 if (aucmd_win == NULL) | |
9045 win = curwin; | |
9046 } | |
1944 | 9047 if (win == NULL && aucmd_win_used) |
9048 /* Strange recursive autocommand, fall back to using the current | |
9049 * window. Expect a few side effects... */ | |
9050 win = curwin; | |
1906 | 9051 |
9052 aco->save_curwin = curwin; | |
9053 aco->save_curbuf = curbuf; | |
7 | 9054 if (win != NULL) |
9055 { | |
1906 | 9056 /* There is a window for "buf" in the current tab page, make it the |
9057 * curwin. This is preferred, it has the least side effects (esp. if | |
9058 * "buf" is curbuf). */ | |
1944 | 9059 aco->use_aucmd_win = FALSE; |
7 | 9060 curwin = win; |
9061 } | |
9062 else | |
9063 { | |
1906 | 9064 /* There is no window for "buf", use "aucmd_win". To minimize the side |
4352 | 9065 * effects, insert it in the current tab page. |
1906 | 9066 * Anything related to a window (e.g., setting folds) may have |
9067 * unexpected results. */ | |
1944 | 9068 aco->use_aucmd_win = TRUE; |
9069 aucmd_win_used = TRUE; | |
1918 | 9070 aucmd_win->w_buffer = buf; |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
9071 #if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) |
2258
772bfca06c18
Fix crash when using ":grep".
Bram Moolenaar <bram@vim.org>
parents:
2244
diff
changeset
|
9072 aucmd_win->w_s = &buf->b_s; |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
9073 #endif |
7 | 9074 ++buf->b_nwindows; |
1918 | 9075 win_init_empty(aucmd_win); /* set cursor and topline to safe values */ |
1944 | 9076 |
9077 /* Make sure w_localdir and globaldir are NULL to avoid a chdir() in | |
9078 * win_enter_ext(). */ | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
9079 VIM_CLEAR(aucmd_win->w_localdir); |
1944 | 9080 aco->globaldir = globaldir; |
9081 globaldir = NULL; | |
9082 | |
7 | 9083 |
1923 | 9084 /* Split the current window, put the aucmd_win in the upper half. |
9085 * We don't want the BufEnter or WinEnter autocommands. */ | |
9086 block_autocmds(); | |
1906 | 9087 make_snapshot(SNAP_AUCMD_IDX); |
9088 save_ea = p_ea; | |
9089 p_ea = FALSE; | |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9090 |
12477
68d7bc045dbe
patch 8.0.1118: FEAT_WINDOWS adds a lot of #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
12216
diff
changeset
|
9091 #ifdef FEAT_AUTOCHDIR |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9092 /* Prevent chdir() call in win_enter_ext(), through do_autochdir(). */ |
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9093 save_acd = p_acd; |
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9094 p_acd = FALSE; |
12477
68d7bc045dbe
patch 8.0.1118: FEAT_WINDOWS adds a lot of #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
12216
diff
changeset
|
9095 #endif |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9096 |
1906 | 9097 (void)win_split_ins(0, WSP_TOP, aucmd_win, 0); |
9098 (void)win_comp_pos(); /* recompute window positions */ | |
9099 p_ea = save_ea; | |
12477
68d7bc045dbe
patch 8.0.1118: FEAT_WINDOWS adds a lot of #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
12216
diff
changeset
|
9100 #ifdef FEAT_AUTOCHDIR |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9101 p_acd = save_acd; |
12477
68d7bc045dbe
patch 8.0.1118: FEAT_WINDOWS adds a lot of #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
12216
diff
changeset
|
9102 #endif |
1923 | 9103 unblock_autocmds(); |
1918 | 9104 curwin = aucmd_win; |
1906 | 9105 } |
7 | 9106 curbuf = buf; |
1906 | 9107 aco->new_curwin = curwin; |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9108 set_bufref(&aco->new_curbuf, curbuf); |
7 | 9109 } |
9110 | |
9111 /* | |
9112 * Cleanup after executing autocommands for a (hidden) buffer. | |
9113 * Restore the window as it was (if possible). | |
9114 */ | |
9115 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9116 aucmd_restbuf( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9117 aco_save_T *aco) /* structure holding saved values */ |
7 | 9118 { |
1906 | 9119 int dummy; |
9120 | |
1944 | 9121 if (aco->use_aucmd_win) |
1906 | 9122 { |
9123 --curbuf->b_nwindows; | |
9124 /* Find "aucmd_win", it can't be closed, but it may be in another tab | |
1923 | 9125 * page. Do not trigger autocommands here. */ |
9126 block_autocmds(); | |
1906 | 9127 if (curwin != aucmd_win) |
9128 { | |
9129 tabpage_T *tp; | |
9130 win_T *wp; | |
9131 | |
9132 FOR_ALL_TAB_WINDOWS(tp, wp) | |
9133 { | |
9134 if (wp == aucmd_win) | |
9135 { | |
9136 if (tp != curtab) | |
4354 | 9137 goto_tabpage_tp(tp, TRUE, TRUE); |
1906 | 9138 win_goto(aucmd_win); |
3340 | 9139 goto win_found; |
1906 | 9140 } |
9141 } | |
9142 } | |
3340 | 9143 win_found: |
1906 | 9144 |
9145 /* Remove the window and frame from the tree of frames. */ | |
9146 (void)winframe_remove(curwin, &dummy, NULL); | |
9147 win_remove(curwin, NULL); | |
1944 | 9148 aucmd_win_used = FALSE; |
1906 | 9149 last_status(FALSE); /* may need to remove last status line */ |
11199
e08ead1d269f
patch 8.0.0486: crash and endless loop when closing windows in autocmd
Christian Brabandt <cb@256bit.org>
parents:
11158
diff
changeset
|
9150 |
e08ead1d269f
patch 8.0.0486: crash and endless loop when closing windows in autocmd
Christian Brabandt <cb@256bit.org>
parents:
11158
diff
changeset
|
9151 if (!valid_tabpage_win(curtab)) |
e08ead1d269f
patch 8.0.0486: crash and endless loop when closing windows in autocmd
Christian Brabandt <cb@256bit.org>
parents:
11158
diff
changeset
|
9152 /* no valid window in current tabpage */ |
e08ead1d269f
patch 8.0.0486: crash and endless loop when closing windows in autocmd
Christian Brabandt <cb@256bit.org>
parents:
11158
diff
changeset
|
9153 close_tabpage(curtab); |
e08ead1d269f
patch 8.0.0486: crash and endless loop when closing windows in autocmd
Christian Brabandt <cb@256bit.org>
parents:
11158
diff
changeset
|
9154 |
1906 | 9155 restore_snapshot(SNAP_AUCMD_IDX, FALSE); |
9156 (void)win_comp_pos(); /* recompute window positions */ | |
1923 | 9157 unblock_autocmds(); |
1906 | 9158 |
9159 if (win_valid(aco->save_curwin)) | |
9160 curwin = aco->save_curwin; | |
9161 else | |
9162 /* Hmm, original window disappeared. Just use the first one. */ | |
9163 curwin = firstwin; | |
12477
68d7bc045dbe
patch 8.0.1118: FEAT_WINDOWS adds a lot of #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
12216
diff
changeset
|
9164 #ifdef FEAT_EVAL |
4287 | 9165 vars_clear(&aucmd_win->w_vars->dv_hashtab); /* free all w: variables */ |
9166 hash_init(&aucmd_win->w_vars->dv_hashtab); /* re-use the hashtab */ | |
1906 | 9167 #endif |
9168 curbuf = curwin->w_buffer; | |
9169 | |
1944 | 9170 vim_free(globaldir); |
9171 globaldir = aco->globaldir; | |
9172 | |
1906 | 9173 /* the buffer contents may have changed */ |
9174 check_cursor(); | |
9175 if (curwin->w_topline > curbuf->b_ml.ml_line_count) | |
9176 { | |
9177 curwin->w_topline = curbuf->b_ml.ml_line_count; | |
9178 #ifdef FEAT_DIFF | |
9179 curwin->w_topfill = 0; | |
9180 #endif | |
9181 } | |
9182 #if defined(FEAT_GUI) | |
9183 /* Hide the scrollbars from the aucmd_win and update. */ | |
9184 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_LEFT], FALSE); | |
9185 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_RIGHT], FALSE); | |
9186 gui_may_update_scrollbars(); | |
9187 #endif | |
9188 } | |
9189 else | |
7 | 9190 { |
9191 /* restore curwin */ | |
9192 if (win_valid(aco->save_curwin)) | |
9193 { | |
1906 | 9194 /* Restore the buffer which was previously edited by curwin, if |
1944 | 9195 * it was changed, we are still the same window and the buffer is |
1906 | 9196 * valid. */ |
7 | 9197 if (curwin == aco->new_curwin |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9198 && curbuf != aco->new_curbuf.br_buf |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9199 && bufref_valid(&aco->new_curbuf) |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9200 && aco->new_curbuf.br_buf->b_ml.ml_mfp != NULL) |
7 | 9201 { |
3497 | 9202 # if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) |
9203 if (curwin->w_s == &curbuf->b_s) | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9204 curwin->w_s = &aco->new_curbuf.br_buf->b_s; |
3497 | 9205 # endif |
7 | 9206 --curbuf->b_nwindows; |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9207 curbuf = aco->new_curbuf.br_buf; |
7 | 9208 curwin->w_buffer = curbuf; |
9209 ++curbuf->b_nwindows; | |
9210 } | |
9211 | |
9212 curwin = aco->save_curwin; | |
9213 curbuf = curwin->w_buffer; | |
6201 | 9214 /* In case the autocommand move the cursor to a position that that |
9215 * not exist in curbuf. */ | |
9216 check_cursor(); | |
7 | 9217 } |
9218 } | |
9219 } | |
9220 | |
9221 static int autocmd_nested = FALSE; | |
9222 | |
9223 /* | |
9224 * Execute autocommands for "event" and file name "fname". | |
9225 * Return TRUE if some commands were executed. | |
9226 */ | |
9227 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9228 apply_autocmds( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9229 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9230 char_u *fname, /* NULL or empty means use actual file name */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9231 char_u *fname_io, /* fname to use for <afile> on cmdline */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9232 int force, /* when TRUE, ignore autocmd_busy */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9233 buf_T *buf) /* buffer for <abuf> */ |
7 | 9234 { |
9235 return apply_autocmds_group(event, fname, fname_io, force, | |
9236 AUGROUP_ALL, buf, NULL); | |
9237 } | |
9238 | |
9239 /* | |
9240 * Like apply_autocmds(), but with extra "eap" argument. This takes care of | |
9241 * setting v:filearg. | |
9242 */ | |
9243 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9244 apply_autocmds_exarg( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9245 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9246 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9247 char_u *fname_io, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9248 int force, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9249 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9250 exarg_T *eap) |
7 | 9251 { |
9252 return apply_autocmds_group(event, fname, fname_io, force, | |
9253 AUGROUP_ALL, buf, eap); | |
9254 } | |
9255 | |
9256 /* | |
9257 * Like apply_autocmds(), but handles the caller's retval. If the script | |
9258 * processing is being aborted or if retval is FAIL when inside a try | |
9259 * conditional, no autocommands are executed. If otherwise the autocommands | |
9260 * cause the script to be aborted, retval is set to FAIL. | |
9261 */ | |
9262 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9263 apply_autocmds_retval( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9264 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9265 char_u *fname, /* NULL or empty means use actual file name */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9266 char_u *fname_io, /* fname to use for <afile> on cmdline */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9267 int force, /* when TRUE, ignore autocmd_busy */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9268 buf_T *buf, /* buffer for <abuf> */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9269 int *retval) /* pointer to caller's retval */ |
7 | 9270 { |
9271 int did_cmd; | |
9272 | |
532 | 9273 #ifdef FEAT_EVAL |
7 | 9274 if (should_abort(*retval)) |
9275 return FALSE; | |
532 | 9276 #endif |
7 | 9277 |
9278 did_cmd = apply_autocmds_group(event, fname, fname_io, force, | |
9279 AUGROUP_ALL, buf, NULL); | |
532 | 9280 if (did_cmd |
9281 #ifdef FEAT_EVAL | |
9282 && aborting() | |
9283 #endif | |
9284 ) | |
7 | 9285 *retval = FAIL; |
9286 return did_cmd; | |
9287 } | |
9288 | |
609 | 9289 /* |
9290 * Return TRUE when there is a CursorHold autocommand defined. | |
9291 */ | |
7 | 9292 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9293 has_cursorhold(void) |
7 | 9294 { |
661 | 9295 return (first_autopat[(int)(get_real_state() == NORMAL_BUSY |
9296 ? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)] != NULL); | |
7 | 9297 } |
609 | 9298 |
9299 /* | |
9300 * Return TRUE if the CursorHold event can be triggered. | |
9301 */ | |
9302 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9303 trigger_cursorhold(void) |
609 | 9304 { |
661 | 9305 int state; |
9306 | |
2976 | 9307 if (!did_cursorhold |
9308 && has_cursorhold() | |
14004
e124262d435e
patch 8.1.0020: cannot tell whether a register is executing or recording
Christian Brabandt <cb@256bit.org>
parents:
13988
diff
changeset
|
9309 && reg_recording == 0 |
2976 | 9310 && typebuf.tb_len == 0 |
978 | 9311 #ifdef FEAT_INS_EXPAND |
9312 && !ins_compl_active() | |
9313 #endif | |
9314 ) | |
661 | 9315 { |
9316 state = get_real_state(); | |
9317 if (state == NORMAL_BUSY || (state & INSERT) != 0) | |
9318 return TRUE; | |
9319 } | |
9320 return FALSE; | |
609 | 9321 } |
661 | 9322 |
9323 /* | |
9324 * Return TRUE when there is a CursorMoved autocommand defined. | |
9325 */ | |
9326 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9327 has_cursormoved(void) |
661 | 9328 { |
9329 return (first_autopat[(int)EVENT_CURSORMOVED] != NULL); | |
9330 } | |
9331 | |
9332 /* | |
9333 * Return TRUE when there is a CursorMovedI autocommand defined. | |
9334 */ | |
9335 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9336 has_cursormovedI(void) |
661 | 9337 { |
9338 return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL); | |
9339 } | |
7 | 9340 |
3390 | 9341 /* |
4232 | 9342 * Return TRUE when there is a TextChanged autocommand defined. |
9343 */ | |
9344 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9345 has_textchanged(void) |
4232 | 9346 { |
9347 return (first_autopat[(int)EVENT_TEXTCHANGED] != NULL); | |
9348 } | |
9349 | |
9350 /* | |
9351 * Return TRUE when there is a TextChangedI autocommand defined. | |
9352 */ | |
9353 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9354 has_textchangedI(void) |
4232 | 9355 { |
9356 return (first_autopat[(int)EVENT_TEXTCHANGEDI] != NULL); | |
9357 } | |
9358 | |
9359 /* | |
13240
5ed6e4a25925
patch 8.0.1494: no autocmd triggered in Insert mode with visible popup menu
Christian Brabandt <cb@256bit.org>
parents:
13170
diff
changeset
|
9360 * Return TRUE when there is a TextChangedP autocommand defined. |
5ed6e4a25925
patch 8.0.1494: no autocmd triggered in Insert mode with visible popup menu
Christian Brabandt <cb@256bit.org>
parents:
13170
diff
changeset
|
9361 */ |
5ed6e4a25925
patch 8.0.1494: no autocmd triggered in Insert mode with visible popup menu
Christian Brabandt <cb@256bit.org>
parents:
13170
diff
changeset
|
9362 int |
5ed6e4a25925
patch 8.0.1494: no autocmd triggered in Insert mode with visible popup menu
Christian Brabandt <cb@256bit.org>
parents:
13170
diff
changeset
|
9363 has_textchangedP(void) |
5ed6e4a25925
patch 8.0.1494: no autocmd triggered in Insert mode with visible popup menu
Christian Brabandt <cb@256bit.org>
parents:
13170
diff
changeset
|
9364 { |
5ed6e4a25925
patch 8.0.1494: no autocmd triggered in Insert mode with visible popup menu
Christian Brabandt <cb@256bit.org>
parents:
13170
diff
changeset
|
9365 return (first_autopat[(int)EVENT_TEXTCHANGEDP] != NULL); |
5ed6e4a25925
patch 8.0.1494: no autocmd triggered in Insert mode with visible popup menu
Christian Brabandt <cb@256bit.org>
parents:
13170
diff
changeset
|
9366 } |
5ed6e4a25925
patch 8.0.1494: no autocmd triggered in Insert mode with visible popup menu
Christian Brabandt <cb@256bit.org>
parents:
13170
diff
changeset
|
9367 |
5ed6e4a25925
patch 8.0.1494: no autocmd triggered in Insert mode with visible popup menu
Christian Brabandt <cb@256bit.org>
parents:
13170
diff
changeset
|
9368 /* |
3390 | 9369 * Return TRUE when there is an InsertCharPre autocommand defined. |
9370 */ | |
9371 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9372 has_insertcharpre(void) |
3390 | 9373 { |
9374 return (first_autopat[(int)EVENT_INSERTCHARPRE] != NULL); | |
9375 } | |
9376 | |
6154 | 9377 /* |
9378 * Return TRUE when there is an CmdUndefined autocommand defined. | |
9379 */ | |
9380 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9381 has_cmdundefined(void) |
6154 | 9382 { |
9383 return (first_autopat[(int)EVENT_CMDUNDEFINED] != NULL); | |
9384 } | |
9385 | |
9386 /* | |
9387 * Return TRUE when there is an FuncUndefined autocommand defined. | |
9388 */ | |
9389 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9390 has_funcundefined(void) |
6154 | 9391 { |
9392 return (first_autopat[(int)EVENT_FUNCUNDEFINED] != NULL); | |
9393 } | |
9394 | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9395 /* |
13037
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9396 * Return TRUE when there is a TextYankPost autocommand defined. |
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9397 */ |
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9398 int |
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9399 has_textyankpost(void) |
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9400 { |
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9401 return (first_autopat[(int)EVENT_TEXTYANKPOST] != NULL); |
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9402 } |
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9403 |
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9404 /* |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9405 * Execute autocommands for "event" and file name "fname". |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9406 * Return TRUE if some commands were executed. |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9407 */ |
7 | 9408 static int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9409 apply_autocmds_group( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9410 event_T event, |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
9411 char_u *fname, /* NULL or empty means use actual file name */ |
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
9412 char_u *fname_io, /* fname to use for <afile> on cmdline, NULL means |
7 | 9413 use fname */ |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
9414 int force, /* when TRUE, ignore autocmd_busy */ |
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
9415 int group, /* group ID, or AUGROUP_ALL */ |
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
9416 buf_T *buf, /* buffer for <abuf> */ |
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
9417 exarg_T *eap UNUSED) /* command arguments */ |
7 | 9418 { |
9419 char_u *sfname = NULL; /* short file name */ | |
9420 char_u *tail; | |
9421 int save_changed; | |
9422 buf_T *old_curbuf; | |
9423 int retval = FALSE; | |
9424 char_u *save_sourcing_name; | |
9425 linenr_T save_sourcing_lnum; | |
9426 char_u *save_autocmd_fname; | |
1723 | 9427 int save_autocmd_fname_full; |
7 | 9428 int save_autocmd_bufnr; |
9429 char_u *save_autocmd_match; | |
9430 int save_autocmd_busy; | |
9431 int save_autocmd_nested; | |
9432 static int nesting = 0; | |
9433 AutoPatCmd patcmd; | |
9434 AutoPat *ap; | |
9435 #ifdef FEAT_EVAL | |
9436 scid_T save_current_SID; | |
9437 void *save_funccalp; | |
9438 char_u *save_cmdarg; | |
9439 long save_cmdbang; | |
9440 #endif | |
9441 static int filechangeshell_busy = FALSE; | |
170 | 9442 #ifdef FEAT_PROFILE |
9443 proftime_T wait_time; | |
9444 #endif | |
6608 | 9445 int did_save_redobuff = FALSE; |
11325
77f3b7316d8b
patch 8.0.0548: saving the redo buffer only works one time
Christian Brabandt <cb@256bit.org>
parents:
11199
diff
changeset
|
9446 save_redo_T save_redo; |
12796
9d51b8c6b84e
patch 8.0.1275: CmdlineLeave autocmd prevents fold from opening
Christian Brabandt <cb@256bit.org>
parents:
12771
diff
changeset
|
9447 int save_KeyTyped = KeyTyped; |
7 | 9448 |
9449 /* | |
9450 * Quickly return if there are no autocommands for this event or | |
9451 * autocommands are blocked. | |
9452 */ | |
11641
dbce7e03bf55
patch 8.0.0703: illegal memory access with empty :doau command
Christian Brabandt <cb@256bit.org>
parents:
11605
diff
changeset
|
9453 if (event == NUM_EVENTS || first_autopat[(int)event] == NULL |
dbce7e03bf55
patch 8.0.0703: illegal memory access with empty :doau command
Christian Brabandt <cb@256bit.org>
parents:
11605
diff
changeset
|
9454 || autocmd_blocked > 0) |
40 | 9455 goto BYPASS_AU; |
7 | 9456 |
9457 /* | |
9458 * When autocommands are busy, new autocommands are only executed when | |
9459 * explicitly enabled with the "nested" flag. | |
9460 */ | |
9461 if (autocmd_busy && !(force || autocmd_nested)) | |
40 | 9462 goto BYPASS_AU; |
7 | 9463 |
9464 #ifdef FEAT_EVAL | |
9465 /* | |
1201 | 9466 * Quickly return when immediately aborting on error, or when an interrupt |
7 | 9467 * occurred or an exception was thrown but not caught. |
9468 */ | |
9469 if (aborting()) | |
40 | 9470 goto BYPASS_AU; |
7 | 9471 #endif |
9472 | |
9473 /* | |
9474 * FileChangedShell never nests, because it can create an endless loop. | |
9475 */ | |
765 | 9476 if (filechangeshell_busy && (event == EVENT_FILECHANGEDSHELL |
9477 || event == EVENT_FILECHANGEDSHELLPOST)) | |
40 | 9478 goto BYPASS_AU; |
7 | 9479 |
9480 /* | |
9481 * Ignore events in 'eventignore'. | |
9482 */ | |
9483 if (event_ignored(event)) | |
40 | 9484 goto BYPASS_AU; |
7 | 9485 |
9486 /* | |
9487 * Allow nesting of autocommands, but restrict the depth, because it's | |
9488 * possible to create an endless loop. | |
9489 */ | |
9490 if (nesting == 10) | |
9491 { | |
9492 EMSG(_("E218: autocommand nesting too deep")); | |
40 | 9493 goto BYPASS_AU; |
7 | 9494 } |
9495 | |
9496 /* | |
9497 * Check if these autocommands are disabled. Used when doing ":all" or | |
9498 * ":ball". | |
9499 */ | |
9500 if ( (autocmd_no_enter | |
9501 && (event == EVENT_WINENTER || event == EVENT_BUFENTER)) | |
9502 || (autocmd_no_leave | |
9503 && (event == EVENT_WINLEAVE || event == EVENT_BUFLEAVE))) | |
40 | 9504 goto BYPASS_AU; |
7 | 9505 |
9506 /* | |
9507 * Save the autocmd_* variables and info about the current buffer. | |
9508 */ | |
9509 save_autocmd_fname = autocmd_fname; | |
1723 | 9510 save_autocmd_fname_full = autocmd_fname_full; |
7 | 9511 save_autocmd_bufnr = autocmd_bufnr; |
9512 save_autocmd_match = autocmd_match; | |
9513 save_autocmd_busy = autocmd_busy; | |
9514 save_autocmd_nested = autocmd_nested; | |
9515 save_changed = curbuf->b_changed; | |
9516 old_curbuf = curbuf; | |
9517 | |
9518 /* | |
9519 * Set the file name to be used for <afile>. | |
1471 | 9520 * Make a copy to avoid that changing a buffer name or directory makes it |
9521 * invalid. | |
7 | 9522 */ |
9523 if (fname_io == NULL) | |
9524 { | |
13810
cad480bac9e1
patch 8.0.1777: cannot cleanup before loading another colorscheme
Christian Brabandt <cb@256bit.org>
parents:
13804
diff
changeset
|
9525 if (event == EVENT_COLORSCHEME || event == EVENT_COLORSCHEMEPRE |
cad480bac9e1
patch 8.0.1777: cannot cleanup before loading another colorscheme
Christian Brabandt <cb@256bit.org>
parents:
13804
diff
changeset
|
9526 || event == EVENT_OPTIONSET) |
5521 | 9527 autocmd_fname = NULL; |
11641
dbce7e03bf55
patch 8.0.0703: illegal memory access with empty :doau command
Christian Brabandt <cb@256bit.org>
parents:
11605
diff
changeset
|
9528 else if (fname != NULL && !ends_excmd(*fname)) |
7 | 9529 autocmd_fname = fname; |
9530 else if (buf != NULL) | |
1723 | 9531 autocmd_fname = buf->b_ffname; |
7 | 9532 else |
9533 autocmd_fname = NULL; | |
9534 } | |
9535 else | |
9536 autocmd_fname = fname_io; | |
1471 | 9537 if (autocmd_fname != NULL) |
1723 | 9538 autocmd_fname = vim_strsave(autocmd_fname); |
9539 autocmd_fname_full = FALSE; /* call FullName_save() later */ | |
7 | 9540 |
9541 /* | |
9542 * Set the buffer number to be used for <abuf>. | |
9543 */ | |
9544 if (buf == NULL) | |
9545 autocmd_bufnr = 0; | |
9546 else | |
9547 autocmd_bufnr = buf->b_fnum; | |
9548 | |
9549 /* | |
9550 * When the file name is NULL or empty, use the file name of buffer "buf". | |
9551 * Always use the full path of the file name to match with, in case | |
9552 * "allow_dirs" is set. | |
9553 */ | |
9554 if (fname == NULL || *fname == NUL) | |
9555 { | |
9556 if (buf == NULL) | |
9557 fname = NULL; | |
9558 else | |
9559 { | |
9560 #ifdef FEAT_SYN_HL | |
9561 if (event == EVENT_SYNTAX) | |
9562 fname = buf->b_p_syn; | |
9563 else | |
9564 #endif | |
9565 if (event == EVENT_FILETYPE) | |
9566 fname = buf->b_p_ft; | |
9567 else | |
9568 { | |
9569 if (buf->b_sfname != NULL) | |
9570 sfname = vim_strsave(buf->b_sfname); | |
9571 fname = buf->b_ffname; | |
9572 } | |
9573 } | |
9574 if (fname == NULL) | |
9575 fname = (char_u *)""; | |
9576 fname = vim_strsave(fname); /* make a copy, so we can change it */ | |
9577 } | |
9578 else | |
9579 { | |
9580 sfname = vim_strsave(fname); | |
5521 | 9581 /* Don't try expanding FileType, Syntax, FuncUndefined, WindowID, |
13170
6559e98f3e74
patch 8.0.1459: cannot handle change of directory
Christian Brabandt <cb@256bit.org>
parents:
13142
diff
changeset
|
9582 * ColorScheme, QuickFixCmd* or DirChanged */ |
161 | 9583 if (event == EVENT_FILETYPE |
9584 || event == EVENT_SYNTAX | |
13752
8a3e9d1e8cf9
patch 8.0.1748: CmdlineEnter command uses backslash instead of slash
Christian Brabandt <cb@256bit.org>
parents:
13730
diff
changeset
|
9585 || event == EVENT_CMDLINECHANGED |
8a3e9d1e8cf9
patch 8.0.1748: CmdlineEnter command uses backslash instead of slash
Christian Brabandt <cb@256bit.org>
parents:
13730
diff
changeset
|
9586 || event == EVENT_CMDLINEENTER |
8a3e9d1e8cf9
patch 8.0.1748: CmdlineEnter command uses backslash instead of slash
Christian Brabandt <cb@256bit.org>
parents:
13730
diff
changeset
|
9587 || event == EVENT_CMDLINELEAVE |
8a3e9d1e8cf9
patch 8.0.1748: CmdlineEnter command uses backslash instead of slash
Christian Brabandt <cb@256bit.org>
parents:
13730
diff
changeset
|
9588 || event == EVENT_CMDWINENTER |
8a3e9d1e8cf9
patch 8.0.1748: CmdlineEnter command uses backslash instead of slash
Christian Brabandt <cb@256bit.org>
parents:
13730
diff
changeset
|
9589 || event == EVENT_CMDWINLEAVE |
8a3e9d1e8cf9
patch 8.0.1748: CmdlineEnter command uses backslash instead of slash
Christian Brabandt <cb@256bit.org>
parents:
13730
diff
changeset
|
9590 || event == EVENT_CMDUNDEFINED |
1867 | 9591 || event == EVENT_FUNCUNDEFINED |
161 | 9592 || event == EVENT_REMOTEREPLY |
649 | 9593 || event == EVENT_SPELLFILEMISSING |
161 | 9594 || event == EVENT_QUICKFIXCMDPRE |
5521 | 9595 || event == EVENT_COLORSCHEME |
13810
cad480bac9e1
patch 8.0.1777: cannot cleanup before loading another colorscheme
Christian Brabandt <cb@256bit.org>
parents:
13804
diff
changeset
|
9596 || event == EVENT_COLORSCHEMEPRE |
6935 | 9597 || event == EVENT_OPTIONSET |
13170
6559e98f3e74
patch 8.0.1459: cannot handle change of directory
Christian Brabandt <cb@256bit.org>
parents:
13142
diff
changeset
|
9598 || event == EVENT_QUICKFIXCMDPOST |
6559e98f3e74
patch 8.0.1459: cannot handle change of directory
Christian Brabandt <cb@256bit.org>
parents:
13142
diff
changeset
|
9599 || event == EVENT_DIRCHANGED) |
13752
8a3e9d1e8cf9
patch 8.0.1748: CmdlineEnter command uses backslash instead of slash
Christian Brabandt <cb@256bit.org>
parents:
13730
diff
changeset
|
9600 { |
7 | 9601 fname = vim_strsave(fname); |
13752
8a3e9d1e8cf9
patch 8.0.1748: CmdlineEnter command uses backslash instead of slash
Christian Brabandt <cb@256bit.org>
parents:
13730
diff
changeset
|
9602 autocmd_fname_full = TRUE; /* don't expand it later */ |
8a3e9d1e8cf9
patch 8.0.1748: CmdlineEnter command uses backslash instead of slash
Christian Brabandt <cb@256bit.org>
parents:
13730
diff
changeset
|
9603 } |
7 | 9604 else |
9605 fname = FullName_save(fname, FALSE); | |
9606 } | |
9607 if (fname == NULL) /* out of memory */ | |
9608 { | |
9609 vim_free(sfname); | |
40 | 9610 retval = FALSE; |
9611 goto BYPASS_AU; | |
7 | 9612 } |
9613 | |
9614 #ifdef BACKSLASH_IN_FILENAME | |
9615 /* | |
9616 * Replace all backslashes with forward slashes. This makes the | |
9617 * autocommand patterns portable between Unix and MS-DOS. | |
9618 */ | |
9619 if (sfname != NULL) | |
9620 forward_slash(sfname); | |
9621 forward_slash(fname); | |
9622 #endif | |
9623 | |
9624 #ifdef VMS | |
9625 /* remove version for correct match */ | |
9626 if (sfname != NULL) | |
9627 vms_remove_version(sfname); | |
9628 vms_remove_version(fname); | |
9629 #endif | |
9630 | |
9631 /* | |
9632 * Set the name to be used for <amatch>. | |
9633 */ | |
9634 autocmd_match = fname; | |
9635 | |
9636 | |
14157
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
9637 /* Don't redraw while doing autocommands. */ |
7 | 9638 ++RedrawingDisabled; |
9639 save_sourcing_name = sourcing_name; | |
9640 sourcing_name = NULL; /* don't free this one */ | |
9641 save_sourcing_lnum = sourcing_lnum; | |
9642 sourcing_lnum = 0; /* no line number here */ | |
9643 | |
9644 #ifdef FEAT_EVAL | |
9645 save_current_SID = current_SID; | |
9646 | |
170 | 9647 # ifdef FEAT_PROFILE |
788 | 9648 if (do_profiling == PROF_YES) |
170 | 9649 prof_child_enter(&wait_time); /* doesn't count for the caller itself */ |
9650 # endif | |
9651 | |
7 | 9652 /* Don't use local function variables, if called from a function */ |
9653 save_funccalp = save_funccal(); | |
9654 #endif | |
9655 | |
9656 /* | |
9657 * When starting to execute autocommands, save the search patterns. | |
9658 */ | |
9659 if (!autocmd_busy) | |
9660 { | |
9661 save_search_patterns(); | |
7285
a34232b17763
commit https://github.com/vim/vim/commit/20ad69ccfb60ef718bd26387ef0e5424461a643e
Christian Brabandt <cb@256bit.org>
parents:
7170
diff
changeset
|
9662 #ifdef FEAT_INS_EXPAND |
6608 | 9663 if (!ins_compl_active()) |
7285
a34232b17763
commit https://github.com/vim/vim/commit/20ad69ccfb60ef718bd26387ef0e5424461a643e
Christian Brabandt <cb@256bit.org>
parents:
7170
diff
changeset
|
9664 #endif |
6608 | 9665 { |
11325
77f3b7316d8b
patch 8.0.0548: saving the redo buffer only works one time
Christian Brabandt <cb@256bit.org>
parents:
11199
diff
changeset
|
9666 saveRedobuff(&save_redo); |
6608 | 9667 did_save_redobuff = TRUE; |
9668 } | |
7 | 9669 did_filetype = keep_filetype; |
9670 } | |
9671 | |
9672 /* | |
9673 * Note that we are applying autocmds. Some commands need to know. | |
9674 */ | |
9675 autocmd_busy = TRUE; | |
9676 filechangeshell_busy = (event == EVENT_FILECHANGEDSHELL); | |
9677 ++nesting; /* see matching decrement below */ | |
9678 | |
9679 /* Remember that FileType was triggered. Used for did_filetype(). */ | |
9680 if (event == EVENT_FILETYPE) | |
9681 did_filetype = TRUE; | |
9682 | |
9683 tail = gettail(fname); | |
9684 | |
9685 /* Find first autocommand that matches */ | |
9686 patcmd.curpat = first_autopat[(int)event]; | |
9687 patcmd.nextcmd = NULL; | |
9688 patcmd.group = group; | |
9689 patcmd.fname = fname; | |
9690 patcmd.sfname = sfname; | |
9691 patcmd.tail = tail; | |
9692 patcmd.event = event; | |
40 | 9693 patcmd.arg_bufnr = autocmd_bufnr; |
9694 patcmd.next = NULL; | |
7 | 9695 auto_next_pat(&patcmd, FALSE); |
9696 | |
9697 /* found one, start executing the autocommands */ | |
9698 if (patcmd.curpat != NULL) | |
9699 { | |
40 | 9700 /* add to active_apc_list */ |
9701 patcmd.next = active_apc_list; | |
9702 active_apc_list = &patcmd; | |
9703 | |
7 | 9704 #ifdef FEAT_EVAL |
9705 /* set v:cmdarg (only when there is a matching pattern) */ | |
9389
32e34e574716
commit https://github.com/vim/vim/commit/22fcfad29276bd5f317faf516637dcd491b96a12
Christian Brabandt <cb@256bit.org>
parents:
9387
diff
changeset
|
9706 save_cmdbang = (long)get_vim_var_nr(VV_CMDBANG); |
7 | 9707 if (eap != NULL) |
9708 { | |
9709 save_cmdarg = set_cmdarg(eap, NULL); | |
9710 set_vim_var_nr(VV_CMDBANG, (long)eap->forceit); | |
9711 } | |
9712 else | |
9713 save_cmdarg = NULL; /* avoid gcc warning */ | |
9714 #endif | |
9715 retval = TRUE; | |
9716 /* mark the last pattern, to avoid an endless loop when more patterns | |
9717 * are added when executing autocommands */ | |
9718 for (ap = patcmd.curpat; ap->next != NULL; ap = ap->next) | |
9719 ap->last = FALSE; | |
9720 ap->last = TRUE; | |
9721 check_lnums(TRUE); /* make sure cursor and topline are valid */ | |
9722 do_cmdline(NULL, getnextac, (void *)&patcmd, | |
9723 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT); | |
9724 #ifdef FEAT_EVAL | |
9725 if (eap != NULL) | |
9726 { | |
9727 (void)set_cmdarg(NULL, save_cmdarg); | |
9728 set_vim_var_nr(VV_CMDBANG, save_cmdbang); | |
9729 } | |
9730 #endif | |
40 | 9731 /* delete from active_apc_list */ |
9732 if (active_apc_list == &patcmd) /* just in case */ | |
9733 active_apc_list = patcmd.next; | |
7 | 9734 } |
9735 | |
9736 --RedrawingDisabled; | |
9737 autocmd_busy = save_autocmd_busy; | |
9738 filechangeshell_busy = FALSE; | |
9739 autocmd_nested = save_autocmd_nested; | |
9740 vim_free(sourcing_name); | |
9741 sourcing_name = save_sourcing_name; | |
9742 sourcing_lnum = save_sourcing_lnum; | |
1471 | 9743 vim_free(autocmd_fname); |
7 | 9744 autocmd_fname = save_autocmd_fname; |
1723 | 9745 autocmd_fname_full = save_autocmd_fname_full; |
7 | 9746 autocmd_bufnr = save_autocmd_bufnr; |
9747 autocmd_match = save_autocmd_match; | |
9748 #ifdef FEAT_EVAL | |
9749 current_SID = save_current_SID; | |
9750 restore_funccal(save_funccalp); | |
170 | 9751 # ifdef FEAT_PROFILE |
788 | 9752 if (do_profiling == PROF_YES) |
170 | 9753 prof_child_exit(&wait_time); |
9754 # endif | |
7 | 9755 #endif |
12796
9d51b8c6b84e
patch 8.0.1275: CmdlineLeave autocmd prevents fold from opening
Christian Brabandt <cb@256bit.org>
parents:
12771
diff
changeset
|
9756 KeyTyped = save_KeyTyped; |
7 | 9757 vim_free(fname); |
9758 vim_free(sfname); | |
9759 --nesting; /* see matching increment above */ | |
9760 | |
9761 /* | |
9762 * When stopping to execute autocommands, restore the search patterns and | |
5958 | 9763 * the redo buffer. Free any buffers in the au_pending_free_buf list and |
9764 * free any windows in the au_pending_free_win list. | |
7 | 9765 */ |
9766 if (!autocmd_busy) | |
9767 { | |
9768 restore_search_patterns(); | |
6608 | 9769 if (did_save_redobuff) |
11325
77f3b7316d8b
patch 8.0.0548: saving the redo buffer only works one time
Christian Brabandt <cb@256bit.org>
parents:
11199
diff
changeset
|
9770 restoreRedobuff(&save_redo); |
7 | 9771 did_filetype = FALSE; |
5816 | 9772 while (au_pending_free_buf != NULL) |
9773 { | |
9774 buf_T *b = au_pending_free_buf->b_next; | |
9775 vim_free(au_pending_free_buf); | |
9776 au_pending_free_buf = b; | |
9777 } | |
5958 | 9778 while (au_pending_free_win != NULL) |
9779 { | |
9780 win_T *w = au_pending_free_win->w_next; | |
9781 vim_free(au_pending_free_win); | |
9782 au_pending_free_win = w; | |
9783 } | |
7 | 9784 } |
9785 | |
9786 /* | |
9787 * Some events don't set or reset the Changed flag. | |
9788 * Check if still in the same buffer! | |
9789 */ | |
9790 if (curbuf == old_curbuf | |
9791 && (event == EVENT_BUFREADPOST | |
9792 || event == EVENT_BUFWRITEPOST | |
9793 || event == EVENT_FILEAPPENDPOST | |
9794 || event == EVENT_VIMLEAVE | |
9795 || event == EVENT_VIMLEAVEPRE)) | |
9796 { | |
9797 #ifdef FEAT_TITLE | |
9798 if (curbuf->b_changed != save_changed) | |
9799 need_maketitle = TRUE; | |
9800 #endif | |
9801 curbuf->b_changed = save_changed; | |
9802 } | |
9803 | |
9804 au_cleanup(); /* may really delete removed patterns/commands now */ | |
40 | 9805 |
9806 BYPASS_AU: | |
9807 /* When wiping out a buffer make sure all its buffer-local autocommands | |
9808 * are deleted. */ | |
9809 if (event == EVENT_BUFWIPEOUT && buf != NULL) | |
9810 aubuflocal_remove(buf); | |
9811 | |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
9812 if (retval == OK && event == EVENT_FILETYPE) |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
9813 au_did_filetype = TRUE; |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
9814 |
7 | 9815 return retval; |
9816 } | |
9817 | |
1410 | 9818 # ifdef FEAT_EVAL |
9819 static char_u *old_termresponse = NULL; | |
9820 # endif | |
9821 | |
9822 /* | |
9823 * Block triggering autocommands until unblock_autocmd() is called. | |
9824 * Can be used recursively, so long as it's symmetric. | |
9825 */ | |
9826 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9827 block_autocmds(void) |
1410 | 9828 { |
9829 # ifdef FEAT_EVAL | |
9830 /* Remember the value of v:termresponse. */ | |
9831 if (autocmd_blocked == 0) | |
9832 old_termresponse = get_vim_var_str(VV_TERMRESPONSE); | |
9833 # endif | |
9834 ++autocmd_blocked; | |
9835 } | |
9836 | |
9837 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9838 unblock_autocmds(void) |
1410 | 9839 { |
9840 --autocmd_blocked; | |
9841 | |
9842 # ifdef FEAT_EVAL | |
9843 /* When v:termresponse was set while autocommands were blocked, trigger | |
9844 * the autocommands now. Esp. useful when executing a shell command | |
9845 * during startup (vimdiff). */ | |
9846 if (autocmd_blocked == 0 | |
9847 && get_vim_var_str(VV_TERMRESPONSE) != old_termresponse) | |
9848 apply_autocmds(EVENT_TERMRESPONSE, NULL, NULL, FALSE, curbuf); | |
9849 # endif | |
9850 } | |
9851 | |
5008
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9852 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9853 is_autocmd_blocked(void) |
5008
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9854 { |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9855 return autocmd_blocked != 0; |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9856 } |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9857 |
7 | 9858 /* |
9859 * Find next autocommand pattern that matches. | |
9860 */ | |
9861 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9862 auto_next_pat( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9863 AutoPatCmd *apc, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9864 int stop_at_last) /* stop when 'last' flag is set */ |
7 | 9865 { |
9866 AutoPat *ap; | |
9867 AutoCmd *cp; | |
9868 char_u *name; | |
9869 char *s; | |
9870 | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
9871 VIM_CLEAR(sourcing_name); |
7 | 9872 |
9873 for (ap = apc->curpat; ap != NULL && !got_int; ap = ap->next) | |
9874 { | |
9875 apc->curpat = NULL; | |
9876 | |
1723 | 9877 /* Only use a pattern when it has not been removed, has commands and |
40 | 9878 * the group matches. For buffer-local autocommands only check the |
9879 * buffer number. */ | |
7 | 9880 if (ap->pat != NULL && ap->cmds != NULL |
9881 && (apc->group == AUGROUP_ALL || apc->group == ap->group)) | |
9882 { | |
40 | 9883 /* execution-condition */ |
9884 if (ap->buflocal_nr == 0 | |
6375 | 9885 ? (match_file_pat(NULL, &ap->reg_prog, apc->fname, |
153 | 9886 apc->sfname, apc->tail, ap->allow_dirs)) |
40 | 9887 : ap->buflocal_nr == apc->arg_bufnr) |
7 | 9888 { |
9889 name = event_nr2name(apc->event); | |
14157
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
9890 s = _("%s Autocommands for \"%s\""); |
7 | 9891 sourcing_name = alloc((unsigned)(STRLEN(s) |
9892 + STRLEN(name) + ap->patlen + 1)); | |
9893 if (sourcing_name != NULL) | |
9894 { | |
9895 sprintf((char *)sourcing_name, s, | |
9896 (char *)name, (char *)ap->pat); | |
9897 if (p_verbose >= 8) | |
292 | 9898 { |
9899 verbose_enter(); | |
273 | 9900 smsg((char_u *)_("Executing %s"), sourcing_name); |
292 | 9901 verbose_leave(); |
9902 } | |
7 | 9903 } |
9904 | |
9905 apc->curpat = ap; | |
9906 apc->nextcmd = ap->cmds; | |
9907 /* mark last command */ | |
9908 for (cp = ap->cmds; cp->next != NULL; cp = cp->next) | |
9909 cp->last = FALSE; | |
9910 cp->last = TRUE; | |
9911 } | |
9912 line_breakcheck(); | |
9913 if (apc->curpat != NULL) /* found a match */ | |
9914 break; | |
9915 } | |
9916 if (stop_at_last && ap->last) | |
9917 break; | |
9918 } | |
9919 } | |
9920 | |
9921 /* | |
9922 * Get next autocommand command. | |
9923 * Called by do_cmdline() to get the next line for ":if". | |
9924 * Returns allocated string, or NULL for end of autocommands. | |
9925 */ | |
3997 | 9926 char_u * |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9927 getnextac(int c UNUSED, void *cookie, int indent UNUSED) |
7 | 9928 { |
9929 AutoPatCmd *acp = (AutoPatCmd *)cookie; | |
9930 char_u *retval; | |
9931 AutoCmd *ac; | |
9932 | |
9933 /* Can be called again after returning the last line. */ | |
9934 if (acp->curpat == NULL) | |
9935 return NULL; | |
9936 | |
9937 /* repeat until we find an autocommand to execute */ | |
9938 for (;;) | |
9939 { | |
9940 /* skip removed commands */ | |
9941 while (acp->nextcmd != NULL && acp->nextcmd->cmd == NULL) | |
9942 if (acp->nextcmd->last) | |
9943 acp->nextcmd = NULL; | |
9944 else | |
9945 acp->nextcmd = acp->nextcmd->next; | |
9946 | |
9947 if (acp->nextcmd != NULL) | |
9948 break; | |
9949 | |
9950 /* at end of commands, find next pattern that matches */ | |
9951 if (acp->curpat->last) | |
9952 acp->curpat = NULL; | |
9953 else | |
9954 acp->curpat = acp->curpat->next; | |
9955 if (acp->curpat != NULL) | |
9956 auto_next_pat(acp, TRUE); | |
9957 if (acp->curpat == NULL) | |
9958 return NULL; | |
9959 } | |
9960 | |
9961 ac = acp->nextcmd; | |
9962 | |
9963 if (p_verbose >= 9) | |
9964 { | |
292 | 9965 verbose_enter_scroll(); |
273 | 9966 smsg((char_u *)_("autocommand %s"), ac->cmd); |
7 | 9967 msg_puts((char_u *)"\n"); /* don't overwrite this either */ |
292 | 9968 verbose_leave_scroll(); |
7 | 9969 } |
9970 retval = vim_strsave(ac->cmd); | |
9971 autocmd_nested = ac->nested; | |
9972 #ifdef FEAT_EVAL | |
9973 current_SID = ac->scriptID; | |
9974 #endif | |
9975 if (ac->last) | |
9976 acp->nextcmd = NULL; | |
9977 else | |
9978 acp->nextcmd = ac->next; | |
9979 return retval; | |
9980 } | |
9981 | |
9982 /* | |
9983 * Return TRUE if there is a matching autocommand for "fname". | |
40 | 9984 * To account for buffer-local autocommands, function needs to know |
9985 * in which buffer the file will be opened. | |
7 | 9986 */ |
9987 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9988 has_autocmd(event_T event, char_u *sfname, buf_T *buf) |
7 | 9989 { |
9990 AutoPat *ap; | |
9991 char_u *fname; | |
9992 char_u *tail = gettail(sfname); | |
9993 int retval = FALSE; | |
9994 | |
9995 fname = FullName_save(sfname, FALSE); | |
9996 if (fname == NULL) | |
9997 return FALSE; | |
9998 | |
9999 #ifdef BACKSLASH_IN_FILENAME | |
10000 /* | |
10001 * Replace all backslashes with forward slashes. This makes the | |
10002 * autocommand patterns portable between Unix and MS-DOS. | |
10003 */ | |
10004 sfname = vim_strsave(sfname); | |
10005 if (sfname != NULL) | |
10006 forward_slash(sfname); | |
10007 forward_slash(fname); | |
10008 #endif | |
10009 | |
10010 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
10011 if (ap->pat != NULL && ap->cmds != NULL | |
856 | 10012 && (ap->buflocal_nr == 0 |
6375 | 10013 ? match_file_pat(NULL, &ap->reg_prog, |
153 | 10014 fname, sfname, tail, ap->allow_dirs) |
856 | 10015 : buf != NULL && ap->buflocal_nr == buf->b_fnum |
40 | 10016 )) |
7 | 10017 { |
10018 retval = TRUE; | |
10019 break; | |
10020 } | |
10021 | |
10022 vim_free(fname); | |
10023 #ifdef BACKSLASH_IN_FILENAME | |
10024 vim_free(sfname); | |
10025 #endif | |
10026 | |
10027 return retval; | |
10028 } | |
10029 | |
10030 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
10031 /* | |
10032 * Function given to ExpandGeneric() to obtain the list of autocommand group | |
10033 * names. | |
10034 */ | |
10035 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10036 get_augroup_name(expand_T *xp UNUSED, int idx) |
7 | 10037 { |
10038 if (idx == augroups.ga_len) /* add "END" add the end */ | |
10039 return (char_u *)"END"; | |
10040 if (idx >= augroups.ga_len) /* end of list */ | |
10041 return NULL; | |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
10042 if (AUGROUP_NAME(idx) == NULL || AUGROUP_NAME(idx) == get_deleted_augroup()) |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
10043 /* skip deleted entries */ |
7 | 10044 return (char_u *)""; |
10045 return AUGROUP_NAME(idx); /* return a name */ | |
10046 } | |
10047 | |
10048 static int include_groups = FALSE; | |
10049 | |
10050 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10051 set_context_in_autocmd( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10052 expand_T *xp, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10053 char_u *arg, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10054 int doautocmd) /* TRUE for :doauto*, FALSE for :autocmd */ |
7 | 10055 { |
10056 char_u *p; | |
10057 int group; | |
10058 | |
10059 /* check for a group name, skip it if present */ | |
10060 include_groups = FALSE; | |
10061 p = arg; | |
10062 group = au_get_grouparg(&arg); | |
10063 if (group == AUGROUP_ERROR) | |
10064 return NULL; | |
10065 /* If there only is a group name that's what we expand. */ | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
10066 if (*arg == NUL && group != AUGROUP_ALL && !VIM_ISWHITE(arg[-1])) |
7 | 10067 { |
10068 arg = p; | |
10069 group = AUGROUP_ALL; | |
10070 } | |
10071 | |
10072 /* skip over event name */ | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
10073 for (p = arg; *p != NUL && !VIM_ISWHITE(*p); ++p) |
7 | 10074 if (*p == ',') |
10075 arg = p + 1; | |
10076 if (*p == NUL) | |
10077 { | |
10078 if (group == AUGROUP_ALL) | |
10079 include_groups = TRUE; | |
10080 xp->xp_context = EXPAND_EVENTS; /* expand event name */ | |
10081 xp->xp_pattern = arg; | |
10082 return NULL; | |
10083 } | |
10084 | |
10085 /* skip over pattern */ | |
10086 arg = skipwhite(p); | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
10087 while (*arg && (!VIM_ISWHITE(*arg) || arg[-1] == '\\')) |
7 | 10088 arg++; |
10089 if (*arg) | |
10090 return arg; /* expand (next) command */ | |
10091 | |
10092 if (doautocmd) | |
10093 xp->xp_context = EXPAND_FILES; /* expand file names */ | |
10094 else | |
10095 xp->xp_context = EXPAND_NOTHING; /* pattern is not expanded */ | |
10096 return NULL; | |
10097 } | |
10098 | |
10099 /* | |
10100 * Function given to ExpandGeneric() to obtain the list of event names. | |
10101 */ | |
10102 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10103 get_event_name(expand_T *xp UNUSED, int idx) |
7 | 10104 { |
10105 if (idx < augroups.ga_len) /* First list group names, if wanted */ | |
10106 { | |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
10107 if (!include_groups || AUGROUP_NAME(idx) == NULL |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
10108 || AUGROUP_NAME(idx) == get_deleted_augroup()) |
7 | 10109 return (char_u *)""; /* skip deleted entries */ |
10110 return AUGROUP_NAME(idx); /* return a name */ | |
10111 } | |
10112 return (char_u *)event_names[idx - augroups.ga_len].name; | |
10113 } | |
10114 | |
10115 #endif /* FEAT_CMDL_COMPL */ | |
10116 | |
10117 /* | |
615 | 10118 * Return TRUE if autocmd is supported. |
10119 */ | |
10120 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10121 autocmd_supported(char_u *name) |
615 | 10122 { |
10123 char_u *p; | |
10124 | |
10125 return (event_name2nr(name, &p) != NUM_EVENTS); | |
10126 } | |
10127 | |
10128 /* | |
612 | 10129 * Return TRUE if an autocommand is defined for a group, event and |
10130 * pattern: The group can be omitted to accept any group. "event" and "pattern" | |
10131 * can be NULL to accept any event and pattern. "pattern" can be NULL to accept | |
10132 * any pattern. Buffer-local patterns <buffer> or <buffer=N> are accepted. | |
10133 * Used for: | |
10134 * exists("#Group") or | |
10135 * exists("#Group#Event") or | |
10136 * exists("#Group#Event#pat") or | |
10137 * exists("#Event") or | |
10138 * exists("#Event#pat") | |
7 | 10139 */ |
10140 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10141 au_exists(char_u *arg) |
7 | 10142 { |
612 | 10143 char_u *arg_save; |
10144 char_u *pattern = NULL; | |
7 | 10145 char_u *event_name; |
10146 char_u *p; | |
661 | 10147 event_T event; |
7 | 10148 AutoPat *ap; |
40 | 10149 buf_T *buflocal_buf = NULL; |
612 | 10150 int group; |
10151 int retval = FALSE; | |
10152 | |
615 | 10153 /* Make a copy so that we can change the '#' chars to a NUL. */ |
612 | 10154 arg_save = vim_strsave(arg); |
10155 if (arg_save == NULL) | |
10156 return FALSE; | |
615 | 10157 p = vim_strchr(arg_save, '#'); |
612 | 10158 if (p != NULL) |
10159 *p++ = NUL; | |
10160 | |
10161 /* First, look for an autocmd group name */ | |
10162 group = au_find_group(arg_save); | |
10163 if (group == AUGROUP_ERROR) | |
10164 { | |
10165 /* Didn't match a group name, assume the first argument is an event. */ | |
10166 group = AUGROUP_ALL; | |
10167 event_name = arg_save; | |
10168 } | |
10169 else | |
10170 { | |
10171 if (p == NULL) | |
10172 { | |
10173 /* "Group": group name is present and it's recognized */ | |
10174 retval = TRUE; | |
10175 goto theend; | |
10176 } | |
10177 | |
10178 /* Must be "Group#Event" or "Group#Event#pat". */ | |
10179 event_name = p; | |
10180 p = vim_strchr(event_name, '#'); | |
10181 if (p != NULL) | |
10182 *p++ = NUL; /* "Group#Event#pat" */ | |
10183 } | |
10184 | |
10185 pattern = p; /* "pattern" is NULL when there is no pattern */ | |
7 | 10186 |
10187 /* find the index (enum) for the event name */ | |
10188 event = event_name2nr(event_name, &p); | |
10189 | |
10190 /* return FALSE if the event name is not recognized */ | |
612 | 10191 if (event == NUM_EVENTS) |
10192 goto theend; | |
7 | 10193 |
10194 /* Find the first autocommand for this event. | |
10195 * If there isn't any, return FALSE; | |
10196 * If there is one and no pattern given, return TRUE; */ | |
10197 ap = first_autopat[(int)event]; | |
10198 if (ap == NULL) | |
612 | 10199 goto theend; |
7 | 10200 |
40 | 10201 /* if pattern is "<buffer>", special handling is needed which uses curbuf */ |
10202 /* for pattern "<buffer=N>, fnamecmp() will work fine */ | |
1962 | 10203 if (pattern != NULL && STRICMP(pattern, "<buffer>") == 0) |
40 | 10204 buflocal_buf = curbuf; |
10205 | |
7 | 10206 /* Check if there is an autocommand with the given pattern. */ |
10207 for ( ; ap != NULL; ap = ap->next) | |
40 | 10208 /* only use a pattern when it has not been removed and has commands. */ |
10209 /* For buffer-local autocommands, fnamecmp() works fine. */ | |
7 | 10210 if (ap->pat != NULL && ap->cmds != NULL |
612 | 10211 && (group == AUGROUP_ALL || ap->group == group) |
1962 | 10212 && (pattern == NULL |
10213 || (buflocal_buf == NULL | |
10214 ? fnamecmp(ap->pat, pattern) == 0 | |
10215 : ap->buflocal_nr == buflocal_buf->b_fnum))) | |
612 | 10216 { |
10217 retval = TRUE; | |
10218 break; | |
10219 } | |
10220 | |
10221 theend: | |
10222 vim_free(arg_save); | |
10223 return retval; | |
7 | 10224 } |
40 | 10225 |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
10226 |
7 | 10227 /* |
153 | 10228 * Try matching a filename with a "pattern" ("prog" is NULL), or use the |
10229 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling | |
10230 * vim_regcomp() often. | |
7 | 10231 * Used for autocommands and 'wildignore'. |
10232 * Returns TRUE if there is a match, FALSE otherwise. | |
10233 */ | |
6375 | 10234 static int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10235 match_file_pat( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10236 char_u *pattern, /* pattern to match with */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10237 regprog_T **prog, /* pre-compiled regprog or NULL */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10238 char_u *fname, /* full path of file name */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10239 char_u *sfname, /* short file name or NULL */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10240 char_u *tail, /* tail of path */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10241 int allow_dirs) /* allow matching with dir */ |
7 | 10242 { |
10243 regmatch_T regmatch; | |
10244 int result = FALSE; | |
10245 | |
4242 | 10246 regmatch.rm_ic = p_fic; /* ignore case if 'fileignorecase' is set */ |
6470 | 10247 if (prog != NULL) |
10248 regmatch.regprog = *prog; | |
7 | 10249 else |
6470 | 10250 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC); |
7 | 10251 |
10252 /* | |
10253 * Try for a match with the pattern with: | |
10254 * 1. the full file name, when the pattern has a '/'. | |
10255 * 2. the short file name, when the pattern has a '/'. | |
10256 * 3. the tail of the file name, when the pattern has no '/'. | |
10257 */ | |
6470 | 10258 if (regmatch.regprog != NULL |
7 | 10259 && ((allow_dirs |
10260 && (vim_regexec(®match, fname, (colnr_T)0) | |
10261 || (sfname != NULL | |
10262 && vim_regexec(®match, sfname, (colnr_T)0)))) | |
6470 | 10263 || (!allow_dirs && vim_regexec(®match, tail, (colnr_T)0)))) |
7 | 10264 result = TRUE; |
10265 | |
6375 | 10266 if (prog != NULL) |
10267 *prog = regmatch.regprog; | |
10268 else | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4354
diff
changeset
|
10269 vim_regfree(regmatch.regprog); |
7 | 10270 return result; |
10271 } | |
10272 | |
10273 #if defined(FEAT_WILDIGN) || defined(PROTO) | |
10274 /* | |
10275 * Return TRUE if a file matches with a pattern in "list". | |
10276 * "list" is a comma-separated list of patterns, like 'wildignore'. | |
10277 * "sfname" is the short file name or NULL, "ffname" the long file name. | |
10278 */ | |
10279 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10280 match_file_list(char_u *list, char_u *sfname, char_u *ffname) |
7 | 10281 { |
10282 char_u buf[100]; | |
10283 char_u *tail; | |
10284 char_u *regpat; | |
10285 char allow_dirs; | |
10286 int match; | |
10287 char_u *p; | |
10288 | |
10289 tail = gettail(sfname); | |
10290 | |
10291 /* try all patterns in 'wildignore' */ | |
10292 p = list; | |
10293 while (*p) | |
10294 { | |
10295 copy_option_part(&p, buf, 100, ","); | |
10296 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE); | |
10297 if (regpat == NULL) | |
10298 break; | |
153 | 10299 match = match_file_pat(regpat, NULL, ffname, sfname, |
10300 tail, (int)allow_dirs); | |
7 | 10301 vim_free(regpat); |
10302 if (match) | |
10303 return TRUE; | |
10304 } | |
10305 return FALSE; | |
10306 } | |
10307 #endif | |
10308 | |
10309 /* | |
10310 * Convert the given pattern "pat" which has shell style wildcards in it, into | |
10311 * a regular expression, and return the result in allocated memory. If there | |
10312 * is a directory path separator to be matched, then TRUE is put in | |
10313 * allow_dirs, otherwise FALSE is put there -- webb. | |
10314 * Handle backslashes before special characters, like "\*" and "\ ". | |
10315 * | |
10316 * Returns NULL when out of memory. | |
10317 */ | |
10318 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10319 file_pat_to_reg_pat( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10320 char_u *pat, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10321 char_u *pat_end, /* first char after pattern or NULL */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10322 char *allow_dirs, /* Result passed back out in here */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10323 int no_bslash UNUSED) /* Don't use a backward slash as pathsep */ |
7 | 10324 { |
6470 | 10325 int size = 2; /* '^' at start, '$' at end */ |
7 | 10326 char_u *endp; |
10327 char_u *reg_pat; | |
10328 char_u *p; | |
10329 int i; | |
10330 int nested = 0; | |
10331 int add_dollar = TRUE; | |
10332 | |
10333 if (allow_dirs != NULL) | |
10334 *allow_dirs = FALSE; | |
10335 if (pat_end == NULL) | |
10336 pat_end = pat + STRLEN(pat); | |
10337 | |
10338 for (p = pat; p < pat_end; p++) | |
10339 { | |
10340 switch (*p) | |
10341 { | |
10342 case '*': | |
10343 case '.': | |
10344 case ',': | |
10345 case '{': | |
10346 case '}': | |
10347 case '~': | |
10348 size += 2; /* extra backslash */ | |
10349 break; | |
10350 #ifdef BACKSLASH_IN_FILENAME | |
10351 case '\\': | |
10352 case '/': | |
10353 size += 4; /* could become "[\/]" */ | |
10354 break; | |
10355 #endif | |
10356 default: | |
10357 size++; | |
6999 | 10358 # ifdef FEAT_MBYTE |
474 | 10359 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 10360 { |
10361 ++p; | |
10362 ++size; | |
10363 } | |
10364 # endif | |
10365 break; | |
10366 } | |
10367 } | |
10368 reg_pat = alloc(size + 1); | |
10369 if (reg_pat == NULL) | |
10370 return NULL; | |
10371 | |
10372 i = 0; | |
10373 | |
10374 if (pat[0] == '*') | |
10375 while (pat[0] == '*' && pat < pat_end - 1) | |
10376 pat++; | |
10377 else | |
10378 reg_pat[i++] = '^'; | |
10379 endp = pat_end - 1; | |
7005 | 10380 if (endp >= pat && *endp == '*') |
7 | 10381 { |
10382 while (endp - pat > 0 && *endp == '*') | |
10383 endp--; | |
10384 add_dollar = FALSE; | |
10385 } | |
10386 for (p = pat; *p && nested >= 0 && p <= endp; p++) | |
10387 { | |
10388 switch (*p) | |
10389 { | |
10390 case '*': | |
10391 reg_pat[i++] = '.'; | |
10392 reg_pat[i++] = '*'; | |
444 | 10393 while (p[1] == '*') /* "**" matches like "*" */ |
10394 ++p; | |
7 | 10395 break; |
10396 case '.': | |
10397 case '~': | |
10398 reg_pat[i++] = '\\'; | |
10399 reg_pat[i++] = *p; | |
10400 break; | |
10401 case '?': | |
10402 reg_pat[i++] = '.'; | |
10403 break; | |
10404 case '\\': | |
10405 if (p[1] == NUL) | |
10406 break; | |
10407 #ifdef BACKSLASH_IN_FILENAME | |
10408 if (!no_bslash) | |
10409 { | |
10410 /* translate: | |
10411 * "\x" to "\\x" e.g., "dir\file" | |
10412 * "\*" to "\\.*" e.g., "dir\*.c" | |
10413 * "\?" to "\\." e.g., "dir\??.c" | |
10414 * "\+" to "\+" e.g., "fileX\+.c" | |
10415 */ | |
10416 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?') | |
10417 && p[1] != '+') | |
10418 { | |
10419 reg_pat[i++] = '['; | |
10420 reg_pat[i++] = '\\'; | |
10421 reg_pat[i++] = '/'; | |
10422 reg_pat[i++] = ']'; | |
10423 if (allow_dirs != NULL) | |
10424 *allow_dirs = TRUE; | |
10425 break; | |
10426 } | |
10427 } | |
10428 #endif | |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10429 /* Undo escaping from ExpandEscape(): |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10430 * foo\?bar -> foo?bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10431 * foo\%bar -> foo%bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10432 * foo\,bar -> foo,bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10433 * foo\ bar -> foo bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10434 * Don't unescape \, * and others that are also special in a |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
5008
diff
changeset
|
10435 * regexp. |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
5008
diff
changeset
|
10436 * An escaped { must be unescaped since we use magic not |
5259
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10437 * verymagic. Use "\\\{n,m\}"" to get "\{n,m}". |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
5008
diff
changeset
|
10438 */ |
7 | 10439 if (*++p == '?' |
10440 #ifdef BACKSLASH_IN_FILENAME | |
10441 && no_bslash | |
10442 #endif | |
10443 ) | |
10444 reg_pat[i++] = '?'; | |
10445 else | |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
5008
diff
changeset
|
10446 if (*p == ',' || *p == '%' || *p == '#' |
6999 | 10447 || vim_isspace(*p) || *p == '{' || *p == '}') |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10448 reg_pat[i++] = *p; |
5259
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10449 else if (*p == '\\' && p[1] == '\\' && p[2] == '{') |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10450 { |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10451 reg_pat[i++] = '\\'; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10452 reg_pat[i++] = '{'; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10453 p += 2; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10454 } |
7 | 10455 else |
10456 { | |
10457 if (allow_dirs != NULL && vim_ispathsep(*p) | |
10458 #ifdef BACKSLASH_IN_FILENAME | |
10459 && (!no_bslash || *p != '\\') | |
10460 #endif | |
10461 ) | |
10462 *allow_dirs = TRUE; | |
10463 reg_pat[i++] = '\\'; | |
10464 reg_pat[i++] = *p; | |
10465 } | |
10466 break; | |
10467 #ifdef BACKSLASH_IN_FILENAME | |
10468 case '/': | |
10469 reg_pat[i++] = '['; | |
10470 reg_pat[i++] = '\\'; | |
10471 reg_pat[i++] = '/'; | |
10472 reg_pat[i++] = ']'; | |
10473 if (allow_dirs != NULL) | |
10474 *allow_dirs = TRUE; | |
10475 break; | |
10476 #endif | |
10477 case '{': | |
10478 reg_pat[i++] = '\\'; | |
10479 reg_pat[i++] = '('; | |
10480 nested++; | |
10481 break; | |
10482 case '}': | |
10483 reg_pat[i++] = '\\'; | |
10484 reg_pat[i++] = ')'; | |
10485 --nested; | |
10486 break; | |
10487 case ',': | |
10488 if (nested) | |
10489 { | |
10490 reg_pat[i++] = '\\'; | |
10491 reg_pat[i++] = '|'; | |
10492 } | |
10493 else | |
10494 reg_pat[i++] = ','; | |
10495 break; | |
10496 default: | |
10497 # ifdef FEAT_MBYTE | |
474 | 10498 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 10499 reg_pat[i++] = *p++; |
10500 else | |
10501 # endif | |
10502 if (allow_dirs != NULL && vim_ispathsep(*p)) | |
10503 *allow_dirs = TRUE; | |
10504 reg_pat[i++] = *p; | |
10505 break; | |
10506 } | |
10507 } | |
10508 if (add_dollar) | |
10509 reg_pat[i++] = '$'; | |
10510 reg_pat[i] = NUL; | |
10511 if (nested != 0) | |
10512 { | |
10513 if (nested < 0) | |
10514 EMSG(_("E219: Missing {.")); | |
10515 else | |
10516 EMSG(_("E220: Missing }.")); | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
10517 VIM_CLEAR(reg_pat); |
7 | 10518 } |
10519 return reg_pat; | |
10520 } | |
2664 | 10521 |
10522 #if defined(EINTR) || defined(PROTO) | |
10523 /* | |
10524 * Version of read() that retries when interrupted by EINTR (possibly | |
10525 * by a SIGWINCH). | |
10526 */ | |
10527 long | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10528 read_eintr(int fd, void *buf, size_t bufsize) |
2664 | 10529 { |
10530 long ret; | |
10531 | |
10532 for (;;) | |
10533 { | |
10534 ret = vim_read(fd, buf, bufsize); | |
10535 if (ret >= 0 || errno != EINTR) | |
10536 break; | |
10537 } | |
10538 return ret; | |
10539 } | |
10540 | |
10541 /* | |
10542 * Version of write() that retries when interrupted by EINTR (possibly | |
10543 * by a SIGWINCH). | |
10544 */ | |
10545 long | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10546 write_eintr(int fd, void *buf, size_t bufsize) |
2664 | 10547 { |
10548 long ret = 0; | |
10549 long wlen; | |
10550 | |
10551 /* Repeat the write() so long it didn't fail, other than being interrupted | |
10552 * by a signal. */ | |
10553 while (ret < (long)bufsize) | |
10554 { | |
2666 | 10555 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret); |
2664 | 10556 if (wlen < 0) |
10557 { | |
10558 if (errno != EINTR) | |
10559 break; | |
10560 } | |
10561 else | |
10562 ret += wlen; | |
10563 } | |
10564 return ret; | |
10565 } | |
10566 #endif |