Mercurial > vim
annotate src/fileio.c @ 9536:b2aada04d84e v7.4.2048
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat Jul 16 14:47:36 2016 +0200
patch 7.4.2048
Problem: There is still code and help for unsupported systems.
Solution: Remove the code and text. (Hirohito Higashi)
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sat, 16 Jul 2016 15:00:07 +0200 |
parents | e61974ab3ed0 |
children | 0190d5de215f |
rev | line source |
---|---|
7 | 1 /* vi:set ts=8 sts=4 sw=4: |
2 * | |
3 * VIM - Vi IMproved by Bram Moolenaar | |
4 * | |
5 * Do ":help uganda" in Vim to read copying and usage conditions. | |
6 * Do ":help credits" in Vim to see a list of people who contributed. | |
7 * See README.txt for an overview of the Vim source code. | |
8 */ | |
9 | |
10 /* | |
11 * fileio.c: read from and write to a file | |
12 */ | |
13 | |
14 #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 |
7320
cc484bbb73dc
commit https://github.com/vim/vim/commit/941aea2b975623a0c8bc24b140881ef0032a8bb8
Christian Brabandt <cb@256bit.org>
parents:
7305
diff
changeset
|
30 #if (defined(sun) || defined(__FreeBSD__)) && defined(S_ISCHR) |
1313 | 31 # define OPEN_CHR_FILES |
32 static int is_dev_fd_file(char_u *fname); | |
33 #endif | |
7 | 34 #ifdef FEAT_MBYTE |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
35 static char_u *next_fenc(char_u **pp); |
7 | 36 # ifdef FEAT_EVAL |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
37 static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp); |
7 | 38 # endif |
39 #endif | |
40 #ifdef FEAT_VIMINFO | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
41 static void check_marks_read(void); |
7 | 42 #endif |
43 #ifdef FEAT_CRYPT | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
44 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 | 45 #endif |
46 #ifdef UNIX | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
47 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
|
48 #endif |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
49 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
|
50 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
|
51 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
|
52 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
|
53 static int time_differs(long t1, long t2); |
7 | 54 #ifdef FEAT_AUTOCMD |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
55 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
|
56 static int au_find_group(char_u *name); |
676 | 57 |
58 # define AUGROUP_DEFAULT -1 /* default autocmd group */ | |
1834 | 59 # define AUGROUP_ERROR -2 /* erroneous autocmd group */ |
676 | 60 # define AUGROUP_ALL -3 /* all autocmd groups */ |
7 | 61 #endif |
62 | |
63 #if defined(FEAT_CRYPT) || defined(FEAT_MBYTE) | |
64 # define HAS_BW_FLAGS | |
65 # define FIO_LATIN1 0x01 /* convert Latin1 */ | |
66 # define FIO_UTF8 0x02 /* convert UTF-8 */ | |
67 # define FIO_UCS2 0x04 /* convert UCS-2 */ | |
68 # define FIO_UCS4 0x08 /* convert UCS-4 */ | |
69 # define FIO_UTF16 0x10 /* convert UTF-16 */ | |
70 # ifdef WIN3264 | |
71 # define FIO_CODEPAGE 0x20 /* convert MS-Windows codepage */ | |
72 # define FIO_PUT_CP(x) (((x) & 0xffff) << 16) /* put codepage in top word */ | |
73 # define FIO_GET_CP(x) (((x)>>16) & 0xffff) /* get codepage from top word */ | |
74 # endif | |
75 # ifdef MACOS_X | |
76 # define FIO_MACROMAN 0x20 /* convert MacRoman */ | |
77 # endif | |
78 # define FIO_ENDIAN_L 0x80 /* little endian */ | |
79 # define FIO_ENCRYPTED 0x1000 /* encrypt written bytes */ | |
80 # define FIO_NOCONVERT 0x2000 /* skip encoding conversion */ | |
81 # define FIO_UCSBOM 0x4000 /* check for BOM at start of file */ | |
82 # define FIO_ALL -1 /* allow all formats */ | |
83 #endif | |
84 | |
85 /* When converting, a read() or write() may leave some bytes to be converted | |
86 * for the next call. The value is guessed... */ | |
87 #define CONV_RESTLEN 30 | |
88 | |
89 /* We have to guess how much a sequence of bytes may expand when converting | |
90 * with iconv() to be able to allocate a buffer. */ | |
91 #define ICONV_MULT 8 | |
92 | |
93 /* | |
94 * Structure to pass arguments from buf_write() to buf_write_bytes(). | |
95 */ | |
96 struct bw_info | |
97 { | |
98 int bw_fd; /* file descriptor */ | |
99 char_u *bw_buf; /* buffer with data to be written */ | |
1411 | 100 int bw_len; /* length of data */ |
7 | 101 #ifdef HAS_BW_FLAGS |
102 int bw_flags; /* FIO_ flags */ | |
103 #endif | |
6122 | 104 #ifdef FEAT_CRYPT |
105 buf_T *bw_buffer; /* buffer being written */ | |
106 #endif | |
7 | 107 #ifdef FEAT_MBYTE |
108 char_u bw_rest[CONV_RESTLEN]; /* not converted bytes */ | |
109 int bw_restlen; /* nr of bytes in bw_rest[] */ | |
110 int bw_first; /* first write call */ | |
111 char_u *bw_conv_buf; /* buffer for writing converted chars */ | |
112 int bw_conv_buflen; /* size of bw_conv_buf */ | |
113 int bw_conv_error; /* set for conversion error */ | |
1947 | 114 linenr_T bw_conv_error_lnum; /* first line with error or zero */ |
115 linenr_T bw_start_lnum; /* line number at start of buffer */ | |
7 | 116 # ifdef USE_ICONV |
117 iconv_t bw_iconv_fd; /* descriptor for iconv() or -1 */ | |
118 # endif | |
119 #endif | |
120 }; | |
121 | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
122 static int buf_write_bytes(struct bw_info *ip); |
7 | 123 |
124 #ifdef FEAT_MBYTE | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
125 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
|
126 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
|
127 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
|
128 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
|
129 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
|
130 static int make_bom(char_u *buf, char_u *name); |
7 | 131 # ifdef WIN3264 |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
132 static int get_win_fio_flags(char_u *ptr); |
7 | 133 # endif |
134 # ifdef MACOS_X | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
135 static int get_mac_fio_flags(char_u *ptr); |
7 | 136 # endif |
137 #endif | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
138 static int move_lines(buf_T *frombuf, buf_T *tobuf); |
2006 | 139 #ifdef TEMPDIRNAMES |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
140 static void vim_settempdir(char_u *tempdir); |
2006 | 141 #endif |
1834 | 142 #ifdef FEAT_AUTOCMD |
143 static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name"); | |
144 #endif | |
595 | 145 |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
146 #ifdef FEAT_AUTOCMD |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
147 /* |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
148 * 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
|
149 * 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
|
150 * EVENT_BUFREADPOST triggered the EVENT_FILETYPE. |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
151 * |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
152 * 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
|
153 * apply_autocmds_group. |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
154 */ |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
155 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
|
156 #endif |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
157 |
7 | 158 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
159 filemess( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
160 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
161 char_u *name, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
162 char_u *s, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
163 int attr) |
7 | 164 { |
165 int msg_scroll_save; | |
166 | |
167 if (msg_silent != 0) | |
168 return; | |
169 msg_add_fname(buf, name); /* put file name in IObuff with quotes */ | |
170 /* If it's extremely long, truncate it. */ | |
171 if (STRLEN(IObuff) > IOSIZE - 80) | |
172 IObuff[IOSIZE - 80] = NUL; | |
173 STRCAT(IObuff, s); | |
174 /* | |
175 * For the first message may have to start a new line. | |
176 * For further ones overwrite the previous one, reset msg_scroll before | |
177 * calling filemess(). | |
178 */ | |
179 msg_scroll_save = msg_scroll; | |
180 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) | |
181 msg_scroll = FALSE; | |
182 if (!msg_scroll) /* wait a bit when overwriting an error msg */ | |
183 check_for_delay(FALSE); | |
184 msg_start(); | |
185 msg_scroll = msg_scroll_save; | |
186 msg_scrolled_ign = TRUE; | |
187 /* may truncate the message to avoid a hit-return prompt */ | |
188 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr); | |
189 msg_clr_eos(); | |
190 out_flush(); | |
191 msg_scrolled_ign = FALSE; | |
192 } | |
193 | |
194 /* | |
195 * Read lines from file "fname" into the buffer after line "from". | |
196 * | |
197 * 1. We allocate blocks with lalloc, as big as possible. | |
198 * 2. Each block is filled with characters from the file with a single read(). | |
199 * 3. The lines are inserted in the buffer with ml_append(). | |
200 * | |
201 * (caller must check that fname != NULL, unless READ_STDIN is used) | |
202 * | |
203 * "lines_to_skip" is the number of lines that must be skipped | |
204 * "lines_to_read" is the number of lines that are appended | |
205 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM. | |
206 * | |
207 * flags: | |
208 * READ_NEW starting to edit a new buffer | |
209 * READ_FILTER reading filter output | |
210 * READ_STDIN read from stdin instead of a file | |
211 * READ_BUFFER read from curbuf instead of a file (converting after reading | |
212 * stdin) | |
213 * READ_DUMMY read into a dummy buffer (to check if file contents changed) | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
214 * READ_KEEP_UNDO don't clear undo info or read it from a file |
7 | 215 * |
216 * return FAIL for failure, OK otherwise | |
217 */ | |
218 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
219 readfile( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
220 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
221 char_u *sfname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
222 linenr_T from, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
223 linenr_T lines_to_skip, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
224 linenr_T lines_to_read, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
225 exarg_T *eap, /* can be NULL! */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
226 int flags) |
7 | 227 { |
228 int fd = 0; | |
229 int newfile = (flags & READ_NEW); | |
230 int check_readonly; | |
231 int filtering = (flags & READ_FILTER); | |
232 int read_stdin = (flags & READ_STDIN); | |
233 int read_buffer = (flags & READ_BUFFER); | |
1486 | 234 int set_options = newfile || read_buffer |
235 || (eap != NULL && eap->read_edit); | |
7 | 236 linenr_T read_buf_lnum = 1; /* next line to read from curbuf */ |
237 colnr_T read_buf_col = 0; /* next char to read from this line */ | |
238 char_u c; | |
239 linenr_T lnum = from; | |
240 char_u *ptr = NULL; /* pointer into read buffer */ | |
241 char_u *buffer = NULL; /* read buffer */ | |
242 char_u *new_buffer = NULL; /* init to shut up gcc */ | |
243 char_u *line_start = NULL; /* init to shut up gcc */ | |
244 int wasempty; /* buffer was empty before reading */ | |
245 colnr_T len; | |
246 long size = 0; | |
247 char_u *p; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
248 off_T filesize = 0; |
7 | 249 int skip_read = FALSE; |
250 #ifdef FEAT_CRYPT | |
251 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
|
252 int did_ask_for_key = FALSE; |
7 | 253 #endif |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
254 #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
|
255 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
|
256 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
|
257 #endif |
7 | 258 int split = 0; /* number of split lines */ |
259 #define UNKNOWN 0x0fffffff /* file size is unknown */ | |
260 linenr_T linecnt; | |
261 int error = FALSE; /* errors encountered */ | |
262 int ff_error = EOL_UNKNOWN; /* file format with errors */ | |
263 long linerest = 0; /* remaining chars in line */ | |
264 #ifdef UNIX | |
265 int perm = 0; | |
266 int swap_mode = -1; /* protection bits for swap file */ | |
267 #else | |
268 int perm; | |
269 #endif | |
270 int fileformat = 0; /* end-of-line format */ | |
271 int keep_fileformat = FALSE; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
272 stat_T st; |
7 | 273 int file_readonly; |
274 linenr_T skip_count = 0; | |
275 linenr_T read_count = 0; | |
276 int msg_save = msg_scroll; | |
277 linenr_T read_no_eol_lnum = 0; /* non-zero lnum when last line of | |
278 * last read was missing the eol */ | |
279 int try_mac = (vim_strchr(p_ffs, 'm') != NULL); | |
280 int try_dos = (vim_strchr(p_ffs, 'd') != NULL); | |
281 int try_unix = (vim_strchr(p_ffs, 'x') != NULL); | |
282 int file_rewind = FALSE; | |
283 #ifdef FEAT_MBYTE | |
284 int can_retry; | |
595 | 285 linenr_T conv_error = 0; /* line nr with conversion error */ |
286 linenr_T illegal_byte = 0; /* line nr with illegal byte */ | |
7 | 287 int keep_dest_enc = FALSE; /* don't retry when char doesn't fit |
288 in destination encoding */ | |
595 | 289 int bad_char_behavior = BAD_REPLACE; |
290 /* BAD_KEEP, BAD_DROP or character to | |
291 * replace with */ | |
7 | 292 char_u *tmpname = NULL; /* name of 'charconvert' output file */ |
293 int fio_flags = 0; | |
294 char_u *fenc; /* fileencoding to use */ | |
295 int fenc_alloced; /* fenc_next is in allocated memory */ | |
296 char_u *fenc_next = NULL; /* next item in 'fencs' or NULL */ | |
297 int advance_fenc = FALSE; | |
298 long real_size = 0; | |
299 # ifdef USE_ICONV | |
300 iconv_t iconv_fd = (iconv_t)-1; /* descriptor for iconv() or -1 */ | |
301 # ifdef FEAT_EVAL | |
302 int did_iconv = FALSE; /* TRUE when iconv() failed and trying | |
303 'charconvert' next */ | |
304 # endif | |
305 # endif | |
306 int converted = FALSE; /* TRUE if conversion done */ | |
307 int notconverted = FALSE; /* TRUE if conversion wanted but it | |
308 wasn't possible */ | |
309 char_u conv_rest[CONV_RESTLEN]; | |
310 int conv_restlen = 0; /* nr of bytes in conv_rest[] */ | |
311 #endif | |
1834 | 312 #ifdef FEAT_AUTOCMD |
2563
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
313 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
|
314 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
|
315 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
|
316 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
|
317 int using_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
|
318 #endif |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
319 |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
320 #ifdef FEAT_AUTOCMD |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
321 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
|
322 #endif |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
323 |
2707 | 324 curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */ |
7 | 325 |
326 /* | |
327 * If there is no file name yet, use the one for the read file. | |
328 * BF_NOTEDITED is set to reflect this. | |
329 * Don't do this for a read from a filter. | |
330 * Only do this when 'cpoptions' contains the 'f' flag. | |
331 */ | |
332 if (curbuf->b_ffname == NULL | |
333 && !filtering | |
334 && fname != NULL | |
335 && vim_strchr(p_cpo, CPO_FNAMER) != NULL | |
336 && !(flags & READ_DUMMY)) | |
337 { | |
633 | 338 if (set_rw_fname(fname, sfname) == FAIL) |
339 return FAIL; | |
7 | 340 } |
341 | |
2563
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
342 #ifdef FEAT_AUTOCMD |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
343 /* 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
|
344 * 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
|
345 * 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
|
346 * 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
|
347 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
|
348 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
|
349 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
|
350 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
|
351 || (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
|
352 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
|
353 #endif |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
354 |
167 | 355 /* After reading a file the cursor line changes but we don't want to |
356 * display the line. */ | |
357 ex_no_reprint = TRUE; | |
358 | |
968 | 359 /* don't display the file info for another buffer now */ |
360 need_fileinfo = FALSE; | |
361 | |
7 | 362 /* |
363 * For Unix: Use the short file name whenever possible. | |
364 * Avoids problems with networks and when directory names are changed. | |
365 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to | |
366 * another directory, which we don't detect. | |
367 */ | |
368 if (sfname == NULL) | |
369 sfname = fname; | |
9536
b2aada04d84e
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Christian Brabandt <cb@256bit.org>
parents:
9509
diff
changeset
|
370 #if defined(UNIX) |
7 | 371 fname = sfname; |
372 #endif | |
373 | |
374 #ifdef FEAT_AUTOCMD | |
375 /* | |
376 * The BufReadCmd and FileReadCmd events intercept the reading process by | |
377 * executing the associated commands instead. | |
378 */ | |
379 if (!filtering && !read_stdin && !read_buffer) | |
380 { | |
381 pos_T pos; | |
382 | |
383 pos = curbuf->b_op_start; | |
384 | |
385 /* Set '[ mark to the line above where the lines go (line 1 if zero). */ | |
386 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); | |
387 curbuf->b_op_start.col = 0; | |
388 | |
389 if (newfile) | |
390 { | |
391 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname, | |
392 FALSE, curbuf, eap)) | |
393 #ifdef FEAT_EVAL | |
394 return aborting() ? FAIL : OK; | |
395 #else | |
396 return OK; | |
397 #endif | |
398 } | |
399 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname, | |
400 FALSE, NULL, eap)) | |
401 #ifdef FEAT_EVAL | |
402 return aborting() ? FAIL : OK; | |
403 #else | |
404 return OK; | |
405 #endif | |
406 | |
407 curbuf->b_op_start = pos; | |
408 } | |
409 #endif | |
410 | |
411 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0) | |
412 msg_scroll = FALSE; /* overwrite previous file message */ | |
413 else | |
414 msg_scroll = TRUE; /* don't overwrite previous file message */ | |
415 | |
416 /* | |
417 * If the name ends in a path separator, we can't open it. Check here, | |
418 * because reading the file may actually work, but then creating the swap | |
419 * file may destroy it! Reported on MS-DOS and Win 95. | |
420 * If the name is too long we might crash further on, quit here. | |
421 */ | |
23 | 422 if (fname != NULL && *fname != NUL) |
423 { | |
39 | 424 p = fname + STRLEN(fname); |
425 if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL) | |
23 | 426 { |
427 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0); | |
428 msg_end(); | |
429 msg_scroll = msg_save; | |
430 return FAIL; | |
431 } | |
7 | 432 } |
433 | |
5322 | 434 if (!read_stdin && !read_buffer) |
435 { | |
7 | 436 #ifdef UNIX |
5322 | 437 /* |
438 * On Unix it is possible to read a directory, so we have to | |
439 * check for it before the mch_open(). | |
440 */ | |
7 | 441 perm = mch_getperm(fname); |
442 if (perm >= 0 && !S_ISREG(perm) /* not a regular file ... */ | |
443 # ifdef S_ISFIFO | |
444 && !S_ISFIFO(perm) /* ... or fifo */ | |
445 # endif | |
446 # ifdef S_ISSOCK | |
447 && !S_ISSOCK(perm) /* ... or socket */ | |
448 # endif | |
1313 | 449 # ifdef OPEN_CHR_FILES |
450 && !(S_ISCHR(perm) && is_dev_fd_file(fname)) | |
451 /* ... or a character special file named /dev/fd/<n> */ | |
452 # endif | |
7 | 453 ) |
454 { | |
455 if (S_ISDIR(perm)) | |
456 filemess(curbuf, fname, (char_u *)_("is a directory"), 0); | |
457 else | |
458 filemess(curbuf, fname, (char_u *)_("is not a file"), 0); | |
459 msg_end(); | |
460 msg_scroll = msg_save; | |
461 return FAIL; | |
462 } | |
5322 | 463 #endif |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
464 #if defined(MSWIN) |
1006 | 465 /* |
466 * MS-Windows allows opening a device, but we will probably get stuck | |
467 * trying to read it. | |
468 */ | |
469 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE) | |
470 { | |
1303 | 471 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0); |
1006 | 472 msg_end(); |
473 msg_scroll = msg_save; | |
474 return FAIL; | |
475 } | |
5322 | 476 #endif |
477 } | |
1004 | 478 |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
479 /* Set default or forced 'fileformat' and 'binary'. */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
480 set_file_options(set_options, eap); |
7 | 481 |
482 /* | |
483 * When opening a new file we take the readonly flag from the file. | |
484 * Default is r/w, can be set to r/o below. | |
485 * Don't reset it when in readonly mode | |
486 * Only set/reset b_p_ro when BF_CHECK_RO is set. | |
487 */ | |
488 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO)); | |
164 | 489 if (check_readonly && !readonlymode) |
7 | 490 curbuf->b_p_ro = FALSE; |
491 | |
492 if (newfile && !read_stdin && !read_buffer) | |
493 { | |
2823 | 494 /* Remember time of file. */ |
7 | 495 if (mch_stat((char *)fname, &st) >= 0) |
496 { | |
497 buf_store_time(curbuf, &st, fname); | |
498 curbuf->b_mtime_read = curbuf->b_mtime; | |
499 #ifdef UNIX | |
500 /* | |
501 * Use the protection bits of the original file for the swap file. | |
502 * This makes it possible for others to read the name of the | |
503 * edited file from the swapfile, but only if they can read the | |
504 * edited file. | |
505 * Remove the "write" and "execute" bits for group and others | |
506 * (they must not write the swapfile). | |
507 * Add the "read" and "write" bits for the user, otherwise we may | |
508 * not be able to write to the file ourselves. | |
509 * Setting the bits is done below, after creating the swap file. | |
510 */ | |
511 swap_mode = (st.st_mode & 0644) | 0600; | |
512 #endif | |
513 #ifdef FEAT_CW_EDITOR | |
514 /* Get the FSSpec on MacOS | |
515 * TODO: Update it properly when the buffer name changes | |
516 */ | |
517 (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec); | |
518 #endif | |
519 #ifdef VMS | |
520 curbuf->b_fab_rfm = st.st_fab_rfm; | |
22 | 521 curbuf->b_fab_rat = st.st_fab_rat; |
522 curbuf->b_fab_mrs = st.st_fab_mrs; | |
7 | 523 #endif |
524 } | |
525 else | |
526 { | |
527 curbuf->b_mtime = 0; | |
528 curbuf->b_mtime_read = 0; | |
529 curbuf->b_orig_size = 0; | |
530 curbuf->b_orig_mode = 0; | |
531 } | |
532 | |
533 /* Reset the "new file" flag. It will be set again below when the | |
534 * file doesn't exist. */ | |
535 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W); | |
536 } | |
537 | |
538 /* | |
539 * 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
|
540 * for Amiga: check readonly by trying to open the file for writing |
7 | 541 */ |
542 file_readonly = FALSE; | |
543 if (read_stdin) | |
544 { | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
545 #if defined(MSWIN) |
7 | 546 /* Force binary I/O on stdin to avoid CR-LF -> LF conversion. */ |
547 setmode(0, O_BINARY); | |
548 #endif | |
549 } | |
550 else if (!read_buffer) | |
551 { | |
552 #ifdef USE_MCH_ACCESS | |
553 if ( | |
554 # ifdef UNIX | |
555 !(perm & 0222) || | |
556 # endif | |
557 mch_access((char *)fname, W_OK)) | |
558 file_readonly = TRUE; | |
559 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
560 #else | |
561 if (!newfile | |
562 || readonlymode | |
563 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0) | |
564 { | |
565 file_readonly = TRUE; | |
566 /* try to open ro */ | |
567 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
568 } | |
569 #endif | |
570 } | |
571 | |
572 if (fd < 0) /* cannot open at all */ | |
573 { | |
574 #ifndef UNIX | |
575 int isdir_f; | |
576 #endif | |
577 msg_scroll = msg_save; | |
578 #ifndef UNIX | |
579 /* | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
580 * On Amiga we can't open a directory, check here. |
7 | 581 */ |
582 isdir_f = (mch_isdir(fname)); | |
583 perm = mch_getperm(fname); /* check if the file exists */ | |
584 if (isdir_f) | |
585 { | |
586 filemess(curbuf, sfname, (char_u *)_("is a directory"), 0); | |
587 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
588 } | |
589 else | |
590 #endif | |
591 if (newfile) | |
592 { | |
2147
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
593 if (perm < 0 |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
594 #ifdef ENOENT |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
595 && errno == ENOENT |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
596 #endif |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
597 ) |
7 | 598 { |
599 /* | |
600 * Set the 'new-file' flag, so that when the file has | |
601 * been created by someone else, a ":w" will complain. | |
602 */ | |
603 curbuf->b_flags |= BF_NEW; | |
604 | |
605 /* Create a swap file now, so that other Vims are warned | |
606 * that we are editing this file. Don't do this for a | |
607 * "nofile" or "nowrite" buffer type. */ | |
608 #ifdef FEAT_QUICKFIX | |
609 if (!bt_dontwrite(curbuf)) | |
610 #endif | |
1834 | 611 { |
7 | 612 check_need_swap(newfile); |
1834 | 613 #ifdef FEAT_AUTOCMD |
614 /* SwapExists autocommand may mess things up */ | |
615 if (curbuf != old_curbuf | |
616 || (using_b_ffname | |
617 && (old_b_ffname != curbuf->b_ffname)) | |
618 || (using_b_fname | |
619 && (old_b_fname != curbuf->b_fname))) | |
620 { | |
621 EMSG(_(e_auchangedbuf)); | |
622 return FAIL; | |
623 } | |
624 #endif | |
625 } | |
592 | 626 if (dir_of_file_exists(fname)) |
627 filemess(curbuf, sfname, (char_u *)_("[New File]"), 0); | |
628 else | |
629 filemess(curbuf, sfname, | |
630 (char_u *)_("[New DIRECTORY]"), 0); | |
7 | 631 #ifdef FEAT_VIMINFO |
632 /* Even though this is a new file, it might have been | |
633 * edited before and deleted. Get the old marks. */ | |
634 check_marks_read(); | |
635 #endif | |
636 #ifdef FEAT_MBYTE | |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
637 /* Set forced 'fileencoding'. */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
638 if (eap != NULL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
639 set_forced_fenc(eap); |
7 | 640 #endif |
641 #ifdef FEAT_AUTOCMD | |
642 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname, | |
643 FALSE, curbuf, eap); | |
644 #endif | |
645 /* remember the current fileformat */ | |
646 save_file_ff(curbuf); | |
647 | |
648 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
649 if (aborting()) /* autocmds may abort script processing */ | |
650 return FAIL; | |
651 #endif | |
652 return OK; /* a new file is not an error */ | |
653 } | |
654 else | |
655 { | |
550 | 656 filemess(curbuf, sfname, (char_u *)( |
657 # ifdef EFBIG | |
658 (errno == EFBIG) ? _("[File too big]") : | |
659 # endif | |
2147
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
660 # ifdef EOVERFLOW |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
661 (errno == EOVERFLOW) ? _("[File too big]") : |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
662 # endif |
550 | 663 _("[Permission Denied]")), 0); |
7 | 664 curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
665 } | |
666 } | |
667 | |
668 return FAIL; | |
669 } | |
670 | |
671 /* | |
672 * Only set the 'ro' flag for readonly files the first time they are | |
673 * loaded. Help files always get readonly mode | |
674 */ | |
675 if ((check_readonly && file_readonly) || curbuf->b_help) | |
676 curbuf->b_p_ro = TRUE; | |
677 | |
819 | 678 if (set_options) |
7 | 679 { |
1486 | 680 /* Don't change 'eol' if reading from buffer as it will already be |
681 * correctly set when reading stdin. */ | |
682 if (!read_buffer) | |
683 { | |
684 curbuf->b_p_eol = TRUE; | |
685 curbuf->b_start_eol = TRUE; | |
686 } | |
7 | 687 #ifdef FEAT_MBYTE |
688 curbuf->b_p_bomb = FALSE; | |
1352 | 689 curbuf->b_start_bomb = FALSE; |
7 | 690 #endif |
691 } | |
692 | |
693 /* Create a swap file now, so that other Vims are warned that we are | |
694 * editing this file. | |
695 * Don't do this for a "nofile" or "nowrite" buffer type. */ | |
696 #ifdef FEAT_QUICKFIX | |
697 if (!bt_dontwrite(curbuf)) | |
698 #endif | |
699 { | |
700 check_need_swap(newfile); | |
1834 | 701 #ifdef FEAT_AUTOCMD |
702 if (!read_stdin && (curbuf != old_curbuf | |
703 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) | |
704 || (using_b_fname && (old_b_fname != curbuf->b_fname)))) | |
705 { | |
706 EMSG(_(e_auchangedbuf)); | |
707 if (!read_buffer) | |
708 close(fd); | |
709 return FAIL; | |
710 } | |
711 #endif | |
7 | 712 #ifdef UNIX |
713 /* Set swap file protection bits after creating it. */ | |
1918 | 714 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL |
715 && curbuf->b_ml.ml_mfp->mf_fname != NULL) | |
7 | 716 (void)mch_setperm(curbuf->b_ml.ml_mfp->mf_fname, (long)swap_mode); |
717 #endif | |
718 } | |
719 | |
578 | 720 #if defined(HAS_SWAP_EXISTS_ACTION) |
7 | 721 /* If "Quit" selected at ATTENTION dialog, don't load the file */ |
722 if (swap_exists_action == SEA_QUIT) | |
723 { | |
724 if (!read_buffer && !read_stdin) | |
725 close(fd); | |
726 return FAIL; | |
727 } | |
728 #endif | |
729 | |
730 ++no_wait_return; /* don't wait for return yet */ | |
731 | |
732 /* | |
733 * Set '[ mark to the line above where the lines go (line 1 if zero). | |
734 */ | |
735 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); | |
736 curbuf->b_op_start.col = 0; | |
737 | |
738 #ifdef FEAT_AUTOCMD | |
739 if (!read_buffer) | |
740 { | |
741 int m = msg_scroll; | |
742 int n = msg_scrolled; | |
743 | |
744 /* | |
745 * The file must be closed again, the autocommands may want to change | |
746 * the file before reading it. | |
747 */ | |
748 if (!read_stdin) | |
749 close(fd); /* ignore errors */ | |
750 | |
751 /* | |
752 * The output from the autocommands should not overwrite anything and | |
753 * should not be overwritten: Set msg_scroll, restore its value if no | |
754 * output was done. | |
755 */ | |
756 msg_scroll = TRUE; | |
757 if (filtering) | |
758 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname, | |
759 FALSE, curbuf, eap); | |
760 else if (read_stdin) | |
761 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname, | |
762 FALSE, curbuf, eap); | |
763 else if (newfile) | |
764 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname, | |
765 FALSE, curbuf, eap); | |
766 else | |
767 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname, | |
768 FALSE, NULL, eap); | |
769 if (msg_scrolled == n) | |
770 msg_scroll = m; | |
771 | |
772 #ifdef FEAT_EVAL | |
773 if (aborting()) /* autocmds may abort script processing */ | |
774 { | |
775 --no_wait_return; | |
776 msg_scroll = msg_save; | |
777 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
778 return FAIL; | |
779 } | |
780 #endif | |
781 /* | |
782 * Don't allow the autocommands to change the current buffer. | |
783 * Try to re-open the file. | |
1834 | 784 * |
785 * Don't allow the autocommands to change the buffer name either | |
786 * (cd for example) if it invalidates fname or sfname. | |
7 | 787 */ |
788 if (!read_stdin && (curbuf != old_curbuf | |
1834 | 789 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) |
790 || (using_b_fname && (old_b_fname != curbuf->b_fname)) | |
7 | 791 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0)) |
792 { | |
793 --no_wait_return; | |
794 msg_scroll = msg_save; | |
795 if (fd < 0) | |
796 EMSG(_("E200: *ReadPre autocommands made the file unreadable")); | |
797 else | |
798 EMSG(_("E201: *ReadPre autocommands must not change current buffer")); | |
799 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
800 return FAIL; | |
801 } | |
802 } | |
803 #endif /* FEAT_AUTOCMD */ | |
804 | |
805 /* Autocommands may add lines to the file, need to check if it is empty */ | |
806 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY); | |
807 | |
808 if (!recoverymode && !filtering && !(flags & READ_DUMMY)) | |
809 { | |
810 /* | |
811 * Show the user that we are busy reading the input. Sometimes this | |
812 * may take a while. When reading from stdin another program may | |
813 * still be running, don't move the cursor to the last line, unless | |
814 * always using the GUI. | |
815 */ | |
816 if (read_stdin) | |
817 { | |
818 #ifndef ALWAYS_USE_GUI | |
819 mch_msg(_("Vim: Reading from stdin...\n")); | |
820 #endif | |
821 #ifdef FEAT_GUI | |
822 /* Also write a message in the GUI window, if there is one. */ | |
823 if (gui.in_use && !gui.dying && !gui.starting) | |
824 { | |
825 p = (char_u *)_("Reading from stdin..."); | |
826 gui_write(p, (int)STRLEN(p)); | |
827 } | |
828 #endif | |
829 } | |
830 else if (!read_buffer) | |
831 filemess(curbuf, sfname, (char_u *)"", 0); | |
832 } | |
833 | |
834 msg_scroll = FALSE; /* overwrite the file message */ | |
835 | |
836 /* | |
837 * Set linecnt now, before the "retry" caused by a wrong guess for | |
838 * fileformat, and after the autocommands, which may change them. | |
839 */ | |
840 linecnt = curbuf->b_ml.ml_line_count; | |
841 | |
842 #ifdef FEAT_MBYTE | |
595 | 843 /* "++bad=" argument. */ |
844 if (eap != NULL && eap->bad_char != 0) | |
612 | 845 { |
595 | 846 bad_char_behavior = eap->bad_char; |
819 | 847 if (set_options) |
612 | 848 curbuf->b_bad_char = eap->bad_char; |
849 } | |
850 else | |
851 curbuf->b_bad_char = 0; | |
595 | 852 |
7 | 853 /* |
595 | 854 * Decide which 'encoding' to use or use first. |
7 | 855 */ |
856 if (eap != NULL && eap->force_enc != 0) | |
857 { | |
858 fenc = enc_canonize(eap->cmd + eap->force_enc); | |
859 fenc_alloced = TRUE; | |
595 | 860 keep_dest_enc = TRUE; |
7 | 861 } |
862 else if (curbuf->b_p_bin) | |
863 { | |
864 fenc = (char_u *)""; /* binary: don't convert */ | |
865 fenc_alloced = FALSE; | |
866 } | |
867 else if (curbuf->b_help) | |
868 { | |
869 char_u firstline[80]; | |
301 | 870 int fc; |
7 | 871 |
872 /* Help files are either utf-8 or latin1. Try utf-8 first, if this | |
873 * fails it must be latin1. | |
874 * Always do this when 'encoding' is "utf-8". Otherwise only do | |
875 * this when needed to avoid [converted] remarks all the time. | |
876 * It is needed when the first line contains non-ASCII characters. | |
877 * That is only in *.??x files. */ | |
878 fenc = (char_u *)"latin1"; | |
879 c = enc_utf8; | |
301 | 880 if (!c && !read_stdin) |
881 { | |
882 fc = fname[STRLEN(fname) - 1]; | |
883 if (TOLOWER_ASC(fc) == 'x') | |
884 { | |
885 /* Read the first line (and a bit more). Immediately rewind to | |
886 * the start of the file. If the read() fails "len" is -1. */ | |
2664 | 887 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
|
888 vim_lseek(fd, (off_T)0L, SEEK_SET); |
301 | 889 for (p = firstline; p < firstline + len; ++p) |
890 if (*p >= 0x80) | |
891 { | |
892 c = TRUE; | |
893 break; | |
894 } | |
895 } | |
7 | 896 } |
897 | |
898 if (c) | |
899 { | |
900 fenc_next = fenc; | |
901 fenc = (char_u *)"utf-8"; | |
902 | |
903 /* When the file is utf-8 but a character doesn't fit in | |
904 * 'encoding' don't retry. In help text editing utf-8 bytes | |
905 * doesn't make sense. */ | |
844 | 906 if (!enc_utf8) |
907 keep_dest_enc = TRUE; | |
7 | 908 } |
909 fenc_alloced = FALSE; | |
910 } | |
911 else if (*p_fencs == NUL) | |
912 { | |
913 fenc = curbuf->b_p_fenc; /* use format from buffer */ | |
914 fenc_alloced = FALSE; | |
915 } | |
916 else | |
917 { | |
918 fenc_next = p_fencs; /* try items in 'fileencodings' */ | |
919 fenc = next_fenc(&fenc_next); | |
920 fenc_alloced = TRUE; | |
921 } | |
922 #endif | |
923 | |
924 /* | |
925 * Jump back here to retry reading the file in different ways. | |
926 * Reasons to retry: | |
927 * - encoding conversion failed: try another one from "fenc_next" | |
928 * - BOM detected and fenc was set, need to setup conversion | |
929 * - "fileformat" check failed: try another | |
930 * | |
931 * Variables set for special retry actions: | |
932 * "file_rewind" Rewind the file to start reading it again. | |
933 * "advance_fenc" Advance "fenc" using "fenc_next". | |
934 * "skip_read" Re-use already read bytes (BOM detected). | |
935 * "did_iconv" iconv() conversion failed, try 'charconvert'. | |
936 * "keep_fileformat" Don't reset "fileformat". | |
937 * | |
938 * Other status indicators: | |
939 * "tmpname" When != NULL did conversion with 'charconvert'. | |
940 * Output file has to be deleted afterwards. | |
941 * "iconv_fd" When != -1 did conversion with iconv(). | |
942 */ | |
943 retry: | |
944 | |
945 if (file_rewind) | |
946 { | |
947 if (read_buffer) | |
948 { | |
949 read_buf_lnum = 1; | |
950 read_buf_col = 0; | |
951 } | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
952 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0) |
7 | 953 { |
954 /* Can't rewind the file, give up. */ | |
955 error = TRUE; | |
956 goto failed; | |
957 } | |
958 /* Delete the previously read lines. */ | |
959 while (lnum > from) | |
960 ml_delete(lnum--, FALSE); | |
961 file_rewind = FALSE; | |
962 #ifdef FEAT_MBYTE | |
819 | 963 if (set_options) |
1352 | 964 { |
7 | 965 curbuf->b_p_bomb = FALSE; |
1352 | 966 curbuf->b_start_bomb = FALSE; |
967 } | |
595 | 968 conv_error = 0; |
7 | 969 #endif |
970 } | |
971 | |
972 /* | |
973 * When retrying with another "fenc" and the first time "fileformat" | |
974 * will be reset. | |
975 */ | |
976 if (keep_fileformat) | |
977 keep_fileformat = FALSE; | |
978 else | |
979 { | |
980 if (eap != NULL && eap->force_ff != 0) | |
1742 | 981 { |
7 | 982 fileformat = get_fileformat_force(curbuf, eap); |
1742 | 983 try_unix = try_dos = try_mac = FALSE; |
984 } | |
7 | 985 else if (curbuf->b_p_bin) |
986 fileformat = EOL_UNIX; /* binary: use Unix format */ | |
987 else if (*p_ffs == NUL) | |
988 fileformat = get_fileformat(curbuf);/* use format from buffer */ | |
989 else | |
990 fileformat = EOL_UNKNOWN; /* detect from file */ | |
991 } | |
992 | |
993 #ifdef FEAT_MBYTE | |
994 # ifdef USE_ICONV | |
995 if (iconv_fd != (iconv_t)-1) | |
996 { | |
997 /* aborted conversion with iconv(), close the descriptor */ | |
998 iconv_close(iconv_fd); | |
999 iconv_fd = (iconv_t)-1; | |
1000 } | |
1001 # endif | |
1002 | |
1003 if (advance_fenc) | |
1004 { | |
1005 /* | |
1006 * Try the next entry in 'fileencodings'. | |
1007 */ | |
1008 advance_fenc = FALSE; | |
1009 | |
1010 if (eap != NULL && eap->force_enc != 0) | |
1011 { | |
1012 /* Conversion given with "++cc=" wasn't possible, read | |
1013 * without conversion. */ | |
1014 notconverted = TRUE; | |
595 | 1015 conv_error = 0; |
7 | 1016 if (fenc_alloced) |
1017 vim_free(fenc); | |
1018 fenc = (char_u *)""; | |
1019 fenc_alloced = FALSE; | |
1020 } | |
1021 else | |
1022 { | |
1023 if (fenc_alloced) | |
1024 vim_free(fenc); | |
1025 if (fenc_next != NULL) | |
1026 { | |
1027 fenc = next_fenc(&fenc_next); | |
1028 fenc_alloced = (fenc_next != NULL); | |
1029 } | |
1030 else | |
1031 { | |
1032 fenc = (char_u *)""; | |
1033 fenc_alloced = FALSE; | |
1034 } | |
1035 } | |
1036 if (tmpname != NULL) | |
1037 { | |
1038 mch_remove(tmpname); /* delete converted file */ | |
1039 vim_free(tmpname); | |
1040 tmpname = NULL; | |
1041 } | |
1042 } | |
1043 | |
1044 /* | |
1948 | 1045 * Conversion may be required when the encoding of the file is different |
1046 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4. | |
7 | 1047 */ |
1048 fio_flags = 0; | |
1948 | 1049 converted = need_conversion(fenc); |
1050 if (converted) | |
7 | 1051 { |
1052 | |
1053 /* "ucs-bom" means we need to check the first bytes of the file | |
1054 * for a BOM. */ | |
1055 if (STRCMP(fenc, ENC_UCSBOM) == 0) | |
1056 fio_flags = FIO_UCSBOM; | |
1057 | |
1058 /* | |
1059 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be | |
1060 * done. This is handled below after read(). Prepare the | |
1061 * fio_flags to avoid having to parse the string each time. | |
1062 * Also check for Unicode to Latin1 conversion, because iconv() | |
1063 * appears not to handle this correctly. This works just like | |
1064 * conversion to UTF-8 except how the resulting character is put in | |
1065 * the buffer. | |
1066 */ | |
1067 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
1068 fio_flags = get_fio_flags(fenc); | |
1069 | |
1070 # ifdef WIN3264 | |
1071 /* | |
1072 * Conversion from an MS-Windows codepage to UTF-8 or another codepage | |
1073 * is handled with MultiByteToWideChar(). | |
1074 */ | |
1075 if (fio_flags == 0) | |
1076 fio_flags = get_win_fio_flags(fenc); | |
1077 # endif | |
1078 | |
1079 # ifdef MACOS_X | |
1080 /* Conversion from Apple MacRoman to latin1 or UTF-8 */ | |
1081 if (fio_flags == 0) | |
1082 fio_flags = get_mac_fio_flags(fenc); | |
1083 # endif | |
1084 | |
1085 # ifdef USE_ICONV | |
1086 /* | |
1087 * Try using iconv() if we can't convert internally. | |
1088 */ | |
1089 if (fio_flags == 0 | |
1090 # ifdef FEAT_EVAL | |
1091 && !did_iconv | |
1092 # endif | |
1093 ) | |
1094 iconv_fd = (iconv_t)my_iconv_open( | |
1095 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc); | |
1096 # endif | |
1097 | |
1098 # ifdef FEAT_EVAL | |
1099 /* | |
1100 * Use the 'charconvert' expression when conversion is required | |
1101 * and we can't do it internally or with iconv(). | |
1102 */ | |
1103 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL | |
1104 # ifdef USE_ICONV | |
1105 && iconv_fd == (iconv_t)-1 | |
1106 # endif | |
1107 ) | |
1108 { | |
1109 # ifdef USE_ICONV | |
1110 did_iconv = FALSE; | |
1111 # endif | |
1112 /* Skip conversion when it's already done (retry for wrong | |
1113 * "fileformat"). */ | |
1114 if (tmpname == NULL) | |
1115 { | |
1116 tmpname = readfile_charconvert(fname, fenc, &fd); | |
1117 if (tmpname == NULL) | |
1118 { | |
1119 /* Conversion failed. Try another one. */ | |
1120 advance_fenc = TRUE; | |
1121 if (fd < 0) | |
1122 { | |
1123 /* Re-opening the original file failed! */ | |
1124 EMSG(_("E202: Conversion made file unreadable!")); | |
1125 error = TRUE; | |
1126 goto failed; | |
1127 } | |
1128 goto retry; | |
1129 } | |
1130 } | |
1131 } | |
1132 else | |
1133 # endif | |
1134 { | |
1135 if (fio_flags == 0 | |
1136 # ifdef USE_ICONV | |
1137 && iconv_fd == (iconv_t)-1 | |
1138 # endif | |
1139 ) | |
1140 { | |
1141 /* Conversion wanted but we can't. | |
1142 * Try the next conversion in 'fileencodings' */ | |
1143 advance_fenc = TRUE; | |
1144 goto retry; | |
1145 } | |
1146 } | |
1147 } | |
1148 | |
595 | 1149 /* Set "can_retry" when it's possible to rewind the file and try with |
7 | 1150 * another "fenc" value. It's FALSE when no other "fenc" to try, reading |
595 | 1151 * stdin or fixed at a specific encoding. */ |
1152 can_retry = (*fenc != NUL && !read_stdin && !keep_dest_enc); | |
7 | 1153 #endif |
1154 | |
1155 if (!skip_read) | |
1156 { | |
1157 linerest = 0; | |
1158 filesize = 0; | |
1159 skip_count = lines_to_skip; | |
1160 read_count = lines_to_read; | |
1161 #ifdef FEAT_MBYTE | |
1162 conv_restlen = 0; | |
1163 #endif | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1164 #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
|
1165 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
|
1166 && 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
|
1167 && 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
|
1168 && !filtering |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1169 && !read_stdin |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1170 && !read_buffer); |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1171 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
|
1172 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
|
1173 #endif |
6122 | 1174 #ifdef FEAT_CRYPT |
1175 if (curbuf->b_cryptstate != NULL) | |
1176 { | |
1177 /* Need to free the state, but keep the key, don't want to ask for | |
1178 * it again. */ | |
1179 crypt_free_state(curbuf->b_cryptstate); | |
1180 curbuf->b_cryptstate = NULL; | |
1181 } | |
1182 #endif | |
7 | 1183 } |
1184 | |
1185 while (!error && !got_int) | |
1186 { | |
1187 /* | |
1188 * We allocate as much space for the file as we can get, plus | |
1189 * space for the old line plus room for one terminating NUL. | |
1190 * The amount is limited by the fact that read() only can read | |
1191 * upto max_unsigned characters (and other things). | |
1192 */ | |
5684 | 1193 #if VIM_SIZEOF_INT <= 2 |
7 | 1194 if (linerest >= 0x7ff0) |
1195 { | |
1196 ++split; | |
1197 *ptr = NL; /* split line by inserting a NL */ | |
1198 size = 1; | |
1199 } | |
1200 else | |
1201 #endif | |
1202 { | |
1203 if (!skip_read) | |
1204 { | |
5684 | 1205 #if VIM_SIZEOF_INT > 2 |
1076 | 1206 # if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L) |
7 | 1207 size = SSIZE_MAX; /* use max I/O size, 52K */ |
1208 # else | |
1209 size = 0x10000L; /* use buffer >= 64K */ | |
1210 # endif | |
1211 #else | |
1212 size = 0x7ff0L - linerest; /* limit buffer to 32K */ | |
1213 #endif | |
1214 | |
836 | 1215 for ( ; size >= 10; size = (long)((long_u)size >> 1)) |
7 | 1216 { |
1217 if ((new_buffer = lalloc((long_u)(size + linerest + 1), | |
1218 FALSE)) != NULL) | |
1219 break; | |
1220 } | |
1221 if (new_buffer == NULL) | |
1222 { | |
1223 do_outofmem_msg((long_u)(size * 2 + linerest + 1)); | |
1224 error = TRUE; | |
1225 break; | |
1226 } | |
1227 if (linerest) /* copy characters from the previous buffer */ | |
1228 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest); | |
1229 vim_free(buffer); | |
1230 buffer = new_buffer; | |
1231 ptr = buffer + linerest; | |
1232 line_start = buffer; | |
1233 | |
1234 #ifdef FEAT_MBYTE | |
1235 /* May need room to translate into. | |
1236 * For iconv() we don't really know the required space, use a | |
1237 * factor ICONV_MULT. | |
1238 * latin1 to utf-8: 1 byte becomes up to 2 bytes | |
1239 * utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes | |
1240 * become up to 4 bytes, size must be multiple of 2 | |
1241 * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be | |
1242 * multiple of 2 | |
1243 * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be | |
1244 * multiple of 4 */ | |
835 | 1245 real_size = (int)size; |
7 | 1246 # ifdef USE_ICONV |
1247 if (iconv_fd != (iconv_t)-1) | |
1248 size = size / ICONV_MULT; | |
1249 else | |
1250 # endif | |
1251 if (fio_flags & FIO_LATIN1) | |
1252 size = size / 2; | |
1253 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1254 size = (size * 2 / 3) & ~1; | |
1255 else if (fio_flags & FIO_UCS4) | |
1256 size = (size * 2 / 3) & ~3; | |
1257 else if (fio_flags == FIO_UCSBOM) | |
1258 size = size / ICONV_MULT; /* worst case */ | |
1259 # ifdef WIN3264 | |
1260 else if (fio_flags & FIO_CODEPAGE) | |
1261 size = size / ICONV_MULT; /* also worst case */ | |
1262 # endif | |
1263 # ifdef MACOS_X | |
1264 else if (fio_flags & FIO_MACROMAN) | |
1265 size = size / ICONV_MULT; /* also worst case */ | |
1266 # endif | |
1267 #endif | |
1268 | |
1269 #ifdef FEAT_MBYTE | |
1270 if (conv_restlen > 0) | |
1271 { | |
1272 /* Insert unconverted bytes from previous line. */ | |
1273 mch_memmove(ptr, conv_rest, conv_restlen); | |
1274 ptr += conv_restlen; | |
1275 size -= conv_restlen; | |
1276 } | |
1277 #endif | |
1278 | |
1279 if (read_buffer) | |
1280 { | |
1281 /* | |
1282 * Read bytes from curbuf. Used for converting text read | |
1283 * from stdin. | |
1284 */ | |
1285 if (read_buf_lnum > from) | |
1286 size = 0; | |
1287 else | |
1288 { | |
1289 int n, ni; | |
1290 long tlen; | |
1291 | |
1292 tlen = 0; | |
1293 for (;;) | |
1294 { | |
1295 p = ml_get(read_buf_lnum) + read_buf_col; | |
1296 n = (int)STRLEN(p); | |
1297 if ((int)tlen + n + 1 > size) | |
1298 { | |
1299 /* Filled up to "size", append partial line. | |
1300 * Change NL to NUL to reverse the effect done | |
1301 * below. */ | |
835 | 1302 n = (int)(size - tlen); |
7 | 1303 for (ni = 0; ni < n; ++ni) |
1304 { | |
1305 if (p[ni] == NL) | |
1306 ptr[tlen++] = NUL; | |
1307 else | |
1308 ptr[tlen++] = p[ni]; | |
1309 } | |
1310 read_buf_col += n; | |
1311 break; | |
1312 } | |
1313 else | |
1314 { | |
1315 /* Append whole line and new-line. Change NL | |
1316 * to NUL to reverse the effect done below. */ | |
1317 for (ni = 0; ni < n; ++ni) | |
1318 { | |
1319 if (p[ni] == NL) | |
1320 ptr[tlen++] = NUL; | |
1321 else | |
1322 ptr[tlen++] = p[ni]; | |
1323 } | |
1324 ptr[tlen++] = NL; | |
1325 read_buf_col = 0; | |
1326 if (++read_buf_lnum > from) | |
1327 { | |
1328 /* When the last line didn't have an | |
1329 * end-of-line don't add it now either. */ | |
1330 if (!curbuf->b_p_eol) | |
1331 --tlen; | |
1332 size = tlen; | |
1333 break; | |
1334 } | |
1335 } | |
1336 } | |
1337 } | |
1338 } | |
1339 else | |
1340 { | |
1341 /* | |
1342 * Read bytes from the file. | |
1343 */ | |
2664 | 1344 size = read_eintr(fd, ptr, size); |
7 | 1345 } |
1346 | |
6122 | 1347 #ifdef FEAT_CRYPT |
1348 /* | |
1349 * At start of file: Check for magic number of encryption. | |
1350 */ | |
1351 if (filesize == 0 && size > 0) | |
1352 cryptkey = check_for_cryptkey(cryptkey, ptr, &size, | |
1353 &filesize, newfile, sfname, | |
1354 &did_ask_for_key); | |
1355 /* | |
1356 * Decrypt the read bytes. This is done before checking for | |
1357 * EOF because the crypt layer may be buffering. | |
1358 */ | |
1359 if (cryptkey != NULL && size > 0) | |
1360 { | |
1361 if (crypt_works_inplace(curbuf->b_cryptstate)) | |
1362 { | |
1363 crypt_decode_inplace(curbuf->b_cryptstate, ptr, size); | |
1364 } | |
1365 else | |
1366 { | |
1367 char_u *newptr = NULL; | |
1368 int decrypted_size; | |
1369 | |
1370 decrypted_size = crypt_decode_alloc( | |
1371 curbuf->b_cryptstate, ptr, size, &newptr); | |
1372 | |
1373 /* If the crypt layer is buffering, not producing | |
1374 * anything yet, need to read more. */ | |
1375 if (size > 0 && decrypted_size == 0) | |
1376 continue; | |
1377 | |
1378 if (linerest == 0) | |
1379 { | |
1380 /* Simple case: reuse returned buffer (may be | |
1381 * NULL, checked later). */ | |
1382 new_buffer = newptr; | |
1383 } | |
1384 else | |
1385 { | |
1386 long_u new_size; | |
1387 | |
1388 /* Need new buffer to add bytes carried over. */ | |
1389 new_size = (long_u)(decrypted_size + linerest + 1); | |
1390 new_buffer = lalloc(new_size, FALSE); | |
1391 if (new_buffer == NULL) | |
1392 { | |
1393 do_outofmem_msg(new_size); | |
1394 error = TRUE; | |
1395 break; | |
1396 } | |
1397 | |
1398 mch_memmove(new_buffer, buffer, linerest); | |
1399 if (newptr != NULL) | |
1400 mch_memmove(new_buffer + linerest, newptr, | |
1401 decrypted_size); | |
1402 } | |
1403 | |
1404 if (new_buffer != NULL) | |
1405 { | |
1406 vim_free(buffer); | |
1407 buffer = new_buffer; | |
1408 new_buffer = NULL; | |
1409 line_start = buffer; | |
1410 ptr = buffer + linerest; | |
1411 } | |
1412 size = decrypted_size; | |
1413 } | |
1414 } | |
1415 #endif | |
1416 | |
7 | 1417 if (size <= 0) |
1418 { | |
1419 if (size < 0) /* read error */ | |
1420 error = TRUE; | |
1421 #ifdef FEAT_MBYTE | |
1422 else if (conv_restlen > 0) | |
595 | 1423 { |
1597 | 1424 /* |
1425 * Reached end-of-file but some trailing bytes could | |
1426 * not be converted. Truncated file? | |
1427 */ | |
1428 | |
1429 /* When we did a conversion report an error. */ | |
1430 if (fio_flags != 0 | |
1431 # ifdef USE_ICONV | |
1432 || iconv_fd != (iconv_t)-1 | |
1433 # endif | |
1434 ) | |
1435 { | |
4331 | 1436 if (can_retry) |
1437 goto rewind_retry; | |
1597 | 1438 if (conv_error == 0) |
1439 conv_error = curbuf->b_ml.ml_line_count | |
1440 - linecnt + 1; | |
1441 } | |
1442 /* Remember the first linenr with an illegal byte */ | |
1443 else if (illegal_byte == 0) | |
1444 illegal_byte = curbuf->b_ml.ml_line_count | |
1445 - linecnt + 1; | |
1446 if (bad_char_behavior == BAD_DROP) | |
595 | 1447 { |
1597 | 1448 *(ptr - conv_restlen) = NUL; |
1449 conv_restlen = 0; | |
1450 } | |
1451 else | |
1452 { | |
1453 /* Replace the trailing bytes with the replacement | |
1454 * character if we were converting; if we weren't, | |
1455 * leave the UTF8 checking code to do it, as it | |
1456 * works slightly differently. */ | |
1457 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0 | |
1458 # ifdef USE_ICONV | |
1459 || iconv_fd != (iconv_t)-1 | |
1460 # endif | |
1461 )) | |
1462 { | |
1463 while (conv_restlen > 0) | |
1464 { | |
1465 *(--ptr) = bad_char_behavior; | |
1466 --conv_restlen; | |
1467 } | |
1468 } | |
595 | 1469 fio_flags = 0; /* don't convert this */ |
834 | 1470 # ifdef USE_ICONV |
1471 if (iconv_fd != (iconv_t)-1) | |
1472 { | |
1473 iconv_close(iconv_fd); | |
1474 iconv_fd = (iconv_t)-1; | |
1475 } | |
1476 # endif | |
595 | 1477 } |
1478 } | |
7 | 1479 #endif |
1480 } | |
1481 } | |
1482 skip_read = FALSE; | |
1483 | |
1484 #ifdef FEAT_MBYTE | |
1485 /* | |
1486 * At start of file (or after crypt magic number): Check for BOM. | |
1487 * Also check for a BOM for other Unicode encodings, but not after | |
1488 * converting with 'charconvert' or when a BOM has already been | |
1489 * found. | |
1490 */ | |
1491 if ((filesize == 0 | |
1492 # ifdef FEAT_CRYPT | |
6122 | 1493 || (cryptkey != NULL |
1494 && filesize == crypt_get_header_len( | |
1495 crypt_get_method_nr(curbuf))) | |
7 | 1496 # endif |
1497 ) | |
1498 && (fio_flags == FIO_UCSBOM | |
1499 || (!curbuf->b_p_bomb | |
1500 && tmpname == NULL | |
1501 && (*fenc == 'u' || (*fenc == NUL && enc_utf8))))) | |
1502 { | |
1503 char_u *ccname; | |
1504 int blen; | |
1505 | |
1506 /* no BOM detection in a short file or in binary mode */ | |
1507 if (size < 2 || curbuf->b_p_bin) | |
1508 ccname = NULL; | |
1509 else | |
1510 ccname = check_for_bom(ptr, size, &blen, | |
1511 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc)); | |
1512 if (ccname != NULL) | |
1513 { | |
1514 /* Remove BOM from the text */ | |
1515 filesize += blen; | |
1516 size -= blen; | |
1517 mch_memmove(ptr, ptr + blen, (size_t)size); | |
819 | 1518 if (set_options) |
1352 | 1519 { |
7 | 1520 curbuf->b_p_bomb = TRUE; |
1352 | 1521 curbuf->b_start_bomb = TRUE; |
1522 } | |
7 | 1523 } |
1524 | |
1525 if (fio_flags == FIO_UCSBOM) | |
1526 { | |
1527 if (ccname == NULL) | |
1528 { | |
1529 /* No BOM detected: retry with next encoding. */ | |
1530 advance_fenc = TRUE; | |
1531 } | |
1532 else | |
1533 { | |
1534 /* BOM detected: set "fenc" and jump back */ | |
1535 if (fenc_alloced) | |
1536 vim_free(fenc); | |
1537 fenc = ccname; | |
1538 fenc_alloced = FALSE; | |
1539 } | |
1540 /* retry reading without getting new bytes or rewinding */ | |
1541 skip_read = TRUE; | |
1542 goto retry; | |
1543 } | |
1544 } | |
1597 | 1545 |
1546 /* Include not converted bytes. */ | |
1547 ptr -= conv_restlen; | |
1548 size += conv_restlen; | |
1549 conv_restlen = 0; | |
7 | 1550 #endif |
1551 /* | |
1552 * Break here for a read error or end-of-file. | |
1553 */ | |
1554 if (size <= 0) | |
1555 break; | |
1556 | |
1557 #ifdef FEAT_MBYTE | |
1558 | |
1559 # ifdef USE_ICONV | |
1560 if (iconv_fd != (iconv_t)-1) | |
1561 { | |
1562 /* | |
1563 * Attempt conversion of the read bytes to 'encoding' using | |
1564 * iconv(). | |
1565 */ | |
1566 const char *fromp; | |
1567 char *top; | |
1568 size_t from_size; | |
1569 size_t to_size; | |
1570 | |
1571 fromp = (char *)ptr; | |
1572 from_size = size; | |
1573 ptr += size; | |
1574 top = (char *)ptr; | |
1575 to_size = real_size - size; | |
1576 | |
1577 /* | |
1578 * If there is conversion error or not enough room try using | |
179 | 1579 * another conversion. Except for when there is no |
1580 * alternative (help files). | |
7 | 1581 */ |
177 | 1582 while ((iconv(iconv_fd, (void *)&fromp, &from_size, |
1583 &top, &to_size) | |
7 | 1584 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) |
1585 || from_size > CONV_RESTLEN) | |
177 | 1586 { |
595 | 1587 if (can_retry) |
375 | 1588 goto rewind_retry; |
595 | 1589 if (conv_error == 0) |
1590 conv_error = readfile_linenr(linecnt, | |
1591 ptr, (char_u *)top); | |
1592 | |
1593 /* Deal with a bad byte and continue with the next. */ | |
177 | 1594 ++fromp; |
1595 --from_size; | |
595 | 1596 if (bad_char_behavior == BAD_KEEP) |
1597 { | |
1598 *top++ = *(fromp - 1); | |
1599 --to_size; | |
1600 } | |
1601 else if (bad_char_behavior != BAD_DROP) | |
1602 { | |
1603 *top++ = bad_char_behavior; | |
1604 --to_size; | |
1605 } | |
177 | 1606 } |
7 | 1607 |
1608 if (from_size > 0) | |
1609 { | |
1610 /* Some remaining characters, keep them for the next | |
1611 * round. */ | |
1612 mch_memmove(conv_rest, (char_u *)fromp, from_size); | |
1613 conv_restlen = (int)from_size; | |
1614 } | |
1615 | |
1616 /* move the linerest to before the converted characters */ | |
1617 line_start = ptr - linerest; | |
1618 mch_memmove(line_start, buffer, (size_t)linerest); | |
1619 size = (long)((char_u *)top - ptr); | |
1620 } | |
1621 # endif | |
1622 | |
1623 # ifdef WIN3264 | |
1624 if (fio_flags & FIO_CODEPAGE) | |
1625 { | |
595 | 1626 char_u *src, *dst; |
1627 WCHAR ucs2buf[3]; | |
1628 int ucs2len; | |
1629 int codepage = FIO_GET_CP(fio_flags); | |
1630 int bytelen; | |
1631 int found_bad; | |
1632 char replstr[2]; | |
1633 | |
7 | 1634 /* |
1635 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or | |
595 | 1636 * a codepage, using standard MS-Windows functions. This |
1637 * requires two steps: | |
1638 * 1. convert from 'fileencoding' to ucs-2 | |
1639 * 2. convert from ucs-2 to 'encoding' | |
1640 * | |
1641 * Because there may be illegal bytes AND an incomplete byte | |
1642 * sequence at the end, we may have to do the conversion one | |
1643 * character at a time to get it right. | |
7 | 1644 */ |
595 | 1645 |
1646 /* Replacement string for WideCharToMultiByte(). */ | |
1647 if (bad_char_behavior > 0) | |
1648 replstr[0] = bad_char_behavior; | |
1649 else | |
1650 replstr[0] = '?'; | |
1651 replstr[1] = NUL; | |
1652 | |
1653 /* | |
1654 * Move the bytes to the end of the buffer, so that we have | |
1655 * room to put the result at the start. | |
1656 */ | |
1657 src = ptr + real_size - size; | |
1658 mch_memmove(src, ptr, size); | |
7 | 1659 |
1660 /* | |
595 | 1661 * Do the conversion. |
7 | 1662 */ |
595 | 1663 dst = ptr; |
1664 size = size; | |
1665 while (size > 0) | |
7 | 1666 { |
595 | 1667 found_bad = FALSE; |
7 | 1668 |
1669 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
595 | 1670 if (codepage == CP_UTF8) |
7 | 1671 { |
595 | 1672 /* Handle CP_UTF8 input ourselves to be able to handle |
1673 * trailing bytes properly. | |
1674 * Get one UTF-8 character from src. */ | |
835 | 1675 bytelen = (int)utf_ptr2len_len(src, size); |
595 | 1676 if (bytelen > size) |
1677 { | |
1678 /* Only got some bytes of a character. Normally | |
1679 * it's put in "conv_rest", but if it's too long | |
1680 * deal with it as if they were illegal bytes. */ | |
1681 if (bytelen <= CONV_RESTLEN) | |
1682 break; | |
1683 | |
1684 /* weird overlong byte sequence */ | |
1685 bytelen = size; | |
1686 found_bad = TRUE; | |
1687 } | |
1688 else | |
1689 { | |
799 | 1690 int u8c = utf_ptr2char(src); |
1691 | |
622 | 1692 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1)) |
595 | 1693 found_bad = TRUE; |
1694 ucs2buf[0] = u8c; | |
1695 ucs2len = 1; | |
1696 } | |
7 | 1697 } |
595 | 1698 else |
7 | 1699 # endif |
595 | 1700 { |
1701 /* We don't know how long the byte sequence is, try | |
1702 * from one to three bytes. */ | |
1703 for (bytelen = 1; bytelen <= size && bytelen <= 3; | |
1704 ++bytelen) | |
1705 { | |
1706 ucs2len = MultiByteToWideChar(codepage, | |
1707 MB_ERR_INVALID_CHARS, | |
1708 (LPCSTR)src, bytelen, | |
1709 ucs2buf, 3); | |
1710 if (ucs2len > 0) | |
1711 break; | |
1712 } | |
1713 if (ucs2len == 0) | |
1714 { | |
1715 /* If we have only one byte then it's probably an | |
1716 * incomplete byte sequence. Otherwise discard | |
1717 * one byte as a bad character. */ | |
1718 if (size == 1) | |
1719 break; | |
1720 found_bad = TRUE; | |
1721 bytelen = 1; | |
1722 } | |
1723 } | |
1724 | |
1725 if (!found_bad) | |
7 | 1726 { |
595 | 1727 int i; |
1728 | |
1729 /* Convert "ucs2buf[ucs2len]" to 'enc' in "dst". */ | |
1730 if (enc_utf8) | |
1731 { | |
1732 /* From UCS-2 to UTF-8. Cannot fail. */ | |
1733 for (i = 0; i < ucs2len; ++i) | |
1734 dst += utf_char2bytes(ucs2buf[i], dst); | |
1735 } | |
1736 else | |
1737 { | |
1738 BOOL bad = FALSE; | |
1739 int dstlen; | |
1740 | |
1741 /* From UCS-2 to "enc_codepage". If the | |
1742 * conversion uses the default character "?", | |
1743 * the data doesn't fit in this encoding. */ | |
1744 dstlen = WideCharToMultiByte(enc_codepage, 0, | |
1745 (LPCWSTR)ucs2buf, ucs2len, | |
835 | 1746 (LPSTR)dst, (int)(src - dst), |
595 | 1747 replstr, &bad); |
1748 if (bad) | |
1749 found_bad = TRUE; | |
1750 else | |
1751 dst += dstlen; | |
1752 } | |
7 | 1753 } |
595 | 1754 |
1755 if (found_bad) | |
1756 { | |
1757 /* Deal with bytes we can't convert. */ | |
1758 if (can_retry) | |
1759 goto rewind_retry; | |
1760 if (conv_error == 0) | |
1761 conv_error = readfile_linenr(linecnt, ptr, dst); | |
1762 if (bad_char_behavior != BAD_DROP) | |
1763 { | |
1764 if (bad_char_behavior == BAD_KEEP) | |
1765 { | |
1766 mch_memmove(dst, src, bytelen); | |
1767 dst += bytelen; | |
1768 } | |
1769 else | |
1770 *dst++ = bad_char_behavior; | |
1771 } | |
1772 } | |
1773 | |
1774 src += bytelen; | |
1775 size -= bytelen; | |
7 | 1776 } |
595 | 1777 |
1778 if (size > 0) | |
7 | 1779 { |
595 | 1780 /* An incomplete byte sequence remaining. */ |
1781 mch_memmove(conv_rest, src, size); | |
1782 conv_restlen = size; | |
7 | 1783 } |
595 | 1784 |
1785 /* The new size is equal to how much "dst" was advanced. */ | |
835 | 1786 size = (long)(dst - ptr); |
7 | 1787 } |
1788 else | |
1789 # endif | |
765 | 1790 # ifdef MACOS_CONVERT |
7 | 1791 if (fio_flags & FIO_MACROMAN) |
1792 { | |
1793 /* | |
1794 * Conversion from Apple MacRoman char encoding to UTF-8 or | |
18 | 1795 * latin1. This is in os_mac_conv.c. |
7 | 1796 */ |
18 | 1797 if (macroman2enc(ptr, &size, real_size) == FAIL) |
7 | 1798 goto rewind_retry; |
1799 } | |
1800 else | |
1801 # endif | |
1802 if (fio_flags != 0) | |
1803 { | |
1804 int u8c; | |
1805 char_u *dest; | |
1806 char_u *tail = NULL; | |
1807 | |
1808 /* | |
1809 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8. | |
1810 * "enc_utf8" not set: Convert Unicode to Latin1. | |
1811 * Go from end to start through the buffer, because the number | |
1812 * of bytes may increase. | |
1813 * "dest" points to after where the UTF-8 bytes go, "p" points | |
1814 * to after the next character to convert. | |
1815 */ | |
1816 dest = ptr + real_size; | |
1817 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8) | |
1818 { | |
1819 p = ptr + size; | |
1820 if (fio_flags == FIO_UTF8) | |
1821 { | |
1822 /* Check for a trailing incomplete UTF-8 sequence */ | |
1823 tail = ptr + size - 1; | |
1824 while (tail > ptr && (*tail & 0xc0) == 0x80) | |
1825 --tail; | |
1826 if (tail + utf_byte2len(*tail) <= ptr + size) | |
1827 tail = NULL; | |
1828 else | |
1829 p = tail; | |
1830 } | |
1831 } | |
1832 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1833 { | |
1834 /* Check for a trailing byte */ | |
1835 p = ptr + (size & ~1); | |
1836 if (size & 1) | |
1837 tail = p; | |
1838 if ((fio_flags & FIO_UTF16) && p > ptr) | |
1839 { | |
1840 /* Check for a trailing leading word */ | |
1841 if (fio_flags & FIO_ENDIAN_L) | |
1842 { | |
1843 u8c = (*--p << 8); | |
1844 u8c += *--p; | |
1845 } | |
1846 else | |
1847 { | |
1848 u8c = *--p; | |
1849 u8c += (*--p << 8); | |
1850 } | |
1851 if (u8c >= 0xd800 && u8c <= 0xdbff) | |
1852 tail = p; | |
1853 else | |
1854 p += 2; | |
1855 } | |
1856 } | |
1857 else /* FIO_UCS4 */ | |
1858 { | |
1859 /* Check for trailing 1, 2 or 3 bytes */ | |
1860 p = ptr + (size & ~3); | |
1861 if (size & 3) | |
1862 tail = p; | |
1863 } | |
1864 | |
1865 /* If there is a trailing incomplete sequence move it to | |
1866 * conv_rest[]. */ | |
1867 if (tail != NULL) | |
1868 { | |
1869 conv_restlen = (int)((ptr + size) - tail); | |
1870 mch_memmove(conv_rest, (char_u *)tail, conv_restlen); | |
1871 size -= conv_restlen; | |
1872 } | |
1873 | |
1874 | |
1875 while (p > ptr) | |
1876 { | |
1877 if (fio_flags & FIO_LATIN1) | |
1878 u8c = *--p; | |
1879 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1880 { | |
1881 if (fio_flags & FIO_ENDIAN_L) | |
1882 { | |
1883 u8c = (*--p << 8); | |
1884 u8c += *--p; | |
1885 } | |
1886 else | |
1887 { | |
1888 u8c = *--p; | |
1889 u8c += (*--p << 8); | |
1890 } | |
1891 if ((fio_flags & FIO_UTF16) | |
1892 && u8c >= 0xdc00 && u8c <= 0xdfff) | |
1893 { | |
1894 int u16c; | |
1895 | |
1896 if (p == ptr) | |
1897 { | |
1898 /* Missing leading word. */ | |
1899 if (can_retry) | |
1900 goto rewind_retry; | |
595 | 1901 if (conv_error == 0) |
1902 conv_error = readfile_linenr(linecnt, | |
1903 ptr, p); | |
1904 if (bad_char_behavior == BAD_DROP) | |
1905 continue; | |
1906 if (bad_char_behavior != BAD_KEEP) | |
1907 u8c = bad_char_behavior; | |
7 | 1908 } |
1909 | |
1910 /* found second word of double-word, get the first | |
1911 * word and compute the resulting character */ | |
1912 if (fio_flags & FIO_ENDIAN_L) | |
1913 { | |
1914 u16c = (*--p << 8); | |
1915 u16c += *--p; | |
1916 } | |
1917 else | |
1918 { | |
1919 u16c = *--p; | |
1920 u16c += (*--p << 8); | |
1921 } | |
595 | 1922 u8c = 0x10000 + ((u16c & 0x3ff) << 10) |
1923 + (u8c & 0x3ff); | |
1924 | |
7 | 1925 /* Check if the word is indeed a leading word. */ |
1926 if (u16c < 0xd800 || u16c > 0xdbff) | |
1927 { | |
1928 if (can_retry) | |
1929 goto rewind_retry; | |
595 | 1930 if (conv_error == 0) |
1931 conv_error = readfile_linenr(linecnt, | |
1932 ptr, p); | |
1933 if (bad_char_behavior == BAD_DROP) | |
1934 continue; | |
1935 if (bad_char_behavior != BAD_KEEP) | |
1936 u8c = bad_char_behavior; | |
7 | 1937 } |
1938 } | |
1939 } | |
1940 else if (fio_flags & FIO_UCS4) | |
1941 { | |
1942 if (fio_flags & FIO_ENDIAN_L) | |
1943 { | |
1944 u8c = (*--p << 24); | |
1945 u8c += (*--p << 16); | |
1946 u8c += (*--p << 8); | |
1947 u8c += *--p; | |
1948 } | |
1949 else /* big endian */ | |
1950 { | |
1951 u8c = *--p; | |
1952 u8c += (*--p << 8); | |
1953 u8c += (*--p << 16); | |
1954 u8c += (*--p << 24); | |
1955 } | |
1956 } | |
1957 else /* UTF-8 */ | |
1958 { | |
1959 if (*--p < 0x80) | |
1960 u8c = *p; | |
1961 else | |
1962 { | |
1963 len = utf_head_off(ptr, p); | |
595 | 1964 p -= len; |
1965 u8c = utf_ptr2char(p); | |
7 | 1966 if (len == 0) |
1967 { | |
1968 /* Not a valid UTF-8 character, retry with | |
1969 * another fenc when possible, otherwise just | |
1970 * report the error. */ | |
1971 if (can_retry) | |
1972 goto rewind_retry; | |
595 | 1973 if (conv_error == 0) |
1974 conv_error = readfile_linenr(linecnt, | |
1975 ptr, p); | |
1976 if (bad_char_behavior == BAD_DROP) | |
1977 continue; | |
1978 if (bad_char_behavior != BAD_KEEP) | |
1979 u8c = bad_char_behavior; | |
7 | 1980 } |
1981 } | |
1982 } | |
1983 if (enc_utf8) /* produce UTF-8 */ | |
1984 { | |
1985 dest -= utf_char2len(u8c); | |
1986 (void)utf_char2bytes(u8c, dest); | |
1987 } | |
1988 else /* produce Latin1 */ | |
1989 { | |
1990 --dest; | |
1991 if (u8c >= 0x100) | |
1992 { | |
1993 /* character doesn't fit in latin1, retry with | |
1994 * another fenc when possible, otherwise just | |
1995 * report the error. */ | |
595 | 1996 if (can_retry) |
7 | 1997 goto rewind_retry; |
595 | 1998 if (conv_error == 0) |
1999 conv_error = readfile_linenr(linecnt, ptr, p); | |
2000 if (bad_char_behavior == BAD_DROP) | |
2001 ++dest; | |
2002 else if (bad_char_behavior == BAD_KEEP) | |
2003 *dest = u8c; | |
2004 else if (eap != NULL && eap->bad_char != 0) | |
2005 *dest = bad_char_behavior; | |
2006 else | |
2007 *dest = 0xBF; | |
7 | 2008 } |
2009 else | |
2010 *dest = u8c; | |
2011 } | |
2012 } | |
2013 | |
2014 /* move the linerest to before the converted characters */ | |
2015 line_start = dest - linerest; | |
2016 mch_memmove(line_start, buffer, (size_t)linerest); | |
2017 size = (long)((ptr + real_size) - dest); | |
2018 ptr = dest; | |
2019 } | |
1597 | 2020 else if (enc_utf8 && !curbuf->b_p_bin) |
2021 { | |
2022 int incomplete_tail = FALSE; | |
2023 | |
2024 /* Reading UTF-8: Check if the bytes are valid UTF-8. */ | |
2025 for (p = ptr; ; ++p) | |
7 | 2026 { |
835 | 2027 int todo = (int)((ptr + size) - p); |
595 | 2028 int l; |
2029 | |
2030 if (todo <= 0) | |
2031 break; | |
7 | 2032 if (*p >= 0x80) |
2033 { | |
2034 /* A length of 1 means it's an illegal byte. Accept | |
2035 * an incomplete character at the end though, the next | |
2036 * read() will get the next bytes, we'll check it | |
2037 * then. */ | |
595 | 2038 l = utf_ptr2len_len(p, todo); |
1597 | 2039 if (l > todo && !incomplete_tail) |
7 | 2040 { |
1597 | 2041 /* Avoid retrying with a different encoding when |
2042 * a truncated file is more likely, or attempting | |
2043 * to read the rest of an incomplete sequence when | |
2044 * we have already done so. */ | |
2045 if (p > ptr || filesize > 0) | |
2046 incomplete_tail = TRUE; | |
2047 /* Incomplete byte sequence, move it to conv_rest[] | |
2048 * and try to read the rest of it, unless we've | |
2049 * already done so. */ | |
2050 if (p > ptr) | |
2051 { | |
2052 conv_restlen = todo; | |
2053 mch_memmove(conv_rest, p, conv_restlen); | |
2054 size -= conv_restlen; | |
2055 break; | |
2056 } | |
7 | 2057 } |
1597 | 2058 if (l == 1 || l > todo) |
595 | 2059 { |
2060 /* Illegal byte. If we can try another encoding | |
1597 | 2061 * do that, unless at EOF where a truncated |
2062 * file is more likely than a conversion error. */ | |
2063 if (can_retry && !incomplete_tail) | |
595 | 2064 break; |
2065 # ifdef USE_ICONV | |
2066 /* When we did a conversion report an error. */ | |
2067 if (iconv_fd != (iconv_t)-1 && conv_error == 0) | |
2068 conv_error = readfile_linenr(linecnt, ptr, p); | |
2069 # endif | |
1597 | 2070 /* Remember the first linenr with an illegal byte */ |
2071 if (conv_error == 0 && illegal_byte == 0) | |
2072 illegal_byte = readfile_linenr(linecnt, ptr, p); | |
595 | 2073 |
2074 /* Drop, keep or replace the bad byte. */ | |
2075 if (bad_char_behavior == BAD_DROP) | |
2076 { | |
1597 | 2077 mch_memmove(p, p + 1, todo - 1); |
595 | 2078 --p; |
2079 --size; | |
2080 } | |
2081 else if (bad_char_behavior != BAD_KEEP) | |
2082 *p = bad_char_behavior; | |
2083 } | |
1597 | 2084 else |
2085 p += l - 1; | |
7 | 2086 } |
2087 } | |
1597 | 2088 if (p < ptr + size && !incomplete_tail) |
7 | 2089 { |
2090 /* Detected a UTF-8 error. */ | |
2091 rewind_retry: | |
595 | 2092 /* Retry reading with another conversion. */ |
7 | 2093 # if defined(FEAT_EVAL) && defined(USE_ICONV) |
595 | 2094 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1) |
2095 /* iconv() failed, try 'charconvert' */ | |
2096 did_iconv = TRUE; | |
7 | 2097 else |
2098 # endif | |
595 | 2099 /* use next item from 'fileencodings' */ |
2100 advance_fenc = TRUE; | |
2101 file_rewind = TRUE; | |
2102 goto retry; | |
7 | 2103 } |
2104 } | |
2105 #endif | |
2106 | |
2107 /* count the number of characters (after conversion!) */ | |
2108 filesize += size; | |
2109 | |
2110 /* | |
2111 * when reading the first part of a file: guess EOL type | |
2112 */ | |
2113 if (fileformat == EOL_UNKNOWN) | |
2114 { | |
2115 /* First try finding a NL, for Dos and Unix */ | |
2116 if (try_dos || try_unix) | |
2117 { | |
6635 | 2118 /* Reset the carriage return counter. */ |
2119 if (try_mac) | |
2120 try_mac = 1; | |
2121 | |
7 | 2122 for (p = ptr; p < ptr + size; ++p) |
2123 { | |
2124 if (*p == NL) | |
2125 { | |
2126 if (!try_unix | |
2127 || (try_dos && p > ptr && p[-1] == CAR)) | |
2128 fileformat = EOL_DOS; | |
2129 else | |
2130 fileformat = EOL_UNIX; | |
2131 break; | |
2132 } | |
6618 | 2133 else if (*p == CAR && try_mac) |
2134 try_mac++; | |
7 | 2135 } |
2136 | |
2137 /* Don't give in to EOL_UNIX if EOL_MAC is more likely */ | |
2138 if (fileformat == EOL_UNIX && try_mac) | |
2139 { | |
2140 /* Need to reset the counters when retrying fenc. */ | |
2141 try_mac = 1; | |
2142 try_unix = 1; | |
2143 for (; p >= ptr && *p != CAR; p--) | |
2144 ; | |
2145 if (p >= ptr) | |
2146 { | |
2147 for (p = ptr; p < ptr + size; ++p) | |
2148 { | |
2149 if (*p == NL) | |
2150 try_unix++; | |
2151 else if (*p == CAR) | |
2152 try_mac++; | |
2153 } | |
2154 if (try_mac > try_unix) | |
2155 fileformat = EOL_MAC; | |
2156 } | |
2157 } | |
6618 | 2158 else if (fileformat == EOL_UNKNOWN && try_mac == 1) |
2159 /* Looking for CR but found no end-of-line markers at | |
2160 * all: use the default format. */ | |
2161 fileformat = default_fileformat(); | |
7 | 2162 } |
2163 | |
2164 /* No NL found: may use Mac format */ | |
2165 if (fileformat == EOL_UNKNOWN && try_mac) | |
2166 fileformat = EOL_MAC; | |
2167 | |
2168 /* Still nothing found? Use first format in 'ffs' */ | |
2169 if (fileformat == EOL_UNKNOWN) | |
2170 fileformat = default_fileformat(); | |
2171 | |
2172 /* if editing a new file: may set p_tx and p_ff */ | |
819 | 2173 if (set_options) |
7 | 2174 set_fileformat(fileformat, OPT_LOCAL); |
2175 } | |
2176 } | |
2177 | |
2178 /* | |
2179 * This loop is executed once for every character read. | |
2180 * Keep it fast! | |
2181 */ | |
2182 if (fileformat == EOL_MAC) | |
2183 { | |
2184 --ptr; | |
2185 while (++ptr, --size >= 0) | |
2186 { | |
2187 /* catch most common case first */ | |
2188 if ((c = *ptr) != NUL && c != CAR && c != NL) | |
2189 continue; | |
2190 if (c == NUL) | |
2191 *ptr = NL; /* NULs are replaced by newlines! */ | |
2192 else if (c == NL) | |
2193 *ptr = CAR; /* NLs are replaced by CRs! */ | |
2194 else | |
2195 { | |
2196 if (skip_count == 0) | |
2197 { | |
2198 *ptr = NUL; /* end of line */ | |
2199 len = (colnr_T) (ptr - line_start + 1); | |
2200 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2201 { | |
2202 error = TRUE; | |
2203 break; | |
2204 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2205 #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
|
2206 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
|
2207 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
|
2208 #endif |
7 | 2209 ++lnum; |
2210 if (--read_count == 0) | |
2211 { | |
2212 error = TRUE; /* break loop */ | |
2213 line_start = ptr; /* nothing left to write */ | |
2214 break; | |
2215 } | |
2216 } | |
2217 else | |
2218 --skip_count; | |
2219 line_start = ptr + 1; | |
2220 } | |
2221 } | |
2222 } | |
2223 else | |
2224 { | |
2225 --ptr; | |
2226 while (++ptr, --size >= 0) | |
2227 { | |
2228 if ((c = *ptr) != NUL && c != NL) /* catch most common case */ | |
2229 continue; | |
2230 if (c == NUL) | |
2231 *ptr = NL; /* NULs are replaced by newlines! */ | |
2232 else | |
2233 { | |
2234 if (skip_count == 0) | |
2235 { | |
2236 *ptr = NUL; /* end of line */ | |
2237 len = (colnr_T)(ptr - line_start + 1); | |
2238 if (fileformat == EOL_DOS) | |
2239 { | |
2240 if (ptr[-1] == CAR) /* remove CR */ | |
2241 { | |
2242 ptr[-1] = NUL; | |
2243 --len; | |
2244 } | |
2245 /* | |
2246 * Reading in Dos format, but no CR-LF found! | |
2247 * When 'fileformats' includes "unix", delete all | |
2248 * the lines read so far and start all over again. | |
2249 * Otherwise give an error message later. | |
2250 */ | |
2251 else if (ff_error != EOL_DOS) | |
2252 { | |
2253 if ( try_unix | |
2254 && !read_stdin | |
2255 && (read_buffer | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
2256 || 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
|
2257 == 0)) |
7 | 2258 { |
2259 fileformat = EOL_UNIX; | |
819 | 2260 if (set_options) |
7 | 2261 set_fileformat(EOL_UNIX, OPT_LOCAL); |
2262 file_rewind = TRUE; | |
2263 keep_fileformat = TRUE; | |
2264 goto retry; | |
2265 } | |
2266 ff_error = EOL_DOS; | |
2267 } | |
2268 } | |
2269 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2270 { | |
2271 error = TRUE; | |
2272 break; | |
2273 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2274 #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
|
2275 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
|
2276 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
|
2277 #endif |
7 | 2278 ++lnum; |
2279 if (--read_count == 0) | |
2280 { | |
2281 error = TRUE; /* break loop */ | |
2282 line_start = ptr; /* nothing left to write */ | |
2283 break; | |
2284 } | |
2285 } | |
2286 else | |
2287 --skip_count; | |
2288 line_start = ptr + 1; | |
2289 } | |
2290 } | |
2291 } | |
2292 linerest = (long)(ptr - line_start); | |
2293 ui_breakcheck(); | |
2294 } | |
2295 | |
2296 failed: | |
2297 /* not an error, max. number of lines reached */ | |
2298 if (error && read_count == 0) | |
2299 error = FALSE; | |
2300 | |
2301 /* | |
2302 * If we get EOF in the middle of a line, note the fact and | |
2303 * complete the line ourselves. | |
2304 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set. | |
2305 */ | |
2306 if (!error | |
2307 && !got_int | |
2308 && linerest != 0 | |
2309 && !(!curbuf->b_p_bin | |
2310 && fileformat == EOL_DOS | |
2311 && *line_start == Ctrl_Z | |
2312 && ptr == line_start + 1)) | |
2313 { | |
819 | 2314 /* remember for when writing */ |
2315 if (set_options) | |
7 | 2316 curbuf->b_p_eol = FALSE; |
2317 *ptr = NUL; | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2318 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
|
2319 if (ml_append(lnum, line_start, len, newfile) == FAIL) |
7 | 2320 error = TRUE; |
2321 else | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2322 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2323 #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
|
2324 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
|
2325 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
|
2326 #endif |
7 | 2327 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
|
2328 } |
7 | 2329 } |
2330 | |
819 | 2331 if (set_options) |
7 | 2332 save_file_ff(curbuf); /* remember the current file format */ |
2333 | |
2334 #ifdef FEAT_CRYPT | |
6122 | 2335 if (curbuf->b_cryptstate != NULL) |
2336 { | |
2337 crypt_free_state(curbuf->b_cryptstate); | |
2338 curbuf->b_cryptstate = NULL; | |
2339 } | |
2340 if (cryptkey != NULL && cryptkey != curbuf->b_p_key) | |
2341 crypt_free_key(cryptkey); | |
2342 /* Don't set cryptkey to NULL, it's used below as a flag that | |
2343 * encryption was used. */ | |
7 | 2344 #endif |
2345 | |
2346 #ifdef FEAT_MBYTE | |
819 | 2347 /* If editing a new file: set 'fenc' for the current buffer. |
2348 * Also for ":read ++edit file". */ | |
2349 if (set_options) | |
7 | 2350 set_string_option_direct((char_u *)"fenc", -1, fenc, |
694 | 2351 OPT_FREE|OPT_LOCAL, 0); |
7 | 2352 if (fenc_alloced) |
2353 vim_free(fenc); | |
2354 # ifdef USE_ICONV | |
2355 if (iconv_fd != (iconv_t)-1) | |
2356 { | |
2357 iconv_close(iconv_fd); | |
2358 iconv_fd = (iconv_t)-1; | |
2359 } | |
2360 # endif | |
2361 #endif | |
2362 | |
2363 if (!read_buffer && !read_stdin) | |
2364 close(fd); /* errors are ignored */ | |
2003 | 2365 #ifdef HAVE_FD_CLOEXEC |
2366 else | |
2367 { | |
2368 int fdflags = fcntl(fd, F_GETFD); | |
2369 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
|
2370 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC); |
2003 | 2371 } |
2372 #endif | |
7 | 2373 vim_free(buffer); |
2374 | |
2375 #ifdef HAVE_DUP | |
2376 if (read_stdin) | |
2377 { | |
2378 /* Use stderr for stdin, makes shell commands work. */ | |
2379 close(0); | |
1757 | 2380 ignored = dup(2); |
7 | 2381 } |
2382 #endif | |
2383 | |
2384 #ifdef FEAT_MBYTE | |
2385 if (tmpname != NULL) | |
2386 { | |
2387 mch_remove(tmpname); /* delete converted file */ | |
2388 vim_free(tmpname); | |
2389 } | |
2390 #endif | |
2391 --no_wait_return; /* may wait for return now */ | |
2392 | |
2393 /* | |
2394 * In recovery mode everything but autocommands is skipped. | |
2395 */ | |
2396 if (!recoverymode) | |
2397 { | |
2398 /* need to delete the last line, which comes from the empty buffer */ | |
2399 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY)) | |
2400 { | |
2401 #ifdef FEAT_NETBEANS_INTG | |
2402 netbeansFireChanges = 0; | |
2403 #endif | |
2404 ml_delete(curbuf->b_ml.ml_line_count, FALSE); | |
2405 #ifdef FEAT_NETBEANS_INTG | |
2406 netbeansFireChanges = 1; | |
2407 #endif | |
2408 --linecnt; | |
2409 } | |
2410 linecnt = curbuf->b_ml.ml_line_count - linecnt; | |
2411 if (filesize == 0) | |
2412 linecnt = 0; | |
2413 if (newfile || read_buffer) | |
1201 | 2414 { |
7 | 2415 redraw_curbuf_later(NOT_VALID); |
1201 | 2416 #ifdef FEAT_DIFF |
2417 /* After reading the text into the buffer the diff info needs to | |
2418 * be updated. */ | |
2419 diff_invalidate(curbuf); | |
2420 #endif | |
2421 #ifdef FEAT_FOLDING | |
2422 /* All folds in the window are invalid now. Mark them for update | |
2423 * before triggering autocommands. */ | |
2424 foldUpdateAll(curwin); | |
2425 #endif | |
2426 } | |
7 | 2427 else if (linecnt) /* appended at least one line */ |
2428 appended_lines_mark(from, linecnt); | |
2429 | |
2430 #ifndef ALWAYS_USE_GUI | |
2431 /* | |
2432 * If we were reading from the same terminal as where messages go, | |
2433 * the screen will have been messed up. | |
2434 * Switch on raw mode now and clear the screen. | |
2435 */ | |
2436 if (read_stdin) | |
2437 { | |
2438 settmode(TMODE_RAW); /* set to raw mode */ | |
2439 starttermcap(); | |
2440 screenclear(); | |
2441 } | |
2442 #endif | |
2443 | |
2444 if (got_int) | |
2445 { | |
2446 if (!(flags & READ_DUMMY)) | |
2447 { | |
2448 filemess(curbuf, sfname, (char_u *)_(e_interr), 0); | |
2449 if (newfile) | |
2450 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
2451 } | |
2452 msg_scroll = msg_save; | |
2453 #ifdef FEAT_VIMINFO | |
2454 check_marks_read(); | |
2455 #endif | |
2456 return OK; /* an interrupt isn't really an error */ | |
2457 } | |
2458 | |
2459 if (!filtering && !(flags & READ_DUMMY)) | |
2460 { | |
2461 msg_add_fname(curbuf, sfname); /* fname in IObuff with quotes */ | |
2462 c = FALSE; | |
2463 | |
2464 #ifdef UNIX | |
2465 # ifdef S_ISFIFO | |
2466 if (S_ISFIFO(perm)) /* fifo or socket */ | |
2467 { | |
2468 STRCAT(IObuff, _("[fifo/socket]")); | |
2469 c = TRUE; | |
2470 } | |
2471 # else | |
2472 # ifdef S_IFIFO | |
2473 if ((perm & S_IFMT) == S_IFIFO) /* fifo */ | |
2474 { | |
2475 STRCAT(IObuff, _("[fifo]")); | |
2476 c = TRUE; | |
2477 } | |
2478 # endif | |
2479 # ifdef S_IFSOCK | |
2480 if ((perm & S_IFMT) == S_IFSOCK) /* or socket */ | |
2481 { | |
2482 STRCAT(IObuff, _("[socket]")); | |
2483 c = TRUE; | |
2484 } | |
2485 # endif | |
2486 # endif | |
1313 | 2487 # ifdef OPEN_CHR_FILES |
2488 if (S_ISCHR(perm)) /* or character special */ | |
2489 { | |
2490 STRCAT(IObuff, _("[character special]")); | |
2491 c = TRUE; | |
2492 } | |
2493 # endif | |
7 | 2494 #endif |
2495 if (curbuf->b_p_ro) | |
2496 { | |
2497 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")); | |
2498 c = TRUE; | |
2499 } | |
2500 if (read_no_eol_lnum) | |
2501 { | |
2502 msg_add_eol(); | |
2503 c = TRUE; | |
2504 } | |
2505 if (ff_error == EOL_DOS) | |
2506 { | |
2507 STRCAT(IObuff, _("[CR missing]")); | |
2508 c = TRUE; | |
2509 } | |
2510 if (split) | |
2511 { | |
2512 STRCAT(IObuff, _("[long lines split]")); | |
2513 c = TRUE; | |
2514 } | |
2515 #ifdef FEAT_MBYTE | |
2516 if (notconverted) | |
2517 { | |
2518 STRCAT(IObuff, _("[NOT converted]")); | |
2519 c = TRUE; | |
2520 } | |
2521 else if (converted) | |
2522 { | |
2523 STRCAT(IObuff, _("[converted]")); | |
2524 c = TRUE; | |
2525 } | |
2526 #endif | |
2527 #ifdef FEAT_CRYPT | |
2528 if (cryptkey != NULL) | |
2529 { | |
6122 | 2530 crypt_append_msg(curbuf); |
7 | 2531 c = TRUE; |
2532 } | |
2533 #endif | |
2534 #ifdef FEAT_MBYTE | |
595 | 2535 if (conv_error != 0) |
2536 { | |
2537 sprintf((char *)IObuff + STRLEN(IObuff), | |
2538 _("[CONVERSION ERROR in line %ld]"), (long)conv_error); | |
7 | 2539 c = TRUE; |
2540 } | |
2541 else if (illegal_byte > 0) | |
2542 { | |
2543 sprintf((char *)IObuff + STRLEN(IObuff), | |
2544 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte); | |
2545 c = TRUE; | |
2546 } | |
2547 else | |
2548 #endif | |
2549 if (error) | |
2550 { | |
2551 STRCAT(IObuff, _("[READ ERRORS]")); | |
2552 c = TRUE; | |
2553 } | |
2554 if (msg_add_fileformat(fileformat)) | |
2555 c = TRUE; | |
2556 #ifdef FEAT_CRYPT | |
2557 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
|
2558 msg_add_lines(c, (long)linecnt, filesize |
6122 | 2559 - crypt_get_header_len(crypt_get_method_nr(curbuf))); |
7 | 2560 else |
2561 #endif | |
2562 msg_add_lines(c, (long)linecnt, filesize); | |
2563 | |
2564 vim_free(keep_msg); | |
2565 keep_msg = NULL; | |
2566 msg_scrolled_ign = TRUE; | |
2567 #ifdef ALWAYS_USE_GUI | |
2568 /* Don't show the message when reading stdin, it would end up in a | |
2569 * message box (which might be shown when exiting!) */ | |
2570 if (read_stdin || read_buffer) | |
2571 p = msg_may_trunc(FALSE, IObuff); | |
2572 else | |
2573 #endif | |
2574 p = msg_trunc_attr(IObuff, FALSE, 0); | |
2575 if (read_stdin || read_buffer || restart_edit != 0 | |
540 | 2576 || (msg_scrolled != 0 && !need_wait_return)) |
7 | 2577 /* Need to repeat the message after redrawing when: |
2578 * - When reading from stdin (the screen will be cleared next). | |
2579 * - When restart_edit is set (otherwise there will be a delay | |
2580 * before redrawing). | |
2581 * - When the screen was scrolled but there is no wait-return | |
2582 * prompt. */ | |
679 | 2583 set_keep_msg(p, 0); |
7 | 2584 msg_scrolled_ign = FALSE; |
2585 } | |
2586 | |
2587 /* with errors writing the file requires ":w!" */ | |
2588 if (newfile && (error | |
2589 #ifdef FEAT_MBYTE | |
595 | 2590 || conv_error != 0 |
679 | 2591 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP) |
7 | 2592 #endif |
2593 )) | |
2594 curbuf->b_p_ro = TRUE; | |
2595 | |
2596 u_clearline(); /* cannot use "U" command after adding lines */ | |
2597 | |
2598 /* | |
2599 * In Ex mode: cursor at last new line. | |
2600 * Otherwise: cursor at first new line. | |
2601 */ | |
2602 if (exmode_active) | |
2603 curwin->w_cursor.lnum = from + linecnt; | |
2604 else | |
2605 curwin->w_cursor.lnum = from + 1; | |
2606 check_cursor_lnum(); | |
2607 beginline(BL_WHITE | BL_FIX); /* on first non-blank */ | |
2608 | |
2609 /* | |
2610 * Set '[ and '] marks to the newly read lines. | |
2611 */ | |
2612 curbuf->b_op_start.lnum = from + 1; | |
2613 curbuf->b_op_start.col = 0; | |
2614 curbuf->b_op_end.lnum = from + linecnt; | |
2615 curbuf->b_op_end.col = 0; | |
696 | 2616 |
2617 #ifdef WIN32 | |
2618 /* | |
2619 * Work around a weird problem: When a file has two links (only | |
2620 * possible on NTFS) and we write through one link, then stat() it | |
1651 | 2621 * through the other link, the timestamp information may be wrong. |
696 | 2622 * It's correct again after reading the file, thus reset the timestamp |
2623 * here. | |
2624 */ | |
2625 if (newfile && !read_stdin && !read_buffer | |
2626 && mch_stat((char *)fname, &st) >= 0) | |
2627 { | |
2628 buf_store_time(curbuf, &st, fname); | |
2629 curbuf->b_mtime_read = curbuf->b_mtime; | |
2630 } | |
2631 #endif | |
7 | 2632 } |
2633 msg_scroll = msg_save; | |
2634 | |
2635 #ifdef FEAT_VIMINFO | |
2636 /* | |
2637 * Get the marks before executing autocommands, so they can be used there. | |
2638 */ | |
2639 check_marks_read(); | |
2640 #endif | |
2641 | |
2642 /* | |
6933 | 2643 * We remember if the last line of the read didn't have |
2644 * an eol even when 'binary' is off, to support turning 'fixeol' off, | |
2645 * or writing the read again with 'binary' on. The latter is required | |
2646 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work. | |
7 | 2647 */ |
2707 | 2648 curbuf->b_no_eol_lnum = read_no_eol_lnum; |
7 | 2649 |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2650 /* 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
|
2651 * different. */ |
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2652 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
|
2653 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
|
2654 |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2655 #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
|
2656 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2657 * 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
|
2658 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2659 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
|
2660 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2661 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
|
2662 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2663 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
|
2664 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
|
2665 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2666 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2667 |
167 | 2668 #ifdef FEAT_AUTOCMD |
7 | 2669 if (!read_stdin && !read_buffer) |
2670 { | |
2671 int m = msg_scroll; | |
2672 int n = msg_scrolled; | |
2673 | |
2674 /* Save the fileformat now, otherwise the buffer will be considered | |
2675 * modified if the format/encoding was automatically detected. */ | |
819 | 2676 if (set_options) |
7 | 2677 save_file_ff(curbuf); |
2678 | |
2679 /* | |
2680 * The output from the autocommands should not overwrite anything and | |
2681 * should not be overwritten: Set msg_scroll, restore its value if no | |
2682 * output was done. | |
2683 */ | |
2684 msg_scroll = TRUE; | |
2685 if (filtering) | |
2686 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname, | |
2687 FALSE, curbuf, eap); | |
2688 else if (newfile) | |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2689 { |
7 | 2690 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname, |
2691 FALSE, curbuf, eap); | |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2692 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
|
2693 /* |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2694 * 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
|
2695 * 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
|
2696 */ |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2697 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
|
2698 TRUE, curbuf); |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2699 } |
7 | 2700 else |
2701 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname, | |
2702 FALSE, NULL, eap); | |
2703 if (msg_scrolled == n) | |
2704 msg_scroll = m; | |
2707 | 2705 # ifdef FEAT_EVAL |
7 | 2706 if (aborting()) /* autocmds may abort script processing */ |
2707 return FAIL; | |
2707 | 2708 # endif |
2709 } | |
2710 #endif | |
2711 | |
7 | 2712 if (recoverymode && error) |
2713 return FAIL; | |
2714 return OK; | |
2715 } | |
2716 | |
1313 | 2717 #ifdef OPEN_CHR_FILES |
2718 /* | |
2719 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+", | |
2720 * which is the name of files used for process substitution output by | |
2721 * some shells on some operating systems, e.g., bash on SunOS. | |
2722 * Do not accept "/dev/fd/[012]", opening these may hang Vim. | |
2723 */ | |
2724 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2725 is_dev_fd_file(char_u *fname) |
1313 | 2726 { |
2727 return (STRNCMP(fname, "/dev/fd/", 8) == 0 | |
2728 && VIM_ISDIGIT(fname[8]) | |
2729 && *skipdigits(fname + 9) == NUL | |
2730 && (fname[9] != NUL | |
2731 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2'))); | |
2732 } | |
2733 #endif | |
2734 | |
595 | 2735 #ifdef FEAT_MBYTE |
2736 | |
2737 /* | |
2738 * From the current line count and characters read after that, estimate the | |
2739 * line number where we are now. | |
2740 * Used for error messages that include a line number. | |
2741 */ | |
2742 static linenr_T | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2743 readfile_linenr( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2744 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
|
2745 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
|
2746 char_u *endp) /* end of more bytes read */ |
595 | 2747 { |
2748 char_u *s; | |
2749 linenr_T lnum; | |
2750 | |
2751 lnum = curbuf->b_ml.ml_line_count - linecnt + 1; | |
2752 for (s = p; s < endp; ++s) | |
2753 if (*s == '\n') | |
2754 ++lnum; | |
2755 return lnum; | |
2756 } | |
2757 #endif | |
2758 | |
7 | 2759 /* |
612 | 2760 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be |
2761 * equal to the buffer "buf". Used for calling readfile(). | |
7 | 2762 * Returns OK or FAIL. |
2763 */ | |
2764 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2765 prep_exarg(exarg_T *eap, buf_T *buf) |
7 | 2766 { |
2767 eap->cmd = alloc((unsigned)(STRLEN(buf->b_p_ff) | |
2768 #ifdef FEAT_MBYTE | |
2769 + STRLEN(buf->b_p_fenc) | |
2770 #endif | |
2771 + 15)); | |
2772 if (eap->cmd == NULL) | |
2773 return FAIL; | |
2774 | |
2775 #ifdef FEAT_MBYTE | |
2776 sprintf((char *)eap->cmd, "e ++ff=%s ++enc=%s", buf->b_p_ff, buf->b_p_fenc); | |
2777 eap->force_enc = 14 + (int)STRLEN(buf->b_p_ff); | |
612 | 2778 eap->bad_char = buf->b_bad_char; |
7 | 2779 #else |
2780 sprintf((char *)eap->cmd, "e ++ff=%s", buf->b_p_ff); | |
2781 #endif | |
2782 eap->force_ff = 7; | |
612 | 2783 |
2784 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN; | |
819 | 2785 eap->read_edit = FALSE; |
612 | 2786 eap->forceit = FALSE; |
7 | 2787 return OK; |
2788 } | |
2789 | |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2790 /* |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2791 * Set default or forced 'fileformat' and 'binary'. |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2792 */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2793 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2794 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
|
2795 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2796 /* set default 'fileformat' */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2797 if (set_options) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2798 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2799 if (eap != NULL && eap->force_ff != 0) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2800 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
|
2801 else if (*p_ffs != NUL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2802 set_fileformat(default_fileformat(), OPT_LOCAL); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2803 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2804 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2805 /* set or reset 'binary' */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2806 if (eap != NULL && eap->force_bin != 0) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2807 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2808 int oldval = curbuf->b_p_bin; |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2809 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2810 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
|
2811 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
|
2812 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2813 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2814 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2815 #if defined(FEAT_MBYTE) || defined(PROTO) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2816 /* |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2817 * Set forced 'fileencoding'. |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2818 */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2819 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2820 set_forced_fenc(exarg_T *eap) |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2821 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2822 if (eap->force_enc != 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 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
|
2825 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2826 if (fenc != NULL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2827 set_string_option_direct((char_u *)"fenc", -1, |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2828 fenc, OPT_FREE|OPT_LOCAL, 0); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2829 vim_free(fenc); |
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 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2832 |
7 | 2833 /* |
2834 * Find next fileencoding to use from 'fileencodings'. | |
2835 * "pp" points to fenc_next. It's advanced to the next item. | |
2836 * When there are no more items, an empty string is returned and *pp is set to | |
2837 * NULL. | |
2838 * When *pp is not set to NULL, the result is in allocated memory. | |
2839 */ | |
2840 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2841 next_fenc(char_u **pp) |
7 | 2842 { |
2843 char_u *p; | |
2844 char_u *r; | |
2845 | |
2846 if (**pp == NUL) | |
2847 { | |
2848 *pp = NULL; | |
2849 return (char_u *)""; | |
2850 } | |
2851 p = vim_strchr(*pp, ','); | |
2852 if (p == NULL) | |
2853 { | |
2854 r = enc_canonize(*pp); | |
2855 *pp += STRLEN(*pp); | |
2856 } | |
2857 else | |
2858 { | |
2859 r = vim_strnsave(*pp, (int)(p - *pp)); | |
2860 *pp = p + 1; | |
2861 if (r != NULL) | |
2862 { | |
2863 p = enc_canonize(r); | |
2864 vim_free(r); | |
2865 r = p; | |
2866 } | |
2867 } | |
2868 if (r == NULL) /* out of memory */ | |
2869 { | |
2870 r = (char_u *)""; | |
2871 *pp = NULL; | |
2872 } | |
2873 return r; | |
2874 } | |
2875 | |
2876 # ifdef FEAT_EVAL | |
2877 /* | |
2878 * Convert a file with the 'charconvert' expression. | |
2879 * This closes the file which is to be read, converts it and opens the | |
2880 * resulting file for reading. | |
2881 * Returns name of the resulting converted file (the caller should delete it | |
2882 * after reading it). | |
2883 * Returns NULL if the conversion failed ("*fdp" is not set) . | |
2884 */ | |
2885 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2886 readfile_charconvert( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2887 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
|
2888 char_u *fenc, /* converted from */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2889 int *fdp) /* in/out: file descriptor of file */ |
7 | 2890 { |
2891 char_u *tmpname; | |
2892 char_u *errmsg = NULL; | |
2893 | |
6721 | 2894 tmpname = vim_tempname('r', FALSE); |
7 | 2895 if (tmpname == NULL) |
2896 errmsg = (char_u *)_("Can't find temp file for conversion"); | |
2897 else | |
2898 { | |
2899 close(*fdp); /* close the input file, ignore errors */ | |
2900 *fdp = -1; | |
2901 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc, | |
2902 fname, tmpname) == FAIL) | |
2903 errmsg = (char_u *)_("Conversion with 'charconvert' failed"); | |
2904 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname, | |
2905 O_RDONLY | O_EXTRA, 0)) < 0) | |
2906 errmsg = (char_u *)_("can't read output of 'charconvert'"); | |
2907 } | |
2908 | |
2909 if (errmsg != NULL) | |
2910 { | |
2911 /* Don't use emsg(), it breaks mappings, the retry with | |
2912 * another type of conversion might still work. */ | |
2913 MSG(errmsg); | |
2914 if (tmpname != NULL) | |
2915 { | |
2916 mch_remove(tmpname); /* delete converted file */ | |
2917 vim_free(tmpname); | |
2918 tmpname = NULL; | |
2919 } | |
2920 } | |
2921 | |
2922 /* If the input file is closed, open it (caller should check for error). */ | |
2923 if (*fdp < 0) | |
2924 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
2925 | |
2926 return tmpname; | |
2927 } | |
2928 # endif | |
2929 | |
2930 #endif | |
2931 | |
2932 #ifdef FEAT_VIMINFO | |
2933 /* | |
2934 * Read marks for the current buffer from the viminfo file, when we support | |
2935 * buffer marks and the buffer has a name. | |
2936 */ | |
2937 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2938 check_marks_read(void) |
7 | 2939 { |
2940 if (!curbuf->b_marks_read && get_viminfo_parameter('\'') > 0 | |
2941 && curbuf->b_ffname != NULL) | |
1733 | 2942 read_viminfo(NULL, VIF_WANT_MARKS); |
7 | 2943 |
2944 /* Always set b_marks_read; needed when 'viminfo' is changed to include | |
2945 * the ' parameter after opening a buffer. */ | |
2946 curbuf->b_marks_read = TRUE; | |
2947 } | |
2948 #endif | |
2949 | |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2950 #if defined(FEAT_CRYPT) || defined(PROTO) |
7 | 2951 /* |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2952 * Check for magic number used for encryption. Applies to the current buffer. |
7 | 2953 * If found, the magic number is removed from ptr[*sizep] and *sizep and |
2954 * *filesizep are updated. | |
2955 * Return the (new) encryption key, NULL for no encryption. | |
2956 */ | |
2957 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2958 check_for_cryptkey( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2959 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
|
2960 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
|
2961 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
|
2962 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
|
2963 int newfile, /* editing a new buffer */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2964 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
|
2965 int *did_ask) /* flag: whether already asked for key */ |
7 | 2966 { |
6122 | 2967 int method = crypt_method_nr_from_magic((char *)ptr, *sizep); |
5312 | 2968 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
|
2969 |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2970 if (method >= 0) |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2971 { |
5312 | 2972 /* Mark the buffer as read-only until the decryption has taken place. |
2973 * Avoids accidentally overwriting the file with garbage. */ | |
2974 curbuf->b_p_ro = TRUE; | |
2975 | |
6130 | 2976 /* Set the cryptmethod local to the buffer. */ |
6122 | 2977 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
|
2978 if (cryptkey == NULL && !*did_ask) |
7 | 2979 { |
2980 if (*curbuf->b_p_key) | |
2981 cryptkey = curbuf->b_p_key; | |
2982 else | |
2983 { | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2984 /* 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
|
2985 * 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
|
2986 * 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
|
2987 * Happens when retrying to detect encoding. */ |
2267 | 2988 smsg((char_u *)_(need_key_msg), fname); |
2989 msg_scroll = TRUE; | |
6353 | 2990 crypt_check_method(method); |
6122 | 2991 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
|
2992 *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
|
2993 |
7 | 2994 /* check if empty key entered */ |
2995 if (cryptkey != NULL && *cryptkey == NUL) | |
2996 { | |
2997 if (cryptkey != curbuf->b_p_key) | |
2998 vim_free(cryptkey); | |
2999 cryptkey = NULL; | |
3000 } | |
3001 } | |
3002 } | |
3003 | |
3004 if (cryptkey != NULL) | |
3005 { | |
6122 | 3006 int header_len; |
3007 | |
3008 curbuf->b_cryptstate = crypt_create_from_header( | |
3009 method, cryptkey, ptr); | |
3010 crypt_set_cm_option(curbuf, method); | |
3011 | |
3012 /* Remove cryptmethod specific header from the text. */ | |
3013 header_len = crypt_get_header_len(method); | |
3014 *filesizep += header_len; | |
3015 *sizep -= header_len; | |
3016 mch_memmove(ptr, ptr + header_len, (size_t)*sizep); | |
3017 | |
5312 | 3018 /* Restore the read-only flag. */ |
3019 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
|
3020 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3021 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3022 /* 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
|
3023 * 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
|
3024 else if (newfile && *curbuf->b_p_key != NUL && !starting) |
7 | 3025 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL); |
3026 | |
3027 return cryptkey; | |
3028 } | |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3029 #endif /* FEAT_CRYPT */ |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3030 |
7 | 3031 #ifdef UNIX |
3032 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3033 set_file_time( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3034 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3035 time_t atime, /* access time */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3036 time_t mtime) /* modification time */ |
7 | 3037 { |
3038 # if defined(HAVE_UTIME) && defined(HAVE_UTIME_H) | |
3039 struct utimbuf buf; | |
3040 | |
3041 buf.actime = atime; | |
3042 buf.modtime = mtime; | |
3043 (void)utime((char *)fname, &buf); | |
3044 # else | |
3045 # if defined(HAVE_UTIMES) | |
3046 struct timeval tvp[2]; | |
3047 | |
3048 tvp[0].tv_sec = atime; | |
3049 tvp[0].tv_usec = 0; | |
3050 tvp[1].tv_sec = mtime; | |
3051 tvp[1].tv_usec = 0; | |
3052 # ifdef NeXT | |
3053 (void)utimes((char *)fname, tvp); | |
3054 # else | |
3055 (void)utimes((char *)fname, (const struct timeval *)&tvp); | |
3056 # endif | |
3057 # endif | |
3058 # endif | |
3059 } | |
3060 #endif /* UNIX */ | |
3061 | |
22 | 3062 #if defined(VMS) && !defined(MIN) |
3063 /* Older DECC compiler for VAX doesn't define MIN() */ | |
3064 # define MIN(a, b) ((a) < (b) ? (a) : (b)) | |
3065 #endif | |
3066 | |
7 | 3067 /* |
1303 | 3068 * Return TRUE if a file appears to be read-only from the file permissions. |
3069 */ | |
3070 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3071 check_file_readonly( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3072 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
|
3073 int perm) /* known permissions on file */ |
1303 | 3074 { |
3075 #ifndef USE_MCH_ACCESS | |
3076 int fd = 0; | |
3077 #endif | |
3078 | |
3079 return ( | |
3080 #ifdef USE_MCH_ACCESS | |
3081 # ifdef UNIX | |
3082 (perm & 0222) == 0 || | |
3083 # endif | |
3084 mch_access((char *)fname, W_OK) | |
3085 #else | |
3086 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0 | |
3087 ? TRUE : (close(fd), FALSE) | |
3088 #endif | |
3089 ); | |
3090 } | |
3091 | |
3092 | |
3093 /* | |
589 | 3094 * buf_write() - write to file "fname" lines "start" through "end" |
7 | 3095 * |
3096 * We do our own buffering here because fwrite() is so slow. | |
3097 * | |
589 | 3098 * If "forceit" is true, we don't care for errors when attempting backups. |
3099 * In case of an error everything possible is done to restore the original | |
1698 | 3100 * file. But when "forceit" is TRUE, we risk losing it. |
589 | 3101 * |
3102 * When "reset_changed" is TRUE and "append" == FALSE and "start" == 1 and | |
3103 * "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed. | |
7 | 3104 * |
3105 * This function must NOT use NameBuff (because it's called by autowrite()). | |
3106 * | |
3107 * return FAIL for failure, OK otherwise | |
3108 */ | |
3109 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3110 buf_write( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3111 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3112 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3113 char_u *sfname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3114 linenr_T start, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3115 linenr_T end, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3116 exarg_T *eap, /* for forced 'ff' and 'fenc', can be |
7 | 3117 NULL! */ |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3118 int append, /* append to the file */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3119 int forceit, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3120 int reset_changed, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3121 int filtering) |
7 | 3122 { |
3123 int fd; | |
3124 char_u *backup = NULL; | |
3125 int backup_copy = FALSE; /* copy the original file? */ | |
3126 int dobackup; | |
3127 char_u *ffname; | |
3128 char_u *wfname = NULL; /* name of file to write to */ | |
3129 char_u *s; | |
3130 char_u *ptr; | |
3131 char_u c; | |
3132 int len; | |
3133 linenr_T lnum; | |
3134 long nchars; | |
3135 char_u *errmsg = NULL; | |
1947 | 3136 int errmsg_allocated = FALSE; |
7 | 3137 char_u *errnum = NULL; |
3138 char_u *buffer; | |
3139 char_u smallbuf[SMBUFSIZE]; | |
3140 char_u *backup_ext; | |
3141 int bufsize; | |
3142 long perm; /* file permissions */ | |
3143 int retval = OK; | |
3144 int newfile = FALSE; /* TRUE if file doesn't exist yet */ | |
3145 int msg_save = msg_scroll; | |
3146 int overwriting; /* TRUE if writing over original */ | |
3147 int no_eol = FALSE; /* no end-of-line written */ | |
3148 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
|
3149 stat_T st_old; |
7 | 3150 int prev_got_int = got_int; |
3151 int file_readonly = FALSE; /* overwritten file is read-only */ | |
3152 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
|
3153 #if defined(UNIX) /*XXX fix me sometime? */ |
7 | 3154 int made_writable = FALSE; /* 'w' bit has been set */ |
3155 #endif | |
3156 /* writing everything */ | |
3157 int whole = (start == 1 && end == buf->b_ml.ml_line_count); | |
3158 #ifdef FEAT_AUTOCMD | |
3159 linenr_T old_line_count = buf->b_ml.ml_line_count; | |
3160 #endif | |
3161 int attr; | |
3162 int fileformat; | |
3163 int write_bin; | |
3164 struct bw_info write_info; /* info for buf_write_bytes() */ | |
3165 #ifdef FEAT_MBYTE | |
3166 int converted = FALSE; | |
3167 int notconverted = FALSE; | |
3168 char_u *fenc; /* effective 'fileencoding' */ | |
3169 char_u *fenc_tofree = NULL; /* allocated "fenc" */ | |
3170 #endif | |
3171 #ifdef HAS_BW_FLAGS | |
3172 int wb_flags = 0; | |
3173 #endif | |
3174 #ifdef HAVE_ACL | |
3175 vim_acl_T acl = NULL; /* ACL copied from original file to | |
3176 backup or new file */ | |
3177 #endif | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3178 #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
|
3179 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
|
3180 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
|
3181 #endif |
6243 | 3182 unsigned int bkc = get_bkc_value(buf); |
7 | 3183 |
3184 if (fname == NULL || *fname == NUL) /* safety check */ | |
3185 return FAIL; | |
2028 | 3186 if (buf->b_ml.ml_mfp == NULL) |
3187 { | |
3188 /* This can happen during startup when there is a stray "w" in the | |
3189 * vimrc file. */ | |
3190 EMSG(_(e_emptybuf)); | |
3191 return FAIL; | |
3192 } | |
7 | 3193 |
3194 /* | |
3195 * Disallow writing from .exrc and .vimrc in current directory for | |
3196 * security reasons. | |
3197 */ | |
3198 if (check_secure()) | |
3199 return FAIL; | |
3200 | |
3201 /* Avoid a crash for a long name. */ | |
3202 if (STRLEN(fname) >= MAXPATHL) | |
3203 { | |
3204 EMSG(_(e_longname)); | |
3205 return FAIL; | |
3206 } | |
3207 | |
3208 #ifdef FEAT_MBYTE | |
3209 /* must init bw_conv_buf and bw_iconv_fd before jumping to "fail" */ | |
3210 write_info.bw_conv_buf = NULL; | |
3211 write_info.bw_conv_error = FALSE; | |
1947 | 3212 write_info.bw_conv_error_lnum = 0; |
7 | 3213 write_info.bw_restlen = 0; |
3214 # ifdef USE_ICONV | |
3215 write_info.bw_iconv_fd = (iconv_t)-1; | |
3216 # endif | |
3217 #endif | |
6122 | 3218 #ifdef FEAT_CRYPT |
3219 write_info.bw_buffer = buf; | |
3220 #endif | |
7 | 3221 |
167 | 3222 /* After writing a file changedtick changes but we don't want to display |
3223 * the line. */ | |
3224 ex_no_reprint = TRUE; | |
3225 | |
7 | 3226 /* |
3227 * If there is no file name yet, use the one for the written file. | |
3228 * BF_NOTEDITED is set to reflect this (in case the write fails). | |
3229 * Don't do this when the write is for a filter command. | |
589 | 3230 * Don't do this when appending. |
3231 * Only do this when 'cpoptions' contains the 'F' flag. | |
7 | 3232 */ |
633 | 3233 if (buf->b_ffname == NULL |
3234 && reset_changed | |
7 | 3235 && whole |
3236 && buf == curbuf | |
236 | 3237 #ifdef FEAT_QUICKFIX |
3238 && !bt_nofile(buf) | |
3239 #endif | |
7 | 3240 && !filtering |
589 | 3241 && (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL) |
7 | 3242 && vim_strchr(p_cpo, CPO_FNAMEW) != NULL) |
3243 { | |
633 | 3244 if (set_rw_fname(fname, sfname) == FAIL) |
7 | 3245 return FAIL; |
633 | 3246 buf = curbuf; /* just in case autocmds made "buf" invalid */ |
7 | 3247 } |
3248 | |
3249 if (sfname == NULL) | |
3250 sfname = fname; | |
3251 /* | |
3252 * For Unix: Use the short file name whenever possible. | |
3253 * Avoids problems with networks and when directory names are changed. | |
3254 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to | |
3255 * another directory, which we don't detect | |
3256 */ | |
3257 ffname = fname; /* remember full fname */ | |
3258 #ifdef UNIX | |
3259 fname = sfname; | |
3260 #endif | |
3261 | |
3262 if (buf->b_ffname != NULL && fnamecmp(ffname, buf->b_ffname) == 0) | |
3263 overwriting = TRUE; | |
3264 else | |
3265 overwriting = FALSE; | |
3266 | |
3267 if (exiting) | |
4352 | 3268 settmode(TMODE_COOK); /* when exiting allow typeahead now */ |
7 | 3269 |
3270 ++no_wait_return; /* don't wait for return yet */ | |
3271 | |
3272 /* | |
3273 * Set '[ and '] marks to the lines to be written. | |
3274 */ | |
3275 buf->b_op_start.lnum = start; | |
3276 buf->b_op_start.col = 0; | |
3277 buf->b_op_end.lnum = end; | |
3278 buf->b_op_end.col = 0; | |
3279 | |
3280 #ifdef FEAT_AUTOCMD | |
3281 { | |
3282 aco_save_T aco; | |
3283 int buf_ffname = FALSE; | |
3284 int buf_sfname = FALSE; | |
3285 int buf_fname_f = FALSE; | |
3286 int buf_fname_s = FALSE; | |
3287 int did_cmd = FALSE; | |
17 | 3288 int nofile_err = FALSE; |
161 | 3289 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
|
3290 bufref_T bufref; |
7 | 3291 |
3292 /* | |
4352 | 3293 * Apply PRE autocommands. |
7 | 3294 * Set curbuf to the buffer to be written. |
3295 * Careful: The autocommands may call buf_write() recursively! | |
3296 */ | |
3297 if (ffname == buf->b_ffname) | |
3298 buf_ffname = TRUE; | |
3299 if (sfname == buf->b_sfname) | |
3300 buf_sfname = TRUE; | |
3301 if (fname == buf->b_ffname) | |
3302 buf_fname_f = TRUE; | |
3303 if (fname == buf->b_sfname) | |
3304 buf_fname_s = TRUE; | |
3305 | |
3306 /* set curwin/curbuf to buf and save a few things */ | |
3307 aucmd_prepbuf(&aco, buf); | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
3308 set_bufref(&bufref, buf); |
7 | 3309 |
3310 if (append) | |
3311 { | |
3312 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD, | |
3313 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3314 { |
635 | 3315 #ifdef FEAT_QUICKFIX |
18 | 3316 if (overwriting && bt_nofile(curbuf)) |
17 | 3317 nofile_err = TRUE; |
3318 else | |
635 | 3319 #endif |
17 | 3320 apply_autocmds_exarg(EVENT_FILEAPPENDPRE, |
7 | 3321 sfname, sfname, FALSE, curbuf, eap); |
17 | 3322 } |
7 | 3323 } |
3324 else if (filtering) | |
3325 { | |
3326 apply_autocmds_exarg(EVENT_FILTERWRITEPRE, | |
3327 NULL, sfname, FALSE, curbuf, eap); | |
3328 } | |
3329 else if (reset_changed && whole) | |
3330 { | |
3036 | 3331 int was_changed = curbufIsChanged(); |
3332 | |
3333 did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD, | |
3334 sfname, sfname, FALSE, curbuf, eap); | |
3335 if (did_cmd) | |
3336 { | |
3337 if (was_changed && !curbufIsChanged()) | |
3338 { | |
3339 /* Written everything correctly and BufWriteCmd has reset | |
3340 * 'modified': Correct the undo information so that an | |
3341 * undo now sets 'modified'. */ | |
3342 u_unchanged(curbuf); | |
3343 u_update_save_nr(curbuf); | |
3344 } | |
3345 } | |
3346 else | |
17 | 3347 { |
635 | 3348 #ifdef FEAT_QUICKFIX |
179 | 3349 if (overwriting && bt_nofile(curbuf)) |
17 | 3350 nofile_err = TRUE; |
3351 else | |
635 | 3352 #endif |
17 | 3353 apply_autocmds_exarg(EVENT_BUFWRITEPRE, |
7 | 3354 sfname, sfname, FALSE, curbuf, eap); |
17 | 3355 } |
7 | 3356 } |
3357 else | |
3358 { | |
3359 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD, | |
3360 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3361 { |
635 | 3362 #ifdef FEAT_QUICKFIX |
179 | 3363 if (overwriting && bt_nofile(curbuf)) |
17 | 3364 nofile_err = TRUE; |
3365 else | |
635 | 3366 #endif |
17 | 3367 apply_autocmds_exarg(EVENT_FILEWRITEPRE, |
7 | 3368 sfname, sfname, FALSE, curbuf, eap); |
17 | 3369 } |
7 | 3370 } |
3371 | |
3372 /* restore curwin/curbuf and a few other things */ | |
3373 aucmd_restbuf(&aco); | |
3374 | |
3375 /* | |
3376 * In three situations we return here and don't write the file: | |
3377 * 1. the autocommands deleted or unloaded the buffer. | |
3378 * 2. The autocommands abort script processing. | |
3379 * 3. If one of the "Cmd" autocommands was executed. | |
3380 */ | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
3381 if (!bufref_valid(&bufref)) |
7 | 3382 buf = NULL; |
161 | 3383 if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline) |
532 | 3384 || did_cmd || nofile_err |
3385 #ifdef FEAT_EVAL | |
3386 || aborting() | |
3387 #endif | |
3388 ) | |
7 | 3389 { |
3390 --no_wait_return; | |
3391 msg_scroll = msg_save; | |
17 | 3392 if (nofile_err) |
3393 EMSG(_("E676: No matching autocommands for acwrite buffer")); | |
3394 | |
532 | 3395 if (nofile_err |
3396 #ifdef FEAT_EVAL | |
3397 || aborting() | |
3398 #endif | |
3399 ) | |
7 | 3400 /* An aborting error, interrupt or exception in the |
3401 * autocommands. */ | |
3402 return FAIL; | |
3403 if (did_cmd) | |
3404 { | |
3405 if (buf == NULL) | |
3406 /* The buffer was deleted. We assume it was written | |
3407 * (can't retry anyway). */ | |
3408 return OK; | |
3409 if (overwriting) | |
3410 { | |
3411 /* Assume the buffer was written, update the timestamp. */ | |
3412 ml_timestamp(buf); | |
589 | 3413 if (append) |
3414 buf->b_flags &= ~BF_NEW; | |
3415 else | |
3416 buf->b_flags &= ~BF_WRITE_MASK; | |
7 | 3417 } |
589 | 3418 if (reset_changed && buf->b_changed && !append |
39 | 3419 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)) |
7 | 3420 /* Buffer still changed, the autocommands didn't work |
3421 * properly. */ | |
3422 return FAIL; | |
3423 return OK; | |
3424 } | |
3425 #ifdef FEAT_EVAL | |
3426 if (!aborting()) | |
3427 #endif | |
3428 EMSG(_("E203: Autocommands deleted or unloaded buffer to be written")); | |
3429 return FAIL; | |
3430 } | |
3431 | |
3432 /* | |
3433 * The autocommands may have changed the number of lines in the file. | |
3434 * When writing the whole file, adjust the end. | |
3435 * When writing part of the file, assume that the autocommands only | |
3436 * changed the number of lines that are to be written (tricky!). | |
3437 */ | |
3438 if (buf->b_ml.ml_line_count != old_line_count) | |
3439 { | |
3440 if (whole) /* write all */ | |
3441 end = buf->b_ml.ml_line_count; | |
3442 else if (buf->b_ml.ml_line_count > old_line_count) /* more lines */ | |
3443 end += buf->b_ml.ml_line_count - old_line_count; | |
3444 else /* less lines */ | |
3445 { | |
3446 end -= old_line_count - buf->b_ml.ml_line_count; | |
3447 if (end < start) | |
3448 { | |
3449 --no_wait_return; | |
3450 msg_scroll = msg_save; | |
3451 EMSG(_("E204: Autocommand changed number of lines in unexpected way")); | |
3452 return FAIL; | |
3453 } | |
3454 } | |
3455 } | |
3456 | |
3457 /* | |
3458 * The autocommands may have changed the name of the buffer, which may | |
3459 * be kept in fname, ffname and sfname. | |
3460 */ | |
3461 if (buf_ffname) | |
3462 ffname = buf->b_ffname; | |
3463 if (buf_sfname) | |
3464 sfname = buf->b_sfname; | |
3465 if (buf_fname_f) | |
3466 fname = buf->b_ffname; | |
3467 if (buf_fname_s) | |
3468 fname = buf->b_sfname; | |
3469 } | |
3470 #endif | |
3471 | |
3472 #ifdef FEAT_NETBEANS_INTG | |
2210 | 3473 if (netbeans_active() && isNetbeansBuffer(buf)) |
7 | 3474 { |
3475 if (whole) | |
3476 { | |
3477 /* | |
3478 * b_changed can be 0 after an undo, but we still need to write | |
3479 * the buffer to NetBeans. | |
3480 */ | |
3481 if (buf->b_changed || isNetbeansModified(buf)) | |
3482 { | |
33 | 3483 --no_wait_return; /* may wait for return now */ |
3484 msg_scroll = msg_save; | |
3485 netbeans_save_buffer(buf); /* no error checking... */ | |
7 | 3486 return retval; |
3487 } | |
3488 else | |
3489 { | |
3490 errnum = (char_u *)"E656: "; | |
1653 | 3491 errmsg = (char_u *)_("NetBeans disallows writes of unmodified buffers"); |
7 | 3492 buffer = NULL; |
3493 goto fail; | |
3494 } | |
3495 } | |
3496 else | |
3497 { | |
3498 errnum = (char_u *)"E657: "; | |
3499 errmsg = (char_u *)_("Partial writes disallowed for NetBeans buffers"); | |
3500 buffer = NULL; | |
3501 goto fail; | |
3502 } | |
3503 } | |
3504 #endif | |
3505 | |
3506 if (shortmess(SHM_OVER) && !exiting) | |
3507 msg_scroll = FALSE; /* overwrite previous file message */ | |
3508 else | |
3509 msg_scroll = TRUE; /* don't overwrite previous file message */ | |
3510 if (!filtering) | |
3511 filemess(buf, | |
3512 #ifndef UNIX | |
3513 sfname, | |
3514 #else | |
3515 fname, | |
3516 #endif | |
3517 (char_u *)"", 0); /* show that we are busy */ | |
3518 msg_scroll = FALSE; /* always overwrite the file message now */ | |
3519 | |
3520 buffer = alloc(BUFSIZE); | |
3521 if (buffer == NULL) /* can't allocate big buffer, use small | |
3522 * one (to be able to write when out of | |
3523 * memory) */ | |
3524 { | |
3525 buffer = smallbuf; | |
3526 bufsize = SMBUFSIZE; | |
3527 } | |
3528 else | |
3529 bufsize = BUFSIZE; | |
3530 | |
3531 /* | |
3532 * Get information about original file (if there is one). | |
3533 */ | |
7410
08e62c4fc17d
commit https://github.com/vim/vim/commit/53076830fea6df737455523f7e235bfe4f79864d
Christian Brabandt <cb@256bit.org>
parents:
7408
diff
changeset
|
3534 #if defined(UNIX) |
1438 | 3535 st_old.st_dev = 0; |
3536 st_old.st_ino = 0; | |
7 | 3537 perm = -1; |
3538 if (mch_stat((char *)fname, &st_old) < 0) | |
3539 newfile = TRUE; | |
3540 else | |
3541 { | |
3542 perm = st_old.st_mode; | |
3543 if (!S_ISREG(st_old.st_mode)) /* not a file */ | |
3544 { | |
3545 if (S_ISDIR(st_old.st_mode)) | |
3546 { | |
3547 errnum = (char_u *)"E502: "; | |
3548 errmsg = (char_u *)_("is a directory"); | |
3549 goto fail; | |
3550 } | |
3551 if (mch_nodetype(fname) != NODE_WRITABLE) | |
3552 { | |
3553 errnum = (char_u *)"E503: "; | |
3554 errmsg = (char_u *)_("is not a file or writable device"); | |
3555 goto fail; | |
3556 } | |
3557 /* It's a device of some kind (or a fifo) which we can write to | |
3558 * but for which we can't make a backup. */ | |
3559 device = TRUE; | |
3560 newfile = TRUE; | |
3561 perm = -1; | |
3562 } | |
3563 } | |
3564 #else /* !UNIX */ | |
3565 /* | |
3566 * Check for a writable device name. | |
3567 */ | |
3568 c = mch_nodetype(fname); | |
3569 if (c == NODE_OTHER) | |
3570 { | |
3571 errnum = (char_u *)"E503: "; | |
3572 errmsg = (char_u *)_("is not a file or writable device"); | |
3573 goto fail; | |
3574 } | |
3575 if (c == NODE_WRITABLE) | |
3576 { | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3577 # if defined(MSWIN) |
1004 | 3578 /* MS-Windows allows opening a device, but we will probably get stuck |
3579 * trying to write to it. */ | |
3580 if (!p_odev) | |
3581 { | |
3582 errnum = (char_u *)"E796: "; | |
3583 errmsg = (char_u *)_("writing to device disabled with 'opendevice' option"); | |
3584 goto fail; | |
3585 } | |
3586 # endif | |
7 | 3587 device = TRUE; |
3588 newfile = TRUE; | |
3589 perm = -1; | |
3590 } | |
3591 else | |
3592 { | |
3593 perm = mch_getperm(fname); | |
3594 if (perm < 0) | |
3595 newfile = TRUE; | |
3596 else if (mch_isdir(fname)) | |
3597 { | |
3598 errnum = (char_u *)"E502: "; | |
3599 errmsg = (char_u *)_("is a directory"); | |
3600 goto fail; | |
3601 } | |
3602 if (overwriting) | |
3603 (void)mch_stat((char *)fname, &st_old); | |
3604 } | |
3605 #endif /* !UNIX */ | |
3606 | |
3607 if (!device && !newfile) | |
3608 { | |
3609 /* | |
3610 * Check if the file is really writable (when renaming the file to | |
3611 * make a backup we won't discover it later). | |
3612 */ | |
1303 | 3613 file_readonly = check_file_readonly(fname, (int)perm); |
3614 | |
7 | 3615 if (!forceit && file_readonly) |
3616 { | |
3617 if (vim_strchr(p_cpo, CPO_FWRITE) != NULL) | |
3618 { | |
3619 errnum = (char_u *)"E504: "; | |
3620 errmsg = (char_u *)_(err_readonly); | |
3621 } | |
3622 else | |
3623 { | |
3624 errnum = (char_u *)"E505: "; | |
3625 errmsg = (char_u *)_("is read-only (add ! to override)"); | |
3626 } | |
3627 goto fail; | |
3628 } | |
3629 | |
3630 /* | |
3631 * Check if the timestamp hasn't changed since reading the file. | |
3632 */ | |
3633 if (overwriting) | |
3634 { | |
3635 retval = check_mtime(buf, &st_old); | |
3636 if (retval == FAIL) | |
3637 goto fail; | |
3638 } | |
3639 } | |
3640 | |
3641 #ifdef HAVE_ACL | |
3642 /* | |
3643 * For systems that support ACL: get the ACL from the original file. | |
3644 */ | |
3645 if (!newfile) | |
3646 acl = mch_get_acl(fname); | |
3647 #endif | |
3648 | |
3649 /* | |
3650 * If 'backupskip' is not empty, don't make a backup for some files. | |
3651 */ | |
3652 dobackup = (p_wb || p_bk || *p_pm != NUL); | |
3653 #ifdef FEAT_WILDIGN | |
3654 if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname)) | |
3655 dobackup = FALSE; | |
3656 #endif | |
3657 | |
3658 /* | |
3659 * Save the value of got_int and reset it. We don't want a previous | |
3660 * interruption cancel writing, only hitting CTRL-C while writing should | |
3661 * abort it. | |
3662 */ | |
3663 prev_got_int = got_int; | |
3664 got_int = FALSE; | |
3665 | |
3666 /* Mark the buffer as 'being saved' to prevent changed buffer warnings */ | |
3667 buf->b_saving = TRUE; | |
3668 | |
3669 /* | |
3670 * If we are not appending or filtering, the file exists, and the | |
3671 * 'writebackup', 'backup' or 'patchmode' option is set, need a backup. | |
3672 * When 'patchmode' is set also make a backup when appending. | |
3673 * | |
3674 * Do not make any backup, if 'writebackup' and 'backup' are both switched | |
3675 * off. This helps when editing large files on almost-full disks. | |
3676 */ | |
3677 if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup) | |
3678 { | |
3679 #if defined(UNIX) || defined(WIN32) | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
3680 stat_T st; |
7 | 3681 #endif |
3682 | |
6243 | 3683 if ((bkc & BKC_YES) || append) /* "yes" */ |
7 | 3684 backup_copy = TRUE; |
3685 #if defined(UNIX) || defined(WIN32) | |
6243 | 3686 else if ((bkc & BKC_AUTO)) /* "auto" */ |
7 | 3687 { |
3688 int i; | |
3689 | |
3690 # ifdef UNIX | |
3691 /* | |
3692 * Don't rename the file when: | |
3693 * - it's a hard link | |
3694 * - it's a symbolic link | |
3695 * - we don't have write permission in the directory | |
3696 * - we can't set the owner/group of the new file | |
3697 */ | |
3698 if (st_old.st_nlink > 1 | |
3699 || mch_lstat((char *)fname, &st) < 0 | |
3700 || st.st_dev != st_old.st_dev | |
557 | 3701 || st.st_ino != st_old.st_ino |
3702 # ifndef HAVE_FCHOWN | |
3703 || st.st_uid != st_old.st_uid | |
3704 || st.st_gid != st_old.st_gid | |
3705 # endif | |
3706 ) | |
7 | 3707 backup_copy = TRUE; |
3708 else | |
696 | 3709 # else |
3710 # ifdef WIN32 | |
3711 /* On NTFS file systems hard links are possible. */ | |
3712 if (mch_is_linked(fname)) | |
3713 backup_copy = TRUE; | |
3714 else | |
3715 # endif | |
7 | 3716 # endif |
3717 { | |
3718 /* | |
3719 * Check if we can create a file and set the owner/group to | |
3720 * the ones from the original file. | |
3721 * First find a file name that doesn't exist yet (use some | |
3722 * arbitrary numbers). | |
3723 */ | |
3724 STRCPY(IObuff, fname); | |
3725 for (i = 4913; ; i += 123) | |
3726 { | |
3727 sprintf((char *)gettail(IObuff), "%d", i); | |
557 | 3728 if (mch_lstat((char *)IObuff, &st) < 0) |
7 | 3729 break; |
3730 } | |
557 | 3731 fd = mch_open((char *)IObuff, |
3732 O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm); | |
7 | 3733 if (fd < 0) /* can't write in directory */ |
3734 backup_copy = TRUE; | |
3735 else | |
3736 { | |
3737 # ifdef UNIX | |
557 | 3738 # ifdef HAVE_FCHOWN |
1757 | 3739 ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
557 | 3740 # endif |
7 | 3741 if (mch_stat((char *)IObuff, &st) < 0 |
3742 || st.st_uid != st_old.st_uid | |
3743 || st.st_gid != st_old.st_gid | |
1877 | 3744 || (long)st.st_mode != perm) |
7 | 3745 backup_copy = TRUE; |
3746 # endif | |
567 | 3747 /* Close the file before removing it, on MS-Windows we |
3748 * can't delete an open file. */ | |
3749 close(fd); | |
7 | 3750 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
|
3751 # 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
|
3752 /* 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
|
3753 * 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
|
3754 * second. */ |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3755 { |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3756 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
|
3757 |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3758 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
|
3759 { |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3760 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
|
3761 break; |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3762 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
|
3763 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
|
3764 } |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3765 } |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3766 # endif |
7 | 3767 } |
3768 } | |
3769 } | |
3770 | |
3771 /* | |
3772 * Break symlinks and/or hardlinks if we've been asked to. | |
3773 */ | |
6243 | 3774 if ((bkc & BKC_BREAKSYMLINK) || (bkc & BKC_BREAKHARDLINK)) |
7 | 3775 { |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3776 # ifdef UNIX |
7 | 3777 int lstat_res; |
3778 | |
3779 lstat_res = mch_lstat((char *)fname, &st); | |
3780 | |
3781 /* Symlinks. */ | |
6243 | 3782 if ((bkc & BKC_BREAKSYMLINK) |
7 | 3783 && lstat_res == 0 |
3784 && st.st_ino != st_old.st_ino) | |
3785 backup_copy = FALSE; | |
3786 | |
3787 /* Hardlinks. */ | |
6243 | 3788 if ((bkc & BKC_BREAKHARDLINK) |
7 | 3789 && st_old.st_nlink > 1 |
3790 && (lstat_res != 0 || st.st_ino == st_old.st_ino)) | |
3791 backup_copy = FALSE; | |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3792 # else |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3793 # if defined(WIN32) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3794 /* Symlinks. */ |
6243 | 3795 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
|
3796 backup_copy = FALSE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3797 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3798 /* Hardlinks. */ |
6243 | 3799 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
|
3800 backup_copy = FALSE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3801 # endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3802 # endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3803 } |
7 | 3804 |
3805 #endif | |
3806 | |
3807 /* make sure we have a valid backup extension to use */ | |
3808 if (*p_bex == NUL) | |
3809 backup_ext = (char_u *)".bak"; | |
3810 else | |
3811 backup_ext = p_bex; | |
3812 | |
3813 if (backup_copy | |
3814 && (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) >= 0) | |
3815 { | |
3816 int bfd; | |
3817 char_u *copybuf, *wp; | |
3818 int some_error = FALSE; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
3819 stat_T st_new; |
7 | 3820 char_u *dirp; |
3821 char_u *rootname; | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3822 #if defined(UNIX) |
7 | 3823 int did_set_shortname; |
3824 #endif | |
3825 | |
3826 copybuf = alloc(BUFSIZE + 1); | |
3827 if (copybuf == NULL) | |
3828 { | |
3829 some_error = TRUE; /* out of memory */ | |
3830 goto nobackup; | |
3831 } | |
3832 | |
3833 /* | |
3834 * Try to make the backup in each directory in the 'bdir' option. | |
3835 * | |
3836 * Unix semantics has it, that we may have a writable file, | |
3837 * that cannot be recreated with a simple open(..., O_CREAT, ) e.g: | |
3838 * - the directory is not writable, | |
3839 * - the file may be a symbolic link, | |
3840 * - the file may belong to another user/group, etc. | |
3841 * | |
3842 * For these reasons, the existing writable file must be truncated | |
3843 * and reused. Creation of a backup COPY will be attempted. | |
3844 */ | |
3845 dirp = p_bdir; | |
3846 while (*dirp) | |
3847 { | |
3848 #ifdef UNIX | |
3849 st_new.st_ino = 0; | |
3850 st_new.st_dev = 0; | |
3851 st_new.st_gid = 0; | |
3852 #endif | |
3853 | |
3854 /* | |
3855 * Isolate one directory name, using an entry in 'bdir'. | |
3856 */ | |
3857 (void)copy_option_part(&dirp, copybuf, BUFSIZE, ","); | |
3858 rootname = get_file_in_dir(fname, copybuf); | |
3859 if (rootname == NULL) | |
3860 { | |
3861 some_error = TRUE; /* out of memory */ | |
3862 goto nobackup; | |
3863 } | |
3864 | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3865 #if defined(UNIX) |
7 | 3866 did_set_shortname = FALSE; |
3867 #endif | |
3868 | |
3869 /* | |
3870 * May try twice if 'shortname' not set. | |
3871 */ | |
3872 for (;;) | |
3873 { | |
3874 /* | |
3875 * Make backup file name. | |
3876 */ | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3877 backup = buf_modname((buf->b_p_sn || buf->b_shortname), |
7 | 3878 rootname, backup_ext, FALSE); |
3879 if (backup == NULL) | |
3880 { | |
3881 vim_free(rootname); | |
3882 some_error = TRUE; /* out of memory */ | |
3883 goto nobackup; | |
3884 } | |
3885 | |
3886 /* | |
3887 * Check if backup file already exists. | |
3888 */ | |
3889 if (mch_stat((char *)backup, &st_new) >= 0) | |
3890 { | |
3891 #ifdef UNIX | |
3892 /* | |
3893 * Check if backup file is same as original file. | |
3894 * May happen when modname() gave the same file back. | |
3895 * E.g. silly link, or file name-length reached. | |
3896 * If we don't check here, we either ruin the file | |
3897 * when copying or erase it after writing. jw. | |
3898 */ | |
3899 if (st_new.st_dev == st_old.st_dev | |
3900 && st_new.st_ino == st_old.st_ino) | |
3901 { | |
3902 vim_free(backup); | |
3903 backup = NULL; /* no backup file to delete */ | |
3904 /* | |
3905 * may try again with 'shortname' set | |
3906 */ | |
3907 if (!(buf->b_shortname || buf->b_p_sn)) | |
3908 { | |
3909 buf->b_shortname = TRUE; | |
3910 did_set_shortname = TRUE; | |
3911 continue; | |
3912 } | |
3913 /* setting shortname didn't help */ | |
3914 if (did_set_shortname) | |
3915 buf->b_shortname = FALSE; | |
3916 break; | |
3917 } | |
3918 #endif | |
3919 | |
3920 /* | |
3921 * If we are not going to keep the backup file, don't | |
3922 * delete an existing one, try to use another name. | |
3923 * Change one character, just before the extension. | |
3924 */ | |
3925 if (!p_bk) | |
3926 { | |
3927 wp = backup + STRLEN(backup) - 1 | |
3928 - STRLEN(backup_ext); | |
3929 if (wp < backup) /* empty file name ??? */ | |
3930 wp = backup; | |
3931 *wp = 'z'; | |
3932 while (*wp > 'a' | |
3933 && mch_stat((char *)backup, &st_new) >= 0) | |
3934 --*wp; | |
3935 /* They all exist??? Must be something wrong. */ | |
3936 if (*wp == 'a') | |
3937 { | |
3938 vim_free(backup); | |
3939 backup = NULL; | |
3940 } | |
3941 } | |
3942 } | |
3943 break; | |
3944 } | |
3945 vim_free(rootname); | |
3946 | |
3947 /* | |
3948 * Try to create the backup file | |
3949 */ | |
3950 if (backup != NULL) | |
3951 { | |
3952 /* remove old backup, if present */ | |
3953 mch_remove(backup); | |
3954 /* Open with O_EXCL to avoid the file being created while | |
3955 * we were sleeping (symlink hacker attack?) */ | |
3956 bfd = mch_open((char *)backup, | |
557 | 3957 O_WRONLY|O_CREAT|O_EXTRA|O_EXCL|O_NOFOLLOW, |
3958 perm & 0777); | |
7 | 3959 if (bfd < 0) |
3960 { | |
3961 vim_free(backup); | |
3962 backup = NULL; | |
3963 } | |
3964 else | |
3965 { | |
3966 /* set file protection same as original file, but | |
3967 * strip s-bit */ | |
3968 (void)mch_setperm(backup, perm & 0777); | |
3969 | |
3970 #ifdef UNIX | |
3971 /* | |
3972 * Try to set the group of the backup same as the | |
3973 * original file. If this fails, set the protection | |
3974 * bits for the group same as the protection bits for | |
3975 * others. | |
3976 */ | |
557 | 3977 if (st_new.st_gid != st_old.st_gid |
7 | 3978 # ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */ |
557 | 3979 && fchown(bfd, (uid_t)-1, st_old.st_gid) != 0 |
7 | 3980 # endif |
3981 ) | |
3982 mch_setperm(backup, | |
3983 (perm & 0707) | ((perm & 07) << 3)); | |
5788 | 3984 # if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
1583 | 3985 mch_copy_sec(fname, backup); |
3986 # endif | |
7 | 3987 #endif |
3988 | |
3989 /* | |
3990 * copy the file. | |
3991 */ | |
3992 write_info.bw_fd = bfd; | |
3993 write_info.bw_buf = copybuf; | |
3994 #ifdef HAS_BW_FLAGS | |
3995 write_info.bw_flags = FIO_NOCONVERT; | |
3996 #endif | |
2664 | 3997 while ((write_info.bw_len = read_eintr(fd, copybuf, |
7 | 3998 BUFSIZE)) > 0) |
3999 { | |
4000 if (buf_write_bytes(&write_info) == FAIL) | |
4001 { | |
4002 errmsg = (char_u *)_("E506: Can't write to backup file (add ! to override)"); | |
4003 break; | |
4004 } | |
4005 ui_breakcheck(); | |
4006 if (got_int) | |
4007 { | |
4008 errmsg = (char_u *)_(e_interr); | |
4009 break; | |
4010 } | |
4011 } | |
4012 | |
4013 if (close(bfd) < 0 && errmsg == NULL) | |
4014 errmsg = (char_u *)_("E507: Close error for backup file (add ! to override)"); | |
4015 if (write_info.bw_len < 0) | |
4016 errmsg = (char_u *)_("E508: Can't read file for backup (add ! to override)"); | |
4017 #ifdef UNIX | |
4018 set_file_time(backup, st_old.st_atime, st_old.st_mtime); | |
4019 #endif | |
4020 #ifdef HAVE_ACL | |
4021 mch_set_acl(backup, acl); | |
4022 #endif | |
5788 | 4023 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
1583 | 4024 mch_copy_sec(fname, backup); |
4025 #endif | |
7 | 4026 break; |
4027 } | |
4028 } | |
4029 } | |
4030 nobackup: | |
4031 close(fd); /* ignore errors for closing read file */ | |
4032 vim_free(copybuf); | |
4033 | |
4034 if (backup == NULL && errmsg == NULL) | |
4035 errmsg = (char_u *)_("E509: Cannot create backup file (add ! to override)"); | |
4036 /* ignore errors when forceit is TRUE */ | |
4037 if ((some_error || errmsg != NULL) && !forceit) | |
4038 { | |
4039 retval = FAIL; | |
4040 goto fail; | |
4041 } | |
4042 errmsg = NULL; | |
4043 } | |
4044 else | |
4045 { | |
4046 char_u *dirp; | |
4047 char_u *p; | |
4048 char_u *rootname; | |
4049 | |
4050 /* | |
4051 * Make a backup by renaming the original file. | |
4052 */ | |
4053 /* | |
4054 * If 'cpoptions' includes the "W" flag, we don't want to | |
4055 * overwrite a read-only file. But rename may be possible | |
4056 * anyway, thus we need an extra check here. | |
4057 */ | |
4058 if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL) | |
4059 { | |
4060 errnum = (char_u *)"E504: "; | |
4061 errmsg = (char_u *)_(err_readonly); | |
4062 goto fail; | |
4063 } | |
4064 | |
4065 /* | |
4066 * | |
4067 * Form the backup file name - change path/fo.o.h to | |
4068 * path/fo.o.h.bak Try all directories in 'backupdir', first one | |
4069 * that works is used. | |
4070 */ | |
4071 dirp = p_bdir; | |
4072 while (*dirp) | |
4073 { | |
4074 /* | |
4075 * Isolate one directory name and make the backup file name. | |
4076 */ | |
4077 (void)copy_option_part(&dirp, IObuff, IOSIZE, ","); | |
4078 rootname = get_file_in_dir(fname, IObuff); | |
4079 if (rootname == NULL) | |
4080 backup = NULL; | |
4081 else | |
4082 { | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
4083 backup = buf_modname((buf->b_p_sn || buf->b_shortname), |
7 | 4084 rootname, backup_ext, FALSE); |
4085 vim_free(rootname); | |
4086 } | |
4087 | |
4088 if (backup != NULL) | |
4089 { | |
4090 /* | |
4091 * If we are not going to keep the backup file, don't | |
4092 * delete an existing one, try to use another name. | |
4093 * Change one character, just before the extension. | |
4094 */ | |
4095 if (!p_bk && mch_getperm(backup) >= 0) | |
4096 { | |
4097 p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext); | |
4098 if (p < backup) /* empty file name ??? */ | |
4099 p = backup; | |
4100 *p = 'z'; | |
4101 while (*p > 'a' && mch_getperm(backup) >= 0) | |
4102 --*p; | |
4103 /* They all exist??? Must be something wrong! */ | |
4104 if (*p == 'a') | |
4105 { | |
4106 vim_free(backup); | |
4107 backup = NULL; | |
4108 } | |
4109 } | |
4110 } | |
4111 if (backup != NULL) | |
4112 { | |
4113 /* | |
531 | 4114 * Delete any existing backup and move the current version |
4115 * to the backup. For safety, we don't remove the backup | |
4116 * until the write has finished successfully. And if the | |
4117 * 'backup' option is set, leave it around. | |
7 | 4118 */ |
4119 /* | |
4120 * If the renaming of the original file to the backup file | |
4121 * works, quit here. | |
4122 */ | |
4123 if (vim_rename(fname, backup) == 0) | |
4124 break; | |
4125 | |
4126 vim_free(backup); /* don't do the rename below */ | |
4127 backup = NULL; | |
4128 } | |
4129 } | |
4130 if (backup == NULL && !forceit) | |
4131 { | |
4132 errmsg = (char_u *)_("E510: Can't make backup file (add ! to override)"); | |
4133 goto fail; | |
4134 } | |
4135 } | |
4136 } | |
4137 | |
7410
08e62c4fc17d
commit https://github.com/vim/vim/commit/53076830fea6df737455523f7e235bfe4f79864d
Christian Brabandt <cb@256bit.org>
parents:
7408
diff
changeset
|
4138 #if defined(UNIX) |
7 | 4139 /* When using ":w!" and the file was read-only: make it writable */ |
4140 if (forceit && perm >= 0 && !(perm & 0200) && st_old.st_uid == getuid() | |
4141 && vim_strchr(p_cpo, CPO_FWRITE) == NULL) | |
4142 { | |
4143 perm |= 0200; | |
4144 (void)mch_setperm(fname, perm); | |
4145 made_writable = TRUE; | |
4146 } | |
4147 #endif | |
4148 | |
819 | 4149 /* When using ":w!" and writing to the current file, 'readonly' makes no |
164 | 4150 * sense, reset it, unless 'Z' appears in 'cpoptions'. */ |
4151 if (forceit && overwriting && vim_strchr(p_cpo, CPO_KEEPRO) == NULL) | |
7 | 4152 { |
4153 buf->b_p_ro = FALSE; | |
4154 #ifdef FEAT_TITLE | |
4155 need_maketitle = TRUE; /* set window title later */ | |
4156 #endif | |
4157 #ifdef FEAT_WINDOWS | |
4158 status_redraw_all(); /* redraw status lines later */ | |
4159 #endif | |
4160 } | |
4161 | |
4162 if (end > buf->b_ml.ml_line_count) | |
4163 end = buf->b_ml.ml_line_count; | |
4164 if (buf->b_ml.ml_flags & ML_EMPTY) | |
4165 start = end + 1; | |
4166 | |
4167 /* | |
4168 * If the original file is being overwritten, there is a small chance that | |
4169 * we crash in the middle of writing. Therefore the file is preserved now. | |
4170 * This makes all block numbers positive so that recovery does not need | |
4171 * the original file. | |
4172 * Don't do this if there is a backup file and we are exiting. | |
4173 */ | |
39 | 4174 if (reset_changed && !newfile && overwriting |
7 | 4175 && !(exiting && backup != NULL)) |
4176 { | |
4177 ml_preserve(buf, FALSE); | |
4178 if (got_int) | |
4179 { | |
4180 errmsg = (char_u *)_(e_interr); | |
4181 goto restore_backup; | |
4182 } | |
4183 } | |
4184 | |
4185 #ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */ | |
4186 /* | |
4187 * Before risking to lose the original file verify if there's | |
4188 * a resource fork to preserve, and if cannot be done warn | |
4189 * the users. This happens when overwriting without backups. | |
4190 */ | |
4191 if (backup == NULL && overwriting && !append) | |
4192 if (mch_has_resource_fork(fname)) | |
4193 { | |
4194 errmsg = (char_u *)_("E460: The resource fork would be lost (add ! to override)"); | |
4195 goto restore_backup; | |
4196 } | |
4197 #endif | |
4198 | |
4199 #ifdef VMS | |
4200 vms_remove_version(fname); /* remove version */ | |
4201 #endif | |
1651 | 4202 /* Default: write the file directly. May write to a temp file for |
7 | 4203 * multi-byte conversion. */ |
4204 wfname = fname; | |
4205 | |
4206 #ifdef FEAT_MBYTE | |
4207 /* Check for forced 'fileencoding' from "++opt=val" argument. */ | |
4208 if (eap != NULL && eap->force_enc != 0) | |
4209 { | |
4210 fenc = eap->cmd + eap->force_enc; | |
4211 fenc = enc_canonize(fenc); | |
4212 fenc_tofree = fenc; | |
4213 } | |
4214 else | |
4215 fenc = buf->b_p_fenc; | |
4216 | |
4217 /* | |
1948 | 4218 * Check if the file needs to be converted. |
7 | 4219 */ |
1948 | 4220 converted = need_conversion(fenc); |
7 | 4221 |
4222 /* | |
4223 * Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done. Or | |
4224 * Latin1 to Unicode conversion. This is handled in buf_write_bytes(). | |
4225 * Prepare the flags for it and allocate bw_conv_buf when needed. | |
4226 */ | |
4227 if (converted && (enc_utf8 || STRCMP(p_enc, "latin1") == 0)) | |
4228 { | |
4229 wb_flags = get_fio_flags(fenc); | |
4230 if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8)) | |
4231 { | |
4232 /* Need to allocate a buffer to translate into. */ | |
4233 if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8)) | |
4234 write_info.bw_conv_buflen = bufsize * 2; | |
4235 else /* FIO_UCS4 */ | |
4236 write_info.bw_conv_buflen = bufsize * 4; | |
4237 write_info.bw_conv_buf | |
4238 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4239 if (write_info.bw_conv_buf == NULL) | |
4240 end = 0; | |
4241 } | |
4242 } | |
4243 | |
4244 # ifdef WIN3264 | |
4245 if (converted && wb_flags == 0 && (wb_flags = get_win_fio_flags(fenc)) != 0) | |
4246 { | |
4247 /* Convert UTF-8 -> UCS-2 and UCS-2 -> DBCS. Worst-case * 4: */ | |
4248 write_info.bw_conv_buflen = bufsize * 4; | |
4249 write_info.bw_conv_buf | |
4250 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4251 if (write_info.bw_conv_buf == NULL) | |
4252 end = 0; | |
4253 } | |
4254 # endif | |
4255 | |
4256 # ifdef MACOS_X | |
4257 if (converted && wb_flags == 0 && (wb_flags = get_mac_fio_flags(fenc)) != 0) | |
4258 { | |
4259 write_info.bw_conv_buflen = bufsize * 3; | |
4260 write_info.bw_conv_buf | |
4261 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4262 if (write_info.bw_conv_buf == NULL) | |
4263 end = 0; | |
4264 } | |
4265 # endif | |
4266 | |
4267 # if defined(FEAT_EVAL) || defined(USE_ICONV) | |
4268 if (converted && wb_flags == 0) | |
4269 { | |
4270 # ifdef USE_ICONV | |
4271 /* | |
4272 * Use iconv() conversion when conversion is needed and it's not done | |
4273 * internally. | |
4274 */ | |
4275 write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc, | |
4276 enc_utf8 ? (char_u *)"utf-8" : p_enc); | |
4277 if (write_info.bw_iconv_fd != (iconv_t)-1) | |
4278 { | |
4279 /* We're going to use iconv(), allocate a buffer to convert in. */ | |
4280 write_info.bw_conv_buflen = bufsize * ICONV_MULT; | |
4281 write_info.bw_conv_buf | |
4282 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4283 if (write_info.bw_conv_buf == NULL) | |
4284 end = 0; | |
4285 write_info.bw_first = TRUE; | |
4286 } | |
4287 # ifdef FEAT_EVAL | |
4288 else | |
4289 # endif | |
4290 # endif | |
4291 | |
4292 # ifdef FEAT_EVAL | |
4293 /* | |
4294 * When the file needs to be converted with 'charconvert' after | |
4295 * writing, write to a temp file instead and let the conversion | |
4296 * overwrite the original file. | |
4297 */ | |
4298 if (*p_ccv != NUL) | |
4299 { | |
6721 | 4300 wfname = vim_tempname('w', FALSE); |
7 | 4301 if (wfname == NULL) /* Can't write without a tempfile! */ |
4302 { | |
4303 errmsg = (char_u *)_("E214: Can't find temp file for writing"); | |
4304 goto restore_backup; | |
4305 } | |
4306 } | |
4307 # endif | |
4308 } | |
4309 # endif | |
4310 if (converted && wb_flags == 0 | |
4311 # ifdef USE_ICONV | |
4312 && write_info.bw_iconv_fd == (iconv_t)-1 | |
4313 # endif | |
4314 # ifdef FEAT_EVAL | |
4315 && wfname == fname | |
4316 # endif | |
4317 ) | |
4318 { | |
4319 if (!forceit) | |
4320 { | |
4321 errmsg = (char_u *)_("E213: Cannot convert (add ! to write without conversion)"); | |
4322 goto restore_backup; | |
4323 } | |
4324 notconverted = TRUE; | |
4325 } | |
4326 #endif | |
4327 | |
4328 /* | |
4329 * Open the file "wfname" for writing. | |
4330 * We may try to open the file twice: If we can't write to the | |
4331 * file and forceit is TRUE we delete the existing file and try to create | |
4332 * a new one. If this still fails we may have lost the original file! | |
4333 * (this may happen when the user reached his quotum for number of files). | |
4334 * Appending will fail if the file does not exist and forceit is FALSE. | |
4335 */ | |
4336 while ((fd = mch_open((char *)wfname, O_WRONLY | O_EXTRA | (append | |
4337 ? (forceit ? (O_APPEND | O_CREAT) : O_APPEND) | |
4338 : (O_CREAT | O_TRUNC)) | |
216 | 4339 , perm < 0 ? 0666 : (perm & 0777))) < 0) |
7 | 4340 { |
4341 /* | |
4342 * A forced write will try to create a new file if the old one is | |
4343 * still readonly. This may also happen when the directory is | |
4344 * read-only. In that case the mch_remove() will fail. | |
4345 */ | |
4346 if (errmsg == NULL) | |
4347 { | |
4348 #ifdef UNIX | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
4349 stat_T st; |
7 | 4350 |
4351 /* Don't delete the file when it's a hard or symbolic link. */ | |
4352 if ((!newfile && st_old.st_nlink > 1) | |
4353 || (mch_lstat((char *)fname, &st) == 0 | |
4354 && (st.st_dev != st_old.st_dev | |
4355 || st.st_ino != st_old.st_ino))) | |
4356 errmsg = (char_u *)_("E166: Can't open linked file for writing"); | |
4357 else | |
4358 #endif | |
4359 { | |
4360 errmsg = (char_u *)_("E212: Can't open file for writing"); | |
4361 if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL | |
4362 && perm >= 0) | |
4363 { | |
4364 #ifdef UNIX | |
4365 /* we write to the file, thus it should be marked | |
4366 writable after all */ | |
4367 if (!(perm & 0200)) | |
4368 made_writable = TRUE; | |
4369 perm |= 0200; | |
4370 if (st_old.st_uid != getuid() || st_old.st_gid != getgid()) | |
4371 perm &= 0777; | |
4372 #endif | |
4373 if (!append) /* don't remove when appending */ | |
4374 mch_remove(wfname); | |
4375 continue; | |
4376 } | |
4377 } | |
4378 } | |
4379 | |
4380 restore_backup: | |
4381 { | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
4382 stat_T st; |
7 | 4383 |
4384 /* | |
4385 * If we failed to open the file, we don't need a backup. Throw it | |
4386 * away. If we moved or removed the original file try to put the | |
4387 * backup in its place. | |
4388 */ | |
4389 if (backup != NULL && wfname == fname) | |
4390 { | |
4391 if (backup_copy) | |
4392 { | |
4393 /* | |
4394 * There is a small chance that we removed the original, | |
4395 * try to move the copy in its place. | |
4396 * This may not work if the vim_rename() fails. | |
4397 * In that case we leave the copy around. | |
4398 */ | |
4399 /* If file does not exist, put the copy in its place */ | |
4400 if (mch_stat((char *)fname, &st) < 0) | |
4401 vim_rename(backup, fname); | |
4402 /* if original file does exist throw away the copy */ | |
4403 if (mch_stat((char *)fname, &st) >= 0) | |
4404 mch_remove(backup); | |
4405 } | |
4406 else | |
4407 { | |
4408 /* try to put the original file back */ | |
4409 vim_rename(backup, fname); | |
4410 } | |
4411 } | |
4412 | |
4413 /* if original file no longer exists give an extra warning */ | |
4414 if (!newfile && mch_stat((char *)fname, &st) < 0) | |
4415 end = 0; | |
4416 } | |
4417 | |
4418 #ifdef FEAT_MBYTE | |
4419 if (wfname != fname) | |
4420 vim_free(wfname); | |
4421 #endif | |
4422 goto fail; | |
4423 } | |
4424 errmsg = NULL; | |
4425 | |
4426 #if defined(MACOS_CLASSIC) || defined(WIN3264) | |
4427 /* TODO: Is it need for MACOS_X? (Dany) */ | |
4428 /* | |
4429 * On macintosh copy the original files attributes (i.e. the backup) | |
1201 | 4430 * This is done in order to preserve the resource fork and the |
4431 * Finder attribute (label, comments, custom icons, file creator) | |
7 | 4432 */ |
4433 if (backup != NULL && overwriting && !append) | |
4434 { | |
4435 if (backup_copy) | |
4436 (void)mch_copy_file_attribute(wfname, backup); | |
4437 else | |
4438 (void)mch_copy_file_attribute(backup, wfname); | |
4439 } | |
4440 | |
4441 if (!overwriting && !append) | |
4442 { | |
4443 if (buf->b_ffname != NULL) | |
4444 (void)mch_copy_file_attribute(buf->b_ffname, wfname); | |
1201 | 4445 /* Should copy resource fork */ |
7 | 4446 } |
4447 #endif | |
4448 | |
4449 write_info.bw_fd = fd; | |
4450 | |
4451 #ifdef FEAT_CRYPT | |
2267 | 4452 if (*buf->b_p_key != NUL && !filtering) |
7 | 4453 { |
6122 | 4454 char_u *header; |
4455 int header_len; | |
4456 | |
4457 buf->b_cryptstate = crypt_create_for_writing(crypt_get_method_nr(buf), | |
4458 buf->b_p_key, &header, &header_len); | |
4459 if (buf->b_cryptstate == NULL || header == NULL) | |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4460 end = 0; |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
4461 else |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
4462 { |
6122 | 4463 /* Write magic number, so that Vim knows how this file is |
4464 * encrypted when reading it back. */ | |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4465 write_info.bw_buf = header; |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4466 write_info.bw_len = header_len; |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4467 write_info.bw_flags = FIO_NOCONVERT; |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4468 if (buf_write_bytes(&write_info) == FAIL) |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4469 end = 0; |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4470 wb_flags |= FIO_ENCRYPTED; |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4471 vim_free(header); |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4472 } |
7 | 4473 } |
4474 #endif | |
4475 | |
4476 write_info.bw_buf = buffer; | |
4477 nchars = 0; | |
4478 | |
4479 /* use "++bin", "++nobin" or 'binary' */ | |
4480 if (eap != NULL && eap->force_bin != 0) | |
4481 write_bin = (eap->force_bin == FORCE_BIN); | |
4482 else | |
4483 write_bin = buf->b_p_bin; | |
4484 | |
4485 #ifdef FEAT_MBYTE | |
4486 /* | |
4487 * The BOM is written just after the encryption magic number. | |
24 | 4488 * Skip it when appending and the file already existed, the BOM only makes |
4489 * sense at the start of the file. | |
7 | 4490 */ |
24 | 4491 if (buf->b_p_bomb && !write_bin && (!append || perm < 0)) |
7 | 4492 { |
4493 write_info.bw_len = make_bom(buffer, fenc); | |
4494 if (write_info.bw_len > 0) | |
4495 { | |
4496 /* don't convert, do encryption */ | |
4497 write_info.bw_flags = FIO_NOCONVERT | wb_flags; | |
4498 if (buf_write_bytes(&write_info) == FAIL) | |
4499 end = 0; | |
4500 else | |
4501 nchars += write_info.bw_len; | |
4502 } | |
4503 } | |
1947 | 4504 write_info.bw_start_lnum = start; |
7 | 4505 #endif |
4506 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4507 #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
|
4508 write_undo_file = (buf->b_p_udf && overwriting && !append |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4509 && !filtering && reset_changed); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4510 if (write_undo_file) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4511 /* Prepare for computing the hash value of the text. */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4512 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
|
4513 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4514 |
7 | 4515 write_info.bw_len = bufsize; |
4516 #ifdef HAS_BW_FLAGS | |
4517 write_info.bw_flags = wb_flags; | |
4518 #endif | |
4519 fileformat = get_fileformat_force(buf, eap); | |
4520 s = buffer; | |
4521 len = 0; | |
4522 for (lnum = start; lnum <= end; ++lnum) | |
4523 { | |
4524 /* | |
4525 * The next while loop is done once for each character written. | |
4526 * Keep it fast! | |
4527 */ | |
4528 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
|
4529 #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
|
4530 if (write_undo_file) |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
4531 sha256_update(&sha_ctx, ptr + 1, (UINT32_T)(STRLEN(ptr + 1) + 1)); |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4532 #endif |
7 | 4533 while ((c = *++ptr) != NUL) |
4534 { | |
4535 if (c == NL) | |
4536 *s = NUL; /* replace newlines with NULs */ | |
4537 else if (c == CAR && fileformat == EOL_MAC) | |
4538 *s = NL; /* Mac: replace CRs with NLs */ | |
4539 else | |
4540 *s = c; | |
4541 ++s; | |
4542 if (++len != bufsize) | |
4543 continue; | |
4544 if (buf_write_bytes(&write_info) == FAIL) | |
4545 { | |
4546 end = 0; /* write error: break loop */ | |
4547 break; | |
4548 } | |
4549 nchars += bufsize; | |
4550 s = buffer; | |
4551 len = 0; | |
1947 | 4552 #ifdef FEAT_MBYTE |
4553 write_info.bw_start_lnum = lnum; | |
4554 #endif | |
7 | 4555 } |
4556 /* write failed or last line has no EOL: stop here */ | |
4557 if (end == 0 | |
4558 || (lnum == end | |
6933 | 4559 && (write_bin || !buf->b_p_fixeol) |
2707 | 4560 && (lnum == buf->b_no_eol_lnum |
7 | 4561 || (lnum == buf->b_ml.ml_line_count && !buf->b_p_eol)))) |
4562 { | |
4563 ++lnum; /* written the line, count it */ | |
4564 no_eol = TRUE; | |
4565 break; | |
4566 } | |
4567 if (fileformat == EOL_UNIX) | |
4568 *s++ = NL; | |
4569 else | |
4570 { | |
4571 *s++ = CAR; /* EOL_MAC or EOL_DOS: write CR */ | |
4572 if (fileformat == EOL_DOS) /* write CR-NL */ | |
4573 { | |
4574 if (++len == bufsize) | |
4575 { | |
4576 if (buf_write_bytes(&write_info) == FAIL) | |
4577 { | |
4578 end = 0; /* write error: break loop */ | |
4579 break; | |
4580 } | |
4581 nchars += bufsize; | |
4582 s = buffer; | |
4583 len = 0; | |
4584 } | |
4585 *s++ = NL; | |
4586 } | |
4587 } | |
4588 if (++len == bufsize && end) | |
4589 { | |
4590 if (buf_write_bytes(&write_info) == FAIL) | |
4591 { | |
4592 end = 0; /* write error: break loop */ | |
4593 break; | |
4594 } | |
4595 nchars += bufsize; | |
4596 s = buffer; | |
4597 len = 0; | |
4598 | |
4599 ui_breakcheck(); | |
4600 if (got_int) | |
4601 { | |
4602 end = 0; /* Interrupted, break loop */ | |
4603 break; | |
4604 } | |
4605 } | |
4606 #ifdef VMS | |
4607 /* | |
4608 * On VMS there is a problem: newlines get added when writing blocks | |
4609 * at a time. Fix it by writing a line at a time. | |
4610 * This is much slower! | |
22 | 4611 * Explanation: VAX/DECC RTL insists that records in some RMS |
4612 * structures end with a newline (carriage return) character, and if | |
4613 * they don't it adds one. | |
4614 * With other RMS structures it works perfect without this fix. | |
7 | 4615 */ |
1431 | 4616 if (buf->b_fab_rfm == FAB$C_VFC |
4617 || ((buf->b_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0)) | |
22 | 4618 { |
4619 int b2write; | |
4620 | |
4621 buf->b_fab_mrs = (buf->b_fab_mrs == 0 | |
4622 ? MIN(4096, bufsize) | |
4623 : MIN(buf->b_fab_mrs, bufsize)); | |
4624 | |
4625 b2write = len; | |
4626 while (b2write > 0) | |
7 | 4627 { |
22 | 4628 write_info.bw_len = MIN(b2write, buf->b_fab_mrs); |
4629 if (buf_write_bytes(&write_info) == FAIL) | |
4630 { | |
4631 end = 0; | |
4632 break; | |
4633 } | |
4634 b2write -= MIN(b2write, buf->b_fab_mrs); | |
7 | 4635 } |
4636 write_info.bw_len = bufsize; | |
4637 nchars += len; | |
4638 s = buffer; | |
4639 len = 0; | |
4640 } | |
4641 #endif | |
4642 } | |
4643 if (len > 0 && end > 0) | |
4644 { | |
4645 write_info.bw_len = len; | |
4646 if (buf_write_bytes(&write_info) == FAIL) | |
4647 end = 0; /* write error */ | |
4648 nchars += len; | |
4649 } | |
4650 | |
4651 #if defined(UNIX) && defined(HAVE_FSYNC) | |
4652 /* On many journalling file systems there is a bug that causes both the | |
4653 * original and the backup file to be lost when halting the system right | |
4654 * after writing the file. That's because only the meta-data is | |
4655 * journalled. Syncing the file slows down the system, but assures it has | |
10 | 4656 * been written to disk and we don't lose it. |
4657 * For a device do try the fsync() but don't complain if it does not work | |
36 | 4658 * (could be a pipe). |
4659 * If the 'fsync' option is FALSE, don't fsync(). Useful for laptops. */ | |
4660 if (p_fs && fsync(fd) != 0 && !device) | |
7 | 4661 { |
4662 errmsg = (char_u *)_("E667: Fsync failed"); | |
4663 end = 0; | |
4664 } | |
4665 #endif | |
4666 | |
5788 | 4667 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
1583 | 4668 /* Probably need to set the security context. */ |
4669 if (!backup_copy) | |
4670 mch_copy_sec(backup, wfname); | |
4671 #endif | |
4672 | |
557 | 4673 #ifdef UNIX |
4674 /* When creating a new file, set its owner/group to that of the original | |
4675 * file. Get the new device and inode number. */ | |
4676 if (backup != NULL && !backup_copy) | |
4677 { | |
4678 # ifdef HAVE_FCHOWN | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
4679 stat_T st; |
557 | 4680 |
4681 /* don't change the owner when it's already OK, some systems remove | |
4682 * permission or ACL stuff */ | |
4683 if (mch_stat((char *)wfname, &st) < 0 | |
4684 || st.st_uid != st_old.st_uid | |
4685 || st.st_gid != st_old.st_gid) | |
4686 { | |
1757 | 4687 ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
557 | 4688 if (perm >= 0) /* set permission again, may have changed */ |
4689 (void)mch_setperm(wfname, perm); | |
4690 } | |
4691 # endif | |
4692 buf_setino(buf); | |
4693 } | |
1873 | 4694 else if (!buf->b_dev_valid) |
617 | 4695 /* Set the inode when creating a new file. */ |
4696 buf_setino(buf); | |
557 | 4697 #endif |
4698 | |
7 | 4699 if (close(fd) != 0) |
4700 { | |
4701 errmsg = (char_u *)_("E512: Close failed"); | |
4702 end = 0; | |
4703 } | |
4704 | |
4705 #ifdef UNIX | |
4706 if (made_writable) | |
4707 perm &= ~0200; /* reset 'w' bit for security reasons */ | |
4708 #endif | |
4709 if (perm >= 0) /* set perm. of new file same as old file */ | |
4710 (void)mch_setperm(wfname, perm); | |
4711 #ifdef HAVE_ACL | |
9509
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
4712 /* |
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
4713 * Probably need to set the ACL before changing the user (can't set the |
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
4714 * ACL on a file the user doesn't own). |
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
4715 * On Solaris, with ZFS and the aclmode property set to "discard" (the |
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
4716 * default), chmod() discards all part of a file's ACL that don't represent |
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
4717 * the mode of the file. It's non-trivial for us to discover whether we're |
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
4718 * in that situation, so we simply always re-set the ACL. |
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
4719 */ |
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
4720 # ifndef HAVE_SOLARIS_ZFS_ACL |
7 | 4721 if (!backup_copy) |
9509
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
4722 # endif |
7 | 4723 mch_set_acl(wfname, acl); |
4724 #endif | |
2267 | 4725 #ifdef FEAT_CRYPT |
6122 | 4726 if (buf->b_cryptstate != NULL) |
4727 { | |
4728 crypt_free_state(buf->b_cryptstate); | |
4729 buf->b_cryptstate = NULL; | |
4730 } | |
4731 #endif | |
7 | 4732 |
4733 #if defined(FEAT_MBYTE) && defined(FEAT_EVAL) | |
4734 if (wfname != fname) | |
4735 { | |
4736 /* | |
4737 * The file was written to a temp file, now it needs to be converted | |
4738 * with 'charconvert' to (overwrite) the output file. | |
4739 */ | |
4740 if (end != 0) | |
4741 { | |
4742 if (eval_charconvert(enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc, | |
4743 wfname, fname) == FAIL) | |
4744 { | |
4745 write_info.bw_conv_error = TRUE; | |
4746 end = 0; | |
4747 } | |
4748 } | |
4749 mch_remove(wfname); | |
4750 vim_free(wfname); | |
4751 } | |
4752 #endif | |
4753 | |
4754 if (end == 0) | |
4755 { | |
4756 if (errmsg == NULL) | |
4757 { | |
4758 #ifdef FEAT_MBYTE | |
4759 if (write_info.bw_conv_error) | |
1947 | 4760 { |
4761 if (write_info.bw_conv_error_lnum == 0) | |
4762 errmsg = (char_u *)_("E513: write error, conversion failed (make 'fenc' empty to override)"); | |
4763 else | |
4764 { | |
4765 errmsg_allocated = TRUE; | |
4766 errmsg = alloc(300); | |
4767 vim_snprintf((char *)errmsg, 300, _("E513: write error, conversion failed in line %ld (make 'fenc' empty to override)"), | |
4768 (long)write_info.bw_conv_error_lnum); | |
4769 } | |
4770 } | |
7 | 4771 else |
4772 #endif | |
4773 if (got_int) | |
4774 errmsg = (char_u *)_(e_interr); | |
4775 else | |
4776 errmsg = (char_u *)_("E514: write error (file system full?)"); | |
4777 } | |
4778 | |
4779 /* | |
4780 * If we have a backup file, try to put it in place of the new file, | |
1698 | 4781 * because the new file is probably corrupt. This avoids losing the |
7 | 4782 * original file when trying to make a backup when writing the file a |
4783 * second time. | |
4784 * When "backup_copy" is set we need to copy the backup over the new | |
4785 * file. Otherwise rename the backup file. | |
4786 * If this is OK, don't give the extra warning message. | |
4787 */ | |
4788 if (backup != NULL) | |
4789 { | |
4790 if (backup_copy) | |
4791 { | |
4792 /* This may take a while, if we were interrupted let the user | |
4793 * know we got the message. */ | |
4794 if (got_int) | |
4795 { | |
4796 MSG(_(e_interr)); | |
4797 out_flush(); | |
4798 } | |
4799 if ((fd = mch_open((char *)backup, O_RDONLY | O_EXTRA, 0)) >= 0) | |
4800 { | |
4801 if ((write_info.bw_fd = mch_open((char *)fname, | |
194 | 4802 O_WRONLY | O_CREAT | O_TRUNC | O_EXTRA, |
4803 perm & 0777)) >= 0) | |
7 | 4804 { |
4805 /* copy the file. */ | |
4806 write_info.bw_buf = smallbuf; | |
4807 #ifdef HAS_BW_FLAGS | |
4808 write_info.bw_flags = FIO_NOCONVERT; | |
4809 #endif | |
2664 | 4810 while ((write_info.bw_len = read_eintr(fd, smallbuf, |
7 | 4811 SMBUFSIZE)) > 0) |
4812 if (buf_write_bytes(&write_info) == FAIL) | |
4813 break; | |
4814 | |
4815 if (close(write_info.bw_fd) >= 0 | |
4816 && write_info.bw_len == 0) | |
4817 end = 1; /* success */ | |
4818 } | |
4819 close(fd); /* ignore errors for closing read file */ | |
4820 } | |
4821 } | |
4822 else | |
4823 { | |
4824 if (vim_rename(backup, fname) == 0) | |
4825 end = 1; | |
4826 } | |
4827 } | |
4828 goto fail; | |
4829 } | |
4830 | |
4831 lnum -= start; /* compute number of written lines */ | |
4832 --no_wait_return; /* may wait for return now */ | |
4833 | |
4834 #if !(defined(UNIX) || defined(VMS)) | |
4835 fname = sfname; /* use shortname now, for the messages */ | |
4836 #endif | |
4837 if (!filtering) | |
4838 { | |
4839 msg_add_fname(buf, fname); /* put fname in IObuff with quotes */ | |
4840 c = FALSE; | |
4841 #ifdef FEAT_MBYTE | |
4842 if (write_info.bw_conv_error) | |
4843 { | |
4844 STRCAT(IObuff, _(" CONVERSION ERROR")); | |
4845 c = TRUE; | |
1947 | 4846 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
|
4847 vim_snprintf_add((char *)IObuff, IOSIZE, _(" in line %ld;"), |
1947 | 4848 (long)write_info.bw_conv_error_lnum); |
7 | 4849 } |
4850 else if (notconverted) | |
4851 { | |
4852 STRCAT(IObuff, _("[NOT converted]")); | |
4853 c = TRUE; | |
4854 } | |
4855 else if (converted) | |
4856 { | |
4857 STRCAT(IObuff, _("[converted]")); | |
4858 c = TRUE; | |
4859 } | |
4860 #endif | |
4861 if (device) | |
4862 { | |
4863 STRCAT(IObuff, _("[Device]")); | |
4864 c = TRUE; | |
4865 } | |
4866 else if (newfile) | |
4867 { | |
4868 STRCAT(IObuff, shortmess(SHM_NEW) ? _("[New]") : _("[New File]")); | |
4869 c = TRUE; | |
4870 } | |
4871 if (no_eol) | |
4872 { | |
4873 msg_add_eol(); | |
4874 c = TRUE; | |
4875 } | |
4876 /* may add [unix/dos/mac] */ | |
4877 if (msg_add_fileformat(fileformat)) | |
4878 c = TRUE; | |
4879 #ifdef FEAT_CRYPT | |
4880 if (wb_flags & FIO_ENCRYPTED) | |
4881 { | |
6122 | 4882 crypt_append_msg(buf); |
7 | 4883 c = TRUE; |
4884 } | |
4885 #endif | |
4886 msg_add_lines(c, (long)lnum, nchars); /* add line/char count */ | |
4887 if (!shortmess(SHM_WRITE)) | |
4888 { | |
4889 if (append) | |
4890 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [a]") : _(" appended")); | |
4891 else | |
4892 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [w]") : _(" written")); | |
4893 } | |
4894 | |
679 | 4895 set_keep_msg(msg_trunc_attr(IObuff, FALSE, 0), 0); |
7 | 4896 } |
4897 | |
39 | 4898 /* When written everything correctly: reset 'modified'. Unless not |
4899 * writing to the original file and '+' is not in 'cpoptions'. */ | |
589 | 4900 if (reset_changed && whole && !append |
7 | 4901 #ifdef FEAT_MBYTE |
4902 && !write_info.bw_conv_error | |
4903 #endif | |
39 | 4904 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL) |
4905 ) | |
7 | 4906 { |
4907 unchanged(buf, TRUE); | |
6320 | 4908 #ifdef FEAT_AUTOCMD |
4909 /* buf->b_changedtick is always incremented in unchanged() but that | |
4910 * should not trigger a TextChanged event. */ | |
4911 if (last_changedtick + 1 == buf->b_changedtick | |
4912 && last_changedtick_buf == buf) | |
4913 last_changedtick = buf->b_changedtick; | |
4914 #endif | |
7 | 4915 u_unchanged(buf); |
2281
e41433ea71df
Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents:
2280
diff
changeset
|
4916 u_update_save_nr(buf); |
7 | 4917 } |
4918 | |
4919 /* | |
4920 * If written to the current file, update the timestamp of the swap file | |
4921 * and reset the BF_WRITE_MASK flags. Also sets buf->b_mtime. | |
4922 */ | |
4923 if (overwriting) | |
4924 { | |
4925 ml_timestamp(buf); | |
589 | 4926 if (append) |
4927 buf->b_flags &= ~BF_NEW; | |
4928 else | |
4929 buf->b_flags &= ~BF_WRITE_MASK; | |
7 | 4930 } |
4931 | |
4932 /* | |
4933 * If we kept a backup until now, and we are in patch mode, then we make | |
4934 * the backup file our 'original' file. | |
4935 */ | |
4936 if (*p_pm && dobackup) | |
4937 { | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
4938 char *org = (char *)buf_modname((buf->b_p_sn || buf->b_shortname), |
7 | 4939 fname, p_pm, FALSE); |
4940 | |
4941 if (backup != NULL) | |
4942 { | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
4943 stat_T st; |
7 | 4944 |
4945 /* | |
4946 * If the original file does not exist yet | |
4947 * the current backup file becomes the original file | |
4948 */ | |
4949 if (org == NULL) | |
4950 EMSG(_("E205: Patchmode: can't save original file")); | |
4951 else if (mch_stat(org, &st) < 0) | |
4952 { | |
4953 vim_rename(backup, (char_u *)org); | |
4954 vim_free(backup); /* don't delete the file */ | |
4955 backup = NULL; | |
4956 #ifdef UNIX | |
4957 set_file_time((char_u *)org, st_old.st_atime, st_old.st_mtime); | |
4958 #endif | |
4959 } | |
4960 } | |
4961 /* | |
4962 * If there is no backup file, remember that a (new) file was | |
4963 * created. | |
4964 */ | |
4965 else | |
4966 { | |
4967 int empty_fd; | |
4968 | |
4969 if (org == NULL | |
557 | 4970 || (empty_fd = mch_open(org, |
4971 O_CREAT | O_EXTRA | O_EXCL | O_NOFOLLOW, | |
216 | 4972 perm < 0 ? 0666 : (perm & 0777))) < 0) |
7 | 4973 EMSG(_("E206: patchmode: can't touch empty original file")); |
4974 else | |
4975 close(empty_fd); | |
4976 } | |
4977 if (org != NULL) | |
4978 { | |
4979 mch_setperm((char_u *)org, mch_getperm(fname) & 0777); | |
4980 vim_free(org); | |
4981 } | |
4982 } | |
4983 | |
4984 /* | |
4985 * Remove the backup unless 'backup' option is set | |
4986 */ | |
4987 if (!p_bk && backup != NULL && mch_remove(backup) != 0) | |
4988 EMSG(_("E207: Can't delete backup file")); | |
4989 | |
4990 #ifdef FEAT_SUN_WORKSHOP | |
4991 if (usingSunWorkShop) | |
4992 workshop_file_saved((char *) ffname); | |
4993 #endif | |
4994 | |
4995 goto nofail; | |
4996 | |
4997 /* | |
4998 * Finish up. We get here either after failure or success. | |
4999 */ | |
5000 fail: | |
5001 --no_wait_return; /* may wait for return now */ | |
5002 nofail: | |
5003 | |
5004 /* Done saving, we accept changed buffer warnings again */ | |
5005 buf->b_saving = FALSE; | |
5006 | |
5007 vim_free(backup); | |
5008 if (buffer != smallbuf) | |
5009 vim_free(buffer); | |
5010 #ifdef FEAT_MBYTE | |
5011 vim_free(fenc_tofree); | |
5012 vim_free(write_info.bw_conv_buf); | |
5013 # ifdef USE_ICONV | |
5014 if (write_info.bw_iconv_fd != (iconv_t)-1) | |
5015 { | |
5016 iconv_close(write_info.bw_iconv_fd); | |
5017 write_info.bw_iconv_fd = (iconv_t)-1; | |
5018 } | |
5019 # endif | |
5020 #endif | |
5021 #ifdef HAVE_ACL | |
5022 mch_free_acl(acl); | |
5023 #endif | |
5024 | |
5025 if (errmsg != NULL) | |
5026 { | |
835 | 5027 int numlen = errnum != NULL ? (int)STRLEN(errnum) : 0; |
7 | 5028 |
5029 attr = hl_attr(HLF_E); /* set highlight for error messages */ | |
5030 msg_add_fname(buf, | |
5031 #ifndef UNIX | |
5032 sfname | |
5033 #else | |
5034 fname | |
5035 #endif | |
5036 ); /* put file name in IObuff with quotes */ | |
5037 if (STRLEN(IObuff) + STRLEN(errmsg) + numlen >= IOSIZE) | |
5038 IObuff[IOSIZE - STRLEN(errmsg) - numlen - 1] = NUL; | |
5039 /* If the error message has the form "is ...", put the error number in | |
5040 * front of the file name. */ | |
5041 if (errnum != NULL) | |
5042 { | |
1620 | 5043 STRMOVE(IObuff + numlen, IObuff); |
7 | 5044 mch_memmove(IObuff, errnum, (size_t)numlen); |
5045 } | |
5046 STRCAT(IObuff, errmsg); | |
5047 emsg(IObuff); | |
1947 | 5048 if (errmsg_allocated) |
5049 vim_free(errmsg); | |
7 | 5050 |
5051 retval = FAIL; | |
5052 if (end == 0) | |
5053 { | |
5054 MSG_PUTS_ATTR(_("\nWARNING: Original file may be lost or damaged\n"), | |
5055 attr | MSG_HIST); | |
5056 MSG_PUTS_ATTR(_("don't quit the editor until the file is successfully written!"), | |
5057 attr | MSG_HIST); | |
5058 | |
5059 /* Update the timestamp to avoid an "overwrite changed file" | |
5060 * prompt when writing again. */ | |
5061 if (mch_stat((char *)fname, &st_old) >= 0) | |
5062 { | |
5063 buf_store_time(buf, &st_old, fname); | |
5064 buf->b_mtime_read = buf->b_mtime; | |
5065 } | |
5066 } | |
5067 } | |
5068 msg_scroll = msg_save; | |
5069 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5070 #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
|
5071 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5072 * 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
|
5073 * file. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5074 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5075 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
|
5076 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5077 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
|
5078 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5079 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
|
5080 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
|
5081 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5082 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5083 |
7 | 5084 #ifdef FEAT_AUTOCMD |
5085 #ifdef FEAT_EVAL | |
5086 if (!should_abort(retval)) | |
5087 #else | |
5088 if (!got_int) | |
5089 #endif | |
5090 { | |
5091 aco_save_T aco; | |
5092 | |
3482 | 5093 curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */ |
5094 | |
7 | 5095 /* |
5096 * Apply POST autocommands. | |
5097 * Careful: The autocommands may call buf_write() recursively! | |
5098 */ | |
5099 aucmd_prepbuf(&aco, buf); | |
5100 | |
5101 if (append) | |
5102 apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname, | |
5103 FALSE, curbuf, eap); | |
5104 else if (filtering) | |
5105 apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname, | |
5106 FALSE, curbuf, eap); | |
5107 else if (reset_changed && whole) | |
5108 apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname, | |
5109 FALSE, curbuf, eap); | |
5110 else | |
5111 apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname, | |
5112 FALSE, curbuf, eap); | |
5113 | |
5114 /* restore curwin/curbuf and a few other things */ | |
5115 aucmd_restbuf(&aco); | |
5116 | |
5117 #ifdef FEAT_EVAL | |
5118 if (aborting()) /* autocmds may abort script processing */ | |
5119 retval = FALSE; | |
5120 #endif | |
5121 } | |
5122 #endif | |
5123 | |
5124 got_int |= prev_got_int; | |
5125 | |
5126 #ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */ | |
5127 /* Update machine specific information. */ | |
5128 mch_post_buffer_write(buf); | |
5129 #endif | |
5130 return retval; | |
5131 } | |
5132 | |
5133 /* | |
633 | 5134 * Set the name of the current buffer. Use when the buffer doesn't have a |
5135 * name and a ":r" or ":w" command with a file name is used. | |
5136 */ | |
5137 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5138 set_rw_fname(char_u *fname, char_u *sfname) |
633 | 5139 { |
5140 #ifdef FEAT_AUTOCMD | |
1905 | 5141 buf_T *buf = curbuf; |
5142 | |
633 | 5143 /* It's like the unnamed buffer is deleted.... */ |
5144 if (curbuf->b_p_bl) | |
5145 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); | |
5146 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf); | |
5147 # ifdef FEAT_EVAL | |
5148 if (aborting()) /* autocmds may abort script processing */ | |
5149 return FAIL; | |
5150 # endif | |
1905 | 5151 if (curbuf != buf) |
5152 { | |
5153 /* We are in another buffer now, don't do the renaming. */ | |
5154 EMSG(_(e_auchangedbuf)); | |
5155 return FAIL; | |
5156 } | |
633 | 5157 #endif |
5158 | |
5159 if (setfname(curbuf, fname, sfname, FALSE) == OK) | |
5160 curbuf->b_flags |= BF_NOTEDITED; | |
5161 | |
5162 #ifdef FEAT_AUTOCMD | |
5163 /* ....and a new named one is created */ | |
5164 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf); | |
5165 if (curbuf->b_p_bl) | |
5166 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf); | |
5167 # ifdef FEAT_EVAL | |
5168 if (aborting()) /* autocmds may abort script processing */ | |
5169 return FAIL; | |
5170 # endif | |
5171 | |
5172 /* Do filetype detection now if 'filetype' is empty. */ | |
5173 if (*curbuf->b_p_ft == NUL) | |
5174 { | |
819 | 5175 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
|
5176 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL); |
717 | 5177 do_modelines(0); |
633 | 5178 } |
5179 #endif | |
5180 | |
5181 return OK; | |
5182 } | |
5183 | |
5184 /* | |
7 | 5185 * Put file name into IObuff with quotes. |
5186 */ | |
33 | 5187 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5188 msg_add_fname(buf_T *buf, char_u *fname) |
7 | 5189 { |
5190 if (fname == NULL) | |
5191 fname = (char_u *)"-stdin-"; | |
5192 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE); | |
5193 IObuff[0] = '"'; | |
5194 STRCAT(IObuff, "\" "); | |
5195 } | |
5196 | |
5197 /* | |
5198 * Append message for text mode to IObuff. | |
5199 * Return TRUE if something appended. | |
5200 */ | |
5201 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5202 msg_add_fileformat(int eol_type) |
7 | 5203 { |
5204 #ifndef USE_CRNL | |
5205 if (eol_type == EOL_DOS) | |
5206 { | |
5207 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]")); | |
5208 return TRUE; | |
5209 } | |
5210 #endif | |
5211 #ifndef USE_CR | |
5212 if (eol_type == EOL_MAC) | |
5213 { | |
5214 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]")); | |
5215 return TRUE; | |
5216 } | |
5217 #endif | |
5218 #if defined(USE_CRNL) || defined(USE_CR) | |
5219 if (eol_type == EOL_UNIX) | |
5220 { | |
5221 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]")); | |
5222 return TRUE; | |
5223 } | |
5224 #endif | |
5225 return FALSE; | |
5226 } | |
5227 | |
5228 /* | |
5229 * Append line and character count to IObuff. | |
5230 */ | |
33 | 5231 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5232 msg_add_lines( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5233 int insert_space, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5234 long lnum, |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
5235 off_T nchars) |
7 | 5236 { |
5237 char_u *p; | |
5238 | |
5239 p = IObuff + STRLEN(IObuff); | |
5240 | |
5241 if (insert_space) | |
5242 *p++ = ' '; | |
5243 if (shortmess(SHM_LINES)) | |
9391
4c40631238e4
commit https://github.com/vim/vim/commit/bde9810d6103ffe3a22a9330021cb21db1ed1792
Christian Brabandt <cb@256bit.org>
parents:
9389
diff
changeset
|
5244 vim_snprintf((char *)p, IOSIZE - (p - IObuff), |
4c40631238e4
commit https://github.com/vim/vim/commit/bde9810d6103ffe3a22a9330021cb21db1ed1792
Christian Brabandt <cb@256bit.org>
parents:
9389
diff
changeset
|
5245 "%ldL, %lldC", lnum, (varnumber_T)nchars); |
7 | 5246 else |
5247 { | |
5248 if (lnum == 1) | |
5249 STRCPY(p, _("1 line, ")); | |
5250 else | |
5251 sprintf((char *)p, _("%ld lines, "), lnum); | |
5252 p += STRLEN(p); | |
5253 if (nchars == 1) | |
5254 STRCPY(p, _("1 character")); | |
5255 else | |
9391
4c40631238e4
commit https://github.com/vim/vim/commit/bde9810d6103ffe3a22a9330021cb21db1ed1792
Christian Brabandt <cb@256bit.org>
parents:
9389
diff
changeset
|
5256 vim_snprintf((char *)p, IOSIZE - (p - IObuff), |
4c40631238e4
commit https://github.com/vim/vim/commit/bde9810d6103ffe3a22a9330021cb21db1ed1792
Christian Brabandt <cb@256bit.org>
parents:
9389
diff
changeset
|
5257 _("%lld characters"), (varnumber_T)nchars); |
7 | 5258 } |
5259 } | |
5260 | |
5261 /* | |
5262 * Append message for missing line separator to IObuff. | |
5263 */ | |
5264 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5265 msg_add_eol(void) |
7 | 5266 { |
5267 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]")); | |
5268 } | |
5269 | |
5270 /* | |
5271 * Check modification time of file, before writing to it. | |
5272 * The size isn't checked, because using a tool like "gzip" takes care of | |
5273 * using the same timestamp but can't set the size. | |
5274 */ | |
5275 static int | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
5276 check_mtime(buf_T *buf, stat_T *st) |
7 | 5277 { |
5278 if (buf->b_mtime_read != 0 | |
5279 && time_differs((long)st->st_mtime, buf->b_mtime_read)) | |
5280 { | |
5281 msg_scroll = TRUE; /* don't overwrite messages here */ | |
5282 msg_silent = 0; /* must give this prompt */ | |
5283 /* don't use emsg() here, don't want to flush the buffers */ | |
5284 MSG_ATTR(_("WARNING: The file has been changed since reading it!!!"), | |
5285 hl_attr(HLF_E)); | |
5286 if (ask_yesno((char_u *)_("Do you really want to write to it"), | |
5287 TRUE) == 'n') | |
5288 return FAIL; | |
5289 msg_scroll = FALSE; /* always overwrite the file message now */ | |
5290 } | |
5291 return OK; | |
5292 } | |
5293 | |
5294 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5295 time_differs(long t1, long t2) |
7 | 5296 { |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
5297 #if defined(__linux__) || defined(MSWIN) |
7 | 5298 /* On a FAT filesystem, esp. under Linux, there are only 5 bits to store |
5299 * the seconds. Since the roundoff is done when flushing the inode, the | |
5300 * time may change unexpectedly by one second!!! */ | |
5301 return (t1 - t2 > 1 || t2 - t1 > 1); | |
5302 #else | |
5303 return (t1 != t2); | |
5304 #endif | |
5305 } | |
5306 | |
5307 /* | |
5308 * Call write() to write a number of bytes to the file. | |
2664 | 5309 * Handles encryption and 'encoding' conversion. |
7 | 5310 * |
5311 * Return FAIL for failure, OK otherwise. | |
5312 */ | |
5313 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5314 buf_write_bytes(struct bw_info *ip) |
7 | 5315 { |
5316 int wlen; | |
5317 char_u *buf = ip->bw_buf; /* data to write */ | |
5318 int len = ip->bw_len; /* length of data */ | |
5319 #ifdef HAS_BW_FLAGS | |
5320 int flags = ip->bw_flags; /* extra flags */ | |
5321 #endif | |
5322 | |
5323 #ifdef FEAT_MBYTE | |
5324 /* | |
5325 * Skip conversion when writing the crypt magic number or the BOM. | |
5326 */ | |
5327 if (!(flags & FIO_NOCONVERT)) | |
5328 { | |
5329 char_u *p; | |
5330 unsigned c; | |
5331 int n; | |
5332 | |
5333 if (flags & FIO_UTF8) | |
5334 { | |
5335 /* | |
5336 * Convert latin1 in the buffer to UTF-8 in the file. | |
5337 */ | |
5338 p = ip->bw_conv_buf; /* translate to buffer */ | |
5339 for (wlen = 0; wlen < len; ++wlen) | |
5340 p += utf_char2bytes(buf[wlen], p); | |
5341 buf = ip->bw_conv_buf; | |
5342 len = (int)(p - ip->bw_conv_buf); | |
5343 } | |
5344 else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1)) | |
5345 { | |
5346 /* | |
5347 * Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or | |
5348 * Latin1 chars in the file. | |
5349 */ | |
5350 if (flags & FIO_LATIN1) | |
5351 p = buf; /* translate in-place (can only get shorter) */ | |
5352 else | |
5353 p = ip->bw_conv_buf; /* translate to buffer */ | |
5354 for (wlen = 0; wlen < len; wlen += n) | |
5355 { | |
5356 if (wlen == 0 && ip->bw_restlen != 0) | |
5357 { | |
5358 int l; | |
5359 | |
5360 /* Use remainder of previous call. Append the start of | |
5361 * buf[] to get a full sequence. Might still be too | |
5362 * short! */ | |
5363 l = CONV_RESTLEN - ip->bw_restlen; | |
5364 if (l > len) | |
5365 l = len; | |
5366 mch_memmove(ip->bw_rest + ip->bw_restlen, buf, (size_t)l); | |
474 | 5367 n = utf_ptr2len_len(ip->bw_rest, ip->bw_restlen + l); |
7 | 5368 if (n > ip->bw_restlen + len) |
5369 { | |
5370 /* We have an incomplete byte sequence at the end to | |
5371 * be written. We can't convert it without the | |
5372 * remaining bytes. Keep them for the next call. */ | |
5373 if (ip->bw_restlen + len > CONV_RESTLEN) | |
5374 return FAIL; | |
5375 ip->bw_restlen += len; | |
5376 break; | |
5377 } | |
5378 if (n > 1) | |
5379 c = utf_ptr2char(ip->bw_rest); | |
5380 else | |
5381 c = ip->bw_rest[0]; | |
5382 if (n >= ip->bw_restlen) | |
5383 { | |
5384 n -= ip->bw_restlen; | |
5385 ip->bw_restlen = 0; | |
5386 } | |
5387 else | |
5388 { | |
5389 ip->bw_restlen -= n; | |
5390 mch_memmove(ip->bw_rest, ip->bw_rest + n, | |
5391 (size_t)ip->bw_restlen); | |
5392 n = 0; | |
5393 } | |
5394 } | |
5395 else | |
5396 { | |
474 | 5397 n = utf_ptr2len_len(buf + wlen, len - wlen); |
7 | 5398 if (n > len - wlen) |
5399 { | |
5400 /* We have an incomplete byte sequence at the end to | |
5401 * be written. We can't convert it without the | |
5402 * remaining bytes. Keep them for the next call. */ | |
5403 if (len - wlen > CONV_RESTLEN) | |
5404 return FAIL; | |
5405 ip->bw_restlen = len - wlen; | |
5406 mch_memmove(ip->bw_rest, buf + wlen, | |
5407 (size_t)ip->bw_restlen); | |
5408 break; | |
5409 } | |
5410 if (n > 1) | |
5411 c = utf_ptr2char(buf + wlen); | |
5412 else | |
5413 c = buf[wlen]; | |
5414 } | |
5415 | |
1947 | 5416 if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error) |
5417 { | |
5418 ip->bw_conv_error = TRUE; | |
5419 ip->bw_conv_error_lnum = ip->bw_start_lnum; | |
5420 } | |
5421 if (c == NL) | |
5422 ++ip->bw_start_lnum; | |
7 | 5423 } |
5424 if (flags & FIO_LATIN1) | |
5425 len = (int)(p - buf); | |
5426 else | |
5427 { | |
5428 buf = ip->bw_conv_buf; | |
5429 len = (int)(p - ip->bw_conv_buf); | |
5430 } | |
5431 } | |
5432 | |
5433 # ifdef WIN3264 | |
5434 else if (flags & FIO_CODEPAGE) | |
5435 { | |
5436 /* | |
5437 * Convert UTF-8 or codepage to UCS-2 and then to MS-Windows | |
5438 * codepage. | |
5439 */ | |
5440 char_u *from; | |
5441 size_t fromlen; | |
5442 char_u *to; | |
5443 int u8c; | |
5444 BOOL bad = FALSE; | |
5445 int needed; | |
5446 | |
5447 if (ip->bw_restlen > 0) | |
5448 { | |
5449 /* Need to concatenate the remainder of the previous call and | |
5450 * the bytes of the current call. Use the end of the | |
5451 * conversion buffer for this. */ | |
5452 fromlen = len + ip->bw_restlen; | |
5453 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; | |
5454 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); | |
5455 mch_memmove(from + ip->bw_restlen, buf, (size_t)len); | |
5456 } | |
5457 else | |
5458 { | |
5459 from = buf; | |
5460 fromlen = len; | |
5461 } | |
5462 | |
5463 to = ip->bw_conv_buf; | |
5464 if (enc_utf8) | |
5465 { | |
5466 /* Convert from UTF-8 to UCS-2, to the start of the buffer. | |
5467 * The buffer has been allocated to be big enough. */ | |
5468 while (fromlen > 0) | |
5469 { | |
835 | 5470 n = (int)utf_ptr2len_len(from, (int)fromlen); |
7 | 5471 if (n > (int)fromlen) /* incomplete byte sequence */ |
5472 break; | |
5473 u8c = utf_ptr2char(from); | |
5474 *to++ = (u8c & 0xff); | |
5475 *to++ = (u8c >> 8); | |
5476 fromlen -= n; | |
5477 from += n; | |
5478 } | |
5479 | |
5480 /* Copy remainder to ip->bw_rest[] to be used for the next | |
5481 * call. */ | |
5482 if (fromlen > CONV_RESTLEN) | |
5483 { | |
5484 /* weird overlong sequence */ | |
5485 ip->bw_conv_error = TRUE; | |
5486 return FAIL; | |
5487 } | |
5488 mch_memmove(ip->bw_rest, from, fromlen); | |
835 | 5489 ip->bw_restlen = (int)fromlen; |
7 | 5490 } |
5491 else | |
5492 { | |
5493 /* Convert from enc_codepage to UCS-2, to the start of the | |
5494 * buffer. The buffer has been allocated to be big enough. */ | |
5495 ip->bw_restlen = 0; | |
5496 needed = MultiByteToWideChar(enc_codepage, | |
1142 | 5497 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen, |
7 | 5498 NULL, 0); |
5499 if (needed == 0) | |
5500 { | |
5501 /* When conversion fails there may be a trailing byte. */ | |
5502 needed = MultiByteToWideChar(enc_codepage, | |
1142 | 5503 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen - 1, |
7 | 5504 NULL, 0); |
5505 if (needed == 0) | |
5506 { | |
5507 /* Conversion doesn't work. */ | |
5508 ip->bw_conv_error = TRUE; | |
5509 return FAIL; | |
5510 } | |
5511 /* Save the trailing byte for the next call. */ | |
5512 ip->bw_rest[0] = from[fromlen - 1]; | |
5513 ip->bw_restlen = 1; | |
5514 } | |
5515 needed = MultiByteToWideChar(enc_codepage, MB_ERR_INVALID_CHARS, | |
1142 | 5516 (LPCSTR)from, (int)(fromlen - ip->bw_restlen), |
7 | 5517 (LPWSTR)to, needed); |
5518 if (needed == 0) | |
5519 { | |
5520 /* Safety check: Conversion doesn't work. */ | |
5521 ip->bw_conv_error = TRUE; | |
5522 return FAIL; | |
5523 } | |
5524 to += needed * 2; | |
5525 } | |
5526 | |
5527 fromlen = to - ip->bw_conv_buf; | |
5528 buf = to; | |
5529 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
5530 if (FIO_GET_CP(flags) == CP_UTF8) | |
5531 { | |
5532 /* Convert from UCS-2 to UTF-8, using the remainder of the | |
5533 * conversion buffer. Fails when out of space. */ | |
5534 for (from = ip->bw_conv_buf; fromlen > 1; fromlen -= 2) | |
5535 { | |
5536 u8c = *from++; | |
5537 u8c += (*from++ << 8); | |
5538 to += utf_char2bytes(u8c, to); | |
5539 if (to + 6 >= ip->bw_conv_buf + ip->bw_conv_buflen) | |
5540 { | |
5541 ip->bw_conv_error = TRUE; | |
5542 return FAIL; | |
5543 } | |
5544 } | |
835 | 5545 len = (int)(to - buf); |
7 | 5546 } |
5547 else | |
5548 #endif | |
5549 { | |
5550 /* Convert from UCS-2 to the codepage, using the remainder of | |
5551 * the conversion buffer. If the conversion uses the default | |
5552 * character "0", the data doesn't fit in this encoding, so | |
5553 * fail. */ | |
5554 len = WideCharToMultiByte(FIO_GET_CP(flags), 0, | |
5555 (LPCWSTR)ip->bw_conv_buf, (int)fromlen / sizeof(WCHAR), | |
1142 | 5556 (LPSTR)to, (int)(ip->bw_conv_buflen - fromlen), 0, |
5557 &bad); | |
7 | 5558 if (bad) |
5559 { | |
5560 ip->bw_conv_error = TRUE; | |
5561 return FAIL; | |
5562 } | |
5563 } | |
5564 } | |
5565 # endif | |
5566 | |
765 | 5567 # ifdef MACOS_CONVERT |
7 | 5568 else if (flags & FIO_MACROMAN) |
5569 { | |
5570 /* | |
5571 * Convert UTF-8 or latin1 to Apple MacRoman. | |
5572 */ | |
5573 char_u *from; | |
5574 size_t fromlen; | |
5575 | |
5576 if (ip->bw_restlen > 0) | |
5577 { | |
5578 /* Need to concatenate the remainder of the previous call and | |
5579 * the bytes of the current call. Use the end of the | |
5580 * conversion buffer for this. */ | |
5581 fromlen = len + ip->bw_restlen; | |
5582 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; | |
5583 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); | |
5584 mch_memmove(from + ip->bw_restlen, buf, (size_t)len); | |
5585 } | |
5586 else | |
5587 { | |
5588 from = buf; | |
5589 fromlen = len; | |
5590 } | |
5591 | |
18 | 5592 if (enc2macroman(from, fromlen, |
5593 ip->bw_conv_buf, &len, ip->bw_conv_buflen, | |
5594 ip->bw_rest, &ip->bw_restlen) == FAIL) | |
7 | 5595 { |
5596 ip->bw_conv_error = TRUE; | |
5597 return FAIL; | |
5598 } | |
5599 buf = ip->bw_conv_buf; | |
5600 } | |
5601 # endif | |
5602 | |
5603 # ifdef USE_ICONV | |
5604 if (ip->bw_iconv_fd != (iconv_t)-1) | |
5605 { | |
5606 const char *from; | |
5607 size_t fromlen; | |
5608 char *to; | |
5609 size_t tolen; | |
5610 | |
5611 /* Convert with iconv(). */ | |
5612 if (ip->bw_restlen > 0) | |
5613 { | |
1836 | 5614 char *fp; |
5615 | |
7 | 5616 /* Need to concatenate the remainder of the previous call and |
5617 * the bytes of the current call. Use the end of the | |
5618 * conversion buffer for this. */ | |
5619 fromlen = len + ip->bw_restlen; | |
1836 | 5620 fp = (char *)ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; |
5621 mch_memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen); | |
5622 mch_memmove(fp + ip->bw_restlen, buf, (size_t)len); | |
5623 from = fp; | |
7 | 5624 tolen = ip->bw_conv_buflen - fromlen; |
5625 } | |
5626 else | |
5627 { | |
5628 from = (const char *)buf; | |
5629 fromlen = len; | |
5630 tolen = ip->bw_conv_buflen; | |
5631 } | |
5632 to = (char *)ip->bw_conv_buf; | |
5633 | |
5634 if (ip->bw_first) | |
5635 { | |
5636 size_t save_len = tolen; | |
5637 | |
5638 /* output the initial shift state sequence */ | |
5639 (void)iconv(ip->bw_iconv_fd, NULL, NULL, &to, &tolen); | |
5640 | |
5641 /* There is a bug in iconv() on Linux (which appears to be | |
5642 * wide-spread) which sets "to" to NULL and messes up "tolen". | |
5643 */ | |
5644 if (to == NULL) | |
5645 { | |
5646 to = (char *)ip->bw_conv_buf; | |
5647 tolen = save_len; | |
5648 } | |
5649 ip->bw_first = FALSE; | |
5650 } | |
5651 | |
5652 /* | |
5653 * If iconv() has an error or there is not enough room, fail. | |
5654 */ | |
5655 if ((iconv(ip->bw_iconv_fd, (void *)&from, &fromlen, &to, &tolen) | |
5656 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) | |
5657 || fromlen > CONV_RESTLEN) | |
5658 { | |
5659 ip->bw_conv_error = TRUE; | |
5660 return FAIL; | |
5661 } | |
5662 | |
5663 /* copy remainder to ip->bw_rest[] to be used for the next call. */ | |
5664 if (fromlen > 0) | |
5665 mch_memmove(ip->bw_rest, (void *)from, fromlen); | |
5666 ip->bw_restlen = (int)fromlen; | |
5667 | |
5668 buf = ip->bw_conv_buf; | |
5669 len = (int)((char_u *)to - ip->bw_conv_buf); | |
5670 } | |
5671 # endif | |
5672 } | |
5673 #endif /* FEAT_MBYTE */ | |
5674 | |
5675 #ifdef FEAT_CRYPT | |
6122 | 5676 if (flags & FIO_ENCRYPTED) |
5677 { | |
5678 /* Encrypt the data. Do it in-place if possible, otherwise use an | |
5679 * allocated buffer. */ | |
5680 if (crypt_works_inplace(ip->bw_buffer->b_cryptstate)) | |
5681 { | |
5682 crypt_encode_inplace(ip->bw_buffer->b_cryptstate, buf, len); | |
5683 } | |
5684 else | |
5685 { | |
5686 char_u *outbuf; | |
5687 | |
5688 len = crypt_encode_alloc(curbuf->b_cryptstate, buf, len, &outbuf); | |
5689 if (len == 0) | |
5690 return OK; /* Crypt layer is buffering, will flush later. */ | |
5691 wlen = write_eintr(ip->bw_fd, outbuf, len); | |
5692 vim_free(outbuf); | |
5693 return (wlen < len) ? FAIL : OK; | |
5694 } | |
5695 } | |
7 | 5696 #endif |
5697 | |
2664 | 5698 wlen = write_eintr(ip->bw_fd, buf, len); |
5699 return (wlen < len) ? FAIL : OK; | |
7 | 5700 } |
5701 | |
5702 #ifdef FEAT_MBYTE | |
5703 /* | |
5704 * Convert a Unicode character to bytes. | |
1947 | 5705 * Return TRUE for an error, FALSE when it's OK. |
7 | 5706 */ |
5707 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5708 ucs2bytes( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5709 unsigned c, /* in: character */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5710 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
|
5711 int flags) /* FIO_ flags */ |
7 | 5712 { |
5713 char_u *p = *pp; | |
5714 int error = FALSE; | |
5715 int cc; | |
5716 | |
5717 | |
5718 if (flags & FIO_UCS4) | |
5719 { | |
5720 if (flags & FIO_ENDIAN_L) | |
5721 { | |
5722 *p++ = c; | |
5723 *p++ = (c >> 8); | |
5724 *p++ = (c >> 16); | |
5725 *p++ = (c >> 24); | |
5726 } | |
5727 else | |
5728 { | |
5729 *p++ = (c >> 24); | |
5730 *p++ = (c >> 16); | |
5731 *p++ = (c >> 8); | |
5732 *p++ = c; | |
5733 } | |
5734 } | |
5735 else if (flags & (FIO_UCS2 | FIO_UTF16)) | |
5736 { | |
5737 if (c >= 0x10000) | |
5738 { | |
5739 if (flags & FIO_UTF16) | |
5740 { | |
5741 /* Make two words, ten bits of the character in each. First | |
5742 * word is 0xd800 - 0xdbff, second one 0xdc00 - 0xdfff */ | |
5743 c -= 0x10000; | |
5744 if (c >= 0x100000) | |
5745 error = TRUE; | |
5746 cc = ((c >> 10) & 0x3ff) + 0xd800; | |
5747 if (flags & FIO_ENDIAN_L) | |
5748 { | |
5749 *p++ = cc; | |
5750 *p++ = ((unsigned)cc >> 8); | |
5751 } | |
5752 else | |
5753 { | |
5754 *p++ = ((unsigned)cc >> 8); | |
5755 *p++ = cc; | |
5756 } | |
5757 c = (c & 0x3ff) + 0xdc00; | |
5758 } | |
5759 else | |
5760 error = TRUE; | |
5761 } | |
5762 if (flags & FIO_ENDIAN_L) | |
5763 { | |
5764 *p++ = c; | |
5765 *p++ = (c >> 8); | |
5766 } | |
5767 else | |
5768 { | |
5769 *p++ = (c >> 8); | |
5770 *p++ = c; | |
5771 } | |
5772 } | |
5773 else /* Latin1 */ | |
5774 { | |
5775 if (c >= 0x100) | |
5776 { | |
5777 error = TRUE; | |
5778 *p++ = 0xBF; | |
5779 } | |
5780 else | |
5781 *p++ = c; | |
5782 } | |
5783 | |
5784 *pp = p; | |
5785 return error; | |
5786 } | |
5787 | |
5788 /* | |
1948 | 5789 * Return TRUE if file encoding "fenc" requires conversion from or to |
5790 * 'encoding'. | |
7 | 5791 */ |
5792 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5793 need_conversion(char_u *fenc) |
7 | 5794 { |
1948 | 5795 int same_encoding; |
5796 int enc_flags; | |
5797 int fenc_flags; | |
5798 | |
5799 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0) | |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5800 { |
1948 | 5801 same_encoding = TRUE; |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5802 fenc_flags = 0; |
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5803 } |
1948 | 5804 else |
5805 { | |
5806 /* Ignore difference between "ansi" and "latin1", "ucs-4" and | |
5807 * "ucs-4be", etc. */ | |
5808 enc_flags = get_fio_flags(p_enc); | |
5809 fenc_flags = get_fio_flags(fenc); | |
5810 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags); | |
5811 } | |
5812 if (same_encoding) | |
5813 { | |
5814 /* Specified encoding matches with 'encoding'. This requires | |
5815 * conversion when 'encoding' is Unicode but not UTF-8. */ | |
5816 return enc_unicode != 0; | |
5817 } | |
5818 | |
5819 /* Encodings differ. However, conversion is not needed when 'enc' is any | |
5820 * Unicode encoding and the file is UTF-8. */ | |
5821 return !(enc_utf8 && fenc_flags == FIO_UTF8); | |
7 | 5822 } |
5823 | |
5824 /* | |
5825 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the | |
5826 * internal conversion. | |
5827 * if "ptr" is an empty string, use 'encoding'. | |
5828 */ | |
5829 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5830 get_fio_flags(char_u *ptr) |
7 | 5831 { |
5832 int prop; | |
5833 | |
5834 if (*ptr == NUL) | |
5835 ptr = p_enc; | |
5836 | |
5837 prop = enc_canon_props(ptr); | |
5838 if (prop & ENC_UNICODE) | |
5839 { | |
5840 if (prop & ENC_2BYTE) | |
5841 { | |
5842 if (prop & ENC_ENDIAN_L) | |
5843 return FIO_UCS2 | FIO_ENDIAN_L; | |
5844 return FIO_UCS2; | |
5845 } | |
5846 if (prop & ENC_4BYTE) | |
5847 { | |
5848 if (prop & ENC_ENDIAN_L) | |
5849 return FIO_UCS4 | FIO_ENDIAN_L; | |
5850 return FIO_UCS4; | |
5851 } | |
5852 if (prop & ENC_2WORD) | |
5853 { | |
5854 if (prop & ENC_ENDIAN_L) | |
5855 return FIO_UTF16 | FIO_ENDIAN_L; | |
5856 return FIO_UTF16; | |
5857 } | |
5858 return FIO_UTF8; | |
5859 } | |
5860 if (prop & ENC_LATIN1) | |
5861 return FIO_LATIN1; | |
5862 /* must be ENC_DBCS, requires iconv() */ | |
5863 return 0; | |
5864 } | |
5865 | |
5866 #ifdef WIN3264 | |
5867 /* | |
5868 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed | |
5869 * for the conversion MS-Windows can do for us. Also accept "utf-8". | |
5870 * Used for conversion between 'encoding' and 'fileencoding'. | |
5871 */ | |
5872 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5873 get_win_fio_flags(char_u *ptr) |
7 | 5874 { |
5875 int cp; | |
5876 | |
5877 /* Cannot do this when 'encoding' is not utf-8 and not a codepage. */ | |
5878 if (!enc_utf8 && enc_codepage <= 0) | |
5879 return 0; | |
5880 | |
5881 cp = encname2codepage(ptr); | |
5882 if (cp == 0) | |
5883 { | |
5884 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
5885 if (STRCMP(ptr, "utf-8") == 0) | |
5886 cp = CP_UTF8; | |
5887 else | |
5888 # endif | |
5889 return 0; | |
5890 } | |
5891 return FIO_PUT_CP(cp) | FIO_CODEPAGE; | |
5892 } | |
5893 #endif | |
5894 | |
5895 #ifdef MACOS_X | |
5896 /* | |
5897 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags | |
5898 * needed for the internal conversion to/from utf-8 or latin1. | |
5899 */ | |
5900 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5901 get_mac_fio_flags(char_u *ptr) |
7 | 5902 { |
5903 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
5904 && (enc_canon_props(ptr) & ENC_MACROMAN)) | |
5905 return FIO_MACROMAN; | |
5906 return 0; | |
5907 } | |
5908 #endif | |
5909 | |
5910 /* | |
5911 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size]. | |
5912 * "size" must be at least 2. | |
5913 * Return the name of the encoding and set "*lenp" to the length. | |
5914 * Returns NULL when no BOM found. | |
5915 */ | |
5916 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5917 check_for_bom( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5918 char_u *p, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5919 long size, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5920 int *lenp, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5921 int flags) |
7 | 5922 { |
5923 char *name = NULL; | |
5924 int len = 2; | |
5925 | |
5926 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf | |
1688 | 5927 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0)) |
7 | 5928 { |
5929 name = "utf-8"; /* EF BB BF */ | |
5930 len = 3; | |
5931 } | |
5932 else if (p[0] == 0xff && p[1] == 0xfe) | |
5933 { | |
5934 if (size >= 4 && p[2] == 0 && p[3] == 0 | |
5935 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L))) | |
5936 { | |
5937 name = "ucs-4le"; /* FF FE 00 00 */ | |
5938 len = 4; | |
5939 } | |
1735 | 5940 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L)) |
7 | 5941 name = "ucs-2le"; /* FF FE */ |
1735 | 5942 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L)) |
5943 /* utf-16le is preferred, it also works for ucs-2le text */ | |
7 | 5944 name = "utf-16le"; /* FF FE */ |
5945 } | |
5946 else if (p[0] == 0xfe && p[1] == 0xff | |
5947 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16)) | |
5948 { | |
1547 | 5949 /* Default to utf-16, it works also for ucs-2 text. */ |
5950 if (flags == FIO_UCS2) | |
5951 name = "ucs-2"; /* FE FF */ | |
5952 else | |
7 | 5953 name = "utf-16"; /* FE FF */ |
5954 } | |
5955 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe | |
5956 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4)) | |
5957 { | |
5958 name = "ucs-4"; /* 00 00 FE FF */ | |
5959 len = 4; | |
5960 } | |
5961 | |
5962 *lenp = len; | |
5963 return (char_u *)name; | |
5964 } | |
5965 | |
5966 /* | |
5967 * Generate a BOM in "buf[4]" for encoding "name". | |
5968 * Return the length of the BOM (zero when no BOM). | |
5969 */ | |
5970 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5971 make_bom(char_u *buf, char_u *name) |
7 | 5972 { |
5973 int flags; | |
5974 char_u *p; | |
5975 | |
5976 flags = get_fio_flags(name); | |
5977 | |
5978 /* Can't put a BOM in a non-Unicode file. */ | |
5979 if (flags == FIO_LATIN1 || flags == 0) | |
5980 return 0; | |
5981 | |
5982 if (flags == FIO_UTF8) /* UTF-8 */ | |
5983 { | |
5984 buf[0] = 0xef; | |
5985 buf[1] = 0xbb; | |
5986 buf[2] = 0xbf; | |
5987 return 3; | |
5988 } | |
5989 p = buf; | |
5990 (void)ucs2bytes(0xfeff, &p, flags); | |
5991 return (int)(p - buf); | |
5992 } | |
5993 #endif | |
5994 | |
1418 | 5995 #if defined(FEAT_VIMINFO) || defined(FEAT_BROWSE) || \ |
1471 | 5996 defined(FEAT_QUICKFIX) || defined(FEAT_AUTOCMD) || defined(PROTO) |
7 | 5997 /* |
5998 * Try to find a shortname by comparing the fullname with the current | |
5999 * directory. | |
1411 | 6000 * Returns "full_path" or pointer into "full_path" if shortened. |
6001 */ | |
6002 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6003 shorten_fname1(char_u *full_path) |
1411 | 6004 { |
2770 | 6005 char_u *dirname; |
1411 | 6006 char_u *p = full_path; |
6007 | |
2770 | 6008 dirname = alloc(MAXPATHL); |
6009 if (dirname == NULL) | |
6010 return full_path; | |
1411 | 6011 if (mch_dirname(dirname, MAXPATHL) == OK) |
6012 { | |
6013 p = shorten_fname(full_path, dirname); | |
6014 if (p == NULL || *p == NUL) | |
6015 p = full_path; | |
6016 } | |
2770 | 6017 vim_free(dirname); |
1411 | 6018 return p; |
6019 } | |
1418 | 6020 #endif |
1411 | 6021 |
6022 /* | |
6023 * Try to find a shortname by comparing the fullname with the current | |
6024 * directory. | |
7 | 6025 * Returns NULL if not shorter name possible, pointer into "full_path" |
6026 * otherwise. | |
6027 */ | |
6028 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6029 shorten_fname(char_u *full_path, char_u *dir_name) |
7 | 6030 { |
6031 int len; | |
6032 char_u *p; | |
6033 | |
6034 if (full_path == NULL) | |
6035 return NULL; | |
6036 len = (int)STRLEN(dir_name); | |
6037 if (fnamencmp(dir_name, full_path, len) == 0) | |
6038 { | |
6039 p = full_path + len; | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6040 #if defined(MSWIN) |
7 | 6041 /* |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6042 * MSWIN: when a file is in the root directory, dir_name will end in a |
7 | 6043 * slash, since C: by itself does not define a specific dir. In this |
6044 * case p may already be correct. <negri> | |
6045 */ | |
6046 if (!((len > 2) && (*(p - 2) == ':'))) | |
6047 #endif | |
6048 { | |
6049 if (vim_ispathsep(*p)) | |
6050 ++p; | |
6051 #ifndef VMS /* the path separator is always part of the path */ | |
6052 else | |
6053 p = NULL; | |
6054 #endif | |
6055 } | |
6056 } | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6057 #if defined(MSWIN) |
7 | 6058 /* |
6059 * When using a file in the current drive, remove the drive name: | |
6060 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on | |
6061 * a floppy from "A:\dir" to "B:\dir". | |
6062 */ | |
6063 else if (len > 3 | |
6064 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0]) | |
6065 && full_path[1] == ':' | |
6066 && vim_ispathsep(full_path[2])) | |
6067 p = full_path + 2; | |
6068 #endif | |
6069 else | |
6070 p = NULL; | |
6071 return p; | |
6072 } | |
6073 | |
6074 /* | |
6075 * Shorten filenames for all buffers. | |
6076 * When "force" is TRUE: Use full path from now on for files currently being | |
6077 * edited, both for file name and swap file name. Try to shorten the file | |
6078 * names a bit, if safe to do so. | |
6079 * When "force" is FALSE: Only try to shorten absolute file names. | |
6080 * For buffers that have buftype "nofile" or "scratch": never change the file | |
6081 * name. | |
6082 */ | |
6083 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6084 shorten_fnames(int force) |
7 | 6085 { |
6086 char_u dirname[MAXPATHL]; | |
6087 buf_T *buf; | |
6088 char_u *p; | |
6089 | |
6090 mch_dirname(dirname, MAXPATHL); | |
6091 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
6092 { | |
6093 if (buf->b_fname != NULL | |
6094 #ifdef FEAT_QUICKFIX | |
6095 && !bt_nofile(buf) | |
6096 #endif | |
6097 && !path_with_url(buf->b_fname) | |
6098 && (force | |
6099 || buf->b_sfname == NULL | |
6100 || mch_isFullName(buf->b_sfname))) | |
6101 { | |
6102 vim_free(buf->b_sfname); | |
6103 buf->b_sfname = NULL; | |
6104 p = shorten_fname(buf->b_ffname, dirname); | |
6105 if (p != NULL) | |
6106 { | |
6107 buf->b_sfname = vim_strsave(p); | |
6108 buf->b_fname = buf->b_sfname; | |
6109 } | |
6110 if (p == NULL || buf->b_fname == NULL) | |
6111 buf->b_fname = buf->b_ffname; | |
9 | 6112 } |
6113 | |
6114 /* Always make the swap file name a full path, a "nofile" buffer may | |
6115 * also have a swap file. */ | |
6116 mf_fullname(buf->b_ml.ml_mfp); | |
7 | 6117 } |
6118 #ifdef FEAT_WINDOWS | |
6119 status_redraw_all(); | |
672 | 6120 redraw_tabline = TRUE; |
7 | 6121 #endif |
6122 } | |
6123 | |
6124 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \ | |
6125 || defined(FEAT_GUI_MSWIN) \ | |
6126 || defined(FEAT_GUI_MAC) \ | |
6127 || defined(PROTO) | |
6128 /* | |
6129 * Shorten all filenames in "fnames[count]" by current directory. | |
6130 */ | |
6131 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6132 shorten_filenames(char_u **fnames, int count) |
7 | 6133 { |
6134 int i; | |
6135 char_u dirname[MAXPATHL]; | |
6136 char_u *p; | |
6137 | |
6138 if (fnames == NULL || count < 1) | |
6139 return; | |
6140 mch_dirname(dirname, sizeof(dirname)); | |
6141 for (i = 0; i < count; ++i) | |
6142 { | |
6143 if ((p = shorten_fname(fnames[i], dirname)) != NULL) | |
6144 { | |
6145 /* shorten_fname() returns pointer in given "fnames[i]". If free | |
6146 * "fnames[i]" first, "p" becomes invalid. So we need to copy | |
6147 * "p" first then free fnames[i]. */ | |
6148 p = vim_strsave(p); | |
6149 vim_free(fnames[i]); | |
6150 fnames[i] = p; | |
6151 } | |
6152 } | |
6153 } | |
6154 #endif | |
6155 | |
6156 /* | |
1651 | 6157 * add extension to file name - change path/fo.o.h to path/fo.o.h.ext or |
7 | 6158 * fo_o_h.ext for MSDOS or when shortname option set. |
6159 * | |
6160 * Assumed that fname is a valid name found in the filesystem we assure that | |
6161 * the return value is a different name and ends in 'ext'. | |
6162 * "ext" MUST be at most 4 characters long if it starts with a dot, 3 | |
6163 * characters otherwise. | |
6164 * Space for the returned name is allocated, must be freed later. | |
6165 * Returns NULL when out of memory. | |
6166 */ | |
6167 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6168 modname( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6169 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6170 char_u *ext, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6171 int prepend_dot) /* may prepend a '.' to file name */ |
7 | 6172 { |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6173 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname), |
7 | 6174 fname, ext, prepend_dot); |
6175 } | |
6176 | |
6177 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6178 buf_modname( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6179 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
|
6180 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6181 char_u *ext, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6182 int prepend_dot) /* may prepend a '.' to file name */ |
7 | 6183 { |
6184 char_u *retval; | |
6185 char_u *s; | |
6186 char_u *e; | |
6187 char_u *ptr; | |
6188 int fnamelen, extlen; | |
6189 | |
6190 extlen = (int)STRLEN(ext); | |
6191 | |
6192 /* | |
6193 * If there is no file name we must get the name of the current directory | |
6194 * (we need the full path in case :cd is used). | |
6195 */ | |
6196 if (fname == NULL || *fname == NUL) | |
6197 { | |
6198 retval = alloc((unsigned)(MAXPATHL + extlen + 3)); | |
6199 if (retval == NULL) | |
6200 return NULL; | |
6201 if (mch_dirname(retval, MAXPATHL) == FAIL || | |
6202 (fnamelen = (int)STRLEN(retval)) == 0) | |
6203 { | |
6204 vim_free(retval); | |
6205 return NULL; | |
6206 } | |
39 | 6207 if (!after_pathsep(retval, retval + fnamelen)) |
7 | 6208 { |
6209 retval[fnamelen++] = PATHSEP; | |
6210 retval[fnamelen] = NUL; | |
6211 } | |
6212 prepend_dot = FALSE; /* nothing to prepend a dot to */ | |
6213 } | |
6214 else | |
6215 { | |
6216 fnamelen = (int)STRLEN(fname); | |
6217 retval = alloc((unsigned)(fnamelen + extlen + 3)); | |
6218 if (retval == NULL) | |
6219 return NULL; | |
6220 STRCPY(retval, fname); | |
6221 #ifdef VMS | |
6222 vms_remove_version(retval); /* we do not need versions here */ | |
6223 #endif | |
6224 } | |
6225 | |
6226 /* | |
6227 * search backwards until we hit a '/', '\' or ':' replacing all '.' | |
6228 * by '_' for MSDOS or when shortname option set and ext starts with a dot. | |
6229 * Then truncate what is after the '/', '\' or ':' to 8 characters for | |
6230 * MSDOS and 26 characters for AMIGA, a lot more for UNIX. | |
6231 */ | |
391 | 6232 for (ptr = retval + fnamelen; ptr > retval; mb_ptr_back(retval, ptr)) |
7 | 6233 { |
6234 if (*ext == '.' | |
2823 | 6235 #ifdef USE_LONG_FNAME |
7 | 6236 && (!USE_LONG_FNAME || shortname) |
2823 | 6237 #else |
7 | 6238 && shortname |
2823 | 6239 #endif |
7 | 6240 ) |
6241 if (*ptr == '.') /* replace '.' by '_' */ | |
6242 *ptr = '_'; | |
6243 if (vim_ispathsep(*ptr)) | |
391 | 6244 { |
6245 ++ptr; | |
7 | 6246 break; |
391 | 6247 } |
6248 } | |
7 | 6249 |
6250 /* the file name has at most BASENAMELEN characters. */ | |
6251 if (STRLEN(ptr) > (unsigned)BASENAMELEN) | |
6252 ptr[BASENAMELEN] = '\0'; | |
6253 | |
6254 s = ptr + STRLEN(ptr); | |
6255 | |
6256 /* | |
6257 * For 8.3 file names we may have to reduce the length. | |
6258 */ | |
6259 #ifdef USE_LONG_FNAME | |
6260 if (!USE_LONG_FNAME || shortname) | |
6261 #else | |
6262 if (shortname) | |
6263 #endif | |
6264 { | |
6265 /* | |
6266 * If there is no file name, or the file name ends in '/', and the | |
6267 * extension starts with '.', put a '_' before the dot, because just | |
6268 * ".ext" is invalid. | |
6269 */ | |
6270 if (fname == NULL || *fname == NUL | |
6271 || vim_ispathsep(fname[STRLEN(fname) - 1])) | |
6272 { | |
6273 if (*ext == '.') | |
6274 *s++ = '_'; | |
6275 } | |
6276 /* | |
6277 * If the extension starts with '.', truncate the base name at 8 | |
6278 * characters | |
6279 */ | |
6280 else if (*ext == '.') | |
6281 { | |
1877 | 6282 if ((size_t)(s - ptr) > (size_t)8) |
7 | 6283 { |
6284 s = ptr + 8; | |
6285 *s = '\0'; | |
6286 } | |
6287 } | |
6288 /* | |
6289 * If the extension doesn't start with '.', and the file name | |
6290 * doesn't have an extension yet, append a '.' | |
6291 */ | |
6292 else if ((e = vim_strchr(ptr, '.')) == NULL) | |
6293 *s++ = '.'; | |
6294 /* | |
6295 * If the extension doesn't start with '.', and there already is an | |
1201 | 6296 * extension, it may need to be truncated |
7 | 6297 */ |
6298 else if ((int)STRLEN(e) + extlen > 4) | |
6299 s = e + 4 - extlen; | |
6300 } | |
7408
1886f2863437
commit https://github.com/vim/vim/commit/e7fedb6ebe72d9a475aa65109b77d5ed4667067a
Christian Brabandt <cb@256bit.org>
parents:
7320
diff
changeset
|
6301 #if defined(USE_LONG_FNAME) || defined(WIN3264) |
7 | 6302 /* |
6303 * If there is no file name, and the extension starts with '.', put a | |
6304 * '_' before the dot, because just ".ext" may be invalid if it's on a | |
6305 * FAT partition, and on HPFS it doesn't matter. | |
6306 */ | |
6307 else if ((fname == NULL || *fname == NUL) && *ext == '.') | |
6308 *s++ = '_'; | |
6309 #endif | |
6310 | |
6311 /* | |
1651 | 6312 * Append the extension. |
7 | 6313 * ext can start with '.' and cannot exceed 3 more characters. |
6314 */ | |
6315 STRCPY(s, ext); | |
6316 | |
6317 /* | |
6318 * Prepend the dot. | |
6319 */ | |
2823 | 6320 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.' |
7 | 6321 #ifdef USE_LONG_FNAME |
6322 && USE_LONG_FNAME | |
6323 #endif | |
6324 ) | |
6325 { | |
1620 | 6326 STRMOVE(e + 1, e); |
7 | 6327 *e = '.'; |
6328 } | |
6329 | |
6330 /* | |
6331 * Check that, after appending the extension, the file name is really | |
6332 * different. | |
6333 */ | |
6334 if (fname != NULL && STRCMP(fname, retval) == 0) | |
6335 { | |
6336 /* we search for a character that can be replaced by '_' */ | |
6337 while (--s >= ptr) | |
6338 { | |
6339 if (*s != '_') | |
6340 { | |
6341 *s = '_'; | |
6342 break; | |
6343 } | |
6344 } | |
6345 if (s < ptr) /* fname was "________.<ext>", how tricky! */ | |
6346 *ptr = 'v'; | |
6347 } | |
6348 return retval; | |
6349 } | |
6350 | |
6351 /* | |
6352 * Like fgets(), but if the file line is too long, it is truncated and the | |
6353 * rest of the line is thrown away. Returns TRUE for end-of-file. | |
6354 */ | |
6355 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6356 vim_fgets(char_u *buf, int size, FILE *fp) |
7 | 6357 { |
6358 char *eof; | |
6359 #define FGETS_SIZE 200 | |
6360 char tbuf[FGETS_SIZE]; | |
6361 | |
6362 buf[size - 2] = NUL; | |
6363 #ifdef USE_CR | |
6364 eof = fgets_cr((char *)buf, size, fp); | |
6365 #else | |
6366 eof = fgets((char *)buf, size, fp); | |
6367 #endif | |
6368 if (buf[size - 2] != NUL && buf[size - 2] != '\n') | |
6369 { | |
6370 buf[size - 1] = NUL; /* Truncate the line */ | |
6371 | |
6372 /* Now throw away the rest of the line: */ | |
6373 do | |
6374 { | |
6375 tbuf[FGETS_SIZE - 2] = NUL; | |
6376 #ifdef USE_CR | |
1757 | 6377 ignoredp = fgets_cr((char *)tbuf, FGETS_SIZE, fp); |
7 | 6378 #else |
1757 | 6379 ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp); |
7 | 6380 #endif |
6381 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n'); | |
6382 } | |
6383 return (eof == NULL); | |
6384 } | |
6385 | |
6386 #if defined(USE_CR) || defined(PROTO) | |
6387 /* | |
6388 * Like vim_fgets(), but accept any line terminator: CR, CR-LF or LF. | |
6389 * Returns TRUE for end-of-file. | |
6390 * Only used for the Mac, because it's much slower than vim_fgets(). | |
6391 */ | |
6392 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6393 tag_fgets(char_u *buf, int size, FILE *fp) |
7 | 6394 { |
6395 int i = 0; | |
6396 int c; | |
6397 int eof = FALSE; | |
6398 | |
6399 for (;;) | |
6400 { | |
6401 c = fgetc(fp); | |
6402 if (c == EOF) | |
6403 { | |
6404 eof = TRUE; | |
6405 break; | |
6406 } | |
6407 if (c == '\r') | |
6408 { | |
6409 /* Always store a NL for end-of-line. */ | |
6410 if (i < size - 1) | |
6411 buf[i++] = '\n'; | |
6412 c = fgetc(fp); | |
6413 if (c != '\n') /* Macintosh format: single CR. */ | |
6414 ungetc(c, fp); | |
6415 break; | |
6416 } | |
6417 if (i < size - 1) | |
6418 buf[i++] = c; | |
6419 if (c == '\n') | |
6420 break; | |
6421 } | |
6422 buf[i] = NUL; | |
6423 return eof; | |
6424 } | |
6425 #endif | |
6426 | |
6427 /* | |
6428 * rename() only works if both files are on the same file system, this | |
6429 * function will (attempts to?) copy the file across if rename fails -- webb | |
6430 * Return -1 for failure, 0 for success. | |
6431 */ | |
6432 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6433 vim_rename(char_u *from, char_u *to) |
7 | 6434 { |
6435 int fd_in; | |
6436 int fd_out; | |
6437 int n; | |
6438 char *errmsg = NULL; | |
6439 char *buffer; | |
6440 #ifdef AMIGA | |
6441 BPTR flock; | |
6442 #endif | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6443 stat_T st; |
194 | 6444 long perm; |
199 | 6445 #ifdef HAVE_ACL |
6446 vim_acl_T acl; /* ACL from original file */ | |
6447 #endif | |
1779 | 6448 int use_tmp_file = FALSE; |
7 | 6449 |
6450 /* | |
1779 | 6451 * When the names are identical, there is nothing to do. When they refer |
6452 * to the same file (ignoring case and slash/backslash differences) but | |
6453 * the file name differs we need to go through a temp file. | |
7 | 6454 */ |
6455 if (fnamecmp(from, to) == 0) | |
1779 | 6456 { |
4242 | 6457 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0) |
1779 | 6458 use_tmp_file = TRUE; |
6459 else | |
6460 return 0; | |
6461 } | |
7 | 6462 |
6463 /* | |
6464 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted. | |
6465 */ | |
6466 if (mch_stat((char *)from, &st) < 0) | |
6467 return -1; | |
6468 | |
1778 | 6469 #ifdef UNIX |
6470 { | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6471 stat_T st_to; |
1778 | 6472 |
6473 /* It's possible for the source and destination to be the same file. | |
6474 * This happens when "from" and "to" differ in case and are on a FAT32 | |
6475 * filesystem. In that case go through a temp file name. */ | |
6476 if (mch_stat((char *)to, &st_to) >= 0 | |
6477 && st.st_dev == st_to.st_dev | |
6478 && st.st_ino == st_to.st_ino) | |
1779 | 6479 use_tmp_file = TRUE; |
6480 } | |
6481 #endif | |
2793 | 6482 #ifdef WIN3264 |
6483 { | |
6484 BY_HANDLE_FILE_INFORMATION info1, info2; | |
6485 | |
6486 /* It's possible for the source and destination to be the same file. | |
6487 * In that case go through a temp file name. This makes rename("foo", | |
6488 * "./foo") a no-op (in a complicated way). */ | |
6489 if (win32_fileinfo(from, &info1) == FILEINFO_OK | |
6490 && win32_fileinfo(to, &info2) == FILEINFO_OK | |
6491 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber | |
6492 && info1.nFileIndexHigh == info2.nFileIndexHigh | |
6493 && info1.nFileIndexLow == info2.nFileIndexLow) | |
6494 use_tmp_file = TRUE; | |
6495 } | |
6496 #endif | |
1779 | 6497 |
6498 if (use_tmp_file) | |
6499 { | |
6500 char tempname[MAXPATHL + 1]; | |
6501 | |
6502 /* | |
6503 * Find a name that doesn't exist and is in the same directory. | |
6504 * Rename "from" to "tempname" and then rename "tempname" to "to". | |
6505 */ | |
6506 if (STRLEN(from) >= MAXPATHL - 5) | |
6507 return -1; | |
6508 STRCPY(tempname, from); | |
6509 for (n = 123; n < 99999; ++n) | |
6510 { | |
6511 sprintf((char *)gettail((char_u *)tempname), "%d", n); | |
6512 if (mch_stat(tempname, &st) < 0) | |
6513 { | |
6514 if (mch_rename((char *)from, tempname) == 0) | |
1778 | 6515 { |
1779 | 6516 if (mch_rename(tempname, (char *)to) == 0) |
6517 return 0; | |
6518 /* Strange, the second step failed. Try moving the | |
6519 * file back and return failure. */ | |
6520 mch_rename(tempname, (char *)from); | |
1778 | 6521 return -1; |
6522 } | |
1779 | 6523 /* If it fails for one temp name it will most likely fail |
6524 * for any temp name, give up. */ | |
6525 return -1; | |
6526 } | |
6527 } | |
6528 return -1; | |
1778 | 6529 } |
6530 | |
7 | 6531 /* |
6532 * Delete the "to" file, this is required on some systems to make the | |
6533 * mch_rename() work, on other systems it makes sure that we don't have | |
6534 * two files when the mch_rename() fails. | |
6535 */ | |
6536 | |
6537 #ifdef AMIGA | |
6538 /* | |
6539 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible | |
6540 * that the name of the "to" file is the same as the "from" file, even | |
1201 | 6541 * though the names are different. To avoid the chance of accidentally |
7 | 6542 * deleting the "from" file (horror!) we lock it during the remove. |
6543 * | |
6544 * When used for making a backup before writing the file: This should not | |
6545 * happen with ":w", because startscript() should detect this problem and | |
6546 * set buf->b_shortname, causing modname() to return a correct ".bak" file | |
6547 * name. This problem does exist with ":w filename", but then the | |
6548 * original file will be somewhere else so the backup isn't really | |
6549 * important. If autoscripting is off the rename may fail. | |
6550 */ | |
6551 flock = Lock((UBYTE *)from, (long)ACCESS_READ); | |
6552 #endif | |
6553 mch_remove(to); | |
6554 #ifdef AMIGA | |
6555 if (flock) | |
6556 UnLock(flock); | |
6557 #endif | |
6558 | |
6559 /* | |
6560 * First try a normal rename, return if it works. | |
6561 */ | |
6562 if (mch_rename((char *)from, (char *)to) == 0) | |
6563 return 0; | |
6564 | |
6565 /* | |
6566 * Rename() failed, try copying the file. | |
6567 */ | |
194 | 6568 perm = mch_getperm(from); |
199 | 6569 #ifdef HAVE_ACL |
6570 /* For systems that support ACL: get the ACL from the original file. */ | |
6571 acl = mch_get_acl(from); | |
6572 #endif | |
7 | 6573 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0); |
6574 if (fd_in == -1) | |
1651 | 6575 { |
6576 #ifdef HAVE_ACL | |
6577 mch_free_acl(acl); | |
6578 #endif | |
7 | 6579 return -1; |
1651 | 6580 } |
194 | 6581 |
6582 /* Create the new file with same permissions as the original. */ | |
557 | 6583 fd_out = mch_open((char *)to, |
6584 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm); | |
7 | 6585 if (fd_out == -1) |
6586 { | |
6587 close(fd_in); | |
1651 | 6588 #ifdef HAVE_ACL |
6589 mch_free_acl(acl); | |
6590 #endif | |
7 | 6591 return -1; |
6592 } | |
6593 | |
6594 buffer = (char *)alloc(BUFSIZE); | |
6595 if (buffer == NULL) | |
6596 { | |
1651 | 6597 close(fd_out); |
7 | 6598 close(fd_in); |
1651 | 6599 #ifdef HAVE_ACL |
6600 mch_free_acl(acl); | |
6601 #endif | |
7 | 6602 return -1; |
6603 } | |
6604 | |
2664 | 6605 while ((n = read_eintr(fd_in, buffer, BUFSIZE)) > 0) |
6606 if (write_eintr(fd_out, buffer, n) != n) | |
7 | 6607 { |
6608 errmsg = _("E208: Error writing to \"%s\""); | |
6609 break; | |
6610 } | |
6611 | |
6612 vim_free(buffer); | |
6613 close(fd_in); | |
6614 if (close(fd_out) < 0) | |
6615 errmsg = _("E209: Error closing \"%s\""); | |
6616 if (n < 0) | |
6617 { | |
6618 errmsg = _("E210: Error reading \"%s\""); | |
6619 to = from; | |
6620 } | |
1201 | 6621 #ifndef UNIX /* for Unix mch_open() already set the permission */ |
194 | 6622 mch_setperm(to, perm); |
570 | 6623 #endif |
199 | 6624 #ifdef HAVE_ACL |
6625 mch_set_acl(to, acl); | |
1651 | 6626 mch_free_acl(acl); |
199 | 6627 #endif |
5788 | 6628 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
5483 | 6629 mch_copy_sec(from, to); |
5479 | 6630 #endif |
7 | 6631 if (errmsg != NULL) |
6632 { | |
6633 EMSG2(errmsg, to); | |
6634 return -1; | |
6635 } | |
6636 mch_remove(from); | |
6637 return 0; | |
6638 } | |
6639 | |
6640 static int already_warned = FALSE; | |
6641 | |
6642 /* | |
6643 * Check if any not hidden buffer has been changed. | |
6644 * Postpone the check if there are characters in the stuff buffer, a global | |
6645 * command is being executed, a mapping is being executed or an autocommand is | |
6646 * busy. | |
6647 * Returns TRUE if some message was written (screen should be redrawn and | |
6648 * cursor positioned). | |
6649 */ | |
6650 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6651 check_timestamps( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6652 int focus) /* called for GUI focus event */ |
7 | 6653 { |
6654 buf_T *buf; | |
6655 int didit = 0; | |
6656 int n; | |
6657 | |
6658 /* Don't check timestamps while system() or another low-level function may | |
6659 * cause us to lose and gain focus. */ | |
6660 if (no_check_timestamps > 0) | |
6661 return FALSE; | |
6662 | |
6663 /* Avoid doing a check twice. The OK/Reload dialog can cause a focus | |
6664 * event and we would keep on checking if the file is steadily growing. | |
6665 * Do check again after typing something. */ | |
6666 if (focus && did_check_timestamps) | |
6667 { | |
6668 need_check_timestamps = TRUE; | |
6669 return FALSE; | |
6670 } | |
6671 | |
6672 if (!stuff_empty() || global_busy || !typebuf_typed() | |
6673 #ifdef FEAT_AUTOCMD | |
1834 | 6674 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0 |
7 | 6675 #endif |
6676 ) | |
6677 need_check_timestamps = TRUE; /* check later */ | |
6678 else | |
6679 { | |
6680 ++no_wait_return; | |
6681 did_check_timestamps = TRUE; | |
6682 already_warned = FALSE; | |
6683 for (buf = firstbuf; buf != NULL; ) | |
6684 { | |
6685 /* Only check buffers in a window. */ | |
6686 if (buf->b_nwindows > 0) | |
6687 { | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6688 bufref_T bufref; |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6689 |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6690 set_bufref(&bufref, buf); |
7 | 6691 n = buf_check_timestamp(buf, focus); |
6692 if (didit < n) | |
6693 didit = n; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6694 if (n > 0 && !bufref_valid(&bufref)) |
7 | 6695 { |
6696 /* Autocommands have removed the buffer, start at the | |
6697 * first one again. */ | |
6698 buf = firstbuf; | |
6699 continue; | |
6700 } | |
6701 } | |
6702 buf = buf->b_next; | |
6703 } | |
6704 --no_wait_return; | |
6705 need_check_timestamps = FALSE; | |
6706 if (need_wait_return && didit == 2) | |
6707 { | |
6708 /* make sure msg isn't overwritten */ | |
6709 msg_puts((char_u *)"\n"); | |
6710 out_flush(); | |
6711 } | |
6712 } | |
6713 return didit; | |
6714 } | |
6715 | |
6716 /* | |
6717 * Move all the lines from buffer "frombuf" to buffer "tobuf". | |
6718 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not | |
6719 * empty. | |
6720 */ | |
6721 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6722 move_lines(buf_T *frombuf, buf_T *tobuf) |
7 | 6723 { |
6724 buf_T *tbuf = curbuf; | |
6725 int retval = OK; | |
6726 linenr_T lnum; | |
6727 char_u *p; | |
6728 | |
6729 /* Copy the lines in "frombuf" to "tobuf". */ | |
6730 curbuf = tobuf; | |
6731 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum) | |
6732 { | |
6733 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE)); | |
6734 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL) | |
6735 { | |
6736 vim_free(p); | |
6737 retval = FAIL; | |
6738 break; | |
6739 } | |
6740 vim_free(p); | |
6741 } | |
6742 | |
6743 /* Delete all the lines in "frombuf". */ | |
6744 if (retval != FAIL) | |
6745 { | |
6746 curbuf = frombuf; | |
1055 | 6747 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum) |
6748 if (ml_delete(lnum, FALSE) == FAIL) | |
7 | 6749 { |
6750 /* Oops! We could try putting back the saved lines, but that | |
6751 * might fail again... */ | |
6752 retval = FAIL; | |
6753 break; | |
6754 } | |
6755 } | |
6756 | |
6757 curbuf = tbuf; | |
6758 return retval; | |
6759 } | |
6760 | |
6761 /* | |
6762 * Check if buffer "buf" has been changed. | |
6763 * Also check if the file for a new buffer unexpectedly appeared. | |
6764 * return 1 if a changed buffer was found. | |
6765 * return 2 if a message has been displayed. | |
6766 * return 0 otherwise. | |
6767 */ | |
6768 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6769 buf_check_timestamp( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6770 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6771 int focus UNUSED) /* called for GUI focus event */ |
7 | 6772 { |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6773 stat_T st; |
7 | 6774 int stat_res; |
6775 int retval = 0; | |
6776 char_u *path; | |
6777 char_u *tbuf; | |
6778 char *mesg = NULL; | |
188 | 6779 char *mesg2 = ""; |
7 | 6780 int helpmesg = FALSE; |
6781 int reload = FALSE; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6782 char *reason; |
7 | 6783 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
6784 int can_reload = FALSE; | |
6785 #endif | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6786 off_T orig_size = buf->b_orig_size; |
7 | 6787 int orig_mode = buf->b_orig_mode; |
6788 #ifdef FEAT_GUI | |
6789 int save_mouse_correct = need_mouse_correct; | |
6790 #endif | |
6791 #ifdef FEAT_AUTOCMD | |
6792 static int busy = FALSE; | |
179 | 6793 int n; |
6794 char_u *s; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6795 bufref_T bufref; |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6796 |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6797 set_bufref(&bufref, buf); |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6798 #endif |
7 | 6799 |
6800 /* If there is no file name, the buffer is not loaded, 'buftype' is | |
6801 * set, we are in the middle of a save or being called recursively: ignore | |
6802 * this buffer. */ | |
6803 if (buf->b_ffname == NULL | |
6804 || buf->b_ml.ml_mfp == NULL | |
6805 #if defined(FEAT_QUICKFIX) | |
6806 || *buf->b_p_bt != NUL | |
6807 #endif | |
6808 || buf->b_saving | |
6809 #ifdef FEAT_AUTOCMD | |
6810 || busy | |
6811 #endif | |
33 | 6812 #ifdef FEAT_NETBEANS_INTG |
6813 || isNetbeansBuffer(buf) | |
6814 #endif | |
7 | 6815 ) |
6816 return 0; | |
6817 | |
6818 if ( !(buf->b_flags & BF_NOTEDITED) | |
6819 && buf->b_mtime != 0 | |
6820 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0 | |
6821 || time_differs((long)st.st_mtime, buf->b_mtime) | |
5863 | 6822 || st.st_size != buf->b_orig_size |
7 | 6823 #ifdef HAVE_ST_MODE |
6824 || (int)st.st_mode != buf->b_orig_mode | |
6825 #else | |
6826 || mch_getperm(buf->b_ffname) != buf->b_orig_mode | |
6827 #endif | |
6828 )) | |
6829 { | |
6830 retval = 1; | |
6831 | |
628 | 6832 /* set b_mtime to stop further warnings (e.g., when executing |
6833 * FileChangedShell autocmd) */ | |
7 | 6834 if (stat_res < 0) |
6835 { | |
6836 buf->b_mtime = 0; | |
6837 buf->b_orig_size = 0; | |
6838 buf->b_orig_mode = 0; | |
6839 } | |
6840 else | |
6841 buf_store_time(buf, &st, buf->b_ffname); | |
6842 | |
6843 /* Don't do anything for a directory. Might contain the file | |
6844 * explorer. */ | |
6845 if (mch_isdir(buf->b_fname)) | |
6846 ; | |
6847 | |
6848 /* | |
6849 * If 'autoread' is set, the buffer has no changes and the file still | |
6850 * exists, reload the buffer. Use the buffer-local option value if it | |
6851 * was set, the global option value otherwise. | |
6852 */ | |
6853 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar) | |
6854 && !bufIsChanged(buf) && stat_res >= 0) | |
6855 reload = TRUE; | |
6856 else | |
6857 { | |
179 | 6858 if (stat_res < 0) |
6859 reason = "deleted"; | |
6860 else if (bufIsChanged(buf)) | |
6861 reason = "conflict"; | |
6862 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf)) | |
6863 reason = "changed"; | |
6864 else if (orig_mode != buf->b_orig_mode) | |
6865 reason = "mode"; | |
6866 else | |
6867 reason = "time"; | |
6868 | |
7 | 6869 #ifdef FEAT_AUTOCMD |
6870 /* | |
6871 * Only give the warning if there are no FileChangedShell | |
6872 * autocommands. | |
6873 * Avoid being called recursively by setting "busy". | |
6874 */ | |
6875 busy = TRUE; | |
532 | 6876 # ifdef FEAT_EVAL |
179 | 6877 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1); |
6878 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1); | |
532 | 6879 # endif |
1834 | 6880 ++allbuf_lock; |
7 | 6881 n = apply_autocmds(EVENT_FILECHANGEDSHELL, |
6882 buf->b_fname, buf->b_fname, FALSE, buf); | |
1834 | 6883 --allbuf_lock; |
7 | 6884 busy = FALSE; |
6885 if (n) | |
6886 { | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6887 if (!bufref_valid(&bufref)) |
7 | 6888 EMSG(_("E246: FileChangedShell autocommand deleted buffer")); |
532 | 6889 # ifdef FEAT_EVAL |
179 | 6890 s = get_vim_var_str(VV_FCS_CHOICE); |
6891 if (STRCMP(s, "reload") == 0 && *reason != 'd') | |
6892 reload = TRUE; | |
6893 else if (STRCMP(s, "ask") == 0) | |
6894 n = FALSE; | |
6895 else | |
532 | 6896 # endif |
179 | 6897 return 2; |
7 | 6898 } |
179 | 6899 if (!n) |
7 | 6900 #endif |
6901 { | |
179 | 6902 if (*reason == 'd') |
6903 mesg = _("E211: File \"%s\" no longer available"); | |
7 | 6904 else |
6905 { | |
6906 helpmesg = TRUE; | |
6907 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
6908 can_reload = TRUE; | |
6909 #endif | |
6910 /* | |
6911 * Check if the file contents really changed to avoid | |
6912 * giving a warning when only the timestamp was set (e.g., | |
6913 * checked out of CVS). Always warn when the buffer was | |
6914 * changed. | |
6915 */ | |
179 | 6916 if (reason[2] == 'n') |
6917 { | |
7 | 6918 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well"); |
179 | 6919 mesg2 = _("See \":help W12\" for more info."); |
6920 } | |
6921 else if (reason[1] == 'h') | |
6922 { | |
7 | 6923 mesg = _("W11: Warning: File \"%s\" has changed since editing started"); |
179 | 6924 mesg2 = _("See \":help W11\" for more info."); |
6925 } | |
6926 else if (*reason == 'm') | |
6927 { | |
7 | 6928 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started"); |
179 | 6929 mesg2 = _("See \":help W16\" for more info."); |
6930 } | |
1913 | 6931 else |
6932 /* Only timestamp changed, store it to avoid a warning | |
6933 * in check_mtime() later. */ | |
6934 buf->b_mtime_read = buf->b_mtime; | |
7 | 6935 } |
6936 } | |
6937 } | |
6938 | |
6939 } | |
6940 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W) | |
6941 && vim_fexists(buf->b_ffname)) | |
6942 { | |
6943 retval = 1; | |
6944 mesg = _("W13: Warning: File \"%s\" has been created after editing started"); | |
6945 buf->b_flags |= BF_NEW_W; | |
6946 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
6947 can_reload = TRUE; | |
6948 #endif | |
6949 } | |
6950 | |
6951 if (mesg != NULL) | |
6952 { | |
6953 path = home_replace_save(buf, buf->b_fname); | |
6954 if (path != NULL) | |
6955 { | |
179 | 6956 if (!helpmesg) |
7 | 6957 mesg2 = ""; |
6958 tbuf = alloc((unsigned)(STRLEN(path) + STRLEN(mesg) | |
6959 + STRLEN(mesg2) + 2)); | |
6960 sprintf((char *)tbuf, mesg, path); | |
1848 | 6961 #ifdef FEAT_EVAL |
6962 /* Set warningmsg here, before the unimportant and output-specific | |
6963 * mesg2 has been appended. */ | |
6964 set_vim_var_string(VV_WARNINGMSG, tbuf, -1); | |
6965 #endif | |
7 | 6966 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
6967 if (can_reload) | |
6968 { | |
6969 if (*mesg2 != NUL) | |
6970 { | |
6971 STRCAT(tbuf, "\n"); | |
6972 STRCAT(tbuf, mesg2); | |
6973 } | |
6974 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"), tbuf, | |
2684 | 6975 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2) |
7 | 6976 reload = TRUE; |
6977 } | |
6978 else | |
6979 #endif | |
6980 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned) | |
6981 { | |
6982 if (*mesg2 != NUL) | |
6983 { | |
6984 STRCAT(tbuf, "; "); | |
6985 STRCAT(tbuf, mesg2); | |
6986 } | |
6987 EMSG(tbuf); | |
6988 retval = 2; | |
6989 } | |
6990 else | |
6991 { | |
8 | 6992 # ifdef FEAT_AUTOCMD |
7 | 6993 if (!autocmd_busy) |
8 | 6994 # endif |
7 | 6995 { |
6996 msg_start(); | |
6997 msg_puts_attr(tbuf, hl_attr(HLF_E) + MSG_HIST); | |
6998 if (*mesg2 != NUL) | |
6999 msg_puts_attr((char_u *)mesg2, | |
7000 hl_attr(HLF_W) + MSG_HIST); | |
7001 msg_clr_eos(); | |
7002 (void)msg_end(); | |
7003 if (emsg_silent == 0) | |
7004 { | |
7005 out_flush(); | |
8 | 7006 # ifdef FEAT_GUI |
7 | 7007 if (!focus) |
8 | 7008 # endif |
7 | 7009 /* give the user some time to think about it */ |
7010 ui_delay(1000L, TRUE); | |
7011 | |
7012 /* don't redraw and erase the message */ | |
7013 redraw_cmdline = FALSE; | |
7014 } | |
7015 } | |
7016 already_warned = TRUE; | |
7017 } | |
7018 | |
7019 vim_free(path); | |
7020 vim_free(tbuf); | |
7021 } | |
7022 } | |
7023 | |
7024 if (reload) | |
3776 | 7025 { |
311 | 7026 /* Reload the buffer. */ |
628 | 7027 buf_reload(buf, orig_mode); |
3776 | 7028 #ifdef FEAT_PERSISTENT_UNDO |
7029 if (buf->b_p_udf && buf->b_ffname != NULL) | |
7030 { | |
7031 char_u hash[UNDO_HASH_SIZE]; | |
7032 buf_T *save_curbuf = curbuf; | |
7033 | |
7034 /* Any existing undo file is unusable, write it now. */ | |
7035 curbuf = buf; | |
7036 u_compute_hash(hash); | |
7037 u_write_undo(NULL, FALSE, buf, hash); | |
7038 curbuf = save_curbuf; | |
7039 } | |
7040 #endif | |
7041 } | |
7 | 7042 |
765 | 7043 #ifdef FEAT_AUTOCMD |
1620 | 7044 /* 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
|
7045 if (bufref_valid(&bufref) && retval != 0) |
765 | 7046 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, |
7047 buf->b_fname, buf->b_fname, FALSE, buf); | |
7048 #endif | |
7 | 7049 #ifdef FEAT_GUI |
7050 /* restore this in case an autocommand has set it; it would break | |
7051 * 'mousefocus' */ | |
7052 need_mouse_correct = save_mouse_correct; | |
7053 #endif | |
7054 | |
7055 return retval; | |
7056 } | |
7057 | |
311 | 7058 /* |
7059 * Reload a buffer that is already loaded. | |
7060 * Used when the file was changed outside of Vim. | |
628 | 7061 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected. |
7062 * buf->b_orig_mode may have been reset already. | |
311 | 7063 */ |
7064 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7065 buf_reload(buf_T *buf, int orig_mode) |
311 | 7066 { |
7067 exarg_T ea; | |
7068 pos_T old_cursor; | |
7069 linenr_T old_topline; | |
7070 int old_ro = buf->b_p_ro; | |
7071 buf_T *savebuf; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
7072 bufref_T bufref; |
311 | 7073 int saved = OK; |
7074 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
|
7075 int flags = READ_NEW; |
311 | 7076 |
7077 /* set curwin/curbuf for "buf" and save some things */ | |
7078 aucmd_prepbuf(&aco, buf); | |
7079 | |
7080 /* We only want to read the text from the file, not reset the syntax | |
7081 * highlighting, clear marks, diff status, etc. Force the fileformat | |
7082 * and encoding to be the same. */ | |
7083 if (prep_exarg(&ea, buf) == OK) | |
7084 { | |
7085 old_cursor = curwin->w_cursor; | |
7086 old_topline = curwin->w_topline; | |
7087 | |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
7088 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
|
7089 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7090 /* 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
|
7091 * 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
|
7092 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
|
7093 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
|
7094 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
|
7095 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7096 |
311 | 7097 /* |
7098 * To behave like when a new file is edited (matters for | |
7099 * BufReadPost autocommands) we first need to delete the current | |
7100 * buffer contents. But if reading the file fails we should keep | |
7101 * the old contents. Can't use memory only, the file might be | |
7102 * too big. Use a hidden buffer to move the buffer contents to. | |
7103 */ | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7104 if (bufempty() || saved == FAIL) |
311 | 7105 savebuf = NULL; |
7106 else | |
7107 { | |
7108 /* Allocate a buffer without putting it in the buffer list. */ | |
7109 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
|
7110 set_bufref(&bufref, savebuf); |
837 | 7111 if (savebuf != NULL && buf == curbuf) |
311 | 7112 { |
7113 /* Open the memline. */ | |
7114 curbuf = savebuf; | |
7115 curwin->w_buffer = savebuf; | |
625 | 7116 saved = ml_open(curbuf); |
311 | 7117 curbuf = buf; |
7118 curwin->w_buffer = buf; | |
7119 } | |
837 | 7120 if (savebuf == NULL || saved == FAIL || buf != curbuf |
311 | 7121 || move_lines(buf, savebuf) == FAIL) |
7122 { | |
7123 EMSG2(_("E462: Could not prepare for reloading \"%s\""), | |
7124 buf->b_fname); | |
7125 saved = FAIL; | |
7126 } | |
7127 } | |
7128 | |
7129 if (saved == OK) | |
7130 { | |
7131 curbuf->b_flags |= BF_CHECK_RO; /* check for RO again */ | |
7132 #ifdef FEAT_AUTOCMD | |
7133 keep_filetype = TRUE; /* don't detect 'filetype' */ | |
7134 #endif | |
7135 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, | |
7136 (linenr_T)0, | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7137 (linenr_T)MAXLNUM, &ea, flags) == FAIL) |
311 | 7138 { |
7139 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
7140 if (!aborting()) | |
7141 #endif | |
7142 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
|
7143 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf) |
311 | 7144 { |
7145 /* Put the text back from the save buffer. First | |
7146 * delete any lines that readfile() added. */ | |
7147 while (!bufempty()) | |
837 | 7148 if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL) |
311 | 7149 break; |
7150 (void)move_lines(savebuf, buf); | |
7151 } | |
7152 } | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7153 else if (buf == curbuf) /* "buf" still valid */ |
311 | 7154 { |
7155 /* Mark the buffer as unmodified and free undo info. */ | |
7156 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
|
7157 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
|
7158 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7159 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
|
7160 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
|
7161 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7162 else |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
7163 { |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7164 /* 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
|
7165 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
|
7166 } |
311 | 7167 } |
7168 } | |
7169 vim_free(ea.cmd); | |
7170 | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
7171 if (savebuf != NULL && bufref_valid(&bufref)) |
311 | 7172 wipe_buffer(savebuf, FALSE); |
7173 | |
7174 #ifdef FEAT_DIFF | |
7175 /* Invalidate diff info if necessary. */ | |
837 | 7176 diff_invalidate(curbuf); |
311 | 7177 #endif |
7178 | |
7179 /* Restore the topline and cursor position and check it (lines may | |
7180 * have been removed). */ | |
7181 if (old_topline > curbuf->b_ml.ml_line_count) | |
7182 curwin->w_topline = curbuf->b_ml.ml_line_count; | |
7183 else | |
7184 curwin->w_topline = old_topline; | |
7185 curwin->w_cursor = old_cursor; | |
7186 check_cursor(); | |
7187 update_topline(); | |
7188 #ifdef FEAT_AUTOCMD | |
7189 keep_filetype = FALSE; | |
7190 #endif | |
7191 #ifdef FEAT_FOLDING | |
7192 { | |
1863 | 7193 win_T *wp; |
7194 tabpage_T *tp; | |
311 | 7195 |
7196 /* Update folds unless they are defined manually. */ | |
1863 | 7197 FOR_ALL_TAB_WINDOWS(tp, wp) |
311 | 7198 if (wp->w_buffer == curwin->w_buffer |
7199 && !foldmethodIsManual(wp)) | |
7200 foldUpdateAll(wp); | |
7201 } | |
7202 #endif | |
7203 /* If the mode didn't change and 'readonly' was set, keep the old | |
7204 * value; the user probably used the ":view" command. But don't | |
7205 * reset it, might have had a read error. */ | |
7206 if (orig_mode == curbuf->b_orig_mode) | |
7207 curbuf->b_p_ro |= old_ro; | |
4071 | 7208 |
7209 /* Modelines must override settings done by autocommands. */ | |
7210 do_modelines(0); | |
311 | 7211 } |
7212 | |
7213 /* restore curwin/curbuf and a few other things */ | |
7214 aucmd_restbuf(&aco); | |
7215 /* Careful: autocommands may have made "buf" invalid! */ | |
7216 } | |
7217 | |
7 | 7218 void |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
7219 buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED) |
7 | 7220 { |
7221 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
|
7222 buf->b_orig_size = st->st_size; |
7 | 7223 #ifdef HAVE_ST_MODE |
7224 buf->b_orig_mode = (int)st->st_mode; | |
7225 #else | |
7226 buf->b_orig_mode = mch_getperm(fname); | |
7227 #endif | |
7228 } | |
7229 | |
7230 /* | |
7231 * Adjust the line with missing eol, used for the next write. | |
7232 * Used for do_filter(), when the input lines for the filter are deleted. | |
7233 */ | |
7234 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7235 write_lnum_adjust(linenr_T offset) |
7 | 7236 { |
2707 | 7237 if (curbuf->b_no_eol_lnum != 0) /* only if there is a missing eol */ |
7238 curbuf->b_no_eol_lnum += offset; | |
7 | 7239 } |
7240 | |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7241 #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
|
7242 /* |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7243 * 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
|
7244 * 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
|
7245 */ |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7246 int |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7247 delete_recursive(char_u *name) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7248 { |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7249 int result = 0; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7250 char_u **files; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7251 int file_count; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7252 int i; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7253 char_u *exp; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7254 |
7629
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7255 /* 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
|
7256 * points to. */ |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7257 if ( |
7657
9c5e8254ea6b
commit https://github.com/vim/vim/commit/203258c3ad2966cc9d08b3805b103333988b30b7
Christian Brabandt <cb@256bit.org>
parents:
7641
diff
changeset
|
7258 # if defined(UNIX) || defined(WIN32) |
9c5e8254ea6b
commit https://github.com/vim/vim/commit/203258c3ad2966cc9d08b3805b103333988b30b7
Christian Brabandt <cb@256bit.org>
parents:
7641
diff
changeset
|
7259 mch_isrealdir(name) |
7629
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7260 # else |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7261 mch_isdir(name) |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7262 # endif |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7263 ) |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7264 { |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7265 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
|
7266 exp = vim_strsave(NameBuff); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7267 if (exp == NULL) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7268 return -1; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7269 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
|
7270 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
|
7271 { |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7272 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
|
7273 if (delete_recursive(files[i]) != 0) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7274 result = -1; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7275 FreeWild(file_count, files); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7276 } |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7277 else |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7278 result = -1; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7279 vim_free(exp); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7280 (void)mch_rmdir(name); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7281 } |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7282 else |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7283 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
|
7284 |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7285 return result; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7286 } |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7287 #endif |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7288 |
7 | 7289 #if defined(TEMPDIRNAMES) || defined(PROTO) |
7290 static long temp_count = 0; /* Temp filename counter. */ | |
7291 | |
7292 /* | |
7293 * Delete the temp directory and all files it contains. | |
7294 */ | |
7295 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7296 vim_deltempdir(void) |
7 | 7297 { |
7298 if (vim_tempdir != NULL) | |
7299 { | |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7300 /* remove the trailing path separator */ |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7301 gettail(vim_tempdir)[-1] = NUL; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7302 delete_recursive(vim_tempdir); |
7 | 7303 vim_free(vim_tempdir); |
7304 vim_tempdir = NULL; | |
7305 } | |
7306 } | |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7307 |
7 | 7308 /* |
1997 | 7309 * Directory "tempdir" was created. Expand this name to a full path and put |
7310 * it in "vim_tempdir". This avoids that using ":cd" would confuse us. | |
7311 * "tempdir" must be no longer than MAXPATHL. | |
7312 */ | |
7313 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7314 vim_settempdir(char_u *tempdir) |
1997 | 7315 { |
7316 char_u *buf; | |
7317 | |
7318 buf = alloc((unsigned)MAXPATHL + 2); | |
7319 if (buf != NULL) | |
7320 { | |
7321 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL) | |
7322 STRCPY(buf, tempdir); | |
9536
b2aada04d84e
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Christian Brabandt <cb@256bit.org>
parents:
9509
diff
changeset
|
7323 add_pathsep(buf); |
1997 | 7324 vim_tempdir = vim_strsave(buf); |
7325 vim_free(buf); | |
7326 } | |
7327 } | |
2006 | 7328 #endif |
1997 | 7329 |
7330 /* | |
7 | 7331 * vim_tempname(): Return a unique name that can be used for a temp file. |
7332 * | |
9291
bff3cd5cf922
commit https://github.com/vim/vim/commit/76ae22fef3cb224ca7fbf97517f881e825d4d0c2
Christian Brabandt <cb@256bit.org>
parents:
9260
diff
changeset
|
7333 * 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
|
7334 * guaranteed to NOT be created. |
7 | 7335 * |
7336 * The returned pointer is to allocated memory. | |
7337 * The returned pointer is NULL if no valid name was found. | |
7338 */ | |
7339 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7340 vim_tempname( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7341 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
|
7342 int keep UNUSED) |
7 | 7343 { |
7344 #ifdef USE_TMPNAM | |
7345 char_u itmp[L_tmpnam]; /* use tmpnam() */ | |
7346 #else | |
7347 char_u itmp[TEMPNAMELEN]; | |
7348 #endif | |
7349 | |
7350 #ifdef TEMPDIRNAMES | |
7351 static char *(tempdirs[]) = {TEMPDIRNAMES}; | |
7352 int i; | |
7353 # ifndef EEXIST | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
7354 stat_T st; |
7 | 7355 # endif |
7356 | |
7357 /* | |
7358 * This will create a directory for private use by this instance of Vim. | |
7359 * This is done once, and the same directory is used for all temp files. | |
7360 * This method avoids security problems because of symlink attacks et al. | |
7361 * It's also a bit faster, because we only need to check for an existing | |
7362 * file when creating the directory and not for each temp file. | |
7363 */ | |
7364 if (vim_tempdir == NULL) | |
7365 { | |
7366 /* | |
7367 * Try the entries in TEMPDIRNAMES to create the temp directory. | |
7368 */ | |
1877 | 7369 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i) |
7 | 7370 { |
2047
85da03763130
updated for version 7.2.333
Bram Moolenaar <bram@zimbu.org>
parents:
2028
diff
changeset
|
7371 # ifndef HAVE_MKDTEMP |
1997 | 7372 size_t itmplen; |
7373 long nr; | |
7374 long off; | |
7375 # endif | |
7376 | |
7305
372c785c04b6
commit https://github.com/vim/vim/commit/e1a61991d9b6fd5f65636d17583f93118268cda5
Christian Brabandt <cb@256bit.org>
parents:
7285
diff
changeset
|
7377 /* 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
|
7378 * Skip the directory check if the expansion fails. */ |
7 | 7379 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
|
7380 if (itmp[0] != '$' && mch_isdir(itmp)) |
372c785c04b6
commit https://github.com/vim/vim/commit/e1a61991d9b6fd5f65636d17583f93118268cda5
Christian Brabandt <cb@256bit.org>
parents:
7285
diff
changeset
|
7381 { |
372c785c04b6
commit https://github.com/vim/vim/commit/e1a61991d9b6fd5f65636d17583f93118268cda5
Christian Brabandt <cb@256bit.org>
parents:
7285
diff
changeset
|
7382 /* directory exists */ |
9536
b2aada04d84e
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Christian Brabandt <cb@256bit.org>
parents:
9509
diff
changeset
|
7383 add_pathsep(itmp); |
1997 | 7384 |
7385 # ifdef HAVE_MKDTEMP | |
9211
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7386 { |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7387 # if defined(UNIX) || defined(VMS) |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7388 /* 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
|
7389 * "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
|
7390 mode_t umask_save = umask(077); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7391 # endif |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7392 /* Leave room for filename */ |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7393 STRCAT(itmp, "vXXXXXX"); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7394 if (mkdtemp((char *)itmp) != NULL) |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7395 vim_settempdir(itmp); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7396 # if defined(UNIX) || defined(VMS) |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7397 (void)umask(umask_save); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7398 # endif |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7399 } |
1997 | 7400 # else |
7 | 7401 /* Get an arbitrary number of up to 6 digits. When it's |
7402 * unlikely that it already exists it will be faster, | |
7403 * otherwise it doesn't matter. The use of mkdir() avoids any | |
7404 * security problems because of the predictable number. */ | |
7405 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
|
7406 itmplen = STRLEN(itmp); |
7 | 7407 |
7408 /* Try up to 10000 different values until we find a name that | |
7409 * doesn't exist. */ | |
7410 for (off = 0; off < 10000L; ++off) | |
7411 { | |
7412 int r; | |
1997 | 7413 # if defined(UNIX) || defined(VMS) |
7 | 7414 mode_t umask_save; |
1997 | 7415 # endif |
7416 | |
7417 sprintf((char *)itmp + itmplen, "v%ld", nr + off); | |
7418 # ifndef EEXIST | |
7 | 7419 /* If mkdir() does not set errno to EEXIST, check for |
7420 * existing file here. There is a race condition then, | |
7421 * although it's fail-safe. */ | |
7422 if (mch_stat((char *)itmp, &st) >= 0) | |
7423 continue; | |
1997 | 7424 # endif |
7425 # if defined(UNIX) || defined(VMS) | |
7 | 7426 /* Make sure the umask doesn't remove the executable bit. |
7427 * "repl" has been reported to use "177". */ | |
7428 umask_save = umask(077); | |
1997 | 7429 # endif |
7 | 7430 r = vim_mkdir(itmp, 0700); |
1997 | 7431 # if defined(UNIX) || defined(VMS) |
7 | 7432 (void)umask(umask_save); |
1997 | 7433 # endif |
7 | 7434 if (r == 0) |
7435 { | |
1997 | 7436 vim_settempdir(itmp); |
7 | 7437 break; |
7438 } | |
1997 | 7439 # ifdef EEXIST |
7 | 7440 /* If the mkdir() didn't fail because the file/dir exists, |
7441 * we probably can't create any dir here, try another | |
7442 * place. */ | |
7443 if (errno != EEXIST) | |
1997 | 7444 # endif |
7 | 7445 break; |
7446 } | |
1997 | 7447 # endif /* HAVE_MKDTEMP */ |
7 | 7448 if (vim_tempdir != NULL) |
7449 break; | |
7450 } | |
7451 } | |
7452 } | |
7453 | |
7454 if (vim_tempdir != NULL) | |
7455 { | |
7456 /* There is no need to check if the file exists, because we own the | |
7457 * directory and nobody else creates a file in it. */ | |
7458 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++); | |
7459 return vim_strsave(itmp); | |
7460 } | |
7461 | |
7462 return NULL; | |
7463 | |
7464 #else /* TEMPDIRNAMES */ | |
7465 | |
7466 # ifdef WIN3264 | |
7467 char szTempFile[_MAX_PATH + 1]; | |
7468 char buf4[4]; | |
7469 char_u *retval; | |
7470 char_u *p; | |
7471 | |
7472 STRCPY(itmp, ""); | |
7473 if (GetTempPath(_MAX_PATH, szTempFile) == 0) | |
2695 | 7474 { |
7475 szTempFile[0] = '.'; /* GetTempPath() failed, use current dir */ | |
7476 szTempFile[1] = NUL; | |
7477 } | |
7 | 7478 strcpy(buf4, "VIM"); |
7479 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
|
7480 if (GetTempFileName(szTempFile, buf4, 0, (LPSTR)itmp) == 0) |
7 | 7481 return NULL; |
6721 | 7482 if (!keep) |
7483 /* 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
|
7484 (void)DeleteFile((LPSTR)itmp); |
7 | 7485 |
7486 /* Backslashes in a temp file name cause problems when filtering with | |
7487 * "sh". NOTE: This also checks 'shellcmdflag' to help those people who | |
7488 * didn't set 'shellslash'. */ | |
7489 retval = vim_strsave(itmp); | |
7490 if (*p_shcf == '-' || p_ssl) | |
7491 for (p = retval; *p; ++p) | |
7492 if (*p == '\\') | |
7493 *p = '/'; | |
7494 return retval; | |
7495 | |
7496 # else /* WIN3264 */ | |
7497 | |
7498 # ifdef USE_TMPNAM | |
2697 | 7499 char_u *p; |
7500 | |
7 | 7501 /* tmpnam() will make its own name */ |
2697 | 7502 p = tmpnam((char *)itmp); |
7503 if (p == NULL || *p == NUL) | |
7 | 7504 return NULL; |
7505 # else | |
7506 char_u *p; | |
7507 | |
7508 # ifdef VMS_TEMPNAM | |
7509 /* mktemp() is not working on VMS. It seems to be | |
7510 * a do-nothing function. Therefore we use tempnam(). | |
7511 */ | |
7512 sprintf((char *)itmp, "VIM%c", extra_char); | |
7513 p = (char_u *)tempnam("tmp:", (char *)itmp); | |
7514 if (p != NULL) | |
7515 { | |
5704 | 7516 /* VMS will use '.LIS' if we don't explicitly specify an extension, |
7 | 7517 * and VIM will then be unable to find the file later */ |
7518 STRCPY(itmp, p); | |
7519 STRCAT(itmp, ".txt"); | |
7520 free(p); | |
7521 } | |
7522 else | |
7523 return NULL; | |
7524 # else | |
7525 STRCPY(itmp, TEMPNAME); | |
7526 if ((p = vim_strchr(itmp, '?')) != NULL) | |
7527 *p = extra_char; | |
7528 if (mktemp((char *)itmp) == NULL) | |
7529 return NULL; | |
7530 # endif | |
7531 # endif | |
7532 | |
7533 return vim_strsave(itmp); | |
7534 # endif /* WIN3264 */ | |
7535 #endif /* TEMPDIRNAMES */ | |
7536 } | |
7537 | |
7538 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) | |
7539 /* | |
7170
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
7540 * 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
|
7541 * it looks like a URL. |
7 | 7542 */ |
7543 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7544 forward_slash(char_u *fname) |
7 | 7545 { |
7546 char_u *p; | |
7547 | |
7170
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
7548 if (path_with_url(fname)) |
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
7549 return; |
7 | 7550 for (p = fname; *p != NUL; ++p) |
7551 # ifdef FEAT_MBYTE | |
7552 /* The Big5 encoding can have '\' in the trail byte. */ | |
474 | 7553 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 7554 ++p; |
7555 else | |
7556 # endif | |
7557 if (*p == '\\') | |
7558 *p = '/'; | |
7559 } | |
7560 #endif | |
7561 | |
7562 | |
7563 /* | |
7564 * Code for automatic commands. | |
7565 * | |
7566 * Only included when "FEAT_AUTOCMD" has been defined. | |
7567 */ | |
7568 | |
7569 #if defined(FEAT_AUTOCMD) || defined(PROTO) | |
7570 | |
7571 /* | |
7572 * The autocommands are stored in a list for each event. | |
7573 * Autocommands for the same pattern, that are consecutive, are joined | |
7574 * together, to avoid having to match the pattern too often. | |
7575 * The result is an array of Autopat lists, which point to AutoCmd lists: | |
7576 * | |
7577 * first_autopat[0] --> Autopat.next --> Autopat.next --> NULL | |
7578 * Autopat.cmds Autopat.cmds | |
7579 * | | | |
7580 * V V | |
7581 * AutoCmd.next AutoCmd.next | |
7582 * | | | |
7583 * V V | |
7584 * AutoCmd.next NULL | |
7585 * | | |
7586 * V | |
7587 * NULL | |
7588 * | |
7589 * first_autopat[1] --> Autopat.next --> NULL | |
7590 * Autopat.cmds | |
7591 * | | |
7592 * V | |
7593 * AutoCmd.next | |
7594 * | | |
7595 * V | |
7596 * NULL | |
7597 * etc. | |
7598 * | |
7599 * The order of AutoCmds is important, this is the order in which they were | |
7600 * defined and will have to be executed. | |
7601 */ | |
7602 typedef struct AutoCmd | |
7603 { | |
7604 char_u *cmd; /* The command to be executed (NULL | |
7605 when command has been removed) */ | |
7606 char nested; /* If autocommands nest here */ | |
7607 char last; /* last command in list */ | |
7608 #ifdef FEAT_EVAL | |
7609 scid_T scriptID; /* script ID where defined */ | |
7610 #endif | |
7611 struct AutoCmd *next; /* Next AutoCmd in list */ | |
7612 } AutoCmd; | |
7613 | |
7614 typedef struct AutoPat | |
7615 { | |
7616 char_u *pat; /* pattern as typed (NULL when pattern | |
7617 has been removed) */ | |
4861
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7618 regprog_T *reg_prog; /* compiled regprog for pattern */ |
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7619 AutoCmd *cmds; /* list of commands to do */ |
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7620 struct AutoPat *next; /* next AutoPat in AutoPat list */ |
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7621 int group; /* group ID */ |
7 | 7622 int patlen; /* strlen() of pat */ |
4861
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7623 int buflocal_nr; /* !=0 for buffer-local AutoPat */ |
7 | 7624 char allow_dirs; /* Pattern may match whole path */ |
7625 char last; /* last pattern for apply_autocmds() */ | |
7626 } AutoPat; | |
7627 | |
7628 static struct event_name | |
7629 { | |
7630 char *name; /* event name */ | |
661 | 7631 event_T event; /* event number */ |
7 | 7632 } event_names[] = |
7633 { | |
7634 {"BufAdd", EVENT_BUFADD}, | |
7635 {"BufCreate", EVENT_BUFADD}, | |
7636 {"BufDelete", EVENT_BUFDELETE}, | |
7637 {"BufEnter", EVENT_BUFENTER}, | |
7638 {"BufFilePost", EVENT_BUFFILEPOST}, | |
7639 {"BufFilePre", EVENT_BUFFILEPRE}, | |
7640 {"BufHidden", EVENT_BUFHIDDEN}, | |
7641 {"BufLeave", EVENT_BUFLEAVE}, | |
7642 {"BufNew", EVENT_BUFNEW}, | |
7643 {"BufNewFile", EVENT_BUFNEWFILE}, | |
7644 {"BufRead", EVENT_BUFREADPOST}, | |
7645 {"BufReadCmd", EVENT_BUFREADCMD}, | |
7646 {"BufReadPost", EVENT_BUFREADPOST}, | |
7647 {"BufReadPre", EVENT_BUFREADPRE}, | |
7648 {"BufUnload", EVENT_BUFUNLOAD}, | |
7649 {"BufWinEnter", EVENT_BUFWINENTER}, | |
7650 {"BufWinLeave", EVENT_BUFWINLEAVE}, | |
7651 {"BufWipeout", EVENT_BUFWIPEOUT}, | |
7652 {"BufWrite", EVENT_BUFWRITEPRE}, | |
7653 {"BufWritePost", EVENT_BUFWRITEPOST}, | |
7654 {"BufWritePre", EVENT_BUFWRITEPRE}, | |
7655 {"BufWriteCmd", EVENT_BUFWRITECMD}, | |
7656 {"CmdwinEnter", EVENT_CMDWINENTER}, | |
7657 {"CmdwinLeave", EVENT_CMDWINLEAVE}, | |
6154 | 7658 {"CmdUndefined", EVENT_CMDUNDEFINED}, |
12 | 7659 {"ColorScheme", EVENT_COLORSCHEME}, |
3676 | 7660 {"CompleteDone", EVENT_COMPLETEDONE}, |
661 | 7661 {"CursorHold", EVENT_CURSORHOLD}, |
7662 {"CursorHoldI", EVENT_CURSORHOLDI}, | |
7663 {"CursorMoved", EVENT_CURSORMOVED}, | |
7664 {"CursorMovedI", EVENT_CURSORMOVEDI}, | |
7 | 7665 {"EncodingChanged", EVENT_ENCODINGCHANGED}, |
7666 {"FileEncoding", EVENT_ENCODINGCHANGED}, | |
7667 {"FileAppendPost", EVENT_FILEAPPENDPOST}, | |
7668 {"FileAppendPre", EVENT_FILEAPPENDPRE}, | |
7669 {"FileAppendCmd", EVENT_FILEAPPENDCMD}, | |
7670 {"FileChangedShell",EVENT_FILECHANGEDSHELL}, | |
765 | 7671 {"FileChangedShellPost",EVENT_FILECHANGEDSHELLPOST}, |
7 | 7672 {"FileChangedRO", EVENT_FILECHANGEDRO}, |
7673 {"FileReadPost", EVENT_FILEREADPOST}, | |
7674 {"FileReadPre", EVENT_FILEREADPRE}, | |
7675 {"FileReadCmd", EVENT_FILEREADCMD}, | |
7676 {"FileType", EVENT_FILETYPE}, | |
7677 {"FileWritePost", EVENT_FILEWRITEPOST}, | |
7678 {"FileWritePre", EVENT_FILEWRITEPRE}, | |
7679 {"FileWriteCmd", EVENT_FILEWRITECMD}, | |
7680 {"FilterReadPost", EVENT_FILTERREADPOST}, | |
7681 {"FilterReadPre", EVENT_FILTERREADPRE}, | |
7682 {"FilterWritePost", EVENT_FILTERWRITEPOST}, | |
7683 {"FilterWritePre", EVENT_FILTERWRITEPRE}, | |
7684 {"FocusGained", EVENT_FOCUSGAINED}, | |
7685 {"FocusLost", EVENT_FOCUSLOST}, | |
7686 {"FuncUndefined", EVENT_FUNCUNDEFINED}, | |
7687 {"GUIEnter", EVENT_GUIENTER}, | |
946 | 7688 {"GUIFailed", EVENT_GUIFAILED}, |
11 | 7689 {"InsertChange", EVENT_INSERTCHANGE}, |
7690 {"InsertEnter", EVENT_INSERTENTER}, | |
7691 {"InsertLeave", EVENT_INSERTLEAVE}, | |
2845 | 7692 {"InsertCharPre", EVENT_INSERTCHARPRE}, |
434 | 7693 {"MenuPopup", EVENT_MENUPOPUP}, |
6935 | 7694 {"OptionSet", EVENT_OPTIONSET}, |
161 | 7695 {"QuickFixCmdPost", EVENT_QUICKFIXCMDPOST}, |
7696 {"QuickFixCmdPre", EVENT_QUICKFIXCMDPRE}, | |
3568 | 7697 {"QuitPre", EVENT_QUITPRE}, |
7 | 7698 {"RemoteReply", EVENT_REMOTEREPLY}, |
574 | 7699 {"SessionLoadPost", EVENT_SESSIONLOADPOST}, |
723 | 7700 {"ShellCmdPost", EVENT_SHELLCMDPOST}, |
7701 {"ShellFilterPost", EVENT_SHELLFILTERPOST}, | |
715 | 7702 {"SourcePre", EVENT_SOURCEPRE}, |
1061 | 7703 {"SourceCmd", EVENT_SOURCECMD}, |
649 | 7704 {"SpellFileMissing",EVENT_SPELLFILEMISSING}, |
7 | 7705 {"StdinReadPost", EVENT_STDINREADPOST}, |
7706 {"StdinReadPre", EVENT_STDINREADPRE}, | |
674 | 7707 {"SwapExists", EVENT_SWAPEXISTS}, |
7 | 7708 {"Syntax", EVENT_SYNTAX}, |
676 | 7709 {"TabEnter", EVENT_TABENTER}, |
7710 {"TabLeave", EVENT_TABLEAVE}, | |
7 | 7711 {"TermChanged", EVENT_TERMCHANGED}, |
7712 {"TermResponse", EVENT_TERMRESPONSE}, | |
4232 | 7713 {"TextChanged", EVENT_TEXTCHANGED}, |
7714 {"TextChangedI", EVENT_TEXTCHANGEDI}, | |
7 | 7715 {"User", EVENT_USER}, |
7716 {"VimEnter", EVENT_VIMENTER}, | |
7717 {"VimLeave", EVENT_VIMLEAVE}, | |
7718 {"VimLeavePre", EVENT_VIMLEAVEPRE}, | |
7719 {"WinEnter", EVENT_WINENTER}, | |
7720 {"WinLeave", EVENT_WINLEAVE}, | |
765 | 7721 {"VimResized", EVENT_VIMRESIZED}, |
661 | 7722 {NULL, (event_T)0} |
7 | 7723 }; |
7724 | |
7725 static AutoPat *first_autopat[NUM_EVENTS] = | |
7726 { | |
7727 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7728 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7729 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7730 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
18 | 7731 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
6935 | 7732 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
7 | 7733 }; |
7734 | |
7735 /* | |
7736 * struct used to keep status while executing autocommands for an event. | |
7737 */ | |
7738 typedef struct AutoPatCmd | |
7739 { | |
7740 AutoPat *curpat; /* next AutoPat to examine */ | |
7741 AutoCmd *nextcmd; /* next AutoCmd to execute */ | |
7742 int group; /* group being used */ | |
7743 char_u *fname; /* fname to match with */ | |
7744 char_u *sfname; /* sfname to match with */ | |
7745 char_u *tail; /* tail of fname */ | |
661 | 7746 event_T event; /* current event */ |
40 | 7747 int arg_bufnr; /* initially equal to <abuf>, set to zero when |
7748 buf is deleted */ | |
7749 struct AutoPatCmd *next; /* chain of active apc-s for auto-invalidation*/ | |
7 | 7750 } AutoPatCmd; |
7751 | |
297 | 7752 static AutoPatCmd *active_apc_list = NULL; /* stack of active autocommands */ |
40 | 7753 |
7 | 7754 /* |
7755 * augroups stores a list of autocmd group names. | |
7756 */ | |
297 | 7757 static garray_T augroups = {0, 0, sizeof(char_u *), 10, NULL}; |
7 | 7758 #define AUGROUP_NAME(i) (((char_u **)augroups.ga_data)[i]) |
7759 | |
7760 /* | |
7761 * The ID of the current group. Group 0 is the default one. | |
7762 */ | |
7763 static int current_augroup = AUGROUP_DEFAULT; | |
7764 | |
7765 static int au_need_clean = FALSE; /* need to delete marked patterns */ | |
7766 | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7767 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
|
7768 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
|
7769 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
|
7770 static void au_cleanup(void); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7771 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
|
7772 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
|
7773 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
|
7774 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
|
7775 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
|
7776 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
|
7777 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
|
7778 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
|
7779 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
|
7780 static void auto_next_pat(AutoPatCmd *apc, int stop_at_last); |
6375 | 7781 #if defined(FEAT_AUTOCMD) || defined(FEAT_WILDIGN) |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7782 static int match_file_pat(char_u *pattern, regprog_T **prog, char_u *fname, char_u *sfname, char_u *tail, int allow_dirs); |
6375 | 7783 #endif |
7 | 7784 |
40 | 7785 |
661 | 7786 static event_T last_event; |
7 | 7787 static int last_group; |
1410 | 7788 static int autocmd_blocked = 0; /* block all autocmds */ |
7 | 7789 |
7790 /* | |
7791 * Show the autocommands for one AutoPat. | |
7792 */ | |
7793 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7794 show_autocmd(AutoPat *ap, event_T event) |
7 | 7795 { |
7796 AutoCmd *ac; | |
7797 | |
7798 /* Check for "got_int" (here and at various places below), which is set | |
7799 * when "q" has been hit for the "--more--" prompt */ | |
7800 if (got_int) | |
7801 return; | |
7802 if (ap->pat == NULL) /* pattern has been removed */ | |
7803 return; | |
7804 | |
7805 msg_putchar('\n'); | |
7806 if (got_int) | |
7807 return; | |
7808 if (event != last_event || ap->group != last_group) | |
7809 { | |
7810 if (ap->group != AUGROUP_DEFAULT) | |
7811 { | |
7812 if (AUGROUP_NAME(ap->group) == NULL) | |
7813 msg_puts_attr((char_u *)_("--Deleted--"), hl_attr(HLF_E)); | |
7814 else | |
7815 msg_puts_attr(AUGROUP_NAME(ap->group), hl_attr(HLF_T)); | |
7816 msg_puts((char_u *)" "); | |
7817 } | |
7818 msg_puts_attr(event_nr2name(event), hl_attr(HLF_T)); | |
7819 last_event = event; | |
7820 last_group = ap->group; | |
7821 msg_putchar('\n'); | |
7822 if (got_int) | |
7823 return; | |
7824 } | |
7825 msg_col = 4; | |
7826 msg_outtrans(ap->pat); | |
7827 | |
7828 for (ac = ap->cmds; ac != NULL; ac = ac->next) | |
7829 { | |
7830 if (ac->cmd != NULL) /* skip removed commands */ | |
7831 { | |
7832 if (msg_col >= 14) | |
7833 msg_putchar('\n'); | |
7834 msg_col = 14; | |
7835 if (got_int) | |
7836 return; | |
7837 msg_outtrans(ac->cmd); | |
500 | 7838 #ifdef FEAT_EVAL |
7839 if (p_verbose > 0) | |
7840 last_set_msg(ac->scriptID); | |
7841 #endif | |
7 | 7842 if (got_int) |
7843 return; | |
7844 if (ac->next != NULL) | |
7845 { | |
7846 msg_putchar('\n'); | |
7847 if (got_int) | |
7848 return; | |
7849 } | |
7850 } | |
7851 } | |
7852 } | |
7853 | |
7854 /* | |
7855 * Mark an autocommand pattern for deletion. | |
7856 */ | |
7857 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7858 au_remove_pat(AutoPat *ap) |
7 | 7859 { |
7860 vim_free(ap->pat); | |
7861 ap->pat = NULL; | |
40 | 7862 ap->buflocal_nr = -1; |
7 | 7863 au_need_clean = TRUE; |
7864 } | |
7865 | |
7866 /* | |
7867 * Mark all commands for a pattern for deletion. | |
7868 */ | |
7869 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7870 au_remove_cmds(AutoPat *ap) |
7 | 7871 { |
7872 AutoCmd *ac; | |
7873 | |
7874 for (ac = ap->cmds; ac != NULL; ac = ac->next) | |
7875 { | |
7876 vim_free(ac->cmd); | |
7877 ac->cmd = NULL; | |
7878 } | |
7879 au_need_clean = TRUE; | |
7880 } | |
7881 | |
7882 /* | |
7883 * Cleanup autocommands and patterns that have been deleted. | |
7884 * This is only done when not executing autocommands. | |
7885 */ | |
7886 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7887 au_cleanup(void) |
7 | 7888 { |
7889 AutoPat *ap, **prev_ap; | |
7890 AutoCmd *ac, **prev_ac; | |
661 | 7891 event_T event; |
7 | 7892 |
7893 if (autocmd_busy || !au_need_clean) | |
7894 return; | |
7895 | |
7896 /* loop over all events */ | |
661 | 7897 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
7898 event = (event_T)((int)event + 1)) | |
7 | 7899 { |
7900 /* loop over all autocommand patterns */ | |
7901 prev_ap = &(first_autopat[(int)event]); | |
7902 for (ap = *prev_ap; ap != NULL; ap = *prev_ap) | |
7903 { | |
7904 /* loop over all commands for this pattern */ | |
7905 prev_ac = &(ap->cmds); | |
7906 for (ac = *prev_ac; ac != NULL; ac = *prev_ac) | |
7907 { | |
7908 /* remove the command if the pattern is to be deleted or when | |
7909 * the command has been marked for deletion */ | |
7910 if (ap->pat == NULL || ac->cmd == NULL) | |
7911 { | |
7912 *prev_ac = ac->next; | |
7913 vim_free(ac->cmd); | |
7914 vim_free(ac); | |
7915 } | |
7916 else | |
7917 prev_ac = &(ac->next); | |
7918 } | |
7919 | |
7920 /* remove the pattern if it has been marked for deletion */ | |
7921 if (ap->pat == NULL) | |
7922 { | |
7923 *prev_ap = ap->next; | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4354
diff
changeset
|
7924 vim_regfree(ap->reg_prog); |
7 | 7925 vim_free(ap); |
7926 } | |
7927 else | |
7928 prev_ap = &(ap->next); | |
7929 } | |
7930 } | |
7931 | |
7932 au_need_clean = FALSE; | |
7933 } | |
7934 | |
7935 /* | |
40 | 7936 * Called when buffer is freed, to remove/invalidate related buffer-local |
7937 * autocmds. | |
7938 */ | |
7939 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7940 aubuflocal_remove(buf_T *buf) |
40 | 7941 { |
7942 AutoPat *ap; | |
661 | 7943 event_T event; |
40 | 7944 AutoPatCmd *apc; |
7945 | |
7946 /* invalidate currently executing autocommands */ | |
7947 for (apc = active_apc_list; apc; apc = apc->next) | |
7948 if (buf->b_fnum == apc->arg_bufnr) | |
7949 apc->arg_bufnr = 0; | |
7950 | |
7951 /* invalidate buflocals looping through events */ | |
661 | 7952 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
7953 event = (event_T)((int)event + 1)) | |
40 | 7954 /* loop over all autocommand patterns */ |
7955 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
7956 if (ap->buflocal_nr == buf->b_fnum) | |
7957 { | |
7958 au_remove_pat(ap); | |
7959 if (p_verbose >= 6) | |
292 | 7960 { |
7961 verbose_enter(); | |
40 | 7962 smsg((char_u *) |
7963 _("auto-removing autocommand: %s <buffer=%d>"), | |
7964 event_nr2name(event), buf->b_fnum); | |
292 | 7965 verbose_leave(); |
7966 } | |
40 | 7967 } |
7968 au_cleanup(); | |
7969 } | |
7970 | |
7971 /* | |
7 | 7972 * Add an autocmd group name. |
7973 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. | |
7974 */ | |
7975 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7976 au_new_group(char_u *name) |
7 | 7977 { |
7978 int i; | |
7979 | |
7980 i = au_find_group(name); | |
7981 if (i == AUGROUP_ERROR) /* the group doesn't exist yet, add it */ | |
7982 { | |
7983 /* First try using a free entry. */ | |
7984 for (i = 0; i < augroups.ga_len; ++i) | |
7985 if (AUGROUP_NAME(i) == NULL) | |
7986 break; | |
7987 if (i == augroups.ga_len && ga_grow(&augroups, 1) == FAIL) | |
7988 return AUGROUP_ERROR; | |
7989 | |
7990 AUGROUP_NAME(i) = vim_strsave(name); | |
7991 if (AUGROUP_NAME(i) == NULL) | |
7992 return AUGROUP_ERROR; | |
7993 if (i == augroups.ga_len) | |
7994 ++augroups.ga_len; | |
7995 } | |
7996 | |
7997 return i; | |
7998 } | |
7999 | |
8000 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8001 au_del_group(char_u *name) |
7 | 8002 { |
8003 int i; | |
8004 | |
8005 i = au_find_group(name); | |
8006 if (i == AUGROUP_ERROR) /* the group doesn't exist */ | |
8007 EMSG2(_("E367: No such group: \"%s\""), name); | |
8008 else | |
8009 { | |
8010 vim_free(AUGROUP_NAME(i)); | |
8011 AUGROUP_NAME(i) = NULL; | |
8012 } | |
8013 } | |
8014 | |
8015 /* | |
8016 * Find the ID of an autocmd group name. | |
8017 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. | |
8018 */ | |
8019 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8020 au_find_group(char_u *name) |
7 | 8021 { |
8022 int i; | |
8023 | |
8024 for (i = 0; i < augroups.ga_len; ++i) | |
8025 if (AUGROUP_NAME(i) != NULL && STRCMP(AUGROUP_NAME(i), name) == 0) | |
8026 return i; | |
8027 return AUGROUP_ERROR; | |
8028 } | |
8029 | |
461 | 8030 /* |
8031 * Return TRUE if augroup "name" exists. | |
8032 */ | |
8033 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8034 au_has_group(char_u *name) |
461 | 8035 { |
8036 return au_find_group(name) != AUGROUP_ERROR; | |
8037 } | |
8038 | |
7 | 8039 /* |
8040 * ":augroup {name}". | |
8041 */ | |
8042 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8043 do_augroup(char_u *arg, int del_group) |
7 | 8044 { |
8045 int i; | |
8046 | |
8047 if (del_group) | |
8048 { | |
8049 if (*arg == NUL) | |
8050 EMSG(_(e_argreq)); | |
8051 else | |
8052 au_del_group(arg); | |
8053 } | |
8054 else if (STRICMP(arg, "end") == 0) /* ":aug end": back to group 0 */ | |
8055 current_augroup = AUGROUP_DEFAULT; | |
8056 else if (*arg) /* ":aug xxx": switch to group xxx */ | |
8057 { | |
8058 i = au_new_group(arg); | |
8059 if (i != AUGROUP_ERROR) | |
8060 current_augroup = i; | |
8061 } | |
8062 else /* ":aug": list the group names */ | |
8063 { | |
8064 msg_start(); | |
8065 for (i = 0; i < augroups.ga_len; ++i) | |
8066 { | |
8067 if (AUGROUP_NAME(i) != NULL) | |
8068 { | |
8069 msg_puts(AUGROUP_NAME(i)); | |
8070 msg_puts((char_u *)" "); | |
8071 } | |
8072 } | |
8073 msg_clr_eos(); | |
8074 msg_end(); | |
8075 } | |
8076 } | |
8077 | |
355 | 8078 #if defined(EXITFREE) || defined(PROTO) |
8079 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8080 free_all_autocmds(void) |
355 | 8081 { |
8082 for (current_augroup = -1; current_augroup < augroups.ga_len; | |
8083 ++current_augroup) | |
8084 do_autocmd((char_u *)"", TRUE); | |
8085 ga_clear_strings(&augroups); | |
8086 } | |
8087 #endif | |
8088 | |
7 | 8089 /* |
8090 * Return the event number for event name "start". | |
8091 * Return NUM_EVENTS if the event name was not found. | |
8092 * Return a pointer to the next event name in "end". | |
8093 */ | |
661 | 8094 static event_T |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8095 event_name2nr(char_u *start, char_u **end) |
7 | 8096 { |
8097 char_u *p; | |
8098 int i; | |
8099 int len; | |
8100 | |
8101 /* the event name ends with end of line, a blank or a comma */ | |
8102 for (p = start; *p && !vim_iswhite(*p) && *p != ','; ++p) | |
8103 ; | |
8104 for (i = 0; event_names[i].name != NULL; ++i) | |
8105 { | |
8106 len = (int)STRLEN(event_names[i].name); | |
8107 if (len == p - start && STRNICMP(event_names[i].name, start, len) == 0) | |
8108 break; | |
8109 } | |
8110 if (*p == ',') | |
8111 ++p; | |
8112 *end = p; | |
8113 if (event_names[i].name == NULL) | |
8114 return NUM_EVENTS; | |
8115 return event_names[i].event; | |
8116 } | |
8117 | |
8118 /* | |
8119 * Return the name for event "event". | |
8120 */ | |
8121 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8122 event_nr2name(event_T event) |
7 | 8123 { |
8124 int i; | |
8125 | |
8126 for (i = 0; event_names[i].name != NULL; ++i) | |
8127 if (event_names[i].event == event) | |
8128 return (char_u *)event_names[i].name; | |
8129 return (char_u *)"Unknown"; | |
8130 } | |
8131 | |
8132 /* | |
8133 * Scan over the events. "*" stands for all events. | |
8134 */ | |
8135 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8136 find_end_event( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8137 char_u *arg, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8138 int have_group) /* TRUE when group name was found */ |
7 | 8139 { |
8140 char_u *pat; | |
8141 char_u *p; | |
8142 | |
8143 if (*arg == '*') | |
8144 { | |
8145 if (arg[1] && !vim_iswhite(arg[1])) | |
8146 { | |
8147 EMSG2(_("E215: Illegal character after *: %s"), arg); | |
8148 return NULL; | |
8149 } | |
8150 pat = arg + 1; | |
8151 } | |
8152 else | |
8153 { | |
8154 for (pat = arg; *pat && !vim_iswhite(*pat); pat = p) | |
8155 { | |
8156 if ((int)event_name2nr(pat, &p) >= (int)NUM_EVENTS) | |
8157 { | |
8158 if (have_group) | |
8159 EMSG2(_("E216: No such event: %s"), pat); | |
8160 else | |
8161 EMSG2(_("E216: No such group or event: %s"), pat); | |
8162 return NULL; | |
8163 } | |
8164 } | |
8165 } | |
8166 return pat; | |
8167 } | |
8168 | |
8169 /* | |
8170 * Return TRUE if "event" is included in 'eventignore'. | |
8171 */ | |
8172 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8173 event_ignored(event_T event) |
7 | 8174 { |
8175 char_u *p = p_ei; | |
8176 | |
844 | 8177 while (*p != NUL) |
8178 { | |
8179 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) | |
8180 return TRUE; | |
7 | 8181 if (event_name2nr(p, &p) == event) |
8182 return TRUE; | |
844 | 8183 } |
7 | 8184 |
8185 return FALSE; | |
8186 } | |
8187 | |
8188 /* | |
8189 * Return OK when the contents of p_ei is valid, FAIL otherwise. | |
8190 */ | |
8191 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8192 check_ei(void) |
7 | 8193 { |
8194 char_u *p = p_ei; | |
8195 | |
8196 while (*p) | |
844 | 8197 { |
8198 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) | |
8199 { | |
8200 p += 3; | |
8201 if (*p == ',') | |
8202 ++p; | |
8203 } | |
8204 else if (event_name2nr(p, &p) == NUM_EVENTS) | |
7 | 8205 return FAIL; |
844 | 8206 } |
7 | 8207 |
8208 return OK; | |
8209 } | |
8210 | |
123 | 8211 # if defined(FEAT_SYN_HL) || defined(PROTO) |
8212 | |
8213 /* | |
8214 * Add "what" to 'eventignore' to skip loading syntax highlighting for every | |
8215 * buffer loaded into the window. "what" must start with a comma. | |
8216 * Returns the old value of 'eventignore' in allocated memory. | |
8217 */ | |
8218 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8219 au_event_disable(char *what) |
123 | 8220 { |
8221 char_u *new_ei; | |
8222 char_u *save_ei; | |
8223 | |
8224 save_ei = vim_strsave(p_ei); | |
8225 if (save_ei != NULL) | |
8226 { | |
557 | 8227 new_ei = vim_strnsave(p_ei, (int)(STRLEN(p_ei) + STRLEN(what))); |
123 | 8228 if (new_ei != NULL) |
8229 { | |
2095
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8230 if (*what == ',' && *p_ei == NUL) |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8231 STRCPY(new_ei, what + 1); |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8232 else |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8233 STRCAT(new_ei, what); |
694 | 8234 set_string_option_direct((char_u *)"ei", -1, new_ei, |
8235 OPT_FREE, SID_NONE); | |
123 | 8236 vim_free(new_ei); |
8237 } | |
8238 } | |
8239 return save_ei; | |
8240 } | |
8241 | |
8242 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8243 au_event_restore(char_u *old_ei) |
123 | 8244 { |
8245 if (old_ei != NULL) | |
8246 { | |
694 | 8247 set_string_option_direct((char_u *)"ei", -1, old_ei, |
8248 OPT_FREE, SID_NONE); | |
123 | 8249 vim_free(old_ei); |
8250 } | |
8251 } | |
8252 # endif /* FEAT_SYN_HL */ | |
8253 | |
7 | 8254 /* |
8255 * do_autocmd() -- implements the :autocmd command. Can be used in the | |
8256 * following ways: | |
8257 * | |
8258 * :autocmd <event> <pat> <cmd> Add <cmd> to the list of commands that | |
8259 * will be automatically executed for <event> | |
8260 * when editing a file matching <pat>, in | |
8261 * the current group. | |
8262 * :autocmd <event> <pat> Show the auto-commands associated with | |
8263 * <event> and <pat>. | |
8264 * :autocmd <event> Show the auto-commands associated with | |
8265 * <event>. | |
8266 * :autocmd Show all auto-commands. | |
8267 * :autocmd! <event> <pat> <cmd> Remove all auto-commands associated with | |
8268 * <event> and <pat>, and add the command | |
8269 * <cmd>, for the current group. | |
8270 * :autocmd! <event> <pat> Remove all auto-commands associated with | |
8271 * <event> and <pat> for the current group. | |
8272 * :autocmd! <event> Remove all auto-commands associated with | |
8273 * <event> for the current group. | |
8274 * :autocmd! Remove ALL auto-commands for the current | |
8275 * group. | |
8276 * | |
8277 * Multiple events and patterns may be given separated by commas. Here are | |
8278 * some examples: | |
8279 * :autocmd bufread,bufenter *.c,*.h set tw=0 smartindent noic | |
8280 * :autocmd bufleave * set tw=79 nosmartindent ic infercase | |
8281 * | |
8282 * :autocmd * *.c show all autocommands for *.c files. | |
609 | 8283 * |
8284 * Mostly a {group} argument can optionally appear before <event>. | |
7 | 8285 */ |
8286 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8287 do_autocmd(char_u *arg, int forceit) |
7 | 8288 { |
8289 char_u *pat; | |
8290 char_u *envpat = NULL; | |
8291 char_u *cmd; | |
661 | 8292 event_T event; |
7 | 8293 int need_free = FALSE; |
8294 int nested = FALSE; | |
8295 int group; | |
8296 | |
8297 /* | |
8298 * Check for a legal group name. If not, use AUGROUP_ALL. | |
8299 */ | |
8300 group = au_get_grouparg(&arg); | |
8301 if (arg == NULL) /* out of memory */ | |
8302 return; | |
8303 | |
8304 /* | |
8305 * Scan over the events. | |
8306 * If we find an illegal name, return here, don't do anything. | |
8307 */ | |
8308 pat = find_end_event(arg, group != AUGROUP_ALL); | |
8309 if (pat == NULL) | |
8310 return; | |
8311 | |
8312 /* | |
8313 * Scan over the pattern. Put a NUL at the end. | |
8314 */ | |
8315 pat = skipwhite(pat); | |
8316 cmd = pat; | |
8317 while (*cmd && (!vim_iswhite(*cmd) || cmd[-1] == '\\')) | |
8318 cmd++; | |
8319 if (*cmd) | |
8320 *cmd++ = NUL; | |
8321 | |
8322 /* Expand environment variables in the pattern. Set 'shellslash', we want | |
8323 * forward slashes here. */ | |
8324 if (vim_strchr(pat, '$') != NULL || vim_strchr(pat, '~') != NULL) | |
8325 { | |
8326 #ifdef BACKSLASH_IN_FILENAME | |
8327 int p_ssl_save = p_ssl; | |
8328 | |
8329 p_ssl = TRUE; | |
8330 #endif | |
8331 envpat = expand_env_save(pat); | |
8332 #ifdef BACKSLASH_IN_FILENAME | |
8333 p_ssl = p_ssl_save; | |
8334 #endif | |
8335 if (envpat != NULL) | |
8336 pat = envpat; | |
8337 } | |
8338 | |
8339 /* | |
8340 * Check for "nested" flag. | |
8341 */ | |
8342 cmd = skipwhite(cmd); | |
8343 if (*cmd != NUL && STRNCMP(cmd, "nested", 6) == 0 && vim_iswhite(cmd[6])) | |
8344 { | |
8345 nested = TRUE; | |
8346 cmd = skipwhite(cmd + 6); | |
8347 } | |
8348 | |
8349 /* | |
8350 * Find the start of the commands. | |
8351 * Expand <sfile> in it. | |
8352 */ | |
8353 if (*cmd != NUL) | |
8354 { | |
8355 cmd = expand_sfile(cmd); | |
8356 if (cmd == NULL) /* some error */ | |
8357 return; | |
8358 need_free = TRUE; | |
8359 } | |
8360 | |
8361 /* | |
8362 * Print header when showing autocommands. | |
8363 */ | |
8364 if (!forceit && *cmd == NUL) | |
8365 { | |
8366 /* Highlight title */ | |
8367 MSG_PUTS_TITLE(_("\n--- Auto-Commands ---")); | |
8368 } | |
8369 | |
8370 /* | |
8371 * Loop over the events. | |
8372 */ | |
661 | 8373 last_event = (event_T)-1; /* for listing the event name */ |
7 | 8374 last_group = AUGROUP_ERROR; /* for listing the group name */ |
8375 if (*arg == '*' || *arg == NUL) | |
8376 { | |
661 | 8377 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
8378 event = (event_T)((int)event + 1)) | |
7 | 8379 if (do_autocmd_event(event, pat, |
8380 nested, cmd, forceit, group) == FAIL) | |
8381 break; | |
8382 } | |
8383 else | |
8384 { | |
8385 while (*arg && !vim_iswhite(*arg)) | |
8386 if (do_autocmd_event(event_name2nr(arg, &arg), pat, | |
8387 nested, cmd, forceit, group) == FAIL) | |
8388 break; | |
8389 } | |
8390 | |
8391 if (need_free) | |
8392 vim_free(cmd); | |
8393 vim_free(envpat); | |
8394 } | |
8395 | |
8396 /* | |
8397 * Find the group ID in a ":autocmd" or ":doautocmd" argument. | |
8398 * The "argp" argument is advanced to the following argument. | |
8399 * | |
8400 * Returns the group ID, AUGROUP_ERROR for error (out of memory). | |
8401 */ | |
8402 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8403 au_get_grouparg(char_u **argp) |
7 | 8404 { |
8405 char_u *group_name; | |
8406 char_u *p; | |
8407 char_u *arg = *argp; | |
8408 int group = AUGROUP_ALL; | |
8409 | |
8410 p = skiptowhite(arg); | |
8411 if (p > arg) | |
8412 { | |
8413 group_name = vim_strnsave(arg, (int)(p - arg)); | |
8414 if (group_name == NULL) /* out of memory */ | |
8415 return AUGROUP_ERROR; | |
8416 group = au_find_group(group_name); | |
8417 if (group == AUGROUP_ERROR) | |
8418 group = AUGROUP_ALL; /* no match, use all groups */ | |
8419 else | |
8420 *argp = skipwhite(p); /* match, skip over group name */ | |
8421 vim_free(group_name); | |
8422 } | |
8423 return group; | |
8424 } | |
8425 | |
8426 /* | |
8427 * do_autocmd() for one event. | |
8428 * If *pat == NUL do for all patterns. | |
8429 * If *cmd == NUL show entries. | |
8430 * If forceit == TRUE delete entries. | |
8431 * If group is not AUGROUP_ALL, only use this group. | |
8432 */ | |
8433 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8434 do_autocmd_event( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8435 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8436 char_u *pat, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8437 int nested, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8438 char_u *cmd, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8439 int forceit, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8440 int group) |
7 | 8441 { |
8442 AutoPat *ap; | |
8443 AutoPat **prev_ap; | |
8444 AutoCmd *ac; | |
8445 AutoCmd **prev_ac; | |
8446 int brace_level; | |
8447 char_u *endpat; | |
8448 int findgroup; | |
8449 int allgroups; | |
8450 int patlen; | |
856 | 8451 int is_buflocal; |
40 | 8452 int buflocal_nr; |
8453 char_u buflocal_pat[25]; /* for "<buffer=X>" */ | |
7 | 8454 |
8455 if (group == AUGROUP_ALL) | |
8456 findgroup = current_augroup; | |
8457 else | |
8458 findgroup = group; | |
8459 allgroups = (group == AUGROUP_ALL && !forceit && *cmd == NUL); | |
8460 | |
8461 /* | |
8462 * Show or delete all patterns for an event. | |
8463 */ | |
8464 if (*pat == NUL) | |
8465 { | |
8466 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
8467 { | |
8468 if (forceit) /* delete the AutoPat, if it's in the current group */ | |
8469 { | |
8470 if (ap->group == findgroup) | |
8471 au_remove_pat(ap); | |
8472 } | |
8473 else if (group == AUGROUP_ALL || ap->group == group) | |
8474 show_autocmd(ap, event); | |
8475 } | |
8476 } | |
8477 | |
8478 /* | |
8479 * Loop through all the specified patterns. | |
8480 */ | |
8481 for ( ; *pat; pat = (*endpat == ',' ? endpat + 1 : endpat)) | |
8482 { | |
8483 /* | |
8484 * Find end of the pattern. | |
8485 * Watch out for a comma in braces, like "*.\{obj,o\}". | |
8486 */ | |
8487 brace_level = 0; | |
8488 for (endpat = pat; *endpat && (*endpat != ',' || brace_level | |
6963 | 8489 || (endpat > pat && endpat[-1] == '\\')); ++endpat) |
7 | 8490 { |
8491 if (*endpat == '{') | |
8492 brace_level++; | |
8493 else if (*endpat == '}') | |
8494 brace_level--; | |
8495 } | |
8496 if (pat == endpat) /* ignore single comma */ | |
8497 continue; | |
8498 patlen = (int)(endpat - pat); | |
8499 | |
8500 /* | |
40 | 8501 * detect special <buflocal[=X]> buffer-local patterns |
8502 */ | |
8503 is_buflocal = FALSE; | |
8504 buflocal_nr = 0; | |
8505 | |
6623 | 8506 if (patlen >= 8 && STRNCMP(pat, "<buffer", 7) == 0 |
40 | 8507 && pat[patlen - 1] == '>') |
8508 { | |
6623 | 8509 /* "<buffer...>": Error will be printed only for addition. |
8510 * printing and removing will proceed silently. */ | |
40 | 8511 is_buflocal = TRUE; |
8512 if (patlen == 8) | |
6623 | 8513 /* "<buffer>" */ |
40 | 8514 buflocal_nr = curbuf->b_fnum; |
8515 else if (patlen > 9 && pat[7] == '=') | |
8516 { | |
6623 | 8517 if (patlen == 13 && STRNICMP(pat, "<buffer=abuf>", 13) == 0) |
8518 /* "<buffer=abuf>" */ | |
40 | 8519 buflocal_nr = autocmd_bufnr; |
8520 else if (skipdigits(pat + 8) == pat + patlen - 1) | |
6623 | 8521 /* "<buffer=123>" */ |
40 | 8522 buflocal_nr = atoi((char *)pat + 8); |
8523 } | |
8524 } | |
8525 | |
8526 if (is_buflocal) | |
8527 { | |
8528 /* normalize pat into standard "<buffer>#N" form */ | |
8529 sprintf((char *)buflocal_pat, "<buffer=%d>", buflocal_nr); | |
8530 pat = buflocal_pat; /* can modify pat and patlen */ | |
835 | 8531 patlen = (int)STRLEN(buflocal_pat); /* but not endpat */ |
40 | 8532 } |
8533 | |
8534 /* | |
7 | 8535 * Find AutoPat entries with this pattern. |
8536 */ | |
8537 prev_ap = &first_autopat[(int)event]; | |
8538 while ((ap = *prev_ap) != NULL) | |
8539 { | |
8540 if (ap->pat != NULL) | |
8541 { | |
8542 /* Accept a pattern when: | |
8543 * - a group was specified and it's that group, or a group was | |
8544 * not specified and it's the current group, or a group was | |
8545 * not specified and we are listing | |
8546 * - the length of the pattern matches | |
40 | 8547 * - the pattern matches. |
8548 * For <buffer[=X]>, this condition works because we normalize | |
8549 * all buffer-local patterns. | |
7 | 8550 */ |
8551 if ((allgroups || ap->group == findgroup) | |
8552 && ap->patlen == patlen | |
8553 && STRNCMP(pat, ap->pat, patlen) == 0) | |
8554 { | |
8555 /* | |
8556 * Remove existing autocommands. | |
8557 * If adding any new autocmd's for this AutoPat, don't | |
8558 * delete the pattern from the autopat list, append to | |
8559 * this list. | |
8560 */ | |
8561 if (forceit) | |
8562 { | |
8563 if (*cmd != NUL && ap->next == NULL) | |
8564 { | |
8565 au_remove_cmds(ap); | |
8566 break; | |
8567 } | |
8568 au_remove_pat(ap); | |
8569 } | |
8570 | |
8571 /* | |
40 | 8572 * Show autocmd's for this autopat, or buflocals <buffer=X> |
7 | 8573 */ |
8574 else if (*cmd == NUL) | |
8575 show_autocmd(ap, event); | |
8576 | |
8577 /* | |
8578 * Add autocmd to this autopat, if it's the last one. | |
8579 */ | |
8580 else if (ap->next == NULL) | |
8581 break; | |
8582 } | |
8583 } | |
8584 prev_ap = &ap->next; | |
8585 } | |
8586 | |
8587 /* | |
8588 * Add a new command. | |
8589 */ | |
8590 if (*cmd != NUL) | |
8591 { | |
8592 /* | |
8593 * If the pattern we want to add a command to does appear at the | |
8594 * end of the list (or not is not in the list at all), add the | |
8595 * pattern at the end of the list. | |
8596 */ | |
8597 if (ap == NULL) | |
8598 { | |
40 | 8599 /* refuse to add buffer-local ap if buffer number is invalid */ |
8600 if (is_buflocal && (buflocal_nr == 0 | |
8601 || buflist_findnr(buflocal_nr) == NULL)) | |
8602 { | |
8603 EMSGN(_("E680: <buffer=%d>: invalid buffer number "), | |
8604 buflocal_nr); | |
8605 return FAIL; | |
8606 } | |
8607 | |
7 | 8608 ap = (AutoPat *)alloc((unsigned)sizeof(AutoPat)); |
8609 if (ap == NULL) | |
8610 return FAIL; | |
8611 ap->pat = vim_strnsave(pat, patlen); | |
8612 ap->patlen = patlen; | |
8613 if (ap->pat == NULL) | |
8614 { | |
8615 vim_free(ap); | |
8616 return FAIL; | |
8617 } | |
40 | 8618 |
8619 if (is_buflocal) | |
8620 { | |
8621 ap->buflocal_nr = buflocal_nr; | |
153 | 8622 ap->reg_prog = NULL; |
40 | 8623 } |
8624 else | |
7 | 8625 { |
153 | 8626 char_u *reg_pat; |
8627 | |
40 | 8628 ap->buflocal_nr = 0; |
153 | 8629 reg_pat = file_pat_to_reg_pat(pat, endpat, |
40 | 8630 &ap->allow_dirs, TRUE); |
153 | 8631 if (reg_pat != NULL) |
8632 ap->reg_prog = vim_regcomp(reg_pat, RE_MAGIC); | |
355 | 8633 vim_free(reg_pat); |
153 | 8634 if (reg_pat == NULL || ap->reg_prog == NULL) |
40 | 8635 { |
8636 vim_free(ap->pat); | |
8637 vim_free(ap); | |
8638 return FAIL; | |
8639 } | |
7 | 8640 } |
8641 ap->cmds = NULL; | |
8642 *prev_ap = ap; | |
8643 ap->next = NULL; | |
8644 if (group == AUGROUP_ALL) | |
8645 ap->group = current_augroup; | |
8646 else | |
8647 ap->group = group; | |
8648 } | |
8649 | |
8650 /* | |
8651 * Add the autocmd at the end of the AutoCmd list. | |
8652 */ | |
8653 prev_ac = &(ap->cmds); | |
8654 while ((ac = *prev_ac) != NULL) | |
8655 prev_ac = &ac->next; | |
8656 ac = (AutoCmd *)alloc((unsigned)sizeof(AutoCmd)); | |
8657 if (ac == NULL) | |
8658 return FAIL; | |
8659 ac->cmd = vim_strsave(cmd); | |
8660 #ifdef FEAT_EVAL | |
8661 ac->scriptID = current_SID; | |
8662 #endif | |
8663 if (ac->cmd == NULL) | |
8664 { | |
8665 vim_free(ac); | |
8666 return FAIL; | |
8667 } | |
8668 ac->next = NULL; | |
8669 *prev_ac = ac; | |
8670 ac->nested = nested; | |
8671 } | |
8672 } | |
8673 | |
8674 au_cleanup(); /* may really delete removed patterns/commands now */ | |
8675 return OK; | |
8676 } | |
8677 | |
8678 /* | |
8679 * Implementation of ":doautocmd [group] event [fname]". | |
8680 * Return OK for success, FAIL for failure; | |
8681 */ | |
8682 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8683 do_doautocmd( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8684 char_u *arg, |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8685 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
|
8686 int *did_something) |
7 | 8687 { |
8688 char_u *fname; | |
8689 int nothing_done = TRUE; | |
8690 int group; | |
8691 | |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8692 if (did_something != NULL) |
9291
bff3cd5cf922
commit https://github.com/vim/vim/commit/76ae22fef3cb224ca7fbf97517f881e825d4d0c2
Christian Brabandt <cb@256bit.org>
parents:
9260
diff
changeset
|
8693 *did_something = FALSE; |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8694 |
7 | 8695 /* |
8696 * Check for a legal group name. If not, use AUGROUP_ALL. | |
8697 */ | |
8698 group = au_get_grouparg(&arg); | |
8699 if (arg == NULL) /* out of memory */ | |
8700 return FAIL; | |
8701 | |
8702 if (*arg == '*') | |
8703 { | |
8704 EMSG(_("E217: Can't execute autocommands for ALL events")); | |
8705 return FAIL; | |
8706 } | |
8707 | |
8708 /* | |
8709 * Scan over the events. | |
8710 * If we find an illegal name, return here, don't do anything. | |
8711 */ | |
8712 fname = find_end_event(arg, group != AUGROUP_ALL); | |
8713 if (fname == NULL) | |
8714 return FAIL; | |
8715 | |
8716 fname = skipwhite(fname); | |
8717 | |
8718 /* | |
8719 * Loop over the events. | |
8720 */ | |
8721 while (*arg && !vim_iswhite(*arg)) | |
8722 if (apply_autocmds_group(event_name2nr(arg, &arg), | |
8723 fname, NULL, TRUE, group, curbuf, NULL)) | |
8724 nothing_done = FALSE; | |
8725 | |
8726 if (nothing_done && do_msg) | |
8727 MSG(_("No matching autocommands")); | |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8728 if (did_something != NULL) |
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8729 *did_something = !nothing_done; |
7 | 8730 |
8731 #ifdef FEAT_EVAL | |
8732 return aborting() ? FAIL : OK; | |
8733 #else | |
8734 return OK; | |
8735 #endif | |
8736 } | |
8737 | |
8738 /* | |
8739 * ":doautoall": execute autocommands for each loaded buffer. | |
8740 */ | |
8741 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8742 ex_doautoall(exarg_T *eap) |
7 | 8743 { |
8744 int retval; | |
8745 aco_save_T aco; | |
8746 buf_T *buf; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
8747 bufref_T bufref; |
3342 | 8748 char_u *arg = eap->arg; |
3350 | 8749 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
|
8750 int did_aucmd; |
7 | 8751 |
8752 /* | |
8753 * This is a bit tricky: For some commands curwin->w_buffer needs to be | |
8754 * equal to curbuf, but for some buffers there may not be a window. | |
8755 * So we change the buffer for the current window for a moment. This | |
8756 * gives problems when the autocommands make changes to the list of | |
8757 * buffers or windows... | |
8758 */ | |
8759 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
8760 { | |
1665 | 8761 if (buf->b_ml.ml_mfp != NULL) |
7 | 8762 { |
8763 /* find a window for this buffer and save some values */ | |
8764 aucmd_prepbuf(&aco, buf); | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
8765 set_bufref(&bufref, buf); |
7 | 8766 |
8767 /* 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
|
8768 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
|
8769 |
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8770 if (call_do_modelines && did_aucmd) |
3342 | 8771 { |
8772 /* Execute the modeline settings, but don't set window-local | |
8773 * options if we are using the current window for another | |
8774 * buffer. */ | |
8775 do_modelines(curwin == aucmd_win ? OPT_NOWIN : 0); | |
8776 } | |
7 | 8777 |
8778 /* restore the current window */ | |
8779 aucmd_restbuf(&aco); | |
8780 | |
8781 /* 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
|
8782 if (retval == FAIL || !bufref_valid(&bufref)) |
7 | 8783 break; |
8784 } | |
8785 } | |
8786 | |
8787 check_cursor(); /* just in case lines got deleted */ | |
8788 } | |
8789 | |
8790 /* | |
3350 | 8791 * Check *argp for <nomodeline>. When it is present return FALSE, otherwise |
8792 * return TRUE and advance *argp to after it. | |
8793 * Thus return TRUE when do_modelines() should be called. | |
8794 */ | |
8795 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8796 check_nomodeline(char_u **argp) |
3350 | 8797 { |
8798 if (STRNCMP(*argp, "<nomodeline>", 12) == 0) | |
8799 { | |
8800 *argp = skipwhite(*argp + 12); | |
8801 return FALSE; | |
8802 } | |
8803 return TRUE; | |
8804 } | |
8805 | |
8806 /* | |
7 | 8807 * Prepare for executing autocommands for (hidden) buffer "buf". |
1906 | 8808 * Search for a visible window containing the current buffer. If there isn't |
8809 * one then use "aucmd_win". | |
7 | 8810 * Set "curbuf" and "curwin" to match "buf". |
934 | 8811 * When FEAT_AUTOCMD is not defined another version is used, see below. |
7 | 8812 */ |
8813 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8814 aucmd_prepbuf( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8815 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
|
8816 buf_T *buf) /* new curbuf */ |
7 | 8817 { |
8818 win_T *win; | |
1906 | 8819 #ifdef FEAT_WINDOWS |
8820 int save_ea; | |
8821 #endif | |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8822 #ifdef FEAT_AUTOCHDIR |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8823 int save_acd; |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8824 #endif |
7 | 8825 |
8826 /* Find a window that is for the new buffer */ | |
8827 if (buf == curbuf) /* be quick when buf is curbuf */ | |
8828 win = curwin; | |
8829 else | |
8830 #ifdef FEAT_WINDOWS | |
8831 for (win = firstwin; win != NULL; win = win->w_next) | |
8832 if (win->w_buffer == buf) | |
8833 break; | |
8834 #else | |
8835 win = NULL; | |
8836 #endif | |
8837 | |
1906 | 8838 /* Allocate "aucmd_win" when needed. If this fails (out of memory) fall |
8839 * back to using the current window. */ | |
8840 if (win == NULL && aucmd_win == NULL) | |
8841 { | |
8842 win_alloc_aucmd_win(); | |
8843 if (aucmd_win == NULL) | |
8844 win = curwin; | |
8845 } | |
1944 | 8846 if (win == NULL && aucmd_win_used) |
8847 /* Strange recursive autocommand, fall back to using the current | |
8848 * window. Expect a few side effects... */ | |
8849 win = curwin; | |
1906 | 8850 |
8851 aco->save_curwin = curwin; | |
8852 aco->save_curbuf = curbuf; | |
7 | 8853 if (win != NULL) |
8854 { | |
1906 | 8855 /* There is a window for "buf" in the current tab page, make it the |
8856 * curwin. This is preferred, it has the least side effects (esp. if | |
8857 * "buf" is curbuf). */ | |
1944 | 8858 aco->use_aucmd_win = FALSE; |
7 | 8859 curwin = win; |
8860 } | |
8861 else | |
8862 { | |
1906 | 8863 /* There is no window for "buf", use "aucmd_win". To minimize the side |
4352 | 8864 * effects, insert it in the current tab page. |
1906 | 8865 * Anything related to a window (e.g., setting folds) may have |
8866 * unexpected results. */ | |
1944 | 8867 aco->use_aucmd_win = TRUE; |
8868 aucmd_win_used = TRUE; | |
1918 | 8869 aucmd_win->w_buffer = buf; |
2258
772bfca06c18
Fix crash when using ":grep".
Bram Moolenaar <bram@vim.org>
parents:
2244
diff
changeset
|
8870 aucmd_win->w_s = &buf->b_s; |
7 | 8871 ++buf->b_nwindows; |
1918 | 8872 win_init_empty(aucmd_win); /* set cursor and topline to safe values */ |
1944 | 8873 |
8874 /* Make sure w_localdir and globaldir are NULL to avoid a chdir() in | |
8875 * win_enter_ext(). */ | |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8876 vim_free(aucmd_win->w_localdir); |
1944 | 8877 aucmd_win->w_localdir = NULL; |
8878 aco->globaldir = globaldir; | |
8879 globaldir = NULL; | |
8880 | |
7 | 8881 |
1906 | 8882 #ifdef FEAT_WINDOWS |
1923 | 8883 /* Split the current window, put the aucmd_win in the upper half. |
8884 * We don't want the BufEnter or WinEnter autocommands. */ | |
8885 block_autocmds(); | |
1906 | 8886 make_snapshot(SNAP_AUCMD_IDX); |
8887 save_ea = p_ea; | |
8888 p_ea = FALSE; | |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8889 |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8890 # ifdef FEAT_AUTOCHDIR |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8891 /* 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
|
8892 save_acd = p_acd; |
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8893 p_acd = FALSE; |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8894 # endif |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8895 |
1906 | 8896 (void)win_split_ins(0, WSP_TOP, aucmd_win, 0); |
8897 (void)win_comp_pos(); /* recompute window positions */ | |
8898 p_ea = save_ea; | |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8899 # ifdef FEAT_AUTOCHDIR |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8900 p_acd = save_acd; |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8901 # endif |
1923 | 8902 unblock_autocmds(); |
1906 | 8903 #endif |
1918 | 8904 curwin = aucmd_win; |
1906 | 8905 } |
7 | 8906 curbuf = buf; |
1906 | 8907 aco->new_curwin = curwin; |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
8908 set_bufref(&aco->new_curbuf, curbuf); |
7 | 8909 } |
8910 | |
8911 /* | |
8912 * Cleanup after executing autocommands for a (hidden) buffer. | |
8913 * Restore the window as it was (if possible). | |
934 | 8914 * When FEAT_AUTOCMD is not defined another version is used, see below. |
7 | 8915 */ |
8916 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8917 aucmd_restbuf( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8918 aco_save_T *aco) /* structure holding saved values */ |
7 | 8919 { |
1906 | 8920 #ifdef FEAT_WINDOWS |
8921 int dummy; | |
8922 #endif | |
8923 | |
1944 | 8924 if (aco->use_aucmd_win) |
1906 | 8925 { |
8926 --curbuf->b_nwindows; | |
8927 #ifdef FEAT_WINDOWS | |
8928 /* Find "aucmd_win", it can't be closed, but it may be in another tab | |
1923 | 8929 * page. Do not trigger autocommands here. */ |
8930 block_autocmds(); | |
1906 | 8931 if (curwin != aucmd_win) |
8932 { | |
8933 tabpage_T *tp; | |
8934 win_T *wp; | |
8935 | |
8936 FOR_ALL_TAB_WINDOWS(tp, wp) | |
8937 { | |
8938 if (wp == aucmd_win) | |
8939 { | |
8940 if (tp != curtab) | |
4354 | 8941 goto_tabpage_tp(tp, TRUE, TRUE); |
1906 | 8942 win_goto(aucmd_win); |
3340 | 8943 goto win_found; |
1906 | 8944 } |
8945 } | |
8946 } | |
3340 | 8947 win_found: |
1906 | 8948 |
8949 /* Remove the window and frame from the tree of frames. */ | |
8950 (void)winframe_remove(curwin, &dummy, NULL); | |
8951 win_remove(curwin, NULL); | |
1944 | 8952 aucmd_win_used = FALSE; |
1906 | 8953 last_status(FALSE); /* may need to remove last status line */ |
8954 restore_snapshot(SNAP_AUCMD_IDX, FALSE); | |
8955 (void)win_comp_pos(); /* recompute window positions */ | |
1923 | 8956 unblock_autocmds(); |
1906 | 8957 |
8958 if (win_valid(aco->save_curwin)) | |
8959 curwin = aco->save_curwin; | |
8960 else | |
8961 /* Hmm, original window disappeared. Just use the first one. */ | |
8962 curwin = firstwin; | |
8963 # ifdef FEAT_EVAL | |
4287 | 8964 vars_clear(&aucmd_win->w_vars->dv_hashtab); /* free all w: variables */ |
8965 hash_init(&aucmd_win->w_vars->dv_hashtab); /* re-use the hashtab */ | |
1906 | 8966 # endif |
8967 #else | |
8968 curwin = aco->save_curwin; | |
8969 #endif | |
8970 curbuf = curwin->w_buffer; | |
8971 | |
1944 | 8972 vim_free(globaldir); |
8973 globaldir = aco->globaldir; | |
8974 | |
1906 | 8975 /* the buffer contents may have changed */ |
8976 check_cursor(); | |
8977 if (curwin->w_topline > curbuf->b_ml.ml_line_count) | |
8978 { | |
8979 curwin->w_topline = curbuf->b_ml.ml_line_count; | |
8980 #ifdef FEAT_DIFF | |
8981 curwin->w_topfill = 0; | |
8982 #endif | |
8983 } | |
8984 #if defined(FEAT_GUI) | |
8985 /* Hide the scrollbars from the aucmd_win and update. */ | |
8986 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_LEFT], FALSE); | |
8987 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_RIGHT], FALSE); | |
8988 gui_may_update_scrollbars(); | |
8989 #endif | |
8990 } | |
8991 else | |
7 | 8992 { |
8993 /* restore curwin */ | |
8994 #ifdef FEAT_WINDOWS | |
8995 if (win_valid(aco->save_curwin)) | |
8996 #endif | |
8997 { | |
1906 | 8998 /* Restore the buffer which was previously edited by curwin, if |
1944 | 8999 * it was changed, we are still the same window and the buffer is |
1906 | 9000 * valid. */ |
7 | 9001 if (curwin == aco->new_curwin |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9002 && curbuf != aco->new_curbuf.br_buf |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9003 && bufref_valid(&aco->new_curbuf) |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9004 && aco->new_curbuf.br_buf->b_ml.ml_mfp != NULL) |
7 | 9005 { |
3497 | 9006 # if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) |
9007 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
|
9008 curwin->w_s = &aco->new_curbuf.br_buf->b_s; |
3497 | 9009 # endif |
7 | 9010 --curbuf->b_nwindows; |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9011 curbuf = aco->new_curbuf.br_buf; |
7 | 9012 curwin->w_buffer = curbuf; |
9013 ++curbuf->b_nwindows; | |
9014 } | |
9015 | |
9016 curwin = aco->save_curwin; | |
9017 curbuf = curwin->w_buffer; | |
6201 | 9018 /* In case the autocommand move the cursor to a position that that |
9019 * not exist in curbuf. */ | |
9020 check_cursor(); | |
7 | 9021 } |
9022 } | |
9023 } | |
9024 | |
9025 static int autocmd_nested = FALSE; | |
9026 | |
9027 /* | |
9028 * Execute autocommands for "event" and file name "fname". | |
9029 * Return TRUE if some commands were executed. | |
9030 */ | |
9031 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9032 apply_autocmds( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9033 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9034 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
|
9035 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
|
9036 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
|
9037 buf_T *buf) /* buffer for <abuf> */ |
7 | 9038 { |
9039 return apply_autocmds_group(event, fname, fname_io, force, | |
9040 AUGROUP_ALL, buf, NULL); | |
9041 } | |
9042 | |
9043 /* | |
9044 * Like apply_autocmds(), but with extra "eap" argument. This takes care of | |
9045 * setting v:filearg. | |
9046 */ | |
9047 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9048 apply_autocmds_exarg( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9049 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9050 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9051 char_u *fname_io, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9052 int force, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9053 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9054 exarg_T *eap) |
7 | 9055 { |
9056 return apply_autocmds_group(event, fname, fname_io, force, | |
9057 AUGROUP_ALL, buf, eap); | |
9058 } | |
9059 | |
9060 /* | |
9061 * Like apply_autocmds(), but handles the caller's retval. If the script | |
9062 * processing is being aborted or if retval is FAIL when inside a try | |
9063 * conditional, no autocommands are executed. If otherwise the autocommands | |
9064 * cause the script to be aborted, retval is set to FAIL. | |
9065 */ | |
9066 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9067 apply_autocmds_retval( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9068 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9069 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
|
9070 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
|
9071 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
|
9072 buf_T *buf, /* buffer for <abuf> */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9073 int *retval) /* pointer to caller's retval */ |
7 | 9074 { |
9075 int did_cmd; | |
9076 | |
532 | 9077 #ifdef FEAT_EVAL |
7 | 9078 if (should_abort(*retval)) |
9079 return FALSE; | |
532 | 9080 #endif |
7 | 9081 |
9082 did_cmd = apply_autocmds_group(event, fname, fname_io, force, | |
9083 AUGROUP_ALL, buf, NULL); | |
532 | 9084 if (did_cmd |
9085 #ifdef FEAT_EVAL | |
9086 && aborting() | |
9087 #endif | |
9088 ) | |
7 | 9089 *retval = FAIL; |
9090 return did_cmd; | |
9091 } | |
9092 | |
609 | 9093 /* |
9094 * Return TRUE when there is a CursorHold autocommand defined. | |
9095 */ | |
7 | 9096 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9097 has_cursorhold(void) |
7 | 9098 { |
661 | 9099 return (first_autopat[(int)(get_real_state() == NORMAL_BUSY |
9100 ? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)] != NULL); | |
7 | 9101 } |
609 | 9102 |
9103 /* | |
9104 * Return TRUE if the CursorHold event can be triggered. | |
9105 */ | |
9106 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9107 trigger_cursorhold(void) |
609 | 9108 { |
661 | 9109 int state; |
9110 | |
2976 | 9111 if (!did_cursorhold |
9112 && has_cursorhold() | |
9113 && !Recording | |
9114 && typebuf.tb_len == 0 | |
978 | 9115 #ifdef FEAT_INS_EXPAND |
9116 && !ins_compl_active() | |
9117 #endif | |
9118 ) | |
661 | 9119 { |
9120 state = get_real_state(); | |
9121 if (state == NORMAL_BUSY || (state & INSERT) != 0) | |
9122 return TRUE; | |
9123 } | |
9124 return FALSE; | |
609 | 9125 } |
661 | 9126 |
9127 /* | |
9128 * Return TRUE when there is a CursorMoved autocommand defined. | |
9129 */ | |
9130 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9131 has_cursormoved(void) |
661 | 9132 { |
9133 return (first_autopat[(int)EVENT_CURSORMOVED] != NULL); | |
9134 } | |
9135 | |
9136 /* | |
9137 * Return TRUE when there is a CursorMovedI autocommand defined. | |
9138 */ | |
9139 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9140 has_cursormovedI(void) |
661 | 9141 { |
9142 return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL); | |
9143 } | |
7 | 9144 |
3390 | 9145 /* |
4232 | 9146 * Return TRUE when there is a TextChanged autocommand defined. |
9147 */ | |
9148 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9149 has_textchanged(void) |
4232 | 9150 { |
9151 return (first_autopat[(int)EVENT_TEXTCHANGED] != NULL); | |
9152 } | |
9153 | |
9154 /* | |
9155 * Return TRUE when there is a TextChangedI autocommand defined. | |
9156 */ | |
9157 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9158 has_textchangedI(void) |
4232 | 9159 { |
9160 return (first_autopat[(int)EVENT_TEXTCHANGEDI] != NULL); | |
9161 } | |
9162 | |
9163 /* | |
3390 | 9164 * Return TRUE when there is an InsertCharPre autocommand defined. |
9165 */ | |
9166 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9167 has_insertcharpre(void) |
3390 | 9168 { |
9169 return (first_autopat[(int)EVENT_INSERTCHARPRE] != NULL); | |
9170 } | |
9171 | |
6154 | 9172 /* |
9173 * Return TRUE when there is an CmdUndefined autocommand defined. | |
9174 */ | |
9175 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9176 has_cmdundefined(void) |
6154 | 9177 { |
9178 return (first_autopat[(int)EVENT_CMDUNDEFINED] != NULL); | |
9179 } | |
9180 | |
9181 /* | |
9182 * Return TRUE when there is an FuncUndefined autocommand defined. | |
9183 */ | |
9184 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9185 has_funcundefined(void) |
6154 | 9186 { |
9187 return (first_autopat[(int)EVENT_FUNCUNDEFINED] != NULL); | |
9188 } | |
9189 | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9190 /* |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9191 * 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
|
9192 * 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
|
9193 */ |
7 | 9194 static int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9195 apply_autocmds_group( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9196 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9197 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
|
9198 char_u *fname_io, /* fname to use for <afile> on cmdline, NULL means |
7 | 9199 use fname */ |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9200 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
|
9201 int group, /* group ID, or AUGROUP_ALL */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9202 buf_T *buf, /* buffer for <abuf> */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9203 exarg_T *eap) /* command arguments */ |
7 | 9204 { |
9205 char_u *sfname = NULL; /* short file name */ | |
9206 char_u *tail; | |
9207 int save_changed; | |
9208 buf_T *old_curbuf; | |
9209 int retval = FALSE; | |
9210 char_u *save_sourcing_name; | |
9211 linenr_T save_sourcing_lnum; | |
9212 char_u *save_autocmd_fname; | |
1723 | 9213 int save_autocmd_fname_full; |
7 | 9214 int save_autocmd_bufnr; |
9215 char_u *save_autocmd_match; | |
9216 int save_autocmd_busy; | |
9217 int save_autocmd_nested; | |
9218 static int nesting = 0; | |
9219 AutoPatCmd patcmd; | |
9220 AutoPat *ap; | |
9221 #ifdef FEAT_EVAL | |
9222 scid_T save_current_SID; | |
9223 void *save_funccalp; | |
9224 char_u *save_cmdarg; | |
9225 long save_cmdbang; | |
9226 #endif | |
9227 static int filechangeshell_busy = FALSE; | |
170 | 9228 #ifdef FEAT_PROFILE |
9229 proftime_T wait_time; | |
9230 #endif | |
6608 | 9231 int did_save_redobuff = FALSE; |
7 | 9232 |
9233 /* | |
9234 * Quickly return if there are no autocommands for this event or | |
9235 * autocommands are blocked. | |
9236 */ | |
1410 | 9237 if (first_autopat[(int)event] == NULL || autocmd_blocked > 0) |
40 | 9238 goto BYPASS_AU; |
7 | 9239 |
9240 /* | |
9241 * When autocommands are busy, new autocommands are only executed when | |
9242 * explicitly enabled with the "nested" flag. | |
9243 */ | |
9244 if (autocmd_busy && !(force || autocmd_nested)) | |
40 | 9245 goto BYPASS_AU; |
7 | 9246 |
9247 #ifdef FEAT_EVAL | |
9248 /* | |
1201 | 9249 * Quickly return when immediately aborting on error, or when an interrupt |
7 | 9250 * occurred or an exception was thrown but not caught. |
9251 */ | |
9252 if (aborting()) | |
40 | 9253 goto BYPASS_AU; |
7 | 9254 #endif |
9255 | |
9256 /* | |
9257 * FileChangedShell never nests, because it can create an endless loop. | |
9258 */ | |
765 | 9259 if (filechangeshell_busy && (event == EVENT_FILECHANGEDSHELL |
9260 || event == EVENT_FILECHANGEDSHELLPOST)) | |
40 | 9261 goto BYPASS_AU; |
7 | 9262 |
9263 /* | |
9264 * Ignore events in 'eventignore'. | |
9265 */ | |
9266 if (event_ignored(event)) | |
40 | 9267 goto BYPASS_AU; |
7 | 9268 |
9269 /* | |
9270 * Allow nesting of autocommands, but restrict the depth, because it's | |
9271 * possible to create an endless loop. | |
9272 */ | |
9273 if (nesting == 10) | |
9274 { | |
9275 EMSG(_("E218: autocommand nesting too deep")); | |
40 | 9276 goto BYPASS_AU; |
7 | 9277 } |
9278 | |
9279 /* | |
9280 * Check if these autocommands are disabled. Used when doing ":all" or | |
9281 * ":ball". | |
9282 */ | |
9283 if ( (autocmd_no_enter | |
9284 && (event == EVENT_WINENTER || event == EVENT_BUFENTER)) | |
9285 || (autocmd_no_leave | |
9286 && (event == EVENT_WINLEAVE || event == EVENT_BUFLEAVE))) | |
40 | 9287 goto BYPASS_AU; |
7 | 9288 |
9289 /* | |
9290 * Save the autocmd_* variables and info about the current buffer. | |
9291 */ | |
9292 save_autocmd_fname = autocmd_fname; | |
1723 | 9293 save_autocmd_fname_full = autocmd_fname_full; |
7 | 9294 save_autocmd_bufnr = autocmd_bufnr; |
9295 save_autocmd_match = autocmd_match; | |
9296 save_autocmd_busy = autocmd_busy; | |
9297 save_autocmd_nested = autocmd_nested; | |
9298 save_changed = curbuf->b_changed; | |
9299 old_curbuf = curbuf; | |
9300 | |
9301 /* | |
9302 * Set the file name to be used for <afile>. | |
1471 | 9303 * Make a copy to avoid that changing a buffer name or directory makes it |
9304 * invalid. | |
7 | 9305 */ |
9306 if (fname_io == NULL) | |
9307 { | |
6935 | 9308 if (event == EVENT_COLORSCHEME || event == EVENT_OPTIONSET) |
5521 | 9309 autocmd_fname = NULL; |
9310 else if (fname != NULL && *fname != NUL) | |
7 | 9311 autocmd_fname = fname; |
9312 else if (buf != NULL) | |
1723 | 9313 autocmd_fname = buf->b_ffname; |
7 | 9314 else |
9315 autocmd_fname = NULL; | |
9316 } | |
9317 else | |
9318 autocmd_fname = fname_io; | |
1471 | 9319 if (autocmd_fname != NULL) |
1723 | 9320 autocmd_fname = vim_strsave(autocmd_fname); |
9321 autocmd_fname_full = FALSE; /* call FullName_save() later */ | |
7 | 9322 |
9323 /* | |
9324 * Set the buffer number to be used for <abuf>. | |
9325 */ | |
9326 if (buf == NULL) | |
9327 autocmd_bufnr = 0; | |
9328 else | |
9329 autocmd_bufnr = buf->b_fnum; | |
9330 | |
9331 /* | |
9332 * When the file name is NULL or empty, use the file name of buffer "buf". | |
9333 * Always use the full path of the file name to match with, in case | |
9334 * "allow_dirs" is set. | |
9335 */ | |
9336 if (fname == NULL || *fname == NUL) | |
9337 { | |
9338 if (buf == NULL) | |
9339 fname = NULL; | |
9340 else | |
9341 { | |
9342 #ifdef FEAT_SYN_HL | |
9343 if (event == EVENT_SYNTAX) | |
9344 fname = buf->b_p_syn; | |
9345 else | |
9346 #endif | |
9347 if (event == EVENT_FILETYPE) | |
9348 fname = buf->b_p_ft; | |
9349 else | |
9350 { | |
9351 if (buf->b_sfname != NULL) | |
9352 sfname = vim_strsave(buf->b_sfname); | |
9353 fname = buf->b_ffname; | |
9354 } | |
9355 } | |
9356 if (fname == NULL) | |
9357 fname = (char_u *)""; | |
9358 fname = vim_strsave(fname); /* make a copy, so we can change it */ | |
9359 } | |
9360 else | |
9361 { | |
9362 sfname = vim_strsave(fname); | |
5521 | 9363 /* Don't try expanding FileType, Syntax, FuncUndefined, WindowID, |
9364 * ColorScheme or QuickFixCmd* */ | |
161 | 9365 if (event == EVENT_FILETYPE |
9366 || event == EVENT_SYNTAX | |
1867 | 9367 || event == EVENT_FUNCUNDEFINED |
161 | 9368 || event == EVENT_REMOTEREPLY |
649 | 9369 || event == EVENT_SPELLFILEMISSING |
161 | 9370 || event == EVENT_QUICKFIXCMDPRE |
5521 | 9371 || event == EVENT_COLORSCHEME |
6935 | 9372 || event == EVENT_OPTIONSET |
161 | 9373 || event == EVENT_QUICKFIXCMDPOST) |
7 | 9374 fname = vim_strsave(fname); |
9375 else | |
9376 fname = FullName_save(fname, FALSE); | |
9377 } | |
9378 if (fname == NULL) /* out of memory */ | |
9379 { | |
9380 vim_free(sfname); | |
40 | 9381 retval = FALSE; |
9382 goto BYPASS_AU; | |
7 | 9383 } |
9384 | |
9385 #ifdef BACKSLASH_IN_FILENAME | |
9386 /* | |
9387 * Replace all backslashes with forward slashes. This makes the | |
9388 * autocommand patterns portable between Unix and MS-DOS. | |
9389 */ | |
9390 if (sfname != NULL) | |
9391 forward_slash(sfname); | |
9392 forward_slash(fname); | |
9393 #endif | |
9394 | |
9395 #ifdef VMS | |
9396 /* remove version for correct match */ | |
9397 if (sfname != NULL) | |
9398 vms_remove_version(sfname); | |
9399 vms_remove_version(fname); | |
9400 #endif | |
9401 | |
9402 /* | |
9403 * Set the name to be used for <amatch>. | |
9404 */ | |
9405 autocmd_match = fname; | |
9406 | |
9407 | |
9408 /* Don't redraw while doing auto commands. */ | |
9409 ++RedrawingDisabled; | |
9410 save_sourcing_name = sourcing_name; | |
9411 sourcing_name = NULL; /* don't free this one */ | |
9412 save_sourcing_lnum = sourcing_lnum; | |
9413 sourcing_lnum = 0; /* no line number here */ | |
9414 | |
9415 #ifdef FEAT_EVAL | |
9416 save_current_SID = current_SID; | |
9417 | |
170 | 9418 # ifdef FEAT_PROFILE |
788 | 9419 if (do_profiling == PROF_YES) |
170 | 9420 prof_child_enter(&wait_time); /* doesn't count for the caller itself */ |
9421 # endif | |
9422 | |
7 | 9423 /* Don't use local function variables, if called from a function */ |
9424 save_funccalp = save_funccal(); | |
9425 #endif | |
9426 | |
9427 /* | |
9428 * When starting to execute autocommands, save the search patterns. | |
9429 */ | |
9430 if (!autocmd_busy) | |
9431 { | |
9432 save_search_patterns(); | |
7285
a34232b17763
commit https://github.com/vim/vim/commit/20ad69ccfb60ef718bd26387ef0e5424461a643e
Christian Brabandt <cb@256bit.org>
parents:
7170
diff
changeset
|
9433 #ifdef FEAT_INS_EXPAND |
6608 | 9434 if (!ins_compl_active()) |
7285
a34232b17763
commit https://github.com/vim/vim/commit/20ad69ccfb60ef718bd26387ef0e5424461a643e
Christian Brabandt <cb@256bit.org>
parents:
7170
diff
changeset
|
9435 #endif |
6608 | 9436 { |
9437 saveRedobuff(); | |
9438 did_save_redobuff = TRUE; | |
9439 } | |
7 | 9440 did_filetype = keep_filetype; |
9441 } | |
9442 | |
9443 /* | |
9444 * Note that we are applying autocmds. Some commands need to know. | |
9445 */ | |
9446 autocmd_busy = TRUE; | |
9447 filechangeshell_busy = (event == EVENT_FILECHANGEDSHELL); | |
9448 ++nesting; /* see matching decrement below */ | |
9449 | |
9450 /* Remember that FileType was triggered. Used for did_filetype(). */ | |
9451 if (event == EVENT_FILETYPE) | |
9452 did_filetype = TRUE; | |
9453 | |
9454 tail = gettail(fname); | |
9455 | |
9456 /* Find first autocommand that matches */ | |
9457 patcmd.curpat = first_autopat[(int)event]; | |
9458 patcmd.nextcmd = NULL; | |
9459 patcmd.group = group; | |
9460 patcmd.fname = fname; | |
9461 patcmd.sfname = sfname; | |
9462 patcmd.tail = tail; | |
9463 patcmd.event = event; | |
40 | 9464 patcmd.arg_bufnr = autocmd_bufnr; |
9465 patcmd.next = NULL; | |
7 | 9466 auto_next_pat(&patcmd, FALSE); |
9467 | |
9468 /* found one, start executing the autocommands */ | |
9469 if (patcmd.curpat != NULL) | |
9470 { | |
40 | 9471 /* add to active_apc_list */ |
9472 patcmd.next = active_apc_list; | |
9473 active_apc_list = &patcmd; | |
9474 | |
7 | 9475 #ifdef FEAT_EVAL |
9476 /* 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
|
9477 save_cmdbang = (long)get_vim_var_nr(VV_CMDBANG); |
7 | 9478 if (eap != NULL) |
9479 { | |
9480 save_cmdarg = set_cmdarg(eap, NULL); | |
9481 set_vim_var_nr(VV_CMDBANG, (long)eap->forceit); | |
9482 } | |
9483 else | |
9484 save_cmdarg = NULL; /* avoid gcc warning */ | |
9485 #endif | |
9486 retval = TRUE; | |
9487 /* mark the last pattern, to avoid an endless loop when more patterns | |
9488 * are added when executing autocommands */ | |
9489 for (ap = patcmd.curpat; ap->next != NULL; ap = ap->next) | |
9490 ap->last = FALSE; | |
9491 ap->last = TRUE; | |
9492 check_lnums(TRUE); /* make sure cursor and topline are valid */ | |
9493 do_cmdline(NULL, getnextac, (void *)&patcmd, | |
9494 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT); | |
9495 #ifdef FEAT_EVAL | |
9496 if (eap != NULL) | |
9497 { | |
9498 (void)set_cmdarg(NULL, save_cmdarg); | |
9499 set_vim_var_nr(VV_CMDBANG, save_cmdbang); | |
9500 } | |
9501 #endif | |
40 | 9502 /* delete from active_apc_list */ |
9503 if (active_apc_list == &patcmd) /* just in case */ | |
9504 active_apc_list = patcmd.next; | |
7 | 9505 } |
9506 | |
9507 --RedrawingDisabled; | |
9508 autocmd_busy = save_autocmd_busy; | |
9509 filechangeshell_busy = FALSE; | |
9510 autocmd_nested = save_autocmd_nested; | |
9511 vim_free(sourcing_name); | |
9512 sourcing_name = save_sourcing_name; | |
9513 sourcing_lnum = save_sourcing_lnum; | |
1471 | 9514 vim_free(autocmd_fname); |
7 | 9515 autocmd_fname = save_autocmd_fname; |
1723 | 9516 autocmd_fname_full = save_autocmd_fname_full; |
7 | 9517 autocmd_bufnr = save_autocmd_bufnr; |
9518 autocmd_match = save_autocmd_match; | |
9519 #ifdef FEAT_EVAL | |
9520 current_SID = save_current_SID; | |
9521 restore_funccal(save_funccalp); | |
170 | 9522 # ifdef FEAT_PROFILE |
788 | 9523 if (do_profiling == PROF_YES) |
170 | 9524 prof_child_exit(&wait_time); |
9525 # endif | |
7 | 9526 #endif |
9527 vim_free(fname); | |
9528 vim_free(sfname); | |
9529 --nesting; /* see matching increment above */ | |
9530 | |
9531 /* | |
9532 * When stopping to execute autocommands, restore the search patterns and | |
5958 | 9533 * the redo buffer. Free any buffers in the au_pending_free_buf list and |
9534 * free any windows in the au_pending_free_win list. | |
7 | 9535 */ |
9536 if (!autocmd_busy) | |
9537 { | |
9538 restore_search_patterns(); | |
6608 | 9539 if (did_save_redobuff) |
9540 restoreRedobuff(); | |
7 | 9541 did_filetype = FALSE; |
5816 | 9542 while (au_pending_free_buf != NULL) |
9543 { | |
9544 buf_T *b = au_pending_free_buf->b_next; | |
9545 vim_free(au_pending_free_buf); | |
9546 au_pending_free_buf = b; | |
9547 } | |
5958 | 9548 while (au_pending_free_win != NULL) |
9549 { | |
9550 win_T *w = au_pending_free_win->w_next; | |
9551 vim_free(au_pending_free_win); | |
9552 au_pending_free_win = w; | |
9553 } | |
7 | 9554 } |
9555 | |
9556 /* | |
9557 * Some events don't set or reset the Changed flag. | |
9558 * Check if still in the same buffer! | |
9559 */ | |
9560 if (curbuf == old_curbuf | |
9561 && (event == EVENT_BUFREADPOST | |
9562 || event == EVENT_BUFWRITEPOST | |
9563 || event == EVENT_FILEAPPENDPOST | |
9564 || event == EVENT_VIMLEAVE | |
9565 || event == EVENT_VIMLEAVEPRE)) | |
9566 { | |
9567 #ifdef FEAT_TITLE | |
9568 if (curbuf->b_changed != save_changed) | |
9569 need_maketitle = TRUE; | |
9570 #endif | |
9571 curbuf->b_changed = save_changed; | |
9572 } | |
9573 | |
9574 au_cleanup(); /* may really delete removed patterns/commands now */ | |
40 | 9575 |
9576 BYPASS_AU: | |
9577 /* When wiping out a buffer make sure all its buffer-local autocommands | |
9578 * are deleted. */ | |
9579 if (event == EVENT_BUFWIPEOUT && buf != NULL) | |
9580 aubuflocal_remove(buf); | |
9581 | |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
9582 if (retval == OK && event == EVENT_FILETYPE) |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
9583 au_did_filetype = TRUE; |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
9584 |
7 | 9585 return retval; |
9586 } | |
9587 | |
1410 | 9588 # ifdef FEAT_EVAL |
9589 static char_u *old_termresponse = NULL; | |
9590 # endif | |
9591 | |
9592 /* | |
9593 * Block triggering autocommands until unblock_autocmd() is called. | |
9594 * Can be used recursively, so long as it's symmetric. | |
9595 */ | |
9596 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9597 block_autocmds(void) |
1410 | 9598 { |
9599 # ifdef FEAT_EVAL | |
9600 /* Remember the value of v:termresponse. */ | |
9601 if (autocmd_blocked == 0) | |
9602 old_termresponse = get_vim_var_str(VV_TERMRESPONSE); | |
9603 # endif | |
9604 ++autocmd_blocked; | |
9605 } | |
9606 | |
9607 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9608 unblock_autocmds(void) |
1410 | 9609 { |
9610 --autocmd_blocked; | |
9611 | |
9612 # ifdef FEAT_EVAL | |
9613 /* When v:termresponse was set while autocommands were blocked, trigger | |
9614 * the autocommands now. Esp. useful when executing a shell command | |
9615 * during startup (vimdiff). */ | |
9616 if (autocmd_blocked == 0 | |
9617 && get_vim_var_str(VV_TERMRESPONSE) != old_termresponse) | |
9618 apply_autocmds(EVENT_TERMRESPONSE, NULL, NULL, FALSE, curbuf); | |
9619 # endif | |
9620 } | |
9621 | |
5008
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9622 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9623 is_autocmd_blocked(void) |
5008
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9624 { |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9625 return autocmd_blocked != 0; |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9626 } |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9627 |
7 | 9628 /* |
9629 * Find next autocommand pattern that matches. | |
9630 */ | |
9631 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9632 auto_next_pat( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9633 AutoPatCmd *apc, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9634 int stop_at_last) /* stop when 'last' flag is set */ |
7 | 9635 { |
9636 AutoPat *ap; | |
9637 AutoCmd *cp; | |
9638 char_u *name; | |
9639 char *s; | |
9640 | |
9641 vim_free(sourcing_name); | |
9642 sourcing_name = NULL; | |
9643 | |
9644 for (ap = apc->curpat; ap != NULL && !got_int; ap = ap->next) | |
9645 { | |
9646 apc->curpat = NULL; | |
9647 | |
1723 | 9648 /* Only use a pattern when it has not been removed, has commands and |
40 | 9649 * the group matches. For buffer-local autocommands only check the |
9650 * buffer number. */ | |
7 | 9651 if (ap->pat != NULL && ap->cmds != NULL |
9652 && (apc->group == AUGROUP_ALL || apc->group == ap->group)) | |
9653 { | |
40 | 9654 /* execution-condition */ |
9655 if (ap->buflocal_nr == 0 | |
6375 | 9656 ? (match_file_pat(NULL, &ap->reg_prog, apc->fname, |
153 | 9657 apc->sfname, apc->tail, ap->allow_dirs)) |
40 | 9658 : ap->buflocal_nr == apc->arg_bufnr) |
7 | 9659 { |
9660 name = event_nr2name(apc->event); | |
9661 s = _("%s Auto commands for \"%s\""); | |
9662 sourcing_name = alloc((unsigned)(STRLEN(s) | |
9663 + STRLEN(name) + ap->patlen + 1)); | |
9664 if (sourcing_name != NULL) | |
9665 { | |
9666 sprintf((char *)sourcing_name, s, | |
9667 (char *)name, (char *)ap->pat); | |
9668 if (p_verbose >= 8) | |
292 | 9669 { |
9670 verbose_enter(); | |
273 | 9671 smsg((char_u *)_("Executing %s"), sourcing_name); |
292 | 9672 verbose_leave(); |
9673 } | |
7 | 9674 } |
9675 | |
9676 apc->curpat = ap; | |
9677 apc->nextcmd = ap->cmds; | |
9678 /* mark last command */ | |
9679 for (cp = ap->cmds; cp->next != NULL; cp = cp->next) | |
9680 cp->last = FALSE; | |
9681 cp->last = TRUE; | |
9682 } | |
9683 line_breakcheck(); | |
9684 if (apc->curpat != NULL) /* found a match */ | |
9685 break; | |
9686 } | |
9687 if (stop_at_last && ap->last) | |
9688 break; | |
9689 } | |
9690 } | |
9691 | |
9692 /* | |
9693 * Get next autocommand command. | |
9694 * Called by do_cmdline() to get the next line for ":if". | |
9695 * Returns allocated string, or NULL for end of autocommands. | |
9696 */ | |
3997 | 9697 char_u * |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9698 getnextac(int c UNUSED, void *cookie, int indent UNUSED) |
7 | 9699 { |
9700 AutoPatCmd *acp = (AutoPatCmd *)cookie; | |
9701 char_u *retval; | |
9702 AutoCmd *ac; | |
9703 | |
9704 /* Can be called again after returning the last line. */ | |
9705 if (acp->curpat == NULL) | |
9706 return NULL; | |
9707 | |
9708 /* repeat until we find an autocommand to execute */ | |
9709 for (;;) | |
9710 { | |
9711 /* skip removed commands */ | |
9712 while (acp->nextcmd != NULL && acp->nextcmd->cmd == NULL) | |
9713 if (acp->nextcmd->last) | |
9714 acp->nextcmd = NULL; | |
9715 else | |
9716 acp->nextcmd = acp->nextcmd->next; | |
9717 | |
9718 if (acp->nextcmd != NULL) | |
9719 break; | |
9720 | |
9721 /* at end of commands, find next pattern that matches */ | |
9722 if (acp->curpat->last) | |
9723 acp->curpat = NULL; | |
9724 else | |
9725 acp->curpat = acp->curpat->next; | |
9726 if (acp->curpat != NULL) | |
9727 auto_next_pat(acp, TRUE); | |
9728 if (acp->curpat == NULL) | |
9729 return NULL; | |
9730 } | |
9731 | |
9732 ac = acp->nextcmd; | |
9733 | |
9734 if (p_verbose >= 9) | |
9735 { | |
292 | 9736 verbose_enter_scroll(); |
273 | 9737 smsg((char_u *)_("autocommand %s"), ac->cmd); |
7 | 9738 msg_puts((char_u *)"\n"); /* don't overwrite this either */ |
292 | 9739 verbose_leave_scroll(); |
7 | 9740 } |
9741 retval = vim_strsave(ac->cmd); | |
9742 autocmd_nested = ac->nested; | |
9743 #ifdef FEAT_EVAL | |
9744 current_SID = ac->scriptID; | |
9745 #endif | |
9746 if (ac->last) | |
9747 acp->nextcmd = NULL; | |
9748 else | |
9749 acp->nextcmd = ac->next; | |
9750 return retval; | |
9751 } | |
9752 | |
9753 /* | |
9754 * Return TRUE if there is a matching autocommand for "fname". | |
40 | 9755 * To account for buffer-local autocommands, function needs to know |
9756 * in which buffer the file will be opened. | |
7 | 9757 */ |
9758 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9759 has_autocmd(event_T event, char_u *sfname, buf_T *buf) |
7 | 9760 { |
9761 AutoPat *ap; | |
9762 char_u *fname; | |
9763 char_u *tail = gettail(sfname); | |
9764 int retval = FALSE; | |
9765 | |
9766 fname = FullName_save(sfname, FALSE); | |
9767 if (fname == NULL) | |
9768 return FALSE; | |
9769 | |
9770 #ifdef BACKSLASH_IN_FILENAME | |
9771 /* | |
9772 * Replace all backslashes with forward slashes. This makes the | |
9773 * autocommand patterns portable between Unix and MS-DOS. | |
9774 */ | |
9775 sfname = vim_strsave(sfname); | |
9776 if (sfname != NULL) | |
9777 forward_slash(sfname); | |
9778 forward_slash(fname); | |
9779 #endif | |
9780 | |
9781 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
9782 if (ap->pat != NULL && ap->cmds != NULL | |
856 | 9783 && (ap->buflocal_nr == 0 |
6375 | 9784 ? match_file_pat(NULL, &ap->reg_prog, |
153 | 9785 fname, sfname, tail, ap->allow_dirs) |
856 | 9786 : buf != NULL && ap->buflocal_nr == buf->b_fnum |
40 | 9787 )) |
7 | 9788 { |
9789 retval = TRUE; | |
9790 break; | |
9791 } | |
9792 | |
9793 vim_free(fname); | |
9794 #ifdef BACKSLASH_IN_FILENAME | |
9795 vim_free(sfname); | |
9796 #endif | |
9797 | |
9798 return retval; | |
9799 } | |
9800 | |
9801 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
9802 /* | |
9803 * Function given to ExpandGeneric() to obtain the list of autocommand group | |
9804 * names. | |
9805 */ | |
9806 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9807 get_augroup_name(expand_T *xp UNUSED, int idx) |
7 | 9808 { |
9809 if (idx == augroups.ga_len) /* add "END" add the end */ | |
9810 return (char_u *)"END"; | |
9811 if (idx >= augroups.ga_len) /* end of list */ | |
9812 return NULL; | |
9813 if (AUGROUP_NAME(idx) == NULL) /* skip deleted entries */ | |
9814 return (char_u *)""; | |
9815 return AUGROUP_NAME(idx); /* return a name */ | |
9816 } | |
9817 | |
9818 static int include_groups = FALSE; | |
9819 | |
9820 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9821 set_context_in_autocmd( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9822 expand_T *xp, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9823 char_u *arg, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9824 int doautocmd) /* TRUE for :doauto*, FALSE for :autocmd */ |
7 | 9825 { |
9826 char_u *p; | |
9827 int group; | |
9828 | |
9829 /* check for a group name, skip it if present */ | |
9830 include_groups = FALSE; | |
9831 p = arg; | |
9832 group = au_get_grouparg(&arg); | |
9833 if (group == AUGROUP_ERROR) | |
9834 return NULL; | |
9835 /* If there only is a group name that's what we expand. */ | |
9836 if (*arg == NUL && group != AUGROUP_ALL && !vim_iswhite(arg[-1])) | |
9837 { | |
9838 arg = p; | |
9839 group = AUGROUP_ALL; | |
9840 } | |
9841 | |
9842 /* skip over event name */ | |
9843 for (p = arg; *p != NUL && !vim_iswhite(*p); ++p) | |
9844 if (*p == ',') | |
9845 arg = p + 1; | |
9846 if (*p == NUL) | |
9847 { | |
9848 if (group == AUGROUP_ALL) | |
9849 include_groups = TRUE; | |
9850 xp->xp_context = EXPAND_EVENTS; /* expand event name */ | |
9851 xp->xp_pattern = arg; | |
9852 return NULL; | |
9853 } | |
9854 | |
9855 /* skip over pattern */ | |
9856 arg = skipwhite(p); | |
9857 while (*arg && (!vim_iswhite(*arg) || arg[-1] == '\\')) | |
9858 arg++; | |
9859 if (*arg) | |
9860 return arg; /* expand (next) command */ | |
9861 | |
9862 if (doautocmd) | |
9863 xp->xp_context = EXPAND_FILES; /* expand file names */ | |
9864 else | |
9865 xp->xp_context = EXPAND_NOTHING; /* pattern is not expanded */ | |
9866 return NULL; | |
9867 } | |
9868 | |
9869 /* | |
9870 * Function given to ExpandGeneric() to obtain the list of event names. | |
9871 */ | |
9872 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9873 get_event_name(expand_T *xp UNUSED, int idx) |
7 | 9874 { |
9875 if (idx < augroups.ga_len) /* First list group names, if wanted */ | |
9876 { | |
9877 if (!include_groups || AUGROUP_NAME(idx) == NULL) | |
9878 return (char_u *)""; /* skip deleted entries */ | |
9879 return AUGROUP_NAME(idx); /* return a name */ | |
9880 } | |
9881 return (char_u *)event_names[idx - augroups.ga_len].name; | |
9882 } | |
9883 | |
9884 #endif /* FEAT_CMDL_COMPL */ | |
9885 | |
9886 /* | |
615 | 9887 * Return TRUE if autocmd is supported. |
9888 */ | |
9889 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9890 autocmd_supported(char_u *name) |
615 | 9891 { |
9892 char_u *p; | |
9893 | |
9894 return (event_name2nr(name, &p) != NUM_EVENTS); | |
9895 } | |
9896 | |
9897 /* | |
612 | 9898 * Return TRUE if an autocommand is defined for a group, event and |
9899 * pattern: The group can be omitted to accept any group. "event" and "pattern" | |
9900 * can be NULL to accept any event and pattern. "pattern" can be NULL to accept | |
9901 * any pattern. Buffer-local patterns <buffer> or <buffer=N> are accepted. | |
9902 * Used for: | |
9903 * exists("#Group") or | |
9904 * exists("#Group#Event") or | |
9905 * exists("#Group#Event#pat") or | |
9906 * exists("#Event") or | |
9907 * exists("#Event#pat") | |
7 | 9908 */ |
9909 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9910 au_exists(char_u *arg) |
7 | 9911 { |
612 | 9912 char_u *arg_save; |
9913 char_u *pattern = NULL; | |
7 | 9914 char_u *event_name; |
9915 char_u *p; | |
661 | 9916 event_T event; |
7 | 9917 AutoPat *ap; |
40 | 9918 buf_T *buflocal_buf = NULL; |
612 | 9919 int group; |
9920 int retval = FALSE; | |
9921 | |
615 | 9922 /* Make a copy so that we can change the '#' chars to a NUL. */ |
612 | 9923 arg_save = vim_strsave(arg); |
9924 if (arg_save == NULL) | |
9925 return FALSE; | |
615 | 9926 p = vim_strchr(arg_save, '#'); |
612 | 9927 if (p != NULL) |
9928 *p++ = NUL; | |
9929 | |
9930 /* First, look for an autocmd group name */ | |
9931 group = au_find_group(arg_save); | |
9932 if (group == AUGROUP_ERROR) | |
9933 { | |
9934 /* Didn't match a group name, assume the first argument is an event. */ | |
9935 group = AUGROUP_ALL; | |
9936 event_name = arg_save; | |
9937 } | |
9938 else | |
9939 { | |
9940 if (p == NULL) | |
9941 { | |
9942 /* "Group": group name is present and it's recognized */ | |
9943 retval = TRUE; | |
9944 goto theend; | |
9945 } | |
9946 | |
9947 /* Must be "Group#Event" or "Group#Event#pat". */ | |
9948 event_name = p; | |
9949 p = vim_strchr(event_name, '#'); | |
9950 if (p != NULL) | |
9951 *p++ = NUL; /* "Group#Event#pat" */ | |
9952 } | |
9953 | |
9954 pattern = p; /* "pattern" is NULL when there is no pattern */ | |
7 | 9955 |
9956 /* find the index (enum) for the event name */ | |
9957 event = event_name2nr(event_name, &p); | |
9958 | |
9959 /* return FALSE if the event name is not recognized */ | |
612 | 9960 if (event == NUM_EVENTS) |
9961 goto theend; | |
7 | 9962 |
9963 /* Find the first autocommand for this event. | |
9964 * If there isn't any, return FALSE; | |
9965 * If there is one and no pattern given, return TRUE; */ | |
9966 ap = first_autopat[(int)event]; | |
9967 if (ap == NULL) | |
612 | 9968 goto theend; |
7 | 9969 |
40 | 9970 /* if pattern is "<buffer>", special handling is needed which uses curbuf */ |
9971 /* for pattern "<buffer=N>, fnamecmp() will work fine */ | |
1962 | 9972 if (pattern != NULL && STRICMP(pattern, "<buffer>") == 0) |
40 | 9973 buflocal_buf = curbuf; |
9974 | |
7 | 9975 /* Check if there is an autocommand with the given pattern. */ |
9976 for ( ; ap != NULL; ap = ap->next) | |
40 | 9977 /* only use a pattern when it has not been removed and has commands. */ |
9978 /* For buffer-local autocommands, fnamecmp() works fine. */ | |
7 | 9979 if (ap->pat != NULL && ap->cmds != NULL |
612 | 9980 && (group == AUGROUP_ALL || ap->group == group) |
1962 | 9981 && (pattern == NULL |
9982 || (buflocal_buf == NULL | |
9983 ? fnamecmp(ap->pat, pattern) == 0 | |
9984 : ap->buflocal_nr == buflocal_buf->b_fnum))) | |
612 | 9985 { |
9986 retval = TRUE; | |
9987 break; | |
9988 } | |
9989 | |
9990 theend: | |
9991 vim_free(arg_save); | |
9992 return retval; | |
7 | 9993 } |
40 | 9994 |
934 | 9995 #else /* FEAT_AUTOCMD */ |
9996 | |
9997 /* | |
9998 * Prepare for executing commands for (hidden) buffer "buf". | |
9999 * This is the non-autocommand version, it simply saves "curbuf" and sets | |
10000 * "curbuf" and "curwin" to match "buf". | |
10001 */ | |
10002 void | |
7856
226ed297307f
commit https://github.com/vim/vim/commit/d14e00ea67afbaa8cb4a7e6b1eb306da6a2d5adb
Christian Brabandt <cb@256bit.org>
parents:
7819
diff
changeset
|
10003 aucmd_prepbuf( |
226ed297307f
commit https://github.com/vim/vim/commit/d14e00ea67afbaa8cb4a7e6b1eb306da6a2d5adb
Christian Brabandt <cb@256bit.org>
parents:
7819
diff
changeset
|
10004 aco_save_T *aco, /* structure to save values in */ |
226ed297307f
commit https://github.com/vim/vim/commit/d14e00ea67afbaa8cb4a7e6b1eb306da6a2d5adb
Christian Brabandt <cb@256bit.org>
parents:
7819
diff
changeset
|
10005 buf_T *buf) /* new curbuf */ |
934 | 10006 { |
1906 | 10007 aco->save_curbuf = curbuf; |
10008 --curbuf->b_nwindows; | |
934 | 10009 curbuf = buf; |
10010 curwin->w_buffer = buf; | |
1906 | 10011 ++curbuf->b_nwindows; |
934 | 10012 } |
10013 | |
10014 /* | |
10015 * Restore after executing commands for a (hidden) buffer. | |
10016 * This is the non-autocommand version. | |
10017 */ | |
10018 void | |
7856
226ed297307f
commit https://github.com/vim/vim/commit/d14e00ea67afbaa8cb4a7e6b1eb306da6a2d5adb
Christian Brabandt <cb@256bit.org>
parents:
7819
diff
changeset
|
10019 aucmd_restbuf( |
226ed297307f
commit https://github.com/vim/vim/commit/d14e00ea67afbaa8cb4a7e6b1eb306da6a2d5adb
Christian Brabandt <cb@256bit.org>
parents:
7819
diff
changeset
|
10020 aco_save_T *aco) /* structure holding saved values */ |
934 | 10021 { |
1906 | 10022 --curbuf->b_nwindows; |
10023 curbuf = aco->save_curbuf; | |
934 | 10024 curwin->w_buffer = curbuf; |
1906 | 10025 ++curbuf->b_nwindows; |
934 | 10026 } |
10027 | |
7 | 10028 #endif /* FEAT_AUTOCMD */ |
10029 | |
934 | 10030 |
7 | 10031 #if defined(FEAT_AUTOCMD) || defined(FEAT_WILDIGN) || defined(PROTO) |
10032 /* | |
153 | 10033 * Try matching a filename with a "pattern" ("prog" is NULL), or use the |
10034 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling | |
10035 * vim_regcomp() often. | |
7 | 10036 * Used for autocommands and 'wildignore'. |
10037 * Returns TRUE if there is a match, FALSE otherwise. | |
10038 */ | |
6375 | 10039 static int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10040 match_file_pat( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10041 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
|
10042 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
|
10043 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
|
10044 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
|
10045 char_u *tail, /* tail of path */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10046 int allow_dirs) /* allow matching with dir */ |
7 | 10047 { |
10048 regmatch_T regmatch; | |
10049 int result = FALSE; | |
10050 | |
4242 | 10051 regmatch.rm_ic = p_fic; /* ignore case if 'fileignorecase' is set */ |
6470 | 10052 if (prog != NULL) |
10053 regmatch.regprog = *prog; | |
7 | 10054 else |
6470 | 10055 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC); |
7 | 10056 |
10057 /* | |
10058 * Try for a match with the pattern with: | |
10059 * 1. the full file name, when the pattern has a '/'. | |
10060 * 2. the short file name, when the pattern has a '/'. | |
10061 * 3. the tail of the file name, when the pattern has no '/'. | |
10062 */ | |
6470 | 10063 if (regmatch.regprog != NULL |
7 | 10064 && ((allow_dirs |
10065 && (vim_regexec(®match, fname, (colnr_T)0) | |
10066 || (sfname != NULL | |
10067 && vim_regexec(®match, sfname, (colnr_T)0)))) | |
6470 | 10068 || (!allow_dirs && vim_regexec(®match, tail, (colnr_T)0)))) |
7 | 10069 result = TRUE; |
10070 | |
6375 | 10071 if (prog != NULL) |
10072 *prog = regmatch.regprog; | |
10073 else | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4354
diff
changeset
|
10074 vim_regfree(regmatch.regprog); |
7 | 10075 return result; |
10076 } | |
10077 #endif | |
10078 | |
10079 #if defined(FEAT_WILDIGN) || defined(PROTO) | |
10080 /* | |
10081 * Return TRUE if a file matches with a pattern in "list". | |
10082 * "list" is a comma-separated list of patterns, like 'wildignore'. | |
10083 * "sfname" is the short file name or NULL, "ffname" the long file name. | |
10084 */ | |
10085 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10086 match_file_list(char_u *list, char_u *sfname, char_u *ffname) |
7 | 10087 { |
10088 char_u buf[100]; | |
10089 char_u *tail; | |
10090 char_u *regpat; | |
10091 char allow_dirs; | |
10092 int match; | |
10093 char_u *p; | |
10094 | |
10095 tail = gettail(sfname); | |
10096 | |
10097 /* try all patterns in 'wildignore' */ | |
10098 p = list; | |
10099 while (*p) | |
10100 { | |
10101 copy_option_part(&p, buf, 100, ","); | |
10102 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE); | |
10103 if (regpat == NULL) | |
10104 break; | |
153 | 10105 match = match_file_pat(regpat, NULL, ffname, sfname, |
10106 tail, (int)allow_dirs); | |
7 | 10107 vim_free(regpat); |
10108 if (match) | |
10109 return TRUE; | |
10110 } | |
10111 return FALSE; | |
10112 } | |
10113 #endif | |
10114 | |
10115 /* | |
10116 * Convert the given pattern "pat" which has shell style wildcards in it, into | |
10117 * a regular expression, and return the result in allocated memory. If there | |
10118 * is a directory path separator to be matched, then TRUE is put in | |
10119 * allow_dirs, otherwise FALSE is put there -- webb. | |
10120 * Handle backslashes before special characters, like "\*" and "\ ". | |
10121 * | |
10122 * Returns NULL when out of memory. | |
10123 */ | |
10124 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10125 file_pat_to_reg_pat( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10126 char_u *pat, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10127 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
|
10128 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
|
10129 int no_bslash UNUSED) /* Don't use a backward slash as pathsep */ |
7 | 10130 { |
6470 | 10131 int size = 2; /* '^' at start, '$' at end */ |
7 | 10132 char_u *endp; |
10133 char_u *reg_pat; | |
10134 char_u *p; | |
10135 int i; | |
10136 int nested = 0; | |
10137 int add_dollar = TRUE; | |
10138 | |
10139 if (allow_dirs != NULL) | |
10140 *allow_dirs = FALSE; | |
10141 if (pat_end == NULL) | |
10142 pat_end = pat + STRLEN(pat); | |
10143 | |
10144 for (p = pat; p < pat_end; p++) | |
10145 { | |
10146 switch (*p) | |
10147 { | |
10148 case '*': | |
10149 case '.': | |
10150 case ',': | |
10151 case '{': | |
10152 case '}': | |
10153 case '~': | |
10154 size += 2; /* extra backslash */ | |
10155 break; | |
10156 #ifdef BACKSLASH_IN_FILENAME | |
10157 case '\\': | |
10158 case '/': | |
10159 size += 4; /* could become "[\/]" */ | |
10160 break; | |
10161 #endif | |
10162 default: | |
10163 size++; | |
6999 | 10164 # ifdef FEAT_MBYTE |
474 | 10165 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 10166 { |
10167 ++p; | |
10168 ++size; | |
10169 } | |
10170 # endif | |
10171 break; | |
10172 } | |
10173 } | |
10174 reg_pat = alloc(size + 1); | |
10175 if (reg_pat == NULL) | |
10176 return NULL; | |
10177 | |
10178 i = 0; | |
10179 | |
10180 if (pat[0] == '*') | |
10181 while (pat[0] == '*' && pat < pat_end - 1) | |
10182 pat++; | |
10183 else | |
10184 reg_pat[i++] = '^'; | |
10185 endp = pat_end - 1; | |
7005 | 10186 if (endp >= pat && *endp == '*') |
7 | 10187 { |
10188 while (endp - pat > 0 && *endp == '*') | |
10189 endp--; | |
10190 add_dollar = FALSE; | |
10191 } | |
10192 for (p = pat; *p && nested >= 0 && p <= endp; p++) | |
10193 { | |
10194 switch (*p) | |
10195 { | |
10196 case '*': | |
10197 reg_pat[i++] = '.'; | |
10198 reg_pat[i++] = '*'; | |
444 | 10199 while (p[1] == '*') /* "**" matches like "*" */ |
10200 ++p; | |
7 | 10201 break; |
10202 case '.': | |
10203 case '~': | |
10204 reg_pat[i++] = '\\'; | |
10205 reg_pat[i++] = *p; | |
10206 break; | |
10207 case '?': | |
10208 reg_pat[i++] = '.'; | |
10209 break; | |
10210 case '\\': | |
10211 if (p[1] == NUL) | |
10212 break; | |
10213 #ifdef BACKSLASH_IN_FILENAME | |
10214 if (!no_bslash) | |
10215 { | |
10216 /* translate: | |
10217 * "\x" to "\\x" e.g., "dir\file" | |
10218 * "\*" to "\\.*" e.g., "dir\*.c" | |
10219 * "\?" to "\\." e.g., "dir\??.c" | |
10220 * "\+" to "\+" e.g., "fileX\+.c" | |
10221 */ | |
10222 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?') | |
10223 && p[1] != '+') | |
10224 { | |
10225 reg_pat[i++] = '['; | |
10226 reg_pat[i++] = '\\'; | |
10227 reg_pat[i++] = '/'; | |
10228 reg_pat[i++] = ']'; | |
10229 if (allow_dirs != NULL) | |
10230 *allow_dirs = TRUE; | |
10231 break; | |
10232 } | |
10233 } | |
10234 #endif | |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10235 /* Undo escaping from ExpandEscape(): |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10236 * foo\?bar -> foo?bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10237 * foo\%bar -> foo%bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10238 * foo\,bar -> foo,bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10239 * foo\ bar -> foo bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10240 * 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
|
10241 * regexp. |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
5008
diff
changeset
|
10242 * 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
|
10243 * 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
|
10244 */ |
7 | 10245 if (*++p == '?' |
10246 #ifdef BACKSLASH_IN_FILENAME | |
10247 && no_bslash | |
10248 #endif | |
10249 ) | |
10250 reg_pat[i++] = '?'; | |
10251 else | |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
5008
diff
changeset
|
10252 if (*p == ',' || *p == '%' || *p == '#' |
6999 | 10253 || vim_isspace(*p) || *p == '{' || *p == '}') |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10254 reg_pat[i++] = *p; |
5259
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10255 else if (*p == '\\' && p[1] == '\\' && p[2] == '{') |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10256 { |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10257 reg_pat[i++] = '\\'; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10258 reg_pat[i++] = '{'; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10259 p += 2; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10260 } |
7 | 10261 else |
10262 { | |
10263 if (allow_dirs != NULL && vim_ispathsep(*p) | |
10264 #ifdef BACKSLASH_IN_FILENAME | |
10265 && (!no_bslash || *p != '\\') | |
10266 #endif | |
10267 ) | |
10268 *allow_dirs = TRUE; | |
10269 reg_pat[i++] = '\\'; | |
10270 reg_pat[i++] = *p; | |
10271 } | |
10272 break; | |
10273 #ifdef BACKSLASH_IN_FILENAME | |
10274 case '/': | |
10275 reg_pat[i++] = '['; | |
10276 reg_pat[i++] = '\\'; | |
10277 reg_pat[i++] = '/'; | |
10278 reg_pat[i++] = ']'; | |
10279 if (allow_dirs != NULL) | |
10280 *allow_dirs = TRUE; | |
10281 break; | |
10282 #endif | |
10283 case '{': | |
10284 reg_pat[i++] = '\\'; | |
10285 reg_pat[i++] = '('; | |
10286 nested++; | |
10287 break; | |
10288 case '}': | |
10289 reg_pat[i++] = '\\'; | |
10290 reg_pat[i++] = ')'; | |
10291 --nested; | |
10292 break; | |
10293 case ',': | |
10294 if (nested) | |
10295 { | |
10296 reg_pat[i++] = '\\'; | |
10297 reg_pat[i++] = '|'; | |
10298 } | |
10299 else | |
10300 reg_pat[i++] = ','; | |
10301 break; | |
10302 default: | |
10303 # ifdef FEAT_MBYTE | |
474 | 10304 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 10305 reg_pat[i++] = *p++; |
10306 else | |
10307 # endif | |
10308 if (allow_dirs != NULL && vim_ispathsep(*p)) | |
10309 *allow_dirs = TRUE; | |
10310 reg_pat[i++] = *p; | |
10311 break; | |
10312 } | |
10313 } | |
10314 if (add_dollar) | |
10315 reg_pat[i++] = '$'; | |
10316 reg_pat[i] = NUL; | |
10317 if (nested != 0) | |
10318 { | |
10319 if (nested < 0) | |
10320 EMSG(_("E219: Missing {.")); | |
10321 else | |
10322 EMSG(_("E220: Missing }.")); | |
10323 vim_free(reg_pat); | |
10324 reg_pat = NULL; | |
10325 } | |
10326 return reg_pat; | |
10327 } | |
2664 | 10328 |
10329 #if defined(EINTR) || defined(PROTO) | |
10330 /* | |
10331 * Version of read() that retries when interrupted by EINTR (possibly | |
10332 * by a SIGWINCH). | |
10333 */ | |
10334 long | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10335 read_eintr(int fd, void *buf, size_t bufsize) |
2664 | 10336 { |
10337 long ret; | |
10338 | |
10339 for (;;) | |
10340 { | |
10341 ret = vim_read(fd, buf, bufsize); | |
10342 if (ret >= 0 || errno != EINTR) | |
10343 break; | |
10344 } | |
10345 return ret; | |
10346 } | |
10347 | |
10348 /* | |
10349 * Version of write() that retries when interrupted by EINTR (possibly | |
10350 * by a SIGWINCH). | |
10351 */ | |
10352 long | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10353 write_eintr(int fd, void *buf, size_t bufsize) |
2664 | 10354 { |
10355 long ret = 0; | |
10356 long wlen; | |
10357 | |
10358 /* Repeat the write() so long it didn't fail, other than being interrupted | |
10359 * by a signal. */ | |
10360 while (ret < (long)bufsize) | |
10361 { | |
2666 | 10362 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret); |
2664 | 10363 if (wlen < 0) |
10364 { | |
10365 if (errno != EINTR) | |
10366 break; | |
10367 } | |
10368 else | |
10369 ret += wlen; | |
10370 } | |
10371 return ret; | |
10372 } | |
10373 #endif |