Mercurial > vim
annotate src/fileio.c @ 12771:8984342ab09e v8.0.1263
patch 8.0.1263: others can read the swap file if a user is careless
commit https://github.com/vim/vim/commit/5a73e0ca54c77e067c3b12ea6f35e3e8681e8cf8
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat Nov 4 21:35:01 2017 +0100
patch 8.0.1263: others can read the swap file if a user is careless
Problem: Others can read the swap file if a user is careless with his
primary group.
Solution: If the group permission allows for reading but the world
permissions doesn't, make sure the group is right.
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sat, 04 Nov 2017 21:45:04 +0100 |
parents | 351cf7c67bbe |
children | 9d51b8c6b84e |
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 | |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
71 # ifdef MACOS_CONVERT |
7 | 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 |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
130 # ifdef MACOS_CONVERT |
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) | |
12771
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
719 { |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
720 char_u *swap_fname = curbuf->b_ml.ml_mfp->mf_fname; |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
721 |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
722 /* |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
723 * If the group-read bit is set but not the world-read bit, then |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
724 * the group must be equal to the group of the original file. If |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
725 * we can't make that happen then reset the group-read bit. This |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
726 * avoids making the swap file readable to more users when the |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
727 * primary group of the user is too permissive. |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
728 */ |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
729 if ((swap_mode & 044) == 040) |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
730 { |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
731 stat_T swap_st; |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
732 |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
733 if (mch_stat((char *)swap_fname, &swap_st) >= 0 |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
734 && st.st_gid != swap_st.st_gid |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
735 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid) |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
736 == -1) |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
737 swap_mode &= 0600; |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
738 } |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
739 |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
740 (void)mch_setperm(swap_fname, (long)swap_mode); |
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
741 } |
7 | 742 #endif |
743 } | |
744 | |
578 | 745 #if defined(HAS_SWAP_EXISTS_ACTION) |
7 | 746 /* If "Quit" selected at ATTENTION dialog, don't load the file */ |
747 if (swap_exists_action == SEA_QUIT) | |
748 { | |
749 if (!read_buffer && !read_stdin) | |
750 close(fd); | |
751 return FAIL; | |
752 } | |
753 #endif | |
754 | |
755 ++no_wait_return; /* don't wait for return yet */ | |
756 | |
757 /* | |
758 * Set '[ mark to the line above where the lines go (line 1 if zero). | |
759 */ | |
760 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); | |
761 curbuf->b_op_start.col = 0; | |
762 | |
10668
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
763 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
|
764 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
|
765 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
|
766 |
7 | 767 #ifdef FEAT_AUTOCMD |
768 if (!read_buffer) | |
769 { | |
770 int m = msg_scroll; | |
771 int n = msg_scrolled; | |
772 | |
773 /* | |
774 * The file must be closed again, the autocommands may want to change | |
775 * the file before reading it. | |
776 */ | |
777 if (!read_stdin) | |
778 close(fd); /* ignore errors */ | |
779 | |
780 /* | |
781 * The output from the autocommands should not overwrite anything and | |
782 * should not be overwritten: Set msg_scroll, restore its value if no | |
783 * output was done. | |
784 */ | |
785 msg_scroll = TRUE; | |
786 if (filtering) | |
787 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname, | |
788 FALSE, curbuf, eap); | |
789 else if (read_stdin) | |
790 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname, | |
791 FALSE, curbuf, eap); | |
792 else if (newfile) | |
793 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname, | |
794 FALSE, curbuf, eap); | |
795 else | |
796 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname, | |
797 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
|
798 /* 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
|
799 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
|
800 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
|
801 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
|
802 |
7 | 803 if (msg_scrolled == n) |
804 msg_scroll = m; | |
805 | |
806 #ifdef FEAT_EVAL | |
807 if (aborting()) /* autocmds may abort script processing */ | |
808 { | |
809 --no_wait_return; | |
810 msg_scroll = msg_save; | |
811 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
812 return FAIL; | |
813 } | |
814 #endif | |
815 /* | |
816 * Don't allow the autocommands to change the current buffer. | |
817 * Try to re-open the file. | |
1834 | 818 * |
819 * Don't allow the autocommands to change the buffer name either | |
820 * (cd for example) if it invalidates fname or sfname. | |
7 | 821 */ |
822 if (!read_stdin && (curbuf != old_curbuf | |
1834 | 823 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) |
824 || (using_b_fname && (old_b_fname != curbuf->b_fname)) | |
7 | 825 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0)) |
826 { | |
827 --no_wait_return; | |
828 msg_scroll = msg_save; | |
829 if (fd < 0) | |
830 EMSG(_("E200: *ReadPre autocommands made the file unreadable")); | |
831 else | |
832 EMSG(_("E201: *ReadPre autocommands must not change current buffer")); | |
833 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
834 return FAIL; | |
835 } | |
836 } | |
837 #endif /* FEAT_AUTOCMD */ | |
838 | |
839 /* Autocommands may add lines to the file, need to check if it is empty */ | |
840 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY); | |
841 | |
842 if (!recoverymode && !filtering && !(flags & READ_DUMMY)) | |
843 { | |
844 /* | |
845 * Show the user that we are busy reading the input. Sometimes this | |
846 * may take a while. When reading from stdin another program may | |
847 * still be running, don't move the cursor to the last line, unless | |
848 * always using the GUI. | |
849 */ | |
850 if (read_stdin) | |
851 { | |
852 #ifndef ALWAYS_USE_GUI | |
853 mch_msg(_("Vim: Reading from stdin...\n")); | |
854 #endif | |
855 #ifdef FEAT_GUI | |
856 /* Also write a message in the GUI window, if there is one. */ | |
857 if (gui.in_use && !gui.dying && !gui.starting) | |
858 { | |
859 p = (char_u *)_("Reading from stdin..."); | |
860 gui_write(p, (int)STRLEN(p)); | |
861 } | |
862 #endif | |
863 } | |
864 else if (!read_buffer) | |
865 filemess(curbuf, sfname, (char_u *)"", 0); | |
866 } | |
867 | |
868 msg_scroll = FALSE; /* overwrite the file message */ | |
869 | |
870 /* | |
871 * Set linecnt now, before the "retry" caused by a wrong guess for | |
872 * fileformat, and after the autocommands, which may change them. | |
873 */ | |
874 linecnt = curbuf->b_ml.ml_line_count; | |
875 | |
876 #ifdef FEAT_MBYTE | |
595 | 877 /* "++bad=" argument. */ |
878 if (eap != NULL && eap->bad_char != 0) | |
612 | 879 { |
595 | 880 bad_char_behavior = eap->bad_char; |
819 | 881 if (set_options) |
612 | 882 curbuf->b_bad_char = eap->bad_char; |
883 } | |
884 else | |
885 curbuf->b_bad_char = 0; | |
595 | 886 |
7 | 887 /* |
595 | 888 * Decide which 'encoding' to use or use first. |
7 | 889 */ |
890 if (eap != NULL && eap->force_enc != 0) | |
891 { | |
892 fenc = enc_canonize(eap->cmd + eap->force_enc); | |
893 fenc_alloced = TRUE; | |
595 | 894 keep_dest_enc = TRUE; |
7 | 895 } |
896 else if (curbuf->b_p_bin) | |
897 { | |
898 fenc = (char_u *)""; /* binary: don't convert */ | |
899 fenc_alloced = FALSE; | |
900 } | |
901 else if (curbuf->b_help) | |
902 { | |
903 char_u firstline[80]; | |
301 | 904 int fc; |
7 | 905 |
906 /* Help files are either utf-8 or latin1. Try utf-8 first, if this | |
907 * fails it must be latin1. | |
908 * Always do this when 'encoding' is "utf-8". Otherwise only do | |
909 * this when needed to avoid [converted] remarks all the time. | |
910 * It is needed when the first line contains non-ASCII characters. | |
911 * That is only in *.??x files. */ | |
912 fenc = (char_u *)"latin1"; | |
913 c = enc_utf8; | |
301 | 914 if (!c && !read_stdin) |
915 { | |
916 fc = fname[STRLEN(fname) - 1]; | |
917 if (TOLOWER_ASC(fc) == 'x') | |
918 { | |
919 /* Read the first line (and a bit more). Immediately rewind to | |
920 * the start of the file. If the read() fails "len" is -1. */ | |
2664 | 921 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
|
922 vim_lseek(fd, (off_T)0L, SEEK_SET); |
301 | 923 for (p = firstline; p < firstline + len; ++p) |
924 if (*p >= 0x80) | |
925 { | |
926 c = TRUE; | |
927 break; | |
928 } | |
929 } | |
7 | 930 } |
931 | |
932 if (c) | |
933 { | |
934 fenc_next = fenc; | |
935 fenc = (char_u *)"utf-8"; | |
936 | |
937 /* When the file is utf-8 but a character doesn't fit in | |
938 * 'encoding' don't retry. In help text editing utf-8 bytes | |
939 * doesn't make sense. */ | |
844 | 940 if (!enc_utf8) |
941 keep_dest_enc = TRUE; | |
7 | 942 } |
943 fenc_alloced = FALSE; | |
944 } | |
945 else if (*p_fencs == NUL) | |
946 { | |
947 fenc = curbuf->b_p_fenc; /* use format from buffer */ | |
948 fenc_alloced = FALSE; | |
949 } | |
950 else | |
951 { | |
952 fenc_next = p_fencs; /* try items in 'fileencodings' */ | |
953 fenc = next_fenc(&fenc_next); | |
954 fenc_alloced = TRUE; | |
955 } | |
956 #endif | |
957 | |
958 /* | |
959 * Jump back here to retry reading the file in different ways. | |
960 * Reasons to retry: | |
961 * - encoding conversion failed: try another one from "fenc_next" | |
962 * - BOM detected and fenc was set, need to setup conversion | |
963 * - "fileformat" check failed: try another | |
964 * | |
965 * Variables set for special retry actions: | |
966 * "file_rewind" Rewind the file to start reading it again. | |
967 * "advance_fenc" Advance "fenc" using "fenc_next". | |
968 * "skip_read" Re-use already read bytes (BOM detected). | |
969 * "did_iconv" iconv() conversion failed, try 'charconvert'. | |
970 * "keep_fileformat" Don't reset "fileformat". | |
971 * | |
972 * Other status indicators: | |
973 * "tmpname" When != NULL did conversion with 'charconvert'. | |
974 * Output file has to be deleted afterwards. | |
975 * "iconv_fd" When != -1 did conversion with iconv(). | |
976 */ | |
977 retry: | |
978 | |
979 if (file_rewind) | |
980 { | |
981 if (read_buffer) | |
982 { | |
983 read_buf_lnum = 1; | |
984 read_buf_col = 0; | |
985 } | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
986 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0) |
7 | 987 { |
988 /* Can't rewind the file, give up. */ | |
989 error = TRUE; | |
990 goto failed; | |
991 } | |
992 /* Delete the previously read lines. */ | |
993 while (lnum > from) | |
994 ml_delete(lnum--, FALSE); | |
995 file_rewind = FALSE; | |
996 #ifdef FEAT_MBYTE | |
819 | 997 if (set_options) |
1352 | 998 { |
7 | 999 curbuf->b_p_bomb = FALSE; |
1352 | 1000 curbuf->b_start_bomb = FALSE; |
1001 } | |
595 | 1002 conv_error = 0; |
7 | 1003 #endif |
1004 } | |
1005 | |
1006 /* | |
1007 * When retrying with another "fenc" and the first time "fileformat" | |
1008 * will be reset. | |
1009 */ | |
1010 if (keep_fileformat) | |
1011 keep_fileformat = FALSE; | |
1012 else | |
1013 { | |
1014 if (eap != NULL && eap->force_ff != 0) | |
1742 | 1015 { |
7 | 1016 fileformat = get_fileformat_force(curbuf, eap); |
1742 | 1017 try_unix = try_dos = try_mac = FALSE; |
1018 } | |
7 | 1019 else if (curbuf->b_p_bin) |
1020 fileformat = EOL_UNIX; /* binary: use Unix format */ | |
1021 else if (*p_ffs == NUL) | |
1022 fileformat = get_fileformat(curbuf);/* use format from buffer */ | |
1023 else | |
1024 fileformat = EOL_UNKNOWN; /* detect from file */ | |
1025 } | |
1026 | |
1027 #ifdef FEAT_MBYTE | |
1028 # ifdef USE_ICONV | |
1029 if (iconv_fd != (iconv_t)-1) | |
1030 { | |
1031 /* aborted conversion with iconv(), close the descriptor */ | |
1032 iconv_close(iconv_fd); | |
1033 iconv_fd = (iconv_t)-1; | |
1034 } | |
1035 # endif | |
1036 | |
1037 if (advance_fenc) | |
1038 { | |
1039 /* | |
1040 * Try the next entry in 'fileencodings'. | |
1041 */ | |
1042 advance_fenc = FALSE; | |
1043 | |
1044 if (eap != NULL && eap->force_enc != 0) | |
1045 { | |
1046 /* Conversion given with "++cc=" wasn't possible, read | |
1047 * without conversion. */ | |
1048 notconverted = TRUE; | |
595 | 1049 conv_error = 0; |
7 | 1050 if (fenc_alloced) |
1051 vim_free(fenc); | |
1052 fenc = (char_u *)""; | |
1053 fenc_alloced = FALSE; | |
1054 } | |
1055 else | |
1056 { | |
1057 if (fenc_alloced) | |
1058 vim_free(fenc); | |
1059 if (fenc_next != NULL) | |
1060 { | |
1061 fenc = next_fenc(&fenc_next); | |
1062 fenc_alloced = (fenc_next != NULL); | |
1063 } | |
1064 else | |
1065 { | |
1066 fenc = (char_u *)""; | |
1067 fenc_alloced = FALSE; | |
1068 } | |
1069 } | |
1070 if (tmpname != NULL) | |
1071 { | |
1072 mch_remove(tmpname); /* delete converted file */ | |
1073 vim_free(tmpname); | |
1074 tmpname = NULL; | |
1075 } | |
1076 } | |
1077 | |
1078 /* | |
1948 | 1079 * Conversion may be required when the encoding of the file is different |
1080 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4. | |
7 | 1081 */ |
1082 fio_flags = 0; | |
1948 | 1083 converted = need_conversion(fenc); |
1084 if (converted) | |
7 | 1085 { |
1086 | |
1087 /* "ucs-bom" means we need to check the first bytes of the file | |
1088 * for a BOM. */ | |
1089 if (STRCMP(fenc, ENC_UCSBOM) == 0) | |
1090 fio_flags = FIO_UCSBOM; | |
1091 | |
1092 /* | |
1093 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be | |
1094 * done. This is handled below after read(). Prepare the | |
1095 * fio_flags to avoid having to parse the string each time. | |
1096 * Also check for Unicode to Latin1 conversion, because iconv() | |
1097 * appears not to handle this correctly. This works just like | |
1098 * conversion to UTF-8 except how the resulting character is put in | |
1099 * the buffer. | |
1100 */ | |
1101 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
1102 fio_flags = get_fio_flags(fenc); | |
1103 | |
1104 # ifdef WIN3264 | |
1105 /* | |
1106 * Conversion from an MS-Windows codepage to UTF-8 or another codepage | |
1107 * is handled with MultiByteToWideChar(). | |
1108 */ | |
1109 if (fio_flags == 0) | |
1110 fio_flags = get_win_fio_flags(fenc); | |
1111 # endif | |
1112 | |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
1113 # ifdef MACOS_CONVERT |
7 | 1114 /* Conversion from Apple MacRoman to latin1 or UTF-8 */ |
1115 if (fio_flags == 0) | |
1116 fio_flags = get_mac_fio_flags(fenc); | |
1117 # endif | |
1118 | |
1119 # ifdef USE_ICONV | |
1120 /* | |
1121 * Try using iconv() if we can't convert internally. | |
1122 */ | |
1123 if (fio_flags == 0 | |
1124 # ifdef FEAT_EVAL | |
1125 && !did_iconv | |
1126 # endif | |
1127 ) | |
1128 iconv_fd = (iconv_t)my_iconv_open( | |
1129 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc); | |
1130 # endif | |
1131 | |
1132 # ifdef FEAT_EVAL | |
1133 /* | |
1134 * Use the 'charconvert' expression when conversion is required | |
1135 * and we can't do it internally or with iconv(). | |
1136 */ | |
1137 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
|
1138 && !read_fifo |
7 | 1139 # ifdef USE_ICONV |
1140 && iconv_fd == (iconv_t)-1 | |
1141 # endif | |
1142 ) | |
1143 { | |
1144 # ifdef USE_ICONV | |
1145 did_iconv = FALSE; | |
1146 # endif | |
1147 /* Skip conversion when it's already done (retry for wrong | |
1148 * "fileformat"). */ | |
1149 if (tmpname == NULL) | |
1150 { | |
1151 tmpname = readfile_charconvert(fname, fenc, &fd); | |
1152 if (tmpname == NULL) | |
1153 { | |
1154 /* Conversion failed. Try another one. */ | |
1155 advance_fenc = TRUE; | |
1156 if (fd < 0) | |
1157 { | |
1158 /* Re-opening the original file failed! */ | |
1159 EMSG(_("E202: Conversion made file unreadable!")); | |
1160 error = TRUE; | |
1161 goto failed; | |
1162 } | |
1163 goto retry; | |
1164 } | |
1165 } | |
1166 } | |
1167 else | |
1168 # endif | |
1169 { | |
1170 if (fio_flags == 0 | |
1171 # ifdef USE_ICONV | |
1172 && iconv_fd == (iconv_t)-1 | |
1173 # endif | |
1174 ) | |
1175 { | |
1176 /* Conversion wanted but we can't. | |
1177 * Try the next conversion in 'fileencodings' */ | |
1178 advance_fenc = TRUE; | |
1179 goto retry; | |
1180 } | |
1181 } | |
1182 } | |
1183 | |
595 | 1184 /* Set "can_retry" when it's possible to rewind the file and try with |
7 | 1185 * another "fenc" value. It's FALSE when no other "fenc" to try, reading |
595 | 1186 * 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
|
1187 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc); |
7 | 1188 #endif |
1189 | |
1190 if (!skip_read) | |
1191 { | |
1192 linerest = 0; | |
1193 filesize = 0; | |
1194 skip_count = lines_to_skip; | |
1195 read_count = lines_to_read; | |
1196 #ifdef FEAT_MBYTE | |
1197 conv_restlen = 0; | |
1198 #endif | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1199 #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
|
1200 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
|
1201 && 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
|
1202 && 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
|
1203 && !filtering |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
1204 && !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
|
1205 && !read_stdin |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1206 && !read_buffer); |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1207 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
|
1208 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
|
1209 #endif |
6122 | 1210 #ifdef FEAT_CRYPT |
1211 if (curbuf->b_cryptstate != NULL) | |
1212 { | |
1213 /* Need to free the state, but keep the key, don't want to ask for | |
1214 * it again. */ | |
1215 crypt_free_state(curbuf->b_cryptstate); | |
1216 curbuf->b_cryptstate = NULL; | |
1217 } | |
1218 #endif | |
7 | 1219 } |
1220 | |
1221 while (!error && !got_int) | |
1222 { | |
1223 /* | |
1224 * We allocate as much space for the file as we can get, plus | |
1225 * space for the old line plus room for one terminating NUL. | |
1226 * The amount is limited by the fact that read() only can read | |
1227 * upto max_unsigned characters (and other things). | |
1228 */ | |
5684 | 1229 #if VIM_SIZEOF_INT <= 2 |
7 | 1230 if (linerest >= 0x7ff0) |
1231 { | |
1232 ++split; | |
1233 *ptr = NL; /* split line by inserting a NL */ | |
1234 size = 1; | |
1235 } | |
1236 else | |
1237 #endif | |
1238 { | |
1239 if (!skip_read) | |
1240 { | |
5684 | 1241 #if VIM_SIZEOF_INT > 2 |
1076 | 1242 # if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L) |
7 | 1243 size = SSIZE_MAX; /* use max I/O size, 52K */ |
1244 # else | |
1245 size = 0x10000L; /* use buffer >= 64K */ | |
1246 # endif | |
1247 #else | |
1248 size = 0x7ff0L - linerest; /* limit buffer to 32K */ | |
1249 #endif | |
1250 | |
836 | 1251 for ( ; size >= 10; size = (long)((long_u)size >> 1)) |
7 | 1252 { |
1253 if ((new_buffer = lalloc((long_u)(size + linerest + 1), | |
1254 FALSE)) != NULL) | |
1255 break; | |
1256 } | |
1257 if (new_buffer == NULL) | |
1258 { | |
1259 do_outofmem_msg((long_u)(size * 2 + linerest + 1)); | |
1260 error = TRUE; | |
1261 break; | |
1262 } | |
1263 if (linerest) /* copy characters from the previous buffer */ | |
1264 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest); | |
1265 vim_free(buffer); | |
1266 buffer = new_buffer; | |
1267 ptr = buffer + linerest; | |
1268 line_start = buffer; | |
1269 | |
1270 #ifdef FEAT_MBYTE | |
1271 /* May need room to translate into. | |
1272 * For iconv() we don't really know the required space, use a | |
1273 * factor ICONV_MULT. | |
1274 * latin1 to utf-8: 1 byte becomes up to 2 bytes | |
1275 * utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes | |
1276 * become up to 4 bytes, size must be multiple of 2 | |
1277 * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be | |
1278 * multiple of 2 | |
1279 * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be | |
1280 * multiple of 4 */ | |
835 | 1281 real_size = (int)size; |
7 | 1282 # ifdef USE_ICONV |
1283 if (iconv_fd != (iconv_t)-1) | |
1284 size = size / ICONV_MULT; | |
1285 else | |
1286 # endif | |
1287 if (fio_flags & FIO_LATIN1) | |
1288 size = size / 2; | |
1289 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1290 size = (size * 2 / 3) & ~1; | |
1291 else if (fio_flags & FIO_UCS4) | |
1292 size = (size * 2 / 3) & ~3; | |
1293 else if (fio_flags == FIO_UCSBOM) | |
1294 size = size / ICONV_MULT; /* worst case */ | |
1295 # ifdef WIN3264 | |
1296 else if (fio_flags & FIO_CODEPAGE) | |
1297 size = size / ICONV_MULT; /* also worst case */ | |
1298 # endif | |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
1299 # ifdef MACOS_CONVERT |
7 | 1300 else if (fio_flags & FIO_MACROMAN) |
1301 size = size / ICONV_MULT; /* also worst case */ | |
1302 # endif | |
1303 #endif | |
1304 | |
1305 #ifdef FEAT_MBYTE | |
1306 if (conv_restlen > 0) | |
1307 { | |
1308 /* Insert unconverted bytes from previous line. */ | |
1309 mch_memmove(ptr, conv_rest, conv_restlen); | |
1310 ptr += conv_restlen; | |
1311 size -= conv_restlen; | |
1312 } | |
1313 #endif | |
1314 | |
1315 if (read_buffer) | |
1316 { | |
1317 /* | |
1318 * Read bytes from curbuf. Used for converting text read | |
1319 * from stdin. | |
1320 */ | |
1321 if (read_buf_lnum > from) | |
1322 size = 0; | |
1323 else | |
1324 { | |
1325 int n, ni; | |
1326 long tlen; | |
1327 | |
1328 tlen = 0; | |
1329 for (;;) | |
1330 { | |
1331 p = ml_get(read_buf_lnum) + read_buf_col; | |
1332 n = (int)STRLEN(p); | |
1333 if ((int)tlen + n + 1 > size) | |
1334 { | |
1335 /* Filled up to "size", append partial line. | |
1336 * Change NL to NUL to reverse the effect done | |
1337 * below. */ | |
835 | 1338 n = (int)(size - tlen); |
7 | 1339 for (ni = 0; ni < n; ++ni) |
1340 { | |
1341 if (p[ni] == NL) | |
1342 ptr[tlen++] = NUL; | |
1343 else | |
1344 ptr[tlen++] = p[ni]; | |
1345 } | |
1346 read_buf_col += n; | |
1347 break; | |
1348 } | |
1349 else | |
1350 { | |
1351 /* Append whole line and new-line. Change NL | |
1352 * to NUL to reverse the effect done below. */ | |
1353 for (ni = 0; ni < n; ++ni) | |
1354 { | |
1355 if (p[ni] == NL) | |
1356 ptr[tlen++] = NUL; | |
1357 else | |
1358 ptr[tlen++] = p[ni]; | |
1359 } | |
1360 ptr[tlen++] = NL; | |
1361 read_buf_col = 0; | |
1362 if (++read_buf_lnum > from) | |
1363 { | |
1364 /* When the last line didn't have an | |
1365 * end-of-line don't add it now either. */ | |
1366 if (!curbuf->b_p_eol) | |
1367 --tlen; | |
1368 size = tlen; | |
1369 break; | |
1370 } | |
1371 } | |
1372 } | |
1373 } | |
1374 } | |
1375 else | |
1376 { | |
1377 /* | |
1378 * Read bytes from the file. | |
1379 */ | |
2664 | 1380 size = read_eintr(fd, ptr, size); |
7 | 1381 } |
1382 | |
6122 | 1383 #ifdef FEAT_CRYPT |
1384 /* | |
1385 * At start of file: Check for magic number of encryption. | |
1386 */ | |
1387 if (filesize == 0 && size > 0) | |
1388 cryptkey = check_for_cryptkey(cryptkey, ptr, &size, | |
1389 &filesize, newfile, sfname, | |
1390 &did_ask_for_key); | |
1391 /* | |
1392 * Decrypt the read bytes. This is done before checking for | |
1393 * EOF because the crypt layer may be buffering. | |
1394 */ | |
12216
e971ef6c0dee
patch 8.0.0988: warning from Covscan about using NULL pointer
Christian Brabandt <cb@256bit.org>
parents:
12064
diff
changeset
|
1395 if (cryptkey != NULL && curbuf->b_cryptstate != NULL |
e971ef6c0dee
patch 8.0.0988: warning from Covscan about using NULL pointer
Christian Brabandt <cb@256bit.org>
parents:
12064
diff
changeset
|
1396 && size > 0) |
6122 | 1397 { |
1398 if (crypt_works_inplace(curbuf->b_cryptstate)) | |
1399 { | |
1400 crypt_decode_inplace(curbuf->b_cryptstate, ptr, size); | |
1401 } | |
1402 else | |
1403 { | |
1404 char_u *newptr = NULL; | |
1405 int decrypted_size; | |
1406 | |
1407 decrypted_size = crypt_decode_alloc( | |
1408 curbuf->b_cryptstate, ptr, size, &newptr); | |
1409 | |
1410 /* If the crypt layer is buffering, not producing | |
1411 * anything yet, need to read more. */ | |
1412 if (size > 0 && decrypted_size == 0) | |
1413 continue; | |
1414 | |
1415 if (linerest == 0) | |
1416 { | |
1417 /* Simple case: reuse returned buffer (may be | |
1418 * NULL, checked later). */ | |
1419 new_buffer = newptr; | |
1420 } | |
1421 else | |
1422 { | |
1423 long_u new_size; | |
1424 | |
1425 /* Need new buffer to add bytes carried over. */ | |
1426 new_size = (long_u)(decrypted_size + linerest + 1); | |
1427 new_buffer = lalloc(new_size, FALSE); | |
1428 if (new_buffer == NULL) | |
1429 { | |
1430 do_outofmem_msg(new_size); | |
1431 error = TRUE; | |
1432 break; | |
1433 } | |
1434 | |
1435 mch_memmove(new_buffer, buffer, linerest); | |
1436 if (newptr != NULL) | |
1437 mch_memmove(new_buffer + linerest, newptr, | |
1438 decrypted_size); | |
1439 } | |
1440 | |
1441 if (new_buffer != NULL) | |
1442 { | |
1443 vim_free(buffer); | |
1444 buffer = new_buffer; | |
1445 new_buffer = NULL; | |
1446 line_start = buffer; | |
1447 ptr = buffer + linerest; | |
1448 } | |
1449 size = decrypted_size; | |
1450 } | |
1451 } | |
1452 #endif | |
1453 | |
7 | 1454 if (size <= 0) |
1455 { | |
1456 if (size < 0) /* read error */ | |
1457 error = TRUE; | |
1458 #ifdef FEAT_MBYTE | |
1459 else if (conv_restlen > 0) | |
595 | 1460 { |
1597 | 1461 /* |
1462 * Reached end-of-file but some trailing bytes could | |
1463 * not be converted. Truncated file? | |
1464 */ | |
1465 | |
1466 /* When we did a conversion report an error. */ | |
1467 if (fio_flags != 0 | |
1468 # ifdef USE_ICONV | |
1469 || iconv_fd != (iconv_t)-1 | |
1470 # endif | |
1471 ) | |
1472 { | |
4331 | 1473 if (can_retry) |
1474 goto rewind_retry; | |
1597 | 1475 if (conv_error == 0) |
1476 conv_error = curbuf->b_ml.ml_line_count | |
1477 - linecnt + 1; | |
1478 } | |
1479 /* Remember the first linenr with an illegal byte */ | |
1480 else if (illegal_byte == 0) | |
1481 illegal_byte = curbuf->b_ml.ml_line_count | |
1482 - linecnt + 1; | |
1483 if (bad_char_behavior == BAD_DROP) | |
595 | 1484 { |
1597 | 1485 *(ptr - conv_restlen) = NUL; |
1486 conv_restlen = 0; | |
1487 } | |
1488 else | |
1489 { | |
1490 /* Replace the trailing bytes with the replacement | |
1491 * character if we were converting; if we weren't, | |
1492 * leave the UTF8 checking code to do it, as it | |
1493 * works slightly differently. */ | |
1494 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0 | |
1495 # ifdef USE_ICONV | |
1496 || iconv_fd != (iconv_t)-1 | |
1497 # endif | |
1498 )) | |
1499 { | |
1500 while (conv_restlen > 0) | |
1501 { | |
1502 *(--ptr) = bad_char_behavior; | |
1503 --conv_restlen; | |
1504 } | |
1505 } | |
595 | 1506 fio_flags = 0; /* don't convert this */ |
834 | 1507 # ifdef USE_ICONV |
1508 if (iconv_fd != (iconv_t)-1) | |
1509 { | |
1510 iconv_close(iconv_fd); | |
1511 iconv_fd = (iconv_t)-1; | |
1512 } | |
1513 # endif | |
595 | 1514 } |
1515 } | |
7 | 1516 #endif |
1517 } | |
1518 } | |
1519 skip_read = FALSE; | |
1520 | |
1521 #ifdef FEAT_MBYTE | |
1522 /* | |
1523 * At start of file (or after crypt magic number): Check for BOM. | |
1524 * Also check for a BOM for other Unicode encodings, but not after | |
1525 * converting with 'charconvert' or when a BOM has already been | |
1526 * found. | |
1527 */ | |
1528 if ((filesize == 0 | |
1529 # ifdef FEAT_CRYPT | |
6122 | 1530 || (cryptkey != NULL |
1531 && filesize == crypt_get_header_len( | |
1532 crypt_get_method_nr(curbuf))) | |
7 | 1533 # endif |
1534 ) | |
1535 && (fio_flags == FIO_UCSBOM | |
1536 || (!curbuf->b_p_bomb | |
1537 && tmpname == NULL | |
1538 && (*fenc == 'u' || (*fenc == NUL && enc_utf8))))) | |
1539 { | |
1540 char_u *ccname; | |
1541 int blen; | |
1542 | |
1543 /* no BOM detection in a short file or in binary mode */ | |
1544 if (size < 2 || curbuf->b_p_bin) | |
1545 ccname = NULL; | |
1546 else | |
1547 ccname = check_for_bom(ptr, size, &blen, | |
1548 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc)); | |
1549 if (ccname != NULL) | |
1550 { | |
1551 /* Remove BOM from the text */ | |
1552 filesize += blen; | |
1553 size -= blen; | |
1554 mch_memmove(ptr, ptr + blen, (size_t)size); | |
819 | 1555 if (set_options) |
1352 | 1556 { |
7 | 1557 curbuf->b_p_bomb = TRUE; |
1352 | 1558 curbuf->b_start_bomb = TRUE; |
1559 } | |
7 | 1560 } |
1561 | |
1562 if (fio_flags == FIO_UCSBOM) | |
1563 { | |
1564 if (ccname == NULL) | |
1565 { | |
1566 /* No BOM detected: retry with next encoding. */ | |
1567 advance_fenc = TRUE; | |
1568 } | |
1569 else | |
1570 { | |
1571 /* BOM detected: set "fenc" and jump back */ | |
1572 if (fenc_alloced) | |
1573 vim_free(fenc); | |
1574 fenc = ccname; | |
1575 fenc_alloced = FALSE; | |
1576 } | |
1577 /* retry reading without getting new bytes or rewinding */ | |
1578 skip_read = TRUE; | |
1579 goto retry; | |
1580 } | |
1581 } | |
1597 | 1582 |
1583 /* Include not converted bytes. */ | |
1584 ptr -= conv_restlen; | |
1585 size += conv_restlen; | |
1586 conv_restlen = 0; | |
7 | 1587 #endif |
1588 /* | |
1589 * Break here for a read error or end-of-file. | |
1590 */ | |
1591 if (size <= 0) | |
1592 break; | |
1593 | |
1594 #ifdef FEAT_MBYTE | |
1595 | |
1596 # ifdef USE_ICONV | |
1597 if (iconv_fd != (iconv_t)-1) | |
1598 { | |
1599 /* | |
1600 * Attempt conversion of the read bytes to 'encoding' using | |
1601 * iconv(). | |
1602 */ | |
1603 const char *fromp; | |
1604 char *top; | |
1605 size_t from_size; | |
1606 size_t to_size; | |
1607 | |
1608 fromp = (char *)ptr; | |
1609 from_size = size; | |
1610 ptr += size; | |
1611 top = (char *)ptr; | |
1612 to_size = real_size - size; | |
1613 | |
1614 /* | |
1615 * If there is conversion error or not enough room try using | |
179 | 1616 * another conversion. Except for when there is no |
1617 * alternative (help files). | |
7 | 1618 */ |
177 | 1619 while ((iconv(iconv_fd, (void *)&fromp, &from_size, |
1620 &top, &to_size) | |
7 | 1621 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) |
1622 || from_size > CONV_RESTLEN) | |
177 | 1623 { |
595 | 1624 if (can_retry) |
375 | 1625 goto rewind_retry; |
595 | 1626 if (conv_error == 0) |
1627 conv_error = readfile_linenr(linecnt, | |
1628 ptr, (char_u *)top); | |
1629 | |
1630 /* Deal with a bad byte and continue with the next. */ | |
177 | 1631 ++fromp; |
1632 --from_size; | |
595 | 1633 if (bad_char_behavior == BAD_KEEP) |
1634 { | |
1635 *top++ = *(fromp - 1); | |
1636 --to_size; | |
1637 } | |
1638 else if (bad_char_behavior != BAD_DROP) | |
1639 { | |
1640 *top++ = bad_char_behavior; | |
1641 --to_size; | |
1642 } | |
177 | 1643 } |
7 | 1644 |
1645 if (from_size > 0) | |
1646 { | |
1647 /* Some remaining characters, keep them for the next | |
1648 * round. */ | |
1649 mch_memmove(conv_rest, (char_u *)fromp, from_size); | |
1650 conv_restlen = (int)from_size; | |
1651 } | |
1652 | |
1653 /* move the linerest to before the converted characters */ | |
1654 line_start = ptr - linerest; | |
1655 mch_memmove(line_start, buffer, (size_t)linerest); | |
1656 size = (long)((char_u *)top - ptr); | |
1657 } | |
1658 # endif | |
1659 | |
1660 # ifdef WIN3264 | |
1661 if (fio_flags & FIO_CODEPAGE) | |
1662 { | |
595 | 1663 char_u *src, *dst; |
1664 WCHAR ucs2buf[3]; | |
1665 int ucs2len; | |
1666 int codepage = FIO_GET_CP(fio_flags); | |
1667 int bytelen; | |
1668 int found_bad; | |
1669 char replstr[2]; | |
1670 | |
7 | 1671 /* |
1672 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or | |
595 | 1673 * a codepage, using standard MS-Windows functions. This |
1674 * requires two steps: | |
1675 * 1. convert from 'fileencoding' to ucs-2 | |
1676 * 2. convert from ucs-2 to 'encoding' | |
1677 * | |
1678 * Because there may be illegal bytes AND an incomplete byte | |
1679 * sequence at the end, we may have to do the conversion one | |
1680 * character at a time to get it right. | |
7 | 1681 */ |
595 | 1682 |
1683 /* Replacement string for WideCharToMultiByte(). */ | |
1684 if (bad_char_behavior > 0) | |
1685 replstr[0] = bad_char_behavior; | |
1686 else | |
1687 replstr[0] = '?'; | |
1688 replstr[1] = NUL; | |
1689 | |
1690 /* | |
1691 * Move the bytes to the end of the buffer, so that we have | |
1692 * room to put the result at the start. | |
1693 */ | |
1694 src = ptr + real_size - size; | |
1695 mch_memmove(src, ptr, size); | |
7 | 1696 |
1697 /* | |
595 | 1698 * Do the conversion. |
7 | 1699 */ |
595 | 1700 dst = ptr; |
1701 size = size; | |
1702 while (size > 0) | |
7 | 1703 { |
595 | 1704 found_bad = FALSE; |
7 | 1705 |
1706 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
595 | 1707 if (codepage == CP_UTF8) |
7 | 1708 { |
595 | 1709 /* Handle CP_UTF8 input ourselves to be able to handle |
1710 * trailing bytes properly. | |
1711 * Get one UTF-8 character from src. */ | |
835 | 1712 bytelen = (int)utf_ptr2len_len(src, size); |
595 | 1713 if (bytelen > size) |
1714 { | |
1715 /* Only got some bytes of a character. Normally | |
1716 * it's put in "conv_rest", but if it's too long | |
1717 * deal with it as if they were illegal bytes. */ | |
1718 if (bytelen <= CONV_RESTLEN) | |
1719 break; | |
1720 | |
1721 /* weird overlong byte sequence */ | |
1722 bytelen = size; | |
1723 found_bad = TRUE; | |
1724 } | |
1725 else | |
1726 { | |
799 | 1727 int u8c = utf_ptr2char(src); |
1728 | |
622 | 1729 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1)) |
595 | 1730 found_bad = TRUE; |
1731 ucs2buf[0] = u8c; | |
1732 ucs2len = 1; | |
1733 } | |
7 | 1734 } |
595 | 1735 else |
7 | 1736 # endif |
595 | 1737 { |
1738 /* We don't know how long the byte sequence is, try | |
1739 * from one to three bytes. */ | |
1740 for (bytelen = 1; bytelen <= size && bytelen <= 3; | |
1741 ++bytelen) | |
1742 { | |
1743 ucs2len = MultiByteToWideChar(codepage, | |
1744 MB_ERR_INVALID_CHARS, | |
1745 (LPCSTR)src, bytelen, | |
1746 ucs2buf, 3); | |
1747 if (ucs2len > 0) | |
1748 break; | |
1749 } | |
1750 if (ucs2len == 0) | |
1751 { | |
1752 /* If we have only one byte then it's probably an | |
1753 * incomplete byte sequence. Otherwise discard | |
1754 * one byte as a bad character. */ | |
1755 if (size == 1) | |
1756 break; | |
1757 found_bad = TRUE; | |
1758 bytelen = 1; | |
1759 } | |
1760 } | |
1761 | |
1762 if (!found_bad) | |
7 | 1763 { |
595 | 1764 int i; |
1765 | |
1766 /* Convert "ucs2buf[ucs2len]" to 'enc' in "dst". */ | |
1767 if (enc_utf8) | |
1768 { | |
1769 /* From UCS-2 to UTF-8. Cannot fail. */ | |
1770 for (i = 0; i < ucs2len; ++i) | |
1771 dst += utf_char2bytes(ucs2buf[i], dst); | |
1772 } | |
1773 else | |
1774 { | |
1775 BOOL bad = FALSE; | |
1776 int dstlen; | |
1777 | |
1778 /* From UCS-2 to "enc_codepage". If the | |
1779 * conversion uses the default character "?", | |
1780 * the data doesn't fit in this encoding. */ | |
1781 dstlen = WideCharToMultiByte(enc_codepage, 0, | |
1782 (LPCWSTR)ucs2buf, ucs2len, | |
835 | 1783 (LPSTR)dst, (int)(src - dst), |
595 | 1784 replstr, &bad); |
1785 if (bad) | |
1786 found_bad = TRUE; | |
1787 else | |
1788 dst += dstlen; | |
1789 } | |
7 | 1790 } |
595 | 1791 |
1792 if (found_bad) | |
1793 { | |
1794 /* Deal with bytes we can't convert. */ | |
1795 if (can_retry) | |
1796 goto rewind_retry; | |
1797 if (conv_error == 0) | |
1798 conv_error = readfile_linenr(linecnt, ptr, dst); | |
1799 if (bad_char_behavior != BAD_DROP) | |
1800 { | |
1801 if (bad_char_behavior == BAD_KEEP) | |
1802 { | |
1803 mch_memmove(dst, src, bytelen); | |
1804 dst += bytelen; | |
1805 } | |
1806 else | |
1807 *dst++ = bad_char_behavior; | |
1808 } | |
1809 } | |
1810 | |
1811 src += bytelen; | |
1812 size -= bytelen; | |
7 | 1813 } |
595 | 1814 |
1815 if (size > 0) | |
7 | 1816 { |
595 | 1817 /* An incomplete byte sequence remaining. */ |
1818 mch_memmove(conv_rest, src, size); | |
1819 conv_restlen = size; | |
7 | 1820 } |
595 | 1821 |
1822 /* The new size is equal to how much "dst" was advanced. */ | |
835 | 1823 size = (long)(dst - ptr); |
7 | 1824 } |
1825 else | |
1826 # endif | |
765 | 1827 # ifdef MACOS_CONVERT |
7 | 1828 if (fio_flags & FIO_MACROMAN) |
1829 { | |
1830 /* | |
1831 * Conversion from Apple MacRoman char encoding to UTF-8 or | |
18 | 1832 * latin1. This is in os_mac_conv.c. |
7 | 1833 */ |
18 | 1834 if (macroman2enc(ptr, &size, real_size) == FAIL) |
7 | 1835 goto rewind_retry; |
1836 } | |
1837 else | |
1838 # endif | |
1839 if (fio_flags != 0) | |
1840 { | |
1841 int u8c; | |
1842 char_u *dest; | |
1843 char_u *tail = NULL; | |
1844 | |
1845 /* | |
1846 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8. | |
1847 * "enc_utf8" not set: Convert Unicode to Latin1. | |
1848 * Go from end to start through the buffer, because the number | |
1849 * of bytes may increase. | |
1850 * "dest" points to after where the UTF-8 bytes go, "p" points | |
1851 * to after the next character to convert. | |
1852 */ | |
1853 dest = ptr + real_size; | |
1854 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8) | |
1855 { | |
1856 p = ptr + size; | |
1857 if (fio_flags == FIO_UTF8) | |
1858 { | |
1859 /* Check for a trailing incomplete UTF-8 sequence */ | |
1860 tail = ptr + size - 1; | |
1861 while (tail > ptr && (*tail & 0xc0) == 0x80) | |
1862 --tail; | |
1863 if (tail + utf_byte2len(*tail) <= ptr + size) | |
1864 tail = NULL; | |
1865 else | |
1866 p = tail; | |
1867 } | |
1868 } | |
1869 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1870 { | |
1871 /* Check for a trailing byte */ | |
1872 p = ptr + (size & ~1); | |
1873 if (size & 1) | |
1874 tail = p; | |
1875 if ((fio_flags & FIO_UTF16) && p > ptr) | |
1876 { | |
1877 /* Check for a trailing leading word */ | |
1878 if (fio_flags & FIO_ENDIAN_L) | |
1879 { | |
1880 u8c = (*--p << 8); | |
1881 u8c += *--p; | |
1882 } | |
1883 else | |
1884 { | |
1885 u8c = *--p; | |
1886 u8c += (*--p << 8); | |
1887 } | |
1888 if (u8c >= 0xd800 && u8c <= 0xdbff) | |
1889 tail = p; | |
1890 else | |
1891 p += 2; | |
1892 } | |
1893 } | |
1894 else /* FIO_UCS4 */ | |
1895 { | |
1896 /* Check for trailing 1, 2 or 3 bytes */ | |
1897 p = ptr + (size & ~3); | |
1898 if (size & 3) | |
1899 tail = p; | |
1900 } | |
1901 | |
1902 /* If there is a trailing incomplete sequence move it to | |
1903 * conv_rest[]. */ | |
1904 if (tail != NULL) | |
1905 { | |
1906 conv_restlen = (int)((ptr + size) - tail); | |
1907 mch_memmove(conv_rest, (char_u *)tail, conv_restlen); | |
1908 size -= conv_restlen; | |
1909 } | |
1910 | |
1911 | |
1912 while (p > ptr) | |
1913 { | |
1914 if (fio_flags & FIO_LATIN1) | |
1915 u8c = *--p; | |
1916 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1917 { | |
1918 if (fio_flags & FIO_ENDIAN_L) | |
1919 { | |
1920 u8c = (*--p << 8); | |
1921 u8c += *--p; | |
1922 } | |
1923 else | |
1924 { | |
1925 u8c = *--p; | |
1926 u8c += (*--p << 8); | |
1927 } | |
1928 if ((fio_flags & FIO_UTF16) | |
1929 && u8c >= 0xdc00 && u8c <= 0xdfff) | |
1930 { | |
1931 int u16c; | |
1932 | |
1933 if (p == ptr) | |
1934 { | |
1935 /* Missing leading word. */ | |
1936 if (can_retry) | |
1937 goto rewind_retry; | |
595 | 1938 if (conv_error == 0) |
1939 conv_error = readfile_linenr(linecnt, | |
1940 ptr, p); | |
1941 if (bad_char_behavior == BAD_DROP) | |
1942 continue; | |
1943 if (bad_char_behavior != BAD_KEEP) | |
1944 u8c = bad_char_behavior; | |
7 | 1945 } |
1946 | |
1947 /* found second word of double-word, get the first | |
1948 * word and compute the resulting character */ | |
1949 if (fio_flags & FIO_ENDIAN_L) | |
1950 { | |
1951 u16c = (*--p << 8); | |
1952 u16c += *--p; | |
1953 } | |
1954 else | |
1955 { | |
1956 u16c = *--p; | |
1957 u16c += (*--p << 8); | |
1958 } | |
595 | 1959 u8c = 0x10000 + ((u16c & 0x3ff) << 10) |
1960 + (u8c & 0x3ff); | |
1961 | |
7 | 1962 /* Check if the word is indeed a leading word. */ |
1963 if (u16c < 0xd800 || u16c > 0xdbff) | |
1964 { | |
1965 if (can_retry) | |
1966 goto rewind_retry; | |
595 | 1967 if (conv_error == 0) |
1968 conv_error = readfile_linenr(linecnt, | |
1969 ptr, p); | |
1970 if (bad_char_behavior == BAD_DROP) | |
1971 continue; | |
1972 if (bad_char_behavior != BAD_KEEP) | |
1973 u8c = bad_char_behavior; | |
7 | 1974 } |
1975 } | |
1976 } | |
1977 else if (fio_flags & FIO_UCS4) | |
1978 { | |
1979 if (fio_flags & FIO_ENDIAN_L) | |
1980 { | |
12698
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1981 u8c = (unsigned)*--p << 24; |
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1982 u8c += (unsigned)*--p << 16; |
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1983 u8c += (unsigned)*--p << 8; |
7 | 1984 u8c += *--p; |
1985 } | |
1986 else /* big endian */ | |
1987 { | |
1988 u8c = *--p; | |
12698
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1989 u8c += (unsigned)*--p << 8; |
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1990 u8c += (unsigned)*--p << 16; |
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1991 u8c += (unsigned)*--p << 24; |
7 | 1992 } |
1993 } | |
1994 else /* UTF-8 */ | |
1995 { | |
1996 if (*--p < 0x80) | |
1997 u8c = *p; | |
1998 else | |
1999 { | |
2000 len = utf_head_off(ptr, p); | |
595 | 2001 p -= len; |
2002 u8c = utf_ptr2char(p); | |
7 | 2003 if (len == 0) |
2004 { | |
2005 /* Not a valid UTF-8 character, retry with | |
2006 * another fenc when possible, otherwise just | |
2007 * report the error. */ | |
2008 if (can_retry) | |
2009 goto rewind_retry; | |
595 | 2010 if (conv_error == 0) |
2011 conv_error = readfile_linenr(linecnt, | |
2012 ptr, p); | |
2013 if (bad_char_behavior == BAD_DROP) | |
2014 continue; | |
2015 if (bad_char_behavior != BAD_KEEP) | |
2016 u8c = bad_char_behavior; | |
7 | 2017 } |
2018 } | |
2019 } | |
2020 if (enc_utf8) /* produce UTF-8 */ | |
2021 { | |
2022 dest -= utf_char2len(u8c); | |
2023 (void)utf_char2bytes(u8c, dest); | |
2024 } | |
2025 else /* produce Latin1 */ | |
2026 { | |
2027 --dest; | |
2028 if (u8c >= 0x100) | |
2029 { | |
2030 /* character doesn't fit in latin1, retry with | |
2031 * another fenc when possible, otherwise just | |
2032 * report the error. */ | |
595 | 2033 if (can_retry) |
7 | 2034 goto rewind_retry; |
595 | 2035 if (conv_error == 0) |
2036 conv_error = readfile_linenr(linecnt, ptr, p); | |
2037 if (bad_char_behavior == BAD_DROP) | |
2038 ++dest; | |
2039 else if (bad_char_behavior == BAD_KEEP) | |
2040 *dest = u8c; | |
2041 else if (eap != NULL && eap->bad_char != 0) | |
2042 *dest = bad_char_behavior; | |
2043 else | |
2044 *dest = 0xBF; | |
7 | 2045 } |
2046 else | |
2047 *dest = u8c; | |
2048 } | |
2049 } | |
2050 | |
2051 /* move the linerest to before the converted characters */ | |
2052 line_start = dest - linerest; | |
2053 mch_memmove(line_start, buffer, (size_t)linerest); | |
2054 size = (long)((ptr + real_size) - dest); | |
2055 ptr = dest; | |
2056 } | |
1597 | 2057 else if (enc_utf8 && !curbuf->b_p_bin) |
2058 { | |
2059 int incomplete_tail = FALSE; | |
2060 | |
2061 /* Reading UTF-8: Check if the bytes are valid UTF-8. */ | |
2062 for (p = ptr; ; ++p) | |
7 | 2063 { |
835 | 2064 int todo = (int)((ptr + size) - p); |
595 | 2065 int l; |
2066 | |
2067 if (todo <= 0) | |
2068 break; | |
7 | 2069 if (*p >= 0x80) |
2070 { | |
2071 /* A length of 1 means it's an illegal byte. Accept | |
2072 * an incomplete character at the end though, the next | |
2073 * read() will get the next bytes, we'll check it | |
2074 * then. */ | |
595 | 2075 l = utf_ptr2len_len(p, todo); |
1597 | 2076 if (l > todo && !incomplete_tail) |
7 | 2077 { |
1597 | 2078 /* Avoid retrying with a different encoding when |
2079 * a truncated file is more likely, or attempting | |
2080 * to read the rest of an incomplete sequence when | |
2081 * we have already done so. */ | |
2082 if (p > ptr || filesize > 0) | |
2083 incomplete_tail = TRUE; | |
2084 /* Incomplete byte sequence, move it to conv_rest[] | |
2085 * and try to read the rest of it, unless we've | |
2086 * already done so. */ | |
2087 if (p > ptr) | |
2088 { | |
2089 conv_restlen = todo; | |
2090 mch_memmove(conv_rest, p, conv_restlen); | |
2091 size -= conv_restlen; | |
2092 break; | |
2093 } | |
7 | 2094 } |
1597 | 2095 if (l == 1 || l > todo) |
595 | 2096 { |
2097 /* Illegal byte. If we can try another encoding | |
1597 | 2098 * do that, unless at EOF where a truncated |
2099 * file is more likely than a conversion error. */ | |
2100 if (can_retry && !incomplete_tail) | |
595 | 2101 break; |
2102 # ifdef USE_ICONV | |
2103 /* When we did a conversion report an error. */ | |
2104 if (iconv_fd != (iconv_t)-1 && conv_error == 0) | |
2105 conv_error = readfile_linenr(linecnt, ptr, p); | |
2106 # endif | |
1597 | 2107 /* Remember the first linenr with an illegal byte */ |
2108 if (conv_error == 0 && illegal_byte == 0) | |
2109 illegal_byte = readfile_linenr(linecnt, ptr, p); | |
595 | 2110 |
2111 /* Drop, keep or replace the bad byte. */ | |
2112 if (bad_char_behavior == BAD_DROP) | |
2113 { | |
1597 | 2114 mch_memmove(p, p + 1, todo - 1); |
595 | 2115 --p; |
2116 --size; | |
2117 } | |
2118 else if (bad_char_behavior != BAD_KEEP) | |
2119 *p = bad_char_behavior; | |
2120 } | |
1597 | 2121 else |
2122 p += l - 1; | |
7 | 2123 } |
2124 } | |
1597 | 2125 if (p < ptr + size && !incomplete_tail) |
7 | 2126 { |
2127 /* Detected a UTF-8 error. */ | |
2128 rewind_retry: | |
595 | 2129 /* Retry reading with another conversion. */ |
7 | 2130 # if defined(FEAT_EVAL) && defined(USE_ICONV) |
595 | 2131 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1) |
2132 /* iconv() failed, try 'charconvert' */ | |
2133 did_iconv = TRUE; | |
7 | 2134 else |
2135 # endif | |
595 | 2136 /* use next item from 'fileencodings' */ |
2137 advance_fenc = TRUE; | |
2138 file_rewind = TRUE; | |
2139 goto retry; | |
7 | 2140 } |
2141 } | |
2142 #endif | |
2143 | |
2144 /* count the number of characters (after conversion!) */ | |
2145 filesize += size; | |
2146 | |
2147 /* | |
2148 * when reading the first part of a file: guess EOL type | |
2149 */ | |
2150 if (fileformat == EOL_UNKNOWN) | |
2151 { | |
2152 /* First try finding a NL, for Dos and Unix */ | |
2153 if (try_dos || try_unix) | |
2154 { | |
6635 | 2155 /* Reset the carriage return counter. */ |
2156 if (try_mac) | |
2157 try_mac = 1; | |
2158 | |
7 | 2159 for (p = ptr; p < ptr + size; ++p) |
2160 { | |
2161 if (*p == NL) | |
2162 { | |
2163 if (!try_unix | |
2164 || (try_dos && p > ptr && p[-1] == CAR)) | |
2165 fileformat = EOL_DOS; | |
2166 else | |
2167 fileformat = EOL_UNIX; | |
2168 break; | |
2169 } | |
6618 | 2170 else if (*p == CAR && try_mac) |
2171 try_mac++; | |
7 | 2172 } |
2173 | |
2174 /* Don't give in to EOL_UNIX if EOL_MAC is more likely */ | |
2175 if (fileformat == EOL_UNIX && try_mac) | |
2176 { | |
2177 /* Need to reset the counters when retrying fenc. */ | |
2178 try_mac = 1; | |
2179 try_unix = 1; | |
2180 for (; p >= ptr && *p != CAR; p--) | |
2181 ; | |
2182 if (p >= ptr) | |
2183 { | |
2184 for (p = ptr; p < ptr + size; ++p) | |
2185 { | |
2186 if (*p == NL) | |
2187 try_unix++; | |
2188 else if (*p == CAR) | |
2189 try_mac++; | |
2190 } | |
2191 if (try_mac > try_unix) | |
2192 fileformat = EOL_MAC; | |
2193 } | |
2194 } | |
6618 | 2195 else if (fileformat == EOL_UNKNOWN && try_mac == 1) |
2196 /* Looking for CR but found no end-of-line markers at | |
2197 * all: use the default format. */ | |
2198 fileformat = default_fileformat(); | |
7 | 2199 } |
2200 | |
2201 /* No NL found: may use Mac format */ | |
2202 if (fileformat == EOL_UNKNOWN && try_mac) | |
2203 fileformat = EOL_MAC; | |
2204 | |
2205 /* Still nothing found? Use first format in 'ffs' */ | |
2206 if (fileformat == EOL_UNKNOWN) | |
2207 fileformat = default_fileformat(); | |
2208 | |
2209 /* if editing a new file: may set p_tx and p_ff */ | |
819 | 2210 if (set_options) |
7 | 2211 set_fileformat(fileformat, OPT_LOCAL); |
2212 } | |
2213 } | |
2214 | |
2215 /* | |
2216 * This loop is executed once for every character read. | |
2217 * Keep it fast! | |
2218 */ | |
2219 if (fileformat == EOL_MAC) | |
2220 { | |
2221 --ptr; | |
2222 while (++ptr, --size >= 0) | |
2223 { | |
2224 /* catch most common case first */ | |
2225 if ((c = *ptr) != NUL && c != CAR && c != NL) | |
2226 continue; | |
2227 if (c == NUL) | |
2228 *ptr = NL; /* NULs are replaced by newlines! */ | |
2229 else if (c == NL) | |
2230 *ptr = CAR; /* NLs are replaced by CRs! */ | |
2231 else | |
2232 { | |
2233 if (skip_count == 0) | |
2234 { | |
2235 *ptr = NUL; /* end of line */ | |
2236 len = (colnr_T) (ptr - line_start + 1); | |
2237 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2238 { | |
2239 error = TRUE; | |
2240 break; | |
2241 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2242 #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
|
2243 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
|
2244 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
|
2245 #endif |
7 | 2246 ++lnum; |
2247 if (--read_count == 0) | |
2248 { | |
2249 error = TRUE; /* break loop */ | |
2250 line_start = ptr; /* nothing left to write */ | |
2251 break; | |
2252 } | |
2253 } | |
2254 else | |
2255 --skip_count; | |
2256 line_start = ptr + 1; | |
2257 } | |
2258 } | |
2259 } | |
2260 else | |
2261 { | |
2262 --ptr; | |
2263 while (++ptr, --size >= 0) | |
2264 { | |
2265 if ((c = *ptr) != NUL && c != NL) /* catch most common case */ | |
2266 continue; | |
2267 if (c == NUL) | |
2268 *ptr = NL; /* NULs are replaced by newlines! */ | |
2269 else | |
2270 { | |
2271 if (skip_count == 0) | |
2272 { | |
2273 *ptr = NUL; /* end of line */ | |
2274 len = (colnr_T)(ptr - line_start + 1); | |
2275 if (fileformat == EOL_DOS) | |
2276 { | |
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
|
2277 if (ptr > line_start && ptr[-1] == CAR) |
7 | 2278 { |
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
|
2279 /* remove CR before NL */ |
7 | 2280 ptr[-1] = NUL; |
2281 --len; | |
2282 } | |
2283 /* | |
2284 * Reading in Dos format, but no CR-LF found! | |
2285 * When 'fileformats' includes "unix", delete all | |
2286 * the lines read so far and start all over again. | |
2287 * Otherwise give an error message later. | |
2288 */ | |
2289 else if (ff_error != EOL_DOS) | |
2290 { | |
2291 if ( try_unix | |
2292 && !read_stdin | |
2293 && (read_buffer | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
2294 || 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
|
2295 == 0)) |
7 | 2296 { |
2297 fileformat = EOL_UNIX; | |
819 | 2298 if (set_options) |
7 | 2299 set_fileformat(EOL_UNIX, OPT_LOCAL); |
2300 file_rewind = TRUE; | |
2301 keep_fileformat = TRUE; | |
2302 goto retry; | |
2303 } | |
2304 ff_error = EOL_DOS; | |
2305 } | |
2306 } | |
2307 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2308 { | |
2309 error = TRUE; | |
2310 break; | |
2311 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2312 #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
|
2313 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
|
2314 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
|
2315 #endif |
7 | 2316 ++lnum; |
2317 if (--read_count == 0) | |
2318 { | |
2319 error = TRUE; /* break loop */ | |
2320 line_start = ptr; /* nothing left to write */ | |
2321 break; | |
2322 } | |
2323 } | |
2324 else | |
2325 --skip_count; | |
2326 line_start = ptr + 1; | |
2327 } | |
2328 } | |
2329 } | |
2330 linerest = (long)(ptr - line_start); | |
2331 ui_breakcheck(); | |
2332 } | |
2333 | |
2334 failed: | |
2335 /* not an error, max. number of lines reached */ | |
2336 if (error && read_count == 0) | |
2337 error = FALSE; | |
2338 | |
2339 /* | |
2340 * If we get EOF in the middle of a line, note the fact and | |
2341 * complete the line ourselves. | |
2342 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set. | |
2343 */ | |
2344 if (!error | |
2345 && !got_int | |
2346 && linerest != 0 | |
2347 && !(!curbuf->b_p_bin | |
2348 && fileformat == EOL_DOS | |
2349 && *line_start == Ctrl_Z | |
2350 && ptr == line_start + 1)) | |
2351 { | |
819 | 2352 /* remember for when writing */ |
2353 if (set_options) | |
7 | 2354 curbuf->b_p_eol = FALSE; |
2355 *ptr = NUL; | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2356 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
|
2357 if (ml_append(lnum, line_start, len, newfile) == FAIL) |
7 | 2358 error = TRUE; |
2359 else | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2360 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2361 #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
|
2362 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
|
2363 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
|
2364 #endif |
7 | 2365 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
|
2366 } |
7 | 2367 } |
2368 | |
819 | 2369 if (set_options) |
7 | 2370 save_file_ff(curbuf); /* remember the current file format */ |
2371 | |
2372 #ifdef FEAT_CRYPT | |
6122 | 2373 if (curbuf->b_cryptstate != NULL) |
2374 { | |
2375 crypt_free_state(curbuf->b_cryptstate); | |
2376 curbuf->b_cryptstate = NULL; | |
2377 } | |
2378 if (cryptkey != NULL && cryptkey != curbuf->b_p_key) | |
2379 crypt_free_key(cryptkey); | |
2380 /* Don't set cryptkey to NULL, it's used below as a flag that | |
2381 * encryption was used. */ | |
7 | 2382 #endif |
2383 | |
2384 #ifdef FEAT_MBYTE | |
819 | 2385 /* If editing a new file: set 'fenc' for the current buffer. |
2386 * Also for ":read ++edit file". */ | |
2387 if (set_options) | |
7 | 2388 set_string_option_direct((char_u *)"fenc", -1, fenc, |
694 | 2389 OPT_FREE|OPT_LOCAL, 0); |
7 | 2390 if (fenc_alloced) |
2391 vim_free(fenc); | |
2392 # ifdef USE_ICONV | |
2393 if (iconv_fd != (iconv_t)-1) | |
2394 { | |
2395 iconv_close(iconv_fd); | |
2396 iconv_fd = (iconv_t)-1; | |
2397 } | |
2398 # endif | |
2399 #endif | |
2400 | |
2401 if (!read_buffer && !read_stdin) | |
2402 close(fd); /* errors are ignored */ | |
2003 | 2403 #ifdef HAVE_FD_CLOEXEC |
2404 else | |
2405 { | |
2406 int fdflags = fcntl(fd, F_GETFD); | |
2407 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
|
2408 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC); |
2003 | 2409 } |
2410 #endif | |
7 | 2411 vim_free(buffer); |
2412 | |
2413 #ifdef HAVE_DUP | |
2414 if (read_stdin) | |
2415 { | |
2416 /* Use stderr for stdin, makes shell commands work. */ | |
2417 close(0); | |
1757 | 2418 ignored = dup(2); |
7 | 2419 } |
2420 #endif | |
2421 | |
2422 #ifdef FEAT_MBYTE | |
2423 if (tmpname != NULL) | |
2424 { | |
2425 mch_remove(tmpname); /* delete converted file */ | |
2426 vim_free(tmpname); | |
2427 } | |
2428 #endif | |
2429 --no_wait_return; /* may wait for return now */ | |
2430 | |
2431 /* | |
2432 * In recovery mode everything but autocommands is skipped. | |
2433 */ | |
2434 if (!recoverymode) | |
2435 { | |
2436 /* need to delete the last line, which comes from the empty buffer */ | |
2437 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY)) | |
2438 { | |
2439 #ifdef FEAT_NETBEANS_INTG | |
2440 netbeansFireChanges = 0; | |
2441 #endif | |
2442 ml_delete(curbuf->b_ml.ml_line_count, FALSE); | |
2443 #ifdef FEAT_NETBEANS_INTG | |
2444 netbeansFireChanges = 1; | |
2445 #endif | |
2446 --linecnt; | |
2447 } | |
2448 linecnt = curbuf->b_ml.ml_line_count - linecnt; | |
2449 if (filesize == 0) | |
2450 linecnt = 0; | |
2451 if (newfile || read_buffer) | |
1201 | 2452 { |
7 | 2453 redraw_curbuf_later(NOT_VALID); |
1201 | 2454 #ifdef FEAT_DIFF |
2455 /* After reading the text into the buffer the diff info needs to | |
2456 * be updated. */ | |
2457 diff_invalidate(curbuf); | |
2458 #endif | |
2459 #ifdef FEAT_FOLDING | |
2460 /* All folds in the window are invalid now. Mark them for update | |
2461 * before triggering autocommands. */ | |
2462 foldUpdateAll(curwin); | |
2463 #endif | |
2464 } | |
7 | 2465 else if (linecnt) /* appended at least one line */ |
2466 appended_lines_mark(from, linecnt); | |
2467 | |
2468 #ifndef ALWAYS_USE_GUI | |
2469 /* | |
2470 * If we were reading from the same terminal as where messages go, | |
2471 * the screen will have been messed up. | |
2472 * Switch on raw mode now and clear the screen. | |
2473 */ | |
2474 if (read_stdin) | |
2475 { | |
2476 settmode(TMODE_RAW); /* set to raw mode */ | |
2477 starttermcap(); | |
2478 screenclear(); | |
2479 } | |
2480 #endif | |
2481 | |
2482 if (got_int) | |
2483 { | |
2484 if (!(flags & READ_DUMMY)) | |
2485 { | |
2486 filemess(curbuf, sfname, (char_u *)_(e_interr), 0); | |
2487 if (newfile) | |
2488 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
2489 } | |
2490 msg_scroll = msg_save; | |
2491 #ifdef FEAT_VIMINFO | |
2492 check_marks_read(); | |
2493 #endif | |
2494 return OK; /* an interrupt isn't really an error */ | |
2495 } | |
2496 | |
2497 if (!filtering && !(flags & READ_DUMMY)) | |
2498 { | |
2499 msg_add_fname(curbuf, sfname); /* fname in IObuff with quotes */ | |
2500 c = FALSE; | |
2501 | |
2502 #ifdef UNIX | |
2503 # ifdef S_ISFIFO | |
2504 if (S_ISFIFO(perm)) /* fifo or socket */ | |
2505 { | |
2506 STRCAT(IObuff, _("[fifo/socket]")); | |
2507 c = TRUE; | |
2508 } | |
2509 # else | |
2510 # ifdef S_IFIFO | |
2511 if ((perm & S_IFMT) == S_IFIFO) /* fifo */ | |
2512 { | |
2513 STRCAT(IObuff, _("[fifo]")); | |
2514 c = TRUE; | |
2515 } | |
2516 # endif | |
2517 # ifdef S_IFSOCK | |
2518 if ((perm & S_IFMT) == S_IFSOCK) /* or socket */ | |
2519 { | |
2520 STRCAT(IObuff, _("[socket]")); | |
2521 c = TRUE; | |
2522 } | |
2523 # endif | |
2524 # endif | |
1313 | 2525 # ifdef OPEN_CHR_FILES |
2526 if (S_ISCHR(perm)) /* or character special */ | |
2527 { | |
2528 STRCAT(IObuff, _("[character special]")); | |
2529 c = TRUE; | |
2530 } | |
2531 # endif | |
7 | 2532 #endif |
2533 if (curbuf->b_p_ro) | |
2534 { | |
2535 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")); | |
2536 c = TRUE; | |
2537 } | |
2538 if (read_no_eol_lnum) | |
2539 { | |
2540 msg_add_eol(); | |
2541 c = TRUE; | |
2542 } | |
2543 if (ff_error == EOL_DOS) | |
2544 { | |
2545 STRCAT(IObuff, _("[CR missing]")); | |
2546 c = TRUE; | |
2547 } | |
2548 if (split) | |
2549 { | |
2550 STRCAT(IObuff, _("[long lines split]")); | |
2551 c = TRUE; | |
2552 } | |
2553 #ifdef FEAT_MBYTE | |
2554 if (notconverted) | |
2555 { | |
2556 STRCAT(IObuff, _("[NOT converted]")); | |
2557 c = TRUE; | |
2558 } | |
2559 else if (converted) | |
2560 { | |
2561 STRCAT(IObuff, _("[converted]")); | |
2562 c = TRUE; | |
2563 } | |
2564 #endif | |
2565 #ifdef FEAT_CRYPT | |
2566 if (cryptkey != NULL) | |
2567 { | |
6122 | 2568 crypt_append_msg(curbuf); |
7 | 2569 c = TRUE; |
2570 } | |
2571 #endif | |
2572 #ifdef FEAT_MBYTE | |
595 | 2573 if (conv_error != 0) |
2574 { | |
2575 sprintf((char *)IObuff + STRLEN(IObuff), | |
2576 _("[CONVERSION ERROR in line %ld]"), (long)conv_error); | |
7 | 2577 c = TRUE; |
2578 } | |
2579 else if (illegal_byte > 0) | |
2580 { | |
2581 sprintf((char *)IObuff + STRLEN(IObuff), | |
2582 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte); | |
2583 c = TRUE; | |
2584 } | |
2585 else | |
2586 #endif | |
2587 if (error) | |
2588 { | |
2589 STRCAT(IObuff, _("[READ ERRORS]")); | |
2590 c = TRUE; | |
2591 } | |
2592 if (msg_add_fileformat(fileformat)) | |
2593 c = TRUE; | |
2594 #ifdef FEAT_CRYPT | |
2595 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
|
2596 msg_add_lines(c, (long)linecnt, filesize |
6122 | 2597 - crypt_get_header_len(crypt_get_method_nr(curbuf))); |
7 | 2598 else |
2599 #endif | |
2600 msg_add_lines(c, (long)linecnt, filesize); | |
2601 | |
2602 vim_free(keep_msg); | |
2603 keep_msg = NULL; | |
2604 msg_scrolled_ign = TRUE; | |
2605 #ifdef ALWAYS_USE_GUI | |
2606 /* Don't show the message when reading stdin, it would end up in a | |
2607 * message box (which might be shown when exiting!) */ | |
2608 if (read_stdin || read_buffer) | |
2609 p = msg_may_trunc(FALSE, IObuff); | |
2610 else | |
2611 #endif | |
2612 p = msg_trunc_attr(IObuff, FALSE, 0); | |
2613 if (read_stdin || read_buffer || restart_edit != 0 | |
540 | 2614 || (msg_scrolled != 0 && !need_wait_return)) |
7 | 2615 /* Need to repeat the message after redrawing when: |
2616 * - When reading from stdin (the screen will be cleared next). | |
2617 * - When restart_edit is set (otherwise there will be a delay | |
2618 * before redrawing). | |
2619 * - When the screen was scrolled but there is no wait-return | |
2620 * prompt. */ | |
679 | 2621 set_keep_msg(p, 0); |
7 | 2622 msg_scrolled_ign = FALSE; |
2623 } | |
2624 | |
2625 /* with errors writing the file requires ":w!" */ | |
2626 if (newfile && (error | |
2627 #ifdef FEAT_MBYTE | |
595 | 2628 || conv_error != 0 |
679 | 2629 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP) |
7 | 2630 #endif |
2631 )) | |
2632 curbuf->b_p_ro = TRUE; | |
2633 | |
2634 u_clearline(); /* cannot use "U" command after adding lines */ | |
2635 | |
2636 /* | |
2637 * In Ex mode: cursor at last new line. | |
2638 * Otherwise: cursor at first new line. | |
2639 */ | |
2640 if (exmode_active) | |
2641 curwin->w_cursor.lnum = from + linecnt; | |
2642 else | |
2643 curwin->w_cursor.lnum = from + 1; | |
2644 check_cursor_lnum(); | |
2645 beginline(BL_WHITE | BL_FIX); /* on first non-blank */ | |
2646 | |
2647 /* | |
2648 * Set '[ and '] marks to the newly read lines. | |
2649 */ | |
2650 curbuf->b_op_start.lnum = from + 1; | |
2651 curbuf->b_op_start.col = 0; | |
2652 curbuf->b_op_end.lnum = from + linecnt; | |
2653 curbuf->b_op_end.col = 0; | |
696 | 2654 |
2655 #ifdef WIN32 | |
2656 /* | |
2657 * Work around a weird problem: When a file has two links (only | |
2658 * possible on NTFS) and we write through one link, then stat() it | |
1651 | 2659 * through the other link, the timestamp information may be wrong. |
696 | 2660 * It's correct again after reading the file, thus reset the timestamp |
2661 * here. | |
2662 */ | |
2663 if (newfile && !read_stdin && !read_buffer | |
2664 && mch_stat((char *)fname, &st) >= 0) | |
2665 { | |
2666 buf_store_time(curbuf, &st, fname); | |
2667 curbuf->b_mtime_read = curbuf->b_mtime; | |
2668 } | |
2669 #endif | |
7 | 2670 } |
2671 msg_scroll = msg_save; | |
2672 | |
2673 #ifdef FEAT_VIMINFO | |
2674 /* | |
2675 * Get the marks before executing autocommands, so they can be used there. | |
2676 */ | |
2677 check_marks_read(); | |
2678 #endif | |
2679 | |
2680 /* | |
6933 | 2681 * We remember if the last line of the read didn't have |
2682 * an eol even when 'binary' is off, to support turning 'fixeol' off, | |
2683 * or writing the read again with 'binary' on. The latter is required | |
2684 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work. | |
7 | 2685 */ |
2707 | 2686 curbuf->b_no_eol_lnum = read_no_eol_lnum; |
7 | 2687 |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2688 /* 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
|
2689 * different. */ |
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2690 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
|
2691 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
|
2692 |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2693 #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
|
2694 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2695 * 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
|
2696 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2697 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
|
2698 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2699 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
|
2700 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2701 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
|
2702 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
|
2703 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2704 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2705 |
167 | 2706 #ifdef FEAT_AUTOCMD |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
2707 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL)) |
7 | 2708 { |
2709 int m = msg_scroll; | |
2710 int n = msg_scrolled; | |
2711 | |
2712 /* Save the fileformat now, otherwise the buffer will be considered | |
2713 * modified if the format/encoding was automatically detected. */ | |
819 | 2714 if (set_options) |
7 | 2715 save_file_ff(curbuf); |
2716 | |
2717 /* | |
2718 * The output from the autocommands should not overwrite anything and | |
2719 * should not be overwritten: Set msg_scroll, restore its value if no | |
2720 * output was done. | |
2721 */ | |
2722 msg_scroll = TRUE; | |
2723 if (filtering) | |
2724 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname, | |
2725 FALSE, curbuf, eap); | |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
2726 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
|
2727 { |
7 | 2728 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname, |
2729 FALSE, curbuf, eap); | |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2730 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
|
2731 /* |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2732 * 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
|
2733 * 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
|
2734 */ |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2735 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
|
2736 TRUE, curbuf); |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2737 } |
7 | 2738 else |
2739 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname, | |
2740 FALSE, NULL, eap); | |
2741 if (msg_scrolled == n) | |
2742 msg_scroll = m; | |
2707 | 2743 # ifdef FEAT_EVAL |
7 | 2744 if (aborting()) /* autocmds may abort script processing */ |
2745 return FAIL; | |
2707 | 2746 # endif |
2747 } | |
2748 #endif | |
2749 | |
7 | 2750 if (recoverymode && error) |
2751 return FAIL; | |
2752 return OK; | |
2753 } | |
2754 | |
9911
74e345d2878c
commit https://github.com/vim/vim/commit/f04507d132fbcb63999167ec006fc6e700b5af4f
Christian Brabandt <cb@256bit.org>
parents:
9828
diff
changeset
|
2755 #if defined(OPEN_CHR_FILES) || defined(PROTO) |
1313 | 2756 /* |
2757 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+", | |
2758 * which is the name of files used for process substitution output by | |
2759 * some shells on some operating systems, e.g., bash on SunOS. | |
2760 * Do not accept "/dev/fd/[012]", opening these may hang Vim. | |
2761 */ | |
9911
74e345d2878c
commit https://github.com/vim/vim/commit/f04507d132fbcb63999167ec006fc6e700b5af4f
Christian Brabandt <cb@256bit.org>
parents:
9828
diff
changeset
|
2762 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2763 is_dev_fd_file(char_u *fname) |
1313 | 2764 { |
2765 return (STRNCMP(fname, "/dev/fd/", 8) == 0 | |
2766 && VIM_ISDIGIT(fname[8]) | |
2767 && *skipdigits(fname + 9) == NUL | |
2768 && (fname[9] != NUL | |
2769 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2'))); | |
2770 } | |
2771 #endif | |
2772 | |
595 | 2773 #ifdef FEAT_MBYTE |
2774 | |
2775 /* | |
2776 * From the current line count and characters read after that, estimate the | |
2777 * line number where we are now. | |
2778 * Used for error messages that include a line number. | |
2779 */ | |
2780 static linenr_T | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2781 readfile_linenr( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2782 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
|
2783 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
|
2784 char_u *endp) /* end of more bytes read */ |
595 | 2785 { |
2786 char_u *s; | |
2787 linenr_T lnum; | |
2788 | |
2789 lnum = curbuf->b_ml.ml_line_count - linecnt + 1; | |
2790 for (s = p; s < endp; ++s) | |
2791 if (*s == '\n') | |
2792 ++lnum; | |
2793 return lnum; | |
2794 } | |
2795 #endif | |
2796 | |
7 | 2797 /* |
612 | 2798 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be |
2799 * equal to the buffer "buf". Used for calling readfile(). | |
7 | 2800 * Returns OK or FAIL. |
2801 */ | |
2802 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2803 prep_exarg(exarg_T *eap, buf_T *buf) |
7 | 2804 { |
2805 eap->cmd = alloc((unsigned)(STRLEN(buf->b_p_ff) | |
2806 #ifdef FEAT_MBYTE | |
2807 + STRLEN(buf->b_p_fenc) | |
2808 #endif | |
2809 + 15)); | |
2810 if (eap->cmd == NULL) | |
2811 return FAIL; | |
2812 | |
2813 #ifdef FEAT_MBYTE | |
2814 sprintf((char *)eap->cmd, "e ++ff=%s ++enc=%s", buf->b_p_ff, buf->b_p_fenc); | |
2815 eap->force_enc = 14 + (int)STRLEN(buf->b_p_ff); | |
612 | 2816 eap->bad_char = buf->b_bad_char; |
7 | 2817 #else |
2818 sprintf((char *)eap->cmd, "e ++ff=%s", buf->b_p_ff); | |
2819 #endif | |
2820 eap->force_ff = 7; | |
612 | 2821 |
2822 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN; | |
819 | 2823 eap->read_edit = FALSE; |
612 | 2824 eap->forceit = FALSE; |
7 | 2825 return OK; |
2826 } | |
2827 | |
5231
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 * Set default or forced 'fileformat' and 'binary'. |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2830 */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2831 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2832 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
|
2833 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2834 /* set default 'fileformat' */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2835 if (set_options) |
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 != NULL && eap->force_ff != 0) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2838 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
|
2839 else if (*p_ffs != NUL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2840 set_fileformat(default_fileformat(), OPT_LOCAL); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2841 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2842 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2843 /* set or reset 'binary' */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2844 if (eap != NULL && eap->force_bin != 0) |
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 int oldval = curbuf->b_p_bin; |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2847 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2848 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
|
2849 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
|
2850 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2851 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2852 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2853 #if defined(FEAT_MBYTE) || defined(PROTO) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2854 /* |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2855 * Set forced 'fileencoding'. |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2856 */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2857 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2858 set_forced_fenc(exarg_T *eap) |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2859 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2860 if (eap->force_enc != 0) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2861 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2862 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
|
2863 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2864 if (fenc != NULL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2865 set_string_option_direct((char_u *)"fenc", -1, |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2866 fenc, OPT_FREE|OPT_LOCAL, 0); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2867 vim_free(fenc); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2868 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2869 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2870 |
7 | 2871 /* |
2872 * Find next fileencoding to use from 'fileencodings'. | |
2873 * "pp" points to fenc_next. It's advanced to the next item. | |
2874 * When there are no more items, an empty string is returned and *pp is set to | |
2875 * NULL. | |
2876 * When *pp is not set to NULL, the result is in allocated memory. | |
2877 */ | |
2878 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2879 next_fenc(char_u **pp) |
7 | 2880 { |
2881 char_u *p; | |
2882 char_u *r; | |
2883 | |
2884 if (**pp == NUL) | |
2885 { | |
2886 *pp = NULL; | |
2887 return (char_u *)""; | |
2888 } | |
2889 p = vim_strchr(*pp, ','); | |
2890 if (p == NULL) | |
2891 { | |
2892 r = enc_canonize(*pp); | |
2893 *pp += STRLEN(*pp); | |
2894 } | |
2895 else | |
2896 { | |
2897 r = vim_strnsave(*pp, (int)(p - *pp)); | |
2898 *pp = p + 1; | |
2899 if (r != NULL) | |
2900 { | |
2901 p = enc_canonize(r); | |
2902 vim_free(r); | |
2903 r = p; | |
2904 } | |
2905 } | |
2906 if (r == NULL) /* out of memory */ | |
2907 { | |
2908 r = (char_u *)""; | |
2909 *pp = NULL; | |
2910 } | |
2911 return r; | |
2912 } | |
2913 | |
2914 # ifdef FEAT_EVAL | |
2915 /* | |
2916 * Convert a file with the 'charconvert' expression. | |
2917 * This closes the file which is to be read, converts it and opens the | |
2918 * resulting file for reading. | |
2919 * Returns name of the resulting converted file (the caller should delete it | |
2920 * after reading it). | |
2921 * Returns NULL if the conversion failed ("*fdp" is not set) . | |
2922 */ | |
2923 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2924 readfile_charconvert( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2925 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
|
2926 char_u *fenc, /* converted from */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2927 int *fdp) /* in/out: file descriptor of file */ |
7 | 2928 { |
2929 char_u *tmpname; | |
2930 char_u *errmsg = NULL; | |
2931 | |
6721 | 2932 tmpname = vim_tempname('r', FALSE); |
7 | 2933 if (tmpname == NULL) |
2934 errmsg = (char_u *)_("Can't find temp file for conversion"); | |
2935 else | |
2936 { | |
2937 close(*fdp); /* close the input file, ignore errors */ | |
2938 *fdp = -1; | |
2939 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc, | |
2940 fname, tmpname) == FAIL) | |
2941 errmsg = (char_u *)_("Conversion with 'charconvert' failed"); | |
2942 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname, | |
2943 O_RDONLY | O_EXTRA, 0)) < 0) | |
2944 errmsg = (char_u *)_("can't read output of 'charconvert'"); | |
2945 } | |
2946 | |
2947 if (errmsg != NULL) | |
2948 { | |
2949 /* Don't use emsg(), it breaks mappings, the retry with | |
2950 * another type of conversion might still work. */ | |
2951 MSG(errmsg); | |
2952 if (tmpname != NULL) | |
2953 { | |
2954 mch_remove(tmpname); /* delete converted file */ | |
2955 vim_free(tmpname); | |
2956 tmpname = NULL; | |
2957 } | |
2958 } | |
2959 | |
2960 /* If the input file is closed, open it (caller should check for error). */ | |
2961 if (*fdp < 0) | |
2962 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
2963 | |
2964 return tmpname; | |
2965 } | |
2966 # endif | |
2967 | |
2968 #endif | |
2969 | |
2970 #ifdef FEAT_VIMINFO | |
2971 /* | |
2972 * Read marks for the current buffer from the viminfo file, when we support | |
2973 * buffer marks and the buffer has a name. | |
2974 */ | |
2975 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2976 check_marks_read(void) |
7 | 2977 { |
2978 if (!curbuf->b_marks_read && get_viminfo_parameter('\'') > 0 | |
2979 && curbuf->b_ffname != NULL) | |
1733 | 2980 read_viminfo(NULL, VIF_WANT_MARKS); |
7 | 2981 |
2982 /* Always set b_marks_read; needed when 'viminfo' is changed to include | |
2983 * the ' parameter after opening a buffer. */ | |
2984 curbuf->b_marks_read = TRUE; | |
2985 } | |
2986 #endif | |
2987 | |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2988 #if defined(FEAT_CRYPT) || defined(PROTO) |
7 | 2989 /* |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2990 * Check for magic number used for encryption. Applies to the current buffer. |
7 | 2991 * If found, the magic number is removed from ptr[*sizep] and *sizep and |
2992 * *filesizep are updated. | |
2993 * Return the (new) encryption key, NULL for no encryption. | |
2994 */ | |
2995 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2996 check_for_cryptkey( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2997 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
|
2998 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
|
2999 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
|
3000 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
|
3001 int newfile, /* editing a new buffer */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3002 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
|
3003 int *did_ask) /* flag: whether already asked for key */ |
7 | 3004 { |
6122 | 3005 int method = crypt_method_nr_from_magic((char *)ptr, *sizep); |
5312 | 3006 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
|
3007 |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3008 if (method >= 0) |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3009 { |
5312 | 3010 /* Mark the buffer as read-only until the decryption has taken place. |
3011 * Avoids accidentally overwriting the file with garbage. */ | |
3012 curbuf->b_p_ro = TRUE; | |
3013 | |
6130 | 3014 /* Set the cryptmethod local to the buffer. */ |
6122 | 3015 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
|
3016 if (cryptkey == NULL && !*did_ask) |
7 | 3017 { |
3018 if (*curbuf->b_p_key) | |
3019 cryptkey = curbuf->b_p_key; | |
3020 else | |
3021 { | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3022 /* 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
|
3023 * 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
|
3024 * 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
|
3025 * Happens when retrying to detect encoding. */ |
2267 | 3026 smsg((char_u *)_(need_key_msg), fname); |
3027 msg_scroll = TRUE; | |
6353 | 3028 crypt_check_method(method); |
6122 | 3029 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
|
3030 *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
|
3031 |
7 | 3032 /* check if empty key entered */ |
3033 if (cryptkey != NULL && *cryptkey == NUL) | |
3034 { | |
3035 if (cryptkey != curbuf->b_p_key) | |
3036 vim_free(cryptkey); | |
3037 cryptkey = NULL; | |
3038 } | |
3039 } | |
3040 } | |
3041 | |
3042 if (cryptkey != NULL) | |
3043 { | |
6122 | 3044 int header_len; |
3045 | |
3046 curbuf->b_cryptstate = crypt_create_from_header( | |
3047 method, cryptkey, ptr); | |
3048 crypt_set_cm_option(curbuf, method); | |
3049 | |
3050 /* Remove cryptmethod specific header from the text. */ | |
3051 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
|
3052 if (*sizep <= header_len) |
fb1fde4fcff7
commit https://github.com/vim/vim/commit/680e015bfe19be6772d3bd754486fbd45c1a9d3b
Christian Brabandt <cb@256bit.org>
parents:
10086
diff
changeset
|
3053 /* 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
|
3054 return NULL; |
6122 | 3055 *filesizep += header_len; |
3056 *sizep -= header_len; | |
3057 mch_memmove(ptr, ptr + header_len, (size_t)*sizep); | |
3058 | |
5312 | 3059 /* Restore the read-only flag. */ |
3060 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
|
3061 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3062 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
3063 /* 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
|
3064 * 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
|
3065 else if (newfile && *curbuf->b_p_key != NUL && !starting) |
7 | 3066 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL); |
3067 | |
3068 return cryptkey; | |
3069 } | |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3070 #endif /* FEAT_CRYPT */ |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3071 |
7 | 3072 #ifdef UNIX |
3073 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3074 set_file_time( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3075 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3076 time_t atime, /* access time */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3077 time_t mtime) /* modification time */ |
7 | 3078 { |
3079 # if defined(HAVE_UTIME) && defined(HAVE_UTIME_H) | |
3080 struct utimbuf buf; | |
3081 | |
3082 buf.actime = atime; | |
3083 buf.modtime = mtime; | |
3084 (void)utime((char *)fname, &buf); | |
3085 # else | |
3086 # if defined(HAVE_UTIMES) | |
3087 struct timeval tvp[2]; | |
3088 | |
3089 tvp[0].tv_sec = atime; | |
3090 tvp[0].tv_usec = 0; | |
3091 tvp[1].tv_sec = mtime; | |
3092 tvp[1].tv_usec = 0; | |
3093 # ifdef NeXT | |
3094 (void)utimes((char *)fname, tvp); | |
3095 # else | |
3096 (void)utimes((char *)fname, (const struct timeval *)&tvp); | |
3097 # endif | |
3098 # endif | |
3099 # endif | |
3100 } | |
3101 #endif /* UNIX */ | |
3102 | |
22 | 3103 #if defined(VMS) && !defined(MIN) |
3104 /* Older DECC compiler for VAX doesn't define MIN() */ | |
3105 # define MIN(a, b) ((a) < (b) ? (a) : (b)) | |
3106 #endif | |
3107 | |
7 | 3108 /* |
1303 | 3109 * Return TRUE if a file appears to be read-only from the file permissions. |
3110 */ | |
3111 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3112 check_file_readonly( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3113 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
|
3114 int perm) /* known permissions on file */ |
1303 | 3115 { |
3116 #ifndef USE_MCH_ACCESS | |
3117 int fd = 0; | |
3118 #endif | |
3119 | |
3120 return ( | |
3121 #ifdef USE_MCH_ACCESS | |
3122 # ifdef UNIX | |
3123 (perm & 0222) == 0 || | |
3124 # endif | |
3125 mch_access((char *)fname, W_OK) | |
3126 #else | |
3127 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0 | |
3128 ? TRUE : (close(fd), FALSE) | |
3129 #endif | |
3130 ); | |
3131 } | |
3132 | |
3133 | |
3134 /* | |
589 | 3135 * buf_write() - write to file "fname" lines "start" through "end" |
7 | 3136 * |
3137 * We do our own buffering here because fwrite() is so slow. | |
3138 * | |
589 | 3139 * If "forceit" is true, we don't care for errors when attempting backups. |
3140 * In case of an error everything possible is done to restore the original | |
1698 | 3141 * file. But when "forceit" is TRUE, we risk losing it. |
589 | 3142 * |
3143 * When "reset_changed" is TRUE and "append" == FALSE and "start" == 1 and | |
3144 * "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed. | |
7 | 3145 * |
3146 * This function must NOT use NameBuff (because it's called by autowrite()). | |
3147 * | |
3148 * return FAIL for failure, OK otherwise | |
3149 */ | |
3150 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3151 buf_write( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3152 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3153 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3154 char_u *sfname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3155 linenr_T start, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3156 linenr_T end, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3157 exarg_T *eap, /* for forced 'ff' and 'fenc', can be |
7 | 3158 NULL! */ |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3159 int append, /* append to the file */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3160 int forceit, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3161 int reset_changed, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3162 int filtering) |
7 | 3163 { |
3164 int fd; | |
3165 char_u *backup = NULL; | |
3166 int backup_copy = FALSE; /* copy the original file? */ | |
3167 int dobackup; | |
3168 char_u *ffname; | |
3169 char_u *wfname = NULL; /* name of file to write to */ | |
3170 char_u *s; | |
3171 char_u *ptr; | |
3172 char_u c; | |
3173 int len; | |
3174 linenr_T lnum; | |
3175 long nchars; | |
3176 char_u *errmsg = NULL; | |
1947 | 3177 int errmsg_allocated = FALSE; |
7 | 3178 char_u *errnum = NULL; |
3179 char_u *buffer; | |
3180 char_u smallbuf[SMBUFSIZE]; | |
3181 char_u *backup_ext; | |
3182 int bufsize; | |
3183 long perm; /* file permissions */ | |
3184 int retval = OK; | |
3185 int newfile = FALSE; /* TRUE if file doesn't exist yet */ | |
3186 int msg_save = msg_scroll; | |
3187 int overwriting; /* TRUE if writing over original */ | |
3188 int no_eol = FALSE; /* no end-of-line written */ | |
3189 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
|
3190 stat_T st_old; |
7 | 3191 int prev_got_int = got_int; |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
3192 int checking_conversion; |
7 | 3193 int file_readonly = FALSE; /* overwritten file is read-only */ |
3194 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
|
3195 #if defined(UNIX) /*XXX fix me sometime? */ |
7 | 3196 int made_writable = FALSE; /* 'w' bit has been set */ |
3197 #endif | |
3198 /* writing everything */ | |
3199 int whole = (start == 1 && end == buf->b_ml.ml_line_count); | |
3200 #ifdef FEAT_AUTOCMD | |
3201 linenr_T old_line_count = buf->b_ml.ml_line_count; | |
3202 #endif | |
3203 int attr; | |
3204 int fileformat; | |
3205 int write_bin; | |
3206 struct bw_info write_info; /* info for buf_write_bytes() */ | |
3207 #ifdef FEAT_MBYTE | |
3208 int converted = FALSE; | |
3209 int notconverted = FALSE; | |
3210 char_u *fenc; /* effective 'fileencoding' */ | |
3211 char_u *fenc_tofree = NULL; /* allocated "fenc" */ | |
3212 #endif | |
3213 #ifdef HAS_BW_FLAGS | |
3214 int wb_flags = 0; | |
3215 #endif | |
3216 #ifdef HAVE_ACL | |
3217 vim_acl_T acl = NULL; /* ACL copied from original file to | |
3218 backup or new file */ | |
3219 #endif | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3220 #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
|
3221 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
|
3222 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
|
3223 #endif |
6243 | 3224 unsigned int bkc = get_bkc_value(buf); |
7 | 3225 |
3226 if (fname == NULL || *fname == NUL) /* safety check */ | |
3227 return FAIL; | |
2028 | 3228 if (buf->b_ml.ml_mfp == NULL) |
3229 { | |
3230 /* This can happen during startup when there is a stray "w" in the | |
3231 * vimrc file. */ | |
3232 EMSG(_(e_emptybuf)); | |
3233 return FAIL; | |
3234 } | |
7 | 3235 |
3236 /* | |
3237 * Disallow writing from .exrc and .vimrc in current directory for | |
3238 * security reasons. | |
3239 */ | |
3240 if (check_secure()) | |
3241 return FAIL; | |
3242 | |
3243 /* Avoid a crash for a long name. */ | |
3244 if (STRLEN(fname) >= MAXPATHL) | |
3245 { | |
3246 EMSG(_(e_longname)); | |
3247 return FAIL; | |
3248 } | |
3249 | |
3250 #ifdef FEAT_MBYTE | |
3251 /* must init bw_conv_buf and bw_iconv_fd before jumping to "fail" */ | |
3252 write_info.bw_conv_buf = NULL; | |
3253 write_info.bw_conv_error = FALSE; | |
1947 | 3254 write_info.bw_conv_error_lnum = 0; |
7 | 3255 write_info.bw_restlen = 0; |
3256 # ifdef USE_ICONV | |
3257 write_info.bw_iconv_fd = (iconv_t)-1; | |
3258 # endif | |
3259 #endif | |
6122 | 3260 #ifdef FEAT_CRYPT |
3261 write_info.bw_buffer = buf; | |
3262 #endif | |
7 | 3263 |
167 | 3264 /* After writing a file changedtick changes but we don't want to display |
3265 * the line. */ | |
3266 ex_no_reprint = TRUE; | |
3267 | |
7 | 3268 /* |
3269 * If there is no file name yet, use the one for the written file. | |
3270 * BF_NOTEDITED is set to reflect this (in case the write fails). | |
3271 * Don't do this when the write is for a filter command. | |
589 | 3272 * Don't do this when appending. |
3273 * Only do this when 'cpoptions' contains the 'F' flag. | |
7 | 3274 */ |
633 | 3275 if (buf->b_ffname == NULL |
3276 && reset_changed | |
7 | 3277 && whole |
3278 && buf == curbuf | |
236 | 3279 #ifdef FEAT_QUICKFIX |
3280 && !bt_nofile(buf) | |
3281 #endif | |
7 | 3282 && !filtering |
589 | 3283 && (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL) |
7 | 3284 && vim_strchr(p_cpo, CPO_FNAMEW) != NULL) |
3285 { | |
633 | 3286 if (set_rw_fname(fname, sfname) == FAIL) |
7 | 3287 return FAIL; |
633 | 3288 buf = curbuf; /* just in case autocmds made "buf" invalid */ |
7 | 3289 } |
3290 | |
3291 if (sfname == NULL) | |
3292 sfname = fname; | |
3293 /* | |
3294 * For Unix: Use the short file name whenever possible. | |
3295 * Avoids problems with networks and when directory names are changed. | |
3296 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to | |
3297 * another directory, which we don't detect | |
3298 */ | |
3299 ffname = fname; /* remember full fname */ | |
3300 #ifdef UNIX | |
3301 fname = sfname; | |
3302 #endif | |
3303 | |
3304 if (buf->b_ffname != NULL && fnamecmp(ffname, buf->b_ffname) == 0) | |
3305 overwriting = TRUE; | |
3306 else | |
3307 overwriting = FALSE; | |
3308 | |
3309 if (exiting) | |
4352 | 3310 settmode(TMODE_COOK); /* when exiting allow typeahead now */ |
7 | 3311 |
3312 ++no_wait_return; /* don't wait for return yet */ | |
3313 | |
3314 /* | |
3315 * Set '[ and '] marks to the lines to be written. | |
3316 */ | |
3317 buf->b_op_start.lnum = start; | |
3318 buf->b_op_start.col = 0; | |
3319 buf->b_op_end.lnum = end; | |
3320 buf->b_op_end.col = 0; | |
3321 | |
3322 #ifdef FEAT_AUTOCMD | |
3323 { | |
3324 aco_save_T aco; | |
3325 int buf_ffname = FALSE; | |
3326 int buf_sfname = FALSE; | |
3327 int buf_fname_f = FALSE; | |
3328 int buf_fname_s = FALSE; | |
3329 int did_cmd = FALSE; | |
17 | 3330 int nofile_err = FALSE; |
161 | 3331 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
|
3332 bufref_T bufref; |
7 | 3333 |
3334 /* | |
4352 | 3335 * Apply PRE autocommands. |
7 | 3336 * Set curbuf to the buffer to be written. |
3337 * Careful: The autocommands may call buf_write() recursively! | |
3338 */ | |
3339 if (ffname == buf->b_ffname) | |
3340 buf_ffname = TRUE; | |
3341 if (sfname == buf->b_sfname) | |
3342 buf_sfname = TRUE; | |
3343 if (fname == buf->b_ffname) | |
3344 buf_fname_f = TRUE; | |
3345 if (fname == buf->b_sfname) | |
3346 buf_fname_s = TRUE; | |
3347 | |
3348 /* set curwin/curbuf to buf and save a few things */ | |
3349 aucmd_prepbuf(&aco, buf); | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
3350 set_bufref(&bufref, buf); |
7 | 3351 |
3352 if (append) | |
3353 { | |
3354 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD, | |
3355 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3356 { |
635 | 3357 #ifdef FEAT_QUICKFIX |
18 | 3358 if (overwriting && bt_nofile(curbuf)) |
17 | 3359 nofile_err = TRUE; |
3360 else | |
635 | 3361 #endif |
17 | 3362 apply_autocmds_exarg(EVENT_FILEAPPENDPRE, |
7 | 3363 sfname, sfname, FALSE, curbuf, eap); |
17 | 3364 } |
7 | 3365 } |
3366 else if (filtering) | |
3367 { | |
3368 apply_autocmds_exarg(EVENT_FILTERWRITEPRE, | |
3369 NULL, sfname, FALSE, curbuf, eap); | |
3370 } | |
3371 else if (reset_changed && whole) | |
3372 { | |
3036 | 3373 int was_changed = curbufIsChanged(); |
3374 | |
3375 did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD, | |
3376 sfname, sfname, FALSE, curbuf, eap); | |
3377 if (did_cmd) | |
3378 { | |
3379 if (was_changed && !curbufIsChanged()) | |
3380 { | |
3381 /* Written everything correctly and BufWriteCmd has reset | |
3382 * 'modified': Correct the undo information so that an | |
3383 * undo now sets 'modified'. */ | |
3384 u_unchanged(curbuf); | |
3385 u_update_save_nr(curbuf); | |
3386 } | |
3387 } | |
3388 else | |
17 | 3389 { |
635 | 3390 #ifdef FEAT_QUICKFIX |
179 | 3391 if (overwriting && bt_nofile(curbuf)) |
17 | 3392 nofile_err = TRUE; |
3393 else | |
635 | 3394 #endif |
17 | 3395 apply_autocmds_exarg(EVENT_BUFWRITEPRE, |
7 | 3396 sfname, sfname, FALSE, curbuf, eap); |
17 | 3397 } |
7 | 3398 } |
3399 else | |
3400 { | |
3401 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD, | |
3402 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3403 { |
635 | 3404 #ifdef FEAT_QUICKFIX |
179 | 3405 if (overwriting && bt_nofile(curbuf)) |
17 | 3406 nofile_err = TRUE; |
3407 else | |
635 | 3408 #endif |
17 | 3409 apply_autocmds_exarg(EVENT_FILEWRITEPRE, |
7 | 3410 sfname, sfname, FALSE, curbuf, eap); |
17 | 3411 } |
7 | 3412 } |
3413 | |
3414 /* restore curwin/curbuf and a few other things */ | |
3415 aucmd_restbuf(&aco); | |
3416 | |
3417 /* | |
3418 * In three situations we return here and don't write the file: | |
3419 * 1. the autocommands deleted or unloaded the buffer. | |
3420 * 2. The autocommands abort script processing. | |
3421 * 3. If one of the "Cmd" autocommands was executed. | |
3422 */ | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
3423 if (!bufref_valid(&bufref)) |
7 | 3424 buf = NULL; |
161 | 3425 if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline) |
532 | 3426 || did_cmd || nofile_err |
3427 #ifdef FEAT_EVAL | |
3428 || aborting() | |
3429 #endif | |
3430 ) | |
7 | 3431 { |
3432 --no_wait_return; | |
3433 msg_scroll = msg_save; | |
17 | 3434 if (nofile_err) |
3435 EMSG(_("E676: No matching autocommands for acwrite buffer")); | |
3436 | |
532 | 3437 if (nofile_err |
3438 #ifdef FEAT_EVAL | |
3439 || aborting() | |
3440 #endif | |
3441 ) | |
7 | 3442 /* An aborting error, interrupt or exception in the |
3443 * autocommands. */ | |
3444 return FAIL; | |
3445 if (did_cmd) | |
3446 { | |
3447 if (buf == NULL) | |
3448 /* The buffer was deleted. We assume it was written | |
3449 * (can't retry anyway). */ | |
3450 return OK; | |
3451 if (overwriting) | |
3452 { | |
3453 /* Assume the buffer was written, update the timestamp. */ | |
3454 ml_timestamp(buf); | |
589 | 3455 if (append) |
3456 buf->b_flags &= ~BF_NEW; | |
3457 else | |
3458 buf->b_flags &= ~BF_WRITE_MASK; | |
7 | 3459 } |
589 | 3460 if (reset_changed && buf->b_changed && !append |
39 | 3461 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)) |
7 | 3462 /* Buffer still changed, the autocommands didn't work |
3463 * properly. */ | |
3464 return FAIL; | |
3465 return OK; | |
3466 } | |
3467 #ifdef FEAT_EVAL | |
3468 if (!aborting()) | |
3469 #endif | |
3470 EMSG(_("E203: Autocommands deleted or unloaded buffer to be written")); | |
3471 return FAIL; | |
3472 } | |
3473 | |
3474 /* | |
3475 * The autocommands may have changed the number of lines in the file. | |
3476 * When writing the whole file, adjust the end. | |
3477 * When writing part of the file, assume that the autocommands only | |
3478 * changed the number of lines that are to be written (tricky!). | |
3479 */ | |
3480 if (buf->b_ml.ml_line_count != old_line_count) | |
3481 { | |
3482 if (whole) /* write all */ | |
3483 end = buf->b_ml.ml_line_count; | |
3484 else if (buf->b_ml.ml_line_count > old_line_count) /* more lines */ | |
3485 end += buf->b_ml.ml_line_count - old_line_count; | |
3486 else /* less lines */ | |
3487 { | |
3488 end -= old_line_count - buf->b_ml.ml_line_count; | |
3489 if (end < start) | |
3490 { | |
3491 --no_wait_return; | |
3492 msg_scroll = msg_save; | |
3493 EMSG(_("E204: Autocommand changed number of lines in unexpected way")); | |
3494 return FAIL; | |
3495 } | |
3496 } | |
3497 } | |
3498 | |
3499 /* | |
3500 * The autocommands may have changed the name of the buffer, which may | |
3501 * be kept in fname, ffname and sfname. | |
3502 */ | |
3503 if (buf_ffname) | |
3504 ffname = buf->b_ffname; | |
3505 if (buf_sfname) | |
3506 sfname = buf->b_sfname; | |
3507 if (buf_fname_f) | |
3508 fname = buf->b_ffname; | |
3509 if (buf_fname_s) | |
3510 fname = buf->b_sfname; | |
3511 } | |
3512 #endif | |
3513 | |
3514 #ifdef FEAT_NETBEANS_INTG | |
2210 | 3515 if (netbeans_active() && isNetbeansBuffer(buf)) |
7 | 3516 { |
3517 if (whole) | |
3518 { | |
3519 /* | |
3520 * b_changed can be 0 after an undo, but we still need to write | |
3521 * the buffer to NetBeans. | |
3522 */ | |
3523 if (buf->b_changed || isNetbeansModified(buf)) | |
3524 { | |
33 | 3525 --no_wait_return; /* may wait for return now */ |
3526 msg_scroll = msg_save; | |
3527 netbeans_save_buffer(buf); /* no error checking... */ | |
7 | 3528 return retval; |
3529 } | |
3530 else | |
3531 { | |
3532 errnum = (char_u *)"E656: "; | |
1653 | 3533 errmsg = (char_u *)_("NetBeans disallows writes of unmodified buffers"); |
7 | 3534 buffer = NULL; |
3535 goto fail; | |
3536 } | |
3537 } | |
3538 else | |
3539 { | |
3540 errnum = (char_u *)"E657: "; | |
3541 errmsg = (char_u *)_("Partial writes disallowed for NetBeans buffers"); | |
3542 buffer = NULL; | |
3543 goto fail; | |
3544 } | |
3545 } | |
3546 #endif | |
3547 | |
3548 if (shortmess(SHM_OVER) && !exiting) | |
3549 msg_scroll = FALSE; /* overwrite previous file message */ | |
3550 else | |
3551 msg_scroll = TRUE; /* don't overwrite previous file message */ | |
3552 if (!filtering) | |
3553 filemess(buf, | |
3554 #ifndef UNIX | |
3555 sfname, | |
3556 #else | |
3557 fname, | |
3558 #endif | |
3559 (char_u *)"", 0); /* show that we are busy */ | |
3560 msg_scroll = FALSE; /* always overwrite the file message now */ | |
3561 | |
3562 buffer = alloc(BUFSIZE); | |
3563 if (buffer == NULL) /* can't allocate big buffer, use small | |
3564 * one (to be able to write when out of | |
3565 * memory) */ | |
3566 { | |
3567 buffer = smallbuf; | |
3568 bufsize = SMBUFSIZE; | |
3569 } | |
3570 else | |
3571 bufsize = BUFSIZE; | |
3572 | |
3573 /* | |
3574 * Get information about original file (if there is one). | |
3575 */ | |
7410
08e62c4fc17d
commit https://github.com/vim/vim/commit/53076830fea6df737455523f7e235bfe4f79864d
Christian Brabandt <cb@256bit.org>
parents:
7408
diff
changeset
|
3576 #if defined(UNIX) |
1438 | 3577 st_old.st_dev = 0; |
3578 st_old.st_ino = 0; | |
7 | 3579 perm = -1; |
3580 if (mch_stat((char *)fname, &st_old) < 0) | |
3581 newfile = TRUE; | |
3582 else | |
3583 { | |
3584 perm = st_old.st_mode; | |
3585 if (!S_ISREG(st_old.st_mode)) /* not a file */ | |
3586 { | |
3587 if (S_ISDIR(st_old.st_mode)) | |
3588 { | |
3589 errnum = (char_u *)"E502: "; | |
3590 errmsg = (char_u *)_("is a directory"); | |
3591 goto fail; | |
3592 } | |
3593 if (mch_nodetype(fname) != NODE_WRITABLE) | |
3594 { | |
3595 errnum = (char_u *)"E503: "; | |
3596 errmsg = (char_u *)_("is not a file or writable device"); | |
3597 goto fail; | |
3598 } | |
3599 /* It's a device of some kind (or a fifo) which we can write to | |
3600 * but for which we can't make a backup. */ | |
3601 device = TRUE; | |
3602 newfile = TRUE; | |
3603 perm = -1; | |
3604 } | |
3605 } | |
3606 #else /* !UNIX */ | |
3607 /* | |
3608 * Check for a writable device name. | |
3609 */ | |
3610 c = mch_nodetype(fname); | |
3611 if (c == NODE_OTHER) | |
3612 { | |
3613 errnum = (char_u *)"E503: "; | |
3614 errmsg = (char_u *)_("is not a file or writable device"); | |
3615 goto fail; | |
3616 } | |
3617 if (c == NODE_WRITABLE) | |
3618 { | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3619 # if defined(MSWIN) |
1004 | 3620 /* MS-Windows allows opening a device, but we will probably get stuck |
3621 * trying to write to it. */ | |
3622 if (!p_odev) | |
3623 { | |
3624 errnum = (char_u *)"E796: "; | |
3625 errmsg = (char_u *)_("writing to device disabled with 'opendevice' option"); | |
3626 goto fail; | |
3627 } | |
3628 # endif | |
7 | 3629 device = TRUE; |
3630 newfile = TRUE; | |
3631 perm = -1; | |
3632 } | |
3633 else | |
3634 { | |
3635 perm = mch_getperm(fname); | |
3636 if (perm < 0) | |
3637 newfile = TRUE; | |
3638 else if (mch_isdir(fname)) | |
3639 { | |
3640 errnum = (char_u *)"E502: "; | |
3641 errmsg = (char_u *)_("is a directory"); | |
3642 goto fail; | |
3643 } | |
3644 if (overwriting) | |
3645 (void)mch_stat((char *)fname, &st_old); | |
3646 } | |
3647 #endif /* !UNIX */ | |
3648 | |
3649 if (!device && !newfile) | |
3650 { | |
3651 /* | |
3652 * Check if the file is really writable (when renaming the file to | |
3653 * make a backup we won't discover it later). | |
3654 */ | |
1303 | 3655 file_readonly = check_file_readonly(fname, (int)perm); |
3656 | |
7 | 3657 if (!forceit && file_readonly) |
3658 { | |
3659 if (vim_strchr(p_cpo, CPO_FWRITE) != NULL) | |
3660 { | |
3661 errnum = (char_u *)"E504: "; | |
3662 errmsg = (char_u *)_(err_readonly); | |
3663 } | |
3664 else | |
3665 { | |
3666 errnum = (char_u *)"E505: "; | |
3667 errmsg = (char_u *)_("is read-only (add ! to override)"); | |
3668 } | |
3669 goto fail; | |
3670 } | |
3671 | |
3672 /* | |
3673 * Check if the timestamp hasn't changed since reading the file. | |
3674 */ | |
3675 if (overwriting) | |
3676 { | |
3677 retval = check_mtime(buf, &st_old); | |
3678 if (retval == FAIL) | |
3679 goto fail; | |
3680 } | |
3681 } | |
3682 | |
3683 #ifdef HAVE_ACL | |
3684 /* | |
3685 * For systems that support ACL: get the ACL from the original file. | |
3686 */ | |
3687 if (!newfile) | |
3688 acl = mch_get_acl(fname); | |
3689 #endif | |
3690 | |
3691 /* | |
3692 * If 'backupskip' is not empty, don't make a backup for some files. | |
3693 */ | |
3694 dobackup = (p_wb || p_bk || *p_pm != NUL); | |
3695 #ifdef FEAT_WILDIGN | |
3696 if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname)) | |
3697 dobackup = FALSE; | |
3698 #endif | |
3699 | |
3700 /* | |
3701 * Save the value of got_int and reset it. We don't want a previous | |
3702 * interruption cancel writing, only hitting CTRL-C while writing should | |
3703 * abort it. | |
3704 */ | |
3705 prev_got_int = got_int; | |
3706 got_int = FALSE; | |
3707 | |
3708 /* Mark the buffer as 'being saved' to prevent changed buffer warnings */ | |
3709 buf->b_saving = TRUE; | |
3710 | |
3711 /* | |
3712 * If we are not appending or filtering, the file exists, and the | |
3713 * 'writebackup', 'backup' or 'patchmode' option is set, need a backup. | |
3714 * When 'patchmode' is set also make a backup when appending. | |
3715 * | |
3716 * Do not make any backup, if 'writebackup' and 'backup' are both switched | |
3717 * off. This helps when editing large files on almost-full disks. | |
3718 */ | |
3719 if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup) | |
3720 { | |
3721 #if defined(UNIX) || defined(WIN32) | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
3722 stat_T st; |
7 | 3723 #endif |
3724 | |
6243 | 3725 if ((bkc & BKC_YES) || append) /* "yes" */ |
7 | 3726 backup_copy = TRUE; |
3727 #if defined(UNIX) || defined(WIN32) | |
6243 | 3728 else if ((bkc & BKC_AUTO)) /* "auto" */ |
7 | 3729 { |
3730 int i; | |
3731 | |
3732 # ifdef UNIX | |
3733 /* | |
3734 * Don't rename the file when: | |
3735 * - it's a hard link | |
3736 * - it's a symbolic link | |
3737 * - we don't have write permission in the directory | |
3738 * - we can't set the owner/group of the new file | |
3739 */ | |
3740 if (st_old.st_nlink > 1 | |
3741 || mch_lstat((char *)fname, &st) < 0 | |
3742 || st.st_dev != st_old.st_dev | |
557 | 3743 || st.st_ino != st_old.st_ino |
3744 # ifndef HAVE_FCHOWN | |
3745 || st.st_uid != st_old.st_uid | |
3746 || st.st_gid != st_old.st_gid | |
3747 # endif | |
3748 ) | |
7 | 3749 backup_copy = TRUE; |
3750 else | |
696 | 3751 # else |
3752 # ifdef WIN32 | |
3753 /* On NTFS file systems hard links are possible. */ | |
3754 if (mch_is_linked(fname)) | |
3755 backup_copy = TRUE; | |
3756 else | |
3757 # endif | |
7 | 3758 # endif |
3759 { | |
3760 /* | |
3761 * Check if we can create a file and set the owner/group to | |
3762 * the ones from the original file. | |
3763 * First find a file name that doesn't exist yet (use some | |
3764 * arbitrary numbers). | |
3765 */ | |
3766 STRCPY(IObuff, fname); | |
3767 for (i = 4913; ; i += 123) | |
3768 { | |
3769 sprintf((char *)gettail(IObuff), "%d", i); | |
557 | 3770 if (mch_lstat((char *)IObuff, &st) < 0) |
7 | 3771 break; |
3772 } | |
557 | 3773 fd = mch_open((char *)IObuff, |
3774 O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm); | |
7 | 3775 if (fd < 0) /* can't write in directory */ |
3776 backup_copy = TRUE; | |
3777 else | |
3778 { | |
3779 # ifdef UNIX | |
557 | 3780 # ifdef HAVE_FCHOWN |
1757 | 3781 ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
557 | 3782 # endif |
7 | 3783 if (mch_stat((char *)IObuff, &st) < 0 |
3784 || st.st_uid != st_old.st_uid | |
3785 || st.st_gid != st_old.st_gid | |
1877 | 3786 || (long)st.st_mode != perm) |
7 | 3787 backup_copy = TRUE; |
3788 # endif | |
567 | 3789 /* Close the file before removing it, on MS-Windows we |
3790 * can't delete an open file. */ | |
3791 close(fd); | |
7 | 3792 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
|
3793 # 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
|
3794 /* 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
|
3795 * 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
|
3796 * second. */ |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3797 { |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3798 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
|
3799 |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3800 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
|
3801 { |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3802 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
|
3803 break; |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3804 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
|
3805 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
|
3806 } |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3807 } |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3808 # endif |
7 | 3809 } |
3810 } | |
3811 } | |
3812 | |
3813 /* | |
3814 * Break symlinks and/or hardlinks if we've been asked to. | |
3815 */ | |
6243 | 3816 if ((bkc & BKC_BREAKSYMLINK) || (bkc & BKC_BREAKHARDLINK)) |
7 | 3817 { |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3818 # ifdef UNIX |
7 | 3819 int lstat_res; |
3820 | |
3821 lstat_res = mch_lstat((char *)fname, &st); | |
3822 | |
3823 /* Symlinks. */ | |
6243 | 3824 if ((bkc & BKC_BREAKSYMLINK) |
7 | 3825 && lstat_res == 0 |
3826 && st.st_ino != st_old.st_ino) | |
3827 backup_copy = FALSE; | |
3828 | |
3829 /* Hardlinks. */ | |
6243 | 3830 if ((bkc & BKC_BREAKHARDLINK) |
7 | 3831 && st_old.st_nlink > 1 |
3832 && (lstat_res != 0 || st.st_ino == st_old.st_ino)) | |
3833 backup_copy = FALSE; | |
4872
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3834 # else |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3835 # if defined(WIN32) |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3836 /* Symlinks. */ |
6243 | 3837 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
|
3838 backup_copy = FALSE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3839 |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3840 /* Hardlinks. */ |
6243 | 3841 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
|
3842 backup_copy = FALSE; |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3843 # endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3844 # endif |
fa98c2b030ed
updated for version 7.3.1182
Bram Moolenaar <bram@vim.org>
parents:
4861
diff
changeset
|
3845 } |
7 | 3846 |
3847 #endif | |
3848 | |
3849 /* make sure we have a valid backup extension to use */ | |
3850 if (*p_bex == NUL) | |
3851 backup_ext = (char_u *)".bak"; | |
3852 else | |
3853 backup_ext = p_bex; | |
3854 | |
3855 if (backup_copy | |
3856 && (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) >= 0) | |
3857 { | |
3858 int bfd; | |
3859 char_u *copybuf, *wp; | |
3860 int some_error = FALSE; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
3861 stat_T st_new; |
7 | 3862 char_u *dirp; |
3863 char_u *rootname; | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3864 #if defined(UNIX) |
7 | 3865 int did_set_shortname; |
3866 #endif | |
3867 | |
3868 copybuf = alloc(BUFSIZE + 1); | |
3869 if (copybuf == NULL) | |
3870 { | |
3871 some_error = TRUE; /* out of memory */ | |
3872 goto nobackup; | |
3873 } | |
3874 | |
3875 /* | |
3876 * Try to make the backup in each directory in the 'bdir' option. | |
3877 * | |
3878 * Unix semantics has it, that we may have a writable file, | |
3879 * that cannot be recreated with a simple open(..., O_CREAT, ) e.g: | |
3880 * - the directory is not writable, | |
3881 * - the file may be a symbolic link, | |
3882 * - the file may belong to another user/group, etc. | |
3883 * | |
3884 * For these reasons, the existing writable file must be truncated | |
3885 * and reused. Creation of a backup COPY will be attempted. | |
3886 */ | |
3887 dirp = p_bdir; | |
3888 while (*dirp) | |
3889 { | |
3890 #ifdef UNIX | |
3891 st_new.st_ino = 0; | |
3892 st_new.st_dev = 0; | |
3893 st_new.st_gid = 0; | |
3894 #endif | |
3895 | |
3896 /* | |
3897 * Isolate one directory name, using an entry in 'bdir'. | |
3898 */ | |
3899 (void)copy_option_part(&dirp, copybuf, BUFSIZE, ","); | |
3900 rootname = get_file_in_dir(fname, copybuf); | |
3901 if (rootname == NULL) | |
3902 { | |
3903 some_error = TRUE; /* out of memory */ | |
3904 goto nobackup; | |
3905 } | |
3906 | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3907 #if defined(UNIX) |
7 | 3908 did_set_shortname = FALSE; |
3909 #endif | |
3910 | |
3911 /* | |
3912 * May try twice if 'shortname' not set. | |
3913 */ | |
3914 for (;;) | |
3915 { | |
3916 /* | |
3917 * Make backup file name. | |
3918 */ | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3919 backup = buf_modname((buf->b_p_sn || buf->b_shortname), |
7 | 3920 rootname, backup_ext, FALSE); |
3921 if (backup == NULL) | |
3922 { | |
3923 vim_free(rootname); | |
3924 some_error = TRUE; /* out of memory */ | |
3925 goto nobackup; | |
3926 } | |
3927 | |
3928 /* | |
3929 * Check if backup file already exists. | |
3930 */ | |
3931 if (mch_stat((char *)backup, &st_new) >= 0) | |
3932 { | |
3933 #ifdef UNIX | |
3934 /* | |
3935 * Check if backup file is same as original file. | |
3936 * May happen when modname() gave the same file back. | |
3937 * E.g. silly link, or file name-length reached. | |
3938 * If we don't check here, we either ruin the file | |
3939 * when copying or erase it after writing. jw. | |
3940 */ | |
3941 if (st_new.st_dev == st_old.st_dev | |
3942 && st_new.st_ino == st_old.st_ino) | |
3943 { | |
3944 vim_free(backup); | |
3945 backup = NULL; /* no backup file to delete */ | |
3946 /* | |
3947 * may try again with 'shortname' set | |
3948 */ | |
3949 if (!(buf->b_shortname || buf->b_p_sn)) | |
3950 { | |
3951 buf->b_shortname = TRUE; | |
3952 did_set_shortname = TRUE; | |
3953 continue; | |
3954 } | |
3955 /* setting shortname didn't help */ | |
3956 if (did_set_shortname) | |
3957 buf->b_shortname = FALSE; | |
3958 break; | |
3959 } | |
3960 #endif | |
3961 | |
3962 /* | |
3963 * If we are not going to keep the backup file, don't | |
3964 * delete an existing one, try to use another name. | |
3965 * Change one character, just before the extension. | |
3966 */ | |
3967 if (!p_bk) | |
3968 { | |
3969 wp = backup + STRLEN(backup) - 1 | |
3970 - STRLEN(backup_ext); | |
3971 if (wp < backup) /* empty file name ??? */ | |
3972 wp = backup; | |
3973 *wp = 'z'; | |
3974 while (*wp > 'a' | |
3975 && mch_stat((char *)backup, &st_new) >= 0) | |
3976 --*wp; | |
3977 /* They all exist??? Must be something wrong. */ | |
3978 if (*wp == 'a') | |
3979 { | |
3980 vim_free(backup); | |
3981 backup = NULL; | |
3982 } | |
3983 } | |
3984 } | |
3985 break; | |
3986 } | |
3987 vim_free(rootname); | |
3988 | |
3989 /* | |
3990 * Try to create the backup file | |
3991 */ | |
3992 if (backup != NULL) | |
3993 { | |
3994 /* remove old backup, if present */ | |
3995 mch_remove(backup); | |
3996 /* Open with O_EXCL to avoid the file being created while | |
3997 * we were sleeping (symlink hacker attack?) */ | |
3998 bfd = mch_open((char *)backup, | |
557 | 3999 O_WRONLY|O_CREAT|O_EXTRA|O_EXCL|O_NOFOLLOW, |
4000 perm & 0777); | |
7 | 4001 if (bfd < 0) |
4002 { | |
4003 vim_free(backup); | |
4004 backup = NULL; | |
4005 } | |
4006 else | |
4007 { | |
4008 /* set file protection same as original file, but | |
4009 * strip s-bit */ | |
4010 (void)mch_setperm(backup, perm & 0777); | |
4011 | |
4012 #ifdef UNIX | |
4013 /* | |
4014 * Try to set the group of the backup same as the | |
4015 * original file. If this fails, set the protection | |
4016 * bits for the group same as the protection bits for | |
4017 * others. | |
4018 */ | |
557 | 4019 if (st_new.st_gid != st_old.st_gid |
7 | 4020 # ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */ |
557 | 4021 && fchown(bfd, (uid_t)-1, st_old.st_gid) != 0 |
7 | 4022 # endif |
4023 ) | |
4024 mch_setperm(backup, | |
4025 (perm & 0707) | ((perm & 07) << 3)); | |
5788 | 4026 # if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
1583 | 4027 mch_copy_sec(fname, backup); |
4028 # endif | |
7 | 4029 #endif |
4030 | |
4031 /* | |
4032 * copy the file. | |
4033 */ | |
4034 write_info.bw_fd = bfd; | |
4035 write_info.bw_buf = copybuf; | |
4036 #ifdef HAS_BW_FLAGS | |
4037 write_info.bw_flags = FIO_NOCONVERT; | |
4038 #endif | |
2664 | 4039 while ((write_info.bw_len = read_eintr(fd, copybuf, |
7 | 4040 BUFSIZE)) > 0) |
4041 { | |
4042 if (buf_write_bytes(&write_info) == FAIL) | |
4043 { | |
4044 errmsg = (char_u *)_("E506: Can't write to backup file (add ! to override)"); | |
4045 break; | |
4046 } | |
4047 ui_breakcheck(); | |
4048 if (got_int) | |
4049 { | |
4050 errmsg = (char_u *)_(e_interr); | |
4051 break; | |
4052 } | |
4053 } | |
4054 | |
4055 if (close(bfd) < 0 && errmsg == NULL) | |
4056 errmsg = (char_u *)_("E507: Close error for backup file (add ! to override)"); | |
4057 if (write_info.bw_len < 0) | |
4058 errmsg = (char_u *)_("E508: Can't read file for backup (add ! to override)"); | |
4059 #ifdef UNIX | |
4060 set_file_time(backup, st_old.st_atime, st_old.st_mtime); | |
4061 #endif | |
4062 #ifdef HAVE_ACL | |
4063 mch_set_acl(backup, acl); | |
4064 #endif | |
5788 | 4065 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
1583 | 4066 mch_copy_sec(fname, backup); |
4067 #endif | |
7 | 4068 break; |
4069 } | |
4070 } | |
4071 } | |
4072 nobackup: | |
4073 close(fd); /* ignore errors for closing read file */ | |
4074 vim_free(copybuf); | |
4075 | |
4076 if (backup == NULL && errmsg == NULL) | |
4077 errmsg = (char_u *)_("E509: Cannot create backup file (add ! to override)"); | |
4078 /* ignore errors when forceit is TRUE */ | |
4079 if ((some_error || errmsg != NULL) && !forceit) | |
4080 { | |
4081 retval = FAIL; | |
4082 goto fail; | |
4083 } | |
4084 errmsg = NULL; | |
4085 } | |
4086 else | |
4087 { | |
4088 char_u *dirp; | |
4089 char_u *p; | |
4090 char_u *rootname; | |
4091 | |
4092 /* | |
4093 * Make a backup by renaming the original file. | |
4094 */ | |
4095 /* | |
4096 * If 'cpoptions' includes the "W" flag, we don't want to | |
4097 * overwrite a read-only file. But rename may be possible | |
4098 * anyway, thus we need an extra check here. | |
4099 */ | |
4100 if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL) | |
4101 { | |
4102 errnum = (char_u *)"E504: "; | |
4103 errmsg = (char_u *)_(err_readonly); | |
4104 goto fail; | |
4105 } | |
4106 | |
4107 /* | |
4108 * | |
4109 * Form the backup file name - change path/fo.o.h to | |
4110 * path/fo.o.h.bak Try all directories in 'backupdir', first one | |
4111 * that works is used. | |
4112 */ | |
4113 dirp = p_bdir; | |
4114 while (*dirp) | |
4115 { | |
4116 /* | |
4117 * Isolate one directory name and make the backup file name. | |
4118 */ | |
4119 (void)copy_option_part(&dirp, IObuff, IOSIZE, ","); | |
4120 rootname = get_file_in_dir(fname, IObuff); | |
4121 if (rootname == NULL) | |
4122 backup = NULL; | |
4123 else | |
4124 { | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
4125 backup = buf_modname((buf->b_p_sn || buf->b_shortname), |
7 | 4126 rootname, backup_ext, FALSE); |
4127 vim_free(rootname); | |
4128 } | |
4129 | |
4130 if (backup != NULL) | |
4131 { | |
4132 /* | |
4133 * If we are not going to keep the backup file, don't | |
4134 * delete an existing one, try to use another name. | |
4135 * Change one character, just before the extension. | |
4136 */ | |
4137 if (!p_bk && mch_getperm(backup) >= 0) | |
4138 { | |
4139 p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext); | |
4140 if (p < backup) /* empty file name ??? */ | |
4141 p = backup; | |
4142 *p = 'z'; | |
4143 while (*p > 'a' && mch_getperm(backup) >= 0) | |
4144 --*p; | |
4145 /* They all exist??? Must be something wrong! */ | |
4146 if (*p == 'a') | |
4147 { | |
4148 vim_free(backup); | |
4149 backup = NULL; | |
4150 } | |
4151 } | |
4152 } | |
4153 if (backup != NULL) | |
4154 { | |
4155 /* | |
531 | 4156 * Delete any existing backup and move the current version |
4157 * to the backup. For safety, we don't remove the backup | |
4158 * until the write has finished successfully. And if the | |
4159 * 'backup' option is set, leave it around. | |
7 | 4160 */ |
4161 /* | |
4162 * If the renaming of the original file to the backup file | |
4163 * works, quit here. | |
4164 */ | |
4165 if (vim_rename(fname, backup) == 0) | |
4166 break; | |
4167 | |
4168 vim_free(backup); /* don't do the rename below */ | |
4169 backup = NULL; | |
4170 } | |
4171 } | |
4172 if (backup == NULL && !forceit) | |
4173 { | |
4174 errmsg = (char_u *)_("E510: Can't make backup file (add ! to override)"); | |
4175 goto fail; | |
4176 } | |
4177 } | |
4178 } | |
4179 | |
7410
08e62c4fc17d
commit https://github.com/vim/vim/commit/53076830fea6df737455523f7e235bfe4f79864d
Christian Brabandt <cb@256bit.org>
parents:
7408
diff
changeset
|
4180 #if defined(UNIX) |
7 | 4181 /* When using ":w!" and the file was read-only: make it writable */ |
4182 if (forceit && perm >= 0 && !(perm & 0200) && st_old.st_uid == getuid() | |
4183 && vim_strchr(p_cpo, CPO_FWRITE) == NULL) | |
4184 { | |
4185 perm |= 0200; | |
4186 (void)mch_setperm(fname, perm); | |
4187 made_writable = TRUE; | |
4188 } | |
4189 #endif | |
4190 | |
819 | 4191 /* When using ":w!" and writing to the current file, 'readonly' makes no |
164 | 4192 * sense, reset it, unless 'Z' appears in 'cpoptions'. */ |
4193 if (forceit && overwriting && vim_strchr(p_cpo, CPO_KEEPRO) == NULL) | |
7 | 4194 { |
4195 buf->b_p_ro = FALSE; | |
4196 #ifdef FEAT_TITLE | |
4197 need_maketitle = TRUE; /* set window title later */ | |
4198 #endif | |
4199 status_redraw_all(); /* redraw status lines later */ | |
4200 } | |
4201 | |
4202 if (end > buf->b_ml.ml_line_count) | |
4203 end = buf->b_ml.ml_line_count; | |
4204 if (buf->b_ml.ml_flags & ML_EMPTY) | |
4205 start = end + 1; | |
4206 | |
4207 /* | |
4208 * If the original file is being overwritten, there is a small chance that | |
4209 * we crash in the middle of writing. Therefore the file is preserved now. | |
4210 * This makes all block numbers positive so that recovery does not need | |
4211 * the original file. | |
4212 * Don't do this if there is a backup file and we are exiting. | |
4213 */ | |
39 | 4214 if (reset_changed && !newfile && overwriting |
7 | 4215 && !(exiting && backup != NULL)) |
4216 { | |
4217 ml_preserve(buf, FALSE); | |
4218 if (got_int) | |
4219 { | |
4220 errmsg = (char_u *)_(e_interr); | |
4221 goto restore_backup; | |
4222 } | |
4223 } | |
4224 | |
4225 #ifdef VMS | |
4226 vms_remove_version(fname); /* remove version */ | |
4227 #endif | |
1651 | 4228 /* Default: write the file directly. May write to a temp file for |
7 | 4229 * multi-byte conversion. */ |
4230 wfname = fname; | |
4231 | |
4232 #ifdef FEAT_MBYTE | |
4233 /* Check for forced 'fileencoding' from "++opt=val" argument. */ | |
4234 if (eap != NULL && eap->force_enc != 0) | |
4235 { | |
4236 fenc = eap->cmd + eap->force_enc; | |
4237 fenc = enc_canonize(fenc); | |
4238 fenc_tofree = fenc; | |
4239 } | |
4240 else | |
4241 fenc = buf->b_p_fenc; | |
4242 | |
4243 /* | |
1948 | 4244 * Check if the file needs to be converted. |
7 | 4245 */ |
1948 | 4246 converted = need_conversion(fenc); |
7 | 4247 |
4248 /* | |
4249 * Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done. Or | |
4250 * Latin1 to Unicode conversion. This is handled in buf_write_bytes(). | |
4251 * Prepare the flags for it and allocate bw_conv_buf when needed. | |
4252 */ | |
4253 if (converted && (enc_utf8 || STRCMP(p_enc, "latin1") == 0)) | |
4254 { | |
4255 wb_flags = get_fio_flags(fenc); | |
4256 if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8)) | |
4257 { | |
4258 /* Need to allocate a buffer to translate into. */ | |
4259 if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8)) | |
4260 write_info.bw_conv_buflen = bufsize * 2; | |
4261 else /* FIO_UCS4 */ | |
4262 write_info.bw_conv_buflen = bufsize * 4; | |
4263 write_info.bw_conv_buf | |
4264 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4265 if (write_info.bw_conv_buf == NULL) | |
4266 end = 0; | |
4267 } | |
4268 } | |
4269 | |
4270 # ifdef WIN3264 | |
4271 if (converted && wb_flags == 0 && (wb_flags = get_win_fio_flags(fenc)) != 0) | |
4272 { | |
4273 /* Convert UTF-8 -> UCS-2 and UCS-2 -> DBCS. Worst-case * 4: */ | |
4274 write_info.bw_conv_buflen = bufsize * 4; | |
4275 write_info.bw_conv_buf | |
4276 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4277 if (write_info.bw_conv_buf == NULL) | |
4278 end = 0; | |
4279 } | |
4280 # endif | |
4281 | |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
4282 # ifdef MACOS_CONVERT |
7 | 4283 if (converted && wb_flags == 0 && (wb_flags = get_mac_fio_flags(fenc)) != 0) |
4284 { | |
4285 write_info.bw_conv_buflen = bufsize * 3; | |
4286 write_info.bw_conv_buf | |
4287 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4288 if (write_info.bw_conv_buf == NULL) | |
4289 end = 0; | |
4290 } | |
4291 # endif | |
4292 | |
4293 # if defined(FEAT_EVAL) || defined(USE_ICONV) | |
4294 if (converted && wb_flags == 0) | |
4295 { | |
4296 # ifdef USE_ICONV | |
4297 /* | |
4298 * Use iconv() conversion when conversion is needed and it's not done | |
4299 * internally. | |
4300 */ | |
4301 write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc, | |
4302 enc_utf8 ? (char_u *)"utf-8" : p_enc); | |
4303 if (write_info.bw_iconv_fd != (iconv_t)-1) | |
4304 { | |
4305 /* We're going to use iconv(), allocate a buffer to convert in. */ | |
4306 write_info.bw_conv_buflen = bufsize * ICONV_MULT; | |
4307 write_info.bw_conv_buf | |
4308 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4309 if (write_info.bw_conv_buf == NULL) | |
4310 end = 0; | |
4311 write_info.bw_first = TRUE; | |
4312 } | |
4313 # ifdef FEAT_EVAL | |
4314 else | |
4315 # endif | |
4316 # endif | |
4317 | |
4318 # ifdef FEAT_EVAL | |
4319 /* | |
4320 * When the file needs to be converted with 'charconvert' after | |
4321 * writing, write to a temp file instead and let the conversion | |
4322 * overwrite the original file. | |
4323 */ | |
4324 if (*p_ccv != NUL) | |
4325 { | |
6721 | 4326 wfname = vim_tempname('w', FALSE); |
7 | 4327 if (wfname == NULL) /* Can't write without a tempfile! */ |
4328 { | |
4329 errmsg = (char_u *)_("E214: Can't find temp file for writing"); | |
4330 goto restore_backup; | |
4331 } | |
4332 } | |
4333 # endif | |
4334 } | |
4335 # endif | |
4336 if (converted && wb_flags == 0 | |
4337 # ifdef USE_ICONV | |
4338 && write_info.bw_iconv_fd == (iconv_t)-1 | |
4339 # endif | |
4340 # ifdef FEAT_EVAL | |
4341 && wfname == fname | |
4342 # endif | |
4343 ) | |
4344 { | |
4345 if (!forceit) | |
4346 { | |
4347 errmsg = (char_u *)_("E213: Cannot convert (add ! to write without conversion)"); | |
4348 goto restore_backup; | |
4349 } | |
4350 notconverted = TRUE; | |
4351 } | |
4352 #endif | |
4353 | |
4354 /* | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4355 * If conversion is taking place, we may first pretend to write and check |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4356 * for conversion errors. Then loop again to write for real. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4357 * When not doing conversion this writes for real right away. |
7 | 4358 */ |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4359 for (checking_conversion = TRUE; ; checking_conversion = FALSE) |
7 | 4360 { |
4361 /* | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4362 * There is no need to check conversion when: |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4363 * - there is no conversion |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4364 * - we make a backup file, that can be restored in case of conversion |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4365 * failure. |
7 | 4366 */ |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4367 #ifdef FEAT_MBYTE |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4368 if (!converted || dobackup) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4369 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4370 checking_conversion = FALSE; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4371 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4372 if (checking_conversion) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4373 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4374 /* Make sure we don't write anything. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4375 fd = -1; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4376 write_info.bw_fd = fd; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4377 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4378 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4379 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4380 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4381 * Open the file "wfname" for writing. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4382 * We may try to open the file twice: If we can't write to the file |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4383 * and forceit is TRUE we delete the existing file and try to |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4384 * create a new one. If this still fails we may have lost the |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4385 * original file! (this may happen when the user reached his |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4386 * quotum for number of files). |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4387 * Appending will fail if the file does not exist and forceit is |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4388 * FALSE. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4389 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4390 while ((fd = mch_open((char *)wfname, O_WRONLY | O_EXTRA | (append |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4391 ? (forceit ? (O_APPEND | O_CREAT) : O_APPEND) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4392 : (O_CREAT | O_TRUNC)) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4393 , perm < 0 ? 0666 : (perm & 0777))) < 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4394 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4395 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4396 * A forced write will try to create a new file if the old one |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4397 * is still readonly. This may also happen when the directory |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4398 * is read-only. In that case the mch_remove() will fail. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4399 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4400 if (errmsg == NULL) |
7 | 4401 { |
4402 #ifdef UNIX | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4403 stat_T st; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4404 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4405 /* Don't delete the file when it's a hard or symbolic link. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4406 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4407 if ((!newfile && st_old.st_nlink > 1) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4408 || (mch_lstat((char *)fname, &st) == 0 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4409 && (st.st_dev != st_old.st_dev |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4410 || st.st_ino != st_old.st_ino))) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4411 errmsg = (char_u *)_("E166: Can't open linked file for writing"); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4412 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4413 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4414 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4415 errmsg = (char_u *)_("E212: Can't open file for writing"); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4416 if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4417 && perm >= 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4418 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4419 #ifdef UNIX |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4420 /* we write to the file, thus it should be marked |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4421 writable after all */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4422 if (!(perm & 0200)) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4423 made_writable = TRUE; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4424 perm |= 0200; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4425 if (st_old.st_uid != getuid() |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4426 || st_old.st_gid != getgid()) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4427 perm &= 0777; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4428 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4429 if (!append) /* don't remove when appending */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4430 mch_remove(wfname); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4431 continue; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4432 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4433 } |
7 | 4434 } |
4435 | |
4436 restore_backup: | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4437 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4438 stat_T st; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4439 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4440 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4441 * If we failed to open the file, we don't need a backup. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4442 * Throw it away. If we moved or removed the original file |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4443 * try to put the backup in its place. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4444 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4445 if (backup != NULL && wfname == fname) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4446 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4447 if (backup_copy) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4448 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4449 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4450 * There is a small chance that we removed the |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4451 * original, try to move the copy in its place. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4452 * This may not work if the vim_rename() fails. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4453 * In that case we leave the copy around. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4454 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4455 /* If file does not exist, put the copy in its |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4456 * place */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4457 if (mch_stat((char *)fname, &st) < 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4458 vim_rename(backup, fname); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4459 /* if original file does exist throw away the copy |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4460 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4461 if (mch_stat((char *)fname, &st) >= 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4462 mch_remove(backup); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4463 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4464 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4465 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4466 /* try to put the original file back */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4467 vim_rename(backup, fname); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4468 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4469 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4470 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4471 /* if original file no longer exists give an extra warning |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4472 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4473 if (!newfile && mch_stat((char *)fname, &st) < 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4474 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4475 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4476 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4477 #ifdef FEAT_MBYTE |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4478 if (wfname != fname) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4479 vim_free(wfname); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4480 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4481 goto fail; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4482 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4483 write_info.bw_fd = fd; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4484 |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
4485 #if defined(WIN3264) |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4486 if (backup != NULL && overwriting && !append) |
7 | 4487 { |
4488 if (backup_copy) | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4489 (void)mch_copy_file_attribute(wfname, backup); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4490 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4491 (void)mch_copy_file_attribute(backup, wfname); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4492 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4493 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4494 if (!overwriting && !append) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4495 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4496 if (buf->b_ffname != NULL) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4497 (void)mch_copy_file_attribute(buf->b_ffname, wfname); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4498 /* Should copy resource fork */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4499 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4500 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4501 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4502 #ifdef FEAT_CRYPT |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4503 if (*buf->b_p_key != NUL && !filtering) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4504 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4505 char_u *header; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4506 int header_len; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4507 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4508 buf->b_cryptstate = crypt_create_for_writing( |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4509 crypt_get_method_nr(buf), |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4510 buf->b_p_key, &header, &header_len); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4511 if (buf->b_cryptstate == NULL || header == NULL) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4512 end = 0; |
7 | 4513 else |
4514 { | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4515 /* Write magic number, so that Vim knows how this file is |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4516 * encrypted when reading it back. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4517 write_info.bw_buf = header; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4518 write_info.bw_len = header_len; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4519 write_info.bw_flags = FIO_NOCONVERT; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4520 if (buf_write_bytes(&write_info) == FAIL) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4521 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4522 wb_flags |= FIO_ENCRYPTED; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4523 vim_free(header); |
7 | 4524 } |
4525 } | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4526 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4527 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4528 errmsg = NULL; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4529 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4530 write_info.bw_buf = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4531 nchars = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4532 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4533 /* use "++bin", "++nobin" or 'binary' */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4534 if (eap != NULL && eap->force_bin != 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4535 write_bin = (eap->force_bin == FORCE_BIN); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4536 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4537 write_bin = buf->b_p_bin; |
7 | 4538 |
4539 #ifdef FEAT_MBYTE | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4540 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4541 * The BOM is written just after the encryption magic number. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4542 * Skip it when appending and the file already existed, the BOM only |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4543 * makes sense at the start of the file. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4544 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4545 if (buf->b_p_bomb && !write_bin && (!append || perm < 0)) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4546 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4547 write_info.bw_len = make_bom(buffer, fenc); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4548 if (write_info.bw_len > 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4549 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4550 /* don't convert, do encryption */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4551 write_info.bw_flags = FIO_NOCONVERT | wb_flags; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4552 if (buf_write_bytes(&write_info) == FAIL) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4553 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4554 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4555 nchars += write_info.bw_len; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4556 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4557 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4558 write_info.bw_start_lnum = start; |
7 | 4559 #endif |
4560 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4561 #ifdef FEAT_PERSISTENT_UNDO |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4562 write_undo_file = (buf->b_p_udf |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4563 && overwriting |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4564 && !append |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4565 && !filtering |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4566 && reset_changed |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4567 && !checking_conversion); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4568 if (write_undo_file) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4569 /* Prepare for computing the hash value of the text. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4570 sha256_start(&sha_ctx); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4571 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4572 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4573 write_info.bw_len = bufsize; |
7 | 4574 #ifdef HAS_BW_FLAGS |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4575 write_info.bw_flags = wb_flags; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4576 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4577 fileformat = get_fileformat_force(buf, eap); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4578 s = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4579 len = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4580 for (lnum = start; lnum <= end; ++lnum) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4581 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4582 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4583 * The next while loop is done once for each character written. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4584 * Keep it fast! |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4585 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4586 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
|
4587 #ifdef FEAT_PERSISTENT_UNDO |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4588 if (write_undo_file) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4589 sha256_update(&sha_ctx, ptr + 1, |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4590 (UINT32_T)(STRLEN(ptr + 1) + 1)); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4591 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4592 while ((c = *++ptr) != NUL) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4593 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4594 if (c == NL) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4595 *s = NUL; /* replace newlines with NULs */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4596 else if (c == CAR && fileformat == EOL_MAC) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4597 *s = NL; /* Mac: replace CRs with NLs */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4598 else |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4599 *s = c; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4600 ++s; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4601 if (++len != bufsize) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4602 continue; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4603 if (buf_write_bytes(&write_info) == FAIL) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4604 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4605 end = 0; /* write error: break loop */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4606 break; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4607 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4608 nchars += bufsize; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4609 s = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4610 len = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4611 #ifdef FEAT_MBYTE |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4612 write_info.bw_start_lnum = lnum; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4613 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4614 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4615 /* write failed or last line has no EOL: stop here */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4616 if (end == 0 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4617 || (lnum == end |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4618 && (write_bin || !buf->b_p_fixeol) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4619 && (lnum == buf->b_no_eol_lnum |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4620 || (lnum == buf->b_ml.ml_line_count |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4621 && !buf->b_p_eol)))) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4622 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4623 ++lnum; /* written the line, count it */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4624 no_eol = TRUE; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4625 break; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4626 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4627 if (fileformat == EOL_UNIX) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4628 *s++ = NL; |
7 | 4629 else |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4630 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4631 *s++ = CAR; /* EOL_MAC or EOL_DOS: write CR */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4632 if (fileformat == EOL_DOS) /* write CR-NL */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4633 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4634 if (++len == bufsize) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4635 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4636 if (buf_write_bytes(&write_info) == FAIL) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4637 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4638 end = 0; /* write error: break loop */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4639 break; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4640 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4641 nchars += bufsize; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4642 s = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4643 len = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4644 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4645 *s++ = NL; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4646 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4647 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4648 if (++len == bufsize && end) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4649 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4650 if (buf_write_bytes(&write_info) == FAIL) |
7 | 4651 { |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4652 end = 0; /* write error: break loop */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4653 break; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4654 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4655 nchars += bufsize; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4656 s = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4657 len = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4658 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4659 ui_breakcheck(); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4660 if (got_int) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4661 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4662 end = 0; /* Interrupted, break loop */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4663 break; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4664 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4665 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4666 #ifdef VMS |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4667 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4668 * On VMS there is a problem: newlines get added when writing |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4669 * blocks at a time. Fix it by writing a line at a time. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4670 * This is much slower! |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4671 * Explanation: VAX/DECC RTL insists that records in some RMS |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4672 * structures end with a newline (carriage return) character, and |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4673 * if they don't it adds one. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4674 * With other RMS structures it works perfect without this fix. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4675 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4676 if (buf->b_fab_rfm == FAB$C_VFC |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4677 || ((buf->b_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0)) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4678 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4679 int b2write; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4680 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4681 buf->b_fab_mrs = (buf->b_fab_mrs == 0 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4682 ? MIN(4096, bufsize) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4683 : MIN(buf->b_fab_mrs, bufsize)); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4684 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4685 b2write = len; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4686 while (b2write > 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4687 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4688 write_info.bw_len = MIN(b2write, buf->b_fab_mrs); |
7 | 4689 if (buf_write_bytes(&write_info) == FAIL) |
4690 { | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4691 end = 0; |
7 | 4692 break; |
4693 } | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4694 b2write -= MIN(b2write, buf->b_fab_mrs); |
7 | 4695 } |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4696 write_info.bw_len = bufsize; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4697 nchars += len; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4698 s = buffer; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4699 len = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4700 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4701 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4702 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4703 if (len > 0 && end > 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4704 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4705 write_info.bw_len = len; |
7 | 4706 if (buf_write_bytes(&write_info) == FAIL) |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4707 end = 0; /* write error */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4708 nchars += len; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4709 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4710 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4711 /* Stop when writing done or an error was encountered. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4712 if (!checking_conversion || end == 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4713 break; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4714 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4715 /* If no error happened until now, writing should be ok, so loop to |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4716 * really write the buffer. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4717 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4718 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4719 /* If we started writing, finish writing. Also when an error was |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4720 * encountered. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4721 if (!checking_conversion) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4722 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4723 #if defined(UNIX) && defined(HAVE_FSYNC) |
7 | 4724 /* |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4725 * On many journalling file systems there is a bug that causes both the |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4726 * original and the backup file to be lost when halting the system |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4727 * right after writing the file. That's because only the meta-data is |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4728 * journalled. Syncing the file slows down the system, but assures it |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4729 * has been written to disk and we don't lose it. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4730 * For a device do try the fsync() but don't complain if it does not |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4731 * work (could be a pipe). |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4732 * If the 'fsync' option is FALSE, don't fsync(). Useful for laptops. |
7 | 4733 */ |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4734 if (p_fs && fsync(fd) != 0 && !device) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4735 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4736 errmsg = (char_u *)_("E667: Fsync failed"); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4737 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4738 } |
7 | 4739 #endif |
4740 | |
5788 | 4741 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4742 /* Probably need to set the security context. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4743 if (!backup_copy) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4744 mch_copy_sec(backup, wfname); |
1583 | 4745 #endif |
4746 | |
557 | 4747 #ifdef UNIX |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4748 /* When creating a new file, set its owner/group to that of the |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4749 * original file. Get the new device and inode number. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4750 if (backup != NULL && !backup_copy) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4751 { |
557 | 4752 # ifdef HAVE_FCHOWN |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4753 stat_T st; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4754 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4755 /* don't change the owner when it's already OK, some systems remove |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4756 * permission or ACL stuff */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4757 if (mch_stat((char *)wfname, &st) < 0 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4758 || st.st_uid != st_old.st_uid |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4759 || st.st_gid != st_old.st_gid) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4760 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4761 ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4762 if (perm >= 0) /* set permission again, may have changed */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4763 (void)mch_setperm(wfname, perm); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4764 } |
557 | 4765 # endif |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4766 buf_setino(buf); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4767 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4768 else if (!buf->b_dev_valid) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4769 /* Set the inode when creating a new file. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4770 buf_setino(buf); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4771 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4772 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4773 if (close(fd) != 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4774 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4775 errmsg = (char_u *)_("E512: Close failed"); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4776 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4777 } |
7 | 4778 |
4779 #ifdef UNIX | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4780 if (made_writable) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4781 perm &= ~0200; /* reset 'w' bit for security reasons */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4782 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4783 if (perm >= 0) /* set perm. of new file same as old file */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4784 (void)mch_setperm(wfname, perm); |
7 | 4785 #ifdef HAVE_ACL |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4786 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4787 * Probably need to set the ACL before changing the user (can't set the |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4788 * ACL on a file the user doesn't own). |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4789 * On Solaris, with ZFS and the aclmode property set to "discard" (the |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4790 * default), chmod() discards all part of a file's ACL that don't |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4791 * represent the mode of the file. It's non-trivial for us to discover |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4792 * whether we're in that situation, so we simply always re-set the ACL. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4793 */ |
9509
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
4794 # ifndef HAVE_SOLARIS_ZFS_ACL |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4795 if (!backup_copy) |
9509
e61974ab3ed0
commit https://github.com/vim/vim/commit/da4127794aa333631bde879e73bcfce4aef42f85
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
4796 # endif |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4797 mch_set_acl(wfname, acl); |
7 | 4798 #endif |
2267 | 4799 #ifdef FEAT_CRYPT |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4800 if (buf->b_cryptstate != NULL) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4801 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4802 crypt_free_state(buf->b_cryptstate); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4803 buf->b_cryptstate = NULL; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4804 } |
6122 | 4805 #endif |
7 | 4806 |
4807 #if defined(FEAT_MBYTE) && defined(FEAT_EVAL) | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4808 if (wfname != fname) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4809 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4810 /* |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4811 * The file was written to a temp file, now it needs to be |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4812 * converted with 'charconvert' to (overwrite) the output file. |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4813 */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4814 if (end != 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4815 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4816 if (eval_charconvert(enc_utf8 ? (char_u *)"utf-8" : p_enc, |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4817 fenc, wfname, fname) == FAIL) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4818 { |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4819 write_info.bw_conv_error = TRUE; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4820 end = 0; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4821 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4822 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4823 mch_remove(wfname); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4824 vim_free(wfname); |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4825 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4826 #endif |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4827 } |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4828 |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4829 if (end == 0) |
7 | 4830 { |
4831 /* | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
4832 * Error encountered. |
7 | 4833 */ |
4834 if (errmsg == NULL) | |
4835 { | |
4836 #ifdef FEAT_MBYTE | |
4837 if (write_info.bw_conv_error) | |
1947 | 4838 { |
4839 if (write_info.bw_conv_error_lnum == 0) | |
4840 errmsg = (char_u *)_("E513: write error, conversion failed (make 'fenc' empty to override)"); | |
4841 else | |
4842 { | |
4843 errmsg_allocated = TRUE; | |
4844 errmsg = alloc(300); | |
4845 vim_snprintf((char *)errmsg, 300, _("E513: write error, conversion failed in line %ld (make 'fenc' empty to override)"), | |
4846 (long)write_info.bw_conv_error_lnum); | |
4847 } | |
4848 } | |
7 | 4849 else |
4850 #endif | |
4851 if (got_int) | |
4852 errmsg = (char_u *)_(e_interr); | |
4853 else | |
4854 errmsg = (char_u *)_("E514: write error (file system full?)"); | |
4855 } | |
4856 | |
4857 /* | |
4858 * If we have a backup file, try to put it in place of the new file, | |
1698 | 4859 * because the new file is probably corrupt. This avoids losing the |
7 | 4860 * original file when trying to make a backup when writing the file a |
4861 * second time. | |
4862 * When "backup_copy" is set we need to copy the backup over the new | |
4863 * file. Otherwise rename the backup file. | |
4864 * If this is OK, don't give the extra warning message. | |
4865 */ | |
4866 if (backup != NULL) | |
4867 { | |
4868 if (backup_copy) | |
4869 { | |
4870 /* This may take a while, if we were interrupted let the user | |
4871 * know we got the message. */ | |
4872 if (got_int) | |
4873 { | |
4874 MSG(_(e_interr)); | |
4875 out_flush(); | |
4876 } | |
4877 if ((fd = mch_open((char *)backup, O_RDONLY | O_EXTRA, 0)) >= 0) | |
4878 { | |
4879 if ((write_info.bw_fd = mch_open((char *)fname, | |
194 | 4880 O_WRONLY | O_CREAT | O_TRUNC | O_EXTRA, |
4881 perm & 0777)) >= 0) | |
7 | 4882 { |
4883 /* copy the file. */ | |
4884 write_info.bw_buf = smallbuf; | |
4885 #ifdef HAS_BW_FLAGS | |
4886 write_info.bw_flags = FIO_NOCONVERT; | |
4887 #endif | |
2664 | 4888 while ((write_info.bw_len = read_eintr(fd, smallbuf, |
7 | 4889 SMBUFSIZE)) > 0) |
4890 if (buf_write_bytes(&write_info) == FAIL) | |
4891 break; | |
4892 | |
4893 if (close(write_info.bw_fd) >= 0 | |
4894 && write_info.bw_len == 0) | |
4895 end = 1; /* success */ | |
4896 } | |
4897 close(fd); /* ignore errors for closing read file */ | |
4898 } | |
4899 } | |
4900 else | |
4901 { | |
4902 if (vim_rename(backup, fname) == 0) | |
4903 end = 1; | |
4904 } | |
4905 } | |
4906 goto fail; | |
4907 } | |
4908 | |
4909 lnum -= start; /* compute number of written lines */ | |
4910 --no_wait_return; /* may wait for return now */ | |
4911 | |
4912 #if !(defined(UNIX) || defined(VMS)) | |
4913 fname = sfname; /* use shortname now, for the messages */ | |
4914 #endif | |
4915 if (!filtering) | |
4916 { | |
4917 msg_add_fname(buf, fname); /* put fname in IObuff with quotes */ | |
4918 c = FALSE; | |
4919 #ifdef FEAT_MBYTE | |
4920 if (write_info.bw_conv_error) | |
4921 { | |
4922 STRCAT(IObuff, _(" CONVERSION ERROR")); | |
4923 c = TRUE; | |
1947 | 4924 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
|
4925 vim_snprintf_add((char *)IObuff, IOSIZE, _(" in line %ld;"), |
1947 | 4926 (long)write_info.bw_conv_error_lnum); |
7 | 4927 } |
4928 else if (notconverted) | |
4929 { | |
4930 STRCAT(IObuff, _("[NOT converted]")); | |
4931 c = TRUE; | |
4932 } | |
4933 else if (converted) | |
4934 { | |
4935 STRCAT(IObuff, _("[converted]")); | |
4936 c = TRUE; | |
4937 } | |
4938 #endif | |
4939 if (device) | |
4940 { | |
4941 STRCAT(IObuff, _("[Device]")); | |
4942 c = TRUE; | |
4943 } | |
4944 else if (newfile) | |
4945 { | |
4946 STRCAT(IObuff, shortmess(SHM_NEW) ? _("[New]") : _("[New File]")); | |
4947 c = TRUE; | |
4948 } | |
4949 if (no_eol) | |
4950 { | |
4951 msg_add_eol(); | |
4952 c = TRUE; | |
4953 } | |
4954 /* may add [unix/dos/mac] */ | |
4955 if (msg_add_fileformat(fileformat)) | |
4956 c = TRUE; | |
4957 #ifdef FEAT_CRYPT | |
4958 if (wb_flags & FIO_ENCRYPTED) | |
4959 { | |
6122 | 4960 crypt_append_msg(buf); |
7 | 4961 c = TRUE; |
4962 } | |
4963 #endif | |
4964 msg_add_lines(c, (long)lnum, nchars); /* add line/char count */ | |
4965 if (!shortmess(SHM_WRITE)) | |
4966 { | |
4967 if (append) | |
4968 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [a]") : _(" appended")); | |
4969 else | |
4970 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [w]") : _(" written")); | |
4971 } | |
4972 | |
679 | 4973 set_keep_msg(msg_trunc_attr(IObuff, FALSE, 0), 0); |
7 | 4974 } |
4975 | |
39 | 4976 /* When written everything correctly: reset 'modified'. Unless not |
4977 * writing to the original file and '+' is not in 'cpoptions'. */ | |
589 | 4978 if (reset_changed && whole && !append |
7 | 4979 #ifdef FEAT_MBYTE |
4980 && !write_info.bw_conv_error | |
4981 #endif | |
39 | 4982 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL) |
4983 ) | |
7 | 4984 { |
4985 unchanged(buf, TRUE); | |
6320 | 4986 #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
|
4987 /* b:changedtick is always incremented in unchanged() but that |
6320 | 4988 * 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
|
4989 if (last_changedtick + 1 == CHANGEDTICK(buf) |
6320 | 4990 && 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
|
4991 last_changedtick = CHANGEDTICK(buf); |
6320 | 4992 #endif |
7 | 4993 u_unchanged(buf); |
2281
e41433ea71df
Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents:
2280
diff
changeset
|
4994 u_update_save_nr(buf); |
7 | 4995 } |
4996 | |
4997 /* | |
4998 * If written to the current file, update the timestamp of the swap file | |
4999 * and reset the BF_WRITE_MASK flags. Also sets buf->b_mtime. | |
5000 */ | |
5001 if (overwriting) | |
5002 { | |
5003 ml_timestamp(buf); | |
589 | 5004 if (append) |
5005 buf->b_flags &= ~BF_NEW; | |
5006 else | |
5007 buf->b_flags &= ~BF_WRITE_MASK; | |
7 | 5008 } |
5009 | |
5010 /* | |
5011 * If we kept a backup until now, and we are in patch mode, then we make | |
5012 * the backup file our 'original' file. | |
5013 */ | |
5014 if (*p_pm && dobackup) | |
5015 { | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
5016 char *org = (char *)buf_modname((buf->b_p_sn || buf->b_shortname), |
7 | 5017 fname, p_pm, FALSE); |
5018 | |
5019 if (backup != NULL) | |
5020 { | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
5021 stat_T st; |
7 | 5022 |
5023 /* | |
5024 * If the original file does not exist yet | |
5025 * the current backup file becomes the original file | |
5026 */ | |
5027 if (org == NULL) | |
5028 EMSG(_("E205: Patchmode: can't save original file")); | |
5029 else if (mch_stat(org, &st) < 0) | |
5030 { | |
5031 vim_rename(backup, (char_u *)org); | |
5032 vim_free(backup); /* don't delete the file */ | |
5033 backup = NULL; | |
5034 #ifdef UNIX | |
5035 set_file_time((char_u *)org, st_old.st_atime, st_old.st_mtime); | |
5036 #endif | |
5037 } | |
5038 } | |
5039 /* | |
5040 * If there is no backup file, remember that a (new) file was | |
5041 * created. | |
5042 */ | |
5043 else | |
5044 { | |
5045 int empty_fd; | |
5046 | |
5047 if (org == NULL | |
557 | 5048 || (empty_fd = mch_open(org, |
5049 O_CREAT | O_EXTRA | O_EXCL | O_NOFOLLOW, | |
216 | 5050 perm < 0 ? 0666 : (perm & 0777))) < 0) |
7 | 5051 EMSG(_("E206: patchmode: can't touch empty original file")); |
5052 else | |
5053 close(empty_fd); | |
5054 } | |
5055 if (org != NULL) | |
5056 { | |
5057 mch_setperm((char_u *)org, mch_getperm(fname) & 0777); | |
5058 vim_free(org); | |
5059 } | |
5060 } | |
5061 | |
5062 /* | |
5063 * Remove the backup unless 'backup' option is set | |
5064 */ | |
5065 if (!p_bk && backup != NULL && mch_remove(backup) != 0) | |
5066 EMSG(_("E207: Can't delete backup file")); | |
5067 | |
5068 #ifdef FEAT_SUN_WORKSHOP | |
5069 if (usingSunWorkShop) | |
5070 workshop_file_saved((char *) ffname); | |
5071 #endif | |
5072 | |
5073 goto nofail; | |
5074 | |
5075 /* | |
5076 * Finish up. We get here either after failure or success. | |
5077 */ | |
5078 fail: | |
5079 --no_wait_return; /* may wait for return now */ | |
5080 nofail: | |
5081 | |
5082 /* Done saving, we accept changed buffer warnings again */ | |
5083 buf->b_saving = FALSE; | |
5084 | |
5085 vim_free(backup); | |
5086 if (buffer != smallbuf) | |
5087 vim_free(buffer); | |
5088 #ifdef FEAT_MBYTE | |
5089 vim_free(fenc_tofree); | |
5090 vim_free(write_info.bw_conv_buf); | |
5091 # ifdef USE_ICONV | |
5092 if (write_info.bw_iconv_fd != (iconv_t)-1) | |
5093 { | |
5094 iconv_close(write_info.bw_iconv_fd); | |
5095 write_info.bw_iconv_fd = (iconv_t)-1; | |
5096 } | |
5097 # endif | |
5098 #endif | |
5099 #ifdef HAVE_ACL | |
5100 mch_free_acl(acl); | |
5101 #endif | |
5102 | |
5103 if (errmsg != NULL) | |
5104 { | |
835 | 5105 int numlen = errnum != NULL ? (int)STRLEN(errnum) : 0; |
7 | 5106 |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
5107 attr = HL_ATTR(HLF_E); /* set highlight for error messages */ |
7 | 5108 msg_add_fname(buf, |
5109 #ifndef UNIX | |
5110 sfname | |
5111 #else | |
5112 fname | |
5113 #endif | |
5114 ); /* put file name in IObuff with quotes */ | |
5115 if (STRLEN(IObuff) + STRLEN(errmsg) + numlen >= IOSIZE) | |
5116 IObuff[IOSIZE - STRLEN(errmsg) - numlen - 1] = NUL; | |
5117 /* If the error message has the form "is ...", put the error number in | |
5118 * front of the file name. */ | |
5119 if (errnum != NULL) | |
5120 { | |
1620 | 5121 STRMOVE(IObuff + numlen, IObuff); |
7 | 5122 mch_memmove(IObuff, errnum, (size_t)numlen); |
5123 } | |
5124 STRCAT(IObuff, errmsg); | |
5125 emsg(IObuff); | |
1947 | 5126 if (errmsg_allocated) |
5127 vim_free(errmsg); | |
7 | 5128 |
5129 retval = FAIL; | |
5130 if (end == 0) | |
5131 { | |
5132 MSG_PUTS_ATTR(_("\nWARNING: Original file may be lost or damaged\n"), | |
5133 attr | MSG_HIST); | |
5134 MSG_PUTS_ATTR(_("don't quit the editor until the file is successfully written!"), | |
5135 attr | MSG_HIST); | |
5136 | |
5137 /* Update the timestamp to avoid an "overwrite changed file" | |
5138 * prompt when writing again. */ | |
5139 if (mch_stat((char *)fname, &st_old) >= 0) | |
5140 { | |
5141 buf_store_time(buf, &st_old, fname); | |
5142 buf->b_mtime_read = buf->b_mtime; | |
5143 } | |
5144 } | |
5145 } | |
5146 msg_scroll = msg_save; | |
5147 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5148 #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
|
5149 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5150 * 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
|
5151 * file. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5152 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5153 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
|
5154 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5155 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
|
5156 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5157 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
|
5158 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
|
5159 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5160 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5161 |
7 | 5162 #ifdef FEAT_AUTOCMD |
5163 #ifdef FEAT_EVAL | |
5164 if (!should_abort(retval)) | |
5165 #else | |
5166 if (!got_int) | |
5167 #endif | |
5168 { | |
5169 aco_save_T aco; | |
5170 | |
3482 | 5171 curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */ |
5172 | |
7 | 5173 /* |
5174 * Apply POST autocommands. | |
5175 * Careful: The autocommands may call buf_write() recursively! | |
5176 */ | |
5177 aucmd_prepbuf(&aco, buf); | |
5178 | |
5179 if (append) | |
5180 apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname, | |
5181 FALSE, curbuf, eap); | |
5182 else if (filtering) | |
5183 apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname, | |
5184 FALSE, curbuf, eap); | |
5185 else if (reset_changed && whole) | |
5186 apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname, | |
5187 FALSE, curbuf, eap); | |
5188 else | |
5189 apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname, | |
5190 FALSE, curbuf, eap); | |
5191 | |
5192 /* restore curwin/curbuf and a few other things */ | |
5193 aucmd_restbuf(&aco); | |
5194 | |
5195 #ifdef FEAT_EVAL | |
5196 if (aborting()) /* autocmds may abort script processing */ | |
5197 retval = FALSE; | |
5198 #endif | |
5199 } | |
5200 #endif | |
5201 | |
5202 got_int |= prev_got_int; | |
5203 | |
5204 return retval; | |
5205 } | |
5206 | |
5207 /* | |
633 | 5208 * Set the name of the current buffer. Use when the buffer doesn't have a |
5209 * name and a ":r" or ":w" command with a file name is used. | |
5210 */ | |
5211 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5212 set_rw_fname(char_u *fname, char_u *sfname) |
633 | 5213 { |
5214 #ifdef FEAT_AUTOCMD | |
1905 | 5215 buf_T *buf = curbuf; |
5216 | |
633 | 5217 /* It's like the unnamed buffer is deleted.... */ |
5218 if (curbuf->b_p_bl) | |
5219 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); | |
5220 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf); | |
5221 # ifdef FEAT_EVAL | |
5222 if (aborting()) /* autocmds may abort script processing */ | |
5223 return FAIL; | |
5224 # endif | |
1905 | 5225 if (curbuf != buf) |
5226 { | |
5227 /* We are in another buffer now, don't do the renaming. */ | |
5228 EMSG(_(e_auchangedbuf)); | |
5229 return FAIL; | |
5230 } | |
633 | 5231 #endif |
5232 | |
5233 if (setfname(curbuf, fname, sfname, FALSE) == OK) | |
5234 curbuf->b_flags |= BF_NOTEDITED; | |
5235 | |
5236 #ifdef FEAT_AUTOCMD | |
5237 /* ....and a new named one is created */ | |
5238 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf); | |
5239 if (curbuf->b_p_bl) | |
5240 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf); | |
5241 # ifdef FEAT_EVAL | |
5242 if (aborting()) /* autocmds may abort script processing */ | |
5243 return FAIL; | |
5244 # endif | |
5245 | |
5246 /* Do filetype detection now if 'filetype' is empty. */ | |
5247 if (*curbuf->b_p_ft == NUL) | |
5248 { | |
819 | 5249 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
|
5250 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL); |
717 | 5251 do_modelines(0); |
633 | 5252 } |
5253 #endif | |
5254 | |
5255 return OK; | |
5256 } | |
5257 | |
5258 /* | |
7 | 5259 * Put file name into IObuff with quotes. |
5260 */ | |
33 | 5261 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5262 msg_add_fname(buf_T *buf, char_u *fname) |
7 | 5263 { |
5264 if (fname == NULL) | |
5265 fname = (char_u *)"-stdin-"; | |
5266 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE); | |
5267 IObuff[0] = '"'; | |
5268 STRCAT(IObuff, "\" "); | |
5269 } | |
5270 | |
5271 /* | |
5272 * Append message for text mode to IObuff. | |
5273 * Return TRUE if something appended. | |
5274 */ | |
5275 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5276 msg_add_fileformat(int eol_type) |
7 | 5277 { |
5278 #ifndef USE_CRNL | |
5279 if (eol_type == EOL_DOS) | |
5280 { | |
5281 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]")); | |
5282 return TRUE; | |
5283 } | |
5284 #endif | |
5285 #ifndef USE_CR | |
5286 if (eol_type == EOL_MAC) | |
5287 { | |
5288 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]")); | |
5289 return TRUE; | |
5290 } | |
5291 #endif | |
5292 #if defined(USE_CRNL) || defined(USE_CR) | |
5293 if (eol_type == EOL_UNIX) | |
5294 { | |
5295 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]")); | |
5296 return TRUE; | |
5297 } | |
5298 #endif | |
5299 return FALSE; | |
5300 } | |
5301 | |
5302 /* | |
5303 * Append line and character count to IObuff. | |
5304 */ | |
33 | 5305 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5306 msg_add_lines( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5307 int insert_space, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5308 long lnum, |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
5309 off_T nchars) |
7 | 5310 { |
5311 char_u *p; | |
5312 | |
5313 p = IObuff + STRLEN(IObuff); | |
5314 | |
5315 if (insert_space) | |
5316 *p++ = ' '; | |
5317 if (shortmess(SHM_LINES)) | |
9391
4c40631238e4
commit https://github.com/vim/vim/commit/bde9810d6103ffe3a22a9330021cb21db1ed1792
Christian Brabandt <cb@256bit.org>
parents:
9389
diff
changeset
|
5318 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
|
5319 "%ldL, %lldC", lnum, (varnumber_T)nchars); |
7 | 5320 else |
5321 { | |
5322 if (lnum == 1) | |
5323 STRCPY(p, _("1 line, ")); | |
5324 else | |
5325 sprintf((char *)p, _("%ld lines, "), lnum); | |
5326 p += STRLEN(p); | |
5327 if (nchars == 1) | |
5328 STRCPY(p, _("1 character")); | |
5329 else | |
9391
4c40631238e4
commit https://github.com/vim/vim/commit/bde9810d6103ffe3a22a9330021cb21db1ed1792
Christian Brabandt <cb@256bit.org>
parents:
9389
diff
changeset
|
5330 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
|
5331 _("%lld characters"), (varnumber_T)nchars); |
7 | 5332 } |
5333 } | |
5334 | |
5335 /* | |
5336 * Append message for missing line separator to IObuff. | |
5337 */ | |
5338 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5339 msg_add_eol(void) |
7 | 5340 { |
5341 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]")); | |
5342 } | |
5343 | |
5344 /* | |
5345 * Check modification time of file, before writing to it. | |
5346 * The size isn't checked, because using a tool like "gzip" takes care of | |
5347 * using the same timestamp but can't set the size. | |
5348 */ | |
5349 static int | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
5350 check_mtime(buf_T *buf, stat_T *st) |
7 | 5351 { |
5352 if (buf->b_mtime_read != 0 | |
5353 && time_differs((long)st->st_mtime, buf->b_mtime_read)) | |
5354 { | |
5355 msg_scroll = TRUE; /* don't overwrite messages here */ | |
5356 msg_silent = 0; /* must give this prompt */ | |
5357 /* don't use emsg() here, don't want to flush the buffers */ | |
5358 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
|
5359 HL_ATTR(HLF_E)); |
7 | 5360 if (ask_yesno((char_u *)_("Do you really want to write to it"), |
5361 TRUE) == 'n') | |
5362 return FAIL; | |
5363 msg_scroll = FALSE; /* always overwrite the file message now */ | |
5364 } | |
5365 return OK; | |
5366 } | |
5367 | |
5368 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5369 time_differs(long t1, long t2) |
7 | 5370 { |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
5371 #if defined(__linux__) || defined(MSWIN) |
7 | 5372 /* On a FAT filesystem, esp. under Linux, there are only 5 bits to store |
5373 * the seconds. Since the roundoff is done when flushing the inode, the | |
5374 * time may change unexpectedly by one second!!! */ | |
5375 return (t1 - t2 > 1 || t2 - t1 > 1); | |
5376 #else | |
5377 return (t1 != t2); | |
5378 #endif | |
5379 } | |
5380 | |
5381 /* | |
5382 * Call write() to write a number of bytes to the file. | |
2664 | 5383 * Handles encryption and 'encoding' conversion. |
7 | 5384 * |
5385 * Return FAIL for failure, OK otherwise. | |
5386 */ | |
5387 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5388 buf_write_bytes(struct bw_info *ip) |
7 | 5389 { |
5390 int wlen; | |
5391 char_u *buf = ip->bw_buf; /* data to write */ | |
5392 int len = ip->bw_len; /* length of data */ | |
5393 #ifdef HAS_BW_FLAGS | |
5394 int flags = ip->bw_flags; /* extra flags */ | |
5395 #endif | |
5396 | |
5397 #ifdef FEAT_MBYTE | |
5398 /* | |
5399 * Skip conversion when writing the crypt magic number or the BOM. | |
5400 */ | |
5401 if (!(flags & FIO_NOCONVERT)) | |
5402 { | |
5403 char_u *p; | |
5404 unsigned c; | |
5405 int n; | |
5406 | |
5407 if (flags & FIO_UTF8) | |
5408 { | |
5409 /* | |
5410 * Convert latin1 in the buffer to UTF-8 in the file. | |
5411 */ | |
5412 p = ip->bw_conv_buf; /* translate to buffer */ | |
5413 for (wlen = 0; wlen < len; ++wlen) | |
5414 p += utf_char2bytes(buf[wlen], p); | |
5415 buf = ip->bw_conv_buf; | |
5416 len = (int)(p - ip->bw_conv_buf); | |
5417 } | |
5418 else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1)) | |
5419 { | |
5420 /* | |
5421 * Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or | |
5422 * Latin1 chars in the file. | |
5423 */ | |
5424 if (flags & FIO_LATIN1) | |
5425 p = buf; /* translate in-place (can only get shorter) */ | |
5426 else | |
5427 p = ip->bw_conv_buf; /* translate to buffer */ | |
5428 for (wlen = 0; wlen < len; wlen += n) | |
5429 { | |
5430 if (wlen == 0 && ip->bw_restlen != 0) | |
5431 { | |
5432 int l; | |
5433 | |
5434 /* Use remainder of previous call. Append the start of | |
5435 * buf[] to get a full sequence. Might still be too | |
5436 * short! */ | |
5437 l = CONV_RESTLEN - ip->bw_restlen; | |
5438 if (l > len) | |
5439 l = len; | |
5440 mch_memmove(ip->bw_rest + ip->bw_restlen, buf, (size_t)l); | |
474 | 5441 n = utf_ptr2len_len(ip->bw_rest, ip->bw_restlen + l); |
7 | 5442 if (n > ip->bw_restlen + len) |
5443 { | |
5444 /* We have an incomplete byte sequence at the end to | |
5445 * be written. We can't convert it without the | |
5446 * remaining bytes. Keep them for the next call. */ | |
5447 if (ip->bw_restlen + len > CONV_RESTLEN) | |
5448 return FAIL; | |
5449 ip->bw_restlen += len; | |
5450 break; | |
5451 } | |
5452 if (n > 1) | |
5453 c = utf_ptr2char(ip->bw_rest); | |
5454 else | |
5455 c = ip->bw_rest[0]; | |
5456 if (n >= ip->bw_restlen) | |
5457 { | |
5458 n -= ip->bw_restlen; | |
5459 ip->bw_restlen = 0; | |
5460 } | |
5461 else | |
5462 { | |
5463 ip->bw_restlen -= n; | |
5464 mch_memmove(ip->bw_rest, ip->bw_rest + n, | |
5465 (size_t)ip->bw_restlen); | |
5466 n = 0; | |
5467 } | |
5468 } | |
5469 else | |
5470 { | |
474 | 5471 n = utf_ptr2len_len(buf + wlen, len - wlen); |
7 | 5472 if (n > len - wlen) |
5473 { | |
5474 /* We have an incomplete byte sequence at the end to | |
5475 * be written. We can't convert it without the | |
5476 * remaining bytes. Keep them for the next call. */ | |
5477 if (len - wlen > CONV_RESTLEN) | |
5478 return FAIL; | |
5479 ip->bw_restlen = len - wlen; | |
5480 mch_memmove(ip->bw_rest, buf + wlen, | |
5481 (size_t)ip->bw_restlen); | |
5482 break; | |
5483 } | |
5484 if (n > 1) | |
5485 c = utf_ptr2char(buf + wlen); | |
5486 else | |
5487 c = buf[wlen]; | |
5488 } | |
5489 | |
1947 | 5490 if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error) |
5491 { | |
5492 ip->bw_conv_error = TRUE; | |
5493 ip->bw_conv_error_lnum = ip->bw_start_lnum; | |
5494 } | |
5495 if (c == NL) | |
5496 ++ip->bw_start_lnum; | |
7 | 5497 } |
5498 if (flags & FIO_LATIN1) | |
5499 len = (int)(p - buf); | |
5500 else | |
5501 { | |
5502 buf = ip->bw_conv_buf; | |
5503 len = (int)(p - ip->bw_conv_buf); | |
5504 } | |
5505 } | |
5506 | |
5507 # ifdef WIN3264 | |
5508 else if (flags & FIO_CODEPAGE) | |
5509 { | |
5510 /* | |
5511 * Convert UTF-8 or codepage to UCS-2 and then to MS-Windows | |
5512 * codepage. | |
5513 */ | |
5514 char_u *from; | |
5515 size_t fromlen; | |
5516 char_u *to; | |
5517 int u8c; | |
5518 BOOL bad = FALSE; | |
5519 int needed; | |
5520 | |
5521 if (ip->bw_restlen > 0) | |
5522 { | |
5523 /* Need to concatenate the remainder of the previous call and | |
5524 * the bytes of the current call. Use the end of the | |
5525 * conversion buffer for this. */ | |
5526 fromlen = len + ip->bw_restlen; | |
5527 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; | |
5528 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); | |
5529 mch_memmove(from + ip->bw_restlen, buf, (size_t)len); | |
5530 } | |
5531 else | |
5532 { | |
5533 from = buf; | |
5534 fromlen = len; | |
5535 } | |
5536 | |
5537 to = ip->bw_conv_buf; | |
5538 if (enc_utf8) | |
5539 { | |
5540 /* Convert from UTF-8 to UCS-2, to the start of the buffer. | |
5541 * The buffer has been allocated to be big enough. */ | |
5542 while (fromlen > 0) | |
5543 { | |
835 | 5544 n = (int)utf_ptr2len_len(from, (int)fromlen); |
7 | 5545 if (n > (int)fromlen) /* incomplete byte sequence */ |
5546 break; | |
5547 u8c = utf_ptr2char(from); | |
5548 *to++ = (u8c & 0xff); | |
5549 *to++ = (u8c >> 8); | |
5550 fromlen -= n; | |
5551 from += n; | |
5552 } | |
5553 | |
5554 /* Copy remainder to ip->bw_rest[] to be used for the next | |
5555 * call. */ | |
5556 if (fromlen > CONV_RESTLEN) | |
5557 { | |
5558 /* weird overlong sequence */ | |
5559 ip->bw_conv_error = TRUE; | |
5560 return FAIL; | |
5561 } | |
5562 mch_memmove(ip->bw_rest, from, fromlen); | |
835 | 5563 ip->bw_restlen = (int)fromlen; |
7 | 5564 } |
5565 else | |
5566 { | |
5567 /* Convert from enc_codepage to UCS-2, to the start of the | |
5568 * buffer. The buffer has been allocated to be big enough. */ | |
5569 ip->bw_restlen = 0; | |
5570 needed = MultiByteToWideChar(enc_codepage, | |
1142 | 5571 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen, |
7 | 5572 NULL, 0); |
5573 if (needed == 0) | |
5574 { | |
5575 /* When conversion fails there may be a trailing byte. */ | |
5576 needed = MultiByteToWideChar(enc_codepage, | |
1142 | 5577 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen - 1, |
7 | 5578 NULL, 0); |
5579 if (needed == 0) | |
5580 { | |
5581 /* Conversion doesn't work. */ | |
5582 ip->bw_conv_error = TRUE; | |
5583 return FAIL; | |
5584 } | |
5585 /* Save the trailing byte for the next call. */ | |
5586 ip->bw_rest[0] = from[fromlen - 1]; | |
5587 ip->bw_restlen = 1; | |
5588 } | |
5589 needed = MultiByteToWideChar(enc_codepage, MB_ERR_INVALID_CHARS, | |
1142 | 5590 (LPCSTR)from, (int)(fromlen - ip->bw_restlen), |
7 | 5591 (LPWSTR)to, needed); |
5592 if (needed == 0) | |
5593 { | |
5594 /* Safety check: Conversion doesn't work. */ | |
5595 ip->bw_conv_error = TRUE; | |
5596 return FAIL; | |
5597 } | |
5598 to += needed * 2; | |
5599 } | |
5600 | |
5601 fromlen = to - ip->bw_conv_buf; | |
5602 buf = to; | |
5603 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
5604 if (FIO_GET_CP(flags) == CP_UTF8) | |
5605 { | |
5606 /* Convert from UCS-2 to UTF-8, using the remainder of the | |
5607 * conversion buffer. Fails when out of space. */ | |
5608 for (from = ip->bw_conv_buf; fromlen > 1; fromlen -= 2) | |
5609 { | |
5610 u8c = *from++; | |
5611 u8c += (*from++ << 8); | |
5612 to += utf_char2bytes(u8c, to); | |
5613 if (to + 6 >= ip->bw_conv_buf + ip->bw_conv_buflen) | |
5614 { | |
5615 ip->bw_conv_error = TRUE; | |
5616 return FAIL; | |
5617 } | |
5618 } | |
835 | 5619 len = (int)(to - buf); |
7 | 5620 } |
5621 else | |
5622 #endif | |
5623 { | |
5624 /* Convert from UCS-2 to the codepage, using the remainder of | |
5625 * the conversion buffer. If the conversion uses the default | |
5626 * character "0", the data doesn't fit in this encoding, so | |
5627 * fail. */ | |
5628 len = WideCharToMultiByte(FIO_GET_CP(flags), 0, | |
5629 (LPCWSTR)ip->bw_conv_buf, (int)fromlen / sizeof(WCHAR), | |
1142 | 5630 (LPSTR)to, (int)(ip->bw_conv_buflen - fromlen), 0, |
5631 &bad); | |
7 | 5632 if (bad) |
5633 { | |
5634 ip->bw_conv_error = TRUE; | |
5635 return FAIL; | |
5636 } | |
5637 } | |
5638 } | |
5639 # endif | |
5640 | |
765 | 5641 # ifdef MACOS_CONVERT |
7 | 5642 else if (flags & FIO_MACROMAN) |
5643 { | |
5644 /* | |
5645 * Convert UTF-8 or latin1 to Apple MacRoman. | |
5646 */ | |
5647 char_u *from; | |
5648 size_t fromlen; | |
5649 | |
5650 if (ip->bw_restlen > 0) | |
5651 { | |
5652 /* Need to concatenate the remainder of the previous call and | |
5653 * the bytes of the current call. Use the end of the | |
5654 * conversion buffer for this. */ | |
5655 fromlen = len + ip->bw_restlen; | |
5656 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; | |
5657 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); | |
5658 mch_memmove(from + ip->bw_restlen, buf, (size_t)len); | |
5659 } | |
5660 else | |
5661 { | |
5662 from = buf; | |
5663 fromlen = len; | |
5664 } | |
5665 | |
18 | 5666 if (enc2macroman(from, fromlen, |
5667 ip->bw_conv_buf, &len, ip->bw_conv_buflen, | |
5668 ip->bw_rest, &ip->bw_restlen) == FAIL) | |
7 | 5669 { |
5670 ip->bw_conv_error = TRUE; | |
5671 return FAIL; | |
5672 } | |
5673 buf = ip->bw_conv_buf; | |
5674 } | |
5675 # endif | |
5676 | |
5677 # ifdef USE_ICONV | |
5678 if (ip->bw_iconv_fd != (iconv_t)-1) | |
5679 { | |
5680 const char *from; | |
5681 size_t fromlen; | |
5682 char *to; | |
5683 size_t tolen; | |
5684 | |
5685 /* Convert with iconv(). */ | |
5686 if (ip->bw_restlen > 0) | |
5687 { | |
1836 | 5688 char *fp; |
5689 | |
7 | 5690 /* Need to concatenate the remainder of the previous call and |
5691 * the bytes of the current call. Use the end of the | |
5692 * conversion buffer for this. */ | |
5693 fromlen = len + ip->bw_restlen; | |
1836 | 5694 fp = (char *)ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; |
5695 mch_memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen); | |
5696 mch_memmove(fp + ip->bw_restlen, buf, (size_t)len); | |
5697 from = fp; | |
7 | 5698 tolen = ip->bw_conv_buflen - fromlen; |
5699 } | |
5700 else | |
5701 { | |
5702 from = (const char *)buf; | |
5703 fromlen = len; | |
5704 tolen = ip->bw_conv_buflen; | |
5705 } | |
5706 to = (char *)ip->bw_conv_buf; | |
5707 | |
5708 if (ip->bw_first) | |
5709 { | |
5710 size_t save_len = tolen; | |
5711 | |
5712 /* output the initial shift state sequence */ | |
5713 (void)iconv(ip->bw_iconv_fd, NULL, NULL, &to, &tolen); | |
5714 | |
5715 /* There is a bug in iconv() on Linux (which appears to be | |
5716 * wide-spread) which sets "to" to NULL and messes up "tolen". | |
5717 */ | |
5718 if (to == NULL) | |
5719 { | |
5720 to = (char *)ip->bw_conv_buf; | |
5721 tolen = save_len; | |
5722 } | |
5723 ip->bw_first = FALSE; | |
5724 } | |
5725 | |
5726 /* | |
5727 * If iconv() has an error or there is not enough room, fail. | |
5728 */ | |
5729 if ((iconv(ip->bw_iconv_fd, (void *)&from, &fromlen, &to, &tolen) | |
5730 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) | |
5731 || fromlen > CONV_RESTLEN) | |
5732 { | |
5733 ip->bw_conv_error = TRUE; | |
5734 return FAIL; | |
5735 } | |
5736 | |
5737 /* copy remainder to ip->bw_rest[] to be used for the next call. */ | |
5738 if (fromlen > 0) | |
5739 mch_memmove(ip->bw_rest, (void *)from, fromlen); | |
5740 ip->bw_restlen = (int)fromlen; | |
5741 | |
5742 buf = ip->bw_conv_buf; | |
5743 len = (int)((char_u *)to - ip->bw_conv_buf); | |
5744 } | |
5745 # endif | |
5746 } | |
5747 #endif /* FEAT_MBYTE */ | |
5748 | |
11605
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
5749 if (ip->bw_fd < 0) |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
5750 /* Only checking conversion, which is OK if we get here. */ |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
5751 return OK; |
16ccaedce025
patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents:
11325
diff
changeset
|
5752 |
7 | 5753 #ifdef FEAT_CRYPT |
6122 | 5754 if (flags & FIO_ENCRYPTED) |
5755 { | |
5756 /* Encrypt the data. Do it in-place if possible, otherwise use an | |
5757 * allocated buffer. */ | |
5758 if (crypt_works_inplace(ip->bw_buffer->b_cryptstate)) | |
5759 { | |
5760 crypt_encode_inplace(ip->bw_buffer->b_cryptstate, buf, len); | |
5761 } | |
5762 else | |
5763 { | |
5764 char_u *outbuf; | |
5765 | |
5766 len = crypt_encode_alloc(curbuf->b_cryptstate, buf, len, &outbuf); | |
5767 if (len == 0) | |
5768 return OK; /* Crypt layer is buffering, will flush later. */ | |
5769 wlen = write_eintr(ip->bw_fd, outbuf, len); | |
5770 vim_free(outbuf); | |
5771 return (wlen < len) ? FAIL : OK; | |
5772 } | |
5773 } | |
7 | 5774 #endif |
5775 | |
2664 | 5776 wlen = write_eintr(ip->bw_fd, buf, len); |
5777 return (wlen < len) ? FAIL : OK; | |
7 | 5778 } |
5779 | |
5780 #ifdef FEAT_MBYTE | |
5781 /* | |
5782 * Convert a Unicode character to bytes. | |
1947 | 5783 * Return TRUE for an error, FALSE when it's OK. |
7 | 5784 */ |
5785 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5786 ucs2bytes( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5787 unsigned c, /* in: character */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5788 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
|
5789 int flags) /* FIO_ flags */ |
7 | 5790 { |
5791 char_u *p = *pp; | |
5792 int error = FALSE; | |
5793 int cc; | |
5794 | |
5795 | |
5796 if (flags & FIO_UCS4) | |
5797 { | |
5798 if (flags & FIO_ENDIAN_L) | |
5799 { | |
5800 *p++ = c; | |
5801 *p++ = (c >> 8); | |
5802 *p++ = (c >> 16); | |
5803 *p++ = (c >> 24); | |
5804 } | |
5805 else | |
5806 { | |
5807 *p++ = (c >> 24); | |
5808 *p++ = (c >> 16); | |
5809 *p++ = (c >> 8); | |
5810 *p++ = c; | |
5811 } | |
5812 } | |
5813 else if (flags & (FIO_UCS2 | FIO_UTF16)) | |
5814 { | |
5815 if (c >= 0x10000) | |
5816 { | |
5817 if (flags & FIO_UTF16) | |
5818 { | |
5819 /* Make two words, ten bits of the character in each. First | |
5820 * word is 0xd800 - 0xdbff, second one 0xdc00 - 0xdfff */ | |
5821 c -= 0x10000; | |
5822 if (c >= 0x100000) | |
5823 error = TRUE; | |
5824 cc = ((c >> 10) & 0x3ff) + 0xd800; | |
5825 if (flags & FIO_ENDIAN_L) | |
5826 { | |
5827 *p++ = cc; | |
5828 *p++ = ((unsigned)cc >> 8); | |
5829 } | |
5830 else | |
5831 { | |
5832 *p++ = ((unsigned)cc >> 8); | |
5833 *p++ = cc; | |
5834 } | |
5835 c = (c & 0x3ff) + 0xdc00; | |
5836 } | |
5837 else | |
5838 error = TRUE; | |
5839 } | |
5840 if (flags & FIO_ENDIAN_L) | |
5841 { | |
5842 *p++ = c; | |
5843 *p++ = (c >> 8); | |
5844 } | |
5845 else | |
5846 { | |
5847 *p++ = (c >> 8); | |
5848 *p++ = c; | |
5849 } | |
5850 } | |
5851 else /* Latin1 */ | |
5852 { | |
5853 if (c >= 0x100) | |
5854 { | |
5855 error = TRUE; | |
5856 *p++ = 0xBF; | |
5857 } | |
5858 else | |
5859 *p++ = c; | |
5860 } | |
5861 | |
5862 *pp = p; | |
5863 return error; | |
5864 } | |
5865 | |
5866 /* | |
1948 | 5867 * Return TRUE if file encoding "fenc" requires conversion from or to |
5868 * 'encoding'. | |
7 | 5869 */ |
5870 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5871 need_conversion(char_u *fenc) |
7 | 5872 { |
1948 | 5873 int same_encoding; |
5874 int enc_flags; | |
5875 int fenc_flags; | |
5876 | |
5877 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0) | |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5878 { |
1948 | 5879 same_encoding = TRUE; |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5880 fenc_flags = 0; |
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5881 } |
1948 | 5882 else |
5883 { | |
5884 /* Ignore difference between "ansi" and "latin1", "ucs-4" and | |
5885 * "ucs-4be", etc. */ | |
5886 enc_flags = get_fio_flags(p_enc); | |
5887 fenc_flags = get_fio_flags(fenc); | |
5888 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags); | |
5889 } | |
5890 if (same_encoding) | |
5891 { | |
5892 /* Specified encoding matches with 'encoding'. This requires | |
5893 * conversion when 'encoding' is Unicode but not UTF-8. */ | |
5894 return enc_unicode != 0; | |
5895 } | |
5896 | |
5897 /* Encodings differ. However, conversion is not needed when 'enc' is any | |
5898 * Unicode encoding and the file is UTF-8. */ | |
5899 return !(enc_utf8 && fenc_flags == FIO_UTF8); | |
7 | 5900 } |
5901 | |
5902 /* | |
5903 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the | |
5904 * internal conversion. | |
5905 * if "ptr" is an empty string, use 'encoding'. | |
5906 */ | |
5907 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5908 get_fio_flags(char_u *ptr) |
7 | 5909 { |
5910 int prop; | |
5911 | |
5912 if (*ptr == NUL) | |
5913 ptr = p_enc; | |
5914 | |
5915 prop = enc_canon_props(ptr); | |
5916 if (prop & ENC_UNICODE) | |
5917 { | |
5918 if (prop & ENC_2BYTE) | |
5919 { | |
5920 if (prop & ENC_ENDIAN_L) | |
5921 return FIO_UCS2 | FIO_ENDIAN_L; | |
5922 return FIO_UCS2; | |
5923 } | |
5924 if (prop & ENC_4BYTE) | |
5925 { | |
5926 if (prop & ENC_ENDIAN_L) | |
5927 return FIO_UCS4 | FIO_ENDIAN_L; | |
5928 return FIO_UCS4; | |
5929 } | |
5930 if (prop & ENC_2WORD) | |
5931 { | |
5932 if (prop & ENC_ENDIAN_L) | |
5933 return FIO_UTF16 | FIO_ENDIAN_L; | |
5934 return FIO_UTF16; | |
5935 } | |
5936 return FIO_UTF8; | |
5937 } | |
5938 if (prop & ENC_LATIN1) | |
5939 return FIO_LATIN1; | |
5940 /* must be ENC_DBCS, requires iconv() */ | |
5941 return 0; | |
5942 } | |
5943 | |
5944 #ifdef WIN3264 | |
5945 /* | |
5946 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed | |
5947 * for the conversion MS-Windows can do for us. Also accept "utf-8". | |
5948 * Used for conversion between 'encoding' and 'fileencoding'. | |
5949 */ | |
5950 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5951 get_win_fio_flags(char_u *ptr) |
7 | 5952 { |
5953 int cp; | |
5954 | |
5955 /* Cannot do this when 'encoding' is not utf-8 and not a codepage. */ | |
5956 if (!enc_utf8 && enc_codepage <= 0) | |
5957 return 0; | |
5958 | |
5959 cp = encname2codepage(ptr); | |
5960 if (cp == 0) | |
5961 { | |
5962 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
5963 if (STRCMP(ptr, "utf-8") == 0) | |
5964 cp = CP_UTF8; | |
5965 else | |
5966 # endif | |
5967 return 0; | |
5968 } | |
5969 return FIO_PUT_CP(cp) | FIO_CODEPAGE; | |
5970 } | |
5971 #endif | |
5972 | |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12698
diff
changeset
|
5973 #ifdef MACOS_CONVERT |
7 | 5974 /* |
5975 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags | |
5976 * needed for the internal conversion to/from utf-8 or latin1. | |
5977 */ | |
5978 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5979 get_mac_fio_flags(char_u *ptr) |
7 | 5980 { |
5981 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
5982 && (enc_canon_props(ptr) & ENC_MACROMAN)) | |
5983 return FIO_MACROMAN; | |
5984 return 0; | |
5985 } | |
5986 #endif | |
5987 | |
5988 /* | |
5989 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size]. | |
5990 * "size" must be at least 2. | |
5991 * Return the name of the encoding and set "*lenp" to the length. | |
5992 * Returns NULL when no BOM found. | |
5993 */ | |
5994 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5995 check_for_bom( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5996 char_u *p, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5997 long size, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5998 int *lenp, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5999 int flags) |
7 | 6000 { |
6001 char *name = NULL; | |
6002 int len = 2; | |
6003 | |
6004 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf | |
1688 | 6005 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0)) |
7 | 6006 { |
6007 name = "utf-8"; /* EF BB BF */ | |
6008 len = 3; | |
6009 } | |
6010 else if (p[0] == 0xff && p[1] == 0xfe) | |
6011 { | |
6012 if (size >= 4 && p[2] == 0 && p[3] == 0 | |
6013 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L))) | |
6014 { | |
6015 name = "ucs-4le"; /* FF FE 00 00 */ | |
6016 len = 4; | |
6017 } | |
1735 | 6018 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L)) |
7 | 6019 name = "ucs-2le"; /* FF FE */ |
1735 | 6020 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L)) |
6021 /* utf-16le is preferred, it also works for ucs-2le text */ | |
7 | 6022 name = "utf-16le"; /* FF FE */ |
6023 } | |
6024 else if (p[0] == 0xfe && p[1] == 0xff | |
6025 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16)) | |
6026 { | |
1547 | 6027 /* Default to utf-16, it works also for ucs-2 text. */ |
6028 if (flags == FIO_UCS2) | |
6029 name = "ucs-2"; /* FE FF */ | |
6030 else | |
7 | 6031 name = "utf-16"; /* FE FF */ |
6032 } | |
6033 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe | |
6034 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4)) | |
6035 { | |
6036 name = "ucs-4"; /* 00 00 FE FF */ | |
6037 len = 4; | |
6038 } | |
6039 | |
6040 *lenp = len; | |
6041 return (char_u *)name; | |
6042 } | |
6043 | |
6044 /* | |
6045 * Generate a BOM in "buf[4]" for encoding "name". | |
6046 * Return the length of the BOM (zero when no BOM). | |
6047 */ | |
6048 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6049 make_bom(char_u *buf, char_u *name) |
7 | 6050 { |
6051 int flags; | |
6052 char_u *p; | |
6053 | |
6054 flags = get_fio_flags(name); | |
6055 | |
6056 /* Can't put a BOM in a non-Unicode file. */ | |
6057 if (flags == FIO_LATIN1 || flags == 0) | |
6058 return 0; | |
6059 | |
6060 if (flags == FIO_UTF8) /* UTF-8 */ | |
6061 { | |
6062 buf[0] = 0xef; | |
6063 buf[1] = 0xbb; | |
6064 buf[2] = 0xbf; | |
6065 return 3; | |
6066 } | |
6067 p = buf; | |
6068 (void)ucs2bytes(0xfeff, &p, flags); | |
6069 return (int)(p - buf); | |
6070 } | |
6071 #endif | |
6072 | |
1418 | 6073 #if defined(FEAT_VIMINFO) || defined(FEAT_BROWSE) || \ |
1471 | 6074 defined(FEAT_QUICKFIX) || defined(FEAT_AUTOCMD) || defined(PROTO) |
7 | 6075 /* |
6076 * Try to find a shortname by comparing the fullname with the current | |
6077 * directory. | |
1411 | 6078 * Returns "full_path" or pointer into "full_path" if shortened. |
6079 */ | |
6080 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6081 shorten_fname1(char_u *full_path) |
1411 | 6082 { |
2770 | 6083 char_u *dirname; |
1411 | 6084 char_u *p = full_path; |
6085 | |
2770 | 6086 dirname = alloc(MAXPATHL); |
6087 if (dirname == NULL) | |
6088 return full_path; | |
1411 | 6089 if (mch_dirname(dirname, MAXPATHL) == OK) |
6090 { | |
6091 p = shorten_fname(full_path, dirname); | |
6092 if (p == NULL || *p == NUL) | |
6093 p = full_path; | |
6094 } | |
2770 | 6095 vim_free(dirname); |
1411 | 6096 return p; |
6097 } | |
1418 | 6098 #endif |
1411 | 6099 |
6100 /* | |
6101 * Try to find a shortname by comparing the fullname with the current | |
6102 * directory. | |
7 | 6103 * Returns NULL if not shorter name possible, pointer into "full_path" |
6104 * otherwise. | |
6105 */ | |
6106 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6107 shorten_fname(char_u *full_path, char_u *dir_name) |
7 | 6108 { |
6109 int len; | |
6110 char_u *p; | |
6111 | |
6112 if (full_path == NULL) | |
6113 return NULL; | |
6114 len = (int)STRLEN(dir_name); | |
6115 if (fnamencmp(dir_name, full_path, len) == 0) | |
6116 { | |
6117 p = full_path + len; | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6118 #if defined(MSWIN) |
7 | 6119 /* |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6120 * MSWIN: when a file is in the root directory, dir_name will end in a |
7 | 6121 * slash, since C: by itself does not define a specific dir. In this |
6122 * case p may already be correct. <negri> | |
6123 */ | |
6124 if (!((len > 2) && (*(p - 2) == ':'))) | |
6125 #endif | |
6126 { | |
6127 if (vim_ispathsep(*p)) | |
6128 ++p; | |
6129 #ifndef VMS /* the path separator is always part of the path */ | |
6130 else | |
6131 p = NULL; | |
6132 #endif | |
6133 } | |
6134 } | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6135 #if defined(MSWIN) |
7 | 6136 /* |
6137 * When using a file in the current drive, remove the drive name: | |
6138 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on | |
6139 * a floppy from "A:\dir" to "B:\dir". | |
6140 */ | |
6141 else if (len > 3 | |
6142 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0]) | |
6143 && full_path[1] == ':' | |
6144 && vim_ispathsep(full_path[2])) | |
6145 p = full_path + 2; | |
6146 #endif | |
6147 else | |
6148 p = NULL; | |
6149 return p; | |
6150 } | |
6151 | |
6152 /* | |
6153 * Shorten filenames for all buffers. | |
6154 * When "force" is TRUE: Use full path from now on for files currently being | |
6155 * edited, both for file name and swap file name. Try to shorten the file | |
6156 * names a bit, if safe to do so. | |
6157 * When "force" is FALSE: Only try to shorten absolute file names. | |
6158 * For buffers that have buftype "nofile" or "scratch": never change the file | |
6159 * name. | |
6160 */ | |
6161 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6162 shorten_fnames(int force) |
7 | 6163 { |
6164 char_u dirname[MAXPATHL]; | |
6165 buf_T *buf; | |
6166 char_u *p; | |
6167 | |
6168 mch_dirname(dirname, MAXPATHL); | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
6169 FOR_ALL_BUFFERS(buf) |
7 | 6170 { |
6171 if (buf->b_fname != NULL | |
6172 #ifdef FEAT_QUICKFIX | |
6173 && !bt_nofile(buf) | |
6174 #endif | |
6175 && !path_with_url(buf->b_fname) | |
6176 && (force | |
6177 || buf->b_sfname == NULL | |
6178 || mch_isFullName(buf->b_sfname))) | |
6179 { | |
6180 vim_free(buf->b_sfname); | |
6181 buf->b_sfname = NULL; | |
6182 p = shorten_fname(buf->b_ffname, dirname); | |
6183 if (p != NULL) | |
6184 { | |
6185 buf->b_sfname = vim_strsave(p); | |
6186 buf->b_fname = buf->b_sfname; | |
6187 } | |
6188 if (p == NULL || buf->b_fname == NULL) | |
6189 buf->b_fname = buf->b_ffname; | |
9 | 6190 } |
6191 | |
6192 /* Always make the swap file name a full path, a "nofile" buffer may | |
6193 * also have a swap file. */ | |
6194 mf_fullname(buf->b_ml.ml_mfp); | |
7 | 6195 } |
6196 status_redraw_all(); | |
672 | 6197 redraw_tabline = TRUE; |
7 | 6198 } |
6199 | |
6200 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \ | |
6201 || defined(FEAT_GUI_MSWIN) \ | |
6202 || defined(FEAT_GUI_MAC) \ | |
6203 || defined(PROTO) | |
6204 /* | |
6205 * Shorten all filenames in "fnames[count]" by current directory. | |
6206 */ | |
6207 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6208 shorten_filenames(char_u **fnames, int count) |
7 | 6209 { |
6210 int i; | |
6211 char_u dirname[MAXPATHL]; | |
6212 char_u *p; | |
6213 | |
6214 if (fnames == NULL || count < 1) | |
6215 return; | |
6216 mch_dirname(dirname, sizeof(dirname)); | |
6217 for (i = 0; i < count; ++i) | |
6218 { | |
6219 if ((p = shorten_fname(fnames[i], dirname)) != NULL) | |
6220 { | |
6221 /* shorten_fname() returns pointer in given "fnames[i]". If free | |
6222 * "fnames[i]" first, "p" becomes invalid. So we need to copy | |
6223 * "p" first then free fnames[i]. */ | |
6224 p = vim_strsave(p); | |
6225 vim_free(fnames[i]); | |
6226 fnames[i] = p; | |
6227 } | |
6228 } | |
6229 } | |
6230 #endif | |
6231 | |
6232 /* | |
1651 | 6233 * add extension to file name - change path/fo.o.h to path/fo.o.h.ext or |
7 | 6234 * fo_o_h.ext for MSDOS or when shortname option set. |
6235 * | |
6236 * Assumed that fname is a valid name found in the filesystem we assure that | |
6237 * the return value is a different name and ends in 'ext'. | |
6238 * "ext" MUST be at most 4 characters long if it starts with a dot, 3 | |
6239 * characters otherwise. | |
6240 * Space for the returned name is allocated, must be freed later. | |
6241 * Returns NULL when out of memory. | |
6242 */ | |
6243 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6244 modname( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6245 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6246 char_u *ext, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6247 int prepend_dot) /* may prepend a '.' to file name */ |
7 | 6248 { |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
6249 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname), |
7 | 6250 fname, ext, prepend_dot); |
6251 } | |
6252 | |
6253 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6254 buf_modname( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6255 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
|
6256 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6257 char_u *ext, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6258 int prepend_dot) /* may prepend a '.' to file name */ |
7 | 6259 { |
6260 char_u *retval; | |
6261 char_u *s; | |
6262 char_u *e; | |
6263 char_u *ptr; | |
6264 int fnamelen, extlen; | |
6265 | |
6266 extlen = (int)STRLEN(ext); | |
6267 | |
6268 /* | |
6269 * If there is no file name we must get the name of the current directory | |
6270 * (we need the full path in case :cd is used). | |
6271 */ | |
6272 if (fname == NULL || *fname == NUL) | |
6273 { | |
6274 retval = alloc((unsigned)(MAXPATHL + extlen + 3)); | |
6275 if (retval == NULL) | |
6276 return NULL; | |
6277 if (mch_dirname(retval, MAXPATHL) == FAIL || | |
6278 (fnamelen = (int)STRLEN(retval)) == 0) | |
6279 { | |
6280 vim_free(retval); | |
6281 return NULL; | |
6282 } | |
39 | 6283 if (!after_pathsep(retval, retval + fnamelen)) |
7 | 6284 { |
6285 retval[fnamelen++] = PATHSEP; | |
6286 retval[fnamelen] = NUL; | |
6287 } | |
6288 prepend_dot = FALSE; /* nothing to prepend a dot to */ | |
6289 } | |
6290 else | |
6291 { | |
6292 fnamelen = (int)STRLEN(fname); | |
6293 retval = alloc((unsigned)(fnamelen + extlen + 3)); | |
6294 if (retval == NULL) | |
6295 return NULL; | |
6296 STRCPY(retval, fname); | |
6297 #ifdef VMS | |
6298 vms_remove_version(retval); /* we do not need versions here */ | |
6299 #endif | |
6300 } | |
6301 | |
6302 /* | |
6303 * search backwards until we hit a '/', '\' or ':' replacing all '.' | |
6304 * by '_' for MSDOS or when shortname option set and ext starts with a dot. | |
6305 * Then truncate what is after the '/', '\' or ':' to 8 characters for | |
6306 * MSDOS and 26 characters for AMIGA, a lot more for UNIX. | |
6307 */ | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
6308 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr)) |
7 | 6309 { |
6310 if (*ext == '.' | |
2823 | 6311 #ifdef USE_LONG_FNAME |
7 | 6312 && (!USE_LONG_FNAME || shortname) |
2823 | 6313 #else |
7 | 6314 && shortname |
2823 | 6315 #endif |
7 | 6316 ) |
6317 if (*ptr == '.') /* replace '.' by '_' */ | |
6318 *ptr = '_'; | |
6319 if (vim_ispathsep(*ptr)) | |
391 | 6320 { |
6321 ++ptr; | |
7 | 6322 break; |
391 | 6323 } |
6324 } | |
7 | 6325 |
6326 /* the file name has at most BASENAMELEN characters. */ | |
6327 if (STRLEN(ptr) > (unsigned)BASENAMELEN) | |
6328 ptr[BASENAMELEN] = '\0'; | |
6329 | |
6330 s = ptr + STRLEN(ptr); | |
6331 | |
6332 /* | |
6333 * For 8.3 file names we may have to reduce the length. | |
6334 */ | |
6335 #ifdef USE_LONG_FNAME | |
6336 if (!USE_LONG_FNAME || shortname) | |
6337 #else | |
6338 if (shortname) | |
6339 #endif | |
6340 { | |
6341 /* | |
6342 * If there is no file name, or the file name ends in '/', and the | |
6343 * extension starts with '.', put a '_' before the dot, because just | |
6344 * ".ext" is invalid. | |
6345 */ | |
6346 if (fname == NULL || *fname == NUL | |
6347 || vim_ispathsep(fname[STRLEN(fname) - 1])) | |
6348 { | |
6349 if (*ext == '.') | |
6350 *s++ = '_'; | |
6351 } | |
6352 /* | |
6353 * If the extension starts with '.', truncate the base name at 8 | |
6354 * characters | |
6355 */ | |
6356 else if (*ext == '.') | |
6357 { | |
1877 | 6358 if ((size_t)(s - ptr) > (size_t)8) |
7 | 6359 { |
6360 s = ptr + 8; | |
6361 *s = '\0'; | |
6362 } | |
6363 } | |
6364 /* | |
6365 * If the extension doesn't start with '.', and the file name | |
6366 * doesn't have an extension yet, append a '.' | |
6367 */ | |
6368 else if ((e = vim_strchr(ptr, '.')) == NULL) | |
6369 *s++ = '.'; | |
6370 /* | |
6371 * If the extension doesn't start with '.', and there already is an | |
1201 | 6372 * extension, it may need to be truncated |
7 | 6373 */ |
6374 else if ((int)STRLEN(e) + extlen > 4) | |
6375 s = e + 4 - extlen; | |
6376 } | |
7408
1886f2863437
commit https://github.com/vim/vim/commit/e7fedb6ebe72d9a475aa65109b77d5ed4667067a
Christian Brabandt <cb@256bit.org>
parents:
7320
diff
changeset
|
6377 #if defined(USE_LONG_FNAME) || defined(WIN3264) |
7 | 6378 /* |
6379 * If there is no file name, and the extension starts with '.', put a | |
6380 * '_' before the dot, because just ".ext" may be invalid if it's on a | |
6381 * FAT partition, and on HPFS it doesn't matter. | |
6382 */ | |
6383 else if ((fname == NULL || *fname == NUL) && *ext == '.') | |
6384 *s++ = '_'; | |
6385 #endif | |
6386 | |
6387 /* | |
1651 | 6388 * Append the extension. |
7 | 6389 * ext can start with '.' and cannot exceed 3 more characters. |
6390 */ | |
6391 STRCPY(s, ext); | |
6392 | |
6393 /* | |
6394 * Prepend the dot. | |
6395 */ | |
2823 | 6396 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.' |
7 | 6397 #ifdef USE_LONG_FNAME |
6398 && USE_LONG_FNAME | |
6399 #endif | |
6400 ) | |
6401 { | |
1620 | 6402 STRMOVE(e + 1, e); |
7 | 6403 *e = '.'; |
6404 } | |
6405 | |
6406 /* | |
6407 * Check that, after appending the extension, the file name is really | |
6408 * different. | |
6409 */ | |
6410 if (fname != NULL && STRCMP(fname, retval) == 0) | |
6411 { | |
6412 /* we search for a character that can be replaced by '_' */ | |
6413 while (--s >= ptr) | |
6414 { | |
6415 if (*s != '_') | |
6416 { | |
6417 *s = '_'; | |
6418 break; | |
6419 } | |
6420 } | |
6421 if (s < ptr) /* fname was "________.<ext>", how tricky! */ | |
6422 *ptr = 'v'; | |
6423 } | |
6424 return retval; | |
6425 } | |
6426 | |
6427 /* | |
6428 * Like fgets(), but if the file line is too long, it is truncated and the | |
6429 * rest of the line is thrown away. Returns TRUE for end-of-file. | |
6430 */ | |
6431 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6432 vim_fgets(char_u *buf, int size, FILE *fp) |
7 | 6433 { |
6434 char *eof; | |
6435 #define FGETS_SIZE 200 | |
6436 char tbuf[FGETS_SIZE]; | |
6437 | |
6438 buf[size - 2] = NUL; | |
6439 #ifdef USE_CR | |
6440 eof = fgets_cr((char *)buf, size, fp); | |
6441 #else | |
6442 eof = fgets((char *)buf, size, fp); | |
6443 #endif | |
6444 if (buf[size - 2] != NUL && buf[size - 2] != '\n') | |
6445 { | |
6446 buf[size - 1] = NUL; /* Truncate the line */ | |
6447 | |
6448 /* Now throw away the rest of the line: */ | |
6449 do | |
6450 { | |
6451 tbuf[FGETS_SIZE - 2] = NUL; | |
6452 #ifdef USE_CR | |
1757 | 6453 ignoredp = fgets_cr((char *)tbuf, FGETS_SIZE, fp); |
7 | 6454 #else |
1757 | 6455 ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp); |
7 | 6456 #endif |
6457 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n'); | |
6458 } | |
6459 return (eof == NULL); | |
6460 } | |
6461 | |
6462 #if defined(USE_CR) || defined(PROTO) | |
6463 /* | |
6464 * Like vim_fgets(), but accept any line terminator: CR, CR-LF or LF. | |
6465 * Returns TRUE for end-of-file. | |
6466 * Only used for the Mac, because it's much slower than vim_fgets(). | |
6467 */ | |
6468 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6469 tag_fgets(char_u *buf, int size, FILE *fp) |
7 | 6470 { |
6471 int i = 0; | |
6472 int c; | |
6473 int eof = FALSE; | |
6474 | |
6475 for (;;) | |
6476 { | |
6477 c = fgetc(fp); | |
6478 if (c == EOF) | |
6479 { | |
6480 eof = TRUE; | |
6481 break; | |
6482 } | |
6483 if (c == '\r') | |
6484 { | |
6485 /* Always store a NL for end-of-line. */ | |
6486 if (i < size - 1) | |
6487 buf[i++] = '\n'; | |
6488 c = fgetc(fp); | |
6489 if (c != '\n') /* Macintosh format: single CR. */ | |
6490 ungetc(c, fp); | |
6491 break; | |
6492 } | |
6493 if (i < size - 1) | |
6494 buf[i++] = c; | |
6495 if (c == '\n') | |
6496 break; | |
6497 } | |
6498 buf[i] = NUL; | |
6499 return eof; | |
6500 } | |
6501 #endif | |
6502 | |
6503 /* | |
6504 * rename() only works if both files are on the same file system, this | |
6505 * function will (attempts to?) copy the file across if rename fails -- webb | |
6506 * Return -1 for failure, 0 for success. | |
6507 */ | |
6508 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6509 vim_rename(char_u *from, char_u *to) |
7 | 6510 { |
6511 int fd_in; | |
6512 int fd_out; | |
6513 int n; | |
6514 char *errmsg = NULL; | |
6515 char *buffer; | |
6516 #ifdef AMIGA | |
6517 BPTR flock; | |
6518 #endif | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6519 stat_T st; |
194 | 6520 long perm; |
199 | 6521 #ifdef HAVE_ACL |
6522 vim_acl_T acl; /* ACL from original file */ | |
6523 #endif | |
1779 | 6524 int use_tmp_file = FALSE; |
7 | 6525 |
6526 /* | |
1779 | 6527 * When the names are identical, there is nothing to do. When they refer |
6528 * to the same file (ignoring case and slash/backslash differences) but | |
6529 * the file name differs we need to go through a temp file. | |
7 | 6530 */ |
6531 if (fnamecmp(from, to) == 0) | |
1779 | 6532 { |
4242 | 6533 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0) |
1779 | 6534 use_tmp_file = TRUE; |
6535 else | |
6536 return 0; | |
6537 } | |
7 | 6538 |
6539 /* | |
6540 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted. | |
6541 */ | |
6542 if (mch_stat((char *)from, &st) < 0) | |
6543 return -1; | |
6544 | |
1778 | 6545 #ifdef UNIX |
6546 { | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6547 stat_T st_to; |
1778 | 6548 |
6549 /* It's possible for the source and destination to be the same file. | |
6550 * This happens when "from" and "to" differ in case and are on a FAT32 | |
6551 * filesystem. In that case go through a temp file name. */ | |
6552 if (mch_stat((char *)to, &st_to) >= 0 | |
6553 && st.st_dev == st_to.st_dev | |
6554 && st.st_ino == st_to.st_ino) | |
1779 | 6555 use_tmp_file = TRUE; |
6556 } | |
6557 #endif | |
2793 | 6558 #ifdef WIN3264 |
6559 { | |
6560 BY_HANDLE_FILE_INFORMATION info1, info2; | |
6561 | |
6562 /* It's possible for the source and destination to be the same file. | |
6563 * In that case go through a temp file name. This makes rename("foo", | |
6564 * "./foo") a no-op (in a complicated way). */ | |
6565 if (win32_fileinfo(from, &info1) == FILEINFO_OK | |
6566 && win32_fileinfo(to, &info2) == FILEINFO_OK | |
6567 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber | |
6568 && info1.nFileIndexHigh == info2.nFileIndexHigh | |
6569 && info1.nFileIndexLow == info2.nFileIndexLow) | |
6570 use_tmp_file = TRUE; | |
6571 } | |
6572 #endif | |
1779 | 6573 |
6574 if (use_tmp_file) | |
6575 { | |
6576 char tempname[MAXPATHL + 1]; | |
6577 | |
6578 /* | |
6579 * Find a name that doesn't exist and is in the same directory. | |
6580 * Rename "from" to "tempname" and then rename "tempname" to "to". | |
6581 */ | |
6582 if (STRLEN(from) >= MAXPATHL - 5) | |
6583 return -1; | |
6584 STRCPY(tempname, from); | |
6585 for (n = 123; n < 99999; ++n) | |
6586 { | |
6587 sprintf((char *)gettail((char_u *)tempname), "%d", n); | |
6588 if (mch_stat(tempname, &st) < 0) | |
6589 { | |
6590 if (mch_rename((char *)from, tempname) == 0) | |
1778 | 6591 { |
1779 | 6592 if (mch_rename(tempname, (char *)to) == 0) |
6593 return 0; | |
6594 /* Strange, the second step failed. Try moving the | |
6595 * file back and return failure. */ | |
6596 mch_rename(tempname, (char *)from); | |
1778 | 6597 return -1; |
6598 } | |
1779 | 6599 /* If it fails for one temp name it will most likely fail |
6600 * for any temp name, give up. */ | |
6601 return -1; | |
6602 } | |
6603 } | |
6604 return -1; | |
1778 | 6605 } |
6606 | |
7 | 6607 /* |
6608 * Delete the "to" file, this is required on some systems to make the | |
6609 * mch_rename() work, on other systems it makes sure that we don't have | |
6610 * two files when the mch_rename() fails. | |
6611 */ | |
6612 | |
6613 #ifdef AMIGA | |
6614 /* | |
6615 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible | |
6616 * that the name of the "to" file is the same as the "from" file, even | |
1201 | 6617 * though the names are different. To avoid the chance of accidentally |
7 | 6618 * deleting the "from" file (horror!) we lock it during the remove. |
6619 * | |
6620 * When used for making a backup before writing the file: This should not | |
6621 * happen with ":w", because startscript() should detect this problem and | |
6622 * set buf->b_shortname, causing modname() to return a correct ".bak" file | |
6623 * name. This problem does exist with ":w filename", but then the | |
6624 * original file will be somewhere else so the backup isn't really | |
6625 * important. If autoscripting is off the rename may fail. | |
6626 */ | |
6627 flock = Lock((UBYTE *)from, (long)ACCESS_READ); | |
6628 #endif | |
6629 mch_remove(to); | |
6630 #ifdef AMIGA | |
6631 if (flock) | |
6632 UnLock(flock); | |
6633 #endif | |
6634 | |
6635 /* | |
6636 * First try a normal rename, return if it works. | |
6637 */ | |
6638 if (mch_rename((char *)from, (char *)to) == 0) | |
6639 return 0; | |
6640 | |
6641 /* | |
6642 * Rename() failed, try copying the file. | |
6643 */ | |
194 | 6644 perm = mch_getperm(from); |
199 | 6645 #ifdef HAVE_ACL |
6646 /* For systems that support ACL: get the ACL from the original file. */ | |
6647 acl = mch_get_acl(from); | |
6648 #endif | |
7 | 6649 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0); |
6650 if (fd_in == -1) | |
1651 | 6651 { |
6652 #ifdef HAVE_ACL | |
6653 mch_free_acl(acl); | |
6654 #endif | |
7 | 6655 return -1; |
1651 | 6656 } |
194 | 6657 |
6658 /* Create the new file with same permissions as the original. */ | |
557 | 6659 fd_out = mch_open((char *)to, |
6660 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm); | |
7 | 6661 if (fd_out == -1) |
6662 { | |
6663 close(fd_in); | |
1651 | 6664 #ifdef HAVE_ACL |
6665 mch_free_acl(acl); | |
6666 #endif | |
7 | 6667 return -1; |
6668 } | |
6669 | |
6670 buffer = (char *)alloc(BUFSIZE); | |
6671 if (buffer == NULL) | |
6672 { | |
1651 | 6673 close(fd_out); |
7 | 6674 close(fd_in); |
1651 | 6675 #ifdef HAVE_ACL |
6676 mch_free_acl(acl); | |
6677 #endif | |
7 | 6678 return -1; |
6679 } | |
6680 | |
2664 | 6681 while ((n = read_eintr(fd_in, buffer, BUFSIZE)) > 0) |
6682 if (write_eintr(fd_out, buffer, n) != n) | |
7 | 6683 { |
6684 errmsg = _("E208: Error writing to \"%s\""); | |
6685 break; | |
6686 } | |
6687 | |
6688 vim_free(buffer); | |
6689 close(fd_in); | |
6690 if (close(fd_out) < 0) | |
6691 errmsg = _("E209: Error closing \"%s\""); | |
6692 if (n < 0) | |
6693 { | |
6694 errmsg = _("E210: Error reading \"%s\""); | |
6695 to = from; | |
6696 } | |
1201 | 6697 #ifndef UNIX /* for Unix mch_open() already set the permission */ |
194 | 6698 mch_setperm(to, perm); |
570 | 6699 #endif |
199 | 6700 #ifdef HAVE_ACL |
6701 mch_set_acl(to, acl); | |
1651 | 6702 mch_free_acl(acl); |
199 | 6703 #endif |
5788 | 6704 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
5483 | 6705 mch_copy_sec(from, to); |
5479 | 6706 #endif |
7 | 6707 if (errmsg != NULL) |
6708 { | |
6709 EMSG2(errmsg, to); | |
6710 return -1; | |
6711 } | |
6712 mch_remove(from); | |
6713 return 0; | |
6714 } | |
6715 | |
6716 static int already_warned = FALSE; | |
6717 | |
6718 /* | |
6719 * Check if any not hidden buffer has been changed. | |
6720 * Postpone the check if there are characters in the stuff buffer, a global | |
6721 * command is being executed, a mapping is being executed or an autocommand is | |
6722 * busy. | |
6723 * Returns TRUE if some message was written (screen should be redrawn and | |
6724 * cursor positioned). | |
6725 */ | |
6726 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6727 check_timestamps( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6728 int focus) /* called for GUI focus event */ |
7 | 6729 { |
6730 buf_T *buf; | |
6731 int didit = 0; | |
6732 int n; | |
6733 | |
6734 /* Don't check timestamps while system() or another low-level function may | |
6735 * cause us to lose and gain focus. */ | |
6736 if (no_check_timestamps > 0) | |
6737 return FALSE; | |
6738 | |
6739 /* Avoid doing a check twice. The OK/Reload dialog can cause a focus | |
6740 * event and we would keep on checking if the file is steadily growing. | |
6741 * Do check again after typing something. */ | |
6742 if (focus && did_check_timestamps) | |
6743 { | |
6744 need_check_timestamps = TRUE; | |
6745 return FALSE; | |
6746 } | |
6747 | |
6748 if (!stuff_empty() || global_busy || !typebuf_typed() | |
6749 #ifdef FEAT_AUTOCMD | |
1834 | 6750 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0 |
7 | 6751 #endif |
6752 ) | |
6753 need_check_timestamps = TRUE; /* check later */ | |
6754 else | |
6755 { | |
6756 ++no_wait_return; | |
6757 did_check_timestamps = TRUE; | |
6758 already_warned = FALSE; | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
6759 FOR_ALL_BUFFERS(buf) |
7 | 6760 { |
6761 /* Only check buffers in a window. */ | |
6762 if (buf->b_nwindows > 0) | |
6763 { | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6764 bufref_T bufref; |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6765 |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6766 set_bufref(&bufref, buf); |
7 | 6767 n = buf_check_timestamp(buf, focus); |
6768 if (didit < n) | |
6769 didit = n; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6770 if (n > 0 && !bufref_valid(&bufref)) |
7 | 6771 { |
6772 /* Autocommands have removed the buffer, start at the | |
6773 * first one again. */ | |
6774 buf = firstbuf; | |
6775 continue; | |
6776 } | |
6777 } | |
6778 } | |
6779 --no_wait_return; | |
6780 need_check_timestamps = FALSE; | |
6781 if (need_wait_return && didit == 2) | |
6782 { | |
6783 /* make sure msg isn't overwritten */ | |
6784 msg_puts((char_u *)"\n"); | |
6785 out_flush(); | |
6786 } | |
6787 } | |
6788 return didit; | |
6789 } | |
6790 | |
6791 /* | |
6792 * Move all the lines from buffer "frombuf" to buffer "tobuf". | |
6793 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not | |
6794 * empty. | |
6795 */ | |
6796 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6797 move_lines(buf_T *frombuf, buf_T *tobuf) |
7 | 6798 { |
6799 buf_T *tbuf = curbuf; | |
6800 int retval = OK; | |
6801 linenr_T lnum; | |
6802 char_u *p; | |
6803 | |
6804 /* Copy the lines in "frombuf" to "tobuf". */ | |
6805 curbuf = tobuf; | |
6806 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum) | |
6807 { | |
6808 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE)); | |
6809 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL) | |
6810 { | |
6811 vim_free(p); | |
6812 retval = FAIL; | |
6813 break; | |
6814 } | |
6815 vim_free(p); | |
6816 } | |
6817 | |
6818 /* Delete all the lines in "frombuf". */ | |
6819 if (retval != FAIL) | |
6820 { | |
6821 curbuf = frombuf; | |
1055 | 6822 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum) |
6823 if (ml_delete(lnum, FALSE) == FAIL) | |
7 | 6824 { |
6825 /* Oops! We could try putting back the saved lines, but that | |
6826 * might fail again... */ | |
6827 retval = FAIL; | |
6828 break; | |
6829 } | |
6830 } | |
6831 | |
6832 curbuf = tbuf; | |
6833 return retval; | |
6834 } | |
6835 | |
6836 /* | |
6837 * Check if buffer "buf" has been changed. | |
6838 * Also check if the file for a new buffer unexpectedly appeared. | |
6839 * return 1 if a changed buffer was found. | |
6840 * return 2 if a message has been displayed. | |
6841 * return 0 otherwise. | |
6842 */ | |
6843 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6844 buf_check_timestamp( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6845 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
6846 int focus UNUSED) /* called for GUI focus event */ |
7 | 6847 { |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6848 stat_T st; |
7 | 6849 int stat_res; |
6850 int retval = 0; | |
6851 char_u *path; | |
6852 char_u *tbuf; | |
6853 char *mesg = NULL; | |
188 | 6854 char *mesg2 = ""; |
7 | 6855 int helpmesg = FALSE; |
6856 int reload = FALSE; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6857 char *reason; |
7 | 6858 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
6859 int can_reload = FALSE; | |
6860 #endif | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
6861 off_T orig_size = buf->b_orig_size; |
7 | 6862 int orig_mode = buf->b_orig_mode; |
6863 #ifdef FEAT_GUI | |
6864 int save_mouse_correct = need_mouse_correct; | |
6865 #endif | |
6866 #ifdef FEAT_AUTOCMD | |
6867 static int busy = FALSE; | |
179 | 6868 int n; |
6869 char_u *s; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6870 bufref_T bufref; |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6871 |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6872 set_bufref(&bufref, buf); |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6873 #endif |
7 | 6874 |
6875 /* If there is no file name, the buffer is not loaded, 'buftype' is | |
6876 * set, we are in the middle of a save or being called recursively: ignore | |
6877 * this buffer. */ | |
6878 if (buf->b_ffname == NULL | |
6879 || buf->b_ml.ml_mfp == NULL | |
6880 || *buf->b_p_bt != NUL | |
6881 || buf->b_saving | |
6882 #ifdef FEAT_AUTOCMD | |
6883 || busy | |
6884 #endif | |
33 | 6885 #ifdef FEAT_NETBEANS_INTG |
6886 || isNetbeansBuffer(buf) | |
6887 #endif | |
12064
407a475c67fd
patch 8.0.0912: cannot run a job in a hidden terminal
Christian Brabandt <cb@256bit.org>
parents:
11757
diff
changeset
|
6888 #ifdef FEAT_TERMINAL |
407a475c67fd
patch 8.0.0912: cannot run a job in a hidden terminal
Christian Brabandt <cb@256bit.org>
parents:
11757
diff
changeset
|
6889 || buf->b_term != NULL |
407a475c67fd
patch 8.0.0912: cannot run a job in a hidden terminal
Christian Brabandt <cb@256bit.org>
parents:
11757
diff
changeset
|
6890 #endif |
7 | 6891 ) |
6892 return 0; | |
6893 | |
6894 if ( !(buf->b_flags & BF_NOTEDITED) | |
6895 && buf->b_mtime != 0 | |
6896 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0 | |
6897 || time_differs((long)st.st_mtime, buf->b_mtime) | |
5863 | 6898 || st.st_size != buf->b_orig_size |
7 | 6899 #ifdef HAVE_ST_MODE |
6900 || (int)st.st_mode != buf->b_orig_mode | |
6901 #else | |
6902 || mch_getperm(buf->b_ffname) != buf->b_orig_mode | |
6903 #endif | |
6904 )) | |
6905 { | |
6906 retval = 1; | |
6907 | |
628 | 6908 /* set b_mtime to stop further warnings (e.g., when executing |
6909 * FileChangedShell autocmd) */ | |
7 | 6910 if (stat_res < 0) |
6911 { | |
6912 buf->b_mtime = 0; | |
6913 buf->b_orig_size = 0; | |
6914 buf->b_orig_mode = 0; | |
6915 } | |
6916 else | |
6917 buf_store_time(buf, &st, buf->b_ffname); | |
6918 | |
6919 /* Don't do anything for a directory. Might contain the file | |
6920 * explorer. */ | |
6921 if (mch_isdir(buf->b_fname)) | |
6922 ; | |
6923 | |
6924 /* | |
6925 * If 'autoread' is set, the buffer has no changes and the file still | |
6926 * exists, reload the buffer. Use the buffer-local option value if it | |
6927 * was set, the global option value otherwise. | |
6928 */ | |
6929 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar) | |
6930 && !bufIsChanged(buf) && stat_res >= 0) | |
6931 reload = TRUE; | |
6932 else | |
6933 { | |
179 | 6934 if (stat_res < 0) |
6935 reason = "deleted"; | |
6936 else if (bufIsChanged(buf)) | |
6937 reason = "conflict"; | |
6938 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf)) | |
6939 reason = "changed"; | |
6940 else if (orig_mode != buf->b_orig_mode) | |
6941 reason = "mode"; | |
6942 else | |
6943 reason = "time"; | |
6944 | |
7 | 6945 #ifdef FEAT_AUTOCMD |
6946 /* | |
6947 * Only give the warning if there are no FileChangedShell | |
6948 * autocommands. | |
6949 * Avoid being called recursively by setting "busy". | |
6950 */ | |
6951 busy = TRUE; | |
532 | 6952 # ifdef FEAT_EVAL |
179 | 6953 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1); |
6954 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1); | |
532 | 6955 # endif |
1834 | 6956 ++allbuf_lock; |
7 | 6957 n = apply_autocmds(EVENT_FILECHANGEDSHELL, |
6958 buf->b_fname, buf->b_fname, FALSE, buf); | |
1834 | 6959 --allbuf_lock; |
7 | 6960 busy = FALSE; |
6961 if (n) | |
6962 { | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
6963 if (!bufref_valid(&bufref)) |
7 | 6964 EMSG(_("E246: FileChangedShell autocommand deleted buffer")); |
532 | 6965 # ifdef FEAT_EVAL |
179 | 6966 s = get_vim_var_str(VV_FCS_CHOICE); |
6967 if (STRCMP(s, "reload") == 0 && *reason != 'd') | |
6968 reload = TRUE; | |
6969 else if (STRCMP(s, "ask") == 0) | |
6970 n = FALSE; | |
6971 else | |
532 | 6972 # endif |
179 | 6973 return 2; |
7 | 6974 } |
179 | 6975 if (!n) |
7 | 6976 #endif |
6977 { | |
179 | 6978 if (*reason == 'd') |
6979 mesg = _("E211: File \"%s\" no longer available"); | |
7 | 6980 else |
6981 { | |
6982 helpmesg = TRUE; | |
6983 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
6984 can_reload = TRUE; | |
6985 #endif | |
6986 /* | |
6987 * Check if the file contents really changed to avoid | |
6988 * giving a warning when only the timestamp was set (e.g., | |
6989 * checked out of CVS). Always warn when the buffer was | |
6990 * changed. | |
6991 */ | |
179 | 6992 if (reason[2] == 'n') |
6993 { | |
7 | 6994 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well"); |
179 | 6995 mesg2 = _("See \":help W12\" for more info."); |
6996 } | |
6997 else if (reason[1] == 'h') | |
6998 { | |
7 | 6999 mesg = _("W11: Warning: File \"%s\" has changed since editing started"); |
179 | 7000 mesg2 = _("See \":help W11\" for more info."); |
7001 } | |
7002 else if (*reason == 'm') | |
7003 { | |
7 | 7004 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started"); |
179 | 7005 mesg2 = _("See \":help W16\" for more info."); |
7006 } | |
1913 | 7007 else |
7008 /* Only timestamp changed, store it to avoid a warning | |
7009 * in check_mtime() later. */ | |
7010 buf->b_mtime_read = buf->b_mtime; | |
7 | 7011 } |
7012 } | |
7013 } | |
7014 | |
7015 } | |
7016 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W) | |
7017 && vim_fexists(buf->b_ffname)) | |
7018 { | |
7019 retval = 1; | |
7020 mesg = _("W13: Warning: File \"%s\" has been created after editing started"); | |
7021 buf->b_flags |= BF_NEW_W; | |
7022 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
7023 can_reload = TRUE; | |
7024 #endif | |
7025 } | |
7026 | |
7027 if (mesg != NULL) | |
7028 { | |
7029 path = home_replace_save(buf, buf->b_fname); | |
7030 if (path != NULL) | |
7031 { | |
179 | 7032 if (!helpmesg) |
7 | 7033 mesg2 = ""; |
7034 tbuf = alloc((unsigned)(STRLEN(path) + STRLEN(mesg) | |
7035 + STRLEN(mesg2) + 2)); | |
7036 sprintf((char *)tbuf, mesg, path); | |
1848 | 7037 #ifdef FEAT_EVAL |
7038 /* Set warningmsg here, before the unimportant and output-specific | |
7039 * mesg2 has been appended. */ | |
7040 set_vim_var_string(VV_WARNINGMSG, tbuf, -1); | |
7041 #endif | |
7 | 7042 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
7043 if (can_reload) | |
7044 { | |
7045 if (*mesg2 != NUL) | |
7046 { | |
7047 STRCAT(tbuf, "\n"); | |
7048 STRCAT(tbuf, mesg2); | |
7049 } | |
7050 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"), tbuf, | |
2684 | 7051 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2) |
7 | 7052 reload = TRUE; |
7053 } | |
7054 else | |
7055 #endif | |
7056 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned) | |
7057 { | |
7058 if (*mesg2 != NUL) | |
7059 { | |
7060 STRCAT(tbuf, "; "); | |
7061 STRCAT(tbuf, mesg2); | |
7062 } | |
7063 EMSG(tbuf); | |
7064 retval = 2; | |
7065 } | |
7066 else | |
7067 { | |
8 | 7068 # ifdef FEAT_AUTOCMD |
7 | 7069 if (!autocmd_busy) |
8 | 7070 # endif |
7 | 7071 { |
7072 msg_start(); | |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
7073 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST); |
7 | 7074 if (*mesg2 != NUL) |
7075 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
|
7076 HL_ATTR(HLF_W) + MSG_HIST); |
7 | 7077 msg_clr_eos(); |
7078 (void)msg_end(); | |
7079 if (emsg_silent == 0) | |
7080 { | |
7081 out_flush(); | |
8 | 7082 # ifdef FEAT_GUI |
7 | 7083 if (!focus) |
8 | 7084 # endif |
7 | 7085 /* give the user some time to think about it */ |
7086 ui_delay(1000L, TRUE); | |
7087 | |
7088 /* don't redraw and erase the message */ | |
7089 redraw_cmdline = FALSE; | |
7090 } | |
7091 } | |
7092 already_warned = TRUE; | |
7093 } | |
7094 | |
7095 vim_free(path); | |
7096 vim_free(tbuf); | |
7097 } | |
7098 } | |
7099 | |
7100 if (reload) | |
3776 | 7101 { |
311 | 7102 /* Reload the buffer. */ |
628 | 7103 buf_reload(buf, orig_mode); |
3776 | 7104 #ifdef FEAT_PERSISTENT_UNDO |
7105 if (buf->b_p_udf && buf->b_ffname != NULL) | |
7106 { | |
7107 char_u hash[UNDO_HASH_SIZE]; | |
7108 buf_T *save_curbuf = curbuf; | |
7109 | |
7110 /* Any existing undo file is unusable, write it now. */ | |
7111 curbuf = buf; | |
7112 u_compute_hash(hash); | |
7113 u_write_undo(NULL, FALSE, buf, hash); | |
7114 curbuf = save_curbuf; | |
7115 } | |
7116 #endif | |
7117 } | |
7 | 7118 |
765 | 7119 #ifdef FEAT_AUTOCMD |
1620 | 7120 /* 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
|
7121 if (bufref_valid(&bufref) && retval != 0) |
765 | 7122 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, |
7123 buf->b_fname, buf->b_fname, FALSE, buf); | |
7124 #endif | |
7 | 7125 #ifdef FEAT_GUI |
7126 /* restore this in case an autocommand has set it; it would break | |
7127 * 'mousefocus' */ | |
7128 need_mouse_correct = save_mouse_correct; | |
7129 #endif | |
7130 | |
7131 return retval; | |
7132 } | |
7133 | |
311 | 7134 /* |
7135 * Reload a buffer that is already loaded. | |
7136 * Used when the file was changed outside of Vim. | |
628 | 7137 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected. |
7138 * buf->b_orig_mode may have been reset already. | |
311 | 7139 */ |
7140 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7141 buf_reload(buf_T *buf, int orig_mode) |
311 | 7142 { |
7143 exarg_T ea; | |
7144 pos_T old_cursor; | |
7145 linenr_T old_topline; | |
7146 int old_ro = buf->b_p_ro; | |
7147 buf_T *savebuf; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
7148 bufref_T bufref; |
311 | 7149 int saved = OK; |
7150 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
|
7151 int flags = READ_NEW; |
311 | 7152 |
7153 /* set curwin/curbuf for "buf" and save some things */ | |
7154 aucmd_prepbuf(&aco, buf); | |
7155 | |
7156 /* We only want to read the text from the file, not reset the syntax | |
7157 * highlighting, clear marks, diff status, etc. Force the fileformat | |
7158 * and encoding to be the same. */ | |
7159 if (prep_exarg(&ea, buf) == OK) | |
7160 { | |
7161 old_cursor = curwin->w_cursor; | |
7162 old_topline = curwin->w_topline; | |
7163 | |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
7164 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
|
7165 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7166 /* 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
|
7167 * 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
|
7168 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
|
7169 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
|
7170 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
|
7171 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7172 |
311 | 7173 /* |
7174 * To behave like when a new file is edited (matters for | |
7175 * BufReadPost autocommands) we first need to delete the current | |
7176 * buffer contents. But if reading the file fails we should keep | |
7177 * the old contents. Can't use memory only, the file might be | |
7178 * too big. Use a hidden buffer to move the buffer contents to. | |
7179 */ | |
11121
778c10516955
patch 8.0.0448: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
10952
diff
changeset
|
7180 if (BUFEMPTY() || saved == FAIL) |
311 | 7181 savebuf = NULL; |
7182 else | |
7183 { | |
7184 /* Allocate a buffer without putting it in the buffer list. */ | |
7185 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
|
7186 set_bufref(&bufref, savebuf); |
837 | 7187 if (savebuf != NULL && buf == curbuf) |
311 | 7188 { |
7189 /* Open the memline. */ | |
7190 curbuf = savebuf; | |
7191 curwin->w_buffer = savebuf; | |
625 | 7192 saved = ml_open(curbuf); |
311 | 7193 curbuf = buf; |
7194 curwin->w_buffer = buf; | |
7195 } | |
837 | 7196 if (savebuf == NULL || saved == FAIL || buf != curbuf |
311 | 7197 || move_lines(buf, savebuf) == FAIL) |
7198 { | |
7199 EMSG2(_("E462: Could not prepare for reloading \"%s\""), | |
7200 buf->b_fname); | |
7201 saved = FAIL; | |
7202 } | |
7203 } | |
7204 | |
7205 if (saved == OK) | |
7206 { | |
7207 curbuf->b_flags |= BF_CHECK_RO; /* check for RO again */ | |
7208 #ifdef FEAT_AUTOCMD | |
7209 keep_filetype = TRUE; /* don't detect 'filetype' */ | |
7210 #endif | |
7211 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, | |
7212 (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
|
7213 (linenr_T)MAXLNUM, &ea, flags) != OK) |
311 | 7214 { |
7215 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
7216 if (!aborting()) | |
7217 #endif | |
7218 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
|
7219 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf) |
311 | 7220 { |
7221 /* Put the text back from the save buffer. First | |
7222 * 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
|
7223 while (!BUFEMPTY()) |
837 | 7224 if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL) |
311 | 7225 break; |
7226 (void)move_lines(savebuf, buf); | |
7227 } | |
7228 } | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7229 else if (buf == curbuf) /* "buf" still valid */ |
311 | 7230 { |
7231 /* Mark the buffer as unmodified and free undo info. */ | |
7232 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
|
7233 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
|
7234 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7235 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
|
7236 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
|
7237 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7238 else |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
7239 { |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7240 /* 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
|
7241 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
|
7242 } |
311 | 7243 } |
7244 } | |
7245 vim_free(ea.cmd); | |
7246 | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
7247 if (savebuf != NULL && bufref_valid(&bufref)) |
311 | 7248 wipe_buffer(savebuf, FALSE); |
7249 | |
7250 #ifdef FEAT_DIFF | |
7251 /* Invalidate diff info if necessary. */ | |
837 | 7252 diff_invalidate(curbuf); |
311 | 7253 #endif |
7254 | |
7255 /* Restore the topline and cursor position and check it (lines may | |
7256 * have been removed). */ | |
7257 if (old_topline > curbuf->b_ml.ml_line_count) | |
7258 curwin->w_topline = curbuf->b_ml.ml_line_count; | |
7259 else | |
7260 curwin->w_topline = old_topline; | |
7261 curwin->w_cursor = old_cursor; | |
7262 check_cursor(); | |
7263 update_topline(); | |
7264 #ifdef FEAT_AUTOCMD | |
7265 keep_filetype = FALSE; | |
7266 #endif | |
7267 #ifdef FEAT_FOLDING | |
7268 { | |
1863 | 7269 win_T *wp; |
7270 tabpage_T *tp; | |
311 | 7271 |
7272 /* Update folds unless they are defined manually. */ | |
1863 | 7273 FOR_ALL_TAB_WINDOWS(tp, wp) |
311 | 7274 if (wp->w_buffer == curwin->w_buffer |
7275 && !foldmethodIsManual(wp)) | |
7276 foldUpdateAll(wp); | |
7277 } | |
7278 #endif | |
7279 /* If the mode didn't change and 'readonly' was set, keep the old | |
7280 * value; the user probably used the ":view" command. But don't | |
7281 * reset it, might have had a read error. */ | |
7282 if (orig_mode == curbuf->b_orig_mode) | |
7283 curbuf->b_p_ro |= old_ro; | |
4071 | 7284 |
7285 /* Modelines must override settings done by autocommands. */ | |
7286 do_modelines(0); | |
311 | 7287 } |
7288 | |
7289 /* restore curwin/curbuf and a few other things */ | |
7290 aucmd_restbuf(&aco); | |
7291 /* Careful: autocommands may have made "buf" invalid! */ | |
7292 } | |
7293 | |
7 | 7294 void |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
7295 buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED) |
7 | 7296 { |
7297 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
|
7298 buf->b_orig_size = st->st_size; |
7 | 7299 #ifdef HAVE_ST_MODE |
7300 buf->b_orig_mode = (int)st->st_mode; | |
7301 #else | |
7302 buf->b_orig_mode = mch_getperm(fname); | |
7303 #endif | |
7304 } | |
7305 | |
7306 /* | |
7307 * Adjust the line with missing eol, used for the next write. | |
7308 * Used for do_filter(), when the input lines for the filter are deleted. | |
7309 */ | |
7310 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7311 write_lnum_adjust(linenr_T offset) |
7 | 7312 { |
2707 | 7313 if (curbuf->b_no_eol_lnum != 0) /* only if there is a missing eol */ |
7314 curbuf->b_no_eol_lnum += offset; | |
7 | 7315 } |
7316 | |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7317 #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
|
7318 /* |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7319 * 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
|
7320 * 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
|
7321 */ |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7322 int |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7323 delete_recursive(char_u *name) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7324 { |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7325 int result = 0; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7326 char_u **files; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7327 int file_count; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7328 int i; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7329 char_u *exp; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7330 |
7629
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7331 /* 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
|
7332 * points to. */ |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7333 if ( |
7657
9c5e8254ea6b
commit https://github.com/vim/vim/commit/203258c3ad2966cc9d08b3805b103333988b30b7
Christian Brabandt <cb@256bit.org>
parents:
7641
diff
changeset
|
7334 # if defined(UNIX) || defined(WIN32) |
9c5e8254ea6b
commit https://github.com/vim/vim/commit/203258c3ad2966cc9d08b3805b103333988b30b7
Christian Brabandt <cb@256bit.org>
parents:
7641
diff
changeset
|
7335 mch_isrealdir(name) |
7629
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7336 # else |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7337 mch_isdir(name) |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7338 # endif |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
7339 ) |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7340 { |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7341 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
|
7342 exp = vim_strsave(NameBuff); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7343 if (exp == NULL) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7344 return -1; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7345 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
|
7346 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
|
7347 { |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7348 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
|
7349 if (delete_recursive(files[i]) != 0) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7350 result = -1; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7351 FreeWild(file_count, files); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7352 } |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7353 else |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7354 result = -1; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7355 vim_free(exp); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7356 (void)mch_rmdir(name); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7357 } |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7358 else |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7359 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
|
7360 |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7361 return result; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7362 } |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7363 #endif |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7364 |
7 | 7365 #if defined(TEMPDIRNAMES) || defined(PROTO) |
7366 static long temp_count = 0; /* Temp filename counter. */ | |
7367 | |
7368 /* | |
7369 * Delete the temp directory and all files it contains. | |
7370 */ | |
7371 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7372 vim_deltempdir(void) |
7 | 7373 { |
7374 if (vim_tempdir != NULL) | |
7375 { | |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7376 /* remove the trailing path separator */ |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7377 gettail(vim_tempdir)[-1] = NUL; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7378 delete_recursive(vim_tempdir); |
7 | 7379 vim_free(vim_tempdir); |
7380 vim_tempdir = NULL; | |
7381 } | |
7382 } | |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7383 |
7 | 7384 /* |
1997 | 7385 * Directory "tempdir" was created. Expand this name to a full path and put |
7386 * it in "vim_tempdir". This avoids that using ":cd" would confuse us. | |
7387 * "tempdir" must be no longer than MAXPATHL. | |
7388 */ | |
7389 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7390 vim_settempdir(char_u *tempdir) |
1997 | 7391 { |
7392 char_u *buf; | |
7393 | |
7394 buf = alloc((unsigned)MAXPATHL + 2); | |
7395 if (buf != NULL) | |
7396 { | |
7397 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL) | |
7398 STRCPY(buf, tempdir); | |
9536
b2aada04d84e
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Christian Brabandt <cb@256bit.org>
parents:
9509
diff
changeset
|
7399 add_pathsep(buf); |
1997 | 7400 vim_tempdir = vim_strsave(buf); |
7401 vim_free(buf); | |
7402 } | |
7403 } | |
2006 | 7404 #endif |
1997 | 7405 |
7406 /* | |
7 | 7407 * vim_tempname(): Return a unique name that can be used for a temp file. |
7408 * | |
9291
bff3cd5cf922
commit https://github.com/vim/vim/commit/76ae22fef3cb224ca7fbf97517f881e825d4d0c2
Christian Brabandt <cb@256bit.org>
parents:
9260
diff
changeset
|
7409 * 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
|
7410 * guaranteed to NOT be created. |
7 | 7411 * |
7412 * The returned pointer is to allocated memory. | |
7413 * The returned pointer is NULL if no valid name was found. | |
7414 */ | |
7415 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7416 vim_tempname( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7417 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
|
7418 int keep UNUSED) |
7 | 7419 { |
7420 #ifdef USE_TMPNAM | |
7421 char_u itmp[L_tmpnam]; /* use tmpnam() */ | |
7422 #else | |
7423 char_u itmp[TEMPNAMELEN]; | |
7424 #endif | |
7425 | |
7426 #ifdef TEMPDIRNAMES | |
7427 static char *(tempdirs[]) = {TEMPDIRNAMES}; | |
7428 int i; | |
7429 # ifndef EEXIST | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
7430 stat_T st; |
7 | 7431 # endif |
7432 | |
7433 /* | |
7434 * This will create a directory for private use by this instance of Vim. | |
7435 * This is done once, and the same directory is used for all temp files. | |
7436 * This method avoids security problems because of symlink attacks et al. | |
7437 * It's also a bit faster, because we only need to check for an existing | |
7438 * file when creating the directory and not for each temp file. | |
7439 */ | |
7440 if (vim_tempdir == NULL) | |
7441 { | |
7442 /* | |
7443 * Try the entries in TEMPDIRNAMES to create the temp directory. | |
7444 */ | |
1877 | 7445 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i) |
7 | 7446 { |
2047
85da03763130
updated for version 7.2.333
Bram Moolenaar <bram@zimbu.org>
parents:
2028
diff
changeset
|
7447 # ifndef HAVE_MKDTEMP |
1997 | 7448 size_t itmplen; |
7449 long nr; | |
7450 long off; | |
7451 # endif | |
7452 | |
7305
372c785c04b6
commit https://github.com/vim/vim/commit/e1a61991d9b6fd5f65636d17583f93118268cda5
Christian Brabandt <cb@256bit.org>
parents:
7285
diff
changeset
|
7453 /* 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
|
7454 * Skip the directory check if the expansion fails. */ |
7 | 7455 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
|
7456 if (itmp[0] != '$' && mch_isdir(itmp)) |
372c785c04b6
commit https://github.com/vim/vim/commit/e1a61991d9b6fd5f65636d17583f93118268cda5
Christian Brabandt <cb@256bit.org>
parents:
7285
diff
changeset
|
7457 { |
372c785c04b6
commit https://github.com/vim/vim/commit/e1a61991d9b6fd5f65636d17583f93118268cda5
Christian Brabandt <cb@256bit.org>
parents:
7285
diff
changeset
|
7458 /* directory exists */ |
9536
b2aada04d84e
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Christian Brabandt <cb@256bit.org>
parents:
9509
diff
changeset
|
7459 add_pathsep(itmp); |
1997 | 7460 |
7461 # ifdef HAVE_MKDTEMP | |
9211
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7462 { |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7463 # if defined(UNIX) || defined(VMS) |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7464 /* 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
|
7465 * "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
|
7466 mode_t umask_save = umask(077); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7467 # endif |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7468 /* Leave room for filename */ |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7469 STRCAT(itmp, "vXXXXXX"); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7470 if (mkdtemp((char *)itmp) != NULL) |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7471 vim_settempdir(itmp); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7472 # if defined(UNIX) || defined(VMS) |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7473 (void)umask(umask_save); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7474 # endif |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
7475 } |
1997 | 7476 # else |
7 | 7477 /* Get an arbitrary number of up to 6 digits. When it's |
7478 * unlikely that it already exists it will be faster, | |
7479 * otherwise it doesn't matter. The use of mkdir() avoids any | |
7480 * security problems because of the predictable number. */ | |
7481 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
|
7482 itmplen = STRLEN(itmp); |
7 | 7483 |
7484 /* Try up to 10000 different values until we find a name that | |
7485 * doesn't exist. */ | |
7486 for (off = 0; off < 10000L; ++off) | |
7487 { | |
7488 int r; | |
1997 | 7489 # if defined(UNIX) || defined(VMS) |
7 | 7490 mode_t umask_save; |
1997 | 7491 # endif |
7492 | |
7493 sprintf((char *)itmp + itmplen, "v%ld", nr + off); | |
7494 # ifndef EEXIST | |
7 | 7495 /* If mkdir() does not set errno to EEXIST, check for |
7496 * existing file here. There is a race condition then, | |
7497 * although it's fail-safe. */ | |
7498 if (mch_stat((char *)itmp, &st) >= 0) | |
7499 continue; | |
1997 | 7500 # endif |
7501 # if defined(UNIX) || defined(VMS) | |
7 | 7502 /* Make sure the umask doesn't remove the executable bit. |
7503 * "repl" has been reported to use "177". */ | |
7504 umask_save = umask(077); | |
1997 | 7505 # endif |
7 | 7506 r = vim_mkdir(itmp, 0700); |
1997 | 7507 # if defined(UNIX) || defined(VMS) |
7 | 7508 (void)umask(umask_save); |
1997 | 7509 # endif |
7 | 7510 if (r == 0) |
7511 { | |
1997 | 7512 vim_settempdir(itmp); |
7 | 7513 break; |
7514 } | |
1997 | 7515 # ifdef EEXIST |
7 | 7516 /* If the mkdir() didn't fail because the file/dir exists, |
7517 * we probably can't create any dir here, try another | |
7518 * place. */ | |
7519 if (errno != EEXIST) | |
1997 | 7520 # endif |
7 | 7521 break; |
7522 } | |
1997 | 7523 # endif /* HAVE_MKDTEMP */ |
7 | 7524 if (vim_tempdir != NULL) |
7525 break; | |
7526 } | |
7527 } | |
7528 } | |
7529 | |
7530 if (vim_tempdir != NULL) | |
7531 { | |
7532 /* There is no need to check if the file exists, because we own the | |
7533 * directory and nobody else creates a file in it. */ | |
7534 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++); | |
7535 return vim_strsave(itmp); | |
7536 } | |
7537 | |
7538 return NULL; | |
7539 | |
7540 #else /* TEMPDIRNAMES */ | |
7541 | |
7542 # ifdef WIN3264 | |
7543 char szTempFile[_MAX_PATH + 1]; | |
7544 char buf4[4]; | |
7545 char_u *retval; | |
7546 char_u *p; | |
7547 | |
7548 STRCPY(itmp, ""); | |
7549 if (GetTempPath(_MAX_PATH, szTempFile) == 0) | |
2695 | 7550 { |
7551 szTempFile[0] = '.'; /* GetTempPath() failed, use current dir */ | |
7552 szTempFile[1] = NUL; | |
7553 } | |
7 | 7554 strcpy(buf4, "VIM"); |
7555 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
|
7556 if (GetTempFileName(szTempFile, buf4, 0, (LPSTR)itmp) == 0) |
7 | 7557 return NULL; |
6721 | 7558 if (!keep) |
7559 /* 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
|
7560 (void)DeleteFile((LPSTR)itmp); |
7 | 7561 |
7562 /* Backslashes in a temp file name cause problems when filtering with | |
7563 * "sh". NOTE: This also checks 'shellcmdflag' to help those people who | |
7564 * didn't set 'shellslash'. */ | |
7565 retval = vim_strsave(itmp); | |
7566 if (*p_shcf == '-' || p_ssl) | |
7567 for (p = retval; *p; ++p) | |
7568 if (*p == '\\') | |
7569 *p = '/'; | |
7570 return retval; | |
7571 | |
7572 # else /* WIN3264 */ | |
7573 | |
7574 # ifdef USE_TMPNAM | |
2697 | 7575 char_u *p; |
7576 | |
7 | 7577 /* tmpnam() will make its own name */ |
2697 | 7578 p = tmpnam((char *)itmp); |
7579 if (p == NULL || *p == NUL) | |
7 | 7580 return NULL; |
7581 # else | |
7582 char_u *p; | |
7583 | |
7584 # ifdef VMS_TEMPNAM | |
7585 /* mktemp() is not working on VMS. It seems to be | |
7586 * a do-nothing function. Therefore we use tempnam(). | |
7587 */ | |
7588 sprintf((char *)itmp, "VIM%c", extra_char); | |
7589 p = (char_u *)tempnam("tmp:", (char *)itmp); | |
7590 if (p != NULL) | |
7591 { | |
5704 | 7592 /* VMS will use '.LIS' if we don't explicitly specify an extension, |
7 | 7593 * and VIM will then be unable to find the file later */ |
7594 STRCPY(itmp, p); | |
7595 STRCAT(itmp, ".txt"); | |
7596 free(p); | |
7597 } | |
7598 else | |
7599 return NULL; | |
7600 # else | |
7601 STRCPY(itmp, TEMPNAME); | |
7602 if ((p = vim_strchr(itmp, '?')) != NULL) | |
7603 *p = extra_char; | |
7604 if (mktemp((char *)itmp) == NULL) | |
7605 return NULL; | |
7606 # endif | |
7607 # endif | |
7608 | |
7609 return vim_strsave(itmp); | |
7610 # endif /* WIN3264 */ | |
7611 #endif /* TEMPDIRNAMES */ | |
7612 } | |
7613 | |
7614 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) | |
7615 /* | |
7170
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
7616 * 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
|
7617 * it looks like a URL. |
7 | 7618 */ |
7619 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7620 forward_slash(char_u *fname) |
7 | 7621 { |
7622 char_u *p; | |
7623 | |
7170
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
7624 if (path_with_url(fname)) |
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
7625 return; |
7 | 7626 for (p = fname; *p != NUL; ++p) |
7627 # ifdef FEAT_MBYTE | |
7628 /* The Big5 encoding can have '\' in the trail byte. */ | |
474 | 7629 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 7630 ++p; |
7631 else | |
7632 # endif | |
7633 if (*p == '\\') | |
7634 *p = '/'; | |
7635 } | |
7636 #endif | |
7637 | |
7638 | |
7639 /* | |
7640 * Code for automatic commands. | |
7641 * | |
7642 * Only included when "FEAT_AUTOCMD" has been defined. | |
7643 */ | |
7644 | |
7645 #if defined(FEAT_AUTOCMD) || defined(PROTO) | |
7646 | |
7647 /* | |
7648 * The autocommands are stored in a list for each event. | |
7649 * Autocommands for the same pattern, that are consecutive, are joined | |
7650 * together, to avoid having to match the pattern too often. | |
7651 * The result is an array of Autopat lists, which point to AutoCmd lists: | |
7652 * | |
7653 * first_autopat[0] --> Autopat.next --> Autopat.next --> NULL | |
7654 * Autopat.cmds Autopat.cmds | |
7655 * | | | |
7656 * V V | |
7657 * AutoCmd.next AutoCmd.next | |
7658 * | | | |
7659 * V V | |
7660 * AutoCmd.next NULL | |
7661 * | | |
7662 * V | |
7663 * NULL | |
7664 * | |
7665 * first_autopat[1] --> Autopat.next --> NULL | |
7666 * Autopat.cmds | |
7667 * | | |
7668 * V | |
7669 * AutoCmd.next | |
7670 * | | |
7671 * V | |
7672 * NULL | |
7673 * etc. | |
7674 * | |
7675 * The order of AutoCmds is important, this is the order in which they were | |
7676 * defined and will have to be executed. | |
7677 */ | |
7678 typedef struct AutoCmd | |
7679 { | |
7680 char_u *cmd; /* The command to be executed (NULL | |
7681 when command has been removed) */ | |
7682 char nested; /* If autocommands nest here */ | |
7683 char last; /* last command in list */ | |
7684 #ifdef FEAT_EVAL | |
7685 scid_T scriptID; /* script ID where defined */ | |
7686 #endif | |
7687 struct AutoCmd *next; /* Next AutoCmd in list */ | |
7688 } AutoCmd; | |
7689 | |
7690 typedef struct AutoPat | |
7691 { | |
7692 char_u *pat; /* pattern as typed (NULL when pattern | |
7693 has been removed) */ | |
4861
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7694 regprog_T *reg_prog; /* compiled regprog for pattern */ |
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7695 AutoCmd *cmds; /* list of commands to do */ |
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7696 struct AutoPat *next; /* next AutoPat in AutoPat list */ |
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7697 int group; /* group ID */ |
7 | 7698 int patlen; /* strlen() of pat */ |
4861
e79a20f07daa
updated for version 7.3.1177
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
7699 int buflocal_nr; /* !=0 for buffer-local AutoPat */ |
7 | 7700 char allow_dirs; /* Pattern may match whole path */ |
7701 char last; /* last pattern for apply_autocmds() */ | |
7702 } AutoPat; | |
7703 | |
7704 static struct event_name | |
7705 { | |
7706 char *name; /* event name */ | |
661 | 7707 event_T event; /* event number */ |
7 | 7708 } event_names[] = |
7709 { | |
7710 {"BufAdd", EVENT_BUFADD}, | |
7711 {"BufCreate", EVENT_BUFADD}, | |
7712 {"BufDelete", EVENT_BUFDELETE}, | |
7713 {"BufEnter", EVENT_BUFENTER}, | |
7714 {"BufFilePost", EVENT_BUFFILEPOST}, | |
7715 {"BufFilePre", EVENT_BUFFILEPRE}, | |
7716 {"BufHidden", EVENT_BUFHIDDEN}, | |
7717 {"BufLeave", EVENT_BUFLEAVE}, | |
7718 {"BufNew", EVENT_BUFNEW}, | |
7719 {"BufNewFile", EVENT_BUFNEWFILE}, | |
7720 {"BufRead", EVENT_BUFREADPOST}, | |
7721 {"BufReadCmd", EVENT_BUFREADCMD}, | |
7722 {"BufReadPost", EVENT_BUFREADPOST}, | |
7723 {"BufReadPre", EVENT_BUFREADPRE}, | |
7724 {"BufUnload", EVENT_BUFUNLOAD}, | |
7725 {"BufWinEnter", EVENT_BUFWINENTER}, | |
7726 {"BufWinLeave", EVENT_BUFWINLEAVE}, | |
7727 {"BufWipeout", EVENT_BUFWIPEOUT}, | |
7728 {"BufWrite", EVENT_BUFWRITEPRE}, | |
7729 {"BufWritePost", EVENT_BUFWRITEPOST}, | |
7730 {"BufWritePre", EVENT_BUFWRITEPRE}, | |
7731 {"BufWriteCmd", EVENT_BUFWRITECMD}, | |
12656
0a9dacb8826a
patch 8.0.1206: no autocmd for entering or leaving the command line
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
7732 {"CmdlineEnter", EVENT_CMDLINEENTER}, |
0a9dacb8826a
patch 8.0.1206: no autocmd for entering or leaving the command line
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
7733 {"CmdlineLeave", EVENT_CMDLINELEAVE}, |
7 | 7734 {"CmdwinEnter", EVENT_CMDWINENTER}, |
7735 {"CmdwinLeave", EVENT_CMDWINLEAVE}, | |
6154 | 7736 {"CmdUndefined", EVENT_CMDUNDEFINED}, |
12 | 7737 {"ColorScheme", EVENT_COLORSCHEME}, |
3676 | 7738 {"CompleteDone", EVENT_COMPLETEDONE}, |
661 | 7739 {"CursorHold", EVENT_CURSORHOLD}, |
7740 {"CursorHoldI", EVENT_CURSORHOLDI}, | |
7741 {"CursorMoved", EVENT_CURSORMOVED}, | |
7742 {"CursorMovedI", EVENT_CURSORMOVEDI}, | |
7 | 7743 {"EncodingChanged", EVENT_ENCODINGCHANGED}, |
7744 {"FileEncoding", EVENT_ENCODINGCHANGED}, | |
7745 {"FileAppendPost", EVENT_FILEAPPENDPOST}, | |
7746 {"FileAppendPre", EVENT_FILEAPPENDPRE}, | |
7747 {"FileAppendCmd", EVENT_FILEAPPENDCMD}, | |
7748 {"FileChangedShell",EVENT_FILECHANGEDSHELL}, | |
765 | 7749 {"FileChangedShellPost",EVENT_FILECHANGEDSHELLPOST}, |
7 | 7750 {"FileChangedRO", EVENT_FILECHANGEDRO}, |
7751 {"FileReadPost", EVENT_FILEREADPOST}, | |
7752 {"FileReadPre", EVENT_FILEREADPRE}, | |
7753 {"FileReadCmd", EVENT_FILEREADCMD}, | |
7754 {"FileType", EVENT_FILETYPE}, | |
7755 {"FileWritePost", EVENT_FILEWRITEPOST}, | |
7756 {"FileWritePre", EVENT_FILEWRITEPRE}, | |
7757 {"FileWriteCmd", EVENT_FILEWRITECMD}, | |
7758 {"FilterReadPost", EVENT_FILTERREADPOST}, | |
7759 {"FilterReadPre", EVENT_FILTERREADPRE}, | |
7760 {"FilterWritePost", EVENT_FILTERWRITEPOST}, | |
7761 {"FilterWritePre", EVENT_FILTERWRITEPRE}, | |
7762 {"FocusGained", EVENT_FOCUSGAINED}, | |
7763 {"FocusLost", EVENT_FOCUSLOST}, | |
7764 {"FuncUndefined", EVENT_FUNCUNDEFINED}, | |
7765 {"GUIEnter", EVENT_GUIENTER}, | |
946 | 7766 {"GUIFailed", EVENT_GUIFAILED}, |
11 | 7767 {"InsertChange", EVENT_INSERTCHANGE}, |
7768 {"InsertEnter", EVENT_INSERTENTER}, | |
7769 {"InsertLeave", EVENT_INSERTLEAVE}, | |
2845 | 7770 {"InsertCharPre", EVENT_INSERTCHARPRE}, |
434 | 7771 {"MenuPopup", EVENT_MENUPOPUP}, |
6935 | 7772 {"OptionSet", EVENT_OPTIONSET}, |
161 | 7773 {"QuickFixCmdPost", EVENT_QUICKFIXCMDPOST}, |
7774 {"QuickFixCmdPre", EVENT_QUICKFIXCMDPRE}, | |
3568 | 7775 {"QuitPre", EVENT_QUITPRE}, |
7 | 7776 {"RemoteReply", EVENT_REMOTEREPLY}, |
574 | 7777 {"SessionLoadPost", EVENT_SESSIONLOADPOST}, |
723 | 7778 {"ShellCmdPost", EVENT_SHELLCMDPOST}, |
7779 {"ShellFilterPost", EVENT_SHELLFILTERPOST}, | |
715 | 7780 {"SourcePre", EVENT_SOURCEPRE}, |
1061 | 7781 {"SourceCmd", EVENT_SOURCECMD}, |
649 | 7782 {"SpellFileMissing",EVENT_SPELLFILEMISSING}, |
7 | 7783 {"StdinReadPost", EVENT_STDINREADPOST}, |
7784 {"StdinReadPre", EVENT_STDINREADPRE}, | |
674 | 7785 {"SwapExists", EVENT_SWAPEXISTS}, |
7 | 7786 {"Syntax", EVENT_SYNTAX}, |
9595
0190d5de215f
commit https://github.com/vim/vim/commit/c917da4b3e8801a255dbefea8e4ed19c1c716dd8
Christian Brabandt <cb@256bit.org>
parents:
9536
diff
changeset
|
7787 {"TabNew", EVENT_TABNEW}, |
9599
42a8a81decdf
commit https://github.com/vim/vim/commit/12c11d553053f5a9eae9eb3c518279b12fa928c2
Christian Brabandt <cb@256bit.org>
parents:
9595
diff
changeset
|
7788 {"TabClosed", EVENT_TABCLOSED}, |
676 | 7789 {"TabEnter", EVENT_TABENTER}, |
7790 {"TabLeave", EVENT_TABLEAVE}, | |
7 | 7791 {"TermChanged", EVENT_TERMCHANGED}, |
7792 {"TermResponse", EVENT_TERMRESPONSE}, | |
4232 | 7793 {"TextChanged", EVENT_TEXTCHANGED}, |
7794 {"TextChangedI", EVENT_TEXTCHANGEDI}, | |
7 | 7795 {"User", EVENT_USER}, |
7796 {"VimEnter", EVENT_VIMENTER}, | |
7797 {"VimLeave", EVENT_VIMLEAVE}, | |
7798 {"VimLeavePre", EVENT_VIMLEAVEPRE}, | |
9595
0190d5de215f
commit https://github.com/vim/vim/commit/c917da4b3e8801a255dbefea8e4ed19c1c716dd8
Christian Brabandt <cb@256bit.org>
parents:
9536
diff
changeset
|
7799 {"WinNew", EVENT_WINNEW}, |
7 | 7800 {"WinEnter", EVENT_WINENTER}, |
7801 {"WinLeave", EVENT_WINLEAVE}, | |
765 | 7802 {"VimResized", EVENT_VIMRESIZED}, |
661 | 7803 {NULL, (event_T)0} |
7 | 7804 }; |
7805 | |
7806 static AutoPat *first_autopat[NUM_EVENTS] = | |
7807 { | |
7808 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7809 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7810 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7811 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
18 | 7812 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
6935 | 7813 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
7 | 7814 }; |
7815 | |
7816 /* | |
7817 * struct used to keep status while executing autocommands for an event. | |
7818 */ | |
7819 typedef struct AutoPatCmd | |
7820 { | |
7821 AutoPat *curpat; /* next AutoPat to examine */ | |
7822 AutoCmd *nextcmd; /* next AutoCmd to execute */ | |
7823 int group; /* group being used */ | |
7824 char_u *fname; /* fname to match with */ | |
7825 char_u *sfname; /* sfname to match with */ | |
7826 char_u *tail; /* tail of fname */ | |
661 | 7827 event_T event; /* current event */ |
40 | 7828 int arg_bufnr; /* initially equal to <abuf>, set to zero when |
7829 buf is deleted */ | |
7830 struct AutoPatCmd *next; /* chain of active apc-s for auto-invalidation*/ | |
7 | 7831 } AutoPatCmd; |
7832 | |
297 | 7833 static AutoPatCmd *active_apc_list = NULL; /* stack of active autocommands */ |
40 | 7834 |
7 | 7835 /* |
7836 * augroups stores a list of autocmd group names. | |
7837 */ | |
297 | 7838 static garray_T augroups = {0, 0, sizeof(char_u *), 10, NULL}; |
7 | 7839 #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
|
7840 /* 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
|
7841 static char_u *deleted_augroup = NULL; |
7 | 7842 |
7843 /* | |
7844 * The ID of the current group. Group 0 is the default one. | |
7845 */ | |
7846 static int current_augroup = AUGROUP_DEFAULT; | |
7847 | |
7848 static int au_need_clean = FALSE; /* need to delete marked patterns */ | |
7849 | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7850 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
|
7851 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
|
7852 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
|
7853 static void au_cleanup(void); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
7854 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
|
7855 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
|
7856 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
|
7857 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
|
7858 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
|
7859 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
|
7860 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
|
7861 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
|
7862 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
|
7863 static void auto_next_pat(AutoPatCmd *apc, int stop_at_last); |
6375 | 7864 #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
|
7865 static int match_file_pat(char_u *pattern, regprog_T **prog, char_u *fname, char_u *sfname, char_u *tail, int allow_dirs); |
6375 | 7866 #endif |
7 | 7867 |
40 | 7868 |
661 | 7869 static event_T last_event; |
7 | 7870 static int last_group; |
1410 | 7871 static int autocmd_blocked = 0; /* block all autocmds */ |
7 | 7872 |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7873 static char_u * |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7874 get_deleted_augroup(void) |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7875 { |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7876 if (deleted_augroup == NULL) |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7877 deleted_augroup = (char_u *)_("--Deleted--"); |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7878 return deleted_augroup; |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7879 } |
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
7880 |
7 | 7881 /* |
7882 * Show the autocommands for one AutoPat. | |
7883 */ | |
7884 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7885 show_autocmd(AutoPat *ap, event_T event) |
7 | 7886 { |
7887 AutoCmd *ac; | |
7888 | |
7889 /* Check for "got_int" (here and at various places below), which is set | |
7890 * when "q" has been hit for the "--more--" prompt */ | |
7891 if (got_int) | |
7892 return; | |
7893 if (ap->pat == NULL) /* pattern has been removed */ | |
7894 return; | |
7895 | |
7896 msg_putchar('\n'); | |
7897 if (got_int) | |
7898 return; | |
7899 if (event != last_event || ap->group != last_group) | |
7900 { | |
7901 if (ap->group != AUGROUP_DEFAULT) | |
7902 { | |
7903 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
|
7904 msg_puts_attr(get_deleted_augroup(), HL_ATTR(HLF_E)); |
7 | 7905 else |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
7906 msg_puts_attr(AUGROUP_NAME(ap->group), HL_ATTR(HLF_T)); |
7 | 7907 msg_puts((char_u *)" "); |
7908 } | |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
7909 msg_puts_attr(event_nr2name(event), HL_ATTR(HLF_T)); |
7 | 7910 last_event = event; |
7911 last_group = ap->group; | |
7912 msg_putchar('\n'); | |
7913 if (got_int) | |
7914 return; | |
7915 } | |
7916 msg_col = 4; | |
7917 msg_outtrans(ap->pat); | |
7918 | |
7919 for (ac = ap->cmds; ac != NULL; ac = ac->next) | |
7920 { | |
7921 if (ac->cmd != NULL) /* skip removed commands */ | |
7922 { | |
7923 if (msg_col >= 14) | |
7924 msg_putchar('\n'); | |
7925 msg_col = 14; | |
7926 if (got_int) | |
7927 return; | |
7928 msg_outtrans(ac->cmd); | |
500 | 7929 #ifdef FEAT_EVAL |
7930 if (p_verbose > 0) | |
7931 last_set_msg(ac->scriptID); | |
7932 #endif | |
7 | 7933 if (got_int) |
7934 return; | |
7935 if (ac->next != NULL) | |
7936 { | |
7937 msg_putchar('\n'); | |
7938 if (got_int) | |
7939 return; | |
7940 } | |
7941 } | |
7942 } | |
7943 } | |
7944 | |
7945 /* | |
7946 * Mark an autocommand pattern for deletion. | |
7947 */ | |
7948 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7949 au_remove_pat(AutoPat *ap) |
7 | 7950 { |
7951 vim_free(ap->pat); | |
7952 ap->pat = NULL; | |
40 | 7953 ap->buflocal_nr = -1; |
7 | 7954 au_need_clean = TRUE; |
7955 } | |
7956 | |
7957 /* | |
7958 * Mark all commands for a pattern for deletion. | |
7959 */ | |
7960 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7961 au_remove_cmds(AutoPat *ap) |
7 | 7962 { |
7963 AutoCmd *ac; | |
7964 | |
7965 for (ac = ap->cmds; ac != NULL; ac = ac->next) | |
7966 { | |
7967 vim_free(ac->cmd); | |
7968 ac->cmd = NULL; | |
7969 } | |
7970 au_need_clean = TRUE; | |
7971 } | |
7972 | |
7973 /* | |
7974 * Cleanup autocommands and patterns that have been deleted. | |
7975 * This is only done when not executing autocommands. | |
7976 */ | |
7977 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
7978 au_cleanup(void) |
7 | 7979 { |
7980 AutoPat *ap, **prev_ap; | |
7981 AutoCmd *ac, **prev_ac; | |
661 | 7982 event_T event; |
7 | 7983 |
7984 if (autocmd_busy || !au_need_clean) | |
7985 return; | |
7986 | |
7987 /* loop over all events */ | |
661 | 7988 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
7989 event = (event_T)((int)event + 1)) | |
7 | 7990 { |
7991 /* loop over all autocommand patterns */ | |
7992 prev_ap = &(first_autopat[(int)event]); | |
7993 for (ap = *prev_ap; ap != NULL; ap = *prev_ap) | |
7994 { | |
7995 /* loop over all commands for this pattern */ | |
7996 prev_ac = &(ap->cmds); | |
7997 for (ac = *prev_ac; ac != NULL; ac = *prev_ac) | |
7998 { | |
7999 /* remove the command if the pattern is to be deleted or when | |
8000 * the command has been marked for deletion */ | |
8001 if (ap->pat == NULL || ac->cmd == NULL) | |
8002 { | |
8003 *prev_ac = ac->next; | |
8004 vim_free(ac->cmd); | |
8005 vim_free(ac); | |
8006 } | |
8007 else | |
8008 prev_ac = &(ac->next); | |
8009 } | |
8010 | |
8011 /* remove the pattern if it has been marked for deletion */ | |
8012 if (ap->pat == NULL) | |
8013 { | |
8014 *prev_ap = ap->next; | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4354
diff
changeset
|
8015 vim_regfree(ap->reg_prog); |
7 | 8016 vim_free(ap); |
8017 } | |
8018 else | |
8019 prev_ap = &(ap->next); | |
8020 } | |
8021 } | |
8022 | |
8023 au_need_clean = FALSE; | |
8024 } | |
8025 | |
8026 /* | |
40 | 8027 * Called when buffer is freed, to remove/invalidate related buffer-local |
8028 * autocmds. | |
8029 */ | |
8030 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8031 aubuflocal_remove(buf_T *buf) |
40 | 8032 { |
8033 AutoPat *ap; | |
661 | 8034 event_T event; |
40 | 8035 AutoPatCmd *apc; |
8036 | |
8037 /* invalidate currently executing autocommands */ | |
8038 for (apc = active_apc_list; apc; apc = apc->next) | |
8039 if (buf->b_fnum == apc->arg_bufnr) | |
8040 apc->arg_bufnr = 0; | |
8041 | |
8042 /* invalidate buflocals looping through events */ | |
661 | 8043 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
8044 event = (event_T)((int)event + 1)) | |
40 | 8045 /* loop over all autocommand patterns */ |
8046 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
8047 if (ap->buflocal_nr == buf->b_fnum) | |
8048 { | |
8049 au_remove_pat(ap); | |
8050 if (p_verbose >= 6) | |
292 | 8051 { |
8052 verbose_enter(); | |
40 | 8053 smsg((char_u *) |
8054 _("auto-removing autocommand: %s <buffer=%d>"), | |
8055 event_nr2name(event), buf->b_fnum); | |
292 | 8056 verbose_leave(); |
8057 } | |
40 | 8058 } |
8059 au_cleanup(); | |
8060 } | |
8061 | |
8062 /* | |
7 | 8063 * Add an autocmd group name. |
8064 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. | |
8065 */ | |
8066 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8067 au_new_group(char_u *name) |
7 | 8068 { |
8069 int i; | |
8070 | |
8071 i = au_find_group(name); | |
8072 if (i == AUGROUP_ERROR) /* the group doesn't exist yet, add it */ | |
8073 { | |
8074 /* First try using a free entry. */ | |
8075 for (i = 0; i < augroups.ga_len; ++i) | |
8076 if (AUGROUP_NAME(i) == NULL) | |
8077 break; | |
8078 if (i == augroups.ga_len && ga_grow(&augroups, 1) == FAIL) | |
8079 return AUGROUP_ERROR; | |
8080 | |
8081 AUGROUP_NAME(i) = vim_strsave(name); | |
8082 if (AUGROUP_NAME(i) == NULL) | |
8083 return AUGROUP_ERROR; | |
8084 if (i == augroups.ga_len) | |
8085 ++augroups.ga_len; | |
8086 } | |
8087 | |
8088 return i; | |
8089 } | |
8090 | |
8091 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8092 au_del_group(char_u *name) |
7 | 8093 { |
8094 int i; | |
8095 | |
8096 i = au_find_group(name); | |
8097 if (i == AUGROUP_ERROR) /* the group doesn't exist */ | |
8098 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
|
8099 else if (i == current_augroup) |
1de911ef1edf
commit https://github.com/vim/vim/commit/de653f08805dde14424d417502a0480a6ad292f8
Christian Brabandt <cb@256bit.org>
parents:
10084
diff
changeset
|
8100 EMSG(_("E936: Cannot delete the current group")); |
7 | 8101 else |
8102 { | |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8103 event_T event; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8104 AutoPat *ap; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8105 int in_use = FALSE; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8106 |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8107 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
|
8108 event = (event_T)((int)event + 1)) |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8109 { |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8110 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
|
8111 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
|
8112 { |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8113 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
|
8114 in_use = TRUE; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8115 event = NUM_EVENTS; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8116 break; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8117 } |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8118 } |
7 | 8119 vim_free(AUGROUP_NAME(i)); |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8120 if (in_use) |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8121 { |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
8122 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
|
8123 } |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8124 else |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8125 AUGROUP_NAME(i) = NULL; |
7 | 8126 } |
8127 } | |
8128 | |
8129 /* | |
8130 * Find the ID of an autocmd group name. | |
8131 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. | |
8132 */ | |
8133 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8134 au_find_group(char_u *name) |
7 | 8135 { |
8136 int i; | |
8137 | |
8138 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
|
8139 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
|
8140 && STRCMP(AUGROUP_NAME(i), name) == 0) |
7 | 8141 return i; |
8142 return AUGROUP_ERROR; | |
8143 } | |
8144 | |
461 | 8145 /* |
8146 * Return TRUE if augroup "name" exists. | |
8147 */ | |
8148 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8149 au_has_group(char_u *name) |
461 | 8150 { |
8151 return au_find_group(name) != AUGROUP_ERROR; | |
8152 } | |
8153 | |
7 | 8154 /* |
8155 * ":augroup {name}". | |
8156 */ | |
8157 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8158 do_augroup(char_u *arg, int del_group) |
7 | 8159 { |
8160 int i; | |
8161 | |
8162 if (del_group) | |
8163 { | |
8164 if (*arg == NUL) | |
8165 EMSG(_(e_argreq)); | |
8166 else | |
8167 au_del_group(arg); | |
8168 } | |
8169 else if (STRICMP(arg, "end") == 0) /* ":aug end": back to group 0 */ | |
8170 current_augroup = AUGROUP_DEFAULT; | |
8171 else if (*arg) /* ":aug xxx": switch to group xxx */ | |
8172 { | |
8173 i = au_new_group(arg); | |
8174 if (i != AUGROUP_ERROR) | |
8175 current_augroup = i; | |
8176 } | |
8177 else /* ":aug": list the group names */ | |
8178 { | |
8179 msg_start(); | |
8180 for (i = 0; i < augroups.ga_len; ++i) | |
8181 { | |
8182 if (AUGROUP_NAME(i) != NULL) | |
8183 { | |
8184 msg_puts(AUGROUP_NAME(i)); | |
8185 msg_puts((char_u *)" "); | |
8186 } | |
8187 } | |
8188 msg_clr_eos(); | |
8189 msg_end(); | |
8190 } | |
8191 } | |
8192 | |
355 | 8193 #if defined(EXITFREE) || defined(PROTO) |
8194 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8195 free_all_autocmds(void) |
355 | 8196 { |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8197 int i; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8198 char_u *s; |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8199 |
355 | 8200 for (current_augroup = -1; current_augroup < augroups.ga_len; |
8201 ++current_augroup) | |
8202 do_autocmd((char_u *)"", TRUE); | |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8203 |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8204 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
|
8205 { |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8206 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
|
8207 if (s != get_deleted_augroup()) |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8208 vim_free(s); |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8209 } |
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
8210 ga_clear(&augroups); |
355 | 8211 } |
8212 #endif | |
8213 | |
7 | 8214 /* |
8215 * Return the event number for event name "start". | |
8216 * Return NUM_EVENTS if the event name was not found. | |
8217 * Return a pointer to the next event name in "end". | |
8218 */ | |
661 | 8219 static event_T |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8220 event_name2nr(char_u *start, char_u **end) |
7 | 8221 { |
8222 char_u *p; | |
8223 int i; | |
8224 int len; | |
8225 | |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8226 /* 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
|
8227 for (p = start; *p && !VIM_ISWHITE(*p) && *p != ',' && *p != '|'; ++p) |
7 | 8228 ; |
8229 for (i = 0; event_names[i].name != NULL; ++i) | |
8230 { | |
8231 len = (int)STRLEN(event_names[i].name); | |
8232 if (len == p - start && STRNICMP(event_names[i].name, start, len) == 0) | |
8233 break; | |
8234 } | |
8235 if (*p == ',') | |
8236 ++p; | |
8237 *end = p; | |
8238 if (event_names[i].name == NULL) | |
8239 return NUM_EVENTS; | |
8240 return event_names[i].event; | |
8241 } | |
8242 | |
8243 /* | |
8244 * Return the name for event "event". | |
8245 */ | |
8246 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8247 event_nr2name(event_T event) |
7 | 8248 { |
8249 int i; | |
8250 | |
8251 for (i = 0; event_names[i].name != NULL; ++i) | |
8252 if (event_names[i].event == event) | |
8253 return (char_u *)event_names[i].name; | |
8254 return (char_u *)"Unknown"; | |
8255 } | |
8256 | |
8257 /* | |
8258 * Scan over the events. "*" stands for all events. | |
8259 */ | |
8260 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8261 find_end_event( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8262 char_u *arg, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8263 int have_group) /* TRUE when group name was found */ |
7 | 8264 { |
8265 char_u *pat; | |
8266 char_u *p; | |
8267 | |
8268 if (*arg == '*') | |
8269 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8270 if (arg[1] && !VIM_ISWHITE(arg[1])) |
7 | 8271 { |
8272 EMSG2(_("E215: Illegal character after *: %s"), arg); | |
8273 return NULL; | |
8274 } | |
8275 pat = arg + 1; | |
8276 } | |
8277 else | |
8278 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8279 for (pat = arg; *pat && *pat != '|' && !VIM_ISWHITE(*pat); pat = p) |
7 | 8280 { |
8281 if ((int)event_name2nr(pat, &p) >= (int)NUM_EVENTS) | |
8282 { | |
8283 if (have_group) | |
8284 EMSG2(_("E216: No such event: %s"), pat); | |
8285 else | |
8286 EMSG2(_("E216: No such group or event: %s"), pat); | |
8287 return NULL; | |
8288 } | |
8289 } | |
8290 } | |
8291 return pat; | |
8292 } | |
8293 | |
8294 /* | |
8295 * Return TRUE if "event" is included in 'eventignore'. | |
8296 */ | |
8297 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8298 event_ignored(event_T event) |
7 | 8299 { |
8300 char_u *p = p_ei; | |
8301 | |
844 | 8302 while (*p != NUL) |
8303 { | |
8304 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) | |
8305 return TRUE; | |
7 | 8306 if (event_name2nr(p, &p) == event) |
8307 return TRUE; | |
844 | 8308 } |
7 | 8309 |
8310 return FALSE; | |
8311 } | |
8312 | |
8313 /* | |
8314 * Return OK when the contents of p_ei is valid, FAIL otherwise. | |
8315 */ | |
8316 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8317 check_ei(void) |
7 | 8318 { |
8319 char_u *p = p_ei; | |
8320 | |
8321 while (*p) | |
844 | 8322 { |
8323 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) | |
8324 { | |
8325 p += 3; | |
8326 if (*p == ',') | |
8327 ++p; | |
8328 } | |
8329 else if (event_name2nr(p, &p) == NUM_EVENTS) | |
7 | 8330 return FAIL; |
844 | 8331 } |
7 | 8332 |
8333 return OK; | |
8334 } | |
8335 | |
123 | 8336 # if defined(FEAT_SYN_HL) || defined(PROTO) |
8337 | |
8338 /* | |
8339 * Add "what" to 'eventignore' to skip loading syntax highlighting for every | |
8340 * buffer loaded into the window. "what" must start with a comma. | |
8341 * Returns the old value of 'eventignore' in allocated memory. | |
8342 */ | |
8343 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8344 au_event_disable(char *what) |
123 | 8345 { |
8346 char_u *new_ei; | |
8347 char_u *save_ei; | |
8348 | |
8349 save_ei = vim_strsave(p_ei); | |
8350 if (save_ei != NULL) | |
8351 { | |
557 | 8352 new_ei = vim_strnsave(p_ei, (int)(STRLEN(p_ei) + STRLEN(what))); |
123 | 8353 if (new_ei != NULL) |
8354 { | |
2095
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8355 if (*what == ',' && *p_ei == NUL) |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8356 STRCPY(new_ei, what + 1); |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8357 else |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8358 STRCAT(new_ei, what); |
694 | 8359 set_string_option_direct((char_u *)"ei", -1, new_ei, |
8360 OPT_FREE, SID_NONE); | |
123 | 8361 vim_free(new_ei); |
8362 } | |
8363 } | |
8364 return save_ei; | |
8365 } | |
8366 | |
8367 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8368 au_event_restore(char_u *old_ei) |
123 | 8369 { |
8370 if (old_ei != NULL) | |
8371 { | |
694 | 8372 set_string_option_direct((char_u *)"ei", -1, old_ei, |
8373 OPT_FREE, SID_NONE); | |
123 | 8374 vim_free(old_ei); |
8375 } | |
8376 } | |
8377 # endif /* FEAT_SYN_HL */ | |
8378 | |
7 | 8379 /* |
8380 * do_autocmd() -- implements the :autocmd command. Can be used in the | |
8381 * following ways: | |
8382 * | |
8383 * :autocmd <event> <pat> <cmd> Add <cmd> to the list of commands that | |
8384 * will be automatically executed for <event> | |
8385 * when editing a file matching <pat>, in | |
8386 * the current group. | |
8387 * :autocmd <event> <pat> Show the auto-commands associated with | |
8388 * <event> and <pat>. | |
8389 * :autocmd <event> Show the auto-commands associated with | |
8390 * <event>. | |
8391 * :autocmd Show all auto-commands. | |
8392 * :autocmd! <event> <pat> <cmd> Remove all auto-commands associated with | |
8393 * <event> and <pat>, and add the command | |
8394 * <cmd>, for the current group. | |
8395 * :autocmd! <event> <pat> Remove all auto-commands associated with | |
8396 * <event> and <pat> for the current group. | |
8397 * :autocmd! <event> Remove all auto-commands associated with | |
8398 * <event> for the current group. | |
8399 * :autocmd! Remove ALL auto-commands for the current | |
8400 * group. | |
8401 * | |
8402 * Multiple events and patterns may be given separated by commas. Here are | |
8403 * some examples: | |
8404 * :autocmd bufread,bufenter *.c,*.h set tw=0 smartindent noic | |
8405 * :autocmd bufleave * set tw=79 nosmartindent ic infercase | |
8406 * | |
8407 * :autocmd * *.c show all autocommands for *.c files. | |
609 | 8408 * |
8409 * Mostly a {group} argument can optionally appear before <event>. | |
7 | 8410 */ |
8411 void | |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8412 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
|
8413 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8414 char_u *arg = arg_in; |
7 | 8415 char_u *pat; |
8416 char_u *envpat = NULL; | |
8417 char_u *cmd; | |
661 | 8418 event_T event; |
7 | 8419 int need_free = FALSE; |
8420 int nested = FALSE; | |
8421 int group; | |
8422 | |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8423 if (*arg == '|') |
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 arg = (char_u *)""; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8426 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
|
8427 } |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8428 else |
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 /* |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8431 * 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
|
8432 */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8433 group = au_get_grouparg(&arg); |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8434 if (arg == NULL) /* out of memory */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8435 return; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8436 } |
7 | 8437 |
8438 /* | |
8439 * Scan over the events. | |
8440 * If we find an illegal name, return here, don't do anything. | |
8441 */ | |
8442 pat = find_end_event(arg, group != AUGROUP_ALL); | |
8443 if (pat == NULL) | |
8444 return; | |
8445 | |
8446 pat = skipwhite(pat); | |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8447 if (*pat == '|') |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8448 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8449 pat = (char_u *)""; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8450 cmd = (char_u *)""; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8451 } |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8452 else |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8453 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8454 /* |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8455 * 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
|
8456 */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8457 cmd = pat; |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8458 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
|
8459 cmd++; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8460 if (*cmd) |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8461 *cmd++ = NUL; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8462 |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8463 /* 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
|
8464 * forward slashes here. */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8465 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
|
8466 { |
7 | 8467 #ifdef BACKSLASH_IN_FILENAME |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8468 int p_ssl_save = p_ssl; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8469 |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8470 p_ssl = TRUE; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8471 #endif |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8472 envpat = expand_env_save(pat); |
7 | 8473 #ifdef BACKSLASH_IN_FILENAME |
9653
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8474 p_ssl = p_ssl_save; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8475 #endif |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8476 if (envpat != NULL) |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8477 pat = envpat; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8478 } |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8479 |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8480 /* |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8481 * Check for "nested" flag. |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8482 */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8483 cmd = skipwhite(cmd); |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8484 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
|
8485 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8486 nested = TRUE; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8487 cmd = skipwhite(cmd + 6); |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8488 } |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8489 |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8490 /* |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8491 * Find the start of the commands. |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8492 * Expand <sfile> in it. |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8493 */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8494 if (*cmd != NUL) |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8495 { |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8496 cmd = expand_sfile(cmd); |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8497 if (cmd == NULL) /* some error */ |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8498 return; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8499 need_free = TRUE; |
01c9630e80e0
commit https://github.com/vim/vim/commit/e99e84497b89e5f91df519790802770920ecf4fe
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
8500 } |
7 | 8501 } |
8502 | |
8503 /* | |
8504 * Print header when showing autocommands. | |
8505 */ | |
8506 if (!forceit && *cmd == NUL) | |
8507 { | |
8508 /* Highlight title */ | |
8509 MSG_PUTS_TITLE(_("\n--- Auto-Commands ---")); | |
8510 } | |
8511 | |
8512 /* | |
8513 * Loop over the events. | |
8514 */ | |
661 | 8515 last_event = (event_T)-1; /* for listing the event name */ |
7 | 8516 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
|
8517 if (*arg == '*' || *arg == NUL || *arg == '|') |
7 | 8518 { |
661 | 8519 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
8520 event = (event_T)((int)event + 1)) | |
7 | 8521 if (do_autocmd_event(event, pat, |
8522 nested, cmd, forceit, group) == FAIL) | |
8523 break; | |
8524 } | |
8525 else | |
8526 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8527 while (*arg && *arg != '|' && !VIM_ISWHITE(*arg)) |
7 | 8528 if (do_autocmd_event(event_name2nr(arg, &arg), pat, |
8529 nested, cmd, forceit, group) == FAIL) | |
8530 break; | |
8531 } | |
8532 | |
8533 if (need_free) | |
8534 vim_free(cmd); | |
8535 vim_free(envpat); | |
8536 } | |
8537 | |
8538 /* | |
8539 * Find the group ID in a ":autocmd" or ":doautocmd" argument. | |
8540 * The "argp" argument is advanced to the following argument. | |
8541 * | |
8542 * Returns the group ID, AUGROUP_ERROR for error (out of memory). | |
8543 */ | |
8544 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8545 au_get_grouparg(char_u **argp) |
7 | 8546 { |
8547 char_u *group_name; | |
8548 char_u *p; | |
8549 char_u *arg = *argp; | |
8550 int group = AUGROUP_ALL; | |
8551 | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
8552 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
|
8553 ; |
7 | 8554 if (p > arg) |
8555 { | |
8556 group_name = vim_strnsave(arg, (int)(p - arg)); | |
8557 if (group_name == NULL) /* out of memory */ | |
8558 return AUGROUP_ERROR; | |
8559 group = au_find_group(group_name); | |
8560 if (group == AUGROUP_ERROR) | |
8561 group = AUGROUP_ALL; /* no match, use all groups */ | |
8562 else | |
8563 *argp = skipwhite(p); /* match, skip over group name */ | |
8564 vim_free(group_name); | |
8565 } | |
8566 return group; | |
8567 } | |
8568 | |
8569 /* | |
8570 * do_autocmd() for one event. | |
8571 * If *pat == NUL do for all patterns. | |
8572 * If *cmd == NUL show entries. | |
8573 * If forceit == TRUE delete entries. | |
8574 * If group is not AUGROUP_ALL, only use this group. | |
8575 */ | |
8576 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8577 do_autocmd_event( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8578 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8579 char_u *pat, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8580 int nested, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8581 char_u *cmd, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8582 int forceit, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8583 int group) |
7 | 8584 { |
8585 AutoPat *ap; | |
8586 AutoPat **prev_ap; | |
8587 AutoCmd *ac; | |
8588 AutoCmd **prev_ac; | |
8589 int brace_level; | |
8590 char_u *endpat; | |
8591 int findgroup; | |
8592 int allgroups; | |
8593 int patlen; | |
856 | 8594 int is_buflocal; |
40 | 8595 int buflocal_nr; |
8596 char_u buflocal_pat[25]; /* for "<buffer=X>" */ | |
7 | 8597 |
8598 if (group == AUGROUP_ALL) | |
8599 findgroup = current_augroup; | |
8600 else | |
8601 findgroup = group; | |
8602 allgroups = (group == AUGROUP_ALL && !forceit && *cmd == NUL); | |
8603 | |
8604 /* | |
8605 * Show or delete all patterns for an event. | |
8606 */ | |
8607 if (*pat == NUL) | |
8608 { | |
8609 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
8610 { | |
8611 if (forceit) /* delete the AutoPat, if it's in the current group */ | |
8612 { | |
8613 if (ap->group == findgroup) | |
8614 au_remove_pat(ap); | |
8615 } | |
8616 else if (group == AUGROUP_ALL || ap->group == group) | |
8617 show_autocmd(ap, event); | |
8618 } | |
8619 } | |
8620 | |
8621 /* | |
8622 * Loop through all the specified patterns. | |
8623 */ | |
8624 for ( ; *pat; pat = (*endpat == ',' ? endpat + 1 : endpat)) | |
8625 { | |
8626 /* | |
8627 * Find end of the pattern. | |
8628 * Watch out for a comma in braces, like "*.\{obj,o\}". | |
8629 */ | |
8630 brace_level = 0; | |
8631 for (endpat = pat; *endpat && (*endpat != ',' || brace_level | |
6963 | 8632 || (endpat > pat && endpat[-1] == '\\')); ++endpat) |
7 | 8633 { |
8634 if (*endpat == '{') | |
8635 brace_level++; | |
8636 else if (*endpat == '}') | |
8637 brace_level--; | |
8638 } | |
8639 if (pat == endpat) /* ignore single comma */ | |
8640 continue; | |
8641 patlen = (int)(endpat - pat); | |
8642 | |
8643 /* | |
40 | 8644 * detect special <buflocal[=X]> buffer-local patterns |
8645 */ | |
8646 is_buflocal = FALSE; | |
8647 buflocal_nr = 0; | |
8648 | |
6623 | 8649 if (patlen >= 8 && STRNCMP(pat, "<buffer", 7) == 0 |
40 | 8650 && pat[patlen - 1] == '>') |
8651 { | |
6623 | 8652 /* "<buffer...>": Error will be printed only for addition. |
8653 * printing and removing will proceed silently. */ | |
40 | 8654 is_buflocal = TRUE; |
8655 if (patlen == 8) | |
6623 | 8656 /* "<buffer>" */ |
40 | 8657 buflocal_nr = curbuf->b_fnum; |
8658 else if (patlen > 9 && pat[7] == '=') | |
8659 { | |
6623 | 8660 if (patlen == 13 && STRNICMP(pat, "<buffer=abuf>", 13) == 0) |
8661 /* "<buffer=abuf>" */ | |
40 | 8662 buflocal_nr = autocmd_bufnr; |
8663 else if (skipdigits(pat + 8) == pat + patlen - 1) | |
6623 | 8664 /* "<buffer=123>" */ |
40 | 8665 buflocal_nr = atoi((char *)pat + 8); |
8666 } | |
8667 } | |
8668 | |
8669 if (is_buflocal) | |
8670 { | |
8671 /* normalize pat into standard "<buffer>#N" form */ | |
8672 sprintf((char *)buflocal_pat, "<buffer=%d>", buflocal_nr); | |
8673 pat = buflocal_pat; /* can modify pat and patlen */ | |
835 | 8674 patlen = (int)STRLEN(buflocal_pat); /* but not endpat */ |
40 | 8675 } |
8676 | |
8677 /* | |
7 | 8678 * Find AutoPat entries with this pattern. |
8679 */ | |
8680 prev_ap = &first_autopat[(int)event]; | |
8681 while ((ap = *prev_ap) != NULL) | |
8682 { | |
8683 if (ap->pat != NULL) | |
8684 { | |
8685 /* Accept a pattern when: | |
8686 * - a group was specified and it's that group, or a group was | |
8687 * not specified and it's the current group, or a group was | |
8688 * not specified and we are listing | |
8689 * - the length of the pattern matches | |
40 | 8690 * - the pattern matches. |
8691 * For <buffer[=X]>, this condition works because we normalize | |
8692 * all buffer-local patterns. | |
7 | 8693 */ |
8694 if ((allgroups || ap->group == findgroup) | |
8695 && ap->patlen == patlen | |
8696 && STRNCMP(pat, ap->pat, patlen) == 0) | |
8697 { | |
8698 /* | |
8699 * Remove existing autocommands. | |
8700 * If adding any new autocmd's for this AutoPat, don't | |
8701 * delete the pattern from the autopat list, append to | |
8702 * this list. | |
8703 */ | |
8704 if (forceit) | |
8705 { | |
8706 if (*cmd != NUL && ap->next == NULL) | |
8707 { | |
8708 au_remove_cmds(ap); | |
8709 break; | |
8710 } | |
8711 au_remove_pat(ap); | |
8712 } | |
8713 | |
8714 /* | |
40 | 8715 * Show autocmd's for this autopat, or buflocals <buffer=X> |
7 | 8716 */ |
8717 else if (*cmd == NUL) | |
8718 show_autocmd(ap, event); | |
8719 | |
8720 /* | |
8721 * Add autocmd to this autopat, if it's the last one. | |
8722 */ | |
8723 else if (ap->next == NULL) | |
8724 break; | |
8725 } | |
8726 } | |
8727 prev_ap = &ap->next; | |
8728 } | |
8729 | |
8730 /* | |
8731 * Add a new command. | |
8732 */ | |
8733 if (*cmd != NUL) | |
8734 { | |
8735 /* | |
8736 * If the pattern we want to add a command to does appear at the | |
8737 * end of the list (or not is not in the list at all), add the | |
8738 * pattern at the end of the list. | |
8739 */ | |
8740 if (ap == NULL) | |
8741 { | |
40 | 8742 /* refuse to add buffer-local ap if buffer number is invalid */ |
8743 if (is_buflocal && (buflocal_nr == 0 | |
8744 || buflist_findnr(buflocal_nr) == NULL)) | |
8745 { | |
8746 EMSGN(_("E680: <buffer=%d>: invalid buffer number "), | |
8747 buflocal_nr); | |
8748 return FAIL; | |
8749 } | |
8750 | |
7 | 8751 ap = (AutoPat *)alloc((unsigned)sizeof(AutoPat)); |
8752 if (ap == NULL) | |
8753 return FAIL; | |
8754 ap->pat = vim_strnsave(pat, patlen); | |
8755 ap->patlen = patlen; | |
8756 if (ap->pat == NULL) | |
8757 { | |
8758 vim_free(ap); | |
8759 return FAIL; | |
8760 } | |
40 | 8761 |
8762 if (is_buflocal) | |
8763 { | |
8764 ap->buflocal_nr = buflocal_nr; | |
153 | 8765 ap->reg_prog = NULL; |
40 | 8766 } |
8767 else | |
7 | 8768 { |
153 | 8769 char_u *reg_pat; |
8770 | |
40 | 8771 ap->buflocal_nr = 0; |
153 | 8772 reg_pat = file_pat_to_reg_pat(pat, endpat, |
40 | 8773 &ap->allow_dirs, TRUE); |
153 | 8774 if (reg_pat != NULL) |
8775 ap->reg_prog = vim_regcomp(reg_pat, RE_MAGIC); | |
355 | 8776 vim_free(reg_pat); |
153 | 8777 if (reg_pat == NULL || ap->reg_prog == NULL) |
40 | 8778 { |
8779 vim_free(ap->pat); | |
8780 vim_free(ap); | |
8781 return FAIL; | |
8782 } | |
7 | 8783 } |
8784 ap->cmds = NULL; | |
8785 *prev_ap = ap; | |
8786 ap->next = NULL; | |
8787 if (group == AUGROUP_ALL) | |
8788 ap->group = current_augroup; | |
8789 else | |
8790 ap->group = group; | |
8791 } | |
8792 | |
8793 /* | |
8794 * Add the autocmd at the end of the AutoCmd list. | |
8795 */ | |
8796 prev_ac = &(ap->cmds); | |
8797 while ((ac = *prev_ac) != NULL) | |
8798 prev_ac = &ac->next; | |
8799 ac = (AutoCmd *)alloc((unsigned)sizeof(AutoCmd)); | |
8800 if (ac == NULL) | |
8801 return FAIL; | |
8802 ac->cmd = vim_strsave(cmd); | |
8803 #ifdef FEAT_EVAL | |
8804 ac->scriptID = current_SID; | |
8805 #endif | |
8806 if (ac->cmd == NULL) | |
8807 { | |
8808 vim_free(ac); | |
8809 return FAIL; | |
8810 } | |
8811 ac->next = NULL; | |
8812 *prev_ac = ac; | |
8813 ac->nested = nested; | |
8814 } | |
8815 } | |
8816 | |
8817 au_cleanup(); /* may really delete removed patterns/commands now */ | |
8818 return OK; | |
8819 } | |
8820 | |
8821 /* | |
8822 * Implementation of ":doautocmd [group] event [fname]". | |
8823 * Return OK for success, FAIL for failure; | |
8824 */ | |
8825 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8826 do_doautocmd( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8827 char_u *arg, |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8828 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
|
8829 int *did_something) |
7 | 8830 { |
8831 char_u *fname; | |
8832 int nothing_done = TRUE; | |
8833 int group; | |
8834 | |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8835 if (did_something != NULL) |
9291
bff3cd5cf922
commit https://github.com/vim/vim/commit/76ae22fef3cb224ca7fbf97517f881e825d4d0c2
Christian Brabandt <cb@256bit.org>
parents:
9260
diff
changeset
|
8836 *did_something = FALSE; |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8837 |
7 | 8838 /* |
8839 * Check for a legal group name. If not, use AUGROUP_ALL. | |
8840 */ | |
8841 group = au_get_grouparg(&arg); | |
8842 if (arg == NULL) /* out of memory */ | |
8843 return FAIL; | |
8844 | |
8845 if (*arg == '*') | |
8846 { | |
8847 EMSG(_("E217: Can't execute autocommands for ALL events")); | |
8848 return FAIL; | |
8849 } | |
8850 | |
8851 /* | |
8852 * Scan over the events. | |
8853 * If we find an illegal name, return here, don't do anything. | |
8854 */ | |
8855 fname = find_end_event(arg, group != AUGROUP_ALL); | |
8856 if (fname == NULL) | |
8857 return FAIL; | |
8858 | |
8859 fname = skipwhite(fname); | |
8860 | |
8861 /* | |
8862 * Loop over the events. | |
8863 */ | |
11641
dbce7e03bf55
patch 8.0.0703: illegal memory access with empty :doau command
Christian Brabandt <cb@256bit.org>
parents:
11605
diff
changeset
|
8864 while (*arg && !ends_excmd(*arg) && !VIM_ISWHITE(*arg)) |
7 | 8865 if (apply_autocmds_group(event_name2nr(arg, &arg), |
8866 fname, NULL, TRUE, group, curbuf, NULL)) | |
8867 nothing_done = FALSE; | |
8868 | |
8869 if (nothing_done && do_msg) | |
8870 MSG(_("No matching autocommands")); | |
9260
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8871 if (did_something != NULL) |
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8872 *did_something = !nothing_done; |
7 | 8873 |
8874 #ifdef FEAT_EVAL | |
8875 return aborting() ? FAIL : OK; | |
8876 #else | |
8877 return OK; | |
8878 #endif | |
8879 } | |
8880 | |
8881 /* | |
8882 * ":doautoall": execute autocommands for each loaded buffer. | |
8883 */ | |
8884 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8885 ex_doautoall(exarg_T *eap) |
7 | 8886 { |
8887 int retval; | |
8888 aco_save_T aco; | |
8889 buf_T *buf; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
8890 bufref_T bufref; |
3342 | 8891 char_u *arg = eap->arg; |
3350 | 8892 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
|
8893 int did_aucmd; |
7 | 8894 |
8895 /* | |
8896 * This is a bit tricky: For some commands curwin->w_buffer needs to be | |
8897 * equal to curbuf, but for some buffers there may not be a window. | |
8898 * So we change the buffer for the current window for a moment. This | |
8899 * gives problems when the autocommands make changes to the list of | |
8900 * buffers or windows... | |
8901 */ | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
8902 FOR_ALL_BUFFERS(buf) |
7 | 8903 { |
1665 | 8904 if (buf->b_ml.ml_mfp != NULL) |
7 | 8905 { |
8906 /* find a window for this buffer and save some values */ | |
8907 aucmd_prepbuf(&aco, buf); | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
8908 set_bufref(&bufref, buf); |
7 | 8909 |
8910 /* 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
|
8911 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
|
8912 |
ac8180818504
commit https://github.com/vim/vim/commit/1610d052413e0ed664498853a47acc2d677a22d1
Christian Brabandt <cb@256bit.org>
parents:
9211
diff
changeset
|
8913 if (call_do_modelines && did_aucmd) |
3342 | 8914 { |
8915 /* Execute the modeline settings, but don't set window-local | |
8916 * options if we are using the current window for another | |
8917 * buffer. */ | |
8918 do_modelines(curwin == aucmd_win ? OPT_NOWIN : 0); | |
8919 } | |
7 | 8920 |
8921 /* restore the current window */ | |
8922 aucmd_restbuf(&aco); | |
8923 | |
8924 /* 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
|
8925 if (retval == FAIL || !bufref_valid(&bufref)) |
7 | 8926 break; |
8927 } | |
8928 } | |
8929 | |
8930 check_cursor(); /* just in case lines got deleted */ | |
8931 } | |
8932 | |
8933 /* | |
3350 | 8934 * Check *argp for <nomodeline>. When it is present return FALSE, otherwise |
8935 * return TRUE and advance *argp to after it. | |
8936 * Thus return TRUE when do_modelines() should be called. | |
8937 */ | |
8938 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8939 check_nomodeline(char_u **argp) |
3350 | 8940 { |
8941 if (STRNCMP(*argp, "<nomodeline>", 12) == 0) | |
8942 { | |
8943 *argp = skipwhite(*argp + 12); | |
8944 return FALSE; | |
8945 } | |
8946 return TRUE; | |
8947 } | |
8948 | |
8949 /* | |
7 | 8950 * Prepare for executing autocommands for (hidden) buffer "buf". |
1906 | 8951 * Search for a visible window containing the current buffer. If there isn't |
8952 * one then use "aucmd_win". | |
7 | 8953 * Set "curbuf" and "curwin" to match "buf". |
934 | 8954 * When FEAT_AUTOCMD is not defined another version is used, see below. |
7 | 8955 */ |
8956 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8957 aucmd_prepbuf( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
8958 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
|
8959 buf_T *buf) /* new curbuf */ |
7 | 8960 { |
8961 win_T *win; | |
1906 | 8962 int save_ea; |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8963 #ifdef FEAT_AUTOCHDIR |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
8964 int save_acd; |
5278
d96f16667cc4
updated for version 7.4b.015
Bram Moolenaar <bram@vim.org>
parents:
5263
diff
changeset
|
8965 #endif |
7 | 8966 |
8967 /* Find a window that is for the new buffer */ | |
8968 if (buf == curbuf) /* be quick when buf is curbuf */ | |
8969 win = curwin; | |
8970 else | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
8971 FOR_ALL_WINDOWS(win) |
7 | 8972 if (win->w_buffer == buf) |
8973 break; | |
8974 | |
1906 | 8975 /* Allocate "aucmd_win" when needed. If this fails (out of memory) fall |
8976 * back to using the current window. */ | |
8977 if (win == NULL && aucmd_win == NULL) | |
8978 { | |
8979 win_alloc_aucmd_win(); | |
8980 if (aucmd_win == NULL) | |
8981 win = curwin; | |
8982 } | |
1944 | 8983 if (win == NULL && aucmd_win_used) |
8984 /* Strange recursive autocommand, fall back to using the current | |
8985 * window. Expect a few side effects... */ | |
8986 win = curwin; | |
1906 | 8987 |
8988 aco->save_curwin = curwin; | |
8989 aco->save_curbuf = curbuf; | |
7 | 8990 if (win != NULL) |
8991 { | |
1906 | 8992 /* There is a window for "buf" in the current tab page, make it the |
8993 * curwin. This is preferred, it has the least side effects (esp. if | |
8994 * "buf" is curbuf). */ | |
1944 | 8995 aco->use_aucmd_win = FALSE; |
7 | 8996 curwin = win; |
8997 } | |
8998 else | |
8999 { | |
1906 | 9000 /* There is no window for "buf", use "aucmd_win". To minimize the side |
4352 | 9001 * effects, insert it in the current tab page. |
1906 | 9002 * Anything related to a window (e.g., setting folds) may have |
9003 * unexpected results. */ | |
1944 | 9004 aco->use_aucmd_win = TRUE; |
9005 aucmd_win_used = TRUE; | |
1918 | 9006 aucmd_win->w_buffer = buf; |
2258
772bfca06c18
Fix crash when using ":grep".
Bram Moolenaar <bram@vim.org>
parents:
2244
diff
changeset
|
9007 aucmd_win->w_s = &buf->b_s; |
7 | 9008 ++buf->b_nwindows; |
1918 | 9009 win_init_empty(aucmd_win); /* set cursor and topline to safe values */ |
1944 | 9010 |
9011 /* Make sure w_localdir and globaldir are NULL to avoid a chdir() in | |
9012 * win_enter_ext(). */ | |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9013 vim_free(aucmd_win->w_localdir); |
1944 | 9014 aucmd_win->w_localdir = NULL; |
9015 aco->globaldir = globaldir; | |
9016 globaldir = NULL; | |
9017 | |
7 | 9018 |
1923 | 9019 /* Split the current window, put the aucmd_win in the upper half. |
9020 * We don't want the BufEnter or WinEnter autocommands. */ | |
9021 block_autocmds(); | |
1906 | 9022 make_snapshot(SNAP_AUCMD_IDX); |
9023 save_ea = p_ea; | |
9024 p_ea = FALSE; | |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9025 |
12477
68d7bc045dbe
patch 8.0.1118: FEAT_WINDOWS adds a lot of #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
12216
diff
changeset
|
9026 #ifdef FEAT_AUTOCHDIR |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9027 /* 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
|
9028 save_acd = p_acd; |
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9029 p_acd = FALSE; |
12477
68d7bc045dbe
patch 8.0.1118: FEAT_WINDOWS adds a lot of #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
12216
diff
changeset
|
9030 #endif |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9031 |
1906 | 9032 (void)win_split_ins(0, WSP_TOP, aucmd_win, 0); |
9033 (void)win_comp_pos(); /* recompute window positions */ | |
9034 p_ea = save_ea; | |
12477
68d7bc045dbe
patch 8.0.1118: FEAT_WINDOWS adds a lot of #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
12216
diff
changeset
|
9035 #ifdef FEAT_AUTOCHDIR |
5263
3059c799fcd9
updated for version 7.4b.008
Bram Moolenaar <bram@vim.org>
parents:
5259
diff
changeset
|
9036 p_acd = save_acd; |
12477
68d7bc045dbe
patch 8.0.1118: FEAT_WINDOWS adds a lot of #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
12216
diff
changeset
|
9037 #endif |
1923 | 9038 unblock_autocmds(); |
1918 | 9039 curwin = aucmd_win; |
1906 | 9040 } |
7 | 9041 curbuf = buf; |
1906 | 9042 aco->new_curwin = curwin; |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9043 set_bufref(&aco->new_curbuf, curbuf); |
7 | 9044 } |
9045 | |
9046 /* | |
9047 * Cleanup after executing autocommands for a (hidden) buffer. | |
9048 * Restore the window as it was (if possible). | |
934 | 9049 * When FEAT_AUTOCMD is not defined another version is used, see below. |
7 | 9050 */ |
9051 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9052 aucmd_restbuf( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9053 aco_save_T *aco) /* structure holding saved values */ |
7 | 9054 { |
1906 | 9055 int dummy; |
9056 | |
1944 | 9057 if (aco->use_aucmd_win) |
1906 | 9058 { |
9059 --curbuf->b_nwindows; | |
9060 /* Find "aucmd_win", it can't be closed, but it may be in another tab | |
1923 | 9061 * page. Do not trigger autocommands here. */ |
9062 block_autocmds(); | |
1906 | 9063 if (curwin != aucmd_win) |
9064 { | |
9065 tabpage_T *tp; | |
9066 win_T *wp; | |
9067 | |
9068 FOR_ALL_TAB_WINDOWS(tp, wp) | |
9069 { | |
9070 if (wp == aucmd_win) | |
9071 { | |
9072 if (tp != curtab) | |
4354 | 9073 goto_tabpage_tp(tp, TRUE, TRUE); |
1906 | 9074 win_goto(aucmd_win); |
3340 | 9075 goto win_found; |
1906 | 9076 } |
9077 } | |
9078 } | |
3340 | 9079 win_found: |
1906 | 9080 |
9081 /* Remove the window and frame from the tree of frames. */ | |
9082 (void)winframe_remove(curwin, &dummy, NULL); | |
9083 win_remove(curwin, NULL); | |
1944 | 9084 aucmd_win_used = FALSE; |
1906 | 9085 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
|
9086 |
e08ead1d269f
patch 8.0.0486: crash and endless loop when closing windows in autocmd
Christian Brabandt <cb@256bit.org>
parents:
11158
diff
changeset
|
9087 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
|
9088 /* 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
|
9089 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
|
9090 |
1906 | 9091 restore_snapshot(SNAP_AUCMD_IDX, FALSE); |
9092 (void)win_comp_pos(); /* recompute window positions */ | |
1923 | 9093 unblock_autocmds(); |
1906 | 9094 |
9095 if (win_valid(aco->save_curwin)) | |
9096 curwin = aco->save_curwin; | |
9097 else | |
9098 /* Hmm, original window disappeared. Just use the first one. */ | |
9099 curwin = firstwin; | |
12477
68d7bc045dbe
patch 8.0.1118: FEAT_WINDOWS adds a lot of #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
12216
diff
changeset
|
9100 #ifdef FEAT_EVAL |
4287 | 9101 vars_clear(&aucmd_win->w_vars->dv_hashtab); /* free all w: variables */ |
9102 hash_init(&aucmd_win->w_vars->dv_hashtab); /* re-use the hashtab */ | |
1906 | 9103 #endif |
9104 curbuf = curwin->w_buffer; | |
9105 | |
1944 | 9106 vim_free(globaldir); |
9107 globaldir = aco->globaldir; | |
9108 | |
1906 | 9109 /* the buffer contents may have changed */ |
9110 check_cursor(); | |
9111 if (curwin->w_topline > curbuf->b_ml.ml_line_count) | |
9112 { | |
9113 curwin->w_topline = curbuf->b_ml.ml_line_count; | |
9114 #ifdef FEAT_DIFF | |
9115 curwin->w_topfill = 0; | |
9116 #endif | |
9117 } | |
9118 #if defined(FEAT_GUI) | |
9119 /* Hide the scrollbars from the aucmd_win and update. */ | |
9120 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_LEFT], FALSE); | |
9121 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_RIGHT], FALSE); | |
9122 gui_may_update_scrollbars(); | |
9123 #endif | |
9124 } | |
9125 else | |
7 | 9126 { |
9127 /* restore curwin */ | |
9128 if (win_valid(aco->save_curwin)) | |
9129 { | |
1906 | 9130 /* Restore the buffer which was previously edited by curwin, if |
1944 | 9131 * it was changed, we are still the same window and the buffer is |
1906 | 9132 * valid. */ |
7 | 9133 if (curwin == aco->new_curwin |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9134 && curbuf != aco->new_curbuf.br_buf |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9135 && bufref_valid(&aco->new_curbuf) |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9136 && aco->new_curbuf.br_buf->b_ml.ml_mfp != NULL) |
7 | 9137 { |
3497 | 9138 # if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) |
9139 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
|
9140 curwin->w_s = &aco->new_curbuf.br_buf->b_s; |
3497 | 9141 # endif |
7 | 9142 --curbuf->b_nwindows; |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9143 curbuf = aco->new_curbuf.br_buf; |
7 | 9144 curwin->w_buffer = curbuf; |
9145 ++curbuf->b_nwindows; | |
9146 } | |
9147 | |
9148 curwin = aco->save_curwin; | |
9149 curbuf = curwin->w_buffer; | |
6201 | 9150 /* In case the autocommand move the cursor to a position that that |
9151 * not exist in curbuf. */ | |
9152 check_cursor(); | |
7 | 9153 } |
9154 } | |
9155 } | |
9156 | |
9157 static int autocmd_nested = FALSE; | |
9158 | |
9159 /* | |
9160 * Execute autocommands for "event" and file name "fname". | |
9161 * Return TRUE if some commands were executed. | |
9162 */ | |
9163 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9164 apply_autocmds( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9165 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9166 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
|
9167 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
|
9168 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
|
9169 buf_T *buf) /* buffer for <abuf> */ |
7 | 9170 { |
9171 return apply_autocmds_group(event, fname, fname_io, force, | |
9172 AUGROUP_ALL, buf, NULL); | |
9173 } | |
9174 | |
9175 /* | |
9176 * Like apply_autocmds(), but with extra "eap" argument. This takes care of | |
9177 * setting v:filearg. | |
9178 */ | |
9179 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9180 apply_autocmds_exarg( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9181 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9182 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9183 char_u *fname_io, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9184 int force, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9185 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9186 exarg_T *eap) |
7 | 9187 { |
9188 return apply_autocmds_group(event, fname, fname_io, force, | |
9189 AUGROUP_ALL, buf, eap); | |
9190 } | |
9191 | |
9192 /* | |
9193 * Like apply_autocmds(), but handles the caller's retval. If the script | |
9194 * processing is being aborted or if retval is FAIL when inside a try | |
9195 * conditional, no autocommands are executed. If otherwise the autocommands | |
9196 * cause the script to be aborted, retval is set to FAIL. | |
9197 */ | |
9198 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9199 apply_autocmds_retval( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9200 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9201 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
|
9202 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
|
9203 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
|
9204 buf_T *buf, /* buffer for <abuf> */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9205 int *retval) /* pointer to caller's retval */ |
7 | 9206 { |
9207 int did_cmd; | |
9208 | |
532 | 9209 #ifdef FEAT_EVAL |
7 | 9210 if (should_abort(*retval)) |
9211 return FALSE; | |
532 | 9212 #endif |
7 | 9213 |
9214 did_cmd = apply_autocmds_group(event, fname, fname_io, force, | |
9215 AUGROUP_ALL, buf, NULL); | |
532 | 9216 if (did_cmd |
9217 #ifdef FEAT_EVAL | |
9218 && aborting() | |
9219 #endif | |
9220 ) | |
7 | 9221 *retval = FAIL; |
9222 return did_cmd; | |
9223 } | |
9224 | |
609 | 9225 /* |
9226 * Return TRUE when there is a CursorHold autocommand defined. | |
9227 */ | |
7 | 9228 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9229 has_cursorhold(void) |
7 | 9230 { |
661 | 9231 return (first_autopat[(int)(get_real_state() == NORMAL_BUSY |
9232 ? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)] != NULL); | |
7 | 9233 } |
609 | 9234 |
9235 /* | |
9236 * Return TRUE if the CursorHold event can be triggered. | |
9237 */ | |
9238 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9239 trigger_cursorhold(void) |
609 | 9240 { |
661 | 9241 int state; |
9242 | |
2976 | 9243 if (!did_cursorhold |
9244 && has_cursorhold() | |
9245 && !Recording | |
9246 && typebuf.tb_len == 0 | |
978 | 9247 #ifdef FEAT_INS_EXPAND |
9248 && !ins_compl_active() | |
9249 #endif | |
9250 ) | |
661 | 9251 { |
9252 state = get_real_state(); | |
9253 if (state == NORMAL_BUSY || (state & INSERT) != 0) | |
9254 return TRUE; | |
9255 } | |
9256 return FALSE; | |
609 | 9257 } |
661 | 9258 |
9259 /* | |
9260 * Return TRUE when there is a CursorMoved 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_cursormoved(void) |
661 | 9264 { |
9265 return (first_autopat[(int)EVENT_CURSORMOVED] != NULL); | |
9266 } | |
9267 | |
9268 /* | |
9269 * Return TRUE when there is a CursorMovedI 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_cursormovedI(void) |
661 | 9273 { |
9274 return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL); | |
9275 } | |
7 | 9276 |
3390 | 9277 /* |
4232 | 9278 * Return TRUE when there is a TextChanged autocommand defined. |
9279 */ | |
9280 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9281 has_textchanged(void) |
4232 | 9282 { |
9283 return (first_autopat[(int)EVENT_TEXTCHANGED] != NULL); | |
9284 } | |
9285 | |
9286 /* | |
9287 * Return TRUE when there is a TextChangedI autocommand defined. | |
9288 */ | |
9289 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9290 has_textchangedI(void) |
4232 | 9291 { |
9292 return (first_autopat[(int)EVENT_TEXTCHANGEDI] != NULL); | |
9293 } | |
9294 | |
9295 /* | |
3390 | 9296 * Return TRUE when there is an InsertCharPre autocommand defined. |
9297 */ | |
9298 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9299 has_insertcharpre(void) |
3390 | 9300 { |
9301 return (first_autopat[(int)EVENT_INSERTCHARPRE] != NULL); | |
9302 } | |
9303 | |
6154 | 9304 /* |
9305 * Return TRUE when there is an CmdUndefined autocommand defined. | |
9306 */ | |
9307 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9308 has_cmdundefined(void) |
6154 | 9309 { |
9310 return (first_autopat[(int)EVENT_CMDUNDEFINED] != NULL); | |
9311 } | |
9312 | |
9313 /* | |
9314 * Return TRUE when there is an FuncUndefined autocommand defined. | |
9315 */ | |
9316 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9317 has_funcundefined(void) |
6154 | 9318 { |
9319 return (first_autopat[(int)EVENT_FUNCUNDEFINED] != NULL); | |
9320 } | |
9321 | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9322 /* |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
9323 * 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
|
9324 * 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
|
9325 */ |
7 | 9326 static int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9327 apply_autocmds_group( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9328 event_T event, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9329 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
|
9330 char_u *fname_io, /* fname to use for <afile> on cmdline, NULL means |
7 | 9331 use fname */ |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9332 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
|
9333 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
|
9334 buf_T *buf, /* buffer for <abuf> */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9335 exarg_T *eap) /* command arguments */ |
7 | 9336 { |
9337 char_u *sfname = NULL; /* short file name */ | |
9338 char_u *tail; | |
9339 int save_changed; | |
9340 buf_T *old_curbuf; | |
9341 int retval = FALSE; | |
9342 char_u *save_sourcing_name; | |
9343 linenr_T save_sourcing_lnum; | |
9344 char_u *save_autocmd_fname; | |
1723 | 9345 int save_autocmd_fname_full; |
7 | 9346 int save_autocmd_bufnr; |
9347 char_u *save_autocmd_match; | |
9348 int save_autocmd_busy; | |
9349 int save_autocmd_nested; | |
9350 static int nesting = 0; | |
9351 AutoPatCmd patcmd; | |
9352 AutoPat *ap; | |
9353 #ifdef FEAT_EVAL | |
9354 scid_T save_current_SID; | |
9355 void *save_funccalp; | |
9356 char_u *save_cmdarg; | |
9357 long save_cmdbang; | |
9358 #endif | |
9359 static int filechangeshell_busy = FALSE; | |
170 | 9360 #ifdef FEAT_PROFILE |
9361 proftime_T wait_time; | |
9362 #endif | |
6608 | 9363 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
|
9364 save_redo_T save_redo; |
7 | 9365 |
9366 /* | |
9367 * Quickly return if there are no autocommands for this event or | |
9368 * autocommands are blocked. | |
9369 */ | |
11641
dbce7e03bf55
patch 8.0.0703: illegal memory access with empty :doau command
Christian Brabandt <cb@256bit.org>
parents:
11605
diff
changeset
|
9370 if (event == NUM_EVENTS || first_autopat[(int)event] == NULL |
dbce7e03bf55
patch 8.0.0703: illegal memory access with empty :doau command
Christian Brabandt <cb@256bit.org>
parents:
11605
diff
changeset
|
9371 || autocmd_blocked > 0) |
40 | 9372 goto BYPASS_AU; |
7 | 9373 |
9374 /* | |
9375 * When autocommands are busy, new autocommands are only executed when | |
9376 * explicitly enabled with the "nested" flag. | |
9377 */ | |
9378 if (autocmd_busy && !(force || autocmd_nested)) | |
40 | 9379 goto BYPASS_AU; |
7 | 9380 |
9381 #ifdef FEAT_EVAL | |
9382 /* | |
1201 | 9383 * Quickly return when immediately aborting on error, or when an interrupt |
7 | 9384 * occurred or an exception was thrown but not caught. |
9385 */ | |
9386 if (aborting()) | |
40 | 9387 goto BYPASS_AU; |
7 | 9388 #endif |
9389 | |
9390 /* | |
9391 * FileChangedShell never nests, because it can create an endless loop. | |
9392 */ | |
765 | 9393 if (filechangeshell_busy && (event == EVENT_FILECHANGEDSHELL |
9394 || event == EVENT_FILECHANGEDSHELLPOST)) | |
40 | 9395 goto BYPASS_AU; |
7 | 9396 |
9397 /* | |
9398 * Ignore events in 'eventignore'. | |
9399 */ | |
9400 if (event_ignored(event)) | |
40 | 9401 goto BYPASS_AU; |
7 | 9402 |
9403 /* | |
9404 * Allow nesting of autocommands, but restrict the depth, because it's | |
9405 * possible to create an endless loop. | |
9406 */ | |
9407 if (nesting == 10) | |
9408 { | |
9409 EMSG(_("E218: autocommand nesting too deep")); | |
40 | 9410 goto BYPASS_AU; |
7 | 9411 } |
9412 | |
9413 /* | |
9414 * Check if these autocommands are disabled. Used when doing ":all" or | |
9415 * ":ball". | |
9416 */ | |
9417 if ( (autocmd_no_enter | |
9418 && (event == EVENT_WINENTER || event == EVENT_BUFENTER)) | |
9419 || (autocmd_no_leave | |
9420 && (event == EVENT_WINLEAVE || event == EVENT_BUFLEAVE))) | |
40 | 9421 goto BYPASS_AU; |
7 | 9422 |
9423 /* | |
9424 * Save the autocmd_* variables and info about the current buffer. | |
9425 */ | |
9426 save_autocmd_fname = autocmd_fname; | |
1723 | 9427 save_autocmd_fname_full = autocmd_fname_full; |
7 | 9428 save_autocmd_bufnr = autocmd_bufnr; |
9429 save_autocmd_match = autocmd_match; | |
9430 save_autocmd_busy = autocmd_busy; | |
9431 save_autocmd_nested = autocmd_nested; | |
9432 save_changed = curbuf->b_changed; | |
9433 old_curbuf = curbuf; | |
9434 | |
9435 /* | |
9436 * Set the file name to be used for <afile>. | |
1471 | 9437 * Make a copy to avoid that changing a buffer name or directory makes it |
9438 * invalid. | |
7 | 9439 */ |
9440 if (fname_io == NULL) | |
9441 { | |
6935 | 9442 if (event == EVENT_COLORSCHEME || event == EVENT_OPTIONSET) |
5521 | 9443 autocmd_fname = NULL; |
11641
dbce7e03bf55
patch 8.0.0703: illegal memory access with empty :doau command
Christian Brabandt <cb@256bit.org>
parents:
11605
diff
changeset
|
9444 else if (fname != NULL && !ends_excmd(*fname)) |
7 | 9445 autocmd_fname = fname; |
9446 else if (buf != NULL) | |
1723 | 9447 autocmd_fname = buf->b_ffname; |
7 | 9448 else |
9449 autocmd_fname = NULL; | |
9450 } | |
9451 else | |
9452 autocmd_fname = fname_io; | |
1471 | 9453 if (autocmd_fname != NULL) |
1723 | 9454 autocmd_fname = vim_strsave(autocmd_fname); |
9455 autocmd_fname_full = FALSE; /* call FullName_save() later */ | |
7 | 9456 |
9457 /* | |
9458 * Set the buffer number to be used for <abuf>. | |
9459 */ | |
9460 if (buf == NULL) | |
9461 autocmd_bufnr = 0; | |
9462 else | |
9463 autocmd_bufnr = buf->b_fnum; | |
9464 | |
9465 /* | |
9466 * When the file name is NULL or empty, use the file name of buffer "buf". | |
9467 * Always use the full path of the file name to match with, in case | |
9468 * "allow_dirs" is set. | |
9469 */ | |
9470 if (fname == NULL || *fname == NUL) | |
9471 { | |
9472 if (buf == NULL) | |
9473 fname = NULL; | |
9474 else | |
9475 { | |
9476 #ifdef FEAT_SYN_HL | |
9477 if (event == EVENT_SYNTAX) | |
9478 fname = buf->b_p_syn; | |
9479 else | |
9480 #endif | |
9481 if (event == EVENT_FILETYPE) | |
9482 fname = buf->b_p_ft; | |
9483 else | |
9484 { | |
9485 if (buf->b_sfname != NULL) | |
9486 sfname = vim_strsave(buf->b_sfname); | |
9487 fname = buf->b_ffname; | |
9488 } | |
9489 } | |
9490 if (fname == NULL) | |
9491 fname = (char_u *)""; | |
9492 fname = vim_strsave(fname); /* make a copy, so we can change it */ | |
9493 } | |
9494 else | |
9495 { | |
9496 sfname = vim_strsave(fname); | |
5521 | 9497 /* Don't try expanding FileType, Syntax, FuncUndefined, WindowID, |
9498 * ColorScheme or QuickFixCmd* */ | |
161 | 9499 if (event == EVENT_FILETYPE |
9500 || event == EVENT_SYNTAX | |
1867 | 9501 || event == EVENT_FUNCUNDEFINED |
161 | 9502 || event == EVENT_REMOTEREPLY |
649 | 9503 || event == EVENT_SPELLFILEMISSING |
161 | 9504 || event == EVENT_QUICKFIXCMDPRE |
5521 | 9505 || event == EVENT_COLORSCHEME |
6935 | 9506 || event == EVENT_OPTIONSET |
161 | 9507 || event == EVENT_QUICKFIXCMDPOST) |
7 | 9508 fname = vim_strsave(fname); |
9509 else | |
9510 fname = FullName_save(fname, FALSE); | |
9511 } | |
9512 if (fname == NULL) /* out of memory */ | |
9513 { | |
9514 vim_free(sfname); | |
40 | 9515 retval = FALSE; |
9516 goto BYPASS_AU; | |
7 | 9517 } |
9518 | |
9519 #ifdef BACKSLASH_IN_FILENAME | |
9520 /* | |
9521 * Replace all backslashes with forward slashes. This makes the | |
9522 * autocommand patterns portable between Unix and MS-DOS. | |
9523 */ | |
9524 if (sfname != NULL) | |
9525 forward_slash(sfname); | |
9526 forward_slash(fname); | |
9527 #endif | |
9528 | |
9529 #ifdef VMS | |
9530 /* remove version for correct match */ | |
9531 if (sfname != NULL) | |
9532 vms_remove_version(sfname); | |
9533 vms_remove_version(fname); | |
9534 #endif | |
9535 | |
9536 /* | |
9537 * Set the name to be used for <amatch>. | |
9538 */ | |
9539 autocmd_match = fname; | |
9540 | |
9541 | |
9542 /* Don't redraw while doing auto commands. */ | |
9543 ++RedrawingDisabled; | |
9544 save_sourcing_name = sourcing_name; | |
9545 sourcing_name = NULL; /* don't free this one */ | |
9546 save_sourcing_lnum = sourcing_lnum; | |
9547 sourcing_lnum = 0; /* no line number here */ | |
9548 | |
9549 #ifdef FEAT_EVAL | |
9550 save_current_SID = current_SID; | |
9551 | |
170 | 9552 # ifdef FEAT_PROFILE |
788 | 9553 if (do_profiling == PROF_YES) |
170 | 9554 prof_child_enter(&wait_time); /* doesn't count for the caller itself */ |
9555 # endif | |
9556 | |
7 | 9557 /* Don't use local function variables, if called from a function */ |
9558 save_funccalp = save_funccal(); | |
9559 #endif | |
9560 | |
9561 /* | |
9562 * When starting to execute autocommands, save the search patterns. | |
9563 */ | |
9564 if (!autocmd_busy) | |
9565 { | |
9566 save_search_patterns(); | |
7285
a34232b17763
commit https://github.com/vim/vim/commit/20ad69ccfb60ef718bd26387ef0e5424461a643e
Christian Brabandt <cb@256bit.org>
parents:
7170
diff
changeset
|
9567 #ifdef FEAT_INS_EXPAND |
6608 | 9568 if (!ins_compl_active()) |
7285
a34232b17763
commit https://github.com/vim/vim/commit/20ad69ccfb60ef718bd26387ef0e5424461a643e
Christian Brabandt <cb@256bit.org>
parents:
7170
diff
changeset
|
9569 #endif |
6608 | 9570 { |
11325
77f3b7316d8b
patch 8.0.0548: saving the redo buffer only works one time
Christian Brabandt <cb@256bit.org>
parents:
11199
diff
changeset
|
9571 saveRedobuff(&save_redo); |
6608 | 9572 did_save_redobuff = TRUE; |
9573 } | |
7 | 9574 did_filetype = keep_filetype; |
9575 } | |
9576 | |
9577 /* | |
9578 * Note that we are applying autocmds. Some commands need to know. | |
9579 */ | |
9580 autocmd_busy = TRUE; | |
9581 filechangeshell_busy = (event == EVENT_FILECHANGEDSHELL); | |
9582 ++nesting; /* see matching decrement below */ | |
9583 | |
9584 /* Remember that FileType was triggered. Used for did_filetype(). */ | |
9585 if (event == EVENT_FILETYPE) | |
9586 did_filetype = TRUE; | |
9587 | |
9588 tail = gettail(fname); | |
9589 | |
9590 /* Find first autocommand that matches */ | |
9591 patcmd.curpat = first_autopat[(int)event]; | |
9592 patcmd.nextcmd = NULL; | |
9593 patcmd.group = group; | |
9594 patcmd.fname = fname; | |
9595 patcmd.sfname = sfname; | |
9596 patcmd.tail = tail; | |
9597 patcmd.event = event; | |
40 | 9598 patcmd.arg_bufnr = autocmd_bufnr; |
9599 patcmd.next = NULL; | |
7 | 9600 auto_next_pat(&patcmd, FALSE); |
9601 | |
9602 /* found one, start executing the autocommands */ | |
9603 if (patcmd.curpat != NULL) | |
9604 { | |
40 | 9605 /* add to active_apc_list */ |
9606 patcmd.next = active_apc_list; | |
9607 active_apc_list = &patcmd; | |
9608 | |
7 | 9609 #ifdef FEAT_EVAL |
9610 /* 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
|
9611 save_cmdbang = (long)get_vim_var_nr(VV_CMDBANG); |
7 | 9612 if (eap != NULL) |
9613 { | |
9614 save_cmdarg = set_cmdarg(eap, NULL); | |
9615 set_vim_var_nr(VV_CMDBANG, (long)eap->forceit); | |
9616 } | |
9617 else | |
9618 save_cmdarg = NULL; /* avoid gcc warning */ | |
9619 #endif | |
9620 retval = TRUE; | |
9621 /* mark the last pattern, to avoid an endless loop when more patterns | |
9622 * are added when executing autocommands */ | |
9623 for (ap = patcmd.curpat; ap->next != NULL; ap = ap->next) | |
9624 ap->last = FALSE; | |
9625 ap->last = TRUE; | |
9626 check_lnums(TRUE); /* make sure cursor and topline are valid */ | |
9627 do_cmdline(NULL, getnextac, (void *)&patcmd, | |
9628 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT); | |
9629 #ifdef FEAT_EVAL | |
9630 if (eap != NULL) | |
9631 { | |
9632 (void)set_cmdarg(NULL, save_cmdarg); | |
9633 set_vim_var_nr(VV_CMDBANG, save_cmdbang); | |
9634 } | |
9635 #endif | |
40 | 9636 /* delete from active_apc_list */ |
9637 if (active_apc_list == &patcmd) /* just in case */ | |
9638 active_apc_list = patcmd.next; | |
7 | 9639 } |
9640 | |
9641 --RedrawingDisabled; | |
9642 autocmd_busy = save_autocmd_busy; | |
9643 filechangeshell_busy = FALSE; | |
9644 autocmd_nested = save_autocmd_nested; | |
9645 vim_free(sourcing_name); | |
9646 sourcing_name = save_sourcing_name; | |
9647 sourcing_lnum = save_sourcing_lnum; | |
1471 | 9648 vim_free(autocmd_fname); |
7 | 9649 autocmd_fname = save_autocmd_fname; |
1723 | 9650 autocmd_fname_full = save_autocmd_fname_full; |
7 | 9651 autocmd_bufnr = save_autocmd_bufnr; |
9652 autocmd_match = save_autocmd_match; | |
9653 #ifdef FEAT_EVAL | |
9654 current_SID = save_current_SID; | |
9655 restore_funccal(save_funccalp); | |
170 | 9656 # ifdef FEAT_PROFILE |
788 | 9657 if (do_profiling == PROF_YES) |
170 | 9658 prof_child_exit(&wait_time); |
9659 # endif | |
7 | 9660 #endif |
9661 vim_free(fname); | |
9662 vim_free(sfname); | |
9663 --nesting; /* see matching increment above */ | |
9664 | |
9665 /* | |
9666 * When stopping to execute autocommands, restore the search patterns and | |
5958 | 9667 * the redo buffer. Free any buffers in the au_pending_free_buf list and |
9668 * free any windows in the au_pending_free_win list. | |
7 | 9669 */ |
9670 if (!autocmd_busy) | |
9671 { | |
9672 restore_search_patterns(); | |
6608 | 9673 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
|
9674 restoreRedobuff(&save_redo); |
7 | 9675 did_filetype = FALSE; |
5816 | 9676 while (au_pending_free_buf != NULL) |
9677 { | |
9678 buf_T *b = au_pending_free_buf->b_next; | |
9679 vim_free(au_pending_free_buf); | |
9680 au_pending_free_buf = b; | |
9681 } | |
5958 | 9682 while (au_pending_free_win != NULL) |
9683 { | |
9684 win_T *w = au_pending_free_win->w_next; | |
9685 vim_free(au_pending_free_win); | |
9686 au_pending_free_win = w; | |
9687 } | |
7 | 9688 } |
9689 | |
9690 /* | |
9691 * Some events don't set or reset the Changed flag. | |
9692 * Check if still in the same buffer! | |
9693 */ | |
9694 if (curbuf == old_curbuf | |
9695 && (event == EVENT_BUFREADPOST | |
9696 || event == EVENT_BUFWRITEPOST | |
9697 || event == EVENT_FILEAPPENDPOST | |
9698 || event == EVENT_VIMLEAVE | |
9699 || event == EVENT_VIMLEAVEPRE)) | |
9700 { | |
9701 #ifdef FEAT_TITLE | |
9702 if (curbuf->b_changed != save_changed) | |
9703 need_maketitle = TRUE; | |
9704 #endif | |
9705 curbuf->b_changed = save_changed; | |
9706 } | |
9707 | |
9708 au_cleanup(); /* may really delete removed patterns/commands now */ | |
40 | 9709 |
9710 BYPASS_AU: | |
9711 /* When wiping out a buffer make sure all its buffer-local autocommands | |
9712 * are deleted. */ | |
9713 if (event == EVENT_BUFWIPEOUT && buf != NULL) | |
9714 aubuflocal_remove(buf); | |
9715 | |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
9716 if (retval == OK && event == EVENT_FILETYPE) |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
9717 au_did_filetype = TRUE; |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
9718 |
7 | 9719 return retval; |
9720 } | |
9721 | |
1410 | 9722 # ifdef FEAT_EVAL |
9723 static char_u *old_termresponse = NULL; | |
9724 # endif | |
9725 | |
9726 /* | |
9727 * Block triggering autocommands until unblock_autocmd() is called. | |
9728 * Can be used recursively, so long as it's symmetric. | |
9729 */ | |
9730 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9731 block_autocmds(void) |
1410 | 9732 { |
9733 # ifdef FEAT_EVAL | |
9734 /* Remember the value of v:termresponse. */ | |
9735 if (autocmd_blocked == 0) | |
9736 old_termresponse = get_vim_var_str(VV_TERMRESPONSE); | |
9737 # endif | |
9738 ++autocmd_blocked; | |
9739 } | |
9740 | |
9741 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9742 unblock_autocmds(void) |
1410 | 9743 { |
9744 --autocmd_blocked; | |
9745 | |
9746 # ifdef FEAT_EVAL | |
9747 /* When v:termresponse was set while autocommands were blocked, trigger | |
9748 * the autocommands now. Esp. useful when executing a shell command | |
9749 * during startup (vimdiff). */ | |
9750 if (autocmd_blocked == 0 | |
9751 && get_vim_var_str(VV_TERMRESPONSE) != old_termresponse) | |
9752 apply_autocmds(EVENT_TERMRESPONSE, NULL, NULL, FALSE, curbuf); | |
9753 # endif | |
9754 } | |
9755 | |
5008
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9756 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9757 is_autocmd_blocked(void) |
5008
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9758 { |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9759 return autocmd_blocked != 0; |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9760 } |
3717d569027d
updated for version 7.3.1248
Bram Moolenaar <bram@vim.org>
parents:
4872
diff
changeset
|
9761 |
7 | 9762 /* |
9763 * Find next autocommand pattern that matches. | |
9764 */ | |
9765 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9766 auto_next_pat( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9767 AutoPatCmd *apc, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9768 int stop_at_last) /* stop when 'last' flag is set */ |
7 | 9769 { |
9770 AutoPat *ap; | |
9771 AutoCmd *cp; | |
9772 char_u *name; | |
9773 char *s; | |
9774 | |
9775 vim_free(sourcing_name); | |
9776 sourcing_name = NULL; | |
9777 | |
9778 for (ap = apc->curpat; ap != NULL && !got_int; ap = ap->next) | |
9779 { | |
9780 apc->curpat = NULL; | |
9781 | |
1723 | 9782 /* Only use a pattern when it has not been removed, has commands and |
40 | 9783 * the group matches. For buffer-local autocommands only check the |
9784 * buffer number. */ | |
7 | 9785 if (ap->pat != NULL && ap->cmds != NULL |
9786 && (apc->group == AUGROUP_ALL || apc->group == ap->group)) | |
9787 { | |
40 | 9788 /* execution-condition */ |
9789 if (ap->buflocal_nr == 0 | |
6375 | 9790 ? (match_file_pat(NULL, &ap->reg_prog, apc->fname, |
153 | 9791 apc->sfname, apc->tail, ap->allow_dirs)) |
40 | 9792 : ap->buflocal_nr == apc->arg_bufnr) |
7 | 9793 { |
9794 name = event_nr2name(apc->event); | |
9795 s = _("%s Auto commands for \"%s\""); | |
9796 sourcing_name = alloc((unsigned)(STRLEN(s) | |
9797 + STRLEN(name) + ap->patlen + 1)); | |
9798 if (sourcing_name != NULL) | |
9799 { | |
9800 sprintf((char *)sourcing_name, s, | |
9801 (char *)name, (char *)ap->pat); | |
9802 if (p_verbose >= 8) | |
292 | 9803 { |
9804 verbose_enter(); | |
273 | 9805 smsg((char_u *)_("Executing %s"), sourcing_name); |
292 | 9806 verbose_leave(); |
9807 } | |
7 | 9808 } |
9809 | |
9810 apc->curpat = ap; | |
9811 apc->nextcmd = ap->cmds; | |
9812 /* mark last command */ | |
9813 for (cp = ap->cmds; cp->next != NULL; cp = cp->next) | |
9814 cp->last = FALSE; | |
9815 cp->last = TRUE; | |
9816 } | |
9817 line_breakcheck(); | |
9818 if (apc->curpat != NULL) /* found a match */ | |
9819 break; | |
9820 } | |
9821 if (stop_at_last && ap->last) | |
9822 break; | |
9823 } | |
9824 } | |
9825 | |
9826 /* | |
9827 * Get next autocommand command. | |
9828 * Called by do_cmdline() to get the next line for ":if". | |
9829 * Returns allocated string, or NULL for end of autocommands. | |
9830 */ | |
3997 | 9831 char_u * |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9832 getnextac(int c UNUSED, void *cookie, int indent UNUSED) |
7 | 9833 { |
9834 AutoPatCmd *acp = (AutoPatCmd *)cookie; | |
9835 char_u *retval; | |
9836 AutoCmd *ac; | |
9837 | |
9838 /* Can be called again after returning the last line. */ | |
9839 if (acp->curpat == NULL) | |
9840 return NULL; | |
9841 | |
9842 /* repeat until we find an autocommand to execute */ | |
9843 for (;;) | |
9844 { | |
9845 /* skip removed commands */ | |
9846 while (acp->nextcmd != NULL && acp->nextcmd->cmd == NULL) | |
9847 if (acp->nextcmd->last) | |
9848 acp->nextcmd = NULL; | |
9849 else | |
9850 acp->nextcmd = acp->nextcmd->next; | |
9851 | |
9852 if (acp->nextcmd != NULL) | |
9853 break; | |
9854 | |
9855 /* at end of commands, find next pattern that matches */ | |
9856 if (acp->curpat->last) | |
9857 acp->curpat = NULL; | |
9858 else | |
9859 acp->curpat = acp->curpat->next; | |
9860 if (acp->curpat != NULL) | |
9861 auto_next_pat(acp, TRUE); | |
9862 if (acp->curpat == NULL) | |
9863 return NULL; | |
9864 } | |
9865 | |
9866 ac = acp->nextcmd; | |
9867 | |
9868 if (p_verbose >= 9) | |
9869 { | |
292 | 9870 verbose_enter_scroll(); |
273 | 9871 smsg((char_u *)_("autocommand %s"), ac->cmd); |
7 | 9872 msg_puts((char_u *)"\n"); /* don't overwrite this either */ |
292 | 9873 verbose_leave_scroll(); |
7 | 9874 } |
9875 retval = vim_strsave(ac->cmd); | |
9876 autocmd_nested = ac->nested; | |
9877 #ifdef FEAT_EVAL | |
9878 current_SID = ac->scriptID; | |
9879 #endif | |
9880 if (ac->last) | |
9881 acp->nextcmd = NULL; | |
9882 else | |
9883 acp->nextcmd = ac->next; | |
9884 return retval; | |
9885 } | |
9886 | |
9887 /* | |
9888 * Return TRUE if there is a matching autocommand for "fname". | |
40 | 9889 * To account for buffer-local autocommands, function needs to know |
9890 * in which buffer the file will be opened. | |
7 | 9891 */ |
9892 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9893 has_autocmd(event_T event, char_u *sfname, buf_T *buf) |
7 | 9894 { |
9895 AutoPat *ap; | |
9896 char_u *fname; | |
9897 char_u *tail = gettail(sfname); | |
9898 int retval = FALSE; | |
9899 | |
9900 fname = FullName_save(sfname, FALSE); | |
9901 if (fname == NULL) | |
9902 return FALSE; | |
9903 | |
9904 #ifdef BACKSLASH_IN_FILENAME | |
9905 /* | |
9906 * Replace all backslashes with forward slashes. This makes the | |
9907 * autocommand patterns portable between Unix and MS-DOS. | |
9908 */ | |
9909 sfname = vim_strsave(sfname); | |
9910 if (sfname != NULL) | |
9911 forward_slash(sfname); | |
9912 forward_slash(fname); | |
9913 #endif | |
9914 | |
9915 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
9916 if (ap->pat != NULL && ap->cmds != NULL | |
856 | 9917 && (ap->buflocal_nr == 0 |
6375 | 9918 ? match_file_pat(NULL, &ap->reg_prog, |
153 | 9919 fname, sfname, tail, ap->allow_dirs) |
856 | 9920 : buf != NULL && ap->buflocal_nr == buf->b_fnum |
40 | 9921 )) |
7 | 9922 { |
9923 retval = TRUE; | |
9924 break; | |
9925 } | |
9926 | |
9927 vim_free(fname); | |
9928 #ifdef BACKSLASH_IN_FILENAME | |
9929 vim_free(sfname); | |
9930 #endif | |
9931 | |
9932 return retval; | |
9933 } | |
9934 | |
9935 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
9936 /* | |
9937 * Function given to ExpandGeneric() to obtain the list of autocommand group | |
9938 * names. | |
9939 */ | |
9940 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9941 get_augroup_name(expand_T *xp UNUSED, int idx) |
7 | 9942 { |
9943 if (idx == augroups.ga_len) /* add "END" add the end */ | |
9944 return (char_u *)"END"; | |
9945 if (idx >= augroups.ga_len) /* end of list */ | |
9946 return NULL; | |
10084
3e410e6e1986
commit https://github.com/vim/vim/commit/b62cc36a600e2e1e5a1d1d484fef89898c847c4c
Christian Brabandt <cb@256bit.org>
parents:
10058
diff
changeset
|
9947 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
|
9948 /* skip deleted entries */ |
7 | 9949 return (char_u *)""; |
9950 return AUGROUP_NAME(idx); /* return a name */ | |
9951 } | |
9952 | |
9953 static int include_groups = FALSE; | |
9954 | |
9955 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9956 set_context_in_autocmd( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9957 expand_T *xp, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9958 char_u *arg, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
9959 int doautocmd) /* TRUE for :doauto*, FALSE for :autocmd */ |
7 | 9960 { |
9961 char_u *p; | |
9962 int group; | |
9963 | |
9964 /* check for a group name, skip it if present */ | |
9965 include_groups = FALSE; | |
9966 p = arg; | |
9967 group = au_get_grouparg(&arg); | |
9968 if (group == AUGROUP_ERROR) | |
9969 return NULL; | |
9970 /* 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
|
9971 if (*arg == NUL && group != AUGROUP_ALL && !VIM_ISWHITE(arg[-1])) |
7 | 9972 { |
9973 arg = p; | |
9974 group = AUGROUP_ALL; | |
9975 } | |
9976 | |
9977 /* 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
|
9978 for (p = arg; *p != NUL && !VIM_ISWHITE(*p); ++p) |
7 | 9979 if (*p == ',') |
9980 arg = p + 1; | |
9981 if (*p == NUL) | |
9982 { | |
9983 if (group == AUGROUP_ALL) | |
9984 include_groups = TRUE; | |
9985 xp->xp_context = EXPAND_EVENTS; /* expand event name */ | |
9986 xp->xp_pattern = arg; | |
9987 return NULL; | |
9988 } | |
9989 | |
9990 /* skip over pattern */ | |
9991 arg = skipwhite(p); | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
9992 while (*arg && (!VIM_ISWHITE(*arg) || arg[-1] == '\\')) |
7 | 9993 arg++; |
9994 if (*arg) | |
9995 return arg; /* expand (next) command */ | |
9996 | |
9997 if (doautocmd) | |
9998 xp->xp_context = EXPAND_FILES; /* expand file names */ | |
9999 else | |
10000 xp->xp_context = EXPAND_NOTHING; /* pattern is not expanded */ | |
10001 return NULL; | |
10002 } | |
10003 | |
10004 /* | |
10005 * Function given to ExpandGeneric() to obtain the list of event names. | |
10006 */ | |
10007 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10008 get_event_name(expand_T *xp UNUSED, int idx) |
7 | 10009 { |
10010 if (idx < augroups.ga_len) /* First list group names, if wanted */ | |
10011 { | |
9682
a98607bb756c
commit https://github.com/vim/vim/commit/f2c4c391192cab6e923b1a418d4af09106fba25f
Christian Brabandt <cb@256bit.org>
parents:
9653
diff
changeset
|
10012 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
|
10013 || AUGROUP_NAME(idx) == get_deleted_augroup()) |
7 | 10014 return (char_u *)""; /* skip deleted entries */ |
10015 return AUGROUP_NAME(idx); /* return a name */ | |
10016 } | |
10017 return (char_u *)event_names[idx - augroups.ga_len].name; | |
10018 } | |
10019 | |
10020 #endif /* FEAT_CMDL_COMPL */ | |
10021 | |
10022 /* | |
615 | 10023 * Return TRUE if autocmd is supported. |
10024 */ | |
10025 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10026 autocmd_supported(char_u *name) |
615 | 10027 { |
10028 char_u *p; | |
10029 | |
10030 return (event_name2nr(name, &p) != NUM_EVENTS); | |
10031 } | |
10032 | |
10033 /* | |
612 | 10034 * Return TRUE if an autocommand is defined for a group, event and |
10035 * pattern: The group can be omitted to accept any group. "event" and "pattern" | |
10036 * can be NULL to accept any event and pattern. "pattern" can be NULL to accept | |
10037 * any pattern. Buffer-local patterns <buffer> or <buffer=N> are accepted. | |
10038 * Used for: | |
10039 * exists("#Group") or | |
10040 * exists("#Group#Event") or | |
10041 * exists("#Group#Event#pat") or | |
10042 * exists("#Event") or | |
10043 * exists("#Event#pat") | |
7 | 10044 */ |
10045 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10046 au_exists(char_u *arg) |
7 | 10047 { |
612 | 10048 char_u *arg_save; |
10049 char_u *pattern = NULL; | |
7 | 10050 char_u *event_name; |
10051 char_u *p; | |
661 | 10052 event_T event; |
7 | 10053 AutoPat *ap; |
40 | 10054 buf_T *buflocal_buf = NULL; |
612 | 10055 int group; |
10056 int retval = FALSE; | |
10057 | |
615 | 10058 /* Make a copy so that we can change the '#' chars to a NUL. */ |
612 | 10059 arg_save = vim_strsave(arg); |
10060 if (arg_save == NULL) | |
10061 return FALSE; | |
615 | 10062 p = vim_strchr(arg_save, '#'); |
612 | 10063 if (p != NULL) |
10064 *p++ = NUL; | |
10065 | |
10066 /* First, look for an autocmd group name */ | |
10067 group = au_find_group(arg_save); | |
10068 if (group == AUGROUP_ERROR) | |
10069 { | |
10070 /* Didn't match a group name, assume the first argument is an event. */ | |
10071 group = AUGROUP_ALL; | |
10072 event_name = arg_save; | |
10073 } | |
10074 else | |
10075 { | |
10076 if (p == NULL) | |
10077 { | |
10078 /* "Group": group name is present and it's recognized */ | |
10079 retval = TRUE; | |
10080 goto theend; | |
10081 } | |
10082 | |
10083 /* Must be "Group#Event" or "Group#Event#pat". */ | |
10084 event_name = p; | |
10085 p = vim_strchr(event_name, '#'); | |
10086 if (p != NULL) | |
10087 *p++ = NUL; /* "Group#Event#pat" */ | |
10088 } | |
10089 | |
10090 pattern = p; /* "pattern" is NULL when there is no pattern */ | |
7 | 10091 |
10092 /* find the index (enum) for the event name */ | |
10093 event = event_name2nr(event_name, &p); | |
10094 | |
10095 /* return FALSE if the event name is not recognized */ | |
612 | 10096 if (event == NUM_EVENTS) |
10097 goto theend; | |
7 | 10098 |
10099 /* Find the first autocommand for this event. | |
10100 * If there isn't any, return FALSE; | |
10101 * If there is one and no pattern given, return TRUE; */ | |
10102 ap = first_autopat[(int)event]; | |
10103 if (ap == NULL) | |
612 | 10104 goto theend; |
7 | 10105 |
40 | 10106 /* if pattern is "<buffer>", special handling is needed which uses curbuf */ |
10107 /* for pattern "<buffer=N>, fnamecmp() will work fine */ | |
1962 | 10108 if (pattern != NULL && STRICMP(pattern, "<buffer>") == 0) |
40 | 10109 buflocal_buf = curbuf; |
10110 | |
7 | 10111 /* Check if there is an autocommand with the given pattern. */ |
10112 for ( ; ap != NULL; ap = ap->next) | |
40 | 10113 /* only use a pattern when it has not been removed and has commands. */ |
10114 /* For buffer-local autocommands, fnamecmp() works fine. */ | |
7 | 10115 if (ap->pat != NULL && ap->cmds != NULL |
612 | 10116 && (group == AUGROUP_ALL || ap->group == group) |
1962 | 10117 && (pattern == NULL |
10118 || (buflocal_buf == NULL | |
10119 ? fnamecmp(ap->pat, pattern) == 0 | |
10120 : ap->buflocal_nr == buflocal_buf->b_fnum))) | |
612 | 10121 { |
10122 retval = TRUE; | |
10123 break; | |
10124 } | |
10125 | |
10126 theend: | |
10127 vim_free(arg_save); | |
10128 return retval; | |
7 | 10129 } |
40 | 10130 |
934 | 10131 #else /* FEAT_AUTOCMD */ |
10132 | |
10133 /* | |
10134 * Prepare for executing commands for (hidden) buffer "buf". | |
10135 * This is the non-autocommand version, it simply saves "curbuf" and sets | |
10136 * "curbuf" and "curwin" to match "buf". | |
10137 */ | |
10138 void | |
7856
226ed297307f
commit https://github.com/vim/vim/commit/d14e00ea67afbaa8cb4a7e6b1eb306da6a2d5adb
Christian Brabandt <cb@256bit.org>
parents:
7819
diff
changeset
|
10139 aucmd_prepbuf( |
226ed297307f
commit https://github.com/vim/vim/commit/d14e00ea67afbaa8cb4a7e6b1eb306da6a2d5adb
Christian Brabandt <cb@256bit.org>
parents:
7819
diff
changeset
|
10140 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
|
10141 buf_T *buf) /* new curbuf */ |
934 | 10142 { |
1906 | 10143 aco->save_curbuf = curbuf; |
10144 --curbuf->b_nwindows; | |
934 | 10145 curbuf = buf; |
10146 curwin->w_buffer = buf; | |
1906 | 10147 ++curbuf->b_nwindows; |
934 | 10148 } |
10149 | |
10150 /* | |
10151 * Restore after executing commands for a (hidden) buffer. | |
10152 * This is the non-autocommand version. | |
10153 */ | |
10154 void | |
7856
226ed297307f
commit https://github.com/vim/vim/commit/d14e00ea67afbaa8cb4a7e6b1eb306da6a2d5adb
Christian Brabandt <cb@256bit.org>
parents:
7819
diff
changeset
|
10155 aucmd_restbuf( |
226ed297307f
commit https://github.com/vim/vim/commit/d14e00ea67afbaa8cb4a7e6b1eb306da6a2d5adb
Christian Brabandt <cb@256bit.org>
parents:
7819
diff
changeset
|
10156 aco_save_T *aco) /* structure holding saved values */ |
934 | 10157 { |
1906 | 10158 --curbuf->b_nwindows; |
10159 curbuf = aco->save_curbuf; | |
934 | 10160 curwin->w_buffer = curbuf; |
1906 | 10161 ++curbuf->b_nwindows; |
934 | 10162 } |
10163 | |
7 | 10164 #endif /* FEAT_AUTOCMD */ |
10165 | |
934 | 10166 |
7 | 10167 #if defined(FEAT_AUTOCMD) || defined(FEAT_WILDIGN) || defined(PROTO) |
10168 /* | |
153 | 10169 * Try matching a filename with a "pattern" ("prog" is NULL), or use the |
10170 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling | |
10171 * vim_regcomp() often. | |
7 | 10172 * Used for autocommands and 'wildignore'. |
10173 * Returns TRUE if there is a match, FALSE otherwise. | |
10174 */ | |
6375 | 10175 static int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10176 match_file_pat( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10177 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
|
10178 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
|
10179 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
|
10180 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
|
10181 char_u *tail, /* tail of path */ |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10182 int allow_dirs) /* allow matching with dir */ |
7 | 10183 { |
10184 regmatch_T regmatch; | |
10185 int result = FALSE; | |
10186 | |
4242 | 10187 regmatch.rm_ic = p_fic; /* ignore case if 'fileignorecase' is set */ |
6470 | 10188 if (prog != NULL) |
10189 regmatch.regprog = *prog; | |
7 | 10190 else |
6470 | 10191 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC); |
7 | 10192 |
10193 /* | |
10194 * Try for a match with the pattern with: | |
10195 * 1. the full file name, when the pattern has a '/'. | |
10196 * 2. the short file name, when the pattern has a '/'. | |
10197 * 3. the tail of the file name, when the pattern has no '/'. | |
10198 */ | |
6470 | 10199 if (regmatch.regprog != NULL |
7 | 10200 && ((allow_dirs |
10201 && (vim_regexec(®match, fname, (colnr_T)0) | |
10202 || (sfname != NULL | |
10203 && vim_regexec(®match, sfname, (colnr_T)0)))) | |
6470 | 10204 || (!allow_dirs && vim_regexec(®match, tail, (colnr_T)0)))) |
7 | 10205 result = TRUE; |
10206 | |
6375 | 10207 if (prog != NULL) |
10208 *prog = regmatch.regprog; | |
10209 else | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4354
diff
changeset
|
10210 vim_regfree(regmatch.regprog); |
7 | 10211 return result; |
10212 } | |
10213 #endif | |
10214 | |
10215 #if defined(FEAT_WILDIGN) || defined(PROTO) | |
10216 /* | |
10217 * Return TRUE if a file matches with a pattern in "list". | |
10218 * "list" is a comma-separated list of patterns, like 'wildignore'. | |
10219 * "sfname" is the short file name or NULL, "ffname" the long file name. | |
10220 */ | |
10221 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10222 match_file_list(char_u *list, char_u *sfname, char_u *ffname) |
7 | 10223 { |
10224 char_u buf[100]; | |
10225 char_u *tail; | |
10226 char_u *regpat; | |
10227 char allow_dirs; | |
10228 int match; | |
10229 char_u *p; | |
10230 | |
10231 tail = gettail(sfname); | |
10232 | |
10233 /* try all patterns in 'wildignore' */ | |
10234 p = list; | |
10235 while (*p) | |
10236 { | |
10237 copy_option_part(&p, buf, 100, ","); | |
10238 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE); | |
10239 if (regpat == NULL) | |
10240 break; | |
153 | 10241 match = match_file_pat(regpat, NULL, ffname, sfname, |
10242 tail, (int)allow_dirs); | |
7 | 10243 vim_free(regpat); |
10244 if (match) | |
10245 return TRUE; | |
10246 } | |
10247 return FALSE; | |
10248 } | |
10249 #endif | |
10250 | |
10251 /* | |
10252 * Convert the given pattern "pat" which has shell style wildcards in it, into | |
10253 * a regular expression, and return the result in allocated memory. If there | |
10254 * is a directory path separator to be matched, then TRUE is put in | |
10255 * allow_dirs, otherwise FALSE is put there -- webb. | |
10256 * Handle backslashes before special characters, like "\*" and "\ ". | |
10257 * | |
10258 * Returns NULL when out of memory. | |
10259 */ | |
10260 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10261 file_pat_to_reg_pat( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10262 char_u *pat, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10263 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
|
10264 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
|
10265 int no_bslash UNUSED) /* Don't use a backward slash as pathsep */ |
7 | 10266 { |
6470 | 10267 int size = 2; /* '^' at start, '$' at end */ |
7 | 10268 char_u *endp; |
10269 char_u *reg_pat; | |
10270 char_u *p; | |
10271 int i; | |
10272 int nested = 0; | |
10273 int add_dollar = TRUE; | |
10274 | |
10275 if (allow_dirs != NULL) | |
10276 *allow_dirs = FALSE; | |
10277 if (pat_end == NULL) | |
10278 pat_end = pat + STRLEN(pat); | |
10279 | |
10280 for (p = pat; p < pat_end; p++) | |
10281 { | |
10282 switch (*p) | |
10283 { | |
10284 case '*': | |
10285 case '.': | |
10286 case ',': | |
10287 case '{': | |
10288 case '}': | |
10289 case '~': | |
10290 size += 2; /* extra backslash */ | |
10291 break; | |
10292 #ifdef BACKSLASH_IN_FILENAME | |
10293 case '\\': | |
10294 case '/': | |
10295 size += 4; /* could become "[\/]" */ | |
10296 break; | |
10297 #endif | |
10298 default: | |
10299 size++; | |
6999 | 10300 # ifdef FEAT_MBYTE |
474 | 10301 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 10302 { |
10303 ++p; | |
10304 ++size; | |
10305 } | |
10306 # endif | |
10307 break; | |
10308 } | |
10309 } | |
10310 reg_pat = alloc(size + 1); | |
10311 if (reg_pat == NULL) | |
10312 return NULL; | |
10313 | |
10314 i = 0; | |
10315 | |
10316 if (pat[0] == '*') | |
10317 while (pat[0] == '*' && pat < pat_end - 1) | |
10318 pat++; | |
10319 else | |
10320 reg_pat[i++] = '^'; | |
10321 endp = pat_end - 1; | |
7005 | 10322 if (endp >= pat && *endp == '*') |
7 | 10323 { |
10324 while (endp - pat > 0 && *endp == '*') | |
10325 endp--; | |
10326 add_dollar = FALSE; | |
10327 } | |
10328 for (p = pat; *p && nested >= 0 && p <= endp; p++) | |
10329 { | |
10330 switch (*p) | |
10331 { | |
10332 case '*': | |
10333 reg_pat[i++] = '.'; | |
10334 reg_pat[i++] = '*'; | |
444 | 10335 while (p[1] == '*') /* "**" matches like "*" */ |
10336 ++p; | |
7 | 10337 break; |
10338 case '.': | |
10339 case '~': | |
10340 reg_pat[i++] = '\\'; | |
10341 reg_pat[i++] = *p; | |
10342 break; | |
10343 case '?': | |
10344 reg_pat[i++] = '.'; | |
10345 break; | |
10346 case '\\': | |
10347 if (p[1] == NUL) | |
10348 break; | |
10349 #ifdef BACKSLASH_IN_FILENAME | |
10350 if (!no_bslash) | |
10351 { | |
10352 /* translate: | |
10353 * "\x" to "\\x" e.g., "dir\file" | |
10354 * "\*" to "\\.*" e.g., "dir\*.c" | |
10355 * "\?" to "\\." e.g., "dir\??.c" | |
10356 * "\+" to "\+" e.g., "fileX\+.c" | |
10357 */ | |
10358 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?') | |
10359 && p[1] != '+') | |
10360 { | |
10361 reg_pat[i++] = '['; | |
10362 reg_pat[i++] = '\\'; | |
10363 reg_pat[i++] = '/'; | |
10364 reg_pat[i++] = ']'; | |
10365 if (allow_dirs != NULL) | |
10366 *allow_dirs = TRUE; | |
10367 break; | |
10368 } | |
10369 } | |
10370 #endif | |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10371 /* Undo escaping from ExpandEscape(): |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10372 * foo\?bar -> foo?bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10373 * foo\%bar -> foo%bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10374 * foo\,bar -> foo,bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10375 * foo\ bar -> foo bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10376 * 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
|
10377 * regexp. |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
5008
diff
changeset
|
10378 * 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
|
10379 * 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
|
10380 */ |
7 | 10381 if (*++p == '?' |
10382 #ifdef BACKSLASH_IN_FILENAME | |
10383 && no_bslash | |
10384 #endif | |
10385 ) | |
10386 reg_pat[i++] = '?'; | |
10387 else | |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
5008
diff
changeset
|
10388 if (*p == ',' || *p == '%' || *p == '#' |
6999 | 10389 || vim_isspace(*p) || *p == '{' || *p == '}') |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10390 reg_pat[i++] = *p; |
5259
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10391 else if (*p == '\\' && p[1] == '\\' && p[2] == '{') |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10392 { |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10393 reg_pat[i++] = '\\'; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10394 reg_pat[i++] = '{'; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10395 p += 2; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
10396 } |
7 | 10397 else |
10398 { | |
10399 if (allow_dirs != NULL && vim_ispathsep(*p) | |
10400 #ifdef BACKSLASH_IN_FILENAME | |
10401 && (!no_bslash || *p != '\\') | |
10402 #endif | |
10403 ) | |
10404 *allow_dirs = TRUE; | |
10405 reg_pat[i++] = '\\'; | |
10406 reg_pat[i++] = *p; | |
10407 } | |
10408 break; | |
10409 #ifdef BACKSLASH_IN_FILENAME | |
10410 case '/': | |
10411 reg_pat[i++] = '['; | |
10412 reg_pat[i++] = '\\'; | |
10413 reg_pat[i++] = '/'; | |
10414 reg_pat[i++] = ']'; | |
10415 if (allow_dirs != NULL) | |
10416 *allow_dirs = TRUE; | |
10417 break; | |
10418 #endif | |
10419 case '{': | |
10420 reg_pat[i++] = '\\'; | |
10421 reg_pat[i++] = '('; | |
10422 nested++; | |
10423 break; | |
10424 case '}': | |
10425 reg_pat[i++] = '\\'; | |
10426 reg_pat[i++] = ')'; | |
10427 --nested; | |
10428 break; | |
10429 case ',': | |
10430 if (nested) | |
10431 { | |
10432 reg_pat[i++] = '\\'; | |
10433 reg_pat[i++] = '|'; | |
10434 } | |
10435 else | |
10436 reg_pat[i++] = ','; | |
10437 break; | |
10438 default: | |
10439 # ifdef FEAT_MBYTE | |
474 | 10440 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 10441 reg_pat[i++] = *p++; |
10442 else | |
10443 # endif | |
10444 if (allow_dirs != NULL && vim_ispathsep(*p)) | |
10445 *allow_dirs = TRUE; | |
10446 reg_pat[i++] = *p; | |
10447 break; | |
10448 } | |
10449 } | |
10450 if (add_dollar) | |
10451 reg_pat[i++] = '$'; | |
10452 reg_pat[i] = NUL; | |
10453 if (nested != 0) | |
10454 { | |
10455 if (nested < 0) | |
10456 EMSG(_("E219: Missing {.")); | |
10457 else | |
10458 EMSG(_("E220: Missing }.")); | |
10459 vim_free(reg_pat); | |
10460 reg_pat = NULL; | |
10461 } | |
10462 return reg_pat; | |
10463 } | |
2664 | 10464 |
10465 #if defined(EINTR) || defined(PROTO) | |
10466 /* | |
10467 * Version of read() that retries when interrupted by EINTR (possibly | |
10468 * by a SIGWINCH). | |
10469 */ | |
10470 long | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10471 read_eintr(int fd, void *buf, size_t bufsize) |
2664 | 10472 { |
10473 long ret; | |
10474 | |
10475 for (;;) | |
10476 { | |
10477 ret = vim_read(fd, buf, bufsize); | |
10478 if (ret >= 0 || errno != EINTR) | |
10479 break; | |
10480 } | |
10481 return ret; | |
10482 } | |
10483 | |
10484 /* | |
10485 * Version of write() that retries when interrupted by EINTR (possibly | |
10486 * by a SIGWINCH). | |
10487 */ | |
10488 long | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
10489 write_eintr(int fd, void *buf, size_t bufsize) |
2664 | 10490 { |
10491 long ret = 0; | |
10492 long wlen; | |
10493 | |
10494 /* Repeat the write() so long it didn't fail, other than being interrupted | |
10495 * by a signal. */ | |
10496 while (ret < (long)bufsize) | |
10497 { | |
2666 | 10498 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret); |
2664 | 10499 if (wlen < 0) |
10500 { | |
10501 if (errno != EINTR) | |
10502 break; | |
10503 } | |
10504 else | |
10505 ret += wlen; | |
10506 } | |
10507 return ret; | |
10508 } | |
10509 #endif |