Mercurial > vim
annotate src/fileio.c @ 14917:6f2ce3b311de v8.1.0470
patch 8.1.0470: pointer ownership around fname_expand() is unclear
commit https://github.com/vim/vim/commit/3d6014f0336d9a64c01a7518fe45fde0a925fa20
Author: Bram Moolenaar <Bram@vim.org>
Date: Thu Oct 11 19:27:47 2018 +0200
patch 8.1.0470: pointer ownership around fname_expand() is unclear
Problem: Pointer ownership around fname_expand() is unclear.
Solution: Allow b_ffname and b_sfname to point to the same allocated memory,
only free one. Update comments.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Thu, 11 Oct 2018 19:30:05 +0200 |
parents | 27b9a84395b5 |
children | 162d79d273e6 |
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 |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
42 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
|
43 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
|
44 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
|
45 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
|
46 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
|
47 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
|
48 static int au_find_group(char_u *name); |
676 | 49 |
50 # define AUGROUP_DEFAULT -1 /* default autocmd group */ | |
1834 | 51 # define AUGROUP_ERROR -2 /* erroneous autocmd group */ |
676 | 52 # define AUGROUP_ALL -3 /* all autocmd groups */ |
7 | 53 |
54 #if defined(FEAT_CRYPT) || defined(FEAT_MBYTE) | |
55 # define HAS_BW_FLAGS | |
56 # define FIO_LATIN1 0x01 /* convert Latin1 */ | |
57 # define FIO_UTF8 0x02 /* convert UTF-8 */ | |
58 # define FIO_UCS2 0x04 /* convert UCS-2 */ | |
59 # define FIO_UCS4 0x08 /* convert UCS-4 */ | |
60 # define FIO_UTF16 0x10 /* convert UTF-16 */ | |
61 # ifdef WIN3264 | |
62 # define FIO_CODEPAGE 0x20 /* convert MS-Windows codepage */ | |
63 # define FIO_PUT_CP(x) (((x) & 0xffff) << 16) /* put codepage in top word */ | |
64 # define FIO_GET_CP(x) (((x)>>16) & 0xffff) /* get codepage from top word */ | |
65 # endif | |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
66 # ifdef MACOS_CONVERT |
7 | 67 # define FIO_MACROMAN 0x20 /* convert MacRoman */ |
68 # endif | |
69 # define FIO_ENDIAN_L 0x80 /* little endian */ | |
70 # define FIO_ENCRYPTED 0x1000 /* encrypt written bytes */ | |
71 # define FIO_NOCONVERT 0x2000 /* skip encoding conversion */ | |
72 # define FIO_UCSBOM 0x4000 /* check for BOM at start of file */ | |
73 # define FIO_ALL -1 /* allow all formats */ | |
74 #endif | |
75 | |
76 /* When converting, a read() or write() may leave some bytes to be converted | |
77 * for the next call. The value is guessed... */ | |
78 #define CONV_RESTLEN 30 | |
79 | |
80 /* We have to guess how much a sequence of bytes may expand when converting | |
81 * with iconv() to be able to allocate a buffer. */ | |
82 #define ICONV_MULT 8 | |
83 | |
84 /* | |
85 * Structure to pass arguments from buf_write() to buf_write_bytes(). | |
86 */ | |
87 struct bw_info | |
88 { | |
89 int bw_fd; /* file descriptor */ | |
90 char_u *bw_buf; /* buffer with data to be written */ | |
1411 | 91 int bw_len; /* length of data */ |
7 | 92 #ifdef HAS_BW_FLAGS |
93 int bw_flags; /* FIO_ flags */ | |
94 #endif | |
6122 | 95 #ifdef FEAT_CRYPT |
96 buf_T *bw_buffer; /* buffer being written */ | |
97 #endif | |
7 | 98 #ifdef FEAT_MBYTE |
99 char_u bw_rest[CONV_RESTLEN]; /* not converted bytes */ | |
100 int bw_restlen; /* nr of bytes in bw_rest[] */ | |
101 int bw_first; /* first write call */ | |
102 char_u *bw_conv_buf; /* buffer for writing converted chars */ | |
103 int bw_conv_buflen; /* size of bw_conv_buf */ | |
104 int bw_conv_error; /* set for conversion error */ | |
1947 | 105 linenr_T bw_conv_error_lnum; /* first line with error or zero */ |
106 linenr_T bw_start_lnum; /* line number at start of buffer */ | |
7 | 107 # ifdef USE_ICONV |
108 iconv_t bw_iconv_fd; /* descriptor for iconv() or -1 */ | |
109 # endif | |
110 #endif | |
111 }; | |
112 | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
113 static int buf_write_bytes(struct bw_info *ip); |
7 | 114 |
115 #ifdef FEAT_MBYTE | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
116 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
|
117 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
|
118 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
|
119 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
|
120 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
|
121 static int make_bom(char_u *buf, char_u *name); |
7 | 122 # ifdef WIN3264 |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
123 static int get_win_fio_flags(char_u *ptr); |
7 | 124 # endif |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
125 # ifdef MACOS_CONVERT |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
126 static int get_mac_fio_flags(char_u *ptr); |
7 | 127 # endif |
128 #endif | |
1834 | 129 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
|
130 |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
131 /* |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
132 * 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
|
133 * 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
|
134 * EVENT_BUFREADPOST triggered the EVENT_FILETYPE. |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
135 * |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
136 * 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
|
137 * apply_autocmds_group. |
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 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
|
140 |
7 | 141 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
142 filemess( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
143 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
144 char_u *name, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
145 char_u *s, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
146 int attr) |
7 | 147 { |
148 int msg_scroll_save; | |
149 | |
150 if (msg_silent != 0) | |
151 return; | |
152 msg_add_fname(buf, name); /* put file name in IObuff with quotes */ | |
153 /* If it's extremely long, truncate it. */ | |
154 if (STRLEN(IObuff) > IOSIZE - 80) | |
155 IObuff[IOSIZE - 80] = NUL; | |
156 STRCAT(IObuff, s); | |
157 /* | |
158 * For the first message may have to start a new line. | |
159 * For further ones overwrite the previous one, reset msg_scroll before | |
160 * calling filemess(). | |
161 */ | |
162 msg_scroll_save = msg_scroll; | |
163 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) | |
164 msg_scroll = FALSE; | |
165 if (!msg_scroll) /* wait a bit when overwriting an error msg */ | |
166 check_for_delay(FALSE); | |
167 msg_start(); | |
168 msg_scroll = msg_scroll_save; | |
169 msg_scrolled_ign = TRUE; | |
170 /* may truncate the message to avoid a hit-return prompt */ | |
171 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr); | |
172 msg_clr_eos(); | |
173 out_flush(); | |
174 msg_scrolled_ign = FALSE; | |
175 } | |
176 | |
177 /* | |
178 * Read lines from file "fname" into the buffer after line "from". | |
179 * | |
180 * 1. We allocate blocks with lalloc, as big as possible. | |
181 * 2. Each block is filled with characters from the file with a single read(). | |
182 * 3. The lines are inserted in the buffer with ml_append(). | |
183 * | |
184 * (caller must check that fname != NULL, unless READ_STDIN is used) | |
185 * | |
186 * "lines_to_skip" is the number of lines that must be skipped | |
187 * "lines_to_read" is the number of lines that are appended | |
188 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM. | |
189 * | |
190 * flags: | |
191 * READ_NEW starting to edit a new buffer | |
192 * READ_FILTER reading filter output | |
193 * READ_STDIN read from stdin instead of a file | |
194 * READ_BUFFER read from curbuf instead of a file (converting after reading | |
195 * stdin) | |
196 * 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
|
197 * 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
|
198 * READ_FIFO read from fifo/socket instead of a file |
7 | 199 * |
10575
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
200 * return FAIL for failure, NOTDONE for directory (failure), or OK |
7 | 201 */ |
202 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
203 readfile( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
204 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
205 char_u *sfname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
206 linenr_T from, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
207 linenr_T lines_to_skip, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
208 linenr_T lines_to_read, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
209 exarg_T *eap, /* can be NULL! */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
210 int flags) |
7 | 211 { |
212 int fd = 0; | |
213 int newfile = (flags & READ_NEW); | |
214 int check_readonly; | |
215 int filtering = (flags & READ_FILTER); | |
216 int read_stdin = (flags & READ_STDIN); | |
217 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
|
218 int read_fifo = (flags & READ_FIFO); |
1486 | 219 int set_options = newfile || read_buffer |
220 || (eap != NULL && eap->read_edit); | |
7 | 221 linenr_T read_buf_lnum = 1; /* next line to read from curbuf */ |
222 colnr_T read_buf_col = 0; /* next char to read from this line */ | |
223 char_u c; | |
224 linenr_T lnum = from; | |
225 char_u *ptr = NULL; /* pointer into read buffer */ | |
226 char_u *buffer = NULL; /* read buffer */ | |
227 char_u *new_buffer = NULL; /* init to shut up gcc */ | |
228 char_u *line_start = NULL; /* init to shut up gcc */ | |
229 int wasempty; /* buffer was empty before reading */ | |
230 colnr_T len; | |
231 long size = 0; | |
232 char_u *p; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
233 off_T filesize = 0; |
7 | 234 int skip_read = FALSE; |
235 #ifdef FEAT_CRYPT | |
236 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
|
237 int did_ask_for_key = FALSE; |
7 | 238 #endif |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
239 #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
|
240 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
|
241 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
|
242 #endif |
7 | 243 int split = 0; /* number of split lines */ |
244 #define UNKNOWN 0x0fffffff /* file size is unknown */ | |
245 linenr_T linecnt; | |
246 int error = FALSE; /* errors encountered */ | |
247 int ff_error = EOL_UNKNOWN; /* file format with errors */ | |
248 long linerest = 0; /* remaining chars in line */ | |
249 #ifdef UNIX | |
250 int perm = 0; | |
251 int swap_mode = -1; /* protection bits for swap file */ | |
252 #else | |
253 int perm; | |
254 #endif | |
255 int fileformat = 0; /* end-of-line format */ | |
256 int keep_fileformat = FALSE; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
257 stat_T st; |
7 | 258 int file_readonly; |
259 linenr_T skip_count = 0; | |
260 linenr_T read_count = 0; | |
261 int msg_save = msg_scroll; | |
262 linenr_T read_no_eol_lnum = 0; /* non-zero lnum when last line of | |
263 * 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
|
264 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
|
265 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
|
266 int try_unix; |
7 | 267 int file_rewind = FALSE; |
268 #ifdef FEAT_MBYTE | |
269 int can_retry; | |
595 | 270 linenr_T conv_error = 0; /* line nr with conversion error */ |
271 linenr_T illegal_byte = 0; /* line nr with illegal byte */ | |
7 | 272 int keep_dest_enc = FALSE; /* don't retry when char doesn't fit |
273 in destination encoding */ | |
595 | 274 int bad_char_behavior = BAD_REPLACE; |
275 /* BAD_KEEP, BAD_DROP or character to | |
276 * replace with */ | |
7 | 277 char_u *tmpname = NULL; /* name of 'charconvert' output file */ |
278 int fio_flags = 0; | |
279 char_u *fenc; /* fileencoding to use */ | |
280 int fenc_alloced; /* fenc_next is in allocated memory */ | |
281 char_u *fenc_next = NULL; /* next item in 'fencs' or NULL */ | |
282 int advance_fenc = FALSE; | |
283 long real_size = 0; | |
284 # ifdef USE_ICONV | |
285 iconv_t iconv_fd = (iconv_t)-1; /* descriptor for iconv() or -1 */ | |
286 # ifdef FEAT_EVAL | |
287 int did_iconv = FALSE; /* TRUE when iconv() failed and trying | |
288 'charconvert' next */ | |
289 # endif | |
290 # endif | |
291 int converted = FALSE; /* TRUE if conversion done */ | |
292 int notconverted = FALSE; /* TRUE if conversion wanted but it | |
293 wasn't possible */ | |
294 char_u conv_rest[CONV_RESTLEN]; | |
295 int conv_restlen = 0; /* nr of bytes in conv_rest[] */ | |
296 #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
|
297 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
|
298 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
|
299 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
|
300 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
|
301 int using_b_fname; |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
302 |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
303 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
|
304 |
2707 | 305 curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */ |
7 | 306 |
307 /* | |
308 * If there is no file name yet, use the one for the read file. | |
309 * BF_NOTEDITED is set to reflect this. | |
310 * Don't do this for a read from a filter. | |
311 * Only do this when 'cpoptions' contains the 'f' flag. | |
312 */ | |
313 if (curbuf->b_ffname == NULL | |
314 && !filtering | |
315 && fname != NULL | |
316 && vim_strchr(p_cpo, CPO_FNAMER) != NULL | |
317 && !(flags & READ_DUMMY)) | |
318 { | |
633 | 319 if (set_rw_fname(fname, sfname) == FAIL) |
320 return FAIL; | |
7 | 321 } |
322 | |
2563
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
323 /* 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
|
324 * 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
|
325 * 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
|
326 * 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
|
327 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
|
328 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
|
329 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
|
330 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
|
331 || (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
|
332 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
|
333 |
167 | 334 /* After reading a file the cursor line changes but we don't want to |
335 * display the line. */ | |
336 ex_no_reprint = TRUE; | |
337 | |
968 | 338 /* don't display the file info for another buffer now */ |
339 need_fileinfo = FALSE; | |
340 | |
7 | 341 /* |
342 * For Unix: Use the short file name whenever possible. | |
343 * Avoids problems with networks and when directory names are changed. | |
344 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to | |
345 * another directory, which we don't detect. | |
346 */ | |
347 if (sfname == NULL) | |
348 sfname = fname; | |
9536
b2aada04d84e
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Christian Brabandt <cb@256bit.org>
parents:
9509
diff
changeset
|
349 #if defined(UNIX) |
7 | 350 fname = sfname; |
351 #endif | |
352 | |
353 /* | |
354 * The BufReadCmd and FileReadCmd events intercept the reading process by | |
355 * executing the associated commands instead. | |
356 */ | |
357 if (!filtering && !read_stdin && !read_buffer) | |
358 { | |
359 pos_T pos; | |
360 | |
361 pos = curbuf->b_op_start; | |
362 | |
363 /* Set '[ mark to the line above where the lines go (line 1 if zero). */ | |
364 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); | |
365 curbuf->b_op_start.col = 0; | |
366 | |
367 if (newfile) | |
368 { | |
369 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname, | |
370 FALSE, curbuf, eap)) | |
371 #ifdef FEAT_EVAL | |
372 return aborting() ? FAIL : OK; | |
373 #else | |
374 return OK; | |
375 #endif | |
376 } | |
377 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname, | |
378 FALSE, NULL, eap)) | |
379 #ifdef FEAT_EVAL | |
380 return aborting() ? FAIL : OK; | |
381 #else | |
382 return OK; | |
383 #endif | |
384 | |
385 curbuf->b_op_start = pos; | |
386 } | |
387 | |
388 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0) | |
389 msg_scroll = FALSE; /* overwrite previous file message */ | |
390 else | |
391 msg_scroll = TRUE; /* don't overwrite previous file message */ | |
392 | |
393 /* | |
394 * If the name ends in a path separator, we can't open it. Check here, | |
395 * because reading the file may actually work, but then creating the swap | |
396 * file may destroy it! Reported on MS-DOS and Win 95. | |
397 * If the name is too long we might crash further on, quit here. | |
398 */ | |
23 | 399 if (fname != NULL && *fname != NUL) |
400 { | |
39 | 401 p = fname + STRLEN(fname); |
402 if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL) | |
23 | 403 { |
404 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0); | |
405 msg_end(); | |
406 msg_scroll = msg_save; | |
407 return FAIL; | |
408 } | |
7 | 409 } |
410 | |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
411 if (!read_stdin && !read_buffer && !read_fifo) |
5322 | 412 { |
7 | 413 #ifdef UNIX |
5322 | 414 /* |
415 * On Unix it is possible to read a directory, so we have to | |
416 * check for it before the mch_open(). | |
417 */ | |
7 | 418 perm = mch_getperm(fname); |
419 if (perm >= 0 && !S_ISREG(perm) /* not a regular file ... */ | |
420 && !S_ISFIFO(perm) /* ... or fifo */ | |
421 && !S_ISSOCK(perm) /* ... or socket */ | |
1313 | 422 # ifdef OPEN_CHR_FILES |
423 && !(S_ISCHR(perm) && is_dev_fd_file(fname)) | |
424 /* ... or a character special file named /dev/fd/<n> */ | |
425 # endif | |
7 | 426 ) |
427 { | |
10575
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
428 int retval = FAIL; |
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
429 |
7 | 430 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
|
431 { |
7 | 432 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
|
433 retval = NOTDONE; |
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
434 } |
7 | 435 else |
436 filemess(curbuf, fname, (char_u *)_("is not a file"), 0); | |
437 msg_end(); | |
438 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
|
439 return retval; |
7 | 440 } |
5322 | 441 #endif |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
442 #if defined(MSWIN) |
1006 | 443 /* |
444 * MS-Windows allows opening a device, but we will probably get stuck | |
445 * trying to read it. | |
446 */ | |
447 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE) | |
448 { | |
1303 | 449 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0); |
1006 | 450 msg_end(); |
451 msg_scroll = msg_save; | |
452 return FAIL; | |
453 } | |
5322 | 454 #endif |
455 } | |
1004 | 456 |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
457 /* Set default or forced 'fileformat' and 'binary'. */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
458 set_file_options(set_options, eap); |
7 | 459 |
460 /* | |
461 * When opening a new file we take the readonly flag from the file. | |
462 * Default is r/w, can be set to r/o below. | |
463 * Don't reset it when in readonly mode | |
464 * Only set/reset b_p_ro when BF_CHECK_RO is set. | |
465 */ | |
466 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO)); | |
164 | 467 if (check_readonly && !readonlymode) |
7 | 468 curbuf->b_p_ro = FALSE; |
469 | |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
470 if (newfile && !read_stdin && !read_buffer && !read_fifo) |
7 | 471 { |
2823 | 472 /* Remember time of file. */ |
7 | 473 if (mch_stat((char *)fname, &st) >= 0) |
474 { | |
475 buf_store_time(curbuf, &st, fname); | |
476 curbuf->b_mtime_read = curbuf->b_mtime; | |
477 #ifdef UNIX | |
478 /* | |
479 * Use the protection bits of the original file for the swap file. | |
480 * This makes it possible for others to read the name of the | |
481 * edited file from the swapfile, but only if they can read the | |
482 * edited file. | |
483 * Remove the "write" and "execute" bits for group and others | |
484 * (they must not write the swapfile). | |
485 * Add the "read" and "write" bits for the user, otherwise we may | |
486 * not be able to write to the file ourselves. | |
487 * Setting the bits is done below, after creating the swap file. | |
488 */ | |
489 swap_mode = (st.st_mode & 0644) | 0600; | |
490 #endif | |
491 #ifdef FEAT_CW_EDITOR | |
492 /* Get the FSSpec on MacOS | |
493 * TODO: Update it properly when the buffer name changes | |
494 */ | |
495 (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec); | |
496 #endif | |
497 #ifdef VMS | |
498 curbuf->b_fab_rfm = st.st_fab_rfm; | |
22 | 499 curbuf->b_fab_rat = st.st_fab_rat; |
500 curbuf->b_fab_mrs = st.st_fab_mrs; | |
7 | 501 #endif |
502 } | |
503 else | |
504 { | |
505 curbuf->b_mtime = 0; | |
506 curbuf->b_mtime_read = 0; | |
507 curbuf->b_orig_size = 0; | |
508 curbuf->b_orig_mode = 0; | |
509 } | |
510 | |
511 /* Reset the "new file" flag. It will be set again below when the | |
512 * file doesn't exist. */ | |
513 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W); | |
514 } | |
515 | |
516 /* | |
517 * 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
|
518 * for Amiga: check readonly by trying to open the file for writing |
7 | 519 */ |
520 file_readonly = FALSE; | |
521 if (read_stdin) | |
522 { | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
523 #if defined(MSWIN) |
7 | 524 /* Force binary I/O on stdin to avoid CR-LF -> LF conversion. */ |
525 setmode(0, O_BINARY); | |
526 #endif | |
527 } | |
528 else if (!read_buffer) | |
529 { | |
530 #ifdef USE_MCH_ACCESS | |
531 if ( | |
532 # ifdef UNIX | |
533 !(perm & 0222) || | |
534 # endif | |
535 mch_access((char *)fname, W_OK)) | |
536 file_readonly = TRUE; | |
537 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
538 #else | |
539 if (!newfile | |
540 || readonlymode | |
541 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0) | |
542 { | |
543 file_readonly = TRUE; | |
544 /* try to open ro */ | |
545 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
546 } | |
547 #endif | |
548 } | |
549 | |
550 if (fd < 0) /* cannot open at all */ | |
551 { | |
552 #ifndef UNIX | |
553 int isdir_f; | |
554 #endif | |
555 msg_scroll = msg_save; | |
556 #ifndef UNIX | |
557 /* | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
558 * On Amiga we can't open a directory, check here. |
7 | 559 */ |
560 isdir_f = (mch_isdir(fname)); | |
561 perm = mch_getperm(fname); /* check if the file exists */ | |
562 if (isdir_f) | |
563 { | |
564 filemess(curbuf, sfname, (char_u *)_("is a directory"), 0); | |
565 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
566 } | |
567 else | |
568 #endif | |
569 if (newfile) | |
570 { | |
2147
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
571 if (perm < 0 |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
572 #ifdef ENOENT |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
573 && errno == ENOENT |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
574 #endif |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
575 ) |
7 | 576 { |
577 /* | |
578 * Set the 'new-file' flag, so that when the file has | |
579 * been created by someone else, a ":w" will complain. | |
580 */ | |
581 curbuf->b_flags |= BF_NEW; | |
582 | |
583 /* Create a swap file now, so that other Vims are warned | |
584 * that we are editing this file. Don't do this for a | |
585 * "nofile" or "nowrite" buffer type. */ | |
586 #ifdef FEAT_QUICKFIX | |
587 if (!bt_dontwrite(curbuf)) | |
588 #endif | |
1834 | 589 { |
7 | 590 check_need_swap(newfile); |
1834 | 591 /* SwapExists autocommand may mess things up */ |
592 if (curbuf != old_curbuf | |
593 || (using_b_ffname | |
594 && (old_b_ffname != curbuf->b_ffname)) | |
595 || (using_b_fname | |
596 && (old_b_fname != curbuf->b_fname))) | |
597 { | |
598 EMSG(_(e_auchangedbuf)); | |
599 return FAIL; | |
600 } | |
601 } | |
592 | 602 if (dir_of_file_exists(fname)) |
603 filemess(curbuf, sfname, (char_u *)_("[New File]"), 0); | |
604 else | |
605 filemess(curbuf, sfname, | |
606 (char_u *)_("[New DIRECTORY]"), 0); | |
7 | 607 #ifdef FEAT_VIMINFO |
608 /* Even though this is a new file, it might have been | |
609 * edited before and deleted. Get the old marks. */ | |
610 check_marks_read(); | |
611 #endif | |
612 #ifdef FEAT_MBYTE | |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
613 /* Set forced 'fileencoding'. */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
614 if (eap != NULL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
615 set_forced_fenc(eap); |
7 | 616 #endif |
617 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname, | |
618 FALSE, curbuf, eap); | |
619 /* remember the current fileformat */ | |
620 save_file_ff(curbuf); | |
621 | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
622 #if defined(FEAT_EVAL) |
7 | 623 if (aborting()) /* autocmds may abort script processing */ |
624 return FAIL; | |
625 #endif | |
626 return OK; /* a new file is not an error */ | |
627 } | |
628 else | |
629 { | |
550 | 630 filemess(curbuf, sfname, (char_u *)( |
631 # ifdef EFBIG | |
632 (errno == EFBIG) ? _("[File too big]") : | |
633 # endif | |
2147
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
634 # ifdef EOVERFLOW |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
635 (errno == EOVERFLOW) ? _("[File too big]") : |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
636 # endif |
550 | 637 _("[Permission Denied]")), 0); |
7 | 638 curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
639 } | |
640 } | |
641 | |
642 return FAIL; | |
643 } | |
644 | |
645 /* | |
646 * Only set the 'ro' flag for readonly files the first time they are | |
647 * loaded. Help files always get readonly mode | |
648 */ | |
649 if ((check_readonly && file_readonly) || curbuf->b_help) | |
650 curbuf->b_p_ro = TRUE; | |
651 | |
819 | 652 if (set_options) |
7 | 653 { |
1486 | 654 /* Don't change 'eol' if reading from buffer as it will already be |
655 * correctly set when reading stdin. */ | |
656 if (!read_buffer) | |
657 { | |
658 curbuf->b_p_eol = TRUE; | |
659 curbuf->b_start_eol = TRUE; | |
660 } | |
7 | 661 #ifdef FEAT_MBYTE |
662 curbuf->b_p_bomb = FALSE; | |
1352 | 663 curbuf->b_start_bomb = FALSE; |
7 | 664 #endif |
665 } | |
666 | |
667 /* Create a swap file now, so that other Vims are warned that we are | |
668 * editing this file. | |
669 * Don't do this for a "nofile" or "nowrite" buffer type. */ | |
670 #ifdef FEAT_QUICKFIX | |
671 if (!bt_dontwrite(curbuf)) | |
672 #endif | |
673 { | |
674 check_need_swap(newfile); | |
1834 | 675 if (!read_stdin && (curbuf != old_curbuf |
676 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) | |
677 || (using_b_fname && (old_b_fname != curbuf->b_fname)))) | |
678 { | |
679 EMSG(_(e_auchangedbuf)); | |
680 if (!read_buffer) | |
681 close(fd); | |
682 return FAIL; | |
683 } | |
7 | 684 #ifdef UNIX |
685 /* Set swap file protection bits after creating it. */ | |
1918 | 686 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL |
687 && 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
|
688 { |
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
|
689 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
|
690 |
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
|
691 /* |
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
|
692 * 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
|
693 * 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
|
694 * 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
|
695 * 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
|
696 * 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
|
697 */ |
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
|
698 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
|
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 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
|
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 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
|
703 && 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
|
704 # 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
|
705 && 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
|
706 == -1 |
13730
7b818f7bb738
patch 8.0.1737: fchown() used when it is not supported
Christian Brabandt <cb@256bit.org>
parents:
13610
diff
changeset
|
707 # endif |
7b818f7bb738
patch 8.0.1737: fchown() used when it is not supported
Christian Brabandt <cb@256bit.org>
parents:
13610
diff
changeset
|
708 ) |
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
|
709 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
|
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 |
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 (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
|
713 } |
7 | 714 #endif |
715 } | |
716 | |
578 | 717 #if defined(HAS_SWAP_EXISTS_ACTION) |
7 | 718 /* If "Quit" selected at ATTENTION dialog, don't load the file */ |
719 if (swap_exists_action == SEA_QUIT) | |
720 { | |
721 if (!read_buffer && !read_stdin) | |
722 close(fd); | |
723 return FAIL; | |
724 } | |
725 #endif | |
726 | |
727 ++no_wait_return; /* don't wait for return yet */ | |
728 | |
729 /* | |
730 * Set '[ mark to the line above where the lines go (line 1 if zero). | |
731 */ | |
732 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); | |
733 curbuf->b_op_start.col = 0; | |
734 | |
10668
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
735 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
|
736 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
|
737 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
|
738 |
7 | 739 if (!read_buffer) |
740 { | |
741 int m = msg_scroll; | |
742 int n = msg_scrolled; | |
743 | |
744 /* | |
745 * The file must be closed again, the autocommands may want to change | |
746 * the file before reading it. | |
747 */ | |
748 if (!read_stdin) | |
749 close(fd); /* ignore errors */ | |
750 | |
751 /* | |
752 * The output from the autocommands should not overwrite anything and | |
753 * should not be overwritten: Set msg_scroll, restore its value if no | |
754 * output was done. | |
755 */ | |
756 msg_scroll = TRUE; | |
757 if (filtering) | |
758 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname, | |
759 FALSE, curbuf, eap); | |
760 else if (read_stdin) | |
761 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname, | |
762 FALSE, curbuf, eap); | |
763 else if (newfile) | |
764 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname, | |
765 FALSE, curbuf, eap); | |
766 else | |
767 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname, | |
768 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
|
769 /* 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
|
770 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
|
771 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
|
772 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
|
773 |
7 | 774 if (msg_scrolled == n) |
775 msg_scroll = m; | |
776 | |
777 #ifdef FEAT_EVAL | |
778 if (aborting()) /* autocmds may abort script processing */ | |
779 { | |
780 --no_wait_return; | |
781 msg_scroll = msg_save; | |
782 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
783 return FAIL; | |
784 } | |
785 #endif | |
786 /* | |
787 * Don't allow the autocommands to change the current buffer. | |
788 * Try to re-open the file. | |
1834 | 789 * |
790 * Don't allow the autocommands to change the buffer name either | |
791 * (cd for example) if it invalidates fname or sfname. | |
7 | 792 */ |
793 if (!read_stdin && (curbuf != old_curbuf | |
1834 | 794 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) |
795 || (using_b_fname && (old_b_fname != curbuf->b_fname)) | |
7 | 796 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0)) |
797 { | |
798 --no_wait_return; | |
799 msg_scroll = msg_save; | |
800 if (fd < 0) | |
801 EMSG(_("E200: *ReadPre autocommands made the file unreadable")); | |
802 else | |
803 EMSG(_("E201: *ReadPre autocommands must not change current buffer")); | |
804 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
805 return FAIL; | |
806 } | |
807 } | |
808 | |
809 /* Autocommands may add lines to the file, need to check if it is empty */ | |
810 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY); | |
811 | |
812 if (!recoverymode && !filtering && !(flags & READ_DUMMY)) | |
813 { | |
814 /* | |
815 * Show the user that we are busy reading the input. Sometimes this | |
816 * may take a while. When reading from stdin another program may | |
817 * still be running, don't move the cursor to the last line, unless | |
818 * always using the GUI. | |
819 */ | |
820 if (read_stdin) | |
821 { | |
12863
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
822 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
|
823 { |
7 | 824 #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
|
825 mch_msg(_("Vim: Reading from stdin...\n")); |
7 | 826 #endif |
827 #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
|
828 /* 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
|
829 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
|
830 { |
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
831 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
|
832 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
|
833 } |
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
834 #endif |
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
835 } |
7 | 836 } |
837 else if (!read_buffer) | |
838 filemess(curbuf, sfname, (char_u *)"", 0); | |
839 } | |
840 | |
841 msg_scroll = FALSE; /* overwrite the file message */ | |
842 | |
843 /* | |
844 * Set linecnt now, before the "retry" caused by a wrong guess for | |
845 * fileformat, and after the autocommands, which may change them. | |
846 */ | |
847 linecnt = curbuf->b_ml.ml_line_count; | |
848 | |
849 #ifdef FEAT_MBYTE | |
595 | 850 /* "++bad=" argument. */ |
851 if (eap != NULL && eap->bad_char != 0) | |
612 | 852 { |
595 | 853 bad_char_behavior = eap->bad_char; |
819 | 854 if (set_options) |
612 | 855 curbuf->b_bad_char = eap->bad_char; |
856 } | |
857 else | |
858 curbuf->b_bad_char = 0; | |
595 | 859 |
7 | 860 /* |
595 | 861 * Decide which 'encoding' to use or use first. |
7 | 862 */ |
863 if (eap != NULL && eap->force_enc != 0) | |
864 { | |
865 fenc = enc_canonize(eap->cmd + eap->force_enc); | |
866 fenc_alloced = TRUE; | |
595 | 867 keep_dest_enc = TRUE; |
7 | 868 } |
869 else if (curbuf->b_p_bin) | |
870 { | |
871 fenc = (char_u *)""; /* binary: don't convert */ | |
872 fenc_alloced = FALSE; | |
873 } | |
874 else if (curbuf->b_help) | |
875 { | |
876 char_u firstline[80]; | |
301 | 877 int fc; |
7 | 878 |
879 /* Help files are either utf-8 or latin1. Try utf-8 first, if this | |
880 * fails it must be latin1. | |
881 * Always do this when 'encoding' is "utf-8". Otherwise only do | |
882 * this when needed to avoid [converted] remarks all the time. | |
883 * It is needed when the first line contains non-ASCII characters. | |
884 * That is only in *.??x files. */ | |
885 fenc = (char_u *)"latin1"; | |
886 c = enc_utf8; | |
301 | 887 if (!c && !read_stdin) |
888 { | |
889 fc = fname[STRLEN(fname) - 1]; | |
890 if (TOLOWER_ASC(fc) == 'x') | |
891 { | |
892 /* Read the first line (and a bit more). Immediately rewind to | |
893 * the start of the file. If the read() fails "len" is -1. */ | |
2664 | 894 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
|
895 vim_lseek(fd, (off_T)0L, SEEK_SET); |
301 | 896 for (p = firstline; p < firstline + len; ++p) |
897 if (*p >= 0x80) | |
898 { | |
899 c = TRUE; | |
900 break; | |
901 } | |
902 } | |
7 | 903 } |
904 | |
905 if (c) | |
906 { | |
907 fenc_next = fenc; | |
908 fenc = (char_u *)"utf-8"; | |
909 | |
910 /* When the file is utf-8 but a character doesn't fit in | |
911 * 'encoding' don't retry. In help text editing utf-8 bytes | |
912 * doesn't make sense. */ | |
844 | 913 if (!enc_utf8) |
914 keep_dest_enc = TRUE; | |
7 | 915 } |
916 fenc_alloced = FALSE; | |
917 } | |
918 else if (*p_fencs == NUL) | |
919 { | |
920 fenc = curbuf->b_p_fenc; /* use format from buffer */ | |
921 fenc_alloced = FALSE; | |
922 } | |
923 else | |
924 { | |
925 fenc_next = p_fencs; /* try items in 'fileencodings' */ | |
926 fenc = next_fenc(&fenc_next); | |
927 fenc_alloced = TRUE; | |
928 } | |
929 #endif | |
930 | |
931 /* | |
932 * Jump back here to retry reading the file in different ways. | |
933 * Reasons to retry: | |
934 * - encoding conversion failed: try another one from "fenc_next" | |
935 * - BOM detected and fenc was set, need to setup conversion | |
936 * - "fileformat" check failed: try another | |
937 * | |
938 * Variables set for special retry actions: | |
939 * "file_rewind" Rewind the file to start reading it again. | |
940 * "advance_fenc" Advance "fenc" using "fenc_next". | |
941 * "skip_read" Re-use already read bytes (BOM detected). | |
942 * "did_iconv" iconv() conversion failed, try 'charconvert'. | |
943 * "keep_fileformat" Don't reset "fileformat". | |
944 * | |
945 * Other status indicators: | |
946 * "tmpname" When != NULL did conversion with 'charconvert'. | |
947 * Output file has to be deleted afterwards. | |
948 * "iconv_fd" When != -1 did conversion with iconv(). | |
949 */ | |
950 retry: | |
951 | |
952 if (file_rewind) | |
953 { | |
954 if (read_buffer) | |
955 { | |
956 read_buf_lnum = 1; | |
957 read_buf_col = 0; | |
958 } | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
959 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0) |
7 | 960 { |
961 /* Can't rewind the file, give up. */ | |
962 error = TRUE; | |
963 goto failed; | |
964 } | |
965 /* Delete the previously read lines. */ | |
966 while (lnum > from) | |
967 ml_delete(lnum--, FALSE); | |
968 file_rewind = FALSE; | |
969 #ifdef FEAT_MBYTE | |
819 | 970 if (set_options) |
1352 | 971 { |
7 | 972 curbuf->b_p_bomb = FALSE; |
1352 | 973 curbuf->b_start_bomb = FALSE; |
974 } | |
595 | 975 conv_error = 0; |
7 | 976 #endif |
977 } | |
978 | |
979 /* | |
980 * When retrying with another "fenc" and the first time "fileformat" | |
981 * will be reset. | |
982 */ | |
983 if (keep_fileformat) | |
984 keep_fileformat = FALSE; | |
985 else | |
986 { | |
987 if (eap != NULL && eap->force_ff != 0) | |
1742 | 988 { |
7 | 989 fileformat = get_fileformat_force(curbuf, eap); |
1742 | 990 try_unix = try_dos = try_mac = FALSE; |
991 } | |
7 | 992 else if (curbuf->b_p_bin) |
993 fileformat = EOL_UNIX; /* binary: use Unix format */ | |
994 else if (*p_ffs == NUL) | |
995 fileformat = get_fileformat(curbuf);/* use format from buffer */ | |
996 else | |
997 fileformat = EOL_UNKNOWN; /* detect from file */ | |
998 } | |
999 | |
1000 #ifdef FEAT_MBYTE | |
1001 # ifdef USE_ICONV | |
1002 if (iconv_fd != (iconv_t)-1) | |
1003 { | |
1004 /* aborted conversion with iconv(), close the descriptor */ | |
1005 iconv_close(iconv_fd); | |
1006 iconv_fd = (iconv_t)-1; | |
1007 } | |
1008 # endif | |
1009 | |
1010 if (advance_fenc) | |
1011 { | |
1012 /* | |
1013 * Try the next entry in 'fileencodings'. | |
1014 */ | |
1015 advance_fenc = FALSE; | |
1016 | |
1017 if (eap != NULL && eap->force_enc != 0) | |
1018 { | |
1019 /* Conversion given with "++cc=" wasn't possible, read | |
1020 * without conversion. */ | |
1021 notconverted = TRUE; | |
595 | 1022 conv_error = 0; |
7 | 1023 if (fenc_alloced) |
1024 vim_free(fenc); | |
1025 fenc = (char_u *)""; | |
1026 fenc_alloced = FALSE; | |
1027 } | |
1028 else | |
1029 { | |
1030 if (fenc_alloced) | |
1031 vim_free(fenc); | |
1032 if (fenc_next != NULL) | |
1033 { | |
1034 fenc = next_fenc(&fenc_next); | |
1035 fenc_alloced = (fenc_next != NULL); | |
1036 } | |
1037 else | |
1038 { | |
1039 fenc = (char_u *)""; | |
1040 fenc_alloced = FALSE; | |
1041 } | |
1042 } | |
1043 if (tmpname != NULL) | |
1044 { | |
1045 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
|
1046 VIM_CLEAR(tmpname); |
7 | 1047 } |
1048 } | |
1049 | |
1050 /* | |
1948 | 1051 * Conversion may be required when the encoding of the file is different |
1052 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4. | |
7 | 1053 */ |
1054 fio_flags = 0; | |
1948 | 1055 converted = need_conversion(fenc); |
1056 if (converted) | |
7 | 1057 { |
1058 | |
1059 /* "ucs-bom" means we need to check the first bytes of the file | |
1060 * for a BOM. */ | |
1061 if (STRCMP(fenc, ENC_UCSBOM) == 0) | |
1062 fio_flags = FIO_UCSBOM; | |
1063 | |
1064 /* | |
1065 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be | |
1066 * done. This is handled below after read(). Prepare the | |
1067 * fio_flags to avoid having to parse the string each time. | |
1068 * Also check for Unicode to Latin1 conversion, because iconv() | |
1069 * appears not to handle this correctly. This works just like | |
1070 * conversion to UTF-8 except how the resulting character is put in | |
1071 * the buffer. | |
1072 */ | |
1073 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
1074 fio_flags = get_fio_flags(fenc); | |
1075 | |
1076 # ifdef WIN3264 | |
1077 /* | |
1078 * Conversion from an MS-Windows codepage to UTF-8 or another codepage | |
1079 * is handled with MultiByteToWideChar(). | |
1080 */ | |
1081 if (fio_flags == 0) | |
1082 fio_flags = get_win_fio_flags(fenc); | |
1083 # endif | |
1084 | |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
1085 # ifdef MACOS_CONVERT |
7 | 1086 /* Conversion from Apple MacRoman to latin1 or UTF-8 */ |
1087 if (fio_flags == 0) | |
1088 fio_flags = get_mac_fio_flags(fenc); | |
1089 # endif | |
1090 | |
1091 # ifdef USE_ICONV | |
1092 /* | |
1093 * Try using iconv() if we can't convert internally. | |
1094 */ | |
1095 if (fio_flags == 0 | |
1096 # ifdef FEAT_EVAL | |
1097 && !did_iconv | |
1098 # endif | |
1099 ) | |
1100 iconv_fd = (iconv_t)my_iconv_open( | |
1101 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc); | |
1102 # endif | |
1103 | |
1104 # ifdef FEAT_EVAL | |
1105 /* | |
1106 * Use the 'charconvert' expression when conversion is required | |
1107 * and we can't do it internally or with iconv(). | |
1108 */ | |
1109 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
|
1110 && !read_fifo |
7 | 1111 # ifdef USE_ICONV |
1112 && iconv_fd == (iconv_t)-1 | |
1113 # endif | |
1114 ) | |
1115 { | |
1116 # ifdef USE_ICONV | |
1117 did_iconv = FALSE; | |
1118 # endif | |
1119 /* Skip conversion when it's already done (retry for wrong | |
1120 * "fileformat"). */ | |
1121 if (tmpname == NULL) | |
1122 { | |
1123 tmpname = readfile_charconvert(fname, fenc, &fd); | |
1124 if (tmpname == NULL) | |
1125 { | |
1126 /* Conversion failed. Try another one. */ | |
1127 advance_fenc = TRUE; | |
1128 if (fd < 0) | |
1129 { | |
1130 /* Re-opening the original file failed! */ | |
1131 EMSG(_("E202: Conversion made file unreadable!")); | |
1132 error = TRUE; | |
1133 goto failed; | |
1134 } | |
1135 goto retry; | |
1136 } | |
1137 } | |
1138 } | |
1139 else | |
1140 # endif | |
1141 { | |
1142 if (fio_flags == 0 | |
1143 # ifdef USE_ICONV | |
1144 && iconv_fd == (iconv_t)-1 | |
1145 # endif | |
1146 ) | |
1147 { | |
1148 /* Conversion wanted but we can't. | |
1149 * Try the next conversion in 'fileencodings' */ | |
1150 advance_fenc = TRUE; | |
1151 goto retry; | |
1152 } | |
1153 } | |
1154 } | |
1155 | |
595 | 1156 /* Set "can_retry" when it's possible to rewind the file and try with |
7 | 1157 * another "fenc" value. It's FALSE when no other "fenc" to try, reading |
595 | 1158 * 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
|
1159 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc); |
7 | 1160 #endif |
1161 | |
1162 if (!skip_read) | |
1163 { | |
1164 linerest = 0; | |
1165 filesize = 0; | |
1166 skip_count = lines_to_skip; | |
1167 read_count = lines_to_read; | |
1168 #ifdef FEAT_MBYTE | |
1169 conv_restlen = 0; | |
1170 #endif | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1171 #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
|
1172 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
|
1173 && 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
|
1174 && 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
|
1175 && !filtering |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
1176 && !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
|
1177 && !read_stdin |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1178 && !read_buffer); |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1179 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
|
1180 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
|
1181 #endif |
6122 | 1182 #ifdef FEAT_CRYPT |
1183 if (curbuf->b_cryptstate != NULL) | |
1184 { | |
1185 /* Need to free the state, but keep the key, don't want to ask for | |
1186 * it again. */ | |
1187 crypt_free_state(curbuf->b_cryptstate); | |
1188 curbuf->b_cryptstate = NULL; | |
1189 } | |
1190 #endif | |
7 | 1191 } |
1192 | |
1193 while (!error && !got_int) | |
1194 { | |
1195 /* | |
1196 * We allocate as much space for the file as we can get, plus | |
1197 * space for the old line plus room for one terminating NUL. | |
1198 * The amount is limited by the fact that read() only can read | |
1199 * upto max_unsigned characters (and other things). | |
1200 */ | |
13804
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1201 if (!skip_read) |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1202 { |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1203 #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
|
1204 # 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
|
1205 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
|
1206 # else |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1207 /* 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
|
1208 * 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
|
1209 * 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
|
1210 */ |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1211 size = 0x10000L + linerest; |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1212 if (size > 0x100000L) |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1213 size = 0x100000L; |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1214 # endif |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1215 #else |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1216 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
|
1217 #endif |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1218 } |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1219 |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1220 /* 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
|
1221 if ( |
5684 | 1222 #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
|
1223 linerest >= 0x7ff0 |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1224 #else |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1225 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
|
1226 #endif |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1227 ) |
7 | 1228 { |
1229 ++split; | |
1230 *ptr = NL; /* split line by inserting a NL */ | |
1231 size = 1; | |
1232 } | |
1233 else | |
1234 { | |
1235 if (!skip_read) | |
1236 { | |
836 | 1237 for ( ; size >= 10; size = (long)((long_u)size >> 1)) |
7 | 1238 { |
1239 if ((new_buffer = lalloc((long_u)(size + linerest + 1), | |
1240 FALSE)) != NULL) | |
1241 break; | |
1242 } | |
1243 if (new_buffer == NULL) | |
1244 { | |
1245 do_outofmem_msg((long_u)(size * 2 + linerest + 1)); | |
1246 error = TRUE; | |
1247 break; | |
1248 } | |
1249 if (linerest) /* copy characters from the previous buffer */ | |
1250 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest); | |
1251 vim_free(buffer); | |
1252 buffer = new_buffer; | |
1253 ptr = buffer + linerest; | |
1254 line_start = buffer; | |
1255 | |
1256 #ifdef FEAT_MBYTE | |
1257 /* May need room to translate into. | |
1258 * For iconv() we don't really know the required space, use a | |
1259 * factor ICONV_MULT. | |
1260 * latin1 to utf-8: 1 byte becomes up to 2 bytes | |
1261 * utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes | |
1262 * become up to 4 bytes, size must be multiple of 2 | |
1263 * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be | |
1264 * multiple of 2 | |
1265 * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be | |
1266 * multiple of 4 */ | |
835 | 1267 real_size = (int)size; |
7 | 1268 # ifdef USE_ICONV |
1269 if (iconv_fd != (iconv_t)-1) | |
1270 size = size / ICONV_MULT; | |
1271 else | |
1272 # endif | |
1273 if (fio_flags & FIO_LATIN1) | |
1274 size = size / 2; | |
1275 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1276 size = (size * 2 / 3) & ~1; | |
1277 else if (fio_flags & FIO_UCS4) | |
1278 size = (size * 2 / 3) & ~3; | |
1279 else if (fio_flags == FIO_UCSBOM) | |
1280 size = size / ICONV_MULT; /* worst case */ | |
1281 # ifdef WIN3264 | |
1282 else if (fio_flags & FIO_CODEPAGE) | |
1283 size = size / ICONV_MULT; /* also worst case */ | |
1284 # endif | |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
1285 # ifdef MACOS_CONVERT |
7 | 1286 else if (fio_flags & FIO_MACROMAN) |
1287 size = size / ICONV_MULT; /* also worst case */ | |
1288 # endif | |
1289 #endif | |
1290 | |
1291 #ifdef FEAT_MBYTE | |
1292 if (conv_restlen > 0) | |
1293 { | |
1294 /* Insert unconverted bytes from previous line. */ | |
1295 mch_memmove(ptr, conv_rest, conv_restlen); | |
1296 ptr += conv_restlen; | |
1297 size -= conv_restlen; | |
1298 } | |
1299 #endif | |
1300 | |
1301 if (read_buffer) | |
1302 { | |
1303 /* | |
1304 * Read bytes from curbuf. Used for converting text read | |
1305 * from stdin. | |
1306 */ | |
1307 if (read_buf_lnum > from) | |
1308 size = 0; | |
1309 else | |
1310 { | |
1311 int n, ni; | |
1312 long tlen; | |
1313 | |
1314 tlen = 0; | |
1315 for (;;) | |
1316 { | |
1317 p = ml_get(read_buf_lnum) + read_buf_col; | |
1318 n = (int)STRLEN(p); | |
1319 if ((int)tlen + n + 1 > size) | |
1320 { | |
1321 /* Filled up to "size", append partial line. | |
1322 * Change NL to NUL to reverse the effect done | |
1323 * below. */ | |
835 | 1324 n = (int)(size - tlen); |
7 | 1325 for (ni = 0; ni < n; ++ni) |
1326 { | |
1327 if (p[ni] == NL) | |
1328 ptr[tlen++] = NUL; | |
1329 else | |
1330 ptr[tlen++] = p[ni]; | |
1331 } | |
1332 read_buf_col += n; | |
1333 break; | |
1334 } | |
1335 else | |
1336 { | |
1337 /* Append whole line and new-line. Change NL | |
1338 * to NUL to reverse the effect done below. */ | |
1339 for (ni = 0; ni < n; ++ni) | |
1340 { | |
1341 if (p[ni] == NL) | |
1342 ptr[tlen++] = NUL; | |
1343 else | |
1344 ptr[tlen++] = p[ni]; | |
1345 } | |
1346 ptr[tlen++] = NL; | |
1347 read_buf_col = 0; | |
1348 if (++read_buf_lnum > from) | |
1349 { | |
1350 /* When the last line didn't have an | |
1351 * end-of-line don't add it now either. */ | |
1352 if (!curbuf->b_p_eol) | |
1353 --tlen; | |
1354 size = tlen; | |
1355 break; | |
1356 } | |
1357 } | |
1358 } | |
1359 } | |
1360 } | |
1361 else | |
1362 { | |
1363 /* | |
1364 * Read bytes from the file. | |
1365 */ | |
2664 | 1366 size = read_eintr(fd, ptr, size); |
7 | 1367 } |
1368 | |
6122 | 1369 #ifdef FEAT_CRYPT |
1370 /* | |
1371 * At start of file: Check for magic number of encryption. | |
1372 */ | |
1373 if (filesize == 0 && size > 0) | |
1374 cryptkey = check_for_cryptkey(cryptkey, ptr, &size, | |
1375 &filesize, newfile, sfname, | |
1376 &did_ask_for_key); | |
1377 /* | |
1378 * Decrypt the read bytes. This is done before checking for | |
1379 * EOF because the crypt layer may be buffering. | |
1380 */ | |
12216
e971ef6c0dee
patch 8.0.0988: warning from Covscan about using NULL pointer
Christian Brabandt <cb@256bit.org>
parents:
12064
diff
changeset
|
1381 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
|
1382 && size > 0) |
6122 | 1383 { |
1384 if (crypt_works_inplace(curbuf->b_cryptstate)) | |
1385 { | |
1386 crypt_decode_inplace(curbuf->b_cryptstate, ptr, size); | |
1387 } | |
1388 else | |
1389 { | |
1390 char_u *newptr = NULL; | |
1391 int decrypted_size; | |
1392 | |
1393 decrypted_size = crypt_decode_alloc( | |
1394 curbuf->b_cryptstate, ptr, size, &newptr); | |
1395 | |
1396 /* If the crypt layer is buffering, not producing | |
1397 * 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
|
1398 if (decrypted_size == 0) |
6122 | 1399 continue; |
1400 | |
1401 if (linerest == 0) | |
1402 { | |
1403 /* Simple case: reuse returned buffer (may be | |
1404 * NULL, checked later). */ | |
1405 new_buffer = newptr; | |
1406 } | |
1407 else | |
1408 { | |
1409 long_u new_size; | |
1410 | |
1411 /* Need new buffer to add bytes carried over. */ | |
1412 new_size = (long_u)(decrypted_size + linerest + 1); | |
1413 new_buffer = lalloc(new_size, FALSE); | |
1414 if (new_buffer == NULL) | |
1415 { | |
1416 do_outofmem_msg(new_size); | |
1417 error = TRUE; | |
1418 break; | |
1419 } | |
1420 | |
1421 mch_memmove(new_buffer, buffer, linerest); | |
1422 if (newptr != NULL) | |
1423 mch_memmove(new_buffer + linerest, newptr, | |
1424 decrypted_size); | |
1425 } | |
1426 | |
1427 if (new_buffer != NULL) | |
1428 { | |
1429 vim_free(buffer); | |
1430 buffer = new_buffer; | |
1431 new_buffer = NULL; | |
1432 line_start = buffer; | |
1433 ptr = buffer + linerest; | |
1434 } | |
1435 size = decrypted_size; | |
1436 } | |
1437 } | |
1438 #endif | |
1439 | |
7 | 1440 if (size <= 0) |
1441 { | |
1442 if (size < 0) /* read error */ | |
1443 error = TRUE; | |
1444 #ifdef FEAT_MBYTE | |
1445 else if (conv_restlen > 0) | |
595 | 1446 { |
1597 | 1447 /* |
1448 * Reached end-of-file but some trailing bytes could | |
1449 * not be converted. Truncated file? | |
1450 */ | |
1451 | |
1452 /* When we did a conversion report an error. */ | |
1453 if (fio_flags != 0 | |
1454 # ifdef USE_ICONV | |
1455 || iconv_fd != (iconv_t)-1 | |
1456 # endif | |
1457 ) | |
1458 { | |
4331 | 1459 if (can_retry) |
1460 goto rewind_retry; | |
1597 | 1461 if (conv_error == 0) |
1462 conv_error = curbuf->b_ml.ml_line_count | |
1463 - linecnt + 1; | |
1464 } | |
1465 /* Remember the first linenr with an illegal byte */ | |
1466 else if (illegal_byte == 0) | |
1467 illegal_byte = curbuf->b_ml.ml_line_count | |
1468 - linecnt + 1; | |
1469 if (bad_char_behavior == BAD_DROP) | |
595 | 1470 { |
1597 | 1471 *(ptr - conv_restlen) = NUL; |
1472 conv_restlen = 0; | |
1473 } | |
1474 else | |
1475 { | |
1476 /* Replace the trailing bytes with the replacement | |
1477 * character if we were converting; if we weren't, | |
1478 * leave the UTF8 checking code to do it, as it | |
1479 * works slightly differently. */ | |
1480 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0 | |
1481 # ifdef USE_ICONV | |
1482 || iconv_fd != (iconv_t)-1 | |
1483 # endif | |
1484 )) | |
1485 { | |
1486 while (conv_restlen > 0) | |
1487 { | |
1488 *(--ptr) = bad_char_behavior; | |
1489 --conv_restlen; | |
1490 } | |
1491 } | |
595 | 1492 fio_flags = 0; /* don't convert this */ |
834 | 1493 # ifdef USE_ICONV |
1494 if (iconv_fd != (iconv_t)-1) | |
1495 { | |
1496 iconv_close(iconv_fd); | |
1497 iconv_fd = (iconv_t)-1; | |
1498 } | |
1499 # endif | |
595 | 1500 } |
1501 } | |
7 | 1502 #endif |
1503 } | |
1504 } | |
1505 skip_read = FALSE; | |
1506 | |
1507 #ifdef FEAT_MBYTE | |
1508 /* | |
1509 * At start of file (or after crypt magic number): Check for BOM. | |
1510 * Also check for a BOM for other Unicode encodings, but not after | |
1511 * converting with 'charconvert' or when a BOM has already been | |
1512 * found. | |
1513 */ | |
1514 if ((filesize == 0 | |
1515 # ifdef FEAT_CRYPT | |
6122 | 1516 || (cryptkey != NULL |
1517 && filesize == crypt_get_header_len( | |
1518 crypt_get_method_nr(curbuf))) | |
7 | 1519 # endif |
1520 ) | |
1521 && (fio_flags == FIO_UCSBOM | |
1522 || (!curbuf->b_p_bomb | |
1523 && tmpname == NULL | |
1524 && (*fenc == 'u' || (*fenc == NUL && enc_utf8))))) | |
1525 { | |
1526 char_u *ccname; | |
1527 int blen; | |
1528 | |
1529 /* no BOM detection in a short file or in binary mode */ | |
1530 if (size < 2 || curbuf->b_p_bin) | |
1531 ccname = NULL; | |
1532 else | |
1533 ccname = check_for_bom(ptr, size, &blen, | |
1534 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc)); | |
1535 if (ccname != NULL) | |
1536 { | |
1537 /* Remove BOM from the text */ | |
1538 filesize += blen; | |
1539 size -= blen; | |
1540 mch_memmove(ptr, ptr + blen, (size_t)size); | |
819 | 1541 if (set_options) |
1352 | 1542 { |
7 | 1543 curbuf->b_p_bomb = TRUE; |
1352 | 1544 curbuf->b_start_bomb = TRUE; |
1545 } | |
7 | 1546 } |
1547 | |
1548 if (fio_flags == FIO_UCSBOM) | |
1549 { | |
1550 if (ccname == NULL) | |
1551 { | |
1552 /* No BOM detected: retry with next encoding. */ | |
1553 advance_fenc = TRUE; | |
1554 } | |
1555 else | |
1556 { | |
1557 /* BOM detected: set "fenc" and jump back */ | |
1558 if (fenc_alloced) | |
1559 vim_free(fenc); | |
1560 fenc = ccname; | |
1561 fenc_alloced = FALSE; | |
1562 } | |
1563 /* retry reading without getting new bytes or rewinding */ | |
1564 skip_read = TRUE; | |
1565 goto retry; | |
1566 } | |
1567 } | |
1597 | 1568 |
1569 /* Include not converted bytes. */ | |
1570 ptr -= conv_restlen; | |
1571 size += conv_restlen; | |
1572 conv_restlen = 0; | |
7 | 1573 #endif |
1574 /* | |
1575 * Break here for a read error or end-of-file. | |
1576 */ | |
1577 if (size <= 0) | |
1578 break; | |
1579 | |
1580 #ifdef FEAT_MBYTE | |
1581 | |
1582 # ifdef USE_ICONV | |
1583 if (iconv_fd != (iconv_t)-1) | |
1584 { | |
1585 /* | |
1586 * Attempt conversion of the read bytes to 'encoding' using | |
1587 * iconv(). | |
1588 */ | |
1589 const char *fromp; | |
1590 char *top; | |
1591 size_t from_size; | |
1592 size_t to_size; | |
1593 | |
1594 fromp = (char *)ptr; | |
1595 from_size = size; | |
1596 ptr += size; | |
1597 top = (char *)ptr; | |
1598 to_size = real_size - size; | |
1599 | |
1600 /* | |
1601 * If there is conversion error or not enough room try using | |
179 | 1602 * another conversion. Except for when there is no |
1603 * alternative (help files). | |
7 | 1604 */ |
177 | 1605 while ((iconv(iconv_fd, (void *)&fromp, &from_size, |
1606 &top, &to_size) | |
7 | 1607 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) |
1608 || from_size > CONV_RESTLEN) | |
177 | 1609 { |
595 | 1610 if (can_retry) |
375 | 1611 goto rewind_retry; |
595 | 1612 if (conv_error == 0) |
1613 conv_error = readfile_linenr(linecnt, | |
1614 ptr, (char_u *)top); | |
1615 | |
1616 /* Deal with a bad byte and continue with the next. */ | |
177 | 1617 ++fromp; |
1618 --from_size; | |
595 | 1619 if (bad_char_behavior == BAD_KEEP) |
1620 { | |
1621 *top++ = *(fromp - 1); | |
1622 --to_size; | |
1623 } | |
1624 else if (bad_char_behavior != BAD_DROP) | |
1625 { | |
1626 *top++ = bad_char_behavior; | |
1627 --to_size; | |
1628 } | |
177 | 1629 } |
7 | 1630 |
1631 if (from_size > 0) | |
1632 { | |
1633 /* Some remaining characters, keep them for the next | |
1634 * round. */ | |
1635 mch_memmove(conv_rest, (char_u *)fromp, from_size); | |
1636 conv_restlen = (int)from_size; | |
1637 } | |
1638 | |
1639 /* move the linerest to before the converted characters */ | |
1640 line_start = ptr - linerest; | |
1641 mch_memmove(line_start, buffer, (size_t)linerest); | |
1642 size = (long)((char_u *)top - ptr); | |
1643 } | |
1644 # endif | |
1645 | |
1646 # ifdef WIN3264 | |
1647 if (fio_flags & FIO_CODEPAGE) | |
1648 { | |
595 | 1649 char_u *src, *dst; |
1650 WCHAR ucs2buf[3]; | |
1651 int ucs2len; | |
1652 int codepage = FIO_GET_CP(fio_flags); | |
1653 int bytelen; | |
1654 int found_bad; | |
1655 char replstr[2]; | |
1656 | |
7 | 1657 /* |
1658 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or | |
595 | 1659 * a codepage, using standard MS-Windows functions. This |
1660 * requires two steps: | |
1661 * 1. convert from 'fileencoding' to ucs-2 | |
1662 * 2. convert from ucs-2 to 'encoding' | |
1663 * | |
1664 * Because there may be illegal bytes AND an incomplete byte | |
1665 * sequence at the end, we may have to do the conversion one | |
1666 * character at a time to get it right. | |
7 | 1667 */ |
595 | 1668 |
1669 /* Replacement string for WideCharToMultiByte(). */ | |
1670 if (bad_char_behavior > 0) | |
1671 replstr[0] = bad_char_behavior; | |
1672 else | |
1673 replstr[0] = '?'; | |
1674 replstr[1] = NUL; | |
1675 | |
1676 /* | |
1677 * Move the bytes to the end of the buffer, so that we have | |
1678 * room to put the result at the start. | |
1679 */ | |
1680 src = ptr + real_size - size; | |
1681 mch_memmove(src, ptr, size); | |
7 | 1682 |
1683 /* | |
595 | 1684 * Do the conversion. |
7 | 1685 */ |
595 | 1686 dst = ptr; |
1687 size = size; | |
1688 while (size > 0) | |
7 | 1689 { |
595 | 1690 found_bad = FALSE; |
7 | 1691 |
1692 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
595 | 1693 if (codepage == CP_UTF8) |
7 | 1694 { |
595 | 1695 /* Handle CP_UTF8 input ourselves to be able to handle |
1696 * trailing bytes properly. | |
1697 * Get one UTF-8 character from src. */ | |
835 | 1698 bytelen = (int)utf_ptr2len_len(src, size); |
595 | 1699 if (bytelen > size) |
1700 { | |
1701 /* Only got some bytes of a character. Normally | |
1702 * it's put in "conv_rest", but if it's too long | |
1703 * deal with it as if they were illegal bytes. */ | |
1704 if (bytelen <= CONV_RESTLEN) | |
1705 break; | |
1706 | |
1707 /* weird overlong byte sequence */ | |
1708 bytelen = size; | |
1709 found_bad = TRUE; | |
1710 } | |
1711 else | |
1712 { | |
799 | 1713 int u8c = utf_ptr2char(src); |
1714 | |
622 | 1715 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1)) |
595 | 1716 found_bad = TRUE; |
1717 ucs2buf[0] = u8c; | |
1718 ucs2len = 1; | |
1719 } | |
7 | 1720 } |
595 | 1721 else |
7 | 1722 # endif |
595 | 1723 { |
1724 /* We don't know how long the byte sequence is, try | |
1725 * from one to three bytes. */ | |
1726 for (bytelen = 1; bytelen <= size && bytelen <= 3; | |
1727 ++bytelen) | |
1728 { | |
1729 ucs2len = MultiByteToWideChar(codepage, | |
1730 MB_ERR_INVALID_CHARS, | |
1731 (LPCSTR)src, bytelen, | |
1732 ucs2buf, 3); | |
1733 if (ucs2len > 0) | |
1734 break; | |
1735 } | |
1736 if (ucs2len == 0) | |
1737 { | |
1738 /* If we have only one byte then it's probably an | |
1739 * incomplete byte sequence. Otherwise discard | |
1740 * one byte as a bad character. */ | |
1741 if (size == 1) | |
1742 break; | |
1743 found_bad = TRUE; | |
1744 bytelen = 1; | |
1745 } | |
1746 } | |
1747 | |
1748 if (!found_bad) | |
7 | 1749 { |
595 | 1750 int i; |
1751 | |
1752 /* Convert "ucs2buf[ucs2len]" to 'enc' in "dst". */ | |
1753 if (enc_utf8) | |
1754 { | |
1755 /* From UCS-2 to UTF-8. Cannot fail. */ | |
1756 for (i = 0; i < ucs2len; ++i) | |
1757 dst += utf_char2bytes(ucs2buf[i], dst); | |
1758 } | |
1759 else | |
1760 { | |
1761 BOOL bad = FALSE; | |
1762 int dstlen; | |
1763 | |
1764 /* From UCS-2 to "enc_codepage". If the | |
1765 * conversion uses the default character "?", | |
1766 * the data doesn't fit in this encoding. */ | |
1767 dstlen = WideCharToMultiByte(enc_codepage, 0, | |
1768 (LPCWSTR)ucs2buf, ucs2len, | |
835 | 1769 (LPSTR)dst, (int)(src - dst), |
595 | 1770 replstr, &bad); |
1771 if (bad) | |
1772 found_bad = TRUE; | |
1773 else | |
1774 dst += dstlen; | |
1775 } | |
7 | 1776 } |
595 | 1777 |
1778 if (found_bad) | |
1779 { | |
1780 /* Deal with bytes we can't convert. */ | |
1781 if (can_retry) | |
1782 goto rewind_retry; | |
1783 if (conv_error == 0) | |
1784 conv_error = readfile_linenr(linecnt, ptr, dst); | |
1785 if (bad_char_behavior != BAD_DROP) | |
1786 { | |
1787 if (bad_char_behavior == BAD_KEEP) | |
1788 { | |
1789 mch_memmove(dst, src, bytelen); | |
1790 dst += bytelen; | |
1791 } | |
1792 else | |
1793 *dst++ = bad_char_behavior; | |
1794 } | |
1795 } | |
1796 | |
1797 src += bytelen; | |
1798 size -= bytelen; | |
7 | 1799 } |
595 | 1800 |
1801 if (size > 0) | |
7 | 1802 { |
595 | 1803 /* An incomplete byte sequence remaining. */ |
1804 mch_memmove(conv_rest, src, size); | |
1805 conv_restlen = size; | |
7 | 1806 } |
595 | 1807 |
1808 /* The new size is equal to how much "dst" was advanced. */ | |
835 | 1809 size = (long)(dst - ptr); |
7 | 1810 } |
1811 else | |
1812 # endif | |
765 | 1813 # ifdef MACOS_CONVERT |
7 | 1814 if (fio_flags & FIO_MACROMAN) |
1815 { | |
1816 /* | |
1817 * Conversion from Apple MacRoman char encoding to UTF-8 or | |
18 | 1818 * latin1. This is in os_mac_conv.c. |
7 | 1819 */ |
18 | 1820 if (macroman2enc(ptr, &size, real_size) == FAIL) |
7 | 1821 goto rewind_retry; |
1822 } | |
1823 else | |
1824 # endif | |
1825 if (fio_flags != 0) | |
1826 { | |
1827 int u8c; | |
1828 char_u *dest; | |
1829 char_u *tail = NULL; | |
1830 | |
1831 /* | |
1832 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8. | |
1833 * "enc_utf8" not set: Convert Unicode to Latin1. | |
1834 * Go from end to start through the buffer, because the number | |
1835 * of bytes may increase. | |
1836 * "dest" points to after where the UTF-8 bytes go, "p" points | |
1837 * to after the next character to convert. | |
1838 */ | |
1839 dest = ptr + real_size; | |
1840 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8) | |
1841 { | |
1842 p = ptr + size; | |
1843 if (fio_flags == FIO_UTF8) | |
1844 { | |
1845 /* Check for a trailing incomplete UTF-8 sequence */ | |
1846 tail = ptr + size - 1; | |
1847 while (tail > ptr && (*tail & 0xc0) == 0x80) | |
1848 --tail; | |
1849 if (tail + utf_byte2len(*tail) <= ptr + size) | |
1850 tail = NULL; | |
1851 else | |
1852 p = tail; | |
1853 } | |
1854 } | |
1855 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1856 { | |
1857 /* Check for a trailing byte */ | |
1858 p = ptr + (size & ~1); | |
1859 if (size & 1) | |
1860 tail = p; | |
1861 if ((fio_flags & FIO_UTF16) && p > ptr) | |
1862 { | |
1863 /* Check for a trailing leading word */ | |
1864 if (fio_flags & FIO_ENDIAN_L) | |
1865 { | |
1866 u8c = (*--p << 8); | |
1867 u8c += *--p; | |
1868 } | |
1869 else | |
1870 { | |
1871 u8c = *--p; | |
1872 u8c += (*--p << 8); | |
1873 } | |
1874 if (u8c >= 0xd800 && u8c <= 0xdbff) | |
1875 tail = p; | |
1876 else | |
1877 p += 2; | |
1878 } | |
1879 } | |
1880 else /* FIO_UCS4 */ | |
1881 { | |
1882 /* Check for trailing 1, 2 or 3 bytes */ | |
1883 p = ptr + (size & ~3); | |
1884 if (size & 3) | |
1885 tail = p; | |
1886 } | |
1887 | |
1888 /* If there is a trailing incomplete sequence move it to | |
1889 * conv_rest[]. */ | |
1890 if (tail != NULL) | |
1891 { | |
1892 conv_restlen = (int)((ptr + size) - tail); | |
1893 mch_memmove(conv_rest, (char_u *)tail, conv_restlen); | |
1894 size -= conv_restlen; | |
1895 } | |
1896 | |
1897 | |
1898 while (p > ptr) | |
1899 { | |
1900 if (fio_flags & FIO_LATIN1) | |
1901 u8c = *--p; | |
1902 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1903 { | |
1904 if (fio_flags & FIO_ENDIAN_L) | |
1905 { | |
1906 u8c = (*--p << 8); | |
1907 u8c += *--p; | |
1908 } | |
1909 else | |
1910 { | |
1911 u8c = *--p; | |
1912 u8c += (*--p << 8); | |
1913 } | |
1914 if ((fio_flags & FIO_UTF16) | |
1915 && u8c >= 0xdc00 && u8c <= 0xdfff) | |
1916 { | |
1917 int u16c; | |
1918 | |
1919 if (p == ptr) | |
1920 { | |
1921 /* Missing leading word. */ | |
1922 if (can_retry) | |
1923 goto rewind_retry; | |
595 | 1924 if (conv_error == 0) |
1925 conv_error = readfile_linenr(linecnt, | |
1926 ptr, p); | |
1927 if (bad_char_behavior == BAD_DROP) | |
1928 continue; | |
1929 if (bad_char_behavior != BAD_KEEP) | |
1930 u8c = bad_char_behavior; | |
7 | 1931 } |
1932 | |
1933 /* found second word of double-word, get the first | |
1934 * word and compute the resulting character */ | |
1935 if (fio_flags & FIO_ENDIAN_L) | |
1936 { | |
1937 u16c = (*--p << 8); | |
1938 u16c += *--p; | |
1939 } | |
1940 else | |
1941 { | |
1942 u16c = *--p; | |
1943 u16c += (*--p << 8); | |
1944 } | |
595 | 1945 u8c = 0x10000 + ((u16c & 0x3ff) << 10) |
1946 + (u8c & 0x3ff); | |
1947 | |
7 | 1948 /* Check if the word is indeed a leading word. */ |
1949 if (u16c < 0xd800 || u16c > 0xdbff) | |
1950 { | |
1951 if (can_retry) | |
1952 goto rewind_retry; | |
595 | 1953 if (conv_error == 0) |
1954 conv_error = readfile_linenr(linecnt, | |
1955 ptr, p); | |
1956 if (bad_char_behavior == BAD_DROP) | |
1957 continue; | |
1958 if (bad_char_behavior != BAD_KEEP) | |
1959 u8c = bad_char_behavior; | |
7 | 1960 } |
1961 } | |
1962 } | |
1963 else if (fio_flags & FIO_UCS4) | |
1964 { | |
1965 if (fio_flags & FIO_ENDIAN_L) | |
1966 { | |
12698
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1967 u8c = (unsigned)*--p << 24; |
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1968 u8c += (unsigned)*--p << 16; |
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1969 u8c += (unsigned)*--p << 8; |
7 | 1970 u8c += *--p; |
1971 } | |
1972 else /* big endian */ | |
1973 { | |
1974 u8c = *--p; | |
12698
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1975 u8c += (unsigned)*--p << 8; |
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1976 u8c += (unsigned)*--p << 16; |
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1977 u8c += (unsigned)*--p << 24; |
7 | 1978 } |
1979 } | |
1980 else /* UTF-8 */ | |
1981 { | |
1982 if (*--p < 0x80) | |
1983 u8c = *p; | |
1984 else | |
1985 { | |
1986 len = utf_head_off(ptr, p); | |
595 | 1987 p -= len; |
1988 u8c = utf_ptr2char(p); | |
7 | 1989 if (len == 0) |
1990 { | |
1991 /* Not a valid UTF-8 character, retry with | |
1992 * another fenc when possible, otherwise just | |
1993 * report the error. */ | |
1994 if (can_retry) | |
1995 goto rewind_retry; | |
595 | 1996 if (conv_error == 0) |
1997 conv_error = readfile_linenr(linecnt, | |
1998 ptr, p); | |
1999 if (bad_char_behavior == BAD_DROP) | |
2000 continue; | |
2001 if (bad_char_behavior != BAD_KEEP) | |
2002 u8c = bad_char_behavior; | |
7 | 2003 } |
2004 } | |
2005 } | |
2006 if (enc_utf8) /* produce UTF-8 */ | |
2007 { | |
2008 dest -= utf_char2len(u8c); | |
2009 (void)utf_char2bytes(u8c, dest); | |
2010 } | |
2011 else /* produce Latin1 */ | |
2012 { | |
2013 --dest; | |
2014 if (u8c >= 0x100) | |
2015 { | |
2016 /* character doesn't fit in latin1, retry with | |
2017 * another fenc when possible, otherwise just | |
2018 * report the error. */ | |
595 | 2019 if (can_retry) |
7 | 2020 goto rewind_retry; |
595 | 2021 if (conv_error == 0) |
2022 conv_error = readfile_linenr(linecnt, ptr, p); | |
2023 if (bad_char_behavior == BAD_DROP) | |
2024 ++dest; | |
2025 else if (bad_char_behavior == BAD_KEEP) | |
2026 *dest = u8c; | |
2027 else if (eap != NULL && eap->bad_char != 0) | |
2028 *dest = bad_char_behavior; | |
2029 else | |
2030 *dest = 0xBF; | |
7 | 2031 } |
2032 else | |
2033 *dest = u8c; | |
2034 } | |
2035 } | |
2036 | |
2037 /* move the linerest to before the converted characters */ | |
2038 line_start = dest - linerest; | |
2039 mch_memmove(line_start, buffer, (size_t)linerest); | |
2040 size = (long)((ptr + real_size) - dest); | |
2041 ptr = dest; | |
2042 } | |
1597 | 2043 else if (enc_utf8 && !curbuf->b_p_bin) |
2044 { | |
2045 int incomplete_tail = FALSE; | |
2046 | |
2047 /* Reading UTF-8: Check if the bytes are valid UTF-8. */ | |
2048 for (p = ptr; ; ++p) | |
7 | 2049 { |
835 | 2050 int todo = (int)((ptr + size) - p); |
595 | 2051 int l; |
2052 | |
2053 if (todo <= 0) | |
2054 break; | |
7 | 2055 if (*p >= 0x80) |
2056 { | |
2057 /* A length of 1 means it's an illegal byte. Accept | |
2058 * an incomplete character at the end though, the next | |
2059 * read() will get the next bytes, we'll check it | |
2060 * then. */ | |
595 | 2061 l = utf_ptr2len_len(p, todo); |
1597 | 2062 if (l > todo && !incomplete_tail) |
7 | 2063 { |
1597 | 2064 /* Avoid retrying with a different encoding when |
2065 * a truncated file is more likely, or attempting | |
2066 * to read the rest of an incomplete sequence when | |
2067 * we have already done so. */ | |
2068 if (p > ptr || filesize > 0) | |
2069 incomplete_tail = TRUE; | |
2070 /* Incomplete byte sequence, move it to conv_rest[] | |
2071 * and try to read the rest of it, unless we've | |
2072 * already done so. */ | |
2073 if (p > ptr) | |
2074 { | |
2075 conv_restlen = todo; | |
2076 mch_memmove(conv_rest, p, conv_restlen); | |
2077 size -= conv_restlen; | |
2078 break; | |
2079 } | |
7 | 2080 } |
1597 | 2081 if (l == 1 || l > todo) |
595 | 2082 { |
2083 /* Illegal byte. If we can try another encoding | |
1597 | 2084 * do that, unless at EOF where a truncated |
2085 * file is more likely than a conversion error. */ | |
2086 if (can_retry && !incomplete_tail) | |
595 | 2087 break; |
2088 # ifdef USE_ICONV | |
2089 /* When we did a conversion report an error. */ | |
2090 if (iconv_fd != (iconv_t)-1 && conv_error == 0) | |
2091 conv_error = readfile_linenr(linecnt, ptr, p); | |
2092 # endif | |
1597 | 2093 /* Remember the first linenr with an illegal byte */ |
2094 if (conv_error == 0 && illegal_byte == 0) | |
2095 illegal_byte = readfile_linenr(linecnt, ptr, p); | |
595 | 2096 |
2097 /* Drop, keep or replace the bad byte. */ | |
2098 if (bad_char_behavior == BAD_DROP) | |
2099 { | |
1597 | 2100 mch_memmove(p, p + 1, todo - 1); |
595 | 2101 --p; |
2102 --size; | |
2103 } | |
2104 else if (bad_char_behavior != BAD_KEEP) | |
2105 *p = bad_char_behavior; | |
2106 } | |
1597 | 2107 else |
2108 p += l - 1; | |
7 | 2109 } |
2110 } | |
1597 | 2111 if (p < ptr + size && !incomplete_tail) |
7 | 2112 { |
2113 /* Detected a UTF-8 error. */ | |
2114 rewind_retry: | |
595 | 2115 /* Retry reading with another conversion. */ |
7 | 2116 # if defined(FEAT_EVAL) && defined(USE_ICONV) |
595 | 2117 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1) |
2118 /* iconv() failed, try 'charconvert' */ | |
2119 did_iconv = TRUE; | |
7 | 2120 else |
2121 # endif | |
595 | 2122 /* use next item from 'fileencodings' */ |
2123 advance_fenc = TRUE; | |
2124 file_rewind = TRUE; | |
2125 goto retry; | |
7 | 2126 } |
2127 } | |
2128 #endif | |
2129 | |
2130 /* count the number of characters (after conversion!) */ | |
2131 filesize += size; | |
2132 | |
2133 /* | |
2134 * when reading the first part of a file: guess EOL type | |
2135 */ | |
2136 if (fileformat == EOL_UNKNOWN) | |
2137 { | |
2138 /* First try finding a NL, for Dos and Unix */ | |
2139 if (try_dos || try_unix) | |
2140 { | |
6635 | 2141 /* Reset the carriage return counter. */ |
2142 if (try_mac) | |
2143 try_mac = 1; | |
2144 | |
7 | 2145 for (p = ptr; p < ptr + size; ++p) |
2146 { | |
2147 if (*p == NL) | |
2148 { | |
2149 if (!try_unix | |
2150 || (try_dos && p > ptr && p[-1] == CAR)) | |
2151 fileformat = EOL_DOS; | |
2152 else | |
2153 fileformat = EOL_UNIX; | |
2154 break; | |
2155 } | |
6618 | 2156 else if (*p == CAR && try_mac) |
2157 try_mac++; | |
7 | 2158 } |
2159 | |
2160 /* Don't give in to EOL_UNIX if EOL_MAC is more likely */ | |
2161 if (fileformat == EOL_UNIX && try_mac) | |
2162 { | |
2163 /* Need to reset the counters when retrying fenc. */ | |
2164 try_mac = 1; | |
2165 try_unix = 1; | |
2166 for (; p >= ptr && *p != CAR; p--) | |
2167 ; | |
2168 if (p >= ptr) | |
2169 { | |
2170 for (p = ptr; p < ptr + size; ++p) | |
2171 { | |
2172 if (*p == NL) | |
2173 try_unix++; | |
2174 else if (*p == CAR) | |
2175 try_mac++; | |
2176 } | |
2177 if (try_mac > try_unix) | |
2178 fileformat = EOL_MAC; | |
2179 } | |
2180 } | |
6618 | 2181 else if (fileformat == EOL_UNKNOWN && try_mac == 1) |
2182 /* Looking for CR but found no end-of-line markers at | |
2183 * all: use the default format. */ | |
2184 fileformat = default_fileformat(); | |
7 | 2185 } |
2186 | |
2187 /* No NL found: may use Mac format */ | |
2188 if (fileformat == EOL_UNKNOWN && try_mac) | |
2189 fileformat = EOL_MAC; | |
2190 | |
2191 /* Still nothing found? Use first format in 'ffs' */ | |
2192 if (fileformat == EOL_UNKNOWN) | |
2193 fileformat = default_fileformat(); | |
2194 | |
2195 /* if editing a new file: may set p_tx and p_ff */ | |
819 | 2196 if (set_options) |
7 | 2197 set_fileformat(fileformat, OPT_LOCAL); |
2198 } | |
2199 } | |
2200 | |
2201 /* | |
2202 * This loop is executed once for every character read. | |
2203 * Keep it fast! | |
2204 */ | |
2205 if (fileformat == EOL_MAC) | |
2206 { | |
2207 --ptr; | |
2208 while (++ptr, --size >= 0) | |
2209 { | |
2210 /* catch most common case first */ | |
2211 if ((c = *ptr) != NUL && c != CAR && c != NL) | |
2212 continue; | |
2213 if (c == NUL) | |
2214 *ptr = NL; /* NULs are replaced by newlines! */ | |
2215 else if (c == NL) | |
2216 *ptr = CAR; /* NLs are replaced by CRs! */ | |
2217 else | |
2218 { | |
2219 if (skip_count == 0) | |
2220 { | |
2221 *ptr = NUL; /* end of line */ | |
2222 len = (colnr_T) (ptr - line_start + 1); | |
2223 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2224 { | |
2225 error = TRUE; | |
2226 break; | |
2227 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2228 #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
|
2229 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
|
2230 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
|
2231 #endif |
7 | 2232 ++lnum; |
2233 if (--read_count == 0) | |
2234 { | |
2235 error = TRUE; /* break loop */ | |
2236 line_start = ptr; /* nothing left to write */ | |
2237 break; | |
2238 } | |
2239 } | |
2240 else | |
2241 --skip_count; | |
2242 line_start = ptr + 1; | |
2243 } | |
2244 } | |
2245 } | |
2246 else | |
2247 { | |
2248 --ptr; | |
2249 while (++ptr, --size >= 0) | |
2250 { | |
2251 if ((c = *ptr) != NUL && c != NL) /* catch most common case */ | |
2252 continue; | |
2253 if (c == NUL) | |
2254 *ptr = NL; /* NULs are replaced by newlines! */ | |
2255 else | |
2256 { | |
2257 if (skip_count == 0) | |
2258 { | |
2259 *ptr = NUL; /* end of line */ | |
2260 len = (colnr_T)(ptr - line_start + 1); | |
2261 if (fileformat == EOL_DOS) | |
2262 { | |
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
|
2263 if (ptr > line_start && ptr[-1] == CAR) |
7 | 2264 { |
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
|
2265 /* remove CR before NL */ |
7 | 2266 ptr[-1] = NUL; |
2267 --len; | |
2268 } | |
2269 /* | |
2270 * Reading in Dos format, but no CR-LF found! | |
2271 * When 'fileformats' includes "unix", delete all | |
2272 * the lines read so far and start all over again. | |
2273 * Otherwise give an error message later. | |
2274 */ | |
2275 else if (ff_error != EOL_DOS) | |
2276 { | |
2277 if ( try_unix | |
2278 && !read_stdin | |
2279 && (read_buffer | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
2280 || 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
|
2281 == 0)) |
7 | 2282 { |
2283 fileformat = EOL_UNIX; | |
819 | 2284 if (set_options) |
7 | 2285 set_fileformat(EOL_UNIX, OPT_LOCAL); |
2286 file_rewind = TRUE; | |
2287 keep_fileformat = TRUE; | |
2288 goto retry; | |
2289 } | |
2290 ff_error = EOL_DOS; | |
2291 } | |
2292 } | |
2293 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2294 { | |
2295 error = TRUE; | |
2296 break; | |
2297 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2298 #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
|
2299 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
|
2300 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
|
2301 #endif |
7 | 2302 ++lnum; |
2303 if (--read_count == 0) | |
2304 { | |
2305 error = TRUE; /* break loop */ | |
2306 line_start = ptr; /* nothing left to write */ | |
2307 break; | |
2308 } | |
2309 } | |
2310 else | |
2311 --skip_count; | |
2312 line_start = ptr + 1; | |
2313 } | |
2314 } | |
2315 } | |
2316 linerest = (long)(ptr - line_start); | |
2317 ui_breakcheck(); | |
2318 } | |
2319 | |
2320 failed: | |
2321 /* not an error, max. number of lines reached */ | |
2322 if (error && read_count == 0) | |
2323 error = FALSE; | |
2324 | |
2325 /* | |
2326 * If we get EOF in the middle of a line, note the fact and | |
2327 * complete the line ourselves. | |
2328 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set. | |
2329 */ | |
2330 if (!error | |
2331 && !got_int | |
2332 && linerest != 0 | |
2333 && !(!curbuf->b_p_bin | |
2334 && fileformat == EOL_DOS | |
2335 && *line_start == Ctrl_Z | |
2336 && ptr == line_start + 1)) | |
2337 { | |
819 | 2338 /* remember for when writing */ |
2339 if (set_options) | |
7 | 2340 curbuf->b_p_eol = FALSE; |
2341 *ptr = NUL; | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2342 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
|
2343 if (ml_append(lnum, line_start, len, newfile) == FAIL) |
7 | 2344 error = TRUE; |
2345 else | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2346 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2347 #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
|
2348 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
|
2349 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
|
2350 #endif |
7 | 2351 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
|
2352 } |
7 | 2353 } |
2354 | |
819 | 2355 if (set_options) |
7 | 2356 save_file_ff(curbuf); /* remember the current file format */ |
2357 | |
2358 #ifdef FEAT_CRYPT | |
6122 | 2359 if (curbuf->b_cryptstate != NULL) |
2360 { | |
2361 crypt_free_state(curbuf->b_cryptstate); | |
2362 curbuf->b_cryptstate = NULL; | |
2363 } | |
2364 if (cryptkey != NULL && cryptkey != curbuf->b_p_key) | |
2365 crypt_free_key(cryptkey); | |
2366 /* Don't set cryptkey to NULL, it's used below as a flag that | |
2367 * encryption was used. */ | |
7 | 2368 #endif |
2369 | |
2370 #ifdef FEAT_MBYTE | |
819 | 2371 /* If editing a new file: set 'fenc' for the current buffer. |
2372 * Also for ":read ++edit file". */ | |
2373 if (set_options) | |
7 | 2374 set_string_option_direct((char_u *)"fenc", -1, fenc, |
694 | 2375 OPT_FREE|OPT_LOCAL, 0); |
7 | 2376 if (fenc_alloced) |
2377 vim_free(fenc); | |
2378 # ifdef USE_ICONV | |
2379 if (iconv_fd != (iconv_t)-1) | |
2380 { | |
2381 iconv_close(iconv_fd); | |
2382 iconv_fd = (iconv_t)-1; | |
2383 } | |
2384 # endif | |
2385 #endif | |
2386 | |
2387 if (!read_buffer && !read_stdin) | |
2388 close(fd); /* errors are ignored */ | |
2003 | 2389 #ifdef HAVE_FD_CLOEXEC |
2390 else | |
2391 { | |
2392 int fdflags = fcntl(fd, F_GETFD); | |
2393 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
|
2394 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC); |
2003 | 2395 } |
2396 #endif | |
7 | 2397 vim_free(buffer); |
2398 | |
2399 #ifdef HAVE_DUP | |
2400 if (read_stdin) | |
2401 { | |
2402 /* Use stderr for stdin, makes shell commands work. */ | |
2403 close(0); | |
14730
193471015e1a
patch 8.1.0377: xdiff doesn't use the Vim memory allocation functions
Christian Brabandt <cb@256bit.org>
parents:
14700
diff
changeset
|
2404 vim_ignored = dup(2); |
7 | 2405 } |
2406 #endif | |
2407 | |
2408 #ifdef FEAT_MBYTE | |
2409 if (tmpname != NULL) | |
2410 { | |
2411 mch_remove(tmpname); /* delete converted file */ | |
2412 vim_free(tmpname); | |
2413 } | |
2414 #endif | |
2415 --no_wait_return; /* may wait for return now */ | |
2416 | |
2417 /* | |
2418 * In recovery mode everything but autocommands is skipped. | |
2419 */ | |
2420 if (!recoverymode) | |
2421 { | |
2422 /* need to delete the last line, which comes from the empty buffer */ | |
2423 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY)) | |
2424 { | |
2425 #ifdef FEAT_NETBEANS_INTG | |
2426 netbeansFireChanges = 0; | |
2427 #endif | |
2428 ml_delete(curbuf->b_ml.ml_line_count, FALSE); | |
2429 #ifdef FEAT_NETBEANS_INTG | |
2430 netbeansFireChanges = 1; | |
2431 #endif | |
2432 --linecnt; | |
2433 } | |
2434 linecnt = curbuf->b_ml.ml_line_count - linecnt; | |
2435 if (filesize == 0) | |
2436 linecnt = 0; | |
2437 if (newfile || read_buffer) | |
1201 | 2438 { |
7 | 2439 redraw_curbuf_later(NOT_VALID); |
1201 | 2440 #ifdef FEAT_DIFF |
2441 /* After reading the text into the buffer the diff info needs to | |
2442 * be updated. */ | |
2443 diff_invalidate(curbuf); | |
2444 #endif | |
2445 #ifdef FEAT_FOLDING | |
2446 /* All folds in the window are invalid now. Mark them for update | |
2447 * before triggering autocommands. */ | |
2448 foldUpdateAll(curwin); | |
2449 #endif | |
2450 } | |
7 | 2451 else if (linecnt) /* appended at least one line */ |
2452 appended_lines_mark(from, linecnt); | |
2453 | |
2454 #ifndef ALWAYS_USE_GUI | |
2455 /* | |
2456 * If we were reading from the same terminal as where messages go, | |
2457 * the screen will have been messed up. | |
2458 * Switch on raw mode now and clear the screen. | |
2459 */ | |
2460 if (read_stdin) | |
2461 { | |
2462 settmode(TMODE_RAW); /* set to raw mode */ | |
2463 starttermcap(); | |
2464 screenclear(); | |
2465 } | |
2466 #endif | |
2467 | |
2468 if (got_int) | |
2469 { | |
2470 if (!(flags & READ_DUMMY)) | |
2471 { | |
2472 filemess(curbuf, sfname, (char_u *)_(e_interr), 0); | |
2473 if (newfile) | |
2474 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
2475 } | |
2476 msg_scroll = msg_save; | |
2477 #ifdef FEAT_VIMINFO | |
2478 check_marks_read(); | |
2479 #endif | |
2480 return OK; /* an interrupt isn't really an error */ | |
2481 } | |
2482 | |
2483 if (!filtering && !(flags & READ_DUMMY)) | |
2484 { | |
2485 msg_add_fname(curbuf, sfname); /* fname in IObuff with quotes */ | |
2486 c = FALSE; | |
2487 | |
2488 #ifdef UNIX | |
14509
80f715651c4c
patch 8.1.0268: file type checking has too many #ifdef
Christian Brabandt <cb@256bit.org>
parents:
14475
diff
changeset
|
2489 if (S_ISFIFO(perm)) /* fifo */ |
7 | 2490 { |
2491 STRCAT(IObuff, _("[fifo]")); | |
2492 c = TRUE; | |
2493 } | |
14509
80f715651c4c
patch 8.1.0268: file type checking has too many #ifdef
Christian Brabandt <cb@256bit.org>
parents:
14475
diff
changeset
|
2494 if (S_ISSOCK(perm)) /* or socket */ |
7 | 2495 { |
2496 STRCAT(IObuff, _("[socket]")); | |
2497 c = TRUE; | |
2498 } | |
1313 | 2499 # ifdef OPEN_CHR_FILES |
2500 if (S_ISCHR(perm)) /* or character special */ | |
2501 { | |
2502 STRCAT(IObuff, _("[character special]")); | |
2503 c = TRUE; | |
2504 } | |
2505 # endif | |
7 | 2506 #endif |
2507 if (curbuf->b_p_ro) | |
2508 { | |
2509 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")); | |
2510 c = TRUE; | |
2511 } | |
2512 if (read_no_eol_lnum) | |
2513 { | |
2514 msg_add_eol(); | |
2515 c = TRUE; | |
2516 } | |
2517 if (ff_error == EOL_DOS) | |
2518 { | |
2519 STRCAT(IObuff, _("[CR missing]")); | |
2520 c = TRUE; | |
2521 } | |
2522 if (split) | |
2523 { | |
2524 STRCAT(IObuff, _("[long lines split]")); | |
2525 c = TRUE; | |
2526 } | |
2527 #ifdef FEAT_MBYTE | |
2528 if (notconverted) | |
2529 { | |
2530 STRCAT(IObuff, _("[NOT converted]")); | |
2531 c = TRUE; | |
2532 } | |
2533 else if (converted) | |
2534 { | |
2535 STRCAT(IObuff, _("[converted]")); | |
2536 c = TRUE; | |
2537 } | |
2538 #endif | |
2539 #ifdef FEAT_CRYPT | |
2540 if (cryptkey != NULL) | |
2541 { | |
6122 | 2542 crypt_append_msg(curbuf); |
7 | 2543 c = TRUE; |
2544 } | |
2545 #endif | |
2546 #ifdef FEAT_MBYTE | |
595 | 2547 if (conv_error != 0) |
2548 { | |
2549 sprintf((char *)IObuff + STRLEN(IObuff), | |
2550 _("[CONVERSION ERROR in line %ld]"), (long)conv_error); | |
7 | 2551 c = TRUE; |
2552 } | |
2553 else if (illegal_byte > 0) | |
2554 { | |
2555 sprintf((char *)IObuff + STRLEN(IObuff), | |
2556 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte); | |
2557 c = TRUE; | |
2558 } | |
2559 else | |
2560 #endif | |
2561 if (error) | |
2562 { | |
2563 STRCAT(IObuff, _("[READ ERRORS]")); | |
2564 c = TRUE; | |
2565 } | |
2566 if (msg_add_fileformat(fileformat)) | |
2567 c = TRUE; | |
2568 #ifdef FEAT_CRYPT | |
2569 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
|
2570 msg_add_lines(c, (long)linecnt, filesize |
6122 | 2571 - crypt_get_header_len(crypt_get_method_nr(curbuf))); |
7 | 2572 else |
2573 #endif | |
2574 msg_add_lines(c, (long)linecnt, filesize); | |
2575 | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
2576 VIM_CLEAR(keep_msg); |
7 | 2577 msg_scrolled_ign = TRUE; |
2578 #ifdef ALWAYS_USE_GUI | |
2579 /* Don't show the message when reading stdin, it would end up in a | |
2580 * message box (which might be shown when exiting!) */ | |
2581 if (read_stdin || read_buffer) | |
2582 p = msg_may_trunc(FALSE, IObuff); | |
2583 else | |
2584 #endif | |
2585 p = msg_trunc_attr(IObuff, FALSE, 0); | |
2586 if (read_stdin || read_buffer || restart_edit != 0 | |
540 | 2587 || (msg_scrolled != 0 && !need_wait_return)) |
7 | 2588 /* Need to repeat the message after redrawing when: |
2589 * - When reading from stdin (the screen will be cleared next). | |
2590 * - When restart_edit is set (otherwise there will be a delay | |
2591 * before redrawing). | |
2592 * - When the screen was scrolled but there is no wait-return | |
2593 * prompt. */ | |
679 | 2594 set_keep_msg(p, 0); |
7 | 2595 msg_scrolled_ign = FALSE; |
2596 } | |
2597 | |
2598 /* with errors writing the file requires ":w!" */ | |
2599 if (newfile && (error | |
2600 #ifdef FEAT_MBYTE | |
595 | 2601 || conv_error != 0 |
679 | 2602 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP) |
7 | 2603 #endif |
2604 )) | |
2605 curbuf->b_p_ro = TRUE; | |
2606 | |
2607 u_clearline(); /* cannot use "U" command after adding lines */ | |
2608 | |
2609 /* | |
2610 * In Ex mode: cursor at last new line. | |
2611 * Otherwise: cursor at first new line. | |
2612 */ | |
2613 if (exmode_active) | |
2614 curwin->w_cursor.lnum = from + linecnt; | |
2615 else | |
2616 curwin->w_cursor.lnum = from + 1; | |
2617 check_cursor_lnum(); | |
2618 beginline(BL_WHITE | BL_FIX); /* on first non-blank */ | |
2619 | |
2620 /* | |
2621 * Set '[ and '] marks to the newly read lines. | |
2622 */ | |
2623 curbuf->b_op_start.lnum = from + 1; | |
2624 curbuf->b_op_start.col = 0; | |
2625 curbuf->b_op_end.lnum = from + linecnt; | |
2626 curbuf->b_op_end.col = 0; | |
696 | 2627 |
2628 #ifdef WIN32 | |
2629 /* | |
2630 * Work around a weird problem: When a file has two links (only | |
2631 * possible on NTFS) and we write through one link, then stat() it | |
1651 | 2632 * through the other link, the timestamp information may be wrong. |
696 | 2633 * It's correct again after reading the file, thus reset the timestamp |
2634 * here. | |
2635 */ | |
2636 if (newfile && !read_stdin && !read_buffer | |
2637 && mch_stat((char *)fname, &st) >= 0) | |
2638 { | |
2639 buf_store_time(curbuf, &st, fname); | |
2640 curbuf->b_mtime_read = curbuf->b_mtime; | |
2641 } | |
2642 #endif | |
7 | 2643 } |
2644 msg_scroll = msg_save; | |
2645 | |
2646 #ifdef FEAT_VIMINFO | |
2647 /* | |
2648 * Get the marks before executing autocommands, so they can be used there. | |
2649 */ | |
2650 check_marks_read(); | |
2651 #endif | |
2652 | |
2653 /* | |
6933 | 2654 * We remember if the last line of the read didn't have |
2655 * an eol even when 'binary' is off, to support turning 'fixeol' off, | |
2656 * or writing the read again with 'binary' on. The latter is required | |
2657 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work. | |
7 | 2658 */ |
2707 | 2659 curbuf->b_no_eol_lnum = read_no_eol_lnum; |
7 | 2660 |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2661 /* 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
|
2662 * different. */ |
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2663 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
|
2664 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
|
2665 |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2666 #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
|
2667 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2668 * 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
|
2669 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2670 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
|
2671 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2672 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
|
2673 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2674 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
|
2675 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
|
2676 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2677 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2678 |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
2679 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL)) |
7 | 2680 { |
2681 int m = msg_scroll; | |
2682 int n = msg_scrolled; | |
2683 | |
2684 /* Save the fileformat now, otherwise the buffer will be considered | |
2685 * modified if the format/encoding was automatically detected. */ | |
819 | 2686 if (set_options) |
7 | 2687 save_file_ff(curbuf); |
2688 | |
2689 /* | |
2690 * The output from the autocommands should not overwrite anything and | |
2691 * should not be overwritten: Set msg_scroll, restore its value if no | |
2692 * output was done. | |
2693 */ | |
2694 msg_scroll = TRUE; | |
2695 if (filtering) | |
2696 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname, | |
2697 FALSE, curbuf, eap); | |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
2698 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
|
2699 { |
7 | 2700 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname, |
2701 FALSE, curbuf, eap); | |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2702 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
|
2703 /* |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2704 * 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
|
2705 * 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
|
2706 */ |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2707 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
|
2708 TRUE, curbuf); |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2709 } |
7 | 2710 else |
2711 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname, | |
2712 FALSE, NULL, eap); | |
2713 if (msg_scrolled == n) | |
2714 msg_scroll = m; | |
2707 | 2715 # ifdef FEAT_EVAL |
7 | 2716 if (aborting()) /* autocmds may abort script processing */ |
2717 return FAIL; | |
2707 | 2718 # endif |
2719 } | |
2720 | |
7 | 2721 if (recoverymode && error) |
2722 return FAIL; | |
2723 return OK; | |
2724 } | |
2725 | |
9911
74e345d2878c
commit https://github.com/vim/vim/commit/f04507d132fbcb63999167ec006fc6e700b5af4f
Christian Brabandt <cb@256bit.org>
parents:
9828
diff
changeset
|
2726 #if defined(OPEN_CHR_FILES) || defined(PROTO) |
1313 | 2727 /* |
2728 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+", | |
2729 * which is the name of files used for process substitution output by | |
2730 * some shells on some operating systems, e.g., bash on SunOS. | |
2731 * Do not accept "/dev/fd/[012]", opening these may hang Vim. | |
2732 */ | |
9911
74e345d2878c
commit https://github.com/vim/vim/commit/f04507d132fbcb63999167ec006fc6e700b5af4f
Christian Brabandt <cb@256bit.org>
parents:
9828
diff
changeset
|
2733 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2734 is_dev_fd_file(char_u *fname) |
1313 | 2735 { |
2736 return (STRNCMP(fname, "/dev/fd/", 8) == 0 | |
2737 && VIM_ISDIGIT(fname[8]) | |
2738 && *skipdigits(fname + 9) == NUL | |
2739 && (fname[9] != NUL | |
2740 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2'))); | |
2741 } | |
2742 #endif | |
2743 | |
595 | 2744 #ifdef FEAT_MBYTE |
2745 | |
2746 /* | |
2747 * From the current line count and characters read after that, estimate the | |
2748 * line number where we are now. | |
2749 * Used for error messages that include a line number. | |
2750 */ | |
2751 static linenr_T | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2752 readfile_linenr( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2753 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
|
2754 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
|
2755 char_u *endp) /* end of more bytes read */ |
595 | 2756 { |
2757 char_u *s; | |
2758 linenr_T lnum; | |
2759 | |
2760 lnum = curbuf->b_ml.ml_line_count - linecnt + 1; | |
2761 for (s = p; s < endp; ++s) | |
2762 if (*s == '\n') | |
2763 ++lnum; | |
2764 return lnum; | |
2765 } | |
2766 #endif | |
2767 | |
7 | 2768 /* |
612 | 2769 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be |
2770 * equal to the buffer "buf". Used for calling readfile(). | |
7 | 2771 * Returns OK or FAIL. |
2772 */ | |
2773 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2774 prep_exarg(exarg_T *eap, buf_T *buf) |
7 | 2775 { |
13575
4df23d9bad47
patch 8.0.1660: the terminal API "drop" command doesn't support options
Christian Brabandt <cb@256bit.org>
parents:
13444
diff
changeset
|
2776 eap->cmd = alloc(15 |
7 | 2777 #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
|
2778 + (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
|
2779 #endif |
4df23d9bad47
patch 8.0.1660: the terminal API "drop" command doesn't support options
Christian Brabandt <cb@256bit.org>
parents:
13444
diff
changeset
|
2780 ); |
7 | 2781 if (eap->cmd == NULL) |
2782 return FAIL; | |
2783 | |
2784 #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
|
2785 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
|
2786 eap->force_enc = 8; |
612 | 2787 eap->bad_char = buf->b_bad_char; |
7 | 2788 #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
|
2789 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
|
2790 #endif |
4df23d9bad47
patch 8.0.1660: the terminal API "drop" command doesn't support options
Christian Brabandt <cb@256bit.org>
parents:
13444
diff
changeset
|
2791 eap->force_ff = *buf->b_p_ff; |
612 | 2792 |
2793 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN; | |
819 | 2794 eap->read_edit = FALSE; |
612 | 2795 eap->forceit = FALSE; |
7 | 2796 return OK; |
2797 } | |
2798 | |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2799 /* |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2800 * Set default or forced 'fileformat' and 'binary'. |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2801 */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2802 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2803 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
|
2804 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2805 /* set default 'fileformat' */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2806 if (set_options) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2807 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2808 if (eap != NULL && eap->force_ff != 0) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2809 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
|
2810 else if (*p_ffs != NUL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2811 set_fileformat(default_fileformat(), OPT_LOCAL); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2812 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2813 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2814 /* set or reset 'binary' */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2815 if (eap != NULL && eap->force_bin != 0) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2816 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2817 int oldval = curbuf->b_p_bin; |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2818 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2819 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
|
2820 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
|
2821 } |
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 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2824 #if defined(FEAT_MBYTE) || defined(PROTO) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2825 /* |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2826 * Set forced 'fileencoding'. |
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 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2829 set_forced_fenc(exarg_T *eap) |
5231
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->force_enc != 0) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2832 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2833 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
|
2834 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2835 if (fenc != NULL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2836 set_string_option_direct((char_u *)"fenc", -1, |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2837 fenc, OPT_FREE|OPT_LOCAL, 0); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2838 vim_free(fenc); |
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 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2841 |
7 | 2842 /* |
2843 * Find next fileencoding to use from 'fileencodings'. | |
2844 * "pp" points to fenc_next. It's advanced to the next item. | |
2845 * When there are no more items, an empty string is returned and *pp is set to | |
2846 * NULL. | |
2847 * When *pp is not set to NULL, the result is in allocated memory. | |
2848 */ | |
2849 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2850 next_fenc(char_u **pp) |
7 | 2851 { |
2852 char_u *p; | |
2853 char_u *r; | |
2854 | |
2855 if (**pp == NUL) | |
2856 { | |
2857 *pp = NULL; | |
2858 return (char_u *)""; | |
2859 } | |
2860 p = vim_strchr(*pp, ','); | |
2861 if (p == NULL) | |
2862 { | |
2863 r = enc_canonize(*pp); | |
2864 *pp += STRLEN(*pp); | |
2865 } | |
2866 else | |
2867 { | |
2868 r = vim_strnsave(*pp, (int)(p - *pp)); | |
2869 *pp = p + 1; | |
2870 if (r != NULL) | |
2871 { | |
2872 p = enc_canonize(r); | |
2873 vim_free(r); | |
2874 r = p; | |
2875 } | |
2876 } | |
2877 if (r == NULL) /* out of memory */ | |
2878 { | |
2879 r = (char_u *)""; | |
2880 *pp = NULL; | |
2881 } | |
2882 return r; | |
2883 } | |
2884 | |
2885 # ifdef FEAT_EVAL | |
2886 /* | |
2887 * Convert a file with the 'charconvert' expression. | |
2888 * This closes the file which is to be read, converts it and opens the | |
2889 * resulting file for reading. | |
2890 * Returns name of the resulting converted file (the caller should delete it | |
2891 * after reading it). | |
2892 * Returns NULL if the conversion failed ("*fdp" is not set) . | |
2893 */ | |
2894 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2895 readfile_charconvert( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2896 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
|
2897 char_u *fenc, /* converted from */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2898 int *fdp) /* in/out: file descriptor of file */ |
7 | 2899 { |
2900 char_u *tmpname; | |
2901 char_u *errmsg = NULL; | |
2902 | |
6721 | 2903 tmpname = vim_tempname('r', FALSE); |
7 | 2904 if (tmpname == NULL) |
2905 errmsg = (char_u *)_("Can't find temp file for conversion"); | |
2906 else | |
2907 { | |
2908 close(*fdp); /* close the input file, ignore errors */ | |
2909 *fdp = -1; | |
2910 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc, | |
2911 fname, tmpname) == FAIL) | |
2912 errmsg = (char_u *)_("Conversion with 'charconvert' failed"); | |
2913 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname, | |
2914 O_RDONLY | O_EXTRA, 0)) < 0) | |
2915 errmsg = (char_u *)_("can't read output of 'charconvert'"); | |
2916 } | |
2917 | |
2918 if (errmsg != NULL) | |
2919 { | |
2920 /* Don't use emsg(), it breaks mappings, the retry with | |
2921 * another type of conversion might still work. */ | |
2922 MSG(errmsg); | |
2923 if (tmpname != NULL) | |
2924 { | |
2925 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
|
2926 VIM_CLEAR(tmpname); |
7 | 2927 } |
2928 } | |
2929 | |
2930 /* If the input file is closed, open it (caller should check for error). */ | |
2931 if (*fdp < 0) | |
2932 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
2933 | |
2934 return tmpname; | |
2935 } | |
2936 # endif | |
2937 | |
2938 #endif | |
2939 | |
2940 #ifdef FEAT_VIMINFO | |
2941 /* | |
2942 * Read marks for the current buffer from the viminfo file, when we support | |
2943 * buffer marks and the buffer has a name. | |
2944 */ | |
2945 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2946 check_marks_read(void) |
7 | 2947 { |
2948 if (!curbuf->b_marks_read && get_viminfo_parameter('\'') > 0 | |
2949 && curbuf->b_ffname != NULL) | |
1733 | 2950 read_viminfo(NULL, VIF_WANT_MARKS); |
7 | 2951 |
2952 /* Always set b_marks_read; needed when 'viminfo' is changed to include | |
2953 * the ' parameter after opening a buffer. */ | |
2954 curbuf->b_marks_read = TRUE; | |
2955 } | |
2956 #endif | |
2957 | |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2958 #if defined(FEAT_CRYPT) || defined(PROTO) |
7 | 2959 /* |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2960 * Check for magic number used for encryption. Applies to the current buffer. |
7 | 2961 * If found, the magic number is removed from ptr[*sizep] and *sizep and |
2962 * *filesizep are updated. | |
2963 * Return the (new) encryption key, NULL for no encryption. | |
2964 */ | |
2965 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2966 check_for_cryptkey( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2967 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
|
2968 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
|
2969 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
|
2970 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
|
2971 int newfile, /* editing a new buffer */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2972 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
|
2973 int *did_ask) /* flag: whether already asked for key */ |
7 | 2974 { |
6122 | 2975 int method = crypt_method_nr_from_magic((char *)ptr, *sizep); |
5312 | 2976 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
|
2977 |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2978 if (method >= 0) |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2979 { |
5312 | 2980 /* Mark the buffer as read-only until the decryption has taken place. |
2981 * Avoids accidentally overwriting the file with garbage. */ | |
2982 curbuf->b_p_ro = TRUE; | |
2983 | |
6130 | 2984 /* Set the cryptmethod local to the buffer. */ |
6122 | 2985 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
|
2986 if (cryptkey == NULL && !*did_ask) |
7 | 2987 { |
2988 if (*curbuf->b_p_key) | |
2989 cryptkey = curbuf->b_p_key; | |
2990 else | |
2991 { | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2992 /* 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
|
2993 * 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
|
2994 * 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
|
2995 * Happens when retrying to detect encoding. */ |
2267 | 2996 smsg((char_u *)_(need_key_msg), fname); |
2997 msg_scroll = TRUE; | |
6353 | 2998 crypt_check_method(method); |
6122 | 2999 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
|
3000 *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
|
3001 |
7 | 3002 /* check if empty key entered */ |
3003 if (cryptkey != NULL && *cryptkey == NUL) | |
3004 { | |
3005 if (cryptkey != curbuf->b_p_key) | |
3006 vim_free(cryptkey); | |
3007 cryptkey = NULL; | |
3008 } | |
3009 } | |
3010 } | |
3011 | |
3012 if (cryptkey != NULL) | |
3013 { | |
6122 | 3014 int header_len; |
3015 | |
3016 curbuf->b_cryptstate = crypt_create_from_header( | |
3017 method, cryptkey, ptr); | |
3018 crypt_set_cm_option(curbuf, method); | |
3019 | |
3020 /* Remove cryptmethod specific header from the text. */ | |
3021 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
|
3022 if (*sizep <= header_len) |
fb1fde4fcff7
commit https://github.com/vim/vim/commit/680e015bfe19be6772d3bd754486fbd45c1a9d3b
Christian Brabandt <cb@256bit.org>
parents:
10086
diff
changeset
|
3023 /* 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
|
3024 return NULL; |
6122 | 3025 *filesizep += header_len; |
3026 *sizep -= header_len; | |
3027 mch_memmove(ptr, ptr + header_len, (size_t)*sizep); | |
3028 | |
5312 | 3029 /* Restore the read-only flag. */ |
3030 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
|
3031 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3032 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3033 /* 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
|
3034 * 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
|
3035 else if (newfile && *curbuf->b_p_key != NUL && !starting) |
7 | 3036 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL); |
3037 | |
3038 return cryptkey; | |
3039 } | |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3040 #endif /* FEAT_CRYPT */ |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3041 |
7 | 3042 #ifdef UNIX |
3043 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3044 set_file_time( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3045 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3046 time_t atime, /* access time */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3047 time_t mtime) /* modification time */ |
7 | 3048 { |
3049 # if defined(HAVE_UTIME) && defined(HAVE_UTIME_H) | |
3050 struct utimbuf buf; | |
3051 | |
3052 buf.actime = atime; | |
3053 buf.modtime = mtime; | |
3054 (void)utime((char *)fname, &buf); | |
3055 # else | |
3056 # if defined(HAVE_UTIMES) | |
3057 struct timeval tvp[2]; | |
3058 | |
3059 tvp[0].tv_sec = atime; | |
3060 tvp[0].tv_usec = 0; | |
3061 tvp[1].tv_sec = mtime; | |
3062 tvp[1].tv_usec = 0; | |
3063 # ifdef NeXT | |
3064 (void)utimes((char *)fname, tvp); | |
3065 # else | |
3066 (void)utimes((char *)fname, (const struct timeval *)&tvp); | |
3067 # endif | |
3068 # endif | |
3069 # endif | |
3070 } | |
3071 #endif /* UNIX */ | |
3072 | |
22 | 3073 #if defined(VMS) && !defined(MIN) |
3074 /* Older DECC compiler for VAX doesn't define MIN() */ | |
3075 # define MIN(a, b) ((a) < (b) ? (a) : (b)) | |
3076 #endif | |
3077 | |
7 | 3078 /* |
1303 | 3079 * Return TRUE if a file appears to be read-only from the file permissions. |
3080 */ | |
3081 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3082 check_file_readonly( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3083 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
|
3084 int perm) /* known permissions on file */ |
1303 | 3085 { |
3086 #ifndef USE_MCH_ACCESS | |
3087 int fd = 0; | |
3088 #endif | |
3089 | |
3090 return ( | |
3091 #ifdef USE_MCH_ACCESS | |
3092 # ifdef UNIX | |
3093 (perm & 0222) == 0 || | |
3094 # endif | |
3095 mch_access((char *)fname, W_OK) | |
3096 #else | |
3097 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0 | |
3098 ? TRUE : (close(fd), FALSE) | |
3099 #endif | |
3100 ); | |
3101 } | |
3102 | |
3103 | |
3104 /* | |
589 | 3105 * buf_write() - write to file "fname" lines "start" through "end" |
7 | 3106 * |
3107 * We do our own buffering here because fwrite() is so slow. | |
3108 * | |
589 | 3109 * If "forceit" is true, we don't care for errors when attempting backups. |
3110 * In case of an error everything possible is done to restore the original | |
1698 | 3111 * file. But when "forceit" is TRUE, we risk losing it. |
589 | 3112 * |
3113 * When "reset_changed" is TRUE and "append" == FALSE and "start" == 1 and | |
3114 * "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed. | |
7 | 3115 * |
3116 * This function must NOT use NameBuff (because it's called by autowrite()). | |
3117 * | |
3118 * return FAIL for failure, OK otherwise | |
3119 */ | |
3120 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3121 buf_write( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3122 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3123 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3124 char_u *sfname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3125 linenr_T start, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3126 linenr_T end, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3127 exarg_T *eap, /* for forced 'ff' and 'fenc', can be |
7 | 3128 NULL! */ |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3129 int append, /* append to the file */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3130 int forceit, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3131 int reset_changed, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3132 int filtering) |
7 | 3133 { |
3134 int fd; | |
3135 char_u *backup = NULL; | |
3136 int backup_copy = FALSE; /* copy the original file? */ | |
3137 int dobackup; | |
3138 char_u *ffname; | |
3139 char_u *wfname = NULL; /* name of file to write to */ | |
3140 char_u *s; | |
3141 char_u *ptr; | |
3142 char_u c; | |
3143 int len; | |
3144 linenr_T lnum; | |
3145 long nchars; | |
3146 char_u *errmsg = NULL; | |
1947 | 3147 int errmsg_allocated = FALSE; |
7 | 3148 char_u *errnum = NULL; |
3149 char_u *buffer; | |
3150 char_u smallbuf[SMBUFSIZE]; | |
3151 char_u *backup_ext; | |
3152 int bufsize; | |
3153 long perm; /* file permissions */ | |
3154 int retval = OK; | |
3155 int newfile = FALSE; /* TRUE if file doesn't exist yet */ | |
3156 int msg_save = msg_scroll; | |
3157 int overwriting; /* TRUE if writing over original */ | |
3158 int no_eol = FALSE; /* no end-of-line written */ | |
3159 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
|
3160 stat_T st_old; |
7 | 3161 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
|
3162 int checking_conversion; |
7 | 3163 int file_readonly = FALSE; /* overwritten file is read-only */ |
3164 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
|
3165 #if defined(UNIX) /*XXX fix me sometime? */ |
7 | 3166 int made_writable = FALSE; /* 'w' bit has been set */ |
3167 #endif | |
3168 /* writing everything */ | |
3169 int whole = (start == 1 && end == buf->b_ml.ml_line_count); | |
3170 linenr_T old_line_count = buf->b_ml.ml_line_count; | |
3171 int attr; | |
3172 int fileformat; | |
3173 int write_bin; | |
3174 struct bw_info write_info; /* info for buf_write_bytes() */ | |
3175 #ifdef FEAT_MBYTE | |
3176 int converted = FALSE; | |
3177 int notconverted = FALSE; | |
3178 char_u *fenc; /* effective 'fileencoding' */ | |
3179 char_u *fenc_tofree = NULL; /* allocated "fenc" */ | |
3180 #endif | |
3181 #ifdef HAS_BW_FLAGS | |
3182 int wb_flags = 0; | |
3183 #endif | |
3184 #ifdef HAVE_ACL | |
3185 vim_acl_T acl = NULL; /* ACL copied from original file to | |
3186 backup or new file */ | |
3187 #endif | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3188 #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
|
3189 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
|
3190 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
|
3191 #endif |
6243 | 3192 unsigned int bkc = get_bkc_value(buf); |
7 | 3193 |
3194 if (fname == NULL || *fname == NUL) /* safety check */ | |
3195 return FAIL; | |
2028 | 3196 if (buf->b_ml.ml_mfp == NULL) |
3197 { | |
3198 /* This can happen during startup when there is a stray "w" in the | |
3199 * vimrc file. */ | |
3200 EMSG(_(e_emptybuf)); | |
3201 return FAIL; | |
3202 } | |
7 | 3203 |
3204 /* | |
3205 * Disallow writing from .exrc and .vimrc in current directory for | |
3206 * security reasons. | |
3207 */ | |
3208 if (check_secure()) | |
3209 return FAIL; | |
3210 | |
3211 /* Avoid a crash for a long name. */ | |
3212 if (STRLEN(fname) >= MAXPATHL) | |
3213 { | |
3214 EMSG(_(e_longname)); | |
3215 return FAIL; | |
3216 } | |
3217 | |
3218 #ifdef FEAT_MBYTE | |
3219 /* must init bw_conv_buf and bw_iconv_fd before jumping to "fail" */ | |
3220 write_info.bw_conv_buf = NULL; | |
3221 write_info.bw_conv_error = FALSE; | |
1947 | 3222 write_info.bw_conv_error_lnum = 0; |
7 | 3223 write_info.bw_restlen = 0; |
3224 # ifdef USE_ICONV | |
3225 write_info.bw_iconv_fd = (iconv_t)-1; | |
3226 # endif | |
3227 #endif | |
6122 | 3228 #ifdef FEAT_CRYPT |
3229 write_info.bw_buffer = buf; | |
3230 #endif | |
7 | 3231 |
167 | 3232 /* After writing a file changedtick changes but we don't want to display |
3233 * the line. */ | |
3234 ex_no_reprint = TRUE; | |
3235 | |
7 | 3236 /* |
3237 * If there is no file name yet, use the one for the written file. | |
3238 * BF_NOTEDITED is set to reflect this (in case the write fails). | |
3239 * Don't do this when the write is for a filter command. | |
589 | 3240 * Don't do this when appending. |
3241 * Only do this when 'cpoptions' contains the 'F' flag. | |
7 | 3242 */ |
633 | 3243 if (buf->b_ffname == NULL |
3244 && reset_changed | |
7 | 3245 && whole |
3246 && buf == curbuf | |
236 | 3247 #ifdef FEAT_QUICKFIX |
3248 && !bt_nofile(buf) | |
3249 #endif | |
7 | 3250 && !filtering |
589 | 3251 && (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL) |
7 | 3252 && vim_strchr(p_cpo, CPO_FNAMEW) != NULL) |
3253 { | |
633 | 3254 if (set_rw_fname(fname, sfname) == FAIL) |
7 | 3255 return FAIL; |
633 | 3256 buf = curbuf; /* just in case autocmds made "buf" invalid */ |
7 | 3257 } |
3258 | |
3259 if (sfname == NULL) | |
3260 sfname = fname; | |
3261 /* | |
3262 * For Unix: Use the short file name whenever possible. | |
3263 * Avoids problems with networks and when directory names are changed. | |
3264 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to | |
3265 * another directory, which we don't detect | |
3266 */ | |
3267 ffname = fname; /* remember full fname */ | |
3268 #ifdef UNIX | |
3269 fname = sfname; | |
3270 #endif | |
3271 | |
3272 if (buf->b_ffname != NULL && fnamecmp(ffname, buf->b_ffname) == 0) | |
3273 overwriting = TRUE; | |
3274 else | |
3275 overwriting = FALSE; | |
3276 | |
3277 if (exiting) | |
4352 | 3278 settmode(TMODE_COOK); /* when exiting allow typeahead now */ |
7 | 3279 |
3280 ++no_wait_return; /* don't wait for return yet */ | |
3281 | |
3282 /* | |
3283 * Set '[ and '] marks to the lines to be written. | |
3284 */ | |
3285 buf->b_op_start.lnum = start; | |
3286 buf->b_op_start.col = 0; | |
3287 buf->b_op_end.lnum = end; | |
3288 buf->b_op_end.col = 0; | |
3289 | |
3290 { | |
3291 aco_save_T aco; | |
3292 int buf_ffname = FALSE; | |
3293 int buf_sfname = FALSE; | |
3294 int buf_fname_f = FALSE; | |
3295 int buf_fname_s = FALSE; | |
3296 int did_cmd = FALSE; | |
17 | 3297 int nofile_err = FALSE; |
161 | 3298 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
|
3299 bufref_T bufref; |
7 | 3300 |
3301 /* | |
4352 | 3302 * Apply PRE autocommands. |
7 | 3303 * Set curbuf to the buffer to be written. |
3304 * Careful: The autocommands may call buf_write() recursively! | |
3305 */ | |
3306 if (ffname == buf->b_ffname) | |
3307 buf_ffname = TRUE; | |
3308 if (sfname == buf->b_sfname) | |
3309 buf_sfname = TRUE; | |
3310 if (fname == buf->b_ffname) | |
3311 buf_fname_f = TRUE; | |
3312 if (fname == buf->b_sfname) | |
3313 buf_fname_s = TRUE; | |
3314 | |
3315 /* set curwin/curbuf to buf and save a few things */ | |
3316 aucmd_prepbuf(&aco, buf); | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
3317 set_bufref(&bufref, buf); |
7 | 3318 |
3319 if (append) | |
3320 { | |
3321 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD, | |
3322 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3323 { |
635 | 3324 #ifdef FEAT_QUICKFIX |
18 | 3325 if (overwriting && bt_nofile(curbuf)) |
17 | 3326 nofile_err = TRUE; |
3327 else | |
635 | 3328 #endif |
17 | 3329 apply_autocmds_exarg(EVENT_FILEAPPENDPRE, |
7 | 3330 sfname, sfname, FALSE, curbuf, eap); |
17 | 3331 } |
7 | 3332 } |
3333 else if (filtering) | |
3334 { | |
3335 apply_autocmds_exarg(EVENT_FILTERWRITEPRE, | |
3336 NULL, sfname, FALSE, curbuf, eap); | |
3337 } | |
3338 else if (reset_changed && whole) | |
3339 { | |
3036 | 3340 int was_changed = curbufIsChanged(); |
3341 | |
3342 did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD, | |
3343 sfname, sfname, FALSE, curbuf, eap); | |
3344 if (did_cmd) | |
3345 { | |
3346 if (was_changed && !curbufIsChanged()) | |
3347 { | |
3348 /* Written everything correctly and BufWriteCmd has reset | |
3349 * 'modified': Correct the undo information so that an | |
3350 * undo now sets 'modified'. */ | |
3351 u_unchanged(curbuf); | |
3352 u_update_save_nr(curbuf); | |
3353 } | |
3354 } | |
3355 else | |
17 | 3356 { |
635 | 3357 #ifdef FEAT_QUICKFIX |
179 | 3358 if (overwriting && bt_nofile(curbuf)) |
17 | 3359 nofile_err = TRUE; |
3360 else | |
635 | 3361 #endif |
17 | 3362 apply_autocmds_exarg(EVENT_BUFWRITEPRE, |
7 | 3363 sfname, sfname, FALSE, curbuf, eap); |
17 | 3364 } |
7 | 3365 } |
3366 else | |
3367 { | |
3368 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD, | |
3369 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3370 { |
635 | 3371 #ifdef FEAT_QUICKFIX |
179 | 3372 if (overwriting && bt_nofile(curbuf)) |
17 | 3373 nofile_err = TRUE; |
3374 else | |
635 | 3375 #endif |
17 | 3376 apply_autocmds_exarg(EVENT_FILEWRITEPRE, |
7 | 3377 sfname, sfname, FALSE, curbuf, eap); |
17 | 3378 } |
7 | 3379 } |
3380 | |
3381 /* restore curwin/curbuf and a few other things */ | |
3382 aucmd_restbuf(&aco); | |
3383 | |
3384 /* | |
3385 * In three situations we return here and don't write the file: | |
3386 * 1. the autocommands deleted or unloaded the buffer. | |
3387 * 2. The autocommands abort script processing. | |
3388 * 3. If one of the "Cmd" autocommands was executed. | |
3389 */ | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
3390 if (!bufref_valid(&bufref)) |
7 | 3391 buf = NULL; |
161 | 3392 if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline) |
532 | 3393 || did_cmd || nofile_err |
3394 #ifdef FEAT_EVAL | |
3395 || aborting() | |
3396 #endif | |
3397 ) | |
7 | 3398 { |
3399 --no_wait_return; | |
3400 msg_scroll = msg_save; | |
17 | 3401 if (nofile_err) |
3402 EMSG(_("E676: No matching autocommands for acwrite buffer")); | |
3403 | |
532 | 3404 if (nofile_err |
3405 #ifdef FEAT_EVAL | |
3406 || aborting() | |
3407 #endif | |
3408 ) | |
7 | 3409 /* An aborting error, interrupt or exception in the |
3410 * autocommands. */ | |
3411 return FAIL; | |
3412 if (did_cmd) | |
3413 { | |
3414 if (buf == NULL) | |
3415 /* The buffer was deleted. We assume it was written | |
3416 * (can't retry anyway). */ | |
3417 return OK; | |
3418 if (overwriting) | |
3419 { | |
3420 /* Assume the buffer was written, update the timestamp. */ | |
3421 ml_timestamp(buf); | |
589 | 3422 if (append) |
3423 buf->b_flags &= ~BF_NEW; | |
3424 else | |
3425 buf->b_flags &= ~BF_WRITE_MASK; | |
7 | 3426 } |
589 | 3427 if (reset_changed && buf->b_changed && !append |
39 | 3428 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)) |
7 | 3429 /* Buffer still changed, the autocommands didn't work |
3430 * properly. */ | |
3431 return FAIL; | |
3432 return OK; | |
3433 } | |
3434 #ifdef FEAT_EVAL | |
3435 if (!aborting()) | |
3436 #endif | |
3437 EMSG(_("E203: Autocommands deleted or unloaded buffer to be written")); | |
3438 return FAIL; | |
3439 } | |
3440 | |
3441 /* | |
3442 * The autocommands may have changed the number of lines in the file. | |
3443 * When writing the whole file, adjust the end. | |
3444 * When writing part of the file, assume that the autocommands only | |
3445 * changed the number of lines that are to be written (tricky!). | |
3446 */ | |
3447 if (buf->b_ml.ml_line_count != old_line_count) | |
3448 { | |
3449 if (whole) /* write all */ | |
3450 end = buf->b_ml.ml_line_count; | |
3451 else if (buf->b_ml.ml_line_count > old_line_count) /* more lines */ | |
3452 end += buf->b_ml.ml_line_count - old_line_count; | |
3453 else /* less lines */ | |
3454 { | |
3455 end -= old_line_count - buf->b_ml.ml_line_count; | |
3456 if (end < start) | |
3457 { | |
3458 --no_wait_return; | |
3459 msg_scroll = msg_save; | |
3460 EMSG(_("E204: Autocommand changed number of lines in unexpected way")); | |
3461 return FAIL; | |
3462 } | |
3463 } | |
3464 } | |
3465 | |
3466 /* | |
3467 * The autocommands may have changed the name of the buffer, which may | |
3468 * be kept in fname, ffname and sfname. | |
3469 */ | |
3470 if (buf_ffname) | |
3471 ffname = buf->b_ffname; | |
3472 if (buf_sfname) | |
3473 sfname = buf->b_sfname; | |
3474 if (buf_fname_f) | |
3475 fname = buf->b_ffname; | |
3476 if (buf_fname_s) | |
3477 fname = buf->b_sfname; | |
3478 } | |
3479 | |
3480 #ifdef FEAT_NETBEANS_INTG | |
2210 | 3481 if (netbeans_active() && isNetbeansBuffer(buf)) |
7 | 3482 { |
3483 if (whole) | |
3484 { | |
3485 /* | |
3486 * b_changed can be 0 after an undo, but we still need to write | |
3487 * the buffer to NetBeans. | |
3488 */ | |
3489 if (buf->b_changed || isNetbeansModified(buf)) | |
3490 { | |
33 | 3491 --no_wait_return; /* may wait for return now */ |
3492 msg_scroll = msg_save; | |
3493 netbeans_save_buffer(buf); /* no error checking... */ | |
7 | 3494 return retval; |
3495 } | |
3496 else | |
3497 { | |
3498 errnum = (char_u *)"E656: "; | |
1653 | 3499 errmsg = (char_u *)_("NetBeans disallows writes of unmodified buffers"); |
7 | 3500 buffer = NULL; |
3501 goto fail; | |
3502 } | |
3503 } | |
3504 else | |
3505 { | |
3506 errnum = (char_u *)"E657: "; | |
3507 errmsg = (char_u *)_("Partial writes disallowed for NetBeans buffers"); | |
3508 buffer = NULL; | |
3509 goto fail; | |
3510 } | |
3511 } | |
3512 #endif | |
3513 | |
3514 if (shortmess(SHM_OVER) && !exiting) | |
3515 msg_scroll = FALSE; /* overwrite previous file message */ | |
3516 else | |
3517 msg_scroll = TRUE; /* don't overwrite previous file message */ | |
3518 if (!filtering) | |
3519 filemess(buf, | |
3520 #ifndef UNIX | |
3521 sfname, | |
3522 #else | |
3523 fname, | |
3524 #endif | |
3525 (char_u *)"", 0); /* show that we are busy */ | |
3526 msg_scroll = FALSE; /* always overwrite the file message now */ | |
3527 | |
3528 buffer = alloc(BUFSIZE); | |
3529 if (buffer == NULL) /* can't allocate big buffer, use small | |
3530 * one (to be able to write when out of | |
3531 * memory) */ | |
3532 { | |
3533 buffer = smallbuf; | |
3534 bufsize = SMBUFSIZE; | |
3535 } | |
3536 else | |
3537 bufsize = BUFSIZE; | |
3538 | |
3539 /* | |
3540 * Get information about original file (if there is one). | |
3541 */ | |
7410
08e62c4fc17d
commit https://github.com/vim/vim/commit/53076830fea6df737455523f7e235bfe4f79864d
Christian Brabandt <cb@256bit.org>
parents:
7408
diff
changeset
|
3542 #if defined(UNIX) |
1438 | 3543 st_old.st_dev = 0; |
3544 st_old.st_ino = 0; | |
7 | 3545 perm = -1; |
3546 if (mch_stat((char *)fname, &st_old) < 0) | |
3547 newfile = TRUE; | |
3548 else | |
3549 { | |
3550 perm = st_old.st_mode; | |
3551 if (!S_ISREG(st_old.st_mode)) /* not a file */ | |
3552 { | |
3553 if (S_ISDIR(st_old.st_mode)) | |
3554 { | |
3555 errnum = (char_u *)"E502: "; | |
3556 errmsg = (char_u *)_("is a directory"); | |
3557 goto fail; | |
3558 } | |
3559 if (mch_nodetype(fname) != NODE_WRITABLE) | |
3560 { | |
3561 errnum = (char_u *)"E503: "; | |
3562 errmsg = (char_u *)_("is not a file or writable device"); | |
3563 goto fail; | |
3564 } | |
3565 /* It's a device of some kind (or a fifo) which we can write to | |
3566 * but for which we can't make a backup. */ | |
3567 device = TRUE; | |
3568 newfile = TRUE; | |
3569 perm = -1; | |
3570 } | |
3571 } | |
3572 #else /* !UNIX */ | |
3573 /* | |
3574 * Check for a writable device name. | |
3575 */ | |
3576 c = mch_nodetype(fname); | |
3577 if (c == NODE_OTHER) | |
3578 { | |
3579 errnum = (char_u *)"E503: "; | |
3580 errmsg = (char_u *)_("is not a file or writable device"); | |
3581 goto fail; | |
3582 } | |
3583 if (c == NODE_WRITABLE) | |
3584 { | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3585 # if defined(MSWIN) |
1004 | 3586 /* MS-Windows allows opening a device, but we will probably get stuck |
3587 * trying to write to it. */ | |
3588 if (!p_odev) | |
3589 { | |
3590 errnum = (char_u *)"E796: "; | |
3591 errmsg = (char_u *)_("writing to device disabled with 'opendevice' option"); | |
3592 goto fail; | |
3593 } | |
3594 # endif | |
7 | 3595 device = TRUE; |
3596 newfile = TRUE; | |
3597 perm = -1; | |
3598 } | |
3599 else | |
3600 { | |
3601 perm = mch_getperm(fname); | |
3602 if (perm < 0) | |
3603 newfile = TRUE; | |
3604 else if (mch_isdir(fname)) | |
3605 { | |
3606 errnum = (char_u *)"E502: "; | |
3607 errmsg = (char_u *)_("is a directory"); | |
3608 goto fail; | |
3609 } | |
3610 if (overwriting) | |
3611 (void)mch_stat((char *)fname, &st_old); | |
3612 } | |
3613 #endif /* !UNIX */ | |
3614 | |
3615 if (!device && !newfile) | |
3616 { | |
3617 /* | |
3618 * Check if the file is really writable (when renaming the file to | |
3619 * make a backup we won't discover it later). | |
3620 */ | |
1303 | 3621 file_readonly = check_file_readonly(fname, (int)perm); |
3622 | |
7 | 3623 if (!forceit && file_readonly) |
3624 { | |
3625 if (vim_strchr(p_cpo, CPO_FWRITE) != NULL) | |
3626 { | |
3627 errnum = (char_u *)"E504: "; | |
3628 errmsg = (char_u *)_(err_readonly); | |
3629 } | |
3630 else | |
3631 { | |
3632 errnum = (char_u *)"E505: "; | |
3633 errmsg = (char_u *)_("is read-only (add ! to override)"); | |
3634 } | |
3635 goto fail; | |
3636 } | |
3637 | |
3638 /* | |
3639 * Check if the timestamp hasn't changed since reading the file. | |
3640 */ | |
3641 if (overwriting) | |
3642 { | |
3643 retval = check_mtime(buf, &st_old); | |
3644 if (retval == FAIL) | |
3645 goto fail; | |
3646 } | |
3647 } | |
3648 | |
3649 #ifdef HAVE_ACL | |
3650 /* | |
3651 * For systems that support ACL: get the ACL from the original file. | |
3652 */ | |
3653 if (!newfile) | |
3654 acl = mch_get_acl(fname); | |
3655 #endif | |
3656 | |
3657 /* | |
3658 * If 'backupskip' is not empty, don't make a backup for some files. | |
3659 */ | |
3660 dobackup = (p_wb || p_bk || *p_pm != NUL); | |
3661 #ifdef FEAT_WILDIGN | |
3662 if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname)) | |
3663 dobackup = FALSE; | |
3664 #endif | |
3665 | |
3666 /* | |
3667 * Save the value of got_int and reset it. We don't want a previous | |
3668 * interruption cancel writing, only hitting CTRL-C while writing should | |
3669 * abort it. | |
3670 */ | |
3671 prev_got_int = got_int; | |
3672 got_int = FALSE; | |
3673 | |
3674 /* Mark the buffer as 'being saved' to prevent changed buffer warnings */ | |
3675 buf->b_saving = TRUE; | |
3676 | |
3677 /* | |
3678 * If we are not appending or filtering, the file exists, and the | |
3679 * 'writebackup', 'backup' or 'patchmode' option is set, need a backup. | |
3680 * When 'patchmode' is set also make a backup when appending. | |
3681 * | |
3682 * Do not make any backup, if 'writebackup' and 'backup' are both switched | |
3683 * off. This helps when editing large files on almost-full disks. | |
3684 */ | |
3685 if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup) | |
3686 { | |
3687 #if defined(UNIX) || defined(WIN32) | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
3688 stat_T st; |
7 | 3689 #endif |
3690 | |
6243 | 3691 if ((bkc & BKC_YES) || append) /* "yes" */ |
7 | 3692 backup_copy = TRUE; |
3693 #if defined(UNIX) || defined(WIN32) | |
6243 | 3694 else if ((bkc & BKC_AUTO)) /* "auto" */ |
7 | 3695 { |
3696 int i; | |
3697 | |
3698 # ifdef UNIX | |
3699 /* | |
3700 * Don't rename the file when: | |
3701 * - it's a hard link | |
3702 * - it's a symbolic link | |
3703 * - we don't have write permission in the directory | |
3704 * - we can't set the owner/group of the new file | |
3705 */ | |
3706 if (st_old.st_nlink > 1 | |
3707 || mch_lstat((char *)fname, &st) < 0 | |
3708 || st.st_dev != st_old.st_dev | |
557 | 3709 || st.st_ino != st_old.st_ino |
3710 # ifndef HAVE_FCHOWN | |
3711 || st.st_uid != st_old.st_uid | |
3712 || st.st_gid != st_old.st_gid | |
3713 # endif | |
3714 ) | |
7 | 3715 backup_copy = TRUE; |
3716 else | |
696 | 3717 # else |
3718 # ifdef WIN32 | |
3719 /* On NTFS file systems hard links are possible. */ | |
3720 if (mch_is_linked(fname)) | |
3721 backup_copy = TRUE; | |
3722 else | |
3723 # endif | |
7 | 3724 # endif |
3725 { | |
3726 /* | |
3727 * Check if we can create a file and set the owner/group to | |
3728 * the ones from the original file. | |
3729 * First find a file name that doesn't exist yet (use some | |
3730 * arbitrary numbers). | |
3731 */ | |
3732 STRCPY(IObuff, fname); | |
3733 for (i = 4913; ; i += 123) | |
3734 { | |
3735 sprintf((char *)gettail(IObuff), "%d", i); | |
557 | 3736 if (mch_lstat((char *)IObuff, &st) < 0) |
7 | 3737 break; |
3738 } | |
557 | 3739 fd = mch_open((char *)IObuff, |
3740 O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm); | |
7 | 3741 if (fd < 0) /* can't write in directory */ |
3742 backup_copy = TRUE; | |
3743 else | |
3744 { | |
3745 # ifdef UNIX | |
557 | 3746 # ifdef HAVE_FCHOWN |
14730
193471015e1a
patch 8.1.0377: xdiff doesn't use the Vim memory allocation functions
Christian Brabandt <cb@256bit.org>
parents:
14700
diff
changeset
|
3747 vim_ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
557 | 3748 # endif |
7 | 3749 if (mch_stat((char *)IObuff, &st) < 0 |
3750 || st.st_uid != st_old.st_uid | |
3751 || st.st_gid != st_old.st_gid | |
1877 | 3752 || (long)st.st_mode != perm) |
7 | 3753 backup_copy = TRUE; |
3754 # endif | |
567 | 3755 /* Close the file before removing it, on MS-Windows we |
3756 * can't delete an open file. */ | |
3757 close(fd); | |
7 | 3758 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
|
3759 # 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
|
3760 /* 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
|
3761 * 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
|
3762 * second. */ |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3763 { |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3764 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
|
3765 |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3766 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
|
3767 { |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3768 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
|
3769 break; |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3770 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
|
3771 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
|
3772 } |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3773 } |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3774 # endif |
7 | 3775 } |
3776 } | |
3777 } | |
3778 | |
3779 /* | |
3780 * Break symlinks and/or hardlinks if we've been asked to. | |
3781 */ | |
6243 | 3782 if ((bkc & BKC_BREAKSYMLINK) || (bkc & BKC_BREAKHARDLINK)) |
7 | 3783 { |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3784 # ifdef UNIX |
7 | 3785 int lstat_res; |
3786 | |
3787 lstat_res = mch_lstat((char *)fname, &st); | |
3788 | |
3789 /* Symlinks. */ | |
6243 | 3790 if ((bkc & BKC_BREAKSYMLINK) |
7 | 3791 && lstat_res == 0 |
3792 && st.st_ino != st_old.st_ino) | |
3793 backup_copy = FALSE; | |
3794 | |
3795 /* Hardlinks. */ | |
6243 | 3796 if ((bkc & BKC_BREAKHARDLINK) |
7 | 3797 && st_old.st_nlink > 1 |
3798 && (lstat_res != 0 || st.st_ino == st_old.st_ino)) | |
3799 backup_copy = FALSE; | |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3800 # else |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3801 # if defined(WIN32) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3802 /* Symlinks. */ |
6243 | 3803 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
|
3804 backup_copy = FALSE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3805 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3806 /* Hardlinks. */ |
6243 | 3807 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
|
3808 backup_copy = FALSE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3809 # endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3810 # endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3811 } |
7 | 3812 |
3813 #endif | |
3814 | |
3815 /* make sure we have a valid backup extension to use */ | |
3816 if (*p_bex == NUL) | |
3817 backup_ext = (char_u *)".bak"; | |
3818 else | |
3819 backup_ext = p_bex; | |
3820 | |
3821 if (backup_copy | |
3822 && (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) >= 0) | |
3823 { | |
3824 int bfd; | |
3825 char_u *copybuf, *wp; | |
3826 int some_error = FALSE; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
3827 stat_T st_new; |
7 | 3828 char_u *dirp; |
3829 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
|
3830 #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
|
3831 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
|
3832 #endif |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3833 #if defined(UNIX) |
7 | 3834 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
|
3835 mode_t umask_save; |
7 | 3836 #endif |
3837 | |
3838 copybuf = alloc(BUFSIZE + 1); | |
3839 if (copybuf == NULL) | |
3840 { | |
3841 some_error = TRUE; /* out of memory */ | |
3842 goto nobackup; | |
3843 } | |
3844 | |
3845 /* | |
3846 * Try to make the backup in each directory in the 'bdir' option. | |
3847 * | |
3848 * Unix semantics has it, that we may have a writable file, | |
3849 * that cannot be recreated with a simple open(..., O_CREAT, ) e.g: | |
3850 * - the directory is not writable, | |
3851 * - the file may be a symbolic link, | |
3852 * - the file may belong to another user/group, etc. | |
3853 * | |
3854 * For these reasons, the existing writable file must be truncated | |
3855 * and reused. Creation of a backup COPY will be attempted. | |
3856 */ | |
3857 dirp = p_bdir; | |
3858 while (*dirp) | |
3859 { | |
3860 #ifdef UNIX | |
3861 st_new.st_ino = 0; | |
3862 st_new.st_dev = 0; | |
3863 st_new.st_gid = 0; | |
3864 #endif | |
3865 | |
3866 /* | |
3867 * Isolate one directory name, using an entry in 'bdir'. | |
3868 */ | |
3869 (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
|
3870 |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3871 #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
|
3872 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
|
3873 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
|
3874 // 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
|
3875 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
|
3876 { |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3877 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
|
3878 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
|
3879 } |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3880 #endif |
7 | 3881 rootname = get_file_in_dir(fname, copybuf); |
3882 if (rootname == NULL) | |
3883 { | |
3884 some_error = TRUE; /* out of memory */ | |
3885 goto nobackup; | |
3886 } | |
3887 | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3888 #if defined(UNIX) |
7 | 3889 did_set_shortname = FALSE; |
3890 #endif | |
3891 | |
3892 /* | |
3893 * May try twice if 'shortname' not set. | |
3894 */ | |
3895 for (;;) | |
3896 { | |
3897 /* | |
14475
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3898 * Make the backup file name. |
7 | 3899 */ |
14475
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3900 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
|
3901 backup = buf_modname((buf->b_p_sn || buf->b_shortname), |
7 | 3902 rootname, backup_ext, FALSE); |
3903 if (backup == NULL) | |
3904 { | |
3905 vim_free(rootname); | |
3906 some_error = TRUE; /* out of memory */ | |
3907 goto nobackup; | |
3908 } | |
3909 | |
3910 /* | |
3911 * Check if backup file already exists. | |
3912 */ | |
3913 if (mch_stat((char *)backup, &st_new) >= 0) | |
3914 { | |
3915 #ifdef UNIX | |
3916 /* | |
3917 * Check if backup file is same as original file. | |
3918 * May happen when modname() gave the same file back. | |
3919 * E.g. silly link, or file name-length reached. | |
3920 * If we don't check here, we either ruin the file | |
3921 * when copying or erase it after writing. jw. | |
3922 */ | |
3923 if (st_new.st_dev == st_old.st_dev | |
3924 && st_new.st_ino == st_old.st_ino) | |
3925 { | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
3926 VIM_CLEAR(backup); /* no backup file to delete */ |
7 | 3927 /* |
3928 * may try again with 'shortname' set | |
3929 */ | |
3930 if (!(buf->b_shortname || buf->b_p_sn)) | |
3931 { | |
3932 buf->b_shortname = TRUE; | |
3933 did_set_shortname = TRUE; | |
3934 continue; | |
3935 } | |
3936 /* setting shortname didn't help */ | |
3937 if (did_set_shortname) | |
3938 buf->b_shortname = FALSE; | |
3939 break; | |
3940 } | |
3941 #endif | |
3942 | |
3943 /* | |
3944 * If we are not going to keep the backup file, don't | |
3945 * delete an existing one, try to use another name. | |
3946 * Change one character, just before the extension. | |
3947 */ | |
3948 if (!p_bk) | |
3949 { | |
3950 wp = backup + STRLEN(backup) - 1 | |
3951 - STRLEN(backup_ext); | |
3952 if (wp < backup) /* empty file name ??? */ | |
3953 wp = backup; | |
3954 *wp = 'z'; | |
3955 while (*wp > 'a' | |
3956 && mch_stat((char *)backup, &st_new) >= 0) | |
3957 --*wp; | |
3958 /* They all exist??? Must be something wrong. */ | |
3959 if (*wp == 'a') | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
3960 VIM_CLEAR(backup); |
7 | 3961 } |
3962 } | |
3963 break; | |
3964 } | |
3965 vim_free(rootname); | |
3966 | |
3967 /* | |
3968 * Try to create the backup file | |
3969 */ | |
3970 if (backup != NULL) | |
3971 { | |
3972 /* remove old backup, if present */ | |
3973 mch_remove(backup); | |
3974 /* 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
|
3975 * 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
|
3976 * 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
|
3977 #ifdef UNIX |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
3978 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
|
3979 #endif |
7 | 3980 bfd = mch_open((char *)backup, |
557 | 3981 O_WRONLY|O_CREAT|O_EXTRA|O_EXCL|O_NOFOLLOW, |
3982 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
|
3983 #ifdef UNIX |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
3984 (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
|
3985 #endif |
7 | 3986 if (bfd < 0) |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
3987 VIM_CLEAR(backup); |
7 | 3988 else |
3989 { | |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
3990 /* 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
|
3991 * 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
|
3992 * above. */ |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
3993 #ifndef UNIX |
7 | 3994 (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
|
3995 #else |
7 | 3996 /* |
3997 * Try to set the group of the backup same as the | |
3998 * original file. If this fails, set the protection | |
3999 * bits for the group same as the protection bits for | |
4000 * others. | |
4001 */ | |
557 | 4002 if (st_new.st_gid != st_old.st_gid |
7 | 4003 # ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */ |
557 | 4004 && fchown(bfd, (uid_t)-1, st_old.st_gid) != 0 |
7 | 4005 # endif |
4006 ) | |
4007 mch_setperm(backup, | |
4008 (perm & 0707) | ((perm & 07) << 3)); | |
5788 | 4009 # if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
1583 | 4010 mch_copy_sec(fname, backup); |
4011 # endif | |
7 | 4012 #endif |
4013 | |
4014 /* | |
4015 * copy the file. | |
4016 */ | |
4017 write_info.bw_fd = bfd; | |
4018 write_info.bw_buf = copybuf; | |
4019 #ifdef HAS_BW_FLAGS | |
4020 write_info.bw_flags = FIO_NOCONVERT; | |
4021 #endif | |
2664 | 4022 while ((write_info.bw_len = read_eintr(fd, copybuf, |
7 | 4023 BUFSIZE)) > 0) |
4024 { | |
4025 if (buf_write_bytes(&write_info) == FAIL) | |
4026 { | |
4027 errmsg = (char_u *)_("E506: Can't write to backup file (add ! to override)"); | |
4028 break; | |
4029 } | |
4030 ui_breakcheck(); | |
4031 if (got_int) | |
4032 { | |
4033 errmsg = (char_u *)_(e_interr); | |
4034 break; | |
4035 } | |
4036 } | |
4037 | |
4038 if (close(bfd) < 0 && errmsg == NULL) | |
4039 errmsg = (char_u *)_("E507: Close error for backup file (add ! to override)"); | |
4040 if (write_info.bw_len < 0) | |
4041 errmsg = (char_u *)_("E508: Can't read file for backup (add ! to override)"); | |
4042 #ifdef UNIX | |
4043 set_file_time(backup, st_old.st_atime, st_old.st_mtime); | |
4044 #endif | |
4045 #ifdef HAVE_ACL | |
4046 mch_set_acl(backup, acl); | |
4047 #endif | |
5788 | 4048 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
1583 | 4049 mch_copy_sec(fname, backup); |
4050 #endif | |
7 | 4051 break; |
4052 } | |
4053 } | |
4054 } | |
4055 nobackup: | |
4056 close(fd); /* ignore errors for closing read file */ | |
4057 vim_free(copybuf); | |
4058 | |
4059 if (backup == NULL && errmsg == NULL) | |
4060 errmsg = (char_u *)_("E509: Cannot create backup file (add ! to override)"); | |
4061 /* ignore errors when forceit is TRUE */ | |
4062 if ((some_error || errmsg != NULL) && !forceit) | |
4063 { | |
4064 retval = FAIL; | |
4065 goto fail; | |
4066 } | |
4067 errmsg = NULL; | |
4068 } | |
4069 else | |
4070 { | |
4071 char_u *dirp; | |
4072 char_u *p; | |
4073 char_u *rootname; | |
4074 | |
4075 /* | |
4076 * Make a backup by renaming the original file. | |
4077 */ | |
4078 /* | |
4079 * If 'cpoptions' includes the "W" flag, we don't want to | |
4080 * overwrite a read-only file. But rename may be possible | |
4081 * anyway, thus we need an extra check here. | |
4082 */ | |
4083 if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL) | |
4084 { | |
4085 errnum = (char_u *)"E504: "; | |
4086 errmsg = (char_u *)_(err_readonly); | |
4087 goto fail; | |
4088 } | |
4089 | |
4090 /* | |
4091 * | |
4092 * Form the backup file name - change path/fo.o.h to | |
4093 * path/fo.o.h.bak Try all directories in 'backupdir', first one | |
4094 * that works is used. | |
4095 */ | |
4096 dirp = p_bdir; | |
4097 while (*dirp) | |
4098 { | |
4099 /* | |
4100 * Isolate one directory name and make the backup file name. | |
4101 */ | |
4102 (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
|
4103 |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4104 #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
|
4105 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
|
4106 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
|
4107 // 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
|
4108 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
|
4109 { |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4110 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
|
4111 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
|
4112 } |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4113 #endif |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4114 if (backup == NULL) |
7 | 4115 { |
14475
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4116 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
|
4117 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
|
4118 backup = NULL; |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4119 else |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4120 { |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4121 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
|
4122 (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
|
4123 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
|
4124 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
|
4125 } |
7 | 4126 } |
4127 | |
4128 if (backup != NULL) | |
4129 { | |
4130 /* | |
4131 * If we are not going to keep the backup file, don't | |
4132 * delete an existing one, try to use another name. | |
4133 * Change one character, just before the extension. | |
4134 */ | |
4135 if (!p_bk && mch_getperm(backup) >= 0) | |
4136 { | |
4137 p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext); | |
4138 if (p < backup) /* empty file name ??? */ | |
4139 p = backup; | |
4140 *p = 'z'; | |
4141 while (*p > 'a' && mch_getperm(backup) >= 0) | |
4142 --*p; | |
4143 /* They all exist??? Must be something wrong! */ | |
4144 if (*p == 'a') | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
4145 VIM_CLEAR(backup); |
7 | 4146 } |
4147 } | |
4148 if (backup != NULL) | |
4149 { | |
4150 /* | |
531 | 4151 * Delete any existing backup and move the current version |
4152 * to the backup. For safety, we don't remove the backup | |
4153 * until the write has finished successfully. And if the | |
4154 * 'backup' option is set, leave it around. | |
7 | 4155 */ |
4156 /* | |
4157 * If the renaming of the original file to the backup file | |
4158 * works, quit here. | |
4159 */ | |
4160 if (vim_rename(fname, backup) == 0) | |
4161 break; | |
4162 | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
4163 VIM_CLEAR(backup); /* don't do the rename below */ |
7 | 4164 } |
4165 } | |
4166 if (backup == NULL && !forceit) | |
4167 { | |
4168 errmsg = (char_u *)_("E510: Can't make backup file (add ! to override)"); | |
4169 goto fail; | |
4170 } | |
4171 } | |
4172 } | |
4173 | |
7410
08e62c4fc17d
commit https://github.com/vim/vim/commit/53076830fea6df737455523f7e235bfe4f79864d
Christian Brabandt <cb@256bit.org>
parents:
7408
diff
changeset
|
4174 #if defined(UNIX) |
7 | 4175 /* When using ":w!" and the file was read-only: make it writable */ |
4176 if (forceit && perm >= 0 && !(perm & 0200) && st_old.st_uid == getuid() | |
4177 && vim_strchr(p_cpo, CPO_FWRITE) == NULL) | |
4178 { | |
4179 perm |= 0200; | |
4180 (void)mch_setperm(fname, perm); | |
4181 made_writable = TRUE; | |
4182 } | |
4183 #endif | |
4184 | |
819 | 4185 /* When using ":w!" and writing to the current file, 'readonly' makes no |
164 | 4186 * sense, reset it, unless 'Z' appears in 'cpoptions'. */ |
4187 if (forceit && overwriting && vim_strchr(p_cpo, CPO_KEEPRO) == NULL) | |
7 | 4188 { |
4189 buf->b_p_ro = FALSE; | |
4190 #ifdef FEAT_TITLE | |
4191 need_maketitle = TRUE; /* set window title later */ | |
4192 #endif | |
4193 status_redraw_all(); /* redraw status lines later */ | |
4194 } | |
4195 | |
4196 if (end > buf->b_ml.ml_line_count) | |
4197 end = buf->b_ml.ml_line_count; | |
4198 if (buf->b_ml.ml_flags & ML_EMPTY) | |
4199 start = end + 1; | |
4200 | |
4201 /* | |
4202 * If the original file is being overwritten, there is a small chance that | |
4203 * we crash in the middle of writing. Therefore the file is preserved now. | |
4204 * This makes all block numbers positive so that recovery does not need | |
4205 * the original file. | |
4206 * Don't do this if there is a backup file and we are exiting. | |
4207 */ | |
39 | 4208 if (reset_changed && !newfile && overwriting |
7 | 4209 && !(exiting && backup != NULL)) |
4210 { | |
4211 ml_preserve(buf, FALSE); | |
4212 if (got_int) | |
4213 { | |
4214 errmsg = (char_u *)_(e_interr); | |
4215 goto restore_backup; | |
4216 } | |
4217 } | |
4218 | |
4219 #ifdef VMS | |
4220 vms_remove_version(fname); /* remove version */ | |
4221 #endif | |
1651 | 4222 /* Default: write the file directly. May write to a temp file for |
7 | 4223 * multi-byte conversion. */ |
4224 wfname = fname; | |
4225 | |
4226 #ifdef FEAT_MBYTE | |
4227 /* Check for forced 'fileencoding' from "++opt=val" argument. */ | |
4228 if (eap != NULL && eap->force_enc != 0) | |
4229 { | |
4230 fenc = eap->cmd + eap->force_enc; | |
4231 fenc = enc_canonize(fenc); | |
4232 fenc_tofree = fenc; | |
4233 } | |
4234 else | |
4235 fenc = buf->b_p_fenc; | |
4236 | |
4237 /* | |
1948 | 4238 * Check if the file needs to be converted. |
7 | 4239 */ |
1948 | 4240 converted = need_conversion(fenc); |
7 | 4241 |
4242 /* | |
4243 * Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done. Or | |
4244 * Latin1 to Unicode conversion. This is handled in buf_write_bytes(). | |
4245 * Prepare the flags for it and allocate bw_conv_buf when needed. | |
4246 */ | |
4247 if (converted && (enc_utf8 || STRCMP(p_enc, "latin1") == 0)) | |
4248 { | |
4249 wb_flags = get_fio_flags(fenc); | |
4250 if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8)) | |
4251 { | |
4252 /* Need to allocate a buffer to translate into. */ | |
4253 if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8)) | |
4254 write_info.bw_conv_buflen = bufsize * 2; | |
4255 else /* FIO_UCS4 */ | |
4256 write_info.bw_conv_buflen = bufsize * 4; | |
4257 write_info.bw_conv_buf | |
4258 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4259 if (write_info.bw_conv_buf == NULL) | |
4260 end = 0; | |
4261 } | |
4262 } | |
4263 | |
4264 # ifdef WIN3264 | |
4265 if (converted && wb_flags == 0 && (wb_flags = get_win_fio_flags(fenc)) != 0) | |
4266 { | |
4267 /* Convert UTF-8 -> UCS-2 and UCS-2 -> DBCS. Worst-case * 4: */ | |
4268 write_info.bw_conv_buflen = bufsize * 4; | |
4269 write_info.bw_conv_buf | |
4270 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4271 if (write_info.bw_conv_buf == NULL) | |
4272 end = 0; | |
4273 } | |
4274 # endif | |
4275 | |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
4276 # ifdef MACOS_CONVERT |
7 | 4277 if (converted && wb_flags == 0 && (wb_flags = get_mac_fio_flags(fenc)) != 0) |
4278 { | |
4279 write_info.bw_conv_buflen = bufsize * 3; | |
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 # endif | |
4286 | |
4287 # if defined(FEAT_EVAL) || defined(USE_ICONV) | |
4288 if (converted && wb_flags == 0) | |
4289 { | |
4290 # ifdef USE_ICONV | |
4291 /* | |
4292 * Use iconv() conversion when conversion is needed and it's not done | |
4293 * internally. | |
4294 */ | |
4295 write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc, | |
4296 enc_utf8 ? (char_u *)"utf-8" : p_enc); | |
4297 if (write_info.bw_iconv_fd != (iconv_t)-1) | |
4298 { | |
4299 /* We're going to use iconv(), allocate a buffer to convert in. */ | |
4300 write_info.bw_conv_buflen = bufsize * ICONV_MULT; | |
4301 write_info.bw_conv_buf | |
4302 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4303 if (write_info.bw_conv_buf == NULL) | |
4304 end = 0; | |
4305 write_info.bw_first = TRUE; | |
4306 } | |
4307 # ifdef FEAT_EVAL | |
4308 else | |
4309 # endif | |
4310 # endif | |
4311 | |
4312 # ifdef FEAT_EVAL | |
4313 /* | |
4314 * When the file needs to be converted with 'charconvert' after | |
4315 * writing, write to a temp file instead and let the conversion | |
4316 * overwrite the original file. | |
4317 */ | |
4318 if (*p_ccv != NUL) | |
4319 { | |
6721 | 4320 wfname = vim_tempname('w', FALSE); |
7 | 4321 if (wfname == NULL) /* Can't write without a tempfile! */ |
4322 { | |
4323 errmsg = (char_u *)_("E214: Can't find temp file for writing"); | |
4324 goto restore_backup; | |
4325 } | |
4326 } | |
4327 # endif | |
4328 } | |
4329 # endif | |
4330 if (converted && wb_flags == 0 | |
4331 # ifdef USE_ICONV | |
4332 && write_info.bw_iconv_fd == (iconv_t)-1 | |
4333 # endif | |
4334 # ifdef FEAT_EVAL | |
4335 && wfname == fname | |
4336 # endif | |
4337 ) | |
4338 { | |
4339 if (!forceit) | |
4340 { | |
4341 errmsg = (char_u *)_("E213: Cannot convert (add ! to write without conversion)"); | |
4342 goto restore_backup; | |
4343 } | |
4344 notconverted = TRUE; | |
4345 } | |
4346 #endif | |
4347 | |
4348 /* | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4349 * 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
|
4350 * 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
|
4351 * When not doing conversion this writes for real right away. |
7 | 4352 */ |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4353 for (checking_conversion = TRUE; ; checking_conversion = FALSE) |
7 | 4354 { |
4355 /* | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4356 * 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
|
4357 * - 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
|
4358 * - 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
|
4359 * failure. |
7 | 4360 */ |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4361 #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
|
4362 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
|
4363 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4364 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
|
4365 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4366 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
|
4367 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4368 /* 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
|
4369 fd = -1; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4370 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
|
4371 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4372 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4373 { |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4374 #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
|
4375 # 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
|
4376 #else |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4377 # 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
|
4378 #endif |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4379 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4380 * 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
|
4381 * 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
|
4382 * 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
|
4383 * 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
|
4384 * 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
|
4385 * 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
|
4386 * 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
|
4387 * 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 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
|
4390 ? (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
|
4391 : (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
|
4392 , 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
|
4393 { |
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 * 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
|
4396 * 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
|
4397 * 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
|
4398 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4399 if (errmsg == NULL) |
7 | 4400 { |
4401 #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
|
4402 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
|
4403 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4404 /* 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
|
4405 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4406 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
|
4407 || (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
|
4408 && (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
|
4409 || 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
|
4410 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
|
4411 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4412 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4413 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4414 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
|
4415 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
|
4416 && perm >= 0) |
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 #ifdef UNIX |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4419 /* 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
|
4420 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
|
4421 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
|
4422 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
|
4423 perm |= 0200; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4424 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
|
4425 || 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
|
4426 perm &= 0777; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4427 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4428 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
|
4429 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
|
4430 continue; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4431 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4432 } |
7 | 4433 } |
4434 | |
4435 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
|
4436 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4437 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
|
4438 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4439 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4440 * 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
|
4441 * 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
|
4442 * 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
|
4443 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4444 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
|
4445 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4446 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
|
4447 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4448 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4449 * 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
|
4450 * 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
|
4451 * 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
|
4452 * 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
|
4453 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4454 /* 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
|
4455 * place */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4456 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
|
4457 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
|
4458 /* 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
|
4459 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4460 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
|
4461 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
|
4462 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4463 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4464 { |
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 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
|
4466 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
|
4467 } |
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 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4470 /* 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
|
4471 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4472 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
|
4473 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4474 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4475 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4476 #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
|
4477 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
|
4478 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
|
4479 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4480 goto fail; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4481 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4482 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
|
4483 |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4484 #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
|
4485 { |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4486 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
|
4487 |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4488 /* 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
|
4489 * any changes. */ |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4490 if (overwriting |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4491 && (!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
|
4492 && fname == wfname |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4493 && perm >= 0 |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4494 && 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
|
4495 && 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
|
4496 { |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4497 close(fd); |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4498 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
|
4499 goto fail; |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4500 } |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4501 } |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4502 #endif |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4503 #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
|
4504 if (!append) |
14730
193471015e1a
patch 8.1.0377: xdiff doesn't use the Vim memory allocation functions
Christian Brabandt <cb@256bit.org>
parents:
14700
diff
changeset
|
4505 vim_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
|
4506 #endif |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4507 |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
4508 #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
|
4509 if (backup != NULL && overwriting && !append) |
7 | 4510 { |
4511 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
|
4512 (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
|
4513 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4514 (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
|
4515 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4516 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4517 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
|
4518 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4519 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
|
4520 (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
|
4521 /* 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
|
4522 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4523 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4524 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4525 #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
|
4526 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
|
4527 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4528 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
|
4529 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
|
4530 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4531 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
|
4532 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
|
4533 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
|
4534 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
|
4535 end = 0; |
7 | 4536 else |
4537 { | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4538 /* 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
|
4539 * 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
|
4540 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
|
4541 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
|
4542 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
|
4543 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
|
4544 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4545 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
|
4546 vim_free(header); |
7 | 4547 } |
4548 } | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4549 #endif |
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 errmsg = NULL; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4552 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4553 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
|
4554 nchars = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4555 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4556 /* 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
|
4557 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
|
4558 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
|
4559 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4560 write_bin = buf->b_p_bin; |
7 | 4561 |
4562 #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
|
4563 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4564 * 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
|
4565 * 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
|
4566 * 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
|
4567 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4568 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
|
4569 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4570 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
|
4571 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
|
4572 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4573 /* 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
|
4574 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
|
4575 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
|
4576 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4577 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4578 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
|
4579 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4580 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4581 write_info.bw_start_lnum = start; |
7 | 4582 #endif |
4583 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4584 #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
|
4585 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
|
4586 && overwriting |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4587 && !append |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4588 && !filtering |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4589 && reset_changed |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4590 && !checking_conversion); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4591 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
|
4592 /* 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
|
4593 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
|
4594 #endif |
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 write_info.bw_len = bufsize; |
7 | 4597 #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
|
4598 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
|
4599 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4600 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
|
4601 s = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4602 len = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4603 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
|
4604 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4605 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4606 * 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
|
4607 * 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
|
4608 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4609 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
|
4610 #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
|
4611 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
|
4612 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
|
4613 (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
|
4614 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4615 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
|
4616 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4617 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
|
4618 *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
|
4619 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
|
4620 *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
|
4621 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4622 *s = c; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4623 ++s; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4624 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
|
4625 continue; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4626 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
|
4627 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4628 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
|
4629 break; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4630 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4631 nchars += bufsize; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4632 s = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4633 len = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4634 #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
|
4635 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
|
4636 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4637 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4638 /* 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
|
4639 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
|
4640 || (lnum == end |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4641 && (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
|
4642 && (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
|
4643 || (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
|
4644 && !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
|
4645 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4646 ++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
|
4647 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
|
4648 break; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4649 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4650 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
|
4651 *s++ = NL; |
7 | 4652 else |
11605
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 *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
|
4655 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
|
4656 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4657 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
|
4658 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4659 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
|
4660 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4661 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
|
4662 break; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4663 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4664 nchars += bufsize; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4665 s = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4666 len = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4667 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4668 *s++ = NL; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4669 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4670 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4671 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
|
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 (buf_write_bytes(&write_info) == FAIL) |
7 | 4674 { |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4675 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
|
4676 break; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4677 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4678 nchars += bufsize; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4679 s = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4680 len = 0; |
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 ui_breakcheck(); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4683 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
|
4684 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4685 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
|
4686 break; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4687 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4688 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4689 #ifdef VMS |
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 * 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
|
4692 * 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
|
4693 * 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
|
4694 * 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
|
4695 * 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
|
4696 * 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
|
4697 * 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
|
4698 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4699 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
|
4700 || ((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
|
4701 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4702 int b2write; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4703 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4704 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
|
4705 ? 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
|
4706 : 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
|
4707 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4708 b2write = len; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4709 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
|
4710 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4711 write_info.bw_len = MIN(b2write, buf->b_fab_mrs); |
7 | 4712 if (buf_write_bytes(&write_info) == FAIL) |
4713 { | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4714 end = 0; |
7 | 4715 break; |
4716 } | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4717 b2write -= MIN(b2write, buf->b_fab_mrs); |
7 | 4718 } |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4719 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
|
4720 nchars += len; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4721 s = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4722 len = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4723 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4724 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4725 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4726 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
|
4727 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4728 write_info.bw_len = len; |
7 | 4729 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
|
4730 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
|
4731 nchars += len; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4732 } |
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 /* 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
|
4735 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
|
4736 break; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4737 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4738 /* 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
|
4739 * 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
|
4740 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4741 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4742 /* 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
|
4743 * encountered. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4744 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
|
4745 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4746 #if defined(UNIX) && defined(HAVE_FSYNC) |
7 | 4747 /* |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4748 * 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
|
4749 * 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
|
4750 * 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
|
4751 * 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
|
4752 * 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
|
4753 * 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
|
4754 * 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
|
4755 * If the 'fsync' option is FALSE, don't fsync(). Useful for laptops. |
7 | 4756 */ |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4757 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
|
4758 { |
12857
ffdf2e4b5d9a
patch 8.0.1305: writefile() never calls fsync()
Christian Brabandt <cb@256bit.org>
parents:
12847
diff
changeset
|
4759 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
|
4760 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4761 } |
7 | 4762 #endif |
4763 | |
5788 | 4764 #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
|
4765 /* 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
|
4766 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
|
4767 mch_copy_sec(backup, wfname); |
1583 | 4768 #endif |
4769 | |
557 | 4770 #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
|
4771 /* 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
|
4772 * 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
|
4773 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
|
4774 { |
557 | 4775 # 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
|
4776 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
|
4777 |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4778 /* 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
|
4779 * 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
|
4780 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
|
4781 || 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
|
4782 || 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
|
4783 { |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4784 /* changing owner might not be possible */ |
14730
193471015e1a
patch 8.1.0377: xdiff doesn't use the Vim memory allocation functions
Christian Brabandt <cb@256bit.org>
parents:
14700
diff
changeset
|
4785 vim_ignored = fchown(fd, st_old.st_uid, -1); |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4786 /* 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
|
4787 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
|
4788 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
|
4789 } |
557 | 4790 # endif |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4791 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
|
4792 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4793 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
|
4794 /* 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
|
4795 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
|
4796 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4797 |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4798 #ifdef UNIX |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4799 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
|
4800 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
|
4801 #endif |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4802 #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
|
4803 /* 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
|
4804 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
|
4805 (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
|
4806 #endif |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4807 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
|
4808 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4809 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
|
4810 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4811 } |
7 | 4812 |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4813 #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
|
4814 /* 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
|
4815 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
|
4816 (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
|
4817 #endif |
7 | 4818 #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
|
4819 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4820 * 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
|
4821 * 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
|
4822 * 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
|
4823 * 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
|
4824 * 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
|
4825 * 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
|
4826 */ |
9509
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
4827 # 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
|
4828 if (!backup_copy) |
9509
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
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 mch_set_acl(wfname, acl); |
7 | 4831 #endif |
2267 | 4832 #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
|
4833 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
|
4834 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4835 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
|
4836 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
|
4837 } |
6122 | 4838 #endif |
7 | 4839 |
4840 #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
|
4841 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
|
4842 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4843 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4844 * 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
|
4845 * 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
|
4846 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4847 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
|
4848 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4849 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
|
4850 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
|
4851 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4852 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
|
4853 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4854 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4855 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4856 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
|
4857 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
|
4858 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4859 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4860 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4861 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4862 if (end == 0) |
7 | 4863 { |
4864 /* | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4865 * Error encountered. |
7 | 4866 */ |
4867 if (errmsg == NULL) | |
4868 { | |
4869 #ifdef FEAT_MBYTE | |
4870 if (write_info.bw_conv_error) | |
1947 | 4871 { |
4872 if (write_info.bw_conv_error_lnum == 0) | |
4873 errmsg = (char_u *)_("E513: write error, conversion failed (make 'fenc' empty to override)"); | |
4874 else | |
4875 { | |
4876 errmsg_allocated = TRUE; | |
4877 errmsg = alloc(300); | |
4878 vim_snprintf((char *)errmsg, 300, _("E513: write error, conversion failed in line %ld (make 'fenc' empty to override)"), | |
4879 (long)write_info.bw_conv_error_lnum); | |
4880 } | |
4881 } | |
7 | 4882 else |
4883 #endif | |
4884 if (got_int) | |
4885 errmsg = (char_u *)_(e_interr); | |
4886 else | |
4887 errmsg = (char_u *)_("E514: write error (file system full?)"); | |
4888 } | |
4889 | |
4890 /* | |
4891 * If we have a backup file, try to put it in place of the new file, | |
1698 | 4892 * because the new file is probably corrupt. This avoids losing the |
7 | 4893 * original file when trying to make a backup when writing the file a |
4894 * second time. | |
4895 * When "backup_copy" is set we need to copy the backup over the new | |
4896 * file. Otherwise rename the backup file. | |
4897 * If this is OK, don't give the extra warning message. | |
4898 */ | |
4899 if (backup != NULL) | |
4900 { | |
4901 if (backup_copy) | |
4902 { | |
4903 /* This may take a while, if we were interrupted let the user | |
4904 * know we got the message. */ | |
4905 if (got_int) | |
4906 { | |
4907 MSG(_(e_interr)); | |
4908 out_flush(); | |
4909 } | |
4910 if ((fd = mch_open((char *)backup, O_RDONLY | O_EXTRA, 0)) >= 0) | |
4911 { | |
4912 if ((write_info.bw_fd = mch_open((char *)fname, | |
194 | 4913 O_WRONLY | O_CREAT | O_TRUNC | O_EXTRA, |
4914 perm & 0777)) >= 0) | |
7 | 4915 { |
4916 /* copy the file. */ | |
4917 write_info.bw_buf = smallbuf; | |
4918 #ifdef HAS_BW_FLAGS | |
4919 write_info.bw_flags = FIO_NOCONVERT; | |
4920 #endif | |
2664 | 4921 while ((write_info.bw_len = read_eintr(fd, smallbuf, |
7 | 4922 SMBUFSIZE)) > 0) |
4923 if (buf_write_bytes(&write_info) == FAIL) | |
4924 break; | |
4925 | |
4926 if (close(write_info.bw_fd) >= 0 | |
4927 && write_info.bw_len == 0) | |
4928 end = 1; /* success */ | |
4929 } | |
4930 close(fd); /* ignore errors for closing read file */ | |
4931 } | |
4932 } | |
4933 else | |
4934 { | |
4935 if (vim_rename(backup, fname) == 0) | |
4936 end = 1; | |
4937 } | |
4938 } | |
4939 goto fail; | |
4940 } | |
4941 | |
4942 lnum -= start; /* compute number of written lines */ | |
4943 --no_wait_return; /* may wait for return now */ | |
4944 | |
4945 #if !(defined(UNIX) || defined(VMS)) | |
4946 fname = sfname; /* use shortname now, for the messages */ | |
4947 #endif | |
4948 if (!filtering) | |
4949 { | |
4950 msg_add_fname(buf, fname); /* put fname in IObuff with quotes */ | |
4951 c = FALSE; | |
4952 #ifdef FEAT_MBYTE | |
4953 if (write_info.bw_conv_error) | |
4954 { | |
4955 STRCAT(IObuff, _(" CONVERSION ERROR")); | |
4956 c = TRUE; | |
1947 | 4957 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
|
4958 vim_snprintf_add((char *)IObuff, IOSIZE, _(" in line %ld;"), |
1947 | 4959 (long)write_info.bw_conv_error_lnum); |
7 | 4960 } |
4961 else if (notconverted) | |
4962 { | |
4963 STRCAT(IObuff, _("[NOT converted]")); | |
4964 c = TRUE; | |
4965 } | |
4966 else if (converted) | |
4967 { | |
4968 STRCAT(IObuff, _("[converted]")); | |
4969 c = TRUE; | |
4970 } | |
4971 #endif | |
4972 if (device) | |
4973 { | |
4974 STRCAT(IObuff, _("[Device]")); | |
4975 c = TRUE; | |
4976 } | |
4977 else if (newfile) | |
4978 { | |
4979 STRCAT(IObuff, shortmess(SHM_NEW) ? _("[New]") : _("[New File]")); | |
4980 c = TRUE; | |
4981 } | |
4982 if (no_eol) | |
4983 { | |
4984 msg_add_eol(); | |
4985 c = TRUE; | |
4986 } | |
4987 /* may add [unix/dos/mac] */ | |
4988 if (msg_add_fileformat(fileformat)) | |
4989 c = TRUE; | |
4990 #ifdef FEAT_CRYPT | |
4991 if (wb_flags & FIO_ENCRYPTED) | |
4992 { | |
6122 | 4993 crypt_append_msg(buf); |
7 | 4994 c = TRUE; |
4995 } | |
4996 #endif | |
4997 msg_add_lines(c, (long)lnum, nchars); /* add line/char count */ | |
4998 if (!shortmess(SHM_WRITE)) | |
4999 { | |
5000 if (append) | |
5001 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [a]") : _(" appended")); | |
5002 else | |
5003 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [w]") : _(" written")); | |
5004 } | |
5005 | |
679 | 5006 set_keep_msg(msg_trunc_attr(IObuff, FALSE, 0), 0); |
7 | 5007 } |
5008 | |
39 | 5009 /* When written everything correctly: reset 'modified'. Unless not |
5010 * writing to the original file and '+' is not in 'cpoptions'. */ | |
589 | 5011 if (reset_changed && whole && !append |
7 | 5012 #ifdef FEAT_MBYTE |
5013 && !write_info.bw_conv_error | |
5014 #endif | |
39 | 5015 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL) |
5016 ) | |
7 | 5017 { |
5018 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
|
5019 /* b:changedtick is always incremented in unchanged() but that |
6320 | 5020 * 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
|
5021 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
|
5022 buf->b_last_changedtick = CHANGEDTICK(buf); |
7 | 5023 u_unchanged(buf); |
2281
e41433ea71df
Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents:
2280
diff
changeset
|
5024 u_update_save_nr(buf); |
7 | 5025 } |
5026 | |
5027 /* | |
5028 * If written to the current file, update the timestamp of the swap file | |
5029 * and reset the BF_WRITE_MASK flags. Also sets buf->b_mtime. | |
5030 */ | |
5031 if (overwriting) | |
5032 { | |
5033 ml_timestamp(buf); | |
589 | 5034 if (append) |
5035 buf->b_flags &= ~BF_NEW; | |
5036 else | |
5037 buf->b_flags &= ~BF_WRITE_MASK; | |
7 | 5038 } |
5039 | |
5040 /* | |
5041 * If we kept a backup until now, and we are in patch mode, then we make | |
5042 * the backup file our 'original' file. | |
5043 */ | |
5044 if (*p_pm && dobackup) | |
5045 { | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
5046 char *org = (char *)buf_modname((buf->b_p_sn || buf->b_shortname), |
7 | 5047 fname, p_pm, FALSE); |
5048 | |
5049 if (backup != NULL) | |
5050 { | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
5051 stat_T st; |
7 | 5052 |
5053 /* | |
5054 * If the original file does not exist yet | |
5055 * the current backup file becomes the original file | |
5056 */ | |
5057 if (org == NULL) | |
5058 EMSG(_("E205: Patchmode: can't save original file")); | |
5059 else if (mch_stat(org, &st) < 0) | |
5060 { | |
5061 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
|
5062 VIM_CLEAR(backup); /* don't delete the file */ |
7 | 5063 #ifdef UNIX |
5064 set_file_time((char_u *)org, st_old.st_atime, st_old.st_mtime); | |
5065 #endif | |
5066 } | |
5067 } | |
5068 /* | |
5069 * If there is no backup file, remember that a (new) file was | |
5070 * created. | |
5071 */ | |
5072 else | |
5073 { | |
5074 int empty_fd; | |
5075 | |
5076 if (org == NULL | |
557 | 5077 || (empty_fd = mch_open(org, |
5078 O_CREAT | O_EXTRA | O_EXCL | O_NOFOLLOW, | |
216 | 5079 perm < 0 ? 0666 : (perm & 0777))) < 0) |
7 | 5080 EMSG(_("E206: patchmode: can't touch empty original file")); |
5081 else | |
5082 close(empty_fd); | |
5083 } | |
5084 if (org != NULL) | |
5085 { | |
5086 mch_setperm((char_u *)org, mch_getperm(fname) & 0777); | |
5087 vim_free(org); | |
5088 } | |
5089 } | |
5090 | |
5091 /* | |
5092 * Remove the backup unless 'backup' option is set | |
5093 */ | |
5094 if (!p_bk && backup != NULL && mch_remove(backup) != 0) | |
5095 EMSG(_("E207: Can't delete backup file")); | |
5096 | |
5097 #ifdef FEAT_SUN_WORKSHOP | |
5098 if (usingSunWorkShop) | |
5099 workshop_file_saved((char *) ffname); | |
5100 #endif | |
5101 | |
5102 goto nofail; | |
5103 | |
5104 /* | |
5105 * Finish up. We get here either after failure or success. | |
5106 */ | |
5107 fail: | |
5108 --no_wait_return; /* may wait for return now */ | |
5109 nofail: | |
5110 | |
5111 /* Done saving, we accept changed buffer warnings again */ | |
5112 buf->b_saving = FALSE; | |
5113 | |
5114 vim_free(backup); | |
5115 if (buffer != smallbuf) | |
5116 vim_free(buffer); | |
5117 #ifdef FEAT_MBYTE | |
5118 vim_free(fenc_tofree); | |
5119 vim_free(write_info.bw_conv_buf); | |
5120 # ifdef USE_ICONV | |
5121 if (write_info.bw_iconv_fd != (iconv_t)-1) | |
5122 { | |
5123 iconv_close(write_info.bw_iconv_fd); | |
5124 write_info.bw_iconv_fd = (iconv_t)-1; | |
5125 } | |
5126 # endif | |
5127 #endif | |
5128 #ifdef HAVE_ACL | |
5129 mch_free_acl(acl); | |
5130 #endif | |
5131 | |
5132 if (errmsg != NULL) | |
5133 { | |
835 | 5134 int numlen = errnum != NULL ? (int)STRLEN(errnum) : 0; |
7 | 5135 |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
5136 attr = HL_ATTR(HLF_E); /* set highlight for error messages */ |
7 | 5137 msg_add_fname(buf, |
5138 #ifndef UNIX | |
5139 sfname | |
5140 #else | |
5141 fname | |
5142 #endif | |
5143 ); /* put file name in IObuff with quotes */ | |
5144 if (STRLEN(IObuff) + STRLEN(errmsg) + numlen >= IOSIZE) | |
5145 IObuff[IOSIZE - STRLEN(errmsg) - numlen - 1] = NUL; | |
5146 /* If the error message has the form "is ...", put the error number in | |
5147 * front of the file name. */ | |
5148 if (errnum != NULL) | |
5149 { | |
1620 | 5150 STRMOVE(IObuff + numlen, IObuff); |
7 | 5151 mch_memmove(IObuff, errnum, (size_t)numlen); |
5152 } | |
5153 STRCAT(IObuff, errmsg); | |
5154 emsg(IObuff); | |
1947 | 5155 if (errmsg_allocated) |
5156 vim_free(errmsg); | |
7 | 5157 |
5158 retval = FAIL; | |
5159 if (end == 0) | |
5160 { | |
5161 MSG_PUTS_ATTR(_("\nWARNING: Original file may be lost or damaged\n"), | |
5162 attr | MSG_HIST); | |
5163 MSG_PUTS_ATTR(_("don't quit the editor until the file is successfully written!"), | |
5164 attr | MSG_HIST); | |
5165 | |
5166 /* Update the timestamp to avoid an "overwrite changed file" | |
5167 * prompt when writing again. */ | |
5168 if (mch_stat((char *)fname, &st_old) >= 0) | |
5169 { | |
5170 buf_store_time(buf, &st_old, fname); | |
5171 buf->b_mtime_read = buf->b_mtime; | |
5172 } | |
5173 } | |
5174 } | |
5175 msg_scroll = msg_save; | |
5176 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5177 #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
|
5178 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5179 * 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
|
5180 * file. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5181 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5182 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
|
5183 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5184 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
|
5185 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5186 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
|
5187 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
|
5188 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5189 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5190 |
7 | 5191 #ifdef FEAT_EVAL |
5192 if (!should_abort(retval)) | |
5193 #else | |
5194 if (!got_int) | |
5195 #endif | |
5196 { | |
5197 aco_save_T aco; | |
5198 | |
3482 | 5199 curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */ |
5200 | |
7 | 5201 /* |
5202 * Apply POST autocommands. | |
5203 * Careful: The autocommands may call buf_write() recursively! | |
5204 */ | |
5205 aucmd_prepbuf(&aco, buf); | |
5206 | |
5207 if (append) | |
5208 apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname, | |
5209 FALSE, curbuf, eap); | |
5210 else if (filtering) | |
5211 apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname, | |
5212 FALSE, curbuf, eap); | |
5213 else if (reset_changed && whole) | |
5214 apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname, | |
5215 FALSE, curbuf, eap); | |
5216 else | |
5217 apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname, | |
5218 FALSE, curbuf, eap); | |
5219 | |
5220 /* restore curwin/curbuf and a few other things */ | |
5221 aucmd_restbuf(&aco); | |
5222 | |
5223 #ifdef FEAT_EVAL | |
5224 if (aborting()) /* autocmds may abort script processing */ | |
5225 retval = FALSE; | |
5226 #endif | |
5227 } | |
5228 | |
5229 got_int |= prev_got_int; | |
5230 | |
5231 return retval; | |
5232 } | |
5233 | |
5234 /* | |
633 | 5235 * Set the name of the current buffer. Use when the buffer doesn't have a |
5236 * name and a ":r" or ":w" command with a file name is used. | |
5237 */ | |
5238 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5239 set_rw_fname(char_u *fname, char_u *sfname) |
633 | 5240 { |
1905 | 5241 buf_T *buf = curbuf; |
5242 | |
633 | 5243 /* It's like the unnamed buffer is deleted.... */ |
5244 if (curbuf->b_p_bl) | |
5245 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); | |
5246 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
|
5247 #ifdef FEAT_EVAL |
633 | 5248 if (aborting()) /* autocmds may abort script processing */ |
5249 return FAIL; | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
5250 #endif |
1905 | 5251 if (curbuf != buf) |
5252 { | |
5253 /* We are in another buffer now, don't do the renaming. */ | |
5254 EMSG(_(e_auchangedbuf)); | |
5255 return FAIL; | |
5256 } | |
633 | 5257 |
5258 if (setfname(curbuf, fname, sfname, FALSE) == OK) | |
5259 curbuf->b_flags |= BF_NOTEDITED; | |
5260 | |
5261 /* ....and a new named one is created */ | |
5262 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf); | |
5263 if (curbuf->b_p_bl) | |
5264 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
|
5265 #ifdef FEAT_EVAL |
633 | 5266 if (aborting()) /* autocmds may abort script processing */ |
5267 return FAIL; | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
5268 #endif |
633 | 5269 |
5270 /* Do filetype detection now if 'filetype' is empty. */ | |
5271 if (*curbuf->b_p_ft == NUL) | |
5272 { | |
819 | 5273 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
|
5274 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL); |
717 | 5275 do_modelines(0); |
633 | 5276 } |
5277 | |
5278 return OK; | |
5279 } | |
5280 | |
5281 /* | |
7 | 5282 * Put file name into IObuff with quotes. |
5283 */ | |
33 | 5284 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5285 msg_add_fname(buf_T *buf, char_u *fname) |
7 | 5286 { |
5287 if (fname == NULL) | |
5288 fname = (char_u *)"-stdin-"; | |
5289 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE); | |
5290 IObuff[0] = '"'; | |
5291 STRCAT(IObuff, "\" "); | |
5292 } | |
5293 | |
5294 /* | |
5295 * Append message for text mode to IObuff. | |
5296 * Return TRUE if something appended. | |
5297 */ | |
5298 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5299 msg_add_fileformat(int eol_type) |
7 | 5300 { |
5301 #ifndef USE_CRNL | |
5302 if (eol_type == EOL_DOS) | |
5303 { | |
5304 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]")); | |
5305 return TRUE; | |
5306 } | |
5307 #endif | |
5308 #ifndef USE_CR | |
5309 if (eol_type == EOL_MAC) | |
5310 { | |
5311 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]")); | |
5312 return TRUE; | |
5313 } | |
5314 #endif | |
5315 #if defined(USE_CRNL) || defined(USE_CR) | |
5316 if (eol_type == EOL_UNIX) | |
5317 { | |
5318 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]")); | |
5319 return TRUE; | |
5320 } | |
5321 #endif | |
5322 return FALSE; | |
5323 } | |
5324 | |
5325 /* | |
5326 * Append line and character count to IObuff. | |
5327 */ | |
33 | 5328 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5329 msg_add_lines( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5330 int insert_space, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5331 long lnum, |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
5332 off_T nchars) |
7 | 5333 { |
5334 char_u *p; | |
5335 | |
5336 p = IObuff + STRLEN(IObuff); | |
5337 | |
5338 if (insert_space) | |
5339 *p++ = ' '; | |
5340 if (shortmess(SHM_LINES)) | |
9391
4c40631238e4
commit https://github.com/vim/vim/commit/bde9810d6103ffe3a22a9330021cb21db1ed1792
Christian Brabandt <cb@256bit.org>
parents:
9389
diff
changeset
|
5341 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
|
5342 "%ldL, %lldC", lnum, (long long)nchars); |
7 | 5343 else |
5344 { | |
14585
c8f07e8b273e
patch 8.1.0306: plural messages are not translated properly
Christian Brabandt <cb@256bit.org>
parents:
14509
diff
changeset
|
5345 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum); |
7 | 5346 p += STRLEN(p); |
14585
c8f07e8b273e
patch 8.1.0306: plural messages are not translated properly
Christian Brabandt <cb@256bit.org>
parents:
14509
diff
changeset
|
5347 vim_snprintf((char *)p, IOSIZE - (p - IObuff), |
c8f07e8b273e
patch 8.1.0306: plural messages are not translated properly
Christian Brabandt <cb@256bit.org>
parents:
14509
diff
changeset
|
5348 NGETTEXT("%lld character", "%lld characters", nchars), |
c8f07e8b273e
patch 8.1.0306: plural messages are not translated properly
Christian Brabandt <cb@256bit.org>
parents:
14509
diff
changeset
|
5349 (long long)nchars); |
7 | 5350 } |
5351 } | |
5352 | |
5353 /* | |
5354 * Append message for missing line separator to IObuff. | |
5355 */ | |
5356 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5357 msg_add_eol(void) |
7 | 5358 { |
5359 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]")); | |
5360 } | |
5361 | |
5362 /* | |
5363 * Check modification time of file, before writing to it. | |
5364 * The size isn't checked, because using a tool like "gzip" takes care of | |
5365 * using the same timestamp but can't set the size. | |
5366 */ | |
5367 static int | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
5368 check_mtime(buf_T *buf, stat_T *st) |
7 | 5369 { |
5370 if (buf->b_mtime_read != 0 | |
5371 && time_differs((long)st->st_mtime, buf->b_mtime_read)) | |
5372 { | |
5373 msg_scroll = TRUE; /* don't overwrite messages here */ | |
5374 msg_silent = 0; /* must give this prompt */ | |
5375 /* don't use emsg() here, don't want to flush the buffers */ | |
5376 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
|
5377 HL_ATTR(HLF_E)); |
7 | 5378 if (ask_yesno((char_u *)_("Do you really want to write to it"), |
5379 TRUE) == 'n') | |
5380 return FAIL; | |
5381 msg_scroll = FALSE; /* always overwrite the file message now */ | |
5382 } | |
5383 return OK; | |
5384 } | |
5385 | |
5386 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5387 time_differs(long t1, long t2) |
7 | 5388 { |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
5389 #if defined(__linux__) || defined(MSWIN) |
7 | 5390 /* On a FAT filesystem, esp. under Linux, there are only 5 bits to store |
5391 * the seconds. Since the roundoff is done when flushing the inode, the | |
5392 * time may change unexpectedly by one second!!! */ | |
5393 return (t1 - t2 > 1 || t2 - t1 > 1); | |
5394 #else | |
5395 return (t1 != t2); | |
5396 #endif | |
5397 } | |
5398 | |
5399 /* | |
5400 * Call write() to write a number of bytes to the file. | |
2664 | 5401 * Handles encryption and 'encoding' conversion. |
7 | 5402 * |
5403 * Return FAIL for failure, OK otherwise. | |
5404 */ | |
5405 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5406 buf_write_bytes(struct bw_info *ip) |
7 | 5407 { |
5408 int wlen; | |
5409 char_u *buf = ip->bw_buf; /* data to write */ | |
5410 int len = ip->bw_len; /* length of data */ | |
5411 #ifdef HAS_BW_FLAGS | |
5412 int flags = ip->bw_flags; /* extra flags */ | |
5413 #endif | |
5414 | |
5415 #ifdef FEAT_MBYTE | |
5416 /* | |
5417 * Skip conversion when writing the crypt magic number or the BOM. | |
5418 */ | |
5419 if (!(flags & FIO_NOCONVERT)) | |
5420 { | |
5421 char_u *p; | |
5422 unsigned c; | |
5423 int n; | |
5424 | |
5425 if (flags & FIO_UTF8) | |
5426 { | |
5427 /* | |
5428 * Convert latin1 in the buffer to UTF-8 in the file. | |
5429 */ | |
5430 p = ip->bw_conv_buf; /* translate to buffer */ | |
5431 for (wlen = 0; wlen < len; ++wlen) | |
5432 p += utf_char2bytes(buf[wlen], p); | |
5433 buf = ip->bw_conv_buf; | |
5434 len = (int)(p - ip->bw_conv_buf); | |
5435 } | |
5436 else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1)) | |
5437 { | |
5438 /* | |
5439 * Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or | |
5440 * Latin1 chars in the file. | |
5441 */ | |
5442 if (flags & FIO_LATIN1) | |
5443 p = buf; /* translate in-place (can only get shorter) */ | |
5444 else | |
5445 p = ip->bw_conv_buf; /* translate to buffer */ | |
5446 for (wlen = 0; wlen < len; wlen += n) | |
5447 { | |
5448 if (wlen == 0 && ip->bw_restlen != 0) | |
5449 { | |
5450 int l; | |
5451 | |
5452 /* Use remainder of previous call. Append the start of | |
5453 * buf[] to get a full sequence. Might still be too | |
5454 * short! */ | |
5455 l = CONV_RESTLEN - ip->bw_restlen; | |
5456 if (l > len) | |
5457 l = len; | |
5458 mch_memmove(ip->bw_rest + ip->bw_restlen, buf, (size_t)l); | |
474 | 5459 n = utf_ptr2len_len(ip->bw_rest, ip->bw_restlen + l); |
7 | 5460 if (n > ip->bw_restlen + len) |
5461 { | |
5462 /* We have an incomplete byte sequence at the end to | |
5463 * be written. We can't convert it without the | |
5464 * remaining bytes. Keep them for the next call. */ | |
5465 if (ip->bw_restlen + len > CONV_RESTLEN) | |
5466 return FAIL; | |
5467 ip->bw_restlen += len; | |
5468 break; | |
5469 } | |
5470 if (n > 1) | |
5471 c = utf_ptr2char(ip->bw_rest); | |
5472 else | |
5473 c = ip->bw_rest[0]; | |
5474 if (n >= ip->bw_restlen) | |
5475 { | |
5476 n -= ip->bw_restlen; | |
5477 ip->bw_restlen = 0; | |
5478 } | |
5479 else | |
5480 { | |
5481 ip->bw_restlen -= n; | |
5482 mch_memmove(ip->bw_rest, ip->bw_rest + n, | |
5483 (size_t)ip->bw_restlen); | |
5484 n = 0; | |
5485 } | |
5486 } | |
5487 else | |
5488 { | |
474 | 5489 n = utf_ptr2len_len(buf + wlen, len - wlen); |
7 | 5490 if (n > len - wlen) |
5491 { | |
5492 /* We have an incomplete byte sequence at the end to | |
5493 * be written. We can't convert it without the | |
5494 * remaining bytes. Keep them for the next call. */ | |
5495 if (len - wlen > CONV_RESTLEN) | |
5496 return FAIL; | |
5497 ip->bw_restlen = len - wlen; | |
5498 mch_memmove(ip->bw_rest, buf + wlen, | |
5499 (size_t)ip->bw_restlen); | |
5500 break; | |
5501 } | |
5502 if (n > 1) | |
5503 c = utf_ptr2char(buf + wlen); | |
5504 else | |
5505 c = buf[wlen]; | |
5506 } | |
5507 | |
1947 | 5508 if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error) |
5509 { | |
5510 ip->bw_conv_error = TRUE; | |
5511 ip->bw_conv_error_lnum = ip->bw_start_lnum; | |
5512 } | |
5513 if (c == NL) | |
5514 ++ip->bw_start_lnum; | |
7 | 5515 } |
5516 if (flags & FIO_LATIN1) | |
5517 len = (int)(p - buf); | |
5518 else | |
5519 { | |
5520 buf = ip->bw_conv_buf; | |
5521 len = (int)(p - ip->bw_conv_buf); | |
5522 } | |
5523 } | |
5524 | |
5525 # ifdef WIN3264 | |
5526 else if (flags & FIO_CODEPAGE) | |
5527 { | |
5528 /* | |
5529 * Convert UTF-8 or codepage to UCS-2 and then to MS-Windows | |
5530 * codepage. | |
5531 */ | |
5532 char_u *from; | |
5533 size_t fromlen; | |
5534 char_u *to; | |
5535 int u8c; | |
5536 BOOL bad = FALSE; | |
5537 int needed; | |
5538 | |
5539 if (ip->bw_restlen > 0) | |
5540 { | |
5541 /* Need to concatenate the remainder of the previous call and | |
5542 * the bytes of the current call. Use the end of the | |
5543 * conversion buffer for this. */ | |
5544 fromlen = len + ip->bw_restlen; | |
5545 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; | |
5546 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); | |
5547 mch_memmove(from + ip->bw_restlen, buf, (size_t)len); | |
5548 } | |
5549 else | |
5550 { | |
5551 from = buf; | |
5552 fromlen = len; | |
5553 } | |
5554 | |
5555 to = ip->bw_conv_buf; | |
5556 if (enc_utf8) | |
5557 { | |
5558 /* Convert from UTF-8 to UCS-2, to the start of the buffer. | |
5559 * The buffer has been allocated to be big enough. */ | |
5560 while (fromlen > 0) | |
5561 { | |
835 | 5562 n = (int)utf_ptr2len_len(from, (int)fromlen); |
7 | 5563 if (n > (int)fromlen) /* incomplete byte sequence */ |
5564 break; | |
5565 u8c = utf_ptr2char(from); | |
5566 *to++ = (u8c & 0xff); | |
5567 *to++ = (u8c >> 8); | |
5568 fromlen -= n; | |
5569 from += n; | |
5570 } | |
5571 | |
5572 /* Copy remainder to ip->bw_rest[] to be used for the next | |
5573 * call. */ | |
5574 if (fromlen > CONV_RESTLEN) | |
5575 { | |
5576 /* weird overlong sequence */ | |
5577 ip->bw_conv_error = TRUE; | |
5578 return FAIL; | |
5579 } | |
5580 mch_memmove(ip->bw_rest, from, fromlen); | |
835 | 5581 ip->bw_restlen = (int)fromlen; |
7 | 5582 } |
5583 else | |
5584 { | |
5585 /* Convert from enc_codepage to UCS-2, to the start of the | |
5586 * buffer. The buffer has been allocated to be big enough. */ | |
5587 ip->bw_restlen = 0; | |
5588 needed = MultiByteToWideChar(enc_codepage, | |
1142 | 5589 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen, |
7 | 5590 NULL, 0); |
5591 if (needed == 0) | |
5592 { | |
5593 /* When conversion fails there may be a trailing byte. */ | |
5594 needed = MultiByteToWideChar(enc_codepage, | |
1142 | 5595 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen - 1, |
7 | 5596 NULL, 0); |
5597 if (needed == 0) | |
5598 { | |
5599 /* Conversion doesn't work. */ | |
5600 ip->bw_conv_error = TRUE; | |
5601 return FAIL; | |
5602 } | |
5603 /* Save the trailing byte for the next call. */ | |
5604 ip->bw_rest[0] = from[fromlen - 1]; | |
5605 ip->bw_restlen = 1; | |
5606 } | |
5607 needed = MultiByteToWideChar(enc_codepage, MB_ERR_INVALID_CHARS, | |
1142 | 5608 (LPCSTR)from, (int)(fromlen - ip->bw_restlen), |
7 | 5609 (LPWSTR)to, needed); |
5610 if (needed == 0) | |
5611 { | |
5612 /* Safety check: Conversion doesn't work. */ | |
5613 ip->bw_conv_error = TRUE; | |
5614 return FAIL; | |
5615 } | |
5616 to += needed * 2; | |
5617 } | |
5618 | |
5619 fromlen = to - ip->bw_conv_buf; | |
5620 buf = to; | |
5621 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
5622 if (FIO_GET_CP(flags) == CP_UTF8) | |
5623 { | |
5624 /* Convert from UCS-2 to UTF-8, using the remainder of the | |
5625 * conversion buffer. Fails when out of space. */ | |
5626 for (from = ip->bw_conv_buf; fromlen > 1; fromlen -= 2) | |
5627 { | |
5628 u8c = *from++; | |
5629 u8c += (*from++ << 8); | |
5630 to += utf_char2bytes(u8c, to); | |
5631 if (to + 6 >= ip->bw_conv_buf + ip->bw_conv_buflen) | |
5632 { | |
5633 ip->bw_conv_error = TRUE; | |
5634 return FAIL; | |
5635 } | |
5636 } | |
835 | 5637 len = (int)(to - buf); |
7 | 5638 } |
5639 else | |
5640 #endif | |
5641 { | |
5642 /* Convert from UCS-2 to the codepage, using the remainder of | |
5643 * the conversion buffer. If the conversion uses the default | |
5644 * character "0", the data doesn't fit in this encoding, so | |
5645 * fail. */ | |
5646 len = WideCharToMultiByte(FIO_GET_CP(flags), 0, | |
5647 (LPCWSTR)ip->bw_conv_buf, (int)fromlen / sizeof(WCHAR), | |
1142 | 5648 (LPSTR)to, (int)(ip->bw_conv_buflen - fromlen), 0, |
5649 &bad); | |
7 | 5650 if (bad) |
5651 { | |
5652 ip->bw_conv_error = TRUE; | |
5653 return FAIL; | |
5654 } | |
5655 } | |
5656 } | |
5657 # endif | |
5658 | |
765 | 5659 # ifdef MACOS_CONVERT |
7 | 5660 else if (flags & FIO_MACROMAN) |
5661 { | |
5662 /* | |
5663 * Convert UTF-8 or latin1 to Apple MacRoman. | |
5664 */ | |
5665 char_u *from; | |
5666 size_t fromlen; | |
5667 | |
5668 if (ip->bw_restlen > 0) | |
5669 { | |
5670 /* Need to concatenate the remainder of the previous call and | |
5671 * the bytes of the current call. Use the end of the | |
5672 * conversion buffer for this. */ | |
5673 fromlen = len + ip->bw_restlen; | |
5674 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; | |
5675 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); | |
5676 mch_memmove(from + ip->bw_restlen, buf, (size_t)len); | |
5677 } | |
5678 else | |
5679 { | |
5680 from = buf; | |
5681 fromlen = len; | |
5682 } | |
5683 | |
18 | 5684 if (enc2macroman(from, fromlen, |
5685 ip->bw_conv_buf, &len, ip->bw_conv_buflen, | |
5686 ip->bw_rest, &ip->bw_restlen) == FAIL) | |
7 | 5687 { |
5688 ip->bw_conv_error = TRUE; | |
5689 return FAIL; | |
5690 } | |
5691 buf = ip->bw_conv_buf; | |
5692 } | |
5693 # endif | |
5694 | |
5695 # ifdef USE_ICONV | |
5696 if (ip->bw_iconv_fd != (iconv_t)-1) | |
5697 { | |
5698 const char *from; | |
5699 size_t fromlen; | |
5700 char *to; | |
5701 size_t tolen; | |
5702 | |
5703 /* Convert with iconv(). */ | |
5704 if (ip->bw_restlen > 0) | |
5705 { | |
1836 | 5706 char *fp; |
5707 | |
7 | 5708 /* Need to concatenate the remainder of the previous call and |
5709 * the bytes of the current call. Use the end of the | |
5710 * conversion buffer for this. */ | |
5711 fromlen = len + ip->bw_restlen; | |
1836 | 5712 fp = (char *)ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; |
5713 mch_memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen); | |
5714 mch_memmove(fp + ip->bw_restlen, buf, (size_t)len); | |
5715 from = fp; | |
7 | 5716 tolen = ip->bw_conv_buflen - fromlen; |
5717 } | |
5718 else | |
5719 { | |
5720 from = (const char *)buf; | |
5721 fromlen = len; | |
5722 tolen = ip->bw_conv_buflen; | |
5723 } | |
5724 to = (char *)ip->bw_conv_buf; | |
5725 | |
5726 if (ip->bw_first) | |
5727 { | |
5728 size_t save_len = tolen; | |
5729 | |
5730 /* output the initial shift state sequence */ | |
5731 (void)iconv(ip->bw_iconv_fd, NULL, NULL, &to, &tolen); | |
5732 | |
5733 /* There is a bug in iconv() on Linux (which appears to be | |
5734 * wide-spread) which sets "to" to NULL and messes up "tolen". | |
5735 */ | |
5736 if (to == NULL) | |
5737 { | |
5738 to = (char *)ip->bw_conv_buf; | |
5739 tolen = save_len; | |
5740 } | |
5741 ip->bw_first = FALSE; | |
5742 } | |
5743 | |
5744 /* | |
5745 * If iconv() has an error or there is not enough room, fail. | |
5746 */ | |
5747 if ((iconv(ip->bw_iconv_fd, (void *)&from, &fromlen, &to, &tolen) | |
5748 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) | |
5749 || fromlen > CONV_RESTLEN) | |
5750 { | |
5751 ip->bw_conv_error = TRUE; | |
5752 return FAIL; | |
5753 } | |
5754 | |
5755 /* copy remainder to ip->bw_rest[] to be used for the next call. */ | |
5756 if (fromlen > 0) | |
5757 mch_memmove(ip->bw_rest, (void *)from, fromlen); | |
5758 ip->bw_restlen = (int)fromlen; | |
5759 | |
5760 buf = ip->bw_conv_buf; | |
5761 len = (int)((char_u *)to - ip->bw_conv_buf); | |
5762 } | |
5763 # endif | |
5764 } | |
5765 #endif /* FEAT_MBYTE */ | |
5766 | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
5767 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
|
5768 /* 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
|
5769 return OK; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
5770 |
7 | 5771 #ifdef FEAT_CRYPT |
6122 | 5772 if (flags & FIO_ENCRYPTED) |
5773 { | |
5774 /* Encrypt the data. Do it in-place if possible, otherwise use an | |
5775 * allocated buffer. */ | |
5776 if (crypt_works_inplace(ip->bw_buffer->b_cryptstate)) | |
5777 { | |
5778 crypt_encode_inplace(ip->bw_buffer->b_cryptstate, buf, len); | |
5779 } | |
5780 else | |
5781 { | |
5782 char_u *outbuf; | |
5783 | |
5784 len = crypt_encode_alloc(curbuf->b_cryptstate, buf, len, &outbuf); | |
5785 if (len == 0) | |
5786 return OK; /* Crypt layer is buffering, will flush later. */ | |
5787 wlen = write_eintr(ip->bw_fd, outbuf, len); | |
5788 vim_free(outbuf); | |
5789 return (wlen < len) ? FAIL : OK; | |
5790 } | |
5791 } | |
7 | 5792 #endif |
5793 | |
2664 | 5794 wlen = write_eintr(ip->bw_fd, buf, len); |
5795 return (wlen < len) ? FAIL : OK; | |
7 | 5796 } |
5797 | |
5798 #ifdef FEAT_MBYTE | |
5799 /* | |
5800 * Convert a Unicode character to bytes. | |
1947 | 5801 * Return TRUE for an error, FALSE when it's OK. |
7 | 5802 */ |
5803 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5804 ucs2bytes( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5805 unsigned c, /* in: character */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5806 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
|
5807 int flags) /* FIO_ flags */ |
7 | 5808 { |
5809 char_u *p = *pp; | |
5810 int error = FALSE; | |
5811 int cc; | |
5812 | |
5813 | |
5814 if (flags & FIO_UCS4) | |
5815 { | |
5816 if (flags & FIO_ENDIAN_L) | |
5817 { | |
5818 *p++ = c; | |
5819 *p++ = (c >> 8); | |
5820 *p++ = (c >> 16); | |
5821 *p++ = (c >> 24); | |
5822 } | |
5823 else | |
5824 { | |
5825 *p++ = (c >> 24); | |
5826 *p++ = (c >> 16); | |
5827 *p++ = (c >> 8); | |
5828 *p++ = c; | |
5829 } | |
5830 } | |
5831 else if (flags & (FIO_UCS2 | FIO_UTF16)) | |
5832 { | |
5833 if (c >= 0x10000) | |
5834 { | |
5835 if (flags & FIO_UTF16) | |
5836 { | |
5837 /* Make two words, ten bits of the character in each. First | |
5838 * word is 0xd800 - 0xdbff, second one 0xdc00 - 0xdfff */ | |
5839 c -= 0x10000; | |
5840 if (c >= 0x100000) | |
5841 error = TRUE; | |
5842 cc = ((c >> 10) & 0x3ff) + 0xd800; | |
5843 if (flags & FIO_ENDIAN_L) | |
5844 { | |
5845 *p++ = cc; | |
5846 *p++ = ((unsigned)cc >> 8); | |
5847 } | |
5848 else | |
5849 { | |
5850 *p++ = ((unsigned)cc >> 8); | |
5851 *p++ = cc; | |
5852 } | |
5853 c = (c & 0x3ff) + 0xdc00; | |
5854 } | |
5855 else | |
5856 error = TRUE; | |
5857 } | |
5858 if (flags & FIO_ENDIAN_L) | |
5859 { | |
5860 *p++ = c; | |
5861 *p++ = (c >> 8); | |
5862 } | |
5863 else | |
5864 { | |
5865 *p++ = (c >> 8); | |
5866 *p++ = c; | |
5867 } | |
5868 } | |
5869 else /* Latin1 */ | |
5870 { | |
5871 if (c >= 0x100) | |
5872 { | |
5873 error = TRUE; | |
5874 *p++ = 0xBF; | |
5875 } | |
5876 else | |
5877 *p++ = c; | |
5878 } | |
5879 | |
5880 *pp = p; | |
5881 return error; | |
5882 } | |
5883 | |
5884 /* | |
1948 | 5885 * Return TRUE if file encoding "fenc" requires conversion from or to |
5886 * 'encoding'. | |
7 | 5887 */ |
5888 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5889 need_conversion(char_u *fenc) |
7 | 5890 { |
1948 | 5891 int same_encoding; |
5892 int enc_flags; | |
5893 int fenc_flags; | |
5894 | |
5895 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0) | |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5896 { |
1948 | 5897 same_encoding = TRUE; |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5898 fenc_flags = 0; |
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5899 } |
1948 | 5900 else |
5901 { | |
5902 /* Ignore difference between "ansi" and "latin1", "ucs-4" and | |
5903 * "ucs-4be", etc. */ | |
5904 enc_flags = get_fio_flags(p_enc); | |
5905 fenc_flags = get_fio_flags(fenc); | |
5906 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags); | |
5907 } | |
5908 if (same_encoding) | |
5909 { | |
5910 /* Specified encoding matches with 'encoding'. This requires | |
5911 * conversion when 'encoding' is Unicode but not UTF-8. */ | |
5912 return enc_unicode != 0; | |
5913 } | |
5914 | |
5915 /* Encodings differ. However, conversion is not needed when 'enc' is any | |
5916 * Unicode encoding and the file is UTF-8. */ | |
5917 return !(enc_utf8 && fenc_flags == FIO_UTF8); | |
7 | 5918 } |
5919 | |
5920 /* | |
5921 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the | |
5922 * internal conversion. | |
5923 * if "ptr" is an empty string, use 'encoding'. | |
5924 */ | |
5925 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5926 get_fio_flags(char_u *ptr) |
7 | 5927 { |
5928 int prop; | |
5929 | |
5930 if (*ptr == NUL) | |
5931 ptr = p_enc; | |
5932 | |
5933 prop = enc_canon_props(ptr); | |
5934 if (prop & ENC_UNICODE) | |
5935 { | |
5936 if (prop & ENC_2BYTE) | |
5937 { | |
5938 if (prop & ENC_ENDIAN_L) | |
5939 return FIO_UCS2 | FIO_ENDIAN_L; | |
5940 return FIO_UCS2; | |
5941 } | |
5942 if (prop & ENC_4BYTE) | |
5943 { | |
5944 if (prop & ENC_ENDIAN_L) | |
5945 return FIO_UCS4 | FIO_ENDIAN_L; | |
5946 return FIO_UCS4; | |
5947 } | |
5948 if (prop & ENC_2WORD) | |
5949 { | |
5950 if (prop & ENC_ENDIAN_L) | |
5951 return FIO_UTF16 | FIO_ENDIAN_L; | |
5952 return FIO_UTF16; | |
5953 } | |
5954 return FIO_UTF8; | |
5955 } | |
5956 if (prop & ENC_LATIN1) | |
5957 return FIO_LATIN1; | |
5958 /* must be ENC_DBCS, requires iconv() */ | |
5959 return 0; | |
5960 } | |
5961 | |
5962 #ifdef WIN3264 | |
5963 /* | |
5964 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed | |
5965 * for the conversion MS-Windows can do for us. Also accept "utf-8". | |
5966 * Used for conversion between 'encoding' and 'fileencoding'. | |
5967 */ | |
5968 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5969 get_win_fio_flags(char_u *ptr) |
7 | 5970 { |
5971 int cp; | |
5972 | |
5973 /* Cannot do this when 'encoding' is not utf-8 and not a codepage. */ | |
5974 if (!enc_utf8 && enc_codepage <= 0) | |
5975 return 0; | |
5976 | |
5977 cp = encname2codepage(ptr); | |
5978 if (cp == 0) | |
5979 { | |
5980 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
5981 if (STRCMP(ptr, "utf-8") == 0) | |
5982 cp = CP_UTF8; | |
5983 else | |
5984 # endif | |
5985 return 0; | |
5986 } | |
5987 return FIO_PUT_CP(cp) | FIO_CODEPAGE; | |
5988 } | |
5989 #endif | |
5990 | |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
5991 #ifdef MACOS_CONVERT |
7 | 5992 /* |
5993 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags | |
5994 * needed for the internal conversion to/from utf-8 or latin1. | |
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_mac_fio_flags(char_u *ptr) |
7 | 5998 { |
5999 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
6000 && (enc_canon_props(ptr) & ENC_MACROMAN)) | |
6001 return FIO_MACROMAN; | |
6002 return 0; | |
6003 } | |
6004 #endif | |
6005 | |
6006 /* | |
6007 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size]. | |
6008 * "size" must be at least 2. | |
6009 * Return the name of the encoding and set "*lenp" to the length. | |
6010 * Returns NULL when no BOM found. | |
6011 */ | |
6012 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6013 check_for_bom( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6014 char_u *p, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6015 long size, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6016 int *lenp, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6017 int flags) |
7 | 6018 { |
6019 char *name = NULL; | |
6020 int len = 2; | |
6021 | |
6022 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf | |
1688 | 6023 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0)) |
7 | 6024 { |
6025 name = "utf-8"; /* EF BB BF */ | |
6026 len = 3; | |
6027 } | |
6028 else if (p[0] == 0xff && p[1] == 0xfe) | |
6029 { | |
6030 if (size >= 4 && p[2] == 0 && p[3] == 0 | |
6031 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L))) | |
6032 { | |
6033 name = "ucs-4le"; /* FF FE 00 00 */ | |
6034 len = 4; | |
6035 } | |
1735 | 6036 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L)) |
7 | 6037 name = "ucs-2le"; /* FF FE */ |
1735 | 6038 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L)) |
6039 /* utf-16le is preferred, it also works for ucs-2le text */ | |
7 | 6040 name = "utf-16le"; /* FF FE */ |
6041 } | |
6042 else if (p[0] == 0xfe && p[1] == 0xff | |
6043 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16)) | |
6044 { | |
1547 | 6045 /* Default to utf-16, it works also for ucs-2 text. */ |
6046 if (flags == FIO_UCS2) | |
6047 name = "ucs-2"; /* FE FF */ | |
6048 else | |
7 | 6049 name = "utf-16"; /* FE FF */ |
6050 } | |
6051 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe | |
6052 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4)) | |
6053 { | |
6054 name = "ucs-4"; /* 00 00 FE FF */ | |
6055 len = 4; | |
6056 } | |
6057 | |
6058 *lenp = len; | |
6059 return (char_u *)name; | |
6060 } | |
6061 | |
6062 /* | |
6063 * Generate a BOM in "buf[4]" for encoding "name". | |
6064 * Return the length of the BOM (zero when no BOM). | |
6065 */ | |
6066 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6067 make_bom(char_u *buf, char_u *name) |
7 | 6068 { |
6069 int flags; | |
6070 char_u *p; | |
6071 | |
6072 flags = get_fio_flags(name); | |
6073 | |
6074 /* Can't put a BOM in a non-Unicode file. */ | |
6075 if (flags == FIO_LATIN1 || flags == 0) | |
6076 return 0; | |
6077 | |
6078 if (flags == FIO_UTF8) /* UTF-8 */ | |
6079 { | |
6080 buf[0] = 0xef; | |
6081 buf[1] = 0xbb; | |
6082 buf[2] = 0xbf; | |
6083 return 3; | |
6084 } | |
6085 p = buf; | |
6086 (void)ucs2bytes(0xfeff, &p, flags); | |
6087 return (int)(p - buf); | |
6088 } | |
6089 #endif | |
6090 | |
6091 /* | |
6092 * Try to find a shortname by comparing the fullname with the current | |
6093 * directory. | |
1411 | 6094 * Returns "full_path" or pointer into "full_path" if shortened. |
6095 */ | |
6096 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6097 shorten_fname1(char_u *full_path) |
1411 | 6098 { |
2770 | 6099 char_u *dirname; |
1411 | 6100 char_u *p = full_path; |
6101 | |
2770 | 6102 dirname = alloc(MAXPATHL); |
6103 if (dirname == NULL) | |
6104 return full_path; | |
1411 | 6105 if (mch_dirname(dirname, MAXPATHL) == OK) |
6106 { | |
6107 p = shorten_fname(full_path, dirname); | |
6108 if (p == NULL || *p == NUL) | |
6109 p = full_path; | |
6110 } | |
2770 | 6111 vim_free(dirname); |
1411 | 6112 return p; |
6113 } | |
6114 | |
6115 /* | |
6116 * Try to find a shortname by comparing the fullname with the current | |
6117 * directory. | |
7 | 6118 * Returns NULL if not shorter name possible, pointer into "full_path" |
6119 * otherwise. | |
6120 */ | |
6121 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6122 shorten_fname(char_u *full_path, char_u *dir_name) |
7 | 6123 { |
6124 int len; | |
6125 char_u *p; | |
6126 | |
6127 if (full_path == NULL) | |
6128 return NULL; | |
6129 len = (int)STRLEN(dir_name); | |
6130 if (fnamencmp(dir_name, full_path, len) == 0) | |
6131 { | |
6132 p = full_path + len; | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6133 #if defined(MSWIN) |
7 | 6134 /* |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6135 * MSWIN: when a file is in the root directory, dir_name will end in a |
7 | 6136 * slash, since C: by itself does not define a specific dir. In this |
6137 * case p may already be correct. <negri> | |
6138 */ | |
6139 if (!((len > 2) && (*(p - 2) == ':'))) | |
6140 #endif | |
6141 { | |
6142 if (vim_ispathsep(*p)) | |
6143 ++p; | |
6144 #ifndef VMS /* the path separator is always part of the path */ | |
6145 else | |
6146 p = NULL; | |
6147 #endif | |
6148 } | |
6149 } | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6150 #if defined(MSWIN) |
7 | 6151 /* |
6152 * When using a file in the current drive, remove the drive name: | |
6153 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on | |
6154 * a floppy from "A:\dir" to "B:\dir". | |
6155 */ | |
6156 else if (len > 3 | |
6157 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0]) | |
6158 && full_path[1] == ':' | |
6159 && vim_ispathsep(full_path[2])) | |
6160 p = full_path + 2; | |
6161 #endif | |
6162 else | |
6163 p = NULL; | |
6164 return p; | |
6165 } | |
6166 | |
6167 /* | |
13819
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6168 * Shorten filename of a buffer. |
7 | 6169 * When "force" is TRUE: Use full path from now on for files currently being |
6170 * edited, both for file name and swap file name. Try to shorten the file | |
6171 * names a bit, if safe to do so. | |
6172 * When "force" is FALSE: Only try to shorten absolute file names. | |
6173 * For buffers that have buftype "nofile" or "scratch": never change the file | |
6174 * name. | |
6175 */ | |
6176 void | |
13819
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6177 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
|
6178 { |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6179 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
|
6180 |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6181 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
|
6182 #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
|
6183 && !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
|
6184 #endif |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6185 && !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
|
6186 && (force |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6187 || 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
|
6188 || 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
|
6189 { |
14917
6f2ce3b311de
patch 8.1.0470: pointer ownership around fname_expand() is unclear
Bram Moolenaar <Bram@vim.org>
parents:
14862
diff
changeset
|
6190 if (buf->b_sfname != buf->b_ffname) |
6f2ce3b311de
patch 8.1.0470: pointer ownership around fname_expand() is unclear
Bram Moolenaar <Bram@vim.org>
parents:
14862
diff
changeset
|
6191 VIM_CLEAR(buf->b_sfname); |
13819
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6192 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
|
6193 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
|
6194 { |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6195 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
|
6196 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
|
6197 } |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6198 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
|
6199 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
|
6200 } |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6201 } |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6202 |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6203 /* |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6204 * 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
|
6205 */ |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6206 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6207 shorten_fnames(int force) |
7 | 6208 { |
6209 char_u dirname[MAXPATHL]; | |
6210 buf_T *buf; | |
6211 | |
6212 mch_dirname(dirname, MAXPATHL); | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
6213 FOR_ALL_BUFFERS(buf) |
7 | 6214 { |
13819
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6215 shorten_buf_fname(buf, dirname, force); |
9 | 6216 |
6217 /* Always make the swap file name a full path, a "nofile" buffer may | |
6218 * also have a swap file. */ | |
6219 mf_fullname(buf->b_ml.ml_mfp); | |
7 | 6220 } |
6221 status_redraw_all(); | |
672 | 6222 redraw_tabline = TRUE; |
7 | 6223 } |
6224 | |
6225 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \ | |
6226 || defined(FEAT_GUI_MSWIN) \ | |
6227 || defined(FEAT_GUI_MAC) \ | |
6228 || defined(PROTO) | |
6229 /* | |
6230 * Shorten all filenames in "fnames[count]" by current directory. | |
6231 */ | |
6232 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6233 shorten_filenames(char_u **fnames, int count) |
7 | 6234 { |
6235 int i; | |
6236 char_u dirname[MAXPATHL]; | |
6237 char_u *p; | |
6238 | |
6239 if (fnames == NULL || count < 1) | |
6240 return; | |
6241 mch_dirname(dirname, sizeof(dirname)); | |
6242 for (i = 0; i < count; ++i) | |
6243 { | |
6244 if ((p = shorten_fname(fnames[i], dirname)) != NULL) | |
6245 { | |
6246 /* shorten_fname() returns pointer in given "fnames[i]". If free | |
6247 * "fnames[i]" first, "p" becomes invalid. So we need to copy | |
6248 * "p" first then free fnames[i]. */ | |
6249 p = vim_strsave(p); | |
6250 vim_free(fnames[i]); | |
6251 fnames[i] = p; | |
6252 } | |
6253 } | |
6254 } | |
6255 #endif | |
6256 | |
6257 /* | |
14475
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
6258 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or |
7 | 6259 * fo_o_h.ext for MSDOS or when shortname option set. |
6260 * | |
6261 * Assumed that fname is a valid name found in the filesystem we assure that | |
6262 * the return value is a different name and ends in 'ext'. | |
6263 * "ext" MUST be at most 4 characters long if it starts with a dot, 3 | |
6264 * characters otherwise. | |
6265 * Space for the returned name is allocated, must be freed later. | |
6266 * Returns NULL when out of memory. | |
6267 */ | |
6268 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6269 modname( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6270 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6271 char_u *ext, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6272 int prepend_dot) /* may prepend a '.' to file name */ |
7 | 6273 { |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6274 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname), |
7 | 6275 fname, ext, prepend_dot); |
6276 } | |
6277 | |
6278 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6279 buf_modname( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6280 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
|
6281 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6282 char_u *ext, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6283 int prepend_dot) /* may prepend a '.' to file name */ |
7 | 6284 { |
6285 char_u *retval; | |
6286 char_u *s; | |
6287 char_u *e; | |
6288 char_u *ptr; | |
6289 int fnamelen, extlen; | |
6290 | |
6291 extlen = (int)STRLEN(ext); | |
6292 | |
6293 /* | |
6294 * If there is no file name we must get the name of the current directory | |
6295 * (we need the full path in case :cd is used). | |
6296 */ | |
6297 if (fname == NULL || *fname == NUL) | |
6298 { | |
6299 retval = alloc((unsigned)(MAXPATHL + extlen + 3)); | |
6300 if (retval == NULL) | |
6301 return NULL; | |
6302 if (mch_dirname(retval, MAXPATHL) == FAIL || | |
6303 (fnamelen = (int)STRLEN(retval)) == 0) | |
6304 { | |
6305 vim_free(retval); | |
6306 return NULL; | |
6307 } | |
39 | 6308 if (!after_pathsep(retval, retval + fnamelen)) |
7 | 6309 { |
6310 retval[fnamelen++] = PATHSEP; | |
6311 retval[fnamelen] = NUL; | |
6312 } | |
6313 prepend_dot = FALSE; /* nothing to prepend a dot to */ | |
6314 } | |
6315 else | |
6316 { | |
6317 fnamelen = (int)STRLEN(fname); | |
6318 retval = alloc((unsigned)(fnamelen + extlen + 3)); | |
6319 if (retval == NULL) | |
6320 return NULL; | |
6321 STRCPY(retval, fname); | |
6322 #ifdef VMS | |
6323 vms_remove_version(retval); /* we do not need versions here */ | |
6324 #endif | |
6325 } | |
6326 | |
6327 /* | |
6328 * search backwards until we hit a '/', '\' or ':' replacing all '.' | |
6329 * by '_' for MSDOS or when shortname option set and ext starts with a dot. | |
6330 * Then truncate what is after the '/', '\' or ':' to 8 characters for | |
6331 * MSDOS and 26 characters for AMIGA, a lot more for UNIX. | |
6332 */ | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
6333 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr)) |
7 | 6334 { |
6335 if (*ext == '.' | |
2823 | 6336 #ifdef USE_LONG_FNAME |
7 | 6337 && (!USE_LONG_FNAME || shortname) |
2823 | 6338 #else |
7 | 6339 && shortname |
2823 | 6340 #endif |
7 | 6341 ) |
6342 if (*ptr == '.') /* replace '.' by '_' */ | |
6343 *ptr = '_'; | |
6344 if (vim_ispathsep(*ptr)) | |
391 | 6345 { |
6346 ++ptr; | |
7 | 6347 break; |
391 | 6348 } |
6349 } | |
7 | 6350 |
6351 /* the file name has at most BASENAMELEN characters. */ | |
6352 if (STRLEN(ptr) > (unsigned)BASENAMELEN) | |
6353 ptr[BASENAMELEN] = '\0'; | |
6354 | |
6355 s = ptr + STRLEN(ptr); | |
6356 | |
6357 /* | |
6358 * For 8.3 file names we may have to reduce the length. | |
6359 */ | |
6360 #ifdef USE_LONG_FNAME | |
6361 if (!USE_LONG_FNAME || shortname) | |
6362 #else | |
6363 if (shortname) | |
6364 #endif | |
6365 { | |
6366 /* | |
6367 * If there is no file name, or the file name ends in '/', and the | |
6368 * extension starts with '.', put a '_' before the dot, because just | |
6369 * ".ext" is invalid. | |
6370 */ | |
6371 if (fname == NULL || *fname == NUL | |
6372 || vim_ispathsep(fname[STRLEN(fname) - 1])) | |
6373 { | |
6374 if (*ext == '.') | |
6375 *s++ = '_'; | |
6376 } | |
6377 /* | |
6378 * If the extension starts with '.', truncate the base name at 8 | |
6379 * characters | |
6380 */ | |
6381 else if (*ext == '.') | |
6382 { | |
1877 | 6383 if ((size_t)(s - ptr) > (size_t)8) |
7 | 6384 { |
6385 s = ptr + 8; | |
6386 *s = '\0'; | |
6387 } | |
6388 } | |
6389 /* | |
6390 * If the extension doesn't start with '.', and the file name | |
6391 * doesn't have an extension yet, append a '.' | |
6392 */ | |
6393 else if ((e = vim_strchr(ptr, '.')) == NULL) | |
6394 *s++ = '.'; | |
6395 /* | |
6396 * If the extension doesn't start with '.', and there already is an | |
1201 | 6397 * extension, it may need to be truncated |
7 | 6398 */ |
6399 else if ((int)STRLEN(e) + extlen > 4) | |
6400 s = e + 4 - extlen; | |
6401 } | |
7408
1886f2863437
commit https://github.com/vim/vim/commit/e7fedb6ebe72d9a475aa65109b77d5ed4667067a
Christian Brabandt <cb@256bit.org>
parents:
7320
diff
changeset
|
6402 #if defined(USE_LONG_FNAME) || defined(WIN3264) |
7 | 6403 /* |
6404 * If there is no file name, and the extension starts with '.', put a | |
6405 * '_' before the dot, because just ".ext" may be invalid if it's on a | |
6406 * FAT partition, and on HPFS it doesn't matter. | |
6407 */ | |
6408 else if ((fname == NULL || *fname == NUL) && *ext == '.') | |
6409 *s++ = '_'; | |
6410 #endif | |
6411 | |
6412 /* | |
1651 | 6413 * Append the extension. |
7 | 6414 * ext can start with '.' and cannot exceed 3 more characters. |
6415 */ | |
6416 STRCPY(s, ext); | |
6417 | |
6418 /* | |
6419 * Prepend the dot. | |
6420 */ | |
2823 | 6421 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.' |
7 | 6422 #ifdef USE_LONG_FNAME |
6423 && USE_LONG_FNAME | |
6424 #endif | |
6425 ) | |
6426 { | |
1620 | 6427 STRMOVE(e + 1, e); |
7 | 6428 *e = '.'; |
6429 } | |
6430 | |
6431 /* | |
6432 * Check that, after appending the extension, the file name is really | |
6433 * different. | |
6434 */ | |
6435 if (fname != NULL && STRCMP(fname, retval) == 0) | |
6436 { | |
6437 /* we search for a character that can be replaced by '_' */ | |
6438 while (--s >= ptr) | |
6439 { | |
6440 if (*s != '_') | |
6441 { | |
6442 *s = '_'; | |
6443 break; | |
6444 } | |
6445 } | |
6446 if (s < ptr) /* fname was "________.<ext>", how tricky! */ | |
6447 *ptr = 'v'; | |
6448 } | |
6449 return retval; | |
6450 } | |
6451 | |
6452 /* | |
6453 * Like fgets(), but if the file line is too long, it is truncated and the | |
6454 * 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
|
6455 * If the line is truncated then buf[size - 2] will not be NUL. |
7 | 6456 */ |
6457 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6458 vim_fgets(char_u *buf, int size, FILE *fp) |
7 | 6459 { |
6460 char *eof; | |
6461 #define FGETS_SIZE 200 | |
6462 char tbuf[FGETS_SIZE]; | |
6463 | |
6464 buf[size - 2] = NUL; | |
6465 #ifdef USE_CR | |
6466 eof = fgets_cr((char *)buf, size, fp); | |
6467 #else | |
6468 eof = fgets((char *)buf, size, fp); | |
6469 #endif | |
6470 if (buf[size - 2] != NUL && buf[size - 2] != '\n') | |
6471 { | |
6472 buf[size - 1] = NUL; /* Truncate the line */ | |
6473 | |
6474 /* Now throw away the rest of the line: */ | |
6475 do | |
6476 { | |
6477 tbuf[FGETS_SIZE - 2] = NUL; | |
6478 #ifdef USE_CR | |
14730
193471015e1a
patch 8.1.0377: xdiff doesn't use the Vim memory allocation functions
Christian Brabandt <cb@256bit.org>
parents:
14700
diff
changeset
|
6479 vim_ignoredp = fgets_cr((char *)tbuf, FGETS_SIZE, fp); |
7 | 6480 #else |
14730
193471015e1a
patch 8.1.0377: xdiff doesn't use the Vim memory allocation functions
Christian Brabandt <cb@256bit.org>
parents:
14700
diff
changeset
|
6481 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp); |
7 | 6482 #endif |
6483 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n'); | |
6484 } | |
6485 return (eof == NULL); | |
6486 } | |
6487 | |
6488 #if defined(USE_CR) || defined(PROTO) | |
6489 /* | |
6490 * Like vim_fgets(), but accept any line terminator: CR, CR-LF or LF. | |
6491 * Returns TRUE for end-of-file. | |
6492 * Only used for the Mac, because it's much slower than vim_fgets(). | |
6493 */ | |
6494 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6495 tag_fgets(char_u *buf, int size, FILE *fp) |
7 | 6496 { |
6497 int i = 0; | |
6498 int c; | |
6499 int eof = FALSE; | |
6500 | |
6501 for (;;) | |
6502 { | |
6503 c = fgetc(fp); | |
6504 if (c == EOF) | |
6505 { | |
6506 eof = TRUE; | |
6507 break; | |
6508 } | |
6509 if (c == '\r') | |
6510 { | |
6511 /* Always store a NL for end-of-line. */ | |
6512 if (i < size - 1) | |
6513 buf[i++] = '\n'; | |
6514 c = fgetc(fp); | |
6515 if (c != '\n') /* Macintosh format: single CR. */ | |
6516 ungetc(c, fp); | |
6517 break; | |
6518 } | |
6519 if (i < size - 1) | |
6520 buf[i++] = c; | |
6521 if (c == '\n') | |
6522 break; | |
6523 } | |
6524 buf[i] = NUL; | |
6525 return eof; | |
6526 } | |
6527 #endif | |
6528 | |
6529 /* | |
6530 * rename() only works if both files are on the same file system, this | |
6531 * function will (attempts to?) copy the file across if rename fails -- webb | |
6532 * Return -1 for failure, 0 for success. | |
6533 */ | |
6534 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6535 vim_rename(char_u *from, char_u *to) |
7 | 6536 { |
6537 int fd_in; | |
6538 int fd_out; | |
6539 int n; | |
6540 char *errmsg = NULL; | |
6541 char *buffer; | |
6542 #ifdef AMIGA | |
6543 BPTR flock; | |
6544 #endif | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6545 stat_T st; |
194 | 6546 long perm; |
199 | 6547 #ifdef HAVE_ACL |
6548 vim_acl_T acl; /* ACL from original file */ | |
6549 #endif | |
1779 | 6550 int use_tmp_file = FALSE; |
7 | 6551 |
6552 /* | |
1779 | 6553 * When the names are identical, there is nothing to do. When they refer |
6554 * to the same file (ignoring case and slash/backslash differences) but | |
6555 * the file name differs we need to go through a temp file. | |
7 | 6556 */ |
6557 if (fnamecmp(from, to) == 0) | |
1779 | 6558 { |
4242 | 6559 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0) |
1779 | 6560 use_tmp_file = TRUE; |
6561 else | |
6562 return 0; | |
6563 } | |
7 | 6564 |
6565 /* | |
6566 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted. | |
6567 */ | |
6568 if (mch_stat((char *)from, &st) < 0) | |
6569 return -1; | |
6570 | |
1778 | 6571 #ifdef UNIX |
6572 { | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6573 stat_T st_to; |
1778 | 6574 |
6575 /* It's possible for the source and destination to be the same file. | |
6576 * This happens when "from" and "to" differ in case and are on a FAT32 | |
6577 * filesystem. In that case go through a temp file name. */ | |
6578 if (mch_stat((char *)to, &st_to) >= 0 | |
6579 && st.st_dev == st_to.st_dev | |
6580 && st.st_ino == st_to.st_ino) | |
1779 | 6581 use_tmp_file = TRUE; |
6582 } | |
6583 #endif | |
2793 | 6584 #ifdef WIN3264 |
6585 { | |
6586 BY_HANDLE_FILE_INFORMATION info1, info2; | |
6587 | |
6588 /* It's possible for the source and destination to be the same file. | |
6589 * In that case go through a temp file name. This makes rename("foo", | |
6590 * "./foo") a no-op (in a complicated way). */ | |
6591 if (win32_fileinfo(from, &info1) == FILEINFO_OK | |
6592 && win32_fileinfo(to, &info2) == FILEINFO_OK | |
6593 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber | |
6594 && info1.nFileIndexHigh == info2.nFileIndexHigh | |
6595 && info1.nFileIndexLow == info2.nFileIndexLow) | |
6596 use_tmp_file = TRUE; | |
6597 } | |
6598 #endif | |
1779 | 6599 |
6600 if (use_tmp_file) | |
6601 { | |
6602 char tempname[MAXPATHL + 1]; | |
6603 | |
6604 /* | |
6605 * Find a name that doesn't exist and is in the same directory. | |
6606 * Rename "from" to "tempname" and then rename "tempname" to "to". | |
6607 */ | |
6608 if (STRLEN(from) >= MAXPATHL - 5) | |
6609 return -1; | |
6610 STRCPY(tempname, from); | |
6611 for (n = 123; n < 99999; ++n) | |
6612 { | |
6613 sprintf((char *)gettail((char_u *)tempname), "%d", n); | |
6614 if (mch_stat(tempname, &st) < 0) | |
6615 { | |
6616 if (mch_rename((char *)from, tempname) == 0) | |
1778 | 6617 { |
1779 | 6618 if (mch_rename(tempname, (char *)to) == 0) |
6619 return 0; | |
6620 /* Strange, the second step failed. Try moving the | |
6621 * file back and return failure. */ | |
6622 mch_rename(tempname, (char *)from); | |
1778 | 6623 return -1; |
6624 } | |
1779 | 6625 /* If it fails for one temp name it will most likely fail |
6626 * for any temp name, give up. */ | |
6627 return -1; | |
6628 } | |
6629 } | |
6630 return -1; | |
1778 | 6631 } |
6632 | |
7 | 6633 /* |
6634 * Delete the "to" file, this is required on some systems to make the | |
6635 * mch_rename() work, on other systems it makes sure that we don't have | |
6636 * two files when the mch_rename() fails. | |
6637 */ | |
6638 | |
6639 #ifdef AMIGA | |
6640 /* | |
6641 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible | |
6642 * that the name of the "to" file is the same as the "from" file, even | |
1201 | 6643 * though the names are different. To avoid the chance of accidentally |
7 | 6644 * deleting the "from" file (horror!) we lock it during the remove. |
6645 * | |
6646 * When used for making a backup before writing the file: This should not | |
6647 * happen with ":w", because startscript() should detect this problem and | |
6648 * set buf->b_shortname, causing modname() to return a correct ".bak" file | |
6649 * name. This problem does exist with ":w filename", but then the | |
6650 * original file will be somewhere else so the backup isn't really | |
6651 * important. If autoscripting is off the rename may fail. | |
6652 */ | |
6653 flock = Lock((UBYTE *)from, (long)ACCESS_READ); | |
6654 #endif | |
6655 mch_remove(to); | |
6656 #ifdef AMIGA | |
6657 if (flock) | |
6658 UnLock(flock); | |
6659 #endif | |
6660 | |
6661 /* | |
6662 * First try a normal rename, return if it works. | |
6663 */ | |
6664 if (mch_rename((char *)from, (char *)to) == 0) | |
6665 return 0; | |
6666 | |
6667 /* | |
6668 * Rename() failed, try copying the file. | |
6669 */ | |
194 | 6670 perm = mch_getperm(from); |
199 | 6671 #ifdef HAVE_ACL |
6672 /* For systems that support ACL: get the ACL from the original file. */ | |
6673 acl = mch_get_acl(from); | |
6674 #endif | |
7 | 6675 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0); |
6676 if (fd_in == -1) | |
1651 | 6677 { |
6678 #ifdef HAVE_ACL | |
6679 mch_free_acl(acl); | |
6680 #endif | |
7 | 6681 return -1; |
1651 | 6682 } |
194 | 6683 |
6684 /* Create the new file with same permissions as the original. */ | |
557 | 6685 fd_out = mch_open((char *)to, |
6686 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm); | |
7 | 6687 if (fd_out == -1) |
6688 { | |
6689 close(fd_in); | |
1651 | 6690 #ifdef HAVE_ACL |
6691 mch_free_acl(acl); | |
6692 #endif | |
7 | 6693 return -1; |
6694 } | |
6695 | |
6696 buffer = (char *)alloc(BUFSIZE); | |
6697 if (buffer == NULL) | |
6698 { | |
1651 | 6699 close(fd_out); |
7 | 6700 close(fd_in); |
1651 | 6701 #ifdef HAVE_ACL |
6702 mch_free_acl(acl); | |
6703 #endif | |
7 | 6704 return -1; |
6705 } | |
6706 | |
2664 | 6707 while ((n = read_eintr(fd_in, buffer, BUFSIZE)) > 0) |
6708 if (write_eintr(fd_out, buffer, n) != n) | |
7 | 6709 { |
6710 errmsg = _("E208: Error writing to \"%s\""); | |
6711 break; | |
6712 } | |
6713 | |
6714 vim_free(buffer); | |
6715 close(fd_in); | |
6716 if (close(fd_out) < 0) | |
6717 errmsg = _("E209: Error closing \"%s\""); | |
6718 if (n < 0) | |
6719 { | |
6720 errmsg = _("E210: Error reading \"%s\""); | |
6721 to = from; | |
6722 } | |
1201 | 6723 #ifndef UNIX /* for Unix mch_open() already set the permission */ |
194 | 6724 mch_setperm(to, perm); |
570 | 6725 #endif |
199 | 6726 #ifdef HAVE_ACL |
6727 mch_set_acl(to, acl); | |
1651 | 6728 mch_free_acl(acl); |
199 | 6729 #endif |
5788 | 6730 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
5483 | 6731 mch_copy_sec(from, to); |
5479 | 6732 #endif |
7 | 6733 if (errmsg != NULL) |
6734 { | |
6735 EMSG2(errmsg, to); | |
6736 return -1; | |
6737 } | |
6738 mch_remove(from); | |
6739 return 0; | |
6740 } | |
6741 | |
6742 static int already_warned = FALSE; | |
6743 | |
6744 /* | |
6745 * Check if any not hidden buffer has been changed. | |
6746 * Postpone the check if there are characters in the stuff buffer, a global | |
6747 * command is being executed, a mapping is being executed or an autocommand is | |
6748 * busy. | |
6749 * Returns TRUE if some message was written (screen should be redrawn and | |
6750 * cursor positioned). | |
6751 */ | |
6752 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6753 check_timestamps( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6754 int focus) /* called for GUI focus event */ |
7 | 6755 { |
6756 buf_T *buf; | |
6757 int didit = 0; | |
6758 int n; | |
6759 | |
6760 /* Don't check timestamps while system() or another low-level function may | |
6761 * cause us to lose and gain focus. */ | |
6762 if (no_check_timestamps > 0) | |
6763 return FALSE; | |
6764 | |
6765 /* Avoid doing a check twice. The OK/Reload dialog can cause a focus | |
6766 * event and we would keep on checking if the file is steadily growing. | |
6767 * Do check again after typing something. */ | |
6768 if (focus && did_check_timestamps) | |
6769 { | |
6770 need_check_timestamps = TRUE; | |
6771 return FALSE; | |
6772 } | |
6773 | |
6774 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
|
6775 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0) |
7 | 6776 need_check_timestamps = TRUE; /* check later */ |
6777 else | |
6778 { | |
6779 ++no_wait_return; | |
6780 did_check_timestamps = TRUE; | |
6781 already_warned = FALSE; | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
6782 FOR_ALL_BUFFERS(buf) |
7 | 6783 { |
6784 /* Only check buffers in a window. */ | |
6785 if (buf->b_nwindows > 0) | |
6786 { | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6787 bufref_T bufref; |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6788 |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6789 set_bufref(&bufref, buf); |
7 | 6790 n = buf_check_timestamp(buf, focus); |
6791 if (didit < n) | |
6792 didit = n; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6793 if (n > 0 && !bufref_valid(&bufref)) |
7 | 6794 { |
6795 /* Autocommands have removed the buffer, start at the | |
6796 * first one again. */ | |
6797 buf = firstbuf; | |
6798 continue; | |
6799 } | |
6800 } | |
6801 } | |
6802 --no_wait_return; | |
6803 need_check_timestamps = FALSE; | |
6804 if (need_wait_return && didit == 2) | |
6805 { | |
6806 /* make sure msg isn't overwritten */ | |
6807 msg_puts((char_u *)"\n"); | |
6808 out_flush(); | |
6809 } | |
6810 } | |
6811 return didit; | |
6812 } | |
6813 | |
6814 /* | |
6815 * Move all the lines from buffer "frombuf" to buffer "tobuf". | |
6816 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not | |
6817 * empty. | |
6818 */ | |
6819 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6820 move_lines(buf_T *frombuf, buf_T *tobuf) |
7 | 6821 { |
6822 buf_T *tbuf = curbuf; | |
6823 int retval = OK; | |
6824 linenr_T lnum; | |
6825 char_u *p; | |
6826 | |
6827 /* Copy the lines in "frombuf" to "tobuf". */ | |
6828 curbuf = tobuf; | |
6829 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum) | |
6830 { | |
6831 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE)); | |
6832 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL) | |
6833 { | |
6834 vim_free(p); | |
6835 retval = FAIL; | |
6836 break; | |
6837 } | |
6838 vim_free(p); | |
6839 } | |
6840 | |
6841 /* Delete all the lines in "frombuf". */ | |
6842 if (retval != FAIL) | |
6843 { | |
6844 curbuf = frombuf; | |
1055 | 6845 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum) |
6846 if (ml_delete(lnum, FALSE) == FAIL) | |
7 | 6847 { |
6848 /* Oops! We could try putting back the saved lines, but that | |
6849 * might fail again... */ | |
6850 retval = FAIL; | |
6851 break; | |
6852 } | |
6853 } | |
6854 | |
6855 curbuf = tbuf; | |
6856 return retval; | |
6857 } | |
6858 | |
6859 /* | |
6860 * Check if buffer "buf" has been changed. | |
6861 * Also check if the file for a new buffer unexpectedly appeared. | |
6862 * return 1 if a changed buffer was found. | |
6863 * return 2 if a message has been displayed. | |
6864 * return 0 otherwise. | |
6865 */ | |
6866 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6867 buf_check_timestamp( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6868 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6869 int focus UNUSED) /* called for GUI focus event */ |
7 | 6870 { |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6871 stat_T st; |
7 | 6872 int stat_res; |
6873 int retval = 0; | |
6874 char_u *path; | |
6875 char_u *tbuf; | |
6876 char *mesg = NULL; | |
188 | 6877 char *mesg2 = ""; |
7 | 6878 int helpmesg = FALSE; |
6879 int reload = FALSE; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6880 char *reason; |
7 | 6881 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
6882 int can_reload = FALSE; | |
6883 #endif | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6884 off_T orig_size = buf->b_orig_size; |
7 | 6885 int orig_mode = buf->b_orig_mode; |
6886 #ifdef FEAT_GUI | |
6887 int save_mouse_correct = need_mouse_correct; | |
6888 #endif | |
6889 static int busy = FALSE; | |
179 | 6890 int n; |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
6891 #ifdef FEAT_EVAL |
179 | 6892 char_u *s; |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
6893 #endif |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6894 bufref_T bufref; |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6895 |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6896 set_bufref(&bufref, buf); |
7 | 6897 |
6898 /* If there is no file name, the buffer is not loaded, 'buftype' is | |
6899 * set, we are in the middle of a save or being called recursively: ignore | |
6900 * this buffer. */ | |
6901 if (buf->b_ffname == NULL | |
6902 || 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
|
6903 || !bt_normal(buf) |
7 | 6904 || buf->b_saving |
6905 || busy | |
33 | 6906 #ifdef FEAT_NETBEANS_INTG |
6907 || isNetbeansBuffer(buf) | |
6908 #endif | |
12064
407a475c67fd
patch 8.0.0912: cannot run a job in a hidden terminal
Christian Brabandt <cb@256bit.org>
parents:
11757
diff
changeset
|
6909 #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
|
6910 || 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
|
6911 #endif |
7 | 6912 ) |
6913 return 0; | |
6914 | |
6915 if ( !(buf->b_flags & BF_NOTEDITED) | |
6916 && buf->b_mtime != 0 | |
6917 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0 | |
6918 || time_differs((long)st.st_mtime, buf->b_mtime) | |
5863 | 6919 || st.st_size != buf->b_orig_size |
7 | 6920 #ifdef HAVE_ST_MODE |
6921 || (int)st.st_mode != buf->b_orig_mode | |
6922 #else | |
6923 || mch_getperm(buf->b_ffname) != buf->b_orig_mode | |
6924 #endif | |
6925 )) | |
6926 { | |
6927 retval = 1; | |
6928 | |
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
|
6929 // 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
|
6930 // FileChangedShell autocmd) |
7 | 6931 if (stat_res < 0) |
6932 { | |
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
|
6933 // 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
|
6934 // 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
|
6935 buf->b_mtime = buf->b_p_ar; |
7 | 6936 buf->b_orig_size = 0; |
6937 buf->b_orig_mode = 0; | |
6938 } | |
6939 else | |
6940 buf_store_time(buf, &st, buf->b_ffname); | |
6941 | |
6942 /* Don't do anything for a directory. Might contain the file | |
6943 * explorer. */ | |
6944 if (mch_isdir(buf->b_fname)) | |
6945 ; | |
6946 | |
6947 /* | |
6948 * If 'autoread' is set, the buffer has no changes and the file still | |
6949 * exists, reload the buffer. Use the buffer-local option value if it | |
6950 * was set, the global option value otherwise. | |
6951 */ | |
6952 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar) | |
6953 && !bufIsChanged(buf) && stat_res >= 0) | |
6954 reload = TRUE; | |
6955 else | |
6956 { | |
179 | 6957 if (stat_res < 0) |
6958 reason = "deleted"; | |
6959 else if (bufIsChanged(buf)) | |
6960 reason = "conflict"; | |
6961 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf)) | |
6962 reason = "changed"; | |
6963 else if (orig_mode != buf->b_orig_mode) | |
6964 reason = "mode"; | |
6965 else | |
6966 reason = "time"; | |
6967 | |
7 | 6968 /* |
6969 * Only give the warning if there are no FileChangedShell | |
6970 * autocommands. | |
6971 * Avoid being called recursively by setting "busy". | |
6972 */ | |
6973 busy = TRUE; | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
6974 #ifdef FEAT_EVAL |
179 | 6975 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1); |
6976 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
|
6977 #endif |
1834 | 6978 ++allbuf_lock; |
7 | 6979 n = apply_autocmds(EVENT_FILECHANGEDSHELL, |
6980 buf->b_fname, buf->b_fname, FALSE, buf); | |
1834 | 6981 --allbuf_lock; |
7 | 6982 busy = FALSE; |
6983 if (n) | |
6984 { | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6985 if (!bufref_valid(&bufref)) |
7 | 6986 EMSG(_("E246: FileChangedShell autocommand deleted buffer")); |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
6987 #ifdef FEAT_EVAL |
179 | 6988 s = get_vim_var_str(VV_FCS_CHOICE); |
6989 if (STRCMP(s, "reload") == 0 && *reason != 'd') | |
6990 reload = TRUE; | |
6991 else if (STRCMP(s, "ask") == 0) | |
6992 n = FALSE; | |
6993 else | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
6994 #endif |
179 | 6995 return 2; |
7 | 6996 } |
179 | 6997 if (!n) |
7 | 6998 { |
179 | 6999 if (*reason == 'd') |
7000 mesg = _("E211: File \"%s\" no longer available"); | |
7 | 7001 else |
7002 { | |
7003 helpmesg = TRUE; | |
7004 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
7005 can_reload = TRUE; | |
7006 #endif | |
7007 /* | |
7008 * Check if the file contents really changed to avoid | |
7009 * giving a warning when only the timestamp was set (e.g., | |
7010 * checked out of CVS). Always warn when the buffer was | |
7011 * changed. | |
7012 */ | |
179 | 7013 if (reason[2] == 'n') |
7014 { | |
7 | 7015 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well"); |
179 | 7016 mesg2 = _("See \":help W12\" for more info."); |
7017 } | |
7018 else if (reason[1] == 'h') | |
7019 { | |
7 | 7020 mesg = _("W11: Warning: File \"%s\" has changed since editing started"); |
179 | 7021 mesg2 = _("See \":help W11\" for more info."); |
7022 } | |
7023 else if (*reason == 'm') | |
7024 { | |
7 | 7025 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started"); |
179 | 7026 mesg2 = _("See \":help W16\" for more info."); |
7027 } | |
1913 | 7028 else |
7029 /* Only timestamp changed, store it to avoid a warning | |
7030 * in check_mtime() later. */ | |
7031 buf->b_mtime_read = buf->b_mtime; | |
7 | 7032 } |
7033 } | |
7034 } | |
7035 | |
7036 } | |
7037 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W) | |
7038 && vim_fexists(buf->b_ffname)) | |
7039 { | |
7040 retval = 1; | |
7041 mesg = _("W13: Warning: File \"%s\" has been created after editing started"); | |
7042 buf->b_flags |= BF_NEW_W; | |
7043 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
7044 can_reload = TRUE; | |
7045 #endif | |
7046 } | |
7047 | |
7048 if (mesg != NULL) | |
7049 { | |
7050 path = home_replace_save(buf, buf->b_fname); | |
7051 if (path != NULL) | |
7052 { | |
179 | 7053 if (!helpmesg) |
7 | 7054 mesg2 = ""; |
7055 tbuf = alloc((unsigned)(STRLEN(path) + STRLEN(mesg) | |
7056 + STRLEN(mesg2) + 2)); | |
7057 sprintf((char *)tbuf, mesg, path); | |
1848 | 7058 #ifdef FEAT_EVAL |
7059 /* Set warningmsg here, before the unimportant and output-specific | |
7060 * mesg2 has been appended. */ | |
7061 set_vim_var_string(VV_WARNINGMSG, tbuf, -1); | |
7062 #endif | |
7 | 7063 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
7064 if (can_reload) | |
7065 { | |
7066 if (*mesg2 != NUL) | |
7067 { | |
7068 STRCAT(tbuf, "\n"); | |
7069 STRCAT(tbuf, mesg2); | |
7070 } | |
7071 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"), tbuf, | |
2684 | 7072 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2) |
7 | 7073 reload = TRUE; |
7074 } | |
7075 else | |
7076 #endif | |
7077 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned) | |
7078 { | |
7079 if (*mesg2 != NUL) | |
7080 { | |
7081 STRCAT(tbuf, "; "); | |
7082 STRCAT(tbuf, mesg2); | |
7083 } | |
7084 EMSG(tbuf); | |
7085 retval = 2; | |
7086 } | |
7087 else | |
7088 { | |
7089 if (!autocmd_busy) | |
7090 { | |
7091 msg_start(); | |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
7092 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST); |
7 | 7093 if (*mesg2 != NUL) |
7094 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
|
7095 HL_ATTR(HLF_W) + MSG_HIST); |
7 | 7096 msg_clr_eos(); |
7097 (void)msg_end(); | |
7098 if (emsg_silent == 0) | |
7099 { | |
7100 out_flush(); | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
7101 #ifdef FEAT_GUI |
7 | 7102 if (!focus) |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
7103 #endif |
7 | 7104 /* give the user some time to think about it */ |
7105 ui_delay(1000L, TRUE); | |
7106 | |
7107 /* don't redraw and erase the message */ | |
7108 redraw_cmdline = FALSE; | |
7109 } | |
7110 } | |
7111 already_warned = TRUE; | |
7112 } | |
7113 | |
7114 vim_free(path); | |
7115 vim_free(tbuf); | |
7116 } | |
7117 } | |
7118 | |
7119 if (reload) | |
3776 | 7120 { |
311 | 7121 /* Reload the buffer. */ |
628 | 7122 buf_reload(buf, orig_mode); |
3776 | 7123 #ifdef FEAT_PERSISTENT_UNDO |
7124 if (buf->b_p_udf && buf->b_ffname != NULL) | |
7125 { | |
7126 char_u hash[UNDO_HASH_SIZE]; | |
7127 buf_T *save_curbuf = curbuf; | |
7128 | |
7129 /* Any existing undo file is unusable, write it now. */ | |
7130 curbuf = buf; | |
7131 u_compute_hash(hash); | |
7132 u_write_undo(NULL, FALSE, buf, hash); | |
7133 curbuf = save_curbuf; | |
7134 } | |
7135 #endif | |
7136 } | |
7 | 7137 |
1620 | 7138 /* 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
|
7139 if (bufref_valid(&bufref) && retval != 0) |
765 | 7140 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, |
7141 buf->b_fname, buf->b_fname, FALSE, buf); | |
7 | 7142 #ifdef FEAT_GUI |
7143 /* restore this in case an autocommand has set it; it would break | |
7144 * 'mousefocus' */ | |
7145 need_mouse_correct = save_mouse_correct; | |
7146 #endif | |
7147 | |
7148 return retval; | |
7149 } | |
7150 | |
311 | 7151 /* |
7152 * Reload a buffer that is already loaded. | |
7153 * Used when the file was changed outside of Vim. | |
628 | 7154 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected. |
7155 * buf->b_orig_mode may have been reset already. | |
311 | 7156 */ |
7157 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7158 buf_reload(buf_T *buf, int orig_mode) |
311 | 7159 { |
7160 exarg_T ea; | |
7161 pos_T old_cursor; | |
7162 linenr_T old_topline; | |
7163 int old_ro = buf->b_p_ro; | |
7164 buf_T *savebuf; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
7165 bufref_T bufref; |
311 | 7166 int saved = OK; |
7167 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
|
7168 int flags = READ_NEW; |
311 | 7169 |
7170 /* set curwin/curbuf for "buf" and save some things */ | |
7171 aucmd_prepbuf(&aco, buf); | |
7172 | |
7173 /* We only want to read the text from the file, not reset the syntax | |
7174 * highlighting, clear marks, diff status, etc. Force the fileformat | |
7175 * and encoding to be the same. */ | |
7176 if (prep_exarg(&ea, buf) == OK) | |
7177 { | |
7178 old_cursor = curwin->w_cursor; | |
7179 old_topline = curwin->w_topline; | |
7180 | |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
7181 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
|
7182 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7183 /* 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
|
7184 * 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
|
7185 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
|
7186 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
|
7187 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
|
7188 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7189 |
311 | 7190 /* |
7191 * To behave like when a new file is edited (matters for | |
7192 * BufReadPost autocommands) we first need to delete the current | |
7193 * buffer contents. But if reading the file fails we should keep | |
7194 * the old contents. Can't use memory only, the file might be | |
7195 * too big. Use a hidden buffer to move the buffer contents to. | |
7196 */ | |
11121
778c10516955
patch 8.0.0448: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
10952
diff
changeset
|
7197 if (BUFEMPTY() || saved == FAIL) |
311 | 7198 savebuf = NULL; |
7199 else | |
7200 { | |
7201 /* Allocate a buffer without putting it in the buffer list. */ | |
7202 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
|
7203 set_bufref(&bufref, savebuf); |
837 | 7204 if (savebuf != NULL && buf == curbuf) |
311 | 7205 { |
7206 /* Open the memline. */ | |
7207 curbuf = savebuf; | |
7208 curwin->w_buffer = savebuf; | |
625 | 7209 saved = ml_open(curbuf); |
311 | 7210 curbuf = buf; |
7211 curwin->w_buffer = buf; | |
7212 } | |
837 | 7213 if (savebuf == NULL || saved == FAIL || buf != curbuf |
311 | 7214 || move_lines(buf, savebuf) == FAIL) |
7215 { | |
7216 EMSG2(_("E462: Could not prepare for reloading \"%s\""), | |
7217 buf->b_fname); | |
7218 saved = FAIL; | |
7219 } | |
7220 } | |
7221 | |
7222 if (saved == OK) | |
7223 { | |
7224 curbuf->b_flags |= BF_CHECK_RO; /* check for RO again */ | |
7225 keep_filetype = TRUE; /* don't detect 'filetype' */ | |
7226 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, | |
7227 (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
|
7228 (linenr_T)MAXLNUM, &ea, flags) != OK) |
311 | 7229 { |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
7230 #if defined(FEAT_EVAL) |
311 | 7231 if (!aborting()) |
7232 #endif | |
7233 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
|
7234 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf) |
311 | 7235 { |
7236 /* Put the text back from the save buffer. First | |
7237 * 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
|
7238 while (!BUFEMPTY()) |
837 | 7239 if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL) |
311 | 7240 break; |
7241 (void)move_lines(savebuf, buf); | |
7242 } | |
7243 } | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7244 else if (buf == curbuf) /* "buf" still valid */ |
311 | 7245 { |
7246 /* Mark the buffer as unmodified and free undo info. */ | |
7247 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
|
7248 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
|
7249 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7250 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
|
7251 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
|
7252 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7253 else |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
7254 { |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7255 /* 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
|
7256 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
|
7257 } |
311 | 7258 } |
7259 } | |
7260 vim_free(ea.cmd); | |
7261 | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
7262 if (savebuf != NULL && bufref_valid(&bufref)) |
311 | 7263 wipe_buffer(savebuf, FALSE); |
7264 | |
7265 #ifdef FEAT_DIFF | |
7266 /* Invalidate diff info if necessary. */ | |
837 | 7267 diff_invalidate(curbuf); |
311 | 7268 #endif |
7269 | |
7270 /* Restore the topline and cursor position and check it (lines may | |
7271 * have been removed). */ | |
7272 if (old_topline > curbuf->b_ml.ml_line_count) | |
7273 curwin->w_topline = curbuf->b_ml.ml_line_count; | |
7274 else | |
7275 curwin->w_topline = old_topline; | |
7276 curwin->w_cursor = old_cursor; | |
7277 check_cursor(); | |
7278 update_topline(); | |
7279 keep_filetype = FALSE; | |
7280 #ifdef FEAT_FOLDING | |
7281 { | |
1863 | 7282 win_T *wp; |
7283 tabpage_T *tp; | |
311 | 7284 |
7285 /* Update folds unless they are defined manually. */ | |
1863 | 7286 FOR_ALL_TAB_WINDOWS(tp, wp) |
311 | 7287 if (wp->w_buffer == curwin->w_buffer |
7288 && !foldmethodIsManual(wp)) | |
7289 foldUpdateAll(wp); | |
7290 } | |
7291 #endif | |
7292 /* If the mode didn't change and 'readonly' was set, keep the old | |
7293 * value; the user probably used the ":view" command. But don't | |
7294 * reset it, might have had a read error. */ | |
7295 if (orig_mode == curbuf->b_orig_mode) | |
7296 curbuf->b_p_ro |= old_ro; | |
4071 | 7297 |
7298 /* Modelines must override settings done by autocommands. */ | |
7299 do_modelines(0); | |
311 | 7300 } |
7301 | |
7302 /* restore curwin/curbuf and a few other things */ | |
7303 aucmd_restbuf(&aco); | |
7304 /* Careful: autocommands may have made "buf" invalid! */ | |
7305 } | |
7306 | |
7 | 7307 void |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
7308 buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED) |
7 | 7309 { |
7310 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
|
7311 buf->b_orig_size = st->st_size; |
7 | 7312 #ifdef HAVE_ST_MODE |
7313 buf->b_orig_mode = (int)st->st_mode; | |
7314 #else | |
7315 buf->b_orig_mode = mch_getperm(fname); | |
7316 #endif | |
7317 } | |
7318 | |
7319 /* | |
7320 * Adjust the line with missing eol, used for the next write. | |
7321 * Used for do_filter(), when the input lines for the filter are deleted. | |
7322 */ | |
7323 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7324 write_lnum_adjust(linenr_T offset) |
7 | 7325 { |
2707 | 7326 if (curbuf->b_no_eol_lnum != 0) /* only if there is a missing eol */ |
7327 curbuf->b_no_eol_lnum += offset; | |
7 | 7328 } |
7329 | |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7330 #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
|
7331 /* |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7332 * 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
|
7333 * 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
|
7334 */ |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7335 int |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7336 delete_recursive(char_u *name) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7337 { |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7338 int result = 0; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7339 char_u **files; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7340 int file_count; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7341 int i; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7342 char_u *exp; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7343 |
7629
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7344 /* 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
|
7345 * points to. */ |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7346 if ( |
7657
9c5e8254ea6b
commit https://github.com/vim/vim/commit/203258c3ad2966cc9d08b3805b103333988b30b7
Christian Brabandt <cb@256bit.org>
parents:
7641
diff
changeset
|
7347 # if defined(UNIX) || defined(WIN32) |
9c5e8254ea6b
commit https://github.com/vim/vim/commit/203258c3ad2966cc9d08b3805b103333988b30b7
Christian Brabandt <cb@256bit.org>
parents:
7641
diff
changeset
|
7348 mch_isrealdir(name) |
7629
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7349 # else |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7350 mch_isdir(name) |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7351 # endif |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7352 ) |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7353 { |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7354 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
|
7355 exp = vim_strsave(NameBuff); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7356 if (exp == NULL) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7357 return -1; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7358 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
|
7359 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
|
7360 { |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7361 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
|
7362 if (delete_recursive(files[i]) != 0) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7363 result = -1; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7364 FreeWild(file_count, files); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7365 } |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7366 else |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7367 result = -1; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7368 vim_free(exp); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7369 (void)mch_rmdir(name); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7370 } |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7371 else |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7372 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
|
7373 |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7374 return result; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7375 } |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7376 #endif |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7377 |
7 | 7378 #if defined(TEMPDIRNAMES) || defined(PROTO) |
7379 static long temp_count = 0; /* Temp filename counter. */ | |
7380 | |
7381 /* | |
7382 * Delete the temp directory and all files it contains. | |
7383 */ | |
7384 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7385 vim_deltempdir(void) |
7 | 7386 { |
7387 if (vim_tempdir != NULL) | |
7388 { | |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7389 /* remove the trailing path separator */ |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7390 gettail(vim_tempdir)[-1] = NUL; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7391 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
|
7392 VIM_CLEAR(vim_tempdir); |
7 | 7393 } |
7394 } | |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7395 |
7 | 7396 /* |
1997 | 7397 * Directory "tempdir" was created. Expand this name to a full path and put |
7398 * it in "vim_tempdir". This avoids that using ":cd" would confuse us. | |
7399 * "tempdir" must be no longer than MAXPATHL. | |
7400 */ | |
7401 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7402 vim_settempdir(char_u *tempdir) |
1997 | 7403 { |
7404 char_u *buf; | |
7405 | |
7406 buf = alloc((unsigned)MAXPATHL + 2); | |
7407 if (buf != NULL) | |
7408 { | |
7409 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL) | |
7410 STRCPY(buf, tempdir); | |
9536
b2aada04d84e
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Christian Brabandt <cb@256bit.org>
parents:
9509
diff
changeset
|
7411 add_pathsep(buf); |
1997 | 7412 vim_tempdir = vim_strsave(buf); |
7413 vim_free(buf); | |
7414 } | |
7415 } | |
2006 | 7416 #endif |
1997 | 7417 |
7418 /* | |
7 | 7419 * vim_tempname(): Return a unique name that can be used for a temp file. |
7420 * | |
9291
bff3cd5cf922
commit https://github.com/vim/vim/commit/76ae22fef3cb224ca7fbf97517f881e825d4d0c2
Christian Brabandt <cb@256bit.org>
parents:
9260
diff
changeset
|
7421 * 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
|
7422 * guaranteed to NOT be created. |
7 | 7423 * |
7424 * The returned pointer is to allocated memory. | |
7425 * The returned pointer is NULL if no valid name was found. | |
7426 */ | |
7427 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7428 vim_tempname( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7429 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
|
7430 int keep UNUSED) |
7 | 7431 { |
7432 #ifdef USE_TMPNAM | |
7433 char_u itmp[L_tmpnam]; /* use tmpnam() */ | |
7434 #else | |
7435 char_u itmp[TEMPNAMELEN]; | |
7436 #endif | |
7437 | |
7438 #ifdef TEMPDIRNAMES | |
7439 static char *(tempdirs[]) = {TEMPDIRNAMES}; | |
7440 int i; | |
7441 # ifndef EEXIST | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
7442 stat_T st; |
7 | 7443 # endif |
7444 | |
7445 /* | |
7446 * This will create a directory for private use by this instance of Vim. | |
7447 * This is done once, and the same directory is used for all temp files. | |
7448 * This method avoids security problems because of symlink attacks et al. | |
7449 * It's also a bit faster, because we only need to check for an existing | |
7450 * file when creating the directory and not for each temp file. | |
7451 */ | |
7452 if (vim_tempdir == NULL) | |
7453 { | |
7454 /* | |
7455 * Try the entries in TEMPDIRNAMES to create the temp directory. | |
7456 */ | |
1877 | 7457 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i) |
7 | 7458 { |
2047
85da03763130
updated for version 7.2.333
Bram Moolenaar <bram@zimbu.org>
parents:
2028
diff
changeset
|
7459 # ifndef HAVE_MKDTEMP |
1997 | 7460 size_t itmplen; |
7461 long nr; | |
7462 long off; | |
7463 # endif | |
7464 | |
7305
372c785c04b6
commit https://github.com/vim/vim/commit/e1a61991d9b6fd5f65636d17583f93118268cda5
Christian Brabandt <cb@256bit.org>
parents:
7285
diff
changeset
|
7465 /* 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
|
7466 * Skip the directory check if the expansion fails. */ |
7 | 7467 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
|
7468 if (itmp[0] != '$' && mch_isdir(itmp)) |
372c785c04b6
commit https://github.com/vim/vim/commit/e1a61991d9b6fd5f65636d17583f93118268cda5
Christian Brabandt <cb@256bit.org>
parents:
7285
diff
changeset
|
7469 { |
372c785c04b6
commit https://github.com/vim/vim/commit/e1a61991d9b6fd5f65636d17583f93118268cda5
Christian Brabandt <cb@256bit.org>
parents:
7285
diff
changeset
|
7470 /* directory exists */ |
9536
b2aada04d84e
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Christian Brabandt <cb@256bit.org>
parents:
9509
diff
changeset
|
7471 add_pathsep(itmp); |
1997 | 7472 |
7473 # ifdef HAVE_MKDTEMP | |
9211
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7474 { |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7475 # if defined(UNIX) || defined(VMS) |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7476 /* 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
|
7477 * "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
|
7478 mode_t umask_save = umask(077); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7479 # endif |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7480 /* Leave room for filename */ |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7481 STRCAT(itmp, "vXXXXXX"); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7482 if (mkdtemp((char *)itmp) != NULL) |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7483 vim_settempdir(itmp); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7484 # if defined(UNIX) || defined(VMS) |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7485 (void)umask(umask_save); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7486 # endif |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7487 } |
1997 | 7488 # else |
7 | 7489 /* Get an arbitrary number of up to 6 digits. When it's |
7490 * unlikely that it already exists it will be faster, | |
7491 * otherwise it doesn't matter. The use of mkdir() avoids any | |
7492 * security problems because of the predictable number. */ | |
7493 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
|
7494 itmplen = STRLEN(itmp); |
7 | 7495 |
7496 /* Try up to 10000 different values until we find a name that | |
7497 * doesn't exist. */ | |
7498 for (off = 0; off < 10000L; ++off) | |
7499 { | |
7500 int r; | |
1997 | 7501 # if defined(UNIX) || defined(VMS) |
7 | 7502 mode_t umask_save; |
1997 | 7503 # endif |
7504 | |
7505 sprintf((char *)itmp + itmplen, "v%ld", nr + off); | |
7506 # ifndef EEXIST | |
7 | 7507 /* If mkdir() does not set errno to EEXIST, check for |
7508 * existing file here. There is a race condition then, | |
7509 * although it's fail-safe. */ | |
7510 if (mch_stat((char *)itmp, &st) >= 0) | |
7511 continue; | |
1997 | 7512 # endif |
7513 # if defined(UNIX) || defined(VMS) | |
7 | 7514 /* Make sure the umask doesn't remove the executable bit. |
7515 * "repl" has been reported to use "177". */ | |
7516 umask_save = umask(077); | |
1997 | 7517 # endif |
7 | 7518 r = vim_mkdir(itmp, 0700); |
1997 | 7519 # if defined(UNIX) || defined(VMS) |
7 | 7520 (void)umask(umask_save); |
1997 | 7521 # endif |
7 | 7522 if (r == 0) |
7523 { | |
1997 | 7524 vim_settempdir(itmp); |
7 | 7525 break; |
7526 } | |
1997 | 7527 # ifdef EEXIST |
7 | 7528 /* If the mkdir() didn't fail because the file/dir exists, |
7529 * we probably can't create any dir here, try another | |
7530 * place. */ | |
7531 if (errno != EEXIST) | |
1997 | 7532 # endif |
7 | 7533 break; |
7534 } | |
1997 | 7535 # endif /* HAVE_MKDTEMP */ |
7 | 7536 if (vim_tempdir != NULL) |
7537 break; | |
7538 } | |
7539 } | |
7540 } | |
7541 | |
7542 if (vim_tempdir != NULL) | |
7543 { | |
7544 /* There is no need to check if the file exists, because we own the | |
7545 * directory and nobody else creates a file in it. */ | |
7546 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++); | |
7547 return vim_strsave(itmp); | |
7548 } | |
7549 | |
7550 return NULL; | |
7551 | |
7552 #else /* TEMPDIRNAMES */ | |
7553 | |
7554 # ifdef WIN3264 | |
7555 char szTempFile[_MAX_PATH + 1]; | |
7556 char buf4[4]; | |
7557 char_u *retval; | |
7558 char_u *p; | |
7559 | |
7560 STRCPY(itmp, ""); | |
7561 if (GetTempPath(_MAX_PATH, szTempFile) == 0) | |
2695 | 7562 { |
7563 szTempFile[0] = '.'; /* GetTempPath() failed, use current dir */ | |
7564 szTempFile[1] = NUL; | |
7565 } | |
7 | 7566 strcpy(buf4, "VIM"); |
7567 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
|
7568 if (GetTempFileName(szTempFile, buf4, 0, (LPSTR)itmp) == 0) |
7 | 7569 return NULL; |
6721 | 7570 if (!keep) |
7571 /* 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
|
7572 (void)DeleteFile((LPSTR)itmp); |
7 | 7573 |
7574 /* Backslashes in a temp file name cause problems when filtering with | |
7575 * "sh". NOTE: This also checks 'shellcmdflag' to help those people who | |
7576 * didn't set 'shellslash'. */ | |
7577 retval = vim_strsave(itmp); | |
7578 if (*p_shcf == '-' || p_ssl) | |
7579 for (p = retval; *p; ++p) | |
7580 if (*p == '\\') | |
7581 *p = '/'; | |
7582 return retval; | |
7583 | |
7584 # else /* WIN3264 */ | |
7585 | |
7586 # ifdef USE_TMPNAM | |
2697 | 7587 char_u *p; |
7588 | |
7 | 7589 /* tmpnam() will make its own name */ |
2697 | 7590 p = tmpnam((char *)itmp); |
7591 if (p == NULL || *p == NUL) | |
7 | 7592 return NULL; |
7593 # else | |
7594 char_u *p; | |
7595 | |
7596 # ifdef VMS_TEMPNAM | |
7597 /* mktemp() is not working on VMS. It seems to be | |
7598 * a do-nothing function. Therefore we use tempnam(). | |
7599 */ | |
7600 sprintf((char *)itmp, "VIM%c", extra_char); | |
7601 p = (char_u *)tempnam("tmp:", (char *)itmp); | |
7602 if (p != NULL) | |
7603 { | |
5704 | 7604 /* VMS will use '.LIS' if we don't explicitly specify an extension, |
7 | 7605 * and VIM will then be unable to find the file later */ |
7606 STRCPY(itmp, p); | |
7607 STRCAT(itmp, ".txt"); | |
7608 free(p); | |
7609 } | |
7610 else | |
7611 return NULL; | |
7612 # else | |
7613 STRCPY(itmp, TEMPNAME); | |
7614 if ((p = vim_strchr(itmp, '?')) != NULL) | |
7615 *p = extra_char; | |
7616 if (mktemp((char *)itmp) == NULL) | |
7617 return NULL; | |
7618 # endif | |
7619 # endif | |
7620 | |
7621 return vim_strsave(itmp); | |
7622 # endif /* WIN3264 */ | |
7623 #endif /* TEMPDIRNAMES */ | |
7624 } | |
7625 | |
7626 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) | |
7627 /* | |
7170
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
7628 * 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
|
7629 * it looks like a URL. |
7 | 7630 */ |
7631 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7632 forward_slash(char_u *fname) |
7 | 7633 { |
7634 char_u *p; | |
7635 | |
7170
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
7636 if (path_with_url(fname)) |
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
7637 return; |
7 | 7638 for (p = fname; *p != NUL; ++p) |
7639 # ifdef FEAT_MBYTE | |
7640 /* The Big5 encoding can have '\' in the trail byte. */ | |
474 | 7641 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 7642 ++p; |
7643 else | |
7644 # endif | |
7645 if (*p == '\\') | |
7646 *p = '/'; | |
7647 } | |
7648 #endif | |
7649 | |
7650 | |
7651 /* | |
7652 * Code for automatic commands. | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
7653 */ |
7 | 7654 |
7655 /* | |
7656 * The autocommands are stored in a list for each event. | |
7657 * Autocommands for the same pattern, that are consecutive, are joined | |
7658 * together, to avoid having to match the pattern too often. | |
7659 * The result is an array of Autopat lists, which point to AutoCmd lists: | |
7660 * | |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7661 * last_autopat[0] -----------------------------+ |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7662 * V |
7 | 7663 * first_autopat[0] --> Autopat.next --> Autopat.next --> NULL |
7664 * Autopat.cmds Autopat.cmds | |
7665 * | | | |
7666 * V V | |
7667 * AutoCmd.next AutoCmd.next | |
7668 * | | | |
7669 * V V | |
7670 * AutoCmd.next NULL | |
7671 * | | |
7672 * V | |
7673 * NULL | |
7674 * | |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7675 * last_autopat[1] --------+ |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7676 * V |
7 | 7677 * first_autopat[1] --> Autopat.next --> NULL |
7678 * Autopat.cmds | |
7679 * | | |
7680 * V | |
7681 * AutoCmd.next | |
7682 * | | |
7683 * V | |
7684 * NULL | |
7685 * etc. | |
7686 * | |
7687 * The order of AutoCmds is important, this is the order in which they were | |
7688 * defined and will have to be executed. | |
7689 */ | |
7690 typedef struct AutoCmd | |
7691 { | |
7692 char_u *cmd; /* The command to be executed (NULL | |
7693 when command has been removed) */ | |
7694 char nested; /* If autocommands nest here */ | |
7695 char last; /* last command in list */ | |
7696 #ifdef FEAT_EVAL | |
14700
0a3b9ecf7cb8
patch 8.1.0362: cannot get the script line number when executing a function
Christian Brabandt <cb@256bit.org>
parents:
14585
diff
changeset
|
7697 sctx_T script_ctx; /* script context where defined */ |
7 | 7698 #endif |
7699 struct AutoCmd *next; /* Next AutoCmd in list */ | |
7700 } AutoCmd; | |
7701 | |
7702 typedef struct AutoPat | |
7703 { | |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7704 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
|
7705 * be the first entry */ |
7 | 7706 char_u *pat; /* pattern as typed (NULL when pattern |
7707 has been removed) */ | |
4861
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7708 regprog_T *reg_prog; /* compiled regprog for pattern */ |
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7709 AutoCmd *cmds; /* list of commands to do */ |
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7710 int group; /* group ID */ |
7 | 7711 int patlen; /* strlen() of pat */ |
4861
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7712 int buflocal_nr; /* !=0 for buffer-local AutoPat */ |
7 | 7713 char allow_dirs; /* Pattern may match whole path */ |
7714 char last; /* last pattern for apply_autocmds() */ | |
7715 } AutoPat; | |
7716 | |
7717 static struct event_name | |
7718 { | |
7719 char *name; /* event name */ | |
661 | 7720 event_T event; /* event number */ |
7 | 7721 } event_names[] = |
7722 { | |
7723 {"BufAdd", EVENT_BUFADD}, | |
7724 {"BufCreate", EVENT_BUFADD}, | |
7725 {"BufDelete", EVENT_BUFDELETE}, | |
7726 {"BufEnter", EVENT_BUFENTER}, | |
7727 {"BufFilePost", EVENT_BUFFILEPOST}, | |
7728 {"BufFilePre", EVENT_BUFFILEPRE}, | |
7729 {"BufHidden", EVENT_BUFHIDDEN}, | |
7730 {"BufLeave", EVENT_BUFLEAVE}, | |
7731 {"BufNew", EVENT_BUFNEW}, | |
7732 {"BufNewFile", EVENT_BUFNEWFILE}, | |
7733 {"BufRead", EVENT_BUFREADPOST}, | |
7734 {"BufReadCmd", EVENT_BUFREADCMD}, | |
7735 {"BufReadPost", EVENT_BUFREADPOST}, | |
7736 {"BufReadPre", EVENT_BUFREADPRE}, | |
7737 {"BufUnload", EVENT_BUFUNLOAD}, | |
7738 {"BufWinEnter", EVENT_BUFWINENTER}, | |
7739 {"BufWinLeave", EVENT_BUFWINLEAVE}, | |
7740 {"BufWipeout", EVENT_BUFWIPEOUT}, | |
7741 {"BufWrite", EVENT_BUFWRITEPRE}, | |
7742 {"BufWritePost", EVENT_BUFWRITEPOST}, | |
7743 {"BufWritePre", EVENT_BUFWRITEPRE}, | |
7744 {"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
|
7745 {"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
|
7746 {"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
|
7747 {"CmdlineLeave", EVENT_CMDLINELEAVE}, |
7 | 7748 {"CmdwinEnter", EVENT_CMDWINENTER}, |
7749 {"CmdwinLeave", EVENT_CMDWINLEAVE}, | |
6154 | 7750 {"CmdUndefined", EVENT_CMDUNDEFINED}, |
12 | 7751 {"ColorScheme", EVENT_COLORSCHEME}, |
13810
cad480bac9e1
patch 8.0.1777: cannot cleanup before loading another colorscheme
Christian Brabandt <cb@256bit.org>
parents:
13804
diff
changeset
|
7752 {"ColorSchemePre", EVENT_COLORSCHEMEPRE}, |
3676 | 7753 {"CompleteDone", EVENT_COMPLETEDONE}, |
661 | 7754 {"CursorHold", EVENT_CURSORHOLD}, |
7755 {"CursorHoldI", EVENT_CURSORHOLDI}, | |
7756 {"CursorMoved", EVENT_CURSORMOVED}, | |
7757 {"CursorMovedI", EVENT_CURSORMOVEDI}, | |
14770
27055ad9276b
patch 8.1.0397: no event triggered after updating diffs
Christian Brabandt <cb@256bit.org>
parents:
14730
diff
changeset
|
7758 {"DiffUpdated", EVENT_DIFFUPDATED}, |
13170
6559e98f3e74
patch 8.0.1459: cannot handle change of directory
Christian Brabandt <cb@256bit.org>
parents:
13142
diff
changeset
|
7759 {"DirChanged", EVENT_DIRCHANGED}, |
7 | 7760 {"EncodingChanged", EVENT_ENCODINGCHANGED}, |
13442
94e638936d3e
patch 8.0.1595: no autocommand triggered before exiting
Christian Brabandt <cb@256bit.org>
parents:
13380
diff
changeset
|
7761 {"ExitPre", EVENT_EXITPRE}, |
7 | 7762 {"FileEncoding", EVENT_ENCODINGCHANGED}, |
7763 {"FileAppendPost", EVENT_FILEAPPENDPOST}, | |
7764 {"FileAppendPre", EVENT_FILEAPPENDPRE}, | |
7765 {"FileAppendCmd", EVENT_FILEAPPENDCMD}, | |
7766 {"FileChangedShell",EVENT_FILECHANGEDSHELL}, | |
765 | 7767 {"FileChangedShellPost",EVENT_FILECHANGEDSHELLPOST}, |
7 | 7768 {"FileChangedRO", EVENT_FILECHANGEDRO}, |
7769 {"FileReadPost", EVENT_FILEREADPOST}, | |
7770 {"FileReadPre", EVENT_FILEREADPRE}, | |
7771 {"FileReadCmd", EVENT_FILEREADCMD}, | |
7772 {"FileType", EVENT_FILETYPE}, | |
7773 {"FileWritePost", EVENT_FILEWRITEPOST}, | |
7774 {"FileWritePre", EVENT_FILEWRITEPRE}, | |
7775 {"FileWriteCmd", EVENT_FILEWRITECMD}, | |
7776 {"FilterReadPost", EVENT_FILTERREADPOST}, | |
7777 {"FilterReadPre", EVENT_FILTERREADPRE}, | |
7778 {"FilterWritePost", EVENT_FILTERWRITEPOST}, | |
7779 {"FilterWritePre", EVENT_FILTERWRITEPRE}, | |
7780 {"FocusGained", EVENT_FOCUSGAINED}, | |
7781 {"FocusLost", EVENT_FOCUSLOST}, | |
7782 {"FuncUndefined", EVENT_FUNCUNDEFINED}, | |
7783 {"GUIEnter", EVENT_GUIENTER}, | |
946 | 7784 {"GUIFailed", EVENT_GUIFAILED}, |
11 | 7785 {"InsertChange", EVENT_INSERTCHANGE}, |
7786 {"InsertEnter", EVENT_INSERTENTER}, | |
7787 {"InsertLeave", EVENT_INSERTLEAVE}, | |
2845 | 7788 {"InsertCharPre", EVENT_INSERTCHARPRE}, |
434 | 7789 {"MenuPopup", EVENT_MENUPOPUP}, |
6935 | 7790 {"OptionSet", EVENT_OPTIONSET}, |
161 | 7791 {"QuickFixCmdPost", EVENT_QUICKFIXCMDPOST}, |
7792 {"QuickFixCmdPre", EVENT_QUICKFIXCMDPRE}, | |
3568 | 7793 {"QuitPre", EVENT_QUITPRE}, |
7 | 7794 {"RemoteReply", EVENT_REMOTEREPLY}, |
574 | 7795 {"SessionLoadPost", EVENT_SESSIONLOADPOST}, |
723 | 7796 {"ShellCmdPost", EVENT_SHELLCMDPOST}, |
7797 {"ShellFilterPost", EVENT_SHELLFILTERPOST}, | |
715 | 7798 {"SourcePre", EVENT_SOURCEPRE}, |
1061 | 7799 {"SourceCmd", EVENT_SOURCECMD}, |
649 | 7800 {"SpellFileMissing",EVENT_SPELLFILEMISSING}, |
7 | 7801 {"StdinReadPost", EVENT_STDINREADPOST}, |
7802 {"StdinReadPre", EVENT_STDINREADPRE}, | |
674 | 7803 {"SwapExists", EVENT_SWAPEXISTS}, |
7 | 7804 {"Syntax", EVENT_SYNTAX}, |
9595
0190d5de215f
commit https://github.com/vim/vim/commit/c917da4b3e8801a255dbefea8e4ed19c1c716dd8
Christian Brabandt <cb@256bit.org>
parents:
9536
diff
changeset
|
7805 {"TabNew", EVENT_TABNEW}, |
9599
42a8a81decdf
commit https://github.com/vim/vim/commit/12c11d553053f5a9eae9eb3c518279b12fa928c2
Christian Brabandt <cb@256bit.org>
parents:
9595
diff
changeset
|
7806 {"TabClosed", EVENT_TABCLOSED}, |
676 | 7807 {"TabEnter", EVENT_TABENTER}, |
7808 {"TabLeave", EVENT_TABLEAVE}, | |
7 | 7809 {"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
|
7810 {"TerminalOpen", EVENT_TERMINALOPEN}, |
7 | 7811 {"TermResponse", EVENT_TERMRESPONSE}, |
4232 | 7812 {"TextChanged", EVENT_TEXTCHANGED}, |
7813 {"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
|
7814 {"TextChangedP", EVENT_TEXTCHANGEDP}, |
7 | 7815 {"User", EVENT_USER}, |
7816 {"VimEnter", EVENT_VIMENTER}, | |
7817 {"VimLeave", EVENT_VIMLEAVE}, | |
7818 {"VimLeavePre", EVENT_VIMLEAVEPRE}, | |
9595
0190d5de215f
commit https://github.com/vim/vim/commit/c917da4b3e8801a255dbefea8e4ed19c1c716dd8
Christian Brabandt <cb@256bit.org>
parents:
9536
diff
changeset
|
7819 {"WinNew", EVENT_WINNEW}, |
7 | 7820 {"WinEnter", EVENT_WINENTER}, |
7821 {"WinLeave", EVENT_WINLEAVE}, | |
765 | 7822 {"VimResized", EVENT_VIMRESIZED}, |
13037
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
7823 {"TextYankPost", EVENT_TEXTYANKPOST}, |
661 | 7824 {NULL, (event_T)0} |
7 | 7825 }; |
7826 | |
7827 static AutoPat *first_autopat[NUM_EVENTS] = | |
7828 { | |
7829 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7830 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7831 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7832 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
18 | 7833 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
6935 | 7834 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
7 | 7835 }; |
7836 | |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7837 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
|
7838 { |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7839 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
|
7840 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
|
7841 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
|
7842 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
|
7843 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
|
7844 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
|
7845 }; |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7846 |
7 | 7847 /* |
7848 * struct used to keep status while executing autocommands for an event. | |
7849 */ | |
7850 typedef struct AutoPatCmd | |
7851 { | |
7852 AutoPat *curpat; /* next AutoPat to examine */ | |
7853 AutoCmd *nextcmd; /* next AutoCmd to execute */ | |
7854 int group; /* group being used */ | |
7855 char_u *fname; /* fname to match with */ | |
7856 char_u *sfname; /* sfname to match with */ | |
7857 char_u *tail; /* tail of fname */ | |
661 | 7858 event_T event; /* current event */ |
40 | 7859 int arg_bufnr; /* initially equal to <abuf>, set to zero when |
7860 buf is deleted */ | |
7861 struct AutoPatCmd *next; /* chain of active apc-s for auto-invalidation*/ | |
7 | 7862 } AutoPatCmd; |
7863 | |
297 | 7864 static AutoPatCmd *active_apc_list = NULL; /* stack of active autocommands */ |
40 | 7865 |
7 | 7866 /* |
7867 * augroups stores a list of autocmd group names. | |
7868 */ | |
297 | 7869 static garray_T augroups = {0, 0, sizeof(char_u *), 10, NULL}; |
7 | 7870 #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
|
7871 /* 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
|
7872 static char_u *deleted_augroup = NULL; |
7 | 7873 |
7874 /* | |
7875 * The ID of the current group. Group 0 is the default one. | |
7876 */ | |
7877 static int current_augroup = AUGROUP_DEFAULT; | |
7878 | |
7879 static int au_need_clean = FALSE; /* need to delete marked patterns */ | |
7880 | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7881 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
|
7882 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
|
7883 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
|
7884 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
|
7885 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
|
7886 static int match_file_pat(char_u *pattern, regprog_T **prog, char_u *fname, char_u *sfname, char_u *tail, int allow_dirs); |
7 | 7887 |
40 | 7888 |
661 | 7889 static event_T last_event; |
7 | 7890 static int last_group; |
1410 | 7891 static int autocmd_blocked = 0; /* block all autocmds */ |
7 | 7892 |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7893 static char_u * |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7894 get_deleted_augroup(void) |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7895 { |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7896 if (deleted_augroup == NULL) |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7897 deleted_augroup = (char_u *)_("--Deleted--"); |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7898 return deleted_augroup; |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7899 } |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7900 |
7 | 7901 /* |
7902 * Show the autocommands for one AutoPat. | |
7903 */ | |
7904 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7905 show_autocmd(AutoPat *ap, event_T event) |
7 | 7906 { |
7907 AutoCmd *ac; | |
7908 | |
7909 /* Check for "got_int" (here and at various places below), which is set | |
7910 * when "q" has been hit for the "--more--" prompt */ | |
7911 if (got_int) | |
7912 return; | |
7913 if (ap->pat == NULL) /* pattern has been removed */ | |
7914 return; | |
7915 | |
7916 msg_putchar('\n'); | |
7917 if (got_int) | |
7918 return; | |
7919 if (event != last_event || ap->group != last_group) | |
7920 { | |
7921 if (ap->group != AUGROUP_DEFAULT) | |
7922 { | |
7923 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
|
7924 msg_puts_attr(get_deleted_augroup(), HL_ATTR(HLF_E)); |
7 | 7925 else |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
7926 msg_puts_attr(AUGROUP_NAME(ap->group), HL_ATTR(HLF_T)); |
7 | 7927 msg_puts((char_u *)" "); |
7928 } | |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
7929 msg_puts_attr(event_nr2name(event), HL_ATTR(HLF_T)); |
7 | 7930 last_event = event; |
7931 last_group = ap->group; | |
7932 msg_putchar('\n'); | |
7933 if (got_int) | |
7934 return; | |
7935 } | |
7936 msg_col = 4; | |
7937 msg_outtrans(ap->pat); | |
7938 | |
7939 for (ac = ap->cmds; ac != NULL; ac = ac->next) | |
7940 { | |
7941 if (ac->cmd != NULL) /* skip removed commands */ | |
7942 { | |
7943 if (msg_col >= 14) | |
7944 msg_putchar('\n'); | |
7945 msg_col = 14; | |
7946 if (got_int) | |
7947 return; | |
7948 msg_outtrans(ac->cmd); | |
500 | 7949 #ifdef FEAT_EVAL |
7950 if (p_verbose > 0) | |
14700
0a3b9ecf7cb8
patch 8.1.0362: cannot get the script line number when executing a function
Christian Brabandt <cb@256bit.org>
parents:
14585
diff
changeset
|
7951 last_set_msg(ac->script_ctx); |
500 | 7952 #endif |
7 | 7953 if (got_int) |
7954 return; | |
7955 if (ac->next != NULL) | |
7956 { | |
7957 msg_putchar('\n'); | |
7958 if (got_int) | |
7959 return; | |
7960 } | |
7961 } | |
7962 } | |
7963 } | |
7964 | |
7965 /* | |
7966 * Mark an autocommand pattern for deletion. | |
7967 */ | |
7968 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7969 au_remove_pat(AutoPat *ap) |
7 | 7970 { |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
7971 VIM_CLEAR(ap->pat); |
40 | 7972 ap->buflocal_nr = -1; |
7 | 7973 au_need_clean = TRUE; |
7974 } | |
7975 | |
7976 /* | |
7977 * Mark all commands for a pattern for deletion. | |
7978 */ | |
7979 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7980 au_remove_cmds(AutoPat *ap) |
7 | 7981 { |
7982 AutoCmd *ac; | |
7983 | |
7984 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
|
7985 VIM_CLEAR(ac->cmd); |
7 | 7986 au_need_clean = TRUE; |
7987 } | |
7988 | |
7989 /* | |
7990 * Cleanup autocommands and patterns that have been deleted. | |
7991 * This is only done when not executing autocommands. | |
7992 */ | |
7993 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7994 au_cleanup(void) |
7 | 7995 { |
7996 AutoPat *ap, **prev_ap; | |
7997 AutoCmd *ac, **prev_ac; | |
661 | 7998 event_T event; |
7 | 7999 |
8000 if (autocmd_busy || !au_need_clean) | |
8001 return; | |
8002 | |
8003 /* loop over all events */ | |
661 | 8004 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
8005 event = (event_T)((int)event + 1)) | |
7 | 8006 { |
8007 /* loop over all autocommand patterns */ | |
8008 prev_ap = &(first_autopat[(int)event]); | |
8009 for (ap = *prev_ap; ap != NULL; ap = *prev_ap) | |
8010 { | |
8011 /* loop over all commands for this pattern */ | |
8012 prev_ac = &(ap->cmds); | |
8013 for (ac = *prev_ac; ac != NULL; ac = *prev_ac) | |
8014 { | |
8015 /* remove the command if the pattern is to be deleted or when | |
8016 * the command has been marked for deletion */ | |
8017 if (ap->pat == NULL || ac->cmd == NULL) | |
8018 { | |
8019 *prev_ac = ac->next; | |
8020 vim_free(ac->cmd); | |
8021 vim_free(ac); | |
8022 } | |
8023 else | |
8024 prev_ac = &(ac->next); | |
8025 } | |
8026 | |
8027 /* remove the pattern if it has been marked for deletion */ | |
8028 if (ap->pat == NULL) | |
8029 { | |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8030 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
|
8031 { |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8032 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
|
8033 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
|
8034 else |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8035 /* 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
|
8036 * the struct */ |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8037 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
|
8038 } |
7 | 8039 *prev_ap = ap->next; |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4354
diff
changeset
|
8040 vim_regfree(ap->reg_prog); |
7 | 8041 vim_free(ap); |
8042 } | |
8043 else | |
8044 prev_ap = &(ap->next); | |
8045 } | |
8046 } | |
8047 | |
8048 au_need_clean = FALSE; | |
8049 } | |
8050 | |
8051 /* | |
40 | 8052 * Called when buffer is freed, to remove/invalidate related buffer-local |
8053 * autocmds. | |
8054 */ | |
8055 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8056 aubuflocal_remove(buf_T *buf) |
40 | 8057 { |
8058 AutoPat *ap; | |
661 | 8059 event_T event; |
40 | 8060 AutoPatCmd *apc; |
8061 | |
8062 /* invalidate currently executing autocommands */ | |
8063 for (apc = active_apc_list; apc; apc = apc->next) | |
8064 if (buf->b_fnum == apc->arg_bufnr) | |
8065 apc->arg_bufnr = 0; | |
8066 | |
8067 /* invalidate buflocals looping through events */ | |
661 | 8068 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
8069 event = (event_T)((int)event + 1)) | |
40 | 8070 /* loop over all autocommand patterns */ |
8071 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
8072 if (ap->buflocal_nr == buf->b_fnum) | |
8073 { | |
8074 au_remove_pat(ap); | |
8075 if (p_verbose >= 6) | |
292 | 8076 { |
8077 verbose_enter(); | |
40 | 8078 smsg((char_u *) |
8079 _("auto-removing autocommand: %s <buffer=%d>"), | |
8080 event_nr2name(event), buf->b_fnum); | |
292 | 8081 verbose_leave(); |
8082 } | |
40 | 8083 } |
8084 au_cleanup(); | |
8085 } | |
8086 | |
8087 /* | |
7 | 8088 * Add an autocmd group name. |
8089 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. | |
8090 */ | |
8091 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8092 au_new_group(char_u *name) |
7 | 8093 { |
8094 int i; | |
8095 | |
8096 i = au_find_group(name); | |
8097 if (i == AUGROUP_ERROR) /* the group doesn't exist yet, add it */ | |
8098 { | |
8099 /* First try using a free entry. */ | |
8100 for (i = 0; i < augroups.ga_len; ++i) | |
8101 if (AUGROUP_NAME(i) == NULL) | |
8102 break; | |
8103 if (i == augroups.ga_len && ga_grow(&augroups, 1) == FAIL) | |
8104 return AUGROUP_ERROR; | |
8105 | |
8106 AUGROUP_NAME(i) = vim_strsave(name); | |
8107 if (AUGROUP_NAME(i) == NULL) | |
8108 return AUGROUP_ERROR; | |
8109 if (i == augroups.ga_len) | |
8110 ++augroups.ga_len; | |
8111 } | |
8112 | |
8113 return i; | |
8114 } | |
8115 | |
8116 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8117 au_del_group(char_u *name) |
7 | 8118 { |
8119 int i; | |
8120 | |
8121 i = au_find_group(name); | |
8122 if (i == AUGROUP_ERROR) /* the group doesn't exist */ | |
8123 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
|
8124 else if (i == current_augroup) |
1de911ef1edf
commit https://github.com/vim/vim/commit/de653f08805dde14424d417502a0480a6ad292f8
Christian Brabandt <cb@256bit.org>
parents:
10084
diff
changeset
|
8125 EMSG(_("E936: Cannot delete the current group")); |
7 | 8126 else |
8127 { | |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8128 event_T event; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8129 AutoPat *ap; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8130 int in_use = FALSE; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8131 |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8132 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
|
8133 event = (event_T)((int)event + 1)) |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8134 { |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8135 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
|
8136 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
|
8137 { |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8138 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
|
8139 in_use = TRUE; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8140 event = NUM_EVENTS; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8141 break; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8142 } |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8143 } |
7 | 8144 vim_free(AUGROUP_NAME(i)); |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8145 if (in_use) |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8146 { |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
8147 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
|
8148 } |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8149 else |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8150 AUGROUP_NAME(i) = NULL; |
7 | 8151 } |
8152 } | |
8153 | |
8154 /* | |
8155 * Find the ID of an autocmd group name. | |
8156 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. | |
8157 */ | |
8158 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8159 au_find_group(char_u *name) |
7 | 8160 { |
8161 int i; | |
8162 | |
8163 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
|
8164 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
|
8165 && STRCMP(AUGROUP_NAME(i), name) == 0) |
7 | 8166 return i; |
8167 return AUGROUP_ERROR; | |
8168 } | |
8169 | |
461 | 8170 /* |
8171 * Return TRUE if augroup "name" exists. | |
8172 */ | |
8173 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8174 au_has_group(char_u *name) |
461 | 8175 { |
8176 return au_find_group(name) != AUGROUP_ERROR; | |
8177 } | |
8178 | |
7 | 8179 /* |
8180 * ":augroup {name}". | |
8181 */ | |
8182 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8183 do_augroup(char_u *arg, int del_group) |
7 | 8184 { |
8185 int i; | |
8186 | |
8187 if (del_group) | |
8188 { | |
8189 if (*arg == NUL) | |
8190 EMSG(_(e_argreq)); | |
8191 else | |
8192 au_del_group(arg); | |
8193 } | |
8194 else if (STRICMP(arg, "end") == 0) /* ":aug end": back to group 0 */ | |
8195 current_augroup = AUGROUP_DEFAULT; | |
8196 else if (*arg) /* ":aug xxx": switch to group xxx */ | |
8197 { | |
8198 i = au_new_group(arg); | |
8199 if (i != AUGROUP_ERROR) | |
8200 current_augroup = i; | |
8201 } | |
8202 else /* ":aug": list the group names */ | |
8203 { | |
8204 msg_start(); | |
8205 for (i = 0; i < augroups.ga_len; ++i) | |
8206 { | |
8207 if (AUGROUP_NAME(i) != NULL) | |
8208 { | |
8209 msg_puts(AUGROUP_NAME(i)); | |
8210 msg_puts((char_u *)" "); | |
8211 } | |
8212 } | |
8213 msg_clr_eos(); | |
8214 msg_end(); | |
8215 } | |
8216 } | |
8217 | |
355 | 8218 #if defined(EXITFREE) || defined(PROTO) |
8219 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8220 free_all_autocmds(void) |
355 | 8221 { |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8222 int i; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8223 char_u *s; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8224 |
355 | 8225 for (current_augroup = -1; current_augroup < augroups.ga_len; |
8226 ++current_augroup) | |
8227 do_autocmd((char_u *)"", TRUE); | |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8228 |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8229 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
|
8230 { |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8231 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
|
8232 if (s != get_deleted_augroup()) |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8233 vim_free(s); |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8234 } |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8235 ga_clear(&augroups); |
355 | 8236 } |
8237 #endif | |
8238 | |
7 | 8239 /* |
8240 * Return the event number for event name "start". | |
8241 * Return NUM_EVENTS if the event name was not found. | |
8242 * Return a pointer to the next event name in "end". | |
8243 */ | |
661 | 8244 static event_T |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8245 event_name2nr(char_u *start, char_u **end) |
7 | 8246 { |
8247 char_u *p; | |
8248 int i; | |
8249 int len; | |
8250 | |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8251 /* 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
|
8252 for (p = start; *p && !VIM_ISWHITE(*p) && *p != ',' && *p != '|'; ++p) |
7 | 8253 ; |
8254 for (i = 0; event_names[i].name != NULL; ++i) | |
8255 { | |
8256 len = (int)STRLEN(event_names[i].name); | |
8257 if (len == p - start && STRNICMP(event_names[i].name, start, len) == 0) | |
8258 break; | |
8259 } | |
8260 if (*p == ',') | |
8261 ++p; | |
8262 *end = p; | |
8263 if (event_names[i].name == NULL) | |
8264 return NUM_EVENTS; | |
8265 return event_names[i].event; | |
8266 } | |
8267 | |
8268 /* | |
8269 * Return the name for event "event". | |
8270 */ | |
8271 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8272 event_nr2name(event_T event) |
7 | 8273 { |
8274 int i; | |
8275 | |
8276 for (i = 0; event_names[i].name != NULL; ++i) | |
8277 if (event_names[i].event == event) | |
8278 return (char_u *)event_names[i].name; | |
8279 return (char_u *)"Unknown"; | |
8280 } | |
8281 | |
8282 /* | |
8283 * Scan over the events. "*" stands for all events. | |
8284 */ | |
8285 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8286 find_end_event( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8287 char_u *arg, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8288 int have_group) /* TRUE when group name was found */ |
7 | 8289 { |
8290 char_u *pat; | |
8291 char_u *p; | |
8292 | |
8293 if (*arg == '*') | |
8294 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8295 if (arg[1] && !VIM_ISWHITE(arg[1])) |
7 | 8296 { |
8297 EMSG2(_("E215: Illegal character after *: %s"), arg); | |
8298 return NULL; | |
8299 } | |
8300 pat = arg + 1; | |
8301 } | |
8302 else | |
8303 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8304 for (pat = arg; *pat && *pat != '|' && !VIM_ISWHITE(*pat); pat = p) |
7 | 8305 { |
8306 if ((int)event_name2nr(pat, &p) >= (int)NUM_EVENTS) | |
8307 { | |
8308 if (have_group) | |
8309 EMSG2(_("E216: No such event: %s"), pat); | |
8310 else | |
8311 EMSG2(_("E216: No such group or event: %s"), pat); | |
8312 return NULL; | |
8313 } | |
8314 } | |
8315 } | |
8316 return pat; | |
8317 } | |
8318 | |
8319 /* | |
8320 * Return TRUE if "event" is included in 'eventignore'. | |
8321 */ | |
8322 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8323 event_ignored(event_T event) |
7 | 8324 { |
8325 char_u *p = p_ei; | |
8326 | |
844 | 8327 while (*p != NUL) |
8328 { | |
8329 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) | |
8330 return TRUE; | |
7 | 8331 if (event_name2nr(p, &p) == event) |
8332 return TRUE; | |
844 | 8333 } |
7 | 8334 |
8335 return FALSE; | |
8336 } | |
8337 | |
8338 /* | |
8339 * Return OK when the contents of p_ei is valid, FAIL otherwise. | |
8340 */ | |
8341 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8342 check_ei(void) |
7 | 8343 { |
8344 char_u *p = p_ei; | |
8345 | |
8346 while (*p) | |
844 | 8347 { |
8348 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) | |
8349 { | |
8350 p += 3; | |
8351 if (*p == ',') | |
8352 ++p; | |
8353 } | |
8354 else if (event_name2nr(p, &p) == NUM_EVENTS) | |
7 | 8355 return FAIL; |
844 | 8356 } |
7 | 8357 |
8358 return OK; | |
8359 } | |
8360 | |
123 | 8361 # if defined(FEAT_SYN_HL) || defined(PROTO) |
8362 | |
8363 /* | |
8364 * Add "what" to 'eventignore' to skip loading syntax highlighting for every | |
8365 * buffer loaded into the window. "what" must start with a comma. | |
8366 * Returns the old value of 'eventignore' in allocated memory. | |
8367 */ | |
8368 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8369 au_event_disable(char *what) |
123 | 8370 { |
8371 char_u *new_ei; | |
8372 char_u *save_ei; | |
8373 | |
8374 save_ei = vim_strsave(p_ei); | |
8375 if (save_ei != NULL) | |
8376 { | |
557 | 8377 new_ei = vim_strnsave(p_ei, (int)(STRLEN(p_ei) + STRLEN(what))); |
123 | 8378 if (new_ei != NULL) |
8379 { | |
2095
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8380 if (*what == ',' && *p_ei == NUL) |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8381 STRCPY(new_ei, what + 1); |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8382 else |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8383 STRCAT(new_ei, what); |
694 | 8384 set_string_option_direct((char_u *)"ei", -1, new_ei, |
8385 OPT_FREE, SID_NONE); | |
123 | 8386 vim_free(new_ei); |
8387 } | |
8388 } | |
8389 return save_ei; | |
8390 } | |
8391 | |
8392 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8393 au_event_restore(char_u *old_ei) |
123 | 8394 { |
8395 if (old_ei != NULL) | |
8396 { | |
694 | 8397 set_string_option_direct((char_u *)"ei", -1, old_ei, |
8398 OPT_FREE, SID_NONE); | |
123 | 8399 vim_free(old_ei); |
8400 } | |
8401 } | |
8402 # endif /* FEAT_SYN_HL */ | |
8403 | |
7 | 8404 /* |
8405 * do_autocmd() -- implements the :autocmd command. Can be used in the | |
8406 * following ways: | |
8407 * | |
8408 * :autocmd <event> <pat> <cmd> Add <cmd> to the list of commands that | |
8409 * will be automatically executed for <event> | |
8410 * when editing a file matching <pat>, in | |
8411 * the current group. | |
14157
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
8412 * :autocmd <event> <pat> Show the autocommands associated with |
7 | 8413 * <event> and <pat>. |
14157
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
8414 * :autocmd <event> Show the autocommands associated with |
7 | 8415 * <event>. |
14157
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
8416 * :autocmd Show all autocommands. |
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
8417 * :autocmd! <event> <pat> <cmd> Remove all autocommands associated with |
7 | 8418 * <event> and <pat>, and add the command |
8419 * <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
|
8420 * :autocmd! <event> <pat> Remove all autocommands associated with |
7 | 8421 * <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
|
8422 * :autocmd! <event> Remove all autocommands associated with |
7 | 8423 * <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
|
8424 * :autocmd! Remove ALL autocommands for the current |
7 | 8425 * group. |
8426 * | |
8427 * Multiple events and patterns may be given separated by commas. Here are | |
8428 * some examples: | |
8429 * :autocmd bufread,bufenter *.c,*.h set tw=0 smartindent noic | |
8430 * :autocmd bufleave * set tw=79 nosmartindent ic infercase | |
8431 * | |
8432 * :autocmd * *.c show all autocommands for *.c files. | |
609 | 8433 * |
8434 * Mostly a {group} argument can optionally appear before <event>. | |
7 | 8435 */ |
8436 void | |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8437 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
|
8438 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8439 char_u *arg = arg_in; |
7 | 8440 char_u *pat; |
8441 char_u *envpat = NULL; | |
8442 char_u *cmd; | |
661 | 8443 event_T event; |
7 | 8444 int need_free = FALSE; |
8445 int nested = FALSE; | |
8446 int group; | |
8447 | |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8448 if (*arg == '|') |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8449 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8450 arg = (char_u *)""; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8451 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
|
8452 } |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8453 else |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8454 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8455 /* |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8456 * 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
|
8457 */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8458 group = au_get_grouparg(&arg); |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8459 if (arg == NULL) /* out of memory */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8460 return; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8461 } |
7 | 8462 |
8463 /* | |
8464 * Scan over the events. | |
8465 * If we find an illegal name, return here, don't do anything. | |
8466 */ | |
8467 pat = find_end_event(arg, group != AUGROUP_ALL); | |
8468 if (pat == NULL) | |
8469 return; | |
8470 | |
8471 pat = skipwhite(pat); | |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8472 if (*pat == '|') |
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 pat = (char_u *)""; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8475 cmd = (char_u *)""; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8476 } |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8477 else |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8478 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8479 /* |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8480 * 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
|
8481 */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8482 cmd = pat; |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8483 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
|
8484 cmd++; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8485 if (*cmd) |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8486 *cmd++ = NUL; |
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 /* 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
|
8489 * forward slashes here. */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8490 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
|
8491 { |
7 | 8492 #ifdef BACKSLASH_IN_FILENAME |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8493 int p_ssl_save = p_ssl; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8494 |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8495 p_ssl = TRUE; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8496 #endif |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8497 envpat = expand_env_save(pat); |
7 | 8498 #ifdef BACKSLASH_IN_FILENAME |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8499 p_ssl = p_ssl_save; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8500 #endif |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8501 if (envpat != NULL) |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8502 pat = envpat; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8503 } |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8504 |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8505 /* |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8506 * Check for "nested" flag. |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8507 */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8508 cmd = skipwhite(cmd); |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8509 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
|
8510 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8511 nested = TRUE; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8512 cmd = skipwhite(cmd + 6); |
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 /* |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8516 * Find the start of the commands. |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8517 * Expand <sfile> in it. |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8518 */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8519 if (*cmd != NUL) |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8520 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8521 cmd = expand_sfile(cmd); |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8522 if (cmd == NULL) /* some error */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8523 return; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8524 need_free = TRUE; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8525 } |
7 | 8526 } |
8527 | |
8528 /* | |
8529 * Print header when showing autocommands. | |
8530 */ | |
8531 if (!forceit && *cmd == NUL) | |
8532 { | |
8533 /* Highlight title */ | |
14157
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
8534 MSG_PUTS_TITLE(_("\n--- Autocommands ---")); |
7 | 8535 } |
8536 | |
8537 /* | |
8538 * Loop over the events. | |
8539 */ | |
661 | 8540 last_event = (event_T)-1; /* for listing the event name */ |
7 | 8541 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
|
8542 if (*arg == '*' || *arg == NUL || *arg == '|') |
7 | 8543 { |
661 | 8544 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
8545 event = (event_T)((int)event + 1)) | |
7 | 8546 if (do_autocmd_event(event, pat, |
8547 nested, cmd, forceit, group) == FAIL) | |
8548 break; | |
8549 } | |
8550 else | |
8551 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8552 while (*arg && *arg != '|' && !VIM_ISWHITE(*arg)) |
7 | 8553 if (do_autocmd_event(event_name2nr(arg, &arg), pat, |
8554 nested, cmd, forceit, group) == FAIL) | |
8555 break; | |
8556 } | |
8557 | |
8558 if (need_free) | |
8559 vim_free(cmd); | |
8560 vim_free(envpat); | |
8561 } | |
8562 | |
8563 /* | |
8564 * Find the group ID in a ":autocmd" or ":doautocmd" argument. | |
8565 * The "argp" argument is advanced to the following argument. | |
8566 * | |
8567 * Returns the group ID, AUGROUP_ERROR for error (out of memory). | |
8568 */ | |
8569 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8570 au_get_grouparg(char_u **argp) |
7 | 8571 { |
8572 char_u *group_name; | |
8573 char_u *p; | |
8574 char_u *arg = *argp; | |
8575 int group = AUGROUP_ALL; | |
8576 | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8577 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
|
8578 ; |
7 | 8579 if (p > arg) |
8580 { | |
8581 group_name = vim_strnsave(arg, (int)(p - arg)); | |
8582 if (group_name == NULL) /* out of memory */ | |
8583 return AUGROUP_ERROR; | |
8584 group = au_find_group(group_name); | |
8585 if (group == AUGROUP_ERROR) | |
8586 group = AUGROUP_ALL; /* no match, use all groups */ | |
8587 else | |
8588 *argp = skipwhite(p); /* match, skip over group name */ | |
8589 vim_free(group_name); | |
8590 } | |
8591 return group; | |
8592 } | |
8593 | |
8594 /* | |
8595 * do_autocmd() for one event. | |
8596 * If *pat == NUL do for all patterns. | |
8597 * If *cmd == NUL show entries. | |
8598 * If forceit == TRUE delete entries. | |
8599 * If group is not AUGROUP_ALL, only use this group. | |
8600 */ | |
8601 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8602 do_autocmd_event( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8603 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8604 char_u *pat, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8605 int nested, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8606 char_u *cmd, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8607 int forceit, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8608 int group) |
7 | 8609 { |
8610 AutoPat *ap; | |
8611 AutoPat **prev_ap; | |
8612 AutoCmd *ac; | |
8613 AutoCmd **prev_ac; | |
8614 int brace_level; | |
8615 char_u *endpat; | |
8616 int findgroup; | |
8617 int allgroups; | |
8618 int patlen; | |
856 | 8619 int is_buflocal; |
40 | 8620 int buflocal_nr; |
8621 char_u buflocal_pat[25]; /* for "<buffer=X>" */ | |
7 | 8622 |
8623 if (group == AUGROUP_ALL) | |
8624 findgroup = current_augroup; | |
8625 else | |
8626 findgroup = group; | |
8627 allgroups = (group == AUGROUP_ALL && !forceit && *cmd == NUL); | |
8628 | |
8629 /* | |
8630 * Show or delete all patterns for an event. | |
8631 */ | |
8632 if (*pat == NUL) | |
8633 { | |
8634 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
8635 { | |
8636 if (forceit) /* delete the AutoPat, if it's in the current group */ | |
8637 { | |
8638 if (ap->group == findgroup) | |
8639 au_remove_pat(ap); | |
8640 } | |
8641 else if (group == AUGROUP_ALL || ap->group == group) | |
8642 show_autocmd(ap, event); | |
8643 } | |
8644 } | |
8645 | |
8646 /* | |
8647 * Loop through all the specified patterns. | |
8648 */ | |
8649 for ( ; *pat; pat = (*endpat == ',' ? endpat + 1 : endpat)) | |
8650 { | |
8651 /* | |
8652 * Find end of the pattern. | |
8653 * Watch out for a comma in braces, like "*.\{obj,o\}". | |
8654 */ | |
8655 brace_level = 0; | |
8656 for (endpat = pat; *endpat && (*endpat != ',' || brace_level | |
6963 | 8657 || (endpat > pat && endpat[-1] == '\\')); ++endpat) |
7 | 8658 { |
8659 if (*endpat == '{') | |
8660 brace_level++; | |
8661 else if (*endpat == '}') | |
8662 brace_level--; | |
8663 } | |
8664 if (pat == endpat) /* ignore single comma */ | |
8665 continue; | |
8666 patlen = (int)(endpat - pat); | |
8667 | |
8668 /* | |
40 | 8669 * detect special <buflocal[=X]> buffer-local patterns |
8670 */ | |
8671 is_buflocal = FALSE; | |
8672 buflocal_nr = 0; | |
8673 | |
6623 | 8674 if (patlen >= 8 && STRNCMP(pat, "<buffer", 7) == 0 |
40 | 8675 && pat[patlen - 1] == '>') |
8676 { | |
6623 | 8677 /* "<buffer...>": Error will be printed only for addition. |
8678 * printing and removing will proceed silently. */ | |
40 | 8679 is_buflocal = TRUE; |
8680 if (patlen == 8) | |
6623 | 8681 /* "<buffer>" */ |
40 | 8682 buflocal_nr = curbuf->b_fnum; |
8683 else if (patlen > 9 && pat[7] == '=') | |
8684 { | |
6623 | 8685 if (patlen == 13 && STRNICMP(pat, "<buffer=abuf>", 13) == 0) |
8686 /* "<buffer=abuf>" */ | |
40 | 8687 buflocal_nr = autocmd_bufnr; |
8688 else if (skipdigits(pat + 8) == pat + patlen - 1) | |
6623 | 8689 /* "<buffer=123>" */ |
40 | 8690 buflocal_nr = atoi((char *)pat + 8); |
8691 } | |
8692 } | |
8693 | |
8694 if (is_buflocal) | |
8695 { | |
8696 /* normalize pat into standard "<buffer>#N" form */ | |
8697 sprintf((char *)buflocal_pat, "<buffer=%d>", buflocal_nr); | |
8698 pat = buflocal_pat; /* can modify pat and patlen */ | |
835 | 8699 patlen = (int)STRLEN(buflocal_pat); /* but not endpat */ |
40 | 8700 } |
8701 | |
8702 /* | |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8703 * 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
|
8704 * always goes at or after the last one, so start at the end. |
7 | 8705 */ |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8706 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
|
8707 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
|
8708 else |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8709 prev_ap = &first_autopat[(int)event]; |
7 | 8710 while ((ap = *prev_ap) != NULL) |
8711 { | |
8712 if (ap->pat != NULL) | |
8713 { | |
8714 /* Accept a pattern when: | |
8715 * - a group was specified and it's that group, or a group was | |
8716 * not specified and it's the current group, or a group was | |
8717 * not specified and we are listing | |
8718 * - the length of the pattern matches | |
40 | 8719 * - the pattern matches. |
8720 * For <buffer[=X]>, this condition works because we normalize | |
8721 * all buffer-local patterns. | |
7 | 8722 */ |
8723 if ((allgroups || ap->group == findgroup) | |
8724 && ap->patlen == patlen | |
8725 && STRNCMP(pat, ap->pat, patlen) == 0) | |
8726 { | |
8727 /* | |
8728 * Remove existing autocommands. | |
8729 * If adding any new autocmd's for this AutoPat, don't | |
8730 * delete the pattern from the autopat list, append to | |
8731 * this list. | |
8732 */ | |
8733 if (forceit) | |
8734 { | |
8735 if (*cmd != NUL && ap->next == NULL) | |
8736 { | |
8737 au_remove_cmds(ap); | |
8738 break; | |
8739 } | |
8740 au_remove_pat(ap); | |
8741 } | |
8742 | |
8743 /* | |
40 | 8744 * Show autocmd's for this autopat, or buflocals <buffer=X> |
7 | 8745 */ |
8746 else if (*cmd == NUL) | |
8747 show_autocmd(ap, event); | |
8748 | |
8749 /* | |
8750 * Add autocmd to this autopat, if it's the last one. | |
8751 */ | |
8752 else if (ap->next == NULL) | |
8753 break; | |
8754 } | |
8755 } | |
8756 prev_ap = &ap->next; | |
8757 } | |
8758 | |
8759 /* | |
8760 * Add a new command. | |
8761 */ | |
8762 if (*cmd != NUL) | |
8763 { | |
8764 /* | |
8765 * If the pattern we want to add a command to does appear at the | |
8766 * end of the list (or not is not in the list at all), add the | |
8767 * pattern at the end of the list. | |
8768 */ | |
8769 if (ap == NULL) | |
8770 { | |
40 | 8771 /* refuse to add buffer-local ap if buffer number is invalid */ |
8772 if (is_buflocal && (buflocal_nr == 0 | |
8773 || buflist_findnr(buflocal_nr) == NULL)) | |
8774 { | |
8775 EMSGN(_("E680: <buffer=%d>: invalid buffer number "), | |
8776 buflocal_nr); | |
8777 return FAIL; | |
8778 } | |
8779 | |
7 | 8780 ap = (AutoPat *)alloc((unsigned)sizeof(AutoPat)); |
8781 if (ap == NULL) | |
8782 return FAIL; | |
8783 ap->pat = vim_strnsave(pat, patlen); | |
8784 ap->patlen = patlen; | |
8785 if (ap->pat == NULL) | |
8786 { | |
8787 vim_free(ap); | |
8788 return FAIL; | |
8789 } | |
40 | 8790 |
8791 if (is_buflocal) | |
8792 { | |
8793 ap->buflocal_nr = buflocal_nr; | |
153 | 8794 ap->reg_prog = NULL; |
40 | 8795 } |
8796 else | |
7 | 8797 { |
153 | 8798 char_u *reg_pat; |
8799 | |
40 | 8800 ap->buflocal_nr = 0; |
153 | 8801 reg_pat = file_pat_to_reg_pat(pat, endpat, |
40 | 8802 &ap->allow_dirs, TRUE); |
153 | 8803 if (reg_pat != NULL) |
8804 ap->reg_prog = vim_regcomp(reg_pat, RE_MAGIC); | |
355 | 8805 vim_free(reg_pat); |
153 | 8806 if (reg_pat == NULL || ap->reg_prog == NULL) |
40 | 8807 { |
8808 vim_free(ap->pat); | |
8809 vim_free(ap); | |
8810 return FAIL; | |
8811 } | |
7 | 8812 } |
8813 ap->cmds = NULL; | |
8814 *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
|
8815 last_autopat[(int)event] = ap; |
7 | 8816 ap->next = NULL; |
8817 if (group == AUGROUP_ALL) | |
8818 ap->group = current_augroup; | |
8819 else | |
8820 ap->group = group; | |
8821 } | |
8822 | |
8823 /* | |
8824 * Add the autocmd at the end of the AutoCmd list. | |
8825 */ | |
8826 prev_ac = &(ap->cmds); | |
8827 while ((ac = *prev_ac) != NULL) | |
8828 prev_ac = &ac->next; | |
8829 ac = (AutoCmd *)alloc((unsigned)sizeof(AutoCmd)); | |
8830 if (ac == NULL) | |
8831 return FAIL; | |
8832 ac->cmd = vim_strsave(cmd); | |
8833 #ifdef FEAT_EVAL | |
14700
0a3b9ecf7cb8
patch 8.1.0362: cannot get the script line number when executing a function
Christian Brabandt <cb@256bit.org>
parents:
14585
diff
changeset
|
8834 ac->script_ctx = current_sctx; |
0a3b9ecf7cb8
patch 8.1.0362: cannot get the script line number when executing a function
Christian Brabandt <cb@256bit.org>
parents:
14585
diff
changeset
|
8835 ac->script_ctx.sc_lnum += sourcing_lnum; |
7 | 8836 #endif |
8837 if (ac->cmd == NULL) | |
8838 { | |
8839 vim_free(ac); | |
8840 return FAIL; | |
8841 } | |
8842 ac->next = NULL; | |
8843 *prev_ac = ac; | |
8844 ac->nested = nested; | |
8845 } | |
8846 } | |
8847 | |
8848 au_cleanup(); /* may really delete removed patterns/commands now */ | |
8849 return OK; | |
8850 } | |
8851 | |
8852 /* | |
8853 * Implementation of ":doautocmd [group] event [fname]". | |
8854 * Return OK for success, FAIL for failure; | |
8855 */ | |
8856 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8857 do_doautocmd( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8858 char_u *arg, |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8859 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
|
8860 int *did_something) |
7 | 8861 { |
8862 char_u *fname; | |
8863 int nothing_done = TRUE; | |
8864 int group; | |
8865 | |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8866 if (did_something != NULL) |
9291
bff3cd5cf922
commit https://github.com/vim/vim/commit/76ae22fef3cb224ca7fbf97517f881e825d4d0c2
Christian Brabandt <cb@256bit.org>
parents:
9260
diff
changeset
|
8867 *did_something = FALSE; |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8868 |
7 | 8869 /* |
8870 * Check for a legal group name. If not, use AUGROUP_ALL. | |
8871 */ | |
8872 group = au_get_grouparg(&arg); | |
8873 if (arg == NULL) /* out of memory */ | |
8874 return FAIL; | |
8875 | |
8876 if (*arg == '*') | |
8877 { | |
8878 EMSG(_("E217: Can't execute autocommands for ALL events")); | |
8879 return FAIL; | |
8880 } | |
8881 | |
8882 /* | |
8883 * Scan over the events. | |
8884 * If we find an illegal name, return here, don't do anything. | |
8885 */ | |
8886 fname = find_end_event(arg, group != AUGROUP_ALL); | |
8887 if (fname == NULL) | |
8888 return FAIL; | |
8889 | |
8890 fname = skipwhite(fname); | |
8891 | |
8892 /* | |
8893 * Loop over the events. | |
8894 */ | |
11641
dbce7e03bf55
patch 8.0.0703: illegal memory access with empty :doau command
Christian Brabandt <cb@256bit.org>
parents:
11605
diff
changeset
|
8895 while (*arg && !ends_excmd(*arg) && !VIM_ISWHITE(*arg)) |
7 | 8896 if (apply_autocmds_group(event_name2nr(arg, &arg), |
8897 fname, NULL, TRUE, group, curbuf, NULL)) | |
8898 nothing_done = FALSE; | |
8899 | |
8900 if (nothing_done && do_msg) | |
8901 MSG(_("No matching autocommands")); | |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8902 if (did_something != NULL) |
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8903 *did_something = !nothing_done; |
7 | 8904 |
8905 #ifdef FEAT_EVAL | |
8906 return aborting() ? FAIL : OK; | |
8907 #else | |
8908 return OK; | |
8909 #endif | |
8910 } | |
8911 | |
8912 /* | |
8913 * ":doautoall": execute autocommands for each loaded buffer. | |
8914 */ | |
8915 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8916 ex_doautoall(exarg_T *eap) |
7 | 8917 { |
8918 int retval; | |
8919 aco_save_T aco; | |
8920 buf_T *buf; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
8921 bufref_T bufref; |
3342 | 8922 char_u *arg = eap->arg; |
3350 | 8923 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
|
8924 int did_aucmd; |
7 | 8925 |
8926 /* | |
8927 * This is a bit tricky: For some commands curwin->w_buffer needs to be | |
8928 * equal to curbuf, but for some buffers there may not be a window. | |
8929 * So we change the buffer for the current window for a moment. This | |
8930 * gives problems when the autocommands make changes to the list of | |
8931 * buffers or windows... | |
8932 */ | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
8933 FOR_ALL_BUFFERS(buf) |
7 | 8934 { |
1665 | 8935 if (buf->b_ml.ml_mfp != NULL) |
7 | 8936 { |
8937 /* find a window for this buffer and save some values */ | |
8938 aucmd_prepbuf(&aco, buf); | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
8939 set_bufref(&bufref, buf); |
7 | 8940 |
8941 /* 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
|
8942 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
|
8943 |
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8944 if (call_do_modelines && did_aucmd) |
3342 | 8945 { |
8946 /* Execute the modeline settings, but don't set window-local | |
8947 * options if we are using the current window for another | |
8948 * buffer. */ | |
8949 do_modelines(curwin == aucmd_win ? OPT_NOWIN : 0); | |
8950 } | |
7 | 8951 |
8952 /* restore the current window */ | |
8953 aucmd_restbuf(&aco); | |
8954 | |
8955 /* 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
|
8956 if (retval == FAIL || !bufref_valid(&bufref)) |
7 | 8957 break; |
8958 } | |
8959 } | |
8960 | |
8961 check_cursor(); /* just in case lines got deleted */ | |
8962 } | |
8963 | |
8964 /* | |
3350 | 8965 * Check *argp for <nomodeline>. When it is present return FALSE, otherwise |
8966 * return TRUE and advance *argp to after it. | |
8967 * Thus return TRUE when do_modelines() should be called. | |
8968 */ | |
8969 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8970 check_nomodeline(char_u **argp) |
3350 | 8971 { |
8972 if (STRNCMP(*argp, "<nomodeline>", 12) == 0) | |
8973 { | |
8974 *argp = skipwhite(*argp + 12); | |
8975 return FALSE; | |
8976 } | |
8977 return TRUE; | |
8978 } | |
8979 | |
8980 /* | |
7 | 8981 * Prepare for executing autocommands for (hidden) buffer "buf". |
1906 | 8982 * Search for a visible window containing the current buffer. If there isn't |
8983 * one then use "aucmd_win". | |
7 | 8984 * Set "curbuf" and "curwin" to match "buf". |
8985 */ | |
8986 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8987 aucmd_prepbuf( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8988 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
|
8989 buf_T *buf) /* new curbuf */ |
7 | 8990 { |
8991 win_T *win; | |
1906 | 8992 int save_ea; |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8993 #ifdef FEAT_AUTOCHDIR |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8994 int save_acd; |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8995 #endif |
7 | 8996 |
8997 /* Find a window that is for the new buffer */ | |
8998 if (buf == curbuf) /* be quick when buf is curbuf */ | |
8999 win = curwin; | |
9000 else | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
9001 FOR_ALL_WINDOWS(win) |
7 | 9002 if (win->w_buffer == buf) |
9003 break; | |
9004 | |
1906 | 9005 /* Allocate "aucmd_win" when needed. If this fails (out of memory) fall |
9006 * back to using the current window. */ | |
9007 if (win == NULL && aucmd_win == NULL) | |
9008 { | |
9009 win_alloc_aucmd_win(); | |
9010 if (aucmd_win == NULL) | |
9011 win = curwin; | |
9012 } | |
1944 | 9013 if (win == NULL && aucmd_win_used) |
9014 /* Strange recursive autocommand, fall back to using the current | |
9015 * window. Expect a few side effects... */ | |
9016 win = curwin; | |
1906 | 9017 |
9018 aco->save_curwin = curwin; | |
9019 aco->save_curbuf = curbuf; | |
7 | 9020 if (win != NULL) |
9021 { | |
1906 | 9022 /* There is a window for "buf" in the current tab page, make it the |
9023 * curwin. This is preferred, it has the least side effects (esp. if | |
9024 * "buf" is curbuf). */ | |
1944 | 9025 aco->use_aucmd_win = FALSE; |
7 | 9026 curwin = win; |
9027 } | |
9028 else | |
9029 { | |
1906 | 9030 /* There is no window for "buf", use "aucmd_win". To minimize the side |
4352 | 9031 * effects, insert it in the current tab page. |
1906 | 9032 * Anything related to a window (e.g., setting folds) may have |
9033 * unexpected results. */ | |
1944 | 9034 aco->use_aucmd_win = TRUE; |
9035 aucmd_win_used = TRUE; | |
1918 | 9036 aucmd_win->w_buffer = buf; |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
9037 #if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) |
2258
772bfca06c18
Fix crash when using ":grep".
Bram Moolenaar <bram@vim.org>
parents:
2244
diff
changeset
|
9038 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
|
9039 #endif |
7 | 9040 ++buf->b_nwindows; |
1918 | 9041 win_init_empty(aucmd_win); /* set cursor and topline to safe values */ |
1944 | 9042 |
9043 /* Make sure w_localdir and globaldir are NULL to avoid a chdir() in | |
9044 * win_enter_ext(). */ | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
9045 VIM_CLEAR(aucmd_win->w_localdir); |
1944 | 9046 aco->globaldir = globaldir; |
9047 globaldir = NULL; | |
9048 | |
7 | 9049 |
1923 | 9050 /* Split the current window, put the aucmd_win in the upper half. |
9051 * We don't want the BufEnter or WinEnter autocommands. */ | |
9052 block_autocmds(); | |
1906 | 9053 make_snapshot(SNAP_AUCMD_IDX); |
9054 save_ea = p_ea; | |
9055 p_ea = FALSE; | |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9056 |
12477
68d7bc045dbe
patch 8.0.1118: FEAT_WINDOWS adds a lot of #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
12216
diff
changeset
|
9057 #ifdef FEAT_AUTOCHDIR |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9058 /* 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
|
9059 save_acd = p_acd; |
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9060 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
|
9061 #endif |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9062 |
1906 | 9063 (void)win_split_ins(0, WSP_TOP, aucmd_win, 0); |
9064 (void)win_comp_pos(); /* recompute window positions */ | |
9065 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
|
9066 #ifdef FEAT_AUTOCHDIR |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9067 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
|
9068 #endif |
1923 | 9069 unblock_autocmds(); |
1918 | 9070 curwin = aucmd_win; |
1906 | 9071 } |
7 | 9072 curbuf = buf; |
1906 | 9073 aco->new_curwin = curwin; |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9074 set_bufref(&aco->new_curbuf, curbuf); |
7 | 9075 } |
9076 | |
9077 /* | |
9078 * Cleanup after executing autocommands for a (hidden) buffer. | |
9079 * Restore the window as it was (if possible). | |
9080 */ | |
9081 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9082 aucmd_restbuf( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9083 aco_save_T *aco) /* structure holding saved values */ |
7 | 9084 { |
1906 | 9085 int dummy; |
9086 | |
1944 | 9087 if (aco->use_aucmd_win) |
1906 | 9088 { |
9089 --curbuf->b_nwindows; | |
9090 /* Find "aucmd_win", it can't be closed, but it may be in another tab | |
1923 | 9091 * page. Do not trigger autocommands here. */ |
9092 block_autocmds(); | |
1906 | 9093 if (curwin != aucmd_win) |
9094 { | |
9095 tabpage_T *tp; | |
9096 win_T *wp; | |
9097 | |
9098 FOR_ALL_TAB_WINDOWS(tp, wp) | |
9099 { | |
9100 if (wp == aucmd_win) | |
9101 { | |
9102 if (tp != curtab) | |
4354 | 9103 goto_tabpage_tp(tp, TRUE, TRUE); |
1906 | 9104 win_goto(aucmd_win); |
3340 | 9105 goto win_found; |
1906 | 9106 } |
9107 } | |
9108 } | |
3340 | 9109 win_found: |
1906 | 9110 |
9111 /* Remove the window and frame from the tree of frames. */ | |
9112 (void)winframe_remove(curwin, &dummy, NULL); | |
9113 win_remove(curwin, NULL); | |
1944 | 9114 aucmd_win_used = FALSE; |
1906 | 9115 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
|
9116 |
e08ead1d269f
patch 8.0.0486: crash and endless loop when closing windows in autocmd
Christian Brabandt <cb@256bit.org>
parents:
11158
diff
changeset
|
9117 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
|
9118 /* 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
|
9119 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
|
9120 |
1906 | 9121 restore_snapshot(SNAP_AUCMD_IDX, FALSE); |
9122 (void)win_comp_pos(); /* recompute window positions */ | |
1923 | 9123 unblock_autocmds(); |
1906 | 9124 |
9125 if (win_valid(aco->save_curwin)) | |
9126 curwin = aco->save_curwin; | |
9127 else | |
9128 /* Hmm, original window disappeared. Just use the first one. */ | |
9129 curwin = firstwin; | |
12477
68d7bc045dbe
patch 8.0.1118: FEAT_WINDOWS adds a lot of #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
12216
diff
changeset
|
9130 #ifdef FEAT_EVAL |
4287 | 9131 vars_clear(&aucmd_win->w_vars->dv_hashtab); /* free all w: variables */ |
9132 hash_init(&aucmd_win->w_vars->dv_hashtab); /* re-use the hashtab */ | |
1906 | 9133 #endif |
9134 curbuf = curwin->w_buffer; | |
9135 | |
1944 | 9136 vim_free(globaldir); |
9137 globaldir = aco->globaldir; | |
9138 | |
1906 | 9139 /* the buffer contents may have changed */ |
9140 check_cursor(); | |
9141 if (curwin->w_topline > curbuf->b_ml.ml_line_count) | |
9142 { | |
9143 curwin->w_topline = curbuf->b_ml.ml_line_count; | |
9144 #ifdef FEAT_DIFF | |
9145 curwin->w_topfill = 0; | |
9146 #endif | |
9147 } | |
9148 #if defined(FEAT_GUI) | |
9149 /* Hide the scrollbars from the aucmd_win and update. */ | |
9150 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_LEFT], FALSE); | |
9151 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_RIGHT], FALSE); | |
9152 gui_may_update_scrollbars(); | |
9153 #endif | |
9154 } | |
9155 else | |
7 | 9156 { |
9157 /* restore curwin */ | |
9158 if (win_valid(aco->save_curwin)) | |
9159 { | |
1906 | 9160 /* Restore the buffer which was previously edited by curwin, if |
1944 | 9161 * it was changed, we are still the same window and the buffer is |
1906 | 9162 * valid. */ |
7 | 9163 if (curwin == aco->new_curwin |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9164 && curbuf != aco->new_curbuf.br_buf |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9165 && bufref_valid(&aco->new_curbuf) |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9166 && aco->new_curbuf.br_buf->b_ml.ml_mfp != NULL) |
7 | 9167 { |
3497 | 9168 # if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) |
9169 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
|
9170 curwin->w_s = &aco->new_curbuf.br_buf->b_s; |
3497 | 9171 # endif |
7 | 9172 --curbuf->b_nwindows; |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9173 curbuf = aco->new_curbuf.br_buf; |
7 | 9174 curwin->w_buffer = curbuf; |
9175 ++curbuf->b_nwindows; | |
9176 } | |
9177 | |
9178 curwin = aco->save_curwin; | |
9179 curbuf = curwin->w_buffer; | |
6201 | 9180 /* In case the autocommand move the cursor to a position that that |
9181 * not exist in curbuf. */ | |
9182 check_cursor(); | |
7 | 9183 } |
9184 } | |
9185 } | |
9186 | |
9187 static int autocmd_nested = FALSE; | |
9188 | |
9189 /* | |
9190 * Execute autocommands for "event" and file name "fname". | |
9191 * Return TRUE if some commands were executed. | |
9192 */ | |
9193 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9194 apply_autocmds( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9195 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9196 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
|
9197 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
|
9198 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
|
9199 buf_T *buf) /* buffer for <abuf> */ |
7 | 9200 { |
9201 return apply_autocmds_group(event, fname, fname_io, force, | |
9202 AUGROUP_ALL, buf, NULL); | |
9203 } | |
9204 | |
9205 /* | |
9206 * Like apply_autocmds(), but with extra "eap" argument. This takes care of | |
9207 * setting v:filearg. | |
9208 */ | |
9209 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9210 apply_autocmds_exarg( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9211 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9212 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9213 char_u *fname_io, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9214 int force, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9215 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9216 exarg_T *eap) |
7 | 9217 { |
9218 return apply_autocmds_group(event, fname, fname_io, force, | |
9219 AUGROUP_ALL, buf, eap); | |
9220 } | |
9221 | |
9222 /* | |
9223 * Like apply_autocmds(), but handles the caller's retval. If the script | |
9224 * processing is being aborted or if retval is FAIL when inside a try | |
9225 * conditional, no autocommands are executed. If otherwise the autocommands | |
9226 * cause the script to be aborted, retval is set to FAIL. | |
9227 */ | |
9228 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9229 apply_autocmds_retval( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9230 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9231 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
|
9232 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
|
9233 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
|
9234 buf_T *buf, /* buffer for <abuf> */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9235 int *retval) /* pointer to caller's retval */ |
7 | 9236 { |
9237 int did_cmd; | |
9238 | |
532 | 9239 #ifdef FEAT_EVAL |
7 | 9240 if (should_abort(*retval)) |
9241 return FALSE; | |
532 | 9242 #endif |
7 | 9243 |
9244 did_cmd = apply_autocmds_group(event, fname, fname_io, force, | |
9245 AUGROUP_ALL, buf, NULL); | |
532 | 9246 if (did_cmd |
9247 #ifdef FEAT_EVAL | |
9248 && aborting() | |
9249 #endif | |
9250 ) | |
7 | 9251 *retval = FAIL; |
9252 return did_cmd; | |
9253 } | |
9254 | |
609 | 9255 /* |
9256 * Return TRUE when there is a CursorHold autocommand defined. | |
9257 */ | |
7 | 9258 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9259 has_cursorhold(void) |
7 | 9260 { |
661 | 9261 return (first_autopat[(int)(get_real_state() == NORMAL_BUSY |
9262 ? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)] != NULL); | |
7 | 9263 } |
609 | 9264 |
9265 /* | |
9266 * Return TRUE if the CursorHold event can be triggered. | |
9267 */ | |
9268 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9269 trigger_cursorhold(void) |
609 | 9270 { |
661 | 9271 int state; |
9272 | |
2976 | 9273 if (!did_cursorhold |
9274 && 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
|
9275 && reg_recording == 0 |
2976 | 9276 && typebuf.tb_len == 0 |
978 | 9277 #ifdef FEAT_INS_EXPAND |
9278 && !ins_compl_active() | |
9279 #endif | |
9280 ) | |
661 | 9281 { |
9282 state = get_real_state(); | |
9283 if (state == NORMAL_BUSY || (state & INSERT) != 0) | |
9284 return TRUE; | |
9285 } | |
9286 return FALSE; | |
609 | 9287 } |
661 | 9288 |
9289 /* | |
9290 * Return TRUE when there is a CursorMoved autocommand defined. | |
9291 */ | |
9292 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9293 has_cursormoved(void) |
661 | 9294 { |
9295 return (first_autopat[(int)EVENT_CURSORMOVED] != NULL); | |
9296 } | |
9297 | |
9298 /* | |
9299 * Return TRUE when there is a CursorMovedI autocommand defined. | |
9300 */ | |
9301 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9302 has_cursormovedI(void) |
661 | 9303 { |
9304 return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL); | |
9305 } | |
7 | 9306 |
3390 | 9307 /* |
4232 | 9308 * Return TRUE when there is a TextChanged autocommand defined. |
9309 */ | |
9310 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9311 has_textchanged(void) |
4232 | 9312 { |
9313 return (first_autopat[(int)EVENT_TEXTCHANGED] != NULL); | |
9314 } | |
9315 | |
9316 /* | |
9317 * Return TRUE when there is a TextChangedI autocommand defined. | |
9318 */ | |
9319 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9320 has_textchangedI(void) |
4232 | 9321 { |
9322 return (first_autopat[(int)EVENT_TEXTCHANGEDI] != NULL); | |
9323 } | |
9324 | |
9325 /* | |
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
|
9326 * 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
|
9327 */ |
5ed6e4a25925
patch 8.0.1494: no autocmd triggered in Insert mode with visible popup menu
Christian Brabandt <cb@256bit.org>
parents:
13170
diff
changeset
|
9328 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
|
9329 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
|
9330 { |
5ed6e4a25925
patch 8.0.1494: no autocmd triggered in Insert mode with visible popup menu
Christian Brabandt <cb@256bit.org>
parents:
13170
diff
changeset
|
9331 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
|
9332 } |
5ed6e4a25925
patch 8.0.1494: no autocmd triggered in Insert mode with visible popup menu
Christian Brabandt <cb@256bit.org>
parents:
13170
diff
changeset
|
9333 |
5ed6e4a25925
patch 8.0.1494: no autocmd triggered in Insert mode with visible popup menu
Christian Brabandt <cb@256bit.org>
parents:
13170
diff
changeset
|
9334 /* |
3390 | 9335 * Return TRUE when there is an InsertCharPre autocommand defined. |
9336 */ | |
9337 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9338 has_insertcharpre(void) |
3390 | 9339 { |
9340 return (first_autopat[(int)EVENT_INSERTCHARPRE] != NULL); | |
9341 } | |
9342 | |
6154 | 9343 /* |
9344 * Return TRUE when there is an CmdUndefined autocommand defined. | |
9345 */ | |
9346 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9347 has_cmdundefined(void) |
6154 | 9348 { |
9349 return (first_autopat[(int)EVENT_CMDUNDEFINED] != NULL); | |
9350 } | |
9351 | |
9352 /* | |
9353 * Return TRUE when there is an FuncUndefined autocommand defined. | |
9354 */ | |
9355 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9356 has_funcundefined(void) |
6154 | 9357 { |
9358 return (first_autopat[(int)EVENT_FUNCUNDEFINED] != NULL); | |
9359 } | |
9360 | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9361 /* |
13037
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9362 * 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
|
9363 */ |
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9364 int |
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9365 has_textyankpost(void) |
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9366 { |
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9367 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
|
9368 } |
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9369 |
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9370 /* |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9371 * 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
|
9372 * 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
|
9373 */ |
7 | 9374 static int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9375 apply_autocmds_group( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9376 event_T event, |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
9377 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
|
9378 char_u *fname_io, /* fname to use for <afile> on cmdline, NULL means |
7 | 9379 use fname */ |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
9380 int force, /* when TRUE, ignore autocmd_busy */ |
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
9381 int group, /* group ID, or AUGROUP_ALL */ |
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
9382 buf_T *buf, /* buffer for <abuf> */ |
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
9383 exarg_T *eap UNUSED) /* command arguments */ |
7 | 9384 { |
9385 char_u *sfname = NULL; /* short file name */ | |
9386 char_u *tail; | |
9387 int save_changed; | |
9388 buf_T *old_curbuf; | |
9389 int retval = FALSE; | |
9390 char_u *save_sourcing_name; | |
9391 linenr_T save_sourcing_lnum; | |
9392 char_u *save_autocmd_fname; | |
1723 | 9393 int save_autocmd_fname_full; |
7 | 9394 int save_autocmd_bufnr; |
9395 char_u *save_autocmd_match; | |
9396 int save_autocmd_busy; | |
9397 int save_autocmd_nested; | |
9398 static int nesting = 0; | |
9399 AutoPatCmd patcmd; | |
9400 AutoPat *ap; | |
9401 #ifdef FEAT_EVAL | |
14700
0a3b9ecf7cb8
patch 8.1.0362: cannot get the script line number when executing a function
Christian Brabandt <cb@256bit.org>
parents:
14585
diff
changeset
|
9402 sctx_T save_current_sctx; |
7 | 9403 void *save_funccalp; |
9404 char_u *save_cmdarg; | |
9405 long save_cmdbang; | |
9406 #endif | |
9407 static int filechangeshell_busy = FALSE; | |
170 | 9408 #ifdef FEAT_PROFILE |
9409 proftime_T wait_time; | |
9410 #endif | |
6608 | 9411 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
|
9412 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
|
9413 int save_KeyTyped = KeyTyped; |
7 | 9414 |
9415 /* | |
9416 * Quickly return if there are no autocommands for this event or | |
9417 * autocommands are blocked. | |
9418 */ | |
11641
dbce7e03bf55
patch 8.0.0703: illegal memory access with empty :doau command
Christian Brabandt <cb@256bit.org>
parents:
11605
diff
changeset
|
9419 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
|
9420 || autocmd_blocked > 0) |
40 | 9421 goto BYPASS_AU; |
7 | 9422 |
9423 /* | |
9424 * When autocommands are busy, new autocommands are only executed when | |
9425 * explicitly enabled with the "nested" flag. | |
9426 */ | |
9427 if (autocmd_busy && !(force || autocmd_nested)) | |
40 | 9428 goto BYPASS_AU; |
7 | 9429 |
9430 #ifdef FEAT_EVAL | |
9431 /* | |
1201 | 9432 * Quickly return when immediately aborting on error, or when an interrupt |
7 | 9433 * occurred or an exception was thrown but not caught. |
9434 */ | |
9435 if (aborting()) | |
40 | 9436 goto BYPASS_AU; |
7 | 9437 #endif |
9438 | |
9439 /* | |
9440 * FileChangedShell never nests, because it can create an endless loop. | |
9441 */ | |
765 | 9442 if (filechangeshell_busy && (event == EVENT_FILECHANGEDSHELL |
9443 || event == EVENT_FILECHANGEDSHELLPOST)) | |
40 | 9444 goto BYPASS_AU; |
7 | 9445 |
9446 /* | |
9447 * Ignore events in 'eventignore'. | |
9448 */ | |
9449 if (event_ignored(event)) | |
40 | 9450 goto BYPASS_AU; |
7 | 9451 |
9452 /* | |
9453 * Allow nesting of autocommands, but restrict the depth, because it's | |
9454 * possible to create an endless loop. | |
9455 */ | |
9456 if (nesting == 10) | |
9457 { | |
9458 EMSG(_("E218: autocommand nesting too deep")); | |
40 | 9459 goto BYPASS_AU; |
7 | 9460 } |
9461 | |
9462 /* | |
9463 * Check if these autocommands are disabled. Used when doing ":all" or | |
9464 * ":ball". | |
9465 */ | |
9466 if ( (autocmd_no_enter | |
9467 && (event == EVENT_WINENTER || event == EVENT_BUFENTER)) | |
9468 || (autocmd_no_leave | |
9469 && (event == EVENT_WINLEAVE || event == EVENT_BUFLEAVE))) | |
40 | 9470 goto BYPASS_AU; |
7 | 9471 |
9472 /* | |
9473 * Save the autocmd_* variables and info about the current buffer. | |
9474 */ | |
9475 save_autocmd_fname = autocmd_fname; | |
1723 | 9476 save_autocmd_fname_full = autocmd_fname_full; |
7 | 9477 save_autocmd_bufnr = autocmd_bufnr; |
9478 save_autocmd_match = autocmd_match; | |
9479 save_autocmd_busy = autocmd_busy; | |
9480 save_autocmd_nested = autocmd_nested; | |
9481 save_changed = curbuf->b_changed; | |
9482 old_curbuf = curbuf; | |
9483 | |
9484 /* | |
9485 * Set the file name to be used for <afile>. | |
1471 | 9486 * Make a copy to avoid that changing a buffer name or directory makes it |
9487 * invalid. | |
7 | 9488 */ |
9489 if (fname_io == NULL) | |
9490 { | |
13810
cad480bac9e1
patch 8.0.1777: cannot cleanup before loading another colorscheme
Christian Brabandt <cb@256bit.org>
parents:
13804
diff
changeset
|
9491 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
|
9492 || event == EVENT_OPTIONSET) |
5521 | 9493 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
|
9494 else if (fname != NULL && !ends_excmd(*fname)) |
7 | 9495 autocmd_fname = fname; |
9496 else if (buf != NULL) | |
1723 | 9497 autocmd_fname = buf->b_ffname; |
7 | 9498 else |
9499 autocmd_fname = NULL; | |
9500 } | |
9501 else | |
9502 autocmd_fname = fname_io; | |
1471 | 9503 if (autocmd_fname != NULL) |
1723 | 9504 autocmd_fname = vim_strsave(autocmd_fname); |
9505 autocmd_fname_full = FALSE; /* call FullName_save() later */ | |
7 | 9506 |
9507 /* | |
9508 * Set the buffer number to be used for <abuf>. | |
9509 */ | |
9510 if (buf == NULL) | |
9511 autocmd_bufnr = 0; | |
9512 else | |
9513 autocmd_bufnr = buf->b_fnum; | |
9514 | |
9515 /* | |
9516 * When the file name is NULL or empty, use the file name of buffer "buf". | |
9517 * Always use the full path of the file name to match with, in case | |
9518 * "allow_dirs" is set. | |
9519 */ | |
9520 if (fname == NULL || *fname == NUL) | |
9521 { | |
9522 if (buf == NULL) | |
9523 fname = NULL; | |
9524 else | |
9525 { | |
9526 #ifdef FEAT_SYN_HL | |
9527 if (event == EVENT_SYNTAX) | |
9528 fname = buf->b_p_syn; | |
9529 else | |
9530 #endif | |
9531 if (event == EVENT_FILETYPE) | |
9532 fname = buf->b_p_ft; | |
9533 else | |
9534 { | |
9535 if (buf->b_sfname != NULL) | |
9536 sfname = vim_strsave(buf->b_sfname); | |
9537 fname = buf->b_ffname; | |
9538 } | |
9539 } | |
9540 if (fname == NULL) | |
9541 fname = (char_u *)""; | |
9542 fname = vim_strsave(fname); /* make a copy, so we can change it */ | |
9543 } | |
9544 else | |
9545 { | |
9546 sfname = vim_strsave(fname); | |
5521 | 9547 /* 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
|
9548 * ColorScheme, QuickFixCmd* or DirChanged */ |
161 | 9549 if (event == EVENT_FILETYPE |
9550 || 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
|
9551 || event == EVENT_CMDLINECHANGED |
8a3e9d1e8cf9
patch 8.0.1748: CmdlineEnter command uses backslash instead of slash
Christian Brabandt <cb@256bit.org>
parents:
13730
diff
changeset
|
9552 || event == EVENT_CMDLINEENTER |
8a3e9d1e8cf9
patch 8.0.1748: CmdlineEnter command uses backslash instead of slash
Christian Brabandt <cb@256bit.org>
parents:
13730
diff
changeset
|
9553 || event == EVENT_CMDLINELEAVE |
8a3e9d1e8cf9
patch 8.0.1748: CmdlineEnter command uses backslash instead of slash
Christian Brabandt <cb@256bit.org>
parents:
13730
diff
changeset
|
9554 || event == EVENT_CMDWINENTER |
8a3e9d1e8cf9
patch 8.0.1748: CmdlineEnter command uses backslash instead of slash
Christian Brabandt <cb@256bit.org>
parents:
13730
diff
changeset
|
9555 || event == EVENT_CMDWINLEAVE |
8a3e9d1e8cf9
patch 8.0.1748: CmdlineEnter command uses backslash instead of slash
Christian Brabandt <cb@256bit.org>
parents:
13730
diff
changeset
|
9556 || event == EVENT_CMDUNDEFINED |
1867 | 9557 || event == EVENT_FUNCUNDEFINED |
161 | 9558 || event == EVENT_REMOTEREPLY |
649 | 9559 || event == EVENT_SPELLFILEMISSING |
161 | 9560 || event == EVENT_QUICKFIXCMDPRE |
5521 | 9561 || event == EVENT_COLORSCHEME |
13810
cad480bac9e1
patch 8.0.1777: cannot cleanup before loading another colorscheme
Christian Brabandt <cb@256bit.org>
parents:
13804
diff
changeset
|
9562 || event == EVENT_COLORSCHEMEPRE |
6935 | 9563 || event == EVENT_OPTIONSET |
13170
6559e98f3e74
patch 8.0.1459: cannot handle change of directory
Christian Brabandt <cb@256bit.org>
parents:
13142
diff
changeset
|
9564 || event == EVENT_QUICKFIXCMDPOST |
6559e98f3e74
patch 8.0.1459: cannot handle change of directory
Christian Brabandt <cb@256bit.org>
parents:
13142
diff
changeset
|
9565 || 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
|
9566 { |
7 | 9567 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
|
9568 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
|
9569 } |
7 | 9570 else |
9571 fname = FullName_save(fname, FALSE); | |
9572 } | |
9573 if (fname == NULL) /* out of memory */ | |
9574 { | |
9575 vim_free(sfname); | |
40 | 9576 retval = FALSE; |
9577 goto BYPASS_AU; | |
7 | 9578 } |
9579 | |
9580 #ifdef BACKSLASH_IN_FILENAME | |
9581 /* | |
9582 * Replace all backslashes with forward slashes. This makes the | |
9583 * autocommand patterns portable between Unix and MS-DOS. | |
9584 */ | |
9585 if (sfname != NULL) | |
9586 forward_slash(sfname); | |
9587 forward_slash(fname); | |
9588 #endif | |
9589 | |
9590 #ifdef VMS | |
9591 /* remove version for correct match */ | |
9592 if (sfname != NULL) | |
9593 vms_remove_version(sfname); | |
9594 vms_remove_version(fname); | |
9595 #endif | |
9596 | |
9597 /* | |
9598 * Set the name to be used for <amatch>. | |
9599 */ | |
9600 autocmd_match = fname; | |
9601 | |
9602 | |
14157
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
9603 /* Don't redraw while doing autocommands. */ |
7 | 9604 ++RedrawingDisabled; |
9605 save_sourcing_name = sourcing_name; | |
9606 sourcing_name = NULL; /* don't free this one */ | |
9607 save_sourcing_lnum = sourcing_lnum; | |
9608 sourcing_lnum = 0; /* no line number here */ | |
9609 | |
9610 #ifdef FEAT_EVAL | |
14700
0a3b9ecf7cb8
patch 8.1.0362: cannot get the script line number when executing a function
Christian Brabandt <cb@256bit.org>
parents:
14585
diff
changeset
|
9611 save_current_sctx = current_sctx; |
7 | 9612 |
170 | 9613 # ifdef FEAT_PROFILE |
788 | 9614 if (do_profiling == PROF_YES) |
170 | 9615 prof_child_enter(&wait_time); /* doesn't count for the caller itself */ |
9616 # endif | |
9617 | |
7 | 9618 /* Don't use local function variables, if called from a function */ |
9619 save_funccalp = save_funccal(); | |
9620 #endif | |
9621 | |
9622 /* | |
9623 * When starting to execute autocommands, save the search patterns. | |
9624 */ | |
9625 if (!autocmd_busy) | |
9626 { | |
9627 save_search_patterns(); | |
7285
a34232b17763
commit https://github.com/vim/vim/commit/20ad69ccfb60ef718bd26387ef0e5424461a643e
Christian Brabandt <cb@256bit.org>
parents:
7170
diff
changeset
|
9628 #ifdef FEAT_INS_EXPAND |
6608 | 9629 if (!ins_compl_active()) |
7285
a34232b17763
commit https://github.com/vim/vim/commit/20ad69ccfb60ef718bd26387ef0e5424461a643e
Christian Brabandt <cb@256bit.org>
parents:
7170
diff
changeset
|
9630 #endif |
6608 | 9631 { |
11325
77f3b7316d8b
patch 8.0.0548: saving the redo buffer only works one time
Christian Brabandt <cb@256bit.org>
parents:
11199
diff
changeset
|
9632 saveRedobuff(&save_redo); |
6608 | 9633 did_save_redobuff = TRUE; |
9634 } | |
7 | 9635 did_filetype = keep_filetype; |
9636 } | |
9637 | |
9638 /* | |
9639 * Note that we are applying autocmds. Some commands need to know. | |
9640 */ | |
9641 autocmd_busy = TRUE; | |
9642 filechangeshell_busy = (event == EVENT_FILECHANGEDSHELL); | |
9643 ++nesting; /* see matching decrement below */ | |
9644 | |
9645 /* Remember that FileType was triggered. Used for did_filetype(). */ | |
9646 if (event == EVENT_FILETYPE) | |
9647 did_filetype = TRUE; | |
9648 | |
9649 tail = gettail(fname); | |
9650 | |
9651 /* Find first autocommand that matches */ | |
9652 patcmd.curpat = first_autopat[(int)event]; | |
9653 patcmd.nextcmd = NULL; | |
9654 patcmd.group = group; | |
9655 patcmd.fname = fname; | |
9656 patcmd.sfname = sfname; | |
9657 patcmd.tail = tail; | |
9658 patcmd.event = event; | |
40 | 9659 patcmd.arg_bufnr = autocmd_bufnr; |
9660 patcmd.next = NULL; | |
7 | 9661 auto_next_pat(&patcmd, FALSE); |
9662 | |
9663 /* found one, start executing the autocommands */ | |
9664 if (patcmd.curpat != NULL) | |
9665 { | |
40 | 9666 /* add to active_apc_list */ |
9667 patcmd.next = active_apc_list; | |
9668 active_apc_list = &patcmd; | |
9669 | |
7 | 9670 #ifdef FEAT_EVAL |
9671 /* 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
|
9672 save_cmdbang = (long)get_vim_var_nr(VV_CMDBANG); |
7 | 9673 if (eap != NULL) |
9674 { | |
9675 save_cmdarg = set_cmdarg(eap, NULL); | |
9676 set_vim_var_nr(VV_CMDBANG, (long)eap->forceit); | |
9677 } | |
9678 else | |
9679 save_cmdarg = NULL; /* avoid gcc warning */ | |
9680 #endif | |
9681 retval = TRUE; | |
9682 /* mark the last pattern, to avoid an endless loop when more patterns | |
9683 * are added when executing autocommands */ | |
9684 for (ap = patcmd.curpat; ap->next != NULL; ap = ap->next) | |
9685 ap->last = FALSE; | |
9686 ap->last = TRUE; | |
9687 check_lnums(TRUE); /* make sure cursor and topline are valid */ | |
9688 do_cmdline(NULL, getnextac, (void *)&patcmd, | |
9689 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT); | |
9690 #ifdef FEAT_EVAL | |
9691 if (eap != NULL) | |
9692 { | |
9693 (void)set_cmdarg(NULL, save_cmdarg); | |
9694 set_vim_var_nr(VV_CMDBANG, save_cmdbang); | |
9695 } | |
9696 #endif | |
40 | 9697 /* delete from active_apc_list */ |
9698 if (active_apc_list == &patcmd) /* just in case */ | |
9699 active_apc_list = patcmd.next; | |
7 | 9700 } |
9701 | |
9702 --RedrawingDisabled; | |
9703 autocmd_busy = save_autocmd_busy; | |
9704 filechangeshell_busy = FALSE; | |
9705 autocmd_nested = save_autocmd_nested; | |
9706 vim_free(sourcing_name); | |
9707 sourcing_name = save_sourcing_name; | |
9708 sourcing_lnum = save_sourcing_lnum; | |
1471 | 9709 vim_free(autocmd_fname); |
7 | 9710 autocmd_fname = save_autocmd_fname; |
1723 | 9711 autocmd_fname_full = save_autocmd_fname_full; |
7 | 9712 autocmd_bufnr = save_autocmd_bufnr; |
9713 autocmd_match = save_autocmd_match; | |
9714 #ifdef FEAT_EVAL | |
14700
0a3b9ecf7cb8
patch 8.1.0362: cannot get the script line number when executing a function
Christian Brabandt <cb@256bit.org>
parents:
14585
diff
changeset
|
9715 current_sctx = save_current_sctx; |
7 | 9716 restore_funccal(save_funccalp); |
170 | 9717 # ifdef FEAT_PROFILE |
788 | 9718 if (do_profiling == PROF_YES) |
170 | 9719 prof_child_exit(&wait_time); |
9720 # endif | |
7 | 9721 #endif |
12796
9d51b8c6b84e
patch 8.0.1275: CmdlineLeave autocmd prevents fold from opening
Christian Brabandt <cb@256bit.org>
parents:
12771
diff
changeset
|
9722 KeyTyped = save_KeyTyped; |
7 | 9723 vim_free(fname); |
9724 vim_free(sfname); | |
9725 --nesting; /* see matching increment above */ | |
9726 | |
9727 /* | |
9728 * When stopping to execute autocommands, restore the search patterns and | |
5958 | 9729 * the redo buffer. Free any buffers in the au_pending_free_buf list and |
9730 * free any windows in the au_pending_free_win list. | |
7 | 9731 */ |
9732 if (!autocmd_busy) | |
9733 { | |
9734 restore_search_patterns(); | |
6608 | 9735 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
|
9736 restoreRedobuff(&save_redo); |
7 | 9737 did_filetype = FALSE; |
5816 | 9738 while (au_pending_free_buf != NULL) |
9739 { | |
9740 buf_T *b = au_pending_free_buf->b_next; | |
9741 vim_free(au_pending_free_buf); | |
9742 au_pending_free_buf = b; | |
9743 } | |
5958 | 9744 while (au_pending_free_win != NULL) |
9745 { | |
9746 win_T *w = au_pending_free_win->w_next; | |
9747 vim_free(au_pending_free_win); | |
9748 au_pending_free_win = w; | |
9749 } | |
7 | 9750 } |
9751 | |
9752 /* | |
9753 * Some events don't set or reset the Changed flag. | |
9754 * Check if still in the same buffer! | |
9755 */ | |
9756 if (curbuf == old_curbuf | |
9757 && (event == EVENT_BUFREADPOST | |
9758 || event == EVENT_BUFWRITEPOST | |
9759 || event == EVENT_FILEAPPENDPOST | |
9760 || event == EVENT_VIMLEAVE | |
9761 || event == EVENT_VIMLEAVEPRE)) | |
9762 { | |
9763 #ifdef FEAT_TITLE | |
9764 if (curbuf->b_changed != save_changed) | |
9765 need_maketitle = TRUE; | |
9766 #endif | |
9767 curbuf->b_changed = save_changed; | |
9768 } | |
9769 | |
9770 au_cleanup(); /* may really delete removed patterns/commands now */ | |
40 | 9771 |
9772 BYPASS_AU: | |
9773 /* When wiping out a buffer make sure all its buffer-local autocommands | |
9774 * are deleted. */ | |
9775 if (event == EVENT_BUFWIPEOUT && buf != NULL) | |
9776 aubuflocal_remove(buf); | |
9777 | |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
9778 if (retval == OK && event == EVENT_FILETYPE) |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
9779 au_did_filetype = TRUE; |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
9780 |
7 | 9781 return retval; |
9782 } | |
9783 | |
1410 | 9784 # ifdef FEAT_EVAL |
9785 static char_u *old_termresponse = NULL; | |
9786 # endif | |
9787 | |
9788 /* | |
9789 * Block triggering autocommands until unblock_autocmd() is called. | |
9790 * Can be used recursively, so long as it's symmetric. | |
9791 */ | |
9792 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9793 block_autocmds(void) |
1410 | 9794 { |
9795 # ifdef FEAT_EVAL | |
9796 /* Remember the value of v:termresponse. */ | |
9797 if (autocmd_blocked == 0) | |
9798 old_termresponse = get_vim_var_str(VV_TERMRESPONSE); | |
9799 # endif | |
9800 ++autocmd_blocked; | |
9801 } | |
9802 | |
9803 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9804 unblock_autocmds(void) |
1410 | 9805 { |
9806 --autocmd_blocked; | |
9807 | |
9808 # ifdef FEAT_EVAL | |
9809 /* When v:termresponse was set while autocommands were blocked, trigger | |
9810 * the autocommands now. Esp. useful when executing a shell command | |
9811 * during startup (vimdiff). */ | |
9812 if (autocmd_blocked == 0 | |
9813 && get_vim_var_str(VV_TERMRESPONSE) != old_termresponse) | |
9814 apply_autocmds(EVENT_TERMRESPONSE, NULL, NULL, FALSE, curbuf); | |
9815 # endif | |
9816 } | |
9817 | |
5008
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9818 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9819 is_autocmd_blocked(void) |
5008
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9820 { |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9821 return autocmd_blocked != 0; |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9822 } |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9823 |
7 | 9824 /* |
9825 * Find next autocommand pattern that matches. | |
9826 */ | |
9827 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9828 auto_next_pat( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9829 AutoPatCmd *apc, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9830 int stop_at_last) /* stop when 'last' flag is set */ |
7 | 9831 { |
9832 AutoPat *ap; | |
9833 AutoCmd *cp; | |
9834 char_u *name; | |
9835 char *s; | |
9836 | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
9837 VIM_CLEAR(sourcing_name); |
7 | 9838 |
9839 for (ap = apc->curpat; ap != NULL && !got_int; ap = ap->next) | |
9840 { | |
9841 apc->curpat = NULL; | |
9842 | |
1723 | 9843 /* Only use a pattern when it has not been removed, has commands and |
40 | 9844 * the group matches. For buffer-local autocommands only check the |
9845 * buffer number. */ | |
7 | 9846 if (ap->pat != NULL && ap->cmds != NULL |
9847 && (apc->group == AUGROUP_ALL || apc->group == ap->group)) | |
9848 { | |
40 | 9849 /* execution-condition */ |
9850 if (ap->buflocal_nr == 0 | |
6375 | 9851 ? (match_file_pat(NULL, &ap->reg_prog, apc->fname, |
153 | 9852 apc->sfname, apc->tail, ap->allow_dirs)) |
40 | 9853 : ap->buflocal_nr == apc->arg_bufnr) |
7 | 9854 { |
9855 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
|
9856 s = _("%s Autocommands for \"%s\""); |
7 | 9857 sourcing_name = alloc((unsigned)(STRLEN(s) |
9858 + STRLEN(name) + ap->patlen + 1)); | |
9859 if (sourcing_name != NULL) | |
9860 { | |
9861 sprintf((char *)sourcing_name, s, | |
9862 (char *)name, (char *)ap->pat); | |
9863 if (p_verbose >= 8) | |
292 | 9864 { |
9865 verbose_enter(); | |
273 | 9866 smsg((char_u *)_("Executing %s"), sourcing_name); |
292 | 9867 verbose_leave(); |
9868 } | |
7 | 9869 } |
9870 | |
9871 apc->curpat = ap; | |
9872 apc->nextcmd = ap->cmds; | |
9873 /* mark last command */ | |
9874 for (cp = ap->cmds; cp->next != NULL; cp = cp->next) | |
9875 cp->last = FALSE; | |
9876 cp->last = TRUE; | |
9877 } | |
9878 line_breakcheck(); | |
9879 if (apc->curpat != NULL) /* found a match */ | |
9880 break; | |
9881 } | |
9882 if (stop_at_last && ap->last) | |
9883 break; | |
9884 } | |
9885 } | |
9886 | |
9887 /* | |
9888 * Get next autocommand command. | |
9889 * Called by do_cmdline() to get the next line for ":if". | |
9890 * Returns allocated string, or NULL for end of autocommands. | |
9891 */ | |
3997 | 9892 char_u * |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9893 getnextac(int c UNUSED, void *cookie, int indent UNUSED) |
7 | 9894 { |
9895 AutoPatCmd *acp = (AutoPatCmd *)cookie; | |
9896 char_u *retval; | |
9897 AutoCmd *ac; | |
9898 | |
9899 /* Can be called again after returning the last line. */ | |
9900 if (acp->curpat == NULL) | |
9901 return NULL; | |
9902 | |
9903 /* repeat until we find an autocommand to execute */ | |
9904 for (;;) | |
9905 { | |
9906 /* skip removed commands */ | |
9907 while (acp->nextcmd != NULL && acp->nextcmd->cmd == NULL) | |
9908 if (acp->nextcmd->last) | |
9909 acp->nextcmd = NULL; | |
9910 else | |
9911 acp->nextcmd = acp->nextcmd->next; | |
9912 | |
9913 if (acp->nextcmd != NULL) | |
9914 break; | |
9915 | |
9916 /* at end of commands, find next pattern that matches */ | |
9917 if (acp->curpat->last) | |
9918 acp->curpat = NULL; | |
9919 else | |
9920 acp->curpat = acp->curpat->next; | |
9921 if (acp->curpat != NULL) | |
9922 auto_next_pat(acp, TRUE); | |
9923 if (acp->curpat == NULL) | |
9924 return NULL; | |
9925 } | |
9926 | |
9927 ac = acp->nextcmd; | |
9928 | |
9929 if (p_verbose >= 9) | |
9930 { | |
292 | 9931 verbose_enter_scroll(); |
273 | 9932 smsg((char_u *)_("autocommand %s"), ac->cmd); |
7 | 9933 msg_puts((char_u *)"\n"); /* don't overwrite this either */ |
292 | 9934 verbose_leave_scroll(); |
7 | 9935 } |
9936 retval = vim_strsave(ac->cmd); | |
9937 autocmd_nested = ac->nested; | |
9938 #ifdef FEAT_EVAL | |
14700
0a3b9ecf7cb8
patch 8.1.0362: cannot get the script line number when executing a function
Christian Brabandt <cb@256bit.org>
parents:
14585
diff
changeset
|
9939 current_sctx = ac->script_ctx; |
7 | 9940 #endif |
9941 if (ac->last) | |
9942 acp->nextcmd = NULL; | |
9943 else | |
9944 acp->nextcmd = ac->next; | |
9945 return retval; | |
9946 } | |
9947 | |
9948 /* | |
9949 * Return TRUE if there is a matching autocommand for "fname". | |
40 | 9950 * To account for buffer-local autocommands, function needs to know |
9951 * in which buffer the file will be opened. | |
7 | 9952 */ |
9953 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9954 has_autocmd(event_T event, char_u *sfname, buf_T *buf) |
7 | 9955 { |
9956 AutoPat *ap; | |
9957 char_u *fname; | |
9958 char_u *tail = gettail(sfname); | |
9959 int retval = FALSE; | |
9960 | |
9961 fname = FullName_save(sfname, FALSE); | |
9962 if (fname == NULL) | |
9963 return FALSE; | |
9964 | |
9965 #ifdef BACKSLASH_IN_FILENAME | |
9966 /* | |
9967 * Replace all backslashes with forward slashes. This makes the | |
9968 * autocommand patterns portable between Unix and MS-DOS. | |
9969 */ | |
9970 sfname = vim_strsave(sfname); | |
9971 if (sfname != NULL) | |
9972 forward_slash(sfname); | |
9973 forward_slash(fname); | |
9974 #endif | |
9975 | |
9976 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
9977 if (ap->pat != NULL && ap->cmds != NULL | |
856 | 9978 && (ap->buflocal_nr == 0 |
6375 | 9979 ? match_file_pat(NULL, &ap->reg_prog, |
153 | 9980 fname, sfname, tail, ap->allow_dirs) |
856 | 9981 : buf != NULL && ap->buflocal_nr == buf->b_fnum |
40 | 9982 )) |
7 | 9983 { |
9984 retval = TRUE; | |
9985 break; | |
9986 } | |
9987 | |
9988 vim_free(fname); | |
9989 #ifdef BACKSLASH_IN_FILENAME | |
9990 vim_free(sfname); | |
9991 #endif | |
9992 | |
9993 return retval; | |
9994 } | |
9995 | |
9996 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
9997 /* | |
9998 * Function given to ExpandGeneric() to obtain the list of autocommand group | |
9999 * names. | |
10000 */ | |
10001 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10002 get_augroup_name(expand_T *xp UNUSED, int idx) |
7 | 10003 { |
10004 if (idx == augroups.ga_len) /* add "END" add the end */ | |
10005 return (char_u *)"END"; | |
10006 if (idx >= augroups.ga_len) /* end of list */ | |
10007 return NULL; | |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
10008 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
|
10009 /* skip deleted entries */ |
7 | 10010 return (char_u *)""; |
10011 return AUGROUP_NAME(idx); /* return a name */ | |
10012 } | |
10013 | |
10014 static int include_groups = FALSE; | |
10015 | |
10016 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10017 set_context_in_autocmd( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10018 expand_T *xp, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10019 char_u *arg, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10020 int doautocmd) /* TRUE for :doauto*, FALSE for :autocmd */ |
7 | 10021 { |
10022 char_u *p; | |
10023 int group; | |
10024 | |
10025 /* check for a group name, skip it if present */ | |
10026 include_groups = FALSE; | |
10027 p = arg; | |
10028 group = au_get_grouparg(&arg); | |
10029 if (group == AUGROUP_ERROR) | |
10030 return NULL; | |
10031 /* 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
|
10032 if (*arg == NUL && group != AUGROUP_ALL && !VIM_ISWHITE(arg[-1])) |
7 | 10033 { |
10034 arg = p; | |
10035 group = AUGROUP_ALL; | |
10036 } | |
10037 | |
10038 /* 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
|
10039 for (p = arg; *p != NUL && !VIM_ISWHITE(*p); ++p) |
7 | 10040 if (*p == ',') |
10041 arg = p + 1; | |
10042 if (*p == NUL) | |
10043 { | |
10044 if (group == AUGROUP_ALL) | |
10045 include_groups = TRUE; | |
10046 xp->xp_context = EXPAND_EVENTS; /* expand event name */ | |
10047 xp->xp_pattern = arg; | |
10048 return NULL; | |
10049 } | |
10050 | |
10051 /* skip over pattern */ | |
10052 arg = skipwhite(p); | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
10053 while (*arg && (!VIM_ISWHITE(*arg) || arg[-1] == '\\')) |
7 | 10054 arg++; |
10055 if (*arg) | |
10056 return arg; /* expand (next) command */ | |
10057 | |
10058 if (doautocmd) | |
10059 xp->xp_context = EXPAND_FILES; /* expand file names */ | |
10060 else | |
10061 xp->xp_context = EXPAND_NOTHING; /* pattern is not expanded */ | |
10062 return NULL; | |
10063 } | |
10064 | |
10065 /* | |
10066 * Function given to ExpandGeneric() to obtain the list of event names. | |
10067 */ | |
10068 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10069 get_event_name(expand_T *xp UNUSED, int idx) |
7 | 10070 { |
10071 if (idx < augroups.ga_len) /* First list group names, if wanted */ | |
10072 { | |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
10073 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
|
10074 || AUGROUP_NAME(idx) == get_deleted_augroup()) |
7 | 10075 return (char_u *)""; /* skip deleted entries */ |
10076 return AUGROUP_NAME(idx); /* return a name */ | |
10077 } | |
10078 return (char_u *)event_names[idx - augroups.ga_len].name; | |
10079 } | |
10080 | |
10081 #endif /* FEAT_CMDL_COMPL */ | |
10082 | |
10083 /* | |
615 | 10084 * Return TRUE if autocmd is supported. |
10085 */ | |
10086 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10087 autocmd_supported(char_u *name) |
615 | 10088 { |
10089 char_u *p; | |
10090 | |
10091 return (event_name2nr(name, &p) != NUM_EVENTS); | |
10092 } | |
10093 | |
10094 /* | |
612 | 10095 * Return TRUE if an autocommand is defined for a group, event and |
10096 * pattern: The group can be omitted to accept any group. "event" and "pattern" | |
10097 * can be NULL to accept any event and pattern. "pattern" can be NULL to accept | |
10098 * any pattern. Buffer-local patterns <buffer> or <buffer=N> are accepted. | |
10099 * Used for: | |
10100 * exists("#Group") or | |
10101 * exists("#Group#Event") or | |
10102 * exists("#Group#Event#pat") or | |
10103 * exists("#Event") or | |
10104 * exists("#Event#pat") | |
7 | 10105 */ |
10106 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10107 au_exists(char_u *arg) |
7 | 10108 { |
612 | 10109 char_u *arg_save; |
10110 char_u *pattern = NULL; | |
7 | 10111 char_u *event_name; |
10112 char_u *p; | |
661 | 10113 event_T event; |
7 | 10114 AutoPat *ap; |
40 | 10115 buf_T *buflocal_buf = NULL; |
612 | 10116 int group; |
10117 int retval = FALSE; | |
10118 | |
615 | 10119 /* Make a copy so that we can change the '#' chars to a NUL. */ |
612 | 10120 arg_save = vim_strsave(arg); |
10121 if (arg_save == NULL) | |
10122 return FALSE; | |
615 | 10123 p = vim_strchr(arg_save, '#'); |
612 | 10124 if (p != NULL) |
10125 *p++ = NUL; | |
10126 | |
10127 /* First, look for an autocmd group name */ | |
10128 group = au_find_group(arg_save); | |
10129 if (group == AUGROUP_ERROR) | |
10130 { | |
10131 /* Didn't match a group name, assume the first argument is an event. */ | |
10132 group = AUGROUP_ALL; | |
10133 event_name = arg_save; | |
10134 } | |
10135 else | |
10136 { | |
10137 if (p == NULL) | |
10138 { | |
10139 /* "Group": group name is present and it's recognized */ | |
10140 retval = TRUE; | |
10141 goto theend; | |
10142 } | |
10143 | |
10144 /* Must be "Group#Event" or "Group#Event#pat". */ | |
10145 event_name = p; | |
10146 p = vim_strchr(event_name, '#'); | |
10147 if (p != NULL) | |
10148 *p++ = NUL; /* "Group#Event#pat" */ | |
10149 } | |
10150 | |
10151 pattern = p; /* "pattern" is NULL when there is no pattern */ | |
7 | 10152 |
10153 /* find the index (enum) for the event name */ | |
10154 event = event_name2nr(event_name, &p); | |
10155 | |
10156 /* return FALSE if the event name is not recognized */ | |
612 | 10157 if (event == NUM_EVENTS) |
10158 goto theend; | |
7 | 10159 |
10160 /* Find the first autocommand for this event. | |
10161 * If there isn't any, return FALSE; | |
10162 * If there is one and no pattern given, return TRUE; */ | |
10163 ap = first_autopat[(int)event]; | |
10164 if (ap == NULL) | |
612 | 10165 goto theend; |
7 | 10166 |
40 | 10167 /* if pattern is "<buffer>", special handling is needed which uses curbuf */ |
10168 /* for pattern "<buffer=N>, fnamecmp() will work fine */ | |
1962 | 10169 if (pattern != NULL && STRICMP(pattern, "<buffer>") == 0) |
40 | 10170 buflocal_buf = curbuf; |
10171 | |
7 | 10172 /* Check if there is an autocommand with the given pattern. */ |
10173 for ( ; ap != NULL; ap = ap->next) | |
40 | 10174 /* only use a pattern when it has not been removed and has commands. */ |
10175 /* For buffer-local autocommands, fnamecmp() works fine. */ | |
7 | 10176 if (ap->pat != NULL && ap->cmds != NULL |
612 | 10177 && (group == AUGROUP_ALL || ap->group == group) |
1962 | 10178 && (pattern == NULL |
10179 || (buflocal_buf == NULL | |
10180 ? fnamecmp(ap->pat, pattern) == 0 | |
10181 : ap->buflocal_nr == buflocal_buf->b_fnum))) | |
612 | 10182 { |
10183 retval = TRUE; | |
10184 break; | |
10185 } | |
10186 | |
10187 theend: | |
10188 vim_free(arg_save); | |
10189 return retval; | |
7 | 10190 } |
40 | 10191 |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
10192 |
7 | 10193 /* |
153 | 10194 * Try matching a filename with a "pattern" ("prog" is NULL), or use the |
10195 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling | |
10196 * vim_regcomp() often. | |
7 | 10197 * Used for autocommands and 'wildignore'. |
10198 * Returns TRUE if there is a match, FALSE otherwise. | |
10199 */ | |
6375 | 10200 static int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10201 match_file_pat( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10202 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
|
10203 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
|
10204 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
|
10205 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
|
10206 char_u *tail, /* tail of path */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10207 int allow_dirs) /* allow matching with dir */ |
7 | 10208 { |
10209 regmatch_T regmatch; | |
10210 int result = FALSE; | |
10211 | |
4242 | 10212 regmatch.rm_ic = p_fic; /* ignore case if 'fileignorecase' is set */ |
6470 | 10213 if (prog != NULL) |
10214 regmatch.regprog = *prog; | |
7 | 10215 else |
6470 | 10216 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC); |
7 | 10217 |
10218 /* | |
10219 * Try for a match with the pattern with: | |
10220 * 1. the full file name, when the pattern has a '/'. | |
10221 * 2. the short file name, when the pattern has a '/'. | |
10222 * 3. the tail of the file name, when the pattern has no '/'. | |
10223 */ | |
6470 | 10224 if (regmatch.regprog != NULL |
7 | 10225 && ((allow_dirs |
10226 && (vim_regexec(®match, fname, (colnr_T)0) | |
10227 || (sfname != NULL | |
10228 && vim_regexec(®match, sfname, (colnr_T)0)))) | |
6470 | 10229 || (!allow_dirs && vim_regexec(®match, tail, (colnr_T)0)))) |
7 | 10230 result = TRUE; |
10231 | |
6375 | 10232 if (prog != NULL) |
10233 *prog = regmatch.regprog; | |
10234 else | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4354
diff
changeset
|
10235 vim_regfree(regmatch.regprog); |
7 | 10236 return result; |
10237 } | |
10238 | |
10239 #if defined(FEAT_WILDIGN) || defined(PROTO) | |
10240 /* | |
10241 * Return TRUE if a file matches with a pattern in "list". | |
10242 * "list" is a comma-separated list of patterns, like 'wildignore'. | |
10243 * "sfname" is the short file name or NULL, "ffname" the long file name. | |
10244 */ | |
10245 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10246 match_file_list(char_u *list, char_u *sfname, char_u *ffname) |
7 | 10247 { |
10248 char_u buf[100]; | |
10249 char_u *tail; | |
10250 char_u *regpat; | |
10251 char allow_dirs; | |
10252 int match; | |
10253 char_u *p; | |
10254 | |
10255 tail = gettail(sfname); | |
10256 | |
10257 /* try all patterns in 'wildignore' */ | |
10258 p = list; | |
10259 while (*p) | |
10260 { | |
10261 copy_option_part(&p, buf, 100, ","); | |
10262 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE); | |
10263 if (regpat == NULL) | |
10264 break; | |
153 | 10265 match = match_file_pat(regpat, NULL, ffname, sfname, |
10266 tail, (int)allow_dirs); | |
7 | 10267 vim_free(regpat); |
10268 if (match) | |
10269 return TRUE; | |
10270 } | |
10271 return FALSE; | |
10272 } | |
10273 #endif | |
10274 | |
10275 /* | |
10276 * Convert the given pattern "pat" which has shell style wildcards in it, into | |
10277 * a regular expression, and return the result in allocated memory. If there | |
10278 * is a directory path separator to be matched, then TRUE is put in | |
10279 * allow_dirs, otherwise FALSE is put there -- webb. | |
10280 * Handle backslashes before special characters, like "\*" and "\ ". | |
10281 * | |
10282 * Returns NULL when out of memory. | |
10283 */ | |
10284 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10285 file_pat_to_reg_pat( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10286 char_u *pat, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10287 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
|
10288 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
|
10289 int no_bslash UNUSED) /* Don't use a backward slash as pathsep */ |
7 | 10290 { |
6470 | 10291 int size = 2; /* '^' at start, '$' at end */ |
7 | 10292 char_u *endp; |
10293 char_u *reg_pat; | |
10294 char_u *p; | |
10295 int i; | |
10296 int nested = 0; | |
10297 int add_dollar = TRUE; | |
10298 | |
10299 if (allow_dirs != NULL) | |
10300 *allow_dirs = FALSE; | |
10301 if (pat_end == NULL) | |
10302 pat_end = pat + STRLEN(pat); | |
10303 | |
10304 for (p = pat; p < pat_end; p++) | |
10305 { | |
10306 switch (*p) | |
10307 { | |
10308 case '*': | |
10309 case '.': | |
10310 case ',': | |
10311 case '{': | |
10312 case '}': | |
10313 case '~': | |
10314 size += 2; /* extra backslash */ | |
10315 break; | |
10316 #ifdef BACKSLASH_IN_FILENAME | |
10317 case '\\': | |
10318 case '/': | |
10319 size += 4; /* could become "[\/]" */ | |
10320 break; | |
10321 #endif | |
10322 default: | |
10323 size++; | |
6999 | 10324 # ifdef FEAT_MBYTE |
474 | 10325 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 10326 { |
10327 ++p; | |
10328 ++size; | |
10329 } | |
10330 # endif | |
10331 break; | |
10332 } | |
10333 } | |
10334 reg_pat = alloc(size + 1); | |
10335 if (reg_pat == NULL) | |
10336 return NULL; | |
10337 | |
10338 i = 0; | |
10339 | |
10340 if (pat[0] == '*') | |
10341 while (pat[0] == '*' && pat < pat_end - 1) | |
10342 pat++; | |
10343 else | |
10344 reg_pat[i++] = '^'; | |
10345 endp = pat_end - 1; | |
7005 | 10346 if (endp >= pat && *endp == '*') |
7 | 10347 { |
10348 while (endp - pat > 0 && *endp == '*') | |
10349 endp--; | |
10350 add_dollar = FALSE; | |
10351 } | |
10352 for (p = pat; *p && nested >= 0 && p <= endp; p++) | |
10353 { | |
10354 switch (*p) | |
10355 { | |
10356 case '*': | |
10357 reg_pat[i++] = '.'; | |
10358 reg_pat[i++] = '*'; | |
444 | 10359 while (p[1] == '*') /* "**" matches like "*" */ |
10360 ++p; | |
7 | 10361 break; |
10362 case '.': | |
10363 case '~': | |
10364 reg_pat[i++] = '\\'; | |
10365 reg_pat[i++] = *p; | |
10366 break; | |
10367 case '?': | |
10368 reg_pat[i++] = '.'; | |
10369 break; | |
10370 case '\\': | |
10371 if (p[1] == NUL) | |
10372 break; | |
10373 #ifdef BACKSLASH_IN_FILENAME | |
10374 if (!no_bslash) | |
10375 { | |
10376 /* translate: | |
10377 * "\x" to "\\x" e.g., "dir\file" | |
10378 * "\*" to "\\.*" e.g., "dir\*.c" | |
10379 * "\?" to "\\." e.g., "dir\??.c" | |
10380 * "\+" to "\+" e.g., "fileX\+.c" | |
10381 */ | |
10382 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?') | |
10383 && p[1] != '+') | |
10384 { | |
10385 reg_pat[i++] = '['; | |
10386 reg_pat[i++] = '\\'; | |
10387 reg_pat[i++] = '/'; | |
10388 reg_pat[i++] = ']'; | |
10389 if (allow_dirs != NULL) | |
10390 *allow_dirs = TRUE; | |
10391 break; | |
10392 } | |
10393 } | |
10394 #endif | |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10395 /* Undo escaping from ExpandEscape(): |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10396 * foo\?bar -> foo?bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10397 * foo\%bar -> foo%bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10398 * foo\,bar -> foo,bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10399 * foo\ bar -> foo bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10400 * 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
|
10401 * regexp. |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
5008
diff
changeset
|
10402 * 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
|
10403 * 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
|
10404 */ |
7 | 10405 if (*++p == '?' |
10406 #ifdef BACKSLASH_IN_FILENAME | |
10407 && no_bslash | |
10408 #endif | |
10409 ) | |
10410 reg_pat[i++] = '?'; | |
10411 else | |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
5008
diff
changeset
|
10412 if (*p == ',' || *p == '%' || *p == '#' |
6999 | 10413 || vim_isspace(*p) || *p == '{' || *p == '}') |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10414 reg_pat[i++] = *p; |
5259
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10415 else if (*p == '\\' && p[1] == '\\' && p[2] == '{') |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10416 { |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10417 reg_pat[i++] = '\\'; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10418 reg_pat[i++] = '{'; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10419 p += 2; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10420 } |
7 | 10421 else |
10422 { | |
10423 if (allow_dirs != NULL && vim_ispathsep(*p) | |
10424 #ifdef BACKSLASH_IN_FILENAME | |
10425 && (!no_bslash || *p != '\\') | |
10426 #endif | |
10427 ) | |
10428 *allow_dirs = TRUE; | |
10429 reg_pat[i++] = '\\'; | |
10430 reg_pat[i++] = *p; | |
10431 } | |
10432 break; | |
10433 #ifdef BACKSLASH_IN_FILENAME | |
10434 case '/': | |
10435 reg_pat[i++] = '['; | |
10436 reg_pat[i++] = '\\'; | |
10437 reg_pat[i++] = '/'; | |
10438 reg_pat[i++] = ']'; | |
10439 if (allow_dirs != NULL) | |
10440 *allow_dirs = TRUE; | |
10441 break; | |
10442 #endif | |
10443 case '{': | |
10444 reg_pat[i++] = '\\'; | |
10445 reg_pat[i++] = '('; | |
10446 nested++; | |
10447 break; | |
10448 case '}': | |
10449 reg_pat[i++] = '\\'; | |
10450 reg_pat[i++] = ')'; | |
10451 --nested; | |
10452 break; | |
10453 case ',': | |
10454 if (nested) | |
10455 { | |
10456 reg_pat[i++] = '\\'; | |
10457 reg_pat[i++] = '|'; | |
10458 } | |
10459 else | |
10460 reg_pat[i++] = ','; | |
10461 break; | |
10462 default: | |
10463 # ifdef FEAT_MBYTE | |
474 | 10464 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 10465 reg_pat[i++] = *p++; |
10466 else | |
10467 # endif | |
10468 if (allow_dirs != NULL && vim_ispathsep(*p)) | |
10469 *allow_dirs = TRUE; | |
10470 reg_pat[i++] = *p; | |
10471 break; | |
10472 } | |
10473 } | |
10474 if (add_dollar) | |
10475 reg_pat[i++] = '$'; | |
10476 reg_pat[i] = NUL; | |
10477 if (nested != 0) | |
10478 { | |
10479 if (nested < 0) | |
10480 EMSG(_("E219: Missing {.")); | |
10481 else | |
10482 EMSG(_("E220: Missing }.")); | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
10483 VIM_CLEAR(reg_pat); |
7 | 10484 } |
10485 return reg_pat; | |
10486 } | |
2664 | 10487 |
10488 #if defined(EINTR) || defined(PROTO) | |
10489 /* | |
10490 * Version of read() that retries when interrupted by EINTR (possibly | |
10491 * by a SIGWINCH). | |
10492 */ | |
10493 long | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10494 read_eintr(int fd, void *buf, size_t bufsize) |
2664 | 10495 { |
10496 long ret; | |
10497 | |
10498 for (;;) | |
10499 { | |
10500 ret = vim_read(fd, buf, bufsize); | |
10501 if (ret >= 0 || errno != EINTR) | |
10502 break; | |
10503 } | |
10504 return ret; | |
10505 } | |
10506 | |
10507 /* | |
10508 * Version of write() that retries when interrupted by EINTR (possibly | |
10509 * by a SIGWINCH). | |
10510 */ | |
10511 long | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10512 write_eintr(int fd, void *buf, size_t bufsize) |
2664 | 10513 { |
10514 long ret = 0; | |
10515 long wlen; | |
10516 | |
10517 /* Repeat the write() so long it didn't fail, other than being interrupted | |
10518 * by a signal. */ | |
10519 while (ret < (long)bufsize) | |
10520 { | |
2666 | 10521 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret); |
2664 | 10522 if (wlen < 0) |
10523 { | |
10524 if (errno != EINTR) | |
10525 break; | |
10526 } | |
10527 else | |
10528 ret += wlen; | |
10529 } | |
10530 return ret; | |
10531 } | |
10532 #endif |