Mercurial > vim
annotate src/fileio.c @ 14816:d823dfb273c6 v8.1.0420
patch 8.1.0420: generating vim.lib when using ActivePerl 5.20.3 or later
commit https://github.com/vim/vim/commit/1f402806b88f4187e17c0105b4604f425f41dd1c
Author: Bram Moolenaar <Bram@vim.org>
Date: Fri Sep 21 14:01:27 2018 +0200
patch 8.1.0420: generating vim.lib when using ActivePerl 5.20.3 or later
Problem: Generating vim.lib when using ActivePerl 5.20.3 or later.
Solution: Redefine XS_EXTERNAL(). (Ken Takata, closes https://github.com/vim/vim/issues/3462)
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Fri, 21 Sep 2018 14:15:06 +0200 |
parents | 27055ad9276b |
children | 27b9a84395b5 |
rev | line source |
---|---|
10042
4aead6a9b7a9
commit https://github.com/vim/vim/commit/edf3f97ae2af024708ebb4ac614227327033ca47
Christian Brabandt <cb@256bit.org>
parents:
9911
diff
changeset
|
1 /* vi:set ts=8 sts=4 sw=4 noet: |
7 | 2 * |
3 * VIM - Vi IMproved by Bram Moolenaar | |
4 * | |
5 * Do ":help uganda" in Vim to read copying and usage conditions. | |
6 * Do ":help credits" in Vim to see a list of people who contributed. | |
7 * See README.txt for an overview of the Vim source code. | |
8 */ | |
9 | |
10 /* | |
11 * fileio.c: read from and write to a file | |
12 */ | |
13 | |
14 #include "vim.h" | |
15 | |
2014 | 16 #if defined(__TANDEM) || defined(__MINT__) |
7 | 17 # include <limits.h> /* for SSIZE_MAX */ |
18 #endif | |
19 | |
20 #if defined(HAVE_UTIME) && defined(HAVE_UTIME_H) | |
21 # include <utime.h> /* for struct utimbuf */ | |
22 #endif | |
23 | |
24 #define BUFSIZE 8192 /* size of normal write buffer */ | |
25 #define SMBUFSIZE 256 /* size of emergency write buffer */ | |
26 | |
27 /* Is there any system that doesn't have access()? */ | |
574 | 28 #define USE_MCH_ACCESS |
7 | 29 |
30 #ifdef FEAT_MBYTE | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
31 static char_u *next_fenc(char_u **pp); |
7 | 32 # ifdef FEAT_EVAL |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
33 static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp); |
7 | 34 # endif |
35 #endif | |
36 #ifdef FEAT_VIMINFO | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
37 static void check_marks_read(void); |
7 | 38 #endif |
39 #ifdef FEAT_CRYPT | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
40 static char_u *check_for_cryptkey(char_u *cryptkey, char_u *ptr, long *sizep, off_T *filesizep, int newfile, char_u *fname, int *did_ask); |
7 | 41 #endif |
42 #ifdef UNIX | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
43 static void set_file_time(char_u *fname, time_t atime, time_t mtime); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
44 #endif |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
45 static int set_rw_fname(char_u *fname, char_u *sfname); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
46 static int msg_add_fileformat(int eol_type); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
47 static void msg_add_eol(void); |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
48 static int check_mtime(buf_T *buf, stat_T *s); |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
49 static int time_differs(long t1, long t2); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
50 static int apply_autocmds_exarg(event_T event, char_u *fname, char_u *fname_io, int force, buf_T *buf, exarg_T *eap); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
51 static int au_find_group(char_u *name); |
676 | 52 |
53 # define AUGROUP_DEFAULT -1 /* default autocmd group */ | |
1834 | 54 # define AUGROUP_ERROR -2 /* erroneous autocmd group */ |
676 | 55 # define AUGROUP_ALL -3 /* all autocmd groups */ |
7 | 56 |
57 #if defined(FEAT_CRYPT) || defined(FEAT_MBYTE) | |
58 # define HAS_BW_FLAGS | |
59 # define FIO_LATIN1 0x01 /* convert Latin1 */ | |
60 # define FIO_UTF8 0x02 /* convert UTF-8 */ | |
61 # define FIO_UCS2 0x04 /* convert UCS-2 */ | |
62 # define FIO_UCS4 0x08 /* convert UCS-4 */ | |
63 # define FIO_UTF16 0x10 /* convert UTF-16 */ | |
64 # ifdef WIN3264 | |
65 # define FIO_CODEPAGE 0x20 /* convert MS-Windows codepage */ | |
66 # define FIO_PUT_CP(x) (((x) & 0xffff) << 16) /* put codepage in top word */ | |
67 # define FIO_GET_CP(x) (((x)>>16) & 0xffff) /* get codepage from top word */ | |
68 # endif | |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
69 # ifdef MACOS_CONVERT |
7 | 70 # define FIO_MACROMAN 0x20 /* convert MacRoman */ |
71 # endif | |
72 # define FIO_ENDIAN_L 0x80 /* little endian */ | |
73 # define FIO_ENCRYPTED 0x1000 /* encrypt written bytes */ | |
74 # define FIO_NOCONVERT 0x2000 /* skip encoding conversion */ | |
75 # define FIO_UCSBOM 0x4000 /* check for BOM at start of file */ | |
76 # define FIO_ALL -1 /* allow all formats */ | |
77 #endif | |
78 | |
79 /* When converting, a read() or write() may leave some bytes to be converted | |
80 * for the next call. The value is guessed... */ | |
81 #define CONV_RESTLEN 30 | |
82 | |
83 /* We have to guess how much a sequence of bytes may expand when converting | |
84 * with iconv() to be able to allocate a buffer. */ | |
85 #define ICONV_MULT 8 | |
86 | |
87 /* | |
88 * Structure to pass arguments from buf_write() to buf_write_bytes(). | |
89 */ | |
90 struct bw_info | |
91 { | |
92 int bw_fd; /* file descriptor */ | |
93 char_u *bw_buf; /* buffer with data to be written */ | |
1411 | 94 int bw_len; /* length of data */ |
7 | 95 #ifdef HAS_BW_FLAGS |
96 int bw_flags; /* FIO_ flags */ | |
97 #endif | |
6122 | 98 #ifdef FEAT_CRYPT |
99 buf_T *bw_buffer; /* buffer being written */ | |
100 #endif | |
7 | 101 #ifdef FEAT_MBYTE |
102 char_u bw_rest[CONV_RESTLEN]; /* not converted bytes */ | |
103 int bw_restlen; /* nr of bytes in bw_rest[] */ | |
104 int bw_first; /* first write call */ | |
105 char_u *bw_conv_buf; /* buffer for writing converted chars */ | |
106 int bw_conv_buflen; /* size of bw_conv_buf */ | |
107 int bw_conv_error; /* set for conversion error */ | |
1947 | 108 linenr_T bw_conv_error_lnum; /* first line with error or zero */ |
109 linenr_T bw_start_lnum; /* line number at start of buffer */ | |
7 | 110 # ifdef USE_ICONV |
111 iconv_t bw_iconv_fd; /* descriptor for iconv() or -1 */ | |
112 # endif | |
113 #endif | |
114 }; | |
115 | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
116 static int buf_write_bytes(struct bw_info *ip); |
7 | 117 |
118 #ifdef FEAT_MBYTE | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
119 static linenr_T readfile_linenr(linenr_T linecnt, char_u *p, char_u *endp); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
120 static int ucs2bytes(unsigned c, char_u **pp, int flags); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
121 static int need_conversion(char_u *fenc); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
122 static int get_fio_flags(char_u *ptr); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
123 static char_u *check_for_bom(char_u *p, long size, int *lenp, int flags); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
124 static int make_bom(char_u *buf, char_u *name); |
7 | 125 # ifdef WIN3264 |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
126 static int get_win_fio_flags(char_u *ptr); |
7 | 127 # endif |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
128 # ifdef MACOS_CONVERT |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
129 static int get_mac_fio_flags(char_u *ptr); |
7 | 130 # endif |
131 #endif | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
132 static int move_lines(buf_T *frombuf, buf_T *tobuf); |
2006 | 133 #ifdef TEMPDIRNAMES |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
134 static void vim_settempdir(char_u *tempdir); |
2006 | 135 #endif |
1834 | 136 static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name"); |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
137 |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
138 /* |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
139 * Set by the apply_autocmds_group function if the given event is equal to |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
140 * EVENT_FILETYPE. Used by the readfile function in order to determine if |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
141 * EVENT_BUFREADPOST triggered the EVENT_FILETYPE. |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
142 * |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
143 * Relying on this value requires one to reset it prior calling |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
144 * apply_autocmds_group. |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
145 */ |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
146 static int au_did_filetype INIT(= FALSE); |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
147 |
7 | 148 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
149 filemess( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
150 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
151 char_u *name, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
152 char_u *s, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
153 int attr) |
7 | 154 { |
155 int msg_scroll_save; | |
156 | |
157 if (msg_silent != 0) | |
158 return; | |
159 msg_add_fname(buf, name); /* put file name in IObuff with quotes */ | |
160 /* If it's extremely long, truncate it. */ | |
161 if (STRLEN(IObuff) > IOSIZE - 80) | |
162 IObuff[IOSIZE - 80] = NUL; | |
163 STRCAT(IObuff, s); | |
164 /* | |
165 * For the first message may have to start a new line. | |
166 * For further ones overwrite the previous one, reset msg_scroll before | |
167 * calling filemess(). | |
168 */ | |
169 msg_scroll_save = msg_scroll; | |
170 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) | |
171 msg_scroll = FALSE; | |
172 if (!msg_scroll) /* wait a bit when overwriting an error msg */ | |
173 check_for_delay(FALSE); | |
174 msg_start(); | |
175 msg_scroll = msg_scroll_save; | |
176 msg_scrolled_ign = TRUE; | |
177 /* may truncate the message to avoid a hit-return prompt */ | |
178 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr); | |
179 msg_clr_eos(); | |
180 out_flush(); | |
181 msg_scrolled_ign = FALSE; | |
182 } | |
183 | |
184 /* | |
185 * Read lines from file "fname" into the buffer after line "from". | |
186 * | |
187 * 1. We allocate blocks with lalloc, as big as possible. | |
188 * 2. Each block is filled with characters from the file with a single read(). | |
189 * 3. The lines are inserted in the buffer with ml_append(). | |
190 * | |
191 * (caller must check that fname != NULL, unless READ_STDIN is used) | |
192 * | |
193 * "lines_to_skip" is the number of lines that must be skipped | |
194 * "lines_to_read" is the number of lines that are appended | |
195 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM. | |
196 * | |
197 * flags: | |
198 * READ_NEW starting to edit a new buffer | |
199 * READ_FILTER reading filter output | |
200 * READ_STDIN read from stdin instead of a file | |
201 * READ_BUFFER read from curbuf instead of a file (converting after reading | |
202 * stdin) | |
203 * READ_DUMMY read into a dummy buffer (to check if file contents changed) | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
204 * READ_KEEP_UNDO don't clear undo info or read it from a file |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
205 * READ_FIFO read from fifo/socket instead of a file |
7 | 206 * |
10575
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
207 * return FAIL for failure, NOTDONE for directory (failure), or OK |
7 | 208 */ |
209 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
210 readfile( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
211 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
212 char_u *sfname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
213 linenr_T from, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
214 linenr_T lines_to_skip, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
215 linenr_T lines_to_read, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
216 exarg_T *eap, /* can be NULL! */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
217 int flags) |
7 | 218 { |
219 int fd = 0; | |
220 int newfile = (flags & READ_NEW); | |
221 int check_readonly; | |
222 int filtering = (flags & READ_FILTER); | |
223 int read_stdin = (flags & READ_STDIN); | |
224 int read_buffer = (flags & READ_BUFFER); | |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
225 int read_fifo = (flags & READ_FIFO); |
1486 | 226 int set_options = newfile || read_buffer |
227 || (eap != NULL && eap->read_edit); | |
7 | 228 linenr_T read_buf_lnum = 1; /* next line to read from curbuf */ |
229 colnr_T read_buf_col = 0; /* next char to read from this line */ | |
230 char_u c; | |
231 linenr_T lnum = from; | |
232 char_u *ptr = NULL; /* pointer into read buffer */ | |
233 char_u *buffer = NULL; /* read buffer */ | |
234 char_u *new_buffer = NULL; /* init to shut up gcc */ | |
235 char_u *line_start = NULL; /* init to shut up gcc */ | |
236 int wasempty; /* buffer was empty before reading */ | |
237 colnr_T len; | |
238 long size = 0; | |
239 char_u *p; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
240 off_T filesize = 0; |
7 | 241 int skip_read = FALSE; |
242 #ifdef FEAT_CRYPT | |
243 char_u *cryptkey = NULL; | |
2204
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
244 int did_ask_for_key = FALSE; |
7 | 245 #endif |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
246 #ifdef FEAT_PERSISTENT_UNDO |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
247 context_sha256_T sha_ctx; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
248 int read_undo_file = FALSE; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
249 #endif |
7 | 250 int split = 0; /* number of split lines */ |
251 #define UNKNOWN 0x0fffffff /* file size is unknown */ | |
252 linenr_T linecnt; | |
253 int error = FALSE; /* errors encountered */ | |
254 int ff_error = EOL_UNKNOWN; /* file format with errors */ | |
255 long linerest = 0; /* remaining chars in line */ | |
256 #ifdef UNIX | |
257 int perm = 0; | |
258 int swap_mode = -1; /* protection bits for swap file */ | |
259 #else | |
260 int perm; | |
261 #endif | |
262 int fileformat = 0; /* end-of-line format */ | |
263 int keep_fileformat = FALSE; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
264 stat_T st; |
7 | 265 int file_readonly; |
266 linenr_T skip_count = 0; | |
267 linenr_T read_count = 0; | |
268 int msg_save = msg_scroll; | |
269 linenr_T read_no_eol_lnum = 0; /* non-zero lnum when last line of | |
270 * last read was missing the eol */ | |
10668
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
271 int try_mac; |
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
272 int try_dos; |
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
273 int try_unix; |
7 | 274 int file_rewind = FALSE; |
275 #ifdef FEAT_MBYTE | |
276 int can_retry; | |
595 | 277 linenr_T conv_error = 0; /* line nr with conversion error */ |
278 linenr_T illegal_byte = 0; /* line nr with illegal byte */ | |
7 | 279 int keep_dest_enc = FALSE; /* don't retry when char doesn't fit |
280 in destination encoding */ | |
595 | 281 int bad_char_behavior = BAD_REPLACE; |
282 /* BAD_KEEP, BAD_DROP or character to | |
283 * replace with */ | |
7 | 284 char_u *tmpname = NULL; /* name of 'charconvert' output file */ |
285 int fio_flags = 0; | |
286 char_u *fenc; /* fileencoding to use */ | |
287 int fenc_alloced; /* fenc_next is in allocated memory */ | |
288 char_u *fenc_next = NULL; /* next item in 'fencs' or NULL */ | |
289 int advance_fenc = FALSE; | |
290 long real_size = 0; | |
291 # ifdef USE_ICONV | |
292 iconv_t iconv_fd = (iconv_t)-1; /* descriptor for iconv() or -1 */ | |
293 # ifdef FEAT_EVAL | |
294 int did_iconv = FALSE; /* TRUE when iconv() failed and trying | |
295 'charconvert' next */ | |
296 # endif | |
297 # endif | |
298 int converted = FALSE; /* TRUE if conversion done */ | |
299 int notconverted = FALSE; /* TRUE if conversion wanted but it | |
300 wasn't possible */ | |
301 char_u conv_rest[CONV_RESTLEN]; | |
302 int conv_restlen = 0; /* nr of bytes in conv_rest[] */ | |
303 #endif | |
2563
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
304 buf_T *old_curbuf; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
305 char_u *old_b_ffname; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
306 char_u *old_b_fname; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
307 int using_b_ffname; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
308 int using_b_fname; |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
309 |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
310 au_did_filetype = FALSE; /* reset before triggering any autocommands */ |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
311 |
2707 | 312 curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */ |
7 | 313 |
314 /* | |
315 * If there is no file name yet, use the one for the read file. | |
316 * BF_NOTEDITED is set to reflect this. | |
317 * Don't do this for a read from a filter. | |
318 * Only do this when 'cpoptions' contains the 'f' flag. | |
319 */ | |
320 if (curbuf->b_ffname == NULL | |
321 && !filtering | |
322 && fname != NULL | |
323 && vim_strchr(p_cpo, CPO_FNAMER) != NULL | |
324 && !(flags & READ_DUMMY)) | |
325 { | |
633 | 326 if (set_rw_fname(fname, sfname) == FAIL) |
327 return FAIL; | |
7 | 328 } |
329 | |
2563
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
330 /* Remember the initial values of curbuf, curbuf->b_ffname and |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
331 * curbuf->b_fname to detect whether they are altered as a result of |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
332 * executing nasty autocommands. Also check if "fname" and "sfname" |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
333 * point to one of these values. */ |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
334 old_curbuf = curbuf; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
335 old_b_ffname = curbuf->b_ffname; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
336 old_b_fname = curbuf->b_fname; |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
337 using_b_ffname = (fname == curbuf->b_ffname) |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
338 || (sfname == curbuf->b_ffname); |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
339 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname); |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
340 |
167 | 341 /* After reading a file the cursor line changes but we don't want to |
342 * display the line. */ | |
343 ex_no_reprint = TRUE; | |
344 | |
968 | 345 /* don't display the file info for another buffer now */ |
346 need_fileinfo = FALSE; | |
347 | |
7 | 348 /* |
349 * For Unix: Use the short file name whenever possible. | |
350 * Avoids problems with networks and when directory names are changed. | |
351 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to | |
352 * another directory, which we don't detect. | |
353 */ | |
354 if (sfname == NULL) | |
355 sfname = fname; | |
9536
b2aada04d84e
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Christian Brabandt <cb@256bit.org>
parents:
9509
diff
changeset
|
356 #if defined(UNIX) |
7 | 357 fname = sfname; |
358 #endif | |
359 | |
360 /* | |
361 * The BufReadCmd and FileReadCmd events intercept the reading process by | |
362 * executing the associated commands instead. | |
363 */ | |
364 if (!filtering && !read_stdin && !read_buffer) | |
365 { | |
366 pos_T pos; | |
367 | |
368 pos = curbuf->b_op_start; | |
369 | |
370 /* Set '[ mark to the line above where the lines go (line 1 if zero). */ | |
371 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); | |
372 curbuf->b_op_start.col = 0; | |
373 | |
374 if (newfile) | |
375 { | |
376 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname, | |
377 FALSE, curbuf, eap)) | |
378 #ifdef FEAT_EVAL | |
379 return aborting() ? FAIL : OK; | |
380 #else | |
381 return OK; | |
382 #endif | |
383 } | |
384 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname, | |
385 FALSE, NULL, eap)) | |
386 #ifdef FEAT_EVAL | |
387 return aborting() ? FAIL : OK; | |
388 #else | |
389 return OK; | |
390 #endif | |
391 | |
392 curbuf->b_op_start = pos; | |
393 } | |
394 | |
395 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0) | |
396 msg_scroll = FALSE; /* overwrite previous file message */ | |
397 else | |
398 msg_scroll = TRUE; /* don't overwrite previous file message */ | |
399 | |
400 /* | |
401 * If the name ends in a path separator, we can't open it. Check here, | |
402 * because reading the file may actually work, but then creating the swap | |
403 * file may destroy it! Reported on MS-DOS and Win 95. | |
404 * If the name is too long we might crash further on, quit here. | |
405 */ | |
23 | 406 if (fname != NULL && *fname != NUL) |
407 { | |
39 | 408 p = fname + STRLEN(fname); |
409 if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL) | |
23 | 410 { |
411 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0); | |
412 msg_end(); | |
413 msg_scroll = msg_save; | |
414 return FAIL; | |
415 } | |
7 | 416 } |
417 | |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
418 if (!read_stdin && !read_buffer && !read_fifo) |
5322 | 419 { |
7 | 420 #ifdef UNIX |
5322 | 421 /* |
422 * On Unix it is possible to read a directory, so we have to | |
423 * check for it before the mch_open(). | |
424 */ | |
7 | 425 perm = mch_getperm(fname); |
426 if (perm >= 0 && !S_ISREG(perm) /* not a regular file ... */ | |
427 && !S_ISFIFO(perm) /* ... or fifo */ | |
428 && !S_ISSOCK(perm) /* ... or socket */ | |
1313 | 429 # ifdef OPEN_CHR_FILES |
430 && !(S_ISCHR(perm) && is_dev_fd_file(fname)) | |
431 /* ... or a character special file named /dev/fd/<n> */ | |
432 # endif | |
7 | 433 ) |
434 { | |
10575
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
435 int retval = FAIL; |
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
436 |
7 | 437 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
|
438 { |
7 | 439 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
|
440 retval = NOTDONE; |
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
441 } |
7 | 442 else |
443 filemess(curbuf, fname, (char_u *)_("is not a file"), 0); | |
444 msg_end(); | |
445 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
|
446 return retval; |
7 | 447 } |
5322 | 448 #endif |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
449 #if defined(MSWIN) |
1006 | 450 /* |
451 * MS-Windows allows opening a device, but we will probably get stuck | |
452 * trying to read it. | |
453 */ | |
454 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE) | |
455 { | |
1303 | 456 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0); |
1006 | 457 msg_end(); |
458 msg_scroll = msg_save; | |
459 return FAIL; | |
460 } | |
5322 | 461 #endif |
462 } | |
1004 | 463 |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
464 /* Set default or forced 'fileformat' and 'binary'. */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
465 set_file_options(set_options, eap); |
7 | 466 |
467 /* | |
468 * When opening a new file we take the readonly flag from the file. | |
469 * Default is r/w, can be set to r/o below. | |
470 * Don't reset it when in readonly mode | |
471 * Only set/reset b_p_ro when BF_CHECK_RO is set. | |
472 */ | |
473 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO)); | |
164 | 474 if (check_readonly && !readonlymode) |
7 | 475 curbuf->b_p_ro = FALSE; |
476 | |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
477 if (newfile && !read_stdin && !read_buffer && !read_fifo) |
7 | 478 { |
2823 | 479 /* Remember time of file. */ |
7 | 480 if (mch_stat((char *)fname, &st) >= 0) |
481 { | |
482 buf_store_time(curbuf, &st, fname); | |
483 curbuf->b_mtime_read = curbuf->b_mtime; | |
484 #ifdef UNIX | |
485 /* | |
486 * Use the protection bits of the original file for the swap file. | |
487 * This makes it possible for others to read the name of the | |
488 * edited file from the swapfile, but only if they can read the | |
489 * edited file. | |
490 * Remove the "write" and "execute" bits for group and others | |
491 * (they must not write the swapfile). | |
492 * Add the "read" and "write" bits for the user, otherwise we may | |
493 * not be able to write to the file ourselves. | |
494 * Setting the bits is done below, after creating the swap file. | |
495 */ | |
496 swap_mode = (st.st_mode & 0644) | 0600; | |
497 #endif | |
498 #ifdef FEAT_CW_EDITOR | |
499 /* Get the FSSpec on MacOS | |
500 * TODO: Update it properly when the buffer name changes | |
501 */ | |
502 (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec); | |
503 #endif | |
504 #ifdef VMS | |
505 curbuf->b_fab_rfm = st.st_fab_rfm; | |
22 | 506 curbuf->b_fab_rat = st.st_fab_rat; |
507 curbuf->b_fab_mrs = st.st_fab_mrs; | |
7 | 508 #endif |
509 } | |
510 else | |
511 { | |
512 curbuf->b_mtime = 0; | |
513 curbuf->b_mtime_read = 0; | |
514 curbuf->b_orig_size = 0; | |
515 curbuf->b_orig_mode = 0; | |
516 } | |
517 | |
518 /* Reset the "new file" flag. It will be set again below when the | |
519 * file doesn't exist. */ | |
520 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W); | |
521 } | |
522 | |
523 /* | |
524 * 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
|
525 * for Amiga: check readonly by trying to open the file for writing |
7 | 526 */ |
527 file_readonly = FALSE; | |
528 if (read_stdin) | |
529 { | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
530 #if defined(MSWIN) |
7 | 531 /* Force binary I/O on stdin to avoid CR-LF -> LF conversion. */ |
532 setmode(0, O_BINARY); | |
533 #endif | |
534 } | |
535 else if (!read_buffer) | |
536 { | |
537 #ifdef USE_MCH_ACCESS | |
538 if ( | |
539 # ifdef UNIX | |
540 !(perm & 0222) || | |
541 # endif | |
542 mch_access((char *)fname, W_OK)) | |
543 file_readonly = TRUE; | |
544 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
545 #else | |
546 if (!newfile | |
547 || readonlymode | |
548 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0) | |
549 { | |
550 file_readonly = TRUE; | |
551 /* try to open ro */ | |
552 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
553 } | |
554 #endif | |
555 } | |
556 | |
557 if (fd < 0) /* cannot open at all */ | |
558 { | |
559 #ifndef UNIX | |
560 int isdir_f; | |
561 #endif | |
562 msg_scroll = msg_save; | |
563 #ifndef UNIX | |
564 /* | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
565 * On Amiga we can't open a directory, check here. |
7 | 566 */ |
567 isdir_f = (mch_isdir(fname)); | |
568 perm = mch_getperm(fname); /* check if the file exists */ | |
569 if (isdir_f) | |
570 { | |
571 filemess(curbuf, sfname, (char_u *)_("is a directory"), 0); | |
572 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
573 } | |
574 else | |
575 #endif | |
576 if (newfile) | |
577 { | |
2147
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
578 if (perm < 0 |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
579 #ifdef ENOENT |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
580 && errno == ENOENT |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
581 #endif |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
582 ) |
7 | 583 { |
584 /* | |
585 * Set the 'new-file' flag, so that when the file has | |
586 * been created by someone else, a ":w" will complain. | |
587 */ | |
588 curbuf->b_flags |= BF_NEW; | |
589 | |
590 /* Create a swap file now, so that other Vims are warned | |
591 * that we are editing this file. Don't do this for a | |
592 * "nofile" or "nowrite" buffer type. */ | |
593 #ifdef FEAT_QUICKFIX | |
594 if (!bt_dontwrite(curbuf)) | |
595 #endif | |
1834 | 596 { |
7 | 597 check_need_swap(newfile); |
1834 | 598 /* SwapExists autocommand may mess things up */ |
599 if (curbuf != old_curbuf | |
600 || (using_b_ffname | |
601 && (old_b_ffname != curbuf->b_ffname)) | |
602 || (using_b_fname | |
603 && (old_b_fname != curbuf->b_fname))) | |
604 { | |
605 EMSG(_(e_auchangedbuf)); | |
606 return FAIL; | |
607 } | |
608 } | |
592 | 609 if (dir_of_file_exists(fname)) |
610 filemess(curbuf, sfname, (char_u *)_("[New File]"), 0); | |
611 else | |
612 filemess(curbuf, sfname, | |
613 (char_u *)_("[New DIRECTORY]"), 0); | |
7 | 614 #ifdef FEAT_VIMINFO |
615 /* Even though this is a new file, it might have been | |
616 * edited before and deleted. Get the old marks. */ | |
617 check_marks_read(); | |
618 #endif | |
619 #ifdef FEAT_MBYTE | |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
620 /* Set forced 'fileencoding'. */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
621 if (eap != NULL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
622 set_forced_fenc(eap); |
7 | 623 #endif |
624 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname, | |
625 FALSE, curbuf, eap); | |
626 /* remember the current fileformat */ | |
627 save_file_ff(curbuf); | |
628 | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
629 #if defined(FEAT_EVAL) |
7 | 630 if (aborting()) /* autocmds may abort script processing */ |
631 return FAIL; | |
632 #endif | |
633 return OK; /* a new file is not an error */ | |
634 } | |
635 else | |
636 { | |
550 | 637 filemess(curbuf, sfname, (char_u *)( |
638 # ifdef EFBIG | |
639 (errno == EFBIG) ? _("[File too big]") : | |
640 # endif | |
2147
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
641 # ifdef EOVERFLOW |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
642 (errno == EOVERFLOW) ? _("[File too big]") : |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
643 # endif |
550 | 644 _("[Permission Denied]")), 0); |
7 | 645 curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
646 } | |
647 } | |
648 | |
649 return FAIL; | |
650 } | |
651 | |
652 /* | |
653 * Only set the 'ro' flag for readonly files the first time they are | |
654 * loaded. Help files always get readonly mode | |
655 */ | |
656 if ((check_readonly && file_readonly) || curbuf->b_help) | |
657 curbuf->b_p_ro = TRUE; | |
658 | |
819 | 659 if (set_options) |
7 | 660 { |
1486 | 661 /* Don't change 'eol' if reading from buffer as it will already be |
662 * correctly set when reading stdin. */ | |
663 if (!read_buffer) | |
664 { | |
665 curbuf->b_p_eol = TRUE; | |
666 curbuf->b_start_eol = TRUE; | |
667 } | |
7 | 668 #ifdef FEAT_MBYTE |
669 curbuf->b_p_bomb = FALSE; | |
1352 | 670 curbuf->b_start_bomb = FALSE; |
7 | 671 #endif |
672 } | |
673 | |
674 /* Create a swap file now, so that other Vims are warned that we are | |
675 * editing this file. | |
676 * Don't do this for a "nofile" or "nowrite" buffer type. */ | |
677 #ifdef FEAT_QUICKFIX | |
678 if (!bt_dontwrite(curbuf)) | |
679 #endif | |
680 { | |
681 check_need_swap(newfile); | |
1834 | 682 if (!read_stdin && (curbuf != old_curbuf |
683 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) | |
684 || (using_b_fname && (old_b_fname != curbuf->b_fname)))) | |
685 { | |
686 EMSG(_(e_auchangedbuf)); | |
687 if (!read_buffer) | |
688 close(fd); | |
689 return FAIL; | |
690 } | |
7 | 691 #ifdef UNIX |
692 /* Set swap file protection bits after creating it. */ | |
1918 | 693 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL |
694 && 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
|
695 { |
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 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
|
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 /* |
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 * 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
|
700 * 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
|
701 * 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
|
702 * 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
|
703 * 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
|
704 */ |
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 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
|
706 { |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
707 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
|
708 |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
709 if (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
|
710 && 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
|
711 # 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
|
712 && 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
|
713 == -1 |
13730
7b818f7bb738
patch 8.0.1737: fchown() used when it is not supported
Christian Brabandt <cb@256bit.org>
parents:
13610
diff
changeset
|
714 # endif |
7b818f7bb738
patch 8.0.1737: fchown() used when it is not supported
Christian Brabandt <cb@256bit.org>
parents:
13610
diff
changeset
|
715 ) |
12771
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
716 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
|
717 } |
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
|
718 |
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
|
719 (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
|
720 } |
7 | 721 #endif |
722 } | |
723 | |
578 | 724 #if defined(HAS_SWAP_EXISTS_ACTION) |
7 | 725 /* If "Quit" selected at ATTENTION dialog, don't load the file */ |
726 if (swap_exists_action == SEA_QUIT) | |
727 { | |
728 if (!read_buffer && !read_stdin) | |
729 close(fd); | |
730 return FAIL; | |
731 } | |
732 #endif | |
733 | |
734 ++no_wait_return; /* don't wait for return yet */ | |
735 | |
736 /* | |
737 * Set '[ mark to the line above where the lines go (line 1 if zero). | |
738 */ | |
739 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); | |
740 curbuf->b_op_start.col = 0; | |
741 | |
10668
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
742 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
|
743 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
|
744 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
|
745 |
7 | 746 if (!read_buffer) |
747 { | |
748 int m = msg_scroll; | |
749 int n = msg_scrolled; | |
750 | |
751 /* | |
752 * The file must be closed again, the autocommands may want to change | |
753 * the file before reading it. | |
754 */ | |
755 if (!read_stdin) | |
756 close(fd); /* ignore errors */ | |
757 | |
758 /* | |
759 * The output from the autocommands should not overwrite anything and | |
760 * should not be overwritten: Set msg_scroll, restore its value if no | |
761 * output was done. | |
762 */ | |
763 msg_scroll = TRUE; | |
764 if (filtering) | |
765 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname, | |
766 FALSE, curbuf, eap); | |
767 else if (read_stdin) | |
768 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname, | |
769 FALSE, curbuf, eap); | |
770 else if (newfile) | |
771 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname, | |
772 FALSE, curbuf, eap); | |
773 else | |
774 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname, | |
775 FALSE, NULL, eap); | |
10668
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
776 /* 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
|
777 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
|
778 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
|
779 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
|
780 |
7 | 781 if (msg_scrolled == n) |
782 msg_scroll = m; | |
783 | |
784 #ifdef FEAT_EVAL | |
785 if (aborting()) /* autocmds may abort script processing */ | |
786 { | |
787 --no_wait_return; | |
788 msg_scroll = msg_save; | |
789 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
790 return FAIL; | |
791 } | |
792 #endif | |
793 /* | |
794 * Don't allow the autocommands to change the current buffer. | |
795 * Try to re-open the file. | |
1834 | 796 * |
797 * Don't allow the autocommands to change the buffer name either | |
798 * (cd for example) if it invalidates fname or sfname. | |
7 | 799 */ |
800 if (!read_stdin && (curbuf != old_curbuf | |
1834 | 801 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) |
802 || (using_b_fname && (old_b_fname != curbuf->b_fname)) | |
7 | 803 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0)) |
804 { | |
805 --no_wait_return; | |
806 msg_scroll = msg_save; | |
807 if (fd < 0) | |
808 EMSG(_("E200: *ReadPre autocommands made the file unreadable")); | |
809 else | |
810 EMSG(_("E201: *ReadPre autocommands must not change current buffer")); | |
811 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
812 return FAIL; | |
813 } | |
814 } | |
815 | |
816 /* Autocommands may add lines to the file, need to check if it is empty */ | |
817 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY); | |
818 | |
819 if (!recoverymode && !filtering && !(flags & READ_DUMMY)) | |
820 { | |
821 /* | |
822 * Show the user that we are busy reading the input. Sometimes this | |
823 * may take a while. When reading from stdin another program may | |
824 * still be running, don't move the cursor to the last line, unless | |
825 * always using the GUI. | |
826 */ | |
827 if (read_stdin) | |
828 { | |
12863
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
829 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
|
830 { |
7 | 831 #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
|
832 mch_msg(_("Vim: Reading from stdin...\n")); |
7 | 833 #endif |
834 #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
|
835 /* 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
|
836 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
|
837 { |
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
838 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
|
839 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
|
840 } |
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
841 #endif |
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
842 } |
7 | 843 } |
844 else if (!read_buffer) | |
845 filemess(curbuf, sfname, (char_u *)"", 0); | |
846 } | |
847 | |
848 msg_scroll = FALSE; /* overwrite the file message */ | |
849 | |
850 /* | |
851 * Set linecnt now, before the "retry" caused by a wrong guess for | |
852 * fileformat, and after the autocommands, which may change them. | |
853 */ | |
854 linecnt = curbuf->b_ml.ml_line_count; | |
855 | |
856 #ifdef FEAT_MBYTE | |
595 | 857 /* "++bad=" argument. */ |
858 if (eap != NULL && eap->bad_char != 0) | |
612 | 859 { |
595 | 860 bad_char_behavior = eap->bad_char; |
819 | 861 if (set_options) |
612 | 862 curbuf->b_bad_char = eap->bad_char; |
863 } | |
864 else | |
865 curbuf->b_bad_char = 0; | |
595 | 866 |
7 | 867 /* |
595 | 868 * Decide which 'encoding' to use or use first. |
7 | 869 */ |
870 if (eap != NULL && eap->force_enc != 0) | |
871 { | |
872 fenc = enc_canonize(eap->cmd + eap->force_enc); | |
873 fenc_alloced = TRUE; | |
595 | 874 keep_dest_enc = TRUE; |
7 | 875 } |
876 else if (curbuf->b_p_bin) | |
877 { | |
878 fenc = (char_u *)""; /* binary: don't convert */ | |
879 fenc_alloced = FALSE; | |
880 } | |
881 else if (curbuf->b_help) | |
882 { | |
883 char_u firstline[80]; | |
301 | 884 int fc; |
7 | 885 |
886 /* Help files are either utf-8 or latin1. Try utf-8 first, if this | |
887 * fails it must be latin1. | |
888 * Always do this when 'encoding' is "utf-8". Otherwise only do | |
889 * this when needed to avoid [converted] remarks all the time. | |
890 * It is needed when the first line contains non-ASCII characters. | |
891 * That is only in *.??x files. */ | |
892 fenc = (char_u *)"latin1"; | |
893 c = enc_utf8; | |
301 | 894 if (!c && !read_stdin) |
895 { | |
896 fc = fname[STRLEN(fname) - 1]; | |
897 if (TOLOWER_ASC(fc) == 'x') | |
898 { | |
899 /* Read the first line (and a bit more). Immediately rewind to | |
900 * the start of the file. If the read() fails "len" is -1. */ | |
2664 | 901 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
|
902 vim_lseek(fd, (off_T)0L, SEEK_SET); |
301 | 903 for (p = firstline; p < firstline + len; ++p) |
904 if (*p >= 0x80) | |
905 { | |
906 c = TRUE; | |
907 break; | |
908 } | |
909 } | |
7 | 910 } |
911 | |
912 if (c) | |
913 { | |
914 fenc_next = fenc; | |
915 fenc = (char_u *)"utf-8"; | |
916 | |
917 /* When the file is utf-8 but a character doesn't fit in | |
918 * 'encoding' don't retry. In help text editing utf-8 bytes | |
919 * doesn't make sense. */ | |
844 | 920 if (!enc_utf8) |
921 keep_dest_enc = TRUE; | |
7 | 922 } |
923 fenc_alloced = FALSE; | |
924 } | |
925 else if (*p_fencs == NUL) | |
926 { | |
927 fenc = curbuf->b_p_fenc; /* use format from buffer */ | |
928 fenc_alloced = FALSE; | |
929 } | |
930 else | |
931 { | |
932 fenc_next = p_fencs; /* try items in 'fileencodings' */ | |
933 fenc = next_fenc(&fenc_next); | |
934 fenc_alloced = TRUE; | |
935 } | |
936 #endif | |
937 | |
938 /* | |
939 * Jump back here to retry reading the file in different ways. | |
940 * Reasons to retry: | |
941 * - encoding conversion failed: try another one from "fenc_next" | |
942 * - BOM detected and fenc was set, need to setup conversion | |
943 * - "fileformat" check failed: try another | |
944 * | |
945 * Variables set for special retry actions: | |
946 * "file_rewind" Rewind the file to start reading it again. | |
947 * "advance_fenc" Advance "fenc" using "fenc_next". | |
948 * "skip_read" Re-use already read bytes (BOM detected). | |
949 * "did_iconv" iconv() conversion failed, try 'charconvert'. | |
950 * "keep_fileformat" Don't reset "fileformat". | |
951 * | |
952 * Other status indicators: | |
953 * "tmpname" When != NULL did conversion with 'charconvert'. | |
954 * Output file has to be deleted afterwards. | |
955 * "iconv_fd" When != -1 did conversion with iconv(). | |
956 */ | |
957 retry: | |
958 | |
959 if (file_rewind) | |
960 { | |
961 if (read_buffer) | |
962 { | |
963 read_buf_lnum = 1; | |
964 read_buf_col = 0; | |
965 } | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
966 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0) |
7 | 967 { |
968 /* Can't rewind the file, give up. */ | |
969 error = TRUE; | |
970 goto failed; | |
971 } | |
972 /* Delete the previously read lines. */ | |
973 while (lnum > from) | |
974 ml_delete(lnum--, FALSE); | |
975 file_rewind = FALSE; | |
976 #ifdef FEAT_MBYTE | |
819 | 977 if (set_options) |
1352 | 978 { |
7 | 979 curbuf->b_p_bomb = FALSE; |
1352 | 980 curbuf->b_start_bomb = FALSE; |
981 } | |
595 | 982 conv_error = 0; |
7 | 983 #endif |
984 } | |
985 | |
986 /* | |
987 * When retrying with another "fenc" and the first time "fileformat" | |
988 * will be reset. | |
989 */ | |
990 if (keep_fileformat) | |
991 keep_fileformat = FALSE; | |
992 else | |
993 { | |
994 if (eap != NULL && eap->force_ff != 0) | |
1742 | 995 { |
7 | 996 fileformat = get_fileformat_force(curbuf, eap); |
1742 | 997 try_unix = try_dos = try_mac = FALSE; |
998 } | |
7 | 999 else if (curbuf->b_p_bin) |
1000 fileformat = EOL_UNIX; /* binary: use Unix format */ | |
1001 else if (*p_ffs == NUL) | |
1002 fileformat = get_fileformat(curbuf);/* use format from buffer */ | |
1003 else | |
1004 fileformat = EOL_UNKNOWN; /* detect from file */ | |
1005 } | |
1006 | |
1007 #ifdef FEAT_MBYTE | |
1008 # ifdef USE_ICONV | |
1009 if (iconv_fd != (iconv_t)-1) | |
1010 { | |
1011 /* aborted conversion with iconv(), close the descriptor */ | |
1012 iconv_close(iconv_fd); | |
1013 iconv_fd = (iconv_t)-1; | |
1014 } | |
1015 # endif | |
1016 | |
1017 if (advance_fenc) | |
1018 { | |
1019 /* | |
1020 * Try the next entry in 'fileencodings'. | |
1021 */ | |
1022 advance_fenc = FALSE; | |
1023 | |
1024 if (eap != NULL && eap->force_enc != 0) | |
1025 { | |
1026 /* Conversion given with "++cc=" wasn't possible, read | |
1027 * without conversion. */ | |
1028 notconverted = TRUE; | |
595 | 1029 conv_error = 0; |
7 | 1030 if (fenc_alloced) |
1031 vim_free(fenc); | |
1032 fenc = (char_u *)""; | |
1033 fenc_alloced = FALSE; | |
1034 } | |
1035 else | |
1036 { | |
1037 if (fenc_alloced) | |
1038 vim_free(fenc); | |
1039 if (fenc_next != NULL) | |
1040 { | |
1041 fenc = next_fenc(&fenc_next); | |
1042 fenc_alloced = (fenc_next != NULL); | |
1043 } | |
1044 else | |
1045 { | |
1046 fenc = (char_u *)""; | |
1047 fenc_alloced = FALSE; | |
1048 } | |
1049 } | |
1050 if (tmpname != NULL) | |
1051 { | |
1052 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
|
1053 VIM_CLEAR(tmpname); |
7 | 1054 } |
1055 } | |
1056 | |
1057 /* | |
1948 | 1058 * Conversion may be required when the encoding of the file is different |
1059 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4. | |
7 | 1060 */ |
1061 fio_flags = 0; | |
1948 | 1062 converted = need_conversion(fenc); |
1063 if (converted) | |
7 | 1064 { |
1065 | |
1066 /* "ucs-bom" means we need to check the first bytes of the file | |
1067 * for a BOM. */ | |
1068 if (STRCMP(fenc, ENC_UCSBOM) == 0) | |
1069 fio_flags = FIO_UCSBOM; | |
1070 | |
1071 /* | |
1072 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be | |
1073 * done. This is handled below after read(). Prepare the | |
1074 * fio_flags to avoid having to parse the string each time. | |
1075 * Also check for Unicode to Latin1 conversion, because iconv() | |
1076 * appears not to handle this correctly. This works just like | |
1077 * conversion to UTF-8 except how the resulting character is put in | |
1078 * the buffer. | |
1079 */ | |
1080 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
1081 fio_flags = get_fio_flags(fenc); | |
1082 | |
1083 # ifdef WIN3264 | |
1084 /* | |
1085 * Conversion from an MS-Windows codepage to UTF-8 or another codepage | |
1086 * is handled with MultiByteToWideChar(). | |
1087 */ | |
1088 if (fio_flags == 0) | |
1089 fio_flags = get_win_fio_flags(fenc); | |
1090 # endif | |
1091 | |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
1092 # ifdef MACOS_CONVERT |
7 | 1093 /* Conversion from Apple MacRoman to latin1 or UTF-8 */ |
1094 if (fio_flags == 0) | |
1095 fio_flags = get_mac_fio_flags(fenc); | |
1096 # endif | |
1097 | |
1098 # ifdef USE_ICONV | |
1099 /* | |
1100 * Try using iconv() if we can't convert internally. | |
1101 */ | |
1102 if (fio_flags == 0 | |
1103 # ifdef FEAT_EVAL | |
1104 && !did_iconv | |
1105 # endif | |
1106 ) | |
1107 iconv_fd = (iconv_t)my_iconv_open( | |
1108 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc); | |
1109 # endif | |
1110 | |
1111 # ifdef FEAT_EVAL | |
1112 /* | |
1113 * Use the 'charconvert' expression when conversion is required | |
1114 * and we can't do it internally or with iconv(). | |
1115 */ | |
1116 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
|
1117 && !read_fifo |
7 | 1118 # ifdef USE_ICONV |
1119 && iconv_fd == (iconv_t)-1 | |
1120 # endif | |
1121 ) | |
1122 { | |
1123 # ifdef USE_ICONV | |
1124 did_iconv = FALSE; | |
1125 # endif | |
1126 /* Skip conversion when it's already done (retry for wrong | |
1127 * "fileformat"). */ | |
1128 if (tmpname == NULL) | |
1129 { | |
1130 tmpname = readfile_charconvert(fname, fenc, &fd); | |
1131 if (tmpname == NULL) | |
1132 { | |
1133 /* Conversion failed. Try another one. */ | |
1134 advance_fenc = TRUE; | |
1135 if (fd < 0) | |
1136 { | |
1137 /* Re-opening the original file failed! */ | |
1138 EMSG(_("E202: Conversion made file unreadable!")); | |
1139 error = TRUE; | |
1140 goto failed; | |
1141 } | |
1142 goto retry; | |
1143 } | |
1144 } | |
1145 } | |
1146 else | |
1147 # endif | |
1148 { | |
1149 if (fio_flags == 0 | |
1150 # ifdef USE_ICONV | |
1151 && iconv_fd == (iconv_t)-1 | |
1152 # endif | |
1153 ) | |
1154 { | |
1155 /* Conversion wanted but we can't. | |
1156 * Try the next conversion in 'fileencodings' */ | |
1157 advance_fenc = TRUE; | |
1158 goto retry; | |
1159 } | |
1160 } | |
1161 } | |
1162 | |
595 | 1163 /* Set "can_retry" when it's possible to rewind the file and try with |
7 | 1164 * another "fenc" value. It's FALSE when no other "fenc" to try, reading |
595 | 1165 * 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
|
1166 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc); |
7 | 1167 #endif |
1168 | |
1169 if (!skip_read) | |
1170 { | |
1171 linerest = 0; | |
1172 filesize = 0; | |
1173 skip_count = lines_to_skip; | |
1174 read_count = lines_to_read; | |
1175 #ifdef FEAT_MBYTE | |
1176 conv_restlen = 0; | |
1177 #endif | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1178 #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
|
1179 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
|
1180 && 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
|
1181 && 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
|
1182 && !filtering |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
1183 && !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
|
1184 && !read_stdin |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1185 && !read_buffer); |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1186 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
|
1187 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
|
1188 #endif |
6122 | 1189 #ifdef FEAT_CRYPT |
1190 if (curbuf->b_cryptstate != NULL) | |
1191 { | |
1192 /* Need to free the state, but keep the key, don't want to ask for | |
1193 * it again. */ | |
1194 crypt_free_state(curbuf->b_cryptstate); | |
1195 curbuf->b_cryptstate = NULL; | |
1196 } | |
1197 #endif | |
7 | 1198 } |
1199 | |
1200 while (!error && !got_int) | |
1201 { | |
1202 /* | |
1203 * We allocate as much space for the file as we can get, plus | |
1204 * space for the old line plus room for one terminating NUL. | |
1205 * The amount is limited by the fact that read() only can read | |
1206 * upto max_unsigned characters (and other things). | |
1207 */ | |
13804
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1208 if (!skip_read) |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1209 { |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1210 #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
|
1211 # 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
|
1212 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
|
1213 # else |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1214 /* 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
|
1215 * 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
|
1216 * 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
|
1217 */ |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1218 size = 0x10000L + linerest; |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1219 if (size > 0x100000L) |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1220 size = 0x100000L; |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1221 # endif |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1222 #else |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1223 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
|
1224 #endif |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1225 } |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1226 |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1227 /* 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
|
1228 if ( |
5684 | 1229 #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
|
1230 linerest >= 0x7ff0 |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1231 #else |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1232 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
|
1233 #endif |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1234 ) |
7 | 1235 { |
1236 ++split; | |
1237 *ptr = NL; /* split line by inserting a NL */ | |
1238 size = 1; | |
1239 } | |
1240 else | |
1241 { | |
1242 if (!skip_read) | |
1243 { | |
836 | 1244 for ( ; size >= 10; size = (long)((long_u)size >> 1)) |
7 | 1245 { |
1246 if ((new_buffer = lalloc((long_u)(size + linerest + 1), | |
1247 FALSE)) != NULL) | |
1248 break; | |
1249 } | |
1250 if (new_buffer == NULL) | |
1251 { | |
1252 do_outofmem_msg((long_u)(size * 2 + linerest + 1)); | |
1253 error = TRUE; | |
1254 break; | |
1255 } | |
1256 if (linerest) /* copy characters from the previous buffer */ | |
1257 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest); | |
1258 vim_free(buffer); | |
1259 buffer = new_buffer; | |
1260 ptr = buffer + linerest; | |
1261 line_start = buffer; | |
1262 | |
1263 #ifdef FEAT_MBYTE | |
1264 /* May need room to translate into. | |
1265 * For iconv() we don't really know the required space, use a | |
1266 * factor ICONV_MULT. | |
1267 * latin1 to utf-8: 1 byte becomes up to 2 bytes | |
1268 * utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes | |
1269 * become up to 4 bytes, size must be multiple of 2 | |
1270 * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be | |
1271 * multiple of 2 | |
1272 * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be | |
1273 * multiple of 4 */ | |
835 | 1274 real_size = (int)size; |
7 | 1275 # ifdef USE_ICONV |
1276 if (iconv_fd != (iconv_t)-1) | |
1277 size = size / ICONV_MULT; | |
1278 else | |
1279 # endif | |
1280 if (fio_flags & FIO_LATIN1) | |
1281 size = size / 2; | |
1282 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1283 size = (size * 2 / 3) & ~1; | |
1284 else if (fio_flags & FIO_UCS4) | |
1285 size = (size * 2 / 3) & ~3; | |
1286 else if (fio_flags == FIO_UCSBOM) | |
1287 size = size / ICONV_MULT; /* worst case */ | |
1288 # ifdef WIN3264 | |
1289 else if (fio_flags & FIO_CODEPAGE) | |
1290 size = size / ICONV_MULT; /* also worst case */ | |
1291 # endif | |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
1292 # ifdef MACOS_CONVERT |
7 | 1293 else if (fio_flags & FIO_MACROMAN) |
1294 size = size / ICONV_MULT; /* also worst case */ | |
1295 # endif | |
1296 #endif | |
1297 | |
1298 #ifdef FEAT_MBYTE | |
1299 if (conv_restlen > 0) | |
1300 { | |
1301 /* Insert unconverted bytes from previous line. */ | |
1302 mch_memmove(ptr, conv_rest, conv_restlen); | |
1303 ptr += conv_restlen; | |
1304 size -= conv_restlen; | |
1305 } | |
1306 #endif | |
1307 | |
1308 if (read_buffer) | |
1309 { | |
1310 /* | |
1311 * Read bytes from curbuf. Used for converting text read | |
1312 * from stdin. | |
1313 */ | |
1314 if (read_buf_lnum > from) | |
1315 size = 0; | |
1316 else | |
1317 { | |
1318 int n, ni; | |
1319 long tlen; | |
1320 | |
1321 tlen = 0; | |
1322 for (;;) | |
1323 { | |
1324 p = ml_get(read_buf_lnum) + read_buf_col; | |
1325 n = (int)STRLEN(p); | |
1326 if ((int)tlen + n + 1 > size) | |
1327 { | |
1328 /* Filled up to "size", append partial line. | |
1329 * Change NL to NUL to reverse the effect done | |
1330 * below. */ | |
835 | 1331 n = (int)(size - tlen); |
7 | 1332 for (ni = 0; ni < n; ++ni) |
1333 { | |
1334 if (p[ni] == NL) | |
1335 ptr[tlen++] = NUL; | |
1336 else | |
1337 ptr[tlen++] = p[ni]; | |
1338 } | |
1339 read_buf_col += n; | |
1340 break; | |
1341 } | |
1342 else | |
1343 { | |
1344 /* Append whole line and new-line. Change NL | |
1345 * to NUL to reverse the effect done below. */ | |
1346 for (ni = 0; ni < n; ++ni) | |
1347 { | |
1348 if (p[ni] == NL) | |
1349 ptr[tlen++] = NUL; | |
1350 else | |
1351 ptr[tlen++] = p[ni]; | |
1352 } | |
1353 ptr[tlen++] = NL; | |
1354 read_buf_col = 0; | |
1355 if (++read_buf_lnum > from) | |
1356 { | |
1357 /* When the last line didn't have an | |
1358 * end-of-line don't add it now either. */ | |
1359 if (!curbuf->b_p_eol) | |
1360 --tlen; | |
1361 size = tlen; | |
1362 break; | |
1363 } | |
1364 } | |
1365 } | |
1366 } | |
1367 } | |
1368 else | |
1369 { | |
1370 /* | |
1371 * Read bytes from the file. | |
1372 */ | |
2664 | 1373 size = read_eintr(fd, ptr, size); |
7 | 1374 } |
1375 | |
6122 | 1376 #ifdef FEAT_CRYPT |
1377 /* | |
1378 * At start of file: Check for magic number of encryption. | |
1379 */ | |
1380 if (filesize == 0 && size > 0) | |
1381 cryptkey = check_for_cryptkey(cryptkey, ptr, &size, | |
1382 &filesize, newfile, sfname, | |
1383 &did_ask_for_key); | |
1384 /* | |
1385 * Decrypt the read bytes. This is done before checking for | |
1386 * EOF because the crypt layer may be buffering. | |
1387 */ | |
12216
e971ef6c0dee
patch 8.0.0988: warning from Covscan about using NULL pointer
Christian Brabandt <cb@256bit.org>
parents:
12064
diff
changeset
|
1388 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
|
1389 && size > 0) |
6122 | 1390 { |
1391 if (crypt_works_inplace(curbuf->b_cryptstate)) | |
1392 { | |
1393 crypt_decode_inplace(curbuf->b_cryptstate, ptr, size); | |
1394 } | |
1395 else | |
1396 { | |
1397 char_u *newptr = NULL; | |
1398 int decrypted_size; | |
1399 | |
1400 decrypted_size = crypt_decode_alloc( | |
1401 curbuf->b_cryptstate, ptr, size, &newptr); | |
1402 | |
1403 /* If the crypt layer is buffering, not producing | |
1404 * 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
|
1405 if (decrypted_size == 0) |
6122 | 1406 continue; |
1407 | |
1408 if (linerest == 0) | |
1409 { | |
1410 /* Simple case: reuse returned buffer (may be | |
1411 * NULL, checked later). */ | |
1412 new_buffer = newptr; | |
1413 } | |
1414 else | |
1415 { | |
1416 long_u new_size; | |
1417 | |
1418 /* Need new buffer to add bytes carried over. */ | |
1419 new_size = (long_u)(decrypted_size + linerest + 1); | |
1420 new_buffer = lalloc(new_size, FALSE); | |
1421 if (new_buffer == NULL) | |
1422 { | |
1423 do_outofmem_msg(new_size); | |
1424 error = TRUE; | |
1425 break; | |
1426 } | |
1427 | |
1428 mch_memmove(new_buffer, buffer, linerest); | |
1429 if (newptr != NULL) | |
1430 mch_memmove(new_buffer + linerest, newptr, | |
1431 decrypted_size); | |
1432 } | |
1433 | |
1434 if (new_buffer != NULL) | |
1435 { | |
1436 vim_free(buffer); | |
1437 buffer = new_buffer; | |
1438 new_buffer = NULL; | |
1439 line_start = buffer; | |
1440 ptr = buffer + linerest; | |
1441 } | |
1442 size = decrypted_size; | |
1443 } | |
1444 } | |
1445 #endif | |
1446 | |
7 | 1447 if (size <= 0) |
1448 { | |
1449 if (size < 0) /* read error */ | |
1450 error = TRUE; | |
1451 #ifdef FEAT_MBYTE | |
1452 else if (conv_restlen > 0) | |
595 | 1453 { |
1597 | 1454 /* |
1455 * Reached end-of-file but some trailing bytes could | |
1456 * not be converted. Truncated file? | |
1457 */ | |
1458 | |
1459 /* When we did a conversion report an error. */ | |
1460 if (fio_flags != 0 | |
1461 # ifdef USE_ICONV | |
1462 || iconv_fd != (iconv_t)-1 | |
1463 # endif | |
1464 ) | |
1465 { | |
4331 | 1466 if (can_retry) |
1467 goto rewind_retry; | |
1597 | 1468 if (conv_error == 0) |
1469 conv_error = curbuf->b_ml.ml_line_count | |
1470 - linecnt + 1; | |
1471 } | |
1472 /* Remember the first linenr with an illegal byte */ | |
1473 else if (illegal_byte == 0) | |
1474 illegal_byte = curbuf->b_ml.ml_line_count | |
1475 - linecnt + 1; | |
1476 if (bad_char_behavior == BAD_DROP) | |
595 | 1477 { |
1597 | 1478 *(ptr - conv_restlen) = NUL; |
1479 conv_restlen = 0; | |
1480 } | |
1481 else | |
1482 { | |
1483 /* Replace the trailing bytes with the replacement | |
1484 * character if we were converting; if we weren't, | |
1485 * leave the UTF8 checking code to do it, as it | |
1486 * works slightly differently. */ | |
1487 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0 | |
1488 # ifdef USE_ICONV | |
1489 || iconv_fd != (iconv_t)-1 | |
1490 # endif | |
1491 )) | |
1492 { | |
1493 while (conv_restlen > 0) | |
1494 { | |
1495 *(--ptr) = bad_char_behavior; | |
1496 --conv_restlen; | |
1497 } | |
1498 } | |
595 | 1499 fio_flags = 0; /* don't convert this */ |
834 | 1500 # ifdef USE_ICONV |
1501 if (iconv_fd != (iconv_t)-1) | |
1502 { | |
1503 iconv_close(iconv_fd); | |
1504 iconv_fd = (iconv_t)-1; | |
1505 } | |
1506 # endif | |
595 | 1507 } |
1508 } | |
7 | 1509 #endif |
1510 } | |
1511 } | |
1512 skip_read = FALSE; | |
1513 | |
1514 #ifdef FEAT_MBYTE | |
1515 /* | |
1516 * At start of file (or after crypt magic number): Check for BOM. | |
1517 * Also check for a BOM for other Unicode encodings, but not after | |
1518 * converting with 'charconvert' or when a BOM has already been | |
1519 * found. | |
1520 */ | |
1521 if ((filesize == 0 | |
1522 # ifdef FEAT_CRYPT | |
6122 | 1523 || (cryptkey != NULL |
1524 && filesize == crypt_get_header_len( | |
1525 crypt_get_method_nr(curbuf))) | |
7 | 1526 # endif |
1527 ) | |
1528 && (fio_flags == FIO_UCSBOM | |
1529 || (!curbuf->b_p_bomb | |
1530 && tmpname == NULL | |
1531 && (*fenc == 'u' || (*fenc == NUL && enc_utf8))))) | |
1532 { | |
1533 char_u *ccname; | |
1534 int blen; | |
1535 | |
1536 /* no BOM detection in a short file or in binary mode */ | |
1537 if (size < 2 || curbuf->b_p_bin) | |
1538 ccname = NULL; | |
1539 else | |
1540 ccname = check_for_bom(ptr, size, &blen, | |
1541 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc)); | |
1542 if (ccname != NULL) | |
1543 { | |
1544 /* Remove BOM from the text */ | |
1545 filesize += blen; | |
1546 size -= blen; | |
1547 mch_memmove(ptr, ptr + blen, (size_t)size); | |
819 | 1548 if (set_options) |
1352 | 1549 { |
7 | 1550 curbuf->b_p_bomb = TRUE; |
1352 | 1551 curbuf->b_start_bomb = TRUE; |
1552 } | |
7 | 1553 } |
1554 | |
1555 if (fio_flags == FIO_UCSBOM) | |
1556 { | |
1557 if (ccname == NULL) | |
1558 { | |
1559 /* No BOM detected: retry with next encoding. */ | |
1560 advance_fenc = TRUE; | |
1561 } | |
1562 else | |
1563 { | |
1564 /* BOM detected: set "fenc" and jump back */ | |
1565 if (fenc_alloced) | |
1566 vim_free(fenc); | |
1567 fenc = ccname; | |
1568 fenc_alloced = FALSE; | |
1569 } | |
1570 /* retry reading without getting new bytes or rewinding */ | |
1571 skip_read = TRUE; | |
1572 goto retry; | |
1573 } | |
1574 } | |
1597 | 1575 |
1576 /* Include not converted bytes. */ | |
1577 ptr -= conv_restlen; | |
1578 size += conv_restlen; | |
1579 conv_restlen = 0; | |
7 | 1580 #endif |
1581 /* | |
1582 * Break here for a read error or end-of-file. | |
1583 */ | |
1584 if (size <= 0) | |
1585 break; | |
1586 | |
1587 #ifdef FEAT_MBYTE | |
1588 | |
1589 # ifdef USE_ICONV | |
1590 if (iconv_fd != (iconv_t)-1) | |
1591 { | |
1592 /* | |
1593 * Attempt conversion of the read bytes to 'encoding' using | |
1594 * iconv(). | |
1595 */ | |
1596 const char *fromp; | |
1597 char *top; | |
1598 size_t from_size; | |
1599 size_t to_size; | |
1600 | |
1601 fromp = (char *)ptr; | |
1602 from_size = size; | |
1603 ptr += size; | |
1604 top = (char *)ptr; | |
1605 to_size = real_size - size; | |
1606 | |
1607 /* | |
1608 * If there is conversion error or not enough room try using | |
179 | 1609 * another conversion. Except for when there is no |
1610 * alternative (help files). | |
7 | 1611 */ |
177 | 1612 while ((iconv(iconv_fd, (void *)&fromp, &from_size, |
1613 &top, &to_size) | |
7 | 1614 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) |
1615 || from_size > CONV_RESTLEN) | |
177 | 1616 { |
595 | 1617 if (can_retry) |
375 | 1618 goto rewind_retry; |
595 | 1619 if (conv_error == 0) |
1620 conv_error = readfile_linenr(linecnt, | |
1621 ptr, (char_u *)top); | |
1622 | |
1623 /* Deal with a bad byte and continue with the next. */ | |
177 | 1624 ++fromp; |
1625 --from_size; | |
595 | 1626 if (bad_char_behavior == BAD_KEEP) |
1627 { | |
1628 *top++ = *(fromp - 1); | |
1629 --to_size; | |
1630 } | |
1631 else if (bad_char_behavior != BAD_DROP) | |
1632 { | |
1633 *top++ = bad_char_behavior; | |
1634 --to_size; | |
1635 } | |
177 | 1636 } |
7 | 1637 |
1638 if (from_size > 0) | |
1639 { | |
1640 /* Some remaining characters, keep them for the next | |
1641 * round. */ | |
1642 mch_memmove(conv_rest, (char_u *)fromp, from_size); | |
1643 conv_restlen = (int)from_size; | |
1644 } | |
1645 | |
1646 /* move the linerest to before the converted characters */ | |
1647 line_start = ptr - linerest; | |
1648 mch_memmove(line_start, buffer, (size_t)linerest); | |
1649 size = (long)((char_u *)top - ptr); | |
1650 } | |
1651 # endif | |
1652 | |
1653 # ifdef WIN3264 | |
1654 if (fio_flags & FIO_CODEPAGE) | |
1655 { | |
595 | 1656 char_u *src, *dst; |
1657 WCHAR ucs2buf[3]; | |
1658 int ucs2len; | |
1659 int codepage = FIO_GET_CP(fio_flags); | |
1660 int bytelen; | |
1661 int found_bad; | |
1662 char replstr[2]; | |
1663 | |
7 | 1664 /* |
1665 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or | |
595 | 1666 * a codepage, using standard MS-Windows functions. This |
1667 * requires two steps: | |
1668 * 1. convert from 'fileencoding' to ucs-2 | |
1669 * 2. convert from ucs-2 to 'encoding' | |
1670 * | |
1671 * Because there may be illegal bytes AND an incomplete byte | |
1672 * sequence at the end, we may have to do the conversion one | |
1673 * character at a time to get it right. | |
7 | 1674 */ |
595 | 1675 |
1676 /* Replacement string for WideCharToMultiByte(). */ | |
1677 if (bad_char_behavior > 0) | |
1678 replstr[0] = bad_char_behavior; | |
1679 else | |
1680 replstr[0] = '?'; | |
1681 replstr[1] = NUL; | |
1682 | |
1683 /* | |
1684 * Move the bytes to the end of the buffer, so that we have | |
1685 * room to put the result at the start. | |
1686 */ | |
1687 src = ptr + real_size - size; | |
1688 mch_memmove(src, ptr, size); | |
7 | 1689 |
1690 /* | |
595 | 1691 * Do the conversion. |
7 | 1692 */ |
595 | 1693 dst = ptr; |
1694 size = size; | |
1695 while (size > 0) | |
7 | 1696 { |
595 | 1697 found_bad = FALSE; |
7 | 1698 |
1699 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
595 | 1700 if (codepage == CP_UTF8) |
7 | 1701 { |
595 | 1702 /* Handle CP_UTF8 input ourselves to be able to handle |
1703 * trailing bytes properly. | |
1704 * Get one UTF-8 character from src. */ | |
835 | 1705 bytelen = (int)utf_ptr2len_len(src, size); |
595 | 1706 if (bytelen > size) |
1707 { | |
1708 /* Only got some bytes of a character. Normally | |
1709 * it's put in "conv_rest", but if it's too long | |
1710 * deal with it as if they were illegal bytes. */ | |
1711 if (bytelen <= CONV_RESTLEN) | |
1712 break; | |
1713 | |
1714 /* weird overlong byte sequence */ | |
1715 bytelen = size; | |
1716 found_bad = TRUE; | |
1717 } | |
1718 else | |
1719 { | |
799 | 1720 int u8c = utf_ptr2char(src); |
1721 | |
622 | 1722 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1)) |
595 | 1723 found_bad = TRUE; |
1724 ucs2buf[0] = u8c; | |
1725 ucs2len = 1; | |
1726 } | |
7 | 1727 } |
595 | 1728 else |
7 | 1729 # endif |
595 | 1730 { |
1731 /* We don't know how long the byte sequence is, try | |
1732 * from one to three bytes. */ | |
1733 for (bytelen = 1; bytelen <= size && bytelen <= 3; | |
1734 ++bytelen) | |
1735 { | |
1736 ucs2len = MultiByteToWideChar(codepage, | |
1737 MB_ERR_INVALID_CHARS, | |
1738 (LPCSTR)src, bytelen, | |
1739 ucs2buf, 3); | |
1740 if (ucs2len > 0) | |
1741 break; | |
1742 } | |
1743 if (ucs2len == 0) | |
1744 { | |
1745 /* If we have only one byte then it's probably an | |
1746 * incomplete byte sequence. Otherwise discard | |
1747 * one byte as a bad character. */ | |
1748 if (size == 1) | |
1749 break; | |
1750 found_bad = TRUE; | |
1751 bytelen = 1; | |
1752 } | |
1753 } | |
1754 | |
1755 if (!found_bad) | |
7 | 1756 { |
595 | 1757 int i; |
1758 | |
1759 /* Convert "ucs2buf[ucs2len]" to 'enc' in "dst". */ | |
1760 if (enc_utf8) | |
1761 { | |
1762 /* From UCS-2 to UTF-8. Cannot fail. */ | |
1763 for (i = 0; i < ucs2len; ++i) | |
1764 dst += utf_char2bytes(ucs2buf[i], dst); | |
1765 } | |
1766 else | |
1767 { | |
1768 BOOL bad = FALSE; | |
1769 int dstlen; | |
1770 | |
1771 /* From UCS-2 to "enc_codepage". If the | |
1772 * conversion uses the default character "?", | |
1773 * the data doesn't fit in this encoding. */ | |
1774 dstlen = WideCharToMultiByte(enc_codepage, 0, | |
1775 (LPCWSTR)ucs2buf, ucs2len, | |
835 | 1776 (LPSTR)dst, (int)(src - dst), |
595 | 1777 replstr, &bad); |
1778 if (bad) | |
1779 found_bad = TRUE; | |
1780 else | |
1781 dst += dstlen; | |
1782 } | |
7 | 1783 } |
595 | 1784 |
1785 if (found_bad) | |
1786 { | |
1787 /* Deal with bytes we can't convert. */ | |
1788 if (can_retry) | |
1789 goto rewind_retry; | |
1790 if (conv_error == 0) | |
1791 conv_error = readfile_linenr(linecnt, ptr, dst); | |
1792 if (bad_char_behavior != BAD_DROP) | |
1793 { | |
1794 if (bad_char_behavior == BAD_KEEP) | |
1795 { | |
1796 mch_memmove(dst, src, bytelen); | |
1797 dst += bytelen; | |
1798 } | |
1799 else | |
1800 *dst++ = bad_char_behavior; | |
1801 } | |
1802 } | |
1803 | |
1804 src += bytelen; | |
1805 size -= bytelen; | |
7 | 1806 } |
595 | 1807 |
1808 if (size > 0) | |
7 | 1809 { |
595 | 1810 /* An incomplete byte sequence remaining. */ |
1811 mch_memmove(conv_rest, src, size); | |
1812 conv_restlen = size; | |
7 | 1813 } |
595 | 1814 |
1815 /* The new size is equal to how much "dst" was advanced. */ | |
835 | 1816 size = (long)(dst - ptr); |
7 | 1817 } |
1818 else | |
1819 # endif | |
765 | 1820 # ifdef MACOS_CONVERT |
7 | 1821 if (fio_flags & FIO_MACROMAN) |
1822 { | |
1823 /* | |
1824 * Conversion from Apple MacRoman char encoding to UTF-8 or | |
18 | 1825 * latin1. This is in os_mac_conv.c. |
7 | 1826 */ |
18 | 1827 if (macroman2enc(ptr, &size, real_size) == FAIL) |
7 | 1828 goto rewind_retry; |
1829 } | |
1830 else | |
1831 # endif | |
1832 if (fio_flags != 0) | |
1833 { | |
1834 int u8c; | |
1835 char_u *dest; | |
1836 char_u *tail = NULL; | |
1837 | |
1838 /* | |
1839 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8. | |
1840 * "enc_utf8" not set: Convert Unicode to Latin1. | |
1841 * Go from end to start through the buffer, because the number | |
1842 * of bytes may increase. | |
1843 * "dest" points to after where the UTF-8 bytes go, "p" points | |
1844 * to after the next character to convert. | |
1845 */ | |
1846 dest = ptr + real_size; | |
1847 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8) | |
1848 { | |
1849 p = ptr + size; | |
1850 if (fio_flags == FIO_UTF8) | |
1851 { | |
1852 /* Check for a trailing incomplete UTF-8 sequence */ | |
1853 tail = ptr + size - 1; | |
1854 while (tail > ptr && (*tail & 0xc0) == 0x80) | |
1855 --tail; | |
1856 if (tail + utf_byte2len(*tail) <= ptr + size) | |
1857 tail = NULL; | |
1858 else | |
1859 p = tail; | |
1860 } | |
1861 } | |
1862 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1863 { | |
1864 /* Check for a trailing byte */ | |
1865 p = ptr + (size & ~1); | |
1866 if (size & 1) | |
1867 tail = p; | |
1868 if ((fio_flags & FIO_UTF16) && p > ptr) | |
1869 { | |
1870 /* Check for a trailing leading word */ | |
1871 if (fio_flags & FIO_ENDIAN_L) | |
1872 { | |
1873 u8c = (*--p << 8); | |
1874 u8c += *--p; | |
1875 } | |
1876 else | |
1877 { | |
1878 u8c = *--p; | |
1879 u8c += (*--p << 8); | |
1880 } | |
1881 if (u8c >= 0xd800 && u8c <= 0xdbff) | |
1882 tail = p; | |
1883 else | |
1884 p += 2; | |
1885 } | |
1886 } | |
1887 else /* FIO_UCS4 */ | |
1888 { | |
1889 /* Check for trailing 1, 2 or 3 bytes */ | |
1890 p = ptr + (size & ~3); | |
1891 if (size & 3) | |
1892 tail = p; | |
1893 } | |
1894 | |
1895 /* If there is a trailing incomplete sequence move it to | |
1896 * conv_rest[]. */ | |
1897 if (tail != NULL) | |
1898 { | |
1899 conv_restlen = (int)((ptr + size) - tail); | |
1900 mch_memmove(conv_rest, (char_u *)tail, conv_restlen); | |
1901 size -= conv_restlen; | |
1902 } | |
1903 | |
1904 | |
1905 while (p > ptr) | |
1906 { | |
1907 if (fio_flags & FIO_LATIN1) | |
1908 u8c = *--p; | |
1909 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1910 { | |
1911 if (fio_flags & FIO_ENDIAN_L) | |
1912 { | |
1913 u8c = (*--p << 8); | |
1914 u8c += *--p; | |
1915 } | |
1916 else | |
1917 { | |
1918 u8c = *--p; | |
1919 u8c += (*--p << 8); | |
1920 } | |
1921 if ((fio_flags & FIO_UTF16) | |
1922 && u8c >= 0xdc00 && u8c <= 0xdfff) | |
1923 { | |
1924 int u16c; | |
1925 | |
1926 if (p == ptr) | |
1927 { | |
1928 /* Missing leading word. */ | |
1929 if (can_retry) | |
1930 goto rewind_retry; | |
595 | 1931 if (conv_error == 0) |
1932 conv_error = readfile_linenr(linecnt, | |
1933 ptr, p); | |
1934 if (bad_char_behavior == BAD_DROP) | |
1935 continue; | |
1936 if (bad_char_behavior != BAD_KEEP) | |
1937 u8c = bad_char_behavior; | |
7 | 1938 } |
1939 | |
1940 /* found second word of double-word, get the first | |
1941 * word and compute the resulting character */ | |
1942 if (fio_flags & FIO_ENDIAN_L) | |
1943 { | |
1944 u16c = (*--p << 8); | |
1945 u16c += *--p; | |
1946 } | |
1947 else | |
1948 { | |
1949 u16c = *--p; | |
1950 u16c += (*--p << 8); | |
1951 } | |
595 | 1952 u8c = 0x10000 + ((u16c & 0x3ff) << 10) |
1953 + (u8c & 0x3ff); | |
1954 | |
7 | 1955 /* Check if the word is indeed a leading word. */ |
1956 if (u16c < 0xd800 || u16c > 0xdbff) | |
1957 { | |
1958 if (can_retry) | |
1959 goto rewind_retry; | |
595 | 1960 if (conv_error == 0) |
1961 conv_error = readfile_linenr(linecnt, | |
1962 ptr, p); | |
1963 if (bad_char_behavior == BAD_DROP) | |
1964 continue; | |
1965 if (bad_char_behavior != BAD_KEEP) | |
1966 u8c = bad_char_behavior; | |
7 | 1967 } |
1968 } | |
1969 } | |
1970 else if (fio_flags & FIO_UCS4) | |
1971 { | |
1972 if (fio_flags & FIO_ENDIAN_L) | |
1973 { | |
12698
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1974 u8c = (unsigned)*--p << 24; |
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1975 u8c += (unsigned)*--p << 16; |
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1976 u8c += (unsigned)*--p << 8; |
7 | 1977 u8c += *--p; |
1978 } | |
1979 else /* big endian */ | |
1980 { | |
1981 u8c = *--p; | |
12698
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1982 u8c += (unsigned)*--p << 8; |
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1983 u8c += (unsigned)*--p << 16; |
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1984 u8c += (unsigned)*--p << 24; |
7 | 1985 } |
1986 } | |
1987 else /* UTF-8 */ | |
1988 { | |
1989 if (*--p < 0x80) | |
1990 u8c = *p; | |
1991 else | |
1992 { | |
1993 len = utf_head_off(ptr, p); | |
595 | 1994 p -= len; |
1995 u8c = utf_ptr2char(p); | |
7 | 1996 if (len == 0) |
1997 { | |
1998 /* Not a valid UTF-8 character, retry with | |
1999 * another fenc when possible, otherwise just | |
2000 * report the error. */ | |
2001 if (can_retry) | |
2002 goto rewind_retry; | |
595 | 2003 if (conv_error == 0) |
2004 conv_error = readfile_linenr(linecnt, | |
2005 ptr, p); | |
2006 if (bad_char_behavior == BAD_DROP) | |
2007 continue; | |
2008 if (bad_char_behavior != BAD_KEEP) | |
2009 u8c = bad_char_behavior; | |
7 | 2010 } |
2011 } | |
2012 } | |
2013 if (enc_utf8) /* produce UTF-8 */ | |
2014 { | |
2015 dest -= utf_char2len(u8c); | |
2016 (void)utf_char2bytes(u8c, dest); | |
2017 } | |
2018 else /* produce Latin1 */ | |
2019 { | |
2020 --dest; | |
2021 if (u8c >= 0x100) | |
2022 { | |
2023 /* character doesn't fit in latin1, retry with | |
2024 * another fenc when possible, otherwise just | |
2025 * report the error. */ | |
595 | 2026 if (can_retry) |
7 | 2027 goto rewind_retry; |
595 | 2028 if (conv_error == 0) |
2029 conv_error = readfile_linenr(linecnt, ptr, p); | |
2030 if (bad_char_behavior == BAD_DROP) | |
2031 ++dest; | |
2032 else if (bad_char_behavior == BAD_KEEP) | |
2033 *dest = u8c; | |
2034 else if (eap != NULL && eap->bad_char != 0) | |
2035 *dest = bad_char_behavior; | |
2036 else | |
2037 *dest = 0xBF; | |
7 | 2038 } |
2039 else | |
2040 *dest = u8c; | |
2041 } | |
2042 } | |
2043 | |
2044 /* move the linerest to before the converted characters */ | |
2045 line_start = dest - linerest; | |
2046 mch_memmove(line_start, buffer, (size_t)linerest); | |
2047 size = (long)((ptr + real_size) - dest); | |
2048 ptr = dest; | |
2049 } | |
1597 | 2050 else if (enc_utf8 && !curbuf->b_p_bin) |
2051 { | |
2052 int incomplete_tail = FALSE; | |
2053 | |
2054 /* Reading UTF-8: Check if the bytes are valid UTF-8. */ | |
2055 for (p = ptr; ; ++p) | |
7 | 2056 { |
835 | 2057 int todo = (int)((ptr + size) - p); |
595 | 2058 int l; |
2059 | |
2060 if (todo <= 0) | |
2061 break; | |
7 | 2062 if (*p >= 0x80) |
2063 { | |
2064 /* A length of 1 means it's an illegal byte. Accept | |
2065 * an incomplete character at the end though, the next | |
2066 * read() will get the next bytes, we'll check it | |
2067 * then. */ | |
595 | 2068 l = utf_ptr2len_len(p, todo); |
1597 | 2069 if (l > todo && !incomplete_tail) |
7 | 2070 { |
1597 | 2071 /* Avoid retrying with a different encoding when |
2072 * a truncated file is more likely, or attempting | |
2073 * to read the rest of an incomplete sequence when | |
2074 * we have already done so. */ | |
2075 if (p > ptr || filesize > 0) | |
2076 incomplete_tail = TRUE; | |
2077 /* Incomplete byte sequence, move it to conv_rest[] | |
2078 * and try to read the rest of it, unless we've | |
2079 * already done so. */ | |
2080 if (p > ptr) | |
2081 { | |
2082 conv_restlen = todo; | |
2083 mch_memmove(conv_rest, p, conv_restlen); | |
2084 size -= conv_restlen; | |
2085 break; | |
2086 } | |
7 | 2087 } |
1597 | 2088 if (l == 1 || l > todo) |
595 | 2089 { |
2090 /* Illegal byte. If we can try another encoding | |
1597 | 2091 * do that, unless at EOF where a truncated |
2092 * file is more likely than a conversion error. */ | |
2093 if (can_retry && !incomplete_tail) | |
595 | 2094 break; |
2095 # ifdef USE_ICONV | |
2096 /* When we did a conversion report an error. */ | |
2097 if (iconv_fd != (iconv_t)-1 && conv_error == 0) | |
2098 conv_error = readfile_linenr(linecnt, ptr, p); | |
2099 # endif | |
1597 | 2100 /* Remember the first linenr with an illegal byte */ |
2101 if (conv_error == 0 && illegal_byte == 0) | |
2102 illegal_byte = readfile_linenr(linecnt, ptr, p); | |
595 | 2103 |
2104 /* Drop, keep or replace the bad byte. */ | |
2105 if (bad_char_behavior == BAD_DROP) | |
2106 { | |
1597 | 2107 mch_memmove(p, p + 1, todo - 1); |
595 | 2108 --p; |
2109 --size; | |
2110 } | |
2111 else if (bad_char_behavior != BAD_KEEP) | |
2112 *p = bad_char_behavior; | |
2113 } | |
1597 | 2114 else |
2115 p += l - 1; | |
7 | 2116 } |
2117 } | |
1597 | 2118 if (p < ptr + size && !incomplete_tail) |
7 | 2119 { |
2120 /* Detected a UTF-8 error. */ | |
2121 rewind_retry: | |
595 | 2122 /* Retry reading with another conversion. */ |
7 | 2123 # if defined(FEAT_EVAL) && defined(USE_ICONV) |
595 | 2124 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1) |
2125 /* iconv() failed, try 'charconvert' */ | |
2126 did_iconv = TRUE; | |
7 | 2127 else |
2128 # endif | |
595 | 2129 /* use next item from 'fileencodings' */ |
2130 advance_fenc = TRUE; | |
2131 file_rewind = TRUE; | |
2132 goto retry; | |
7 | 2133 } |
2134 } | |
2135 #endif | |
2136 | |
2137 /* count the number of characters (after conversion!) */ | |
2138 filesize += size; | |
2139 | |
2140 /* | |
2141 * when reading the first part of a file: guess EOL type | |
2142 */ | |
2143 if (fileformat == EOL_UNKNOWN) | |
2144 { | |
2145 /* First try finding a NL, for Dos and Unix */ | |
2146 if (try_dos || try_unix) | |
2147 { | |
6635 | 2148 /* Reset the carriage return counter. */ |
2149 if (try_mac) | |
2150 try_mac = 1; | |
2151 | |
7 | 2152 for (p = ptr; p < ptr + size; ++p) |
2153 { | |
2154 if (*p == NL) | |
2155 { | |
2156 if (!try_unix | |
2157 || (try_dos && p > ptr && p[-1] == CAR)) | |
2158 fileformat = EOL_DOS; | |
2159 else | |
2160 fileformat = EOL_UNIX; | |
2161 break; | |
2162 } | |
6618 | 2163 else if (*p == CAR && try_mac) |
2164 try_mac++; | |
7 | 2165 } |
2166 | |
2167 /* Don't give in to EOL_UNIX if EOL_MAC is more likely */ | |
2168 if (fileformat == EOL_UNIX && try_mac) | |
2169 { | |
2170 /* Need to reset the counters when retrying fenc. */ | |
2171 try_mac = 1; | |
2172 try_unix = 1; | |
2173 for (; p >= ptr && *p != CAR; p--) | |
2174 ; | |
2175 if (p >= ptr) | |
2176 { | |
2177 for (p = ptr; p < ptr + size; ++p) | |
2178 { | |
2179 if (*p == NL) | |
2180 try_unix++; | |
2181 else if (*p == CAR) | |
2182 try_mac++; | |
2183 } | |
2184 if (try_mac > try_unix) | |
2185 fileformat = EOL_MAC; | |
2186 } | |
2187 } | |
6618 | 2188 else if (fileformat == EOL_UNKNOWN && try_mac == 1) |
2189 /* Looking for CR but found no end-of-line markers at | |
2190 * all: use the default format. */ | |
2191 fileformat = default_fileformat(); | |
7 | 2192 } |
2193 | |
2194 /* No NL found: may use Mac format */ | |
2195 if (fileformat == EOL_UNKNOWN && try_mac) | |
2196 fileformat = EOL_MAC; | |
2197 | |
2198 /* Still nothing found? Use first format in 'ffs' */ | |
2199 if (fileformat == EOL_UNKNOWN) | |
2200 fileformat = default_fileformat(); | |
2201 | |
2202 /* if editing a new file: may set p_tx and p_ff */ | |
819 | 2203 if (set_options) |
7 | 2204 set_fileformat(fileformat, OPT_LOCAL); |
2205 } | |
2206 } | |
2207 | |
2208 /* | |
2209 * This loop is executed once for every character read. | |
2210 * Keep it fast! | |
2211 */ | |
2212 if (fileformat == EOL_MAC) | |
2213 { | |
2214 --ptr; | |
2215 while (++ptr, --size >= 0) | |
2216 { | |
2217 /* catch most common case first */ | |
2218 if ((c = *ptr) != NUL && c != CAR && c != NL) | |
2219 continue; | |
2220 if (c == NUL) | |
2221 *ptr = NL; /* NULs are replaced by newlines! */ | |
2222 else if (c == NL) | |
2223 *ptr = CAR; /* NLs are replaced by CRs! */ | |
2224 else | |
2225 { | |
2226 if (skip_count == 0) | |
2227 { | |
2228 *ptr = NUL; /* end of line */ | |
2229 len = (colnr_T) (ptr - line_start + 1); | |
2230 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2231 { | |
2232 error = TRUE; | |
2233 break; | |
2234 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2235 #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
|
2236 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
|
2237 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
|
2238 #endif |
7 | 2239 ++lnum; |
2240 if (--read_count == 0) | |
2241 { | |
2242 error = TRUE; /* break loop */ | |
2243 line_start = ptr; /* nothing left to write */ | |
2244 break; | |
2245 } | |
2246 } | |
2247 else | |
2248 --skip_count; | |
2249 line_start = ptr + 1; | |
2250 } | |
2251 } | |
2252 } | |
2253 else | |
2254 { | |
2255 --ptr; | |
2256 while (++ptr, --size >= 0) | |
2257 { | |
2258 if ((c = *ptr) != NUL && c != NL) /* catch most common case */ | |
2259 continue; | |
2260 if (c == NUL) | |
2261 *ptr = NL; /* NULs are replaced by newlines! */ | |
2262 else | |
2263 { | |
2264 if (skip_count == 0) | |
2265 { | |
2266 *ptr = NUL; /* end of line */ | |
2267 len = (colnr_T)(ptr - line_start + 1); | |
2268 if (fileformat == EOL_DOS) | |
2269 { | |
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
|
2270 if (ptr > line_start && ptr[-1] == CAR) |
7 | 2271 { |
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
|
2272 /* remove CR before NL */ |
7 | 2273 ptr[-1] = NUL; |
2274 --len; | |
2275 } | |
2276 /* | |
2277 * Reading in Dos format, but no CR-LF found! | |
2278 * When 'fileformats' includes "unix", delete all | |
2279 * the lines read so far and start all over again. | |
2280 * Otherwise give an error message later. | |
2281 */ | |
2282 else if (ff_error != EOL_DOS) | |
2283 { | |
2284 if ( try_unix | |
2285 && !read_stdin | |
2286 && (read_buffer | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
2287 || 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
|
2288 == 0)) |
7 | 2289 { |
2290 fileformat = EOL_UNIX; | |
819 | 2291 if (set_options) |
7 | 2292 set_fileformat(EOL_UNIX, OPT_LOCAL); |
2293 file_rewind = TRUE; | |
2294 keep_fileformat = TRUE; | |
2295 goto retry; | |
2296 } | |
2297 ff_error = EOL_DOS; | |
2298 } | |
2299 } | |
2300 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2301 { | |
2302 error = TRUE; | |
2303 break; | |
2304 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2305 #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
|
2306 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
|
2307 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
|
2308 #endif |
7 | 2309 ++lnum; |
2310 if (--read_count == 0) | |
2311 { | |
2312 error = TRUE; /* break loop */ | |
2313 line_start = ptr; /* nothing left to write */ | |
2314 break; | |
2315 } | |
2316 } | |
2317 else | |
2318 --skip_count; | |
2319 line_start = ptr + 1; | |
2320 } | |
2321 } | |
2322 } | |
2323 linerest = (long)(ptr - line_start); | |
2324 ui_breakcheck(); | |
2325 } | |
2326 | |
2327 failed: | |
2328 /* not an error, max. number of lines reached */ | |
2329 if (error && read_count == 0) | |
2330 error = FALSE; | |
2331 | |
2332 /* | |
2333 * If we get EOF in the middle of a line, note the fact and | |
2334 * complete the line ourselves. | |
2335 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set. | |
2336 */ | |
2337 if (!error | |
2338 && !got_int | |
2339 && linerest != 0 | |
2340 && !(!curbuf->b_p_bin | |
2341 && fileformat == EOL_DOS | |
2342 && *line_start == Ctrl_Z | |
2343 && ptr == line_start + 1)) | |
2344 { | |
819 | 2345 /* remember for when writing */ |
2346 if (set_options) | |
7 | 2347 curbuf->b_p_eol = FALSE; |
2348 *ptr = NUL; | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2349 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
|
2350 if (ml_append(lnum, line_start, len, newfile) == FAIL) |
7 | 2351 error = TRUE; |
2352 else | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2353 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2354 #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
|
2355 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
|
2356 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
|
2357 #endif |
7 | 2358 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
|
2359 } |
7 | 2360 } |
2361 | |
819 | 2362 if (set_options) |
7 | 2363 save_file_ff(curbuf); /* remember the current file format */ |
2364 | |
2365 #ifdef FEAT_CRYPT | |
6122 | 2366 if (curbuf->b_cryptstate != NULL) |
2367 { | |
2368 crypt_free_state(curbuf->b_cryptstate); | |
2369 curbuf->b_cryptstate = NULL; | |
2370 } | |
2371 if (cryptkey != NULL && cryptkey != curbuf->b_p_key) | |
2372 crypt_free_key(cryptkey); | |
2373 /* Don't set cryptkey to NULL, it's used below as a flag that | |
2374 * encryption was used. */ | |
7 | 2375 #endif |
2376 | |
2377 #ifdef FEAT_MBYTE | |
819 | 2378 /* If editing a new file: set 'fenc' for the current buffer. |
2379 * Also for ":read ++edit file". */ | |
2380 if (set_options) | |
7 | 2381 set_string_option_direct((char_u *)"fenc", -1, fenc, |
694 | 2382 OPT_FREE|OPT_LOCAL, 0); |
7 | 2383 if (fenc_alloced) |
2384 vim_free(fenc); | |
2385 # ifdef USE_ICONV | |
2386 if (iconv_fd != (iconv_t)-1) | |
2387 { | |
2388 iconv_close(iconv_fd); | |
2389 iconv_fd = (iconv_t)-1; | |
2390 } | |
2391 # endif | |
2392 #endif | |
2393 | |
2394 if (!read_buffer && !read_stdin) | |
2395 close(fd); /* errors are ignored */ | |
2003 | 2396 #ifdef HAVE_FD_CLOEXEC |
2397 else | |
2398 { | |
2399 int fdflags = fcntl(fd, F_GETFD); | |
2400 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
|
2401 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC); |
2003 | 2402 } |
2403 #endif | |
7 | 2404 vim_free(buffer); |
2405 | |
2406 #ifdef HAVE_DUP | |
2407 if (read_stdin) | |
2408 { | |
2409 /* Use stderr for stdin, makes shell commands work. */ | |
2410 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
|
2411 vim_ignored = dup(2); |
7 | 2412 } |
2413 #endif | |
2414 | |
2415 #ifdef FEAT_MBYTE | |
2416 if (tmpname != NULL) | |
2417 { | |
2418 mch_remove(tmpname); /* delete converted file */ | |
2419 vim_free(tmpname); | |
2420 } | |
2421 #endif | |
2422 --no_wait_return; /* may wait for return now */ | |
2423 | |
2424 /* | |
2425 * In recovery mode everything but autocommands is skipped. | |
2426 */ | |
2427 if (!recoverymode) | |
2428 { | |
2429 /* need to delete the last line, which comes from the empty buffer */ | |
2430 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY)) | |
2431 { | |
2432 #ifdef FEAT_NETBEANS_INTG | |
2433 netbeansFireChanges = 0; | |
2434 #endif | |
2435 ml_delete(curbuf->b_ml.ml_line_count, FALSE); | |
2436 #ifdef FEAT_NETBEANS_INTG | |
2437 netbeansFireChanges = 1; | |
2438 #endif | |
2439 --linecnt; | |
2440 } | |
2441 linecnt = curbuf->b_ml.ml_line_count - linecnt; | |
2442 if (filesize == 0) | |
2443 linecnt = 0; | |
2444 if (newfile || read_buffer) | |
1201 | 2445 { |
7 | 2446 redraw_curbuf_later(NOT_VALID); |
1201 | 2447 #ifdef FEAT_DIFF |
2448 /* After reading the text into the buffer the diff info needs to | |
2449 * be updated. */ | |
2450 diff_invalidate(curbuf); | |
2451 #endif | |
2452 #ifdef FEAT_FOLDING | |
2453 /* All folds in the window are invalid now. Mark them for update | |
2454 * before triggering autocommands. */ | |
2455 foldUpdateAll(curwin); | |
2456 #endif | |
2457 } | |
7 | 2458 else if (linecnt) /* appended at least one line */ |
2459 appended_lines_mark(from, linecnt); | |
2460 | |
2461 #ifndef ALWAYS_USE_GUI | |
2462 /* | |
2463 * If we were reading from the same terminal as where messages go, | |
2464 * the screen will have been messed up. | |
2465 * Switch on raw mode now and clear the screen. | |
2466 */ | |
2467 if (read_stdin) | |
2468 { | |
2469 settmode(TMODE_RAW); /* set to raw mode */ | |
2470 starttermcap(); | |
2471 screenclear(); | |
2472 } | |
2473 #endif | |
2474 | |
2475 if (got_int) | |
2476 { | |
2477 if (!(flags & READ_DUMMY)) | |
2478 { | |
2479 filemess(curbuf, sfname, (char_u *)_(e_interr), 0); | |
2480 if (newfile) | |
2481 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
2482 } | |
2483 msg_scroll = msg_save; | |
2484 #ifdef FEAT_VIMINFO | |
2485 check_marks_read(); | |
2486 #endif | |
2487 return OK; /* an interrupt isn't really an error */ | |
2488 } | |
2489 | |
2490 if (!filtering && !(flags & READ_DUMMY)) | |
2491 { | |
2492 msg_add_fname(curbuf, sfname); /* fname in IObuff with quotes */ | |
2493 c = FALSE; | |
2494 | |
2495 #ifdef UNIX | |
14509
80f715651c4c
patch 8.1.0268: file type checking has too many #ifdef
Christian Brabandt <cb@256bit.org>
parents:
14475
diff
changeset
|
2496 if (S_ISFIFO(perm)) /* fifo */ |
7 | 2497 { |
2498 STRCAT(IObuff, _("[fifo]")); | |
2499 c = TRUE; | |
2500 } | |
14509
80f715651c4c
patch 8.1.0268: file type checking has too many #ifdef
Christian Brabandt <cb@256bit.org>
parents:
14475
diff
changeset
|
2501 if (S_ISSOCK(perm)) /* or socket */ |
7 | 2502 { |
2503 STRCAT(IObuff, _("[socket]")); | |
2504 c = TRUE; | |
2505 } | |
1313 | 2506 # ifdef OPEN_CHR_FILES |
2507 if (S_ISCHR(perm)) /* or character special */ | |
2508 { | |
2509 STRCAT(IObuff, _("[character special]")); | |
2510 c = TRUE; | |
2511 } | |
2512 # endif | |
7 | 2513 #endif |
2514 if (curbuf->b_p_ro) | |
2515 { | |
2516 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")); | |
2517 c = TRUE; | |
2518 } | |
2519 if (read_no_eol_lnum) | |
2520 { | |
2521 msg_add_eol(); | |
2522 c = TRUE; | |
2523 } | |
2524 if (ff_error == EOL_DOS) | |
2525 { | |
2526 STRCAT(IObuff, _("[CR missing]")); | |
2527 c = TRUE; | |
2528 } | |
2529 if (split) | |
2530 { | |
2531 STRCAT(IObuff, _("[long lines split]")); | |
2532 c = TRUE; | |
2533 } | |
2534 #ifdef FEAT_MBYTE | |
2535 if (notconverted) | |
2536 { | |
2537 STRCAT(IObuff, _("[NOT converted]")); | |
2538 c = TRUE; | |
2539 } | |
2540 else if (converted) | |
2541 { | |
2542 STRCAT(IObuff, _("[converted]")); | |
2543 c = TRUE; | |
2544 } | |
2545 #endif | |
2546 #ifdef FEAT_CRYPT | |
2547 if (cryptkey != NULL) | |
2548 { | |
6122 | 2549 crypt_append_msg(curbuf); |
7 | 2550 c = TRUE; |
2551 } | |
2552 #endif | |
2553 #ifdef FEAT_MBYTE | |
595 | 2554 if (conv_error != 0) |
2555 { | |
2556 sprintf((char *)IObuff + STRLEN(IObuff), | |
2557 _("[CONVERSION ERROR in line %ld]"), (long)conv_error); | |
7 | 2558 c = TRUE; |
2559 } | |
2560 else if (illegal_byte > 0) | |
2561 { | |
2562 sprintf((char *)IObuff + STRLEN(IObuff), | |
2563 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte); | |
2564 c = TRUE; | |
2565 } | |
2566 else | |
2567 #endif | |
2568 if (error) | |
2569 { | |
2570 STRCAT(IObuff, _("[READ ERRORS]")); | |
2571 c = TRUE; | |
2572 } | |
2573 if (msg_add_fileformat(fileformat)) | |
2574 c = TRUE; | |
2575 #ifdef FEAT_CRYPT | |
2576 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
|
2577 msg_add_lines(c, (long)linecnt, filesize |
6122 | 2578 - crypt_get_header_len(crypt_get_method_nr(curbuf))); |
7 | 2579 else |
2580 #endif | |
2581 msg_add_lines(c, (long)linecnt, filesize); | |
2582 | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
2583 VIM_CLEAR(keep_msg); |
7 | 2584 msg_scrolled_ign = TRUE; |
2585 #ifdef ALWAYS_USE_GUI | |
2586 /* Don't show the message when reading stdin, it would end up in a | |
2587 * message box (which might be shown when exiting!) */ | |
2588 if (read_stdin || read_buffer) | |
2589 p = msg_may_trunc(FALSE, IObuff); | |
2590 else | |
2591 #endif | |
2592 p = msg_trunc_attr(IObuff, FALSE, 0); | |
2593 if (read_stdin || read_buffer || restart_edit != 0 | |
540 | 2594 || (msg_scrolled != 0 && !need_wait_return)) |
7 | 2595 /* Need to repeat the message after redrawing when: |
2596 * - When reading from stdin (the screen will be cleared next). | |
2597 * - When restart_edit is set (otherwise there will be a delay | |
2598 * before redrawing). | |
2599 * - When the screen was scrolled but there is no wait-return | |
2600 * prompt. */ | |
679 | 2601 set_keep_msg(p, 0); |
7 | 2602 msg_scrolled_ign = FALSE; |
2603 } | |
2604 | |
2605 /* with errors writing the file requires ":w!" */ | |
2606 if (newfile && (error | |
2607 #ifdef FEAT_MBYTE | |
595 | 2608 || conv_error != 0 |
679 | 2609 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP) |
7 | 2610 #endif |
2611 )) | |
2612 curbuf->b_p_ro = TRUE; | |
2613 | |
2614 u_clearline(); /* cannot use "U" command after adding lines */ | |
2615 | |
2616 /* | |
2617 * In Ex mode: cursor at last new line. | |
2618 * Otherwise: cursor at first new line. | |
2619 */ | |
2620 if (exmode_active) | |
2621 curwin->w_cursor.lnum = from + linecnt; | |
2622 else | |
2623 curwin->w_cursor.lnum = from + 1; | |
2624 check_cursor_lnum(); | |
2625 beginline(BL_WHITE | BL_FIX); /* on first non-blank */ | |
2626 | |
2627 /* | |
2628 * Set '[ and '] marks to the newly read lines. | |
2629 */ | |
2630 curbuf->b_op_start.lnum = from + 1; | |
2631 curbuf->b_op_start.col = 0; | |
2632 curbuf->b_op_end.lnum = from + linecnt; | |
2633 curbuf->b_op_end.col = 0; | |
696 | 2634 |
2635 #ifdef WIN32 | |
2636 /* | |
2637 * Work around a weird problem: When a file has two links (only | |
2638 * possible on NTFS) and we write through one link, then stat() it | |
1651 | 2639 * through the other link, the timestamp information may be wrong. |
696 | 2640 * It's correct again after reading the file, thus reset the timestamp |
2641 * here. | |
2642 */ | |
2643 if (newfile && !read_stdin && !read_buffer | |
2644 && mch_stat((char *)fname, &st) >= 0) | |
2645 { | |
2646 buf_store_time(curbuf, &st, fname); | |
2647 curbuf->b_mtime_read = curbuf->b_mtime; | |
2648 } | |
2649 #endif | |
7 | 2650 } |
2651 msg_scroll = msg_save; | |
2652 | |
2653 #ifdef FEAT_VIMINFO | |
2654 /* | |
2655 * Get the marks before executing autocommands, so they can be used there. | |
2656 */ | |
2657 check_marks_read(); | |
2658 #endif | |
2659 | |
2660 /* | |
6933 | 2661 * We remember if the last line of the read didn't have |
2662 * an eol even when 'binary' is off, to support turning 'fixeol' off, | |
2663 * or writing the read again with 'binary' on. The latter is required | |
2664 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work. | |
7 | 2665 */ |
2707 | 2666 curbuf->b_no_eol_lnum = read_no_eol_lnum; |
7 | 2667 |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2668 /* 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
|
2669 * different. */ |
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2670 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
|
2671 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
|
2672 |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2673 #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
|
2674 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2675 * 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
|
2676 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2677 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
|
2678 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2679 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
|
2680 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2681 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
|
2682 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
|
2683 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2684 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2685 |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
2686 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL)) |
7 | 2687 { |
2688 int m = msg_scroll; | |
2689 int n = msg_scrolled; | |
2690 | |
2691 /* Save the fileformat now, otherwise the buffer will be considered | |
2692 * modified if the format/encoding was automatically detected. */ | |
819 | 2693 if (set_options) |
7 | 2694 save_file_ff(curbuf); |
2695 | |
2696 /* | |
2697 * The output from the autocommands should not overwrite anything and | |
2698 * should not be overwritten: Set msg_scroll, restore its value if no | |
2699 * output was done. | |
2700 */ | |
2701 msg_scroll = TRUE; | |
2702 if (filtering) | |
2703 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname, | |
2704 FALSE, curbuf, eap); | |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
2705 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
|
2706 { |
7 | 2707 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname, |
2708 FALSE, curbuf, eap); | |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2709 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
|
2710 /* |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2711 * 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
|
2712 * 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
|
2713 */ |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2714 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
|
2715 TRUE, curbuf); |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2716 } |
7 | 2717 else |
2718 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname, | |
2719 FALSE, NULL, eap); | |
2720 if (msg_scrolled == n) | |
2721 msg_scroll = m; | |
2707 | 2722 # ifdef FEAT_EVAL |
7 | 2723 if (aborting()) /* autocmds may abort script processing */ |
2724 return FAIL; | |
2707 | 2725 # endif |
2726 } | |
2727 | |
7 | 2728 if (recoverymode && error) |
2729 return FAIL; | |
2730 return OK; | |
2731 } | |
2732 | |
9911
74e345d2878c
commit https://github.com/vim/vim/commit/f04507d132fbcb63999167ec006fc6e700b5af4f
Christian Brabandt <cb@256bit.org>
parents:
9828
diff
changeset
|
2733 #if defined(OPEN_CHR_FILES) || defined(PROTO) |
1313 | 2734 /* |
2735 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+", | |
2736 * which is the name of files used for process substitution output by | |
2737 * some shells on some operating systems, e.g., bash on SunOS. | |
2738 * Do not accept "/dev/fd/[012]", opening these may hang Vim. | |
2739 */ | |
9911
74e345d2878c
commit https://github.com/vim/vim/commit/f04507d132fbcb63999167ec006fc6e700b5af4f
Christian Brabandt <cb@256bit.org>
parents:
9828
diff
changeset
|
2740 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2741 is_dev_fd_file(char_u *fname) |
1313 | 2742 { |
2743 return (STRNCMP(fname, "/dev/fd/", 8) == 0 | |
2744 && VIM_ISDIGIT(fname[8]) | |
2745 && *skipdigits(fname + 9) == NUL | |
2746 && (fname[9] != NUL | |
2747 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2'))); | |
2748 } | |
2749 #endif | |
2750 | |
595 | 2751 #ifdef FEAT_MBYTE |
2752 | |
2753 /* | |
2754 * From the current line count and characters read after that, estimate the | |
2755 * line number where we are now. | |
2756 * Used for error messages that include a line number. | |
2757 */ | |
2758 static linenr_T | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2759 readfile_linenr( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2760 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
|
2761 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
|
2762 char_u *endp) /* end of more bytes read */ |
595 | 2763 { |
2764 char_u *s; | |
2765 linenr_T lnum; | |
2766 | |
2767 lnum = curbuf->b_ml.ml_line_count - linecnt + 1; | |
2768 for (s = p; s < endp; ++s) | |
2769 if (*s == '\n') | |
2770 ++lnum; | |
2771 return lnum; | |
2772 } | |
2773 #endif | |
2774 | |
7 | 2775 /* |
612 | 2776 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be |
2777 * equal to the buffer "buf". Used for calling readfile(). | |
7 | 2778 * Returns OK or FAIL. |
2779 */ | |
2780 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2781 prep_exarg(exarg_T *eap, buf_T *buf) |
7 | 2782 { |
13575
4df23d9bad47
patch 8.0.1660: the terminal API "drop" command doesn't support options
Christian Brabandt <cb@256bit.org>
parents:
13444
diff
changeset
|
2783 eap->cmd = alloc(15 |
7 | 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 + (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
|
2786 #endif |
4df23d9bad47
patch 8.0.1660: the terminal API "drop" command doesn't support options
Christian Brabandt <cb@256bit.org>
parents:
13444
diff
changeset
|
2787 ); |
7 | 2788 if (eap->cmd == NULL) |
2789 return FAIL; | |
2790 | |
2791 #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
|
2792 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
|
2793 eap->force_enc = 8; |
612 | 2794 eap->bad_char = buf->b_bad_char; |
7 | 2795 #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
|
2796 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
|
2797 #endif |
4df23d9bad47
patch 8.0.1660: the terminal API "drop" command doesn't support options
Christian Brabandt <cb@256bit.org>
parents:
13444
diff
changeset
|
2798 eap->force_ff = *buf->b_p_ff; |
612 | 2799 |
2800 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN; | |
819 | 2801 eap->read_edit = FALSE; |
612 | 2802 eap->forceit = FALSE; |
7 | 2803 return OK; |
2804 } | |
2805 | |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2806 /* |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2807 * Set default or forced 'fileformat' and 'binary'. |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2808 */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2809 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2810 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
|
2811 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2812 /* set default 'fileformat' */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2813 if (set_options) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2814 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2815 if (eap != NULL && eap->force_ff != 0) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2816 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
|
2817 else if (*p_ffs != NUL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2818 set_fileformat(default_fileformat(), OPT_LOCAL); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2819 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2820 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2821 /* set or reset 'binary' */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2822 if (eap != NULL && eap->force_bin != 0) |
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 int oldval = curbuf->b_p_bin; |
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 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
|
2827 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
|
2828 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2829 } |
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 defined(FEAT_MBYTE) || defined(PROTO) |
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 * Set forced 'fileencoding'. |
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 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2836 set_forced_fenc(exarg_T *eap) |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2837 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2838 if (eap->force_enc != 0) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2839 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2840 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
|
2841 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2842 if (fenc != NULL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2843 set_string_option_direct((char_u *)"fenc", -1, |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2844 fenc, OPT_FREE|OPT_LOCAL, 0); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2845 vim_free(fenc); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2846 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2847 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2848 |
7 | 2849 /* |
2850 * Find next fileencoding to use from 'fileencodings'. | |
2851 * "pp" points to fenc_next. It's advanced to the next item. | |
2852 * When there are no more items, an empty string is returned and *pp is set to | |
2853 * NULL. | |
2854 * When *pp is not set to NULL, the result is in allocated memory. | |
2855 */ | |
2856 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2857 next_fenc(char_u **pp) |
7 | 2858 { |
2859 char_u *p; | |
2860 char_u *r; | |
2861 | |
2862 if (**pp == NUL) | |
2863 { | |
2864 *pp = NULL; | |
2865 return (char_u *)""; | |
2866 } | |
2867 p = vim_strchr(*pp, ','); | |
2868 if (p == NULL) | |
2869 { | |
2870 r = enc_canonize(*pp); | |
2871 *pp += STRLEN(*pp); | |
2872 } | |
2873 else | |
2874 { | |
2875 r = vim_strnsave(*pp, (int)(p - *pp)); | |
2876 *pp = p + 1; | |
2877 if (r != NULL) | |
2878 { | |
2879 p = enc_canonize(r); | |
2880 vim_free(r); | |
2881 r = p; | |
2882 } | |
2883 } | |
2884 if (r == NULL) /* out of memory */ | |
2885 { | |
2886 r = (char_u *)""; | |
2887 *pp = NULL; | |
2888 } | |
2889 return r; | |
2890 } | |
2891 | |
2892 # ifdef FEAT_EVAL | |
2893 /* | |
2894 * Convert a file with the 'charconvert' expression. | |
2895 * This closes the file which is to be read, converts it and opens the | |
2896 * resulting file for reading. | |
2897 * Returns name of the resulting converted file (the caller should delete it | |
2898 * after reading it). | |
2899 * Returns NULL if the conversion failed ("*fdp" is not set) . | |
2900 */ | |
2901 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2902 readfile_charconvert( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2903 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
|
2904 char_u *fenc, /* converted from */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2905 int *fdp) /* in/out: file descriptor of file */ |
7 | 2906 { |
2907 char_u *tmpname; | |
2908 char_u *errmsg = NULL; | |
2909 | |
6721 | 2910 tmpname = vim_tempname('r', FALSE); |
7 | 2911 if (tmpname == NULL) |
2912 errmsg = (char_u *)_("Can't find temp file for conversion"); | |
2913 else | |
2914 { | |
2915 close(*fdp); /* close the input file, ignore errors */ | |
2916 *fdp = -1; | |
2917 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc, | |
2918 fname, tmpname) == FAIL) | |
2919 errmsg = (char_u *)_("Conversion with 'charconvert' failed"); | |
2920 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname, | |
2921 O_RDONLY | O_EXTRA, 0)) < 0) | |
2922 errmsg = (char_u *)_("can't read output of 'charconvert'"); | |
2923 } | |
2924 | |
2925 if (errmsg != NULL) | |
2926 { | |
2927 /* Don't use emsg(), it breaks mappings, the retry with | |
2928 * another type of conversion might still work. */ | |
2929 MSG(errmsg); | |
2930 if (tmpname != NULL) | |
2931 { | |
2932 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
|
2933 VIM_CLEAR(tmpname); |
7 | 2934 } |
2935 } | |
2936 | |
2937 /* If the input file is closed, open it (caller should check for error). */ | |
2938 if (*fdp < 0) | |
2939 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
2940 | |
2941 return tmpname; | |
2942 } | |
2943 # endif | |
2944 | |
2945 #endif | |
2946 | |
2947 #ifdef FEAT_VIMINFO | |
2948 /* | |
2949 * Read marks for the current buffer from the viminfo file, when we support | |
2950 * buffer marks and the buffer has a name. | |
2951 */ | |
2952 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2953 check_marks_read(void) |
7 | 2954 { |
2955 if (!curbuf->b_marks_read && get_viminfo_parameter('\'') > 0 | |
2956 && curbuf->b_ffname != NULL) | |
1733 | 2957 read_viminfo(NULL, VIF_WANT_MARKS); |
7 | 2958 |
2959 /* Always set b_marks_read; needed when 'viminfo' is changed to include | |
2960 * the ' parameter after opening a buffer. */ | |
2961 curbuf->b_marks_read = TRUE; | |
2962 } | |
2963 #endif | |
2964 | |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2965 #if defined(FEAT_CRYPT) || defined(PROTO) |
7 | 2966 /* |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2967 * Check for magic number used for encryption. Applies to the current buffer. |
7 | 2968 * If found, the magic number is removed from ptr[*sizep] and *sizep and |
2969 * *filesizep are updated. | |
2970 * Return the (new) encryption key, NULL for no encryption. | |
2971 */ | |
2972 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2973 check_for_cryptkey( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2974 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
|
2975 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
|
2976 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
|
2977 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
|
2978 int newfile, /* editing a new buffer */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2979 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
|
2980 int *did_ask) /* flag: whether already asked for key */ |
7 | 2981 { |
6122 | 2982 int method = crypt_method_nr_from_magic((char *)ptr, *sizep); |
5312 | 2983 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
|
2984 |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2985 if (method >= 0) |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2986 { |
5312 | 2987 /* Mark the buffer as read-only until the decryption has taken place. |
2988 * Avoids accidentally overwriting the file with garbage. */ | |
2989 curbuf->b_p_ro = TRUE; | |
2990 | |
6130 | 2991 /* Set the cryptmethod local to the buffer. */ |
6122 | 2992 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
|
2993 if (cryptkey == NULL && !*did_ask) |
7 | 2994 { |
2995 if (*curbuf->b_p_key) | |
2996 cryptkey = curbuf->b_p_key; | |
2997 else | |
2998 { | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2999 /* 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
|
3000 * 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
|
3001 * 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
|
3002 * Happens when retrying to detect encoding. */ |
2267 | 3003 smsg((char_u *)_(need_key_msg), fname); |
3004 msg_scroll = TRUE; | |
6353 | 3005 crypt_check_method(method); |
6122 | 3006 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
|
3007 *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
|
3008 |
7 | 3009 /* check if empty key entered */ |
3010 if (cryptkey != NULL && *cryptkey == NUL) | |
3011 { | |
3012 if (cryptkey != curbuf->b_p_key) | |
3013 vim_free(cryptkey); | |
3014 cryptkey = NULL; | |
3015 } | |
3016 } | |
3017 } | |
3018 | |
3019 if (cryptkey != NULL) | |
3020 { | |
6122 | 3021 int header_len; |
3022 | |
3023 curbuf->b_cryptstate = crypt_create_from_header( | |
3024 method, cryptkey, ptr); | |
3025 crypt_set_cm_option(curbuf, method); | |
3026 | |
3027 /* Remove cryptmethod specific header from the text. */ | |
3028 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
|
3029 if (*sizep <= header_len) |
fb1fde4fcff7
commit https://github.com/vim/vim/commit/680e015bfe19be6772d3bd754486fbd45c1a9d3b
Christian Brabandt <cb@256bit.org>
parents:
10086
diff
changeset
|
3030 /* 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
|
3031 return NULL; |
6122 | 3032 *filesizep += header_len; |
3033 *sizep -= header_len; | |
3034 mch_memmove(ptr, ptr + header_len, (size_t)*sizep); | |
3035 | |
5312 | 3036 /* Restore the read-only flag. */ |
3037 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
|
3038 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3039 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3040 /* 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
|
3041 * 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
|
3042 else if (newfile && *curbuf->b_p_key != NUL && !starting) |
7 | 3043 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL); |
3044 | |
3045 return cryptkey; | |
3046 } | |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3047 #endif /* FEAT_CRYPT */ |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3048 |
7 | 3049 #ifdef UNIX |
3050 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3051 set_file_time( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3052 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3053 time_t atime, /* access time */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3054 time_t mtime) /* modification time */ |
7 | 3055 { |
3056 # if defined(HAVE_UTIME) && defined(HAVE_UTIME_H) | |
3057 struct utimbuf buf; | |
3058 | |
3059 buf.actime = atime; | |
3060 buf.modtime = mtime; | |
3061 (void)utime((char *)fname, &buf); | |
3062 # else | |
3063 # if defined(HAVE_UTIMES) | |
3064 struct timeval tvp[2]; | |
3065 | |
3066 tvp[0].tv_sec = atime; | |
3067 tvp[0].tv_usec = 0; | |
3068 tvp[1].tv_sec = mtime; | |
3069 tvp[1].tv_usec = 0; | |
3070 # ifdef NeXT | |
3071 (void)utimes((char *)fname, tvp); | |
3072 # else | |
3073 (void)utimes((char *)fname, (const struct timeval *)&tvp); | |
3074 # endif | |
3075 # endif | |
3076 # endif | |
3077 } | |
3078 #endif /* UNIX */ | |
3079 | |
22 | 3080 #if defined(VMS) && !defined(MIN) |
3081 /* Older DECC compiler for VAX doesn't define MIN() */ | |
3082 # define MIN(a, b) ((a) < (b) ? (a) : (b)) | |
3083 #endif | |
3084 | |
7 | 3085 /* |
1303 | 3086 * Return TRUE if a file appears to be read-only from the file permissions. |
3087 */ | |
3088 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3089 check_file_readonly( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3090 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
|
3091 int perm) /* known permissions on file */ |
1303 | 3092 { |
3093 #ifndef USE_MCH_ACCESS | |
3094 int fd = 0; | |
3095 #endif | |
3096 | |
3097 return ( | |
3098 #ifdef USE_MCH_ACCESS | |
3099 # ifdef UNIX | |
3100 (perm & 0222) == 0 || | |
3101 # endif | |
3102 mch_access((char *)fname, W_OK) | |
3103 #else | |
3104 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0 | |
3105 ? TRUE : (close(fd), FALSE) | |
3106 #endif | |
3107 ); | |
3108 } | |
3109 | |
3110 | |
3111 /* | |
589 | 3112 * buf_write() - write to file "fname" lines "start" through "end" |
7 | 3113 * |
3114 * We do our own buffering here because fwrite() is so slow. | |
3115 * | |
589 | 3116 * If "forceit" is true, we don't care for errors when attempting backups. |
3117 * In case of an error everything possible is done to restore the original | |
1698 | 3118 * file. But when "forceit" is TRUE, we risk losing it. |
589 | 3119 * |
3120 * When "reset_changed" is TRUE and "append" == FALSE and "start" == 1 and | |
3121 * "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed. | |
7 | 3122 * |
3123 * This function must NOT use NameBuff (because it's called by autowrite()). | |
3124 * | |
3125 * return FAIL for failure, OK otherwise | |
3126 */ | |
3127 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3128 buf_write( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3129 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3130 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3131 char_u *sfname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3132 linenr_T start, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3133 linenr_T end, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3134 exarg_T *eap, /* for forced 'ff' and 'fenc', can be |
7 | 3135 NULL! */ |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3136 int append, /* append to the file */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3137 int forceit, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3138 int reset_changed, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3139 int filtering) |
7 | 3140 { |
3141 int fd; | |
3142 char_u *backup = NULL; | |
3143 int backup_copy = FALSE; /* copy the original file? */ | |
3144 int dobackup; | |
3145 char_u *ffname; | |
3146 char_u *wfname = NULL; /* name of file to write to */ | |
3147 char_u *s; | |
3148 char_u *ptr; | |
3149 char_u c; | |
3150 int len; | |
3151 linenr_T lnum; | |
3152 long nchars; | |
3153 char_u *errmsg = NULL; | |
1947 | 3154 int errmsg_allocated = FALSE; |
7 | 3155 char_u *errnum = NULL; |
3156 char_u *buffer; | |
3157 char_u smallbuf[SMBUFSIZE]; | |
3158 char_u *backup_ext; | |
3159 int bufsize; | |
3160 long perm; /* file permissions */ | |
3161 int retval = OK; | |
3162 int newfile = FALSE; /* TRUE if file doesn't exist yet */ | |
3163 int msg_save = msg_scroll; | |
3164 int overwriting; /* TRUE if writing over original */ | |
3165 int no_eol = FALSE; /* no end-of-line written */ | |
3166 int device = FALSE; /* writing to a device */ | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
3167 stat_T st_old; |
7 | 3168 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
|
3169 int checking_conversion; |
7 | 3170 int file_readonly = FALSE; /* overwritten file is read-only */ |
3171 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
|
3172 #if defined(UNIX) /*XXX fix me sometime? */ |
7 | 3173 int made_writable = FALSE; /* 'w' bit has been set */ |
3174 #endif | |
3175 /* writing everything */ | |
3176 int whole = (start == 1 && end == buf->b_ml.ml_line_count); | |
3177 linenr_T old_line_count = buf->b_ml.ml_line_count; | |
3178 int attr; | |
3179 int fileformat; | |
3180 int write_bin; | |
3181 struct bw_info write_info; /* info for buf_write_bytes() */ | |
3182 #ifdef FEAT_MBYTE | |
3183 int converted = FALSE; | |
3184 int notconverted = FALSE; | |
3185 char_u *fenc; /* effective 'fileencoding' */ | |
3186 char_u *fenc_tofree = NULL; /* allocated "fenc" */ | |
3187 #endif | |
3188 #ifdef HAS_BW_FLAGS | |
3189 int wb_flags = 0; | |
3190 #endif | |
3191 #ifdef HAVE_ACL | |
3192 vim_acl_T acl = NULL; /* ACL copied from original file to | |
3193 backup or new file */ | |
3194 #endif | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3195 #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
|
3196 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
|
3197 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
|
3198 #endif |
6243 | 3199 unsigned int bkc = get_bkc_value(buf); |
7 | 3200 |
3201 if (fname == NULL || *fname == NUL) /* safety check */ | |
3202 return FAIL; | |
2028 | 3203 if (buf->b_ml.ml_mfp == NULL) |
3204 { | |
3205 /* This can happen during startup when there is a stray "w" in the | |
3206 * vimrc file. */ | |
3207 EMSG(_(e_emptybuf)); | |
3208 return FAIL; | |
3209 } | |
7 | 3210 |
3211 /* | |
3212 * Disallow writing from .exrc and .vimrc in current directory for | |
3213 * security reasons. | |
3214 */ | |
3215 if (check_secure()) | |
3216 return FAIL; | |
3217 | |
3218 /* Avoid a crash for a long name. */ | |
3219 if (STRLEN(fname) >= MAXPATHL) | |
3220 { | |
3221 EMSG(_(e_longname)); | |
3222 return FAIL; | |
3223 } | |
3224 | |
3225 #ifdef FEAT_MBYTE | |
3226 /* must init bw_conv_buf and bw_iconv_fd before jumping to "fail" */ | |
3227 write_info.bw_conv_buf = NULL; | |
3228 write_info.bw_conv_error = FALSE; | |
1947 | 3229 write_info.bw_conv_error_lnum = 0; |
7 | 3230 write_info.bw_restlen = 0; |
3231 # ifdef USE_ICONV | |
3232 write_info.bw_iconv_fd = (iconv_t)-1; | |
3233 # endif | |
3234 #endif | |
6122 | 3235 #ifdef FEAT_CRYPT |
3236 write_info.bw_buffer = buf; | |
3237 #endif | |
7 | 3238 |
167 | 3239 /* After writing a file changedtick changes but we don't want to display |
3240 * the line. */ | |
3241 ex_no_reprint = TRUE; | |
3242 | |
7 | 3243 /* |
3244 * If there is no file name yet, use the one for the written file. | |
3245 * BF_NOTEDITED is set to reflect this (in case the write fails). | |
3246 * Don't do this when the write is for a filter command. | |
589 | 3247 * Don't do this when appending. |
3248 * Only do this when 'cpoptions' contains the 'F' flag. | |
7 | 3249 */ |
633 | 3250 if (buf->b_ffname == NULL |
3251 && reset_changed | |
7 | 3252 && whole |
3253 && buf == curbuf | |
236 | 3254 #ifdef FEAT_QUICKFIX |
3255 && !bt_nofile(buf) | |
3256 #endif | |
7 | 3257 && !filtering |
589 | 3258 && (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL) |
7 | 3259 && vim_strchr(p_cpo, CPO_FNAMEW) != NULL) |
3260 { | |
633 | 3261 if (set_rw_fname(fname, sfname) == FAIL) |
7 | 3262 return FAIL; |
633 | 3263 buf = curbuf; /* just in case autocmds made "buf" invalid */ |
7 | 3264 } |
3265 | |
3266 if (sfname == NULL) | |
3267 sfname = fname; | |
3268 /* | |
3269 * For Unix: Use the short file name whenever possible. | |
3270 * Avoids problems with networks and when directory names are changed. | |
3271 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to | |
3272 * another directory, which we don't detect | |
3273 */ | |
3274 ffname = fname; /* remember full fname */ | |
3275 #ifdef UNIX | |
3276 fname = sfname; | |
3277 #endif | |
3278 | |
3279 if (buf->b_ffname != NULL && fnamecmp(ffname, buf->b_ffname) == 0) | |
3280 overwriting = TRUE; | |
3281 else | |
3282 overwriting = FALSE; | |
3283 | |
3284 if (exiting) | |
4352 | 3285 settmode(TMODE_COOK); /* when exiting allow typeahead now */ |
7 | 3286 |
3287 ++no_wait_return; /* don't wait for return yet */ | |
3288 | |
3289 /* | |
3290 * Set '[ and '] marks to the lines to be written. | |
3291 */ | |
3292 buf->b_op_start.lnum = start; | |
3293 buf->b_op_start.col = 0; | |
3294 buf->b_op_end.lnum = end; | |
3295 buf->b_op_end.col = 0; | |
3296 | |
3297 { | |
3298 aco_save_T aco; | |
3299 int buf_ffname = FALSE; | |
3300 int buf_sfname = FALSE; | |
3301 int buf_fname_f = FALSE; | |
3302 int buf_fname_s = FALSE; | |
3303 int did_cmd = FALSE; | |
17 | 3304 int nofile_err = FALSE; |
161 | 3305 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
|
3306 bufref_T bufref; |
7 | 3307 |
3308 /* | |
4352 | 3309 * Apply PRE autocommands. |
7 | 3310 * Set curbuf to the buffer to be written. |
3311 * Careful: The autocommands may call buf_write() recursively! | |
3312 */ | |
3313 if (ffname == buf->b_ffname) | |
3314 buf_ffname = TRUE; | |
3315 if (sfname == buf->b_sfname) | |
3316 buf_sfname = TRUE; | |
3317 if (fname == buf->b_ffname) | |
3318 buf_fname_f = TRUE; | |
3319 if (fname == buf->b_sfname) | |
3320 buf_fname_s = TRUE; | |
3321 | |
3322 /* set curwin/curbuf to buf and save a few things */ | |
3323 aucmd_prepbuf(&aco, buf); | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
3324 set_bufref(&bufref, buf); |
7 | 3325 |
3326 if (append) | |
3327 { | |
3328 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD, | |
3329 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3330 { |
635 | 3331 #ifdef FEAT_QUICKFIX |
18 | 3332 if (overwriting && bt_nofile(curbuf)) |
17 | 3333 nofile_err = TRUE; |
3334 else | |
635 | 3335 #endif |
17 | 3336 apply_autocmds_exarg(EVENT_FILEAPPENDPRE, |
7 | 3337 sfname, sfname, FALSE, curbuf, eap); |
17 | 3338 } |
7 | 3339 } |
3340 else if (filtering) | |
3341 { | |
3342 apply_autocmds_exarg(EVENT_FILTERWRITEPRE, | |
3343 NULL, sfname, FALSE, curbuf, eap); | |
3344 } | |
3345 else if (reset_changed && whole) | |
3346 { | |
3036 | 3347 int was_changed = curbufIsChanged(); |
3348 | |
3349 did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD, | |
3350 sfname, sfname, FALSE, curbuf, eap); | |
3351 if (did_cmd) | |
3352 { | |
3353 if (was_changed && !curbufIsChanged()) | |
3354 { | |
3355 /* Written everything correctly and BufWriteCmd has reset | |
3356 * 'modified': Correct the undo information so that an | |
3357 * undo now sets 'modified'. */ | |
3358 u_unchanged(curbuf); | |
3359 u_update_save_nr(curbuf); | |
3360 } | |
3361 } | |
3362 else | |
17 | 3363 { |
635 | 3364 #ifdef FEAT_QUICKFIX |
179 | 3365 if (overwriting && bt_nofile(curbuf)) |
17 | 3366 nofile_err = TRUE; |
3367 else | |
635 | 3368 #endif |
17 | 3369 apply_autocmds_exarg(EVENT_BUFWRITEPRE, |
7 | 3370 sfname, sfname, FALSE, curbuf, eap); |
17 | 3371 } |
7 | 3372 } |
3373 else | |
3374 { | |
3375 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD, | |
3376 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3377 { |
635 | 3378 #ifdef FEAT_QUICKFIX |
179 | 3379 if (overwriting && bt_nofile(curbuf)) |
17 | 3380 nofile_err = TRUE; |
3381 else | |
635 | 3382 #endif |
17 | 3383 apply_autocmds_exarg(EVENT_FILEWRITEPRE, |
7 | 3384 sfname, sfname, FALSE, curbuf, eap); |
17 | 3385 } |
7 | 3386 } |
3387 | |
3388 /* restore curwin/curbuf and a few other things */ | |
3389 aucmd_restbuf(&aco); | |
3390 | |
3391 /* | |
3392 * In three situations we return here and don't write the file: | |
3393 * 1. the autocommands deleted or unloaded the buffer. | |
3394 * 2. The autocommands abort script processing. | |
3395 * 3. If one of the "Cmd" autocommands was executed. | |
3396 */ | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
3397 if (!bufref_valid(&bufref)) |
7 | 3398 buf = NULL; |
161 | 3399 if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline) |
532 | 3400 || did_cmd || nofile_err |
3401 #ifdef FEAT_EVAL | |
3402 || aborting() | |
3403 #endif | |
3404 ) | |
7 | 3405 { |
3406 --no_wait_return; | |
3407 msg_scroll = msg_save; | |
17 | 3408 if (nofile_err) |
3409 EMSG(_("E676: No matching autocommands for acwrite buffer")); | |
3410 | |
532 | 3411 if (nofile_err |
3412 #ifdef FEAT_EVAL | |
3413 || aborting() | |
3414 #endif | |
3415 ) | |
7 | 3416 /* An aborting error, interrupt or exception in the |
3417 * autocommands. */ | |
3418 return FAIL; | |
3419 if (did_cmd) | |
3420 { | |
3421 if (buf == NULL) | |
3422 /* The buffer was deleted. We assume it was written | |
3423 * (can't retry anyway). */ | |
3424 return OK; | |
3425 if (overwriting) | |
3426 { | |
3427 /* Assume the buffer was written, update the timestamp. */ | |
3428 ml_timestamp(buf); | |
589 | 3429 if (append) |
3430 buf->b_flags &= ~BF_NEW; | |
3431 else | |
3432 buf->b_flags &= ~BF_WRITE_MASK; | |
7 | 3433 } |
589 | 3434 if (reset_changed && buf->b_changed && !append |
39 | 3435 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)) |
7 | 3436 /* Buffer still changed, the autocommands didn't work |
3437 * properly. */ | |
3438 return FAIL; | |
3439 return OK; | |
3440 } | |
3441 #ifdef FEAT_EVAL | |
3442 if (!aborting()) | |
3443 #endif | |
3444 EMSG(_("E203: Autocommands deleted or unloaded buffer to be written")); | |
3445 return FAIL; | |
3446 } | |
3447 | |
3448 /* | |
3449 * The autocommands may have changed the number of lines in the file. | |
3450 * When writing the whole file, adjust the end. | |
3451 * When writing part of the file, assume that the autocommands only | |
3452 * changed the number of lines that are to be written (tricky!). | |
3453 */ | |
3454 if (buf->b_ml.ml_line_count != old_line_count) | |
3455 { | |
3456 if (whole) /* write all */ | |
3457 end = buf->b_ml.ml_line_count; | |
3458 else if (buf->b_ml.ml_line_count > old_line_count) /* more lines */ | |
3459 end += buf->b_ml.ml_line_count - old_line_count; | |
3460 else /* less lines */ | |
3461 { | |
3462 end -= old_line_count - buf->b_ml.ml_line_count; | |
3463 if (end < start) | |
3464 { | |
3465 --no_wait_return; | |
3466 msg_scroll = msg_save; | |
3467 EMSG(_("E204: Autocommand changed number of lines in unexpected way")); | |
3468 return FAIL; | |
3469 } | |
3470 } | |
3471 } | |
3472 | |
3473 /* | |
3474 * The autocommands may have changed the name of the buffer, which may | |
3475 * be kept in fname, ffname and sfname. | |
3476 */ | |
3477 if (buf_ffname) | |
3478 ffname = buf->b_ffname; | |
3479 if (buf_sfname) | |
3480 sfname = buf->b_sfname; | |
3481 if (buf_fname_f) | |
3482 fname = buf->b_ffname; | |
3483 if (buf_fname_s) | |
3484 fname = buf->b_sfname; | |
3485 } | |
3486 | |
3487 #ifdef FEAT_NETBEANS_INTG | |
2210 | 3488 if (netbeans_active() && isNetbeansBuffer(buf)) |
7 | 3489 { |
3490 if (whole) | |
3491 { | |
3492 /* | |
3493 * b_changed can be 0 after an undo, but we still need to write | |
3494 * the buffer to NetBeans. | |
3495 */ | |
3496 if (buf->b_changed || isNetbeansModified(buf)) | |
3497 { | |
33 | 3498 --no_wait_return; /* may wait for return now */ |
3499 msg_scroll = msg_save; | |
3500 netbeans_save_buffer(buf); /* no error checking... */ | |
7 | 3501 return retval; |
3502 } | |
3503 else | |
3504 { | |
3505 errnum = (char_u *)"E656: "; | |
1653 | 3506 errmsg = (char_u *)_("NetBeans disallows writes of unmodified buffers"); |
7 | 3507 buffer = NULL; |
3508 goto fail; | |
3509 } | |
3510 } | |
3511 else | |
3512 { | |
3513 errnum = (char_u *)"E657: "; | |
3514 errmsg = (char_u *)_("Partial writes disallowed for NetBeans buffers"); | |
3515 buffer = NULL; | |
3516 goto fail; | |
3517 } | |
3518 } | |
3519 #endif | |
3520 | |
3521 if (shortmess(SHM_OVER) && !exiting) | |
3522 msg_scroll = FALSE; /* overwrite previous file message */ | |
3523 else | |
3524 msg_scroll = TRUE; /* don't overwrite previous file message */ | |
3525 if (!filtering) | |
3526 filemess(buf, | |
3527 #ifndef UNIX | |
3528 sfname, | |
3529 #else | |
3530 fname, | |
3531 #endif | |
3532 (char_u *)"", 0); /* show that we are busy */ | |
3533 msg_scroll = FALSE; /* always overwrite the file message now */ | |
3534 | |
3535 buffer = alloc(BUFSIZE); | |
3536 if (buffer == NULL) /* can't allocate big buffer, use small | |
3537 * one (to be able to write when out of | |
3538 * memory) */ | |
3539 { | |
3540 buffer = smallbuf; | |
3541 bufsize = SMBUFSIZE; | |
3542 } | |
3543 else | |
3544 bufsize = BUFSIZE; | |
3545 | |
3546 /* | |
3547 * Get information about original file (if there is one). | |
3548 */ | |
7410
08e62c4fc17d
commit https://github.com/vim/vim/commit/53076830fea6df737455523f7e235bfe4f79864d
Christian Brabandt <cb@256bit.org>
parents:
7408
diff
changeset
|
3549 #if defined(UNIX) |
1438 | 3550 st_old.st_dev = 0; |
3551 st_old.st_ino = 0; | |
7 | 3552 perm = -1; |
3553 if (mch_stat((char *)fname, &st_old) < 0) | |
3554 newfile = TRUE; | |
3555 else | |
3556 { | |
3557 perm = st_old.st_mode; | |
3558 if (!S_ISREG(st_old.st_mode)) /* not a file */ | |
3559 { | |
3560 if (S_ISDIR(st_old.st_mode)) | |
3561 { | |
3562 errnum = (char_u *)"E502: "; | |
3563 errmsg = (char_u *)_("is a directory"); | |
3564 goto fail; | |
3565 } | |
3566 if (mch_nodetype(fname) != NODE_WRITABLE) | |
3567 { | |
3568 errnum = (char_u *)"E503: "; | |
3569 errmsg = (char_u *)_("is not a file or writable device"); | |
3570 goto fail; | |
3571 } | |
3572 /* It's a device of some kind (or a fifo) which we can write to | |
3573 * but for which we can't make a backup. */ | |
3574 device = TRUE; | |
3575 newfile = TRUE; | |
3576 perm = -1; | |
3577 } | |
3578 } | |
3579 #else /* !UNIX */ | |
3580 /* | |
3581 * Check for a writable device name. | |
3582 */ | |
3583 c = mch_nodetype(fname); | |
3584 if (c == NODE_OTHER) | |
3585 { | |
3586 errnum = (char_u *)"E503: "; | |
3587 errmsg = (char_u *)_("is not a file or writable device"); | |
3588 goto fail; | |
3589 } | |
3590 if (c == NODE_WRITABLE) | |
3591 { | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3592 # if defined(MSWIN) |
1004 | 3593 /* MS-Windows allows opening a device, but we will probably get stuck |
3594 * trying to write to it. */ | |
3595 if (!p_odev) | |
3596 { | |
3597 errnum = (char_u *)"E796: "; | |
3598 errmsg = (char_u *)_("writing to device disabled with 'opendevice' option"); | |
3599 goto fail; | |
3600 } | |
3601 # endif | |
7 | 3602 device = TRUE; |
3603 newfile = TRUE; | |
3604 perm = -1; | |
3605 } | |
3606 else | |
3607 { | |
3608 perm = mch_getperm(fname); | |
3609 if (perm < 0) | |
3610 newfile = TRUE; | |
3611 else if (mch_isdir(fname)) | |
3612 { | |
3613 errnum = (char_u *)"E502: "; | |
3614 errmsg = (char_u *)_("is a directory"); | |
3615 goto fail; | |
3616 } | |
3617 if (overwriting) | |
3618 (void)mch_stat((char *)fname, &st_old); | |
3619 } | |
3620 #endif /* !UNIX */ | |
3621 | |
3622 if (!device && !newfile) | |
3623 { | |
3624 /* | |
3625 * Check if the file is really writable (when renaming the file to | |
3626 * make a backup we won't discover it later). | |
3627 */ | |
1303 | 3628 file_readonly = check_file_readonly(fname, (int)perm); |
3629 | |
7 | 3630 if (!forceit && file_readonly) |
3631 { | |
3632 if (vim_strchr(p_cpo, CPO_FWRITE) != NULL) | |
3633 { | |
3634 errnum = (char_u *)"E504: "; | |
3635 errmsg = (char_u *)_(err_readonly); | |
3636 } | |
3637 else | |
3638 { | |
3639 errnum = (char_u *)"E505: "; | |
3640 errmsg = (char_u *)_("is read-only (add ! to override)"); | |
3641 } | |
3642 goto fail; | |
3643 } | |
3644 | |
3645 /* | |
3646 * Check if the timestamp hasn't changed since reading the file. | |
3647 */ | |
3648 if (overwriting) | |
3649 { | |
3650 retval = check_mtime(buf, &st_old); | |
3651 if (retval == FAIL) | |
3652 goto fail; | |
3653 } | |
3654 } | |
3655 | |
3656 #ifdef HAVE_ACL | |
3657 /* | |
3658 * For systems that support ACL: get the ACL from the original file. | |
3659 */ | |
3660 if (!newfile) | |
3661 acl = mch_get_acl(fname); | |
3662 #endif | |
3663 | |
3664 /* | |
3665 * If 'backupskip' is not empty, don't make a backup for some files. | |
3666 */ | |
3667 dobackup = (p_wb || p_bk || *p_pm != NUL); | |
3668 #ifdef FEAT_WILDIGN | |
3669 if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname)) | |
3670 dobackup = FALSE; | |
3671 #endif | |
3672 | |
3673 /* | |
3674 * Save the value of got_int and reset it. We don't want a previous | |
3675 * interruption cancel writing, only hitting CTRL-C while writing should | |
3676 * abort it. | |
3677 */ | |
3678 prev_got_int = got_int; | |
3679 got_int = FALSE; | |
3680 | |
3681 /* Mark the buffer as 'being saved' to prevent changed buffer warnings */ | |
3682 buf->b_saving = TRUE; | |
3683 | |
3684 /* | |
3685 * If we are not appending or filtering, the file exists, and the | |
3686 * 'writebackup', 'backup' or 'patchmode' option is set, need a backup. | |
3687 * When 'patchmode' is set also make a backup when appending. | |
3688 * | |
3689 * Do not make any backup, if 'writebackup' and 'backup' are both switched | |
3690 * off. This helps when editing large files on almost-full disks. | |
3691 */ | |
3692 if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup) | |
3693 { | |
3694 #if defined(UNIX) || defined(WIN32) | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
3695 stat_T st; |
7 | 3696 #endif |
3697 | |
6243 | 3698 if ((bkc & BKC_YES) || append) /* "yes" */ |
7 | 3699 backup_copy = TRUE; |
3700 #if defined(UNIX) || defined(WIN32) | |
6243 | 3701 else if ((bkc & BKC_AUTO)) /* "auto" */ |
7 | 3702 { |
3703 int i; | |
3704 | |
3705 # ifdef UNIX | |
3706 /* | |
3707 * Don't rename the file when: | |
3708 * - it's a hard link | |
3709 * - it's a symbolic link | |
3710 * - we don't have write permission in the directory | |
3711 * - we can't set the owner/group of the new file | |
3712 */ | |
3713 if (st_old.st_nlink > 1 | |
3714 || mch_lstat((char *)fname, &st) < 0 | |
3715 || st.st_dev != st_old.st_dev | |
557 | 3716 || st.st_ino != st_old.st_ino |
3717 # ifndef HAVE_FCHOWN | |
3718 || st.st_uid != st_old.st_uid | |
3719 || st.st_gid != st_old.st_gid | |
3720 # endif | |
3721 ) | |
7 | 3722 backup_copy = TRUE; |
3723 else | |
696 | 3724 # else |
3725 # ifdef WIN32 | |
3726 /* On NTFS file systems hard links are possible. */ | |
3727 if (mch_is_linked(fname)) | |
3728 backup_copy = TRUE; | |
3729 else | |
3730 # endif | |
7 | 3731 # endif |
3732 { | |
3733 /* | |
3734 * Check if we can create a file and set the owner/group to | |
3735 * the ones from the original file. | |
3736 * First find a file name that doesn't exist yet (use some | |
3737 * arbitrary numbers). | |
3738 */ | |
3739 STRCPY(IObuff, fname); | |
3740 for (i = 4913; ; i += 123) | |
3741 { | |
3742 sprintf((char *)gettail(IObuff), "%d", i); | |
557 | 3743 if (mch_lstat((char *)IObuff, &st) < 0) |
7 | 3744 break; |
3745 } | |
557 | 3746 fd = mch_open((char *)IObuff, |
3747 O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm); | |
7 | 3748 if (fd < 0) /* can't write in directory */ |
3749 backup_copy = TRUE; | |
3750 else | |
3751 { | |
3752 # ifdef UNIX | |
557 | 3753 # 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
|
3754 vim_ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
557 | 3755 # endif |
7 | 3756 if (mch_stat((char *)IObuff, &st) < 0 |
3757 || st.st_uid != st_old.st_uid | |
3758 || st.st_gid != st_old.st_gid | |
1877 | 3759 || (long)st.st_mode != perm) |
7 | 3760 backup_copy = TRUE; |
3761 # endif | |
567 | 3762 /* Close the file before removing it, on MS-Windows we |
3763 * can't delete an open file. */ | |
3764 close(fd); | |
7 | 3765 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
|
3766 # 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
|
3767 /* 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
|
3768 * 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
|
3769 * second. */ |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3770 { |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3771 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
|
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 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
|
3774 { |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3775 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
|
3776 break; |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3777 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
|
3778 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
|
3779 } |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3780 } |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3781 # endif |
7 | 3782 } |
3783 } | |
3784 } | |
3785 | |
3786 /* | |
3787 * Break symlinks and/or hardlinks if we've been asked to. | |
3788 */ | |
6243 | 3789 if ((bkc & BKC_BREAKSYMLINK) || (bkc & BKC_BREAKHARDLINK)) |
7 | 3790 { |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3791 # ifdef UNIX |
7 | 3792 int lstat_res; |
3793 | |
3794 lstat_res = mch_lstat((char *)fname, &st); | |
3795 | |
3796 /* Symlinks. */ | |
6243 | 3797 if ((bkc & BKC_BREAKSYMLINK) |
7 | 3798 && lstat_res == 0 |
3799 && st.st_ino != st_old.st_ino) | |
3800 backup_copy = FALSE; | |
3801 | |
3802 /* Hardlinks. */ | |
6243 | 3803 if ((bkc & BKC_BREAKHARDLINK) |
7 | 3804 && st_old.st_nlink > 1 |
3805 && (lstat_res != 0 || st.st_ino == st_old.st_ino)) | |
3806 backup_copy = FALSE; | |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3807 # else |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3808 # if defined(WIN32) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3809 /* Symlinks. */ |
6243 | 3810 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
|
3811 backup_copy = FALSE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3812 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3813 /* Hardlinks. */ |
6243 | 3814 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
|
3815 backup_copy = FALSE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3816 # endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3817 # endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3818 } |
7 | 3819 |
3820 #endif | |
3821 | |
3822 /* make sure we have a valid backup extension to use */ | |
3823 if (*p_bex == NUL) | |
3824 backup_ext = (char_u *)".bak"; | |
3825 else | |
3826 backup_ext = p_bex; | |
3827 | |
3828 if (backup_copy | |
3829 && (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) >= 0) | |
3830 { | |
3831 int bfd; | |
3832 char_u *copybuf, *wp; | |
3833 int some_error = FALSE; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
3834 stat_T st_new; |
7 | 3835 char_u *dirp; |
3836 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
|
3837 #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
|
3838 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
|
3839 #endif |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3840 #if defined(UNIX) |
7 | 3841 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
|
3842 mode_t umask_save; |
7 | 3843 #endif |
3844 | |
3845 copybuf = alloc(BUFSIZE + 1); | |
3846 if (copybuf == NULL) | |
3847 { | |
3848 some_error = TRUE; /* out of memory */ | |
3849 goto nobackup; | |
3850 } | |
3851 | |
3852 /* | |
3853 * Try to make the backup in each directory in the 'bdir' option. | |
3854 * | |
3855 * Unix semantics has it, that we may have a writable file, | |
3856 * that cannot be recreated with a simple open(..., O_CREAT, ) e.g: | |
3857 * - the directory is not writable, | |
3858 * - the file may be a symbolic link, | |
3859 * - the file may belong to another user/group, etc. | |
3860 * | |
3861 * For these reasons, the existing writable file must be truncated | |
3862 * and reused. Creation of a backup COPY will be attempted. | |
3863 */ | |
3864 dirp = p_bdir; | |
3865 while (*dirp) | |
3866 { | |
3867 #ifdef UNIX | |
3868 st_new.st_ino = 0; | |
3869 st_new.st_dev = 0; | |
3870 st_new.st_gid = 0; | |
3871 #endif | |
3872 | |
3873 /* | |
3874 * Isolate one directory name, using an entry in 'bdir'. | |
3875 */ | |
3876 (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
|
3877 |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3878 #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
|
3879 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
|
3880 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
|
3881 // 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
|
3882 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
|
3883 { |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3884 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
|
3885 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
|
3886 } |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3887 #endif |
7 | 3888 rootname = get_file_in_dir(fname, copybuf); |
3889 if (rootname == NULL) | |
3890 { | |
3891 some_error = TRUE; /* out of memory */ | |
3892 goto nobackup; | |
3893 } | |
3894 | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3895 #if defined(UNIX) |
7 | 3896 did_set_shortname = FALSE; |
3897 #endif | |
3898 | |
3899 /* | |
3900 * May try twice if 'shortname' not set. | |
3901 */ | |
3902 for (;;) | |
3903 { | |
3904 /* | |
14475
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3905 * Make the backup file name. |
7 | 3906 */ |
14475
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3907 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
|
3908 backup = buf_modname((buf->b_p_sn || buf->b_shortname), |
7 | 3909 rootname, backup_ext, FALSE); |
3910 if (backup == NULL) | |
3911 { | |
3912 vim_free(rootname); | |
3913 some_error = TRUE; /* out of memory */ | |
3914 goto nobackup; | |
3915 } | |
3916 | |
3917 /* | |
3918 * Check if backup file already exists. | |
3919 */ | |
3920 if (mch_stat((char *)backup, &st_new) >= 0) | |
3921 { | |
3922 #ifdef UNIX | |
3923 /* | |
3924 * Check if backup file is same as original file. | |
3925 * May happen when modname() gave the same file back. | |
3926 * E.g. silly link, or file name-length reached. | |
3927 * If we don't check here, we either ruin the file | |
3928 * when copying or erase it after writing. jw. | |
3929 */ | |
3930 if (st_new.st_dev == st_old.st_dev | |
3931 && st_new.st_ino == st_old.st_ino) | |
3932 { | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
3933 VIM_CLEAR(backup); /* no backup file to delete */ |
7 | 3934 /* |
3935 * may try again with 'shortname' set | |
3936 */ | |
3937 if (!(buf->b_shortname || buf->b_p_sn)) | |
3938 { | |
3939 buf->b_shortname = TRUE; | |
3940 did_set_shortname = TRUE; | |
3941 continue; | |
3942 } | |
3943 /* setting shortname didn't help */ | |
3944 if (did_set_shortname) | |
3945 buf->b_shortname = FALSE; | |
3946 break; | |
3947 } | |
3948 #endif | |
3949 | |
3950 /* | |
3951 * If we are not going to keep the backup file, don't | |
3952 * delete an existing one, try to use another name. | |
3953 * Change one character, just before the extension. | |
3954 */ | |
3955 if (!p_bk) | |
3956 { | |
3957 wp = backup + STRLEN(backup) - 1 | |
3958 - STRLEN(backup_ext); | |
3959 if (wp < backup) /* empty file name ??? */ | |
3960 wp = backup; | |
3961 *wp = 'z'; | |
3962 while (*wp > 'a' | |
3963 && mch_stat((char *)backup, &st_new) >= 0) | |
3964 --*wp; | |
3965 /* They all exist??? Must be something wrong. */ | |
3966 if (*wp == 'a') | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
3967 VIM_CLEAR(backup); |
7 | 3968 } |
3969 } | |
3970 break; | |
3971 } | |
3972 vim_free(rootname); | |
3973 | |
3974 /* | |
3975 * Try to create the backup file | |
3976 */ | |
3977 if (backup != NULL) | |
3978 { | |
3979 /* remove old backup, if present */ | |
3980 mch_remove(backup); | |
3981 /* 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
|
3982 * 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
|
3983 * 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
|
3984 #ifdef UNIX |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
3985 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
|
3986 #endif |
7 | 3987 bfd = mch_open((char *)backup, |
557 | 3988 O_WRONLY|O_CREAT|O_EXTRA|O_EXCL|O_NOFOLLOW, |
3989 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
|
3990 #ifdef UNIX |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
3991 (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
|
3992 #endif |
7 | 3993 if (bfd < 0) |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
3994 VIM_CLEAR(backup); |
7 | 3995 else |
3996 { | |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
3997 /* 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
|
3998 * 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
|
3999 * above. */ |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4000 #ifndef UNIX |
7 | 4001 (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
|
4002 #else |
7 | 4003 /* |
4004 * Try to set the group of the backup same as the | |
4005 * original file. If this fails, set the protection | |
4006 * bits for the group same as the protection bits for | |
4007 * others. | |
4008 */ | |
557 | 4009 if (st_new.st_gid != st_old.st_gid |
7 | 4010 # ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */ |
557 | 4011 && fchown(bfd, (uid_t)-1, st_old.st_gid) != 0 |
7 | 4012 # endif |
4013 ) | |
4014 mch_setperm(backup, | |
4015 (perm & 0707) | ((perm & 07) << 3)); | |
5788 | 4016 # if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
1583 | 4017 mch_copy_sec(fname, backup); |
4018 # endif | |
7 | 4019 #endif |
4020 | |
4021 /* | |
4022 * copy the file. | |
4023 */ | |
4024 write_info.bw_fd = bfd; | |
4025 write_info.bw_buf = copybuf; | |
4026 #ifdef HAS_BW_FLAGS | |
4027 write_info.bw_flags = FIO_NOCONVERT; | |
4028 #endif | |
2664 | 4029 while ((write_info.bw_len = read_eintr(fd, copybuf, |
7 | 4030 BUFSIZE)) > 0) |
4031 { | |
4032 if (buf_write_bytes(&write_info) == FAIL) | |
4033 { | |
4034 errmsg = (char_u *)_("E506: Can't write to backup file (add ! to override)"); | |
4035 break; | |
4036 } | |
4037 ui_breakcheck(); | |
4038 if (got_int) | |
4039 { | |
4040 errmsg = (char_u *)_(e_interr); | |
4041 break; | |
4042 } | |
4043 } | |
4044 | |
4045 if (close(bfd) < 0 && errmsg == NULL) | |
4046 errmsg = (char_u *)_("E507: Close error for backup file (add ! to override)"); | |
4047 if (write_info.bw_len < 0) | |
4048 errmsg = (char_u *)_("E508: Can't read file for backup (add ! to override)"); | |
4049 #ifdef UNIX | |
4050 set_file_time(backup, st_old.st_atime, st_old.st_mtime); | |
4051 #endif | |
4052 #ifdef HAVE_ACL | |
4053 mch_set_acl(backup, acl); | |
4054 #endif | |
5788 | 4055 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
1583 | 4056 mch_copy_sec(fname, backup); |
4057 #endif | |
7 | 4058 break; |
4059 } | |
4060 } | |
4061 } | |
4062 nobackup: | |
4063 close(fd); /* ignore errors for closing read file */ | |
4064 vim_free(copybuf); | |
4065 | |
4066 if (backup == NULL && errmsg == NULL) | |
4067 errmsg = (char_u *)_("E509: Cannot create backup file (add ! to override)"); | |
4068 /* ignore errors when forceit is TRUE */ | |
4069 if ((some_error || errmsg != NULL) && !forceit) | |
4070 { | |
4071 retval = FAIL; | |
4072 goto fail; | |
4073 } | |
4074 errmsg = NULL; | |
4075 } | |
4076 else | |
4077 { | |
4078 char_u *dirp; | |
4079 char_u *p; | |
4080 char_u *rootname; | |
4081 | |
4082 /* | |
4083 * Make a backup by renaming the original file. | |
4084 */ | |
4085 /* | |
4086 * If 'cpoptions' includes the "W" flag, we don't want to | |
4087 * overwrite a read-only file. But rename may be possible | |
4088 * anyway, thus we need an extra check here. | |
4089 */ | |
4090 if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL) | |
4091 { | |
4092 errnum = (char_u *)"E504: "; | |
4093 errmsg = (char_u *)_(err_readonly); | |
4094 goto fail; | |
4095 } | |
4096 | |
4097 /* | |
4098 * | |
4099 * Form the backup file name - change path/fo.o.h to | |
4100 * path/fo.o.h.bak Try all directories in 'backupdir', first one | |
4101 * that works is used. | |
4102 */ | |
4103 dirp = p_bdir; | |
4104 while (*dirp) | |
4105 { | |
4106 /* | |
4107 * Isolate one directory name and make the backup file name. | |
4108 */ | |
4109 (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
|
4110 |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4111 #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
|
4112 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
|
4113 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
|
4114 // 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
|
4115 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
|
4116 { |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4117 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
|
4118 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
|
4119 } |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4120 #endif |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4121 if (backup == NULL) |
7 | 4122 { |
14475
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4123 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
|
4124 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
|
4125 backup = NULL; |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4126 else |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4127 { |
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
4128 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
|
4129 (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
|
4130 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
|
4131 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
|
4132 } |
7 | 4133 } |
4134 | |
4135 if (backup != NULL) | |
4136 { | |
4137 /* | |
4138 * If we are not going to keep the backup file, don't | |
4139 * delete an existing one, try to use another name. | |
4140 * Change one character, just before the extension. | |
4141 */ | |
4142 if (!p_bk && mch_getperm(backup) >= 0) | |
4143 { | |
4144 p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext); | |
4145 if (p < backup) /* empty file name ??? */ | |
4146 p = backup; | |
4147 *p = 'z'; | |
4148 while (*p > 'a' && mch_getperm(backup) >= 0) | |
4149 --*p; | |
4150 /* They all exist??? Must be something wrong! */ | |
4151 if (*p == 'a') | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
4152 VIM_CLEAR(backup); |
7 | 4153 } |
4154 } | |
4155 if (backup != NULL) | |
4156 { | |
4157 /* | |
531 | 4158 * Delete any existing backup and move the current version |
4159 * to the backup. For safety, we don't remove the backup | |
4160 * until the write has finished successfully. And if the | |
4161 * 'backup' option is set, leave it around. | |
7 | 4162 */ |
4163 /* | |
4164 * If the renaming of the original file to the backup file | |
4165 * works, quit here. | |
4166 */ | |
4167 if (vim_rename(fname, backup) == 0) | |
4168 break; | |
4169 | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
4170 VIM_CLEAR(backup); /* don't do the rename below */ |
7 | 4171 } |
4172 } | |
4173 if (backup == NULL && !forceit) | |
4174 { | |
4175 errmsg = (char_u *)_("E510: Can't make backup file (add ! to override)"); | |
4176 goto fail; | |
4177 } | |
4178 } | |
4179 } | |
4180 | |
7410
08e62c4fc17d
commit https://github.com/vim/vim/commit/53076830fea6df737455523f7e235bfe4f79864d
Christian Brabandt <cb@256bit.org>
parents:
7408
diff
changeset
|
4181 #if defined(UNIX) |
7 | 4182 /* When using ":w!" and the file was read-only: make it writable */ |
4183 if (forceit && perm >= 0 && !(perm & 0200) && st_old.st_uid == getuid() | |
4184 && vim_strchr(p_cpo, CPO_FWRITE) == NULL) | |
4185 { | |
4186 perm |= 0200; | |
4187 (void)mch_setperm(fname, perm); | |
4188 made_writable = TRUE; | |
4189 } | |
4190 #endif | |
4191 | |
819 | 4192 /* When using ":w!" and writing to the current file, 'readonly' makes no |
164 | 4193 * sense, reset it, unless 'Z' appears in 'cpoptions'. */ |
4194 if (forceit && overwriting && vim_strchr(p_cpo, CPO_KEEPRO) == NULL) | |
7 | 4195 { |
4196 buf->b_p_ro = FALSE; | |
4197 #ifdef FEAT_TITLE | |
4198 need_maketitle = TRUE; /* set window title later */ | |
4199 #endif | |
4200 status_redraw_all(); /* redraw status lines later */ | |
4201 } | |
4202 | |
4203 if (end > buf->b_ml.ml_line_count) | |
4204 end = buf->b_ml.ml_line_count; | |
4205 if (buf->b_ml.ml_flags & ML_EMPTY) | |
4206 start = end + 1; | |
4207 | |
4208 /* | |
4209 * If the original file is being overwritten, there is a small chance that | |
4210 * we crash in the middle of writing. Therefore the file is preserved now. | |
4211 * This makes all block numbers positive so that recovery does not need | |
4212 * the original file. | |
4213 * Don't do this if there is a backup file and we are exiting. | |
4214 */ | |
39 | 4215 if (reset_changed && !newfile && overwriting |
7 | 4216 && !(exiting && backup != NULL)) |
4217 { | |
4218 ml_preserve(buf, FALSE); | |
4219 if (got_int) | |
4220 { | |
4221 errmsg = (char_u *)_(e_interr); | |
4222 goto restore_backup; | |
4223 } | |
4224 } | |
4225 | |
4226 #ifdef VMS | |
4227 vms_remove_version(fname); /* remove version */ | |
4228 #endif | |
1651 | 4229 /* Default: write the file directly. May write to a temp file for |
7 | 4230 * multi-byte conversion. */ |
4231 wfname = fname; | |
4232 | |
4233 #ifdef FEAT_MBYTE | |
4234 /* Check for forced 'fileencoding' from "++opt=val" argument. */ | |
4235 if (eap != NULL && eap->force_enc != 0) | |
4236 { | |
4237 fenc = eap->cmd + eap->force_enc; | |
4238 fenc = enc_canonize(fenc); | |
4239 fenc_tofree = fenc; | |
4240 } | |
4241 else | |
4242 fenc = buf->b_p_fenc; | |
4243 | |
4244 /* | |
1948 | 4245 * Check if the file needs to be converted. |
7 | 4246 */ |
1948 | 4247 converted = need_conversion(fenc); |
7 | 4248 |
4249 /* | |
4250 * Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done. Or | |
4251 * Latin1 to Unicode conversion. This is handled in buf_write_bytes(). | |
4252 * Prepare the flags for it and allocate bw_conv_buf when needed. | |
4253 */ | |
4254 if (converted && (enc_utf8 || STRCMP(p_enc, "latin1") == 0)) | |
4255 { | |
4256 wb_flags = get_fio_flags(fenc); | |
4257 if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8)) | |
4258 { | |
4259 /* Need to allocate a buffer to translate into. */ | |
4260 if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8)) | |
4261 write_info.bw_conv_buflen = bufsize * 2; | |
4262 else /* FIO_UCS4 */ | |
4263 write_info.bw_conv_buflen = bufsize * 4; | |
4264 write_info.bw_conv_buf | |
4265 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4266 if (write_info.bw_conv_buf == NULL) | |
4267 end = 0; | |
4268 } | |
4269 } | |
4270 | |
4271 # ifdef WIN3264 | |
4272 if (converted && wb_flags == 0 && (wb_flags = get_win_fio_flags(fenc)) != 0) | |
4273 { | |
4274 /* Convert UTF-8 -> UCS-2 and UCS-2 -> DBCS. Worst-case * 4: */ | |
4275 write_info.bw_conv_buflen = bufsize * 4; | |
4276 write_info.bw_conv_buf | |
4277 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4278 if (write_info.bw_conv_buf == NULL) | |
4279 end = 0; | |
4280 } | |
4281 # endif | |
4282 | |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
4283 # ifdef MACOS_CONVERT |
7 | 4284 if (converted && wb_flags == 0 && (wb_flags = get_mac_fio_flags(fenc)) != 0) |
4285 { | |
4286 write_info.bw_conv_buflen = bufsize * 3; | |
4287 write_info.bw_conv_buf | |
4288 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4289 if (write_info.bw_conv_buf == NULL) | |
4290 end = 0; | |
4291 } | |
4292 # endif | |
4293 | |
4294 # if defined(FEAT_EVAL) || defined(USE_ICONV) | |
4295 if (converted && wb_flags == 0) | |
4296 { | |
4297 # ifdef USE_ICONV | |
4298 /* | |
4299 * Use iconv() conversion when conversion is needed and it's not done | |
4300 * internally. | |
4301 */ | |
4302 write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc, | |
4303 enc_utf8 ? (char_u *)"utf-8" : p_enc); | |
4304 if (write_info.bw_iconv_fd != (iconv_t)-1) | |
4305 { | |
4306 /* We're going to use iconv(), allocate a buffer to convert in. */ | |
4307 write_info.bw_conv_buflen = bufsize * ICONV_MULT; | |
4308 write_info.bw_conv_buf | |
4309 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4310 if (write_info.bw_conv_buf == NULL) | |
4311 end = 0; | |
4312 write_info.bw_first = TRUE; | |
4313 } | |
4314 # ifdef FEAT_EVAL | |
4315 else | |
4316 # endif | |
4317 # endif | |
4318 | |
4319 # ifdef FEAT_EVAL | |
4320 /* | |
4321 * When the file needs to be converted with 'charconvert' after | |
4322 * writing, write to a temp file instead and let the conversion | |
4323 * overwrite the original file. | |
4324 */ | |
4325 if (*p_ccv != NUL) | |
4326 { | |
6721 | 4327 wfname = vim_tempname('w', FALSE); |
7 | 4328 if (wfname == NULL) /* Can't write without a tempfile! */ |
4329 { | |
4330 errmsg = (char_u *)_("E214: Can't find temp file for writing"); | |
4331 goto restore_backup; | |
4332 } | |
4333 } | |
4334 # endif | |
4335 } | |
4336 # endif | |
4337 if (converted && wb_flags == 0 | |
4338 # ifdef USE_ICONV | |
4339 && write_info.bw_iconv_fd == (iconv_t)-1 | |
4340 # endif | |
4341 # ifdef FEAT_EVAL | |
4342 && wfname == fname | |
4343 # endif | |
4344 ) | |
4345 { | |
4346 if (!forceit) | |
4347 { | |
4348 errmsg = (char_u *)_("E213: Cannot convert (add ! to write without conversion)"); | |
4349 goto restore_backup; | |
4350 } | |
4351 notconverted = TRUE; | |
4352 } | |
4353 #endif | |
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 * 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
|
4357 * 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
|
4358 * When not doing conversion this writes for real right away. |
7 | 4359 */ |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4360 for (checking_conversion = TRUE; ; checking_conversion = FALSE) |
7 | 4361 { |
4362 /* | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4363 * 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
|
4364 * - 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
|
4365 * - 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
|
4366 * failure. |
7 | 4367 */ |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4368 #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
|
4369 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
|
4370 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4371 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
|
4372 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4373 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
|
4374 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4375 /* 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
|
4376 fd = -1; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4377 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
|
4378 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4379 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4380 { |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4381 #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
|
4382 # 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
|
4383 #else |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4384 # 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
|
4385 #endif |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4386 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4387 * 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
|
4388 * 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
|
4389 * 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
|
4390 * 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
|
4391 * 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
|
4392 * 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
|
4393 * 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
|
4394 * FALSE. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4395 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4396 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
|
4397 ? (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
|
4398 : (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
|
4399 , 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
|
4400 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4401 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4402 * 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
|
4403 * 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
|
4404 * 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
|
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 (errmsg == NULL) |
7 | 4407 { |
4408 #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
|
4409 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
|
4410 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4411 /* 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
|
4412 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4413 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
|
4414 || (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
|
4415 && (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
|
4416 || 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
|
4417 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
|
4418 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4419 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4420 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4421 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
|
4422 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
|
4423 && perm >= 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4424 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4425 #ifdef UNIX |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4426 /* 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
|
4427 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
|
4428 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
|
4429 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
|
4430 perm |= 0200; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4431 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
|
4432 || 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
|
4433 perm &= 0777; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4434 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4435 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
|
4436 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
|
4437 continue; |
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 } |
7 | 4440 } |
4441 | |
4442 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
|
4443 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4444 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
|
4445 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4446 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4447 * 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
|
4448 * 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
|
4449 * 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
|
4450 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4451 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
|
4452 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4453 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
|
4454 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4455 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4456 * 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
|
4457 * 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
|
4458 * 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
|
4459 * 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
|
4460 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4461 /* 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
|
4462 * place */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4463 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
|
4464 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
|
4465 /* 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
|
4466 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4467 if (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
|
4468 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
|
4469 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4470 else |
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 /* 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
|
4473 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
|
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 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4477 /* 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
|
4478 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4479 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
|
4480 end = 0; |
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 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4483 #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
|
4484 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
|
4485 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
|
4486 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4487 goto fail; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4488 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4489 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
|
4490 |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4491 #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
|
4492 { |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4493 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
|
4494 |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4495 /* 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
|
4496 * any changes. */ |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4497 if (overwriting |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4498 && (!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
|
4499 && fname == wfname |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4500 && perm >= 0 |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4501 && 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
|
4502 && 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
|
4503 { |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4504 close(fd); |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4505 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
|
4506 goto fail; |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4507 } |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4508 } |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4509 #endif |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4510 #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
|
4511 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
|
4512 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
|
4513 #endif |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4514 |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
4515 #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
|
4516 if (backup != NULL && overwriting && !append) |
7 | 4517 { |
4518 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
|
4519 (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
|
4520 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4521 (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
|
4522 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4523 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4524 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
|
4525 { |
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_ffname != NULL) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4527 (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
|
4528 /* 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
|
4529 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4530 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4531 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4532 #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
|
4533 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
|
4534 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4535 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
|
4536 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
|
4537 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4538 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
|
4539 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
|
4540 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
|
4541 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
|
4542 end = 0; |
7 | 4543 else |
4544 { | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4545 /* 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
|
4546 * 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
|
4547 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
|
4548 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
|
4549 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
|
4550 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
|
4551 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4552 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
|
4553 vim_free(header); |
7 | 4554 } |
4555 } | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4556 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4557 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4558 errmsg = NULL; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4559 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4560 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
|
4561 nchars = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4562 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4563 /* 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
|
4564 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
|
4565 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
|
4566 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4567 write_bin = buf->b_p_bin; |
7 | 4568 |
4569 #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
|
4570 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4571 * 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
|
4572 * 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
|
4573 * 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
|
4574 */ |
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->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
|
4576 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4577 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
|
4578 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
|
4579 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4580 /* 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
|
4581 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
|
4582 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
|
4583 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4584 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4585 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
|
4586 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4587 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4588 write_info.bw_start_lnum = start; |
7 | 4589 #endif |
4590 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4591 #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
|
4592 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
|
4593 && overwriting |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4594 && !append |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4595 && !filtering |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4596 && reset_changed |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4597 && !checking_conversion); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4598 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
|
4599 /* 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
|
4600 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
|
4601 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4602 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4603 write_info.bw_len = bufsize; |
7 | 4604 #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
|
4605 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
|
4606 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4607 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
|
4608 s = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4609 len = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4610 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
|
4611 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4612 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4613 * 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
|
4614 * 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
|
4615 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4616 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
|
4617 #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
|
4618 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
|
4619 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
|
4620 (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
|
4621 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4622 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
|
4623 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4624 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
|
4625 *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
|
4626 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
|
4627 *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
|
4628 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4629 *s = c; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4630 ++s; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4631 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
|
4632 continue; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4633 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
|
4634 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4635 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
|
4636 break; |
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 nchars += bufsize; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4639 s = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4640 len = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4641 #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
|
4642 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
|
4643 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4644 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4645 /* 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
|
4646 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
|
4647 || (lnum == end |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4648 && (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
|
4649 && (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
|
4650 || (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
|
4651 && !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
|
4652 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4653 ++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
|
4654 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
|
4655 break; |
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 (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
|
4658 *s++ = NL; |
7 | 4659 else |
11605
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 *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
|
4662 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
|
4663 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4664 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
|
4665 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4666 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
|
4667 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4668 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
|
4669 break; |
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 nchars += bufsize; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4672 s = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4673 len = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4674 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4675 *s++ = NL; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4676 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4677 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4678 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
|
4679 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4680 if (buf_write_bytes(&write_info) == FAIL) |
7 | 4681 { |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4682 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
|
4683 break; |
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 nchars += bufsize; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4686 s = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4687 len = 0; |
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 ui_breakcheck(); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4690 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
|
4691 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4692 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
|
4693 break; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4694 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4695 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4696 #ifdef VMS |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4697 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4698 * 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
|
4699 * 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
|
4700 * 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
|
4701 * 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
|
4702 * 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
|
4703 * 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
|
4704 * 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
|
4705 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4706 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
|
4707 || ((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
|
4708 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4709 int b2write; |
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 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
|
4712 ? 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
|
4713 : 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
|
4714 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4715 b2write = len; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4716 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
|
4717 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4718 write_info.bw_len = MIN(b2write, buf->b_fab_mrs); |
7 | 4719 if (buf_write_bytes(&write_info) == FAIL) |
4720 { | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4721 end = 0; |
7 | 4722 break; |
4723 } | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4724 b2write -= MIN(b2write, buf->b_fab_mrs); |
7 | 4725 } |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4726 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
|
4727 nchars += len; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4728 s = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4729 len = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4730 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4731 #endif |
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 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
|
4734 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4735 write_info.bw_len = len; |
7 | 4736 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
|
4737 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
|
4738 nchars += len; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4739 } |
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 /* 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
|
4742 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
|
4743 break; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4744 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4745 /* 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
|
4746 * 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
|
4747 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4748 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4749 /* If 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
|
4750 * encountered. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4751 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
|
4752 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4753 #if defined(UNIX) && defined(HAVE_FSYNC) |
7 | 4754 /* |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4755 * 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
|
4756 * 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
|
4757 * 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
|
4758 * 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
|
4759 * 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
|
4760 * 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
|
4761 * 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
|
4762 * If the 'fsync' option is FALSE, don't fsync(). Useful for laptops. |
7 | 4763 */ |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4764 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
|
4765 { |
12857
ffdf2e4b5d9a
patch 8.0.1305: writefile() never calls fsync()
Christian Brabandt <cb@256bit.org>
parents:
12847
diff
changeset
|
4766 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
|
4767 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4768 } |
7 | 4769 #endif |
4770 | |
5788 | 4771 #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
|
4772 /* 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
|
4773 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
|
4774 mch_copy_sec(backup, wfname); |
1583 | 4775 #endif |
4776 | |
557 | 4777 #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
|
4778 /* 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
|
4779 * 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
|
4780 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
|
4781 { |
557 | 4782 # 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
|
4783 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
|
4784 |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4785 /* 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
|
4786 * 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
|
4787 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
|
4788 || 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
|
4789 || 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
|
4790 { |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4791 /* 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
|
4792 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
|
4793 /* 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
|
4794 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
|
4795 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
|
4796 } |
557 | 4797 # endif |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4798 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
|
4799 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4800 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
|
4801 /* 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
|
4802 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
|
4803 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4804 |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4805 #ifdef UNIX |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4806 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
|
4807 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
|
4808 #endif |
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4809 #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
|
4810 /* 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
|
4811 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
|
4812 (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
|
4813 #endif |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4814 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
|
4815 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4816 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
|
4817 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4818 } |
7 | 4819 |
12847
14f287552218
patch 8.0.1300: file permissions may end up wrong when writing
Christian Brabandt <cb@256bit.org>
parents:
12814
diff
changeset
|
4820 #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
|
4821 /* 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
|
4822 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
|
4823 (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
|
4824 #endif |
7 | 4825 #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
|
4826 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4827 * 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
|
4828 * 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
|
4829 * 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
|
4830 * 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
|
4831 * 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
|
4832 * 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
|
4833 */ |
9509
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
4834 # 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
|
4835 if (!backup_copy) |
9509
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
4836 # endif |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4837 mch_set_acl(wfname, acl); |
7 | 4838 #endif |
2267 | 4839 #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
|
4840 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
|
4841 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4842 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
|
4843 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
|
4844 } |
6122 | 4845 #endif |
7 | 4846 |
4847 #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
|
4848 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
|
4849 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4850 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4851 * 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
|
4852 * 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
|
4853 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4854 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
|
4855 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4856 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
|
4857 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
|
4858 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4859 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
|
4860 end = 0; |
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 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4863 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
|
4864 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
|
4865 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4866 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4867 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4868 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4869 if (end == 0) |
7 | 4870 { |
4871 /* | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4872 * Error encountered. |
7 | 4873 */ |
4874 if (errmsg == NULL) | |
4875 { | |
4876 #ifdef FEAT_MBYTE | |
4877 if (write_info.bw_conv_error) | |
1947 | 4878 { |
4879 if (write_info.bw_conv_error_lnum == 0) | |
4880 errmsg = (char_u *)_("E513: write error, conversion failed (make 'fenc' empty to override)"); | |
4881 else | |
4882 { | |
4883 errmsg_allocated = TRUE; | |
4884 errmsg = alloc(300); | |
4885 vim_snprintf((char *)errmsg, 300, _("E513: write error, conversion failed in line %ld (make 'fenc' empty to override)"), | |
4886 (long)write_info.bw_conv_error_lnum); | |
4887 } | |
4888 } | |
7 | 4889 else |
4890 #endif | |
4891 if (got_int) | |
4892 errmsg = (char_u *)_(e_interr); | |
4893 else | |
4894 errmsg = (char_u *)_("E514: write error (file system full?)"); | |
4895 } | |
4896 | |
4897 /* | |
4898 * If we have a backup file, try to put it in place of the new file, | |
1698 | 4899 * because the new file is probably corrupt. This avoids losing the |
7 | 4900 * original file when trying to make a backup when writing the file a |
4901 * second time. | |
4902 * When "backup_copy" is set we need to copy the backup over the new | |
4903 * file. Otherwise rename the backup file. | |
4904 * If this is OK, don't give the extra warning message. | |
4905 */ | |
4906 if (backup != NULL) | |
4907 { | |
4908 if (backup_copy) | |
4909 { | |
4910 /* This may take a while, if we were interrupted let the user | |
4911 * know we got the message. */ | |
4912 if (got_int) | |
4913 { | |
4914 MSG(_(e_interr)); | |
4915 out_flush(); | |
4916 } | |
4917 if ((fd = mch_open((char *)backup, O_RDONLY | O_EXTRA, 0)) >= 0) | |
4918 { | |
4919 if ((write_info.bw_fd = mch_open((char *)fname, | |
194 | 4920 O_WRONLY | O_CREAT | O_TRUNC | O_EXTRA, |
4921 perm & 0777)) >= 0) | |
7 | 4922 { |
4923 /* copy the file. */ | |
4924 write_info.bw_buf = smallbuf; | |
4925 #ifdef HAS_BW_FLAGS | |
4926 write_info.bw_flags = FIO_NOCONVERT; | |
4927 #endif | |
2664 | 4928 while ((write_info.bw_len = read_eintr(fd, smallbuf, |
7 | 4929 SMBUFSIZE)) > 0) |
4930 if (buf_write_bytes(&write_info) == FAIL) | |
4931 break; | |
4932 | |
4933 if (close(write_info.bw_fd) >= 0 | |
4934 && write_info.bw_len == 0) | |
4935 end = 1; /* success */ | |
4936 } | |
4937 close(fd); /* ignore errors for closing read file */ | |
4938 } | |
4939 } | |
4940 else | |
4941 { | |
4942 if (vim_rename(backup, fname) == 0) | |
4943 end = 1; | |
4944 } | |
4945 } | |
4946 goto fail; | |
4947 } | |
4948 | |
4949 lnum -= start; /* compute number of written lines */ | |
4950 --no_wait_return; /* may wait for return now */ | |
4951 | |
4952 #if !(defined(UNIX) || defined(VMS)) | |
4953 fname = sfname; /* use shortname now, for the messages */ | |
4954 #endif | |
4955 if (!filtering) | |
4956 { | |
4957 msg_add_fname(buf, fname); /* put fname in IObuff with quotes */ | |
4958 c = FALSE; | |
4959 #ifdef FEAT_MBYTE | |
4960 if (write_info.bw_conv_error) | |
4961 { | |
4962 STRCAT(IObuff, _(" CONVERSION ERROR")); | |
4963 c = TRUE; | |
1947 | 4964 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
|
4965 vim_snprintf_add((char *)IObuff, IOSIZE, _(" in line %ld;"), |
1947 | 4966 (long)write_info.bw_conv_error_lnum); |
7 | 4967 } |
4968 else if (notconverted) | |
4969 { | |
4970 STRCAT(IObuff, _("[NOT converted]")); | |
4971 c = TRUE; | |
4972 } | |
4973 else if (converted) | |
4974 { | |
4975 STRCAT(IObuff, _("[converted]")); | |
4976 c = TRUE; | |
4977 } | |
4978 #endif | |
4979 if (device) | |
4980 { | |
4981 STRCAT(IObuff, _("[Device]")); | |
4982 c = TRUE; | |
4983 } | |
4984 else if (newfile) | |
4985 { | |
4986 STRCAT(IObuff, shortmess(SHM_NEW) ? _("[New]") : _("[New File]")); | |
4987 c = TRUE; | |
4988 } | |
4989 if (no_eol) | |
4990 { | |
4991 msg_add_eol(); | |
4992 c = TRUE; | |
4993 } | |
4994 /* may add [unix/dos/mac] */ | |
4995 if (msg_add_fileformat(fileformat)) | |
4996 c = TRUE; | |
4997 #ifdef FEAT_CRYPT | |
4998 if (wb_flags & FIO_ENCRYPTED) | |
4999 { | |
6122 | 5000 crypt_append_msg(buf); |
7 | 5001 c = TRUE; |
5002 } | |
5003 #endif | |
5004 msg_add_lines(c, (long)lnum, nchars); /* add line/char count */ | |
5005 if (!shortmess(SHM_WRITE)) | |
5006 { | |
5007 if (append) | |
5008 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [a]") : _(" appended")); | |
5009 else | |
5010 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [w]") : _(" written")); | |
5011 } | |
5012 | |
679 | 5013 set_keep_msg(msg_trunc_attr(IObuff, FALSE, 0), 0); |
7 | 5014 } |
5015 | |
39 | 5016 /* When written everything correctly: reset 'modified'. Unless not |
5017 * writing to the original file and '+' is not in 'cpoptions'. */ | |
589 | 5018 if (reset_changed && whole && !append |
7 | 5019 #ifdef FEAT_MBYTE |
5020 && !write_info.bw_conv_error | |
5021 #endif | |
39 | 5022 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL) |
5023 ) | |
7 | 5024 { |
5025 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
|
5026 /* b:changedtick is always incremented in unchanged() but that |
6320 | 5027 * 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
|
5028 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
|
5029 buf->b_last_changedtick = CHANGEDTICK(buf); |
7 | 5030 u_unchanged(buf); |
2281
e41433ea71df
Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents:
2280
diff
changeset
|
5031 u_update_save_nr(buf); |
7 | 5032 } |
5033 | |
5034 /* | |
5035 * If written to the current file, update the timestamp of the swap file | |
5036 * and reset the BF_WRITE_MASK flags. Also sets buf->b_mtime. | |
5037 */ | |
5038 if (overwriting) | |
5039 { | |
5040 ml_timestamp(buf); | |
589 | 5041 if (append) |
5042 buf->b_flags &= ~BF_NEW; | |
5043 else | |
5044 buf->b_flags &= ~BF_WRITE_MASK; | |
7 | 5045 } |
5046 | |
5047 /* | |
5048 * If we kept a backup until now, and we are in patch mode, then we make | |
5049 * the backup file our 'original' file. | |
5050 */ | |
5051 if (*p_pm && dobackup) | |
5052 { | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
5053 char *org = (char *)buf_modname((buf->b_p_sn || buf->b_shortname), |
7 | 5054 fname, p_pm, FALSE); |
5055 | |
5056 if (backup != NULL) | |
5057 { | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
5058 stat_T st; |
7 | 5059 |
5060 /* | |
5061 * If the original file does not exist yet | |
5062 * the current backup file becomes the original file | |
5063 */ | |
5064 if (org == NULL) | |
5065 EMSG(_("E205: Patchmode: can't save original file")); | |
5066 else if (mch_stat(org, &st) < 0) | |
5067 { | |
5068 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
|
5069 VIM_CLEAR(backup); /* don't delete the file */ |
7 | 5070 #ifdef UNIX |
5071 set_file_time((char_u *)org, st_old.st_atime, st_old.st_mtime); | |
5072 #endif | |
5073 } | |
5074 } | |
5075 /* | |
5076 * If there is no backup file, remember that a (new) file was | |
5077 * created. | |
5078 */ | |
5079 else | |
5080 { | |
5081 int empty_fd; | |
5082 | |
5083 if (org == NULL | |
557 | 5084 || (empty_fd = mch_open(org, |
5085 O_CREAT | O_EXTRA | O_EXCL | O_NOFOLLOW, | |
216 | 5086 perm < 0 ? 0666 : (perm & 0777))) < 0) |
7 | 5087 EMSG(_("E206: patchmode: can't touch empty original file")); |
5088 else | |
5089 close(empty_fd); | |
5090 } | |
5091 if (org != NULL) | |
5092 { | |
5093 mch_setperm((char_u *)org, mch_getperm(fname) & 0777); | |
5094 vim_free(org); | |
5095 } | |
5096 } | |
5097 | |
5098 /* | |
5099 * Remove the backup unless 'backup' option is set | |
5100 */ | |
5101 if (!p_bk && backup != NULL && mch_remove(backup) != 0) | |
5102 EMSG(_("E207: Can't delete backup file")); | |
5103 | |
5104 #ifdef FEAT_SUN_WORKSHOP | |
5105 if (usingSunWorkShop) | |
5106 workshop_file_saved((char *) ffname); | |
5107 #endif | |
5108 | |
5109 goto nofail; | |
5110 | |
5111 /* | |
5112 * Finish up. We get here either after failure or success. | |
5113 */ | |
5114 fail: | |
5115 --no_wait_return; /* may wait for return now */ | |
5116 nofail: | |
5117 | |
5118 /* Done saving, we accept changed buffer warnings again */ | |
5119 buf->b_saving = FALSE; | |
5120 | |
5121 vim_free(backup); | |
5122 if (buffer != smallbuf) | |
5123 vim_free(buffer); | |
5124 #ifdef FEAT_MBYTE | |
5125 vim_free(fenc_tofree); | |
5126 vim_free(write_info.bw_conv_buf); | |
5127 # ifdef USE_ICONV | |
5128 if (write_info.bw_iconv_fd != (iconv_t)-1) | |
5129 { | |
5130 iconv_close(write_info.bw_iconv_fd); | |
5131 write_info.bw_iconv_fd = (iconv_t)-1; | |
5132 } | |
5133 # endif | |
5134 #endif | |
5135 #ifdef HAVE_ACL | |
5136 mch_free_acl(acl); | |
5137 #endif | |
5138 | |
5139 if (errmsg != NULL) | |
5140 { | |
835 | 5141 int numlen = errnum != NULL ? (int)STRLEN(errnum) : 0; |
7 | 5142 |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
5143 attr = HL_ATTR(HLF_E); /* set highlight for error messages */ |
7 | 5144 msg_add_fname(buf, |
5145 #ifndef UNIX | |
5146 sfname | |
5147 #else | |
5148 fname | |
5149 #endif | |
5150 ); /* put file name in IObuff with quotes */ | |
5151 if (STRLEN(IObuff) + STRLEN(errmsg) + numlen >= IOSIZE) | |
5152 IObuff[IOSIZE - STRLEN(errmsg) - numlen - 1] = NUL; | |
5153 /* If the error message has the form "is ...", put the error number in | |
5154 * front of the file name. */ | |
5155 if (errnum != NULL) | |
5156 { | |
1620 | 5157 STRMOVE(IObuff + numlen, IObuff); |
7 | 5158 mch_memmove(IObuff, errnum, (size_t)numlen); |
5159 } | |
5160 STRCAT(IObuff, errmsg); | |
5161 emsg(IObuff); | |
1947 | 5162 if (errmsg_allocated) |
5163 vim_free(errmsg); | |
7 | 5164 |
5165 retval = FAIL; | |
5166 if (end == 0) | |
5167 { | |
5168 MSG_PUTS_ATTR(_("\nWARNING: Original file may be lost or damaged\n"), | |
5169 attr | MSG_HIST); | |
5170 MSG_PUTS_ATTR(_("don't quit the editor until the file is successfully written!"), | |
5171 attr | MSG_HIST); | |
5172 | |
5173 /* Update the timestamp to avoid an "overwrite changed file" | |
5174 * prompt when writing again. */ | |
5175 if (mch_stat((char *)fname, &st_old) >= 0) | |
5176 { | |
5177 buf_store_time(buf, &st_old, fname); | |
5178 buf->b_mtime_read = buf->b_mtime; | |
5179 } | |
5180 } | |
5181 } | |
5182 msg_scroll = msg_save; | |
5183 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5184 #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
|
5185 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5186 * 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
|
5187 * file. |
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 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
|
5190 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5191 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
|
5192 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5193 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
|
5194 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
|
5195 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5196 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5197 |
7 | 5198 #ifdef FEAT_EVAL |
5199 if (!should_abort(retval)) | |
5200 #else | |
5201 if (!got_int) | |
5202 #endif | |
5203 { | |
5204 aco_save_T aco; | |
5205 | |
3482 | 5206 curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */ |
5207 | |
7 | 5208 /* |
5209 * Apply POST autocommands. | |
5210 * Careful: The autocommands may call buf_write() recursively! | |
5211 */ | |
5212 aucmd_prepbuf(&aco, buf); | |
5213 | |
5214 if (append) | |
5215 apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname, | |
5216 FALSE, curbuf, eap); | |
5217 else if (filtering) | |
5218 apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname, | |
5219 FALSE, curbuf, eap); | |
5220 else if (reset_changed && whole) | |
5221 apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname, | |
5222 FALSE, curbuf, eap); | |
5223 else | |
5224 apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname, | |
5225 FALSE, curbuf, eap); | |
5226 | |
5227 /* restore curwin/curbuf and a few other things */ | |
5228 aucmd_restbuf(&aco); | |
5229 | |
5230 #ifdef FEAT_EVAL | |
5231 if (aborting()) /* autocmds may abort script processing */ | |
5232 retval = FALSE; | |
5233 #endif | |
5234 } | |
5235 | |
5236 got_int |= prev_got_int; | |
5237 | |
5238 return retval; | |
5239 } | |
5240 | |
5241 /* | |
633 | 5242 * Set the name of the current buffer. Use when the buffer doesn't have a |
5243 * name and a ":r" or ":w" command with a file name is used. | |
5244 */ | |
5245 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5246 set_rw_fname(char_u *fname, char_u *sfname) |
633 | 5247 { |
1905 | 5248 buf_T *buf = curbuf; |
5249 | |
633 | 5250 /* It's like the unnamed buffer is deleted.... */ |
5251 if (curbuf->b_p_bl) | |
5252 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); | |
5253 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
|
5254 #ifdef FEAT_EVAL |
633 | 5255 if (aborting()) /* autocmds may abort script processing */ |
5256 return FAIL; | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
5257 #endif |
1905 | 5258 if (curbuf != buf) |
5259 { | |
5260 /* We are in another buffer now, don't do the renaming. */ | |
5261 EMSG(_(e_auchangedbuf)); | |
5262 return FAIL; | |
5263 } | |
633 | 5264 |
5265 if (setfname(curbuf, fname, sfname, FALSE) == OK) | |
5266 curbuf->b_flags |= BF_NOTEDITED; | |
5267 | |
5268 /* ....and a new named one is created */ | |
5269 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf); | |
5270 if (curbuf->b_p_bl) | |
5271 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
|
5272 #ifdef FEAT_EVAL |
633 | 5273 if (aborting()) /* autocmds may abort script processing */ |
5274 return FAIL; | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
5275 #endif |
633 | 5276 |
5277 /* Do filetype detection now if 'filetype' is empty. */ | |
5278 if (*curbuf->b_p_ft == NUL) | |
5279 { | |
819 | 5280 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
|
5281 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL); |
717 | 5282 do_modelines(0); |
633 | 5283 } |
5284 | |
5285 return OK; | |
5286 } | |
5287 | |
5288 /* | |
7 | 5289 * Put file name into IObuff with quotes. |
5290 */ | |
33 | 5291 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5292 msg_add_fname(buf_T *buf, char_u *fname) |
7 | 5293 { |
5294 if (fname == NULL) | |
5295 fname = (char_u *)"-stdin-"; | |
5296 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE); | |
5297 IObuff[0] = '"'; | |
5298 STRCAT(IObuff, "\" "); | |
5299 } | |
5300 | |
5301 /* | |
5302 * Append message for text mode to IObuff. | |
5303 * Return TRUE if something appended. | |
5304 */ | |
5305 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5306 msg_add_fileformat(int eol_type) |
7 | 5307 { |
5308 #ifndef USE_CRNL | |
5309 if (eol_type == EOL_DOS) | |
5310 { | |
5311 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]")); | |
5312 return TRUE; | |
5313 } | |
5314 #endif | |
5315 #ifndef USE_CR | |
5316 if (eol_type == EOL_MAC) | |
5317 { | |
5318 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]")); | |
5319 return TRUE; | |
5320 } | |
5321 #endif | |
5322 #if defined(USE_CRNL) || defined(USE_CR) | |
5323 if (eol_type == EOL_UNIX) | |
5324 { | |
5325 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]")); | |
5326 return TRUE; | |
5327 } | |
5328 #endif | |
5329 return FALSE; | |
5330 } | |
5331 | |
5332 /* | |
5333 * Append line and character count to IObuff. | |
5334 */ | |
33 | 5335 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5336 msg_add_lines( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5337 int insert_space, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5338 long lnum, |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
5339 off_T nchars) |
7 | 5340 { |
5341 char_u *p; | |
5342 | |
5343 p = IObuff + STRLEN(IObuff); | |
5344 | |
5345 if (insert_space) | |
5346 *p++ = ' '; | |
5347 if (shortmess(SHM_LINES)) | |
9391
4c40631238e4
commit https://github.com/vim/vim/commit/bde9810d6103ffe3a22a9330021cb21db1ed1792
Christian Brabandt <cb@256bit.org>
parents:
9389
diff
changeset
|
5348 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
|
5349 "%ldL, %lldC", lnum, (long long)nchars); |
7 | 5350 else |
5351 { | |
14585
c8f07e8b273e
patch 8.1.0306: plural messages are not translated properly
Christian Brabandt <cb@256bit.org>
parents:
14509
diff
changeset
|
5352 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum); |
7 | 5353 p += STRLEN(p); |
14585
c8f07e8b273e
patch 8.1.0306: plural messages are not translated properly
Christian Brabandt <cb@256bit.org>
parents:
14509
diff
changeset
|
5354 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
|
5355 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
|
5356 (long long)nchars); |
7 | 5357 } |
5358 } | |
5359 | |
5360 /* | |
5361 * Append message for missing line separator to IObuff. | |
5362 */ | |
5363 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5364 msg_add_eol(void) |
7 | 5365 { |
5366 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]")); | |
5367 } | |
5368 | |
5369 /* | |
5370 * Check modification time of file, before writing to it. | |
5371 * The size isn't checked, because using a tool like "gzip" takes care of | |
5372 * using the same timestamp but can't set the size. | |
5373 */ | |
5374 static int | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
5375 check_mtime(buf_T *buf, stat_T *st) |
7 | 5376 { |
5377 if (buf->b_mtime_read != 0 | |
5378 && time_differs((long)st->st_mtime, buf->b_mtime_read)) | |
5379 { | |
5380 msg_scroll = TRUE; /* don't overwrite messages here */ | |
5381 msg_silent = 0; /* must give this prompt */ | |
5382 /* don't use emsg() here, don't want to flush the buffers */ | |
5383 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
|
5384 HL_ATTR(HLF_E)); |
7 | 5385 if (ask_yesno((char_u *)_("Do you really want to write to it"), |
5386 TRUE) == 'n') | |
5387 return FAIL; | |
5388 msg_scroll = FALSE; /* always overwrite the file message now */ | |
5389 } | |
5390 return OK; | |
5391 } | |
5392 | |
5393 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5394 time_differs(long t1, long t2) |
7 | 5395 { |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
5396 #if defined(__linux__) || defined(MSWIN) |
7 | 5397 /* On a FAT filesystem, esp. under Linux, there are only 5 bits to store |
5398 * the seconds. Since the roundoff is done when flushing the inode, the | |
5399 * time may change unexpectedly by one second!!! */ | |
5400 return (t1 - t2 > 1 || t2 - t1 > 1); | |
5401 #else | |
5402 return (t1 != t2); | |
5403 #endif | |
5404 } | |
5405 | |
5406 /* | |
5407 * Call write() to write a number of bytes to the file. | |
2664 | 5408 * Handles encryption and 'encoding' conversion. |
7 | 5409 * |
5410 * Return FAIL for failure, OK otherwise. | |
5411 */ | |
5412 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5413 buf_write_bytes(struct bw_info *ip) |
7 | 5414 { |
5415 int wlen; | |
5416 char_u *buf = ip->bw_buf; /* data to write */ | |
5417 int len = ip->bw_len; /* length of data */ | |
5418 #ifdef HAS_BW_FLAGS | |
5419 int flags = ip->bw_flags; /* extra flags */ | |
5420 #endif | |
5421 | |
5422 #ifdef FEAT_MBYTE | |
5423 /* | |
5424 * Skip conversion when writing the crypt magic number or the BOM. | |
5425 */ | |
5426 if (!(flags & FIO_NOCONVERT)) | |
5427 { | |
5428 char_u *p; | |
5429 unsigned c; | |
5430 int n; | |
5431 | |
5432 if (flags & FIO_UTF8) | |
5433 { | |
5434 /* | |
5435 * Convert latin1 in the buffer to UTF-8 in the file. | |
5436 */ | |
5437 p = ip->bw_conv_buf; /* translate to buffer */ | |
5438 for (wlen = 0; wlen < len; ++wlen) | |
5439 p += utf_char2bytes(buf[wlen], p); | |
5440 buf = ip->bw_conv_buf; | |
5441 len = (int)(p - ip->bw_conv_buf); | |
5442 } | |
5443 else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1)) | |
5444 { | |
5445 /* | |
5446 * Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or | |
5447 * Latin1 chars in the file. | |
5448 */ | |
5449 if (flags & FIO_LATIN1) | |
5450 p = buf; /* translate in-place (can only get shorter) */ | |
5451 else | |
5452 p = ip->bw_conv_buf; /* translate to buffer */ | |
5453 for (wlen = 0; wlen < len; wlen += n) | |
5454 { | |
5455 if (wlen == 0 && ip->bw_restlen != 0) | |
5456 { | |
5457 int l; | |
5458 | |
5459 /* Use remainder of previous call. Append the start of | |
5460 * buf[] to get a full sequence. Might still be too | |
5461 * short! */ | |
5462 l = CONV_RESTLEN - ip->bw_restlen; | |
5463 if (l > len) | |
5464 l = len; | |
5465 mch_memmove(ip->bw_rest + ip->bw_restlen, buf, (size_t)l); | |
474 | 5466 n = utf_ptr2len_len(ip->bw_rest, ip->bw_restlen + l); |
7 | 5467 if (n > ip->bw_restlen + len) |
5468 { | |
5469 /* We have an incomplete byte sequence at the end to | |
5470 * be written. We can't convert it without the | |
5471 * remaining bytes. Keep them for the next call. */ | |
5472 if (ip->bw_restlen + len > CONV_RESTLEN) | |
5473 return FAIL; | |
5474 ip->bw_restlen += len; | |
5475 break; | |
5476 } | |
5477 if (n > 1) | |
5478 c = utf_ptr2char(ip->bw_rest); | |
5479 else | |
5480 c = ip->bw_rest[0]; | |
5481 if (n >= ip->bw_restlen) | |
5482 { | |
5483 n -= ip->bw_restlen; | |
5484 ip->bw_restlen = 0; | |
5485 } | |
5486 else | |
5487 { | |
5488 ip->bw_restlen -= n; | |
5489 mch_memmove(ip->bw_rest, ip->bw_rest + n, | |
5490 (size_t)ip->bw_restlen); | |
5491 n = 0; | |
5492 } | |
5493 } | |
5494 else | |
5495 { | |
474 | 5496 n = utf_ptr2len_len(buf + wlen, len - wlen); |
7 | 5497 if (n > len - wlen) |
5498 { | |
5499 /* We have an incomplete byte sequence at the end to | |
5500 * be written. We can't convert it without the | |
5501 * remaining bytes. Keep them for the next call. */ | |
5502 if (len - wlen > CONV_RESTLEN) | |
5503 return FAIL; | |
5504 ip->bw_restlen = len - wlen; | |
5505 mch_memmove(ip->bw_rest, buf + wlen, | |
5506 (size_t)ip->bw_restlen); | |
5507 break; | |
5508 } | |
5509 if (n > 1) | |
5510 c = utf_ptr2char(buf + wlen); | |
5511 else | |
5512 c = buf[wlen]; | |
5513 } | |
5514 | |
1947 | 5515 if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error) |
5516 { | |
5517 ip->bw_conv_error = TRUE; | |
5518 ip->bw_conv_error_lnum = ip->bw_start_lnum; | |
5519 } | |
5520 if (c == NL) | |
5521 ++ip->bw_start_lnum; | |
7 | 5522 } |
5523 if (flags & FIO_LATIN1) | |
5524 len = (int)(p - buf); | |
5525 else | |
5526 { | |
5527 buf = ip->bw_conv_buf; | |
5528 len = (int)(p - ip->bw_conv_buf); | |
5529 } | |
5530 } | |
5531 | |
5532 # ifdef WIN3264 | |
5533 else if (flags & FIO_CODEPAGE) | |
5534 { | |
5535 /* | |
5536 * Convert UTF-8 or codepage to UCS-2 and then to MS-Windows | |
5537 * codepage. | |
5538 */ | |
5539 char_u *from; | |
5540 size_t fromlen; | |
5541 char_u *to; | |
5542 int u8c; | |
5543 BOOL bad = FALSE; | |
5544 int needed; | |
5545 | |
5546 if (ip->bw_restlen > 0) | |
5547 { | |
5548 /* Need to concatenate the remainder of the previous call and | |
5549 * the bytes of the current call. Use the end of the | |
5550 * conversion buffer for this. */ | |
5551 fromlen = len + ip->bw_restlen; | |
5552 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; | |
5553 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); | |
5554 mch_memmove(from + ip->bw_restlen, buf, (size_t)len); | |
5555 } | |
5556 else | |
5557 { | |
5558 from = buf; | |
5559 fromlen = len; | |
5560 } | |
5561 | |
5562 to = ip->bw_conv_buf; | |
5563 if (enc_utf8) | |
5564 { | |
5565 /* Convert from UTF-8 to UCS-2, to the start of the buffer. | |
5566 * The buffer has been allocated to be big enough. */ | |
5567 while (fromlen > 0) | |
5568 { | |
835 | 5569 n = (int)utf_ptr2len_len(from, (int)fromlen); |
7 | 5570 if (n > (int)fromlen) /* incomplete byte sequence */ |
5571 break; | |
5572 u8c = utf_ptr2char(from); | |
5573 *to++ = (u8c & 0xff); | |
5574 *to++ = (u8c >> 8); | |
5575 fromlen -= n; | |
5576 from += n; | |
5577 } | |
5578 | |
5579 /* Copy remainder to ip->bw_rest[] to be used for the next | |
5580 * call. */ | |
5581 if (fromlen > CONV_RESTLEN) | |
5582 { | |
5583 /* weird overlong sequence */ | |
5584 ip->bw_conv_error = TRUE; | |
5585 return FAIL; | |
5586 } | |
5587 mch_memmove(ip->bw_rest, from, fromlen); | |
835 | 5588 ip->bw_restlen = (int)fromlen; |
7 | 5589 } |
5590 else | |
5591 { | |
5592 /* Convert from enc_codepage to UCS-2, to the start of the | |
5593 * buffer. The buffer has been allocated to be big enough. */ | |
5594 ip->bw_restlen = 0; | |
5595 needed = MultiByteToWideChar(enc_codepage, | |
1142 | 5596 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen, |
7 | 5597 NULL, 0); |
5598 if (needed == 0) | |
5599 { | |
5600 /* When conversion fails there may be a trailing byte. */ | |
5601 needed = MultiByteToWideChar(enc_codepage, | |
1142 | 5602 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen - 1, |
7 | 5603 NULL, 0); |
5604 if (needed == 0) | |
5605 { | |
5606 /* Conversion doesn't work. */ | |
5607 ip->bw_conv_error = TRUE; | |
5608 return FAIL; | |
5609 } | |
5610 /* Save the trailing byte for the next call. */ | |
5611 ip->bw_rest[0] = from[fromlen - 1]; | |
5612 ip->bw_restlen = 1; | |
5613 } | |
5614 needed = MultiByteToWideChar(enc_codepage, MB_ERR_INVALID_CHARS, | |
1142 | 5615 (LPCSTR)from, (int)(fromlen - ip->bw_restlen), |
7 | 5616 (LPWSTR)to, needed); |
5617 if (needed == 0) | |
5618 { | |
5619 /* Safety check: Conversion doesn't work. */ | |
5620 ip->bw_conv_error = TRUE; | |
5621 return FAIL; | |
5622 } | |
5623 to += needed * 2; | |
5624 } | |
5625 | |
5626 fromlen = to - ip->bw_conv_buf; | |
5627 buf = to; | |
5628 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
5629 if (FIO_GET_CP(flags) == CP_UTF8) | |
5630 { | |
5631 /* Convert from UCS-2 to UTF-8, using the remainder of the | |
5632 * conversion buffer. Fails when out of space. */ | |
5633 for (from = ip->bw_conv_buf; fromlen > 1; fromlen -= 2) | |
5634 { | |
5635 u8c = *from++; | |
5636 u8c += (*from++ << 8); | |
5637 to += utf_char2bytes(u8c, to); | |
5638 if (to + 6 >= ip->bw_conv_buf + ip->bw_conv_buflen) | |
5639 { | |
5640 ip->bw_conv_error = TRUE; | |
5641 return FAIL; | |
5642 } | |
5643 } | |
835 | 5644 len = (int)(to - buf); |
7 | 5645 } |
5646 else | |
5647 #endif | |
5648 { | |
5649 /* Convert from UCS-2 to the codepage, using the remainder of | |
5650 * the conversion buffer. If the conversion uses the default | |
5651 * character "0", the data doesn't fit in this encoding, so | |
5652 * fail. */ | |
5653 len = WideCharToMultiByte(FIO_GET_CP(flags), 0, | |
5654 (LPCWSTR)ip->bw_conv_buf, (int)fromlen / sizeof(WCHAR), | |
1142 | 5655 (LPSTR)to, (int)(ip->bw_conv_buflen - fromlen), 0, |
5656 &bad); | |
7 | 5657 if (bad) |
5658 { | |
5659 ip->bw_conv_error = TRUE; | |
5660 return FAIL; | |
5661 } | |
5662 } | |
5663 } | |
5664 # endif | |
5665 | |
765 | 5666 # ifdef MACOS_CONVERT |
7 | 5667 else if (flags & FIO_MACROMAN) |
5668 { | |
5669 /* | |
5670 * Convert UTF-8 or latin1 to Apple MacRoman. | |
5671 */ | |
5672 char_u *from; | |
5673 size_t fromlen; | |
5674 | |
5675 if (ip->bw_restlen > 0) | |
5676 { | |
5677 /* Need to concatenate the remainder of the previous call and | |
5678 * the bytes of the current call. Use the end of the | |
5679 * conversion buffer for this. */ | |
5680 fromlen = len + ip->bw_restlen; | |
5681 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; | |
5682 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); | |
5683 mch_memmove(from + ip->bw_restlen, buf, (size_t)len); | |
5684 } | |
5685 else | |
5686 { | |
5687 from = buf; | |
5688 fromlen = len; | |
5689 } | |
5690 | |
18 | 5691 if (enc2macroman(from, fromlen, |
5692 ip->bw_conv_buf, &len, ip->bw_conv_buflen, | |
5693 ip->bw_rest, &ip->bw_restlen) == FAIL) | |
7 | 5694 { |
5695 ip->bw_conv_error = TRUE; | |
5696 return FAIL; | |
5697 } | |
5698 buf = ip->bw_conv_buf; | |
5699 } | |
5700 # endif | |
5701 | |
5702 # ifdef USE_ICONV | |
5703 if (ip->bw_iconv_fd != (iconv_t)-1) | |
5704 { | |
5705 const char *from; | |
5706 size_t fromlen; | |
5707 char *to; | |
5708 size_t tolen; | |
5709 | |
5710 /* Convert with iconv(). */ | |
5711 if (ip->bw_restlen > 0) | |
5712 { | |
1836 | 5713 char *fp; |
5714 | |
7 | 5715 /* Need to concatenate the remainder of the previous call and |
5716 * the bytes of the current call. Use the end of the | |
5717 * conversion buffer for this. */ | |
5718 fromlen = len + ip->bw_restlen; | |
1836 | 5719 fp = (char *)ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; |
5720 mch_memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen); | |
5721 mch_memmove(fp + ip->bw_restlen, buf, (size_t)len); | |
5722 from = fp; | |
7 | 5723 tolen = ip->bw_conv_buflen - fromlen; |
5724 } | |
5725 else | |
5726 { | |
5727 from = (const char *)buf; | |
5728 fromlen = len; | |
5729 tolen = ip->bw_conv_buflen; | |
5730 } | |
5731 to = (char *)ip->bw_conv_buf; | |
5732 | |
5733 if (ip->bw_first) | |
5734 { | |
5735 size_t save_len = tolen; | |
5736 | |
5737 /* output the initial shift state sequence */ | |
5738 (void)iconv(ip->bw_iconv_fd, NULL, NULL, &to, &tolen); | |
5739 | |
5740 /* There is a bug in iconv() on Linux (which appears to be | |
5741 * wide-spread) which sets "to" to NULL and messes up "tolen". | |
5742 */ | |
5743 if (to == NULL) | |
5744 { | |
5745 to = (char *)ip->bw_conv_buf; | |
5746 tolen = save_len; | |
5747 } | |
5748 ip->bw_first = FALSE; | |
5749 } | |
5750 | |
5751 /* | |
5752 * If iconv() has an error or there is not enough room, fail. | |
5753 */ | |
5754 if ((iconv(ip->bw_iconv_fd, (void *)&from, &fromlen, &to, &tolen) | |
5755 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) | |
5756 || fromlen > CONV_RESTLEN) | |
5757 { | |
5758 ip->bw_conv_error = TRUE; | |
5759 return FAIL; | |
5760 } | |
5761 | |
5762 /* copy remainder to ip->bw_rest[] to be used for the next call. */ | |
5763 if (fromlen > 0) | |
5764 mch_memmove(ip->bw_rest, (void *)from, fromlen); | |
5765 ip->bw_restlen = (int)fromlen; | |
5766 | |
5767 buf = ip->bw_conv_buf; | |
5768 len = (int)((char_u *)to - ip->bw_conv_buf); | |
5769 } | |
5770 # endif | |
5771 } | |
5772 #endif /* FEAT_MBYTE */ | |
5773 | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
5774 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
|
5775 /* 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
|
5776 return OK; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
5777 |
7 | 5778 #ifdef FEAT_CRYPT |
6122 | 5779 if (flags & FIO_ENCRYPTED) |
5780 { | |
5781 /* Encrypt the data. Do it in-place if possible, otherwise use an | |
5782 * allocated buffer. */ | |
5783 if (crypt_works_inplace(ip->bw_buffer->b_cryptstate)) | |
5784 { | |
5785 crypt_encode_inplace(ip->bw_buffer->b_cryptstate, buf, len); | |
5786 } | |
5787 else | |
5788 { | |
5789 char_u *outbuf; | |
5790 | |
5791 len = crypt_encode_alloc(curbuf->b_cryptstate, buf, len, &outbuf); | |
5792 if (len == 0) | |
5793 return OK; /* Crypt layer is buffering, will flush later. */ | |
5794 wlen = write_eintr(ip->bw_fd, outbuf, len); | |
5795 vim_free(outbuf); | |
5796 return (wlen < len) ? FAIL : OK; | |
5797 } | |
5798 } | |
7 | 5799 #endif |
5800 | |
2664 | 5801 wlen = write_eintr(ip->bw_fd, buf, len); |
5802 return (wlen < len) ? FAIL : OK; | |
7 | 5803 } |
5804 | |
5805 #ifdef FEAT_MBYTE | |
5806 /* | |
5807 * Convert a Unicode character to bytes. | |
1947 | 5808 * Return TRUE for an error, FALSE when it's OK. |
7 | 5809 */ |
5810 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5811 ucs2bytes( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5812 unsigned c, /* in: character */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5813 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
|
5814 int flags) /* FIO_ flags */ |
7 | 5815 { |
5816 char_u *p = *pp; | |
5817 int error = FALSE; | |
5818 int cc; | |
5819 | |
5820 | |
5821 if (flags & FIO_UCS4) | |
5822 { | |
5823 if (flags & FIO_ENDIAN_L) | |
5824 { | |
5825 *p++ = c; | |
5826 *p++ = (c >> 8); | |
5827 *p++ = (c >> 16); | |
5828 *p++ = (c >> 24); | |
5829 } | |
5830 else | |
5831 { | |
5832 *p++ = (c >> 24); | |
5833 *p++ = (c >> 16); | |
5834 *p++ = (c >> 8); | |
5835 *p++ = c; | |
5836 } | |
5837 } | |
5838 else if (flags & (FIO_UCS2 | FIO_UTF16)) | |
5839 { | |
5840 if (c >= 0x10000) | |
5841 { | |
5842 if (flags & FIO_UTF16) | |
5843 { | |
5844 /* Make two words, ten bits of the character in each. First | |
5845 * word is 0xd800 - 0xdbff, second one 0xdc00 - 0xdfff */ | |
5846 c -= 0x10000; | |
5847 if (c >= 0x100000) | |
5848 error = TRUE; | |
5849 cc = ((c >> 10) & 0x3ff) + 0xd800; | |
5850 if (flags & FIO_ENDIAN_L) | |
5851 { | |
5852 *p++ = cc; | |
5853 *p++ = ((unsigned)cc >> 8); | |
5854 } | |
5855 else | |
5856 { | |
5857 *p++ = ((unsigned)cc >> 8); | |
5858 *p++ = cc; | |
5859 } | |
5860 c = (c & 0x3ff) + 0xdc00; | |
5861 } | |
5862 else | |
5863 error = TRUE; | |
5864 } | |
5865 if (flags & FIO_ENDIAN_L) | |
5866 { | |
5867 *p++ = c; | |
5868 *p++ = (c >> 8); | |
5869 } | |
5870 else | |
5871 { | |
5872 *p++ = (c >> 8); | |
5873 *p++ = c; | |
5874 } | |
5875 } | |
5876 else /* Latin1 */ | |
5877 { | |
5878 if (c >= 0x100) | |
5879 { | |
5880 error = TRUE; | |
5881 *p++ = 0xBF; | |
5882 } | |
5883 else | |
5884 *p++ = c; | |
5885 } | |
5886 | |
5887 *pp = p; | |
5888 return error; | |
5889 } | |
5890 | |
5891 /* | |
1948 | 5892 * Return TRUE if file encoding "fenc" requires conversion from or to |
5893 * 'encoding'. | |
7 | 5894 */ |
5895 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5896 need_conversion(char_u *fenc) |
7 | 5897 { |
1948 | 5898 int same_encoding; |
5899 int enc_flags; | |
5900 int fenc_flags; | |
5901 | |
5902 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0) | |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5903 { |
1948 | 5904 same_encoding = TRUE; |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5905 fenc_flags = 0; |
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5906 } |
1948 | 5907 else |
5908 { | |
5909 /* Ignore difference between "ansi" and "latin1", "ucs-4" and | |
5910 * "ucs-4be", etc. */ | |
5911 enc_flags = get_fio_flags(p_enc); | |
5912 fenc_flags = get_fio_flags(fenc); | |
5913 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags); | |
5914 } | |
5915 if (same_encoding) | |
5916 { | |
5917 /* Specified encoding matches with 'encoding'. This requires | |
5918 * conversion when 'encoding' is Unicode but not UTF-8. */ | |
5919 return enc_unicode != 0; | |
5920 } | |
5921 | |
5922 /* Encodings differ. However, conversion is not needed when 'enc' is any | |
5923 * Unicode encoding and the file is UTF-8. */ | |
5924 return !(enc_utf8 && fenc_flags == FIO_UTF8); | |
7 | 5925 } |
5926 | |
5927 /* | |
5928 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the | |
5929 * internal conversion. | |
5930 * if "ptr" is an empty string, use 'encoding'. | |
5931 */ | |
5932 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5933 get_fio_flags(char_u *ptr) |
7 | 5934 { |
5935 int prop; | |
5936 | |
5937 if (*ptr == NUL) | |
5938 ptr = p_enc; | |
5939 | |
5940 prop = enc_canon_props(ptr); | |
5941 if (prop & ENC_UNICODE) | |
5942 { | |
5943 if (prop & ENC_2BYTE) | |
5944 { | |
5945 if (prop & ENC_ENDIAN_L) | |
5946 return FIO_UCS2 | FIO_ENDIAN_L; | |
5947 return FIO_UCS2; | |
5948 } | |
5949 if (prop & ENC_4BYTE) | |
5950 { | |
5951 if (prop & ENC_ENDIAN_L) | |
5952 return FIO_UCS4 | FIO_ENDIAN_L; | |
5953 return FIO_UCS4; | |
5954 } | |
5955 if (prop & ENC_2WORD) | |
5956 { | |
5957 if (prop & ENC_ENDIAN_L) | |
5958 return FIO_UTF16 | FIO_ENDIAN_L; | |
5959 return FIO_UTF16; | |
5960 } | |
5961 return FIO_UTF8; | |
5962 } | |
5963 if (prop & ENC_LATIN1) | |
5964 return FIO_LATIN1; | |
5965 /* must be ENC_DBCS, requires iconv() */ | |
5966 return 0; | |
5967 } | |
5968 | |
5969 #ifdef WIN3264 | |
5970 /* | |
5971 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed | |
5972 * for the conversion MS-Windows can do for us. Also accept "utf-8". | |
5973 * Used for conversion between 'encoding' and 'fileencoding'. | |
5974 */ | |
5975 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5976 get_win_fio_flags(char_u *ptr) |
7 | 5977 { |
5978 int cp; | |
5979 | |
5980 /* Cannot do this when 'encoding' is not utf-8 and not a codepage. */ | |
5981 if (!enc_utf8 && enc_codepage <= 0) | |
5982 return 0; | |
5983 | |
5984 cp = encname2codepage(ptr); | |
5985 if (cp == 0) | |
5986 { | |
5987 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
5988 if (STRCMP(ptr, "utf-8") == 0) | |
5989 cp = CP_UTF8; | |
5990 else | |
5991 # endif | |
5992 return 0; | |
5993 } | |
5994 return FIO_PUT_CP(cp) | FIO_CODEPAGE; | |
5995 } | |
5996 #endif | |
5997 | |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
5998 #ifdef MACOS_CONVERT |
7 | 5999 /* |
6000 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags | |
6001 * needed for the internal conversion to/from utf-8 or latin1. | |
6002 */ | |
6003 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6004 get_mac_fio_flags(char_u *ptr) |
7 | 6005 { |
6006 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
6007 && (enc_canon_props(ptr) & ENC_MACROMAN)) | |
6008 return FIO_MACROMAN; | |
6009 return 0; | |
6010 } | |
6011 #endif | |
6012 | |
6013 /* | |
6014 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size]. | |
6015 * "size" must be at least 2. | |
6016 * Return the name of the encoding and set "*lenp" to the length. | |
6017 * Returns NULL when no BOM found. | |
6018 */ | |
6019 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6020 check_for_bom( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6021 char_u *p, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6022 long size, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6023 int *lenp, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6024 int flags) |
7 | 6025 { |
6026 char *name = NULL; | |
6027 int len = 2; | |
6028 | |
6029 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf | |
1688 | 6030 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0)) |
7 | 6031 { |
6032 name = "utf-8"; /* EF BB BF */ | |
6033 len = 3; | |
6034 } | |
6035 else if (p[0] == 0xff && p[1] == 0xfe) | |
6036 { | |
6037 if (size >= 4 && p[2] == 0 && p[3] == 0 | |
6038 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L))) | |
6039 { | |
6040 name = "ucs-4le"; /* FF FE 00 00 */ | |
6041 len = 4; | |
6042 } | |
1735 | 6043 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L)) |
7 | 6044 name = "ucs-2le"; /* FF FE */ |
1735 | 6045 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L)) |
6046 /* utf-16le is preferred, it also works for ucs-2le text */ | |
7 | 6047 name = "utf-16le"; /* FF FE */ |
6048 } | |
6049 else if (p[0] == 0xfe && p[1] == 0xff | |
6050 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16)) | |
6051 { | |
1547 | 6052 /* Default to utf-16, it works also for ucs-2 text. */ |
6053 if (flags == FIO_UCS2) | |
6054 name = "ucs-2"; /* FE FF */ | |
6055 else | |
7 | 6056 name = "utf-16"; /* FE FF */ |
6057 } | |
6058 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe | |
6059 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4)) | |
6060 { | |
6061 name = "ucs-4"; /* 00 00 FE FF */ | |
6062 len = 4; | |
6063 } | |
6064 | |
6065 *lenp = len; | |
6066 return (char_u *)name; | |
6067 } | |
6068 | |
6069 /* | |
6070 * Generate a BOM in "buf[4]" for encoding "name". | |
6071 * Return the length of the BOM (zero when no BOM). | |
6072 */ | |
6073 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6074 make_bom(char_u *buf, char_u *name) |
7 | 6075 { |
6076 int flags; | |
6077 char_u *p; | |
6078 | |
6079 flags = get_fio_flags(name); | |
6080 | |
6081 /* Can't put a BOM in a non-Unicode file. */ | |
6082 if (flags == FIO_LATIN1 || flags == 0) | |
6083 return 0; | |
6084 | |
6085 if (flags == FIO_UTF8) /* UTF-8 */ | |
6086 { | |
6087 buf[0] = 0xef; | |
6088 buf[1] = 0xbb; | |
6089 buf[2] = 0xbf; | |
6090 return 3; | |
6091 } | |
6092 p = buf; | |
6093 (void)ucs2bytes(0xfeff, &p, flags); | |
6094 return (int)(p - buf); | |
6095 } | |
6096 #endif | |
6097 | |
6098 /* | |
6099 * Try to find a shortname by comparing the fullname with the current | |
6100 * directory. | |
1411 | 6101 * Returns "full_path" or pointer into "full_path" if shortened. |
6102 */ | |
6103 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6104 shorten_fname1(char_u *full_path) |
1411 | 6105 { |
2770 | 6106 char_u *dirname; |
1411 | 6107 char_u *p = full_path; |
6108 | |
2770 | 6109 dirname = alloc(MAXPATHL); |
6110 if (dirname == NULL) | |
6111 return full_path; | |
1411 | 6112 if (mch_dirname(dirname, MAXPATHL) == OK) |
6113 { | |
6114 p = shorten_fname(full_path, dirname); | |
6115 if (p == NULL || *p == NUL) | |
6116 p = full_path; | |
6117 } | |
2770 | 6118 vim_free(dirname); |
1411 | 6119 return p; |
6120 } | |
6121 | |
6122 /* | |
6123 * Try to find a shortname by comparing the fullname with the current | |
6124 * directory. | |
7 | 6125 * Returns NULL if not shorter name possible, pointer into "full_path" |
6126 * otherwise. | |
6127 */ | |
6128 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6129 shorten_fname(char_u *full_path, char_u *dir_name) |
7 | 6130 { |
6131 int len; | |
6132 char_u *p; | |
6133 | |
6134 if (full_path == NULL) | |
6135 return NULL; | |
6136 len = (int)STRLEN(dir_name); | |
6137 if (fnamencmp(dir_name, full_path, len) == 0) | |
6138 { | |
6139 p = full_path + len; | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6140 #if defined(MSWIN) |
7 | 6141 /* |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6142 * MSWIN: when a file is in the root directory, dir_name will end in a |
7 | 6143 * slash, since C: by itself does not define a specific dir. In this |
6144 * case p may already be correct. <negri> | |
6145 */ | |
6146 if (!((len > 2) && (*(p - 2) == ':'))) | |
6147 #endif | |
6148 { | |
6149 if (vim_ispathsep(*p)) | |
6150 ++p; | |
6151 #ifndef VMS /* the path separator is always part of the path */ | |
6152 else | |
6153 p = NULL; | |
6154 #endif | |
6155 } | |
6156 } | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6157 #if defined(MSWIN) |
7 | 6158 /* |
6159 * When using a file in the current drive, remove the drive name: | |
6160 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on | |
6161 * a floppy from "A:\dir" to "B:\dir". | |
6162 */ | |
6163 else if (len > 3 | |
6164 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0]) | |
6165 && full_path[1] == ':' | |
6166 && vim_ispathsep(full_path[2])) | |
6167 p = full_path + 2; | |
6168 #endif | |
6169 else | |
6170 p = NULL; | |
6171 return p; | |
6172 } | |
6173 | |
6174 /* | |
13819
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6175 * Shorten filename of a buffer. |
7 | 6176 * When "force" is TRUE: Use full path from now on for files currently being |
6177 * edited, both for file name and swap file name. Try to shorten the file | |
6178 * names a bit, if safe to do so. | |
6179 * When "force" is FALSE: Only try to shorten absolute file names. | |
6180 * For buffers that have buftype "nofile" or "scratch": never change the file | |
6181 * name. | |
6182 */ | |
6183 void | |
13819
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6184 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
|
6185 { |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6186 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
|
6187 |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6188 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
|
6189 #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
|
6190 && !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
|
6191 #endif |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6192 && !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
|
6193 && (force |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6194 || 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
|
6195 || 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
|
6196 { |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6197 VIM_CLEAR(buf->b_sfname); |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6198 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
|
6199 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
|
6200 { |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6201 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
|
6202 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
|
6203 } |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6204 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
|
6205 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
|
6206 } |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6207 } |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6208 |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6209 /* |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6210 * 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
|
6211 */ |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6212 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6213 shorten_fnames(int force) |
7 | 6214 { |
6215 char_u dirname[MAXPATHL]; | |
6216 buf_T *buf; | |
6217 | |
6218 mch_dirname(dirname, MAXPATHL); | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
6219 FOR_ALL_BUFFERS(buf) |
7 | 6220 { |
13819
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
6221 shorten_buf_fname(buf, dirname, force); |
9 | 6222 |
6223 /* Always make the swap file name a full path, a "nofile" buffer may | |
6224 * also have a swap file. */ | |
6225 mf_fullname(buf->b_ml.ml_mfp); | |
7 | 6226 } |
6227 status_redraw_all(); | |
672 | 6228 redraw_tabline = TRUE; |
7 | 6229 } |
6230 | |
6231 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \ | |
6232 || defined(FEAT_GUI_MSWIN) \ | |
6233 || defined(FEAT_GUI_MAC) \ | |
6234 || defined(PROTO) | |
6235 /* | |
6236 * Shorten all filenames in "fnames[count]" by current directory. | |
6237 */ | |
6238 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6239 shorten_filenames(char_u **fnames, int count) |
7 | 6240 { |
6241 int i; | |
6242 char_u dirname[MAXPATHL]; | |
6243 char_u *p; | |
6244 | |
6245 if (fnames == NULL || count < 1) | |
6246 return; | |
6247 mch_dirname(dirname, sizeof(dirname)); | |
6248 for (i = 0; i < count; ++i) | |
6249 { | |
6250 if ((p = shorten_fname(fnames[i], dirname)) != NULL) | |
6251 { | |
6252 /* shorten_fname() returns pointer in given "fnames[i]". If free | |
6253 * "fnames[i]" first, "p" becomes invalid. So we need to copy | |
6254 * "p" first then free fnames[i]. */ | |
6255 p = vim_strsave(p); | |
6256 vim_free(fnames[i]); | |
6257 fnames[i] = p; | |
6258 } | |
6259 } | |
6260 } | |
6261 #endif | |
6262 | |
6263 /* | |
14475
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
6264 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or |
7 | 6265 * fo_o_h.ext for MSDOS or when shortname option set. |
6266 * | |
6267 * Assumed that fname is a valid name found in the filesystem we assure that | |
6268 * the return value is a different name and ends in 'ext'. | |
6269 * "ext" MUST be at most 4 characters long if it starts with a dot, 3 | |
6270 * characters otherwise. | |
6271 * Space for the returned name is allocated, must be freed later. | |
6272 * Returns NULL when out of memory. | |
6273 */ | |
6274 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6275 modname( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6276 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6277 char_u *ext, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6278 int prepend_dot) /* may prepend a '.' to file name */ |
7 | 6279 { |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6280 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname), |
7 | 6281 fname, ext, prepend_dot); |
6282 } | |
6283 | |
6284 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6285 buf_modname( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6286 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
|
6287 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6288 char_u *ext, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6289 int prepend_dot) /* may prepend a '.' to file name */ |
7 | 6290 { |
6291 char_u *retval; | |
6292 char_u *s; | |
6293 char_u *e; | |
6294 char_u *ptr; | |
6295 int fnamelen, extlen; | |
6296 | |
6297 extlen = (int)STRLEN(ext); | |
6298 | |
6299 /* | |
6300 * If there is no file name we must get the name of the current directory | |
6301 * (we need the full path in case :cd is used). | |
6302 */ | |
6303 if (fname == NULL || *fname == NUL) | |
6304 { | |
6305 retval = alloc((unsigned)(MAXPATHL + extlen + 3)); | |
6306 if (retval == NULL) | |
6307 return NULL; | |
6308 if (mch_dirname(retval, MAXPATHL) == FAIL || | |
6309 (fnamelen = (int)STRLEN(retval)) == 0) | |
6310 { | |
6311 vim_free(retval); | |
6312 return NULL; | |
6313 } | |
39 | 6314 if (!after_pathsep(retval, retval + fnamelen)) |
7 | 6315 { |
6316 retval[fnamelen++] = PATHSEP; | |
6317 retval[fnamelen] = NUL; | |
6318 } | |
6319 prepend_dot = FALSE; /* nothing to prepend a dot to */ | |
6320 } | |
6321 else | |
6322 { | |
6323 fnamelen = (int)STRLEN(fname); | |
6324 retval = alloc((unsigned)(fnamelen + extlen + 3)); | |
6325 if (retval == NULL) | |
6326 return NULL; | |
6327 STRCPY(retval, fname); | |
6328 #ifdef VMS | |
6329 vms_remove_version(retval); /* we do not need versions here */ | |
6330 #endif | |
6331 } | |
6332 | |
6333 /* | |
6334 * search backwards until we hit a '/', '\' or ':' replacing all '.' | |
6335 * by '_' for MSDOS or when shortname option set and ext starts with a dot. | |
6336 * Then truncate what is after the '/', '\' or ':' to 8 characters for | |
6337 * MSDOS and 26 characters for AMIGA, a lot more for UNIX. | |
6338 */ | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
6339 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr)) |
7 | 6340 { |
6341 if (*ext == '.' | |
2823 | 6342 #ifdef USE_LONG_FNAME |
7 | 6343 && (!USE_LONG_FNAME || shortname) |
2823 | 6344 #else |
7 | 6345 && shortname |
2823 | 6346 #endif |
7 | 6347 ) |
6348 if (*ptr == '.') /* replace '.' by '_' */ | |
6349 *ptr = '_'; | |
6350 if (vim_ispathsep(*ptr)) | |
391 | 6351 { |
6352 ++ptr; | |
7 | 6353 break; |
391 | 6354 } |
6355 } | |
7 | 6356 |
6357 /* the file name has at most BASENAMELEN characters. */ | |
6358 if (STRLEN(ptr) > (unsigned)BASENAMELEN) | |
6359 ptr[BASENAMELEN] = '\0'; | |
6360 | |
6361 s = ptr + STRLEN(ptr); | |
6362 | |
6363 /* | |
6364 * For 8.3 file names we may have to reduce the length. | |
6365 */ | |
6366 #ifdef USE_LONG_FNAME | |
6367 if (!USE_LONG_FNAME || shortname) | |
6368 #else | |
6369 if (shortname) | |
6370 #endif | |
6371 { | |
6372 /* | |
6373 * If there is no file name, or the file name ends in '/', and the | |
6374 * extension starts with '.', put a '_' before the dot, because just | |
6375 * ".ext" is invalid. | |
6376 */ | |
6377 if (fname == NULL || *fname == NUL | |
6378 || vim_ispathsep(fname[STRLEN(fname) - 1])) | |
6379 { | |
6380 if (*ext == '.') | |
6381 *s++ = '_'; | |
6382 } | |
6383 /* | |
6384 * If the extension starts with '.', truncate the base name at 8 | |
6385 * characters | |
6386 */ | |
6387 else if (*ext == '.') | |
6388 { | |
1877 | 6389 if ((size_t)(s - ptr) > (size_t)8) |
7 | 6390 { |
6391 s = ptr + 8; | |
6392 *s = '\0'; | |
6393 } | |
6394 } | |
6395 /* | |
6396 * If the extension doesn't start with '.', and the file name | |
6397 * doesn't have an extension yet, append a '.' | |
6398 */ | |
6399 else if ((e = vim_strchr(ptr, '.')) == NULL) | |
6400 *s++ = '.'; | |
6401 /* | |
6402 * If the extension doesn't start with '.', and there already is an | |
1201 | 6403 * extension, it may need to be truncated |
7 | 6404 */ |
6405 else if ((int)STRLEN(e) + extlen > 4) | |
6406 s = e + 4 - extlen; | |
6407 } | |
7408
1886f2863437
commit https://github.com/vim/vim/commit/e7fedb6ebe72d9a475aa65109b77d5ed4667067a
Christian Brabandt <cb@256bit.org>
parents:
7320
diff
changeset
|
6408 #if defined(USE_LONG_FNAME) || defined(WIN3264) |
7 | 6409 /* |
6410 * If there is no file name, and the extension starts with '.', put a | |
6411 * '_' before the dot, because just ".ext" may be invalid if it's on a | |
6412 * FAT partition, and on HPFS it doesn't matter. | |
6413 */ | |
6414 else if ((fname == NULL || *fname == NUL) && *ext == '.') | |
6415 *s++ = '_'; | |
6416 #endif | |
6417 | |
6418 /* | |
1651 | 6419 * Append the extension. |
7 | 6420 * ext can start with '.' and cannot exceed 3 more characters. |
6421 */ | |
6422 STRCPY(s, ext); | |
6423 | |
6424 /* | |
6425 * Prepend the dot. | |
6426 */ | |
2823 | 6427 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.' |
7 | 6428 #ifdef USE_LONG_FNAME |
6429 && USE_LONG_FNAME | |
6430 #endif | |
6431 ) | |
6432 { | |
1620 | 6433 STRMOVE(e + 1, e); |
7 | 6434 *e = '.'; |
6435 } | |
6436 | |
6437 /* | |
6438 * Check that, after appending the extension, the file name is really | |
6439 * different. | |
6440 */ | |
6441 if (fname != NULL && STRCMP(fname, retval) == 0) | |
6442 { | |
6443 /* we search for a character that can be replaced by '_' */ | |
6444 while (--s >= ptr) | |
6445 { | |
6446 if (*s != '_') | |
6447 { | |
6448 *s = '_'; | |
6449 break; | |
6450 } | |
6451 } | |
6452 if (s < ptr) /* fname was "________.<ext>", how tricky! */ | |
6453 *ptr = 'v'; | |
6454 } | |
6455 return retval; | |
6456 } | |
6457 | |
6458 /* | |
6459 * Like fgets(), but if the file line is too long, it is truncated and the | |
6460 * 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
|
6461 * If the line is truncated then buf[size - 2] will not be NUL. |
7 | 6462 */ |
6463 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6464 vim_fgets(char_u *buf, int size, FILE *fp) |
7 | 6465 { |
6466 char *eof; | |
6467 #define FGETS_SIZE 200 | |
6468 char tbuf[FGETS_SIZE]; | |
6469 | |
6470 buf[size - 2] = NUL; | |
6471 #ifdef USE_CR | |
6472 eof = fgets_cr((char *)buf, size, fp); | |
6473 #else | |
6474 eof = fgets((char *)buf, size, fp); | |
6475 #endif | |
6476 if (buf[size - 2] != NUL && buf[size - 2] != '\n') | |
6477 { | |
6478 buf[size - 1] = NUL; /* Truncate the line */ | |
6479 | |
6480 /* Now throw away the rest of the line: */ | |
6481 do | |
6482 { | |
6483 tbuf[FGETS_SIZE - 2] = NUL; | |
6484 #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
|
6485 vim_ignoredp = fgets_cr((char *)tbuf, FGETS_SIZE, fp); |
7 | 6486 #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
|
6487 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp); |
7 | 6488 #endif |
6489 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n'); | |
6490 } | |
6491 return (eof == NULL); | |
6492 } | |
6493 | |
6494 #if defined(USE_CR) || defined(PROTO) | |
6495 /* | |
6496 * Like vim_fgets(), but accept any line terminator: CR, CR-LF or LF. | |
6497 * Returns TRUE for end-of-file. | |
6498 * Only used for the Mac, because it's much slower than vim_fgets(). | |
6499 */ | |
6500 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6501 tag_fgets(char_u *buf, int size, FILE *fp) |
7 | 6502 { |
6503 int i = 0; | |
6504 int c; | |
6505 int eof = FALSE; | |
6506 | |
6507 for (;;) | |
6508 { | |
6509 c = fgetc(fp); | |
6510 if (c == EOF) | |
6511 { | |
6512 eof = TRUE; | |
6513 break; | |
6514 } | |
6515 if (c == '\r') | |
6516 { | |
6517 /* Always store a NL for end-of-line. */ | |
6518 if (i < size - 1) | |
6519 buf[i++] = '\n'; | |
6520 c = fgetc(fp); | |
6521 if (c != '\n') /* Macintosh format: single CR. */ | |
6522 ungetc(c, fp); | |
6523 break; | |
6524 } | |
6525 if (i < size - 1) | |
6526 buf[i++] = c; | |
6527 if (c == '\n') | |
6528 break; | |
6529 } | |
6530 buf[i] = NUL; | |
6531 return eof; | |
6532 } | |
6533 #endif | |
6534 | |
6535 /* | |
6536 * rename() only works if both files are on the same file system, this | |
6537 * function will (attempts to?) copy the file across if rename fails -- webb | |
6538 * Return -1 for failure, 0 for success. | |
6539 */ | |
6540 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6541 vim_rename(char_u *from, char_u *to) |
7 | 6542 { |
6543 int fd_in; | |
6544 int fd_out; | |
6545 int n; | |
6546 char *errmsg = NULL; | |
6547 char *buffer; | |
6548 #ifdef AMIGA | |
6549 BPTR flock; | |
6550 #endif | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6551 stat_T st; |
194 | 6552 long perm; |
199 | 6553 #ifdef HAVE_ACL |
6554 vim_acl_T acl; /* ACL from original file */ | |
6555 #endif | |
1779 | 6556 int use_tmp_file = FALSE; |
7 | 6557 |
6558 /* | |
1779 | 6559 * When the names are identical, there is nothing to do. When they refer |
6560 * to the same file (ignoring case and slash/backslash differences) but | |
6561 * the file name differs we need to go through a temp file. | |
7 | 6562 */ |
6563 if (fnamecmp(from, to) == 0) | |
1779 | 6564 { |
4242 | 6565 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0) |
1779 | 6566 use_tmp_file = TRUE; |
6567 else | |
6568 return 0; | |
6569 } | |
7 | 6570 |
6571 /* | |
6572 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted. | |
6573 */ | |
6574 if (mch_stat((char *)from, &st) < 0) | |
6575 return -1; | |
6576 | |
1778 | 6577 #ifdef UNIX |
6578 { | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6579 stat_T st_to; |
1778 | 6580 |
6581 /* It's possible for the source and destination to be the same file. | |
6582 * This happens when "from" and "to" differ in case and are on a FAT32 | |
6583 * filesystem. In that case go through a temp file name. */ | |
6584 if (mch_stat((char *)to, &st_to) >= 0 | |
6585 && st.st_dev == st_to.st_dev | |
6586 && st.st_ino == st_to.st_ino) | |
1779 | 6587 use_tmp_file = TRUE; |
6588 } | |
6589 #endif | |
2793 | 6590 #ifdef WIN3264 |
6591 { | |
6592 BY_HANDLE_FILE_INFORMATION info1, info2; | |
6593 | |
6594 /* It's possible for the source and destination to be the same file. | |
6595 * In that case go through a temp file name. This makes rename("foo", | |
6596 * "./foo") a no-op (in a complicated way). */ | |
6597 if (win32_fileinfo(from, &info1) == FILEINFO_OK | |
6598 && win32_fileinfo(to, &info2) == FILEINFO_OK | |
6599 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber | |
6600 && info1.nFileIndexHigh == info2.nFileIndexHigh | |
6601 && info1.nFileIndexLow == info2.nFileIndexLow) | |
6602 use_tmp_file = TRUE; | |
6603 } | |
6604 #endif | |
1779 | 6605 |
6606 if (use_tmp_file) | |
6607 { | |
6608 char tempname[MAXPATHL + 1]; | |
6609 | |
6610 /* | |
6611 * Find a name that doesn't exist and is in the same directory. | |
6612 * Rename "from" to "tempname" and then rename "tempname" to "to". | |
6613 */ | |
6614 if (STRLEN(from) >= MAXPATHL - 5) | |
6615 return -1; | |
6616 STRCPY(tempname, from); | |
6617 for (n = 123; n < 99999; ++n) | |
6618 { | |
6619 sprintf((char *)gettail((char_u *)tempname), "%d", n); | |
6620 if (mch_stat(tempname, &st) < 0) | |
6621 { | |
6622 if (mch_rename((char *)from, tempname) == 0) | |
1778 | 6623 { |
1779 | 6624 if (mch_rename(tempname, (char *)to) == 0) |
6625 return 0; | |
6626 /* Strange, the second step failed. Try moving the | |
6627 * file back and return failure. */ | |
6628 mch_rename(tempname, (char *)from); | |
1778 | 6629 return -1; |
6630 } | |
1779 | 6631 /* If it fails for one temp name it will most likely fail |
6632 * for any temp name, give up. */ | |
6633 return -1; | |
6634 } | |
6635 } | |
6636 return -1; | |
1778 | 6637 } |
6638 | |
7 | 6639 /* |
6640 * Delete the "to" file, this is required on some systems to make the | |
6641 * mch_rename() work, on other systems it makes sure that we don't have | |
6642 * two files when the mch_rename() fails. | |
6643 */ | |
6644 | |
6645 #ifdef AMIGA | |
6646 /* | |
6647 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible | |
6648 * that the name of the "to" file is the same as the "from" file, even | |
1201 | 6649 * though the names are different. To avoid the chance of accidentally |
7 | 6650 * deleting the "from" file (horror!) we lock it during the remove. |
6651 * | |
6652 * When used for making a backup before writing the file: This should not | |
6653 * happen with ":w", because startscript() should detect this problem and | |
6654 * set buf->b_shortname, causing modname() to return a correct ".bak" file | |
6655 * name. This problem does exist with ":w filename", but then the | |
6656 * original file will be somewhere else so the backup isn't really | |
6657 * important. If autoscripting is off the rename may fail. | |
6658 */ | |
6659 flock = Lock((UBYTE *)from, (long)ACCESS_READ); | |
6660 #endif | |
6661 mch_remove(to); | |
6662 #ifdef AMIGA | |
6663 if (flock) | |
6664 UnLock(flock); | |
6665 #endif | |
6666 | |
6667 /* | |
6668 * First try a normal rename, return if it works. | |
6669 */ | |
6670 if (mch_rename((char *)from, (char *)to) == 0) | |
6671 return 0; | |
6672 | |
6673 /* | |
6674 * Rename() failed, try copying the file. | |
6675 */ | |
194 | 6676 perm = mch_getperm(from); |
199 | 6677 #ifdef HAVE_ACL |
6678 /* For systems that support ACL: get the ACL from the original file. */ | |
6679 acl = mch_get_acl(from); | |
6680 #endif | |
7 | 6681 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0); |
6682 if (fd_in == -1) | |
1651 | 6683 { |
6684 #ifdef HAVE_ACL | |
6685 mch_free_acl(acl); | |
6686 #endif | |
7 | 6687 return -1; |
1651 | 6688 } |
194 | 6689 |
6690 /* Create the new file with same permissions as the original. */ | |
557 | 6691 fd_out = mch_open((char *)to, |
6692 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm); | |
7 | 6693 if (fd_out == -1) |
6694 { | |
6695 close(fd_in); | |
1651 | 6696 #ifdef HAVE_ACL |
6697 mch_free_acl(acl); | |
6698 #endif | |
7 | 6699 return -1; |
6700 } | |
6701 | |
6702 buffer = (char *)alloc(BUFSIZE); | |
6703 if (buffer == NULL) | |
6704 { | |
1651 | 6705 close(fd_out); |
7 | 6706 close(fd_in); |
1651 | 6707 #ifdef HAVE_ACL |
6708 mch_free_acl(acl); | |
6709 #endif | |
7 | 6710 return -1; |
6711 } | |
6712 | |
2664 | 6713 while ((n = read_eintr(fd_in, buffer, BUFSIZE)) > 0) |
6714 if (write_eintr(fd_out, buffer, n) != n) | |
7 | 6715 { |
6716 errmsg = _("E208: Error writing to \"%s\""); | |
6717 break; | |
6718 } | |
6719 | |
6720 vim_free(buffer); | |
6721 close(fd_in); | |
6722 if (close(fd_out) < 0) | |
6723 errmsg = _("E209: Error closing \"%s\""); | |
6724 if (n < 0) | |
6725 { | |
6726 errmsg = _("E210: Error reading \"%s\""); | |
6727 to = from; | |
6728 } | |
1201 | 6729 #ifndef UNIX /* for Unix mch_open() already set the permission */ |
194 | 6730 mch_setperm(to, perm); |
570 | 6731 #endif |
199 | 6732 #ifdef HAVE_ACL |
6733 mch_set_acl(to, acl); | |
1651 | 6734 mch_free_acl(acl); |
199 | 6735 #endif |
5788 | 6736 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
5483 | 6737 mch_copy_sec(from, to); |
5479 | 6738 #endif |
7 | 6739 if (errmsg != NULL) |
6740 { | |
6741 EMSG2(errmsg, to); | |
6742 return -1; | |
6743 } | |
6744 mch_remove(from); | |
6745 return 0; | |
6746 } | |
6747 | |
6748 static int already_warned = FALSE; | |
6749 | |
6750 /* | |
6751 * Check if any not hidden buffer has been changed. | |
6752 * Postpone the check if there are characters in the stuff buffer, a global | |
6753 * command is being executed, a mapping is being executed or an autocommand is | |
6754 * busy. | |
6755 * Returns TRUE if some message was written (screen should be redrawn and | |
6756 * cursor positioned). | |
6757 */ | |
6758 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6759 check_timestamps( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6760 int focus) /* called for GUI focus event */ |
7 | 6761 { |
6762 buf_T *buf; | |
6763 int didit = 0; | |
6764 int n; | |
6765 | |
6766 /* Don't check timestamps while system() or another low-level function may | |
6767 * cause us to lose and gain focus. */ | |
6768 if (no_check_timestamps > 0) | |
6769 return FALSE; | |
6770 | |
6771 /* Avoid doing a check twice. The OK/Reload dialog can cause a focus | |
6772 * event and we would keep on checking if the file is steadily growing. | |
6773 * Do check again after typing something. */ | |
6774 if (focus && did_check_timestamps) | |
6775 { | |
6776 need_check_timestamps = TRUE; | |
6777 return FALSE; | |
6778 } | |
6779 | |
6780 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
|
6781 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0) |
7 | 6782 need_check_timestamps = TRUE; /* check later */ |
6783 else | |
6784 { | |
6785 ++no_wait_return; | |
6786 did_check_timestamps = TRUE; | |
6787 already_warned = FALSE; | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
6788 FOR_ALL_BUFFERS(buf) |
7 | 6789 { |
6790 /* Only check buffers in a window. */ | |
6791 if (buf->b_nwindows > 0) | |
6792 { | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6793 bufref_T bufref; |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6794 |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6795 set_bufref(&bufref, buf); |
7 | 6796 n = buf_check_timestamp(buf, focus); |
6797 if (didit < n) | |
6798 didit = n; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6799 if (n > 0 && !bufref_valid(&bufref)) |
7 | 6800 { |
6801 /* Autocommands have removed the buffer, start at the | |
6802 * first one again. */ | |
6803 buf = firstbuf; | |
6804 continue; | |
6805 } | |
6806 } | |
6807 } | |
6808 --no_wait_return; | |
6809 need_check_timestamps = FALSE; | |
6810 if (need_wait_return && didit == 2) | |
6811 { | |
6812 /* make sure msg isn't overwritten */ | |
6813 msg_puts((char_u *)"\n"); | |
6814 out_flush(); | |
6815 } | |
6816 } | |
6817 return didit; | |
6818 } | |
6819 | |
6820 /* | |
6821 * Move all the lines from buffer "frombuf" to buffer "tobuf". | |
6822 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not | |
6823 * empty. | |
6824 */ | |
6825 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6826 move_lines(buf_T *frombuf, buf_T *tobuf) |
7 | 6827 { |
6828 buf_T *tbuf = curbuf; | |
6829 int retval = OK; | |
6830 linenr_T lnum; | |
6831 char_u *p; | |
6832 | |
6833 /* Copy the lines in "frombuf" to "tobuf". */ | |
6834 curbuf = tobuf; | |
6835 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum) | |
6836 { | |
6837 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE)); | |
6838 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL) | |
6839 { | |
6840 vim_free(p); | |
6841 retval = FAIL; | |
6842 break; | |
6843 } | |
6844 vim_free(p); | |
6845 } | |
6846 | |
6847 /* Delete all the lines in "frombuf". */ | |
6848 if (retval != FAIL) | |
6849 { | |
6850 curbuf = frombuf; | |
1055 | 6851 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum) |
6852 if (ml_delete(lnum, FALSE) == FAIL) | |
7 | 6853 { |
6854 /* Oops! We could try putting back the saved lines, but that | |
6855 * might fail again... */ | |
6856 retval = FAIL; | |
6857 break; | |
6858 } | |
6859 } | |
6860 | |
6861 curbuf = tbuf; | |
6862 return retval; | |
6863 } | |
6864 | |
6865 /* | |
6866 * Check if buffer "buf" has been changed. | |
6867 * Also check if the file for a new buffer unexpectedly appeared. | |
6868 * return 1 if a changed buffer was found. | |
6869 * return 2 if a message has been displayed. | |
6870 * return 0 otherwise. | |
6871 */ | |
6872 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6873 buf_check_timestamp( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6874 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6875 int focus UNUSED) /* called for GUI focus event */ |
7 | 6876 { |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6877 stat_T st; |
7 | 6878 int stat_res; |
6879 int retval = 0; | |
6880 char_u *path; | |
6881 char_u *tbuf; | |
6882 char *mesg = NULL; | |
188 | 6883 char *mesg2 = ""; |
7 | 6884 int helpmesg = FALSE; |
6885 int reload = FALSE; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6886 char *reason; |
7 | 6887 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
6888 int can_reload = FALSE; | |
6889 #endif | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6890 off_T orig_size = buf->b_orig_size; |
7 | 6891 int orig_mode = buf->b_orig_mode; |
6892 #ifdef FEAT_GUI | |
6893 int save_mouse_correct = need_mouse_correct; | |
6894 #endif | |
6895 static int busy = FALSE; | |
179 | 6896 int n; |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
6897 #ifdef FEAT_EVAL |
179 | 6898 char_u *s; |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
6899 #endif |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6900 bufref_T bufref; |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6901 |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6902 set_bufref(&bufref, buf); |
7 | 6903 |
6904 /* If there is no file name, the buffer is not loaded, 'buftype' is | |
6905 * set, we are in the middle of a save or being called recursively: ignore | |
6906 * this buffer. */ | |
6907 if (buf->b_ffname == NULL | |
6908 || 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
|
6909 || !bt_normal(buf) |
7 | 6910 || buf->b_saving |
6911 || busy | |
33 | 6912 #ifdef FEAT_NETBEANS_INTG |
6913 || isNetbeansBuffer(buf) | |
6914 #endif | |
12064
407a475c67fd
patch 8.0.0912: cannot run a job in a hidden terminal
Christian Brabandt <cb@256bit.org>
parents:
11757
diff
changeset
|
6915 #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
|
6916 || 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
|
6917 #endif |
7 | 6918 ) |
6919 return 0; | |
6920 | |
6921 if ( !(buf->b_flags & BF_NOTEDITED) | |
6922 && buf->b_mtime != 0 | |
6923 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0 | |
6924 || time_differs((long)st.st_mtime, buf->b_mtime) | |
5863 | 6925 || st.st_size != buf->b_orig_size |
7 | 6926 #ifdef HAVE_ST_MODE |
6927 || (int)st.st_mode != buf->b_orig_mode | |
6928 #else | |
6929 || mch_getperm(buf->b_ffname) != buf->b_orig_mode | |
6930 #endif | |
6931 )) | |
6932 { | |
6933 retval = 1; | |
6934 | |
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
|
6935 // 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
|
6936 // FileChangedShell autocmd) |
7 | 6937 if (stat_res < 0) |
6938 { | |
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
|
6939 // 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
|
6940 // 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
|
6941 buf->b_mtime = buf->b_p_ar; |
7 | 6942 buf->b_orig_size = 0; |
6943 buf->b_orig_mode = 0; | |
6944 } | |
6945 else | |
6946 buf_store_time(buf, &st, buf->b_ffname); | |
6947 | |
6948 /* Don't do anything for a directory. Might contain the file | |
6949 * explorer. */ | |
6950 if (mch_isdir(buf->b_fname)) | |
6951 ; | |
6952 | |
6953 /* | |
6954 * If 'autoread' is set, the buffer has no changes and the file still | |
6955 * exists, reload the buffer. Use the buffer-local option value if it | |
6956 * was set, the global option value otherwise. | |
6957 */ | |
6958 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar) | |
6959 && !bufIsChanged(buf) && stat_res >= 0) | |
6960 reload = TRUE; | |
6961 else | |
6962 { | |
179 | 6963 if (stat_res < 0) |
6964 reason = "deleted"; | |
6965 else if (bufIsChanged(buf)) | |
6966 reason = "conflict"; | |
6967 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf)) | |
6968 reason = "changed"; | |
6969 else if (orig_mode != buf->b_orig_mode) | |
6970 reason = "mode"; | |
6971 else | |
6972 reason = "time"; | |
6973 | |
7 | 6974 /* |
6975 * Only give the warning if there are no FileChangedShell | |
6976 * autocommands. | |
6977 * Avoid being called recursively by setting "busy". | |
6978 */ | |
6979 busy = TRUE; | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
6980 #ifdef FEAT_EVAL |
179 | 6981 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1); |
6982 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
|
6983 #endif |
1834 | 6984 ++allbuf_lock; |
7 | 6985 n = apply_autocmds(EVENT_FILECHANGEDSHELL, |
6986 buf->b_fname, buf->b_fname, FALSE, buf); | |
1834 | 6987 --allbuf_lock; |
7 | 6988 busy = FALSE; |
6989 if (n) | |
6990 { | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6991 if (!bufref_valid(&bufref)) |
7 | 6992 EMSG(_("E246: FileChangedShell autocommand deleted buffer")); |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
6993 #ifdef FEAT_EVAL |
179 | 6994 s = get_vim_var_str(VV_FCS_CHOICE); |
6995 if (STRCMP(s, "reload") == 0 && *reason != 'd') | |
6996 reload = TRUE; | |
6997 else if (STRCMP(s, "ask") == 0) | |
6998 n = FALSE; | |
6999 else | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
7000 #endif |
179 | 7001 return 2; |
7 | 7002 } |
179 | 7003 if (!n) |
7 | 7004 { |
179 | 7005 if (*reason == 'd') |
7006 mesg = _("E211: File \"%s\" no longer available"); | |
7 | 7007 else |
7008 { | |
7009 helpmesg = TRUE; | |
7010 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
7011 can_reload = TRUE; | |
7012 #endif | |
7013 /* | |
7014 * Check if the file contents really changed to avoid | |
7015 * giving a warning when only the timestamp was set (e.g., | |
7016 * checked out of CVS). Always warn when the buffer was | |
7017 * changed. | |
7018 */ | |
179 | 7019 if (reason[2] == 'n') |
7020 { | |
7 | 7021 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well"); |
179 | 7022 mesg2 = _("See \":help W12\" for more info."); |
7023 } | |
7024 else if (reason[1] == 'h') | |
7025 { | |
7 | 7026 mesg = _("W11: Warning: File \"%s\" has changed since editing started"); |
179 | 7027 mesg2 = _("See \":help W11\" for more info."); |
7028 } | |
7029 else if (*reason == 'm') | |
7030 { | |
7 | 7031 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started"); |
179 | 7032 mesg2 = _("See \":help W16\" for more info."); |
7033 } | |
1913 | 7034 else |
7035 /* Only timestamp changed, store it to avoid a warning | |
7036 * in check_mtime() later. */ | |
7037 buf->b_mtime_read = buf->b_mtime; | |
7 | 7038 } |
7039 } | |
7040 } | |
7041 | |
7042 } | |
7043 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W) | |
7044 && vim_fexists(buf->b_ffname)) | |
7045 { | |
7046 retval = 1; | |
7047 mesg = _("W13: Warning: File \"%s\" has been created after editing started"); | |
7048 buf->b_flags |= BF_NEW_W; | |
7049 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
7050 can_reload = TRUE; | |
7051 #endif | |
7052 } | |
7053 | |
7054 if (mesg != NULL) | |
7055 { | |
7056 path = home_replace_save(buf, buf->b_fname); | |
7057 if (path != NULL) | |
7058 { | |
179 | 7059 if (!helpmesg) |
7 | 7060 mesg2 = ""; |
7061 tbuf = alloc((unsigned)(STRLEN(path) + STRLEN(mesg) | |
7062 + STRLEN(mesg2) + 2)); | |
7063 sprintf((char *)tbuf, mesg, path); | |
1848 | 7064 #ifdef FEAT_EVAL |
7065 /* Set warningmsg here, before the unimportant and output-specific | |
7066 * mesg2 has been appended. */ | |
7067 set_vim_var_string(VV_WARNINGMSG, tbuf, -1); | |
7068 #endif | |
7 | 7069 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
7070 if (can_reload) | |
7071 { | |
7072 if (*mesg2 != NUL) | |
7073 { | |
7074 STRCAT(tbuf, "\n"); | |
7075 STRCAT(tbuf, mesg2); | |
7076 } | |
7077 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"), tbuf, | |
2684 | 7078 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2) |
7 | 7079 reload = TRUE; |
7080 } | |
7081 else | |
7082 #endif | |
7083 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned) | |
7084 { | |
7085 if (*mesg2 != NUL) | |
7086 { | |
7087 STRCAT(tbuf, "; "); | |
7088 STRCAT(tbuf, mesg2); | |
7089 } | |
7090 EMSG(tbuf); | |
7091 retval = 2; | |
7092 } | |
7093 else | |
7094 { | |
7095 if (!autocmd_busy) | |
7096 { | |
7097 msg_start(); | |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
7098 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST); |
7 | 7099 if (*mesg2 != NUL) |
7100 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
|
7101 HL_ATTR(HLF_W) + MSG_HIST); |
7 | 7102 msg_clr_eos(); |
7103 (void)msg_end(); | |
7104 if (emsg_silent == 0) | |
7105 { | |
7106 out_flush(); | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
7107 #ifdef FEAT_GUI |
7 | 7108 if (!focus) |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
7109 #endif |
7 | 7110 /* give the user some time to think about it */ |
7111 ui_delay(1000L, TRUE); | |
7112 | |
7113 /* don't redraw and erase the message */ | |
7114 redraw_cmdline = FALSE; | |
7115 } | |
7116 } | |
7117 already_warned = TRUE; | |
7118 } | |
7119 | |
7120 vim_free(path); | |
7121 vim_free(tbuf); | |
7122 } | |
7123 } | |
7124 | |
7125 if (reload) | |
3776 | 7126 { |
311 | 7127 /* Reload the buffer. */ |
628 | 7128 buf_reload(buf, orig_mode); |
3776 | 7129 #ifdef FEAT_PERSISTENT_UNDO |
7130 if (buf->b_p_udf && buf->b_ffname != NULL) | |
7131 { | |
7132 char_u hash[UNDO_HASH_SIZE]; | |
7133 buf_T *save_curbuf = curbuf; | |
7134 | |
7135 /* Any existing undo file is unusable, write it now. */ | |
7136 curbuf = buf; | |
7137 u_compute_hash(hash); | |
7138 u_write_undo(NULL, FALSE, buf, hash); | |
7139 curbuf = save_curbuf; | |
7140 } | |
7141 #endif | |
7142 } | |
7 | 7143 |
1620 | 7144 /* 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
|
7145 if (bufref_valid(&bufref) && retval != 0) |
765 | 7146 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, |
7147 buf->b_fname, buf->b_fname, FALSE, buf); | |
7 | 7148 #ifdef FEAT_GUI |
7149 /* restore this in case an autocommand has set it; it would break | |
7150 * 'mousefocus' */ | |
7151 need_mouse_correct = save_mouse_correct; | |
7152 #endif | |
7153 | |
7154 return retval; | |
7155 } | |
7156 | |
311 | 7157 /* |
7158 * Reload a buffer that is already loaded. | |
7159 * Used when the file was changed outside of Vim. | |
628 | 7160 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected. |
7161 * buf->b_orig_mode may have been reset already. | |
311 | 7162 */ |
7163 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7164 buf_reload(buf_T *buf, int orig_mode) |
311 | 7165 { |
7166 exarg_T ea; | |
7167 pos_T old_cursor; | |
7168 linenr_T old_topline; | |
7169 int old_ro = buf->b_p_ro; | |
7170 buf_T *savebuf; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
7171 bufref_T bufref; |
311 | 7172 int saved = OK; |
7173 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
|
7174 int flags = READ_NEW; |
311 | 7175 |
7176 /* set curwin/curbuf for "buf" and save some things */ | |
7177 aucmd_prepbuf(&aco, buf); | |
7178 | |
7179 /* We only want to read the text from the file, not reset the syntax | |
7180 * highlighting, clear marks, diff status, etc. Force the fileformat | |
7181 * and encoding to be the same. */ | |
7182 if (prep_exarg(&ea, buf) == OK) | |
7183 { | |
7184 old_cursor = curwin->w_cursor; | |
7185 old_topline = curwin->w_topline; | |
7186 | |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
7187 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
|
7188 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7189 /* 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
|
7190 * 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
|
7191 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
|
7192 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
|
7193 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
|
7194 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7195 |
311 | 7196 /* |
7197 * To behave like when a new file is edited (matters for | |
7198 * BufReadPost autocommands) we first need to delete the current | |
7199 * buffer contents. But if reading the file fails we should keep | |
7200 * the old contents. Can't use memory only, the file might be | |
7201 * too big. Use a hidden buffer to move the buffer contents to. | |
7202 */ | |
11121
778c10516955
patch 8.0.0448: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
10952
diff
changeset
|
7203 if (BUFEMPTY() || saved == FAIL) |
311 | 7204 savebuf = NULL; |
7205 else | |
7206 { | |
7207 /* Allocate a buffer without putting it in the buffer list. */ | |
7208 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
|
7209 set_bufref(&bufref, savebuf); |
837 | 7210 if (savebuf != NULL && buf == curbuf) |
311 | 7211 { |
7212 /* Open the memline. */ | |
7213 curbuf = savebuf; | |
7214 curwin->w_buffer = savebuf; | |
625 | 7215 saved = ml_open(curbuf); |
311 | 7216 curbuf = buf; |
7217 curwin->w_buffer = buf; | |
7218 } | |
837 | 7219 if (savebuf == NULL || saved == FAIL || buf != curbuf |
311 | 7220 || move_lines(buf, savebuf) == FAIL) |
7221 { | |
7222 EMSG2(_("E462: Could not prepare for reloading \"%s\""), | |
7223 buf->b_fname); | |
7224 saved = FAIL; | |
7225 } | |
7226 } | |
7227 | |
7228 if (saved == OK) | |
7229 { | |
7230 curbuf->b_flags |= BF_CHECK_RO; /* check for RO again */ | |
7231 keep_filetype = TRUE; /* don't detect 'filetype' */ | |
7232 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, | |
7233 (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
|
7234 (linenr_T)MAXLNUM, &ea, flags) != OK) |
311 | 7235 { |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
7236 #if defined(FEAT_EVAL) |
311 | 7237 if (!aborting()) |
7238 #endif | |
7239 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
|
7240 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf) |
311 | 7241 { |
7242 /* Put the text back from the save buffer. First | |
7243 * 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
|
7244 while (!BUFEMPTY()) |
837 | 7245 if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL) |
311 | 7246 break; |
7247 (void)move_lines(savebuf, buf); | |
7248 } | |
7249 } | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7250 else if (buf == curbuf) /* "buf" still valid */ |
311 | 7251 { |
7252 /* Mark the buffer as unmodified and free undo info. */ | |
7253 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
|
7254 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
|
7255 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7256 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
|
7257 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
|
7258 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7259 else |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
7260 { |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7261 /* 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
|
7262 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
|
7263 } |
311 | 7264 } |
7265 } | |
7266 vim_free(ea.cmd); | |
7267 | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
7268 if (savebuf != NULL && bufref_valid(&bufref)) |
311 | 7269 wipe_buffer(savebuf, FALSE); |
7270 | |
7271 #ifdef FEAT_DIFF | |
7272 /* Invalidate diff info if necessary. */ | |
837 | 7273 diff_invalidate(curbuf); |
311 | 7274 #endif |
7275 | |
7276 /* Restore the topline and cursor position and check it (lines may | |
7277 * have been removed). */ | |
7278 if (old_topline > curbuf->b_ml.ml_line_count) | |
7279 curwin->w_topline = curbuf->b_ml.ml_line_count; | |
7280 else | |
7281 curwin->w_topline = old_topline; | |
7282 curwin->w_cursor = old_cursor; | |
7283 check_cursor(); | |
7284 update_topline(); | |
7285 keep_filetype = FALSE; | |
7286 #ifdef FEAT_FOLDING | |
7287 { | |
1863 | 7288 win_T *wp; |
7289 tabpage_T *tp; | |
311 | 7290 |
7291 /* Update folds unless they are defined manually. */ | |
1863 | 7292 FOR_ALL_TAB_WINDOWS(tp, wp) |
311 | 7293 if (wp->w_buffer == curwin->w_buffer |
7294 && !foldmethodIsManual(wp)) | |
7295 foldUpdateAll(wp); | |
7296 } | |
7297 #endif | |
7298 /* If the mode didn't change and 'readonly' was set, keep the old | |
7299 * value; the user probably used the ":view" command. But don't | |
7300 * reset it, might have had a read error. */ | |
7301 if (orig_mode == curbuf->b_orig_mode) | |
7302 curbuf->b_p_ro |= old_ro; | |
4071 | 7303 |
7304 /* Modelines must override settings done by autocommands. */ | |
7305 do_modelines(0); | |
311 | 7306 } |
7307 | |
7308 /* restore curwin/curbuf and a few other things */ | |
7309 aucmd_restbuf(&aco); | |
7310 /* Careful: autocommands may have made "buf" invalid! */ | |
7311 } | |
7312 | |
7 | 7313 void |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
7314 buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED) |
7 | 7315 { |
7316 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
|
7317 buf->b_orig_size = st->st_size; |
7 | 7318 #ifdef HAVE_ST_MODE |
7319 buf->b_orig_mode = (int)st->st_mode; | |
7320 #else | |
7321 buf->b_orig_mode = mch_getperm(fname); | |
7322 #endif | |
7323 } | |
7324 | |
7325 /* | |
7326 * Adjust the line with missing eol, used for the next write. | |
7327 * Used for do_filter(), when the input lines for the filter are deleted. | |
7328 */ | |
7329 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7330 write_lnum_adjust(linenr_T offset) |
7 | 7331 { |
2707 | 7332 if (curbuf->b_no_eol_lnum != 0) /* only if there is a missing eol */ |
7333 curbuf->b_no_eol_lnum += offset; | |
7 | 7334 } |
7335 | |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7336 #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
|
7337 /* |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7338 * 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
|
7339 * 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
|
7340 */ |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7341 int |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7342 delete_recursive(char_u *name) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7343 { |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7344 int result = 0; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7345 char_u **files; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7346 int file_count; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7347 int i; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7348 char_u *exp; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7349 |
7629
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7350 /* 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
|
7351 * points to. */ |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7352 if ( |
7657
9c5e8254ea6b
commit https://github.com/vim/vim/commit/203258c3ad2966cc9d08b3805b103333988b30b7
Christian Brabandt <cb@256bit.org>
parents:
7641
diff
changeset
|
7353 # if defined(UNIX) || defined(WIN32) |
9c5e8254ea6b
commit https://github.com/vim/vim/commit/203258c3ad2966cc9d08b3805b103333988b30b7
Christian Brabandt <cb@256bit.org>
parents:
7641
diff
changeset
|
7354 mch_isrealdir(name) |
7629
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7355 # else |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7356 mch_isdir(name) |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7357 # endif |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7358 ) |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7359 { |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7360 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
|
7361 exp = vim_strsave(NameBuff); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7362 if (exp == NULL) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7363 return -1; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7364 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
|
7365 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
|
7366 { |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7367 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
|
7368 if (delete_recursive(files[i]) != 0) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7369 result = -1; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7370 FreeWild(file_count, files); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7371 } |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7372 else |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7373 result = -1; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7374 vim_free(exp); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7375 (void)mch_rmdir(name); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7376 } |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7377 else |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7378 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
|
7379 |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7380 return result; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7381 } |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7382 #endif |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7383 |
7 | 7384 #if defined(TEMPDIRNAMES) || defined(PROTO) |
7385 static long temp_count = 0; /* Temp filename counter. */ | |
7386 | |
7387 /* | |
7388 * Delete the temp directory and all files it contains. | |
7389 */ | |
7390 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7391 vim_deltempdir(void) |
7 | 7392 { |
7393 if (vim_tempdir != NULL) | |
7394 { | |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7395 /* remove the trailing path separator */ |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7396 gettail(vim_tempdir)[-1] = NUL; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7397 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
|
7398 VIM_CLEAR(vim_tempdir); |
7 | 7399 } |
7400 } | |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7401 |
7 | 7402 /* |
1997 | 7403 * Directory "tempdir" was created. Expand this name to a full path and put |
7404 * it in "vim_tempdir". This avoids that using ":cd" would confuse us. | |
7405 * "tempdir" must be no longer than MAXPATHL. | |
7406 */ | |
7407 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7408 vim_settempdir(char_u *tempdir) |
1997 | 7409 { |
7410 char_u *buf; | |
7411 | |
7412 buf = alloc((unsigned)MAXPATHL + 2); | |
7413 if (buf != NULL) | |
7414 { | |
7415 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL) | |
7416 STRCPY(buf, tempdir); | |
9536
b2aada04d84e
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Christian Brabandt <cb@256bit.org>
parents:
9509
diff
changeset
|
7417 add_pathsep(buf); |
1997 | 7418 vim_tempdir = vim_strsave(buf); |
7419 vim_free(buf); | |
7420 } | |
7421 } | |
2006 | 7422 #endif |
1997 | 7423 |
7424 /* | |
7 | 7425 * vim_tempname(): Return a unique name that can be used for a temp file. |
7426 * | |
9291
bff3cd5cf922
commit https://github.com/vim/vim/commit/76ae22fef3cb224ca7fbf97517f881e825d4d0c2
Christian Brabandt <cb@256bit.org>
parents:
9260
diff
changeset
|
7427 * 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
|
7428 * guaranteed to NOT be created. |
7 | 7429 * |
7430 * The returned pointer is to allocated memory. | |
7431 * The returned pointer is NULL if no valid name was found. | |
7432 */ | |
7433 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7434 vim_tempname( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7435 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
|
7436 int keep UNUSED) |
7 | 7437 { |
7438 #ifdef USE_TMPNAM | |
7439 char_u itmp[L_tmpnam]; /* use tmpnam() */ | |
7440 #else | |
7441 char_u itmp[TEMPNAMELEN]; | |
7442 #endif | |
7443 | |
7444 #ifdef TEMPDIRNAMES | |
7445 static char *(tempdirs[]) = {TEMPDIRNAMES}; | |
7446 int i; | |
7447 # ifndef EEXIST | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
7448 stat_T st; |
7 | 7449 # endif |
7450 | |
7451 /* | |
7452 * This will create a directory for private use by this instance of Vim. | |
7453 * This is done once, and the same directory is used for all temp files. | |
7454 * This method avoids security problems because of symlink attacks et al. | |
7455 * It's also a bit faster, because we only need to check for an existing | |
7456 * file when creating the directory and not for each temp file. | |
7457 */ | |
7458 if (vim_tempdir == NULL) | |
7459 { | |
7460 /* | |
7461 * Try the entries in TEMPDIRNAMES to create the temp directory. | |
7462 */ | |
1877 | 7463 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i) |
7 | 7464 { |
2047
85da03763130
updated for version 7.2.333
Bram Moolenaar <bram@zimbu.org>
parents:
2028
diff
changeset
|
7465 # ifndef HAVE_MKDTEMP |
1997 | 7466 size_t itmplen; |
7467 long nr; | |
7468 long off; | |
7469 # endif | |
7470 | |
7305
372c785c04b6
commit https://github.com/vim/vim/commit/e1a61991d9b6fd5f65636d17583f93118268cda5
Christian Brabandt <cb@256bit.org>
parents:
7285
diff
changeset
|
7471 /* 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
|
7472 * Skip the directory check if the expansion fails. */ |
7 | 7473 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
|
7474 if (itmp[0] != '$' && mch_isdir(itmp)) |
372c785c04b6
commit https://github.com/vim/vim/commit/e1a61991d9b6fd5f65636d17583f93118268cda5
Christian Brabandt <cb@256bit.org>
parents:
7285
diff
changeset
|
7475 { |
372c785c04b6
commit https://github.com/vim/vim/commit/e1a61991d9b6fd5f65636d17583f93118268cda5
Christian Brabandt <cb@256bit.org>
parents:
7285
diff
changeset
|
7476 /* directory exists */ |
9536
b2aada04d84e
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Christian Brabandt <cb@256bit.org>
parents:
9509
diff
changeset
|
7477 add_pathsep(itmp); |
1997 | 7478 |
7479 # ifdef HAVE_MKDTEMP | |
9211
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7480 { |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7481 # if defined(UNIX) || defined(VMS) |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7482 /* 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
|
7483 * "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
|
7484 mode_t umask_save = umask(077); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7485 # endif |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7486 /* Leave room for filename */ |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7487 STRCAT(itmp, "vXXXXXX"); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7488 if (mkdtemp((char *)itmp) != NULL) |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7489 vim_settempdir(itmp); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7490 # if defined(UNIX) || defined(VMS) |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7491 (void)umask(umask_save); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7492 # endif |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7493 } |
1997 | 7494 # else |
7 | 7495 /* Get an arbitrary number of up to 6 digits. When it's |
7496 * unlikely that it already exists it will be faster, | |
7497 * otherwise it doesn't matter. The use of mkdir() avoids any | |
7498 * security problems because of the predictable number. */ | |
7499 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
|
7500 itmplen = STRLEN(itmp); |
7 | 7501 |
7502 /* Try up to 10000 different values until we find a name that | |
7503 * doesn't exist. */ | |
7504 for (off = 0; off < 10000L; ++off) | |
7505 { | |
7506 int r; | |
1997 | 7507 # if defined(UNIX) || defined(VMS) |
7 | 7508 mode_t umask_save; |
1997 | 7509 # endif |
7510 | |
7511 sprintf((char *)itmp + itmplen, "v%ld", nr + off); | |
7512 # ifndef EEXIST | |
7 | 7513 /* If mkdir() does not set errno to EEXIST, check for |
7514 * existing file here. There is a race condition then, | |
7515 * although it's fail-safe. */ | |
7516 if (mch_stat((char *)itmp, &st) >= 0) | |
7517 continue; | |
1997 | 7518 # endif |
7519 # if defined(UNIX) || defined(VMS) | |
7 | 7520 /* Make sure the umask doesn't remove the executable bit. |
7521 * "repl" has been reported to use "177". */ | |
7522 umask_save = umask(077); | |
1997 | 7523 # endif |
7 | 7524 r = vim_mkdir(itmp, 0700); |
1997 | 7525 # if defined(UNIX) || defined(VMS) |
7 | 7526 (void)umask(umask_save); |
1997 | 7527 # endif |
7 | 7528 if (r == 0) |
7529 { | |
1997 | 7530 vim_settempdir(itmp); |
7 | 7531 break; |
7532 } | |
1997 | 7533 # ifdef EEXIST |
7 | 7534 /* If the mkdir() didn't fail because the file/dir exists, |
7535 * we probably can't create any dir here, try another | |
7536 * place. */ | |
7537 if (errno != EEXIST) | |
1997 | 7538 # endif |
7 | 7539 break; |
7540 } | |
1997 | 7541 # endif /* HAVE_MKDTEMP */ |
7 | 7542 if (vim_tempdir != NULL) |
7543 break; | |
7544 } | |
7545 } | |
7546 } | |
7547 | |
7548 if (vim_tempdir != NULL) | |
7549 { | |
7550 /* There is no need to check if the file exists, because we own the | |
7551 * directory and nobody else creates a file in it. */ | |
7552 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++); | |
7553 return vim_strsave(itmp); | |
7554 } | |
7555 | |
7556 return NULL; | |
7557 | |
7558 #else /* TEMPDIRNAMES */ | |
7559 | |
7560 # ifdef WIN3264 | |
7561 char szTempFile[_MAX_PATH + 1]; | |
7562 char buf4[4]; | |
7563 char_u *retval; | |
7564 char_u *p; | |
7565 | |
7566 STRCPY(itmp, ""); | |
7567 if (GetTempPath(_MAX_PATH, szTempFile) == 0) | |
2695 | 7568 { |
7569 szTempFile[0] = '.'; /* GetTempPath() failed, use current dir */ | |
7570 szTempFile[1] = NUL; | |
7571 } | |
7 | 7572 strcpy(buf4, "VIM"); |
7573 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
|
7574 if (GetTempFileName(szTempFile, buf4, 0, (LPSTR)itmp) == 0) |
7 | 7575 return NULL; |
6721 | 7576 if (!keep) |
7577 /* 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
|
7578 (void)DeleteFile((LPSTR)itmp); |
7 | 7579 |
7580 /* Backslashes in a temp file name cause problems when filtering with | |
7581 * "sh". NOTE: This also checks 'shellcmdflag' to help those people who | |
7582 * didn't set 'shellslash'. */ | |
7583 retval = vim_strsave(itmp); | |
7584 if (*p_shcf == '-' || p_ssl) | |
7585 for (p = retval; *p; ++p) | |
7586 if (*p == '\\') | |
7587 *p = '/'; | |
7588 return retval; | |
7589 | |
7590 # else /* WIN3264 */ | |
7591 | |
7592 # ifdef USE_TMPNAM | |
2697 | 7593 char_u *p; |
7594 | |
7 | 7595 /* tmpnam() will make its own name */ |
2697 | 7596 p = tmpnam((char *)itmp); |
7597 if (p == NULL || *p == NUL) | |
7 | 7598 return NULL; |
7599 # else | |
7600 char_u *p; | |
7601 | |
7602 # ifdef VMS_TEMPNAM | |
7603 /* mktemp() is not working on VMS. It seems to be | |
7604 * a do-nothing function. Therefore we use tempnam(). | |
7605 */ | |
7606 sprintf((char *)itmp, "VIM%c", extra_char); | |
7607 p = (char_u *)tempnam("tmp:", (char *)itmp); | |
7608 if (p != NULL) | |
7609 { | |
5704 | 7610 /* VMS will use '.LIS' if we don't explicitly specify an extension, |
7 | 7611 * and VIM will then be unable to find the file later */ |
7612 STRCPY(itmp, p); | |
7613 STRCAT(itmp, ".txt"); | |
7614 free(p); | |
7615 } | |
7616 else | |
7617 return NULL; | |
7618 # else | |
7619 STRCPY(itmp, TEMPNAME); | |
7620 if ((p = vim_strchr(itmp, '?')) != NULL) | |
7621 *p = extra_char; | |
7622 if (mktemp((char *)itmp) == NULL) | |
7623 return NULL; | |
7624 # endif | |
7625 # endif | |
7626 | |
7627 return vim_strsave(itmp); | |
7628 # endif /* WIN3264 */ | |
7629 #endif /* TEMPDIRNAMES */ | |
7630 } | |
7631 | |
7632 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) | |
7633 /* | |
7170
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
7634 * 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
|
7635 * it looks like a URL. |
7 | 7636 */ |
7637 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7638 forward_slash(char_u *fname) |
7 | 7639 { |
7640 char_u *p; | |
7641 | |
7170
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
7642 if (path_with_url(fname)) |
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
7643 return; |
7 | 7644 for (p = fname; *p != NUL; ++p) |
7645 # ifdef FEAT_MBYTE | |
7646 /* The Big5 encoding can have '\' in the trail byte. */ | |
474 | 7647 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 7648 ++p; |
7649 else | |
7650 # endif | |
7651 if (*p == '\\') | |
7652 *p = '/'; | |
7653 } | |
7654 #endif | |
7655 | |
7656 | |
7657 /* | |
7658 * Code for automatic commands. | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
7659 */ |
7 | 7660 |
7661 /* | |
7662 * The autocommands are stored in a list for each event. | |
7663 * Autocommands for the same pattern, that are consecutive, are joined | |
7664 * together, to avoid having to match the pattern too often. | |
7665 * The result is an array of Autopat lists, which point to AutoCmd lists: | |
7666 * | |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7667 * last_autopat[0] -----------------------------+ |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7668 * V |
7 | 7669 * first_autopat[0] --> Autopat.next --> Autopat.next --> NULL |
7670 * Autopat.cmds Autopat.cmds | |
7671 * | | | |
7672 * V V | |
7673 * AutoCmd.next AutoCmd.next | |
7674 * | | | |
7675 * V V | |
7676 * AutoCmd.next NULL | |
7677 * | | |
7678 * V | |
7679 * NULL | |
7680 * | |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7681 * last_autopat[1] --------+ |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7682 * V |
7 | 7683 * first_autopat[1] --> Autopat.next --> NULL |
7684 * Autopat.cmds | |
7685 * | | |
7686 * V | |
7687 * AutoCmd.next | |
7688 * | | |
7689 * V | |
7690 * NULL | |
7691 * etc. | |
7692 * | |
7693 * The order of AutoCmds is important, this is the order in which they were | |
7694 * defined and will have to be executed. | |
7695 */ | |
7696 typedef struct AutoCmd | |
7697 { | |
7698 char_u *cmd; /* The command to be executed (NULL | |
7699 when command has been removed) */ | |
7700 char nested; /* If autocommands nest here */ | |
7701 char last; /* last command in list */ | |
7702 #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
|
7703 sctx_T script_ctx; /* script context where defined */ |
7 | 7704 #endif |
7705 struct AutoCmd *next; /* Next AutoCmd in list */ | |
7706 } AutoCmd; | |
7707 | |
7708 typedef struct AutoPat | |
7709 { | |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7710 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
|
7711 * be the first entry */ |
7 | 7712 char_u *pat; /* pattern as typed (NULL when pattern |
7713 has been removed) */ | |
4861
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7714 regprog_T *reg_prog; /* compiled regprog for pattern */ |
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7715 AutoCmd *cmds; /* list of commands to do */ |
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7716 int group; /* group ID */ |
7 | 7717 int patlen; /* strlen() of pat */ |
4861
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7718 int buflocal_nr; /* !=0 for buffer-local AutoPat */ |
7 | 7719 char allow_dirs; /* Pattern may match whole path */ |
7720 char last; /* last pattern for apply_autocmds() */ | |
7721 } AutoPat; | |
7722 | |
7723 static struct event_name | |
7724 { | |
7725 char *name; /* event name */ | |
661 | 7726 event_T event; /* event number */ |
7 | 7727 } event_names[] = |
7728 { | |
7729 {"BufAdd", EVENT_BUFADD}, | |
7730 {"BufCreate", EVENT_BUFADD}, | |
7731 {"BufDelete", EVENT_BUFDELETE}, | |
7732 {"BufEnter", EVENT_BUFENTER}, | |
7733 {"BufFilePost", EVENT_BUFFILEPOST}, | |
7734 {"BufFilePre", EVENT_BUFFILEPRE}, | |
7735 {"BufHidden", EVENT_BUFHIDDEN}, | |
7736 {"BufLeave", EVENT_BUFLEAVE}, | |
7737 {"BufNew", EVENT_BUFNEW}, | |
7738 {"BufNewFile", EVENT_BUFNEWFILE}, | |
7739 {"BufRead", EVENT_BUFREADPOST}, | |
7740 {"BufReadCmd", EVENT_BUFREADCMD}, | |
7741 {"BufReadPost", EVENT_BUFREADPOST}, | |
7742 {"BufReadPre", EVENT_BUFREADPRE}, | |
7743 {"BufUnload", EVENT_BUFUNLOAD}, | |
7744 {"BufWinEnter", EVENT_BUFWINENTER}, | |
7745 {"BufWinLeave", EVENT_BUFWINLEAVE}, | |
7746 {"BufWipeout", EVENT_BUFWIPEOUT}, | |
7747 {"BufWrite", EVENT_BUFWRITEPRE}, | |
7748 {"BufWritePost", EVENT_BUFWRITEPOST}, | |
7749 {"BufWritePre", EVENT_BUFWRITEPRE}, | |
7750 {"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
|
7751 {"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
|
7752 {"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
|
7753 {"CmdlineLeave", EVENT_CMDLINELEAVE}, |
7 | 7754 {"CmdwinEnter", EVENT_CMDWINENTER}, |
7755 {"CmdwinLeave", EVENT_CMDWINLEAVE}, | |
6154 | 7756 {"CmdUndefined", EVENT_CMDUNDEFINED}, |
12 | 7757 {"ColorScheme", EVENT_COLORSCHEME}, |
13810
cad480bac9e1
patch 8.0.1777: cannot cleanup before loading another colorscheme
Christian Brabandt <cb@256bit.org>
parents:
13804
diff
changeset
|
7758 {"ColorSchemePre", EVENT_COLORSCHEMEPRE}, |
3676 | 7759 {"CompleteDone", EVENT_COMPLETEDONE}, |
661 | 7760 {"CursorHold", EVENT_CURSORHOLD}, |
7761 {"CursorHoldI", EVENT_CURSORHOLDI}, | |
7762 {"CursorMoved", EVENT_CURSORMOVED}, | |
7763 {"CursorMovedI", EVENT_CURSORMOVEDI}, | |
14770
27055ad9276b
patch 8.1.0397: no event triggered after updating diffs
Christian Brabandt <cb@256bit.org>
parents:
14730
diff
changeset
|
7764 {"DiffUpdated", EVENT_DIFFUPDATED}, |
13170
6559e98f3e74
patch 8.0.1459: cannot handle change of directory
Christian Brabandt <cb@256bit.org>
parents:
13142
diff
changeset
|
7765 {"DirChanged", EVENT_DIRCHANGED}, |
7 | 7766 {"EncodingChanged", EVENT_ENCODINGCHANGED}, |
13442
94e638936d3e
patch 8.0.1595: no autocommand triggered before exiting
Christian Brabandt <cb@256bit.org>
parents:
13380
diff
changeset
|
7767 {"ExitPre", EVENT_EXITPRE}, |
7 | 7768 {"FileEncoding", EVENT_ENCODINGCHANGED}, |
7769 {"FileAppendPost", EVENT_FILEAPPENDPOST}, | |
7770 {"FileAppendPre", EVENT_FILEAPPENDPRE}, | |
7771 {"FileAppendCmd", EVENT_FILEAPPENDCMD}, | |
7772 {"FileChangedShell",EVENT_FILECHANGEDSHELL}, | |
765 | 7773 {"FileChangedShellPost",EVENT_FILECHANGEDSHELLPOST}, |
7 | 7774 {"FileChangedRO", EVENT_FILECHANGEDRO}, |
7775 {"FileReadPost", EVENT_FILEREADPOST}, | |
7776 {"FileReadPre", EVENT_FILEREADPRE}, | |
7777 {"FileReadCmd", EVENT_FILEREADCMD}, | |
7778 {"FileType", EVENT_FILETYPE}, | |
7779 {"FileWritePost", EVENT_FILEWRITEPOST}, | |
7780 {"FileWritePre", EVENT_FILEWRITEPRE}, | |
7781 {"FileWriteCmd", EVENT_FILEWRITECMD}, | |
7782 {"FilterReadPost", EVENT_FILTERREADPOST}, | |
7783 {"FilterReadPre", EVENT_FILTERREADPRE}, | |
7784 {"FilterWritePost", EVENT_FILTERWRITEPOST}, | |
7785 {"FilterWritePre", EVENT_FILTERWRITEPRE}, | |
7786 {"FocusGained", EVENT_FOCUSGAINED}, | |
7787 {"FocusLost", EVENT_FOCUSLOST}, | |
7788 {"FuncUndefined", EVENT_FUNCUNDEFINED}, | |
7789 {"GUIEnter", EVENT_GUIENTER}, | |
946 | 7790 {"GUIFailed", EVENT_GUIFAILED}, |
11 | 7791 {"InsertChange", EVENT_INSERTCHANGE}, |
7792 {"InsertEnter", EVENT_INSERTENTER}, | |
7793 {"InsertLeave", EVENT_INSERTLEAVE}, | |
2845 | 7794 {"InsertCharPre", EVENT_INSERTCHARPRE}, |
434 | 7795 {"MenuPopup", EVENT_MENUPOPUP}, |
6935 | 7796 {"OptionSet", EVENT_OPTIONSET}, |
161 | 7797 {"QuickFixCmdPost", EVENT_QUICKFIXCMDPOST}, |
7798 {"QuickFixCmdPre", EVENT_QUICKFIXCMDPRE}, | |
3568 | 7799 {"QuitPre", EVENT_QUITPRE}, |
7 | 7800 {"RemoteReply", EVENT_REMOTEREPLY}, |
574 | 7801 {"SessionLoadPost", EVENT_SESSIONLOADPOST}, |
723 | 7802 {"ShellCmdPost", EVENT_SHELLCMDPOST}, |
7803 {"ShellFilterPost", EVENT_SHELLFILTERPOST}, | |
715 | 7804 {"SourcePre", EVENT_SOURCEPRE}, |
1061 | 7805 {"SourceCmd", EVENT_SOURCECMD}, |
649 | 7806 {"SpellFileMissing",EVENT_SPELLFILEMISSING}, |
7 | 7807 {"StdinReadPost", EVENT_STDINREADPOST}, |
7808 {"StdinReadPre", EVENT_STDINREADPRE}, | |
674 | 7809 {"SwapExists", EVENT_SWAPEXISTS}, |
7 | 7810 {"Syntax", EVENT_SYNTAX}, |
9595
0190d5de215f
commit https://github.com/vim/vim/commit/c917da4b3e8801a255dbefea8e4ed19c1c716dd8
Christian Brabandt <cb@256bit.org>
parents:
9536
diff
changeset
|
7811 {"TabNew", EVENT_TABNEW}, |
9599
42a8a81decdf
commit https://github.com/vim/vim/commit/12c11d553053f5a9eae9eb3c518279b12fa928c2
Christian Brabandt <cb@256bit.org>
parents:
9595
diff
changeset
|
7812 {"TabClosed", EVENT_TABCLOSED}, |
676 | 7813 {"TabEnter", EVENT_TABENTER}, |
7814 {"TabLeave", EVENT_TABLEAVE}, | |
7 | 7815 {"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
|
7816 {"TerminalOpen", EVENT_TERMINALOPEN}, |
7 | 7817 {"TermResponse", EVENT_TERMRESPONSE}, |
4232 | 7818 {"TextChanged", EVENT_TEXTCHANGED}, |
7819 {"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
|
7820 {"TextChangedP", EVENT_TEXTCHANGEDP}, |
7 | 7821 {"User", EVENT_USER}, |
7822 {"VimEnter", EVENT_VIMENTER}, | |
7823 {"VimLeave", EVENT_VIMLEAVE}, | |
7824 {"VimLeavePre", EVENT_VIMLEAVEPRE}, | |
9595
0190d5de215f
commit https://github.com/vim/vim/commit/c917da4b3e8801a255dbefea8e4ed19c1c716dd8
Christian Brabandt <cb@256bit.org>
parents:
9536
diff
changeset
|
7825 {"WinNew", EVENT_WINNEW}, |
7 | 7826 {"WinEnter", EVENT_WINENTER}, |
7827 {"WinLeave", EVENT_WINLEAVE}, | |
765 | 7828 {"VimResized", EVENT_VIMRESIZED}, |
13037
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
7829 {"TextYankPost", EVENT_TEXTYANKPOST}, |
661 | 7830 {NULL, (event_T)0} |
7 | 7831 }; |
7832 | |
7833 static AutoPat *first_autopat[NUM_EVENTS] = | |
7834 { | |
7835 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7836 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7837 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7838 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
18 | 7839 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
6935 | 7840 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
7 | 7841 }; |
7842 | |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7843 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
|
7844 { |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7845 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
|
7846 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
|
7847 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
|
7848 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
|
7849 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
|
7850 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
|
7851 }; |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
7852 |
7 | 7853 /* |
7854 * struct used to keep status while executing autocommands for an event. | |
7855 */ | |
7856 typedef struct AutoPatCmd | |
7857 { | |
7858 AutoPat *curpat; /* next AutoPat to examine */ | |
7859 AutoCmd *nextcmd; /* next AutoCmd to execute */ | |
7860 int group; /* group being used */ | |
7861 char_u *fname; /* fname to match with */ | |
7862 char_u *sfname; /* sfname to match with */ | |
7863 char_u *tail; /* tail of fname */ | |
661 | 7864 event_T event; /* current event */ |
40 | 7865 int arg_bufnr; /* initially equal to <abuf>, set to zero when |
7866 buf is deleted */ | |
7867 struct AutoPatCmd *next; /* chain of active apc-s for auto-invalidation*/ | |
7 | 7868 } AutoPatCmd; |
7869 | |
297 | 7870 static AutoPatCmd *active_apc_list = NULL; /* stack of active autocommands */ |
40 | 7871 |
7 | 7872 /* |
7873 * augroups stores a list of autocmd group names. | |
7874 */ | |
297 | 7875 static garray_T augroups = {0, 0, sizeof(char_u *), 10, NULL}; |
7 | 7876 #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
|
7877 /* 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
|
7878 static char_u *deleted_augroup = NULL; |
7 | 7879 |
7880 /* | |
7881 * The ID of the current group. Group 0 is the default one. | |
7882 */ | |
7883 static int current_augroup = AUGROUP_DEFAULT; | |
7884 | |
7885 static int au_need_clean = FALSE; /* need to delete marked patterns */ | |
7886 | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7887 static void show_autocmd(AutoPat *ap, event_T event); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7888 static void au_remove_pat(AutoPat *ap); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7889 static void au_remove_cmds(AutoPat *ap); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7890 static void au_cleanup(void); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7891 static int au_new_group(char_u *name); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7892 static void au_del_group(char_u *name); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7893 static event_T event_name2nr(char_u *start, char_u **end); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7894 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
|
7895 static char_u *find_end_event(char_u *arg, int have_group); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7896 static int event_ignored(event_T event); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7897 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
|
7898 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
|
7899 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
|
7900 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
|
7901 static int match_file_pat(char_u *pattern, regprog_T **prog, char_u *fname, char_u *sfname, char_u *tail, int allow_dirs); |
7 | 7902 |
40 | 7903 |
661 | 7904 static event_T last_event; |
7 | 7905 static int last_group; |
1410 | 7906 static int autocmd_blocked = 0; /* block all autocmds */ |
7 | 7907 |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7908 static char_u * |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7909 get_deleted_augroup(void) |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7910 { |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7911 if (deleted_augroup == NULL) |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7912 deleted_augroup = (char_u *)_("--Deleted--"); |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7913 return deleted_augroup; |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7914 } |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7915 |
7 | 7916 /* |
7917 * Show the autocommands for one AutoPat. | |
7918 */ | |
7919 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7920 show_autocmd(AutoPat *ap, event_T event) |
7 | 7921 { |
7922 AutoCmd *ac; | |
7923 | |
7924 /* Check for "got_int" (here and at various places below), which is set | |
7925 * when "q" has been hit for the "--more--" prompt */ | |
7926 if (got_int) | |
7927 return; | |
7928 if (ap->pat == NULL) /* pattern has been removed */ | |
7929 return; | |
7930 | |
7931 msg_putchar('\n'); | |
7932 if (got_int) | |
7933 return; | |
7934 if (event != last_event || ap->group != last_group) | |
7935 { | |
7936 if (ap->group != AUGROUP_DEFAULT) | |
7937 { | |
7938 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
|
7939 msg_puts_attr(get_deleted_augroup(), HL_ATTR(HLF_E)); |
7 | 7940 else |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
7941 msg_puts_attr(AUGROUP_NAME(ap->group), HL_ATTR(HLF_T)); |
7 | 7942 msg_puts((char_u *)" "); |
7943 } | |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
7944 msg_puts_attr(event_nr2name(event), HL_ATTR(HLF_T)); |
7 | 7945 last_event = event; |
7946 last_group = ap->group; | |
7947 msg_putchar('\n'); | |
7948 if (got_int) | |
7949 return; | |
7950 } | |
7951 msg_col = 4; | |
7952 msg_outtrans(ap->pat); | |
7953 | |
7954 for (ac = ap->cmds; ac != NULL; ac = ac->next) | |
7955 { | |
7956 if (ac->cmd != NULL) /* skip removed commands */ | |
7957 { | |
7958 if (msg_col >= 14) | |
7959 msg_putchar('\n'); | |
7960 msg_col = 14; | |
7961 if (got_int) | |
7962 return; | |
7963 msg_outtrans(ac->cmd); | |
500 | 7964 #ifdef FEAT_EVAL |
7965 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
|
7966 last_set_msg(ac->script_ctx); |
500 | 7967 #endif |
7 | 7968 if (got_int) |
7969 return; | |
7970 if (ac->next != NULL) | |
7971 { | |
7972 msg_putchar('\n'); | |
7973 if (got_int) | |
7974 return; | |
7975 } | |
7976 } | |
7977 } | |
7978 } | |
7979 | |
7980 /* | |
7981 * Mark an autocommand pattern for deletion. | |
7982 */ | |
7983 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7984 au_remove_pat(AutoPat *ap) |
7 | 7985 { |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
7986 VIM_CLEAR(ap->pat); |
40 | 7987 ap->buflocal_nr = -1; |
7 | 7988 au_need_clean = TRUE; |
7989 } | |
7990 | |
7991 /* | |
7992 * Mark all commands for a pattern for deletion. | |
7993 */ | |
7994 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7995 au_remove_cmds(AutoPat *ap) |
7 | 7996 { |
7997 AutoCmd *ac; | |
7998 | |
7999 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
|
8000 VIM_CLEAR(ac->cmd); |
7 | 8001 au_need_clean = TRUE; |
8002 } | |
8003 | |
8004 /* | |
8005 * Cleanup autocommands and patterns that have been deleted. | |
8006 * This is only done when not executing autocommands. | |
8007 */ | |
8008 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8009 au_cleanup(void) |
7 | 8010 { |
8011 AutoPat *ap, **prev_ap; | |
8012 AutoCmd *ac, **prev_ac; | |
661 | 8013 event_T event; |
7 | 8014 |
8015 if (autocmd_busy || !au_need_clean) | |
8016 return; | |
8017 | |
8018 /* loop over all events */ | |
661 | 8019 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
8020 event = (event_T)((int)event + 1)) | |
7 | 8021 { |
8022 /* loop over all autocommand patterns */ | |
8023 prev_ap = &(first_autopat[(int)event]); | |
8024 for (ap = *prev_ap; ap != NULL; ap = *prev_ap) | |
8025 { | |
8026 /* loop over all commands for this pattern */ | |
8027 prev_ac = &(ap->cmds); | |
8028 for (ac = *prev_ac; ac != NULL; ac = *prev_ac) | |
8029 { | |
8030 /* remove the command if the pattern is to be deleted or when | |
8031 * the command has been marked for deletion */ | |
8032 if (ap->pat == NULL || ac->cmd == NULL) | |
8033 { | |
8034 *prev_ac = ac->next; | |
8035 vim_free(ac->cmd); | |
8036 vim_free(ac); | |
8037 } | |
8038 else | |
8039 prev_ac = &(ac->next); | |
8040 } | |
8041 | |
8042 /* remove the pattern if it has been marked for deletion */ | |
8043 if (ap->pat == NULL) | |
8044 { | |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8045 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
|
8046 { |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8047 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
|
8048 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
|
8049 else |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8050 /* 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
|
8051 * the struct */ |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8052 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
|
8053 } |
7 | 8054 *prev_ap = ap->next; |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4354
diff
changeset
|
8055 vim_regfree(ap->reg_prog); |
7 | 8056 vim_free(ap); |
8057 } | |
8058 else | |
8059 prev_ap = &(ap->next); | |
8060 } | |
8061 } | |
8062 | |
8063 au_need_clean = FALSE; | |
8064 } | |
8065 | |
8066 /* | |
40 | 8067 * Called when buffer is freed, to remove/invalidate related buffer-local |
8068 * autocmds. | |
8069 */ | |
8070 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8071 aubuflocal_remove(buf_T *buf) |
40 | 8072 { |
8073 AutoPat *ap; | |
661 | 8074 event_T event; |
40 | 8075 AutoPatCmd *apc; |
8076 | |
8077 /* invalidate currently executing autocommands */ | |
8078 for (apc = active_apc_list; apc; apc = apc->next) | |
8079 if (buf->b_fnum == apc->arg_bufnr) | |
8080 apc->arg_bufnr = 0; | |
8081 | |
8082 /* invalidate buflocals looping through events */ | |
661 | 8083 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
8084 event = (event_T)((int)event + 1)) | |
40 | 8085 /* loop over all autocommand patterns */ |
8086 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
8087 if (ap->buflocal_nr == buf->b_fnum) | |
8088 { | |
8089 au_remove_pat(ap); | |
8090 if (p_verbose >= 6) | |
292 | 8091 { |
8092 verbose_enter(); | |
40 | 8093 smsg((char_u *) |
8094 _("auto-removing autocommand: %s <buffer=%d>"), | |
8095 event_nr2name(event), buf->b_fnum); | |
292 | 8096 verbose_leave(); |
8097 } | |
40 | 8098 } |
8099 au_cleanup(); | |
8100 } | |
8101 | |
8102 /* | |
7 | 8103 * Add an autocmd group name. |
8104 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. | |
8105 */ | |
8106 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8107 au_new_group(char_u *name) |
7 | 8108 { |
8109 int i; | |
8110 | |
8111 i = au_find_group(name); | |
8112 if (i == AUGROUP_ERROR) /* the group doesn't exist yet, add it */ | |
8113 { | |
8114 /* First try using a free entry. */ | |
8115 for (i = 0; i < augroups.ga_len; ++i) | |
8116 if (AUGROUP_NAME(i) == NULL) | |
8117 break; | |
8118 if (i == augroups.ga_len && ga_grow(&augroups, 1) == FAIL) | |
8119 return AUGROUP_ERROR; | |
8120 | |
8121 AUGROUP_NAME(i) = vim_strsave(name); | |
8122 if (AUGROUP_NAME(i) == NULL) | |
8123 return AUGROUP_ERROR; | |
8124 if (i == augroups.ga_len) | |
8125 ++augroups.ga_len; | |
8126 } | |
8127 | |
8128 return i; | |
8129 } | |
8130 | |
8131 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8132 au_del_group(char_u *name) |
7 | 8133 { |
8134 int i; | |
8135 | |
8136 i = au_find_group(name); | |
8137 if (i == AUGROUP_ERROR) /* the group doesn't exist */ | |
8138 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
|
8139 else if (i == current_augroup) |
1de911ef1edf
commit https://github.com/vim/vim/commit/de653f08805dde14424d417502a0480a6ad292f8
Christian Brabandt <cb@256bit.org>
parents:
10084
diff
changeset
|
8140 EMSG(_("E936: Cannot delete the current group")); |
7 | 8141 else |
8142 { | |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8143 event_T event; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8144 AutoPat *ap; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8145 int in_use = FALSE; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8146 |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8147 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
|
8148 event = (event_T)((int)event + 1)) |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8149 { |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8150 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
|
8151 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
|
8152 { |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8153 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
|
8154 in_use = TRUE; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8155 event = NUM_EVENTS; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8156 break; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8157 } |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8158 } |
7 | 8159 vim_free(AUGROUP_NAME(i)); |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8160 if (in_use) |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8161 { |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
8162 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
|
8163 } |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8164 else |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8165 AUGROUP_NAME(i) = NULL; |
7 | 8166 } |
8167 } | |
8168 | |
8169 /* | |
8170 * Find the ID of an autocmd group name. | |
8171 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. | |
8172 */ | |
8173 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8174 au_find_group(char_u *name) |
7 | 8175 { |
8176 int i; | |
8177 | |
8178 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
|
8179 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
|
8180 && STRCMP(AUGROUP_NAME(i), name) == 0) |
7 | 8181 return i; |
8182 return AUGROUP_ERROR; | |
8183 } | |
8184 | |
461 | 8185 /* |
8186 * Return TRUE if augroup "name" exists. | |
8187 */ | |
8188 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8189 au_has_group(char_u *name) |
461 | 8190 { |
8191 return au_find_group(name) != AUGROUP_ERROR; | |
8192 } | |
8193 | |
7 | 8194 /* |
8195 * ":augroup {name}". | |
8196 */ | |
8197 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8198 do_augroup(char_u *arg, int del_group) |
7 | 8199 { |
8200 int i; | |
8201 | |
8202 if (del_group) | |
8203 { | |
8204 if (*arg == NUL) | |
8205 EMSG(_(e_argreq)); | |
8206 else | |
8207 au_del_group(arg); | |
8208 } | |
8209 else if (STRICMP(arg, "end") == 0) /* ":aug end": back to group 0 */ | |
8210 current_augroup = AUGROUP_DEFAULT; | |
8211 else if (*arg) /* ":aug xxx": switch to group xxx */ | |
8212 { | |
8213 i = au_new_group(arg); | |
8214 if (i != AUGROUP_ERROR) | |
8215 current_augroup = i; | |
8216 } | |
8217 else /* ":aug": list the group names */ | |
8218 { | |
8219 msg_start(); | |
8220 for (i = 0; i < augroups.ga_len; ++i) | |
8221 { | |
8222 if (AUGROUP_NAME(i) != NULL) | |
8223 { | |
8224 msg_puts(AUGROUP_NAME(i)); | |
8225 msg_puts((char_u *)" "); | |
8226 } | |
8227 } | |
8228 msg_clr_eos(); | |
8229 msg_end(); | |
8230 } | |
8231 } | |
8232 | |
355 | 8233 #if defined(EXITFREE) || defined(PROTO) |
8234 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8235 free_all_autocmds(void) |
355 | 8236 { |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8237 int i; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8238 char_u *s; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8239 |
355 | 8240 for (current_augroup = -1; current_augroup < augroups.ga_len; |
8241 ++current_augroup) | |
8242 do_autocmd((char_u *)"", TRUE); | |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8243 |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8244 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
|
8245 { |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8246 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
|
8247 if (s != get_deleted_augroup()) |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8248 vim_free(s); |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8249 } |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8250 ga_clear(&augroups); |
355 | 8251 } |
8252 #endif | |
8253 | |
7 | 8254 /* |
8255 * Return the event number for event name "start". | |
8256 * Return NUM_EVENTS if the event name was not found. | |
8257 * Return a pointer to the next event name in "end". | |
8258 */ | |
661 | 8259 static event_T |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8260 event_name2nr(char_u *start, char_u **end) |
7 | 8261 { |
8262 char_u *p; | |
8263 int i; | |
8264 int len; | |
8265 | |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8266 /* 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
|
8267 for (p = start; *p && !VIM_ISWHITE(*p) && *p != ',' && *p != '|'; ++p) |
7 | 8268 ; |
8269 for (i = 0; event_names[i].name != NULL; ++i) | |
8270 { | |
8271 len = (int)STRLEN(event_names[i].name); | |
8272 if (len == p - start && STRNICMP(event_names[i].name, start, len) == 0) | |
8273 break; | |
8274 } | |
8275 if (*p == ',') | |
8276 ++p; | |
8277 *end = p; | |
8278 if (event_names[i].name == NULL) | |
8279 return NUM_EVENTS; | |
8280 return event_names[i].event; | |
8281 } | |
8282 | |
8283 /* | |
8284 * Return the name for event "event". | |
8285 */ | |
8286 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8287 event_nr2name(event_T event) |
7 | 8288 { |
8289 int i; | |
8290 | |
8291 for (i = 0; event_names[i].name != NULL; ++i) | |
8292 if (event_names[i].event == event) | |
8293 return (char_u *)event_names[i].name; | |
8294 return (char_u *)"Unknown"; | |
8295 } | |
8296 | |
8297 /* | |
8298 * Scan over the events. "*" stands for all events. | |
8299 */ | |
8300 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8301 find_end_event( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8302 char_u *arg, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8303 int have_group) /* TRUE when group name was found */ |
7 | 8304 { |
8305 char_u *pat; | |
8306 char_u *p; | |
8307 | |
8308 if (*arg == '*') | |
8309 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8310 if (arg[1] && !VIM_ISWHITE(arg[1])) |
7 | 8311 { |
8312 EMSG2(_("E215: Illegal character after *: %s"), arg); | |
8313 return NULL; | |
8314 } | |
8315 pat = arg + 1; | |
8316 } | |
8317 else | |
8318 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8319 for (pat = arg; *pat && *pat != '|' && !VIM_ISWHITE(*pat); pat = p) |
7 | 8320 { |
8321 if ((int)event_name2nr(pat, &p) >= (int)NUM_EVENTS) | |
8322 { | |
8323 if (have_group) | |
8324 EMSG2(_("E216: No such event: %s"), pat); | |
8325 else | |
8326 EMSG2(_("E216: No such group or event: %s"), pat); | |
8327 return NULL; | |
8328 } | |
8329 } | |
8330 } | |
8331 return pat; | |
8332 } | |
8333 | |
8334 /* | |
8335 * Return TRUE if "event" is included in 'eventignore'. | |
8336 */ | |
8337 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8338 event_ignored(event_T event) |
7 | 8339 { |
8340 char_u *p = p_ei; | |
8341 | |
844 | 8342 while (*p != NUL) |
8343 { | |
8344 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) | |
8345 return TRUE; | |
7 | 8346 if (event_name2nr(p, &p) == event) |
8347 return TRUE; | |
844 | 8348 } |
7 | 8349 |
8350 return FALSE; | |
8351 } | |
8352 | |
8353 /* | |
8354 * Return OK when the contents of p_ei is valid, FAIL otherwise. | |
8355 */ | |
8356 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8357 check_ei(void) |
7 | 8358 { |
8359 char_u *p = p_ei; | |
8360 | |
8361 while (*p) | |
844 | 8362 { |
8363 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) | |
8364 { | |
8365 p += 3; | |
8366 if (*p == ',') | |
8367 ++p; | |
8368 } | |
8369 else if (event_name2nr(p, &p) == NUM_EVENTS) | |
7 | 8370 return FAIL; |
844 | 8371 } |
7 | 8372 |
8373 return OK; | |
8374 } | |
8375 | |
123 | 8376 # if defined(FEAT_SYN_HL) || defined(PROTO) |
8377 | |
8378 /* | |
8379 * Add "what" to 'eventignore' to skip loading syntax highlighting for every | |
8380 * buffer loaded into the window. "what" must start with a comma. | |
8381 * Returns the old value of 'eventignore' in allocated memory. | |
8382 */ | |
8383 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8384 au_event_disable(char *what) |
123 | 8385 { |
8386 char_u *new_ei; | |
8387 char_u *save_ei; | |
8388 | |
8389 save_ei = vim_strsave(p_ei); | |
8390 if (save_ei != NULL) | |
8391 { | |
557 | 8392 new_ei = vim_strnsave(p_ei, (int)(STRLEN(p_ei) + STRLEN(what))); |
123 | 8393 if (new_ei != NULL) |
8394 { | |
2095
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8395 if (*what == ',' && *p_ei == NUL) |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8396 STRCPY(new_ei, what + 1); |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8397 else |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8398 STRCAT(new_ei, what); |
694 | 8399 set_string_option_direct((char_u *)"ei", -1, new_ei, |
8400 OPT_FREE, SID_NONE); | |
123 | 8401 vim_free(new_ei); |
8402 } | |
8403 } | |
8404 return save_ei; | |
8405 } | |
8406 | |
8407 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8408 au_event_restore(char_u *old_ei) |
123 | 8409 { |
8410 if (old_ei != NULL) | |
8411 { | |
694 | 8412 set_string_option_direct((char_u *)"ei", -1, old_ei, |
8413 OPT_FREE, SID_NONE); | |
123 | 8414 vim_free(old_ei); |
8415 } | |
8416 } | |
8417 # endif /* FEAT_SYN_HL */ | |
8418 | |
7 | 8419 /* |
8420 * do_autocmd() -- implements the :autocmd command. Can be used in the | |
8421 * following ways: | |
8422 * | |
8423 * :autocmd <event> <pat> <cmd> Add <cmd> to the list of commands that | |
8424 * will be automatically executed for <event> | |
8425 * when editing a file matching <pat>, in | |
8426 * the current group. | |
14157
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
8427 * :autocmd <event> <pat> Show the autocommands associated with |
7 | 8428 * <event> and <pat>. |
14157
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
8429 * :autocmd <event> Show the autocommands associated with |
7 | 8430 * <event>. |
14157
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
8431 * :autocmd Show all autocommands. |
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
8432 * :autocmd! <event> <pat> <cmd> Remove all autocommands associated with |
7 | 8433 * <event> and <pat>, and add the command |
8434 * <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
|
8435 * :autocmd! <event> <pat> Remove all autocommands associated with |
7 | 8436 * <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
|
8437 * :autocmd! <event> Remove all autocommands associated with |
7 | 8438 * <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
|
8439 * :autocmd! Remove ALL autocommands for the current |
7 | 8440 * group. |
8441 * | |
8442 * Multiple events and patterns may be given separated by commas. Here are | |
8443 * some examples: | |
8444 * :autocmd bufread,bufenter *.c,*.h set tw=0 smartindent noic | |
8445 * :autocmd bufleave * set tw=79 nosmartindent ic infercase | |
8446 * | |
8447 * :autocmd * *.c show all autocommands for *.c files. | |
609 | 8448 * |
8449 * Mostly a {group} argument can optionally appear before <event>. | |
7 | 8450 */ |
8451 void | |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8452 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
|
8453 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8454 char_u *arg = arg_in; |
7 | 8455 char_u *pat; |
8456 char_u *envpat = NULL; | |
8457 char_u *cmd; | |
661 | 8458 event_T event; |
7 | 8459 int need_free = FALSE; |
8460 int nested = FALSE; | |
8461 int group; | |
8462 | |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8463 if (*arg == '|') |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8464 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8465 arg = (char_u *)""; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8466 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
|
8467 } |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8468 else |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8469 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8470 /* |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8471 * 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
|
8472 */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8473 group = au_get_grouparg(&arg); |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8474 if (arg == NULL) /* out of memory */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8475 return; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8476 } |
7 | 8477 |
8478 /* | |
8479 * Scan over the events. | |
8480 * If we find an illegal name, return here, don't do anything. | |
8481 */ | |
8482 pat = find_end_event(arg, group != AUGROUP_ALL); | |
8483 if (pat == NULL) | |
8484 return; | |
8485 | |
8486 pat = skipwhite(pat); | |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8487 if (*pat == '|') |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8488 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8489 pat = (char_u *)""; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8490 cmd = (char_u *)""; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8491 } |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8492 else |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8493 { |
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 * 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
|
8496 */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8497 cmd = pat; |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8498 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
|
8499 cmd++; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8500 if (*cmd) |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8501 *cmd++ = NUL; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8502 |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8503 /* 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
|
8504 * forward slashes here. */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8505 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
|
8506 { |
7 | 8507 #ifdef BACKSLASH_IN_FILENAME |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8508 int p_ssl_save = p_ssl; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8509 |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8510 p_ssl = TRUE; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8511 #endif |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8512 envpat = expand_env_save(pat); |
7 | 8513 #ifdef BACKSLASH_IN_FILENAME |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8514 p_ssl = p_ssl_save; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8515 #endif |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8516 if (envpat != NULL) |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8517 pat = envpat; |
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 |
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 * Check for "nested" flag. |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8522 */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8523 cmd = skipwhite(cmd); |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8524 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
|
8525 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8526 nested = TRUE; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8527 cmd = skipwhite(cmd + 6); |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8528 } |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8529 |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8530 /* |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8531 * Find the start of the commands. |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8532 * Expand <sfile> in it. |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8533 */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8534 if (*cmd != NUL) |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8535 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8536 cmd = expand_sfile(cmd); |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8537 if (cmd == NULL) /* some error */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8538 return; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8539 need_free = TRUE; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8540 } |
7 | 8541 } |
8542 | |
8543 /* | |
8544 * Print header when showing autocommands. | |
8545 */ | |
8546 if (!forceit && *cmd == NUL) | |
8547 { | |
8548 /* Highlight title */ | |
14157
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
8549 MSG_PUTS_TITLE(_("\n--- Autocommands ---")); |
7 | 8550 } |
8551 | |
8552 /* | |
8553 * Loop over the events. | |
8554 */ | |
661 | 8555 last_event = (event_T)-1; /* for listing the event name */ |
7 | 8556 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
|
8557 if (*arg == '*' || *arg == NUL || *arg == '|') |
7 | 8558 { |
661 | 8559 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
8560 event = (event_T)((int)event + 1)) | |
7 | 8561 if (do_autocmd_event(event, pat, |
8562 nested, cmd, forceit, group) == FAIL) | |
8563 break; | |
8564 } | |
8565 else | |
8566 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8567 while (*arg && *arg != '|' && !VIM_ISWHITE(*arg)) |
7 | 8568 if (do_autocmd_event(event_name2nr(arg, &arg), pat, |
8569 nested, cmd, forceit, group) == FAIL) | |
8570 break; | |
8571 } | |
8572 | |
8573 if (need_free) | |
8574 vim_free(cmd); | |
8575 vim_free(envpat); | |
8576 } | |
8577 | |
8578 /* | |
8579 * Find the group ID in a ":autocmd" or ":doautocmd" argument. | |
8580 * The "argp" argument is advanced to the following argument. | |
8581 * | |
8582 * Returns the group ID, AUGROUP_ERROR for error (out of memory). | |
8583 */ | |
8584 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8585 au_get_grouparg(char_u **argp) |
7 | 8586 { |
8587 char_u *group_name; | |
8588 char_u *p; | |
8589 char_u *arg = *argp; | |
8590 int group = AUGROUP_ALL; | |
8591 | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8592 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
|
8593 ; |
7 | 8594 if (p > arg) |
8595 { | |
8596 group_name = vim_strnsave(arg, (int)(p - arg)); | |
8597 if (group_name == NULL) /* out of memory */ | |
8598 return AUGROUP_ERROR; | |
8599 group = au_find_group(group_name); | |
8600 if (group == AUGROUP_ERROR) | |
8601 group = AUGROUP_ALL; /* no match, use all groups */ | |
8602 else | |
8603 *argp = skipwhite(p); /* match, skip over group name */ | |
8604 vim_free(group_name); | |
8605 } | |
8606 return group; | |
8607 } | |
8608 | |
8609 /* | |
8610 * do_autocmd() for one event. | |
8611 * If *pat == NUL do for all patterns. | |
8612 * If *cmd == NUL show entries. | |
8613 * If forceit == TRUE delete entries. | |
8614 * If group is not AUGROUP_ALL, only use this group. | |
8615 */ | |
8616 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8617 do_autocmd_event( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8618 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8619 char_u *pat, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8620 int nested, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8621 char_u *cmd, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8622 int forceit, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8623 int group) |
7 | 8624 { |
8625 AutoPat *ap; | |
8626 AutoPat **prev_ap; | |
8627 AutoCmd *ac; | |
8628 AutoCmd **prev_ac; | |
8629 int brace_level; | |
8630 char_u *endpat; | |
8631 int findgroup; | |
8632 int allgroups; | |
8633 int patlen; | |
856 | 8634 int is_buflocal; |
40 | 8635 int buflocal_nr; |
8636 char_u buflocal_pat[25]; /* for "<buffer=X>" */ | |
7 | 8637 |
8638 if (group == AUGROUP_ALL) | |
8639 findgroup = current_augroup; | |
8640 else | |
8641 findgroup = group; | |
8642 allgroups = (group == AUGROUP_ALL && !forceit && *cmd == NUL); | |
8643 | |
8644 /* | |
8645 * Show or delete all patterns for an event. | |
8646 */ | |
8647 if (*pat == NUL) | |
8648 { | |
8649 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
8650 { | |
8651 if (forceit) /* delete the AutoPat, if it's in the current group */ | |
8652 { | |
8653 if (ap->group == findgroup) | |
8654 au_remove_pat(ap); | |
8655 } | |
8656 else if (group == AUGROUP_ALL || ap->group == group) | |
8657 show_autocmd(ap, event); | |
8658 } | |
8659 } | |
8660 | |
8661 /* | |
8662 * Loop through all the specified patterns. | |
8663 */ | |
8664 for ( ; *pat; pat = (*endpat == ',' ? endpat + 1 : endpat)) | |
8665 { | |
8666 /* | |
8667 * Find end of the pattern. | |
8668 * Watch out for a comma in braces, like "*.\{obj,o\}". | |
8669 */ | |
8670 brace_level = 0; | |
8671 for (endpat = pat; *endpat && (*endpat != ',' || brace_level | |
6963 | 8672 || (endpat > pat && endpat[-1] == '\\')); ++endpat) |
7 | 8673 { |
8674 if (*endpat == '{') | |
8675 brace_level++; | |
8676 else if (*endpat == '}') | |
8677 brace_level--; | |
8678 } | |
8679 if (pat == endpat) /* ignore single comma */ | |
8680 continue; | |
8681 patlen = (int)(endpat - pat); | |
8682 | |
8683 /* | |
40 | 8684 * detect special <buflocal[=X]> buffer-local patterns |
8685 */ | |
8686 is_buflocal = FALSE; | |
8687 buflocal_nr = 0; | |
8688 | |
6623 | 8689 if (patlen >= 8 && STRNCMP(pat, "<buffer", 7) == 0 |
40 | 8690 && pat[patlen - 1] == '>') |
8691 { | |
6623 | 8692 /* "<buffer...>": Error will be printed only for addition. |
8693 * printing and removing will proceed silently. */ | |
40 | 8694 is_buflocal = TRUE; |
8695 if (patlen == 8) | |
6623 | 8696 /* "<buffer>" */ |
40 | 8697 buflocal_nr = curbuf->b_fnum; |
8698 else if (patlen > 9 && pat[7] == '=') | |
8699 { | |
6623 | 8700 if (patlen == 13 && STRNICMP(pat, "<buffer=abuf>", 13) == 0) |
8701 /* "<buffer=abuf>" */ | |
40 | 8702 buflocal_nr = autocmd_bufnr; |
8703 else if (skipdigits(pat + 8) == pat + patlen - 1) | |
6623 | 8704 /* "<buffer=123>" */ |
40 | 8705 buflocal_nr = atoi((char *)pat + 8); |
8706 } | |
8707 } | |
8708 | |
8709 if (is_buflocal) | |
8710 { | |
8711 /* normalize pat into standard "<buffer>#N" form */ | |
8712 sprintf((char *)buflocal_pat, "<buffer=%d>", buflocal_nr); | |
8713 pat = buflocal_pat; /* can modify pat and patlen */ | |
835 | 8714 patlen = (int)STRLEN(buflocal_pat); /* but not endpat */ |
40 | 8715 } |
8716 | |
8717 /* | |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8718 * 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
|
8719 * always goes at or after the last one, so start at the end. |
7 | 8720 */ |
12814
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8721 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
|
8722 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
|
8723 else |
3f2f468b8b9d
patch 8.0.1284: loading file type detection slows down startup
Christian Brabandt <cb@256bit.org>
parents:
12796
diff
changeset
|
8724 prev_ap = &first_autopat[(int)event]; |
7 | 8725 while ((ap = *prev_ap) != NULL) |
8726 { | |
8727 if (ap->pat != NULL) | |
8728 { | |
8729 /* Accept a pattern when: | |
8730 * - a group was specified and it's that group, or a group was | |
8731 * not specified and it's the current group, or a group was | |
8732 * not specified and we are listing | |
8733 * - the length of the pattern matches | |
40 | 8734 * - the pattern matches. |
8735 * For <buffer[=X]>, this condition works because we normalize | |
8736 * all buffer-local patterns. | |
7 | 8737 */ |
8738 if ((allgroups || ap->group == findgroup) | |
8739 && ap->patlen == patlen | |
8740 && STRNCMP(pat, ap->pat, patlen) == 0) | |
8741 { | |
8742 /* | |
8743 * Remove existing autocommands. | |
8744 * If adding any new autocmd's for this AutoPat, don't | |
8745 * delete the pattern from the autopat list, append to | |
8746 * this list. | |
8747 */ | |
8748 if (forceit) | |
8749 { | |
8750 if (*cmd != NUL && ap->next == NULL) | |
8751 { | |
8752 au_remove_cmds(ap); | |
8753 break; | |
8754 } | |
8755 au_remove_pat(ap); | |
8756 } | |
8757 | |
8758 /* | |
40 | 8759 * Show autocmd's for this autopat, or buflocals <buffer=X> |
7 | 8760 */ |
8761 else if (*cmd == NUL) | |
8762 show_autocmd(ap, event); | |
8763 | |
8764 /* | |
8765 * Add autocmd to this autopat, if it's the last one. | |
8766 */ | |
8767 else if (ap->next == NULL) | |
8768 break; | |
8769 } | |
8770 } | |
8771 prev_ap = &ap->next; | |
8772 } | |
8773 | |
8774 /* | |
8775 * Add a new command. | |
8776 */ | |
8777 if (*cmd != NUL) | |
8778 { | |
8779 /* | |
8780 * If the pattern we want to add a command to does appear at the | |
8781 * end of the list (or not is not in the list at all), add the | |
8782 * pattern at the end of the list. | |
8783 */ | |
8784 if (ap == NULL) | |
8785 { | |
40 | 8786 /* refuse to add buffer-local ap if buffer number is invalid */ |
8787 if (is_buflocal && (buflocal_nr == 0 | |
8788 || buflist_findnr(buflocal_nr) == NULL)) | |
8789 { | |
8790 EMSGN(_("E680: <buffer=%d>: invalid buffer number "), | |
8791 buflocal_nr); | |
8792 return FAIL; | |
8793 } | |
8794 | |
7 | 8795 ap = (AutoPat *)alloc((unsigned)sizeof(AutoPat)); |
8796 if (ap == NULL) | |
8797 return FAIL; | |
8798 ap->pat = vim_strnsave(pat, patlen); | |
8799 ap->patlen = patlen; | |
8800 if (ap->pat == NULL) | |
8801 { | |
8802 vim_free(ap); | |
8803 return FAIL; | |
8804 } | |
40 | 8805 |
8806 if (is_buflocal) | |
8807 { | |
8808 ap->buflocal_nr = buflocal_nr; | |
153 | 8809 ap->reg_prog = NULL; |
40 | 8810 } |
8811 else | |
7 | 8812 { |
153 | 8813 char_u *reg_pat; |
8814 | |
40 | 8815 ap->buflocal_nr = 0; |
153 | 8816 reg_pat = file_pat_to_reg_pat(pat, endpat, |
40 | 8817 &ap->allow_dirs, TRUE); |
153 | 8818 if (reg_pat != NULL) |
8819 ap->reg_prog = vim_regcomp(reg_pat, RE_MAGIC); | |
355 | 8820 vim_free(reg_pat); |
153 | 8821 if (reg_pat == NULL || ap->reg_prog == NULL) |
40 | 8822 { |
8823 vim_free(ap->pat); | |
8824 vim_free(ap); | |
8825 return FAIL; | |
8826 } | |
7 | 8827 } |
8828 ap->cmds = NULL; | |
8829 *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
|
8830 last_autopat[(int)event] = ap; |
7 | 8831 ap->next = NULL; |
8832 if (group == AUGROUP_ALL) | |
8833 ap->group = current_augroup; | |
8834 else | |
8835 ap->group = group; | |
8836 } | |
8837 | |
8838 /* | |
8839 * Add the autocmd at the end of the AutoCmd list. | |
8840 */ | |
8841 prev_ac = &(ap->cmds); | |
8842 while ((ac = *prev_ac) != NULL) | |
8843 prev_ac = &ac->next; | |
8844 ac = (AutoCmd *)alloc((unsigned)sizeof(AutoCmd)); | |
8845 if (ac == NULL) | |
8846 return FAIL; | |
8847 ac->cmd = vim_strsave(cmd); | |
8848 #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
|
8849 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
|
8850 ac->script_ctx.sc_lnum += sourcing_lnum; |
7 | 8851 #endif |
8852 if (ac->cmd == NULL) | |
8853 { | |
8854 vim_free(ac); | |
8855 return FAIL; | |
8856 } | |
8857 ac->next = NULL; | |
8858 *prev_ac = ac; | |
8859 ac->nested = nested; | |
8860 } | |
8861 } | |
8862 | |
8863 au_cleanup(); /* may really delete removed patterns/commands now */ | |
8864 return OK; | |
8865 } | |
8866 | |
8867 /* | |
8868 * Implementation of ":doautocmd [group] event [fname]". | |
8869 * Return OK for success, FAIL for failure; | |
8870 */ | |
8871 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8872 do_doautocmd( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8873 char_u *arg, |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8874 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
|
8875 int *did_something) |
7 | 8876 { |
8877 char_u *fname; | |
8878 int nothing_done = TRUE; | |
8879 int group; | |
8880 | |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8881 if (did_something != NULL) |
9291
bff3cd5cf922
commit https://github.com/vim/vim/commit/76ae22fef3cb224ca7fbf97517f881e825d4d0c2
Christian Brabandt <cb@256bit.org>
parents:
9260
diff
changeset
|
8882 *did_something = FALSE; |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8883 |
7 | 8884 /* |
8885 * Check for a legal group name. If not, use AUGROUP_ALL. | |
8886 */ | |
8887 group = au_get_grouparg(&arg); | |
8888 if (arg == NULL) /* out of memory */ | |
8889 return FAIL; | |
8890 | |
8891 if (*arg == '*') | |
8892 { | |
8893 EMSG(_("E217: Can't execute autocommands for ALL events")); | |
8894 return FAIL; | |
8895 } | |
8896 | |
8897 /* | |
8898 * Scan over the events. | |
8899 * If we find an illegal name, return here, don't do anything. | |
8900 */ | |
8901 fname = find_end_event(arg, group != AUGROUP_ALL); | |
8902 if (fname == NULL) | |
8903 return FAIL; | |
8904 | |
8905 fname = skipwhite(fname); | |
8906 | |
8907 /* | |
8908 * Loop over the events. | |
8909 */ | |
11641
dbce7e03bf55
patch 8.0.0703: illegal memory access with empty :doau command
Christian Brabandt <cb@256bit.org>
parents:
11605
diff
changeset
|
8910 while (*arg && !ends_excmd(*arg) && !VIM_ISWHITE(*arg)) |
7 | 8911 if (apply_autocmds_group(event_name2nr(arg, &arg), |
8912 fname, NULL, TRUE, group, curbuf, NULL)) | |
8913 nothing_done = FALSE; | |
8914 | |
8915 if (nothing_done && do_msg) | |
8916 MSG(_("No matching autocommands")); | |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8917 if (did_something != NULL) |
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8918 *did_something = !nothing_done; |
7 | 8919 |
8920 #ifdef FEAT_EVAL | |
8921 return aborting() ? FAIL : OK; | |
8922 #else | |
8923 return OK; | |
8924 #endif | |
8925 } | |
8926 | |
8927 /* | |
8928 * ":doautoall": execute autocommands for each loaded buffer. | |
8929 */ | |
8930 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8931 ex_doautoall(exarg_T *eap) |
7 | 8932 { |
8933 int retval; | |
8934 aco_save_T aco; | |
8935 buf_T *buf; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
8936 bufref_T bufref; |
3342 | 8937 char_u *arg = eap->arg; |
3350 | 8938 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
|
8939 int did_aucmd; |
7 | 8940 |
8941 /* | |
8942 * This is a bit tricky: For some commands curwin->w_buffer needs to be | |
8943 * equal to curbuf, but for some buffers there may not be a window. | |
8944 * So we change the buffer for the current window for a moment. This | |
8945 * gives problems when the autocommands make changes to the list of | |
8946 * buffers or windows... | |
8947 */ | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
8948 FOR_ALL_BUFFERS(buf) |
7 | 8949 { |
1665 | 8950 if (buf->b_ml.ml_mfp != NULL) |
7 | 8951 { |
8952 /* find a window for this buffer and save some values */ | |
8953 aucmd_prepbuf(&aco, buf); | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
8954 set_bufref(&bufref, buf); |
7 | 8955 |
8956 /* 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
|
8957 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
|
8958 |
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8959 if (call_do_modelines && did_aucmd) |
3342 | 8960 { |
8961 /* Execute the modeline settings, but don't set window-local | |
8962 * options if we are using the current window for another | |
8963 * buffer. */ | |
8964 do_modelines(curwin == aucmd_win ? OPT_NOWIN : 0); | |
8965 } | |
7 | 8966 |
8967 /* restore the current window */ | |
8968 aucmd_restbuf(&aco); | |
8969 | |
8970 /* 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
|
8971 if (retval == FAIL || !bufref_valid(&bufref)) |
7 | 8972 break; |
8973 } | |
8974 } | |
8975 | |
8976 check_cursor(); /* just in case lines got deleted */ | |
8977 } | |
8978 | |
8979 /* | |
3350 | 8980 * Check *argp for <nomodeline>. When it is present return FALSE, otherwise |
8981 * return TRUE and advance *argp to after it. | |
8982 * Thus return TRUE when do_modelines() should be called. | |
8983 */ | |
8984 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8985 check_nomodeline(char_u **argp) |
3350 | 8986 { |
8987 if (STRNCMP(*argp, "<nomodeline>", 12) == 0) | |
8988 { | |
8989 *argp = skipwhite(*argp + 12); | |
8990 return FALSE; | |
8991 } | |
8992 return TRUE; | |
8993 } | |
8994 | |
8995 /* | |
7 | 8996 * Prepare for executing autocommands for (hidden) buffer "buf". |
1906 | 8997 * Search for a visible window containing the current buffer. If there isn't |
8998 * one then use "aucmd_win". | |
7 | 8999 * Set "curbuf" and "curwin" to match "buf". |
9000 */ | |
9001 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9002 aucmd_prepbuf( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9003 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
|
9004 buf_T *buf) /* new curbuf */ |
7 | 9005 { |
9006 win_T *win; | |
1906 | 9007 int save_ea; |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
9008 #ifdef FEAT_AUTOCHDIR |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9009 int save_acd; |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
9010 #endif |
7 | 9011 |
9012 /* Find a window that is for the new buffer */ | |
9013 if (buf == curbuf) /* be quick when buf is curbuf */ | |
9014 win = curwin; | |
9015 else | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
9016 FOR_ALL_WINDOWS(win) |
7 | 9017 if (win->w_buffer == buf) |
9018 break; | |
9019 | |
1906 | 9020 /* Allocate "aucmd_win" when needed. If this fails (out of memory) fall |
9021 * back to using the current window. */ | |
9022 if (win == NULL && aucmd_win == NULL) | |
9023 { | |
9024 win_alloc_aucmd_win(); | |
9025 if (aucmd_win == NULL) | |
9026 win = curwin; | |
9027 } | |
1944 | 9028 if (win == NULL && aucmd_win_used) |
9029 /* Strange recursive autocommand, fall back to using the current | |
9030 * window. Expect a few side effects... */ | |
9031 win = curwin; | |
1906 | 9032 |
9033 aco->save_curwin = curwin; | |
9034 aco->save_curbuf = curbuf; | |
7 | 9035 if (win != NULL) |
9036 { | |
1906 | 9037 /* There is a window for "buf" in the current tab page, make it the |
9038 * curwin. This is preferred, it has the least side effects (esp. if | |
9039 * "buf" is curbuf). */ | |
1944 | 9040 aco->use_aucmd_win = FALSE; |
7 | 9041 curwin = win; |
9042 } | |
9043 else | |
9044 { | |
1906 | 9045 /* There is no window for "buf", use "aucmd_win". To minimize the side |
4352 | 9046 * effects, insert it in the current tab page. |
1906 | 9047 * Anything related to a window (e.g., setting folds) may have |
9048 * unexpected results. */ | |
1944 | 9049 aco->use_aucmd_win = TRUE; |
9050 aucmd_win_used = TRUE; | |
1918 | 9051 aucmd_win->w_buffer = buf; |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
9052 #if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) |
2258
772bfca06c18
Fix crash when using ":grep".
Bram Moolenaar <bram@vim.org>
parents:
2244
diff
changeset
|
9053 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
|
9054 #endif |
7 | 9055 ++buf->b_nwindows; |
1918 | 9056 win_init_empty(aucmd_win); /* set cursor and topline to safe values */ |
1944 | 9057 |
9058 /* Make sure w_localdir and globaldir are NULL to avoid a chdir() in | |
9059 * win_enter_ext(). */ | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
9060 VIM_CLEAR(aucmd_win->w_localdir); |
1944 | 9061 aco->globaldir = globaldir; |
9062 globaldir = NULL; | |
9063 | |
7 | 9064 |
1923 | 9065 /* Split the current window, put the aucmd_win in the upper half. |
9066 * We don't want the BufEnter or WinEnter autocommands. */ | |
9067 block_autocmds(); | |
1906 | 9068 make_snapshot(SNAP_AUCMD_IDX); |
9069 save_ea = p_ea; | |
9070 p_ea = FALSE; | |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9071 |
12477
68d7bc045dbe
patch 8.0.1118: FEAT_WINDOWS adds a lot of #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
12216
diff
changeset
|
9072 #ifdef FEAT_AUTOCHDIR |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9073 /* 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
|
9074 save_acd = p_acd; |
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9075 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
|
9076 #endif |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9077 |
1906 | 9078 (void)win_split_ins(0, WSP_TOP, aucmd_win, 0); |
9079 (void)win_comp_pos(); /* recompute window positions */ | |
9080 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
|
9081 #ifdef FEAT_AUTOCHDIR |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9082 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
|
9083 #endif |
1923 | 9084 unblock_autocmds(); |
1918 | 9085 curwin = aucmd_win; |
1906 | 9086 } |
7 | 9087 curbuf = buf; |
1906 | 9088 aco->new_curwin = curwin; |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9089 set_bufref(&aco->new_curbuf, curbuf); |
7 | 9090 } |
9091 | |
9092 /* | |
9093 * Cleanup after executing autocommands for a (hidden) buffer. | |
9094 * Restore the window as it was (if possible). | |
9095 */ | |
9096 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9097 aucmd_restbuf( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9098 aco_save_T *aco) /* structure holding saved values */ |
7 | 9099 { |
1906 | 9100 int dummy; |
9101 | |
1944 | 9102 if (aco->use_aucmd_win) |
1906 | 9103 { |
9104 --curbuf->b_nwindows; | |
9105 /* Find "aucmd_win", it can't be closed, but it may be in another tab | |
1923 | 9106 * page. Do not trigger autocommands here. */ |
9107 block_autocmds(); | |
1906 | 9108 if (curwin != aucmd_win) |
9109 { | |
9110 tabpage_T *tp; | |
9111 win_T *wp; | |
9112 | |
9113 FOR_ALL_TAB_WINDOWS(tp, wp) | |
9114 { | |
9115 if (wp == aucmd_win) | |
9116 { | |
9117 if (tp != curtab) | |
4354 | 9118 goto_tabpage_tp(tp, TRUE, TRUE); |
1906 | 9119 win_goto(aucmd_win); |
3340 | 9120 goto win_found; |
1906 | 9121 } |
9122 } | |
9123 } | |
3340 | 9124 win_found: |
1906 | 9125 |
9126 /* Remove the window and frame from the tree of frames. */ | |
9127 (void)winframe_remove(curwin, &dummy, NULL); | |
9128 win_remove(curwin, NULL); | |
1944 | 9129 aucmd_win_used = FALSE; |
1906 | 9130 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
|
9131 |
e08ead1d269f
patch 8.0.0486: crash and endless loop when closing windows in autocmd
Christian Brabandt <cb@256bit.org>
parents:
11158
diff
changeset
|
9132 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
|
9133 /* 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
|
9134 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
|
9135 |
1906 | 9136 restore_snapshot(SNAP_AUCMD_IDX, FALSE); |
9137 (void)win_comp_pos(); /* recompute window positions */ | |
1923 | 9138 unblock_autocmds(); |
1906 | 9139 |
9140 if (win_valid(aco->save_curwin)) | |
9141 curwin = aco->save_curwin; | |
9142 else | |
9143 /* Hmm, original window disappeared. Just use the first one. */ | |
9144 curwin = firstwin; | |
12477
68d7bc045dbe
patch 8.0.1118: FEAT_WINDOWS adds a lot of #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
12216
diff
changeset
|
9145 #ifdef FEAT_EVAL |
4287 | 9146 vars_clear(&aucmd_win->w_vars->dv_hashtab); /* free all w: variables */ |
9147 hash_init(&aucmd_win->w_vars->dv_hashtab); /* re-use the hashtab */ | |
1906 | 9148 #endif |
9149 curbuf = curwin->w_buffer; | |
9150 | |
1944 | 9151 vim_free(globaldir); |
9152 globaldir = aco->globaldir; | |
9153 | |
1906 | 9154 /* the buffer contents may have changed */ |
9155 check_cursor(); | |
9156 if (curwin->w_topline > curbuf->b_ml.ml_line_count) | |
9157 { | |
9158 curwin->w_topline = curbuf->b_ml.ml_line_count; | |
9159 #ifdef FEAT_DIFF | |
9160 curwin->w_topfill = 0; | |
9161 #endif | |
9162 } | |
9163 #if defined(FEAT_GUI) | |
9164 /* Hide the scrollbars from the aucmd_win and update. */ | |
9165 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_LEFT], FALSE); | |
9166 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_RIGHT], FALSE); | |
9167 gui_may_update_scrollbars(); | |
9168 #endif | |
9169 } | |
9170 else | |
7 | 9171 { |
9172 /* restore curwin */ | |
9173 if (win_valid(aco->save_curwin)) | |
9174 { | |
1906 | 9175 /* Restore the buffer which was previously edited by curwin, if |
1944 | 9176 * it was changed, we are still the same window and the buffer is |
1906 | 9177 * valid. */ |
7 | 9178 if (curwin == aco->new_curwin |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9179 && curbuf != aco->new_curbuf.br_buf |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9180 && bufref_valid(&aco->new_curbuf) |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9181 && aco->new_curbuf.br_buf->b_ml.ml_mfp != NULL) |
7 | 9182 { |
3497 | 9183 # if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) |
9184 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
|
9185 curwin->w_s = &aco->new_curbuf.br_buf->b_s; |
3497 | 9186 # endif |
7 | 9187 --curbuf->b_nwindows; |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9188 curbuf = aco->new_curbuf.br_buf; |
7 | 9189 curwin->w_buffer = curbuf; |
9190 ++curbuf->b_nwindows; | |
9191 } | |
9192 | |
9193 curwin = aco->save_curwin; | |
9194 curbuf = curwin->w_buffer; | |
6201 | 9195 /* In case the autocommand move the cursor to a position that that |
9196 * not exist in curbuf. */ | |
9197 check_cursor(); | |
7 | 9198 } |
9199 } | |
9200 } | |
9201 | |
9202 static int autocmd_nested = FALSE; | |
9203 | |
9204 /* | |
9205 * Execute autocommands for "event" and file name "fname". | |
9206 * Return TRUE if some commands were executed. | |
9207 */ | |
9208 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9209 apply_autocmds( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9210 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9211 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
|
9212 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
|
9213 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
|
9214 buf_T *buf) /* buffer for <abuf> */ |
7 | 9215 { |
9216 return apply_autocmds_group(event, fname, fname_io, force, | |
9217 AUGROUP_ALL, buf, NULL); | |
9218 } | |
9219 | |
9220 /* | |
9221 * Like apply_autocmds(), but with extra "eap" argument. This takes care of | |
9222 * setting v:filearg. | |
9223 */ | |
9224 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9225 apply_autocmds_exarg( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9226 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9227 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9228 char_u *fname_io, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9229 int force, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9230 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9231 exarg_T *eap) |
7 | 9232 { |
9233 return apply_autocmds_group(event, fname, fname_io, force, | |
9234 AUGROUP_ALL, buf, eap); | |
9235 } | |
9236 | |
9237 /* | |
9238 * Like apply_autocmds(), but handles the caller's retval. If the script | |
9239 * processing is being aborted or if retval is FAIL when inside a try | |
9240 * conditional, no autocommands are executed. If otherwise the autocommands | |
9241 * cause the script to be aborted, retval is set to FAIL. | |
9242 */ | |
9243 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9244 apply_autocmds_retval( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9245 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9246 char_u *fname, /* 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
|
9247 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
|
9248 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
|
9249 buf_T *buf, /* buffer for <abuf> */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9250 int *retval) /* pointer to caller's retval */ |
7 | 9251 { |
9252 int did_cmd; | |
9253 | |
532 | 9254 #ifdef FEAT_EVAL |
7 | 9255 if (should_abort(*retval)) |
9256 return FALSE; | |
532 | 9257 #endif |
7 | 9258 |
9259 did_cmd = apply_autocmds_group(event, fname, fname_io, force, | |
9260 AUGROUP_ALL, buf, NULL); | |
532 | 9261 if (did_cmd |
9262 #ifdef FEAT_EVAL | |
9263 && aborting() | |
9264 #endif | |
9265 ) | |
7 | 9266 *retval = FAIL; |
9267 return did_cmd; | |
9268 } | |
9269 | |
609 | 9270 /* |
9271 * Return TRUE when there is a CursorHold autocommand defined. | |
9272 */ | |
7 | 9273 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9274 has_cursorhold(void) |
7 | 9275 { |
661 | 9276 return (first_autopat[(int)(get_real_state() == NORMAL_BUSY |
9277 ? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)] != NULL); | |
7 | 9278 } |
609 | 9279 |
9280 /* | |
9281 * Return TRUE if the CursorHold event can be triggered. | |
9282 */ | |
9283 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9284 trigger_cursorhold(void) |
609 | 9285 { |
661 | 9286 int state; |
9287 | |
2976 | 9288 if (!did_cursorhold |
9289 && 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
|
9290 && reg_recording == 0 |
2976 | 9291 && typebuf.tb_len == 0 |
978 | 9292 #ifdef FEAT_INS_EXPAND |
9293 && !ins_compl_active() | |
9294 #endif | |
9295 ) | |
661 | 9296 { |
9297 state = get_real_state(); | |
9298 if (state == NORMAL_BUSY || (state & INSERT) != 0) | |
9299 return TRUE; | |
9300 } | |
9301 return FALSE; | |
609 | 9302 } |
661 | 9303 |
9304 /* | |
9305 * Return TRUE when there is a CursorMoved autocommand defined. | |
9306 */ | |
9307 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9308 has_cursormoved(void) |
661 | 9309 { |
9310 return (first_autopat[(int)EVENT_CURSORMOVED] != NULL); | |
9311 } | |
9312 | |
9313 /* | |
9314 * Return TRUE when there is a CursorMovedI autocommand defined. | |
9315 */ | |
9316 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9317 has_cursormovedI(void) |
661 | 9318 { |
9319 return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL); | |
9320 } | |
7 | 9321 |
3390 | 9322 /* |
4232 | 9323 * Return TRUE when there is a TextChanged autocommand defined. |
9324 */ | |
9325 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9326 has_textchanged(void) |
4232 | 9327 { |
9328 return (first_autopat[(int)EVENT_TEXTCHANGED] != NULL); | |
9329 } | |
9330 | |
9331 /* | |
9332 * Return TRUE when there is a TextChangedI autocommand defined. | |
9333 */ | |
9334 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9335 has_textchangedI(void) |
4232 | 9336 { |
9337 return (first_autopat[(int)EVENT_TEXTCHANGEDI] != NULL); | |
9338 } | |
9339 | |
9340 /* | |
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
|
9341 * 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
|
9342 */ |
5ed6e4a25925
patch 8.0.1494: no autocmd triggered in Insert mode with visible popup menu
Christian Brabandt <cb@256bit.org>
parents:
13170
diff
changeset
|
9343 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
|
9344 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
|
9345 { |
5ed6e4a25925
patch 8.0.1494: no autocmd triggered in Insert mode with visible popup menu
Christian Brabandt <cb@256bit.org>
parents:
13170
diff
changeset
|
9346 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
|
9347 } |
5ed6e4a25925
patch 8.0.1494: no autocmd triggered in Insert mode with visible popup menu
Christian Brabandt <cb@256bit.org>
parents:
13170
diff
changeset
|
9348 |
5ed6e4a25925
patch 8.0.1494: no autocmd triggered in Insert mode with visible popup menu
Christian Brabandt <cb@256bit.org>
parents:
13170
diff
changeset
|
9349 /* |
3390 | 9350 * Return TRUE when there is an InsertCharPre autocommand defined. |
9351 */ | |
9352 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9353 has_insertcharpre(void) |
3390 | 9354 { |
9355 return (first_autopat[(int)EVENT_INSERTCHARPRE] != NULL); | |
9356 } | |
9357 | |
6154 | 9358 /* |
9359 * Return TRUE when there is an CmdUndefined autocommand defined. | |
9360 */ | |
9361 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9362 has_cmdundefined(void) |
6154 | 9363 { |
9364 return (first_autopat[(int)EVENT_CMDUNDEFINED] != NULL); | |
9365 } | |
9366 | |
9367 /* | |
9368 * Return TRUE when there is an FuncUndefined autocommand defined. | |
9369 */ | |
9370 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9371 has_funcundefined(void) |
6154 | 9372 { |
9373 return (first_autopat[(int)EVENT_FUNCUNDEFINED] != NULL); | |
9374 } | |
9375 | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9376 /* |
13037
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9377 * 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
|
9378 */ |
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9379 int |
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9380 has_textyankpost(void) |
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9381 { |
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9382 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
|
9383 } |
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9384 |
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
9385 /* |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9386 * 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
|
9387 * 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
|
9388 */ |
7 | 9389 static int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9390 apply_autocmds_group( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9391 event_T event, |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
9392 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
|
9393 char_u *fname_io, /* fname to use for <afile> on cmdline, NULL means |
7 | 9394 use fname */ |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
9395 int force, /* when TRUE, ignore autocmd_busy */ |
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
9396 int group, /* group ID, or AUGROUP_ALL */ |
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
9397 buf_T *buf, /* buffer for <abuf> */ |
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
9398 exarg_T *eap UNUSED) /* command arguments */ |
7 | 9399 { |
9400 char_u *sfname = NULL; /* short file name */ | |
9401 char_u *tail; | |
9402 int save_changed; | |
9403 buf_T *old_curbuf; | |
9404 int retval = FALSE; | |
9405 char_u *save_sourcing_name; | |
9406 linenr_T save_sourcing_lnum; | |
9407 char_u *save_autocmd_fname; | |
1723 | 9408 int save_autocmd_fname_full; |
7 | 9409 int save_autocmd_bufnr; |
9410 char_u *save_autocmd_match; | |
9411 int save_autocmd_busy; | |
9412 int save_autocmd_nested; | |
9413 static int nesting = 0; | |
9414 AutoPatCmd patcmd; | |
9415 AutoPat *ap; | |
9416 #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
|
9417 sctx_T save_current_sctx; |
7 | 9418 void *save_funccalp; |
9419 char_u *save_cmdarg; | |
9420 long save_cmdbang; | |
9421 #endif | |
9422 static int filechangeshell_busy = FALSE; | |
170 | 9423 #ifdef FEAT_PROFILE |
9424 proftime_T wait_time; | |
9425 #endif | |
6608 | 9426 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
|
9427 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
|
9428 int save_KeyTyped = KeyTyped; |
7 | 9429 |
9430 /* | |
9431 * Quickly return if there are no autocommands for this event or | |
9432 * autocommands are blocked. | |
9433 */ | |
11641
dbce7e03bf55
patch 8.0.0703: illegal memory access with empty :doau command
Christian Brabandt <cb@256bit.org>
parents:
11605
diff
changeset
|
9434 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
|
9435 || autocmd_blocked > 0) |
40 | 9436 goto BYPASS_AU; |
7 | 9437 |
9438 /* | |
9439 * When autocommands are busy, new autocommands are only executed when | |
9440 * explicitly enabled with the "nested" flag. | |
9441 */ | |
9442 if (autocmd_busy && !(force || autocmd_nested)) | |
40 | 9443 goto BYPASS_AU; |
7 | 9444 |
9445 #ifdef FEAT_EVAL | |
9446 /* | |
1201 | 9447 * Quickly return when immediately aborting on error, or when an interrupt |
7 | 9448 * occurred or an exception was thrown but not caught. |
9449 */ | |
9450 if (aborting()) | |
40 | 9451 goto BYPASS_AU; |
7 | 9452 #endif |
9453 | |
9454 /* | |
9455 * FileChangedShell never nests, because it can create an endless loop. | |
9456 */ | |
765 | 9457 if (filechangeshell_busy && (event == EVENT_FILECHANGEDSHELL |
9458 || event == EVENT_FILECHANGEDSHELLPOST)) | |
40 | 9459 goto BYPASS_AU; |
7 | 9460 |
9461 /* | |
9462 * Ignore events in 'eventignore'. | |
9463 */ | |
9464 if (event_ignored(event)) | |
40 | 9465 goto BYPASS_AU; |
7 | 9466 |
9467 /* | |
9468 * Allow nesting of autocommands, but restrict the depth, because it's | |
9469 * possible to create an endless loop. | |
9470 */ | |
9471 if (nesting == 10) | |
9472 { | |
9473 EMSG(_("E218: autocommand nesting too deep")); | |
40 | 9474 goto BYPASS_AU; |
7 | 9475 } |
9476 | |
9477 /* | |
9478 * Check if these autocommands are disabled. Used when doing ":all" or | |
9479 * ":ball". | |
9480 */ | |
9481 if ( (autocmd_no_enter | |
9482 && (event == EVENT_WINENTER || event == EVENT_BUFENTER)) | |
9483 || (autocmd_no_leave | |
9484 && (event == EVENT_WINLEAVE || event == EVENT_BUFLEAVE))) | |
40 | 9485 goto BYPASS_AU; |
7 | 9486 |
9487 /* | |
9488 * Save the autocmd_* variables and info about the current buffer. | |
9489 */ | |
9490 save_autocmd_fname = autocmd_fname; | |
1723 | 9491 save_autocmd_fname_full = autocmd_fname_full; |
7 | 9492 save_autocmd_bufnr = autocmd_bufnr; |
9493 save_autocmd_match = autocmd_match; | |
9494 save_autocmd_busy = autocmd_busy; | |
9495 save_autocmd_nested = autocmd_nested; | |
9496 save_changed = curbuf->b_changed; | |
9497 old_curbuf = curbuf; | |
9498 | |
9499 /* | |
9500 * Set the file name to be used for <afile>. | |
1471 | 9501 * Make a copy to avoid that changing a buffer name or directory makes it |
9502 * invalid. | |
7 | 9503 */ |
9504 if (fname_io == NULL) | |
9505 { | |
13810
cad480bac9e1
patch 8.0.1777: cannot cleanup before loading another colorscheme
Christian Brabandt <cb@256bit.org>
parents:
13804
diff
changeset
|
9506 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
|
9507 || event == EVENT_OPTIONSET) |
5521 | 9508 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
|
9509 else if (fname != NULL && !ends_excmd(*fname)) |
7 | 9510 autocmd_fname = fname; |
9511 else if (buf != NULL) | |
1723 | 9512 autocmd_fname = buf->b_ffname; |
7 | 9513 else |
9514 autocmd_fname = NULL; | |
9515 } | |
9516 else | |
9517 autocmd_fname = fname_io; | |
1471 | 9518 if (autocmd_fname != NULL) |
1723 | 9519 autocmd_fname = vim_strsave(autocmd_fname); |
9520 autocmd_fname_full = FALSE; /* call FullName_save() later */ | |
7 | 9521 |
9522 /* | |
9523 * Set the buffer number to be used for <abuf>. | |
9524 */ | |
9525 if (buf == NULL) | |
9526 autocmd_bufnr = 0; | |
9527 else | |
9528 autocmd_bufnr = buf->b_fnum; | |
9529 | |
9530 /* | |
9531 * When the file name is NULL or empty, use the file name of buffer "buf". | |
9532 * Always use the full path of the file name to match with, in case | |
9533 * "allow_dirs" is set. | |
9534 */ | |
9535 if (fname == NULL || *fname == NUL) | |
9536 { | |
9537 if (buf == NULL) | |
9538 fname = NULL; | |
9539 else | |
9540 { | |
9541 #ifdef FEAT_SYN_HL | |
9542 if (event == EVENT_SYNTAX) | |
9543 fname = buf->b_p_syn; | |
9544 else | |
9545 #endif | |
9546 if (event == EVENT_FILETYPE) | |
9547 fname = buf->b_p_ft; | |
9548 else | |
9549 { | |
9550 if (buf->b_sfname != NULL) | |
9551 sfname = vim_strsave(buf->b_sfname); | |
9552 fname = buf->b_ffname; | |
9553 } | |
9554 } | |
9555 if (fname == NULL) | |
9556 fname = (char_u *)""; | |
9557 fname = vim_strsave(fname); /* make a copy, so we can change it */ | |
9558 } | |
9559 else | |
9560 { | |
9561 sfname = vim_strsave(fname); | |
5521 | 9562 /* 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
|
9563 * ColorScheme, QuickFixCmd* or DirChanged */ |
161 | 9564 if (event == EVENT_FILETYPE |
9565 || 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
|
9566 || event == EVENT_CMDLINECHANGED |
8a3e9d1e8cf9
patch 8.0.1748: CmdlineEnter command uses backslash instead of slash
Christian Brabandt <cb@256bit.org>
parents:
13730
diff
changeset
|
9567 || event == EVENT_CMDLINEENTER |
8a3e9d1e8cf9
patch 8.0.1748: CmdlineEnter command uses backslash instead of slash
Christian Brabandt <cb@256bit.org>
parents:
13730
diff
changeset
|
9568 || event == EVENT_CMDLINELEAVE |
8a3e9d1e8cf9
patch 8.0.1748: CmdlineEnter command uses backslash instead of slash
Christian Brabandt <cb@256bit.org>
parents:
13730
diff
changeset
|
9569 || event == EVENT_CMDWINENTER |
8a3e9d1e8cf9
patch 8.0.1748: CmdlineEnter command uses backslash instead of slash
Christian Brabandt <cb@256bit.org>
parents:
13730
diff
changeset
|
9570 || event == EVENT_CMDWINLEAVE |
8a3e9d1e8cf9
patch 8.0.1748: CmdlineEnter command uses backslash instead of slash
Christian Brabandt <cb@256bit.org>
parents:
13730
diff
changeset
|
9571 || event == EVENT_CMDUNDEFINED |
1867 | 9572 || event == EVENT_FUNCUNDEFINED |
161 | 9573 || event == EVENT_REMOTEREPLY |
649 | 9574 || event == EVENT_SPELLFILEMISSING |
161 | 9575 || event == EVENT_QUICKFIXCMDPRE |
5521 | 9576 || event == EVENT_COLORSCHEME |
13810
cad480bac9e1
patch 8.0.1777: cannot cleanup before loading another colorscheme
Christian Brabandt <cb@256bit.org>
parents:
13804
diff
changeset
|
9577 || event == EVENT_COLORSCHEMEPRE |
6935 | 9578 || event == EVENT_OPTIONSET |
13170
6559e98f3e74
patch 8.0.1459: cannot handle change of directory
Christian Brabandt <cb@256bit.org>
parents:
13142
diff
changeset
|
9579 || event == EVENT_QUICKFIXCMDPOST |
6559e98f3e74
patch 8.0.1459: cannot handle change of directory
Christian Brabandt <cb@256bit.org>
parents:
13142
diff
changeset
|
9580 || 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
|
9581 { |
7 | 9582 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
|
9583 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
|
9584 } |
7 | 9585 else |
9586 fname = FullName_save(fname, FALSE); | |
9587 } | |
9588 if (fname == NULL) /* out of memory */ | |
9589 { | |
9590 vim_free(sfname); | |
40 | 9591 retval = FALSE; |
9592 goto BYPASS_AU; | |
7 | 9593 } |
9594 | |
9595 #ifdef BACKSLASH_IN_FILENAME | |
9596 /* | |
9597 * Replace all backslashes with forward slashes. This makes the | |
9598 * autocommand patterns portable between Unix and MS-DOS. | |
9599 */ | |
9600 if (sfname != NULL) | |
9601 forward_slash(sfname); | |
9602 forward_slash(fname); | |
9603 #endif | |
9604 | |
9605 #ifdef VMS | |
9606 /* remove version for correct match */ | |
9607 if (sfname != NULL) | |
9608 vms_remove_version(sfname); | |
9609 vms_remove_version(fname); | |
9610 #endif | |
9611 | |
9612 /* | |
9613 * Set the name to be used for <amatch>. | |
9614 */ | |
9615 autocmd_match = fname; | |
9616 | |
9617 | |
14157
3c05b52ffad8
patch 8.1.0096: inconsistent use of the word autocommands
Christian Brabandt <cb@256bit.org>
parents:
14004
diff
changeset
|
9618 /* Don't redraw while doing autocommands. */ |
7 | 9619 ++RedrawingDisabled; |
9620 save_sourcing_name = sourcing_name; | |
9621 sourcing_name = NULL; /* don't free this one */ | |
9622 save_sourcing_lnum = sourcing_lnum; | |
9623 sourcing_lnum = 0; /* no line number here */ | |
9624 | |
9625 #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
|
9626 save_current_sctx = current_sctx; |
7 | 9627 |
170 | 9628 # ifdef FEAT_PROFILE |
788 | 9629 if (do_profiling == PROF_YES) |
170 | 9630 prof_child_enter(&wait_time); /* doesn't count for the caller itself */ |
9631 # endif | |
9632 | |
7 | 9633 /* Don't use local function variables, if called from a function */ |
9634 save_funccalp = save_funccal(); | |
9635 #endif | |
9636 | |
9637 /* | |
9638 * When starting to execute autocommands, save the search patterns. | |
9639 */ | |
9640 if (!autocmd_busy) | |
9641 { | |
9642 save_search_patterns(); | |
7285
a34232b17763
commit https://github.com/vim/vim/commit/20ad69ccfb60ef718bd26387ef0e5424461a643e
Christian Brabandt <cb@256bit.org>
parents:
7170
diff
changeset
|
9643 #ifdef FEAT_INS_EXPAND |
6608 | 9644 if (!ins_compl_active()) |
7285
a34232b17763
commit https://github.com/vim/vim/commit/20ad69ccfb60ef718bd26387ef0e5424461a643e
Christian Brabandt <cb@256bit.org>
parents:
7170
diff
changeset
|
9645 #endif |
6608 | 9646 { |
11325
77f3b7316d8b
patch 8.0.0548: saving the redo buffer only works one time
Christian Brabandt <cb@256bit.org>
parents:
11199
diff
changeset
|
9647 saveRedobuff(&save_redo); |
6608 | 9648 did_save_redobuff = TRUE; |
9649 } | |
7 | 9650 did_filetype = keep_filetype; |
9651 } | |
9652 | |
9653 /* | |
9654 * Note that we are applying autocmds. Some commands need to know. | |
9655 */ | |
9656 autocmd_busy = TRUE; | |
9657 filechangeshell_busy = (event == EVENT_FILECHANGEDSHELL); | |
9658 ++nesting; /* see matching decrement below */ | |
9659 | |
9660 /* Remember that FileType was triggered. Used for did_filetype(). */ | |
9661 if (event == EVENT_FILETYPE) | |
9662 did_filetype = TRUE; | |
9663 | |
9664 tail = gettail(fname); | |
9665 | |
9666 /* Find first autocommand that matches */ | |
9667 patcmd.curpat = first_autopat[(int)event]; | |
9668 patcmd.nextcmd = NULL; | |
9669 patcmd.group = group; | |
9670 patcmd.fname = fname; | |
9671 patcmd.sfname = sfname; | |
9672 patcmd.tail = tail; | |
9673 patcmd.event = event; | |
40 | 9674 patcmd.arg_bufnr = autocmd_bufnr; |
9675 patcmd.next = NULL; | |
7 | 9676 auto_next_pat(&patcmd, FALSE); |
9677 | |
9678 /* found one, start executing the autocommands */ | |
9679 if (patcmd.curpat != NULL) | |
9680 { | |
40 | 9681 /* add to active_apc_list */ |
9682 patcmd.next = active_apc_list; | |
9683 active_apc_list = &patcmd; | |
9684 | |
7 | 9685 #ifdef FEAT_EVAL |
9686 /* 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
|
9687 save_cmdbang = (long)get_vim_var_nr(VV_CMDBANG); |
7 | 9688 if (eap != NULL) |
9689 { | |
9690 save_cmdarg = set_cmdarg(eap, NULL); | |
9691 set_vim_var_nr(VV_CMDBANG, (long)eap->forceit); | |
9692 } | |
9693 else | |
9694 save_cmdarg = NULL; /* avoid gcc warning */ | |
9695 #endif | |
9696 retval = TRUE; | |
9697 /* mark the last pattern, to avoid an endless loop when more patterns | |
9698 * are added when executing autocommands */ | |
9699 for (ap = patcmd.curpat; ap->next != NULL; ap = ap->next) | |
9700 ap->last = FALSE; | |
9701 ap->last = TRUE; | |
9702 check_lnums(TRUE); /* make sure cursor and topline are valid */ | |
9703 do_cmdline(NULL, getnextac, (void *)&patcmd, | |
9704 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT); | |
9705 #ifdef FEAT_EVAL | |
9706 if (eap != NULL) | |
9707 { | |
9708 (void)set_cmdarg(NULL, save_cmdarg); | |
9709 set_vim_var_nr(VV_CMDBANG, save_cmdbang); | |
9710 } | |
9711 #endif | |
40 | 9712 /* delete from active_apc_list */ |
9713 if (active_apc_list == &patcmd) /* just in case */ | |
9714 active_apc_list = patcmd.next; | |
7 | 9715 } |
9716 | |
9717 --RedrawingDisabled; | |
9718 autocmd_busy = save_autocmd_busy; | |
9719 filechangeshell_busy = FALSE; | |
9720 autocmd_nested = save_autocmd_nested; | |
9721 vim_free(sourcing_name); | |
9722 sourcing_name = save_sourcing_name; | |
9723 sourcing_lnum = save_sourcing_lnum; | |
1471 | 9724 vim_free(autocmd_fname); |
7 | 9725 autocmd_fname = save_autocmd_fname; |
1723 | 9726 autocmd_fname_full = save_autocmd_fname_full; |
7 | 9727 autocmd_bufnr = save_autocmd_bufnr; |
9728 autocmd_match = save_autocmd_match; | |
9729 #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
|
9730 current_sctx = save_current_sctx; |
7 | 9731 restore_funccal(save_funccalp); |
170 | 9732 # ifdef FEAT_PROFILE |
788 | 9733 if (do_profiling == PROF_YES) |
170 | 9734 prof_child_exit(&wait_time); |
9735 # endif | |
7 | 9736 #endif |
12796
9d51b8c6b84e
patch 8.0.1275: CmdlineLeave autocmd prevents fold from opening
Christian Brabandt <cb@256bit.org>
parents:
12771
diff
changeset
|
9737 KeyTyped = save_KeyTyped; |
7 | 9738 vim_free(fname); |
9739 vim_free(sfname); | |
9740 --nesting; /* see matching increment above */ | |
9741 | |
9742 /* | |
9743 * When stopping to execute autocommands, restore the search patterns and | |
5958 | 9744 * the redo buffer. Free any buffers in the au_pending_free_buf list and |
9745 * free any windows in the au_pending_free_win list. | |
7 | 9746 */ |
9747 if (!autocmd_busy) | |
9748 { | |
9749 restore_search_patterns(); | |
6608 | 9750 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
|
9751 restoreRedobuff(&save_redo); |
7 | 9752 did_filetype = FALSE; |
5816 | 9753 while (au_pending_free_buf != NULL) |
9754 { | |
9755 buf_T *b = au_pending_free_buf->b_next; | |
9756 vim_free(au_pending_free_buf); | |
9757 au_pending_free_buf = b; | |
9758 } | |
5958 | 9759 while (au_pending_free_win != NULL) |
9760 { | |
9761 win_T *w = au_pending_free_win->w_next; | |
9762 vim_free(au_pending_free_win); | |
9763 au_pending_free_win = w; | |
9764 } | |
7 | 9765 } |
9766 | |
9767 /* | |
9768 * Some events don't set or reset the Changed flag. | |
9769 * Check if still in the same buffer! | |
9770 */ | |
9771 if (curbuf == old_curbuf | |
9772 && (event == EVENT_BUFREADPOST | |
9773 || event == EVENT_BUFWRITEPOST | |
9774 || event == EVENT_FILEAPPENDPOST | |
9775 || event == EVENT_VIMLEAVE | |
9776 || event == EVENT_VIMLEAVEPRE)) | |
9777 { | |
9778 #ifdef FEAT_TITLE | |
9779 if (curbuf->b_changed != save_changed) | |
9780 need_maketitle = TRUE; | |
9781 #endif | |
9782 curbuf->b_changed = save_changed; | |
9783 } | |
9784 | |
9785 au_cleanup(); /* may really delete removed patterns/commands now */ | |
40 | 9786 |
9787 BYPASS_AU: | |
9788 /* When wiping out a buffer make sure all its buffer-local autocommands | |
9789 * are deleted. */ | |
9790 if (event == EVENT_BUFWIPEOUT && buf != NULL) | |
9791 aubuflocal_remove(buf); | |
9792 | |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
9793 if (retval == OK && event == EVENT_FILETYPE) |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
9794 au_did_filetype = TRUE; |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
9795 |
7 | 9796 return retval; |
9797 } | |
9798 | |
1410 | 9799 # ifdef FEAT_EVAL |
9800 static char_u *old_termresponse = NULL; | |
9801 # endif | |
9802 | |
9803 /* | |
9804 * Block triggering autocommands until unblock_autocmd() is called. | |
9805 * Can be used recursively, so long as it's symmetric. | |
9806 */ | |
9807 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9808 block_autocmds(void) |
1410 | 9809 { |
9810 # ifdef FEAT_EVAL | |
9811 /* Remember the value of v:termresponse. */ | |
9812 if (autocmd_blocked == 0) | |
9813 old_termresponse = get_vim_var_str(VV_TERMRESPONSE); | |
9814 # endif | |
9815 ++autocmd_blocked; | |
9816 } | |
9817 | |
9818 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9819 unblock_autocmds(void) |
1410 | 9820 { |
9821 --autocmd_blocked; | |
9822 | |
9823 # ifdef FEAT_EVAL | |
9824 /* When v:termresponse was set while autocommands were blocked, trigger | |
9825 * the autocommands now. Esp. useful when executing a shell command | |
9826 * during startup (vimdiff). */ | |
9827 if (autocmd_blocked == 0 | |
9828 && get_vim_var_str(VV_TERMRESPONSE) != old_termresponse) | |
9829 apply_autocmds(EVENT_TERMRESPONSE, NULL, NULL, FALSE, curbuf); | |
9830 # endif | |
9831 } | |
9832 | |
5008
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9833 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9834 is_autocmd_blocked(void) |
5008
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9835 { |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9836 return autocmd_blocked != 0; |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9837 } |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9838 |
7 | 9839 /* |
9840 * Find next autocommand pattern that matches. | |
9841 */ | |
9842 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9843 auto_next_pat( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9844 AutoPatCmd *apc, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9845 int stop_at_last) /* stop when 'last' flag is set */ |
7 | 9846 { |
9847 AutoPat *ap; | |
9848 AutoCmd *cp; | |
9849 char_u *name; | |
9850 char *s; | |
9851 | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
9852 VIM_CLEAR(sourcing_name); |
7 | 9853 |
9854 for (ap = apc->curpat; ap != NULL && !got_int; ap = ap->next) | |
9855 { | |
9856 apc->curpat = NULL; | |
9857 | |
1723 | 9858 /* Only use a pattern when it has not been removed, has commands and |
40 | 9859 * the group matches. For buffer-local autocommands only check the |
9860 * buffer number. */ | |
7 | 9861 if (ap->pat != NULL && ap->cmds != NULL |
9862 && (apc->group == AUGROUP_ALL || apc->group == ap->group)) | |
9863 { | |
40 | 9864 /* execution-condition */ |
9865 if (ap->buflocal_nr == 0 | |
6375 | 9866 ? (match_file_pat(NULL, &ap->reg_prog, apc->fname, |
153 | 9867 apc->sfname, apc->tail, ap->allow_dirs)) |
40 | 9868 : ap->buflocal_nr == apc->arg_bufnr) |
7 | 9869 { |
9870 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
|
9871 s = _("%s Autocommands for \"%s\""); |
7 | 9872 sourcing_name = alloc((unsigned)(STRLEN(s) |
9873 + STRLEN(name) + ap->patlen + 1)); | |
9874 if (sourcing_name != NULL) | |
9875 { | |
9876 sprintf((char *)sourcing_name, s, | |
9877 (char *)name, (char *)ap->pat); | |
9878 if (p_verbose >= 8) | |
292 | 9879 { |
9880 verbose_enter(); | |
273 | 9881 smsg((char_u *)_("Executing %s"), sourcing_name); |
292 | 9882 verbose_leave(); |
9883 } | |
7 | 9884 } |
9885 | |
9886 apc->curpat = ap; | |
9887 apc->nextcmd = ap->cmds; | |
9888 /* mark last command */ | |
9889 for (cp = ap->cmds; cp->next != NULL; cp = cp->next) | |
9890 cp->last = FALSE; | |
9891 cp->last = TRUE; | |
9892 } | |
9893 line_breakcheck(); | |
9894 if (apc->curpat != NULL) /* found a match */ | |
9895 break; | |
9896 } | |
9897 if (stop_at_last && ap->last) | |
9898 break; | |
9899 } | |
9900 } | |
9901 | |
9902 /* | |
9903 * Get next autocommand command. | |
9904 * Called by do_cmdline() to get the next line for ":if". | |
9905 * Returns allocated string, or NULL for end of autocommands. | |
9906 */ | |
3997 | 9907 char_u * |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9908 getnextac(int c UNUSED, void *cookie, int indent UNUSED) |
7 | 9909 { |
9910 AutoPatCmd *acp = (AutoPatCmd *)cookie; | |
9911 char_u *retval; | |
9912 AutoCmd *ac; | |
9913 | |
9914 /* Can be called again after returning the last line. */ | |
9915 if (acp->curpat == NULL) | |
9916 return NULL; | |
9917 | |
9918 /* repeat until we find an autocommand to execute */ | |
9919 for (;;) | |
9920 { | |
9921 /* skip removed commands */ | |
9922 while (acp->nextcmd != NULL && acp->nextcmd->cmd == NULL) | |
9923 if (acp->nextcmd->last) | |
9924 acp->nextcmd = NULL; | |
9925 else | |
9926 acp->nextcmd = acp->nextcmd->next; | |
9927 | |
9928 if (acp->nextcmd != NULL) | |
9929 break; | |
9930 | |
9931 /* at end of commands, find next pattern that matches */ | |
9932 if (acp->curpat->last) | |
9933 acp->curpat = NULL; | |
9934 else | |
9935 acp->curpat = acp->curpat->next; | |
9936 if (acp->curpat != NULL) | |
9937 auto_next_pat(acp, TRUE); | |
9938 if (acp->curpat == NULL) | |
9939 return NULL; | |
9940 } | |
9941 | |
9942 ac = acp->nextcmd; | |
9943 | |
9944 if (p_verbose >= 9) | |
9945 { | |
292 | 9946 verbose_enter_scroll(); |
273 | 9947 smsg((char_u *)_("autocommand %s"), ac->cmd); |
7 | 9948 msg_puts((char_u *)"\n"); /* don't overwrite this either */ |
292 | 9949 verbose_leave_scroll(); |
7 | 9950 } |
9951 retval = vim_strsave(ac->cmd); | |
9952 autocmd_nested = ac->nested; | |
9953 #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
|
9954 current_sctx = ac->script_ctx; |
7 | 9955 #endif |
9956 if (ac->last) | |
9957 acp->nextcmd = NULL; | |
9958 else | |
9959 acp->nextcmd = ac->next; | |
9960 return retval; | |
9961 } | |
9962 | |
9963 /* | |
9964 * Return TRUE if there is a matching autocommand for "fname". | |
40 | 9965 * To account for buffer-local autocommands, function needs to know |
9966 * in which buffer the file will be opened. | |
7 | 9967 */ |
9968 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9969 has_autocmd(event_T event, char_u *sfname, buf_T *buf) |
7 | 9970 { |
9971 AutoPat *ap; | |
9972 char_u *fname; | |
9973 char_u *tail = gettail(sfname); | |
9974 int retval = FALSE; | |
9975 | |
9976 fname = FullName_save(sfname, FALSE); | |
9977 if (fname == NULL) | |
9978 return FALSE; | |
9979 | |
9980 #ifdef BACKSLASH_IN_FILENAME | |
9981 /* | |
9982 * Replace all backslashes with forward slashes. This makes the | |
9983 * autocommand patterns portable between Unix and MS-DOS. | |
9984 */ | |
9985 sfname = vim_strsave(sfname); | |
9986 if (sfname != NULL) | |
9987 forward_slash(sfname); | |
9988 forward_slash(fname); | |
9989 #endif | |
9990 | |
9991 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
9992 if (ap->pat != NULL && ap->cmds != NULL | |
856 | 9993 && (ap->buflocal_nr == 0 |
6375 | 9994 ? match_file_pat(NULL, &ap->reg_prog, |
153 | 9995 fname, sfname, tail, ap->allow_dirs) |
856 | 9996 : buf != NULL && ap->buflocal_nr == buf->b_fnum |
40 | 9997 )) |
7 | 9998 { |
9999 retval = TRUE; | |
10000 break; | |
10001 } | |
10002 | |
10003 vim_free(fname); | |
10004 #ifdef BACKSLASH_IN_FILENAME | |
10005 vim_free(sfname); | |
10006 #endif | |
10007 | |
10008 return retval; | |
10009 } | |
10010 | |
10011 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
10012 /* | |
10013 * Function given to ExpandGeneric() to obtain the list of autocommand group | |
10014 * names. | |
10015 */ | |
10016 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10017 get_augroup_name(expand_T *xp UNUSED, int idx) |
7 | 10018 { |
10019 if (idx == augroups.ga_len) /* add "END" add the end */ | |
10020 return (char_u *)"END"; | |
10021 if (idx >= augroups.ga_len) /* end of list */ | |
10022 return NULL; | |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
10023 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
|
10024 /* skip deleted entries */ |
7 | 10025 return (char_u *)""; |
10026 return AUGROUP_NAME(idx); /* return a name */ | |
10027 } | |
10028 | |
10029 static int include_groups = FALSE; | |
10030 | |
10031 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10032 set_context_in_autocmd( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10033 expand_T *xp, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10034 char_u *arg, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10035 int doautocmd) /* TRUE for :doauto*, FALSE for :autocmd */ |
7 | 10036 { |
10037 char_u *p; | |
10038 int group; | |
10039 | |
10040 /* check for a group name, skip it if present */ | |
10041 include_groups = FALSE; | |
10042 p = arg; | |
10043 group = au_get_grouparg(&arg); | |
10044 if (group == AUGROUP_ERROR) | |
10045 return NULL; | |
10046 /* 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
|
10047 if (*arg == NUL && group != AUGROUP_ALL && !VIM_ISWHITE(arg[-1])) |
7 | 10048 { |
10049 arg = p; | |
10050 group = AUGROUP_ALL; | |
10051 } | |
10052 | |
10053 /* 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
|
10054 for (p = arg; *p != NUL && !VIM_ISWHITE(*p); ++p) |
7 | 10055 if (*p == ',') |
10056 arg = p + 1; | |
10057 if (*p == NUL) | |
10058 { | |
10059 if (group == AUGROUP_ALL) | |
10060 include_groups = TRUE; | |
10061 xp->xp_context = EXPAND_EVENTS; /* expand event name */ | |
10062 xp->xp_pattern = arg; | |
10063 return NULL; | |
10064 } | |
10065 | |
10066 /* skip over pattern */ | |
10067 arg = skipwhite(p); | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
10068 while (*arg && (!VIM_ISWHITE(*arg) || arg[-1] == '\\')) |
7 | 10069 arg++; |
10070 if (*arg) | |
10071 return arg; /* expand (next) command */ | |
10072 | |
10073 if (doautocmd) | |
10074 xp->xp_context = EXPAND_FILES; /* expand file names */ | |
10075 else | |
10076 xp->xp_context = EXPAND_NOTHING; /* pattern is not expanded */ | |
10077 return NULL; | |
10078 } | |
10079 | |
10080 /* | |
10081 * Function given to ExpandGeneric() to obtain the list of event names. | |
10082 */ | |
10083 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10084 get_event_name(expand_T *xp UNUSED, int idx) |
7 | 10085 { |
10086 if (idx < augroups.ga_len) /* First list group names, if wanted */ | |
10087 { | |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
10088 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
|
10089 || AUGROUP_NAME(idx) == get_deleted_augroup()) |
7 | 10090 return (char_u *)""; /* skip deleted entries */ |
10091 return AUGROUP_NAME(idx); /* return a name */ | |
10092 } | |
10093 return (char_u *)event_names[idx - augroups.ga_len].name; | |
10094 } | |
10095 | |
10096 #endif /* FEAT_CMDL_COMPL */ | |
10097 | |
10098 /* | |
615 | 10099 * Return TRUE if autocmd is supported. |
10100 */ | |
10101 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10102 autocmd_supported(char_u *name) |
615 | 10103 { |
10104 char_u *p; | |
10105 | |
10106 return (event_name2nr(name, &p) != NUM_EVENTS); | |
10107 } | |
10108 | |
10109 /* | |
612 | 10110 * Return TRUE if an autocommand is defined for a group, event and |
10111 * pattern: The group can be omitted to accept any group. "event" and "pattern" | |
10112 * can be NULL to accept any event and pattern. "pattern" can be NULL to accept | |
10113 * any pattern. Buffer-local patterns <buffer> or <buffer=N> are accepted. | |
10114 * Used for: | |
10115 * exists("#Group") or | |
10116 * exists("#Group#Event") or | |
10117 * exists("#Group#Event#pat") or | |
10118 * exists("#Event") or | |
10119 * exists("#Event#pat") | |
7 | 10120 */ |
10121 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10122 au_exists(char_u *arg) |
7 | 10123 { |
612 | 10124 char_u *arg_save; |
10125 char_u *pattern = NULL; | |
7 | 10126 char_u *event_name; |
10127 char_u *p; | |
661 | 10128 event_T event; |
7 | 10129 AutoPat *ap; |
40 | 10130 buf_T *buflocal_buf = NULL; |
612 | 10131 int group; |
10132 int retval = FALSE; | |
10133 | |
615 | 10134 /* Make a copy so that we can change the '#' chars to a NUL. */ |
612 | 10135 arg_save = vim_strsave(arg); |
10136 if (arg_save == NULL) | |
10137 return FALSE; | |
615 | 10138 p = vim_strchr(arg_save, '#'); |
612 | 10139 if (p != NULL) |
10140 *p++ = NUL; | |
10141 | |
10142 /* First, look for an autocmd group name */ | |
10143 group = au_find_group(arg_save); | |
10144 if (group == AUGROUP_ERROR) | |
10145 { | |
10146 /* Didn't match a group name, assume the first argument is an event. */ | |
10147 group = AUGROUP_ALL; | |
10148 event_name = arg_save; | |
10149 } | |
10150 else | |
10151 { | |
10152 if (p == NULL) | |
10153 { | |
10154 /* "Group": group name is present and it's recognized */ | |
10155 retval = TRUE; | |
10156 goto theend; | |
10157 } | |
10158 | |
10159 /* Must be "Group#Event" or "Group#Event#pat". */ | |
10160 event_name = p; | |
10161 p = vim_strchr(event_name, '#'); | |
10162 if (p != NULL) | |
10163 *p++ = NUL; /* "Group#Event#pat" */ | |
10164 } | |
10165 | |
10166 pattern = p; /* "pattern" is NULL when there is no pattern */ | |
7 | 10167 |
10168 /* find the index (enum) for the event name */ | |
10169 event = event_name2nr(event_name, &p); | |
10170 | |
10171 /* return FALSE if the event name is not recognized */ | |
612 | 10172 if (event == NUM_EVENTS) |
10173 goto theend; | |
7 | 10174 |
10175 /* Find the first autocommand for this event. | |
10176 * If there isn't any, return FALSE; | |
10177 * If there is one and no pattern given, return TRUE; */ | |
10178 ap = first_autopat[(int)event]; | |
10179 if (ap == NULL) | |
612 | 10180 goto theend; |
7 | 10181 |
40 | 10182 /* if pattern is "<buffer>", special handling is needed which uses curbuf */ |
10183 /* for pattern "<buffer=N>, fnamecmp() will work fine */ | |
1962 | 10184 if (pattern != NULL && STRICMP(pattern, "<buffer>") == 0) |
40 | 10185 buflocal_buf = curbuf; |
10186 | |
7 | 10187 /* Check if there is an autocommand with the given pattern. */ |
10188 for ( ; ap != NULL; ap = ap->next) | |
40 | 10189 /* only use a pattern when it has not been removed and has commands. */ |
10190 /* For buffer-local autocommands, fnamecmp() works fine. */ | |
7 | 10191 if (ap->pat != NULL && ap->cmds != NULL |
612 | 10192 && (group == AUGROUP_ALL || ap->group == group) |
1962 | 10193 && (pattern == NULL |
10194 || (buflocal_buf == NULL | |
10195 ? fnamecmp(ap->pat, pattern) == 0 | |
10196 : ap->buflocal_nr == buflocal_buf->b_fnum))) | |
612 | 10197 { |
10198 retval = TRUE; | |
10199 break; | |
10200 } | |
10201 | |
10202 theend: | |
10203 vim_free(arg_save); | |
10204 return retval; | |
7 | 10205 } |
40 | 10206 |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
10207 |
7 | 10208 /* |
153 | 10209 * Try matching a filename with a "pattern" ("prog" is NULL), or use the |
10210 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling | |
10211 * vim_regcomp() often. | |
7 | 10212 * Used for autocommands and 'wildignore'. |
10213 * Returns TRUE if there is a match, FALSE otherwise. | |
10214 */ | |
6375 | 10215 static int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10216 match_file_pat( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10217 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
|
10218 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
|
10219 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
|
10220 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
|
10221 char_u *tail, /* tail of path */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10222 int allow_dirs) /* allow matching with dir */ |
7 | 10223 { |
10224 regmatch_T regmatch; | |
10225 int result = FALSE; | |
10226 | |
4242 | 10227 regmatch.rm_ic = p_fic; /* ignore case if 'fileignorecase' is set */ |
6470 | 10228 if (prog != NULL) |
10229 regmatch.regprog = *prog; | |
7 | 10230 else |
6470 | 10231 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC); |
7 | 10232 |
10233 /* | |
10234 * Try for a match with the pattern with: | |
10235 * 1. the full file name, when the pattern has a '/'. | |
10236 * 2. the short file name, when the pattern has a '/'. | |
10237 * 3. the tail of the file name, when the pattern has no '/'. | |
10238 */ | |
6470 | 10239 if (regmatch.regprog != NULL |
7 | 10240 && ((allow_dirs |
10241 && (vim_regexec(®match, fname, (colnr_T)0) | |
10242 || (sfname != NULL | |
10243 && vim_regexec(®match, sfname, (colnr_T)0)))) | |
6470 | 10244 || (!allow_dirs && vim_regexec(®match, tail, (colnr_T)0)))) |
7 | 10245 result = TRUE; |
10246 | |
6375 | 10247 if (prog != NULL) |
10248 *prog = regmatch.regprog; | |
10249 else | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4354
diff
changeset
|
10250 vim_regfree(regmatch.regprog); |
7 | 10251 return result; |
10252 } | |
10253 | |
10254 #if defined(FEAT_WILDIGN) || defined(PROTO) | |
10255 /* | |
10256 * Return TRUE if a file matches with a pattern in "list". | |
10257 * "list" is a comma-separated list of patterns, like 'wildignore'. | |
10258 * "sfname" is the short file name or NULL, "ffname" the long file name. | |
10259 */ | |
10260 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10261 match_file_list(char_u *list, char_u *sfname, char_u *ffname) |
7 | 10262 { |
10263 char_u buf[100]; | |
10264 char_u *tail; | |
10265 char_u *regpat; | |
10266 char allow_dirs; | |
10267 int match; | |
10268 char_u *p; | |
10269 | |
10270 tail = gettail(sfname); | |
10271 | |
10272 /* try all patterns in 'wildignore' */ | |
10273 p = list; | |
10274 while (*p) | |
10275 { | |
10276 copy_option_part(&p, buf, 100, ","); | |
10277 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE); | |
10278 if (regpat == NULL) | |
10279 break; | |
153 | 10280 match = match_file_pat(regpat, NULL, ffname, sfname, |
10281 tail, (int)allow_dirs); | |
7 | 10282 vim_free(regpat); |
10283 if (match) | |
10284 return TRUE; | |
10285 } | |
10286 return FALSE; | |
10287 } | |
10288 #endif | |
10289 | |
10290 /* | |
10291 * Convert the given pattern "pat" which has shell style wildcards in it, into | |
10292 * a regular expression, and return the result in allocated memory. If there | |
10293 * is a directory path separator to be matched, then TRUE is put in | |
10294 * allow_dirs, otherwise FALSE is put there -- webb. | |
10295 * Handle backslashes before special characters, like "\*" and "\ ". | |
10296 * | |
10297 * Returns NULL when out of memory. | |
10298 */ | |
10299 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10300 file_pat_to_reg_pat( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10301 char_u *pat, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10302 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
|
10303 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
|
10304 int no_bslash UNUSED) /* Don't use a backward slash as pathsep */ |
7 | 10305 { |
6470 | 10306 int size = 2; /* '^' at start, '$' at end */ |
7 | 10307 char_u *endp; |
10308 char_u *reg_pat; | |
10309 char_u *p; | |
10310 int i; | |
10311 int nested = 0; | |
10312 int add_dollar = TRUE; | |
10313 | |
10314 if (allow_dirs != NULL) | |
10315 *allow_dirs = FALSE; | |
10316 if (pat_end == NULL) | |
10317 pat_end = pat + STRLEN(pat); | |
10318 | |
10319 for (p = pat; p < pat_end; p++) | |
10320 { | |
10321 switch (*p) | |
10322 { | |
10323 case '*': | |
10324 case '.': | |
10325 case ',': | |
10326 case '{': | |
10327 case '}': | |
10328 case '~': | |
10329 size += 2; /* extra backslash */ | |
10330 break; | |
10331 #ifdef BACKSLASH_IN_FILENAME | |
10332 case '\\': | |
10333 case '/': | |
10334 size += 4; /* could become "[\/]" */ | |
10335 break; | |
10336 #endif | |
10337 default: | |
10338 size++; | |
6999 | 10339 # ifdef FEAT_MBYTE |
474 | 10340 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 10341 { |
10342 ++p; | |
10343 ++size; | |
10344 } | |
10345 # endif | |
10346 break; | |
10347 } | |
10348 } | |
10349 reg_pat = alloc(size + 1); | |
10350 if (reg_pat == NULL) | |
10351 return NULL; | |
10352 | |
10353 i = 0; | |
10354 | |
10355 if (pat[0] == '*') | |
10356 while (pat[0] == '*' && pat < pat_end - 1) | |
10357 pat++; | |
10358 else | |
10359 reg_pat[i++] = '^'; | |
10360 endp = pat_end - 1; | |
7005 | 10361 if (endp >= pat && *endp == '*') |
7 | 10362 { |
10363 while (endp - pat > 0 && *endp == '*') | |
10364 endp--; | |
10365 add_dollar = FALSE; | |
10366 } | |
10367 for (p = pat; *p && nested >= 0 && p <= endp; p++) | |
10368 { | |
10369 switch (*p) | |
10370 { | |
10371 case '*': | |
10372 reg_pat[i++] = '.'; | |
10373 reg_pat[i++] = '*'; | |
444 | 10374 while (p[1] == '*') /* "**" matches like "*" */ |
10375 ++p; | |
7 | 10376 break; |
10377 case '.': | |
10378 case '~': | |
10379 reg_pat[i++] = '\\'; | |
10380 reg_pat[i++] = *p; | |
10381 break; | |
10382 case '?': | |
10383 reg_pat[i++] = '.'; | |
10384 break; | |
10385 case '\\': | |
10386 if (p[1] == NUL) | |
10387 break; | |
10388 #ifdef BACKSLASH_IN_FILENAME | |
10389 if (!no_bslash) | |
10390 { | |
10391 /* translate: | |
10392 * "\x" to "\\x" e.g., "dir\file" | |
10393 * "\*" to "\\.*" e.g., "dir\*.c" | |
10394 * "\?" to "\\." e.g., "dir\??.c" | |
10395 * "\+" to "\+" e.g., "fileX\+.c" | |
10396 */ | |
10397 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?') | |
10398 && p[1] != '+') | |
10399 { | |
10400 reg_pat[i++] = '['; | |
10401 reg_pat[i++] = '\\'; | |
10402 reg_pat[i++] = '/'; | |
10403 reg_pat[i++] = ']'; | |
10404 if (allow_dirs != NULL) | |
10405 *allow_dirs = TRUE; | |
10406 break; | |
10407 } | |
10408 } | |
10409 #endif | |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10410 /* Undo escaping from ExpandEscape(): |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10411 * foo\?bar -> foo?bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10412 * foo\%bar -> foo%bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10413 * foo\,bar -> foo,bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10414 * foo\ bar -> foo bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10415 * 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
|
10416 * regexp. |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
5008
diff
changeset
|
10417 * 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
|
10418 * 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
|
10419 */ |
7 | 10420 if (*++p == '?' |
10421 #ifdef BACKSLASH_IN_FILENAME | |
10422 && no_bslash | |
10423 #endif | |
10424 ) | |
10425 reg_pat[i++] = '?'; | |
10426 else | |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
5008
diff
changeset
|
10427 if (*p == ',' || *p == '%' || *p == '#' |
6999 | 10428 || vim_isspace(*p) || *p == '{' || *p == '}') |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10429 reg_pat[i++] = *p; |
5259
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10430 else if (*p == '\\' && p[1] == '\\' && p[2] == '{') |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10431 { |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10432 reg_pat[i++] = '\\'; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10433 reg_pat[i++] = '{'; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10434 p += 2; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10435 } |
7 | 10436 else |
10437 { | |
10438 if (allow_dirs != NULL && vim_ispathsep(*p) | |
10439 #ifdef BACKSLASH_IN_FILENAME | |
10440 && (!no_bslash || *p != '\\') | |
10441 #endif | |
10442 ) | |
10443 *allow_dirs = TRUE; | |
10444 reg_pat[i++] = '\\'; | |
10445 reg_pat[i++] = *p; | |
10446 } | |
10447 break; | |
10448 #ifdef BACKSLASH_IN_FILENAME | |
10449 case '/': | |
10450 reg_pat[i++] = '['; | |
10451 reg_pat[i++] = '\\'; | |
10452 reg_pat[i++] = '/'; | |
10453 reg_pat[i++] = ']'; | |
10454 if (allow_dirs != NULL) | |
10455 *allow_dirs = TRUE; | |
10456 break; | |
10457 #endif | |
10458 case '{': | |
10459 reg_pat[i++] = '\\'; | |
10460 reg_pat[i++] = '('; | |
10461 nested++; | |
10462 break; | |
10463 case '}': | |
10464 reg_pat[i++] = '\\'; | |
10465 reg_pat[i++] = ')'; | |
10466 --nested; | |
10467 break; | |
10468 case ',': | |
10469 if (nested) | |
10470 { | |
10471 reg_pat[i++] = '\\'; | |
10472 reg_pat[i++] = '|'; | |
10473 } | |
10474 else | |
10475 reg_pat[i++] = ','; | |
10476 break; | |
10477 default: | |
10478 # ifdef FEAT_MBYTE | |
474 | 10479 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 10480 reg_pat[i++] = *p++; |
10481 else | |
10482 # endif | |
10483 if (allow_dirs != NULL && vim_ispathsep(*p)) | |
10484 *allow_dirs = TRUE; | |
10485 reg_pat[i++] = *p; | |
10486 break; | |
10487 } | |
10488 } | |
10489 if (add_dollar) | |
10490 reg_pat[i++] = '$'; | |
10491 reg_pat[i] = NUL; | |
10492 if (nested != 0) | |
10493 { | |
10494 if (nested < 0) | |
10495 EMSG(_("E219: Missing {.")); | |
10496 else | |
10497 EMSG(_("E220: Missing }.")); | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
10498 VIM_CLEAR(reg_pat); |
7 | 10499 } |
10500 return reg_pat; | |
10501 } | |
2664 | 10502 |
10503 #if defined(EINTR) || defined(PROTO) | |
10504 /* | |
10505 * Version of read() that retries when interrupted by EINTR (possibly | |
10506 * by a SIGWINCH). | |
10507 */ | |
10508 long | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10509 read_eintr(int fd, void *buf, size_t bufsize) |
2664 | 10510 { |
10511 long ret; | |
10512 | |
10513 for (;;) | |
10514 { | |
10515 ret = vim_read(fd, buf, bufsize); | |
10516 if (ret >= 0 || errno != EINTR) | |
10517 break; | |
10518 } | |
10519 return ret; | |
10520 } | |
10521 | |
10522 /* | |
10523 * Version of write() that retries when interrupted by EINTR (possibly | |
10524 * by a SIGWINCH). | |
10525 */ | |
10526 long | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10527 write_eintr(int fd, void *buf, size_t bufsize) |
2664 | 10528 { |
10529 long ret = 0; | |
10530 long wlen; | |
10531 | |
10532 /* Repeat the write() so long it didn't fail, other than being interrupted | |
10533 * by a signal. */ | |
10534 while (ret < (long)bufsize) | |
10535 { | |
2666 | 10536 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret); |
2664 | 10537 if (wlen < 0) |
10538 { | |
10539 if (errno != EINTR) | |
10540 break; | |
10541 } | |
10542 else | |
10543 ret += wlen; | |
10544 } | |
10545 return ret; | |
10546 } | |
10547 #endif |