Mercurial > vim
annotate src/fileio.c @ 11537:e0eb2de60f09 v8.0.0651
patch 8.0.0651: build failure without the auto command feature
commit https://github.com/vim/vim/commit/157069b04e64eff72d2b34a2f727dfb6e2190cf3
Author: Bram Moolenaar <Bram@vim.org>
Date: Thu Jun 22 14:56:12 2017 +0200
patch 8.0.0651: build failure without the auto command feature
Problem: Build failure without the auto command feature.
Solution: Add #ifdef. (closes https://github.com/vim/vim/issues/1782)
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Thu, 22 Jun 2017 15:00:04 +0200 |
parents | 77f3b7316d8b |
children | 16ccaedce025 |
rev | line source |
---|---|
10042
4aead6a9b7a9
commit https://github.com/vim/vim/commit/edf3f97ae2af024708ebb4ac614227327033ca47
Christian Brabandt <cb@256bit.org>
parents:
9911
diff
changeset
|
1 /* vi:set ts=8 sts=4 sw=4 noet: |
7 | 2 * |
3 * VIM - Vi IMproved by Bram Moolenaar | |
4 * | |
5 * Do ":help uganda" in Vim to read copying and usage conditions. | |
6 * Do ":help credits" in Vim to see a list of people who contributed. | |
7 * See README.txt for an overview of the Vim source code. | |
8 */ | |
9 | |
10 /* | |
11 * fileio.c: read from and write to a file | |
12 */ | |
13 | |
14 #include "vim.h" | |
15 | |
2014 | 16 #if defined(__TANDEM) || defined(__MINT__) |
7 | 17 # include <limits.h> /* for SSIZE_MAX */ |
18 #endif | |
19 | |
20 #if defined(HAVE_UTIME) && defined(HAVE_UTIME_H) | |
21 # include <utime.h> /* for struct utimbuf */ | |
22 #endif | |
23 | |
24 #define BUFSIZE 8192 /* size of normal write buffer */ | |
25 #define SMBUFSIZE 256 /* size of emergency write buffer */ | |
26 | |
27 /* Is there any system that doesn't have access()? */ | |
574 | 28 #define USE_MCH_ACCESS |
7 | 29 |
30 #ifdef FEAT_MBYTE | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
31 static char_u *next_fenc(char_u **pp); |
7 | 32 # ifdef FEAT_EVAL |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
33 static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp); |
7 | 34 # endif |
35 #endif | |
36 #ifdef FEAT_VIMINFO | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
37 static void check_marks_read(void); |
7 | 38 #endif |
39 #ifdef FEAT_CRYPT | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
40 static char_u *check_for_cryptkey(char_u *cryptkey, char_u *ptr, long *sizep, off_T *filesizep, int newfile, char_u *fname, int *did_ask); |
7 | 41 #endif |
42 #ifdef UNIX | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
43 static void set_file_time(char_u *fname, time_t atime, time_t mtime); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
44 #endif |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
45 static int set_rw_fname(char_u *fname, char_u *sfname); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
46 static int msg_add_fileformat(int eol_type); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
47 static void msg_add_eol(void); |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
48 static int check_mtime(buf_T *buf, stat_T *s); |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
49 static int time_differs(long t1, long t2); |
7 | 50 #ifdef FEAT_AUTOCMD |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
51 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
|
52 static int au_find_group(char_u *name); |
676 | 53 |
54 # define AUGROUP_DEFAULT -1 /* default autocmd group */ | |
1834 | 55 # define AUGROUP_ERROR -2 /* erroneous autocmd group */ |
676 | 56 # define AUGROUP_ALL -3 /* all autocmd groups */ |
7 | 57 #endif |
58 | |
59 #if defined(FEAT_CRYPT) || defined(FEAT_MBYTE) | |
60 # define HAS_BW_FLAGS | |
61 # define FIO_LATIN1 0x01 /* convert Latin1 */ | |
62 # define FIO_UTF8 0x02 /* convert UTF-8 */ | |
63 # define FIO_UCS2 0x04 /* convert UCS-2 */ | |
64 # define FIO_UCS4 0x08 /* convert UCS-4 */ | |
65 # define FIO_UTF16 0x10 /* convert UTF-16 */ | |
66 # ifdef WIN3264 | |
67 # define FIO_CODEPAGE 0x20 /* convert MS-Windows codepage */ | |
68 # define FIO_PUT_CP(x) (((x) & 0xffff) << 16) /* put codepage in top word */ | |
69 # define FIO_GET_CP(x) (((x)>>16) & 0xffff) /* get codepage from top word */ | |
70 # endif | |
71 # ifdef MACOS_X | |
72 # define FIO_MACROMAN 0x20 /* convert MacRoman */ | |
73 # endif | |
74 # define FIO_ENDIAN_L 0x80 /* little endian */ | |
75 # define FIO_ENCRYPTED 0x1000 /* encrypt written bytes */ | |
76 # define FIO_NOCONVERT 0x2000 /* skip encoding conversion */ | |
77 # define FIO_UCSBOM 0x4000 /* check for BOM at start of file */ | |
78 # define FIO_ALL -1 /* allow all formats */ | |
79 #endif | |
80 | |
81 /* When converting, a read() or write() may leave some bytes to be converted | |
82 * for the next call. The value is guessed... */ | |
83 #define CONV_RESTLEN 30 | |
84 | |
85 /* We have to guess how much a sequence of bytes may expand when converting | |
86 * with iconv() to be able to allocate a buffer. */ | |
87 #define ICONV_MULT 8 | |
88 | |
89 /* | |
90 * Structure to pass arguments from buf_write() to buf_write_bytes(). | |
91 */ | |
92 struct bw_info | |
93 { | |
94 int bw_fd; /* file descriptor */ | |
95 char_u *bw_buf; /* buffer with data to be written */ | |
1411 | 96 int bw_len; /* length of data */ |
7 | 97 #ifdef HAS_BW_FLAGS |
98 int bw_flags; /* FIO_ flags */ | |
99 #endif | |
6122 | 100 #ifdef FEAT_CRYPT |
101 buf_T *bw_buffer; /* buffer being written */ | |
102 #endif | |
7 | 103 #ifdef FEAT_MBYTE |
104 char_u bw_rest[CONV_RESTLEN]; /* not converted bytes */ | |
105 int bw_restlen; /* nr of bytes in bw_rest[] */ | |
106 int bw_first; /* first write call */ | |
107 char_u *bw_conv_buf; /* buffer for writing converted chars */ | |
108 int bw_conv_buflen; /* size of bw_conv_buf */ | |
109 int bw_conv_error; /* set for conversion error */ | |
1947 | 110 linenr_T bw_conv_error_lnum; /* first line with error or zero */ |
111 linenr_T bw_start_lnum; /* line number at start of buffer */ | |
7 | 112 # ifdef USE_ICONV |
113 iconv_t bw_iconv_fd; /* descriptor for iconv() or -1 */ | |
114 # endif | |
115 #endif | |
116 }; | |
117 | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
118 static int buf_write_bytes(struct bw_info *ip); |
7 | 119 |
120 #ifdef FEAT_MBYTE | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
121 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
|
122 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
|
123 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
|
124 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
|
125 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
|
126 static int make_bom(char_u *buf, char_u *name); |
7 | 127 # ifdef WIN3264 |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
128 static int get_win_fio_flags(char_u *ptr); |
7 | 129 # endif |
130 # ifdef MACOS_X | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
131 static int get_mac_fio_flags(char_u *ptr); |
7 | 132 # endif |
133 #endif | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
134 static int move_lines(buf_T *frombuf, buf_T *tobuf); |
2006 | 135 #ifdef TEMPDIRNAMES |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
136 static void vim_settempdir(char_u *tempdir); |
2006 | 137 #endif |
1834 | 138 #ifdef FEAT_AUTOCMD |
139 static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name"); | |
140 #endif | |
595 | 141 |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
142 #ifdef FEAT_AUTOCMD |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
143 /* |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
144 * 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
|
145 * 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
|
146 * EVENT_BUFREADPOST triggered the EVENT_FILETYPE. |
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 * 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
|
149 * apply_autocmds_group. |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
150 */ |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
151 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
|
152 #endif |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
153 |
7 | 154 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
155 filemess( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
156 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
157 char_u *name, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
158 char_u *s, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
159 int attr) |
7 | 160 { |
161 int msg_scroll_save; | |
162 | |
163 if (msg_silent != 0) | |
164 return; | |
165 msg_add_fname(buf, name); /* put file name in IObuff with quotes */ | |
166 /* If it's extremely long, truncate it. */ | |
167 if (STRLEN(IObuff) > IOSIZE - 80) | |
168 IObuff[IOSIZE - 80] = NUL; | |
169 STRCAT(IObuff, s); | |
170 /* | |
171 * For the first message may have to start a new line. | |
172 * For further ones overwrite the previous one, reset msg_scroll before | |
173 * calling filemess(). | |
174 */ | |
175 msg_scroll_save = msg_scroll; | |
176 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) | |
177 msg_scroll = FALSE; | |
178 if (!msg_scroll) /* wait a bit when overwriting an error msg */ | |
179 check_for_delay(FALSE); | |
180 msg_start(); | |
181 msg_scroll = msg_scroll_save; | |
182 msg_scrolled_ign = TRUE; | |
183 /* may truncate the message to avoid a hit-return prompt */ | |
184 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr); | |
185 msg_clr_eos(); | |
186 out_flush(); | |
187 msg_scrolled_ign = FALSE; | |
188 } | |
189 | |
190 /* | |
191 * Read lines from file "fname" into the buffer after line "from". | |
192 * | |
193 * 1. We allocate blocks with lalloc, as big as possible. | |
194 * 2. Each block is filled with characters from the file with a single read(). | |
195 * 3. The lines are inserted in the buffer with ml_append(). | |
196 * | |
197 * (caller must check that fname != NULL, unless READ_STDIN is used) | |
198 * | |
199 * "lines_to_skip" is the number of lines that must be skipped | |
200 * "lines_to_read" is the number of lines that are appended | |
201 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM. | |
202 * | |
203 * flags: | |
204 * READ_NEW starting to edit a new buffer | |
205 * READ_FILTER reading filter output | |
206 * READ_STDIN read from stdin instead of a file | |
207 * READ_BUFFER read from curbuf instead of a file (converting after reading | |
208 * stdin) | |
209 * 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
|
210 * READ_KEEP_UNDO don't clear undo info or read it from a file |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
211 * READ_FIFO read from fifo/socket instead of a file |
7 | 212 * |
10575
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
213 * return FAIL for failure, NOTDONE for directory (failure), or OK |
7 | 214 */ |
215 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
216 readfile( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
217 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
218 char_u *sfname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
219 linenr_T from, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
220 linenr_T lines_to_skip, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
221 linenr_T lines_to_read, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
222 exarg_T *eap, /* can be NULL! */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
223 int flags) |
7 | 224 { |
225 int fd = 0; | |
226 int newfile = (flags & READ_NEW); | |
227 int check_readonly; | |
228 int filtering = (flags & READ_FILTER); | |
229 int read_stdin = (flags & READ_STDIN); | |
230 int read_buffer = (flags & READ_BUFFER); | |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
231 int read_fifo = (flags & READ_FIFO); |
1486 | 232 int set_options = newfile || read_buffer |
233 || (eap != NULL && eap->read_edit); | |
7 | 234 linenr_T read_buf_lnum = 1; /* next line to read from curbuf */ |
235 colnr_T read_buf_col = 0; /* next char to read from this line */ | |
236 char_u c; | |
237 linenr_T lnum = from; | |
238 char_u *ptr = NULL; /* pointer into read buffer */ | |
239 char_u *buffer = NULL; /* read buffer */ | |
240 char_u *new_buffer = NULL; /* init to shut up gcc */ | |
241 char_u *line_start = NULL; /* init to shut up gcc */ | |
242 int wasempty; /* buffer was empty before reading */ | |
243 colnr_T len; | |
244 long size = 0; | |
245 char_u *p; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
246 off_T filesize = 0; |
7 | 247 int skip_read = FALSE; |
248 #ifdef FEAT_CRYPT | |
249 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
|
250 int did_ask_for_key = FALSE; |
7 | 251 #endif |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
252 #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
|
253 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
|
254 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
|
255 #endif |
7 | 256 int split = 0; /* number of split lines */ |
257 #define UNKNOWN 0x0fffffff /* file size is unknown */ | |
258 linenr_T linecnt; | |
259 int error = FALSE; /* errors encountered */ | |
260 int ff_error = EOL_UNKNOWN; /* file format with errors */ | |
261 long linerest = 0; /* remaining chars in line */ | |
262 #ifdef UNIX | |
263 int perm = 0; | |
264 int swap_mode = -1; /* protection bits for swap file */ | |
265 #else | |
266 int perm; | |
267 #endif | |
268 int fileformat = 0; /* end-of-line format */ | |
269 int keep_fileformat = FALSE; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
270 stat_T st; |
7 | 271 int file_readonly; |
272 linenr_T skip_count = 0; | |
273 linenr_T read_count = 0; | |
274 int msg_save = msg_scroll; | |
275 linenr_T read_no_eol_lnum = 0; /* non-zero lnum when last line of | |
276 * last read was missing the eol */ | |
10668
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
277 int try_mac; |
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
278 int try_dos; |
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
279 int try_unix; |
7 | 280 int file_rewind = FALSE; |
281 #ifdef FEAT_MBYTE | |
282 int can_retry; | |
595 | 283 linenr_T conv_error = 0; /* line nr with conversion error */ |
284 linenr_T illegal_byte = 0; /* line nr with illegal byte */ | |
7 | 285 int keep_dest_enc = FALSE; /* don't retry when char doesn't fit |
286 in destination encoding */ | |
595 | 287 int bad_char_behavior = BAD_REPLACE; |
288 /* BAD_KEEP, BAD_DROP or character to | |
289 * replace with */ | |
7 | 290 char_u *tmpname = NULL; /* name of 'charconvert' output file */ |
291 int fio_flags = 0; | |
292 char_u *fenc; /* fileencoding to use */ | |
293 int fenc_alloced; /* fenc_next is in allocated memory */ | |
294 char_u *fenc_next = NULL; /* next item in 'fencs' or NULL */ | |
295 int advance_fenc = FALSE; | |
296 long real_size = 0; | |
297 # ifdef USE_ICONV | |
298 iconv_t iconv_fd = (iconv_t)-1; /* descriptor for iconv() or -1 */ | |
299 # ifdef FEAT_EVAL | |
300 int did_iconv = FALSE; /* TRUE when iconv() failed and trying | |
301 'charconvert' next */ | |
302 # endif | |
303 # endif | |
304 int converted = FALSE; /* TRUE if conversion done */ | |
305 int notconverted = FALSE; /* TRUE if conversion wanted but it | |
306 wasn't possible */ | |
307 char_u conv_rest[CONV_RESTLEN]; | |
308 int conv_restlen = 0; /* nr of bytes in conv_rest[] */ | |
309 #endif | |
1834 | 310 #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
|
311 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
|
312 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
|
313 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
|
314 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
|
315 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
|
316 #endif |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
317 |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
318 #ifdef FEAT_AUTOCMD |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
319 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
|
320 #endif |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
321 |
2707 | 322 curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */ |
7 | 323 |
324 /* | |
325 * If there is no file name yet, use the one for the read file. | |
326 * BF_NOTEDITED is set to reflect this. | |
327 * Don't do this for a read from a filter. | |
328 * Only do this when 'cpoptions' contains the 'f' flag. | |
329 */ | |
330 if (curbuf->b_ffname == NULL | |
331 && !filtering | |
332 && fname != NULL | |
333 && vim_strchr(p_cpo, CPO_FNAMER) != NULL | |
334 && !(flags & READ_DUMMY)) | |
335 { | |
633 | 336 if (set_rw_fname(fname, sfname) == FAIL) |
337 return FAIL; | |
7 | 338 } |
339 | |
2563
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
340 #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
|
341 /* 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
|
342 * 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
|
343 * 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
|
344 * 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
|
345 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
|
346 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
|
347 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
|
348 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
|
349 || (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
|
350 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
|
351 #endif |
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
352 |
167 | 353 /* After reading a file the cursor line changes but we don't want to |
354 * display the line. */ | |
355 ex_no_reprint = TRUE; | |
356 | |
968 | 357 /* don't display the file info for another buffer now */ |
358 need_fileinfo = FALSE; | |
359 | |
7 | 360 /* |
361 * For Unix: Use the short file name whenever possible. | |
362 * Avoids problems with networks and when directory names are changed. | |
363 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to | |
364 * another directory, which we don't detect. | |
365 */ | |
366 if (sfname == NULL) | |
367 sfname = fname; | |
9536
b2aada04d84e
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Christian Brabandt <cb@256bit.org>
parents:
9509
diff
changeset
|
368 #if defined(UNIX) |
7 | 369 fname = sfname; |
370 #endif | |
371 | |
372 #ifdef FEAT_AUTOCMD | |
373 /* | |
374 * The BufReadCmd and FileReadCmd events intercept the reading process by | |
375 * executing the associated commands instead. | |
376 */ | |
377 if (!filtering && !read_stdin && !read_buffer) | |
378 { | |
379 pos_T pos; | |
380 | |
381 pos = curbuf->b_op_start; | |
382 | |
383 /* Set '[ mark to the line above where the lines go (line 1 if zero). */ | |
384 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); | |
385 curbuf->b_op_start.col = 0; | |
386 | |
387 if (newfile) | |
388 { | |
389 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname, | |
390 FALSE, curbuf, eap)) | |
391 #ifdef FEAT_EVAL | |
392 return aborting() ? FAIL : OK; | |
393 #else | |
394 return OK; | |
395 #endif | |
396 } | |
397 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname, | |
398 FALSE, NULL, eap)) | |
399 #ifdef FEAT_EVAL | |
400 return aborting() ? FAIL : OK; | |
401 #else | |
402 return OK; | |
403 #endif | |
404 | |
405 curbuf->b_op_start = pos; | |
406 } | |
407 #endif | |
408 | |
409 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0) | |
410 msg_scroll = FALSE; /* overwrite previous file message */ | |
411 else | |
412 msg_scroll = TRUE; /* don't overwrite previous file message */ | |
413 | |
414 /* | |
415 * If the name ends in a path separator, we can't open it. Check here, | |
416 * because reading the file may actually work, but then creating the swap | |
417 * file may destroy it! Reported on MS-DOS and Win 95. | |
418 * If the name is too long we might crash further on, quit here. | |
419 */ | |
23 | 420 if (fname != NULL && *fname != NUL) |
421 { | |
39 | 422 p = fname + STRLEN(fname); |
423 if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL) | |
23 | 424 { |
425 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0); | |
426 msg_end(); | |
427 msg_scroll = msg_save; | |
428 return FAIL; | |
429 } | |
7 | 430 } |
431 | |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
432 if (!read_stdin && !read_buffer && !read_fifo) |
5322 | 433 { |
7 | 434 #ifdef UNIX |
5322 | 435 /* |
436 * On Unix it is possible to read a directory, so we have to | |
437 * check for it before the mch_open(). | |
438 */ | |
7 | 439 perm = mch_getperm(fname); |
440 if (perm >= 0 && !S_ISREG(perm) /* not a regular file ... */ | |
441 # ifdef S_ISFIFO | |
442 && !S_ISFIFO(perm) /* ... or fifo */ | |
443 # endif | |
444 # ifdef S_ISSOCK | |
445 && !S_ISSOCK(perm) /* ... or socket */ | |
446 # endif | |
1313 | 447 # ifdef OPEN_CHR_FILES |
448 && !(S_ISCHR(perm) && is_dev_fd_file(fname)) | |
449 /* ... or a character special file named /dev/fd/<n> */ | |
450 # endif | |
7 | 451 ) |
452 { | |
10575
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
453 int retval = FAIL; |
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
454 |
7 | 455 if (S_ISDIR(perm)) |
10575
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
456 { |
7 | 457 filemess(curbuf, fname, (char_u *)_("is a directory"), 0); |
10575
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
458 retval = NOTDONE; |
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
459 } |
7 | 460 else |
461 filemess(curbuf, fname, (char_u *)_("is not a file"), 0); | |
462 msg_end(); | |
463 msg_scroll = msg_save; | |
10575
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
464 return retval; |
7 | 465 } |
5322 | 466 #endif |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
467 #if defined(MSWIN) |
1006 | 468 /* |
469 * MS-Windows allows opening a device, but we will probably get stuck | |
470 * trying to read it. | |
471 */ | |
472 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE) | |
473 { | |
1303 | 474 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0); |
1006 | 475 msg_end(); |
476 msg_scroll = msg_save; | |
477 return FAIL; | |
478 } | |
5322 | 479 #endif |
480 } | |
1004 | 481 |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
482 /* Set default or forced 'fileformat' and 'binary'. */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
483 set_file_options(set_options, eap); |
7 | 484 |
485 /* | |
486 * When opening a new file we take the readonly flag from the file. | |
487 * Default is r/w, can be set to r/o below. | |
488 * Don't reset it when in readonly mode | |
489 * Only set/reset b_p_ro when BF_CHECK_RO is set. | |
490 */ | |
491 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO)); | |
164 | 492 if (check_readonly && !readonlymode) |
7 | 493 curbuf->b_p_ro = FALSE; |
494 | |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
495 if (newfile && !read_stdin && !read_buffer && !read_fifo) |
7 | 496 { |
2823 | 497 /* Remember time of file. */ |
7 | 498 if (mch_stat((char *)fname, &st) >= 0) |
499 { | |
500 buf_store_time(curbuf, &st, fname); | |
501 curbuf->b_mtime_read = curbuf->b_mtime; | |
502 #ifdef UNIX | |
503 /* | |
504 * Use the protection bits of the original file for the swap file. | |
505 * This makes it possible for others to read the name of the | |
506 * edited file from the swapfile, but only if they can read the | |
507 * edited file. | |
508 * Remove the "write" and "execute" bits for group and others | |
509 * (they must not write the swapfile). | |
510 * Add the "read" and "write" bits for the user, otherwise we may | |
511 * not be able to write to the file ourselves. | |
512 * Setting the bits is done below, after creating the swap file. | |
513 */ | |
514 swap_mode = (st.st_mode & 0644) | 0600; | |
515 #endif | |
516 #ifdef FEAT_CW_EDITOR | |
517 /* Get the FSSpec on MacOS | |
518 * TODO: Update it properly when the buffer name changes | |
519 */ | |
520 (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec); | |
521 #endif | |
522 #ifdef VMS | |
523 curbuf->b_fab_rfm = st.st_fab_rfm; | |
22 | 524 curbuf->b_fab_rat = st.st_fab_rat; |
525 curbuf->b_fab_mrs = st.st_fab_mrs; | |
7 | 526 #endif |
527 } | |
528 else | |
529 { | |
530 curbuf->b_mtime = 0; | |
531 curbuf->b_mtime_read = 0; | |
532 curbuf->b_orig_size = 0; | |
533 curbuf->b_orig_mode = 0; | |
534 } | |
535 | |
536 /* Reset the "new file" flag. It will be set again below when the | |
537 * file doesn't exist. */ | |
538 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W); | |
539 } | |
540 | |
541 /* | |
542 * 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
|
543 * for Amiga: check readonly by trying to open the file for writing |
7 | 544 */ |
545 file_readonly = FALSE; | |
546 if (read_stdin) | |
547 { | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
548 #if defined(MSWIN) |
7 | 549 /* Force binary I/O on stdin to avoid CR-LF -> LF conversion. */ |
550 setmode(0, O_BINARY); | |
551 #endif | |
552 } | |
553 else if (!read_buffer) | |
554 { | |
555 #ifdef USE_MCH_ACCESS | |
556 if ( | |
557 # ifdef UNIX | |
558 !(perm & 0222) || | |
559 # endif | |
560 mch_access((char *)fname, W_OK)) | |
561 file_readonly = TRUE; | |
562 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
563 #else | |
564 if (!newfile | |
565 || readonlymode | |
566 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0) | |
567 { | |
568 file_readonly = TRUE; | |
569 /* try to open ro */ | |
570 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
571 } | |
572 #endif | |
573 } | |
574 | |
575 if (fd < 0) /* cannot open at all */ | |
576 { | |
577 #ifndef UNIX | |
578 int isdir_f; | |
579 #endif | |
580 msg_scroll = msg_save; | |
581 #ifndef UNIX | |
582 /* | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
583 * On Amiga we can't open a directory, check here. |
7 | 584 */ |
585 isdir_f = (mch_isdir(fname)); | |
586 perm = mch_getperm(fname); /* check if the file exists */ | |
587 if (isdir_f) | |
588 { | |
589 filemess(curbuf, sfname, (char_u *)_("is a directory"), 0); | |
590 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
591 } | |
592 else | |
593 #endif | |
594 if (newfile) | |
595 { | |
2147
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
596 if (perm < 0 |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
597 #ifdef ENOENT |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
598 && errno == ENOENT |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
599 #endif |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
600 ) |
7 | 601 { |
602 /* | |
603 * Set the 'new-file' flag, so that when the file has | |
604 * been created by someone else, a ":w" will complain. | |
605 */ | |
606 curbuf->b_flags |= BF_NEW; | |
607 | |
608 /* Create a swap file now, so that other Vims are warned | |
609 * that we are editing this file. Don't do this for a | |
610 * "nofile" or "nowrite" buffer type. */ | |
611 #ifdef FEAT_QUICKFIX | |
612 if (!bt_dontwrite(curbuf)) | |
613 #endif | |
1834 | 614 { |
7 | 615 check_need_swap(newfile); |
1834 | 616 #ifdef FEAT_AUTOCMD |
617 /* SwapExists autocommand may mess things up */ | |
618 if (curbuf != old_curbuf | |
619 || (using_b_ffname | |
620 && (old_b_ffname != curbuf->b_ffname)) | |
621 || (using_b_fname | |
622 && (old_b_fname != curbuf->b_fname))) | |
623 { | |
624 EMSG(_(e_auchangedbuf)); | |
625 return FAIL; | |
626 } | |
627 #endif | |
628 } | |
592 | 629 if (dir_of_file_exists(fname)) |
630 filemess(curbuf, sfname, (char_u *)_("[New File]"), 0); | |
631 else | |
632 filemess(curbuf, sfname, | |
633 (char_u *)_("[New DIRECTORY]"), 0); | |
7 | 634 #ifdef FEAT_VIMINFO |
635 /* Even though this is a new file, it might have been | |
636 * edited before and deleted. Get the old marks. */ | |
637 check_marks_read(); | |
638 #endif | |
639 #ifdef FEAT_MBYTE | |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
640 /* Set forced 'fileencoding'. */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
641 if (eap != NULL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
642 set_forced_fenc(eap); |
7 | 643 #endif |
644 #ifdef FEAT_AUTOCMD | |
645 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname, | |
646 FALSE, curbuf, eap); | |
647 #endif | |
648 /* remember the current fileformat */ | |
649 save_file_ff(curbuf); | |
650 | |
651 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
652 if (aborting()) /* autocmds may abort script processing */ | |
653 return FAIL; | |
654 #endif | |
655 return OK; /* a new file is not an error */ | |
656 } | |
657 else | |
658 { | |
550 | 659 filemess(curbuf, sfname, (char_u *)( |
660 # ifdef EFBIG | |
661 (errno == EFBIG) ? _("[File too big]") : | |
662 # endif | |
2147
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
663 # ifdef EOVERFLOW |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
664 (errno == EOVERFLOW) ? _("[File too big]") : |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
665 # endif |
550 | 666 _("[Permission Denied]")), 0); |
7 | 667 curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
668 } | |
669 } | |
670 | |
671 return FAIL; | |
672 } | |
673 | |
674 /* | |
675 * Only set the 'ro' flag for readonly files the first time they are | |
676 * loaded. Help files always get readonly mode | |
677 */ | |
678 if ((check_readonly && file_readonly) || curbuf->b_help) | |
679 curbuf->b_p_ro = TRUE; | |
680 | |
819 | 681 if (set_options) |
7 | 682 { |
1486 | 683 /* Don't change 'eol' if reading from buffer as it will already be |
684 * correctly set when reading stdin. */ | |
685 if (!read_buffer) | |
686 { | |
687 curbuf->b_p_eol = TRUE; | |
688 curbuf->b_start_eol = TRUE; | |
689 } | |
7 | 690 #ifdef FEAT_MBYTE |
691 curbuf->b_p_bomb = FALSE; | |
1352 | 692 curbuf->b_start_bomb = FALSE; |
7 | 693 #endif |
694 } | |
695 | |
696 /* Create a swap file now, so that other Vims are warned that we are | |
697 * editing this file. | |
698 * Don't do this for a "nofile" or "nowrite" buffer type. */ | |
699 #ifdef FEAT_QUICKFIX | |
700 if (!bt_dontwrite(curbuf)) | |
701 #endif | |
702 { | |
703 check_need_swap(newfile); | |
1834 | 704 #ifdef FEAT_AUTOCMD |
705 if (!read_stdin && (curbuf != old_curbuf | |
706 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) | |
707 || (using_b_fname && (old_b_fname != curbuf->b_fname)))) | |
708 { | |
709 EMSG(_(e_auchangedbuf)); | |
710 if (!read_buffer) | |
711 close(fd); | |
712 return FAIL; | |
713 } | |
714 #endif | |
7 | 715 #ifdef UNIX |
716 /* Set swap file protection bits after creating it. */ | |
1918 | 717 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL |
718 && curbuf->b_ml.ml_mfp->mf_fname != NULL) | |
7 | 719 (void)mch_setperm(curbuf->b_ml.ml_mfp->mf_fname, (long)swap_mode); |
720 #endif | |
721 } | |
722 | |
578 | 723 #if defined(HAS_SWAP_EXISTS_ACTION) |
7 | 724 /* If "Quit" selected at ATTENTION dialog, don't load the file */ |
725 if (swap_exists_action == SEA_QUIT) | |
726 { | |
727 if (!read_buffer && !read_stdin) | |
728 close(fd); | |
729 return FAIL; | |
730 } | |
731 #endif | |
732 | |
733 ++no_wait_return; /* don't wait for return yet */ | |
734 | |
735 /* | |
736 * Set '[ mark to the line above where the lines go (line 1 if zero). | |
737 */ | |
738 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); | |
739 curbuf->b_op_start.col = 0; | |
740 | |
10668
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
741 try_mac = (vim_strchr(p_ffs, 'm') != NULL); |
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
742 try_dos = (vim_strchr(p_ffs, 'd') != NULL); |
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
743 try_unix = (vim_strchr(p_ffs, 'x') != NULL); |
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
744 |
7 | 745 #ifdef FEAT_AUTOCMD |
746 if (!read_buffer) | |
747 { | |
748 int m = msg_scroll; | |
749 int n = msg_scrolled; | |
750 | |
751 /* | |
752 * The file must be closed again, the autocommands may want to change | |
753 * the file before reading it. | |
754 */ | |
755 if (!read_stdin) | |
756 close(fd); /* ignore errors */ | |
757 | |
758 /* | |
759 * The output from the autocommands should not overwrite anything and | |
760 * should not be overwritten: Set msg_scroll, restore its value if no | |
761 * output was done. | |
762 */ | |
763 msg_scroll = TRUE; | |
764 if (filtering) | |
765 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname, | |
766 FALSE, curbuf, eap); | |
767 else if (read_stdin) | |
768 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname, | |
769 FALSE, curbuf, eap); | |
770 else if (newfile) | |
771 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname, | |
772 FALSE, curbuf, eap); | |
773 else | |
774 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname, | |
775 FALSE, NULL, eap); | |
10668
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
776 /* autocommands may have changed it */ |
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
777 try_mac = (vim_strchr(p_ffs, 'm') != NULL); |
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
778 try_dos = (vim_strchr(p_ffs, 'd') != NULL); |
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
779 try_unix = (vim_strchr(p_ffs, 'x') != NULL); |
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
780 |
7 | 781 if (msg_scrolled == n) |
782 msg_scroll = m; | |
783 | |
784 #ifdef FEAT_EVAL | |
785 if (aborting()) /* autocmds may abort script processing */ | |
786 { | |
787 --no_wait_return; | |
788 msg_scroll = msg_save; | |
789 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
790 return FAIL; | |
791 } | |
792 #endif | |
793 /* | |
794 * Don't allow the autocommands to change the current buffer. | |
795 * Try to re-open the file. | |
1834 | 796 * |
797 * Don't allow the autocommands to change the buffer name either | |
798 * (cd for example) if it invalidates fname or sfname. | |
7 | 799 */ |
800 if (!read_stdin && (curbuf != old_curbuf | |
1834 | 801 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) |
802 || (using_b_fname && (old_b_fname != curbuf->b_fname)) | |
7 | 803 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0)) |
804 { | |
805 --no_wait_return; | |
806 msg_scroll = msg_save; | |
807 if (fd < 0) | |
808 EMSG(_("E200: *ReadPre autocommands made the file unreadable")); | |
809 else | |
810 EMSG(_("E201: *ReadPre autocommands must not change current buffer")); | |
811 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
812 return FAIL; | |
813 } | |
814 } | |
815 #endif /* FEAT_AUTOCMD */ | |
816 | |
817 /* Autocommands may add lines to the file, need to check if it is empty */ | |
818 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY); | |
819 | |
820 if (!recoverymode && !filtering && !(flags & READ_DUMMY)) | |
821 { | |
822 /* | |
823 * Show the user that we are busy reading the input. Sometimes this | |
824 * may take a while. When reading from stdin another program may | |
825 * still be running, don't move the cursor to the last line, unless | |
826 * always using the GUI. | |
827 */ | |
828 if (read_stdin) | |
829 { | |
830 #ifndef ALWAYS_USE_GUI | |
831 mch_msg(_("Vim: Reading from stdin...\n")); | |
832 #endif | |
833 #ifdef FEAT_GUI | |
834 /* Also write a message in the GUI window, if there is one. */ | |
835 if (gui.in_use && !gui.dying && !gui.starting) | |
836 { | |
837 p = (char_u *)_("Reading from stdin..."); | |
838 gui_write(p, (int)STRLEN(p)); | |
839 } | |
840 #endif | |
841 } | |
842 else if (!read_buffer) | |
843 filemess(curbuf, sfname, (char_u *)"", 0); | |
844 } | |
845 | |
846 msg_scroll = FALSE; /* overwrite the file message */ | |
847 | |
848 /* | |
849 * Set linecnt now, before the "retry" caused by a wrong guess for | |
850 * fileformat, and after the autocommands, which may change them. | |
851 */ | |
852 linecnt = curbuf->b_ml.ml_line_count; | |
853 | |
854 #ifdef FEAT_MBYTE | |
595 | 855 /* "++bad=" argument. */ |
856 if (eap != NULL && eap->bad_char != 0) | |
612 | 857 { |
595 | 858 bad_char_behavior = eap->bad_char; |
819 | 859 if (set_options) |
612 | 860 curbuf->b_bad_char = eap->bad_char; |
861 } | |
862 else | |
863 curbuf->b_bad_char = 0; | |
595 | 864 |
7 | 865 /* |
595 | 866 * Decide which 'encoding' to use or use first. |
7 | 867 */ |
868 if (eap != NULL && eap->force_enc != 0) | |
869 { | |
870 fenc = enc_canonize(eap->cmd + eap->force_enc); | |
871 fenc_alloced = TRUE; | |
595 | 872 keep_dest_enc = TRUE; |
7 | 873 } |
874 else if (curbuf->b_p_bin) | |
875 { | |
876 fenc = (char_u *)""; /* binary: don't convert */ | |
877 fenc_alloced = FALSE; | |
878 } | |
879 else if (curbuf->b_help) | |
880 { | |
881 char_u firstline[80]; | |
301 | 882 int fc; |
7 | 883 |
884 /* Help files are either utf-8 or latin1. Try utf-8 first, if this | |
885 * fails it must be latin1. | |
886 * Always do this when 'encoding' is "utf-8". Otherwise only do | |
887 * this when needed to avoid [converted] remarks all the time. | |
888 * It is needed when the first line contains non-ASCII characters. | |
889 * That is only in *.??x files. */ | |
890 fenc = (char_u *)"latin1"; | |
891 c = enc_utf8; | |
301 | 892 if (!c && !read_stdin) |
893 { | |
894 fc = fname[STRLEN(fname) - 1]; | |
895 if (TOLOWER_ASC(fc) == 'x') | |
896 { | |
897 /* Read the first line (and a bit more). Immediately rewind to | |
898 * the start of the file. If the read() fails "len" is -1. */ | |
2664 | 899 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
|
900 vim_lseek(fd, (off_T)0L, SEEK_SET); |
301 | 901 for (p = firstline; p < firstline + len; ++p) |
902 if (*p >= 0x80) | |
903 { | |
904 c = TRUE; | |
905 break; | |
906 } | |
907 } | |
7 | 908 } |
909 | |
910 if (c) | |
911 { | |
912 fenc_next = fenc; | |
913 fenc = (char_u *)"utf-8"; | |
914 | |
915 /* When the file is utf-8 but a character doesn't fit in | |
916 * 'encoding' don't retry. In help text editing utf-8 bytes | |
917 * doesn't make sense. */ | |
844 | 918 if (!enc_utf8) |
919 keep_dest_enc = TRUE; | |
7 | 920 } |
921 fenc_alloced = FALSE; | |
922 } | |
923 else if (*p_fencs == NUL) | |
924 { | |
925 fenc = curbuf->b_p_fenc; /* use format from buffer */ | |
926 fenc_alloced = FALSE; | |
927 } | |
928 else | |
929 { | |
930 fenc_next = p_fencs; /* try items in 'fileencodings' */ | |
931 fenc = next_fenc(&fenc_next); | |
932 fenc_alloced = TRUE; | |
933 } | |
934 #endif | |
935 | |
936 /* | |
937 * Jump back here to retry reading the file in different ways. | |
938 * Reasons to retry: | |
939 * - encoding conversion failed: try another one from "fenc_next" | |
940 * - BOM detected and fenc was set, need to setup conversion | |
941 * - "fileformat" check failed: try another | |
942 * | |
943 * Variables set for special retry actions: | |
944 * "file_rewind" Rewind the file to start reading it again. | |
945 * "advance_fenc" Advance "fenc" using "fenc_next". | |
946 * "skip_read" Re-use already read bytes (BOM detected). | |
947 * "did_iconv" iconv() conversion failed, try 'charconvert'. | |
948 * "keep_fileformat" Don't reset "fileformat". | |
949 * | |
950 * Other status indicators: | |
951 * "tmpname" When != NULL did conversion with 'charconvert'. | |
952 * Output file has to be deleted afterwards. | |
953 * "iconv_fd" When != -1 did conversion with iconv(). | |
954 */ | |
955 retry: | |
956 | |
957 if (file_rewind) | |
958 { | |
959 if (read_buffer) | |
960 { | |
961 read_buf_lnum = 1; | |
962 read_buf_col = 0; | |
963 } | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
964 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0) |
7 | 965 { |
966 /* Can't rewind the file, give up. */ | |
967 error = TRUE; | |
968 goto failed; | |
969 } | |
970 /* Delete the previously read lines. */ | |
971 while (lnum > from) | |
972 ml_delete(lnum--, FALSE); | |
973 file_rewind = FALSE; | |
974 #ifdef FEAT_MBYTE | |
819 | 975 if (set_options) |
1352 | 976 { |
7 | 977 curbuf->b_p_bomb = FALSE; |
1352 | 978 curbuf->b_start_bomb = FALSE; |
979 } | |
595 | 980 conv_error = 0; |
7 | 981 #endif |
982 } | |
983 | |
984 /* | |
985 * When retrying with another "fenc" and the first time "fileformat" | |
986 * will be reset. | |
987 */ | |
988 if (keep_fileformat) | |
989 keep_fileformat = FALSE; | |
990 else | |
991 { | |
992 if (eap != NULL && eap->force_ff != 0) | |
1742 | 993 { |
7 | 994 fileformat = get_fileformat_force(curbuf, eap); |
1742 | 995 try_unix = try_dos = try_mac = FALSE; |
996 } | |
7 | 997 else if (curbuf->b_p_bin) |
998 fileformat = EOL_UNIX; /* binary: use Unix format */ | |
999 else if (*p_ffs == NUL) | |
1000 fileformat = get_fileformat(curbuf);/* use format from buffer */ | |
1001 else | |
1002 fileformat = EOL_UNKNOWN; /* detect from file */ | |
1003 } | |
1004 | |
1005 #ifdef FEAT_MBYTE | |
1006 # ifdef USE_ICONV | |
1007 if (iconv_fd != (iconv_t)-1) | |
1008 { | |
1009 /* aborted conversion with iconv(), close the descriptor */ | |
1010 iconv_close(iconv_fd); | |
1011 iconv_fd = (iconv_t)-1; | |
1012 } | |
1013 # endif | |
1014 | |
1015 if (advance_fenc) | |
1016 { | |
1017 /* | |
1018 * Try the next entry in 'fileencodings'. | |
1019 */ | |
1020 advance_fenc = FALSE; | |
1021 | |
1022 if (eap != NULL && eap->force_enc != 0) | |
1023 { | |
1024 /* Conversion given with "++cc=" wasn't possible, read | |
1025 * without conversion. */ | |
1026 notconverted = TRUE; | |
595 | 1027 conv_error = 0; |
7 | 1028 if (fenc_alloced) |
1029 vim_free(fenc); | |
1030 fenc = (char_u *)""; | |
1031 fenc_alloced = FALSE; | |
1032 } | |
1033 else | |
1034 { | |
1035 if (fenc_alloced) | |
1036 vim_free(fenc); | |
1037 if (fenc_next != NULL) | |
1038 { | |
1039 fenc = next_fenc(&fenc_next); | |
1040 fenc_alloced = (fenc_next != NULL); | |
1041 } | |
1042 else | |
1043 { | |
1044 fenc = (char_u *)""; | |
1045 fenc_alloced = FALSE; | |
1046 } | |
1047 } | |
1048 if (tmpname != NULL) | |
1049 { | |
1050 mch_remove(tmpname); /* delete converted file */ | |
1051 vim_free(tmpname); | |
1052 tmpname = NULL; | |
1053 } | |
1054 } | |
1055 | |
1056 /* | |
1948 | 1057 * Conversion may be required when the encoding of the file is different |
1058 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4. | |
7 | 1059 */ |
1060 fio_flags = 0; | |
1948 | 1061 converted = need_conversion(fenc); |
1062 if (converted) | |
7 | 1063 { |
1064 | |
1065 /* "ucs-bom" means we need to check the first bytes of the file | |
1066 * for a BOM. */ | |
1067 if (STRCMP(fenc, ENC_UCSBOM) == 0) | |
1068 fio_flags = FIO_UCSBOM; | |
1069 | |
1070 /* | |
1071 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be | |
1072 * done. This is handled below after read(). Prepare the | |
1073 * fio_flags to avoid having to parse the string each time. | |
1074 * Also check for Unicode to Latin1 conversion, because iconv() | |
1075 * appears not to handle this correctly. This works just like | |
1076 * conversion to UTF-8 except how the resulting character is put in | |
1077 * the buffer. | |
1078 */ | |
1079 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
1080 fio_flags = get_fio_flags(fenc); | |
1081 | |
1082 # ifdef WIN3264 | |
1083 /* | |
1084 * Conversion from an MS-Windows codepage to UTF-8 or another codepage | |
1085 * is handled with MultiByteToWideChar(). | |
1086 */ | |
1087 if (fio_flags == 0) | |
1088 fio_flags = get_win_fio_flags(fenc); | |
1089 # endif | |
1090 | |
1091 # ifdef MACOS_X | |
1092 /* Conversion from Apple MacRoman to latin1 or UTF-8 */ | |
1093 if (fio_flags == 0) | |
1094 fio_flags = get_mac_fio_flags(fenc); | |
1095 # endif | |
1096 | |
1097 # ifdef USE_ICONV | |
1098 /* | |
1099 * Try using iconv() if we can't convert internally. | |
1100 */ | |
1101 if (fio_flags == 0 | |
1102 # ifdef FEAT_EVAL | |
1103 && !did_iconv | |
1104 # endif | |
1105 ) | |
1106 iconv_fd = (iconv_t)my_iconv_open( | |
1107 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc); | |
1108 # endif | |
1109 | |
1110 # ifdef FEAT_EVAL | |
1111 /* | |
1112 * Use the 'charconvert' expression when conversion is required | |
1113 * and we can't do it internally or with iconv(). | |
1114 */ | |
1115 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL | |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
1116 && !read_fifo |
7 | 1117 # ifdef USE_ICONV |
1118 && iconv_fd == (iconv_t)-1 | |
1119 # endif | |
1120 ) | |
1121 { | |
1122 # ifdef USE_ICONV | |
1123 did_iconv = FALSE; | |
1124 # endif | |
1125 /* Skip conversion when it's already done (retry for wrong | |
1126 * "fileformat"). */ | |
1127 if (tmpname == NULL) | |
1128 { | |
1129 tmpname = readfile_charconvert(fname, fenc, &fd); | |
1130 if (tmpname == NULL) | |
1131 { | |
1132 /* Conversion failed. Try another one. */ | |
1133 advance_fenc = TRUE; | |
1134 if (fd < 0) | |
1135 { | |
1136 /* Re-opening the original file failed! */ | |
1137 EMSG(_("E202: Conversion made file unreadable!")); | |
1138 error = TRUE; | |
1139 goto failed; | |
1140 } | |
1141 goto retry; | |
1142 } | |
1143 } | |
1144 } | |
1145 else | |
1146 # endif | |
1147 { | |
1148 if (fio_flags == 0 | |
1149 # ifdef USE_ICONV | |
1150 && iconv_fd == (iconv_t)-1 | |
1151 # endif | |
1152 ) | |
1153 { | |
1154 /* Conversion wanted but we can't. | |
1155 * Try the next conversion in 'fileencodings' */ | |
1156 advance_fenc = TRUE; | |
1157 goto retry; | |
1158 } | |
1159 } | |
1160 } | |
1161 | |
595 | 1162 /* Set "can_retry" when it's possible to rewind the file and try with |
7 | 1163 * another "fenc" value. It's FALSE when no other "fenc" to try, reading |
595 | 1164 * stdin or fixed at a specific encoding. */ |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
1165 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc); |
7 | 1166 #endif |
1167 | |
1168 if (!skip_read) | |
1169 { | |
1170 linerest = 0; | |
1171 filesize = 0; | |
1172 skip_count = lines_to_skip; | |
1173 read_count = lines_to_read; | |
1174 #ifdef FEAT_MBYTE | |
1175 conv_restlen = 0; | |
1176 #endif | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1177 #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
|
1178 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
|
1179 && 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
|
1180 && 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
|
1181 && !filtering |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
1182 && !read_fifo |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1183 && !read_stdin |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1184 && !read_buffer); |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1185 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
|
1186 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
|
1187 #endif |
6122 | 1188 #ifdef FEAT_CRYPT |
1189 if (curbuf->b_cryptstate != NULL) | |
1190 { | |
1191 /* Need to free the state, but keep the key, don't want to ask for | |
1192 * it again. */ | |
1193 crypt_free_state(curbuf->b_cryptstate); | |
1194 curbuf->b_cryptstate = NULL; | |
1195 } | |
1196 #endif | |
7 | 1197 } |
1198 | |
1199 while (!error && !got_int) | |
1200 { | |
1201 /* | |
1202 * We allocate as much space for the file as we can get, plus | |
1203 * space for the old line plus room for one terminating NUL. | |
1204 * The amount is limited by the fact that read() only can read | |
1205 * upto max_unsigned characters (and other things). | |
1206 */ | |
5684 | 1207 #if VIM_SIZEOF_INT <= 2 |
7 | 1208 if (linerest >= 0x7ff0) |
1209 { | |
1210 ++split; | |
1211 *ptr = NL; /* split line by inserting a NL */ | |
1212 size = 1; | |
1213 } | |
1214 else | |
1215 #endif | |
1216 { | |
1217 if (!skip_read) | |
1218 { | |
5684 | 1219 #if VIM_SIZEOF_INT > 2 |
1076 | 1220 # if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L) |
7 | 1221 size = SSIZE_MAX; /* use max I/O size, 52K */ |
1222 # else | |
1223 size = 0x10000L; /* use buffer >= 64K */ | |
1224 # endif | |
1225 #else | |
1226 size = 0x7ff0L - linerest; /* limit buffer to 32K */ | |
1227 #endif | |
1228 | |
836 | 1229 for ( ; size >= 10; size = (long)((long_u)size >> 1)) |
7 | 1230 { |
1231 if ((new_buffer = lalloc((long_u)(size + linerest + 1), | |
1232 FALSE)) != NULL) | |
1233 break; | |
1234 } | |
1235 if (new_buffer == NULL) | |
1236 { | |
1237 do_outofmem_msg((long_u)(size * 2 + linerest + 1)); | |
1238 error = TRUE; | |
1239 break; | |
1240 } | |
1241 if (linerest) /* copy characters from the previous buffer */ | |
1242 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest); | |
1243 vim_free(buffer); | |
1244 buffer = new_buffer; | |
1245 ptr = buffer + linerest; | |
1246 line_start = buffer; | |
1247 | |
1248 #ifdef FEAT_MBYTE | |
1249 /* May need room to translate into. | |
1250 * For iconv() we don't really know the required space, use a | |
1251 * factor ICONV_MULT. | |
1252 * latin1 to utf-8: 1 byte becomes up to 2 bytes | |
1253 * utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes | |
1254 * become up to 4 bytes, size must be multiple of 2 | |
1255 * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be | |
1256 * multiple of 2 | |
1257 * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be | |
1258 * multiple of 4 */ | |
835 | 1259 real_size = (int)size; |
7 | 1260 # ifdef USE_ICONV |
1261 if (iconv_fd != (iconv_t)-1) | |
1262 size = size / ICONV_MULT; | |
1263 else | |
1264 # endif | |
1265 if (fio_flags & FIO_LATIN1) | |
1266 size = size / 2; | |
1267 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1268 size = (size * 2 / 3) & ~1; | |
1269 else if (fio_flags & FIO_UCS4) | |
1270 size = (size * 2 / 3) & ~3; | |
1271 else if (fio_flags == FIO_UCSBOM) | |
1272 size = size / ICONV_MULT; /* worst case */ | |
1273 # ifdef WIN3264 | |
1274 else if (fio_flags & FIO_CODEPAGE) | |
1275 size = size / ICONV_MULT; /* also worst case */ | |
1276 # endif | |
1277 # ifdef MACOS_X | |
1278 else if (fio_flags & FIO_MACROMAN) | |
1279 size = size / ICONV_MULT; /* also worst case */ | |
1280 # endif | |
1281 #endif | |
1282 | |
1283 #ifdef FEAT_MBYTE | |
1284 if (conv_restlen > 0) | |
1285 { | |
1286 /* Insert unconverted bytes from previous line. */ | |
1287 mch_memmove(ptr, conv_rest, conv_restlen); | |
1288 ptr += conv_restlen; | |
1289 size -= conv_restlen; | |
1290 } | |
1291 #endif | |
1292 | |
1293 if (read_buffer) | |
1294 { | |
1295 /* | |
1296 * Read bytes from curbuf. Used for converting text read | |
1297 * from stdin. | |
1298 */ | |
1299 if (read_buf_lnum > from) | |
1300 size = 0; | |
1301 else | |
1302 { | |
1303 int n, ni; | |
1304 long tlen; | |
1305 | |
1306 tlen = 0; | |
1307 for (;;) | |
1308 { | |
1309 p = ml_get(read_buf_lnum) + read_buf_col; | |
1310 n = (int)STRLEN(p); | |
1311 if ((int)tlen + n + 1 > size) | |
1312 { | |
1313 /* Filled up to "size", append partial line. | |
1314 * Change NL to NUL to reverse the effect done | |
1315 * below. */ | |
835 | 1316 n = (int)(size - tlen); |
7 | 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 read_buf_col += n; | |
1325 break; | |
1326 } | |
1327 else | |
1328 { | |
1329 /* Append whole line and new-line. Change NL | |
1330 * to NUL to reverse the effect done below. */ | |
1331 for (ni = 0; ni < n; ++ni) | |
1332 { | |
1333 if (p[ni] == NL) | |
1334 ptr[tlen++] = NUL; | |
1335 else | |
1336 ptr[tlen++] = p[ni]; | |
1337 } | |
1338 ptr[tlen++] = NL; | |
1339 read_buf_col = 0; | |
1340 if (++read_buf_lnum > from) | |
1341 { | |
1342 /* When the last line didn't have an | |
1343 * end-of-line don't add it now either. */ | |
1344 if (!curbuf->b_p_eol) | |
1345 --tlen; | |
1346 size = tlen; | |
1347 break; | |
1348 } | |
1349 } | |
1350 } | |
1351 } | |
1352 } | |
1353 else | |
1354 { | |
1355 /* | |
1356 * Read bytes from the file. | |
1357 */ | |
2664 | 1358 size = read_eintr(fd, ptr, size); |
7 | 1359 } |
1360 | |
6122 | 1361 #ifdef FEAT_CRYPT |
1362 /* | |
1363 * At start of file: Check for magic number of encryption. | |
1364 */ | |
1365 if (filesize == 0 && size > 0) | |
1366 cryptkey = check_for_cryptkey(cryptkey, ptr, &size, | |
1367 &filesize, newfile, sfname, | |
1368 &did_ask_for_key); | |
1369 /* | |
1370 * Decrypt the read bytes. This is done before checking for | |
1371 * EOF because the crypt layer may be buffering. | |
1372 */ | |
1373 if (cryptkey != NULL && size > 0) | |
1374 { | |
1375 if (crypt_works_inplace(curbuf->b_cryptstate)) | |
1376 { | |
1377 crypt_decode_inplace(curbuf->b_cryptstate, ptr, size); | |
1378 } | |
1379 else | |
1380 { | |
1381 char_u *newptr = NULL; | |
1382 int decrypted_size; | |
1383 | |
1384 decrypted_size = crypt_decode_alloc( | |
1385 curbuf->b_cryptstate, ptr, size, &newptr); | |
1386 | |
1387 /* If the crypt layer is buffering, not producing | |
1388 * anything yet, need to read more. */ | |
1389 if (size > 0 && decrypted_size == 0) | |
1390 continue; | |
1391 | |
1392 if (linerest == 0) | |
1393 { | |
1394 /* Simple case: reuse returned buffer (may be | |
1395 * NULL, checked later). */ | |
1396 new_buffer = newptr; | |
1397 } | |
1398 else | |
1399 { | |
1400 long_u new_size; | |
1401 | |
1402 /* Need new buffer to add bytes carried over. */ | |
1403 new_size = (long_u)(decrypted_size + linerest + 1); | |
1404 new_buffer = lalloc(new_size, FALSE); | |
1405 if (new_buffer == NULL) | |
1406 { | |
1407 do_outofmem_msg(new_size); | |
1408 error = TRUE; | |
1409 break; | |
1410 } | |
1411 | |
1412 mch_memmove(new_buffer, buffer, linerest); | |
1413 if (newptr != NULL) | |
1414 mch_memmove(new_buffer + linerest, newptr, | |
1415 decrypted_size); | |
1416 } | |
1417 | |
1418 if (new_buffer != NULL) | |
1419 { | |
1420 vim_free(buffer); | |
1421 buffer = new_buffer; | |
1422 new_buffer = NULL; | |
1423 line_start = buffer; | |
1424 ptr = buffer + linerest; | |
1425 } | |
1426 size = decrypted_size; | |
1427 } | |
1428 } | |
1429 #endif | |
1430 | |
7 | 1431 if (size <= 0) |
1432 { | |
1433 if (size < 0) /* read error */ | |
1434 error = TRUE; | |
1435 #ifdef FEAT_MBYTE | |
1436 else if (conv_restlen > 0) | |
595 | 1437 { |
1597 | 1438 /* |
1439 * Reached end-of-file but some trailing bytes could | |
1440 * not be converted. Truncated file? | |
1441 */ | |
1442 | |
1443 /* When we did a conversion report an error. */ | |
1444 if (fio_flags != 0 | |
1445 # ifdef USE_ICONV | |
1446 || iconv_fd != (iconv_t)-1 | |
1447 # endif | |
1448 ) | |
1449 { | |
4331 | 1450 if (can_retry) |
1451 goto rewind_retry; | |
1597 | 1452 if (conv_error == 0) |
1453 conv_error = curbuf->b_ml.ml_line_count | |
1454 - linecnt + 1; | |
1455 } | |
1456 /* Remember the first linenr with an illegal byte */ | |
1457 else if (illegal_byte == 0) | |
1458 illegal_byte = curbuf->b_ml.ml_line_count | |
1459 - linecnt + 1; | |
1460 if (bad_char_behavior == BAD_DROP) | |
595 | 1461 { |
1597 | 1462 *(ptr - conv_restlen) = NUL; |
1463 conv_restlen = 0; | |
1464 } | |
1465 else | |
1466 { | |
1467 /* Replace the trailing bytes with the replacement | |
1468 * character if we were converting; if we weren't, | |
1469 * leave the UTF8 checking code to do it, as it | |
1470 * works slightly differently. */ | |
1471 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0 | |
1472 # ifdef USE_ICONV | |
1473 || iconv_fd != (iconv_t)-1 | |
1474 # endif | |
1475 )) | |
1476 { | |
1477 while (conv_restlen > 0) | |
1478 { | |
1479 *(--ptr) = bad_char_behavior; | |
1480 --conv_restlen; | |
1481 } | |
1482 } | |
595 | 1483 fio_flags = 0; /* don't convert this */ |
834 | 1484 # ifdef USE_ICONV |
1485 if (iconv_fd != (iconv_t)-1) | |
1486 { | |
1487 iconv_close(iconv_fd); | |
1488 iconv_fd = (iconv_t)-1; | |
1489 } | |
1490 # endif | |
595 | 1491 } |
1492 } | |
7 | 1493 #endif |
1494 } | |
1495 } | |
1496 skip_read = FALSE; | |
1497 | |
1498 #ifdef FEAT_MBYTE | |
1499 /* | |
1500 * At start of file (or after crypt magic number): Check for BOM. | |
1501 * Also check for a BOM for other Unicode encodings, but not after | |
1502 * converting with 'charconvert' or when a BOM has already been | |
1503 * found. | |
1504 */ | |
1505 if ((filesize == 0 | |
1506 # ifdef FEAT_CRYPT | |
6122 | 1507 || (cryptkey != NULL |
1508 && filesize == crypt_get_header_len( | |
1509 crypt_get_method_nr(curbuf))) | |
7 | 1510 # endif |
1511 ) | |
1512 && (fio_flags == FIO_UCSBOM | |
1513 || (!curbuf->b_p_bomb | |
1514 && tmpname == NULL | |
1515 && (*fenc == 'u' || (*fenc == NUL && enc_utf8))))) | |
1516 { | |
1517 char_u *ccname; | |
1518 int blen; | |
1519 | |
1520 /* no BOM detection in a short file or in binary mode */ | |
1521 if (size < 2 || curbuf->b_p_bin) | |
1522 ccname = NULL; | |
1523 else | |
1524 ccname = check_for_bom(ptr, size, &blen, | |
1525 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc)); | |
1526 if (ccname != NULL) | |
1527 { | |
1528 /* Remove BOM from the text */ | |
1529 filesize += blen; | |
1530 size -= blen; | |
1531 mch_memmove(ptr, ptr + blen, (size_t)size); | |
819 | 1532 if (set_options) |
1352 | 1533 { |
7 | 1534 curbuf->b_p_bomb = TRUE; |
1352 | 1535 curbuf->b_start_bomb = TRUE; |
1536 } | |
7 | 1537 } |
1538 | |
1539 if (fio_flags == FIO_UCSBOM) | |
1540 { | |
1541 if (ccname == NULL) | |
1542 { | |
1543 /* No BOM detected: retry with next encoding. */ | |
1544 advance_fenc = TRUE; | |
1545 } | |
1546 else | |
1547 { | |
1548 /* BOM detected: set "fenc" and jump back */ | |
1549 if (fenc_alloced) | |
1550 vim_free(fenc); | |
1551 fenc = ccname; | |
1552 fenc_alloced = FALSE; | |
1553 } | |
1554 /* retry reading without getting new bytes or rewinding */ | |
1555 skip_read = TRUE; | |
1556 goto retry; | |
1557 } | |
1558 } | |
1597 | 1559 |
1560 /* Include not converted bytes. */ | |
1561 ptr -= conv_restlen; | |
1562 size += conv_restlen; | |
1563 conv_restlen = 0; | |
7 | 1564 #endif |
1565 /* | |
1566 * Break here for a read error or end-of-file. | |
1567 */ | |
1568 if (size <= 0) | |
1569 break; | |
1570 | |
1571 #ifdef FEAT_MBYTE | |
1572 | |
1573 # ifdef USE_ICONV | |
1574 if (iconv_fd != (iconv_t)-1) | |
1575 { | |
1576 /* | |
1577 * Attempt conversion of the read bytes to 'encoding' using | |
1578 * iconv(). | |
1579 */ | |
1580 const char *fromp; | |
1581 char *top; | |
1582 size_t from_size; | |
1583 size_t to_size; | |
1584 | |
1585 fromp = (char *)ptr; | |
1586 from_size = size; | |
1587 ptr += size; | |
1588 top = (char *)ptr; | |
1589 to_size = real_size - size; | |
1590 | |
1591 /* | |
1592 * If there is conversion error or not enough room try using | |
179 | 1593 * another conversion. Except for when there is no |
1594 * alternative (help files). | |
7 | 1595 */ |
177 | 1596 while ((iconv(iconv_fd, (void *)&fromp, &from_size, |
1597 &top, &to_size) | |
7 | 1598 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) |
1599 || from_size > CONV_RESTLEN) | |
177 | 1600 { |
595 | 1601 if (can_retry) |
375 | 1602 goto rewind_retry; |
595 | 1603 if (conv_error == 0) |
1604 conv_error = readfile_linenr(linecnt, | |
1605 ptr, (char_u *)top); | |
1606 | |
1607 /* Deal with a bad byte and continue with the next. */ | |
177 | 1608 ++fromp; |
1609 --from_size; | |
595 | 1610 if (bad_char_behavior == BAD_KEEP) |
1611 { | |
1612 *top++ = *(fromp - 1); | |
1613 --to_size; | |
1614 } | |
1615 else if (bad_char_behavior != BAD_DROP) | |
1616 { | |
1617 *top++ = bad_char_behavior; | |
1618 --to_size; | |
1619 } | |
177 | 1620 } |
7 | 1621 |
1622 if (from_size > 0) | |
1623 { | |
1624 /* Some remaining characters, keep them for the next | |
1625 * round. */ | |
1626 mch_memmove(conv_rest, (char_u *)fromp, from_size); | |
1627 conv_restlen = (int)from_size; | |
1628 } | |
1629 | |
1630 /* move the linerest to before the converted characters */ | |
1631 line_start = ptr - linerest; | |
1632 mch_memmove(line_start, buffer, (size_t)linerest); | |
1633 size = (long)((char_u *)top - ptr); | |
1634 } | |
1635 # endif | |
1636 | |
1637 # ifdef WIN3264 | |
1638 if (fio_flags & FIO_CODEPAGE) | |
1639 { | |
595 | 1640 char_u *src, *dst; |
1641 WCHAR ucs2buf[3]; | |
1642 int ucs2len; | |
1643 int codepage = FIO_GET_CP(fio_flags); | |
1644 int bytelen; | |
1645 int found_bad; | |
1646 char replstr[2]; | |
1647 | |
7 | 1648 /* |
1649 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or | |
595 | 1650 * a codepage, using standard MS-Windows functions. This |
1651 * requires two steps: | |
1652 * 1. convert from 'fileencoding' to ucs-2 | |
1653 * 2. convert from ucs-2 to 'encoding' | |
1654 * | |
1655 * Because there may be illegal bytes AND an incomplete byte | |
1656 * sequence at the end, we may have to do the conversion one | |
1657 * character at a time to get it right. | |
7 | 1658 */ |
595 | 1659 |
1660 /* Replacement string for WideCharToMultiByte(). */ | |
1661 if (bad_char_behavior > 0) | |
1662 replstr[0] = bad_char_behavior; | |
1663 else | |
1664 replstr[0] = '?'; | |
1665 replstr[1] = NUL; | |
1666 | |
1667 /* | |
1668 * Move the bytes to the end of the buffer, so that we have | |
1669 * room to put the result at the start. | |
1670 */ | |
1671 src = ptr + real_size - size; | |
1672 mch_memmove(src, ptr, size); | |
7 | 1673 |
1674 /* | |
595 | 1675 * Do the conversion. |
7 | 1676 */ |
595 | 1677 dst = ptr; |
1678 size = size; | |
1679 while (size > 0) | |
7 | 1680 { |
595 | 1681 found_bad = FALSE; |
7 | 1682 |
1683 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
595 | 1684 if (codepage == CP_UTF8) |
7 | 1685 { |
595 | 1686 /* Handle CP_UTF8 input ourselves to be able to handle |
1687 * trailing bytes properly. | |
1688 * Get one UTF-8 character from src. */ | |
835 | 1689 bytelen = (int)utf_ptr2len_len(src, size); |
595 | 1690 if (bytelen > size) |
1691 { | |
1692 /* Only got some bytes of a character. Normally | |
1693 * it's put in "conv_rest", but if it's too long | |
1694 * deal with it as if they were illegal bytes. */ | |
1695 if (bytelen <= CONV_RESTLEN) | |
1696 break; | |
1697 | |
1698 /* weird overlong byte sequence */ | |
1699 bytelen = size; | |
1700 found_bad = TRUE; | |
1701 } | |
1702 else | |
1703 { | |
799 | 1704 int u8c = utf_ptr2char(src); |
1705 | |
622 | 1706 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1)) |
595 | 1707 found_bad = TRUE; |
1708 ucs2buf[0] = u8c; | |
1709 ucs2len = 1; | |
1710 } | |
7 | 1711 } |
595 | 1712 else |
7 | 1713 # endif |
595 | 1714 { |
1715 /* We don't know how long the byte sequence is, try | |
1716 * from one to three bytes. */ | |
1717 for (bytelen = 1; bytelen <= size && bytelen <= 3; | |
1718 ++bytelen) | |
1719 { | |
1720 ucs2len = MultiByteToWideChar(codepage, | |
1721 MB_ERR_INVALID_CHARS, | |
1722 (LPCSTR)src, bytelen, | |
1723 ucs2buf, 3); | |
1724 if (ucs2len > 0) | |
1725 break; | |
1726 } | |
1727 if (ucs2len == 0) | |
1728 { | |
1729 /* If we have only one byte then it's probably an | |
1730 * incomplete byte sequence. Otherwise discard | |
1731 * one byte as a bad character. */ | |
1732 if (size == 1) | |
1733 break; | |
1734 found_bad = TRUE; | |
1735 bytelen = 1; | |
1736 } | |
1737 } | |
1738 | |
1739 if (!found_bad) | |
7 | 1740 { |
595 | 1741 int i; |
1742 | |
1743 /* Convert "ucs2buf[ucs2len]" to 'enc' in "dst". */ | |
1744 if (enc_utf8) | |
1745 { | |
1746 /* From UCS-2 to UTF-8. Cannot fail. */ | |
1747 for (i = 0; i < ucs2len; ++i) | |
1748 dst += utf_char2bytes(ucs2buf[i], dst); | |
1749 } | |
1750 else | |
1751 { | |
1752 BOOL bad = FALSE; | |
1753 int dstlen; | |
1754 | |
1755 /* From UCS-2 to "enc_codepage". If the | |
1756 * conversion uses the default character "?", | |
1757 * the data doesn't fit in this encoding. */ | |
1758 dstlen = WideCharToMultiByte(enc_codepage, 0, | |
1759 (LPCWSTR)ucs2buf, ucs2len, | |
835 | 1760 (LPSTR)dst, (int)(src - dst), |
595 | 1761 replstr, &bad); |
1762 if (bad) | |
1763 found_bad = TRUE; | |
1764 else | |
1765 dst += dstlen; | |
1766 } | |
7 | 1767 } |
595 | 1768 |
1769 if (found_bad) | |
1770 { | |
1771 /* Deal with bytes we can't convert. */ | |
1772 if (can_retry) | |
1773 goto rewind_retry; | |
1774 if (conv_error == 0) | |
1775 conv_error = readfile_linenr(linecnt, ptr, dst); | |
1776 if (bad_char_behavior != BAD_DROP) | |
1777 { | |
1778 if (bad_char_behavior == BAD_KEEP) | |
1779 { | |
1780 mch_memmove(dst, src, bytelen); | |
1781 dst += bytelen; | |
1782 } | |
1783 else | |
1784 *dst++ = bad_char_behavior; | |
1785 } | |
1786 } | |
1787 | |
1788 src += bytelen; | |
1789 size -= bytelen; | |
7 | 1790 } |
595 | 1791 |
1792 if (size > 0) | |
7 | 1793 { |
595 | 1794 /* An incomplete byte sequence remaining. */ |
1795 mch_memmove(conv_rest, src, size); | |
1796 conv_restlen = size; | |
7 | 1797 } |
595 | 1798 |
1799 /* The new size is equal to how much "dst" was advanced. */ | |
835 | 1800 size = (long)(dst - ptr); |
7 | 1801 } |
1802 else | |
1803 # endif | |
765 | 1804 # ifdef MACOS_CONVERT |
7 | 1805 if (fio_flags & FIO_MACROMAN) |
1806 { | |
1807 /* | |
1808 * Conversion from Apple MacRoman char encoding to UTF-8 or | |
18 | 1809 * latin1. This is in os_mac_conv.c. |
7 | 1810 */ |
18 | 1811 if (macroman2enc(ptr, &size, real_size) == FAIL) |
7 | 1812 goto rewind_retry; |
1813 } | |
1814 else | |
1815 # endif | |
1816 if (fio_flags != 0) | |
1817 { | |
1818 int u8c; | |
1819 char_u *dest; | |
1820 char_u *tail = NULL; | |
1821 | |
1822 /* | |
1823 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8. | |
1824 * "enc_utf8" not set: Convert Unicode to Latin1. | |
1825 * Go from end to start through the buffer, because the number | |
1826 * of bytes may increase. | |
1827 * "dest" points to after where the UTF-8 bytes go, "p" points | |
1828 * to after the next character to convert. | |
1829 */ | |
1830 dest = ptr + real_size; | |
1831 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8) | |
1832 { | |
1833 p = ptr + size; | |
1834 if (fio_flags == FIO_UTF8) | |
1835 { | |
1836 /* Check for a trailing incomplete UTF-8 sequence */ | |
1837 tail = ptr + size - 1; | |
1838 while (tail > ptr && (*tail & 0xc0) == 0x80) | |
1839 --tail; | |
1840 if (tail + utf_byte2len(*tail) <= ptr + size) | |
1841 tail = NULL; | |
1842 else | |
1843 p = tail; | |
1844 } | |
1845 } | |
1846 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1847 { | |
1848 /* Check for a trailing byte */ | |
1849 p = ptr + (size & ~1); | |
1850 if (size & 1) | |
1851 tail = p; | |
1852 if ((fio_flags & FIO_UTF16) && p > ptr) | |
1853 { | |
1854 /* Check for a trailing leading word */ | |
1855 if (fio_flags & FIO_ENDIAN_L) | |
1856 { | |
1857 u8c = (*--p << 8); | |
1858 u8c += *--p; | |
1859 } | |
1860 else | |
1861 { | |
1862 u8c = *--p; | |
1863 u8c += (*--p << 8); | |
1864 } | |
1865 if (u8c >= 0xd800 && u8c <= 0xdbff) | |
1866 tail = p; | |
1867 else | |
1868 p += 2; | |
1869 } | |
1870 } | |
1871 else /* FIO_UCS4 */ | |
1872 { | |
1873 /* Check for trailing 1, 2 or 3 bytes */ | |
1874 p = ptr + (size & ~3); | |
1875 if (size & 3) | |
1876 tail = p; | |
1877 } | |
1878 | |
1879 /* If there is a trailing incomplete sequence move it to | |
1880 * conv_rest[]. */ | |
1881 if (tail != NULL) | |
1882 { | |
1883 conv_restlen = (int)((ptr + size) - tail); | |
1884 mch_memmove(conv_rest, (char_u *)tail, conv_restlen); | |
1885 size -= conv_restlen; | |
1886 } | |
1887 | |
1888 | |
1889 while (p > ptr) | |
1890 { | |
1891 if (fio_flags & FIO_LATIN1) | |
1892 u8c = *--p; | |
1893 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1894 { | |
1895 if (fio_flags & FIO_ENDIAN_L) | |
1896 { | |
1897 u8c = (*--p << 8); | |
1898 u8c += *--p; | |
1899 } | |
1900 else | |
1901 { | |
1902 u8c = *--p; | |
1903 u8c += (*--p << 8); | |
1904 } | |
1905 if ((fio_flags & FIO_UTF16) | |
1906 && u8c >= 0xdc00 && u8c <= 0xdfff) | |
1907 { | |
1908 int u16c; | |
1909 | |
1910 if (p == ptr) | |
1911 { | |
1912 /* Missing leading word. */ | |
1913 if (can_retry) | |
1914 goto rewind_retry; | |
595 | 1915 if (conv_error == 0) |
1916 conv_error = readfile_linenr(linecnt, | |
1917 ptr, p); | |
1918 if (bad_char_behavior == BAD_DROP) | |
1919 continue; | |
1920 if (bad_char_behavior != BAD_KEEP) | |
1921 u8c = bad_char_behavior; | |
7 | 1922 } |
1923 | |
1924 /* found second word of double-word, get the first | |
1925 * word and compute the resulting character */ | |
1926 if (fio_flags & FIO_ENDIAN_L) | |
1927 { | |
1928 u16c = (*--p << 8); | |
1929 u16c += *--p; | |
1930 } | |
1931 else | |
1932 { | |
1933 u16c = *--p; | |
1934 u16c += (*--p << 8); | |
1935 } | |
595 | 1936 u8c = 0x10000 + ((u16c & 0x3ff) << 10) |
1937 + (u8c & 0x3ff); | |
1938 | |
7 | 1939 /* Check if the word is indeed a leading word. */ |
1940 if (u16c < 0xd800 || u16c > 0xdbff) | |
1941 { | |
1942 if (can_retry) | |
1943 goto rewind_retry; | |
595 | 1944 if (conv_error == 0) |
1945 conv_error = readfile_linenr(linecnt, | |
1946 ptr, p); | |
1947 if (bad_char_behavior == BAD_DROP) | |
1948 continue; | |
1949 if (bad_char_behavior != BAD_KEEP) | |
1950 u8c = bad_char_behavior; | |
7 | 1951 } |
1952 } | |
1953 } | |
1954 else if (fio_flags & FIO_UCS4) | |
1955 { | |
1956 if (fio_flags & FIO_ENDIAN_L) | |
1957 { | |
1958 u8c = (*--p << 24); | |
1959 u8c += (*--p << 16); | |
1960 u8c += (*--p << 8); | |
1961 u8c += *--p; | |
1962 } | |
1963 else /* big endian */ | |
1964 { | |
1965 u8c = *--p; | |
1966 u8c += (*--p << 8); | |
1967 u8c += (*--p << 16); | |
1968 u8c += (*--p << 24); | |
1969 } | |
1970 } | |
1971 else /* UTF-8 */ | |
1972 { | |
1973 if (*--p < 0x80) | |
1974 u8c = *p; | |
1975 else | |
1976 { | |
1977 len = utf_head_off(ptr, p); | |
595 | 1978 p -= len; |
1979 u8c = utf_ptr2char(p); | |
7 | 1980 if (len == 0) |
1981 { | |
1982 /* Not a valid UTF-8 character, retry with | |
1983 * another fenc when possible, otherwise just | |
1984 * report the error. */ | |
1985 if (can_retry) | |
1986 goto rewind_retry; | |
595 | 1987 if (conv_error == 0) |
1988 conv_error = readfile_linenr(linecnt, | |
1989 ptr, p); | |
1990 if (bad_char_behavior == BAD_DROP) | |
1991 continue; | |
1992 if (bad_char_behavior != BAD_KEEP) | |
1993 u8c = bad_char_behavior; | |
7 | 1994 } |
1995 } | |
1996 } | |
1997 if (enc_utf8) /* produce UTF-8 */ | |
1998 { | |
1999 dest -= utf_char2len(u8c); | |
2000 (void)utf_char2bytes(u8c, dest); | |
2001 } | |
2002 else /* produce Latin1 */ | |
2003 { | |
2004 --dest; | |
2005 if (u8c >= 0x100) | |
2006 { | |
2007 /* character doesn't fit in latin1, retry with | |
2008 * another fenc when possible, otherwise just | |
2009 * report the error. */ | |
595 | 2010 if (can_retry) |
7 | 2011 goto rewind_retry; |
595 | 2012 if (conv_error == 0) |
2013 conv_error = readfile_linenr(linecnt, ptr, p); | |
2014 if (bad_char_behavior == BAD_DROP) | |
2015 ++dest; | |
2016 else if (bad_char_behavior == BAD_KEEP) | |
2017 *dest = u8c; | |
2018 else if (eap != NULL && eap->bad_char != 0) | |
2019 *dest = bad_char_behavior; | |
2020 else | |
2021 *dest = 0xBF; | |
7 | 2022 } |
2023 else | |
2024 *dest = u8c; | |
2025 } | |
2026 } | |
2027 | |
2028 /* move the linerest to before the converted characters */ | |
2029 line_start = dest - linerest; | |
2030 mch_memmove(line_start, buffer, (size_t)linerest); | |
2031 size = (long)((ptr + real_size) - dest); | |
2032 ptr = dest; | |
2033 } | |
1597 | 2034 else if (enc_utf8 && !curbuf->b_p_bin) |
2035 { | |
2036 int incomplete_tail = FALSE; | |
2037 | |
2038 /* Reading UTF-8: Check if the bytes are valid UTF-8. */ | |
2039 for (p = ptr; ; ++p) | |
7 | 2040 { |
835 | 2041 int todo = (int)((ptr + size) - p); |
595 | 2042 int l; |
2043 | |
2044 if (todo <= 0) | |
2045 break; | |
7 | 2046 if (*p >= 0x80) |
2047 { | |
2048 /* A length of 1 means it's an illegal byte. Accept | |
2049 * an incomplete character at the end though, the next | |
2050 * read() will get the next bytes, we'll check it | |
2051 * then. */ | |
595 | 2052 l = utf_ptr2len_len(p, todo); |
1597 | 2053 if (l > todo && !incomplete_tail) |
7 | 2054 { |
1597 | 2055 /* Avoid retrying with a different encoding when |
2056 * a truncated file is more likely, or attempting | |
2057 * to read the rest of an incomplete sequence when | |
2058 * we have already done so. */ | |
2059 if (p > ptr || filesize > 0) | |
2060 incomplete_tail = TRUE; | |
2061 /* Incomplete byte sequence, move it to conv_rest[] | |
2062 * and try to read the rest of it, unless we've | |
2063 * already done so. */ | |
2064 if (p > ptr) | |
2065 { | |
2066 conv_restlen = todo; | |
2067 mch_memmove(conv_rest, p, conv_restlen); | |
2068 size -= conv_restlen; | |
2069 break; | |
2070 } | |
7 | 2071 } |
1597 | 2072 if (l == 1 || l > todo) |
595 | 2073 { |
2074 /* Illegal byte. If we can try another encoding | |
1597 | 2075 * do that, unless at EOF where a truncated |
2076 * file is more likely than a conversion error. */ | |
2077 if (can_retry && !incomplete_tail) | |
595 | 2078 break; |
2079 # ifdef USE_ICONV | |
2080 /* When we did a conversion report an error. */ | |
2081 if (iconv_fd != (iconv_t)-1 && conv_error == 0) | |
2082 conv_error = readfile_linenr(linecnt, ptr, p); | |
2083 # endif | |
1597 | 2084 /* Remember the first linenr with an illegal byte */ |
2085 if (conv_error == 0 && illegal_byte == 0) | |
2086 illegal_byte = readfile_linenr(linecnt, ptr, p); | |
595 | 2087 |
2088 /* Drop, keep or replace the bad byte. */ | |
2089 if (bad_char_behavior == BAD_DROP) | |
2090 { | |
1597 | 2091 mch_memmove(p, p + 1, todo - 1); |
595 | 2092 --p; |
2093 --size; | |
2094 } | |
2095 else if (bad_char_behavior != BAD_KEEP) | |
2096 *p = bad_char_behavior; | |
2097 } | |
1597 | 2098 else |
2099 p += l - 1; | |
7 | 2100 } |
2101 } | |
1597 | 2102 if (p < ptr + size && !incomplete_tail) |
7 | 2103 { |
2104 /* Detected a UTF-8 error. */ | |
2105 rewind_retry: | |
595 | 2106 /* Retry reading with another conversion. */ |
7 | 2107 # if defined(FEAT_EVAL) && defined(USE_ICONV) |
595 | 2108 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1) |
2109 /* iconv() failed, try 'charconvert' */ | |
2110 did_iconv = TRUE; | |
7 | 2111 else |
2112 # endif | |
595 | 2113 /* use next item from 'fileencodings' */ |
2114 advance_fenc = TRUE; | |
2115 file_rewind = TRUE; | |
2116 goto retry; | |
7 | 2117 } |
2118 } | |
2119 #endif | |
2120 | |
2121 /* count the number of characters (after conversion!) */ | |
2122 filesize += size; | |
2123 | |
2124 /* | |
2125 * when reading the first part of a file: guess EOL type | |
2126 */ | |
2127 if (fileformat == EOL_UNKNOWN) | |
2128 { | |
2129 /* First try finding a NL, for Dos and Unix */ | |
2130 if (try_dos || try_unix) | |
2131 { | |
6635 | 2132 /* Reset the carriage return counter. */ |
2133 if (try_mac) | |
2134 try_mac = 1; | |
2135 | |
7 | 2136 for (p = ptr; p < ptr + size; ++p) |
2137 { | |
2138 if (*p == NL) | |
2139 { | |
2140 if (!try_unix | |
2141 || (try_dos && p > ptr && p[-1] == CAR)) | |
2142 fileformat = EOL_DOS; | |
2143 else | |
2144 fileformat = EOL_UNIX; | |
2145 break; | |
2146 } | |
6618 | 2147 else if (*p == CAR && try_mac) |
2148 try_mac++; | |
7 | 2149 } |
2150 | |
2151 /* Don't give in to EOL_UNIX if EOL_MAC is more likely */ | |
2152 if (fileformat == EOL_UNIX && try_mac) | |
2153 { | |
2154 /* Need to reset the counters when retrying fenc. */ | |
2155 try_mac = 1; | |
2156 try_unix = 1; | |
2157 for (; p >= ptr && *p != CAR; p--) | |
2158 ; | |
2159 if (p >= ptr) | |
2160 { | |
2161 for (p = ptr; p < ptr + size; ++p) | |
2162 { | |
2163 if (*p == NL) | |
2164 try_unix++; | |
2165 else if (*p == CAR) | |
2166 try_mac++; | |
2167 } | |
2168 if (try_mac > try_unix) | |
2169 fileformat = EOL_MAC; | |
2170 } | |
2171 } | |
6618 | 2172 else if (fileformat == EOL_UNKNOWN && try_mac == 1) |
2173 /* Looking for CR but found no end-of-line markers at | |
2174 * all: use the default format. */ | |
2175 fileformat = default_fileformat(); | |
7 | 2176 } |
2177 | |
2178 /* No NL found: may use Mac format */ | |
2179 if (fileformat == EOL_UNKNOWN && try_mac) | |
2180 fileformat = EOL_MAC; | |
2181 | |
2182 /* Still nothing found? Use first format in 'ffs' */ | |
2183 if (fileformat == EOL_UNKNOWN) | |
2184 fileformat = default_fileformat(); | |
2185 | |
2186 /* if editing a new file: may set p_tx and p_ff */ | |
819 | 2187 if (set_options) |
7 | 2188 set_fileformat(fileformat, OPT_LOCAL); |
2189 } | |
2190 } | |
2191 | |
2192 /* | |
2193 * This loop is executed once for every character read. | |
2194 * Keep it fast! | |
2195 */ | |
2196 if (fileformat == EOL_MAC) | |
2197 { | |
2198 --ptr; | |
2199 while (++ptr, --size >= 0) | |
2200 { | |
2201 /* catch most common case first */ | |
2202 if ((c = *ptr) != NUL && c != CAR && c != NL) | |
2203 continue; | |
2204 if (c == NUL) | |
2205 *ptr = NL; /* NULs are replaced by newlines! */ | |
2206 else if (c == NL) | |
2207 *ptr = CAR; /* NLs are replaced by CRs! */ | |
2208 else | |
2209 { | |
2210 if (skip_count == 0) | |
2211 { | |
2212 *ptr = NUL; /* end of line */ | |
2213 len = (colnr_T) (ptr - line_start + 1); | |
2214 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2215 { | |
2216 error = TRUE; | |
2217 break; | |
2218 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2219 #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
|
2220 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
|
2221 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
|
2222 #endif |
7 | 2223 ++lnum; |
2224 if (--read_count == 0) | |
2225 { | |
2226 error = TRUE; /* break loop */ | |
2227 line_start = ptr; /* nothing left to write */ | |
2228 break; | |
2229 } | |
2230 } | |
2231 else | |
2232 --skip_count; | |
2233 line_start = ptr + 1; | |
2234 } | |
2235 } | |
2236 } | |
2237 else | |
2238 { | |
2239 --ptr; | |
2240 while (++ptr, --size >= 0) | |
2241 { | |
2242 if ((c = *ptr) != NUL && c != NL) /* catch most common case */ | |
2243 continue; | |
2244 if (c == NUL) | |
2245 *ptr = NL; /* NULs are replaced by newlines! */ | |
2246 else | |
2247 { | |
2248 if (skip_count == 0) | |
2249 { | |
2250 *ptr = NUL; /* end of line */ | |
2251 len = (colnr_T)(ptr - line_start + 1); | |
2252 if (fileformat == EOL_DOS) | |
2253 { | |
10674
d6857a8dc07e
patch 8.0.0227: crash with ff=dos when first line in file has no CR
Christian Brabandt <cb@256bit.org>
parents:
10668
diff
changeset
|
2254 if (ptr > line_start && ptr[-1] == CAR) |
7 | 2255 { |
10674
d6857a8dc07e
patch 8.0.0227: crash with ff=dos when first line in file has no CR
Christian Brabandt <cb@256bit.org>
parents:
10668
diff
changeset
|
2256 /* remove CR before NL */ |
7 | 2257 ptr[-1] = NUL; |
2258 --len; | |
2259 } | |
2260 /* | |
2261 * Reading in Dos format, but no CR-LF found! | |
2262 * When 'fileformats' includes "unix", delete all | |
2263 * the lines read so far and start all over again. | |
2264 * Otherwise give an error message later. | |
2265 */ | |
2266 else if (ff_error != EOL_DOS) | |
2267 { | |
2268 if ( try_unix | |
2269 && !read_stdin | |
2270 && (read_buffer | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
2271 || 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
|
2272 == 0)) |
7 | 2273 { |
2274 fileformat = EOL_UNIX; | |
819 | 2275 if (set_options) |
7 | 2276 set_fileformat(EOL_UNIX, OPT_LOCAL); |
2277 file_rewind = TRUE; | |
2278 keep_fileformat = TRUE; | |
2279 goto retry; | |
2280 } | |
2281 ff_error = EOL_DOS; | |
2282 } | |
2283 } | |
2284 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2285 { | |
2286 error = TRUE; | |
2287 break; | |
2288 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2289 #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
|
2290 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
|
2291 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
|
2292 #endif |
7 | 2293 ++lnum; |
2294 if (--read_count == 0) | |
2295 { | |
2296 error = TRUE; /* break loop */ | |
2297 line_start = ptr; /* nothing left to write */ | |
2298 break; | |
2299 } | |
2300 } | |
2301 else | |
2302 --skip_count; | |
2303 line_start = ptr + 1; | |
2304 } | |
2305 } | |
2306 } | |
2307 linerest = (long)(ptr - line_start); | |
2308 ui_breakcheck(); | |
2309 } | |
2310 | |
2311 failed: | |
2312 /* not an error, max. number of lines reached */ | |
2313 if (error && read_count == 0) | |
2314 error = FALSE; | |
2315 | |
2316 /* | |
2317 * If we get EOF in the middle of a line, note the fact and | |
2318 * complete the line ourselves. | |
2319 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set. | |
2320 */ | |
2321 if (!error | |
2322 && !got_int | |
2323 && linerest != 0 | |
2324 && !(!curbuf->b_p_bin | |
2325 && fileformat == EOL_DOS | |
2326 && *line_start == Ctrl_Z | |
2327 && ptr == line_start + 1)) | |
2328 { | |
819 | 2329 /* remember for when writing */ |
2330 if (set_options) | |
7 | 2331 curbuf->b_p_eol = FALSE; |
2332 *ptr = NUL; | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2333 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
|
2334 if (ml_append(lnum, line_start, len, newfile) == FAIL) |
7 | 2335 error = TRUE; |
2336 else | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2337 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2338 #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
|
2339 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
|
2340 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
|
2341 #endif |
7 | 2342 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
|
2343 } |
7 | 2344 } |
2345 | |
819 | 2346 if (set_options) |
7 | 2347 save_file_ff(curbuf); /* remember the current file format */ |
2348 | |
2349 #ifdef FEAT_CRYPT | |
6122 | 2350 if (curbuf->b_cryptstate != NULL) |
2351 { | |
2352 crypt_free_state(curbuf->b_cryptstate); | |
2353 curbuf->b_cryptstate = NULL; | |
2354 } | |
2355 if (cryptkey != NULL && cryptkey != curbuf->b_p_key) | |
2356 crypt_free_key(cryptkey); | |
2357 /* Don't set cryptkey to NULL, it's used below as a flag that | |
2358 * encryption was used. */ | |
7 | 2359 #endif |
2360 | |
2361 #ifdef FEAT_MBYTE | |
819 | 2362 /* If editing a new file: set 'fenc' for the current buffer. |
2363 * Also for ":read ++edit file". */ | |
2364 if (set_options) | |
7 | 2365 set_string_option_direct((char_u *)"fenc", -1, fenc, |
694 | 2366 OPT_FREE|OPT_LOCAL, 0); |
7 | 2367 if (fenc_alloced) |
2368 vim_free(fenc); | |
2369 # ifdef USE_ICONV | |
2370 if (iconv_fd != (iconv_t)-1) | |
2371 { | |
2372 iconv_close(iconv_fd); | |
2373 iconv_fd = (iconv_t)-1; | |
2374 } | |
2375 # endif | |
2376 #endif | |
2377 | |
2378 if (!read_buffer && !read_stdin) | |
2379 close(fd); /* errors are ignored */ | |
2003 | 2380 #ifdef HAVE_FD_CLOEXEC |
2381 else | |
2382 { | |
2383 int fdflags = fcntl(fd, F_GETFD); | |
2384 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
|
2385 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC); |
2003 | 2386 } |
2387 #endif | |
7 | 2388 vim_free(buffer); |
2389 | |
2390 #ifdef HAVE_DUP | |
2391 if (read_stdin) | |
2392 { | |
2393 /* Use stderr for stdin, makes shell commands work. */ | |
2394 close(0); | |
1757 | 2395 ignored = dup(2); |
7 | 2396 } |
2397 #endif | |
2398 | |
2399 #ifdef FEAT_MBYTE | |
2400 if (tmpname != NULL) | |
2401 { | |
2402 mch_remove(tmpname); /* delete converted file */ | |
2403 vim_free(tmpname); | |
2404 } | |
2405 #endif | |
2406 --no_wait_return; /* may wait for return now */ | |
2407 | |
2408 /* | |
2409 * In recovery mode everything but autocommands is skipped. | |
2410 */ | |
2411 if (!recoverymode) | |
2412 { | |
2413 /* need to delete the last line, which comes from the empty buffer */ | |
2414 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY)) | |
2415 { | |
2416 #ifdef FEAT_NETBEANS_INTG | |
2417 netbeansFireChanges = 0; | |
2418 #endif | |
2419 ml_delete(curbuf->b_ml.ml_line_count, FALSE); | |
2420 #ifdef FEAT_NETBEANS_INTG | |
2421 netbeansFireChanges = 1; | |
2422 #endif | |
2423 --linecnt; | |
2424 } | |
2425 linecnt = curbuf->b_ml.ml_line_count - linecnt; | |
2426 if (filesize == 0) | |
2427 linecnt = 0; | |
2428 if (newfile || read_buffer) | |
1201 | 2429 { |
7 | 2430 redraw_curbuf_later(NOT_VALID); |
1201 | 2431 #ifdef FEAT_DIFF |
2432 /* After reading the text into the buffer the diff info needs to | |
2433 * be updated. */ | |
2434 diff_invalidate(curbuf); | |
2435 #endif | |
2436 #ifdef FEAT_FOLDING | |
2437 /* All folds in the window are invalid now. Mark them for update | |
2438 * before triggering autocommands. */ | |
2439 foldUpdateAll(curwin); | |
2440 #endif | |
2441 } | |
7 | 2442 else if (linecnt) /* appended at least one line */ |
2443 appended_lines_mark(from, linecnt); | |
2444 | |
2445 #ifndef ALWAYS_USE_GUI | |
2446 /* | |
2447 * If we were reading from the same terminal as where messages go, | |
2448 * the screen will have been messed up. | |
2449 * Switch on raw mode now and clear the screen. | |
2450 */ | |
2451 if (read_stdin) | |
2452 { | |
2453 settmode(TMODE_RAW); /* set to raw mode */ | |
2454 starttermcap(); | |
2455 screenclear(); | |
2456 } | |
2457 #endif | |
2458 | |
2459 if (got_int) | |
2460 { | |
2461 if (!(flags & READ_DUMMY)) | |
2462 { | |
2463 filemess(curbuf, sfname, (char_u *)_(e_interr), 0); | |
2464 if (newfile) | |
2465 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
2466 } | |
2467 msg_scroll = msg_save; | |
2468 #ifdef FEAT_VIMINFO | |
2469 check_marks_read(); | |
2470 #endif | |
2471 return OK; /* an interrupt isn't really an error */ | |
2472 } | |
2473 | |
2474 if (!filtering && !(flags & READ_DUMMY)) | |
2475 { | |
2476 msg_add_fname(curbuf, sfname); /* fname in IObuff with quotes */ | |
2477 c = FALSE; | |
2478 | |
2479 #ifdef UNIX | |
2480 # ifdef S_ISFIFO | |
2481 if (S_ISFIFO(perm)) /* fifo or socket */ | |
2482 { | |
2483 STRCAT(IObuff, _("[fifo/socket]")); | |
2484 c = TRUE; | |
2485 } | |
2486 # else | |
2487 # ifdef S_IFIFO | |
2488 if ((perm & S_IFMT) == S_IFIFO) /* fifo */ | |
2489 { | |
2490 STRCAT(IObuff, _("[fifo]")); | |
2491 c = TRUE; | |
2492 } | |
2493 # endif | |
2494 # ifdef S_IFSOCK | |
2495 if ((perm & S_IFMT) == S_IFSOCK) /* or socket */ | |
2496 { | |
2497 STRCAT(IObuff, _("[socket]")); | |
2498 c = TRUE; | |
2499 } | |
2500 # endif | |
2501 # endif | |
1313 | 2502 # ifdef OPEN_CHR_FILES |
2503 if (S_ISCHR(perm)) /* or character special */ | |
2504 { | |
2505 STRCAT(IObuff, _("[character special]")); | |
2506 c = TRUE; | |
2507 } | |
2508 # endif | |
7 | 2509 #endif |
2510 if (curbuf->b_p_ro) | |
2511 { | |
2512 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")); | |
2513 c = TRUE; | |
2514 } | |
2515 if (read_no_eol_lnum) | |
2516 { | |
2517 msg_add_eol(); | |
2518 c = TRUE; | |
2519 } | |
2520 if (ff_error == EOL_DOS) | |
2521 { | |
2522 STRCAT(IObuff, _("[CR missing]")); | |
2523 c = TRUE; | |
2524 } | |
2525 if (split) | |
2526 { | |
2527 STRCAT(IObuff, _("[long lines split]")); | |
2528 c = TRUE; | |
2529 } | |
2530 #ifdef FEAT_MBYTE | |
2531 if (notconverted) | |
2532 { | |
2533 STRCAT(IObuff, _("[NOT converted]")); | |
2534 c = TRUE; | |
2535 } | |
2536 else if (converted) | |
2537 { | |
2538 STRCAT(IObuff, _("[converted]")); | |
2539 c = TRUE; | |
2540 } | |
2541 #endif | |
2542 #ifdef FEAT_CRYPT | |
2543 if (cryptkey != NULL) | |
2544 { | |
6122 | 2545 crypt_append_msg(curbuf); |
7 | 2546 c = TRUE; |
2547 } | |
2548 #endif | |
2549 #ifdef FEAT_MBYTE | |
595 | 2550 if (conv_error != 0) |
2551 { | |
2552 sprintf((char *)IObuff + STRLEN(IObuff), | |
2553 _("[CONVERSION ERROR in line %ld]"), (long)conv_error); | |
7 | 2554 c = TRUE; |
2555 } | |
2556 else if (illegal_byte > 0) | |
2557 { | |
2558 sprintf((char *)IObuff + STRLEN(IObuff), | |
2559 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte); | |
2560 c = TRUE; | |
2561 } | |
2562 else | |
2563 #endif | |
2564 if (error) | |
2565 { | |
2566 STRCAT(IObuff, _("[READ ERRORS]")); | |
2567 c = TRUE; | |
2568 } | |
2569 if (msg_add_fileformat(fileformat)) | |
2570 c = TRUE; | |
2571 #ifdef FEAT_CRYPT | |
2572 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
|
2573 msg_add_lines(c, (long)linecnt, filesize |
6122 | 2574 - crypt_get_header_len(crypt_get_method_nr(curbuf))); |
7 | 2575 else |
2576 #endif | |
2577 msg_add_lines(c, (long)linecnt, filesize); | |
2578 | |
2579 vim_free(keep_msg); | |
2580 keep_msg = NULL; | |
2581 msg_scrolled_ign = TRUE; | |
2582 #ifdef ALWAYS_USE_GUI | |
2583 /* Don't show the message when reading stdin, it would end up in a | |
2584 * message box (which might be shown when exiting!) */ | |
2585 if (read_stdin || read_buffer) | |
2586 p = msg_may_trunc(FALSE, IObuff); | |
2587 else | |
2588 #endif | |
2589 p = msg_trunc_attr(IObuff, FALSE, 0); | |
2590 if (read_stdin || read_buffer || restart_edit != 0 | |
540 | 2591 || (msg_scrolled != 0 && !need_wait_return)) |
7 | 2592 /* Need to repeat the message after redrawing when: |
2593 * - When reading from stdin (the screen will be cleared next). | |
2594 * - When restart_edit is set (otherwise there will be a delay | |
2595 * before redrawing). | |
2596 * - When the screen was scrolled but there is no wait-return | |
2597 * prompt. */ | |
679 | 2598 set_keep_msg(p, 0); |
7 | 2599 msg_scrolled_ign = FALSE; |
2600 } | |
2601 | |
2602 /* with errors writing the file requires ":w!" */ | |
2603 if (newfile && (error | |
2604 #ifdef FEAT_MBYTE | |
595 | 2605 || conv_error != 0 |
679 | 2606 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP) |
7 | 2607 #endif |
2608 )) | |
2609 curbuf->b_p_ro = TRUE; | |
2610 | |
2611 u_clearline(); /* cannot use "U" command after adding lines */ | |
2612 | |
2613 /* | |
2614 * In Ex mode: cursor at last new line. | |
2615 * Otherwise: cursor at first new line. | |
2616 */ | |
2617 if (exmode_active) | |
2618 curwin->w_cursor.lnum = from + linecnt; | |
2619 else | |
2620 curwin->w_cursor.lnum = from + 1; | |
2621 check_cursor_lnum(); | |
2622 beginline(BL_WHITE | BL_FIX); /* on first non-blank */ | |
2623 | |
2624 /* | |
2625 * Set '[ and '] marks to the newly read lines. | |
2626 */ | |
2627 curbuf->b_op_start.lnum = from + 1; | |
2628 curbuf->b_op_start.col = 0; | |
2629 curbuf->b_op_end.lnum = from + linecnt; | |
2630 curbuf->b_op_end.col = 0; | |
696 | 2631 |
2632 #ifdef WIN32 | |
2633 /* | |
2634 * Work around a weird problem: When a file has two links (only | |
2635 * possible on NTFS) and we write through one link, then stat() it | |
1651 | 2636 * through the other link, the timestamp information may be wrong. |
696 | 2637 * It's correct again after reading the file, thus reset the timestamp |
2638 * here. | |
2639 */ | |
2640 if (newfile && !read_stdin && !read_buffer | |
2641 && mch_stat((char *)fname, &st) >= 0) | |
2642 { | |
2643 buf_store_time(curbuf, &st, fname); | |
2644 curbuf->b_mtime_read = curbuf->b_mtime; | |
2645 } | |
2646 #endif | |
7 | 2647 } |
2648 msg_scroll = msg_save; | |
2649 | |
2650 #ifdef FEAT_VIMINFO | |
2651 /* | |
2652 * Get the marks before executing autocommands, so they can be used there. | |
2653 */ | |
2654 check_marks_read(); | |
2655 #endif | |
2656 | |
2657 /* | |
6933 | 2658 * We remember if the last line of the read didn't have |
2659 * an eol even when 'binary' is off, to support turning 'fixeol' off, | |
2660 * or writing the read again with 'binary' on. The latter is required | |
2661 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work. | |
7 | 2662 */ |
2707 | 2663 curbuf->b_no_eol_lnum = read_no_eol_lnum; |
7 | 2664 |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2665 /* 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
|
2666 * different. */ |
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2667 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
|
2668 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
|
2669 |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2670 #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
|
2671 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2672 * 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
|
2673 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2674 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
|
2675 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2676 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
|
2677 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2678 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
|
2679 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
|
2680 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2681 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2682 |
167 | 2683 #ifdef FEAT_AUTOCMD |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
2684 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL)) |
7 | 2685 { |
2686 int m = msg_scroll; | |
2687 int n = msg_scrolled; | |
2688 | |
2689 /* Save the fileformat now, otherwise the buffer will be considered | |
2690 * modified if the format/encoding was automatically detected. */ | |
819 | 2691 if (set_options) |
7 | 2692 save_file_ff(curbuf); |
2693 | |
2694 /* | |
2695 * The output from the autocommands should not overwrite anything and | |
2696 * should not be overwritten: Set msg_scroll, restore its value if no | |
2697 * output was done. | |
2698 */ | |
2699 msg_scroll = TRUE; | |
2700 if (filtering) | |
2701 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname, | |
2702 FALSE, curbuf, eap); | |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
2703 else if (newfile || (read_buffer && sfname != NULL)) |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2704 { |
7 | 2705 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname, |
2706 FALSE, curbuf, eap); | |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2707 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
|
2708 /* |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2709 * 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
|
2710 * 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
|
2711 */ |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2712 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
|
2713 TRUE, curbuf); |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2714 } |
7 | 2715 else |
2716 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname, | |
2717 FALSE, NULL, eap); | |
2718 if (msg_scrolled == n) | |
2719 msg_scroll = m; | |
2707 | 2720 # ifdef FEAT_EVAL |
7 | 2721 if (aborting()) /* autocmds may abort script processing */ |
2722 return FAIL; | |
2707 | 2723 # endif |
2724 } | |
2725 #endif | |
2726 | |
7 | 2727 if (recoverymode && error) |
2728 return FAIL; | |
2729 return OK; | |
2730 } | |
2731 | |
9911
74e345d2878c
commit https://github.com/vim/vim/commit/f04507d132fbcb63999167ec006fc6e700b5af4f
Christian Brabandt <cb@256bit.org>
parents:
9828
diff
changeset
|
2732 #if defined(OPEN_CHR_FILES) || defined(PROTO) |
1313 | 2733 /* |
2734 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+", | |
2735 * which is the name of files used for process substitution output by | |
2736 * some shells on some operating systems, e.g., bash on SunOS. | |
2737 * Do not accept "/dev/fd/[012]", opening these may hang Vim. | |
2738 */ | |
9911
74e345d2878c
commit https://github.com/vim/vim/commit/f04507d132fbcb63999167ec006fc6e700b5af4f
Christian Brabandt <cb@256bit.org>
parents:
9828
diff
changeset
|
2739 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2740 is_dev_fd_file(char_u *fname) |
1313 | 2741 { |
2742 return (STRNCMP(fname, "/dev/fd/", 8) == 0 | |
2743 && VIM_ISDIGIT(fname[8]) | |
2744 && *skipdigits(fname + 9) == NUL | |
2745 && (fname[9] != NUL | |
2746 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2'))); | |
2747 } | |
2748 #endif | |
2749 | |
595 | 2750 #ifdef FEAT_MBYTE |
2751 | |
2752 /* | |
2753 * From the current line count and characters read after that, estimate the | |
2754 * line number where we are now. | |
2755 * Used for error messages that include a line number. | |
2756 */ | |
2757 static linenr_T | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2758 readfile_linenr( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2759 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
|
2760 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
|
2761 char_u *endp) /* end of more bytes read */ |
595 | 2762 { |
2763 char_u *s; | |
2764 linenr_T lnum; | |
2765 | |
2766 lnum = curbuf->b_ml.ml_line_count - linecnt + 1; | |
2767 for (s = p; s < endp; ++s) | |
2768 if (*s == '\n') | |
2769 ++lnum; | |
2770 return lnum; | |
2771 } | |
2772 #endif | |
2773 | |
7 | 2774 /* |
612 | 2775 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be |
2776 * equal to the buffer "buf". Used for calling readfile(). | |
7 | 2777 * Returns OK or FAIL. |
2778 */ | |
2779 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2780 prep_exarg(exarg_T *eap, buf_T *buf) |
7 | 2781 { |
2782 eap->cmd = alloc((unsigned)(STRLEN(buf->b_p_ff) | |
2783 #ifdef FEAT_MBYTE | |
2784 + STRLEN(buf->b_p_fenc) | |
2785 #endif | |
2786 + 15)); | |
2787 if (eap->cmd == NULL) | |
2788 return FAIL; | |
2789 | |
2790 #ifdef FEAT_MBYTE | |
2791 sprintf((char *)eap->cmd, "e ++ff=%s ++enc=%s", buf->b_p_ff, buf->b_p_fenc); | |
2792 eap->force_enc = 14 + (int)STRLEN(buf->b_p_ff); | |
612 | 2793 eap->bad_char = buf->b_bad_char; |
7 | 2794 #else |
2795 sprintf((char *)eap->cmd, "e ++ff=%s", buf->b_p_ff); | |
2796 #endif | |
2797 eap->force_ff = 7; | |
612 | 2798 |
2799 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN; | |
819 | 2800 eap->read_edit = FALSE; |
612 | 2801 eap->forceit = FALSE; |
7 | 2802 return OK; |
2803 } | |
2804 | |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2805 /* |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2806 * Set default or forced 'fileformat' and 'binary'. |
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 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2809 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
|
2810 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2811 /* set default 'fileformat' */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2812 if (set_options) |
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 if (eap != NULL && eap->force_ff != 0) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2815 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
|
2816 else if (*p_ffs != NUL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2817 set_fileformat(default_fileformat(), OPT_LOCAL); |
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 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2820 /* set or reset 'binary' */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2821 if (eap != NULL && eap->force_bin != 0) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2822 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2823 int oldval = curbuf->b_p_bin; |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2824 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2825 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
|
2826 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
|
2827 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2828 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2829 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2830 #if defined(FEAT_MBYTE) || defined(PROTO) |
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 * Set forced 'fileencoding'. |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2833 */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2834 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2835 set_forced_fenc(exarg_T *eap) |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2836 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2837 if (eap->force_enc != 0) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2838 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2839 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
|
2840 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2841 if (fenc != NULL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2842 set_string_option_direct((char_u *)"fenc", -1, |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2843 fenc, OPT_FREE|OPT_LOCAL, 0); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2844 vim_free(fenc); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2845 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2846 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2847 |
7 | 2848 /* |
2849 * Find next fileencoding to use from 'fileencodings'. | |
2850 * "pp" points to fenc_next. It's advanced to the next item. | |
2851 * When there are no more items, an empty string is returned and *pp is set to | |
2852 * NULL. | |
2853 * When *pp is not set to NULL, the result is in allocated memory. | |
2854 */ | |
2855 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2856 next_fenc(char_u **pp) |
7 | 2857 { |
2858 char_u *p; | |
2859 char_u *r; | |
2860 | |
2861 if (**pp == NUL) | |
2862 { | |
2863 *pp = NULL; | |
2864 return (char_u *)""; | |
2865 } | |
2866 p = vim_strchr(*pp, ','); | |
2867 if (p == NULL) | |
2868 { | |
2869 r = enc_canonize(*pp); | |
2870 *pp += STRLEN(*pp); | |
2871 } | |
2872 else | |
2873 { | |
2874 r = vim_strnsave(*pp, (int)(p - *pp)); | |
2875 *pp = p + 1; | |
2876 if (r != NULL) | |
2877 { | |
2878 p = enc_canonize(r); | |
2879 vim_free(r); | |
2880 r = p; | |
2881 } | |
2882 } | |
2883 if (r == NULL) /* out of memory */ | |
2884 { | |
2885 r = (char_u *)""; | |
2886 *pp = NULL; | |
2887 } | |
2888 return r; | |
2889 } | |
2890 | |
2891 # ifdef FEAT_EVAL | |
2892 /* | |
2893 * Convert a file with the 'charconvert' expression. | |
2894 * This closes the file which is to be read, converts it and opens the | |
2895 * resulting file for reading. | |
2896 * Returns name of the resulting converted file (the caller should delete it | |
2897 * after reading it). | |
2898 * Returns NULL if the conversion failed ("*fdp" is not set) . | |
2899 */ | |
2900 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2901 readfile_charconvert( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2902 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
|
2903 char_u *fenc, /* converted from */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2904 int *fdp) /* in/out: file descriptor of file */ |
7 | 2905 { |
2906 char_u *tmpname; | |
2907 char_u *errmsg = NULL; | |
2908 | |
6721 | 2909 tmpname = vim_tempname('r', FALSE); |
7 | 2910 if (tmpname == NULL) |
2911 errmsg = (char_u *)_("Can't find temp file for conversion"); | |
2912 else | |
2913 { | |
2914 close(*fdp); /* close the input file, ignore errors */ | |
2915 *fdp = -1; | |
2916 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc, | |
2917 fname, tmpname) == FAIL) | |
2918 errmsg = (char_u *)_("Conversion with 'charconvert' failed"); | |
2919 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname, | |
2920 O_RDONLY | O_EXTRA, 0)) < 0) | |
2921 errmsg = (char_u *)_("can't read output of 'charconvert'"); | |
2922 } | |
2923 | |
2924 if (errmsg != NULL) | |
2925 { | |
2926 /* Don't use emsg(), it breaks mappings, the retry with | |
2927 * another type of conversion might still work. */ | |
2928 MSG(errmsg); | |
2929 if (tmpname != NULL) | |
2930 { | |
2931 mch_remove(tmpname); /* delete converted file */ | |
2932 vim_free(tmpname); | |
2933 tmpname = NULL; | |
2934 } | |
2935 } | |
2936 | |
2937 /* If the input file is closed, open it (caller should check for error). */ | |
2938 if (*fdp < 0) | |
2939 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
2940 | |
2941 return tmpname; | |
2942 } | |
2943 # endif | |
2944 | |
2945 #endif | |
2946 | |
2947 #ifdef FEAT_VIMINFO | |
2948 /* | |
2949 * Read marks for the current buffer from the viminfo file, when we support | |
2950 * buffer marks and the buffer has a name. | |
2951 */ | |
2952 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2953 check_marks_read(void) |
7 | 2954 { |
2955 if (!curbuf->b_marks_read && get_viminfo_parameter('\'') > 0 | |
2956 && curbuf->b_ffname != NULL) | |
1733 | 2957 read_viminfo(NULL, VIF_WANT_MARKS); |
7 | 2958 |
2959 /* Always set b_marks_read; needed when 'viminfo' is changed to include | |
2960 * the ' parameter after opening a buffer. */ | |
2961 curbuf->b_marks_read = TRUE; | |
2962 } | |
2963 #endif | |
2964 | |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2965 #if defined(FEAT_CRYPT) || defined(PROTO) |
7 | 2966 /* |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2967 * Check for magic number used for encryption. Applies to the current buffer. |
7 | 2968 * If found, the magic number is removed from ptr[*sizep] and *sizep and |
2969 * *filesizep are updated. | |
2970 * Return the (new) encryption key, NULL for no encryption. | |
2971 */ | |
2972 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2973 check_for_cryptkey( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2974 char_u *cryptkey, /* previous encryption key or NULL */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2975 char_u *ptr, /* pointer to read bytes */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2976 long *sizep, /* length of read bytes */ |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
2977 off_T *filesizep, /* nr of bytes used from file */ |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2978 int newfile, /* editing a new buffer */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2979 char_u *fname, /* file name to display */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2980 int *did_ask) /* flag: whether already asked for key */ |
7 | 2981 { |
6122 | 2982 int method = crypt_method_nr_from_magic((char *)ptr, *sizep); |
5312 | 2983 int b_p_ro = curbuf->b_p_ro; |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2984 |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2985 if (method >= 0) |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2986 { |
5312 | 2987 /* Mark the buffer as read-only until the decryption has taken place. |
2988 * Avoids accidentally overwriting the file with garbage. */ | |
2989 curbuf->b_p_ro = TRUE; | |
2990 | |
6130 | 2991 /* Set the cryptmethod local to the buffer. */ |
6122 | 2992 crypt_set_cm_option(curbuf, method); |
2204
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
2993 if (cryptkey == NULL && !*did_ask) |
7 | 2994 { |
2995 if (*curbuf->b_p_key) | |
2996 cryptkey = curbuf->b_p_key; | |
2997 else | |
2998 { | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2999 /* When newfile is TRUE, store the typed key in the 'key' |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3000 * option and don't free it. bf needs hash of the key saved. |
2204
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
3001 * Don't ask for the key again when first time Enter was hit. |
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
3002 * Happens when retrying to detect encoding. */ |
2267 | 3003 smsg((char_u *)_(need_key_msg), fname); |
3004 msg_scroll = TRUE; | |
6353 | 3005 crypt_check_method(method); |
6122 | 3006 cryptkey = crypt_get_key(newfile, FALSE); |
2204
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
3007 *did_ask = TRUE; |
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
3008 |
7 | 3009 /* check if empty key entered */ |
3010 if (cryptkey != NULL && *cryptkey == NUL) | |
3011 { | |
3012 if (cryptkey != curbuf->b_p_key) | |
3013 vim_free(cryptkey); | |
3014 cryptkey = NULL; | |
3015 } | |
3016 } | |
3017 } | |
3018 | |
3019 if (cryptkey != NULL) | |
3020 { | |
6122 | 3021 int header_len; |
3022 | |
3023 curbuf->b_cryptstate = crypt_create_from_header( | |
3024 method, cryptkey, ptr); | |
3025 crypt_set_cm_option(curbuf, method); | |
3026 | |
3027 /* Remove cryptmethod specific header from the text. */ | |
3028 header_len = crypt_get_header_len(method); | |
10221
fb1fde4fcff7
commit https://github.com/vim/vim/commit/680e015bfe19be6772d3bd754486fbd45c1a9d3b
Christian Brabandt <cb@256bit.org>
parents:
10086
diff
changeset
|
3029 if (*sizep <= header_len) |
fb1fde4fcff7
commit https://github.com/vim/vim/commit/680e015bfe19be6772d3bd754486fbd45c1a9d3b
Christian Brabandt <cb@256bit.org>
parents:
10086
diff
changeset
|
3030 /* invalid header, buffer can't be encrypted */ |
fb1fde4fcff7
commit https://github.com/vim/vim/commit/680e015bfe19be6772d3bd754486fbd45c1a9d3b
Christian Brabandt <cb@256bit.org>
parents:
10086
diff
changeset
|
3031 return NULL; |
6122 | 3032 *filesizep += header_len; |
3033 *sizep -= header_len; | |
3034 mch_memmove(ptr, ptr + header_len, (size_t)*sizep); | |
3035 | |
5312 | 3036 /* Restore the read-only flag. */ |
3037 curbuf->b_p_ro = b_p_ro; | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3038 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3039 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3040 /* When starting to edit a new file which does not have encryption, clear |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3041 * the 'key' option, except when starting up (called with -x argument) */ |
2410
8f6106dd3d12
Fix: editing a not encrypted file after a crypted file messed up reading the
Bram Moolenaar <bram@vim.org>
parents:
2394
diff
changeset
|
3042 else if (newfile && *curbuf->b_p_key != NUL && !starting) |
7 | 3043 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL); |
3044 | |
3045 return cryptkey; | |
3046 } | |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3047 #endif /* FEAT_CRYPT */ |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3048 |
7 | 3049 #ifdef UNIX |
3050 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3051 set_file_time( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3052 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3053 time_t atime, /* access time */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3054 time_t mtime) /* modification time */ |
7 | 3055 { |
3056 # if defined(HAVE_UTIME) && defined(HAVE_UTIME_H) | |
3057 struct utimbuf buf; | |
3058 | |
3059 buf.actime = atime; | |
3060 buf.modtime = mtime; | |
3061 (void)utime((char *)fname, &buf); | |
3062 # else | |
3063 # if defined(HAVE_UTIMES) | |
3064 struct timeval tvp[2]; | |
3065 | |
3066 tvp[0].tv_sec = atime; | |
3067 tvp[0].tv_usec = 0; | |
3068 tvp[1].tv_sec = mtime; | |
3069 tvp[1].tv_usec = 0; | |
3070 # ifdef NeXT | |
3071 (void)utimes((char *)fname, tvp); | |
3072 # else | |
3073 (void)utimes((char *)fname, (const struct timeval *)&tvp); | |
3074 # endif | |
3075 # endif | |
3076 # endif | |
3077 } | |
3078 #endif /* UNIX */ | |
3079 | |
22 | 3080 #if defined(VMS) && !defined(MIN) |
3081 /* Older DECC compiler for VAX doesn't define MIN() */ | |
3082 # define MIN(a, b) ((a) < (b) ? (a) : (b)) | |
3083 #endif | |
3084 | |
7 | 3085 /* |
1303 | 3086 * Return TRUE if a file appears to be read-only from the file permissions. |
3087 */ | |
3088 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3089 check_file_readonly( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3090 char_u *fname, /* full path to file */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3091 int perm) /* known permissions on file */ |
1303 | 3092 { |
3093 #ifndef USE_MCH_ACCESS | |
3094 int fd = 0; | |
3095 #endif | |
3096 | |
3097 return ( | |
3098 #ifdef USE_MCH_ACCESS | |
3099 # ifdef UNIX | |
3100 (perm & 0222) == 0 || | |
3101 # endif | |
3102 mch_access((char *)fname, W_OK) | |
3103 #else | |
3104 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0 | |
3105 ? TRUE : (close(fd), FALSE) | |
3106 #endif | |
3107 ); | |
3108 } | |
3109 | |
3110 | |
3111 /* | |
589 | 3112 * buf_write() - write to file "fname" lines "start" through "end" |
7 | 3113 * |
3114 * We do our own buffering here because fwrite() is so slow. | |
3115 * | |
589 | 3116 * If "forceit" is true, we don't care for errors when attempting backups. |
3117 * In case of an error everything possible is done to restore the original | |
1698 | 3118 * file. But when "forceit" is TRUE, we risk losing it. |
589 | 3119 * |
3120 * When "reset_changed" is TRUE and "append" == FALSE and "start" == 1 and | |
3121 * "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed. | |
7 | 3122 * |
3123 * This function must NOT use NameBuff (because it's called by autowrite()). | |
3124 * | |
3125 * return FAIL for failure, OK otherwise | |
3126 */ | |
3127 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3128 buf_write( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3129 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3130 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3131 char_u *sfname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3132 linenr_T start, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3133 linenr_T end, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3134 exarg_T *eap, /* for forced 'ff' and 'fenc', can be |
7 | 3135 NULL! */ |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3136 int append, /* append to the file */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3137 int forceit, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3138 int reset_changed, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3139 int filtering) |
7 | 3140 { |
3141 int fd; | |
3142 char_u *backup = NULL; | |
3143 int backup_copy = FALSE; /* copy the original file? */ | |
3144 int dobackup; | |
3145 char_u *ffname; | |
3146 char_u *wfname = NULL; /* name of file to write to */ | |
3147 char_u *s; | |
3148 char_u *ptr; | |
3149 char_u c; | |
3150 int len; | |
3151 linenr_T lnum; | |
3152 long nchars; | |
3153 char_u *errmsg = NULL; | |
1947 | 3154 int errmsg_allocated = FALSE; |
7 | 3155 char_u *errnum = NULL; |
3156 char_u *buffer; | |
3157 char_u smallbuf[SMBUFSIZE]; | |
3158 char_u *backup_ext; | |
3159 int bufsize; | |
3160 long perm; /* file permissions */ | |
3161 int retval = OK; | |
3162 int newfile = FALSE; /* TRUE if file doesn't exist yet */ | |
3163 int msg_save = msg_scroll; | |
3164 int overwriting; /* TRUE if writing over original */ | |
3165 int no_eol = FALSE; /* no end-of-line written */ | |
3166 int device = FALSE; /* writing to a device */ | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
3167 stat_T st_old; |
7 | 3168 int prev_got_int = got_int; |
3169 int file_readonly = FALSE; /* overwritten file is read-only */ | |
3170 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
|
3171 #if defined(UNIX) /*XXX fix me sometime? */ |
7 | 3172 int made_writable = FALSE; /* 'w' bit has been set */ |
3173 #endif | |
3174 /* writing everything */ | |
3175 int whole = (start == 1 && end == buf->b_ml.ml_line_count); | |
3176 #ifdef FEAT_AUTOCMD | |
3177 linenr_T old_line_count = buf->b_ml.ml_line_count; | |
3178 #endif | |
3179 int attr; | |
3180 int fileformat; | |
3181 int write_bin; | |
3182 struct bw_info write_info; /* info for buf_write_bytes() */ | |
3183 #ifdef FEAT_MBYTE | |
3184 int converted = FALSE; | |
3185 int notconverted = FALSE; | |
3186 char_u *fenc; /* effective 'fileencoding' */ | |
3187 char_u *fenc_tofree = NULL; /* allocated "fenc" */ | |
3188 #endif | |
3189 #ifdef HAS_BW_FLAGS | |
3190 int wb_flags = 0; | |
3191 #endif | |
3192 #ifdef HAVE_ACL | |
3193 vim_acl_T acl = NULL; /* ACL copied from original file to | |
3194 backup or new file */ | |
3195 #endif | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3196 #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
|
3197 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
|
3198 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
|
3199 #endif |
6243 | 3200 unsigned int bkc = get_bkc_value(buf); |
7 | 3201 |
3202 if (fname == NULL || *fname == NUL) /* safety check */ | |
3203 return FAIL; | |
2028 | 3204 if (buf->b_ml.ml_mfp == NULL) |
3205 { | |
3206 /* This can happen during startup when there is a stray "w" in the | |
3207 * vimrc file. */ | |
3208 EMSG(_(e_emptybuf)); | |
3209 return FAIL; | |
3210 } | |
7 | 3211 |
3212 /* | |
3213 * Disallow writing from .exrc and .vimrc in current directory for | |
3214 * security reasons. | |
3215 */ | |
3216 if (check_secure()) | |
3217 return FAIL; | |
3218 | |
3219 /* Avoid a crash for a long name. */ | |
3220 if (STRLEN(fname) >= MAXPATHL) | |
3221 { | |
3222 EMSG(_(e_longname)); | |
3223 return FAIL; | |
3224 } | |
3225 | |
3226 #ifdef FEAT_MBYTE | |
3227 /* must init bw_conv_buf and bw_iconv_fd before jumping to "fail" */ | |
3228 write_info.bw_conv_buf = NULL; | |
3229 write_info.bw_conv_error = FALSE; | |
1947 | 3230 write_info.bw_conv_error_lnum = 0; |
7 | 3231 write_info.bw_restlen = 0; |
3232 # ifdef USE_ICONV | |
3233 write_info.bw_iconv_fd = (iconv_t)-1; | |
3234 # endif | |
3235 #endif | |
6122 | 3236 #ifdef FEAT_CRYPT |
3237 write_info.bw_buffer = buf; | |
3238 #endif | |
7 | 3239 |
167 | 3240 /* After writing a file changedtick changes but we don't want to display |
3241 * the line. */ | |
3242 ex_no_reprint = TRUE; | |
3243 | |
7 | 3244 /* |
3245 * If there is no file name yet, use the one for the written file. | |
3246 * BF_NOTEDITED is set to reflect this (in case the write fails). | |
3247 * Don't do this when the write is for a filter command. | |
589 | 3248 * Don't do this when appending. |
3249 * Only do this when 'cpoptions' contains the 'F' flag. | |
7 | 3250 */ |
633 | 3251 if (buf->b_ffname == NULL |
3252 && reset_changed | |
7 | 3253 && whole |
3254 && buf == curbuf | |
236 | 3255 #ifdef FEAT_QUICKFIX |
3256 && !bt_nofile(buf) | |
3257 #endif | |
7 | 3258 && !filtering |
589 | 3259 && (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL) |
7 | 3260 && vim_strchr(p_cpo, CPO_FNAMEW) != NULL) |
3261 { | |
633 | 3262 if (set_rw_fname(fname, sfname) == FAIL) |
7 | 3263 return FAIL; |
633 | 3264 buf = curbuf; /* just in case autocmds made "buf" invalid */ |
7 | 3265 } |
3266 | |
3267 if (sfname == NULL) | |
3268 sfname = fname; | |
3269 /* | |
3270 * For Unix: Use the short file name whenever possible. | |
3271 * Avoids problems with networks and when directory names are changed. | |
3272 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to | |
3273 * another directory, which we don't detect | |
3274 */ | |
3275 ffname = fname; /* remember full fname */ | |
3276 #ifdef UNIX | |
3277 fname = sfname; | |
3278 #endif | |
3279 | |
3280 if (buf->b_ffname != NULL && fnamecmp(ffname, buf->b_ffname) == 0) | |
3281 overwriting = TRUE; | |
3282 else | |
3283 overwriting = FALSE; | |
3284 | |
3285 if (exiting) | |
4352 | 3286 settmode(TMODE_COOK); /* when exiting allow typeahead now */ |
7 | 3287 |
3288 ++no_wait_return; /* don't wait for return yet */ | |
3289 | |
3290 /* | |
3291 * Set '[ and '] marks to the lines to be written. | |
3292 */ | |
3293 buf->b_op_start.lnum = start; | |
3294 buf->b_op_start.col = 0; | |
3295 buf->b_op_end.lnum = end; | |
3296 buf->b_op_end.col = 0; | |
3297 | |
3298 #ifdef FEAT_AUTOCMD | |
3299 { | |
3300 aco_save_T aco; | |
3301 int buf_ffname = FALSE; | |
3302 int buf_sfname = FALSE; | |
3303 int buf_fname_f = FALSE; | |
3304 int buf_fname_s = FALSE; | |
3305 int did_cmd = FALSE; | |
17 | 3306 int nofile_err = FALSE; |
161 | 3307 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
|
3308 bufref_T bufref; |
7 | 3309 |
3310 /* | |
4352 | 3311 * Apply PRE autocommands. |
7 | 3312 * Set curbuf to the buffer to be written. |
3313 * Careful: The autocommands may call buf_write() recursively! | |
3314 */ | |
3315 if (ffname == buf->b_ffname) | |
3316 buf_ffname = TRUE; | |
3317 if (sfname == buf->b_sfname) | |
3318 buf_sfname = TRUE; | |
3319 if (fname == buf->b_ffname) | |
3320 buf_fname_f = TRUE; | |
3321 if (fname == buf->b_sfname) | |
3322 buf_fname_s = TRUE; | |
3323 | |
3324 /* set curwin/curbuf to buf and save a few things */ | |
3325 aucmd_prepbuf(&aco, buf); | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
3326 set_bufref(&bufref, buf); |
7 | 3327 |
3328 if (append) | |
3329 { | |
3330 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD, | |
3331 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3332 { |
635 | 3333 #ifdef FEAT_QUICKFIX |
18 | 3334 if (overwriting && bt_nofile(curbuf)) |
17 | 3335 nofile_err = TRUE; |
3336 else | |
635 | 3337 #endif |
17 | 3338 apply_autocmds_exarg(EVENT_FILEAPPENDPRE, |
7 | 3339 sfname, sfname, FALSE, curbuf, eap); |
17 | 3340 } |
7 | 3341 } |
3342 else if (filtering) | |
3343 { | |
3344 apply_autocmds_exarg(EVENT_FILTERWRITEPRE, | |
3345 NULL, sfname, FALSE, curbuf, eap); | |
3346 } | |
3347 else if (reset_changed && whole) | |
3348 { | |
3036 | 3349 int was_changed = curbufIsChanged(); |
3350 | |
3351 did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD, | |
3352 sfname, sfname, FALSE, curbuf, eap); | |
3353 if (did_cmd) | |
3354 { | |
3355 if (was_changed && !curbufIsChanged()) | |
3356 { | |
3357 /* Written everything correctly and BufWriteCmd has reset | |
3358 * 'modified': Correct the undo information so that an | |
3359 * undo now sets 'modified'. */ | |
3360 u_unchanged(curbuf); | |
3361 u_update_save_nr(curbuf); | |
3362 } | |
3363 } | |
3364 else | |
17 | 3365 { |
635 | 3366 #ifdef FEAT_QUICKFIX |
179 | 3367 if (overwriting && bt_nofile(curbuf)) |
17 | 3368 nofile_err = TRUE; |
3369 else | |
635 | 3370 #endif |
17 | 3371 apply_autocmds_exarg(EVENT_BUFWRITEPRE, |
7 | 3372 sfname, sfname, FALSE, curbuf, eap); |
17 | 3373 } |
7 | 3374 } |
3375 else | |
3376 { | |
3377 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD, | |
3378 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3379 { |
635 | 3380 #ifdef FEAT_QUICKFIX |
179 | 3381 if (overwriting && bt_nofile(curbuf)) |
17 | 3382 nofile_err = TRUE; |
3383 else | |
635 | 3384 #endif |
17 | 3385 apply_autocmds_exarg(EVENT_FILEWRITEPRE, |
7 | 3386 sfname, sfname, FALSE, curbuf, eap); |
17 | 3387 } |
7 | 3388 } |
3389 | |
3390 /* restore curwin/curbuf and a few other things */ | |
3391 aucmd_restbuf(&aco); | |
3392 | |
3393 /* | |
3394 * In three situations we return here and don't write the file: | |
3395 * 1. the autocommands deleted or unloaded the buffer. | |
3396 * 2. The autocommands abort script processing. | |
3397 * 3. If one of the "Cmd" autocommands was executed. | |
3398 */ | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
3399 if (!bufref_valid(&bufref)) |
7 | 3400 buf = NULL; |
161 | 3401 if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline) |
532 | 3402 || did_cmd || nofile_err |
3403 #ifdef FEAT_EVAL | |
3404 || aborting() | |
3405 #endif | |
3406 ) | |
7 | 3407 { |
3408 --no_wait_return; | |
3409 msg_scroll = msg_save; | |
17 | 3410 if (nofile_err) |
3411 EMSG(_("E676: No matching autocommands for acwrite buffer")); | |
3412 | |
532 | 3413 if (nofile_err |
3414 #ifdef FEAT_EVAL | |
3415 || aborting() | |
3416 #endif | |
3417 ) | |
7 | 3418 /* An aborting error, interrupt or exception in the |
3419 * autocommands. */ | |
3420 return FAIL; | |
3421 if (did_cmd) | |
3422 { | |
3423 if (buf == NULL) | |
3424 /* The buffer was deleted. We assume it was written | |
3425 * (can't retry anyway). */ | |
3426 return OK; | |
3427 if (overwriting) | |
3428 { | |
3429 /* Assume the buffer was written, update the timestamp. */ | |
3430 ml_timestamp(buf); | |
589 | 3431 if (append) |
3432 buf->b_flags &= ~BF_NEW; | |
3433 else | |
3434 buf->b_flags &= ~BF_WRITE_MASK; | |
7 | 3435 } |
589 | 3436 if (reset_changed && buf->b_changed && !append |
39 | 3437 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)) |
7 | 3438 /* Buffer still changed, the autocommands didn't work |
3439 * properly. */ | |
3440 return FAIL; | |
3441 return OK; | |
3442 } | |
3443 #ifdef FEAT_EVAL | |
3444 if (!aborting()) | |
3445 #endif | |
3446 EMSG(_("E203: Autocommands deleted or unloaded buffer to be written")); | |
3447 return FAIL; | |
3448 } | |
3449 | |
3450 /* | |
3451 * The autocommands may have changed the number of lines in the file. | |
3452 * When writing the whole file, adjust the end. | |
3453 * When writing part of the file, assume that the autocommands only | |
3454 * changed the number of lines that are to be written (tricky!). | |
3455 */ | |
3456 if (buf->b_ml.ml_line_count != old_line_count) | |
3457 { | |
3458 if (whole) /* write all */ | |
3459 end = buf->b_ml.ml_line_count; | |
3460 else if (buf->b_ml.ml_line_count > old_line_count) /* more lines */ | |
3461 end += buf->b_ml.ml_line_count - old_line_count; | |
3462 else /* less lines */ | |
3463 { | |
3464 end -= old_line_count - buf->b_ml.ml_line_count; | |
3465 if (end < start) | |
3466 { | |
3467 --no_wait_return; | |
3468 msg_scroll = msg_save; | |
3469 EMSG(_("E204: Autocommand changed number of lines in unexpected way")); | |
3470 return FAIL; | |
3471 } | |
3472 } | |
3473 } | |
3474 | |
3475 /* | |
3476 * The autocommands may have changed the name of the buffer, which may | |
3477 * be kept in fname, ffname and sfname. | |
3478 */ | |
3479 if (buf_ffname) | |
3480 ffname = buf->b_ffname; | |
3481 if (buf_sfname) | |
3482 sfname = buf->b_sfname; | |
3483 if (buf_fname_f) | |
3484 fname = buf->b_ffname; | |
3485 if (buf_fname_s) | |
3486 fname = buf->b_sfname; | |
3487 } | |
3488 #endif | |
3489 | |
3490 #ifdef FEAT_NETBEANS_INTG | |
2210 | 3491 if (netbeans_active() && isNetbeansBuffer(buf)) |
7 | 3492 { |
3493 if (whole) | |
3494 { | |
3495 /* | |
3496 * b_changed can be 0 after an undo, but we still need to write | |
3497 * the buffer to NetBeans. | |
3498 */ | |
3499 if (buf->b_changed || isNetbeansModified(buf)) | |
3500 { | |
33 | 3501 --no_wait_return; /* may wait for return now */ |
3502 msg_scroll = msg_save; | |
3503 netbeans_save_buffer(buf); /* no error checking... */ | |
7 | 3504 return retval; |
3505 } | |
3506 else | |
3507 { | |
3508 errnum = (char_u *)"E656: "; | |
1653 | 3509 errmsg = (char_u *)_("NetBeans disallows writes of unmodified buffers"); |
7 | 3510 buffer = NULL; |
3511 goto fail; | |
3512 } | |
3513 } | |
3514 else | |
3515 { | |
3516 errnum = (char_u *)"E657: "; | |
3517 errmsg = (char_u *)_("Partial writes disallowed for NetBeans buffers"); | |
3518 buffer = NULL; | |
3519 goto fail; | |
3520 } | |
3521 } | |
3522 #endif | |
3523 | |
3524 if (shortmess(SHM_OVER) && !exiting) | |
3525 msg_scroll = FALSE; /* overwrite previous file message */ | |
3526 else | |
3527 msg_scroll = TRUE; /* don't overwrite previous file message */ | |
3528 if (!filtering) | |
3529 filemess(buf, | |
3530 #ifndef UNIX | |
3531 sfname, | |
3532 #else | |
3533 fname, | |
3534 #endif | |
3535 (char_u *)"", 0); /* show that we are busy */ | |
3536 msg_scroll = FALSE; /* always overwrite the file message now */ | |
3537 | |
3538 buffer = alloc(BUFSIZE); | |
3539 if (buffer == NULL) /* can't allocate big buffer, use small | |
3540 * one (to be able to write when out of | |
3541 * memory) */ | |
3542 { | |
3543 buffer = smallbuf; | |
3544 bufsize = SMBUFSIZE; | |
3545 } | |
3546 else | |
3547 bufsize = BUFSIZE; | |
3548 | |
3549 /* | |
3550 * Get information about original file (if there is one). | |
3551 */ | |
7410
08e62c4fc17d
commit https://github.com/vim/vim/commit/53076830fea6df737455523f7e235bfe4f79864d
Christian Brabandt <cb@256bit.org>
parents:
7408
diff
changeset
|
3552 #if defined(UNIX) |
1438 | 3553 st_old.st_dev = 0; |
3554 st_old.st_ino = 0; | |
7 | 3555 perm = -1; |
3556 if (mch_stat((char *)fname, &st_old) < 0) | |
3557 newfile = TRUE; | |
3558 else | |
3559 { | |
3560 perm = st_old.st_mode; | |
3561 if (!S_ISREG(st_old.st_mode)) /* not a file */ | |
3562 { | |
3563 if (S_ISDIR(st_old.st_mode)) | |
3564 { | |
3565 errnum = (char_u *)"E502: "; | |
3566 errmsg = (char_u *)_("is a directory"); | |
3567 goto fail; | |
3568 } | |
3569 if (mch_nodetype(fname) != NODE_WRITABLE) | |
3570 { | |
3571 errnum = (char_u *)"E503: "; | |
3572 errmsg = (char_u *)_("is not a file or writable device"); | |
3573 goto fail; | |
3574 } | |
3575 /* It's a device of some kind (or a fifo) which we can write to | |
3576 * but for which we can't make a backup. */ | |
3577 device = TRUE; | |
3578 newfile = TRUE; | |
3579 perm = -1; | |
3580 } | |
3581 } | |
3582 #else /* !UNIX */ | |
3583 /* | |
3584 * Check for a writable device name. | |
3585 */ | |
3586 c = mch_nodetype(fname); | |
3587 if (c == NODE_OTHER) | |
3588 { | |
3589 errnum = (char_u *)"E503: "; | |
3590 errmsg = (char_u *)_("is not a file or writable device"); | |
3591 goto fail; | |
3592 } | |
3593 if (c == NODE_WRITABLE) | |
3594 { | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3595 # if defined(MSWIN) |
1004 | 3596 /* MS-Windows allows opening a device, but we will probably get stuck |
3597 * trying to write to it. */ | |
3598 if (!p_odev) | |
3599 { | |
3600 errnum = (char_u *)"E796: "; | |
3601 errmsg = (char_u *)_("writing to device disabled with 'opendevice' option"); | |
3602 goto fail; | |
3603 } | |
3604 # endif | |
7 | 3605 device = TRUE; |
3606 newfile = TRUE; | |
3607 perm = -1; | |
3608 } | |
3609 else | |
3610 { | |
3611 perm = mch_getperm(fname); | |
3612 if (perm < 0) | |
3613 newfile = TRUE; | |
3614 else if (mch_isdir(fname)) | |
3615 { | |
3616 errnum = (char_u *)"E502: "; | |
3617 errmsg = (char_u *)_("is a directory"); | |
3618 goto fail; | |
3619 } | |
3620 if (overwriting) | |
3621 (void)mch_stat((char *)fname, &st_old); | |
3622 } | |
3623 #endif /* !UNIX */ | |
3624 | |
3625 if (!device && !newfile) | |
3626 { | |
3627 /* | |
3628 * Check if the file is really writable (when renaming the file to | |
3629 * make a backup we won't discover it later). | |
3630 */ | |
1303 | 3631 file_readonly = check_file_readonly(fname, (int)perm); |
3632 | |
7 | 3633 if (!forceit && file_readonly) |
3634 { | |
3635 if (vim_strchr(p_cpo, CPO_FWRITE) != NULL) | |
3636 { | |
3637 errnum = (char_u *)"E504: "; | |
3638 errmsg = (char_u *)_(err_readonly); | |
3639 } | |
3640 else | |
3641 { | |
3642 errnum = (char_u *)"E505: "; | |
3643 errmsg = (char_u *)_("is read-only (add ! to override)"); | |
3644 } | |
3645 goto fail; | |
3646 } | |
3647 | |
3648 /* | |
3649 * Check if the timestamp hasn't changed since reading the file. | |
3650 */ | |
3651 if (overwriting) | |
3652 { | |
3653 retval = check_mtime(buf, &st_old); | |
3654 if (retval == FAIL) | |
3655 goto fail; | |
3656 } | |
3657 } | |
3658 | |
3659 #ifdef HAVE_ACL | |
3660 /* | |
3661 * For systems that support ACL: get the ACL from the original file. | |
3662 */ | |
3663 if (!newfile) | |
3664 acl = mch_get_acl(fname); | |
3665 #endif | |
3666 | |
3667 /* | |
3668 * If 'backupskip' is not empty, don't make a backup for some files. | |
3669 */ | |
3670 dobackup = (p_wb || p_bk || *p_pm != NUL); | |
3671 #ifdef FEAT_WILDIGN | |
3672 if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname)) | |
3673 dobackup = FALSE; | |
3674 #endif | |
3675 | |
3676 /* | |
3677 * Save the value of got_int and reset it. We don't want a previous | |
3678 * interruption cancel writing, only hitting CTRL-C while writing should | |
3679 * abort it. | |
3680 */ | |
3681 prev_got_int = got_int; | |
3682 got_int = FALSE; | |
3683 | |
3684 /* Mark the buffer as 'being saved' to prevent changed buffer warnings */ | |
3685 buf->b_saving = TRUE; | |
3686 | |
3687 /* | |
3688 * If we are not appending or filtering, the file exists, and the | |
3689 * 'writebackup', 'backup' or 'patchmode' option is set, need a backup. | |
3690 * When 'patchmode' is set also make a backup when appending. | |
3691 * | |
3692 * Do not make any backup, if 'writebackup' and 'backup' are both switched | |
3693 * off. This helps when editing large files on almost-full disks. | |
3694 */ | |
3695 if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup) | |
3696 { | |
3697 #if defined(UNIX) || defined(WIN32) | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
3698 stat_T st; |
7 | 3699 #endif |
3700 | |
6243 | 3701 if ((bkc & BKC_YES) || append) /* "yes" */ |
7 | 3702 backup_copy = TRUE; |
3703 #if defined(UNIX) || defined(WIN32) | |
6243 | 3704 else if ((bkc & BKC_AUTO)) /* "auto" */ |
7 | 3705 { |
3706 int i; | |
3707 | |
3708 # ifdef UNIX | |
3709 /* | |
3710 * Don't rename the file when: | |
3711 * - it's a hard link | |
3712 * - it's a symbolic link | |
3713 * - we don't have write permission in the directory | |
3714 * - we can't set the owner/group of the new file | |
3715 */ | |
3716 if (st_old.st_nlink > 1 | |
3717 || mch_lstat((char *)fname, &st) < 0 | |
3718 || st.st_dev != st_old.st_dev | |
557 | 3719 || st.st_ino != st_old.st_ino |
3720 # ifndef HAVE_FCHOWN | |
3721 || st.st_uid != st_old.st_uid | |
3722 || st.st_gid != st_old.st_gid | |
3723 # endif | |
3724 ) | |
7 | 3725 backup_copy = TRUE; |
3726 else | |
696 | 3727 # else |
3728 # ifdef WIN32 | |
3729 /* On NTFS file systems hard links are possible. */ | |
3730 if (mch_is_linked(fname)) | |
3731 backup_copy = TRUE; | |
3732 else | |
3733 # endif | |
7 | 3734 # endif |
3735 { | |
3736 /* | |
3737 * Check if we can create a file and set the owner/group to | |
3738 * the ones from the original file. | |
3739 * First find a file name that doesn't exist yet (use some | |
3740 * arbitrary numbers). | |
3741 */ | |
3742 STRCPY(IObuff, fname); | |
3743 for (i = 4913; ; i += 123) | |
3744 { | |
3745 sprintf((char *)gettail(IObuff), "%d", i); | |
557 | 3746 if (mch_lstat((char *)IObuff, &st) < 0) |
7 | 3747 break; |
3748 } | |
557 | 3749 fd = mch_open((char *)IObuff, |
3750 O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm); | |
7 | 3751 if (fd < 0) /* can't write in directory */ |
3752 backup_copy = TRUE; | |
3753 else | |
3754 { | |
3755 # ifdef UNIX | |
557 | 3756 # ifdef HAVE_FCHOWN |
1757 | 3757 ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
557 | 3758 # endif |
7 | 3759 if (mch_stat((char *)IObuff, &st) < 0 |
3760 || st.st_uid != st_old.st_uid | |
3761 || st.st_gid != st_old.st_gid | |
1877 | 3762 || (long)st.st_mode != perm) |
7 | 3763 backup_copy = TRUE; |
3764 # endif | |
567 | 3765 /* Close the file before removing it, on MS-Windows we |
3766 * can't delete an open file. */ | |
3767 close(fd); | |
7 | 3768 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
|
3769 # 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
|
3770 /* 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
|
3771 * 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
|
3772 * second. */ |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3773 { |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3774 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
|
3775 |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3776 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
|
3777 { |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3778 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
|
3779 break; |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3780 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
|
3781 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
|
3782 } |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3783 } |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3784 # endif |
7 | 3785 } |
3786 } | |
3787 } | |
3788 | |
3789 /* | |
3790 * Break symlinks and/or hardlinks if we've been asked to. | |
3791 */ | |
6243 | 3792 if ((bkc & BKC_BREAKSYMLINK) || (bkc & BKC_BREAKHARDLINK)) |
7 | 3793 { |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3794 # ifdef UNIX |
7 | 3795 int lstat_res; |
3796 | |
3797 lstat_res = mch_lstat((char *)fname, &st); | |
3798 | |
3799 /* Symlinks. */ | |
6243 | 3800 if ((bkc & BKC_BREAKSYMLINK) |
7 | 3801 && lstat_res == 0 |
3802 && st.st_ino != st_old.st_ino) | |
3803 backup_copy = FALSE; | |
3804 | |
3805 /* Hardlinks. */ | |
6243 | 3806 if ((bkc & BKC_BREAKHARDLINK) |
7 | 3807 && st_old.st_nlink > 1 |
3808 && (lstat_res != 0 || st.st_ino == st_old.st_ino)) | |
3809 backup_copy = FALSE; | |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3810 # else |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3811 # if defined(WIN32) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3812 /* Symlinks. */ |
6243 | 3813 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
|
3814 backup_copy = FALSE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3815 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3816 /* Hardlinks. */ |
6243 | 3817 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
|
3818 backup_copy = FALSE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3819 # endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3820 # endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3821 } |
7 | 3822 |
3823 #endif | |
3824 | |
3825 /* make sure we have a valid backup extension to use */ | |
3826 if (*p_bex == NUL) | |
3827 backup_ext = (char_u *)".bak"; | |
3828 else | |
3829 backup_ext = p_bex; | |
3830 | |
3831 if (backup_copy | |
3832 && (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) >= 0) | |
3833 { | |
3834 int bfd; | |
3835 char_u *copybuf, *wp; | |
3836 int some_error = FALSE; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
3837 stat_T st_new; |
7 | 3838 char_u *dirp; |
3839 char_u *rootname; | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3840 #if defined(UNIX) |
7 | 3841 int did_set_shortname; |
3842 #endif | |
3843 | |
3844 copybuf = alloc(BUFSIZE + 1); | |
3845 if (copybuf == NULL) | |
3846 { | |
3847 some_error = TRUE; /* out of memory */ | |
3848 goto nobackup; | |
3849 } | |
3850 | |
3851 /* | |
3852 * Try to make the backup in each directory in the 'bdir' option. | |
3853 * | |
3854 * Unix semantics has it, that we may have a writable file, | |
3855 * that cannot be recreated with a simple open(..., O_CREAT, ) e.g: | |
3856 * - the directory is not writable, | |
3857 * - the file may be a symbolic link, | |
3858 * - the file may belong to another user/group, etc. | |
3859 * | |
3860 * For these reasons, the existing writable file must be truncated | |
3861 * and reused. Creation of a backup COPY will be attempted. | |
3862 */ | |
3863 dirp = p_bdir; | |
3864 while (*dirp) | |
3865 { | |
3866 #ifdef UNIX | |
3867 st_new.st_ino = 0; | |
3868 st_new.st_dev = 0; | |
3869 st_new.st_gid = 0; | |
3870 #endif | |
3871 | |
3872 /* | |
3873 * Isolate one directory name, using an entry in 'bdir'. | |
3874 */ | |
3875 (void)copy_option_part(&dirp, copybuf, BUFSIZE, ","); | |
3876 rootname = get_file_in_dir(fname, copybuf); | |
3877 if (rootname == NULL) | |
3878 { | |
3879 some_error = TRUE; /* out of memory */ | |
3880 goto nobackup; | |
3881 } | |
3882 | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3883 #if defined(UNIX) |
7 | 3884 did_set_shortname = FALSE; |
3885 #endif | |
3886 | |
3887 /* | |
3888 * May try twice if 'shortname' not set. | |
3889 */ | |
3890 for (;;) | |
3891 { | |
3892 /* | |
3893 * Make backup file name. | |
3894 */ | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3895 backup = buf_modname((buf->b_p_sn || buf->b_shortname), |
7 | 3896 rootname, backup_ext, FALSE); |
3897 if (backup == NULL) | |
3898 { | |
3899 vim_free(rootname); | |
3900 some_error = TRUE; /* out of memory */ | |
3901 goto nobackup; | |
3902 } | |
3903 | |
3904 /* | |
3905 * Check if backup file already exists. | |
3906 */ | |
3907 if (mch_stat((char *)backup, &st_new) >= 0) | |
3908 { | |
3909 #ifdef UNIX | |
3910 /* | |
3911 * Check if backup file is same as original file. | |
3912 * May happen when modname() gave the same file back. | |
3913 * E.g. silly link, or file name-length reached. | |
3914 * If we don't check here, we either ruin the file | |
3915 * when copying or erase it after writing. jw. | |
3916 */ | |
3917 if (st_new.st_dev == st_old.st_dev | |
3918 && st_new.st_ino == st_old.st_ino) | |
3919 { | |
3920 vim_free(backup); | |
3921 backup = NULL; /* no backup file to delete */ | |
3922 /* | |
3923 * may try again with 'shortname' set | |
3924 */ | |
3925 if (!(buf->b_shortname || buf->b_p_sn)) | |
3926 { | |
3927 buf->b_shortname = TRUE; | |
3928 did_set_shortname = TRUE; | |
3929 continue; | |
3930 } | |
3931 /* setting shortname didn't help */ | |
3932 if (did_set_shortname) | |
3933 buf->b_shortname = FALSE; | |
3934 break; | |
3935 } | |
3936 #endif | |
3937 | |
3938 /* | |
3939 * If we are not going to keep the backup file, don't | |
3940 * delete an existing one, try to use another name. | |
3941 * Change one character, just before the extension. | |
3942 */ | |
3943 if (!p_bk) | |
3944 { | |
3945 wp = backup + STRLEN(backup) - 1 | |
3946 - STRLEN(backup_ext); | |
3947 if (wp < backup) /* empty file name ??? */ | |
3948 wp = backup; | |
3949 *wp = 'z'; | |
3950 while (*wp > 'a' | |
3951 && mch_stat((char *)backup, &st_new) >= 0) | |
3952 --*wp; | |
3953 /* They all exist??? Must be something wrong. */ | |
3954 if (*wp == 'a') | |
3955 { | |
3956 vim_free(backup); | |
3957 backup = NULL; | |
3958 } | |
3959 } | |
3960 } | |
3961 break; | |
3962 } | |
3963 vim_free(rootname); | |
3964 | |
3965 /* | |
3966 * Try to create the backup file | |
3967 */ | |
3968 if (backup != NULL) | |
3969 { | |
3970 /* remove old backup, if present */ | |
3971 mch_remove(backup); | |
3972 /* Open with O_EXCL to avoid the file being created while | |
3973 * we were sleeping (symlink hacker attack?) */ | |
3974 bfd = mch_open((char *)backup, | |
557 | 3975 O_WRONLY|O_CREAT|O_EXTRA|O_EXCL|O_NOFOLLOW, |
3976 perm & 0777); | |
7 | 3977 if (bfd < 0) |
3978 { | |
3979 vim_free(backup); | |
3980 backup = NULL; | |
3981 } | |
3982 else | |
3983 { | |
3984 /* set file protection same as original file, but | |
3985 * strip s-bit */ | |
3986 (void)mch_setperm(backup, perm & 0777); | |
3987 | |
3988 #ifdef UNIX | |
3989 /* | |
3990 * Try to set the group of the backup same as the | |
3991 * original file. If this fails, set the protection | |
3992 * bits for the group same as the protection bits for | |
3993 * others. | |
3994 */ | |
557 | 3995 if (st_new.st_gid != st_old.st_gid |
7 | 3996 # ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */ |
557 | 3997 && fchown(bfd, (uid_t)-1, st_old.st_gid) != 0 |
7 | 3998 # endif |
3999 ) | |
4000 mch_setperm(backup, | |
4001 (perm & 0707) | ((perm & 07) << 3)); | |
5788 | 4002 # if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
1583 | 4003 mch_copy_sec(fname, backup); |
4004 # endif | |
7 | 4005 #endif |
4006 | |
4007 /* | |
4008 * copy the file. | |
4009 */ | |
4010 write_info.bw_fd = bfd; | |
4011 write_info.bw_buf = copybuf; | |
4012 #ifdef HAS_BW_FLAGS | |
4013 write_info.bw_flags = FIO_NOCONVERT; | |
4014 #endif | |
2664 | 4015 while ((write_info.bw_len = read_eintr(fd, copybuf, |
7 | 4016 BUFSIZE)) > 0) |
4017 { | |
4018 if (buf_write_bytes(&write_info) == FAIL) | |
4019 { | |
4020 errmsg = (char_u *)_("E506: Can't write to backup file (add ! to override)"); | |
4021 break; | |
4022 } | |
4023 ui_breakcheck(); | |
4024 if (got_int) | |
4025 { | |
4026 errmsg = (char_u *)_(e_interr); | |
4027 break; | |
4028 } | |
4029 } | |
4030 | |
4031 if (close(bfd) < 0 && errmsg == NULL) | |
4032 errmsg = (char_u *)_("E507: Close error for backup file (add ! to override)"); | |
4033 if (write_info.bw_len < 0) | |
4034 errmsg = (char_u *)_("E508: Can't read file for backup (add ! to override)"); | |
4035 #ifdef UNIX | |
4036 set_file_time(backup, st_old.st_atime, st_old.st_mtime); | |
4037 #endif | |
4038 #ifdef HAVE_ACL | |
4039 mch_set_acl(backup, acl); | |
4040 #endif | |
5788 | 4041 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
1583 | 4042 mch_copy_sec(fname, backup); |
4043 #endif | |
7 | 4044 break; |
4045 } | |
4046 } | |
4047 } | |
4048 nobackup: | |
4049 close(fd); /* ignore errors for closing read file */ | |
4050 vim_free(copybuf); | |
4051 | |
4052 if (backup == NULL && errmsg == NULL) | |
4053 errmsg = (char_u *)_("E509: Cannot create backup file (add ! to override)"); | |
4054 /* ignore errors when forceit is TRUE */ | |
4055 if ((some_error || errmsg != NULL) && !forceit) | |
4056 { | |
4057 retval = FAIL; | |
4058 goto fail; | |
4059 } | |
4060 errmsg = NULL; | |
4061 } | |
4062 else | |
4063 { | |
4064 char_u *dirp; | |
4065 char_u *p; | |
4066 char_u *rootname; | |
4067 | |
4068 /* | |
4069 * Make a backup by renaming the original file. | |
4070 */ | |
4071 /* | |
4072 * If 'cpoptions' includes the "W" flag, we don't want to | |
4073 * overwrite a read-only file. But rename may be possible | |
4074 * anyway, thus we need an extra check here. | |
4075 */ | |
4076 if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL) | |
4077 { | |
4078 errnum = (char_u *)"E504: "; | |
4079 errmsg = (char_u *)_(err_readonly); | |
4080 goto fail; | |
4081 } | |
4082 | |
4083 /* | |
4084 * | |
4085 * Form the backup file name - change path/fo.o.h to | |
4086 * path/fo.o.h.bak Try all directories in 'backupdir', first one | |
4087 * that works is used. | |
4088 */ | |
4089 dirp = p_bdir; | |
4090 while (*dirp) | |
4091 { | |
4092 /* | |
4093 * Isolate one directory name and make the backup file name. | |
4094 */ | |
4095 (void)copy_option_part(&dirp, IObuff, IOSIZE, ","); | |
4096 rootname = get_file_in_dir(fname, IObuff); | |
4097 if (rootname == NULL) | |
4098 backup = NULL; | |
4099 else | |
4100 { | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
4101 backup = buf_modname((buf->b_p_sn || buf->b_shortname), |
7 | 4102 rootname, backup_ext, FALSE); |
4103 vim_free(rootname); | |
4104 } | |
4105 | |
4106 if (backup != NULL) | |
4107 { | |
4108 /* | |
4109 * If we are not going to keep the backup file, don't | |
4110 * delete an existing one, try to use another name. | |
4111 * Change one character, just before the extension. | |
4112 */ | |
4113 if (!p_bk && mch_getperm(backup) >= 0) | |
4114 { | |
4115 p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext); | |
4116 if (p < backup) /* empty file name ??? */ | |
4117 p = backup; | |
4118 *p = 'z'; | |
4119 while (*p > 'a' && mch_getperm(backup) >= 0) | |
4120 --*p; | |
4121 /* They all exist??? Must be something wrong! */ | |
4122 if (*p == 'a') | |
4123 { | |
4124 vim_free(backup); | |
4125 backup = NULL; | |
4126 } | |
4127 } | |
4128 } | |
4129 if (backup != NULL) | |
4130 { | |
4131 /* | |
531 | 4132 * Delete any existing backup and move the current version |
4133 * to the backup. For safety, we don't remove the backup | |
4134 * until the write has finished successfully. And if the | |
4135 * 'backup' option is set, leave it around. | |
7 | 4136 */ |
4137 /* | |
4138 * If the renaming of the original file to the backup file | |
4139 * works, quit here. | |
4140 */ | |
4141 if (vim_rename(fname, backup) == 0) | |
4142 break; | |
4143 | |
4144 vim_free(backup); /* don't do the rename below */ | |
4145 backup = NULL; | |
4146 } | |
4147 } | |
4148 if (backup == NULL && !forceit) | |
4149 { | |
4150 errmsg = (char_u *)_("E510: Can't make backup file (add ! to override)"); | |
4151 goto fail; | |
4152 } | |
4153 } | |
4154 } | |
4155 | |
7410
08e62c4fc17d
commit https://github.com/vim/vim/commit/53076830fea6df737455523f7e235bfe4f79864d
Christian Brabandt <cb@256bit.org>
parents:
7408
diff
changeset
|
4156 #if defined(UNIX) |
7 | 4157 /* When using ":w!" and the file was read-only: make it writable */ |
4158 if (forceit && perm >= 0 && !(perm & 0200) && st_old.st_uid == getuid() | |
4159 && vim_strchr(p_cpo, CPO_FWRITE) == NULL) | |
4160 { | |
4161 perm |= 0200; | |
4162 (void)mch_setperm(fname, perm); | |
4163 made_writable = TRUE; | |
4164 } | |
4165 #endif | |
4166 | |
819 | 4167 /* When using ":w!" and writing to the current file, 'readonly' makes no |
164 | 4168 * sense, reset it, unless 'Z' appears in 'cpoptions'. */ |
4169 if (forceit && overwriting && vim_strchr(p_cpo, CPO_KEEPRO) == NULL) | |
7 | 4170 { |
4171 buf->b_p_ro = FALSE; | |
4172 #ifdef FEAT_TITLE | |
4173 need_maketitle = TRUE; /* set window title later */ | |
4174 #endif | |
4175 #ifdef FEAT_WINDOWS | |
4176 status_redraw_all(); /* redraw status lines later */ | |
4177 #endif | |
4178 } | |
4179 | |
4180 if (end > buf->b_ml.ml_line_count) | |
4181 end = buf->b_ml.ml_line_count; | |
4182 if (buf->b_ml.ml_flags & ML_EMPTY) | |
4183 start = end + 1; | |
4184 | |
4185 /* | |
4186 * If the original file is being overwritten, there is a small chance that | |
4187 * we crash in the middle of writing. Therefore the file is preserved now. | |
4188 * This makes all block numbers positive so that recovery does not need | |
4189 * the original file. | |
4190 * Don't do this if there is a backup file and we are exiting. | |
4191 */ | |
39 | 4192 if (reset_changed && !newfile && overwriting |
7 | 4193 && !(exiting && backup != NULL)) |
4194 { | |
4195 ml_preserve(buf, FALSE); | |
4196 if (got_int) | |
4197 { | |
4198 errmsg = (char_u *)_(e_interr); | |
4199 goto restore_backup; | |
4200 } | |
4201 } | |
4202 | |
4203 #ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */ | |
4204 /* | |
4205 * Before risking to lose the original file verify if there's | |
4206 * a resource fork to preserve, and if cannot be done warn | |
4207 * the users. This happens when overwriting without backups. | |
4208 */ | |
4209 if (backup == NULL && overwriting && !append) | |
4210 if (mch_has_resource_fork(fname)) | |
4211 { | |
4212 errmsg = (char_u *)_("E460: The resource fork would be lost (add ! to override)"); | |
4213 goto restore_backup; | |
4214 } | |
4215 #endif | |
4216 | |
4217 #ifdef VMS | |
4218 vms_remove_version(fname); /* remove version */ | |
4219 #endif | |
1651 | 4220 /* Default: write the file directly. May write to a temp file for |
7 | 4221 * multi-byte conversion. */ |
4222 wfname = fname; | |
4223 | |
4224 #ifdef FEAT_MBYTE | |
4225 /* Check for forced 'fileencoding' from "++opt=val" argument. */ | |
4226 if (eap != NULL && eap->force_enc != 0) | |
4227 { | |
4228 fenc = eap->cmd + eap->force_enc; | |
4229 fenc = enc_canonize(fenc); | |
4230 fenc_tofree = fenc; | |
4231 } | |
4232 else | |
4233 fenc = buf->b_p_fenc; | |
4234 | |
4235 /* | |
1948 | 4236 * Check if the file needs to be converted. |
7 | 4237 */ |
1948 | 4238 converted = need_conversion(fenc); |
7 | 4239 |
4240 /* | |
4241 * Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done. Or | |
4242 * Latin1 to Unicode conversion. This is handled in buf_write_bytes(). | |
4243 * Prepare the flags for it and allocate bw_conv_buf when needed. | |
4244 */ | |
4245 if (converted && (enc_utf8 || STRCMP(p_enc, "latin1") == 0)) | |
4246 { | |
4247 wb_flags = get_fio_flags(fenc); | |
4248 if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8)) | |
4249 { | |
4250 /* Need to allocate a buffer to translate into. */ | |
4251 if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8)) | |
4252 write_info.bw_conv_buflen = bufsize * 2; | |
4253 else /* FIO_UCS4 */ | |
4254 write_info.bw_conv_buflen = bufsize * 4; | |
4255 write_info.bw_conv_buf | |
4256 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4257 if (write_info.bw_conv_buf == NULL) | |
4258 end = 0; | |
4259 } | |
4260 } | |
4261 | |
4262 # ifdef WIN3264 | |
4263 if (converted && wb_flags == 0 && (wb_flags = get_win_fio_flags(fenc)) != 0) | |
4264 { | |
4265 /* Convert UTF-8 -> UCS-2 and UCS-2 -> DBCS. Worst-case * 4: */ | |
4266 write_info.bw_conv_buflen = bufsize * 4; | |
4267 write_info.bw_conv_buf | |
4268 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4269 if (write_info.bw_conv_buf == NULL) | |
4270 end = 0; | |
4271 } | |
4272 # endif | |
4273 | |
4274 # ifdef MACOS_X | |
4275 if (converted && wb_flags == 0 && (wb_flags = get_mac_fio_flags(fenc)) != 0) | |
4276 { | |
4277 write_info.bw_conv_buflen = bufsize * 3; | |
4278 write_info.bw_conv_buf | |
4279 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4280 if (write_info.bw_conv_buf == NULL) | |
4281 end = 0; | |
4282 } | |
4283 # endif | |
4284 | |
4285 # if defined(FEAT_EVAL) || defined(USE_ICONV) | |
4286 if (converted && wb_flags == 0) | |
4287 { | |
4288 # ifdef USE_ICONV | |
4289 /* | |
4290 * Use iconv() conversion when conversion is needed and it's not done | |
4291 * internally. | |
4292 */ | |
4293 write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc, | |
4294 enc_utf8 ? (char_u *)"utf-8" : p_enc); | |
4295 if (write_info.bw_iconv_fd != (iconv_t)-1) | |
4296 { | |
4297 /* We're going to use iconv(), allocate a buffer to convert in. */ | |
4298 write_info.bw_conv_buflen = bufsize * ICONV_MULT; | |
4299 write_info.bw_conv_buf | |
4300 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4301 if (write_info.bw_conv_buf == NULL) | |
4302 end = 0; | |
4303 write_info.bw_first = TRUE; | |
4304 } | |
4305 # ifdef FEAT_EVAL | |
4306 else | |
4307 # endif | |
4308 # endif | |
4309 | |
4310 # ifdef FEAT_EVAL | |
4311 /* | |
4312 * When the file needs to be converted with 'charconvert' after | |
4313 * writing, write to a temp file instead and let the conversion | |
4314 * overwrite the original file. | |
4315 */ | |
4316 if (*p_ccv != NUL) | |
4317 { | |
6721 | 4318 wfname = vim_tempname('w', FALSE); |
7 | 4319 if (wfname == NULL) /* Can't write without a tempfile! */ |
4320 { | |
4321 errmsg = (char_u *)_("E214: Can't find temp file for writing"); | |
4322 goto restore_backup; | |
4323 } | |
4324 } | |
4325 # endif | |
4326 } | |
4327 # endif | |
4328 if (converted && wb_flags == 0 | |
4329 # ifdef USE_ICONV | |
4330 && write_info.bw_iconv_fd == (iconv_t)-1 | |
4331 # endif | |
4332 # ifdef FEAT_EVAL | |
4333 && wfname == fname | |
4334 # endif | |
4335 ) | |
4336 { | |
4337 if (!forceit) | |
4338 { | |
4339 errmsg = (char_u *)_("E213: Cannot convert (add ! to write without conversion)"); | |
4340 goto restore_backup; | |
4341 } | |
4342 notconverted = TRUE; | |
4343 } | |
4344 #endif | |
4345 | |
4346 /* | |
4347 * Open the file "wfname" for writing. | |
4348 * We may try to open the file twice: If we can't write to the | |
4349 * file and forceit is TRUE we delete the existing file and try to create | |
4350 * a new one. If this still fails we may have lost the original file! | |
4351 * (this may happen when the user reached his quotum for number of files). | |
4352 * Appending will fail if the file does not exist and forceit is FALSE. | |
4353 */ | |
4354 while ((fd = mch_open((char *)wfname, O_WRONLY | O_EXTRA | (append | |
4355 ? (forceit ? (O_APPEND | O_CREAT) : O_APPEND) | |
4356 : (O_CREAT | O_TRUNC)) | |
216 | 4357 , perm < 0 ? 0666 : (perm & 0777))) < 0) |
7 | 4358 { |
4359 /* | |
4360 * A forced write will try to create a new file if the old one is | |
4361 * still readonly. This may also happen when the directory is | |
4362 * read-only. In that case the mch_remove() will fail. | |
4363 */ | |
4364 if (errmsg == NULL) | |
4365 { | |
4366 #ifdef UNIX | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
4367 stat_T st; |
7 | 4368 |
4369 /* Don't delete the file when it's a hard or symbolic link. */ | |
4370 if ((!newfile && st_old.st_nlink > 1) | |
4371 || (mch_lstat((char *)fname, &st) == 0 | |
4372 && (st.st_dev != st_old.st_dev | |
4373 || st.st_ino != st_old.st_ino))) | |
4374 errmsg = (char_u *)_("E166: Can't open linked file for writing"); | |
4375 else | |
4376 #endif | |
4377 { | |
4378 errmsg = (char_u *)_("E212: Can't open file for writing"); | |
4379 if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL | |
4380 && perm >= 0) | |
4381 { | |
4382 #ifdef UNIX | |
4383 /* we write to the file, thus it should be marked | |
4384 writable after all */ | |
4385 if (!(perm & 0200)) | |
4386 made_writable = TRUE; | |
4387 perm |= 0200; | |
4388 if (st_old.st_uid != getuid() || st_old.st_gid != getgid()) | |
4389 perm &= 0777; | |
4390 #endif | |
4391 if (!append) /* don't remove when appending */ | |
4392 mch_remove(wfname); | |
4393 continue; | |
4394 } | |
4395 } | |
4396 } | |
4397 | |
4398 restore_backup: | |
4399 { | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
4400 stat_T st; |
7 | 4401 |
4402 /* | |
4403 * If we failed to open the file, we don't need a backup. Throw it | |
4404 * away. If we moved or removed the original file try to put the | |
4405 * backup in its place. | |
4406 */ | |
4407 if (backup != NULL && wfname == fname) | |
4408 { | |
4409 if (backup_copy) | |
4410 { | |
4411 /* | |
4412 * There is a small chance that we removed the original, | |
4413 * try to move the copy in its place. | |
4414 * This may not work if the vim_rename() fails. | |
4415 * In that case we leave the copy around. | |
4416 */ | |
4417 /* If file does not exist, put the copy in its place */ | |
4418 if (mch_stat((char *)fname, &st) < 0) | |
4419 vim_rename(backup, fname); | |
4420 /* if original file does exist throw away the copy */ | |
4421 if (mch_stat((char *)fname, &st) >= 0) | |
4422 mch_remove(backup); | |
4423 } | |
4424 else | |
4425 { | |
4426 /* try to put the original file back */ | |
4427 vim_rename(backup, fname); | |
4428 } | |
4429 } | |
4430 | |
4431 /* if original file no longer exists give an extra warning */ | |
4432 if (!newfile && mch_stat((char *)fname, &st) < 0) | |
4433 end = 0; | |
4434 } | |
4435 | |
4436 #ifdef FEAT_MBYTE | |
4437 if (wfname != fname) | |
4438 vim_free(wfname); | |
4439 #endif | |
4440 goto fail; | |
4441 } | |
4442 errmsg = NULL; | |
4443 | |
4444 #if defined(MACOS_CLASSIC) || defined(WIN3264) | |
4445 /* TODO: Is it need for MACOS_X? (Dany) */ | |
4446 /* | |
4447 * On macintosh copy the original files attributes (i.e. the backup) | |
1201 | 4448 * This is done in order to preserve the resource fork and the |
4449 * Finder attribute (label, comments, custom icons, file creator) | |
7 | 4450 */ |
4451 if (backup != NULL && overwriting && !append) | |
4452 { | |
4453 if (backup_copy) | |
4454 (void)mch_copy_file_attribute(wfname, backup); | |
4455 else | |
4456 (void)mch_copy_file_attribute(backup, wfname); | |
4457 } | |
4458 | |
4459 if (!overwriting && !append) | |
4460 { | |
4461 if (buf->b_ffname != NULL) | |
4462 (void)mch_copy_file_attribute(buf->b_ffname, wfname); | |
1201 | 4463 /* Should copy resource fork */ |
7 | 4464 } |
4465 #endif | |
4466 | |
4467 write_info.bw_fd = fd; | |
4468 | |
4469 #ifdef FEAT_CRYPT | |
2267 | 4470 if (*buf->b_p_key != NUL && !filtering) |
7 | 4471 { |
6122 | 4472 char_u *header; |
4473 int header_len; | |
4474 | |
4475 buf->b_cryptstate = crypt_create_for_writing(crypt_get_method_nr(buf), | |
4476 buf->b_p_key, &header, &header_len); | |
4477 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
|
4478 end = 0; |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
4479 else |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
4480 { |
6122 | 4481 /* Write magic number, so that Vim knows how this file is |
4482 * 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
|
4483 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
|
4484 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
|
4485 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
|
4486 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
|
4487 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
|
4488 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
|
4489 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
|
4490 } |
7 | 4491 } |
4492 #endif | |
4493 | |
4494 write_info.bw_buf = buffer; | |
4495 nchars = 0; | |
4496 | |
4497 /* use "++bin", "++nobin" or 'binary' */ | |
4498 if (eap != NULL && eap->force_bin != 0) | |
4499 write_bin = (eap->force_bin == FORCE_BIN); | |
4500 else | |
4501 write_bin = buf->b_p_bin; | |
4502 | |
4503 #ifdef FEAT_MBYTE | |
4504 /* | |
4505 * The BOM is written just after the encryption magic number. | |
24 | 4506 * Skip it when appending and the file already existed, the BOM only makes |
4507 * sense at the start of the file. | |
7 | 4508 */ |
24 | 4509 if (buf->b_p_bomb && !write_bin && (!append || perm < 0)) |
7 | 4510 { |
4511 write_info.bw_len = make_bom(buffer, fenc); | |
4512 if (write_info.bw_len > 0) | |
4513 { | |
4514 /* don't convert, do encryption */ | |
4515 write_info.bw_flags = FIO_NOCONVERT | wb_flags; | |
4516 if (buf_write_bytes(&write_info) == FAIL) | |
4517 end = 0; | |
4518 else | |
4519 nchars += write_info.bw_len; | |
4520 } | |
4521 } | |
1947 | 4522 write_info.bw_start_lnum = start; |
7 | 4523 #endif |
4524 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4525 #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
|
4526 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
|
4527 && !filtering && reset_changed); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4528 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
|
4529 /* 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
|
4530 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
|
4531 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4532 |
7 | 4533 write_info.bw_len = bufsize; |
4534 #ifdef HAS_BW_FLAGS | |
4535 write_info.bw_flags = wb_flags; | |
4536 #endif | |
4537 fileformat = get_fileformat_force(buf, eap); | |
4538 s = buffer; | |
4539 len = 0; | |
4540 for (lnum = start; lnum <= end; ++lnum) | |
4541 { | |
4542 /* | |
4543 * The next while loop is done once for each character written. | |
4544 * Keep it fast! | |
4545 */ | |
4546 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
|
4547 #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
|
4548 if (write_undo_file) |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
4549 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
|
4550 #endif |
7 | 4551 while ((c = *++ptr) != NUL) |
4552 { | |
4553 if (c == NL) | |
4554 *s = NUL; /* replace newlines with NULs */ | |
4555 else if (c == CAR && fileformat == EOL_MAC) | |
4556 *s = NL; /* Mac: replace CRs with NLs */ | |
4557 else | |
4558 *s = c; | |
4559 ++s; | |
4560 if (++len != bufsize) | |
4561 continue; | |
4562 if (buf_write_bytes(&write_info) == FAIL) | |
4563 { | |
4564 end = 0; /* write error: break loop */ | |
4565 break; | |
4566 } | |
4567 nchars += bufsize; | |
4568 s = buffer; | |
4569 len = 0; | |
1947 | 4570 #ifdef FEAT_MBYTE |
4571 write_info.bw_start_lnum = lnum; | |
4572 #endif | |
7 | 4573 } |
4574 /* write failed or last line has no EOL: stop here */ | |
4575 if (end == 0 | |
4576 || (lnum == end | |
6933 | 4577 && (write_bin || !buf->b_p_fixeol) |
2707 | 4578 && (lnum == buf->b_no_eol_lnum |
7 | 4579 || (lnum == buf->b_ml.ml_line_count && !buf->b_p_eol)))) |
4580 { | |
4581 ++lnum; /* written the line, count it */ | |
4582 no_eol = TRUE; | |
4583 break; | |
4584 } | |
4585 if (fileformat == EOL_UNIX) | |
4586 *s++ = NL; | |
4587 else | |
4588 { | |
4589 *s++ = CAR; /* EOL_MAC or EOL_DOS: write CR */ | |
4590 if (fileformat == EOL_DOS) /* write CR-NL */ | |
4591 { | |
4592 if (++len == bufsize) | |
4593 { | |
4594 if (buf_write_bytes(&write_info) == FAIL) | |
4595 { | |
4596 end = 0; /* write error: break loop */ | |
4597 break; | |
4598 } | |
4599 nchars += bufsize; | |
4600 s = buffer; | |
4601 len = 0; | |
4602 } | |
4603 *s++ = NL; | |
4604 } | |
4605 } | |
4606 if (++len == bufsize && end) | |
4607 { | |
4608 if (buf_write_bytes(&write_info) == FAIL) | |
4609 { | |
4610 end = 0; /* write error: break loop */ | |
4611 break; | |
4612 } | |
4613 nchars += bufsize; | |
4614 s = buffer; | |
4615 len = 0; | |
4616 | |
4617 ui_breakcheck(); | |
4618 if (got_int) | |
4619 { | |
4620 end = 0; /* Interrupted, break loop */ | |
4621 break; | |
4622 } | |
4623 } | |
4624 #ifdef VMS | |
4625 /* | |
4626 * On VMS there is a problem: newlines get added when writing blocks | |
4627 * at a time. Fix it by writing a line at a time. | |
4628 * This is much slower! | |
22 | 4629 * Explanation: VAX/DECC RTL insists that records in some RMS |
4630 * structures end with a newline (carriage return) character, and if | |
4631 * they don't it adds one. | |
4632 * With other RMS structures it works perfect without this fix. | |
7 | 4633 */ |
1431 | 4634 if (buf->b_fab_rfm == FAB$C_VFC |
4635 || ((buf->b_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0)) | |
22 | 4636 { |
4637 int b2write; | |
4638 | |
4639 buf->b_fab_mrs = (buf->b_fab_mrs == 0 | |
4640 ? MIN(4096, bufsize) | |
4641 : MIN(buf->b_fab_mrs, bufsize)); | |
4642 | |
4643 b2write = len; | |
4644 while (b2write > 0) | |
7 | 4645 { |
22 | 4646 write_info.bw_len = MIN(b2write, buf->b_fab_mrs); |
4647 if (buf_write_bytes(&write_info) == FAIL) | |
4648 { | |
4649 end = 0; | |
4650 break; | |
4651 } | |
4652 b2write -= MIN(b2write, buf->b_fab_mrs); | |
7 | 4653 } |
4654 write_info.bw_len = bufsize; | |
4655 nchars += len; | |
4656 s = buffer; | |
4657 len = 0; | |
4658 } | |
4659 #endif | |
4660 } | |
4661 if (len > 0 && end > 0) | |
4662 { | |
4663 write_info.bw_len = len; | |
4664 if (buf_write_bytes(&write_info) == FAIL) | |
4665 end = 0; /* write error */ | |
4666 nchars += len; | |
4667 } | |
4668 | |
4669 #if defined(UNIX) && defined(HAVE_FSYNC) | |
4670 /* On many journalling file systems there is a bug that causes both the | |
4671 * original and the backup file to be lost when halting the system right | |
4672 * after writing the file. That's because only the meta-data is | |
4673 * journalled. Syncing the file slows down the system, but assures it has | |
10 | 4674 * been written to disk and we don't lose it. |
4675 * For a device do try the fsync() but don't complain if it does not work | |
36 | 4676 * (could be a pipe). |
4677 * If the 'fsync' option is FALSE, don't fsync(). Useful for laptops. */ | |
4678 if (p_fs && fsync(fd) != 0 && !device) | |
7 | 4679 { |
4680 errmsg = (char_u *)_("E667: Fsync failed"); | |
4681 end = 0; | |
4682 } | |
4683 #endif | |
4684 | |
5788 | 4685 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
1583 | 4686 /* Probably need to set the security context. */ |
4687 if (!backup_copy) | |
4688 mch_copy_sec(backup, wfname); | |
4689 #endif | |
4690 | |
557 | 4691 #ifdef UNIX |
4692 /* When creating a new file, set its owner/group to that of the original | |
4693 * file. Get the new device and inode number. */ | |
4694 if (backup != NULL && !backup_copy) | |
4695 { | |
4696 # ifdef HAVE_FCHOWN | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
4697 stat_T st; |
557 | 4698 |
4699 /* don't change the owner when it's already OK, some systems remove | |
4700 * permission or ACL stuff */ | |
4701 if (mch_stat((char *)wfname, &st) < 0 | |
4702 || st.st_uid != st_old.st_uid | |
4703 || st.st_gid != st_old.st_gid) | |
4704 { | |
1757 | 4705 ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
557 | 4706 if (perm >= 0) /* set permission again, may have changed */ |
4707 (void)mch_setperm(wfname, perm); | |
4708 } | |
4709 # endif | |
4710 buf_setino(buf); | |
4711 } | |
1873 | 4712 else if (!buf->b_dev_valid) |
617 | 4713 /* Set the inode when creating a new file. */ |
4714 buf_setino(buf); | |
557 | 4715 #endif |
4716 | |
7 | 4717 if (close(fd) != 0) |
4718 { | |
4719 errmsg = (char_u *)_("E512: Close failed"); | |
4720 end = 0; | |
4721 } | |
4722 | |
4723 #ifdef UNIX | |
4724 if (made_writable) | |
4725 perm &= ~0200; /* reset 'w' bit for security reasons */ | |
4726 #endif | |
4727 if (perm >= 0) /* set perm. of new file same as old file */ | |
4728 (void)mch_setperm(wfname, perm); | |
4729 #ifdef HAVE_ACL | |
9509
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
4730 /* |
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
4731 * 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
|
4732 * 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
|
4733 * 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
|
4734 * 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
|
4735 * 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
|
4736 * 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
|
4737 */ |
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
4738 # ifndef HAVE_SOLARIS_ZFS_ACL |
7 | 4739 if (!backup_copy) |
9509
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
4740 # endif |
7 | 4741 mch_set_acl(wfname, acl); |
4742 #endif | |
2267 | 4743 #ifdef FEAT_CRYPT |
6122 | 4744 if (buf->b_cryptstate != NULL) |
4745 { | |
4746 crypt_free_state(buf->b_cryptstate); | |
4747 buf->b_cryptstate = NULL; | |
4748 } | |
4749 #endif | |
7 | 4750 |
4751 #if defined(FEAT_MBYTE) && defined(FEAT_EVAL) | |
4752 if (wfname != fname) | |
4753 { | |
4754 /* | |
4755 * The file was written to a temp file, now it needs to be converted | |
4756 * with 'charconvert' to (overwrite) the output file. | |
4757 */ | |
4758 if (end != 0) | |
4759 { | |
4760 if (eval_charconvert(enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc, | |
4761 wfname, fname) == FAIL) | |
4762 { | |
4763 write_info.bw_conv_error = TRUE; | |
4764 end = 0; | |
4765 } | |
4766 } | |
4767 mch_remove(wfname); | |
4768 vim_free(wfname); | |
4769 } | |
4770 #endif | |
4771 | |
4772 if (end == 0) | |
4773 { | |
4774 if (errmsg == NULL) | |
4775 { | |
4776 #ifdef FEAT_MBYTE | |
4777 if (write_info.bw_conv_error) | |
1947 | 4778 { |
4779 if (write_info.bw_conv_error_lnum == 0) | |
4780 errmsg = (char_u *)_("E513: write error, conversion failed (make 'fenc' empty to override)"); | |
4781 else | |
4782 { | |
4783 errmsg_allocated = TRUE; | |
4784 errmsg = alloc(300); | |
4785 vim_snprintf((char *)errmsg, 300, _("E513: write error, conversion failed in line %ld (make 'fenc' empty to override)"), | |
4786 (long)write_info.bw_conv_error_lnum); | |
4787 } | |
4788 } | |
7 | 4789 else |
4790 #endif | |
4791 if (got_int) | |
4792 errmsg = (char_u *)_(e_interr); | |
4793 else | |
4794 errmsg = (char_u *)_("E514: write error (file system full?)"); | |
4795 } | |
4796 | |
4797 /* | |
4798 * If we have a backup file, try to put it in place of the new file, | |
1698 | 4799 * because the new file is probably corrupt. This avoids losing the |
7 | 4800 * original file when trying to make a backup when writing the file a |
4801 * second time. | |
4802 * When "backup_copy" is set we need to copy the backup over the new | |
4803 * file. Otherwise rename the backup file. | |
4804 * If this is OK, don't give the extra warning message. | |
4805 */ | |
4806 if (backup != NULL) | |
4807 { | |
4808 if (backup_copy) | |
4809 { | |
4810 /* This may take a while, if we were interrupted let the user | |
4811 * know we got the message. */ | |
4812 if (got_int) | |
4813 { | |
4814 MSG(_(e_interr)); | |
4815 out_flush(); | |
4816 } | |
4817 if ((fd = mch_open((char *)backup, O_RDONLY | O_EXTRA, 0)) >= 0) | |
4818 { | |
4819 if ((write_info.bw_fd = mch_open((char *)fname, | |
194 | 4820 O_WRONLY | O_CREAT | O_TRUNC | O_EXTRA, |
4821 perm & 0777)) >= 0) | |
7 | 4822 { |
4823 /* copy the file. */ | |
4824 write_info.bw_buf = smallbuf; | |
4825 #ifdef HAS_BW_FLAGS | |
4826 write_info.bw_flags = FIO_NOCONVERT; | |
4827 #endif | |
2664 | 4828 while ((write_info.bw_len = read_eintr(fd, smallbuf, |
7 | 4829 SMBUFSIZE)) > 0) |
4830 if (buf_write_bytes(&write_info) == FAIL) | |
4831 break; | |
4832 | |
4833 if (close(write_info.bw_fd) >= 0 | |
4834 && write_info.bw_len == 0) | |
4835 end = 1; /* success */ | |
4836 } | |
4837 close(fd); /* ignore errors for closing read file */ | |
4838 } | |
4839 } | |
4840 else | |
4841 { | |
4842 if (vim_rename(backup, fname) == 0) | |
4843 end = 1; | |
4844 } | |
4845 } | |
4846 goto fail; | |
4847 } | |
4848 | |
4849 lnum -= start; /* compute number of written lines */ | |
4850 --no_wait_return; /* may wait for return now */ | |
4851 | |
4852 #if !(defined(UNIX) || defined(VMS)) | |
4853 fname = sfname; /* use shortname now, for the messages */ | |
4854 #endif | |
4855 if (!filtering) | |
4856 { | |
4857 msg_add_fname(buf, fname); /* put fname in IObuff with quotes */ | |
4858 c = FALSE; | |
4859 #ifdef FEAT_MBYTE | |
4860 if (write_info.bw_conv_error) | |
4861 { | |
4862 STRCAT(IObuff, _(" CONVERSION ERROR")); | |
4863 c = TRUE; | |
1947 | 4864 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
|
4865 vim_snprintf_add((char *)IObuff, IOSIZE, _(" in line %ld;"), |
1947 | 4866 (long)write_info.bw_conv_error_lnum); |
7 | 4867 } |
4868 else if (notconverted) | |
4869 { | |
4870 STRCAT(IObuff, _("[NOT converted]")); | |
4871 c = TRUE; | |
4872 } | |
4873 else if (converted) | |
4874 { | |
4875 STRCAT(IObuff, _("[converted]")); | |
4876 c = TRUE; | |
4877 } | |
4878 #endif | |
4879 if (device) | |
4880 { | |
4881 STRCAT(IObuff, _("[Device]")); | |
4882 c = TRUE; | |
4883 } | |
4884 else if (newfile) | |
4885 { | |
4886 STRCAT(IObuff, shortmess(SHM_NEW) ? _("[New]") : _("[New File]")); | |
4887 c = TRUE; | |
4888 } | |
4889 if (no_eol) | |
4890 { | |
4891 msg_add_eol(); | |
4892 c = TRUE; | |
4893 } | |
4894 /* may add [unix/dos/mac] */ | |
4895 if (msg_add_fileformat(fileformat)) | |
4896 c = TRUE; | |
4897 #ifdef FEAT_CRYPT | |
4898 if (wb_flags & FIO_ENCRYPTED) | |
4899 { | |
6122 | 4900 crypt_append_msg(buf); |
7 | 4901 c = TRUE; |
4902 } | |
4903 #endif | |
4904 msg_add_lines(c, (long)lnum, nchars); /* add line/char count */ | |
4905 if (!shortmess(SHM_WRITE)) | |
4906 { | |
4907 if (append) | |
4908 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [a]") : _(" appended")); | |
4909 else | |
4910 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [w]") : _(" written")); | |
4911 } | |
4912 | |
679 | 4913 set_keep_msg(msg_trunc_attr(IObuff, FALSE, 0), 0); |
7 | 4914 } |
4915 | |
39 | 4916 /* When written everything correctly: reset 'modified'. Unless not |
4917 * writing to the original file and '+' is not in 'cpoptions'. */ | |
589 | 4918 if (reset_changed && whole && !append |
7 | 4919 #ifdef FEAT_MBYTE |
4920 && !write_info.bw_conv_error | |
4921 #endif | |
39 | 4922 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL) |
4923 ) | |
7 | 4924 { |
4925 unchanged(buf, TRUE); | |
6320 | 4926 #ifdef FEAT_AUTOCMD |
10952
835604f3c37a
patch 8.0.0365: might free a dict item that wasn't allocated
Christian Brabandt <cb@256bit.org>
parents:
10889
diff
changeset
|
4927 /* b:changedtick is always incremented in unchanged() but that |
6320 | 4928 * should not trigger a TextChanged event. */ |
10952
835604f3c37a
patch 8.0.0365: might free a dict item that wasn't allocated
Christian Brabandt <cb@256bit.org>
parents:
10889
diff
changeset
|
4929 if (last_changedtick + 1 == CHANGEDTICK(buf) |
6320 | 4930 && last_changedtick_buf == buf) |
10952
835604f3c37a
patch 8.0.0365: might free a dict item that wasn't allocated
Christian Brabandt <cb@256bit.org>
parents:
10889
diff
changeset
|
4931 last_changedtick = CHANGEDTICK(buf); |
6320 | 4932 #endif |
7 | 4933 u_unchanged(buf); |
2281
e41433ea71df
Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents:
2280
diff
changeset
|
4934 u_update_save_nr(buf); |
7 | 4935 } |
4936 | |
4937 /* | |
4938 * If written to the current file, update the timestamp of the swap file | |
4939 * and reset the BF_WRITE_MASK flags. Also sets buf->b_mtime. | |
4940 */ | |
4941 if (overwriting) | |
4942 { | |
4943 ml_timestamp(buf); | |
589 | 4944 if (append) |
4945 buf->b_flags &= ~BF_NEW; | |
4946 else | |
4947 buf->b_flags &= ~BF_WRITE_MASK; | |
7 | 4948 } |
4949 | |
4950 /* | |
4951 * If we kept a backup until now, and we are in patch mode, then we make | |
4952 * the backup file our 'original' file. | |
4953 */ | |
4954 if (*p_pm && dobackup) | |
4955 { | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
4956 char *org = (char *)buf_modname((buf->b_p_sn || buf->b_shortname), |
7 | 4957 fname, p_pm, FALSE); |
4958 | |
4959 if (backup != NULL) | |
4960 { | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
4961 stat_T st; |
7 | 4962 |
4963 /* | |
4964 * If the original file does not exist yet | |
4965 * the current backup file becomes the original file | |
4966 */ | |
4967 if (org == NULL) | |
4968 EMSG(_("E205: Patchmode: can't save original file")); | |
4969 else if (mch_stat(org, &st) < 0) | |
4970 { | |
4971 vim_rename(backup, (char_u *)org); | |
4972 vim_free(backup); /* don't delete the file */ | |
4973 backup = NULL; | |
4974 #ifdef UNIX | |
4975 set_file_time((char_u *)org, st_old.st_atime, st_old.st_mtime); | |
4976 #endif | |
4977 } | |
4978 } | |
4979 /* | |
4980 * If there is no backup file, remember that a (new) file was | |
4981 * created. | |
4982 */ | |
4983 else | |
4984 { | |
4985 int empty_fd; | |
4986 | |
4987 if (org == NULL | |
557 | 4988 || (empty_fd = mch_open(org, |
4989 O_CREAT | O_EXTRA | O_EXCL | O_NOFOLLOW, | |
216 | 4990 perm < 0 ? 0666 : (perm & 0777))) < 0) |
7 | 4991 EMSG(_("E206: patchmode: can't touch empty original file")); |
4992 else | |
4993 close(empty_fd); | |
4994 } | |
4995 if (org != NULL) | |
4996 { | |
4997 mch_setperm((char_u *)org, mch_getperm(fname) & 0777); | |
4998 vim_free(org); | |
4999 } | |
5000 } | |
5001 | |
5002 /* | |
5003 * Remove the backup unless 'backup' option is set | |
5004 */ | |
5005 if (!p_bk && backup != NULL && mch_remove(backup) != 0) | |
5006 EMSG(_("E207: Can't delete backup file")); | |
5007 | |
5008 #ifdef FEAT_SUN_WORKSHOP | |
5009 if (usingSunWorkShop) | |
5010 workshop_file_saved((char *) ffname); | |
5011 #endif | |
5012 | |
5013 goto nofail; | |
5014 | |
5015 /* | |
5016 * Finish up. We get here either after failure or success. | |
5017 */ | |
5018 fail: | |
5019 --no_wait_return; /* may wait for return now */ | |
5020 nofail: | |
5021 | |
5022 /* Done saving, we accept changed buffer warnings again */ | |
5023 buf->b_saving = FALSE; | |
5024 | |
5025 vim_free(backup); | |
5026 if (buffer != smallbuf) | |
5027 vim_free(buffer); | |
5028 #ifdef FEAT_MBYTE | |
5029 vim_free(fenc_tofree); | |
5030 vim_free(write_info.bw_conv_buf); | |
5031 # ifdef USE_ICONV | |
5032 if (write_info.bw_iconv_fd != (iconv_t)-1) | |
5033 { | |
5034 iconv_close(write_info.bw_iconv_fd); | |
5035 write_info.bw_iconv_fd = (iconv_t)-1; | |
5036 } | |
5037 # endif | |
5038 #endif | |
5039 #ifdef HAVE_ACL | |
5040 mch_free_acl(acl); | |
5041 #endif | |
5042 | |
5043 if (errmsg != NULL) | |
5044 { | |
835 | 5045 int numlen = errnum != NULL ? (int)STRLEN(errnum) : 0; |
7 | 5046 |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
5047 attr = HL_ATTR(HLF_E); /* set highlight for error messages */ |
7 | 5048 msg_add_fname(buf, |
5049 #ifndef UNIX | |
5050 sfname | |
5051 #else | |
5052 fname | |
5053 #endif | |
5054 ); /* put file name in IObuff with quotes */ | |
5055 if (STRLEN(IObuff) + STRLEN(errmsg) + numlen >= IOSIZE) | |
5056 IObuff[IOSIZE - STRLEN(errmsg) - numlen - 1] = NUL; | |
5057 /* If the error message has the form "is ...", put the error number in | |
5058 * front of the file name. */ | |
5059 if (errnum != NULL) | |
5060 { | |
1620 | 5061 STRMOVE(IObuff + numlen, IObuff); |
7 | 5062 mch_memmove(IObuff, errnum, (size_t)numlen); |
5063 } | |
5064 STRCAT(IObuff, errmsg); | |
5065 emsg(IObuff); | |
1947 | 5066 if (errmsg_allocated) |
5067 vim_free(errmsg); | |
7 | 5068 |
5069 retval = FAIL; | |
5070 if (end == 0) | |
5071 { | |
5072 MSG_PUTS_ATTR(_("\nWARNING: Original file may be lost or damaged\n"), | |
5073 attr | MSG_HIST); | |
5074 MSG_PUTS_ATTR(_("don't quit the editor until the file is successfully written!"), | |
5075 attr | MSG_HIST); | |
5076 | |
5077 /* Update the timestamp to avoid an "overwrite changed file" | |
5078 * prompt when writing again. */ | |
5079 if (mch_stat((char *)fname, &st_old) >= 0) | |
5080 { | |
5081 buf_store_time(buf, &st_old, fname); | |
5082 buf->b_mtime_read = buf->b_mtime; | |
5083 } | |
5084 } | |
5085 } | |
5086 msg_scroll = msg_save; | |
5087 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5088 #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
|
5089 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5090 * 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
|
5091 * file. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5092 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5093 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
|
5094 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5095 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
|
5096 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5097 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
|
5098 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
|
5099 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5100 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5101 |
7 | 5102 #ifdef FEAT_AUTOCMD |
5103 #ifdef FEAT_EVAL | |
5104 if (!should_abort(retval)) | |
5105 #else | |
5106 if (!got_int) | |
5107 #endif | |
5108 { | |
5109 aco_save_T aco; | |
5110 | |
3482 | 5111 curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */ |
5112 | |
7 | 5113 /* |
5114 * Apply POST autocommands. | |
5115 * Careful: The autocommands may call buf_write() recursively! | |
5116 */ | |
5117 aucmd_prepbuf(&aco, buf); | |
5118 | |
5119 if (append) | |
5120 apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname, | |
5121 FALSE, curbuf, eap); | |
5122 else if (filtering) | |
5123 apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname, | |
5124 FALSE, curbuf, eap); | |
5125 else if (reset_changed && whole) | |
5126 apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname, | |
5127 FALSE, curbuf, eap); | |
5128 else | |
5129 apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname, | |
5130 FALSE, curbuf, eap); | |
5131 | |
5132 /* restore curwin/curbuf and a few other things */ | |
5133 aucmd_restbuf(&aco); | |
5134 | |
5135 #ifdef FEAT_EVAL | |
5136 if (aborting()) /* autocmds may abort script processing */ | |
5137 retval = FALSE; | |
5138 #endif | |
5139 } | |
5140 #endif | |
5141 | |
5142 got_int |= prev_got_int; | |
5143 | |
5144 #ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */ | |
5145 /* Update machine specific information. */ | |
5146 mch_post_buffer_write(buf); | |
5147 #endif | |
5148 return retval; | |
5149 } | |
5150 | |
5151 /* | |
633 | 5152 * Set the name of the current buffer. Use when the buffer doesn't have a |
5153 * name and a ":r" or ":w" command with a file name is used. | |
5154 */ | |
5155 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5156 set_rw_fname(char_u *fname, char_u *sfname) |
633 | 5157 { |
5158 #ifdef FEAT_AUTOCMD | |
1905 | 5159 buf_T *buf = curbuf; |
5160 | |
633 | 5161 /* It's like the unnamed buffer is deleted.... */ |
5162 if (curbuf->b_p_bl) | |
5163 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); | |
5164 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf); | |
5165 # ifdef FEAT_EVAL | |
5166 if (aborting()) /* autocmds may abort script processing */ | |
5167 return FAIL; | |
5168 # endif | |
1905 | 5169 if (curbuf != buf) |
5170 { | |
5171 /* We are in another buffer now, don't do the renaming. */ | |
5172 EMSG(_(e_auchangedbuf)); | |
5173 return FAIL; | |
5174 } | |
633 | 5175 #endif |
5176 | |
5177 if (setfname(curbuf, fname, sfname, FALSE) == OK) | |
5178 curbuf->b_flags |= BF_NOTEDITED; | |
5179 | |
5180 #ifdef FEAT_AUTOCMD | |
5181 /* ....and a new named one is created */ | |
5182 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf); | |
5183 if (curbuf->b_p_bl) | |
5184 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf); | |
5185 # ifdef FEAT_EVAL | |
5186 if (aborting()) /* autocmds may abort script processing */ | |
5187 return FAIL; | |
5188 # endif | |
5189 | |
5190 /* Do filetype detection now if 'filetype' is empty. */ | |
5191 if (*curbuf->b_p_ft == NUL) | |
5192 { | |
819 | 5193 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
|
5194 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL); |
717 | 5195 do_modelines(0); |
633 | 5196 } |
5197 #endif | |
5198 | |
5199 return OK; | |
5200 } | |
5201 | |
5202 /* | |
7 | 5203 * Put file name into IObuff with quotes. |
5204 */ | |
33 | 5205 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5206 msg_add_fname(buf_T *buf, char_u *fname) |
7 | 5207 { |
5208 if (fname == NULL) | |
5209 fname = (char_u *)"-stdin-"; | |
5210 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE); | |
5211 IObuff[0] = '"'; | |
5212 STRCAT(IObuff, "\" "); | |
5213 } | |
5214 | |
5215 /* | |
5216 * Append message for text mode to IObuff. | |
5217 * Return TRUE if something appended. | |
5218 */ | |
5219 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5220 msg_add_fileformat(int eol_type) |
7 | 5221 { |
5222 #ifndef USE_CRNL | |
5223 if (eol_type == EOL_DOS) | |
5224 { | |
5225 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]")); | |
5226 return TRUE; | |
5227 } | |
5228 #endif | |
5229 #ifndef USE_CR | |
5230 if (eol_type == EOL_MAC) | |
5231 { | |
5232 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]")); | |
5233 return TRUE; | |
5234 } | |
5235 #endif | |
5236 #if defined(USE_CRNL) || defined(USE_CR) | |
5237 if (eol_type == EOL_UNIX) | |
5238 { | |
5239 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]")); | |
5240 return TRUE; | |
5241 } | |
5242 #endif | |
5243 return FALSE; | |
5244 } | |
5245 | |
5246 /* | |
5247 * Append line and character count to IObuff. | |
5248 */ | |
33 | 5249 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5250 msg_add_lines( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5251 int insert_space, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5252 long lnum, |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
5253 off_T nchars) |
7 | 5254 { |
5255 char_u *p; | |
5256 | |
5257 p = IObuff + STRLEN(IObuff); | |
5258 | |
5259 if (insert_space) | |
5260 *p++ = ' '; | |
5261 if (shortmess(SHM_LINES)) | |
9391
4c40631238e4
commit https://github.com/vim/vim/commit/bde9810d6103ffe3a22a9330021cb21db1ed1792
Christian Brabandt <cb@256bit.org>
parents:
9389
diff
changeset
|
5262 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
|
5263 "%ldL, %lldC", lnum, (varnumber_T)nchars); |
7 | 5264 else |
5265 { | |
5266 if (lnum == 1) | |
5267 STRCPY(p, _("1 line, ")); | |
5268 else | |
5269 sprintf((char *)p, _("%ld lines, "), lnum); | |
5270 p += STRLEN(p); | |
5271 if (nchars == 1) | |
5272 STRCPY(p, _("1 character")); | |
5273 else | |
9391
4c40631238e4
commit https://github.com/vim/vim/commit/bde9810d6103ffe3a22a9330021cb21db1ed1792
Christian Brabandt <cb@256bit.org>
parents:
9389
diff
changeset
|
5274 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
|
5275 _("%lld characters"), (varnumber_T)nchars); |
7 | 5276 } |
5277 } | |
5278 | |
5279 /* | |
5280 * Append message for missing line separator to IObuff. | |
5281 */ | |
5282 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5283 msg_add_eol(void) |
7 | 5284 { |
5285 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]")); | |
5286 } | |
5287 | |
5288 /* | |
5289 * Check modification time of file, before writing to it. | |
5290 * The size isn't checked, because using a tool like "gzip" takes care of | |
5291 * using the same timestamp but can't set the size. | |
5292 */ | |
5293 static int | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
5294 check_mtime(buf_T *buf, stat_T *st) |
7 | 5295 { |
5296 if (buf->b_mtime_read != 0 | |
5297 && time_differs((long)st->st_mtime, buf->b_mtime_read)) | |
5298 { | |
5299 msg_scroll = TRUE; /* don't overwrite messages here */ | |
5300 msg_silent = 0; /* must give this prompt */ | |
5301 /* don't use emsg() here, don't want to flush the buffers */ | |
5302 MSG_ATTR(_("WARNING: The file has been changed since reading it!!!"), | |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
5303 HL_ATTR(HLF_E)); |
7 | 5304 if (ask_yesno((char_u *)_("Do you really want to write to it"), |
5305 TRUE) == 'n') | |
5306 return FAIL; | |
5307 msg_scroll = FALSE; /* always overwrite the file message now */ | |
5308 } | |
5309 return OK; | |
5310 } | |
5311 | |
5312 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5313 time_differs(long t1, long t2) |
7 | 5314 { |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
5315 #if defined(__linux__) || defined(MSWIN) |
7 | 5316 /* On a FAT filesystem, esp. under Linux, there are only 5 bits to store |
5317 * the seconds. Since the roundoff is done when flushing the inode, the | |
5318 * time may change unexpectedly by one second!!! */ | |
5319 return (t1 - t2 > 1 || t2 - t1 > 1); | |
5320 #else | |
5321 return (t1 != t2); | |
5322 #endif | |
5323 } | |
5324 | |
5325 /* | |
5326 * Call write() to write a number of bytes to the file. | |
2664 | 5327 * Handles encryption and 'encoding' conversion. |
7 | 5328 * |
5329 * Return FAIL for failure, OK otherwise. | |
5330 */ | |
5331 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5332 buf_write_bytes(struct bw_info *ip) |
7 | 5333 { |
5334 int wlen; | |
5335 char_u *buf = ip->bw_buf; /* data to write */ | |
5336 int len = ip->bw_len; /* length of data */ | |
5337 #ifdef HAS_BW_FLAGS | |
5338 int flags = ip->bw_flags; /* extra flags */ | |
5339 #endif | |
5340 | |
5341 #ifdef FEAT_MBYTE | |
5342 /* | |
5343 * Skip conversion when writing the crypt magic number or the BOM. | |
5344 */ | |
5345 if (!(flags & FIO_NOCONVERT)) | |
5346 { | |
5347 char_u *p; | |
5348 unsigned c; | |
5349 int n; | |
5350 | |
5351 if (flags & FIO_UTF8) | |
5352 { | |
5353 /* | |
5354 * Convert latin1 in the buffer to UTF-8 in the file. | |
5355 */ | |
5356 p = ip->bw_conv_buf; /* translate to buffer */ | |
5357 for (wlen = 0; wlen < len; ++wlen) | |
5358 p += utf_char2bytes(buf[wlen], p); | |
5359 buf = ip->bw_conv_buf; | |
5360 len = (int)(p - ip->bw_conv_buf); | |
5361 } | |
5362 else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1)) | |
5363 { | |
5364 /* | |
5365 * Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or | |
5366 * Latin1 chars in the file. | |
5367 */ | |
5368 if (flags & FIO_LATIN1) | |
5369 p = buf; /* translate in-place (can only get shorter) */ | |
5370 else | |
5371 p = ip->bw_conv_buf; /* translate to buffer */ | |
5372 for (wlen = 0; wlen < len; wlen += n) | |
5373 { | |
5374 if (wlen == 0 && ip->bw_restlen != 0) | |
5375 { | |
5376 int l; | |
5377 | |
5378 /* Use remainder of previous call. Append the start of | |
5379 * buf[] to get a full sequence. Might still be too | |
5380 * short! */ | |
5381 l = CONV_RESTLEN - ip->bw_restlen; | |
5382 if (l > len) | |
5383 l = len; | |
5384 mch_memmove(ip->bw_rest + ip->bw_restlen, buf, (size_t)l); | |
474 | 5385 n = utf_ptr2len_len(ip->bw_rest, ip->bw_restlen + l); |
7 | 5386 if (n > ip->bw_restlen + len) |
5387 { | |
5388 /* We have an incomplete byte sequence at the end to | |
5389 * be written. We can't convert it without the | |
5390 * remaining bytes. Keep them for the next call. */ | |
5391 if (ip->bw_restlen + len > CONV_RESTLEN) | |
5392 return FAIL; | |
5393 ip->bw_restlen += len; | |
5394 break; | |
5395 } | |
5396 if (n > 1) | |
5397 c = utf_ptr2char(ip->bw_rest); | |
5398 else | |
5399 c = ip->bw_rest[0]; | |
5400 if (n >= ip->bw_restlen) | |
5401 { | |
5402 n -= ip->bw_restlen; | |
5403 ip->bw_restlen = 0; | |
5404 } | |
5405 else | |
5406 { | |
5407 ip->bw_restlen -= n; | |
5408 mch_memmove(ip->bw_rest, ip->bw_rest + n, | |
5409 (size_t)ip->bw_restlen); | |
5410 n = 0; | |
5411 } | |
5412 } | |
5413 else | |
5414 { | |
474 | 5415 n = utf_ptr2len_len(buf + wlen, len - wlen); |
7 | 5416 if (n > len - wlen) |
5417 { | |
5418 /* We have an incomplete byte sequence at the end to | |
5419 * be written. We can't convert it without the | |
5420 * remaining bytes. Keep them for the next call. */ | |
5421 if (len - wlen > CONV_RESTLEN) | |
5422 return FAIL; | |
5423 ip->bw_restlen = len - wlen; | |
5424 mch_memmove(ip->bw_rest, buf + wlen, | |
5425 (size_t)ip->bw_restlen); | |
5426 break; | |
5427 } | |
5428 if (n > 1) | |
5429 c = utf_ptr2char(buf + wlen); | |
5430 else | |
5431 c = buf[wlen]; | |
5432 } | |
5433 | |
1947 | 5434 if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error) |
5435 { | |
5436 ip->bw_conv_error = TRUE; | |
5437 ip->bw_conv_error_lnum = ip->bw_start_lnum; | |
5438 } | |
5439 if (c == NL) | |
5440 ++ip->bw_start_lnum; | |
7 | 5441 } |
5442 if (flags & FIO_LATIN1) | |
5443 len = (int)(p - buf); | |
5444 else | |
5445 { | |
5446 buf = ip->bw_conv_buf; | |
5447 len = (int)(p - ip->bw_conv_buf); | |
5448 } | |
5449 } | |
5450 | |
5451 # ifdef WIN3264 | |
5452 else if (flags & FIO_CODEPAGE) | |
5453 { | |
5454 /* | |
5455 * Convert UTF-8 or codepage to UCS-2 and then to MS-Windows | |
5456 * codepage. | |
5457 */ | |
5458 char_u *from; | |
5459 size_t fromlen; | |
5460 char_u *to; | |
5461 int u8c; | |
5462 BOOL bad = FALSE; | |
5463 int needed; | |
5464 | |
5465 if (ip->bw_restlen > 0) | |
5466 { | |
5467 /* Need to concatenate the remainder of the previous call and | |
5468 * the bytes of the current call. Use the end of the | |
5469 * conversion buffer for this. */ | |
5470 fromlen = len + ip->bw_restlen; | |
5471 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; | |
5472 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); | |
5473 mch_memmove(from + ip->bw_restlen, buf, (size_t)len); | |
5474 } | |
5475 else | |
5476 { | |
5477 from = buf; | |
5478 fromlen = len; | |
5479 } | |
5480 | |
5481 to = ip->bw_conv_buf; | |
5482 if (enc_utf8) | |
5483 { | |
5484 /* Convert from UTF-8 to UCS-2, to the start of the buffer. | |
5485 * The buffer has been allocated to be big enough. */ | |
5486 while (fromlen > 0) | |
5487 { | |
835 | 5488 n = (int)utf_ptr2len_len(from, (int)fromlen); |
7 | 5489 if (n > (int)fromlen) /* incomplete byte sequence */ |
5490 break; | |
5491 u8c = utf_ptr2char(from); | |
5492 *to++ = (u8c & 0xff); | |
5493 *to++ = (u8c >> 8); | |
5494 fromlen -= n; | |
5495 from += n; | |
5496 } | |
5497 | |
5498 /* Copy remainder to ip->bw_rest[] to be used for the next | |
5499 * call. */ | |
5500 if (fromlen > CONV_RESTLEN) | |
5501 { | |
5502 /* weird overlong sequence */ | |
5503 ip->bw_conv_error = TRUE; | |
5504 return FAIL; | |
5505 } | |
5506 mch_memmove(ip->bw_rest, from, fromlen); | |
835 | 5507 ip->bw_restlen = (int)fromlen; |
7 | 5508 } |
5509 else | |
5510 { | |
5511 /* Convert from enc_codepage to UCS-2, to the start of the | |
5512 * buffer. The buffer has been allocated to be big enough. */ | |
5513 ip->bw_restlen = 0; | |
5514 needed = MultiByteToWideChar(enc_codepage, | |
1142 | 5515 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen, |
7 | 5516 NULL, 0); |
5517 if (needed == 0) | |
5518 { | |
5519 /* When conversion fails there may be a trailing byte. */ | |
5520 needed = MultiByteToWideChar(enc_codepage, | |
1142 | 5521 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen - 1, |
7 | 5522 NULL, 0); |
5523 if (needed == 0) | |
5524 { | |
5525 /* Conversion doesn't work. */ | |
5526 ip->bw_conv_error = TRUE; | |
5527 return FAIL; | |
5528 } | |
5529 /* Save the trailing byte for the next call. */ | |
5530 ip->bw_rest[0] = from[fromlen - 1]; | |
5531 ip->bw_restlen = 1; | |
5532 } | |
5533 needed = MultiByteToWideChar(enc_codepage, MB_ERR_INVALID_CHARS, | |
1142 | 5534 (LPCSTR)from, (int)(fromlen - ip->bw_restlen), |
7 | 5535 (LPWSTR)to, needed); |
5536 if (needed == 0) | |
5537 { | |
5538 /* Safety check: Conversion doesn't work. */ | |
5539 ip->bw_conv_error = TRUE; | |
5540 return FAIL; | |
5541 } | |
5542 to += needed * 2; | |
5543 } | |
5544 | |
5545 fromlen = to - ip->bw_conv_buf; | |
5546 buf = to; | |
5547 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
5548 if (FIO_GET_CP(flags) == CP_UTF8) | |
5549 { | |
5550 /* Convert from UCS-2 to UTF-8, using the remainder of the | |
5551 * conversion buffer. Fails when out of space. */ | |
5552 for (from = ip->bw_conv_buf; fromlen > 1; fromlen -= 2) | |
5553 { | |
5554 u8c = *from++; | |
5555 u8c += (*from++ << 8); | |
5556 to += utf_char2bytes(u8c, to); | |
5557 if (to + 6 >= ip->bw_conv_buf + ip->bw_conv_buflen) | |
5558 { | |
5559 ip->bw_conv_error = TRUE; | |
5560 return FAIL; | |
5561 } | |
5562 } | |
835 | 5563 len = (int)(to - buf); |
7 | 5564 } |
5565 else | |
5566 #endif | |
5567 { | |
5568 /* Convert from UCS-2 to the codepage, using the remainder of | |
5569 * the conversion buffer. If the conversion uses the default | |
5570 * character "0", the data doesn't fit in this encoding, so | |
5571 * fail. */ | |
5572 len = WideCharToMultiByte(FIO_GET_CP(flags), 0, | |
5573 (LPCWSTR)ip->bw_conv_buf, (int)fromlen / sizeof(WCHAR), | |
1142 | 5574 (LPSTR)to, (int)(ip->bw_conv_buflen - fromlen), 0, |
5575 &bad); | |
7 | 5576 if (bad) |
5577 { | |
5578 ip->bw_conv_error = TRUE; | |
5579 return FAIL; | |
5580 } | |
5581 } | |
5582 } | |
5583 # endif | |
5584 | |
765 | 5585 # ifdef MACOS_CONVERT |
7 | 5586 else if (flags & FIO_MACROMAN) |
5587 { | |
5588 /* | |
5589 * Convert UTF-8 or latin1 to Apple MacRoman. | |
5590 */ | |
5591 char_u *from; | |
5592 size_t fromlen; | |
5593 | |
5594 if (ip->bw_restlen > 0) | |
5595 { | |
5596 /* Need to concatenate the remainder of the previous call and | |
5597 * the bytes of the current call. Use the end of the | |
5598 * conversion buffer for this. */ | |
5599 fromlen = len + ip->bw_restlen; | |
5600 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; | |
5601 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); | |
5602 mch_memmove(from + ip->bw_restlen, buf, (size_t)len); | |
5603 } | |
5604 else | |
5605 { | |
5606 from = buf; | |
5607 fromlen = len; | |
5608 } | |
5609 | |
18 | 5610 if (enc2macroman(from, fromlen, |
5611 ip->bw_conv_buf, &len, ip->bw_conv_buflen, | |
5612 ip->bw_rest, &ip->bw_restlen) == FAIL) | |
7 | 5613 { |
5614 ip->bw_conv_error = TRUE; | |
5615 return FAIL; | |
5616 } | |
5617 buf = ip->bw_conv_buf; | |
5618 } | |
5619 # endif | |
5620 | |
5621 # ifdef USE_ICONV | |
5622 if (ip->bw_iconv_fd != (iconv_t)-1) | |
5623 { | |
5624 const char *from; | |
5625 size_t fromlen; | |
5626 char *to; | |
5627 size_t tolen; | |
5628 | |
5629 /* Convert with iconv(). */ | |
5630 if (ip->bw_restlen > 0) | |
5631 { | |
1836 | 5632 char *fp; |
5633 | |
7 | 5634 /* Need to concatenate the remainder of the previous call and |
5635 * the bytes of the current call. Use the end of the | |
5636 * conversion buffer for this. */ | |
5637 fromlen = len + ip->bw_restlen; | |
1836 | 5638 fp = (char *)ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; |
5639 mch_memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen); | |
5640 mch_memmove(fp + ip->bw_restlen, buf, (size_t)len); | |
5641 from = fp; | |
7 | 5642 tolen = ip->bw_conv_buflen - fromlen; |
5643 } | |
5644 else | |
5645 { | |
5646 from = (const char *)buf; | |
5647 fromlen = len; | |
5648 tolen = ip->bw_conv_buflen; | |
5649 } | |
5650 to = (char *)ip->bw_conv_buf; | |
5651 | |
5652 if (ip->bw_first) | |
5653 { | |
5654 size_t save_len = tolen; | |
5655 | |
5656 /* output the initial shift state sequence */ | |
5657 (void)iconv(ip->bw_iconv_fd, NULL, NULL, &to, &tolen); | |
5658 | |
5659 /* There is a bug in iconv() on Linux (which appears to be | |
5660 * wide-spread) which sets "to" to NULL and messes up "tolen". | |
5661 */ | |
5662 if (to == NULL) | |
5663 { | |
5664 to = (char *)ip->bw_conv_buf; | |
5665 tolen = save_len; | |
5666 } | |
5667 ip->bw_first = FALSE; | |
5668 } | |
5669 | |
5670 /* | |
5671 * If iconv() has an error or there is not enough room, fail. | |
5672 */ | |
5673 if ((iconv(ip->bw_iconv_fd, (void *)&from, &fromlen, &to, &tolen) | |
5674 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) | |
5675 || fromlen > CONV_RESTLEN) | |
5676 { | |
5677 ip->bw_conv_error = TRUE; | |
5678 return FAIL; | |
5679 } | |
5680 | |
5681 /* copy remainder to ip->bw_rest[] to be used for the next call. */ | |
5682 if (fromlen > 0) | |
5683 mch_memmove(ip->bw_rest, (void *)from, fromlen); | |
5684 ip->bw_restlen = (int)fromlen; | |
5685 | |
5686 buf = ip->bw_conv_buf; | |
5687 len = (int)((char_u *)to - ip->bw_conv_buf); | |
5688 } | |
5689 # endif | |
5690 } | |
5691 #endif /* FEAT_MBYTE */ | |
5692 | |
5693 #ifdef FEAT_CRYPT | |
6122 | 5694 if (flags & FIO_ENCRYPTED) |
5695 { | |
5696 /* Encrypt the data. Do it in-place if possible, otherwise use an | |
5697 * allocated buffer. */ | |
5698 if (crypt_works_inplace(ip->bw_buffer->b_cryptstate)) | |
5699 { | |
5700 crypt_encode_inplace(ip->bw_buffer->b_cryptstate, buf, len); | |
5701 } | |
5702 else | |
5703 { | |
5704 char_u *outbuf; | |
5705 | |
5706 len = crypt_encode_alloc(curbuf->b_cryptstate, buf, len, &outbuf); | |
5707 if (len == 0) | |
5708 return OK; /* Crypt layer is buffering, will flush later. */ | |
5709 wlen = write_eintr(ip->bw_fd, outbuf, len); | |
5710 vim_free(outbuf); | |
5711 return (wlen < len) ? FAIL : OK; | |
5712 } | |
5713 } | |
7 | 5714 #endif |
5715 | |
2664 | 5716 wlen = write_eintr(ip->bw_fd, buf, len); |
5717 return (wlen < len) ? FAIL : OK; | |
7 | 5718 } |
5719 | |
5720 #ifdef FEAT_MBYTE | |
5721 /* | |
5722 * Convert a Unicode character to bytes. | |
1947 | 5723 * Return TRUE for an error, FALSE when it's OK. |
7 | 5724 */ |
5725 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5726 ucs2bytes( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5727 unsigned c, /* in: character */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5728 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
|
5729 int flags) /* FIO_ flags */ |
7 | 5730 { |
5731 char_u *p = *pp; | |
5732 int error = FALSE; | |
5733 int cc; | |
5734 | |
5735 | |
5736 if (flags & FIO_UCS4) | |
5737 { | |
5738 if (flags & FIO_ENDIAN_L) | |
5739 { | |
5740 *p++ = c; | |
5741 *p++ = (c >> 8); | |
5742 *p++ = (c >> 16); | |
5743 *p++ = (c >> 24); | |
5744 } | |
5745 else | |
5746 { | |
5747 *p++ = (c >> 24); | |
5748 *p++ = (c >> 16); | |
5749 *p++ = (c >> 8); | |
5750 *p++ = c; | |
5751 } | |
5752 } | |
5753 else if (flags & (FIO_UCS2 | FIO_UTF16)) | |
5754 { | |
5755 if (c >= 0x10000) | |
5756 { | |
5757 if (flags & FIO_UTF16) | |
5758 { | |
5759 /* Make two words, ten bits of the character in each. First | |
5760 * word is 0xd800 - 0xdbff, second one 0xdc00 - 0xdfff */ | |
5761 c -= 0x10000; | |
5762 if (c >= 0x100000) | |
5763 error = TRUE; | |
5764 cc = ((c >> 10) & 0x3ff) + 0xd800; | |
5765 if (flags & FIO_ENDIAN_L) | |
5766 { | |
5767 *p++ = cc; | |
5768 *p++ = ((unsigned)cc >> 8); | |
5769 } | |
5770 else | |
5771 { | |
5772 *p++ = ((unsigned)cc >> 8); | |
5773 *p++ = cc; | |
5774 } | |
5775 c = (c & 0x3ff) + 0xdc00; | |
5776 } | |
5777 else | |
5778 error = TRUE; | |
5779 } | |
5780 if (flags & FIO_ENDIAN_L) | |
5781 { | |
5782 *p++ = c; | |
5783 *p++ = (c >> 8); | |
5784 } | |
5785 else | |
5786 { | |
5787 *p++ = (c >> 8); | |
5788 *p++ = c; | |
5789 } | |
5790 } | |
5791 else /* Latin1 */ | |
5792 { | |
5793 if (c >= 0x100) | |
5794 { | |
5795 error = TRUE; | |
5796 *p++ = 0xBF; | |
5797 } | |
5798 else | |
5799 *p++ = c; | |
5800 } | |
5801 | |
5802 *pp = p; | |
5803 return error; | |
5804 } | |
5805 | |
5806 /* | |
1948 | 5807 * Return TRUE if file encoding "fenc" requires conversion from or to |
5808 * 'encoding'. | |
7 | 5809 */ |
5810 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5811 need_conversion(char_u *fenc) |
7 | 5812 { |
1948 | 5813 int same_encoding; |
5814 int enc_flags; | |
5815 int fenc_flags; | |
5816 | |
5817 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0) | |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5818 { |
1948 | 5819 same_encoding = TRUE; |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5820 fenc_flags = 0; |
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5821 } |
1948 | 5822 else |
5823 { | |
5824 /* Ignore difference between "ansi" and "latin1", "ucs-4" and | |
5825 * "ucs-4be", etc. */ | |
5826 enc_flags = get_fio_flags(p_enc); | |
5827 fenc_flags = get_fio_flags(fenc); | |
5828 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags); | |
5829 } | |
5830 if (same_encoding) | |
5831 { | |
5832 /* Specified encoding matches with 'encoding'. This requires | |
5833 * conversion when 'encoding' is Unicode but not UTF-8. */ | |
5834 return enc_unicode != 0; | |
5835 } | |
5836 | |
5837 /* Encodings differ. However, conversion is not needed when 'enc' is any | |
5838 * Unicode encoding and the file is UTF-8. */ | |
5839 return !(enc_utf8 && fenc_flags == FIO_UTF8); | |
7 | 5840 } |
5841 | |
5842 /* | |
5843 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the | |
5844 * internal conversion. | |
5845 * if "ptr" is an empty string, use 'encoding'. | |
5846 */ | |
5847 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5848 get_fio_flags(char_u *ptr) |
7 | 5849 { |
5850 int prop; | |
5851 | |
5852 if (*ptr == NUL) | |
5853 ptr = p_enc; | |
5854 | |
5855 prop = enc_canon_props(ptr); | |
5856 if (prop & ENC_UNICODE) | |
5857 { | |
5858 if (prop & ENC_2BYTE) | |
5859 { | |
5860 if (prop & ENC_ENDIAN_L) | |
5861 return FIO_UCS2 | FIO_ENDIAN_L; | |
5862 return FIO_UCS2; | |
5863 } | |
5864 if (prop & ENC_4BYTE) | |
5865 { | |
5866 if (prop & ENC_ENDIAN_L) | |
5867 return FIO_UCS4 | FIO_ENDIAN_L; | |
5868 return FIO_UCS4; | |
5869 } | |
5870 if (prop & ENC_2WORD) | |
5871 { | |
5872 if (prop & ENC_ENDIAN_L) | |
5873 return FIO_UTF16 | FIO_ENDIAN_L; | |
5874 return FIO_UTF16; | |
5875 } | |
5876 return FIO_UTF8; | |
5877 } | |
5878 if (prop & ENC_LATIN1) | |
5879 return FIO_LATIN1; | |
5880 /* must be ENC_DBCS, requires iconv() */ | |
5881 return 0; | |
5882 } | |
5883 | |
5884 #ifdef WIN3264 | |
5885 /* | |
5886 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed | |
5887 * for the conversion MS-Windows can do for us. Also accept "utf-8". | |
5888 * Used for conversion between 'encoding' and 'fileencoding'. | |
5889 */ | |
5890 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5891 get_win_fio_flags(char_u *ptr) |
7 | 5892 { |
5893 int cp; | |
5894 | |
5895 /* Cannot do this when 'encoding' is not utf-8 and not a codepage. */ | |
5896 if (!enc_utf8 && enc_codepage <= 0) | |
5897 return 0; | |
5898 | |
5899 cp = encname2codepage(ptr); | |
5900 if (cp == 0) | |
5901 { | |
5902 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
5903 if (STRCMP(ptr, "utf-8") == 0) | |
5904 cp = CP_UTF8; | |
5905 else | |
5906 # endif | |
5907 return 0; | |
5908 } | |
5909 return FIO_PUT_CP(cp) | FIO_CODEPAGE; | |
5910 } | |
5911 #endif | |
5912 | |
5913 #ifdef MACOS_X | |
5914 /* | |
5915 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags | |
5916 * needed for the internal conversion to/from utf-8 or latin1. | |
5917 */ | |
5918 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5919 get_mac_fio_flags(char_u *ptr) |
7 | 5920 { |
5921 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
5922 && (enc_canon_props(ptr) & ENC_MACROMAN)) | |
5923 return FIO_MACROMAN; | |
5924 return 0; | |
5925 } | |
5926 #endif | |
5927 | |
5928 /* | |
5929 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size]. | |
5930 * "size" must be at least 2. | |
5931 * Return the name of the encoding and set "*lenp" to the length. | |
5932 * Returns NULL when no BOM found. | |
5933 */ | |
5934 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5935 check_for_bom( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5936 char_u *p, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5937 long size, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5938 int *lenp, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5939 int flags) |
7 | 5940 { |
5941 char *name = NULL; | |
5942 int len = 2; | |
5943 | |
5944 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf | |
1688 | 5945 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0)) |
7 | 5946 { |
5947 name = "utf-8"; /* EF BB BF */ | |
5948 len = 3; | |
5949 } | |
5950 else if (p[0] == 0xff && p[1] == 0xfe) | |
5951 { | |
5952 if (size >= 4 && p[2] == 0 && p[3] == 0 | |
5953 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L))) | |
5954 { | |
5955 name = "ucs-4le"; /* FF FE 00 00 */ | |
5956 len = 4; | |
5957 } | |
1735 | 5958 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L)) |
7 | 5959 name = "ucs-2le"; /* FF FE */ |
1735 | 5960 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L)) |
5961 /* utf-16le is preferred, it also works for ucs-2le text */ | |
7 | 5962 name = "utf-16le"; /* FF FE */ |
5963 } | |
5964 else if (p[0] == 0xfe && p[1] == 0xff | |
5965 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16)) | |
5966 { | |
1547 | 5967 /* Default to utf-16, it works also for ucs-2 text. */ |
5968 if (flags == FIO_UCS2) | |
5969 name = "ucs-2"; /* FE FF */ | |
5970 else | |
7 | 5971 name = "utf-16"; /* FE FF */ |
5972 } | |
5973 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe | |
5974 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4)) | |
5975 { | |
5976 name = "ucs-4"; /* 00 00 FE FF */ | |
5977 len = 4; | |
5978 } | |
5979 | |
5980 *lenp = len; | |
5981 return (char_u *)name; | |
5982 } | |
5983 | |
5984 /* | |
5985 * Generate a BOM in "buf[4]" for encoding "name". | |
5986 * Return the length of the BOM (zero when no BOM). | |
5987 */ | |
5988 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5989 make_bom(char_u *buf, char_u *name) |
7 | 5990 { |
5991 int flags; | |
5992 char_u *p; | |
5993 | |
5994 flags = get_fio_flags(name); | |
5995 | |
5996 /* Can't put a BOM in a non-Unicode file. */ | |
5997 if (flags == FIO_LATIN1 || flags == 0) | |
5998 return 0; | |
5999 | |
6000 if (flags == FIO_UTF8) /* UTF-8 */ | |
6001 { | |
6002 buf[0] = 0xef; | |
6003 buf[1] = 0xbb; | |
6004 buf[2] = 0xbf; | |
6005 return 3; | |
6006 } | |
6007 p = buf; | |
6008 (void)ucs2bytes(0xfeff, &p, flags); | |
6009 return (int)(p - buf); | |
6010 } | |
6011 #endif | |
6012 | |
1418 | 6013 #if defined(FEAT_VIMINFO) || defined(FEAT_BROWSE) || \ |
1471 | 6014 defined(FEAT_QUICKFIX) || defined(FEAT_AUTOCMD) || defined(PROTO) |
7 | 6015 /* |
6016 * Try to find a shortname by comparing the fullname with the current | |
6017 * directory. | |
1411 | 6018 * Returns "full_path" or pointer into "full_path" if shortened. |
6019 */ | |
6020 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6021 shorten_fname1(char_u *full_path) |
1411 | 6022 { |
2770 | 6023 char_u *dirname; |
1411 | 6024 char_u *p = full_path; |
6025 | |
2770 | 6026 dirname = alloc(MAXPATHL); |
6027 if (dirname == NULL) | |
6028 return full_path; | |
1411 | 6029 if (mch_dirname(dirname, MAXPATHL) == OK) |
6030 { | |
6031 p = shorten_fname(full_path, dirname); | |
6032 if (p == NULL || *p == NUL) | |
6033 p = full_path; | |
6034 } | |
2770 | 6035 vim_free(dirname); |
1411 | 6036 return p; |
6037 } | |
1418 | 6038 #endif |
1411 | 6039 |
6040 /* | |
6041 * Try to find a shortname by comparing the fullname with the current | |
6042 * directory. | |
7 | 6043 * Returns NULL if not shorter name possible, pointer into "full_path" |
6044 * otherwise. | |
6045 */ | |
6046 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6047 shorten_fname(char_u *full_path, char_u *dir_name) |
7 | 6048 { |
6049 int len; | |
6050 char_u *p; | |
6051 | |
6052 if (full_path == NULL) | |
6053 return NULL; | |
6054 len = (int)STRLEN(dir_name); | |
6055 if (fnamencmp(dir_name, full_path, len) == 0) | |
6056 { | |
6057 p = full_path + len; | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6058 #if defined(MSWIN) |
7 | 6059 /* |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6060 * MSWIN: when a file is in the root directory, dir_name will end in a |
7 | 6061 * slash, since C: by itself does not define a specific dir. In this |
6062 * case p may already be correct. <negri> | |
6063 */ | |
6064 if (!((len > 2) && (*(p - 2) == ':'))) | |
6065 #endif | |
6066 { | |
6067 if (vim_ispathsep(*p)) | |
6068 ++p; | |
6069 #ifndef VMS /* the path separator is always part of the path */ | |
6070 else | |
6071 p = NULL; | |
6072 #endif | |
6073 } | |
6074 } | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6075 #if defined(MSWIN) |
7 | 6076 /* |
6077 * When using a file in the current drive, remove the drive name: | |
6078 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on | |
6079 * a floppy from "A:\dir" to "B:\dir". | |
6080 */ | |
6081 else if (len > 3 | |
6082 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0]) | |
6083 && full_path[1] == ':' | |
6084 && vim_ispathsep(full_path[2])) | |
6085 p = full_path + 2; | |
6086 #endif | |
6087 else | |
6088 p = NULL; | |
6089 return p; | |
6090 } | |
6091 | |
6092 /* | |
6093 * Shorten filenames for all buffers. | |
6094 * When "force" is TRUE: Use full path from now on for files currently being | |
6095 * edited, both for file name and swap file name. Try to shorten the file | |
6096 * names a bit, if safe to do so. | |
6097 * When "force" is FALSE: Only try to shorten absolute file names. | |
6098 * For buffers that have buftype "nofile" or "scratch": never change the file | |
6099 * name. | |
6100 */ | |
6101 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6102 shorten_fnames(int force) |
7 | 6103 { |
6104 char_u dirname[MAXPATHL]; | |
6105 buf_T *buf; | |
6106 char_u *p; | |
6107 | |
6108 mch_dirname(dirname, MAXPATHL); | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
6109 FOR_ALL_BUFFERS(buf) |
7 | 6110 { |
6111 if (buf->b_fname != NULL | |
6112 #ifdef FEAT_QUICKFIX | |
6113 && !bt_nofile(buf) | |
6114 #endif | |
6115 && !path_with_url(buf->b_fname) | |
6116 && (force | |
6117 || buf->b_sfname == NULL | |
6118 || mch_isFullName(buf->b_sfname))) | |
6119 { | |
6120 vim_free(buf->b_sfname); | |
6121 buf->b_sfname = NULL; | |
6122 p = shorten_fname(buf->b_ffname, dirname); | |
6123 if (p != NULL) | |
6124 { | |
6125 buf->b_sfname = vim_strsave(p); | |
6126 buf->b_fname = buf->b_sfname; | |
6127 } | |
6128 if (p == NULL || buf->b_fname == NULL) | |
6129 buf->b_fname = buf->b_ffname; | |
9 | 6130 } |
6131 | |
6132 /* Always make the swap file name a full path, a "nofile" buffer may | |
6133 * also have a swap file. */ | |
6134 mf_fullname(buf->b_ml.ml_mfp); | |
7 | 6135 } |
6136 #ifdef FEAT_WINDOWS | |
6137 status_redraw_all(); | |
672 | 6138 redraw_tabline = TRUE; |
7 | 6139 #endif |
6140 } | |
6141 | |
6142 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \ | |
6143 || defined(FEAT_GUI_MSWIN) \ | |
6144 || defined(FEAT_GUI_MAC) \ | |
6145 || defined(PROTO) | |
6146 /* | |
6147 * Shorten all filenames in "fnames[count]" by current directory. | |
6148 */ | |
6149 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6150 shorten_filenames(char_u **fnames, int count) |
7 | 6151 { |
6152 int i; | |
6153 char_u dirname[MAXPATHL]; | |
6154 char_u *p; | |
6155 | |
6156 if (fnames == NULL || count < 1) | |
6157 return; | |
6158 mch_dirname(dirname, sizeof(dirname)); | |
6159 for (i = 0; i < count; ++i) | |
6160 { | |
6161 if ((p = shorten_fname(fnames[i], dirname)) != NULL) | |
6162 { | |
6163 /* shorten_fname() returns pointer in given "fnames[i]". If free | |
6164 * "fnames[i]" first, "p" becomes invalid. So we need to copy | |
6165 * "p" first then free fnames[i]. */ | |
6166 p = vim_strsave(p); | |
6167 vim_free(fnames[i]); | |
6168 fnames[i] = p; | |
6169 } | |
6170 } | |
6171 } | |
6172 #endif | |
6173 | |
6174 /* | |
1651 | 6175 * add extension to file name - change path/fo.o.h to path/fo.o.h.ext or |
7 | 6176 * fo_o_h.ext for MSDOS or when shortname option set. |
6177 * | |
6178 * Assumed that fname is a valid name found in the filesystem we assure that | |
6179 * the return value is a different name and ends in 'ext'. | |
6180 * "ext" MUST be at most 4 characters long if it starts with a dot, 3 | |
6181 * characters otherwise. | |
6182 * Space for the returned name is allocated, must be freed later. | |
6183 * Returns NULL when out of memory. | |
6184 */ | |
6185 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6186 modname( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6187 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6188 char_u *ext, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6189 int prepend_dot) /* may prepend a '.' to file name */ |
7 | 6190 { |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6191 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname), |
7 | 6192 fname, ext, prepend_dot); |
6193 } | |
6194 | |
6195 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6196 buf_modname( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6197 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
|
6198 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6199 char_u *ext, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6200 int prepend_dot) /* may prepend a '.' to file name */ |
7 | 6201 { |
6202 char_u *retval; | |
6203 char_u *s; | |
6204 char_u *e; | |
6205 char_u *ptr; | |
6206 int fnamelen, extlen; | |
6207 | |
6208 extlen = (int)STRLEN(ext); | |
6209 | |
6210 /* | |
6211 * If there is no file name we must get the name of the current directory | |
6212 * (we need the full path in case :cd is used). | |
6213 */ | |
6214 if (fname == NULL || *fname == NUL) | |
6215 { | |
6216 retval = alloc((unsigned)(MAXPATHL + extlen + 3)); | |
6217 if (retval == NULL) | |
6218 return NULL; | |
6219 if (mch_dirname(retval, MAXPATHL) == FAIL || | |
6220 (fnamelen = (int)STRLEN(retval)) == 0) | |
6221 { | |
6222 vim_free(retval); | |
6223 return NULL; | |
6224 } | |
39 | 6225 if (!after_pathsep(retval, retval + fnamelen)) |
7 | 6226 { |
6227 retval[fnamelen++] = PATHSEP; | |
6228 retval[fnamelen] = NUL; | |
6229 } | |
6230 prepend_dot = FALSE; /* nothing to prepend a dot to */ | |
6231 } | |
6232 else | |
6233 { | |
6234 fnamelen = (int)STRLEN(fname); | |
6235 retval = alloc((unsigned)(fnamelen + extlen + 3)); | |
6236 if (retval == NULL) | |
6237 return NULL; | |
6238 STRCPY(retval, fname); | |
6239 #ifdef VMS | |
6240 vms_remove_version(retval); /* we do not need versions here */ | |
6241 #endif | |
6242 } | |
6243 | |
6244 /* | |
6245 * search backwards until we hit a '/', '\' or ':' replacing all '.' | |
6246 * by '_' for MSDOS or when shortname option set and ext starts with a dot. | |
6247 * Then truncate what is after the '/', '\' or ':' to 8 characters for | |
6248 * MSDOS and 26 characters for AMIGA, a lot more for UNIX. | |
6249 */ | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
6250 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr)) |
7 | 6251 { |
6252 if (*ext == '.' | |
2823 | 6253 #ifdef USE_LONG_FNAME |
7 | 6254 && (!USE_LONG_FNAME || shortname) |
2823 | 6255 #else |
7 | 6256 && shortname |
2823 | 6257 #endif |
7 | 6258 ) |
6259 if (*ptr == '.') /* replace '.' by '_' */ | |
6260 *ptr = '_'; | |
6261 if (vim_ispathsep(*ptr)) | |
391 | 6262 { |
6263 ++ptr; | |
7 | 6264 break; |
391 | 6265 } |
6266 } | |
7 | 6267 |
6268 /* the file name has at most BASENAMELEN characters. */ | |
6269 if (STRLEN(ptr) > (unsigned)BASENAMELEN) | |
6270 ptr[BASENAMELEN] = '\0'; | |
6271 | |
6272 s = ptr + STRLEN(ptr); | |
6273 | |
6274 /* | |
6275 * For 8.3 file names we may have to reduce the length. | |
6276 */ | |
6277 #ifdef USE_LONG_FNAME | |
6278 if (!USE_LONG_FNAME || shortname) | |
6279 #else | |
6280 if (shortname) | |
6281 #endif | |
6282 { | |
6283 /* | |
6284 * If there is no file name, or the file name ends in '/', and the | |
6285 * extension starts with '.', put a '_' before the dot, because just | |
6286 * ".ext" is invalid. | |
6287 */ | |
6288 if (fname == NULL || *fname == NUL | |
6289 || vim_ispathsep(fname[STRLEN(fname) - 1])) | |
6290 { | |
6291 if (*ext == '.') | |
6292 *s++ = '_'; | |
6293 } | |
6294 /* | |
6295 * If the extension starts with '.', truncate the base name at 8 | |
6296 * characters | |
6297 */ | |
6298 else if (*ext == '.') | |
6299 { | |
1877 | 6300 if ((size_t)(s - ptr) > (size_t)8) |
7 | 6301 { |
6302 s = ptr + 8; | |
6303 *s = '\0'; | |
6304 } | |
6305 } | |
6306 /* | |
6307 * If the extension doesn't start with '.', and the file name | |
6308 * doesn't have an extension yet, append a '.' | |
6309 */ | |
6310 else if ((e = vim_strchr(ptr, '.')) == NULL) | |
6311 *s++ = '.'; | |
6312 /* | |
6313 * If the extension doesn't start with '.', and there already is an | |
1201 | 6314 * extension, it may need to be truncated |
7 | 6315 */ |
6316 else if ((int)STRLEN(e) + extlen > 4) | |
6317 s = e + 4 - extlen; | |
6318 } | |
7408
1886f2863437
commit https://github.com/vim/vim/commit/e7fedb6ebe72d9a475aa65109b77d5ed4667067a
Christian Brabandt <cb@256bit.org>
parents:
7320
diff
changeset
|
6319 #if defined(USE_LONG_FNAME) || defined(WIN3264) |
7 | 6320 /* |
6321 * If there is no file name, and the extension starts with '.', put a | |
6322 * '_' before the dot, because just ".ext" may be invalid if it's on a | |
6323 * FAT partition, and on HPFS it doesn't matter. | |
6324 */ | |
6325 else if ((fname == NULL || *fname == NUL) && *ext == '.') | |
6326 *s++ = '_'; | |
6327 #endif | |
6328 | |
6329 /* | |
1651 | 6330 * Append the extension. |
7 | 6331 * ext can start with '.' and cannot exceed 3 more characters. |
6332 */ | |
6333 STRCPY(s, ext); | |
6334 | |
6335 /* | |
6336 * Prepend the dot. | |
6337 */ | |
2823 | 6338 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.' |
7 | 6339 #ifdef USE_LONG_FNAME |
6340 && USE_LONG_FNAME | |
6341 #endif | |
6342 ) | |
6343 { | |
1620 | 6344 STRMOVE(e + 1, e); |
7 | 6345 *e = '.'; |
6346 } | |
6347 | |
6348 /* | |
6349 * Check that, after appending the extension, the file name is really | |
6350 * different. | |
6351 */ | |
6352 if (fname != NULL && STRCMP(fname, retval) == 0) | |
6353 { | |
6354 /* we search for a character that can be replaced by '_' */ | |
6355 while (--s >= ptr) | |
6356 { | |
6357 if (*s != '_') | |
6358 { | |
6359 *s = '_'; | |
6360 break; | |
6361 } | |
6362 } | |
6363 if (s < ptr) /* fname was "________.<ext>", how tricky! */ | |
6364 *ptr = 'v'; | |
6365 } | |
6366 return retval; | |
6367 } | |
6368 | |
6369 /* | |
6370 * Like fgets(), but if the file line is too long, it is truncated and the | |
6371 * rest of the line is thrown away. Returns TRUE for end-of-file. | |
6372 */ | |
6373 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6374 vim_fgets(char_u *buf, int size, FILE *fp) |
7 | 6375 { |
6376 char *eof; | |
6377 #define FGETS_SIZE 200 | |
6378 char tbuf[FGETS_SIZE]; | |
6379 | |
6380 buf[size - 2] = NUL; | |
6381 #ifdef USE_CR | |
6382 eof = fgets_cr((char *)buf, size, fp); | |
6383 #else | |
6384 eof = fgets((char *)buf, size, fp); | |
6385 #endif | |
6386 if (buf[size - 2] != NUL && buf[size - 2] != '\n') | |
6387 { | |
6388 buf[size - 1] = NUL; /* Truncate the line */ | |
6389 | |
6390 /* Now throw away the rest of the line: */ | |
6391 do | |
6392 { | |
6393 tbuf[FGETS_SIZE - 2] = NUL; | |
6394 #ifdef USE_CR | |
1757 | 6395 ignoredp = fgets_cr((char *)tbuf, FGETS_SIZE, fp); |
7 | 6396 #else |
1757 | 6397 ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp); |
7 | 6398 #endif |
6399 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n'); | |
6400 } | |
6401 return (eof == NULL); | |
6402 } | |
6403 | |
6404 #if defined(USE_CR) || defined(PROTO) | |
6405 /* | |
6406 * Like vim_fgets(), but accept any line terminator: CR, CR-LF or LF. | |
6407 * Returns TRUE for end-of-file. | |
6408 * Only used for the Mac, because it's much slower than vim_fgets(). | |
6409 */ | |
6410 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6411 tag_fgets(char_u *buf, int size, FILE *fp) |
7 | 6412 { |
6413 int i = 0; | |
6414 int c; | |
6415 int eof = FALSE; | |
6416 | |
6417 for (;;) | |
6418 { | |
6419 c = fgetc(fp); | |
6420 if (c == EOF) | |
6421 { | |
6422 eof = TRUE; | |
6423 break; | |
6424 } | |
6425 if (c == '\r') | |
6426 { | |
6427 /* Always store a NL for end-of-line. */ | |
6428 if (i < size - 1) | |
6429 buf[i++] = '\n'; | |
6430 c = fgetc(fp); | |
6431 if (c != '\n') /* Macintosh format: single CR. */ | |
6432 ungetc(c, fp); | |
6433 break; | |
6434 } | |
6435 if (i < size - 1) | |
6436 buf[i++] = c; | |
6437 if (c == '\n') | |
6438 break; | |
6439 } | |
6440 buf[i] = NUL; | |
6441 return eof; | |
6442 } | |
6443 #endif | |
6444 | |
6445 /* | |
6446 * rename() only works if both files are on the same file system, this | |
6447 * function will (attempts to?) copy the file across if rename fails -- webb | |
6448 * Return -1 for failure, 0 for success. | |
6449 */ | |
6450 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6451 vim_rename(char_u *from, char_u *to) |
7 | 6452 { |
6453 int fd_in; | |
6454 int fd_out; | |
6455 int n; | |
6456 char *errmsg = NULL; | |
6457 char *buffer; | |
6458 #ifdef AMIGA | |
6459 BPTR flock; | |
6460 #endif | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6461 stat_T st; |
194 | 6462 long perm; |
199 | 6463 #ifdef HAVE_ACL |
6464 vim_acl_T acl; /* ACL from original file */ | |
6465 #endif | |
1779 | 6466 int use_tmp_file = FALSE; |
7 | 6467 |
6468 /* | |
1779 | 6469 * When the names are identical, there is nothing to do. When they refer |
6470 * to the same file (ignoring case and slash/backslash differences) but | |
6471 * the file name differs we need to go through a temp file. | |
7 | 6472 */ |
6473 if (fnamecmp(from, to) == 0) | |
1779 | 6474 { |
4242 | 6475 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0) |
1779 | 6476 use_tmp_file = TRUE; |
6477 else | |
6478 return 0; | |
6479 } | |
7 | 6480 |
6481 /* | |
6482 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted. | |
6483 */ | |
6484 if (mch_stat((char *)from, &st) < 0) | |
6485 return -1; | |
6486 | |
1778 | 6487 #ifdef UNIX |
6488 { | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6489 stat_T st_to; |
1778 | 6490 |
6491 /* It's possible for the source and destination to be the same file. | |
6492 * This happens when "from" and "to" differ in case and are on a FAT32 | |
6493 * filesystem. In that case go through a temp file name. */ | |
6494 if (mch_stat((char *)to, &st_to) >= 0 | |
6495 && st.st_dev == st_to.st_dev | |
6496 && st.st_ino == st_to.st_ino) | |
1779 | 6497 use_tmp_file = TRUE; |
6498 } | |
6499 #endif | |
2793 | 6500 #ifdef WIN3264 |
6501 { | |
6502 BY_HANDLE_FILE_INFORMATION info1, info2; | |
6503 | |
6504 /* It's possible for the source and destination to be the same file. | |
6505 * In that case go through a temp file name. This makes rename("foo", | |
6506 * "./foo") a no-op (in a complicated way). */ | |
6507 if (win32_fileinfo(from, &info1) == FILEINFO_OK | |
6508 && win32_fileinfo(to, &info2) == FILEINFO_OK | |
6509 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber | |
6510 && info1.nFileIndexHigh == info2.nFileIndexHigh | |
6511 && info1.nFileIndexLow == info2.nFileIndexLow) | |
6512 use_tmp_file = TRUE; | |
6513 } | |
6514 #endif | |
1779 | 6515 |
6516 if (use_tmp_file) | |
6517 { | |
6518 char tempname[MAXPATHL + 1]; | |
6519 | |
6520 /* | |
6521 * Find a name that doesn't exist and is in the same directory. | |
6522 * Rename "from" to "tempname" and then rename "tempname" to "to". | |
6523 */ | |
6524 if (STRLEN(from) >= MAXPATHL - 5) | |
6525 return -1; | |
6526 STRCPY(tempname, from); | |
6527 for (n = 123; n < 99999; ++n) | |
6528 { | |
6529 sprintf((char *)gettail((char_u *)tempname), "%d", n); | |
6530 if (mch_stat(tempname, &st) < 0) | |
6531 { | |
6532 if (mch_rename((char *)from, tempname) == 0) | |
1778 | 6533 { |
1779 | 6534 if (mch_rename(tempname, (char *)to) == 0) |
6535 return 0; | |
6536 /* Strange, the second step failed. Try moving the | |
6537 * file back and return failure. */ | |
6538 mch_rename(tempname, (char *)from); | |
1778 | 6539 return -1; |
6540 } | |
1779 | 6541 /* If it fails for one temp name it will most likely fail |
6542 * for any temp name, give up. */ | |
6543 return -1; | |
6544 } | |
6545 } | |
6546 return -1; | |
1778 | 6547 } |
6548 | |
7 | 6549 /* |
6550 * Delete the "to" file, this is required on some systems to make the | |
6551 * mch_rename() work, on other systems it makes sure that we don't have | |
6552 * two files when the mch_rename() fails. | |
6553 */ | |
6554 | |
6555 #ifdef AMIGA | |
6556 /* | |
6557 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible | |
6558 * that the name of the "to" file is the same as the "from" file, even | |
1201 | 6559 * though the names are different. To avoid the chance of accidentally |
7 | 6560 * deleting the "from" file (horror!) we lock it during the remove. |
6561 * | |
6562 * When used for making a backup before writing the file: This should not | |
6563 * happen with ":w", because startscript() should detect this problem and | |
6564 * set buf->b_shortname, causing modname() to return a correct ".bak" file | |
6565 * name. This problem does exist with ":w filename", but then the | |
6566 * original file will be somewhere else so the backup isn't really | |
6567 * important. If autoscripting is off the rename may fail. | |
6568 */ | |
6569 flock = Lock((UBYTE *)from, (long)ACCESS_READ); | |
6570 #endif | |
6571 mch_remove(to); | |
6572 #ifdef AMIGA | |
6573 if (flock) | |
6574 UnLock(flock); | |
6575 #endif | |
6576 | |
6577 /* | |
6578 * First try a normal rename, return if it works. | |
6579 */ | |
6580 if (mch_rename((char *)from, (char *)to) == 0) | |
6581 return 0; | |
6582 | |
6583 /* | |
6584 * Rename() failed, try copying the file. | |
6585 */ | |
194 | 6586 perm = mch_getperm(from); |
199 | 6587 #ifdef HAVE_ACL |
6588 /* For systems that support ACL: get the ACL from the original file. */ | |
6589 acl = mch_get_acl(from); | |
6590 #endif | |
7 | 6591 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0); |
6592 if (fd_in == -1) | |
1651 | 6593 { |
6594 #ifdef HAVE_ACL | |
6595 mch_free_acl(acl); | |
6596 #endif | |
7 | 6597 return -1; |
1651 | 6598 } |
194 | 6599 |
6600 /* Create the new file with same permissions as the original. */ | |
557 | 6601 fd_out = mch_open((char *)to, |
6602 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm); | |
7 | 6603 if (fd_out == -1) |
6604 { | |
6605 close(fd_in); | |
1651 | 6606 #ifdef HAVE_ACL |
6607 mch_free_acl(acl); | |
6608 #endif | |
7 | 6609 return -1; |
6610 } | |
6611 | |
6612 buffer = (char *)alloc(BUFSIZE); | |
6613 if (buffer == NULL) | |
6614 { | |
1651 | 6615 close(fd_out); |
7 | 6616 close(fd_in); |
1651 | 6617 #ifdef HAVE_ACL |
6618 mch_free_acl(acl); | |
6619 #endif | |
7 | 6620 return -1; |
6621 } | |
6622 | |
2664 | 6623 while ((n = read_eintr(fd_in, buffer, BUFSIZE)) > 0) |
6624 if (write_eintr(fd_out, buffer, n) != n) | |
7 | 6625 { |
6626 errmsg = _("E208: Error writing to \"%s\""); | |
6627 break; | |
6628 } | |
6629 | |
6630 vim_free(buffer); | |
6631 close(fd_in); | |
6632 if (close(fd_out) < 0) | |
6633 errmsg = _("E209: Error closing \"%s\""); | |
6634 if (n < 0) | |
6635 { | |
6636 errmsg = _("E210: Error reading \"%s\""); | |
6637 to = from; | |
6638 } | |
1201 | 6639 #ifndef UNIX /* for Unix mch_open() already set the permission */ |
194 | 6640 mch_setperm(to, perm); |
570 | 6641 #endif |
199 | 6642 #ifdef HAVE_ACL |
6643 mch_set_acl(to, acl); | |
1651 | 6644 mch_free_acl(acl); |
199 | 6645 #endif |
5788 | 6646 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
5483 | 6647 mch_copy_sec(from, to); |
5479 | 6648 #endif |
7 | 6649 if (errmsg != NULL) |
6650 { | |
6651 EMSG2(errmsg, to); | |
6652 return -1; | |
6653 } | |
6654 mch_remove(from); | |
6655 return 0; | |
6656 } | |
6657 | |
6658 static int already_warned = FALSE; | |
6659 | |
6660 /* | |
6661 * Check if any not hidden buffer has been changed. | |
6662 * Postpone the check if there are characters in the stuff buffer, a global | |
6663 * command is being executed, a mapping is being executed or an autocommand is | |
6664 * busy. | |
6665 * Returns TRUE if some message was written (screen should be redrawn and | |
6666 * cursor positioned). | |
6667 */ | |
6668 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6669 check_timestamps( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6670 int focus) /* called for GUI focus event */ |
7 | 6671 { |
6672 buf_T *buf; | |
6673 int didit = 0; | |
6674 int n; | |
6675 | |
6676 /* Don't check timestamps while system() or another low-level function may | |
6677 * cause us to lose and gain focus. */ | |
6678 if (no_check_timestamps > 0) | |
6679 return FALSE; | |
6680 | |
6681 /* Avoid doing a check twice. The OK/Reload dialog can cause a focus | |
6682 * event and we would keep on checking if the file is steadily growing. | |
6683 * Do check again after typing something. */ | |
6684 if (focus && did_check_timestamps) | |
6685 { | |
6686 need_check_timestamps = TRUE; | |
6687 return FALSE; | |
6688 } | |
6689 | |
6690 if (!stuff_empty() || global_busy || !typebuf_typed() | |
6691 #ifdef FEAT_AUTOCMD | |
1834 | 6692 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0 |
7 | 6693 #endif |
6694 ) | |
6695 need_check_timestamps = TRUE; /* check later */ | |
6696 else | |
6697 { | |
6698 ++no_wait_return; | |
6699 did_check_timestamps = TRUE; | |
6700 already_warned = FALSE; | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
6701 FOR_ALL_BUFFERS(buf) |
7 | 6702 { |
6703 /* Only check buffers in a window. */ | |
6704 if (buf->b_nwindows > 0) | |
6705 { | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6706 bufref_T bufref; |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6707 |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6708 set_bufref(&bufref, buf); |
7 | 6709 n = buf_check_timestamp(buf, focus); |
6710 if (didit < n) | |
6711 didit = n; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6712 if (n > 0 && !bufref_valid(&bufref)) |
7 | 6713 { |
6714 /* Autocommands have removed the buffer, start at the | |
6715 * first one again. */ | |
6716 buf = firstbuf; | |
6717 continue; | |
6718 } | |
6719 } | |
6720 } | |
6721 --no_wait_return; | |
6722 need_check_timestamps = FALSE; | |
6723 if (need_wait_return && didit == 2) | |
6724 { | |
6725 /* make sure msg isn't overwritten */ | |
6726 msg_puts((char_u *)"\n"); | |
6727 out_flush(); | |
6728 } | |
6729 } | |
6730 return didit; | |
6731 } | |
6732 | |
6733 /* | |
6734 * Move all the lines from buffer "frombuf" to buffer "tobuf". | |
6735 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not | |
6736 * empty. | |
6737 */ | |
6738 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6739 move_lines(buf_T *frombuf, buf_T *tobuf) |
7 | 6740 { |
6741 buf_T *tbuf = curbuf; | |
6742 int retval = OK; | |
6743 linenr_T lnum; | |
6744 char_u *p; | |
6745 | |
6746 /* Copy the lines in "frombuf" to "tobuf". */ | |
6747 curbuf = tobuf; | |
6748 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum) | |
6749 { | |
6750 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE)); | |
6751 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL) | |
6752 { | |
6753 vim_free(p); | |
6754 retval = FAIL; | |
6755 break; | |
6756 } | |
6757 vim_free(p); | |
6758 } | |
6759 | |
6760 /* Delete all the lines in "frombuf". */ | |
6761 if (retval != FAIL) | |
6762 { | |
6763 curbuf = frombuf; | |
1055 | 6764 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum) |
6765 if (ml_delete(lnum, FALSE) == FAIL) | |
7 | 6766 { |
6767 /* Oops! We could try putting back the saved lines, but that | |
6768 * might fail again... */ | |
6769 retval = FAIL; | |
6770 break; | |
6771 } | |
6772 } | |
6773 | |
6774 curbuf = tbuf; | |
6775 return retval; | |
6776 } | |
6777 | |
6778 /* | |
6779 * Check if buffer "buf" has been changed. | |
6780 * Also check if the file for a new buffer unexpectedly appeared. | |
6781 * return 1 if a changed buffer was found. | |
6782 * return 2 if a message has been displayed. | |
6783 * return 0 otherwise. | |
6784 */ | |
6785 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6786 buf_check_timestamp( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6787 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6788 int focus UNUSED) /* called for GUI focus event */ |
7 | 6789 { |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6790 stat_T st; |
7 | 6791 int stat_res; |
6792 int retval = 0; | |
6793 char_u *path; | |
6794 char_u *tbuf; | |
6795 char *mesg = NULL; | |
188 | 6796 char *mesg2 = ""; |
7 | 6797 int helpmesg = FALSE; |
6798 int reload = FALSE; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6799 char *reason; |
7 | 6800 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
6801 int can_reload = FALSE; | |
6802 #endif | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6803 off_T orig_size = buf->b_orig_size; |
7 | 6804 int orig_mode = buf->b_orig_mode; |
6805 #ifdef FEAT_GUI | |
6806 int save_mouse_correct = need_mouse_correct; | |
6807 #endif | |
6808 #ifdef FEAT_AUTOCMD | |
6809 static int busy = FALSE; | |
179 | 6810 int n; |
6811 char_u *s; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6812 bufref_T bufref; |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6813 |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6814 set_bufref(&bufref, buf); |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6815 #endif |
7 | 6816 |
6817 /* If there is no file name, the buffer is not loaded, 'buftype' is | |
6818 * set, we are in the middle of a save or being called recursively: ignore | |
6819 * this buffer. */ | |
6820 if (buf->b_ffname == NULL | |
6821 || buf->b_ml.ml_mfp == NULL | |
6822 #if defined(FEAT_QUICKFIX) | |
6823 || *buf->b_p_bt != NUL | |
6824 #endif | |
6825 || buf->b_saving | |
6826 #ifdef FEAT_AUTOCMD | |
6827 || busy | |
6828 #endif | |
33 | 6829 #ifdef FEAT_NETBEANS_INTG |
6830 || isNetbeansBuffer(buf) | |
6831 #endif | |
7 | 6832 ) |
6833 return 0; | |
6834 | |
6835 if ( !(buf->b_flags & BF_NOTEDITED) | |
6836 && buf->b_mtime != 0 | |
6837 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0 | |
6838 || time_differs((long)st.st_mtime, buf->b_mtime) | |
5863 | 6839 || st.st_size != buf->b_orig_size |
7 | 6840 #ifdef HAVE_ST_MODE |
6841 || (int)st.st_mode != buf->b_orig_mode | |
6842 #else | |
6843 || mch_getperm(buf->b_ffname) != buf->b_orig_mode | |
6844 #endif | |
6845 )) | |
6846 { | |
6847 retval = 1; | |
6848 | |
628 | 6849 /* set b_mtime to stop further warnings (e.g., when executing |
6850 * FileChangedShell autocmd) */ | |
7 | 6851 if (stat_res < 0) |
6852 { | |
6853 buf->b_mtime = 0; | |
6854 buf->b_orig_size = 0; | |
6855 buf->b_orig_mode = 0; | |
6856 } | |
6857 else | |
6858 buf_store_time(buf, &st, buf->b_ffname); | |
6859 | |
6860 /* Don't do anything for a directory. Might contain the file | |
6861 * explorer. */ | |
6862 if (mch_isdir(buf->b_fname)) | |
6863 ; | |
6864 | |
6865 /* | |
6866 * If 'autoread' is set, the buffer has no changes and the file still | |
6867 * exists, reload the buffer. Use the buffer-local option value if it | |
6868 * was set, the global option value otherwise. | |
6869 */ | |
6870 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar) | |
6871 && !bufIsChanged(buf) && stat_res >= 0) | |
6872 reload = TRUE; | |
6873 else | |
6874 { | |
179 | 6875 if (stat_res < 0) |
6876 reason = "deleted"; | |
6877 else if (bufIsChanged(buf)) | |
6878 reason = "conflict"; | |
6879 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf)) | |
6880 reason = "changed"; | |
6881 else if (orig_mode != buf->b_orig_mode) | |
6882 reason = "mode"; | |
6883 else | |
6884 reason = "time"; | |
6885 | |
7 | 6886 #ifdef FEAT_AUTOCMD |
6887 /* | |
6888 * Only give the warning if there are no FileChangedShell | |
6889 * autocommands. | |
6890 * Avoid being called recursively by setting "busy". | |
6891 */ | |
6892 busy = TRUE; | |
532 | 6893 # ifdef FEAT_EVAL |
179 | 6894 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1); |
6895 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1); | |
532 | 6896 # endif |
1834 | 6897 ++allbuf_lock; |
7 | 6898 n = apply_autocmds(EVENT_FILECHANGEDSHELL, |
6899 buf->b_fname, buf->b_fname, FALSE, buf); | |
1834 | 6900 --allbuf_lock; |
7 | 6901 busy = FALSE; |
6902 if (n) | |
6903 { | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6904 if (!bufref_valid(&bufref)) |
7 | 6905 EMSG(_("E246: FileChangedShell autocommand deleted buffer")); |
532 | 6906 # ifdef FEAT_EVAL |
179 | 6907 s = get_vim_var_str(VV_FCS_CHOICE); |
6908 if (STRCMP(s, "reload") == 0 && *reason != 'd') | |
6909 reload = TRUE; | |
6910 else if (STRCMP(s, "ask") == 0) | |
6911 n = FALSE; | |
6912 else | |
532 | 6913 # endif |
179 | 6914 return 2; |
7 | 6915 } |
179 | 6916 if (!n) |
7 | 6917 #endif |
6918 { | |
179 | 6919 if (*reason == 'd') |
6920 mesg = _("E211: File \"%s\" no longer available"); | |
7 | 6921 else |
6922 { | |
6923 helpmesg = TRUE; | |
6924 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
6925 can_reload = TRUE; | |
6926 #endif | |
6927 /* | |
6928 * Check if the file contents really changed to avoid | |
6929 * giving a warning when only the timestamp was set (e.g., | |
6930 * checked out of CVS). Always warn when the buffer was | |
6931 * changed. | |
6932 */ | |
179 | 6933 if (reason[2] == 'n') |
6934 { | |
7 | 6935 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well"); |
179 | 6936 mesg2 = _("See \":help W12\" for more info."); |
6937 } | |
6938 else if (reason[1] == 'h') | |
6939 { | |
7 | 6940 mesg = _("W11: Warning: File \"%s\" has changed since editing started"); |
179 | 6941 mesg2 = _("See \":help W11\" for more info."); |
6942 } | |
6943 else if (*reason == 'm') | |
6944 { | |
7 | 6945 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started"); |
179 | 6946 mesg2 = _("See \":help W16\" for more info."); |
6947 } | |
1913 | 6948 else |
6949 /* Only timestamp changed, store it to avoid a warning | |
6950 * in check_mtime() later. */ | |
6951 buf->b_mtime_read = buf->b_mtime; | |
7 | 6952 } |
6953 } | |
6954 } | |
6955 | |
6956 } | |
6957 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W) | |
6958 && vim_fexists(buf->b_ffname)) | |
6959 { | |
6960 retval = 1; | |
6961 mesg = _("W13: Warning: File \"%s\" has been created after editing started"); | |
6962 buf->b_flags |= BF_NEW_W; | |
6963 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
6964 can_reload = TRUE; | |
6965 #endif | |
6966 } | |
6967 | |
6968 if (mesg != NULL) | |
6969 { | |
6970 path = home_replace_save(buf, buf->b_fname); | |
6971 if (path != NULL) | |
6972 { | |
179 | 6973 if (!helpmesg) |
7 | 6974 mesg2 = ""; |
6975 tbuf = alloc((unsigned)(STRLEN(path) + STRLEN(mesg) | |
6976 + STRLEN(mesg2) + 2)); | |
6977 sprintf((char *)tbuf, mesg, path); | |
1848 | 6978 #ifdef FEAT_EVAL |
6979 /* Set warningmsg here, before the unimportant and output-specific | |
6980 * mesg2 has been appended. */ | |
6981 set_vim_var_string(VV_WARNINGMSG, tbuf, -1); | |
6982 #endif | |
7 | 6983 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
6984 if (can_reload) | |
6985 { | |
6986 if (*mesg2 != NUL) | |
6987 { | |
6988 STRCAT(tbuf, "\n"); | |
6989 STRCAT(tbuf, mesg2); | |
6990 } | |
6991 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"), tbuf, | |
2684 | 6992 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2) |
7 | 6993 reload = TRUE; |
6994 } | |
6995 else | |
6996 #endif | |
6997 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned) | |
6998 { | |
6999 if (*mesg2 != NUL) | |
7000 { | |
7001 STRCAT(tbuf, "; "); | |
7002 STRCAT(tbuf, mesg2); | |
7003 } | |
7004 EMSG(tbuf); | |
7005 retval = 2; | |
7006 } | |
7007 else | |
7008 { | |
8 | 7009 # ifdef FEAT_AUTOCMD |
7 | 7010 if (!autocmd_busy) |
8 | 7011 # endif |
7 | 7012 { |
7013 msg_start(); | |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
7014 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST); |
7 | 7015 if (*mesg2 != NUL) |
7016 msg_puts_attr((char_u *)mesg2, | |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
7017 HL_ATTR(HLF_W) + MSG_HIST); |
7 | 7018 msg_clr_eos(); |
7019 (void)msg_end(); | |
7020 if (emsg_silent == 0) | |
7021 { | |
7022 out_flush(); | |
8 | 7023 # ifdef FEAT_GUI |
7 | 7024 if (!focus) |
8 | 7025 # endif |
7 | 7026 /* give the user some time to think about it */ |
7027 ui_delay(1000L, TRUE); | |
7028 | |
7029 /* don't redraw and erase the message */ | |
7030 redraw_cmdline = FALSE; | |
7031 } | |
7032 } | |
7033 already_warned = TRUE; | |
7034 } | |
7035 | |
7036 vim_free(path); | |
7037 vim_free(tbuf); | |
7038 } | |
7039 } | |
7040 | |
7041 if (reload) | |
3776 | 7042 { |
311 | 7043 /* Reload the buffer. */ |
628 | 7044 buf_reload(buf, orig_mode); |
3776 | 7045 #ifdef FEAT_PERSISTENT_UNDO |
7046 if (buf->b_p_udf && buf->b_ffname != NULL) | |
7047 { | |
7048 char_u hash[UNDO_HASH_SIZE]; | |
7049 buf_T *save_curbuf = curbuf; | |
7050 | |
7051 /* Any existing undo file is unusable, write it now. */ | |
7052 curbuf = buf; | |
7053 u_compute_hash(hash); | |
7054 u_write_undo(NULL, FALSE, buf, hash); | |
7055 curbuf = save_curbuf; | |
7056 } | |
7057 #endif | |
7058 } | |
7 | 7059 |
765 | 7060 #ifdef FEAT_AUTOCMD |
1620 | 7061 /* 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
|
7062 if (bufref_valid(&bufref) && retval != 0) |
765 | 7063 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, |
7064 buf->b_fname, buf->b_fname, FALSE, buf); | |
7065 #endif | |
7 | 7066 #ifdef FEAT_GUI |
7067 /* restore this in case an autocommand has set it; it would break | |
7068 * 'mousefocus' */ | |
7069 need_mouse_correct = save_mouse_correct; | |
7070 #endif | |
7071 | |
7072 return retval; | |
7073 } | |
7074 | |
311 | 7075 /* |
7076 * Reload a buffer that is already loaded. | |
7077 * Used when the file was changed outside of Vim. | |
628 | 7078 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected. |
7079 * buf->b_orig_mode may have been reset already. | |
311 | 7080 */ |
7081 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7082 buf_reload(buf_T *buf, int orig_mode) |
311 | 7083 { |
7084 exarg_T ea; | |
7085 pos_T old_cursor; | |
7086 linenr_T old_topline; | |
7087 int old_ro = buf->b_p_ro; | |
7088 buf_T *savebuf; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
7089 bufref_T bufref; |
311 | 7090 int saved = OK; |
7091 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
|
7092 int flags = READ_NEW; |
311 | 7093 |
7094 /* set curwin/curbuf for "buf" and save some things */ | |
7095 aucmd_prepbuf(&aco, buf); | |
7096 | |
7097 /* We only want to read the text from the file, not reset the syntax | |
7098 * highlighting, clear marks, diff status, etc. Force the fileformat | |
7099 * and encoding to be the same. */ | |
7100 if (prep_exarg(&ea, buf) == OK) | |
7101 { | |
7102 old_cursor = curwin->w_cursor; | |
7103 old_topline = curwin->w_topline; | |
7104 | |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
7105 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
|
7106 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7107 /* 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
|
7108 * 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
|
7109 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
|
7110 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
|
7111 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
|
7112 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7113 |
311 | 7114 /* |
7115 * To behave like when a new file is edited (matters for | |
7116 * BufReadPost autocommands) we first need to delete the current | |
7117 * buffer contents. But if reading the file fails we should keep | |
7118 * the old contents. Can't use memory only, the file might be | |
7119 * too big. Use a hidden buffer to move the buffer contents to. | |
7120 */ | |
11121
778c10516955
patch 8.0.0448: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
10952
diff
changeset
|
7121 if (BUFEMPTY() || saved == FAIL) |
311 | 7122 savebuf = NULL; |
7123 else | |
7124 { | |
7125 /* Allocate a buffer without putting it in the buffer list. */ | |
7126 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
|
7127 set_bufref(&bufref, savebuf); |
837 | 7128 if (savebuf != NULL && buf == curbuf) |
311 | 7129 { |
7130 /* Open the memline. */ | |
7131 curbuf = savebuf; | |
7132 curwin->w_buffer = savebuf; | |
625 | 7133 saved = ml_open(curbuf); |
311 | 7134 curbuf = buf; |
7135 curwin->w_buffer = buf; | |
7136 } | |
837 | 7137 if (savebuf == NULL || saved == FAIL || buf != curbuf |
311 | 7138 || move_lines(buf, savebuf) == FAIL) |
7139 { | |
7140 EMSG2(_("E462: Could not prepare for reloading \"%s\""), | |
7141 buf->b_fname); | |
7142 saved = FAIL; | |
7143 } | |
7144 } | |
7145 | |
7146 if (saved == OK) | |
7147 { | |
7148 curbuf->b_flags |= BF_CHECK_RO; /* check for RO again */ | |
7149 #ifdef FEAT_AUTOCMD | |
7150 keep_filetype = TRUE; /* don't detect 'filetype' */ | |
7151 #endif | |
7152 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, | |
7153 (linenr_T)0, | |
10575
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
7154 (linenr_T)MAXLNUM, &ea, flags) != OK) |
311 | 7155 { |
7156 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
7157 if (!aborting()) | |
7158 #endif | |
7159 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
|
7160 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf) |
311 | 7161 { |
7162 /* Put the text back from the save buffer. First | |
7163 * delete any lines that readfile() added. */ | |
11121
778c10516955
patch 8.0.0448: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
10952
diff
changeset
|
7164 while (!BUFEMPTY()) |
837 | 7165 if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL) |
311 | 7166 break; |
7167 (void)move_lines(savebuf, buf); | |
7168 } | |
7169 } | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7170 else if (buf == curbuf) /* "buf" still valid */ |
311 | 7171 { |
7172 /* Mark the buffer as unmodified and free undo info. */ | |
7173 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
|
7174 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
|
7175 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7176 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
|
7177 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
|
7178 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7179 else |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
7180 { |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7181 /* 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
|
7182 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
|
7183 } |
311 | 7184 } |
7185 } | |
7186 vim_free(ea.cmd); | |
7187 | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
7188 if (savebuf != NULL && bufref_valid(&bufref)) |
311 | 7189 wipe_buffer(savebuf, FALSE); |
7190 | |
7191 #ifdef FEAT_DIFF | |
7192 /* Invalidate diff info if necessary. */ | |
837 | 7193 diff_invalidate(curbuf); |
311 | 7194 #endif |
7195 | |
7196 /* Restore the topline and cursor position and check it (lines may | |
7197 * have been removed). */ | |
7198 if (old_topline > curbuf->b_ml.ml_line_count) | |
7199 curwin->w_topline = curbuf->b_ml.ml_line_count; | |
7200 else | |
7201 curwin->w_topline = old_topline; | |
7202 curwin->w_cursor = old_cursor; | |
7203 check_cursor(); | |
7204 update_topline(); | |
7205 #ifdef FEAT_AUTOCMD | |
7206 keep_filetype = FALSE; | |
7207 #endif | |
7208 #ifdef FEAT_FOLDING | |
7209 { | |
1863 | 7210 win_T *wp; |
7211 tabpage_T *tp; | |
311 | 7212 |
7213 /* Update folds unless they are defined manually. */ | |
1863 | 7214 FOR_ALL_TAB_WINDOWS(tp, wp) |
311 | 7215 if (wp->w_buffer == curwin->w_buffer |
7216 && !foldmethodIsManual(wp)) | |
7217 foldUpdateAll(wp); | |
7218 } | |
7219 #endif | |
7220 /* If the mode didn't change and 'readonly' was set, keep the old | |
7221 * value; the user probably used the ":view" command. But don't | |
7222 * reset it, might have had a read error. */ | |
7223 if (orig_mode == curbuf->b_orig_mode) | |
7224 curbuf->b_p_ro |= old_ro; | |
4071 | 7225 |
7226 /* Modelines must override settings done by autocommands. */ | |
7227 do_modelines(0); | |
311 | 7228 } |
7229 | |
7230 /* restore curwin/curbuf and a few other things */ | |
7231 aucmd_restbuf(&aco); | |
7232 /* Careful: autocommands may have made "buf" invalid! */ | |
7233 } | |
7234 | |
7 | 7235 void |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
7236 buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED) |
7 | 7237 { |
7238 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
|
7239 buf->b_orig_size = st->st_size; |
7 | 7240 #ifdef HAVE_ST_MODE |
7241 buf->b_orig_mode = (int)st->st_mode; | |
7242 #else | |
7243 buf->b_orig_mode = mch_getperm(fname); | |
7244 #endif | |
7245 } | |
7246 | |
7247 /* | |
7248 * Adjust the line with missing eol, used for the next write. | |
7249 * Used for do_filter(), when the input lines for the filter are deleted. | |
7250 */ | |
7251 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7252 write_lnum_adjust(linenr_T offset) |
7 | 7253 { |
2707 | 7254 if (curbuf->b_no_eol_lnum != 0) /* only if there is a missing eol */ |
7255 curbuf->b_no_eol_lnum += offset; | |
7 | 7256 } |
7257 | |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7258 #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
|
7259 /* |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7260 * 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
|
7261 * 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
|
7262 */ |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7263 int |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7264 delete_recursive(char_u *name) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7265 { |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7266 int result = 0; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7267 char_u **files; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7268 int file_count; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7269 int i; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7270 char_u *exp; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7271 |
7629
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7272 /* 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
|
7273 * points to. */ |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7274 if ( |
7657
9c5e8254ea6b
commit https://github.com/vim/vim/commit/203258c3ad2966cc9d08b3805b103333988b30b7
Christian Brabandt <cb@256bit.org>
parents:
7641
diff
changeset
|
7275 # if defined(UNIX) || defined(WIN32) |
9c5e8254ea6b
commit https://github.com/vim/vim/commit/203258c3ad2966cc9d08b3805b103333988b30b7
Christian Brabandt <cb@256bit.org>
parents:
7641
diff
changeset
|
7276 mch_isrealdir(name) |
7629
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7277 # else |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7278 mch_isdir(name) |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7279 # endif |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7280 ) |
7615
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 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
|
7283 exp = vim_strsave(NameBuff); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7284 if (exp == NULL) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7285 return -1; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7286 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
|
7287 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
|
7288 { |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7289 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
|
7290 if (delete_recursive(files[i]) != 0) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7291 result = -1; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7292 FreeWild(file_count, files); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7293 } |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7294 else |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7295 result = -1; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7296 vim_free(exp); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7297 (void)mch_rmdir(name); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7298 } |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7299 else |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7300 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
|
7301 |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7302 return result; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7303 } |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7304 #endif |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7305 |
7 | 7306 #if defined(TEMPDIRNAMES) || defined(PROTO) |
7307 static long temp_count = 0; /* Temp filename counter. */ | |
7308 | |
7309 /* | |
7310 * Delete the temp directory and all files it contains. | |
7311 */ | |
7312 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7313 vim_deltempdir(void) |
7 | 7314 { |
7315 if (vim_tempdir != NULL) | |
7316 { | |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7317 /* remove the trailing path separator */ |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7318 gettail(vim_tempdir)[-1] = NUL; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7319 delete_recursive(vim_tempdir); |
7 | 7320 vim_free(vim_tempdir); |
7321 vim_tempdir = NULL; | |
7322 } | |
7323 } | |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7324 |
7 | 7325 /* |
1997 | 7326 * Directory "tempdir" was created. Expand this name to a full path and put |
7327 * it in "vim_tempdir". This avoids that using ":cd" would confuse us. | |
7328 * "tempdir" must be no longer than MAXPATHL. | |
7329 */ | |
7330 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7331 vim_settempdir(char_u *tempdir) |
1997 | 7332 { |
7333 char_u *buf; | |
7334 | |
7335 buf = alloc((unsigned)MAXPATHL + 2); | |
7336 if (buf != NULL) | |
7337 { | |
7338 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL) | |
7339 STRCPY(buf, tempdir); | |
9536
b2aada04d84e
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Christian Brabandt <cb@256bit.org>
parents:
9509
diff
changeset
|
7340 add_pathsep(buf); |
1997 | 7341 vim_tempdir = vim_strsave(buf); |
7342 vim_free(buf); | |
7343 } | |
7344 } | |
2006 | 7345 #endif |
1997 | 7346 |
7347 /* | |
7 | 7348 * vim_tempname(): Return a unique name that can be used for a temp file. |
7349 * | |
9291
bff3cd5cf922
commit https://github.com/vim/vim/commit/76ae22fef3cb224ca7fbf97517f881e825d4d0c2
Christian Brabandt <cb@256bit.org>
parents:
9260
diff
changeset
|
7350 * 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
|
7351 * guaranteed to NOT be created. |
7 | 7352 * |
7353 * The returned pointer is to allocated memory. | |
7354 * The returned pointer is NULL if no valid name was found. | |
7355 */ | |
7356 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7357 vim_tempname( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7358 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
|
7359 int keep UNUSED) |
7 | 7360 { |
7361 #ifdef USE_TMPNAM | |
7362 char_u itmp[L_tmpnam]; /* use tmpnam() */ | |
7363 #else | |
7364 char_u itmp[TEMPNAMELEN]; | |
7365 #endif | |
7366 | |
7367 #ifdef TEMPDIRNAMES | |
7368 static char *(tempdirs[]) = {TEMPDIRNAMES}; | |
7369 int i; | |
7370 # ifndef EEXIST | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
7371 stat_T st; |
7 | 7372 # endif |
7373 | |
7374 /* | |
7375 * This will create a directory for private use by this instance of Vim. | |
7376 * This is done once, and the same directory is used for all temp files. | |
7377 * This method avoids security problems because of symlink attacks et al. | |
7378 * It's also a bit faster, because we only need to check for an existing | |
7379 * file when creating the directory and not for each temp file. | |
7380 */ | |
7381 if (vim_tempdir == NULL) | |
7382 { | |
7383 /* | |
7384 * Try the entries in TEMPDIRNAMES to create the temp directory. | |
7385 */ | |
1877 | 7386 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i) |
7 | 7387 { |
2047
85da03763130
updated for version 7.2.333
Bram Moolenaar <bram@zimbu.org>
parents:
2028
diff
changeset
|
7388 # ifndef HAVE_MKDTEMP |
1997 | 7389 size_t itmplen; |
7390 long nr; | |
7391 long off; | |
7392 # endif | |
7393 | |
7305
372c785c04b6
commit https://github.com/vim/vim/commit/e1a61991d9b6fd5f65636d17583f93118268cda5
Christian Brabandt <cb@256bit.org>
parents:
7285
diff
changeset
|
7394 /* 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
|
7395 * Skip the directory check if the expansion fails. */ |
7 | 7396 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
|
7397 if (itmp[0] != '$' && mch_isdir(itmp)) |
372c785c04b6
commit https://github.com/vim/vim/commit/e1a61991d9b6fd5f65636d17583f93118268cda5
Christian Brabandt <cb@256bit.org>
parents:
7285
diff
changeset
|
7398 { |
372c785c04b6
commit https://github.com/vim/vim/commit/e1a61991d9b6fd5f65636d17583f93118268cda5
Christian Brabandt <cb@256bit.org>
parents:
7285
diff
changeset
|
7399 /* directory exists */ |
9536
b2aada04d84e
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Christian Brabandt <cb@256bit.org>
parents:
9509
diff
changeset
|
7400 add_pathsep(itmp); |
1997 | 7401 |
7402 # ifdef HAVE_MKDTEMP | |
9211
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7403 { |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7404 # if defined(UNIX) || defined(VMS) |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7405 /* 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
|
7406 * "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
|
7407 mode_t umask_save = umask(077); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7408 # endif |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7409 /* Leave room for filename */ |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7410 STRCAT(itmp, "vXXXXXX"); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7411 if (mkdtemp((char *)itmp) != NULL) |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7412 vim_settempdir(itmp); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7413 # if defined(UNIX) || defined(VMS) |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7414 (void)umask(umask_save); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7415 # endif |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7416 } |
1997 | 7417 # else |
7 | 7418 /* Get an arbitrary number of up to 6 digits. When it's |
7419 * unlikely that it already exists it will be faster, | |
7420 * otherwise it doesn't matter. The use of mkdir() avoids any | |
7421 * security problems because of the predictable number. */ | |
7422 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
|
7423 itmplen = STRLEN(itmp); |
7 | 7424 |
7425 /* Try up to 10000 different values until we find a name that | |
7426 * doesn't exist. */ | |
7427 for (off = 0; off < 10000L; ++off) | |
7428 { | |
7429 int r; | |
1997 | 7430 # if defined(UNIX) || defined(VMS) |
7 | 7431 mode_t umask_save; |
1997 | 7432 # endif |
7433 | |
7434 sprintf((char *)itmp + itmplen, "v%ld", nr + off); | |
7435 # ifndef EEXIST | |
7 | 7436 /* If mkdir() does not set errno to EEXIST, check for |
7437 * existing file here. There is a race condition then, | |
7438 * although it's fail-safe. */ | |
7439 if (mch_stat((char *)itmp, &st) >= 0) | |
7440 continue; | |
1997 | 7441 # endif |
7442 # if defined(UNIX) || defined(VMS) | |
7 | 7443 /* Make sure the umask doesn't remove the executable bit. |
7444 * "repl" has been reported to use "177". */ | |
7445 umask_save = umask(077); | |
1997 | 7446 # endif |
7 | 7447 r = vim_mkdir(itmp, 0700); |
1997 | 7448 # if defined(UNIX) || defined(VMS) |
7 | 7449 (void)umask(umask_save); |
1997 | 7450 # endif |
7 | 7451 if (r == 0) |
7452 { | |
1997 | 7453 vim_settempdir(itmp); |
7 | 7454 break; |
7455 } | |
1997 | 7456 # ifdef EEXIST |
7 | 7457 /* If the mkdir() didn't fail because the file/dir exists, |
7458 * we probably can't create any dir here, try another | |
7459 * place. */ | |
7460 if (errno != EEXIST) | |
1997 | 7461 # endif |
7 | 7462 break; |
7463 } | |
1997 | 7464 # endif /* HAVE_MKDTEMP */ |
7 | 7465 if (vim_tempdir != NULL) |
7466 break; | |
7467 } | |
7468 } | |
7469 } | |
7470 | |
7471 if (vim_tempdir != NULL) | |
7472 { | |
7473 /* There is no need to check if the file exists, because we own the | |
7474 * directory and nobody else creates a file in it. */ | |
7475 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++); | |
7476 return vim_strsave(itmp); | |
7477 } | |
7478 | |
7479 return NULL; | |
7480 | |
7481 #else /* TEMPDIRNAMES */ | |
7482 | |
7483 # ifdef WIN3264 | |
7484 char szTempFile[_MAX_PATH + 1]; | |
7485 char buf4[4]; | |
7486 char_u *retval; | |
7487 char_u *p; | |
7488 | |
7489 STRCPY(itmp, ""); | |
7490 if (GetTempPath(_MAX_PATH, szTempFile) == 0) | |
2695 | 7491 { |
7492 szTempFile[0] = '.'; /* GetTempPath() failed, use current dir */ | |
7493 szTempFile[1] = NUL; | |
7494 } | |
7 | 7495 strcpy(buf4, "VIM"); |
7496 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
|
7497 if (GetTempFileName(szTempFile, buf4, 0, (LPSTR)itmp) == 0) |
7 | 7498 return NULL; |
6721 | 7499 if (!keep) |
7500 /* 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
|
7501 (void)DeleteFile((LPSTR)itmp); |
7 | 7502 |
7503 /* Backslashes in a temp file name cause problems when filtering with | |
7504 * "sh". NOTE: This also checks 'shellcmdflag' to help those people who | |
7505 * didn't set 'shellslash'. */ | |
7506 retval = vim_strsave(itmp); | |
7507 if (*p_shcf == '-' || p_ssl) | |
7508 for (p = retval; *p; ++p) | |
7509 if (*p == '\\') | |
7510 *p = '/'; | |
7511 return retval; | |
7512 | |
7513 # else /* WIN3264 */ | |
7514 | |
7515 # ifdef USE_TMPNAM | |
2697 | 7516 char_u *p; |
7517 | |
7 | 7518 /* tmpnam() will make its own name */ |
2697 | 7519 p = tmpnam((char *)itmp); |
7520 if (p == NULL || *p == NUL) | |
7 | 7521 return NULL; |
7522 # else | |
7523 char_u *p; | |
7524 | |
7525 # ifdef VMS_TEMPNAM | |
7526 /* mktemp() is not working on VMS. It seems to be | |
7527 * a do-nothing function. Therefore we use tempnam(). | |
7528 */ | |
7529 sprintf((char *)itmp, "VIM%c", extra_char); | |
7530 p = (char_u *)tempnam("tmp:", (char *)itmp); | |
7531 if (p != NULL) | |
7532 { | |
5704 | 7533 /* VMS will use '.LIS' if we don't explicitly specify an extension, |
7 | 7534 * and VIM will then be unable to find the file later */ |
7535 STRCPY(itmp, p); | |
7536 STRCAT(itmp, ".txt"); | |
7537 free(p); | |
7538 } | |
7539 else | |
7540 return NULL; | |
7541 # else | |
7542 STRCPY(itmp, TEMPNAME); | |
7543 if ((p = vim_strchr(itmp, '?')) != NULL) | |
7544 *p = extra_char; | |
7545 if (mktemp((char *)itmp) == NULL) | |
7546 return NULL; | |
7547 # endif | |
7548 # endif | |
7549 | |
7550 return vim_strsave(itmp); | |
7551 # endif /* WIN3264 */ | |
7552 #endif /* TEMPDIRNAMES */ | |
7553 } | |
7554 | |
7555 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) | |
7556 /* | |
7170
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
7557 * 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
|
7558 * it looks like a URL. |
7 | 7559 */ |
7560 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7561 forward_slash(char_u *fname) |
7 | 7562 { |
7563 char_u *p; | |
7564 | |
7170
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
7565 if (path_with_url(fname)) |
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
7566 return; |
7 | 7567 for (p = fname; *p != NUL; ++p) |
7568 # ifdef FEAT_MBYTE | |
7569 /* The Big5 encoding can have '\' in the trail byte. */ | |
474 | 7570 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 7571 ++p; |
7572 else | |
7573 # endif | |
7574 if (*p == '\\') | |
7575 *p = '/'; | |
7576 } | |
7577 #endif | |
7578 | |
7579 | |
7580 /* | |
7581 * Code for automatic commands. | |
7582 * | |
7583 * Only included when "FEAT_AUTOCMD" has been defined. | |
7584 */ | |
7585 | |
7586 #if defined(FEAT_AUTOCMD) || defined(PROTO) | |
7587 | |
7588 /* | |
7589 * The autocommands are stored in a list for each event. | |
7590 * Autocommands for the same pattern, that are consecutive, are joined | |
7591 * together, to avoid having to match the pattern too often. | |
7592 * The result is an array of Autopat lists, which point to AutoCmd lists: | |
7593 * | |
7594 * first_autopat[0] --> Autopat.next --> Autopat.next --> NULL | |
7595 * Autopat.cmds Autopat.cmds | |
7596 * | | | |
7597 * V V | |
7598 * AutoCmd.next AutoCmd.next | |
7599 * | | | |
7600 * V V | |
7601 * AutoCmd.next NULL | |
7602 * | | |
7603 * V | |
7604 * NULL | |
7605 * | |
7606 * first_autopat[1] --> Autopat.next --> NULL | |
7607 * Autopat.cmds | |
7608 * | | |
7609 * V | |
7610 * AutoCmd.next | |
7611 * | | |
7612 * V | |
7613 * NULL | |
7614 * etc. | |
7615 * | |
7616 * The order of AutoCmds is important, this is the order in which they were | |
7617 * defined and will have to be executed. | |
7618 */ | |
7619 typedef struct AutoCmd | |
7620 { | |
7621 char_u *cmd; /* The command to be executed (NULL | |
7622 when command has been removed) */ | |
7623 char nested; /* If autocommands nest here */ | |
7624 char last; /* last command in list */ | |
7625 #ifdef FEAT_EVAL | |
7626 scid_T scriptID; /* script ID where defined */ | |
7627 #endif | |
7628 struct AutoCmd *next; /* Next AutoCmd in list */ | |
7629 } AutoCmd; | |
7630 | |
7631 typedef struct AutoPat | |
7632 { | |
7633 char_u *pat; /* pattern as typed (NULL when pattern | |
7634 has been removed) */ | |
4861
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7635 regprog_T *reg_prog; /* compiled regprog for pattern */ |
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7636 AutoCmd *cmds; /* list of commands to do */ |
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7637 struct AutoPat *next; /* next AutoPat in AutoPat list */ |
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7638 int group; /* group ID */ |
7 | 7639 int patlen; /* strlen() of pat */ |
4861
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7640 int buflocal_nr; /* !=0 for buffer-local AutoPat */ |
7 | 7641 char allow_dirs; /* Pattern may match whole path */ |
7642 char last; /* last pattern for apply_autocmds() */ | |
7643 } AutoPat; | |
7644 | |
7645 static struct event_name | |
7646 { | |
7647 char *name; /* event name */ | |
661 | 7648 event_T event; /* event number */ |
7 | 7649 } event_names[] = |
7650 { | |
7651 {"BufAdd", EVENT_BUFADD}, | |
7652 {"BufCreate", EVENT_BUFADD}, | |
7653 {"BufDelete", EVENT_BUFDELETE}, | |
7654 {"BufEnter", EVENT_BUFENTER}, | |
7655 {"BufFilePost", EVENT_BUFFILEPOST}, | |
7656 {"BufFilePre", EVENT_BUFFILEPRE}, | |
7657 {"BufHidden", EVENT_BUFHIDDEN}, | |
7658 {"BufLeave", EVENT_BUFLEAVE}, | |
7659 {"BufNew", EVENT_BUFNEW}, | |
7660 {"BufNewFile", EVENT_BUFNEWFILE}, | |
7661 {"BufRead", EVENT_BUFREADPOST}, | |
7662 {"BufReadCmd", EVENT_BUFREADCMD}, | |
7663 {"BufReadPost", EVENT_BUFREADPOST}, | |
7664 {"BufReadPre", EVENT_BUFREADPRE}, | |
7665 {"BufUnload", EVENT_BUFUNLOAD}, | |
7666 {"BufWinEnter", EVENT_BUFWINENTER}, | |
7667 {"BufWinLeave", EVENT_BUFWINLEAVE}, | |
7668 {"BufWipeout", EVENT_BUFWIPEOUT}, | |
7669 {"BufWrite", EVENT_BUFWRITEPRE}, | |
7670 {"BufWritePost", EVENT_BUFWRITEPOST}, | |
7671 {"BufWritePre", EVENT_BUFWRITEPRE}, | |
7672 {"BufWriteCmd", EVENT_BUFWRITECMD}, | |
7673 {"CmdwinEnter", EVENT_CMDWINENTER}, | |
7674 {"CmdwinLeave", EVENT_CMDWINLEAVE}, | |
6154 | 7675 {"CmdUndefined", EVENT_CMDUNDEFINED}, |
12 | 7676 {"ColorScheme", EVENT_COLORSCHEME}, |
3676 | 7677 {"CompleteDone", EVENT_COMPLETEDONE}, |
661 | 7678 {"CursorHold", EVENT_CURSORHOLD}, |
7679 {"CursorHoldI", EVENT_CURSORHOLDI}, | |
7680 {"CursorMoved", EVENT_CURSORMOVED}, | |
7681 {"CursorMovedI", EVENT_CURSORMOVEDI}, | |
7 | 7682 {"EncodingChanged", EVENT_ENCODINGCHANGED}, |
7683 {"FileEncoding", EVENT_ENCODINGCHANGED}, | |
7684 {"FileAppendPost", EVENT_FILEAPPENDPOST}, | |
7685 {"FileAppendPre", EVENT_FILEAPPENDPRE}, | |
7686 {"FileAppendCmd", EVENT_FILEAPPENDCMD}, | |
7687 {"FileChangedShell",EVENT_FILECHANGEDSHELL}, | |
765 | 7688 {"FileChangedShellPost",EVENT_FILECHANGEDSHELLPOST}, |
7 | 7689 {"FileChangedRO", EVENT_FILECHANGEDRO}, |
7690 {"FileReadPost", EVENT_FILEREADPOST}, | |
7691 {"FileReadPre", EVENT_FILEREADPRE}, | |
7692 {"FileReadCmd", EVENT_FILEREADCMD}, | |
7693 {"FileType", EVENT_FILETYPE}, | |
7694 {"FileWritePost", EVENT_FILEWRITEPOST}, | |
7695 {"FileWritePre", EVENT_FILEWRITEPRE}, | |
7696 {"FileWriteCmd", EVENT_FILEWRITECMD}, | |
7697 {"FilterReadPost", EVENT_FILTERREADPOST}, | |
7698 {"FilterReadPre", EVENT_FILTERREADPRE}, | |
7699 {"FilterWritePost", EVENT_FILTERWRITEPOST}, | |
7700 {"FilterWritePre", EVENT_FILTERWRITEPRE}, | |
7701 {"FocusGained", EVENT_FOCUSGAINED}, | |
7702 {"FocusLost", EVENT_FOCUSLOST}, | |
7703 {"FuncUndefined", EVENT_FUNCUNDEFINED}, | |
7704 {"GUIEnter", EVENT_GUIENTER}, | |
946 | 7705 {"GUIFailed", EVENT_GUIFAILED}, |
11 | 7706 {"InsertChange", EVENT_INSERTCHANGE}, |
7707 {"InsertEnter", EVENT_INSERTENTER}, | |
7708 {"InsertLeave", EVENT_INSERTLEAVE}, | |
2845 | 7709 {"InsertCharPre", EVENT_INSERTCHARPRE}, |
434 | 7710 {"MenuPopup", EVENT_MENUPOPUP}, |
6935 | 7711 {"OptionSet", EVENT_OPTIONSET}, |
161 | 7712 {"QuickFixCmdPost", EVENT_QUICKFIXCMDPOST}, |
7713 {"QuickFixCmdPre", EVENT_QUICKFIXCMDPRE}, | |
3568 | 7714 {"QuitPre", EVENT_QUITPRE}, |
7 | 7715 {"RemoteReply", EVENT_REMOTEREPLY}, |
574 | 7716 {"SessionLoadPost", EVENT_SESSIONLOADPOST}, |
723 | 7717 {"ShellCmdPost", EVENT_SHELLCMDPOST}, |
7718 {"ShellFilterPost", EVENT_SHELLFILTERPOST}, | |
715 | 7719 {"SourcePre", EVENT_SOURCEPRE}, |
1061 | 7720 {"SourceCmd", EVENT_SOURCECMD}, |
649 | 7721 {"SpellFileMissing",EVENT_SPELLFILEMISSING}, |
7 | 7722 {"StdinReadPost", EVENT_STDINREADPOST}, |
7723 {"StdinReadPre", EVENT_STDINREADPRE}, | |
674 | 7724 {"SwapExists", EVENT_SWAPEXISTS}, |
7 | 7725 {"Syntax", EVENT_SYNTAX}, |
9595
0190d5de215f
commit https://github.com/vim/vim/commit/c917da4b3e8801a255dbefea8e4ed19c1c716dd8
Christian Brabandt <cb@256bit.org>
parents:
9536
diff
changeset
|
7726 {"TabNew", EVENT_TABNEW}, |
9599
42a8a81decdf
commit https://github.com/vim/vim/commit/12c11d553053f5a9eae9eb3c518279b12fa928c2
Christian Brabandt <cb@256bit.org>
parents:
9595
diff
changeset
|
7727 {"TabClosed", EVENT_TABCLOSED}, |
676 | 7728 {"TabEnter", EVENT_TABENTER}, |
7729 {"TabLeave", EVENT_TABLEAVE}, | |
7 | 7730 {"TermChanged", EVENT_TERMCHANGED}, |
7731 {"TermResponse", EVENT_TERMRESPONSE}, | |
4232 | 7732 {"TextChanged", EVENT_TEXTCHANGED}, |
7733 {"TextChangedI", EVENT_TEXTCHANGEDI}, | |
7 | 7734 {"User", EVENT_USER}, |
7735 {"VimEnter", EVENT_VIMENTER}, | |
7736 {"VimLeave", EVENT_VIMLEAVE}, | |
7737 {"VimLeavePre", EVENT_VIMLEAVEPRE}, | |
9595
0190d5de215f
commit https://github.com/vim/vim/commit/c917da4b3e8801a255dbefea8e4ed19c1c716dd8
Christian Brabandt <cb@256bit.org>
parents:
9536
diff
changeset
|
7738 {"WinNew", EVENT_WINNEW}, |
7 | 7739 {"WinEnter", EVENT_WINENTER}, |
7740 {"WinLeave", EVENT_WINLEAVE}, | |
765 | 7741 {"VimResized", EVENT_VIMRESIZED}, |
661 | 7742 {NULL, (event_T)0} |
7 | 7743 }; |
7744 | |
7745 static AutoPat *first_autopat[NUM_EVENTS] = | |
7746 { | |
7747 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7748 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7749 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7750 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
18 | 7751 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
6935 | 7752 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
7 | 7753 }; |
7754 | |
7755 /* | |
7756 * struct used to keep status while executing autocommands for an event. | |
7757 */ | |
7758 typedef struct AutoPatCmd | |
7759 { | |
7760 AutoPat *curpat; /* next AutoPat to examine */ | |
7761 AutoCmd *nextcmd; /* next AutoCmd to execute */ | |
7762 int group; /* group being used */ | |
7763 char_u *fname; /* fname to match with */ | |
7764 char_u *sfname; /* sfname to match with */ | |
7765 char_u *tail; /* tail of fname */ | |
661 | 7766 event_T event; /* current event */ |
40 | 7767 int arg_bufnr; /* initially equal to <abuf>, set to zero when |
7768 buf is deleted */ | |
7769 struct AutoPatCmd *next; /* chain of active apc-s for auto-invalidation*/ | |
7 | 7770 } AutoPatCmd; |
7771 | |
297 | 7772 static AutoPatCmd *active_apc_list = NULL; /* stack of active autocommands */ |
40 | 7773 |
7 | 7774 /* |
7775 * augroups stores a list of autocmd group names. | |
7776 */ | |
297 | 7777 static garray_T augroups = {0, 0, sizeof(char_u *), 10, NULL}; |
7 | 7778 #define AUGROUP_NAME(i) (((char_u **)augroups.ga_data)[i]) |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7779 /* use get_deleted_augroup() to get this */ |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
7780 static char_u *deleted_augroup = NULL; |
7 | 7781 |
7782 /* | |
7783 * The ID of the current group. Group 0 is the default one. | |
7784 */ | |
7785 static int current_augroup = AUGROUP_DEFAULT; | |
7786 | |
7787 static int au_need_clean = FALSE; /* need to delete marked patterns */ | |
7788 | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7789 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
|
7790 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
|
7791 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
|
7792 static void au_cleanup(void); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7793 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
|
7794 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
|
7795 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
|
7796 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
|
7797 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
|
7798 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
|
7799 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
|
7800 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
|
7801 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
|
7802 static void auto_next_pat(AutoPatCmd *apc, int stop_at_last); |
6375 | 7803 #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
|
7804 static int match_file_pat(char_u *pattern, regprog_T **prog, char_u *fname, char_u *sfname, char_u *tail, int allow_dirs); |
6375 | 7805 #endif |
7 | 7806 |
40 | 7807 |
661 | 7808 static event_T last_event; |
7 | 7809 static int last_group; |
1410 | 7810 static int autocmd_blocked = 0; /* block all autocmds */ |
7 | 7811 |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7812 static char_u * |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7813 get_deleted_augroup(void) |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7814 { |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7815 if (deleted_augroup == NULL) |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7816 deleted_augroup = (char_u *)_("--Deleted--"); |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7817 return deleted_augroup; |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7818 } |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7819 |
7 | 7820 /* |
7821 * Show the autocommands for one AutoPat. | |
7822 */ | |
7823 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7824 show_autocmd(AutoPat *ap, event_T event) |
7 | 7825 { |
7826 AutoCmd *ac; | |
7827 | |
7828 /* Check for "got_int" (here and at various places below), which is set | |
7829 * when "q" has been hit for the "--more--" prompt */ | |
7830 if (got_int) | |
7831 return; | |
7832 if (ap->pat == NULL) /* pattern has been removed */ | |
7833 return; | |
7834 | |
7835 msg_putchar('\n'); | |
7836 if (got_int) | |
7837 return; | |
7838 if (event != last_event || ap->group != last_group) | |
7839 { | |
7840 if (ap->group != AUGROUP_DEFAULT) | |
7841 { | |
7842 if (AUGROUP_NAME(ap->group) == NULL) | |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
7843 msg_puts_attr(get_deleted_augroup(), HL_ATTR(HLF_E)); |
7 | 7844 else |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
7845 msg_puts_attr(AUGROUP_NAME(ap->group), HL_ATTR(HLF_T)); |
7 | 7846 msg_puts((char_u *)" "); |
7847 } | |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
7848 msg_puts_attr(event_nr2name(event), HL_ATTR(HLF_T)); |
7 | 7849 last_event = event; |
7850 last_group = ap->group; | |
7851 msg_putchar('\n'); | |
7852 if (got_int) | |
7853 return; | |
7854 } | |
7855 msg_col = 4; | |
7856 msg_outtrans(ap->pat); | |
7857 | |
7858 for (ac = ap->cmds; ac != NULL; ac = ac->next) | |
7859 { | |
7860 if (ac->cmd != NULL) /* skip removed commands */ | |
7861 { | |
7862 if (msg_col >= 14) | |
7863 msg_putchar('\n'); | |
7864 msg_col = 14; | |
7865 if (got_int) | |
7866 return; | |
7867 msg_outtrans(ac->cmd); | |
500 | 7868 #ifdef FEAT_EVAL |
7869 if (p_verbose > 0) | |
7870 last_set_msg(ac->scriptID); | |
7871 #endif | |
7 | 7872 if (got_int) |
7873 return; | |
7874 if (ac->next != NULL) | |
7875 { | |
7876 msg_putchar('\n'); | |
7877 if (got_int) | |
7878 return; | |
7879 } | |
7880 } | |
7881 } | |
7882 } | |
7883 | |
7884 /* | |
7885 * Mark an autocommand pattern for deletion. | |
7886 */ | |
7887 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7888 au_remove_pat(AutoPat *ap) |
7 | 7889 { |
7890 vim_free(ap->pat); | |
7891 ap->pat = NULL; | |
40 | 7892 ap->buflocal_nr = -1; |
7 | 7893 au_need_clean = TRUE; |
7894 } | |
7895 | |
7896 /* | |
7897 * Mark all commands for a pattern for deletion. | |
7898 */ | |
7899 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7900 au_remove_cmds(AutoPat *ap) |
7 | 7901 { |
7902 AutoCmd *ac; | |
7903 | |
7904 for (ac = ap->cmds; ac != NULL; ac = ac->next) | |
7905 { | |
7906 vim_free(ac->cmd); | |
7907 ac->cmd = NULL; | |
7908 } | |
7909 au_need_clean = TRUE; | |
7910 } | |
7911 | |
7912 /* | |
7913 * Cleanup autocommands and patterns that have been deleted. | |
7914 * This is only done when not executing autocommands. | |
7915 */ | |
7916 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7917 au_cleanup(void) |
7 | 7918 { |
7919 AutoPat *ap, **prev_ap; | |
7920 AutoCmd *ac, **prev_ac; | |
661 | 7921 event_T event; |
7 | 7922 |
7923 if (autocmd_busy || !au_need_clean) | |
7924 return; | |
7925 | |
7926 /* loop over all events */ | |
661 | 7927 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
7928 event = (event_T)((int)event + 1)) | |
7 | 7929 { |
7930 /* loop over all autocommand patterns */ | |
7931 prev_ap = &(first_autopat[(int)event]); | |
7932 for (ap = *prev_ap; ap != NULL; ap = *prev_ap) | |
7933 { | |
7934 /* loop over all commands for this pattern */ | |
7935 prev_ac = &(ap->cmds); | |
7936 for (ac = *prev_ac; ac != NULL; ac = *prev_ac) | |
7937 { | |
7938 /* remove the command if the pattern is to be deleted or when | |
7939 * the command has been marked for deletion */ | |
7940 if (ap->pat == NULL || ac->cmd == NULL) | |
7941 { | |
7942 *prev_ac = ac->next; | |
7943 vim_free(ac->cmd); | |
7944 vim_free(ac); | |
7945 } | |
7946 else | |
7947 prev_ac = &(ac->next); | |
7948 } | |
7949 | |
7950 /* remove the pattern if it has been marked for deletion */ | |
7951 if (ap->pat == NULL) | |
7952 { | |
7953 *prev_ap = ap->next; | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4354
diff
changeset
|
7954 vim_regfree(ap->reg_prog); |
7 | 7955 vim_free(ap); |
7956 } | |
7957 else | |
7958 prev_ap = &(ap->next); | |
7959 } | |
7960 } | |
7961 | |
7962 au_need_clean = FALSE; | |
7963 } | |
7964 | |
7965 /* | |
40 | 7966 * Called when buffer is freed, to remove/invalidate related buffer-local |
7967 * autocmds. | |
7968 */ | |
7969 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7970 aubuflocal_remove(buf_T *buf) |
40 | 7971 { |
7972 AutoPat *ap; | |
661 | 7973 event_T event; |
40 | 7974 AutoPatCmd *apc; |
7975 | |
7976 /* invalidate currently executing autocommands */ | |
7977 for (apc = active_apc_list; apc; apc = apc->next) | |
7978 if (buf->b_fnum == apc->arg_bufnr) | |
7979 apc->arg_bufnr = 0; | |
7980 | |
7981 /* invalidate buflocals looping through events */ | |
661 | 7982 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
7983 event = (event_T)((int)event + 1)) | |
40 | 7984 /* loop over all autocommand patterns */ |
7985 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
7986 if (ap->buflocal_nr == buf->b_fnum) | |
7987 { | |
7988 au_remove_pat(ap); | |
7989 if (p_verbose >= 6) | |
292 | 7990 { |
7991 verbose_enter(); | |
40 | 7992 smsg((char_u *) |
7993 _("auto-removing autocommand: %s <buffer=%d>"), | |
7994 event_nr2name(event), buf->b_fnum); | |
292 | 7995 verbose_leave(); |
7996 } | |
40 | 7997 } |
7998 au_cleanup(); | |
7999 } | |
8000 | |
8001 /* | |
7 | 8002 * Add an autocmd group name. |
8003 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. | |
8004 */ | |
8005 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8006 au_new_group(char_u *name) |
7 | 8007 { |
8008 int i; | |
8009 | |
8010 i = au_find_group(name); | |
8011 if (i == AUGROUP_ERROR) /* the group doesn't exist yet, add it */ | |
8012 { | |
8013 /* First try using a free entry. */ | |
8014 for (i = 0; i < augroups.ga_len; ++i) | |
8015 if (AUGROUP_NAME(i) == NULL) | |
8016 break; | |
8017 if (i == augroups.ga_len && ga_grow(&augroups, 1) == FAIL) | |
8018 return AUGROUP_ERROR; | |
8019 | |
8020 AUGROUP_NAME(i) = vim_strsave(name); | |
8021 if (AUGROUP_NAME(i) == NULL) | |
8022 return AUGROUP_ERROR; | |
8023 if (i == augroups.ga_len) | |
8024 ++augroups.ga_len; | |
8025 } | |
8026 | |
8027 return i; | |
8028 } | |
8029 | |
8030 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8031 au_del_group(char_u *name) |
7 | 8032 { |
8033 int i; | |
8034 | |
8035 i = au_find_group(name); | |
8036 if (i == AUGROUP_ERROR) /* the group doesn't exist */ | |
8037 EMSG2(_("E367: No such group: \"%s\""), name); | |
10086
1de911ef1edf
commit https://github.com/vim/vim/commit/de653f08805dde14424d417502a0480a6ad292f8
Christian Brabandt <cb@256bit.org>
parents:
10084
diff
changeset
|
8038 else if (i == current_augroup) |
1de911ef1edf
commit https://github.com/vim/vim/commit/de653f08805dde14424d417502a0480a6ad292f8
Christian Brabandt <cb@256bit.org>
parents:
10084
diff
changeset
|
8039 EMSG(_("E936: Cannot delete the current group")); |
7 | 8040 else |
8041 { | |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8042 event_T event; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8043 AutoPat *ap; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8044 int in_use = FALSE; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8045 |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8046 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8047 event = (event_T)((int)event + 1)) |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8048 { |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8049 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) |
10058
65e43481d7de
commit https://github.com/vim/vim/commit/5c80908ced601be6db7554a147cdb0f98ac8daa1
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
8050 if (ap->group == i && ap->pat != NULL) |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8051 { |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8052 give_warning((char_u *)_("W19: Deleting augroup that is still in use"), TRUE); |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8053 in_use = TRUE; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8054 event = NUM_EVENTS; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8055 break; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8056 } |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8057 } |
7 | 8058 vim_free(AUGROUP_NAME(i)); |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8059 if (in_use) |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8060 { |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
8061 AUGROUP_NAME(i) = get_deleted_augroup(); |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8062 } |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8063 else |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8064 AUGROUP_NAME(i) = NULL; |
7 | 8065 } |
8066 } | |
8067 | |
8068 /* | |
8069 * Find the ID of an autocmd group name. | |
8070 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. | |
8071 */ | |
8072 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8073 au_find_group(char_u *name) |
7 | 8074 { |
8075 int i; | |
8076 | |
8077 for (i = 0; i < augroups.ga_len; ++i) | |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
8078 if (AUGROUP_NAME(i) != NULL && AUGROUP_NAME(i) != get_deleted_augroup() |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8079 && STRCMP(AUGROUP_NAME(i), name) == 0) |
7 | 8080 return i; |
8081 return AUGROUP_ERROR; | |
8082 } | |
8083 | |
461 | 8084 /* |
8085 * Return TRUE if augroup "name" exists. | |
8086 */ | |
8087 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8088 au_has_group(char_u *name) |
461 | 8089 { |
8090 return au_find_group(name) != AUGROUP_ERROR; | |
8091 } | |
8092 | |
7 | 8093 /* |
8094 * ":augroup {name}". | |
8095 */ | |
8096 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8097 do_augroup(char_u *arg, int del_group) |
7 | 8098 { |
8099 int i; | |
8100 | |
8101 if (del_group) | |
8102 { | |
8103 if (*arg == NUL) | |
8104 EMSG(_(e_argreq)); | |
8105 else | |
8106 au_del_group(arg); | |
8107 } | |
8108 else if (STRICMP(arg, "end") == 0) /* ":aug end": back to group 0 */ | |
8109 current_augroup = AUGROUP_DEFAULT; | |
8110 else if (*arg) /* ":aug xxx": switch to group xxx */ | |
8111 { | |
8112 i = au_new_group(arg); | |
8113 if (i != AUGROUP_ERROR) | |
8114 current_augroup = i; | |
8115 } | |
8116 else /* ":aug": list the group names */ | |
8117 { | |
8118 msg_start(); | |
8119 for (i = 0; i < augroups.ga_len; ++i) | |
8120 { | |
8121 if (AUGROUP_NAME(i) != NULL) | |
8122 { | |
8123 msg_puts(AUGROUP_NAME(i)); | |
8124 msg_puts((char_u *)" "); | |
8125 } | |
8126 } | |
8127 msg_clr_eos(); | |
8128 msg_end(); | |
8129 } | |
8130 } | |
8131 | |
355 | 8132 #if defined(EXITFREE) || defined(PROTO) |
8133 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8134 free_all_autocmds(void) |
355 | 8135 { |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8136 int i; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8137 char_u *s; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8138 |
355 | 8139 for (current_augroup = -1; current_augroup < augroups.ga_len; |
8140 ++current_augroup) | |
8141 do_autocmd((char_u *)"", TRUE); | |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8142 |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8143 for (i = 0; i < augroups.ga_len; ++i) |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8144 { |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8145 s = ((char_u **)(augroups.ga_data))[i]; |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
8146 if (s != get_deleted_augroup()) |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8147 vim_free(s); |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8148 } |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8149 ga_clear(&augroups); |
355 | 8150 } |
8151 #endif | |
8152 | |
7 | 8153 /* |
8154 * Return the event number for event name "start". | |
8155 * Return NUM_EVENTS if the event name was not found. | |
8156 * Return a pointer to the next event name in "end". | |
8157 */ | |
661 | 8158 static event_T |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8159 event_name2nr(char_u *start, char_u **end) |
7 | 8160 { |
8161 char_u *p; | |
8162 int i; | |
8163 int len; | |
8164 | |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8165 /* the event name ends with end of line, '|', a blank or a comma */ |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8166 for (p = start; *p && !VIM_ISWHITE(*p) && *p != ',' && *p != '|'; ++p) |
7 | 8167 ; |
8168 for (i = 0; event_names[i].name != NULL; ++i) | |
8169 { | |
8170 len = (int)STRLEN(event_names[i].name); | |
8171 if (len == p - start && STRNICMP(event_names[i].name, start, len) == 0) | |
8172 break; | |
8173 } | |
8174 if (*p == ',') | |
8175 ++p; | |
8176 *end = p; | |
8177 if (event_names[i].name == NULL) | |
8178 return NUM_EVENTS; | |
8179 return event_names[i].event; | |
8180 } | |
8181 | |
8182 /* | |
8183 * Return the name for event "event". | |
8184 */ | |
8185 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8186 event_nr2name(event_T event) |
7 | 8187 { |
8188 int i; | |
8189 | |
8190 for (i = 0; event_names[i].name != NULL; ++i) | |
8191 if (event_names[i].event == event) | |
8192 return (char_u *)event_names[i].name; | |
8193 return (char_u *)"Unknown"; | |
8194 } | |
8195 | |
8196 /* | |
8197 * Scan over the events. "*" stands for all events. | |
8198 */ | |
8199 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8200 find_end_event( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8201 char_u *arg, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8202 int have_group) /* TRUE when group name was found */ |
7 | 8203 { |
8204 char_u *pat; | |
8205 char_u *p; | |
8206 | |
8207 if (*arg == '*') | |
8208 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8209 if (arg[1] && !VIM_ISWHITE(arg[1])) |
7 | 8210 { |
8211 EMSG2(_("E215: Illegal character after *: %s"), arg); | |
8212 return NULL; | |
8213 } | |
8214 pat = arg + 1; | |
8215 } | |
8216 else | |
8217 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8218 for (pat = arg; *pat && *pat != '|' && !VIM_ISWHITE(*pat); pat = p) |
7 | 8219 { |
8220 if ((int)event_name2nr(pat, &p) >= (int)NUM_EVENTS) | |
8221 { | |
8222 if (have_group) | |
8223 EMSG2(_("E216: No such event: %s"), pat); | |
8224 else | |
8225 EMSG2(_("E216: No such group or event: %s"), pat); | |
8226 return NULL; | |
8227 } | |
8228 } | |
8229 } | |
8230 return pat; | |
8231 } | |
8232 | |
8233 /* | |
8234 * Return TRUE if "event" is included in 'eventignore'. | |
8235 */ | |
8236 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8237 event_ignored(event_T event) |
7 | 8238 { |
8239 char_u *p = p_ei; | |
8240 | |
844 | 8241 while (*p != NUL) |
8242 { | |
8243 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) | |
8244 return TRUE; | |
7 | 8245 if (event_name2nr(p, &p) == event) |
8246 return TRUE; | |
844 | 8247 } |
7 | 8248 |
8249 return FALSE; | |
8250 } | |
8251 | |
8252 /* | |
8253 * Return OK when the contents of p_ei is valid, FAIL otherwise. | |
8254 */ | |
8255 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8256 check_ei(void) |
7 | 8257 { |
8258 char_u *p = p_ei; | |
8259 | |
8260 while (*p) | |
844 | 8261 { |
8262 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) | |
8263 { | |
8264 p += 3; | |
8265 if (*p == ',') | |
8266 ++p; | |
8267 } | |
8268 else if (event_name2nr(p, &p) == NUM_EVENTS) | |
7 | 8269 return FAIL; |
844 | 8270 } |
7 | 8271 |
8272 return OK; | |
8273 } | |
8274 | |
123 | 8275 # if defined(FEAT_SYN_HL) || defined(PROTO) |
8276 | |
8277 /* | |
8278 * Add "what" to 'eventignore' to skip loading syntax highlighting for every | |
8279 * buffer loaded into the window. "what" must start with a comma. | |
8280 * Returns the old value of 'eventignore' in allocated memory. | |
8281 */ | |
8282 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8283 au_event_disable(char *what) |
123 | 8284 { |
8285 char_u *new_ei; | |
8286 char_u *save_ei; | |
8287 | |
8288 save_ei = vim_strsave(p_ei); | |
8289 if (save_ei != NULL) | |
8290 { | |
557 | 8291 new_ei = vim_strnsave(p_ei, (int)(STRLEN(p_ei) + STRLEN(what))); |
123 | 8292 if (new_ei != NULL) |
8293 { | |
2095
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8294 if (*what == ',' && *p_ei == NUL) |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8295 STRCPY(new_ei, what + 1); |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8296 else |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8297 STRCAT(new_ei, what); |
694 | 8298 set_string_option_direct((char_u *)"ei", -1, new_ei, |
8299 OPT_FREE, SID_NONE); | |
123 | 8300 vim_free(new_ei); |
8301 } | |
8302 } | |
8303 return save_ei; | |
8304 } | |
8305 | |
8306 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8307 au_event_restore(char_u *old_ei) |
123 | 8308 { |
8309 if (old_ei != NULL) | |
8310 { | |
694 | 8311 set_string_option_direct((char_u *)"ei", -1, old_ei, |
8312 OPT_FREE, SID_NONE); | |
123 | 8313 vim_free(old_ei); |
8314 } | |
8315 } | |
8316 # endif /* FEAT_SYN_HL */ | |
8317 | |
7 | 8318 /* |
8319 * do_autocmd() -- implements the :autocmd command. Can be used in the | |
8320 * following ways: | |
8321 * | |
8322 * :autocmd <event> <pat> <cmd> Add <cmd> to the list of commands that | |
8323 * will be automatically executed for <event> | |
8324 * when editing a file matching <pat>, in | |
8325 * the current group. | |
8326 * :autocmd <event> <pat> Show the auto-commands associated with | |
8327 * <event> and <pat>. | |
8328 * :autocmd <event> Show the auto-commands associated with | |
8329 * <event>. | |
8330 * :autocmd Show all auto-commands. | |
8331 * :autocmd! <event> <pat> <cmd> Remove all auto-commands associated with | |
8332 * <event> and <pat>, and add the command | |
8333 * <cmd>, for the current group. | |
8334 * :autocmd! <event> <pat> Remove all auto-commands associated with | |
8335 * <event> and <pat> for the current group. | |
8336 * :autocmd! <event> Remove all auto-commands associated with | |
8337 * <event> for the current group. | |
8338 * :autocmd! Remove ALL auto-commands for the current | |
8339 * group. | |
8340 * | |
8341 * Multiple events and patterns may be given separated by commas. Here are | |
8342 * some examples: | |
8343 * :autocmd bufread,bufenter *.c,*.h set tw=0 smartindent noic | |
8344 * :autocmd bufleave * set tw=79 nosmartindent ic infercase | |
8345 * | |
8346 * :autocmd * *.c show all autocommands for *.c files. | |
609 | 8347 * |
8348 * Mostly a {group} argument can optionally appear before <event>. | |
7 | 8349 */ |
8350 void | |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8351 do_autocmd(char_u *arg_in, int forceit) |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8352 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8353 char_u *arg = arg_in; |
7 | 8354 char_u *pat; |
8355 char_u *envpat = NULL; | |
8356 char_u *cmd; | |
661 | 8357 event_T event; |
7 | 8358 int need_free = FALSE; |
8359 int nested = FALSE; | |
8360 int group; | |
8361 | |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8362 if (*arg == '|') |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8363 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8364 arg = (char_u *)""; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8365 group = AUGROUP_ALL; /* no argument, use all groups */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8366 } |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8367 else |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8368 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8369 /* |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8370 * Check for a legal group name. If not, use AUGROUP_ALL. |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8371 */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8372 group = au_get_grouparg(&arg); |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8373 if (arg == NULL) /* out of memory */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8374 return; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8375 } |
7 | 8376 |
8377 /* | |
8378 * Scan over the events. | |
8379 * If we find an illegal name, return here, don't do anything. | |
8380 */ | |
8381 pat = find_end_event(arg, group != AUGROUP_ALL); | |
8382 if (pat == NULL) | |
8383 return; | |
8384 | |
8385 pat = skipwhite(pat); | |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8386 if (*pat == '|') |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8387 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8388 pat = (char_u *)""; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8389 cmd = (char_u *)""; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8390 } |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8391 else |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8392 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8393 /* |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8394 * Scan over the pattern. Put a NUL at the end. |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8395 */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8396 cmd = pat; |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8397 while (*cmd && (!VIM_ISWHITE(*cmd) || cmd[-1] == '\\')) |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8398 cmd++; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8399 if (*cmd) |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8400 *cmd++ = NUL; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8401 |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8402 /* Expand environment variables in the pattern. Set 'shellslash', we want |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8403 * forward slashes here. */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8404 if (vim_strchr(pat, '$') != NULL || vim_strchr(pat, '~') != NULL) |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8405 { |
7 | 8406 #ifdef BACKSLASH_IN_FILENAME |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8407 int p_ssl_save = p_ssl; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8408 |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8409 p_ssl = TRUE; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8410 #endif |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8411 envpat = expand_env_save(pat); |
7 | 8412 #ifdef BACKSLASH_IN_FILENAME |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8413 p_ssl = p_ssl_save; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8414 #endif |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8415 if (envpat != NULL) |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8416 pat = envpat; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8417 } |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8418 |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8419 /* |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8420 * Check for "nested" flag. |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8421 */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8422 cmd = skipwhite(cmd); |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8423 if (*cmd != NUL && STRNCMP(cmd, "nested", 6) == 0 && VIM_ISWHITE(cmd[6])) |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8424 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8425 nested = TRUE; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8426 cmd = skipwhite(cmd + 6); |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8427 } |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8428 |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8429 /* |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8430 * Find the start of the commands. |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8431 * Expand <sfile> in it. |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8432 */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8433 if (*cmd != NUL) |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8434 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8435 cmd = expand_sfile(cmd); |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8436 if (cmd == NULL) /* some error */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8437 return; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8438 need_free = TRUE; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8439 } |
7 | 8440 } |
8441 | |
8442 /* | |
8443 * Print header when showing autocommands. | |
8444 */ | |
8445 if (!forceit && *cmd == NUL) | |
8446 { | |
8447 /* Highlight title */ | |
8448 MSG_PUTS_TITLE(_("\n--- Auto-Commands ---")); | |
8449 } | |
8450 | |
8451 /* | |
8452 * Loop over the events. | |
8453 */ | |
661 | 8454 last_event = (event_T)-1; /* for listing the event name */ |
7 | 8455 last_group = AUGROUP_ERROR; /* for listing the group name */ |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8456 if (*arg == '*' || *arg == NUL || *arg == '|') |
7 | 8457 { |
661 | 8458 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
8459 event = (event_T)((int)event + 1)) | |
7 | 8460 if (do_autocmd_event(event, pat, |
8461 nested, cmd, forceit, group) == FAIL) | |
8462 break; | |
8463 } | |
8464 else | |
8465 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8466 while (*arg && *arg != '|' && !VIM_ISWHITE(*arg)) |
7 | 8467 if (do_autocmd_event(event_name2nr(arg, &arg), pat, |
8468 nested, cmd, forceit, group) == FAIL) | |
8469 break; | |
8470 } | |
8471 | |
8472 if (need_free) | |
8473 vim_free(cmd); | |
8474 vim_free(envpat); | |
8475 } | |
8476 | |
8477 /* | |
8478 * Find the group ID in a ":autocmd" or ":doautocmd" argument. | |
8479 * The "argp" argument is advanced to the following argument. | |
8480 * | |
8481 * Returns the group ID, AUGROUP_ERROR for error (out of memory). | |
8482 */ | |
8483 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8484 au_get_grouparg(char_u **argp) |
7 | 8485 { |
8486 char_u *group_name; | |
8487 char_u *p; | |
8488 char_u *arg = *argp; | |
8489 int group = AUGROUP_ALL; | |
8490 | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8491 for (p = arg; *p && !VIM_ISWHITE(*p) && *p != '|'; ++p) |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8492 ; |
7 | 8493 if (p > arg) |
8494 { | |
8495 group_name = vim_strnsave(arg, (int)(p - arg)); | |
8496 if (group_name == NULL) /* out of memory */ | |
8497 return AUGROUP_ERROR; | |
8498 group = au_find_group(group_name); | |
8499 if (group == AUGROUP_ERROR) | |
8500 group = AUGROUP_ALL; /* no match, use all groups */ | |
8501 else | |
8502 *argp = skipwhite(p); /* match, skip over group name */ | |
8503 vim_free(group_name); | |
8504 } | |
8505 return group; | |
8506 } | |
8507 | |
8508 /* | |
8509 * do_autocmd() for one event. | |
8510 * If *pat == NUL do for all patterns. | |
8511 * If *cmd == NUL show entries. | |
8512 * If forceit == TRUE delete entries. | |
8513 * If group is not AUGROUP_ALL, only use this group. | |
8514 */ | |
8515 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8516 do_autocmd_event( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8517 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8518 char_u *pat, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8519 int nested, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8520 char_u *cmd, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8521 int forceit, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8522 int group) |
7 | 8523 { |
8524 AutoPat *ap; | |
8525 AutoPat **prev_ap; | |
8526 AutoCmd *ac; | |
8527 AutoCmd **prev_ac; | |
8528 int brace_level; | |
8529 char_u *endpat; | |
8530 int findgroup; | |
8531 int allgroups; | |
8532 int patlen; | |
856 | 8533 int is_buflocal; |
40 | 8534 int buflocal_nr; |
8535 char_u buflocal_pat[25]; /* for "<buffer=X>" */ | |
7 | 8536 |
8537 if (group == AUGROUP_ALL) | |
8538 findgroup = current_augroup; | |
8539 else | |
8540 findgroup = group; | |
8541 allgroups = (group == AUGROUP_ALL && !forceit && *cmd == NUL); | |
8542 | |
8543 /* | |
8544 * Show or delete all patterns for an event. | |
8545 */ | |
8546 if (*pat == NUL) | |
8547 { | |
8548 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
8549 { | |
8550 if (forceit) /* delete the AutoPat, if it's in the current group */ | |
8551 { | |
8552 if (ap->group == findgroup) | |
8553 au_remove_pat(ap); | |
8554 } | |
8555 else if (group == AUGROUP_ALL || ap->group == group) | |
8556 show_autocmd(ap, event); | |
8557 } | |
8558 } | |
8559 | |
8560 /* | |
8561 * Loop through all the specified patterns. | |
8562 */ | |
8563 for ( ; *pat; pat = (*endpat == ',' ? endpat + 1 : endpat)) | |
8564 { | |
8565 /* | |
8566 * Find end of the pattern. | |
8567 * Watch out for a comma in braces, like "*.\{obj,o\}". | |
8568 */ | |
8569 brace_level = 0; | |
8570 for (endpat = pat; *endpat && (*endpat != ',' || brace_level | |
6963 | 8571 || (endpat > pat && endpat[-1] == '\\')); ++endpat) |
7 | 8572 { |
8573 if (*endpat == '{') | |
8574 brace_level++; | |
8575 else if (*endpat == '}') | |
8576 brace_level--; | |
8577 } | |
8578 if (pat == endpat) /* ignore single comma */ | |
8579 continue; | |
8580 patlen = (int)(endpat - pat); | |
8581 | |
8582 /* | |
40 | 8583 * detect special <buflocal[=X]> buffer-local patterns |
8584 */ | |
8585 is_buflocal = FALSE; | |
8586 buflocal_nr = 0; | |
8587 | |
6623 | 8588 if (patlen >= 8 && STRNCMP(pat, "<buffer", 7) == 0 |
40 | 8589 && pat[patlen - 1] == '>') |
8590 { | |
6623 | 8591 /* "<buffer...>": Error will be printed only for addition. |
8592 * printing and removing will proceed silently. */ | |
40 | 8593 is_buflocal = TRUE; |
8594 if (patlen == 8) | |
6623 | 8595 /* "<buffer>" */ |
40 | 8596 buflocal_nr = curbuf->b_fnum; |
8597 else if (patlen > 9 && pat[7] == '=') | |
8598 { | |
6623 | 8599 if (patlen == 13 && STRNICMP(pat, "<buffer=abuf>", 13) == 0) |
8600 /* "<buffer=abuf>" */ | |
40 | 8601 buflocal_nr = autocmd_bufnr; |
8602 else if (skipdigits(pat + 8) == pat + patlen - 1) | |
6623 | 8603 /* "<buffer=123>" */ |
40 | 8604 buflocal_nr = atoi((char *)pat + 8); |
8605 } | |
8606 } | |
8607 | |
8608 if (is_buflocal) | |
8609 { | |
8610 /* normalize pat into standard "<buffer>#N" form */ | |
8611 sprintf((char *)buflocal_pat, "<buffer=%d>", buflocal_nr); | |
8612 pat = buflocal_pat; /* can modify pat and patlen */ | |
835 | 8613 patlen = (int)STRLEN(buflocal_pat); /* but not endpat */ |
40 | 8614 } |
8615 | |
8616 /* | |
7 | 8617 * Find AutoPat entries with this pattern. |
8618 */ | |
8619 prev_ap = &first_autopat[(int)event]; | |
8620 while ((ap = *prev_ap) != NULL) | |
8621 { | |
8622 if (ap->pat != NULL) | |
8623 { | |
8624 /* Accept a pattern when: | |
8625 * - a group was specified and it's that group, or a group was | |
8626 * not specified and it's the current group, or a group was | |
8627 * not specified and we are listing | |
8628 * - the length of the pattern matches | |
40 | 8629 * - the pattern matches. |
8630 * For <buffer[=X]>, this condition works because we normalize | |
8631 * all buffer-local patterns. | |
7 | 8632 */ |
8633 if ((allgroups || ap->group == findgroup) | |
8634 && ap->patlen == patlen | |
8635 && STRNCMP(pat, ap->pat, patlen) == 0) | |
8636 { | |
8637 /* | |
8638 * Remove existing autocommands. | |
8639 * If adding any new autocmd's for this AutoPat, don't | |
8640 * delete the pattern from the autopat list, append to | |
8641 * this list. | |
8642 */ | |
8643 if (forceit) | |
8644 { | |
8645 if (*cmd != NUL && ap->next == NULL) | |
8646 { | |
8647 au_remove_cmds(ap); | |
8648 break; | |
8649 } | |
8650 au_remove_pat(ap); | |
8651 } | |
8652 | |
8653 /* | |
40 | 8654 * Show autocmd's for this autopat, or buflocals <buffer=X> |
7 | 8655 */ |
8656 else if (*cmd == NUL) | |
8657 show_autocmd(ap, event); | |
8658 | |
8659 /* | |
8660 * Add autocmd to this autopat, if it's the last one. | |
8661 */ | |
8662 else if (ap->next == NULL) | |
8663 break; | |
8664 } | |
8665 } | |
8666 prev_ap = &ap->next; | |
8667 } | |
8668 | |
8669 /* | |
8670 * Add a new command. | |
8671 */ | |
8672 if (*cmd != NUL) | |
8673 { | |
8674 /* | |
8675 * If the pattern we want to add a command to does appear at the | |
8676 * end of the list (or not is not in the list at all), add the | |
8677 * pattern at the end of the list. | |
8678 */ | |
8679 if (ap == NULL) | |
8680 { | |
40 | 8681 /* refuse to add buffer-local ap if buffer number is invalid */ |
8682 if (is_buflocal && (buflocal_nr == 0 | |
8683 || buflist_findnr(buflocal_nr) == NULL)) | |
8684 { | |
8685 EMSGN(_("E680: <buffer=%d>: invalid buffer number "), | |
8686 buflocal_nr); | |
8687 return FAIL; | |
8688 } | |
8689 | |
7 | 8690 ap = (AutoPat *)alloc((unsigned)sizeof(AutoPat)); |
8691 if (ap == NULL) | |
8692 return FAIL; | |
8693 ap->pat = vim_strnsave(pat, patlen); | |
8694 ap->patlen = patlen; | |
8695 if (ap->pat == NULL) | |
8696 { | |
8697 vim_free(ap); | |
8698 return FAIL; | |
8699 } | |
40 | 8700 |
8701 if (is_buflocal) | |
8702 { | |
8703 ap->buflocal_nr = buflocal_nr; | |
153 | 8704 ap->reg_prog = NULL; |
40 | 8705 } |
8706 else | |
7 | 8707 { |
153 | 8708 char_u *reg_pat; |
8709 | |
40 | 8710 ap->buflocal_nr = 0; |
153 | 8711 reg_pat = file_pat_to_reg_pat(pat, endpat, |
40 | 8712 &ap->allow_dirs, TRUE); |
153 | 8713 if (reg_pat != NULL) |
8714 ap->reg_prog = vim_regcomp(reg_pat, RE_MAGIC); | |
355 | 8715 vim_free(reg_pat); |
153 | 8716 if (reg_pat == NULL || ap->reg_prog == NULL) |
40 | 8717 { |
8718 vim_free(ap->pat); | |
8719 vim_free(ap); | |
8720 return FAIL; | |
8721 } | |
7 | 8722 } |
8723 ap->cmds = NULL; | |
8724 *prev_ap = ap; | |
8725 ap->next = NULL; | |
8726 if (group == AUGROUP_ALL) | |
8727 ap->group = current_augroup; | |
8728 else | |
8729 ap->group = group; | |
8730 } | |
8731 | |
8732 /* | |
8733 * Add the autocmd at the end of the AutoCmd list. | |
8734 */ | |
8735 prev_ac = &(ap->cmds); | |
8736 while ((ac = *prev_ac) != NULL) | |
8737 prev_ac = &ac->next; | |
8738 ac = (AutoCmd *)alloc((unsigned)sizeof(AutoCmd)); | |
8739 if (ac == NULL) | |
8740 return FAIL; | |
8741 ac->cmd = vim_strsave(cmd); | |
8742 #ifdef FEAT_EVAL | |
8743 ac->scriptID = current_SID; | |
8744 #endif | |
8745 if (ac->cmd == NULL) | |
8746 { | |
8747 vim_free(ac); | |
8748 return FAIL; | |
8749 } | |
8750 ac->next = NULL; | |
8751 *prev_ac = ac; | |
8752 ac->nested = nested; | |
8753 } | |
8754 } | |
8755 | |
8756 au_cleanup(); /* may really delete removed patterns/commands now */ | |
8757 return OK; | |
8758 } | |
8759 | |
8760 /* | |
8761 * Implementation of ":doautocmd [group] event [fname]". | |
8762 * Return OK for success, FAIL for failure; | |
8763 */ | |
8764 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8765 do_doautocmd( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8766 char_u *arg, |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8767 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
|
8768 int *did_something) |
7 | 8769 { |
8770 char_u *fname; | |
8771 int nothing_done = TRUE; | |
8772 int group; | |
8773 | |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8774 if (did_something != NULL) |
9291
bff3cd5cf922
commit https://github.com/vim/vim/commit/76ae22fef3cb224ca7fbf97517f881e825d4d0c2
Christian Brabandt <cb@256bit.org>
parents:
9260
diff
changeset
|
8775 *did_something = FALSE; |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8776 |
7 | 8777 /* |
8778 * Check for a legal group name. If not, use AUGROUP_ALL. | |
8779 */ | |
8780 group = au_get_grouparg(&arg); | |
8781 if (arg == NULL) /* out of memory */ | |
8782 return FAIL; | |
8783 | |
8784 if (*arg == '*') | |
8785 { | |
8786 EMSG(_("E217: Can't execute autocommands for ALL events")); | |
8787 return FAIL; | |
8788 } | |
8789 | |
8790 /* | |
8791 * Scan over the events. | |
8792 * If we find an illegal name, return here, don't do anything. | |
8793 */ | |
8794 fname = find_end_event(arg, group != AUGROUP_ALL); | |
8795 if (fname == NULL) | |
8796 return FAIL; | |
8797 | |
8798 fname = skipwhite(fname); | |
8799 | |
8800 /* | |
8801 * Loop over the events. | |
8802 */ | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8803 while (*arg && !VIM_ISWHITE(*arg)) |
7 | 8804 if (apply_autocmds_group(event_name2nr(arg, &arg), |
8805 fname, NULL, TRUE, group, curbuf, NULL)) | |
8806 nothing_done = FALSE; | |
8807 | |
8808 if (nothing_done && do_msg) | |
8809 MSG(_("No matching autocommands")); | |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8810 if (did_something != NULL) |
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8811 *did_something = !nothing_done; |
7 | 8812 |
8813 #ifdef FEAT_EVAL | |
8814 return aborting() ? FAIL : OK; | |
8815 #else | |
8816 return OK; | |
8817 #endif | |
8818 } | |
8819 | |
8820 /* | |
8821 * ":doautoall": execute autocommands for each loaded buffer. | |
8822 */ | |
8823 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8824 ex_doautoall(exarg_T *eap) |
7 | 8825 { |
8826 int retval; | |
8827 aco_save_T aco; | |
8828 buf_T *buf; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
8829 bufref_T bufref; |
3342 | 8830 char_u *arg = eap->arg; |
3350 | 8831 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
|
8832 int did_aucmd; |
7 | 8833 |
8834 /* | |
8835 * This is a bit tricky: For some commands curwin->w_buffer needs to be | |
8836 * equal to curbuf, but for some buffers there may not be a window. | |
8837 * So we change the buffer for the current window for a moment. This | |
8838 * gives problems when the autocommands make changes to the list of | |
8839 * buffers or windows... | |
8840 */ | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
8841 FOR_ALL_BUFFERS(buf) |
7 | 8842 { |
1665 | 8843 if (buf->b_ml.ml_mfp != NULL) |
7 | 8844 { |
8845 /* find a window for this buffer and save some values */ | |
8846 aucmd_prepbuf(&aco, buf); | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
8847 set_bufref(&bufref, buf); |
7 | 8848 |
8849 /* 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
|
8850 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
|
8851 |
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8852 if (call_do_modelines && did_aucmd) |
3342 | 8853 { |
8854 /* Execute the modeline settings, but don't set window-local | |
8855 * options if we are using the current window for another | |
8856 * buffer. */ | |
8857 do_modelines(curwin == aucmd_win ? OPT_NOWIN : 0); | |
8858 } | |
7 | 8859 |
8860 /* restore the current window */ | |
8861 aucmd_restbuf(&aco); | |
8862 | |
8863 /* 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
|
8864 if (retval == FAIL || !bufref_valid(&bufref)) |
7 | 8865 break; |
8866 } | |
8867 } | |
8868 | |
8869 check_cursor(); /* just in case lines got deleted */ | |
8870 } | |
8871 | |
8872 /* | |
3350 | 8873 * Check *argp for <nomodeline>. When it is present return FALSE, otherwise |
8874 * return TRUE and advance *argp to after it. | |
8875 * Thus return TRUE when do_modelines() should be called. | |
8876 */ | |
8877 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8878 check_nomodeline(char_u **argp) |
3350 | 8879 { |
8880 if (STRNCMP(*argp, "<nomodeline>", 12) == 0) | |
8881 { | |
8882 *argp = skipwhite(*argp + 12); | |
8883 return FALSE; | |
8884 } | |
8885 return TRUE; | |
8886 } | |
8887 | |
8888 /* | |
7 | 8889 * Prepare for executing autocommands for (hidden) buffer "buf". |
1906 | 8890 * Search for a visible window containing the current buffer. If there isn't |
8891 * one then use "aucmd_win". | |
7 | 8892 * Set "curbuf" and "curwin" to match "buf". |
934 | 8893 * When FEAT_AUTOCMD is not defined another version is used, see below. |
7 | 8894 */ |
8895 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8896 aucmd_prepbuf( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8897 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
|
8898 buf_T *buf) /* new curbuf */ |
7 | 8899 { |
8900 win_T *win; | |
1906 | 8901 #ifdef FEAT_WINDOWS |
8902 int save_ea; | |
8903 #endif | |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8904 #ifdef FEAT_AUTOCHDIR |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8905 int save_acd; |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8906 #endif |
7 | 8907 |
8908 /* Find a window that is for the new buffer */ | |
8909 if (buf == curbuf) /* be quick when buf is curbuf */ | |
8910 win = curwin; | |
8911 else | |
8912 #ifdef FEAT_WINDOWS | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
8913 FOR_ALL_WINDOWS(win) |
7 | 8914 if (win->w_buffer == buf) |
8915 break; | |
8916 #else | |
8917 win = NULL; | |
8918 #endif | |
8919 | |
1906 | 8920 /* Allocate "aucmd_win" when needed. If this fails (out of memory) fall |
8921 * back to using the current window. */ | |
8922 if (win == NULL && aucmd_win == NULL) | |
8923 { | |
8924 win_alloc_aucmd_win(); | |
8925 if (aucmd_win == NULL) | |
8926 win = curwin; | |
8927 } | |
1944 | 8928 if (win == NULL && aucmd_win_used) |
8929 /* Strange recursive autocommand, fall back to using the current | |
8930 * window. Expect a few side effects... */ | |
8931 win = curwin; | |
1906 | 8932 |
8933 aco->save_curwin = curwin; | |
8934 aco->save_curbuf = curbuf; | |
7 | 8935 if (win != NULL) |
8936 { | |
1906 | 8937 /* There is a window for "buf" in the current tab page, make it the |
8938 * curwin. This is preferred, it has the least side effects (esp. if | |
8939 * "buf" is curbuf). */ | |
1944 | 8940 aco->use_aucmd_win = FALSE; |
7 | 8941 curwin = win; |
8942 } | |
8943 else | |
8944 { | |
1906 | 8945 /* There is no window for "buf", use "aucmd_win". To minimize the side |
4352 | 8946 * effects, insert it in the current tab page. |
1906 | 8947 * Anything related to a window (e.g., setting folds) may have |
8948 * unexpected results. */ | |
1944 | 8949 aco->use_aucmd_win = TRUE; |
8950 aucmd_win_used = TRUE; | |
1918 | 8951 aucmd_win->w_buffer = buf; |
2258
772bfca06c18
Fix crash when using ":grep".
Bram Moolenaar <bram@vim.org>
parents:
2244
diff
changeset
|
8952 aucmd_win->w_s = &buf->b_s; |
7 | 8953 ++buf->b_nwindows; |
1918 | 8954 win_init_empty(aucmd_win); /* set cursor and topline to safe values */ |
1944 | 8955 |
8956 /* Make sure w_localdir and globaldir are NULL to avoid a chdir() in | |
8957 * win_enter_ext(). */ | |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8958 vim_free(aucmd_win->w_localdir); |
1944 | 8959 aucmd_win->w_localdir = NULL; |
8960 aco->globaldir = globaldir; | |
8961 globaldir = NULL; | |
8962 | |
7 | 8963 |
1906 | 8964 #ifdef FEAT_WINDOWS |
1923 | 8965 /* Split the current window, put the aucmd_win in the upper half. |
8966 * We don't want the BufEnter or WinEnter autocommands. */ | |
8967 block_autocmds(); | |
1906 | 8968 make_snapshot(SNAP_AUCMD_IDX); |
8969 save_ea = p_ea; | |
8970 p_ea = FALSE; | |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8971 |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8972 # ifdef FEAT_AUTOCHDIR |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8973 /* 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
|
8974 save_acd = p_acd; |
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8975 p_acd = FALSE; |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8976 # endif |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8977 |
1906 | 8978 (void)win_split_ins(0, WSP_TOP, aucmd_win, 0); |
8979 (void)win_comp_pos(); /* recompute window positions */ | |
8980 p_ea = save_ea; | |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8981 # ifdef FEAT_AUTOCHDIR |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8982 p_acd = save_acd; |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8983 # endif |
1923 | 8984 unblock_autocmds(); |
1906 | 8985 #endif |
1918 | 8986 curwin = aucmd_win; |
1906 | 8987 } |
7 | 8988 curbuf = buf; |
1906 | 8989 aco->new_curwin = curwin; |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
8990 set_bufref(&aco->new_curbuf, curbuf); |
7 | 8991 } |
8992 | |
8993 /* | |
8994 * Cleanup after executing autocommands for a (hidden) buffer. | |
8995 * Restore the window as it was (if possible). | |
934 | 8996 * When FEAT_AUTOCMD is not defined another version is used, see below. |
7 | 8997 */ |
8998 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8999 aucmd_restbuf( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9000 aco_save_T *aco) /* structure holding saved values */ |
7 | 9001 { |
1906 | 9002 #ifdef FEAT_WINDOWS |
9003 int dummy; | |
9004 #endif | |
9005 | |
1944 | 9006 if (aco->use_aucmd_win) |
1906 | 9007 { |
9008 --curbuf->b_nwindows; | |
9009 #ifdef FEAT_WINDOWS | |
9010 /* Find "aucmd_win", it can't be closed, but it may be in another tab | |
1923 | 9011 * page. Do not trigger autocommands here. */ |
9012 block_autocmds(); | |
1906 | 9013 if (curwin != aucmd_win) |
9014 { | |
9015 tabpage_T *tp; | |
9016 win_T *wp; | |
9017 | |
9018 FOR_ALL_TAB_WINDOWS(tp, wp) | |
9019 { | |
9020 if (wp == aucmd_win) | |
9021 { | |
9022 if (tp != curtab) | |
4354 | 9023 goto_tabpage_tp(tp, TRUE, TRUE); |
1906 | 9024 win_goto(aucmd_win); |
3340 | 9025 goto win_found; |
1906 | 9026 } |
9027 } | |
9028 } | |
3340 | 9029 win_found: |
1906 | 9030 |
9031 /* Remove the window and frame from the tree of frames. */ | |
9032 (void)winframe_remove(curwin, &dummy, NULL); | |
9033 win_remove(curwin, NULL); | |
1944 | 9034 aucmd_win_used = FALSE; |
1906 | 9035 last_status(FALSE); /* may need to remove last status line */ |
11199
e08ead1d269f
patch 8.0.0486: crash and endless loop when closing windows in autocmd
Christian Brabandt <cb@256bit.org>
parents:
11158
diff
changeset
|
9036 |
e08ead1d269f
patch 8.0.0486: crash and endless loop when closing windows in autocmd
Christian Brabandt <cb@256bit.org>
parents:
11158
diff
changeset
|
9037 if (!valid_tabpage_win(curtab)) |
e08ead1d269f
patch 8.0.0486: crash and endless loop when closing windows in autocmd
Christian Brabandt <cb@256bit.org>
parents:
11158
diff
changeset
|
9038 /* no valid window in current tabpage */ |
e08ead1d269f
patch 8.0.0486: crash and endless loop when closing windows in autocmd
Christian Brabandt <cb@256bit.org>
parents:
11158
diff
changeset
|
9039 close_tabpage(curtab); |
e08ead1d269f
patch 8.0.0486: crash and endless loop when closing windows in autocmd
Christian Brabandt <cb@256bit.org>
parents:
11158
diff
changeset
|
9040 |
1906 | 9041 restore_snapshot(SNAP_AUCMD_IDX, FALSE); |
9042 (void)win_comp_pos(); /* recompute window positions */ | |
1923 | 9043 unblock_autocmds(); |
1906 | 9044 |
9045 if (win_valid(aco->save_curwin)) | |
9046 curwin = aco->save_curwin; | |
9047 else | |
9048 /* Hmm, original window disappeared. Just use the first one. */ | |
9049 curwin = firstwin; | |
9050 # ifdef FEAT_EVAL | |
4287 | 9051 vars_clear(&aucmd_win->w_vars->dv_hashtab); /* free all w: variables */ |
9052 hash_init(&aucmd_win->w_vars->dv_hashtab); /* re-use the hashtab */ | |
1906 | 9053 # endif |
9054 #else | |
9055 curwin = aco->save_curwin; | |
9056 #endif | |
9057 curbuf = curwin->w_buffer; | |
9058 | |
1944 | 9059 vim_free(globaldir); |
9060 globaldir = aco->globaldir; | |
9061 | |
1906 | 9062 /* the buffer contents may have changed */ |
9063 check_cursor(); | |
9064 if (curwin->w_topline > curbuf->b_ml.ml_line_count) | |
9065 { | |
9066 curwin->w_topline = curbuf->b_ml.ml_line_count; | |
9067 #ifdef FEAT_DIFF | |
9068 curwin->w_topfill = 0; | |
9069 #endif | |
9070 } | |
9071 #if defined(FEAT_GUI) | |
9072 /* Hide the scrollbars from the aucmd_win and update. */ | |
9073 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_LEFT], FALSE); | |
9074 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_RIGHT], FALSE); | |
9075 gui_may_update_scrollbars(); | |
9076 #endif | |
9077 } | |
9078 else | |
7 | 9079 { |
9080 /* restore curwin */ | |
9081 #ifdef FEAT_WINDOWS | |
9082 if (win_valid(aco->save_curwin)) | |
9083 #endif | |
9084 { | |
1906 | 9085 /* Restore the buffer which was previously edited by curwin, if |
1944 | 9086 * it was changed, we are still the same window and the buffer is |
1906 | 9087 * valid. */ |
7 | 9088 if (curwin == aco->new_curwin |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9089 && curbuf != aco->new_curbuf.br_buf |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9090 && bufref_valid(&aco->new_curbuf) |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9091 && aco->new_curbuf.br_buf->b_ml.ml_mfp != NULL) |
7 | 9092 { |
3497 | 9093 # if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) |
9094 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
|
9095 curwin->w_s = &aco->new_curbuf.br_buf->b_s; |
3497 | 9096 # endif |
7 | 9097 --curbuf->b_nwindows; |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9098 curbuf = aco->new_curbuf.br_buf; |
7 | 9099 curwin->w_buffer = curbuf; |
9100 ++curbuf->b_nwindows; | |
9101 } | |
9102 | |
9103 curwin = aco->save_curwin; | |
9104 curbuf = curwin->w_buffer; | |
6201 | 9105 /* In case the autocommand move the cursor to a position that that |
9106 * not exist in curbuf. */ | |
9107 check_cursor(); | |
7 | 9108 } |
9109 } | |
9110 } | |
9111 | |
9112 static int autocmd_nested = FALSE; | |
9113 | |
9114 /* | |
9115 * Execute autocommands for "event" and file name "fname". | |
9116 * Return TRUE if some commands were executed. | |
9117 */ | |
9118 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9119 apply_autocmds( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9120 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9121 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
|
9122 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
|
9123 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
|
9124 buf_T *buf) /* buffer for <abuf> */ |
7 | 9125 { |
9126 return apply_autocmds_group(event, fname, fname_io, force, | |
9127 AUGROUP_ALL, buf, NULL); | |
9128 } | |
9129 | |
9130 /* | |
9131 * Like apply_autocmds(), but with extra "eap" argument. This takes care of | |
9132 * setting v:filearg. | |
9133 */ | |
9134 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9135 apply_autocmds_exarg( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9136 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9137 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9138 char_u *fname_io, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9139 int force, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9140 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9141 exarg_T *eap) |
7 | 9142 { |
9143 return apply_autocmds_group(event, fname, fname_io, force, | |
9144 AUGROUP_ALL, buf, eap); | |
9145 } | |
9146 | |
9147 /* | |
9148 * Like apply_autocmds(), but handles the caller's retval. If the script | |
9149 * processing is being aborted or if retval is FAIL when inside a try | |
9150 * conditional, no autocommands are executed. If otherwise the autocommands | |
9151 * cause the script to be aborted, retval is set to FAIL. | |
9152 */ | |
9153 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9154 apply_autocmds_retval( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9155 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9156 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
|
9157 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
|
9158 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
|
9159 buf_T *buf, /* buffer for <abuf> */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9160 int *retval) /* pointer to caller's retval */ |
7 | 9161 { |
9162 int did_cmd; | |
9163 | |
532 | 9164 #ifdef FEAT_EVAL |
7 | 9165 if (should_abort(*retval)) |
9166 return FALSE; | |
532 | 9167 #endif |
7 | 9168 |
9169 did_cmd = apply_autocmds_group(event, fname, fname_io, force, | |
9170 AUGROUP_ALL, buf, NULL); | |
532 | 9171 if (did_cmd |
9172 #ifdef FEAT_EVAL | |
9173 && aborting() | |
9174 #endif | |
9175 ) | |
7 | 9176 *retval = FAIL; |
9177 return did_cmd; | |
9178 } | |
9179 | |
609 | 9180 /* |
9181 * Return TRUE when there is a CursorHold autocommand defined. | |
9182 */ | |
7 | 9183 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9184 has_cursorhold(void) |
7 | 9185 { |
661 | 9186 return (first_autopat[(int)(get_real_state() == NORMAL_BUSY |
9187 ? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)] != NULL); | |
7 | 9188 } |
609 | 9189 |
9190 /* | |
9191 * Return TRUE if the CursorHold event can be triggered. | |
9192 */ | |
9193 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9194 trigger_cursorhold(void) |
609 | 9195 { |
661 | 9196 int state; |
9197 | |
2976 | 9198 if (!did_cursorhold |
9199 && has_cursorhold() | |
9200 && !Recording | |
9201 && typebuf.tb_len == 0 | |
978 | 9202 #ifdef FEAT_INS_EXPAND |
9203 && !ins_compl_active() | |
9204 #endif | |
9205 ) | |
661 | 9206 { |
9207 state = get_real_state(); | |
9208 if (state == NORMAL_BUSY || (state & INSERT) != 0) | |
9209 return TRUE; | |
9210 } | |
9211 return FALSE; | |
609 | 9212 } |
661 | 9213 |
9214 /* | |
9215 * Return TRUE when there is a CursorMoved autocommand defined. | |
9216 */ | |
9217 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9218 has_cursormoved(void) |
661 | 9219 { |
9220 return (first_autopat[(int)EVENT_CURSORMOVED] != NULL); | |
9221 } | |
9222 | |
9223 /* | |
9224 * Return TRUE when there is a CursorMovedI autocommand defined. | |
9225 */ | |
9226 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9227 has_cursormovedI(void) |
661 | 9228 { |
9229 return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL); | |
9230 } | |
7 | 9231 |
3390 | 9232 /* |
4232 | 9233 * Return TRUE when there is a TextChanged autocommand defined. |
9234 */ | |
9235 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9236 has_textchanged(void) |
4232 | 9237 { |
9238 return (first_autopat[(int)EVENT_TEXTCHANGED] != NULL); | |
9239 } | |
9240 | |
9241 /* | |
9242 * Return TRUE when there is a TextChangedI autocommand defined. | |
9243 */ | |
9244 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9245 has_textchangedI(void) |
4232 | 9246 { |
9247 return (first_autopat[(int)EVENT_TEXTCHANGEDI] != NULL); | |
9248 } | |
9249 | |
9250 /* | |
3390 | 9251 * Return TRUE when there is an InsertCharPre autocommand defined. |
9252 */ | |
9253 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9254 has_insertcharpre(void) |
3390 | 9255 { |
9256 return (first_autopat[(int)EVENT_INSERTCHARPRE] != NULL); | |
9257 } | |
9258 | |
6154 | 9259 /* |
9260 * Return TRUE when there is an CmdUndefined autocommand defined. | |
9261 */ | |
9262 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9263 has_cmdundefined(void) |
6154 | 9264 { |
9265 return (first_autopat[(int)EVENT_CMDUNDEFINED] != NULL); | |
9266 } | |
9267 | |
9268 /* | |
9269 * Return TRUE when there is an FuncUndefined autocommand defined. | |
9270 */ | |
9271 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9272 has_funcundefined(void) |
6154 | 9273 { |
9274 return (first_autopat[(int)EVENT_FUNCUNDEFINED] != NULL); | |
9275 } | |
9276 | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9277 /* |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9278 * 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
|
9279 * 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
|
9280 */ |
7 | 9281 static int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9282 apply_autocmds_group( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9283 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9284 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
|
9285 char_u *fname_io, /* fname to use for <afile> on cmdline, NULL means |
7 | 9286 use fname */ |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9287 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
|
9288 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
|
9289 buf_T *buf, /* buffer for <abuf> */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9290 exarg_T *eap) /* command arguments */ |
7 | 9291 { |
9292 char_u *sfname = NULL; /* short file name */ | |
9293 char_u *tail; | |
9294 int save_changed; | |
9295 buf_T *old_curbuf; | |
9296 int retval = FALSE; | |
9297 char_u *save_sourcing_name; | |
9298 linenr_T save_sourcing_lnum; | |
9299 char_u *save_autocmd_fname; | |
1723 | 9300 int save_autocmd_fname_full; |
7 | 9301 int save_autocmd_bufnr; |
9302 char_u *save_autocmd_match; | |
9303 int save_autocmd_busy; | |
9304 int save_autocmd_nested; | |
9305 static int nesting = 0; | |
9306 AutoPatCmd patcmd; | |
9307 AutoPat *ap; | |
9308 #ifdef FEAT_EVAL | |
9309 scid_T save_current_SID; | |
9310 void *save_funccalp; | |
9311 char_u *save_cmdarg; | |
9312 long save_cmdbang; | |
9313 #endif | |
9314 static int filechangeshell_busy = FALSE; | |
170 | 9315 #ifdef FEAT_PROFILE |
9316 proftime_T wait_time; | |
9317 #endif | |
6608 | 9318 int did_save_redobuff = FALSE; |
11325
77f3b7316d8b
patch 8.0.0548: saving the redo buffer only works one time
Christian Brabandt <cb@256bit.org>
parents:
11199
diff
changeset
|
9319 save_redo_T save_redo; |
7 | 9320 |
9321 /* | |
9322 * Quickly return if there are no autocommands for this event or | |
9323 * autocommands are blocked. | |
9324 */ | |
1410 | 9325 if (first_autopat[(int)event] == NULL || autocmd_blocked > 0) |
40 | 9326 goto BYPASS_AU; |
7 | 9327 |
9328 /* | |
9329 * When autocommands are busy, new autocommands are only executed when | |
9330 * explicitly enabled with the "nested" flag. | |
9331 */ | |
9332 if (autocmd_busy && !(force || autocmd_nested)) | |
40 | 9333 goto BYPASS_AU; |
7 | 9334 |
9335 #ifdef FEAT_EVAL | |
9336 /* | |
1201 | 9337 * Quickly return when immediately aborting on error, or when an interrupt |
7 | 9338 * occurred or an exception was thrown but not caught. |
9339 */ | |
9340 if (aborting()) | |
40 | 9341 goto BYPASS_AU; |
7 | 9342 #endif |
9343 | |
9344 /* | |
9345 * FileChangedShell never nests, because it can create an endless loop. | |
9346 */ | |
765 | 9347 if (filechangeshell_busy && (event == EVENT_FILECHANGEDSHELL |
9348 || event == EVENT_FILECHANGEDSHELLPOST)) | |
40 | 9349 goto BYPASS_AU; |
7 | 9350 |
9351 /* | |
9352 * Ignore events in 'eventignore'. | |
9353 */ | |
9354 if (event_ignored(event)) | |
40 | 9355 goto BYPASS_AU; |
7 | 9356 |
9357 /* | |
9358 * Allow nesting of autocommands, but restrict the depth, because it's | |
9359 * possible to create an endless loop. | |
9360 */ | |
9361 if (nesting == 10) | |
9362 { | |
9363 EMSG(_("E218: autocommand nesting too deep")); | |
40 | 9364 goto BYPASS_AU; |
7 | 9365 } |
9366 | |
9367 /* | |
9368 * Check if these autocommands are disabled. Used when doing ":all" or | |
9369 * ":ball". | |
9370 */ | |
9371 if ( (autocmd_no_enter | |
9372 && (event == EVENT_WINENTER || event == EVENT_BUFENTER)) | |
9373 || (autocmd_no_leave | |
9374 && (event == EVENT_WINLEAVE || event == EVENT_BUFLEAVE))) | |
40 | 9375 goto BYPASS_AU; |
7 | 9376 |
9377 /* | |
9378 * Save the autocmd_* variables and info about the current buffer. | |
9379 */ | |
9380 save_autocmd_fname = autocmd_fname; | |
1723 | 9381 save_autocmd_fname_full = autocmd_fname_full; |
7 | 9382 save_autocmd_bufnr = autocmd_bufnr; |
9383 save_autocmd_match = autocmd_match; | |
9384 save_autocmd_busy = autocmd_busy; | |
9385 save_autocmd_nested = autocmd_nested; | |
9386 save_changed = curbuf->b_changed; | |
9387 old_curbuf = curbuf; | |
9388 | |
9389 /* | |
9390 * Set the file name to be used for <afile>. | |
1471 | 9391 * Make a copy to avoid that changing a buffer name or directory makes it |
9392 * invalid. | |
7 | 9393 */ |
9394 if (fname_io == NULL) | |
9395 { | |
6935 | 9396 if (event == EVENT_COLORSCHEME || event == EVENT_OPTIONSET) |
5521 | 9397 autocmd_fname = NULL; |
9398 else if (fname != NULL && *fname != NUL) | |
7 | 9399 autocmd_fname = fname; |
9400 else if (buf != NULL) | |
1723 | 9401 autocmd_fname = buf->b_ffname; |
7 | 9402 else |
9403 autocmd_fname = NULL; | |
9404 } | |
9405 else | |
9406 autocmd_fname = fname_io; | |
1471 | 9407 if (autocmd_fname != NULL) |
1723 | 9408 autocmd_fname = vim_strsave(autocmd_fname); |
9409 autocmd_fname_full = FALSE; /* call FullName_save() later */ | |
7 | 9410 |
9411 /* | |
9412 * Set the buffer number to be used for <abuf>. | |
9413 */ | |
9414 if (buf == NULL) | |
9415 autocmd_bufnr = 0; | |
9416 else | |
9417 autocmd_bufnr = buf->b_fnum; | |
9418 | |
9419 /* | |
9420 * When the file name is NULL or empty, use the file name of buffer "buf". | |
9421 * Always use the full path of the file name to match with, in case | |
9422 * "allow_dirs" is set. | |
9423 */ | |
9424 if (fname == NULL || *fname == NUL) | |
9425 { | |
9426 if (buf == NULL) | |
9427 fname = NULL; | |
9428 else | |
9429 { | |
9430 #ifdef FEAT_SYN_HL | |
9431 if (event == EVENT_SYNTAX) | |
9432 fname = buf->b_p_syn; | |
9433 else | |
9434 #endif | |
9435 if (event == EVENT_FILETYPE) | |
9436 fname = buf->b_p_ft; | |
9437 else | |
9438 { | |
9439 if (buf->b_sfname != NULL) | |
9440 sfname = vim_strsave(buf->b_sfname); | |
9441 fname = buf->b_ffname; | |
9442 } | |
9443 } | |
9444 if (fname == NULL) | |
9445 fname = (char_u *)""; | |
9446 fname = vim_strsave(fname); /* make a copy, so we can change it */ | |
9447 } | |
9448 else | |
9449 { | |
9450 sfname = vim_strsave(fname); | |
5521 | 9451 /* Don't try expanding FileType, Syntax, FuncUndefined, WindowID, |
9452 * ColorScheme or QuickFixCmd* */ | |
161 | 9453 if (event == EVENT_FILETYPE |
9454 || event == EVENT_SYNTAX | |
1867 | 9455 || event == EVENT_FUNCUNDEFINED |
161 | 9456 || event == EVENT_REMOTEREPLY |
649 | 9457 || event == EVENT_SPELLFILEMISSING |
161 | 9458 || event == EVENT_QUICKFIXCMDPRE |
5521 | 9459 || event == EVENT_COLORSCHEME |
6935 | 9460 || event == EVENT_OPTIONSET |
161 | 9461 || event == EVENT_QUICKFIXCMDPOST) |
7 | 9462 fname = vim_strsave(fname); |
9463 else | |
9464 fname = FullName_save(fname, FALSE); | |
9465 } | |
9466 if (fname == NULL) /* out of memory */ | |
9467 { | |
9468 vim_free(sfname); | |
40 | 9469 retval = FALSE; |
9470 goto BYPASS_AU; | |
7 | 9471 } |
9472 | |
9473 #ifdef BACKSLASH_IN_FILENAME | |
9474 /* | |
9475 * Replace all backslashes with forward slashes. This makes the | |
9476 * autocommand patterns portable between Unix and MS-DOS. | |
9477 */ | |
9478 if (sfname != NULL) | |
9479 forward_slash(sfname); | |
9480 forward_slash(fname); | |
9481 #endif | |
9482 | |
9483 #ifdef VMS | |
9484 /* remove version for correct match */ | |
9485 if (sfname != NULL) | |
9486 vms_remove_version(sfname); | |
9487 vms_remove_version(fname); | |
9488 #endif | |
9489 | |
9490 /* | |
9491 * Set the name to be used for <amatch>. | |
9492 */ | |
9493 autocmd_match = fname; | |
9494 | |
9495 | |
9496 /* Don't redraw while doing auto commands. */ | |
9497 ++RedrawingDisabled; | |
9498 save_sourcing_name = sourcing_name; | |
9499 sourcing_name = NULL; /* don't free this one */ | |
9500 save_sourcing_lnum = sourcing_lnum; | |
9501 sourcing_lnum = 0; /* no line number here */ | |
9502 | |
9503 #ifdef FEAT_EVAL | |
9504 save_current_SID = current_SID; | |
9505 | |
170 | 9506 # ifdef FEAT_PROFILE |
788 | 9507 if (do_profiling == PROF_YES) |
170 | 9508 prof_child_enter(&wait_time); /* doesn't count for the caller itself */ |
9509 # endif | |
9510 | |
7 | 9511 /* Don't use local function variables, if called from a function */ |
9512 save_funccalp = save_funccal(); | |
9513 #endif | |
9514 | |
9515 /* | |
9516 * When starting to execute autocommands, save the search patterns. | |
9517 */ | |
9518 if (!autocmd_busy) | |
9519 { | |
9520 save_search_patterns(); | |
7285
a34232b17763
commit https://github.com/vim/vim/commit/20ad69ccfb60ef718bd26387ef0e5424461a643e
Christian Brabandt <cb@256bit.org>
parents:
7170
diff
changeset
|
9521 #ifdef FEAT_INS_EXPAND |
6608 | 9522 if (!ins_compl_active()) |
7285
a34232b17763
commit https://github.com/vim/vim/commit/20ad69ccfb60ef718bd26387ef0e5424461a643e
Christian Brabandt <cb@256bit.org>
parents:
7170
diff
changeset
|
9523 #endif |
6608 | 9524 { |
11325
77f3b7316d8b
patch 8.0.0548: saving the redo buffer only works one time
Christian Brabandt <cb@256bit.org>
parents:
11199
diff
changeset
|
9525 saveRedobuff(&save_redo); |
6608 | 9526 did_save_redobuff = TRUE; |
9527 } | |
7 | 9528 did_filetype = keep_filetype; |
9529 } | |
9530 | |
9531 /* | |
9532 * Note that we are applying autocmds. Some commands need to know. | |
9533 */ | |
9534 autocmd_busy = TRUE; | |
9535 filechangeshell_busy = (event == EVENT_FILECHANGEDSHELL); | |
9536 ++nesting; /* see matching decrement below */ | |
9537 | |
9538 /* Remember that FileType was triggered. Used for did_filetype(). */ | |
9539 if (event == EVENT_FILETYPE) | |
9540 did_filetype = TRUE; | |
9541 | |
9542 tail = gettail(fname); | |
9543 | |
9544 /* Find first autocommand that matches */ | |
9545 patcmd.curpat = first_autopat[(int)event]; | |
9546 patcmd.nextcmd = NULL; | |
9547 patcmd.group = group; | |
9548 patcmd.fname = fname; | |
9549 patcmd.sfname = sfname; | |
9550 patcmd.tail = tail; | |
9551 patcmd.event = event; | |
40 | 9552 patcmd.arg_bufnr = autocmd_bufnr; |
9553 patcmd.next = NULL; | |
7 | 9554 auto_next_pat(&patcmd, FALSE); |
9555 | |
9556 /* found one, start executing the autocommands */ | |
9557 if (patcmd.curpat != NULL) | |
9558 { | |
40 | 9559 /* add to active_apc_list */ |
9560 patcmd.next = active_apc_list; | |
9561 active_apc_list = &patcmd; | |
9562 | |
7 | 9563 #ifdef FEAT_EVAL |
9564 /* 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
|
9565 save_cmdbang = (long)get_vim_var_nr(VV_CMDBANG); |
7 | 9566 if (eap != NULL) |
9567 { | |
9568 save_cmdarg = set_cmdarg(eap, NULL); | |
9569 set_vim_var_nr(VV_CMDBANG, (long)eap->forceit); | |
9570 } | |
9571 else | |
9572 save_cmdarg = NULL; /* avoid gcc warning */ | |
9573 #endif | |
9574 retval = TRUE; | |
9575 /* mark the last pattern, to avoid an endless loop when more patterns | |
9576 * are added when executing autocommands */ | |
9577 for (ap = patcmd.curpat; ap->next != NULL; ap = ap->next) | |
9578 ap->last = FALSE; | |
9579 ap->last = TRUE; | |
9580 check_lnums(TRUE); /* make sure cursor and topline are valid */ | |
9581 do_cmdline(NULL, getnextac, (void *)&patcmd, | |
9582 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT); | |
9583 #ifdef FEAT_EVAL | |
9584 if (eap != NULL) | |
9585 { | |
9586 (void)set_cmdarg(NULL, save_cmdarg); | |
9587 set_vim_var_nr(VV_CMDBANG, save_cmdbang); | |
9588 } | |
9589 #endif | |
40 | 9590 /* delete from active_apc_list */ |
9591 if (active_apc_list == &patcmd) /* just in case */ | |
9592 active_apc_list = patcmd.next; | |
7 | 9593 } |
9594 | |
9595 --RedrawingDisabled; | |
9596 autocmd_busy = save_autocmd_busy; | |
9597 filechangeshell_busy = FALSE; | |
9598 autocmd_nested = save_autocmd_nested; | |
9599 vim_free(sourcing_name); | |
9600 sourcing_name = save_sourcing_name; | |
9601 sourcing_lnum = save_sourcing_lnum; | |
1471 | 9602 vim_free(autocmd_fname); |
7 | 9603 autocmd_fname = save_autocmd_fname; |
1723 | 9604 autocmd_fname_full = save_autocmd_fname_full; |
7 | 9605 autocmd_bufnr = save_autocmd_bufnr; |
9606 autocmd_match = save_autocmd_match; | |
9607 #ifdef FEAT_EVAL | |
9608 current_SID = save_current_SID; | |
9609 restore_funccal(save_funccalp); | |
170 | 9610 # ifdef FEAT_PROFILE |
788 | 9611 if (do_profiling == PROF_YES) |
170 | 9612 prof_child_exit(&wait_time); |
9613 # endif | |
7 | 9614 #endif |
9615 vim_free(fname); | |
9616 vim_free(sfname); | |
9617 --nesting; /* see matching increment above */ | |
9618 | |
9619 /* | |
9620 * When stopping to execute autocommands, restore the search patterns and | |
5958 | 9621 * the redo buffer. Free any buffers in the au_pending_free_buf list and |
9622 * free any windows in the au_pending_free_win list. | |
7 | 9623 */ |
9624 if (!autocmd_busy) | |
9625 { | |
9626 restore_search_patterns(); | |
6608 | 9627 if (did_save_redobuff) |
11325
77f3b7316d8b
patch 8.0.0548: saving the redo buffer only works one time
Christian Brabandt <cb@256bit.org>
parents:
11199
diff
changeset
|
9628 restoreRedobuff(&save_redo); |
7 | 9629 did_filetype = FALSE; |
5816 | 9630 while (au_pending_free_buf != NULL) |
9631 { | |
9632 buf_T *b = au_pending_free_buf->b_next; | |
9633 vim_free(au_pending_free_buf); | |
9634 au_pending_free_buf = b; | |
9635 } | |
5958 | 9636 while (au_pending_free_win != NULL) |
9637 { | |
9638 win_T *w = au_pending_free_win->w_next; | |
9639 vim_free(au_pending_free_win); | |
9640 au_pending_free_win = w; | |
9641 } | |
7 | 9642 } |
9643 | |
9644 /* | |
9645 * Some events don't set or reset the Changed flag. | |
9646 * Check if still in the same buffer! | |
9647 */ | |
9648 if (curbuf == old_curbuf | |
9649 && (event == EVENT_BUFREADPOST | |
9650 || event == EVENT_BUFWRITEPOST | |
9651 || event == EVENT_FILEAPPENDPOST | |
9652 || event == EVENT_VIMLEAVE | |
9653 || event == EVENT_VIMLEAVEPRE)) | |
9654 { | |
9655 #ifdef FEAT_TITLE | |
9656 if (curbuf->b_changed != save_changed) | |
9657 need_maketitle = TRUE; | |
9658 #endif | |
9659 curbuf->b_changed = save_changed; | |
9660 } | |
9661 | |
9662 au_cleanup(); /* may really delete removed patterns/commands now */ | |
40 | 9663 |
9664 BYPASS_AU: | |
9665 /* When wiping out a buffer make sure all its buffer-local autocommands | |
9666 * are deleted. */ | |
9667 if (event == EVENT_BUFWIPEOUT && buf != NULL) | |
9668 aubuflocal_remove(buf); | |
9669 | |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
9670 if (retval == OK && event == EVENT_FILETYPE) |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
9671 au_did_filetype = TRUE; |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
9672 |
7 | 9673 return retval; |
9674 } | |
9675 | |
1410 | 9676 # ifdef FEAT_EVAL |
9677 static char_u *old_termresponse = NULL; | |
9678 # endif | |
9679 | |
9680 /* | |
9681 * Block triggering autocommands until unblock_autocmd() is called. | |
9682 * Can be used recursively, so long as it's symmetric. | |
9683 */ | |
9684 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9685 block_autocmds(void) |
1410 | 9686 { |
9687 # ifdef FEAT_EVAL | |
9688 /* Remember the value of v:termresponse. */ | |
9689 if (autocmd_blocked == 0) | |
9690 old_termresponse = get_vim_var_str(VV_TERMRESPONSE); | |
9691 # endif | |
9692 ++autocmd_blocked; | |
9693 } | |
9694 | |
9695 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9696 unblock_autocmds(void) |
1410 | 9697 { |
9698 --autocmd_blocked; | |
9699 | |
9700 # ifdef FEAT_EVAL | |
9701 /* When v:termresponse was set while autocommands were blocked, trigger | |
9702 * the autocommands now. Esp. useful when executing a shell command | |
9703 * during startup (vimdiff). */ | |
9704 if (autocmd_blocked == 0 | |
9705 && get_vim_var_str(VV_TERMRESPONSE) != old_termresponse) | |
9706 apply_autocmds(EVENT_TERMRESPONSE, NULL, NULL, FALSE, curbuf); | |
9707 # endif | |
9708 } | |
9709 | |
5008
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9710 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9711 is_autocmd_blocked(void) |
5008
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9712 { |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9713 return autocmd_blocked != 0; |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9714 } |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9715 |
7 | 9716 /* |
9717 * Find next autocommand pattern that matches. | |
9718 */ | |
9719 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9720 auto_next_pat( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9721 AutoPatCmd *apc, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9722 int stop_at_last) /* stop when 'last' flag is set */ |
7 | 9723 { |
9724 AutoPat *ap; | |
9725 AutoCmd *cp; | |
9726 char_u *name; | |
9727 char *s; | |
9728 | |
9729 vim_free(sourcing_name); | |
9730 sourcing_name = NULL; | |
9731 | |
9732 for (ap = apc->curpat; ap != NULL && !got_int; ap = ap->next) | |
9733 { | |
9734 apc->curpat = NULL; | |
9735 | |
1723 | 9736 /* Only use a pattern when it has not been removed, has commands and |
40 | 9737 * the group matches. For buffer-local autocommands only check the |
9738 * buffer number. */ | |
7 | 9739 if (ap->pat != NULL && ap->cmds != NULL |
9740 && (apc->group == AUGROUP_ALL || apc->group == ap->group)) | |
9741 { | |
40 | 9742 /* execution-condition */ |
9743 if (ap->buflocal_nr == 0 | |
6375 | 9744 ? (match_file_pat(NULL, &ap->reg_prog, apc->fname, |
153 | 9745 apc->sfname, apc->tail, ap->allow_dirs)) |
40 | 9746 : ap->buflocal_nr == apc->arg_bufnr) |
7 | 9747 { |
9748 name = event_nr2name(apc->event); | |
9749 s = _("%s Auto commands for \"%s\""); | |
9750 sourcing_name = alloc((unsigned)(STRLEN(s) | |
9751 + STRLEN(name) + ap->patlen + 1)); | |
9752 if (sourcing_name != NULL) | |
9753 { | |
9754 sprintf((char *)sourcing_name, s, | |
9755 (char *)name, (char *)ap->pat); | |
9756 if (p_verbose >= 8) | |
292 | 9757 { |
9758 verbose_enter(); | |
273 | 9759 smsg((char_u *)_("Executing %s"), sourcing_name); |
292 | 9760 verbose_leave(); |
9761 } | |
7 | 9762 } |
9763 | |
9764 apc->curpat = ap; | |
9765 apc->nextcmd = ap->cmds; | |
9766 /* mark last command */ | |
9767 for (cp = ap->cmds; cp->next != NULL; cp = cp->next) | |
9768 cp->last = FALSE; | |
9769 cp->last = TRUE; | |
9770 } | |
9771 line_breakcheck(); | |
9772 if (apc->curpat != NULL) /* found a match */ | |
9773 break; | |
9774 } | |
9775 if (stop_at_last && ap->last) | |
9776 break; | |
9777 } | |
9778 } | |
9779 | |
9780 /* | |
9781 * Get next autocommand command. | |
9782 * Called by do_cmdline() to get the next line for ":if". | |
9783 * Returns allocated string, or NULL for end of autocommands. | |
9784 */ | |
3997 | 9785 char_u * |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9786 getnextac(int c UNUSED, void *cookie, int indent UNUSED) |
7 | 9787 { |
9788 AutoPatCmd *acp = (AutoPatCmd *)cookie; | |
9789 char_u *retval; | |
9790 AutoCmd *ac; | |
9791 | |
9792 /* Can be called again after returning the last line. */ | |
9793 if (acp->curpat == NULL) | |
9794 return NULL; | |
9795 | |
9796 /* repeat until we find an autocommand to execute */ | |
9797 for (;;) | |
9798 { | |
9799 /* skip removed commands */ | |
9800 while (acp->nextcmd != NULL && acp->nextcmd->cmd == NULL) | |
9801 if (acp->nextcmd->last) | |
9802 acp->nextcmd = NULL; | |
9803 else | |
9804 acp->nextcmd = acp->nextcmd->next; | |
9805 | |
9806 if (acp->nextcmd != NULL) | |
9807 break; | |
9808 | |
9809 /* at end of commands, find next pattern that matches */ | |
9810 if (acp->curpat->last) | |
9811 acp->curpat = NULL; | |
9812 else | |
9813 acp->curpat = acp->curpat->next; | |
9814 if (acp->curpat != NULL) | |
9815 auto_next_pat(acp, TRUE); | |
9816 if (acp->curpat == NULL) | |
9817 return NULL; | |
9818 } | |
9819 | |
9820 ac = acp->nextcmd; | |
9821 | |
9822 if (p_verbose >= 9) | |
9823 { | |
292 | 9824 verbose_enter_scroll(); |
273 | 9825 smsg((char_u *)_("autocommand %s"), ac->cmd); |
7 | 9826 msg_puts((char_u *)"\n"); /* don't overwrite this either */ |
292 | 9827 verbose_leave_scroll(); |
7 | 9828 } |
9829 retval = vim_strsave(ac->cmd); | |
9830 autocmd_nested = ac->nested; | |
9831 #ifdef FEAT_EVAL | |
9832 current_SID = ac->scriptID; | |
9833 #endif | |
9834 if (ac->last) | |
9835 acp->nextcmd = NULL; | |
9836 else | |
9837 acp->nextcmd = ac->next; | |
9838 return retval; | |
9839 } | |
9840 | |
9841 /* | |
9842 * Return TRUE if there is a matching autocommand for "fname". | |
40 | 9843 * To account for buffer-local autocommands, function needs to know |
9844 * in which buffer the file will be opened. | |
7 | 9845 */ |
9846 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9847 has_autocmd(event_T event, char_u *sfname, buf_T *buf) |
7 | 9848 { |
9849 AutoPat *ap; | |
9850 char_u *fname; | |
9851 char_u *tail = gettail(sfname); | |
9852 int retval = FALSE; | |
9853 | |
9854 fname = FullName_save(sfname, FALSE); | |
9855 if (fname == NULL) | |
9856 return FALSE; | |
9857 | |
9858 #ifdef BACKSLASH_IN_FILENAME | |
9859 /* | |
9860 * Replace all backslashes with forward slashes. This makes the | |
9861 * autocommand patterns portable between Unix and MS-DOS. | |
9862 */ | |
9863 sfname = vim_strsave(sfname); | |
9864 if (sfname != NULL) | |
9865 forward_slash(sfname); | |
9866 forward_slash(fname); | |
9867 #endif | |
9868 | |
9869 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
9870 if (ap->pat != NULL && ap->cmds != NULL | |
856 | 9871 && (ap->buflocal_nr == 0 |
6375 | 9872 ? match_file_pat(NULL, &ap->reg_prog, |
153 | 9873 fname, sfname, tail, ap->allow_dirs) |
856 | 9874 : buf != NULL && ap->buflocal_nr == buf->b_fnum |
40 | 9875 )) |
7 | 9876 { |
9877 retval = TRUE; | |
9878 break; | |
9879 } | |
9880 | |
9881 vim_free(fname); | |
9882 #ifdef BACKSLASH_IN_FILENAME | |
9883 vim_free(sfname); | |
9884 #endif | |
9885 | |
9886 return retval; | |
9887 } | |
9888 | |
9889 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
9890 /* | |
9891 * Function given to ExpandGeneric() to obtain the list of autocommand group | |
9892 * names. | |
9893 */ | |
9894 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9895 get_augroup_name(expand_T *xp UNUSED, int idx) |
7 | 9896 { |
9897 if (idx == augroups.ga_len) /* add "END" add the end */ | |
9898 return (char_u *)"END"; | |
9899 if (idx >= augroups.ga_len) /* end of list */ | |
9900 return NULL; | |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
9901 if (AUGROUP_NAME(idx) == NULL || AUGROUP_NAME(idx) == get_deleted_augroup()) |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
9902 /* skip deleted entries */ |
7 | 9903 return (char_u *)""; |
9904 return AUGROUP_NAME(idx); /* return a name */ | |
9905 } | |
9906 | |
9907 static int include_groups = FALSE; | |
9908 | |
9909 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9910 set_context_in_autocmd( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9911 expand_T *xp, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9912 char_u *arg, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9913 int doautocmd) /* TRUE for :doauto*, FALSE for :autocmd */ |
7 | 9914 { |
9915 char_u *p; | |
9916 int group; | |
9917 | |
9918 /* check for a group name, skip it if present */ | |
9919 include_groups = FALSE; | |
9920 p = arg; | |
9921 group = au_get_grouparg(&arg); | |
9922 if (group == AUGROUP_ERROR) | |
9923 return NULL; | |
9924 /* If there only is a group name that's what we expand. */ | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
9925 if (*arg == NUL && group != AUGROUP_ALL && !VIM_ISWHITE(arg[-1])) |
7 | 9926 { |
9927 arg = p; | |
9928 group = AUGROUP_ALL; | |
9929 } | |
9930 | |
9931 /* skip over event name */ | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
9932 for (p = arg; *p != NUL && !VIM_ISWHITE(*p); ++p) |
7 | 9933 if (*p == ',') |
9934 arg = p + 1; | |
9935 if (*p == NUL) | |
9936 { | |
9937 if (group == AUGROUP_ALL) | |
9938 include_groups = TRUE; | |
9939 xp->xp_context = EXPAND_EVENTS; /* expand event name */ | |
9940 xp->xp_pattern = arg; | |
9941 return NULL; | |
9942 } | |
9943 | |
9944 /* skip over pattern */ | |
9945 arg = skipwhite(p); | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
9946 while (*arg && (!VIM_ISWHITE(*arg) || arg[-1] == '\\')) |
7 | 9947 arg++; |
9948 if (*arg) | |
9949 return arg; /* expand (next) command */ | |
9950 | |
9951 if (doautocmd) | |
9952 xp->xp_context = EXPAND_FILES; /* expand file names */ | |
9953 else | |
9954 xp->xp_context = EXPAND_NOTHING; /* pattern is not expanded */ | |
9955 return NULL; | |
9956 } | |
9957 | |
9958 /* | |
9959 * Function given to ExpandGeneric() to obtain the list of event names. | |
9960 */ | |
9961 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9962 get_event_name(expand_T *xp UNUSED, int idx) |
7 | 9963 { |
9964 if (idx < augroups.ga_len) /* First list group names, if wanted */ | |
9965 { | |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
9966 if (!include_groups || AUGROUP_NAME(idx) == NULL |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
9967 || AUGROUP_NAME(idx) == get_deleted_augroup()) |
7 | 9968 return (char_u *)""; /* skip deleted entries */ |
9969 return AUGROUP_NAME(idx); /* return a name */ | |
9970 } | |
9971 return (char_u *)event_names[idx - augroups.ga_len].name; | |
9972 } | |
9973 | |
9974 #endif /* FEAT_CMDL_COMPL */ | |
9975 | |
9976 /* | |
615 | 9977 * Return TRUE if autocmd is supported. |
9978 */ | |
9979 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9980 autocmd_supported(char_u *name) |
615 | 9981 { |
9982 char_u *p; | |
9983 | |
9984 return (event_name2nr(name, &p) != NUM_EVENTS); | |
9985 } | |
9986 | |
9987 /* | |
612 | 9988 * Return TRUE if an autocommand is defined for a group, event and |
9989 * pattern: The group can be omitted to accept any group. "event" and "pattern" | |
9990 * can be NULL to accept any event and pattern. "pattern" can be NULL to accept | |
9991 * any pattern. Buffer-local patterns <buffer> or <buffer=N> are accepted. | |
9992 * Used for: | |
9993 * exists("#Group") or | |
9994 * exists("#Group#Event") or | |
9995 * exists("#Group#Event#pat") or | |
9996 * exists("#Event") or | |
9997 * exists("#Event#pat") | |
7 | 9998 */ |
9999 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10000 au_exists(char_u *arg) |
7 | 10001 { |
612 | 10002 char_u *arg_save; |
10003 char_u *pattern = NULL; | |
7 | 10004 char_u *event_name; |
10005 char_u *p; | |
661 | 10006 event_T event; |
7 | 10007 AutoPat *ap; |
40 | 10008 buf_T *buflocal_buf = NULL; |
612 | 10009 int group; |
10010 int retval = FALSE; | |
10011 | |
615 | 10012 /* Make a copy so that we can change the '#' chars to a NUL. */ |
612 | 10013 arg_save = vim_strsave(arg); |
10014 if (arg_save == NULL) | |
10015 return FALSE; | |
615 | 10016 p = vim_strchr(arg_save, '#'); |
612 | 10017 if (p != NULL) |
10018 *p++ = NUL; | |
10019 | |
10020 /* First, look for an autocmd group name */ | |
10021 group = au_find_group(arg_save); | |
10022 if (group == AUGROUP_ERROR) | |
10023 { | |
10024 /* Didn't match a group name, assume the first argument is an event. */ | |
10025 group = AUGROUP_ALL; | |
10026 event_name = arg_save; | |
10027 } | |
10028 else | |
10029 { | |
10030 if (p == NULL) | |
10031 { | |
10032 /* "Group": group name is present and it's recognized */ | |
10033 retval = TRUE; | |
10034 goto theend; | |
10035 } | |
10036 | |
10037 /* Must be "Group#Event" or "Group#Event#pat". */ | |
10038 event_name = p; | |
10039 p = vim_strchr(event_name, '#'); | |
10040 if (p != NULL) | |
10041 *p++ = NUL; /* "Group#Event#pat" */ | |
10042 } | |
10043 | |
10044 pattern = p; /* "pattern" is NULL when there is no pattern */ | |
7 | 10045 |
10046 /* find the index (enum) for the event name */ | |
10047 event = event_name2nr(event_name, &p); | |
10048 | |
10049 /* return FALSE if the event name is not recognized */ | |
612 | 10050 if (event == NUM_EVENTS) |
10051 goto theend; | |
7 | 10052 |
10053 /* Find the first autocommand for this event. | |
10054 * If there isn't any, return FALSE; | |
10055 * If there is one and no pattern given, return TRUE; */ | |
10056 ap = first_autopat[(int)event]; | |
10057 if (ap == NULL) | |
612 | 10058 goto theend; |
7 | 10059 |
40 | 10060 /* if pattern is "<buffer>", special handling is needed which uses curbuf */ |
10061 /* for pattern "<buffer=N>, fnamecmp() will work fine */ | |
1962 | 10062 if (pattern != NULL && STRICMP(pattern, "<buffer>") == 0) |
40 | 10063 buflocal_buf = curbuf; |
10064 | |
7 | 10065 /* Check if there is an autocommand with the given pattern. */ |
10066 for ( ; ap != NULL; ap = ap->next) | |
40 | 10067 /* only use a pattern when it has not been removed and has commands. */ |
10068 /* For buffer-local autocommands, fnamecmp() works fine. */ | |
7 | 10069 if (ap->pat != NULL && ap->cmds != NULL |
612 | 10070 && (group == AUGROUP_ALL || ap->group == group) |
1962 | 10071 && (pattern == NULL |
10072 || (buflocal_buf == NULL | |
10073 ? fnamecmp(ap->pat, pattern) == 0 | |
10074 : ap->buflocal_nr == buflocal_buf->b_fnum))) | |
612 | 10075 { |
10076 retval = TRUE; | |
10077 break; | |
10078 } | |
10079 | |
10080 theend: | |
10081 vim_free(arg_save); | |
10082 return retval; | |
7 | 10083 } |
40 | 10084 |
934 | 10085 #else /* FEAT_AUTOCMD */ |
10086 | |
10087 /* | |
10088 * Prepare for executing commands for (hidden) buffer "buf". | |
10089 * This is the non-autocommand version, it simply saves "curbuf" and sets | |
10090 * "curbuf" and "curwin" to match "buf". | |
10091 */ | |
10092 void | |
7856
226ed297307f
commit https://github.com/vim/vim/commit/d14e00ea67afbaa8cb4a7e6b1eb306da6a2d5adb
Christian Brabandt <cb@256bit.org>
parents:
7819
diff
changeset
|
10093 aucmd_prepbuf( |
226ed297307f
commit https://github.com/vim/vim/commit/d14e00ea67afbaa8cb4a7e6b1eb306da6a2d5adb
Christian Brabandt <cb@256bit.org>
parents:
7819
diff
changeset
|
10094 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
|
10095 buf_T *buf) /* new curbuf */ |
934 | 10096 { |
1906 | 10097 aco->save_curbuf = curbuf; |
10098 --curbuf->b_nwindows; | |
934 | 10099 curbuf = buf; |
10100 curwin->w_buffer = buf; | |
1906 | 10101 ++curbuf->b_nwindows; |
934 | 10102 } |
10103 | |
10104 /* | |
10105 * Restore after executing commands for a (hidden) buffer. | |
10106 * This is the non-autocommand version. | |
10107 */ | |
10108 void | |
7856
226ed297307f
commit https://github.com/vim/vim/commit/d14e00ea67afbaa8cb4a7e6b1eb306da6a2d5adb
Christian Brabandt <cb@256bit.org>
parents:
7819
diff
changeset
|
10109 aucmd_restbuf( |
226ed297307f
commit https://github.com/vim/vim/commit/d14e00ea67afbaa8cb4a7e6b1eb306da6a2d5adb
Christian Brabandt <cb@256bit.org>
parents:
7819
diff
changeset
|
10110 aco_save_T *aco) /* structure holding saved values */ |
934 | 10111 { |
1906 | 10112 --curbuf->b_nwindows; |
10113 curbuf = aco->save_curbuf; | |
934 | 10114 curwin->w_buffer = curbuf; |
1906 | 10115 ++curbuf->b_nwindows; |
934 | 10116 } |
10117 | |
7 | 10118 #endif /* FEAT_AUTOCMD */ |
10119 | |
934 | 10120 |
7 | 10121 #if defined(FEAT_AUTOCMD) || defined(FEAT_WILDIGN) || defined(PROTO) |
10122 /* | |
153 | 10123 * Try matching a filename with a "pattern" ("prog" is NULL), or use the |
10124 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling | |
10125 * vim_regcomp() often. | |
7 | 10126 * Used for autocommands and 'wildignore'. |
10127 * Returns TRUE if there is a match, FALSE otherwise. | |
10128 */ | |
6375 | 10129 static int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10130 match_file_pat( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10131 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
|
10132 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
|
10133 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
|
10134 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
|
10135 char_u *tail, /* tail of path */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10136 int allow_dirs) /* allow matching with dir */ |
7 | 10137 { |
10138 regmatch_T regmatch; | |
10139 int result = FALSE; | |
10140 | |
4242 | 10141 regmatch.rm_ic = p_fic; /* ignore case if 'fileignorecase' is set */ |
6470 | 10142 if (prog != NULL) |
10143 regmatch.regprog = *prog; | |
7 | 10144 else |
6470 | 10145 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC); |
7 | 10146 |
10147 /* | |
10148 * Try for a match with the pattern with: | |
10149 * 1. the full file name, when the pattern has a '/'. | |
10150 * 2. the short file name, when the pattern has a '/'. | |
10151 * 3. the tail of the file name, when the pattern has no '/'. | |
10152 */ | |
6470 | 10153 if (regmatch.regprog != NULL |
7 | 10154 && ((allow_dirs |
10155 && (vim_regexec(®match, fname, (colnr_T)0) | |
10156 || (sfname != NULL | |
10157 && vim_regexec(®match, sfname, (colnr_T)0)))) | |
6470 | 10158 || (!allow_dirs && vim_regexec(®match, tail, (colnr_T)0)))) |
7 | 10159 result = TRUE; |
10160 | |
6375 | 10161 if (prog != NULL) |
10162 *prog = regmatch.regprog; | |
10163 else | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4354
diff
changeset
|
10164 vim_regfree(regmatch.regprog); |
7 | 10165 return result; |
10166 } | |
10167 #endif | |
10168 | |
10169 #if defined(FEAT_WILDIGN) || defined(PROTO) | |
10170 /* | |
10171 * Return TRUE if a file matches with a pattern in "list". | |
10172 * "list" is a comma-separated list of patterns, like 'wildignore'. | |
10173 * "sfname" is the short file name or NULL, "ffname" the long file name. | |
10174 */ | |
10175 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10176 match_file_list(char_u *list, char_u *sfname, char_u *ffname) |
7 | 10177 { |
10178 char_u buf[100]; | |
10179 char_u *tail; | |
10180 char_u *regpat; | |
10181 char allow_dirs; | |
10182 int match; | |
10183 char_u *p; | |
10184 | |
10185 tail = gettail(sfname); | |
10186 | |
10187 /* try all patterns in 'wildignore' */ | |
10188 p = list; | |
10189 while (*p) | |
10190 { | |
10191 copy_option_part(&p, buf, 100, ","); | |
10192 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE); | |
10193 if (regpat == NULL) | |
10194 break; | |
153 | 10195 match = match_file_pat(regpat, NULL, ffname, sfname, |
10196 tail, (int)allow_dirs); | |
7 | 10197 vim_free(regpat); |
10198 if (match) | |
10199 return TRUE; | |
10200 } | |
10201 return FALSE; | |
10202 } | |
10203 #endif | |
10204 | |
10205 /* | |
10206 * Convert the given pattern "pat" which has shell style wildcards in it, into | |
10207 * a regular expression, and return the result in allocated memory. If there | |
10208 * is a directory path separator to be matched, then TRUE is put in | |
10209 * allow_dirs, otherwise FALSE is put there -- webb. | |
10210 * Handle backslashes before special characters, like "\*" and "\ ". | |
10211 * | |
10212 * Returns NULL when out of memory. | |
10213 */ | |
10214 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10215 file_pat_to_reg_pat( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10216 char_u *pat, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10217 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
|
10218 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
|
10219 int no_bslash UNUSED) /* Don't use a backward slash as pathsep */ |
7 | 10220 { |
6470 | 10221 int size = 2; /* '^' at start, '$' at end */ |
7 | 10222 char_u *endp; |
10223 char_u *reg_pat; | |
10224 char_u *p; | |
10225 int i; | |
10226 int nested = 0; | |
10227 int add_dollar = TRUE; | |
10228 | |
10229 if (allow_dirs != NULL) | |
10230 *allow_dirs = FALSE; | |
10231 if (pat_end == NULL) | |
10232 pat_end = pat + STRLEN(pat); | |
10233 | |
10234 for (p = pat; p < pat_end; p++) | |
10235 { | |
10236 switch (*p) | |
10237 { | |
10238 case '*': | |
10239 case '.': | |
10240 case ',': | |
10241 case '{': | |
10242 case '}': | |
10243 case '~': | |
10244 size += 2; /* extra backslash */ | |
10245 break; | |
10246 #ifdef BACKSLASH_IN_FILENAME | |
10247 case '\\': | |
10248 case '/': | |
10249 size += 4; /* could become "[\/]" */ | |
10250 break; | |
10251 #endif | |
10252 default: | |
10253 size++; | |
6999 | 10254 # ifdef FEAT_MBYTE |
474 | 10255 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 10256 { |
10257 ++p; | |
10258 ++size; | |
10259 } | |
10260 # endif | |
10261 break; | |
10262 } | |
10263 } | |
10264 reg_pat = alloc(size + 1); | |
10265 if (reg_pat == NULL) | |
10266 return NULL; | |
10267 | |
10268 i = 0; | |
10269 | |
10270 if (pat[0] == '*') | |
10271 while (pat[0] == '*' && pat < pat_end - 1) | |
10272 pat++; | |
10273 else | |
10274 reg_pat[i++] = '^'; | |
10275 endp = pat_end - 1; | |
7005 | 10276 if (endp >= pat && *endp == '*') |
7 | 10277 { |
10278 while (endp - pat > 0 && *endp == '*') | |
10279 endp--; | |
10280 add_dollar = FALSE; | |
10281 } | |
10282 for (p = pat; *p && nested >= 0 && p <= endp; p++) | |
10283 { | |
10284 switch (*p) | |
10285 { | |
10286 case '*': | |
10287 reg_pat[i++] = '.'; | |
10288 reg_pat[i++] = '*'; | |
444 | 10289 while (p[1] == '*') /* "**" matches like "*" */ |
10290 ++p; | |
7 | 10291 break; |
10292 case '.': | |
10293 case '~': | |
10294 reg_pat[i++] = '\\'; | |
10295 reg_pat[i++] = *p; | |
10296 break; | |
10297 case '?': | |
10298 reg_pat[i++] = '.'; | |
10299 break; | |
10300 case '\\': | |
10301 if (p[1] == NUL) | |
10302 break; | |
10303 #ifdef BACKSLASH_IN_FILENAME | |
10304 if (!no_bslash) | |
10305 { | |
10306 /* translate: | |
10307 * "\x" to "\\x" e.g., "dir\file" | |
10308 * "\*" to "\\.*" e.g., "dir\*.c" | |
10309 * "\?" to "\\." e.g., "dir\??.c" | |
10310 * "\+" to "\+" e.g., "fileX\+.c" | |
10311 */ | |
10312 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?') | |
10313 && p[1] != '+') | |
10314 { | |
10315 reg_pat[i++] = '['; | |
10316 reg_pat[i++] = '\\'; | |
10317 reg_pat[i++] = '/'; | |
10318 reg_pat[i++] = ']'; | |
10319 if (allow_dirs != NULL) | |
10320 *allow_dirs = TRUE; | |
10321 break; | |
10322 } | |
10323 } | |
10324 #endif | |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10325 /* Undo escaping from ExpandEscape(): |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10326 * foo\?bar -> foo?bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10327 * foo\%bar -> foo%bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10328 * foo\,bar -> foo,bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10329 * foo\ bar -> foo bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10330 * 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
|
10331 * regexp. |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
5008
diff
changeset
|
10332 * 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
|
10333 * 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
|
10334 */ |
7 | 10335 if (*++p == '?' |
10336 #ifdef BACKSLASH_IN_FILENAME | |
10337 && no_bslash | |
10338 #endif | |
10339 ) | |
10340 reg_pat[i++] = '?'; | |
10341 else | |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
5008
diff
changeset
|
10342 if (*p == ',' || *p == '%' || *p == '#' |
6999 | 10343 || vim_isspace(*p) || *p == '{' || *p == '}') |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10344 reg_pat[i++] = *p; |
5259
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10345 else if (*p == '\\' && p[1] == '\\' && p[2] == '{') |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10346 { |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10347 reg_pat[i++] = '\\'; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10348 reg_pat[i++] = '{'; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10349 p += 2; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10350 } |
7 | 10351 else |
10352 { | |
10353 if (allow_dirs != NULL && vim_ispathsep(*p) | |
10354 #ifdef BACKSLASH_IN_FILENAME | |
10355 && (!no_bslash || *p != '\\') | |
10356 #endif | |
10357 ) | |
10358 *allow_dirs = TRUE; | |
10359 reg_pat[i++] = '\\'; | |
10360 reg_pat[i++] = *p; | |
10361 } | |
10362 break; | |
10363 #ifdef BACKSLASH_IN_FILENAME | |
10364 case '/': | |
10365 reg_pat[i++] = '['; | |
10366 reg_pat[i++] = '\\'; | |
10367 reg_pat[i++] = '/'; | |
10368 reg_pat[i++] = ']'; | |
10369 if (allow_dirs != NULL) | |
10370 *allow_dirs = TRUE; | |
10371 break; | |
10372 #endif | |
10373 case '{': | |
10374 reg_pat[i++] = '\\'; | |
10375 reg_pat[i++] = '('; | |
10376 nested++; | |
10377 break; | |
10378 case '}': | |
10379 reg_pat[i++] = '\\'; | |
10380 reg_pat[i++] = ')'; | |
10381 --nested; | |
10382 break; | |
10383 case ',': | |
10384 if (nested) | |
10385 { | |
10386 reg_pat[i++] = '\\'; | |
10387 reg_pat[i++] = '|'; | |
10388 } | |
10389 else | |
10390 reg_pat[i++] = ','; | |
10391 break; | |
10392 default: | |
10393 # ifdef FEAT_MBYTE | |
474 | 10394 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 10395 reg_pat[i++] = *p++; |
10396 else | |
10397 # endif | |
10398 if (allow_dirs != NULL && vim_ispathsep(*p)) | |
10399 *allow_dirs = TRUE; | |
10400 reg_pat[i++] = *p; | |
10401 break; | |
10402 } | |
10403 } | |
10404 if (add_dollar) | |
10405 reg_pat[i++] = '$'; | |
10406 reg_pat[i] = NUL; | |
10407 if (nested != 0) | |
10408 { | |
10409 if (nested < 0) | |
10410 EMSG(_("E219: Missing {.")); | |
10411 else | |
10412 EMSG(_("E220: Missing }.")); | |
10413 vim_free(reg_pat); | |
10414 reg_pat = NULL; | |
10415 } | |
10416 return reg_pat; | |
10417 } | |
2664 | 10418 |
10419 #if defined(EINTR) || defined(PROTO) | |
10420 /* | |
10421 * Version of read() that retries when interrupted by EINTR (possibly | |
10422 * by a SIGWINCH). | |
10423 */ | |
10424 long | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10425 read_eintr(int fd, void *buf, size_t bufsize) |
2664 | 10426 { |
10427 long ret; | |
10428 | |
10429 for (;;) | |
10430 { | |
10431 ret = vim_read(fd, buf, bufsize); | |
10432 if (ret >= 0 || errno != EINTR) | |
10433 break; | |
10434 } | |
10435 return ret; | |
10436 } | |
10437 | |
10438 /* | |
10439 * Version of write() that retries when interrupted by EINTR (possibly | |
10440 * by a SIGWINCH). | |
10441 */ | |
10442 long | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10443 write_eintr(int fd, void *buf, size_t bufsize) |
2664 | 10444 { |
10445 long ret = 0; | |
10446 long wlen; | |
10447 | |
10448 /* Repeat the write() so long it didn't fail, other than being interrupted | |
10449 * by a signal. */ | |
10450 while (ret < (long)bufsize) | |
10451 { | |
2666 | 10452 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret); |
2664 | 10453 if (wlen < 0) |
10454 { | |
10455 if (errno != EINTR) | |
10456 break; | |
10457 } | |
10458 else | |
10459 ret += wlen; | |
10460 } | |
10461 return ret; | |
10462 } | |
10463 #endif |