Mercurial > vim
annotate src/fileio.c @ 20071:385e2f8aa8c1 v8.2.0591
patch 8.2.0591: MS-Windows: should always support IPv6
Commit: https://github.com/vim/vim/commit/aa82259fef8d08326029c580282b417790230e93
Author: Bram Moolenaar <Bram@vim.org>
Date: Fri Apr 17 20:48:57 2020 +0200
patch 8.2.0591: MS-Windows: should always support IPv6
Problem: MS-Windows: should always support IPv6
Solution: Add build flag. (Ozaki Kiichi, closes https://github.com/vim/vim/issues/5944)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Fri, 17 Apr 2020 21:00:03 +0200 |
parents | c32b295af9c5 |
children | 05b4efb062a7 |
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__) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
17 # include <limits.h> // for SSIZE_MAX |
7 | 18 #endif |
19 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
20 // Is there any system that doesn't have access()? |
574 | 21 #define USE_MCH_ACCESS |
7 | 22 |
17692
1e3ff1eae4c3
patch 8.1.1843: might be freeing memory that was not allocated
Bram Moolenaar <Bram@vim.org>
parents:
17584
diff
changeset
|
23 static char_u *next_fenc(char_u **pp, int *alloced); |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
24 #ifdef FEAT_EVAL |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
25 static char_u *readfile_charconvert(char_u *fname, char_u *fenc, int *fdp); |
7 | 26 #endif |
27 #ifdef FEAT_CRYPT | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
28 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 | 29 #endif |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7657
diff
changeset
|
30 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
|
31 static char_u *check_for_bom(char_u *p, long size, int *lenp, int flags); |
1834 | 32 static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name"); |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
33 |
18199
e2be5a6485f5
patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
18139
diff
changeset
|
34 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
35 filemess( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
36 buf_T *buf, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
37 char_u *name, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
38 char_u *s, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
39 int attr) |
7 | 40 { |
41 int msg_scroll_save; | |
18199
e2be5a6485f5
patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
18139
diff
changeset
|
42 int prev_msg_col = msg_col; |
7 | 43 |
44 if (msg_silent != 0) | |
45 return; | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
46 msg_add_fname(buf, name); // put file name in IObuff with quotes |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
47 // If it's extremely long, truncate it. |
7 | 48 if (STRLEN(IObuff) > IOSIZE - 80) |
49 IObuff[IOSIZE - 80] = NUL; | |
50 STRCAT(IObuff, s); | |
51 /* | |
52 * For the first message may have to start a new line. | |
53 * For further ones overwrite the previous one, reset msg_scroll before | |
54 * calling filemess(). | |
55 */ | |
56 msg_scroll_save = msg_scroll; | |
57 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) | |
58 msg_scroll = FALSE; | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
59 if (!msg_scroll) // wait a bit when overwriting an error msg |
7 | 60 check_for_delay(FALSE); |
61 msg_start(); | |
18199
e2be5a6485f5
patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
18139
diff
changeset
|
62 if (prev_msg_col != 0 && msg_col == 0) |
e2be5a6485f5
patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
18139
diff
changeset
|
63 msg_putchar('\r'); // overwrite any previous message. |
7 | 64 msg_scroll = msg_scroll_save; |
65 msg_scrolled_ign = TRUE; | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
66 // may truncate the message to avoid a hit-return prompt |
7 | 67 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr); |
68 msg_clr_eos(); | |
69 out_flush(); | |
70 msg_scrolled_ign = FALSE; | |
71 } | |
72 | |
73 /* | |
74 * Read lines from file "fname" into the buffer after line "from". | |
75 * | |
76 * 1. We allocate blocks with lalloc, as big as possible. | |
77 * 2. Each block is filled with characters from the file with a single read(). | |
78 * 3. The lines are inserted in the buffer with ml_append(). | |
79 * | |
80 * (caller must check that fname != NULL, unless READ_STDIN is used) | |
81 * | |
82 * "lines_to_skip" is the number of lines that must be skipped | |
83 * "lines_to_read" is the number of lines that are appended | |
84 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM. | |
85 * | |
86 * flags: | |
87 * READ_NEW starting to edit a new buffer | |
88 * READ_FILTER reading filter output | |
89 * READ_STDIN read from stdin instead of a file | |
90 * READ_BUFFER read from curbuf instead of a file (converting after reading | |
91 * stdin) | |
92 * 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
|
93 * 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
|
94 * READ_FIFO read from fifo/socket instead of a file |
7 | 95 * |
10575
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
96 * return FAIL for failure, NOTDONE for directory (failure), or OK |
7 | 97 */ |
98 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
99 readfile( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
100 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
101 char_u *sfname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
102 linenr_T from, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
103 linenr_T lines_to_skip, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
104 linenr_T lines_to_read, |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
105 exarg_T *eap, // can be NULL! |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
106 int flags) |
7 | 107 { |
108 int fd = 0; | |
109 int newfile = (flags & READ_NEW); | |
110 int check_readonly; | |
111 int filtering = (flags & READ_FILTER); | |
112 int read_stdin = (flags & READ_STDIN); | |
113 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
|
114 int read_fifo = (flags & READ_FIFO); |
1486 | 115 int set_options = newfile || read_buffer |
116 || (eap != NULL && eap->read_edit); | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
117 linenr_T read_buf_lnum = 1; // next line to read from curbuf |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
118 colnr_T read_buf_col = 0; // next char to read from this line |
7 | 119 char_u c; |
120 linenr_T lnum = from; | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
121 char_u *ptr = NULL; // pointer into read buffer |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
122 char_u *buffer = NULL; // read buffer |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
123 char_u *new_buffer = NULL; // init to shut up gcc |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
124 char_u *line_start = NULL; // init to shut up gcc |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
125 int wasempty; // buffer was empty before reading |
7 | 126 colnr_T len; |
127 long size = 0; | |
128 char_u *p; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
129 off_T filesize = 0; |
7 | 130 int skip_read = FALSE; |
131 #ifdef FEAT_CRYPT | |
132 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
|
133 int did_ask_for_key = FALSE; |
7 | 134 #endif |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
135 #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
|
136 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
|
137 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
|
138 #endif |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
139 int split = 0; // number of split lines |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
140 #define UNKNOWN 0x0fffffff // file size is unknown |
7 | 141 linenr_T linecnt; |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
142 int error = FALSE; // errors encountered |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
143 int ff_error = EOL_UNKNOWN; // file format with errors |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
144 long linerest = 0; // remaining chars in line |
7 | 145 #ifdef UNIX |
146 int perm = 0; | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
147 int swap_mode = -1; // protection bits for swap file |
7 | 148 #else |
149 int perm; | |
150 #endif | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
151 int fileformat = 0; // end-of-line format |
7 | 152 int keep_fileformat = FALSE; |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
153 stat_T st; |
7 | 154 int file_readonly; |
155 linenr_T skip_count = 0; | |
156 linenr_T read_count = 0; | |
157 int msg_save = msg_scroll; | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
158 linenr_T read_no_eol_lnum = 0; // non-zero lnum when last line of |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
159 // 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
|
160 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
|
161 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
|
162 int try_unix; |
7 | 163 int file_rewind = FALSE; |
164 int can_retry; | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
165 linenr_T conv_error = 0; // line nr with conversion error |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
166 linenr_T illegal_byte = 0; // line nr with illegal byte |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
167 int keep_dest_enc = FALSE; // don't retry when char doesn't fit |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
168 // in destination encoding |
595 | 169 int bad_char_behavior = BAD_REPLACE; |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
170 // BAD_KEEP, BAD_DROP or character to |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
171 // replace with |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
172 char_u *tmpname = NULL; // name of 'charconvert' output file |
7 | 173 int fio_flags = 0; |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
174 char_u *fenc; // fileencoding to use |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
175 int fenc_alloced; // fenc_next is in allocated memory |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
176 char_u *fenc_next = NULL; // next item in 'fencs' or NULL |
7 | 177 int advance_fenc = FALSE; |
178 long real_size = 0; | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
179 #ifdef USE_ICONV |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
180 iconv_t iconv_fd = (iconv_t)-1; // descriptor for iconv() or -1 |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
181 # ifdef FEAT_EVAL |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
182 int did_iconv = FALSE; // TRUE when iconv() failed and trying |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
183 // 'charconvert' next |
7 | 184 # endif |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
185 #endif |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
186 int converted = FALSE; // TRUE if conversion done |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
187 int notconverted = FALSE; // TRUE if conversion wanted but it |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
188 // wasn't possible |
7 | 189 char_u conv_rest[CONV_RESTLEN]; |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
190 int conv_restlen = 0; // nr of bytes in conv_rest[] |
18619
788d76db02ac
patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents:
18199
diff
changeset
|
191 pos_T orig_start; |
2563
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
192 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
|
193 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
|
194 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
|
195 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
|
196 int using_b_fname; |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
197 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
198 au_did_filetype = FALSE; // reset before triggering any autocommands |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
199 |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
200 curbuf->b_no_eol_lnum = 0; // in case it was set by the previous read |
7 | 201 |
202 /* | |
203 * If there is no file name yet, use the one for the read file. | |
204 * BF_NOTEDITED is set to reflect this. | |
205 * Don't do this for a read from a filter. | |
206 * Only do this when 'cpoptions' contains the 'f' flag. | |
207 */ | |
208 if (curbuf->b_ffname == NULL | |
209 && !filtering | |
210 && fname != NULL | |
211 && vim_strchr(p_cpo, CPO_FNAMER) != NULL | |
212 && !(flags & READ_DUMMY)) | |
213 { | |
633 | 214 if (set_rw_fname(fname, sfname) == FAIL) |
215 return FAIL; | |
7 | 216 } |
217 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
218 // Remember the initial values of curbuf, curbuf->b_ffname and |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
219 // curbuf->b_fname to detect whether they are altered as a result of |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
220 // executing nasty autocommands. Also check if "fname" and "sfname" |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
221 // point to one of these values. |
2563
5769dc787ec5
Fix: in compatible mode, in an empty buffer, ":r file" triggered an error
Bram Moolenaar <bram@vim.org>
parents:
2523
diff
changeset
|
222 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
|
223 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
|
224 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
|
225 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
|
226 || (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
|
227 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
|
228 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
229 // After reading a file the cursor line changes but we don't want to |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
230 // display the line. |
167 | 231 ex_no_reprint = TRUE; |
232 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
233 // don't display the file info for another buffer now |
968 | 234 need_fileinfo = FALSE; |
235 | |
7 | 236 /* |
237 * For Unix: Use the short file name whenever possible. | |
238 * Avoids problems with networks and when directory names are changed. | |
239 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to | |
240 * another directory, which we don't detect. | |
241 */ | |
242 if (sfname == NULL) | |
243 sfname = fname; | |
9536
b2aada04d84e
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Christian Brabandt <cb@256bit.org>
parents:
9509
diff
changeset
|
244 #if defined(UNIX) |
7 | 245 fname = sfname; |
246 #endif | |
247 | |
248 /* | |
249 * The BufReadCmd and FileReadCmd events intercept the reading process by | |
250 * executing the associated commands instead. | |
251 */ | |
252 if (!filtering && !read_stdin && !read_buffer) | |
253 { | |
18619
788d76db02ac
patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents:
18199
diff
changeset
|
254 orig_start = curbuf->b_op_start; |
7 | 255 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
256 // Set '[ mark to the line above where the lines go (line 1 if zero). |
7 | 257 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); |
258 curbuf->b_op_start.col = 0; | |
259 | |
260 if (newfile) | |
261 { | |
262 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname, | |
263 FALSE, curbuf, eap)) | |
19836
c32b295af9c5
patch 8.2.0474: cannot use :write when using a plugin with BufWriteCmd
Bram Moolenaar <Bram@vim.org>
parents:
19526
diff
changeset
|
264 { |
c32b295af9c5
patch 8.2.0474: cannot use :write when using a plugin with BufWriteCmd
Bram Moolenaar <Bram@vim.org>
parents:
19526
diff
changeset
|
265 int status = OK; |
7 | 266 #ifdef FEAT_EVAL |
19836
c32b295af9c5
patch 8.2.0474: cannot use :write when using a plugin with BufWriteCmd
Bram Moolenaar <Bram@vim.org>
parents:
19526
diff
changeset
|
267 if (aborting()) |
c32b295af9c5
patch 8.2.0474: cannot use :write when using a plugin with BufWriteCmd
Bram Moolenaar <Bram@vim.org>
parents:
19526
diff
changeset
|
268 status = FAIL; |
7 | 269 #endif |
19836
c32b295af9c5
patch 8.2.0474: cannot use :write when using a plugin with BufWriteCmd
Bram Moolenaar <Bram@vim.org>
parents:
19526
diff
changeset
|
270 // The BufReadCmd code usually uses ":read" to get the text and |
c32b295af9c5
patch 8.2.0474: cannot use :write when using a plugin with BufWriteCmd
Bram Moolenaar <Bram@vim.org>
parents:
19526
diff
changeset
|
271 // perhaps ":file" to change the buffer name. But we should |
c32b295af9c5
patch 8.2.0474: cannot use :write when using a plugin with BufWriteCmd
Bram Moolenaar <Bram@vim.org>
parents:
19526
diff
changeset
|
272 // consider this to work like ":edit", thus reset the |
c32b295af9c5
patch 8.2.0474: cannot use :write when using a plugin with BufWriteCmd
Bram Moolenaar <Bram@vim.org>
parents:
19526
diff
changeset
|
273 // BF_NOTEDITED flag. Then ":write" will work to overwrite the |
c32b295af9c5
patch 8.2.0474: cannot use :write when using a plugin with BufWriteCmd
Bram Moolenaar <Bram@vim.org>
parents:
19526
diff
changeset
|
274 // same file. |
c32b295af9c5
patch 8.2.0474: cannot use :write when using a plugin with BufWriteCmd
Bram Moolenaar <Bram@vim.org>
parents:
19526
diff
changeset
|
275 if (status == OK) |
c32b295af9c5
patch 8.2.0474: cannot use :write when using a plugin with BufWriteCmd
Bram Moolenaar <Bram@vim.org>
parents:
19526
diff
changeset
|
276 curbuf->b_flags &= ~BF_NOTEDITED; |
c32b295af9c5
patch 8.2.0474: cannot use :write when using a plugin with BufWriteCmd
Bram Moolenaar <Bram@vim.org>
parents:
19526
diff
changeset
|
277 return status; |
c32b295af9c5
patch 8.2.0474: cannot use :write when using a plugin with BufWriteCmd
Bram Moolenaar <Bram@vim.org>
parents:
19526
diff
changeset
|
278 } |
7 | 279 } |
280 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname, | |
281 FALSE, NULL, eap)) | |
282 #ifdef FEAT_EVAL | |
283 return aborting() ? FAIL : OK; | |
284 #else | |
285 return OK; | |
286 #endif | |
287 | |
18619
788d76db02ac
patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents:
18199
diff
changeset
|
288 curbuf->b_op_start = orig_start; |
7 | 289 } |
290 | |
291 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0) | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
292 msg_scroll = FALSE; // overwrite previous file message |
7 | 293 else |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
294 msg_scroll = TRUE; // don't overwrite previous file message |
7 | 295 |
296 /* | |
297 * If the name ends in a path separator, we can't open it. Check here, | |
298 * because reading the file may actually work, but then creating the swap | |
299 * file may destroy it! Reported on MS-DOS and Win 95. | |
300 * If the name is too long we might crash further on, quit here. | |
301 */ | |
23 | 302 if (fname != NULL && *fname != NUL) |
303 { | |
39 | 304 p = fname + STRLEN(fname); |
305 if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL) | |
23 | 306 { |
307 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0); | |
308 msg_end(); | |
309 msg_scroll = msg_save; | |
310 return FAIL; | |
311 } | |
7 | 312 } |
313 | |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
314 if (!read_stdin && !read_buffer && !read_fifo) |
5322 | 315 { |
7 | 316 #ifdef UNIX |
5322 | 317 /* |
318 * On Unix it is possible to read a directory, so we have to | |
319 * check for it before the mch_open(). | |
320 */ | |
7 | 321 perm = mch_getperm(fname); |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
322 if (perm >= 0 && !S_ISREG(perm) // not a regular file ... |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
323 && !S_ISFIFO(perm) // ... or fifo |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
324 && !S_ISSOCK(perm) // ... or socket |
1313 | 325 # ifdef OPEN_CHR_FILES |
326 && !(S_ISCHR(perm) && is_dev_fd_file(fname)) | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
327 // ... or a character special file named /dev/fd/<n> |
1313 | 328 # endif |
7 | 329 ) |
330 { | |
10575
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
331 int retval = FAIL; |
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
332 |
7 | 333 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
|
334 { |
7 | 335 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
|
336 retval = NOTDONE; |
01a5f64a7a20
patch 8.0.0177: BufEnter autocommand not fired for a directory
Christian Brabandt <cb@256bit.org>
parents:
10221
diff
changeset
|
337 } |
7 | 338 else |
339 filemess(curbuf, fname, (char_u *)_("is not a file"), 0); | |
340 msg_end(); | |
341 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
|
342 return retval; |
7 | 343 } |
5322 | 344 #endif |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
345 #if defined(MSWIN) |
1006 | 346 /* |
347 * MS-Windows allows opening a device, but we will probably get stuck | |
348 * trying to read it. | |
349 */ | |
350 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE) | |
351 { | |
1303 | 352 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0); |
1006 | 353 msg_end(); |
354 msg_scroll = msg_save; | |
355 return FAIL; | |
356 } | |
5322 | 357 #endif |
358 } | |
1004 | 359 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
360 // Set default or forced 'fileformat' and 'binary'. |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
361 set_file_options(set_options, eap); |
7 | 362 |
363 /* | |
364 * When opening a new file we take the readonly flag from the file. | |
365 * Default is r/w, can be set to r/o below. | |
366 * Don't reset it when in readonly mode | |
367 * Only set/reset b_p_ro when BF_CHECK_RO is set. | |
368 */ | |
369 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO)); | |
164 | 370 if (check_readonly && !readonlymode) |
7 | 371 curbuf->b_p_ro = FALSE; |
372 | |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
373 if (newfile && !read_stdin && !read_buffer && !read_fifo) |
7 | 374 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
375 // Remember time of file. |
7 | 376 if (mch_stat((char *)fname, &st) >= 0) |
377 { | |
378 buf_store_time(curbuf, &st, fname); | |
379 curbuf->b_mtime_read = curbuf->b_mtime; | |
380 #ifdef UNIX | |
381 /* | |
382 * Use the protection bits of the original file for the swap file. | |
383 * This makes it possible for others to read the name of the | |
384 * edited file from the swapfile, but only if they can read the | |
385 * edited file. | |
386 * Remove the "write" and "execute" bits for group and others | |
387 * (they must not write the swapfile). | |
388 * Add the "read" and "write" bits for the user, otherwise we may | |
389 * not be able to write to the file ourselves. | |
390 * Setting the bits is done below, after creating the swap file. | |
391 */ | |
392 swap_mode = (st.st_mode & 0644) | 0600; | |
393 #endif | |
394 #ifdef FEAT_CW_EDITOR | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
395 // Get the FSSpec on MacOS |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
396 // TODO: Update it properly when the buffer name changes |
7 | 397 (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec); |
398 #endif | |
399 #ifdef VMS | |
400 curbuf->b_fab_rfm = st.st_fab_rfm; | |
22 | 401 curbuf->b_fab_rat = st.st_fab_rat; |
402 curbuf->b_fab_mrs = st.st_fab_mrs; | |
7 | 403 #endif |
404 } | |
405 else | |
406 { | |
407 curbuf->b_mtime = 0; | |
408 curbuf->b_mtime_read = 0; | |
409 curbuf->b_orig_size = 0; | |
410 curbuf->b_orig_mode = 0; | |
411 } | |
412 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
413 // Reset the "new file" flag. It will be set again below when the |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
414 // file doesn't exist. |
7 | 415 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W); |
416 } | |
417 | |
418 /* | |
419 * 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
|
420 * for Amiga: check readonly by trying to open the file for writing |
7 | 421 */ |
422 file_readonly = FALSE; | |
423 if (read_stdin) | |
424 { | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
425 #if defined(MSWIN) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
426 // Force binary I/O on stdin to avoid CR-LF -> LF conversion. |
7 | 427 setmode(0, O_BINARY); |
428 #endif | |
429 } | |
430 else if (!read_buffer) | |
431 { | |
432 #ifdef USE_MCH_ACCESS | |
433 if ( | |
434 # ifdef UNIX | |
435 !(perm & 0222) || | |
436 # endif | |
437 mch_access((char *)fname, W_OK)) | |
438 file_readonly = TRUE; | |
439 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
440 #else | |
441 if (!newfile | |
442 || readonlymode | |
443 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0) | |
444 { | |
445 file_readonly = TRUE; | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
446 // try to open ro |
7 | 447 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); |
448 } | |
449 #endif | |
450 } | |
451 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
452 if (fd < 0) // cannot open at all |
7 | 453 { |
454 #ifndef UNIX | |
455 int isdir_f; | |
456 #endif | |
457 msg_scroll = msg_save; | |
458 #ifndef UNIX | |
459 /* | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
460 * On Amiga we can't open a directory, check here. |
7 | 461 */ |
462 isdir_f = (mch_isdir(fname)); | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
463 perm = mch_getperm(fname); // check if the file exists |
7 | 464 if (isdir_f) |
465 { | |
466 filemess(curbuf, sfname, (char_u *)_("is a directory"), 0); | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
467 curbuf->b_p_ro = TRUE; // must use "w!" now |
7 | 468 } |
469 else | |
470 #endif | |
471 if (newfile) | |
472 { | |
2147
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
473 if (perm < 0 |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
474 #ifdef ENOENT |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
475 && errno == ENOENT |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
476 #endif |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
477 ) |
7 | 478 { |
479 /* | |
480 * Set the 'new-file' flag, so that when the file has | |
481 * been created by someone else, a ":w" will complain. | |
482 */ | |
483 curbuf->b_flags |= BF_NEW; | |
484 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
485 // Create a swap file now, so that other Vims are warned |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
486 // that we are editing this file. Don't do this for a |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
487 // "nofile" or "nowrite" buffer type. |
7 | 488 #ifdef FEAT_QUICKFIX |
489 if (!bt_dontwrite(curbuf)) | |
490 #endif | |
1834 | 491 { |
7 | 492 check_need_swap(newfile); |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
493 // SwapExists autocommand may mess things up |
1834 | 494 if (curbuf != old_curbuf |
495 || (using_b_ffname | |
496 && (old_b_ffname != curbuf->b_ffname)) | |
497 || (using_b_fname | |
498 && (old_b_fname != curbuf->b_fname))) | |
499 { | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15442
diff
changeset
|
500 emsg(_(e_auchangedbuf)); |
1834 | 501 return FAIL; |
502 } | |
503 } | |
592 | 504 if (dir_of_file_exists(fname)) |
505 filemess(curbuf, sfname, (char_u *)_("[New File]"), 0); | |
506 else | |
507 filemess(curbuf, sfname, | |
508 (char_u *)_("[New DIRECTORY]"), 0); | |
7 | 509 #ifdef FEAT_VIMINFO |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
510 // Even though this is a new file, it might have been |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
511 // edited before and deleted. Get the old marks. |
7 | 512 check_marks_read(); |
513 #endif | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
514 // Set forced 'fileencoding'. |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
515 if (eap != NULL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
516 set_forced_fenc(eap); |
7 | 517 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname, |
518 FALSE, curbuf, eap); | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
519 // remember the current fileformat |
7 | 520 save_file_ff(curbuf); |
521 | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
522 #if defined(FEAT_EVAL) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
523 if (aborting()) // autocmds may abort script processing |
7 | 524 return FAIL; |
525 #endif | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
526 return OK; // a new file is not an error |
7 | 527 } |
528 else | |
529 { | |
550 | 530 filemess(curbuf, sfname, (char_u *)( |
531 # ifdef EFBIG | |
532 (errno == EFBIG) ? _("[File too big]") : | |
533 # endif | |
2147
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
534 # ifdef EOVERFLOW |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
535 (errno == EOVERFLOW) ? _("[File too big]") : |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
536 # endif |
550 | 537 _("[Permission Denied]")), 0); |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
538 curbuf->b_p_ro = TRUE; // must use "w!" now |
7 | 539 } |
540 } | |
541 | |
542 return FAIL; | |
543 } | |
544 | |
545 /* | |
546 * Only set the 'ro' flag for readonly files the first time they are | |
547 * loaded. Help files always get readonly mode | |
548 */ | |
549 if ((check_readonly && file_readonly) || curbuf->b_help) | |
550 curbuf->b_p_ro = TRUE; | |
551 | |
819 | 552 if (set_options) |
7 | 553 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
554 // Don't change 'eol' if reading from buffer as it will already be |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
555 // correctly set when reading stdin. |
1486 | 556 if (!read_buffer) |
557 { | |
558 curbuf->b_p_eol = TRUE; | |
559 curbuf->b_start_eol = TRUE; | |
560 } | |
7 | 561 curbuf->b_p_bomb = FALSE; |
1352 | 562 curbuf->b_start_bomb = FALSE; |
7 | 563 } |
564 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
565 // Create a swap file now, so that other Vims are warned that we are |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
566 // editing this file. |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
567 // Don't do this for a "nofile" or "nowrite" buffer type. |
7 | 568 #ifdef FEAT_QUICKFIX |
569 if (!bt_dontwrite(curbuf)) | |
570 #endif | |
571 { | |
572 check_need_swap(newfile); | |
1834 | 573 if (!read_stdin && (curbuf != old_curbuf |
574 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) | |
575 || (using_b_fname && (old_b_fname != curbuf->b_fname)))) | |
576 { | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15442
diff
changeset
|
577 emsg(_(e_auchangedbuf)); |
1834 | 578 if (!read_buffer) |
579 close(fd); | |
580 return FAIL; | |
581 } | |
7 | 582 #ifdef UNIX |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
583 // Set swap file protection bits after creating it. |
1918 | 584 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL |
585 && 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
|
586 { |
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
|
587 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
|
588 |
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
|
589 /* |
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
|
590 * 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
|
591 * 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
|
592 * 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
|
593 * 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
|
594 * 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
|
595 */ |
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
|
596 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
|
597 { |
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
|
598 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
|
599 |
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
|
600 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
|
601 && st.st_gid != swap_st.st_gid |
13730
7b818f7bb738
patch 8.0.1737: fchown() used when it is not supported
Christian Brabandt <cb@256bit.org>
parents:
13610
diff
changeset
|
602 # ifdef HAVE_FCHOWN |
12771
8984342ab09e
patch 8.0.1263: others can read the swap file if a user is careless
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
603 && fchown(curbuf->b_ml.ml_mfp->mf_fd, -1, st.st_gid) |
13988
68290d296dc9
patch 8.1.0012: misplaced #endif
Christian Brabandt <cb@256bit.org>
parents:
13819
diff
changeset
|
604 == -1 |
13730
7b818f7bb738
patch 8.0.1737: fchown() used when it is not supported
Christian Brabandt <cb@256bit.org>
parents:
13610
diff
changeset
|
605 # endif |
7b818f7bb738
patch 8.0.1737: fchown() used when it is not supported
Christian Brabandt <cb@256bit.org>
parents:
13610
diff
changeset
|
606 ) |
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
|
607 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
|
608 } |
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
|
609 |
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
|
610 (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
|
611 } |
7 | 612 #endif |
613 } | |
614 | |
16453
4e9bea9b8025
patch 8.1.1231: asking about existing swap file unnecessarily
Bram Moolenaar <Bram@vim.org>
parents:
16451
diff
changeset
|
615 // If "Quit" selected at ATTENTION dialog, don't load the file |
7 | 616 if (swap_exists_action == SEA_QUIT) |
617 { | |
618 if (!read_buffer && !read_stdin) | |
619 close(fd); | |
620 return FAIL; | |
621 } | |
622 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
623 ++no_wait_return; // don't wait for return yet |
7 | 624 |
625 /* | |
626 * Set '[ mark to the line above where the lines go (line 1 if zero). | |
627 */ | |
18619
788d76db02ac
patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents:
18199
diff
changeset
|
628 orig_start = curbuf->b_op_start; |
7 | 629 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); |
630 curbuf->b_op_start.col = 0; | |
631 | |
10668
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
632 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
|
633 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
|
634 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
|
635 |
7 | 636 if (!read_buffer) |
637 { | |
638 int m = msg_scroll; | |
639 int n = msg_scrolled; | |
640 | |
641 /* | |
642 * The file must be closed again, the autocommands may want to change | |
643 * the file before reading it. | |
644 */ | |
645 if (!read_stdin) | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
646 close(fd); // ignore errors |
7 | 647 |
648 /* | |
649 * The output from the autocommands should not overwrite anything and | |
650 * should not be overwritten: Set msg_scroll, restore its value if no | |
651 * output was done. | |
652 */ | |
653 msg_scroll = TRUE; | |
654 if (filtering) | |
655 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname, | |
656 FALSE, curbuf, eap); | |
657 else if (read_stdin) | |
658 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname, | |
659 FALSE, curbuf, eap); | |
660 else if (newfile) | |
661 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname, | |
662 FALSE, curbuf, eap); | |
663 else | |
664 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname, | |
665 FALSE, NULL, eap); | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
666 // autocommands may have changed it |
10668
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
667 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
|
668 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
|
669 try_unix = (vim_strchr(p_ffs, 'x') != NULL); |
18619
788d76db02ac
patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents:
18199
diff
changeset
|
670 curbuf->b_op_start = orig_start; |
10668
6a252c6afd5b
patch 8.0.0224: change to 'fileformats' from autocmd does not take effect
Christian Brabandt <cb@256bit.org>
parents:
10575
diff
changeset
|
671 |
7 | 672 if (msg_scrolled == n) |
673 msg_scroll = m; | |
674 | |
675 #ifdef FEAT_EVAL | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
676 if (aborting()) // autocmds may abort script processing |
7 | 677 { |
678 --no_wait_return; | |
679 msg_scroll = msg_save; | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
680 curbuf->b_p_ro = TRUE; // must use "w!" now |
7 | 681 return FAIL; |
682 } | |
683 #endif | |
684 /* | |
685 * Don't allow the autocommands to change the current buffer. | |
686 * Try to re-open the file. | |
1834 | 687 * |
688 * Don't allow the autocommands to change the buffer name either | |
689 * (cd for example) if it invalidates fname or sfname. | |
7 | 690 */ |
691 if (!read_stdin && (curbuf != old_curbuf | |
1834 | 692 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) |
693 || (using_b_fname && (old_b_fname != curbuf->b_fname)) | |
7 | 694 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0)) |
695 { | |
696 --no_wait_return; | |
697 msg_scroll = msg_save; | |
698 if (fd < 0) | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15442
diff
changeset
|
699 emsg(_("E200: *ReadPre autocommands made the file unreadable")); |
7 | 700 else |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15442
diff
changeset
|
701 emsg(_("E201: *ReadPre autocommands must not change current buffer")); |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
702 curbuf->b_p_ro = TRUE; // must use "w!" now |
7 | 703 return FAIL; |
704 } | |
705 } | |
706 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
707 // Autocommands may add lines to the file, need to check if it is empty |
7 | 708 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY); |
709 | |
710 if (!recoverymode && !filtering && !(flags & READ_DUMMY)) | |
711 { | |
712 /* | |
713 * Show the user that we are busy reading the input. Sometimes this | |
714 * may take a while. When reading from stdin another program may | |
715 * still be running, don't move the cursor to the last line, unless | |
716 * always using the GUI. | |
717 */ | |
718 if (read_stdin) | |
719 { | |
12863
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
720 if (!is_not_a_term()) |
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
721 { |
7 | 722 #ifndef ALWAYS_USE_GUI |
16451
7ae2396cef62
patch 8.1.1230: a lot of code is shared between vim.exe and gvim.exe
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
723 # ifdef VIMDLL |
7ae2396cef62
patch 8.1.1230: a lot of code is shared between vim.exe and gvim.exe
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
724 if (!gui.in_use) |
7ae2396cef62
patch 8.1.1230: a lot of code is shared between vim.exe and gvim.exe
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
725 # endif |
7ae2396cef62
patch 8.1.1230: a lot of code is shared between vim.exe and gvim.exe
Bram Moolenaar <Bram@vim.org>
parents:
16054
diff
changeset
|
726 mch_msg(_("Vim: Reading from stdin...\n")); |
7 | 727 #endif |
728 #ifdef FEAT_GUI | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
729 // Also write a message in the GUI window, if there is one. |
12863
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
730 if (gui.in_use && !gui.dying && !gui.starting) |
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
731 { |
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
732 p = (char_u *)_("Reading from stdin..."); |
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
733 gui_write(p, (int)STRLEN(p)); |
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
734 } |
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
735 #endif |
e7ec107f6354
patch 8.0.1308: the "Reading from stdin" message may be undesired
Christian Brabandt <cb@256bit.org>
parents:
12861
diff
changeset
|
736 } |
7 | 737 } |
738 else if (!read_buffer) | |
739 filemess(curbuf, sfname, (char_u *)"", 0); | |
740 } | |
741 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
742 msg_scroll = FALSE; // overwrite the file message |
7 | 743 |
744 /* | |
745 * Set linecnt now, before the "retry" caused by a wrong guess for | |
746 * fileformat, and after the autocommands, which may change them. | |
747 */ | |
748 linecnt = curbuf->b_ml.ml_line_count; | |
749 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
750 // "++bad=" argument. |
595 | 751 if (eap != NULL && eap->bad_char != 0) |
612 | 752 { |
595 | 753 bad_char_behavior = eap->bad_char; |
819 | 754 if (set_options) |
612 | 755 curbuf->b_bad_char = eap->bad_char; |
756 } | |
757 else | |
758 curbuf->b_bad_char = 0; | |
595 | 759 |
7 | 760 /* |
595 | 761 * Decide which 'encoding' to use or use first. |
7 | 762 */ |
763 if (eap != NULL && eap->force_enc != 0) | |
764 { | |
765 fenc = enc_canonize(eap->cmd + eap->force_enc); | |
766 fenc_alloced = TRUE; | |
595 | 767 keep_dest_enc = TRUE; |
7 | 768 } |
769 else if (curbuf->b_p_bin) | |
770 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
771 fenc = (char_u *)""; // binary: don't convert |
7 | 772 fenc_alloced = FALSE; |
773 } | |
774 else if (curbuf->b_help) | |
775 { | |
776 char_u firstline[80]; | |
301 | 777 int fc; |
7 | 778 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
779 // Help files are either utf-8 or latin1. Try utf-8 first, if this |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
780 // fails it must be latin1. |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
781 // Always do this when 'encoding' is "utf-8". Otherwise only do |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
782 // this when needed to avoid [converted] remarks all the time. |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
783 // It is needed when the first line contains non-ASCII characters. |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
784 // That is only in *.??x files. |
7 | 785 fenc = (char_u *)"latin1"; |
786 c = enc_utf8; | |
301 | 787 if (!c && !read_stdin) |
788 { | |
789 fc = fname[STRLEN(fname) - 1]; | |
790 if (TOLOWER_ASC(fc) == 'x') | |
791 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
792 // Read the first line (and a bit more). Immediately rewind to |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
793 // the start of the file. If the read() fails "len" is -1. |
2664 | 794 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
|
795 vim_lseek(fd, (off_T)0L, SEEK_SET); |
301 | 796 for (p = firstline; p < firstline + len; ++p) |
797 if (*p >= 0x80) | |
798 { | |
799 c = TRUE; | |
800 break; | |
801 } | |
802 } | |
7 | 803 } |
804 | |
805 if (c) | |
806 { | |
807 fenc_next = fenc; | |
808 fenc = (char_u *)"utf-8"; | |
809 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
810 // When the file is utf-8 but a character doesn't fit in |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
811 // 'encoding' don't retry. In help text editing utf-8 bytes |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
812 // doesn't make sense. |
844 | 813 if (!enc_utf8) |
814 keep_dest_enc = TRUE; | |
7 | 815 } |
816 fenc_alloced = FALSE; | |
817 } | |
818 else if (*p_fencs == NUL) | |
819 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
820 fenc = curbuf->b_p_fenc; // use format from buffer |
7 | 821 fenc_alloced = FALSE; |
822 } | |
823 else | |
824 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
825 fenc_next = p_fencs; // try items in 'fileencodings' |
17692
1e3ff1eae4c3
patch 8.1.1843: might be freeing memory that was not allocated
Bram Moolenaar <Bram@vim.org>
parents:
17584
diff
changeset
|
826 fenc = next_fenc(&fenc_next, &fenc_alloced); |
7 | 827 } |
828 | |
829 /* | |
830 * Jump back here to retry reading the file in different ways. | |
831 * Reasons to retry: | |
832 * - encoding conversion failed: try another one from "fenc_next" | |
833 * - BOM detected and fenc was set, need to setup conversion | |
834 * - "fileformat" check failed: try another | |
835 * | |
836 * Variables set for special retry actions: | |
837 * "file_rewind" Rewind the file to start reading it again. | |
838 * "advance_fenc" Advance "fenc" using "fenc_next". | |
839 * "skip_read" Re-use already read bytes (BOM detected). | |
840 * "did_iconv" iconv() conversion failed, try 'charconvert'. | |
841 * "keep_fileformat" Don't reset "fileformat". | |
842 * | |
843 * Other status indicators: | |
844 * "tmpname" When != NULL did conversion with 'charconvert'. | |
845 * Output file has to be deleted afterwards. | |
846 * "iconv_fd" When != -1 did conversion with iconv(). | |
847 */ | |
848 retry: | |
849 | |
850 if (file_rewind) | |
851 { | |
852 if (read_buffer) | |
853 { | |
854 read_buf_lnum = 1; | |
855 read_buf_col = 0; | |
856 } | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
857 else if (read_stdin || vim_lseek(fd, (off_T)0L, SEEK_SET) != 0) |
7 | 858 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
859 // Can't rewind the file, give up. |
7 | 860 error = TRUE; |
861 goto failed; | |
862 } | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
863 // Delete the previously read lines. |
7 | 864 while (lnum > from) |
865 ml_delete(lnum--, FALSE); | |
866 file_rewind = FALSE; | |
819 | 867 if (set_options) |
1352 | 868 { |
7 | 869 curbuf->b_p_bomb = FALSE; |
1352 | 870 curbuf->b_start_bomb = FALSE; |
871 } | |
595 | 872 conv_error = 0; |
7 | 873 } |
874 | |
875 /* | |
876 * When retrying with another "fenc" and the first time "fileformat" | |
877 * will be reset. | |
878 */ | |
879 if (keep_fileformat) | |
880 keep_fileformat = FALSE; | |
881 else | |
882 { | |
883 if (eap != NULL && eap->force_ff != 0) | |
1742 | 884 { |
7 | 885 fileformat = get_fileformat_force(curbuf, eap); |
1742 | 886 try_unix = try_dos = try_mac = FALSE; |
887 } | |
7 | 888 else if (curbuf->b_p_bin) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
889 fileformat = EOL_UNIX; // binary: use Unix format |
7 | 890 else if (*p_ffs == NUL) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
891 fileformat = get_fileformat(curbuf);// use format from buffer |
7 | 892 else |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
893 fileformat = EOL_UNKNOWN; // detect from file |
7 | 894 } |
895 | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
896 #ifdef USE_ICONV |
7 | 897 if (iconv_fd != (iconv_t)-1) |
898 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
899 // aborted conversion with iconv(), close the descriptor |
7 | 900 iconv_close(iconv_fd); |
901 iconv_fd = (iconv_t)-1; | |
902 } | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
903 #endif |
7 | 904 |
905 if (advance_fenc) | |
906 { | |
907 /* | |
908 * Try the next entry in 'fileencodings'. | |
909 */ | |
910 advance_fenc = FALSE; | |
911 | |
912 if (eap != NULL && eap->force_enc != 0) | |
913 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
914 // Conversion given with "++cc=" wasn't possible, read |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
915 // without conversion. |
7 | 916 notconverted = TRUE; |
595 | 917 conv_error = 0; |
7 | 918 if (fenc_alloced) |
919 vim_free(fenc); | |
920 fenc = (char_u *)""; | |
921 fenc_alloced = FALSE; | |
922 } | |
923 else | |
924 { | |
925 if (fenc_alloced) | |
926 vim_free(fenc); | |
927 if (fenc_next != NULL) | |
928 { | |
17692
1e3ff1eae4c3
patch 8.1.1843: might be freeing memory that was not allocated
Bram Moolenaar <Bram@vim.org>
parents:
17584
diff
changeset
|
929 fenc = next_fenc(&fenc_next, &fenc_alloced); |
7 | 930 } |
931 else | |
932 { | |
933 fenc = (char_u *)""; | |
934 fenc_alloced = FALSE; | |
935 } | |
936 } | |
937 if (tmpname != NULL) | |
938 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
939 mch_remove(tmpname); // delete converted file |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
940 VIM_CLEAR(tmpname); |
7 | 941 } |
942 } | |
943 | |
944 /* | |
1948 | 945 * Conversion may be required when the encoding of the file is different |
946 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4. | |
7 | 947 */ |
948 fio_flags = 0; | |
1948 | 949 converted = need_conversion(fenc); |
950 if (converted) | |
7 | 951 { |
952 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
953 // "ucs-bom" means we need to check the first bytes of the file |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
954 // for a BOM. |
7 | 955 if (STRCMP(fenc, ENC_UCSBOM) == 0) |
956 fio_flags = FIO_UCSBOM; | |
957 | |
958 /* | |
959 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be | |
960 * done. This is handled below after read(). Prepare the | |
961 * fio_flags to avoid having to parse the string each time. | |
962 * Also check for Unicode to Latin1 conversion, because iconv() | |
963 * appears not to handle this correctly. This works just like | |
964 * conversion to UTF-8 except how the resulting character is put in | |
965 * the buffer. | |
966 */ | |
967 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
968 fio_flags = get_fio_flags(fenc); | |
969 | |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15840
diff
changeset
|
970 #ifdef MSWIN |
7 | 971 /* |
972 * Conversion from an MS-Windows codepage to UTF-8 or another codepage | |
973 * is handled with MultiByteToWideChar(). | |
974 */ | |
975 if (fio_flags == 0) | |
976 fio_flags = get_win_fio_flags(fenc); | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
977 #endif |
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
978 |
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
979 #ifdef MACOS_CONVERT |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
980 // Conversion from Apple MacRoman to latin1 or UTF-8 |
7 | 981 if (fio_flags == 0) |
982 fio_flags = get_mac_fio_flags(fenc); | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
983 #endif |
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
984 |
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
985 #ifdef USE_ICONV |
7 | 986 /* |
987 * Try using iconv() if we can't convert internally. | |
988 */ | |
989 if (fio_flags == 0 | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
990 # ifdef FEAT_EVAL |
7 | 991 && !did_iconv |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
992 # endif |
7 | 993 ) |
994 iconv_fd = (iconv_t)my_iconv_open( | |
995 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc); | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
996 #endif |
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
997 |
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
998 #ifdef FEAT_EVAL |
7 | 999 /* |
1000 * Use the 'charconvert' expression when conversion is required | |
1001 * and we can't do it internally or with iconv(). | |
1002 */ | |
1003 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
|
1004 && !read_fifo |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1005 # ifdef USE_ICONV |
7 | 1006 && iconv_fd == (iconv_t)-1 |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1007 # endif |
7 | 1008 ) |
1009 { | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1010 # ifdef USE_ICONV |
7 | 1011 did_iconv = FALSE; |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1012 # endif |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1013 // Skip conversion when it's already done (retry for wrong |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1014 // "fileformat"). |
7 | 1015 if (tmpname == NULL) |
1016 { | |
1017 tmpname = readfile_charconvert(fname, fenc, &fd); | |
1018 if (tmpname == NULL) | |
1019 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1020 // Conversion failed. Try another one. |
7 | 1021 advance_fenc = TRUE; |
1022 if (fd < 0) | |
1023 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1024 // Re-opening the original file failed! |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15442
diff
changeset
|
1025 emsg(_("E202: Conversion made file unreadable!")); |
7 | 1026 error = TRUE; |
1027 goto failed; | |
1028 } | |
1029 goto retry; | |
1030 } | |
1031 } | |
1032 } | |
1033 else | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1034 #endif |
7 | 1035 { |
1036 if (fio_flags == 0 | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1037 #ifdef USE_ICONV |
7 | 1038 && iconv_fd == (iconv_t)-1 |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1039 #endif |
7 | 1040 ) |
1041 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1042 // Conversion wanted but we can't. |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1043 // Try the next conversion in 'fileencodings' |
7 | 1044 advance_fenc = TRUE; |
1045 goto retry; | |
1046 } | |
1047 } | |
1048 } | |
1049 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1050 // Set "can_retry" when it's possible to rewind the file and try with |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1051 // another "fenc" value. It's FALSE when no other "fenc" to try, reading |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1052 // 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
|
1053 can_retry = (*fenc != NUL && !read_stdin && !read_fifo && !keep_dest_enc); |
7 | 1054 |
1055 if (!skip_read) | |
1056 { | |
1057 linerest = 0; | |
1058 filesize = 0; | |
1059 skip_count = lines_to_skip; | |
1060 read_count = lines_to_read; | |
1061 conv_restlen = 0; | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1062 #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
|
1063 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
|
1064 && 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
|
1065 && 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
|
1066 && !filtering |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
1067 && !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
|
1068 && !read_stdin |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1069 && !read_buffer); |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1070 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
|
1071 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
|
1072 #endif |
6122 | 1073 #ifdef FEAT_CRYPT |
1074 if (curbuf->b_cryptstate != NULL) | |
1075 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1076 // Need to free the state, but keep the key, don't want to ask for |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1077 // it again. |
6122 | 1078 crypt_free_state(curbuf->b_cryptstate); |
1079 curbuf->b_cryptstate = NULL; | |
1080 } | |
1081 #endif | |
7 | 1082 } |
1083 | |
1084 while (!error && !got_int) | |
1085 { | |
1086 /* | |
1087 * We allocate as much space for the file as we can get, plus | |
1088 * space for the old line plus room for one terminating NUL. | |
1089 * The amount is limited by the fact that read() only can read | |
18763
49b78d6465e5
patch 8.1.2371: FEAT_TEXT_PROP is a confusing name
Bram Moolenaar <Bram@vim.org>
parents:
18642
diff
changeset
|
1090 * up to max_unsigned characters (and other things). |
7 | 1091 */ |
13804
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1092 if (!skip_read) |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1093 { |
15607
2dcaa860e3fc
patch 8.1.0811: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15595
diff
changeset
|
1094 #if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1095 size = SSIZE_MAX; // use max I/O size, 52K |
15607
2dcaa860e3fc
patch 8.1.0811: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15595
diff
changeset
|
1096 #else |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1097 // Use buffer >= 64K. Add linerest to double the size if the |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1098 // line gets very long, to avoid a lot of copying. But don't |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1099 // read more than 1 Mbyte at a time, so we can be interrupted. |
13804
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1100 size = 0x10000L + linerest; |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1101 if (size > 0x100000L) |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1102 size = 0x100000L; |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1103 #endif |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1104 } |
8a35543f5f97
patch 8.0.1774: reading very long lines can be slow
Christian Brabandt <cb@256bit.org>
parents:
13762
diff
changeset
|
1105 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1106 // Protect against the argument of lalloc() going negative. |
15607
2dcaa860e3fc
patch 8.1.0811: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15595
diff
changeset
|
1107 if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL) |
7 | 1108 { |
1109 ++split; | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1110 *ptr = NL; // split line by inserting a NL |
7 | 1111 size = 1; |
1112 } | |
1113 else | |
1114 { | |
1115 if (!skip_read) | |
1116 { | |
836 | 1117 for ( ; size >= 10; size = (long)((long_u)size >> 1)) |
7 | 1118 { |
16768
695d9ef00b03
patch 8.1.1386: unessesary type casts for lalloc()
Bram Moolenaar <Bram@vim.org>
parents:
16766
diff
changeset
|
1119 if ((new_buffer = lalloc(size + linerest + 1, |
7 | 1120 FALSE)) != NULL) |
1121 break; | |
1122 } | |
1123 if (new_buffer == NULL) | |
1124 { | |
1125 do_outofmem_msg((long_u)(size * 2 + linerest + 1)); | |
1126 error = TRUE; | |
1127 break; | |
1128 } | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1129 if (linerest) // copy characters from the previous buffer |
7 | 1130 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest); |
1131 vim_free(buffer); | |
1132 buffer = new_buffer; | |
1133 ptr = buffer + linerest; | |
1134 line_start = buffer; | |
1135 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1136 // May need room to translate into. |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1137 // For iconv() we don't really know the required space, use a |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1138 // factor ICONV_MULT. |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1139 // latin1 to utf-8: 1 byte becomes up to 2 bytes |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1140 // utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1141 // become up to 4 bytes, size must be multiple of 2 |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1142 // ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1143 // multiple of 2 |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1144 // ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1145 // multiple of 4 |
835 | 1146 real_size = (int)size; |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1147 #ifdef USE_ICONV |
7 | 1148 if (iconv_fd != (iconv_t)-1) |
1149 size = size / ICONV_MULT; | |
1150 else | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1151 #endif |
7 | 1152 if (fio_flags & FIO_LATIN1) |
1153 size = size / 2; | |
1154 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1155 size = (size * 2 / 3) & ~1; | |
1156 else if (fio_flags & FIO_UCS4) | |
1157 size = (size * 2 / 3) & ~3; | |
1158 else if (fio_flags == FIO_UCSBOM) | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1159 size = size / ICONV_MULT; // worst case |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15840
diff
changeset
|
1160 #ifdef MSWIN |
7 | 1161 else if (fio_flags & FIO_CODEPAGE) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1162 size = size / ICONV_MULT; // also worst case |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1163 #endif |
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1164 #ifdef MACOS_CONVERT |
7 | 1165 else if (fio_flags & FIO_MACROMAN) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1166 size = size / ICONV_MULT; // also worst case |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1167 #endif |
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1168 |
7 | 1169 if (conv_restlen > 0) |
1170 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1171 // Insert unconverted bytes from previous line. |
7 | 1172 mch_memmove(ptr, conv_rest, conv_restlen); |
1173 ptr += conv_restlen; | |
1174 size -= conv_restlen; | |
1175 } | |
1176 | |
1177 if (read_buffer) | |
1178 { | |
1179 /* | |
1180 * Read bytes from curbuf. Used for converting text read | |
1181 * from stdin. | |
1182 */ | |
1183 if (read_buf_lnum > from) | |
1184 size = 0; | |
1185 else | |
1186 { | |
1187 int n, ni; | |
1188 long tlen; | |
1189 | |
1190 tlen = 0; | |
1191 for (;;) | |
1192 { | |
1193 p = ml_get(read_buf_lnum) + read_buf_col; | |
1194 n = (int)STRLEN(p); | |
1195 if ((int)tlen + n + 1 > size) | |
1196 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1197 // Filled up to "size", append partial line. |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1198 // Change NL to NUL to reverse the effect done |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1199 // below. |
835 | 1200 n = (int)(size - tlen); |
7 | 1201 for (ni = 0; ni < n; ++ni) |
1202 { | |
1203 if (p[ni] == NL) | |
1204 ptr[tlen++] = NUL; | |
1205 else | |
1206 ptr[tlen++] = p[ni]; | |
1207 } | |
1208 read_buf_col += n; | |
1209 break; | |
1210 } | |
1211 else | |
1212 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1213 // Append whole line and new-line. Change NL |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1214 // to NUL to reverse the effect done below. |
7 | 1215 for (ni = 0; ni < n; ++ni) |
1216 { | |
1217 if (p[ni] == NL) | |
1218 ptr[tlen++] = NUL; | |
1219 else | |
1220 ptr[tlen++] = p[ni]; | |
1221 } | |
1222 ptr[tlen++] = NL; | |
1223 read_buf_col = 0; | |
1224 if (++read_buf_lnum > from) | |
1225 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1226 // When the last line didn't have an |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1227 // end-of-line don't add it now either. |
7 | 1228 if (!curbuf->b_p_eol) |
1229 --tlen; | |
1230 size = tlen; | |
1231 break; | |
1232 } | |
1233 } | |
1234 } | |
1235 } | |
1236 } | |
1237 else | |
1238 { | |
1239 /* | |
1240 * Read bytes from the file. | |
1241 */ | |
2664 | 1242 size = read_eintr(fd, ptr, size); |
7 | 1243 } |
1244 | |
6122 | 1245 #ifdef FEAT_CRYPT |
1246 /* | |
1247 * At start of file: Check for magic number of encryption. | |
1248 */ | |
1249 if (filesize == 0 && size > 0) | |
1250 cryptkey = check_for_cryptkey(cryptkey, ptr, &size, | |
1251 &filesize, newfile, sfname, | |
1252 &did_ask_for_key); | |
1253 /* | |
1254 * Decrypt the read bytes. This is done before checking for | |
1255 * EOF because the crypt layer may be buffering. | |
1256 */ | |
12216
e971ef6c0dee
patch 8.0.0988: warning from Covscan about using NULL pointer
Christian Brabandt <cb@256bit.org>
parents:
12064
diff
changeset
|
1257 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
|
1258 && size > 0) |
6122 | 1259 { |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15517
diff
changeset
|
1260 # ifdef CRYPT_NOT_INPLACE |
6122 | 1261 if (crypt_works_inplace(curbuf->b_cryptstate)) |
1262 { | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15517
diff
changeset
|
1263 # endif |
6122 | 1264 crypt_decode_inplace(curbuf->b_cryptstate, ptr, size); |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15517
diff
changeset
|
1265 # ifdef CRYPT_NOT_INPLACE |
6122 | 1266 } |
1267 else | |
1268 { | |
1269 char_u *newptr = NULL; | |
1270 int decrypted_size; | |
1271 | |
1272 decrypted_size = crypt_decode_alloc( | |
1273 curbuf->b_cryptstate, ptr, size, &newptr); | |
1274 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1275 // If the crypt layer is buffering, not producing |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1276 // anything yet, need to read more. |
13762
9de2b25932eb
patch 8.0.1753: various warnings from a static analyser
Christian Brabandt <cb@256bit.org>
parents:
13752
diff
changeset
|
1277 if (decrypted_size == 0) |
6122 | 1278 continue; |
1279 | |
1280 if (linerest == 0) | |
1281 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1282 // Simple case: reuse returned buffer (may be |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1283 // NULL, checked later). |
6122 | 1284 new_buffer = newptr; |
1285 } | |
1286 else | |
1287 { | |
1288 long_u new_size; | |
1289 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1290 // Need new buffer to add bytes carried over. |
6122 | 1291 new_size = (long_u)(decrypted_size + linerest + 1); |
1292 new_buffer = lalloc(new_size, FALSE); | |
1293 if (new_buffer == NULL) | |
1294 { | |
1295 do_outofmem_msg(new_size); | |
1296 error = TRUE; | |
1297 break; | |
1298 } | |
1299 | |
1300 mch_memmove(new_buffer, buffer, linerest); | |
1301 if (newptr != NULL) | |
1302 mch_memmove(new_buffer + linerest, newptr, | |
1303 decrypted_size); | |
1304 } | |
1305 | |
1306 if (new_buffer != NULL) | |
1307 { | |
1308 vim_free(buffer); | |
1309 buffer = new_buffer; | |
1310 new_buffer = NULL; | |
1311 line_start = buffer; | |
1312 ptr = buffer + linerest; | |
1313 } | |
1314 size = decrypted_size; | |
1315 } | |
15531
959cf4c63b18
patch 8.1.0773: not all crypt code is tested
Bram Moolenaar <Bram@vim.org>
parents:
15517
diff
changeset
|
1316 # endif |
6122 | 1317 } |
1318 #endif | |
1319 | |
7 | 1320 if (size <= 0) |
1321 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1322 if (size < 0) // read error |
7 | 1323 error = TRUE; |
1324 else if (conv_restlen > 0) | |
595 | 1325 { |
1597 | 1326 /* |
1327 * Reached end-of-file but some trailing bytes could | |
1328 * not be converted. Truncated file? | |
1329 */ | |
1330 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1331 // When we did a conversion report an error. |
1597 | 1332 if (fio_flags != 0 |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1333 #ifdef USE_ICONV |
1597 | 1334 || iconv_fd != (iconv_t)-1 |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1335 #endif |
1597 | 1336 ) |
1337 { | |
4331 | 1338 if (can_retry) |
1339 goto rewind_retry; | |
1597 | 1340 if (conv_error == 0) |
1341 conv_error = curbuf->b_ml.ml_line_count | |
1342 - linecnt + 1; | |
1343 } | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1344 // Remember the first linenr with an illegal byte |
1597 | 1345 else if (illegal_byte == 0) |
1346 illegal_byte = curbuf->b_ml.ml_line_count | |
1347 - linecnt + 1; | |
1348 if (bad_char_behavior == BAD_DROP) | |
595 | 1349 { |
1597 | 1350 *(ptr - conv_restlen) = NUL; |
1351 conv_restlen = 0; | |
1352 } | |
1353 else | |
1354 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1355 // Replace the trailing bytes with the replacement |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1356 // character if we were converting; if we weren't, |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1357 // leave the UTF8 checking code to do it, as it |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1358 // works slightly differently. |
1597 | 1359 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0 |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1360 #ifdef USE_ICONV |
1597 | 1361 || iconv_fd != (iconv_t)-1 |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1362 #endif |
1597 | 1363 )) |
1364 { | |
1365 while (conv_restlen > 0) | |
1366 { | |
1367 *(--ptr) = bad_char_behavior; | |
1368 --conv_restlen; | |
1369 } | |
1370 } | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1371 fio_flags = 0; // don't convert this |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1372 #ifdef USE_ICONV |
834 | 1373 if (iconv_fd != (iconv_t)-1) |
1374 { | |
1375 iconv_close(iconv_fd); | |
1376 iconv_fd = (iconv_t)-1; | |
1377 } | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1378 #endif |
595 | 1379 } |
1380 } | |
7 | 1381 } |
1382 } | |
1383 skip_read = FALSE; | |
1384 | |
1385 /* | |
1386 * At start of file (or after crypt magic number): Check for BOM. | |
1387 * Also check for a BOM for other Unicode encodings, but not after | |
1388 * converting with 'charconvert' or when a BOM has already been | |
1389 * found. | |
1390 */ | |
1391 if ((filesize == 0 | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1392 #ifdef FEAT_CRYPT |
6122 | 1393 || (cryptkey != NULL |
1394 && filesize == crypt_get_header_len( | |
1395 crypt_get_method_nr(curbuf))) | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1396 #endif |
7 | 1397 ) |
1398 && (fio_flags == FIO_UCSBOM | |
1399 || (!curbuf->b_p_bomb | |
1400 && tmpname == NULL | |
1401 && (*fenc == 'u' || (*fenc == NUL && enc_utf8))))) | |
1402 { | |
1403 char_u *ccname; | |
1404 int blen; | |
1405 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1406 // no BOM detection in a short file or in binary mode |
7 | 1407 if (size < 2 || curbuf->b_p_bin) |
1408 ccname = NULL; | |
1409 else | |
1410 ccname = check_for_bom(ptr, size, &blen, | |
1411 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc)); | |
1412 if (ccname != NULL) | |
1413 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1414 // Remove BOM from the text |
7 | 1415 filesize += blen; |
1416 size -= blen; | |
1417 mch_memmove(ptr, ptr + blen, (size_t)size); | |
819 | 1418 if (set_options) |
1352 | 1419 { |
7 | 1420 curbuf->b_p_bomb = TRUE; |
1352 | 1421 curbuf->b_start_bomb = TRUE; |
1422 } | |
7 | 1423 } |
1424 | |
1425 if (fio_flags == FIO_UCSBOM) | |
1426 { | |
1427 if (ccname == NULL) | |
1428 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1429 // No BOM detected: retry with next encoding. |
7 | 1430 advance_fenc = TRUE; |
1431 } | |
1432 else | |
1433 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1434 // BOM detected: set "fenc" and jump back |
7 | 1435 if (fenc_alloced) |
1436 vim_free(fenc); | |
1437 fenc = ccname; | |
1438 fenc_alloced = FALSE; | |
1439 } | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1440 // retry reading without getting new bytes or rewinding |
7 | 1441 skip_read = TRUE; |
1442 goto retry; | |
1443 } | |
1444 } | |
1597 | 1445 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1446 // Include not converted bytes. |
1597 | 1447 ptr -= conv_restlen; |
1448 size += conv_restlen; | |
1449 conv_restlen = 0; | |
7 | 1450 /* |
1451 * Break here for a read error or end-of-file. | |
1452 */ | |
1453 if (size <= 0) | |
1454 break; | |
1455 | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1456 |
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1457 #ifdef USE_ICONV |
7 | 1458 if (iconv_fd != (iconv_t)-1) |
1459 { | |
1460 /* | |
1461 * Attempt conversion of the read bytes to 'encoding' using | |
1462 * iconv(). | |
1463 */ | |
1464 const char *fromp; | |
1465 char *top; | |
1466 size_t from_size; | |
1467 size_t to_size; | |
1468 | |
1469 fromp = (char *)ptr; | |
1470 from_size = size; | |
1471 ptr += size; | |
1472 top = (char *)ptr; | |
1473 to_size = real_size - size; | |
1474 | |
1475 /* | |
1476 * If there is conversion error or not enough room try using | |
179 | 1477 * another conversion. Except for when there is no |
1478 * alternative (help files). | |
7 | 1479 */ |
177 | 1480 while ((iconv(iconv_fd, (void *)&fromp, &from_size, |
1481 &top, &to_size) | |
7 | 1482 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) |
1483 || from_size > CONV_RESTLEN) | |
177 | 1484 { |
595 | 1485 if (can_retry) |
375 | 1486 goto rewind_retry; |
595 | 1487 if (conv_error == 0) |
1488 conv_error = readfile_linenr(linecnt, | |
1489 ptr, (char_u *)top); | |
1490 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1491 // Deal with a bad byte and continue with the next. |
177 | 1492 ++fromp; |
1493 --from_size; | |
595 | 1494 if (bad_char_behavior == BAD_KEEP) |
1495 { | |
1496 *top++ = *(fromp - 1); | |
1497 --to_size; | |
1498 } | |
1499 else if (bad_char_behavior != BAD_DROP) | |
1500 { | |
1501 *top++ = bad_char_behavior; | |
1502 --to_size; | |
1503 } | |
177 | 1504 } |
7 | 1505 |
1506 if (from_size > 0) | |
1507 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1508 // Some remaining characters, keep them for the next |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1509 // round. |
7 | 1510 mch_memmove(conv_rest, (char_u *)fromp, from_size); |
1511 conv_restlen = (int)from_size; | |
1512 } | |
1513 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1514 // move the linerest to before the converted characters |
7 | 1515 line_start = ptr - linerest; |
1516 mch_memmove(line_start, buffer, (size_t)linerest); | |
1517 size = (long)((char_u *)top - ptr); | |
1518 } | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1519 #endif |
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1520 |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15840
diff
changeset
|
1521 #ifdef MSWIN |
7 | 1522 if (fio_flags & FIO_CODEPAGE) |
1523 { | |
595 | 1524 char_u *src, *dst; |
1525 WCHAR ucs2buf[3]; | |
1526 int ucs2len; | |
1527 int codepage = FIO_GET_CP(fio_flags); | |
1528 int bytelen; | |
1529 int found_bad; | |
1530 char replstr[2]; | |
1531 | |
7 | 1532 /* |
1533 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or | |
595 | 1534 * a codepage, using standard MS-Windows functions. This |
1535 * requires two steps: | |
1536 * 1. convert from 'fileencoding' to ucs-2 | |
1537 * 2. convert from ucs-2 to 'encoding' | |
1538 * | |
1539 * Because there may be illegal bytes AND an incomplete byte | |
1540 * sequence at the end, we may have to do the conversion one | |
1541 * character at a time to get it right. | |
7 | 1542 */ |
595 | 1543 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1544 // Replacement string for WideCharToMultiByte(). |
595 | 1545 if (bad_char_behavior > 0) |
1546 replstr[0] = bad_char_behavior; | |
1547 else | |
1548 replstr[0] = '?'; | |
1549 replstr[1] = NUL; | |
1550 | |
1551 /* | |
1552 * Move the bytes to the end of the buffer, so that we have | |
1553 * room to put the result at the start. | |
1554 */ | |
1555 src = ptr + real_size - size; | |
1556 mch_memmove(src, ptr, size); | |
7 | 1557 |
1558 /* | |
595 | 1559 * Do the conversion. |
7 | 1560 */ |
595 | 1561 dst = ptr; |
1562 size = size; | |
1563 while (size > 0) | |
7 | 1564 { |
595 | 1565 found_bad = FALSE; |
7 | 1566 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1567 # ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8 |
595 | 1568 if (codepage == CP_UTF8) |
7 | 1569 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1570 // Handle CP_UTF8 input ourselves to be able to handle |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1571 // trailing bytes properly. |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1572 // Get one UTF-8 character from src. |
835 | 1573 bytelen = (int)utf_ptr2len_len(src, size); |
595 | 1574 if (bytelen > size) |
1575 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1576 // Only got some bytes of a character. Normally |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1577 // it's put in "conv_rest", but if it's too long |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1578 // deal with it as if they were illegal bytes. |
595 | 1579 if (bytelen <= CONV_RESTLEN) |
1580 break; | |
1581 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1582 // weird overlong byte sequence |
595 | 1583 bytelen = size; |
1584 found_bad = TRUE; | |
1585 } | |
1586 else | |
1587 { | |
799 | 1588 int u8c = utf_ptr2char(src); |
1589 | |
622 | 1590 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1)) |
595 | 1591 found_bad = TRUE; |
1592 ucs2buf[0] = u8c; | |
1593 ucs2len = 1; | |
1594 } | |
7 | 1595 } |
595 | 1596 else |
7 | 1597 # endif |
595 | 1598 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1599 // We don't know how long the byte sequence is, try |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1600 // from one to three bytes. |
595 | 1601 for (bytelen = 1; bytelen <= size && bytelen <= 3; |
1602 ++bytelen) | |
1603 { | |
1604 ucs2len = MultiByteToWideChar(codepage, | |
1605 MB_ERR_INVALID_CHARS, | |
1606 (LPCSTR)src, bytelen, | |
1607 ucs2buf, 3); | |
1608 if (ucs2len > 0) | |
1609 break; | |
1610 } | |
1611 if (ucs2len == 0) | |
1612 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1613 // If we have only one byte then it's probably an |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1614 // incomplete byte sequence. Otherwise discard |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1615 // one byte as a bad character. |
595 | 1616 if (size == 1) |
1617 break; | |
1618 found_bad = TRUE; | |
1619 bytelen = 1; | |
1620 } | |
1621 } | |
1622 | |
1623 if (!found_bad) | |
7 | 1624 { |
595 | 1625 int i; |
1626 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1627 // Convert "ucs2buf[ucs2len]" to 'enc' in "dst". |
595 | 1628 if (enc_utf8) |
1629 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1630 // From UCS-2 to UTF-8. Cannot fail. |
595 | 1631 for (i = 0; i < ucs2len; ++i) |
1632 dst += utf_char2bytes(ucs2buf[i], dst); | |
1633 } | |
1634 else | |
1635 { | |
1636 BOOL bad = FALSE; | |
1637 int dstlen; | |
1638 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1639 // From UCS-2 to "enc_codepage". If the |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1640 // conversion uses the default character "?", |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1641 // the data doesn't fit in this encoding. |
595 | 1642 dstlen = WideCharToMultiByte(enc_codepage, 0, |
1643 (LPCWSTR)ucs2buf, ucs2len, | |
835 | 1644 (LPSTR)dst, (int)(src - dst), |
595 | 1645 replstr, &bad); |
1646 if (bad) | |
1647 found_bad = TRUE; | |
1648 else | |
1649 dst += dstlen; | |
1650 } | |
7 | 1651 } |
595 | 1652 |
1653 if (found_bad) | |
1654 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1655 // Deal with bytes we can't convert. |
595 | 1656 if (can_retry) |
1657 goto rewind_retry; | |
1658 if (conv_error == 0) | |
1659 conv_error = readfile_linenr(linecnt, ptr, dst); | |
1660 if (bad_char_behavior != BAD_DROP) | |
1661 { | |
1662 if (bad_char_behavior == BAD_KEEP) | |
1663 { | |
1664 mch_memmove(dst, src, bytelen); | |
1665 dst += bytelen; | |
1666 } | |
1667 else | |
1668 *dst++ = bad_char_behavior; | |
1669 } | |
1670 } | |
1671 | |
1672 src += bytelen; | |
1673 size -= bytelen; | |
7 | 1674 } |
595 | 1675 |
1676 if (size > 0) | |
7 | 1677 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1678 // An incomplete byte sequence remaining. |
595 | 1679 mch_memmove(conv_rest, src, size); |
1680 conv_restlen = size; | |
7 | 1681 } |
595 | 1682 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1683 // The new size is equal to how much "dst" was advanced. |
835 | 1684 size = (long)(dst - ptr); |
7 | 1685 } |
1686 else | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1687 #endif |
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1688 #ifdef MACOS_CONVERT |
7 | 1689 if (fio_flags & FIO_MACROMAN) |
1690 { | |
1691 /* | |
1692 * Conversion from Apple MacRoman char encoding to UTF-8 or | |
18 | 1693 * latin1. This is in os_mac_conv.c. |
7 | 1694 */ |
18 | 1695 if (macroman2enc(ptr, &size, real_size) == FAIL) |
7 | 1696 goto rewind_retry; |
1697 } | |
1698 else | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1699 #endif |
7 | 1700 if (fio_flags != 0) |
1701 { | |
1702 int u8c; | |
1703 char_u *dest; | |
1704 char_u *tail = NULL; | |
1705 | |
1706 /* | |
1707 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8. | |
1708 * "enc_utf8" not set: Convert Unicode to Latin1. | |
1709 * Go from end to start through the buffer, because the number | |
1710 * of bytes may increase. | |
1711 * "dest" points to after where the UTF-8 bytes go, "p" points | |
1712 * to after the next character to convert. | |
1713 */ | |
1714 dest = ptr + real_size; | |
1715 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8) | |
1716 { | |
1717 p = ptr + size; | |
1718 if (fio_flags == FIO_UTF8) | |
1719 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1720 // Check for a trailing incomplete UTF-8 sequence |
7 | 1721 tail = ptr + size - 1; |
1722 while (tail > ptr && (*tail & 0xc0) == 0x80) | |
1723 --tail; | |
1724 if (tail + utf_byte2len(*tail) <= ptr + size) | |
1725 tail = NULL; | |
1726 else | |
1727 p = tail; | |
1728 } | |
1729 } | |
1730 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1731 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1732 // Check for a trailing byte |
7 | 1733 p = ptr + (size & ~1); |
1734 if (size & 1) | |
1735 tail = p; | |
1736 if ((fio_flags & FIO_UTF16) && p > ptr) | |
1737 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1738 // Check for a trailing leading word |
7 | 1739 if (fio_flags & FIO_ENDIAN_L) |
1740 { | |
1741 u8c = (*--p << 8); | |
1742 u8c += *--p; | |
1743 } | |
1744 else | |
1745 { | |
1746 u8c = *--p; | |
1747 u8c += (*--p << 8); | |
1748 } | |
1749 if (u8c >= 0xd800 && u8c <= 0xdbff) | |
1750 tail = p; | |
1751 else | |
1752 p += 2; | |
1753 } | |
1754 } | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1755 else // FIO_UCS4 |
7 | 1756 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1757 // Check for trailing 1, 2 or 3 bytes |
7 | 1758 p = ptr + (size & ~3); |
1759 if (size & 3) | |
1760 tail = p; | |
1761 } | |
1762 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1763 // If there is a trailing incomplete sequence move it to |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1764 // conv_rest[]. |
7 | 1765 if (tail != NULL) |
1766 { | |
1767 conv_restlen = (int)((ptr + size) - tail); | |
1768 mch_memmove(conv_rest, (char_u *)tail, conv_restlen); | |
1769 size -= conv_restlen; | |
1770 } | |
1771 | |
1772 | |
1773 while (p > ptr) | |
1774 { | |
1775 if (fio_flags & FIO_LATIN1) | |
1776 u8c = *--p; | |
1777 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1778 { | |
1779 if (fio_flags & FIO_ENDIAN_L) | |
1780 { | |
1781 u8c = (*--p << 8); | |
1782 u8c += *--p; | |
1783 } | |
1784 else | |
1785 { | |
1786 u8c = *--p; | |
1787 u8c += (*--p << 8); | |
1788 } | |
1789 if ((fio_flags & FIO_UTF16) | |
1790 && u8c >= 0xdc00 && u8c <= 0xdfff) | |
1791 { | |
1792 int u16c; | |
1793 | |
1794 if (p == ptr) | |
1795 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1796 // Missing leading word. |
7 | 1797 if (can_retry) |
1798 goto rewind_retry; | |
595 | 1799 if (conv_error == 0) |
1800 conv_error = readfile_linenr(linecnt, | |
1801 ptr, p); | |
1802 if (bad_char_behavior == BAD_DROP) | |
1803 continue; | |
1804 if (bad_char_behavior != BAD_KEEP) | |
1805 u8c = bad_char_behavior; | |
7 | 1806 } |
1807 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1808 // found second word of double-word, get the first |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1809 // word and compute the resulting character |
7 | 1810 if (fio_flags & FIO_ENDIAN_L) |
1811 { | |
1812 u16c = (*--p << 8); | |
1813 u16c += *--p; | |
1814 } | |
1815 else | |
1816 { | |
1817 u16c = *--p; | |
1818 u16c += (*--p << 8); | |
1819 } | |
595 | 1820 u8c = 0x10000 + ((u16c & 0x3ff) << 10) |
1821 + (u8c & 0x3ff); | |
1822 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1823 // Check if the word is indeed a leading word. |
7 | 1824 if (u16c < 0xd800 || u16c > 0xdbff) |
1825 { | |
1826 if (can_retry) | |
1827 goto rewind_retry; | |
595 | 1828 if (conv_error == 0) |
1829 conv_error = readfile_linenr(linecnt, | |
1830 ptr, p); | |
1831 if (bad_char_behavior == BAD_DROP) | |
1832 continue; | |
1833 if (bad_char_behavior != BAD_KEEP) | |
1834 u8c = bad_char_behavior; | |
7 | 1835 } |
1836 } | |
1837 } | |
1838 else if (fio_flags & FIO_UCS4) | |
1839 { | |
1840 if (fio_flags & FIO_ENDIAN_L) | |
1841 { | |
12698
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1842 u8c = (unsigned)*--p << 24; |
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1843 u8c += (unsigned)*--p << 16; |
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1844 u8c += (unsigned)*--p << 8; |
7 | 1845 u8c += *--p; |
1846 } | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1847 else // big endian |
7 | 1848 { |
1849 u8c = *--p; | |
12698
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1850 u8c += (unsigned)*--p << 8; |
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1851 u8c += (unsigned)*--p << 16; |
6d3d64be7945
patch 8.0.1227: undefined left shift in readfile()
Christian Brabandt <cb@256bit.org>
parents:
12656
diff
changeset
|
1852 u8c += (unsigned)*--p << 24; |
7 | 1853 } |
1854 } | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1855 else // UTF-8 |
7 | 1856 { |
1857 if (*--p < 0x80) | |
1858 u8c = *p; | |
1859 else | |
1860 { | |
1861 len = utf_head_off(ptr, p); | |
595 | 1862 p -= len; |
1863 u8c = utf_ptr2char(p); | |
7 | 1864 if (len == 0) |
1865 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1866 // Not a valid UTF-8 character, retry with |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1867 // another fenc when possible, otherwise just |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1868 // report the error. |
7 | 1869 if (can_retry) |
1870 goto rewind_retry; | |
595 | 1871 if (conv_error == 0) |
1872 conv_error = readfile_linenr(linecnt, | |
1873 ptr, p); | |
1874 if (bad_char_behavior == BAD_DROP) | |
1875 continue; | |
1876 if (bad_char_behavior != BAD_KEEP) | |
1877 u8c = bad_char_behavior; | |
7 | 1878 } |
1879 } | |
1880 } | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1881 if (enc_utf8) // produce UTF-8 |
7 | 1882 { |
1883 dest -= utf_char2len(u8c); | |
1884 (void)utf_char2bytes(u8c, dest); | |
1885 } | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1886 else // produce Latin1 |
7 | 1887 { |
1888 --dest; | |
1889 if (u8c >= 0x100) | |
1890 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1891 // character doesn't fit in latin1, retry with |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1892 // another fenc when possible, otherwise just |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1893 // report the error. |
595 | 1894 if (can_retry) |
7 | 1895 goto rewind_retry; |
595 | 1896 if (conv_error == 0) |
1897 conv_error = readfile_linenr(linecnt, ptr, p); | |
1898 if (bad_char_behavior == BAD_DROP) | |
1899 ++dest; | |
1900 else if (bad_char_behavior == BAD_KEEP) | |
1901 *dest = u8c; | |
1902 else if (eap != NULL && eap->bad_char != 0) | |
1903 *dest = bad_char_behavior; | |
1904 else | |
1905 *dest = 0xBF; | |
7 | 1906 } |
1907 else | |
1908 *dest = u8c; | |
1909 } | |
1910 } | |
1911 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1912 // move the linerest to before the converted characters |
7 | 1913 line_start = dest - linerest; |
1914 mch_memmove(line_start, buffer, (size_t)linerest); | |
1915 size = (long)((ptr + real_size) - dest); | |
1916 ptr = dest; | |
1917 } | |
1597 | 1918 else if (enc_utf8 && !curbuf->b_p_bin) |
1919 { | |
1920 int incomplete_tail = FALSE; | |
1921 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1922 // Reading UTF-8: Check if the bytes are valid UTF-8. |
1597 | 1923 for (p = ptr; ; ++p) |
7 | 1924 { |
835 | 1925 int todo = (int)((ptr + size) - p); |
595 | 1926 int l; |
1927 | |
1928 if (todo <= 0) | |
1929 break; | |
7 | 1930 if (*p >= 0x80) |
1931 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1932 // A length of 1 means it's an illegal byte. Accept |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1933 // an incomplete character at the end though, the next |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1934 // read() will get the next bytes, we'll check it |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1935 // then. |
595 | 1936 l = utf_ptr2len_len(p, todo); |
1597 | 1937 if (l > todo && !incomplete_tail) |
7 | 1938 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1939 // Avoid retrying with a different encoding when |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1940 // a truncated file is more likely, or attempting |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1941 // to read the rest of an incomplete sequence when |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1942 // we have already done so. |
1597 | 1943 if (p > ptr || filesize > 0) |
1944 incomplete_tail = TRUE; | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1945 // Incomplete byte sequence, move it to conv_rest[] |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1946 // and try to read the rest of it, unless we've |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1947 // already done so. |
1597 | 1948 if (p > ptr) |
1949 { | |
1950 conv_restlen = todo; | |
1951 mch_memmove(conv_rest, p, conv_restlen); | |
1952 size -= conv_restlen; | |
1953 break; | |
1954 } | |
7 | 1955 } |
1597 | 1956 if (l == 1 || l > todo) |
595 | 1957 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1958 // Illegal byte. If we can try another encoding |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1959 // do that, unless at EOF where a truncated |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1960 // file is more likely than a conversion error. |
1597 | 1961 if (can_retry && !incomplete_tail) |
595 | 1962 break; |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1963 #ifdef USE_ICONV |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1964 // When we did a conversion report an error. |
595 | 1965 if (iconv_fd != (iconv_t)-1 && conv_error == 0) |
1966 conv_error = readfile_linenr(linecnt, ptr, p); | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1967 #endif |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1968 // Remember the first linenr with an illegal byte |
1597 | 1969 if (conv_error == 0 && illegal_byte == 0) |
1970 illegal_byte = readfile_linenr(linecnt, ptr, p); | |
595 | 1971 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1972 // Drop, keep or replace the bad byte. |
595 | 1973 if (bad_char_behavior == BAD_DROP) |
1974 { | |
1597 | 1975 mch_memmove(p, p + 1, todo - 1); |
595 | 1976 --p; |
1977 --size; | |
1978 } | |
1979 else if (bad_char_behavior != BAD_KEEP) | |
1980 *p = bad_char_behavior; | |
1981 } | |
1597 | 1982 else |
1983 p += l - 1; | |
7 | 1984 } |
1985 } | |
1597 | 1986 if (p < ptr + size && !incomplete_tail) |
7 | 1987 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1988 // Detected a UTF-8 error. |
7 | 1989 rewind_retry: |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1990 // Retry reading with another conversion. |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1991 #if defined(FEAT_EVAL) && defined(USE_ICONV) |
595 | 1992 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1993 // iconv() failed, try 'charconvert' |
595 | 1994 did_iconv = TRUE; |
7 | 1995 else |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
1996 #endif |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
1997 // use next item from 'fileencodings' |
595 | 1998 advance_fenc = TRUE; |
1999 file_rewind = TRUE; | |
2000 goto retry; | |
7 | 2001 } |
2002 } | |
2003 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2004 // count the number of characters (after conversion!) |
7 | 2005 filesize += size; |
2006 | |
2007 /* | |
2008 * when reading the first part of a file: guess EOL type | |
2009 */ | |
2010 if (fileformat == EOL_UNKNOWN) | |
2011 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2012 // First try finding a NL, for Dos and Unix |
7 | 2013 if (try_dos || try_unix) |
2014 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2015 // Reset the carriage return counter. |
6635 | 2016 if (try_mac) |
2017 try_mac = 1; | |
2018 | |
7 | 2019 for (p = ptr; p < ptr + size; ++p) |
2020 { | |
2021 if (*p == NL) | |
2022 { | |
2023 if (!try_unix | |
2024 || (try_dos && p > ptr && p[-1] == CAR)) | |
2025 fileformat = EOL_DOS; | |
2026 else | |
2027 fileformat = EOL_UNIX; | |
2028 break; | |
2029 } | |
6618 | 2030 else if (*p == CAR && try_mac) |
2031 try_mac++; | |
7 | 2032 } |
2033 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2034 // Don't give in to EOL_UNIX if EOL_MAC is more likely |
7 | 2035 if (fileformat == EOL_UNIX && try_mac) |
2036 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2037 // Need to reset the counters when retrying fenc. |
7 | 2038 try_mac = 1; |
2039 try_unix = 1; | |
2040 for (; p >= ptr && *p != CAR; p--) | |
2041 ; | |
2042 if (p >= ptr) | |
2043 { | |
2044 for (p = ptr; p < ptr + size; ++p) | |
2045 { | |
2046 if (*p == NL) | |
2047 try_unix++; | |
2048 else if (*p == CAR) | |
2049 try_mac++; | |
2050 } | |
2051 if (try_mac > try_unix) | |
2052 fileformat = EOL_MAC; | |
2053 } | |
2054 } | |
6618 | 2055 else if (fileformat == EOL_UNKNOWN && try_mac == 1) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2056 // Looking for CR but found no end-of-line markers at |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2057 // all: use the default format. |
6618 | 2058 fileformat = default_fileformat(); |
7 | 2059 } |
2060 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2061 // No NL found: may use Mac format |
7 | 2062 if (fileformat == EOL_UNKNOWN && try_mac) |
2063 fileformat = EOL_MAC; | |
2064 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2065 // Still nothing found? Use first format in 'ffs' |
7 | 2066 if (fileformat == EOL_UNKNOWN) |
2067 fileformat = default_fileformat(); | |
2068 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2069 // if editing a new file: may set p_tx and p_ff |
819 | 2070 if (set_options) |
7 | 2071 set_fileformat(fileformat, OPT_LOCAL); |
2072 } | |
2073 } | |
2074 | |
2075 /* | |
2076 * This loop is executed once for every character read. | |
2077 * Keep it fast! | |
2078 */ | |
2079 if (fileformat == EOL_MAC) | |
2080 { | |
2081 --ptr; | |
2082 while (++ptr, --size >= 0) | |
2083 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2084 // catch most common case first |
7 | 2085 if ((c = *ptr) != NUL && c != CAR && c != NL) |
2086 continue; | |
2087 if (c == NUL) | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2088 *ptr = NL; // NULs are replaced by newlines! |
7 | 2089 else if (c == NL) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2090 *ptr = CAR; // NLs are replaced by CRs! |
7 | 2091 else |
2092 { | |
2093 if (skip_count == 0) | |
2094 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2095 *ptr = NUL; // end of line |
7 | 2096 len = (colnr_T) (ptr - line_start + 1); |
2097 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2098 { | |
2099 error = TRUE; | |
2100 break; | |
2101 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2102 #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
|
2103 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
|
2104 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
|
2105 #endif |
7 | 2106 ++lnum; |
2107 if (--read_count == 0) | |
2108 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2109 error = TRUE; // break loop |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2110 line_start = ptr; // nothing left to write |
7 | 2111 break; |
2112 } | |
2113 } | |
2114 else | |
2115 --skip_count; | |
2116 line_start = ptr + 1; | |
2117 } | |
2118 } | |
2119 } | |
2120 else | |
2121 { | |
2122 --ptr; | |
2123 while (++ptr, --size >= 0) | |
2124 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2125 if ((c = *ptr) != NUL && c != NL) // catch most common case |
7 | 2126 continue; |
2127 if (c == NUL) | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2128 *ptr = NL; // NULs are replaced by newlines! |
7 | 2129 else |
2130 { | |
2131 if (skip_count == 0) | |
2132 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2133 *ptr = NUL; // end of line |
7 | 2134 len = (colnr_T)(ptr - line_start + 1); |
2135 if (fileformat == EOL_DOS) | |
2136 { | |
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
|
2137 if (ptr > line_start && ptr[-1] == CAR) |
7 | 2138 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2139 // remove CR before NL |
7 | 2140 ptr[-1] = NUL; |
2141 --len; | |
2142 } | |
2143 /* | |
2144 * Reading in Dos format, but no CR-LF found! | |
2145 * When 'fileformats' includes "unix", delete all | |
2146 * the lines read so far and start all over again. | |
2147 * Otherwise give an error message later. | |
2148 */ | |
2149 else if (ff_error != EOL_DOS) | |
2150 { | |
2151 if ( try_unix | |
2152 && !read_stdin | |
2153 && (read_buffer | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
2154 || 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
|
2155 == 0)) |
7 | 2156 { |
2157 fileformat = EOL_UNIX; | |
819 | 2158 if (set_options) |
7 | 2159 set_fileformat(EOL_UNIX, OPT_LOCAL); |
2160 file_rewind = TRUE; | |
2161 keep_fileformat = TRUE; | |
2162 goto retry; | |
2163 } | |
2164 ff_error = EOL_DOS; | |
2165 } | |
2166 } | |
2167 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2168 { | |
2169 error = TRUE; | |
2170 break; | |
2171 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2172 #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
|
2173 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
|
2174 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
|
2175 #endif |
7 | 2176 ++lnum; |
2177 if (--read_count == 0) | |
2178 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2179 error = TRUE; // break loop |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2180 line_start = ptr; // nothing left to write |
7 | 2181 break; |
2182 } | |
2183 } | |
2184 else | |
2185 --skip_count; | |
2186 line_start = ptr + 1; | |
2187 } | |
2188 } | |
2189 } | |
2190 linerest = (long)(ptr - line_start); | |
2191 ui_breakcheck(); | |
2192 } | |
2193 | |
2194 failed: | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2195 // not an error, max. number of lines reached |
7 | 2196 if (error && read_count == 0) |
2197 error = FALSE; | |
2198 | |
2199 /* | |
2200 * If we get EOF in the middle of a line, note the fact and | |
2201 * complete the line ourselves. | |
2202 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set. | |
2203 */ | |
2204 if (!error | |
2205 && !got_int | |
2206 && linerest != 0 | |
2207 && !(!curbuf->b_p_bin | |
2208 && fileformat == EOL_DOS | |
2209 && *line_start == Ctrl_Z | |
2210 && ptr == line_start + 1)) | |
2211 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2212 // remember for when writing |
819 | 2213 if (set_options) |
7 | 2214 curbuf->b_p_eol = FALSE; |
2215 *ptr = NUL; | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2216 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
|
2217 if (ml_append(lnum, line_start, len, newfile) == FAIL) |
7 | 2218 error = TRUE; |
2219 else | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2220 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2221 #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
|
2222 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
|
2223 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
|
2224 #endif |
7 | 2225 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
|
2226 } |
7 | 2227 } |
2228 | |
819 | 2229 if (set_options) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2230 save_file_ff(curbuf); // remember the current file format |
7 | 2231 |
2232 #ifdef FEAT_CRYPT | |
6122 | 2233 if (curbuf->b_cryptstate != NULL) |
2234 { | |
2235 crypt_free_state(curbuf->b_cryptstate); | |
2236 curbuf->b_cryptstate = NULL; | |
2237 } | |
2238 if (cryptkey != NULL && cryptkey != curbuf->b_p_key) | |
2239 crypt_free_key(cryptkey); | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2240 // Don't set cryptkey to NULL, it's used below as a flag that |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2241 // encryption was used. |
7 | 2242 #endif |
2243 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2244 // If editing a new file: set 'fenc' for the current buffer. |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2245 // Also for ":read ++edit file". |
819 | 2246 if (set_options) |
7 | 2247 set_string_option_direct((char_u *)"fenc", -1, fenc, |
694 | 2248 OPT_FREE|OPT_LOCAL, 0); |
7 | 2249 if (fenc_alloced) |
2250 vim_free(fenc); | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
2251 #ifdef USE_ICONV |
7 | 2252 if (iconv_fd != (iconv_t)-1) |
2253 iconv_close(iconv_fd); | |
2254 #endif | |
2255 | |
2256 if (!read_buffer && !read_stdin) | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2257 close(fd); // errors are ignored |
2003 | 2258 #ifdef HAVE_FD_CLOEXEC |
2259 else | |
2260 { | |
2261 int fdflags = fcntl(fd, F_GETFD); | |
2262 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
|
2263 (void)fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC); |
2003 | 2264 } |
2265 #endif | |
7 | 2266 vim_free(buffer); |
2267 | |
2268 #ifdef HAVE_DUP | |
2269 if (read_stdin) | |
2270 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2271 // Use stderr for stdin, makes shell commands work. |
7 | 2272 close(0); |
14730
193471015e1a
patch 8.1.0377: xdiff doesn't use the Vim memory allocation functions
Christian Brabandt <cb@256bit.org>
parents:
14700
diff
changeset
|
2273 vim_ignored = dup(2); |
7 | 2274 } |
2275 #endif | |
2276 | |
2277 if (tmpname != NULL) | |
2278 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2279 mch_remove(tmpname); // delete converted file |
7 | 2280 vim_free(tmpname); |
2281 } | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2282 --no_wait_return; // may wait for return now |
7 | 2283 |
2284 /* | |
2285 * In recovery mode everything but autocommands is skipped. | |
2286 */ | |
2287 if (!recoverymode) | |
2288 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2289 // need to delete the last line, which comes from the empty buffer |
7 | 2290 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY)) |
2291 { | |
2292 #ifdef FEAT_NETBEANS_INTG | |
2293 netbeansFireChanges = 0; | |
2294 #endif | |
2295 ml_delete(curbuf->b_ml.ml_line_count, FALSE); | |
2296 #ifdef FEAT_NETBEANS_INTG | |
2297 netbeansFireChanges = 1; | |
2298 #endif | |
2299 --linecnt; | |
2300 } | |
2301 linecnt = curbuf->b_ml.ml_line_count - linecnt; | |
2302 if (filesize == 0) | |
2303 linecnt = 0; | |
2304 if (newfile || read_buffer) | |
1201 | 2305 { |
7 | 2306 redraw_curbuf_later(NOT_VALID); |
1201 | 2307 #ifdef FEAT_DIFF |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2308 // After reading the text into the buffer the diff info needs to |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2309 // be updated. |
1201 | 2310 diff_invalidate(curbuf); |
2311 #endif | |
2312 #ifdef FEAT_FOLDING | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2313 // All folds in the window are invalid now. Mark them for update |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2314 // before triggering autocommands. |
1201 | 2315 foldUpdateAll(curwin); |
2316 #endif | |
2317 } | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2318 else if (linecnt) // appended at least one line |
7 | 2319 appended_lines_mark(from, linecnt); |
2320 | |
2321 #ifndef ALWAYS_USE_GUI | |
2322 /* | |
2323 * If we were reading from the same terminal as where messages go, | |
2324 * the screen will have been messed up. | |
2325 * Switch on raw mode now and clear the screen. | |
2326 */ | |
2327 if (read_stdin) | |
2328 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2329 settmode(TMODE_RAW); // set to raw mode |
7 | 2330 starttermcap(); |
2331 screenclear(); | |
2332 } | |
2333 #endif | |
2334 | |
2335 if (got_int) | |
2336 { | |
2337 if (!(flags & READ_DUMMY)) | |
2338 { | |
2339 filemess(curbuf, sfname, (char_u *)_(e_interr), 0); | |
2340 if (newfile) | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2341 curbuf->b_p_ro = TRUE; // must use "w!" now |
7 | 2342 } |
2343 msg_scroll = msg_save; | |
2344 #ifdef FEAT_VIMINFO | |
2345 check_marks_read(); | |
2346 #endif | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2347 return OK; // an interrupt isn't really an error |
7 | 2348 } |
2349 | |
2350 if (!filtering && !(flags & READ_DUMMY)) | |
2351 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2352 msg_add_fname(curbuf, sfname); // fname in IObuff with quotes |
7 | 2353 c = FALSE; |
2354 | |
2355 #ifdef UNIX | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2356 if (S_ISFIFO(perm)) // fifo |
7 | 2357 { |
2358 STRCAT(IObuff, _("[fifo]")); | |
2359 c = TRUE; | |
2360 } | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2361 if (S_ISSOCK(perm)) // or socket |
7 | 2362 { |
2363 STRCAT(IObuff, _("[socket]")); | |
2364 c = TRUE; | |
2365 } | |
1313 | 2366 # ifdef OPEN_CHR_FILES |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2367 if (S_ISCHR(perm)) // or character special |
1313 | 2368 { |
2369 STRCAT(IObuff, _("[character special]")); | |
2370 c = TRUE; | |
2371 } | |
2372 # endif | |
7 | 2373 #endif |
2374 if (curbuf->b_p_ro) | |
2375 { | |
2376 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")); | |
2377 c = TRUE; | |
2378 } | |
2379 if (read_no_eol_lnum) | |
2380 { | |
2381 msg_add_eol(); | |
2382 c = TRUE; | |
2383 } | |
2384 if (ff_error == EOL_DOS) | |
2385 { | |
2386 STRCAT(IObuff, _("[CR missing]")); | |
2387 c = TRUE; | |
2388 } | |
2389 if (split) | |
2390 { | |
2391 STRCAT(IObuff, _("[long lines split]")); | |
2392 c = TRUE; | |
2393 } | |
2394 if (notconverted) | |
2395 { | |
2396 STRCAT(IObuff, _("[NOT converted]")); | |
2397 c = TRUE; | |
2398 } | |
2399 else if (converted) | |
2400 { | |
2401 STRCAT(IObuff, _("[converted]")); | |
2402 c = TRUE; | |
2403 } | |
2404 #ifdef FEAT_CRYPT | |
2405 if (cryptkey != NULL) | |
2406 { | |
6122 | 2407 crypt_append_msg(curbuf); |
7 | 2408 c = TRUE; |
2409 } | |
2410 #endif | |
595 | 2411 if (conv_error != 0) |
2412 { | |
2413 sprintf((char *)IObuff + STRLEN(IObuff), | |
2414 _("[CONVERSION ERROR in line %ld]"), (long)conv_error); | |
7 | 2415 c = TRUE; |
2416 } | |
2417 else if (illegal_byte > 0) | |
2418 { | |
2419 sprintf((char *)IObuff + STRLEN(IObuff), | |
2420 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte); | |
2421 c = TRUE; | |
2422 } | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
2423 else if (error) |
7 | 2424 { |
2425 STRCAT(IObuff, _("[READ ERRORS]")); | |
2426 c = TRUE; | |
2427 } | |
2428 if (msg_add_fileformat(fileformat)) | |
2429 c = TRUE; | |
2430 #ifdef FEAT_CRYPT | |
2431 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
|
2432 msg_add_lines(c, (long)linecnt, filesize |
6122 | 2433 - crypt_get_header_len(crypt_get_method_nr(curbuf))); |
7 | 2434 else |
2435 #endif | |
2436 msg_add_lines(c, (long)linecnt, filesize); | |
2437 | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
2438 VIM_CLEAR(keep_msg); |
7 | 2439 msg_scrolled_ign = TRUE; |
2440 #ifdef ALWAYS_USE_GUI | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2441 // Don't show the message when reading stdin, it would end up in a |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2442 // message box (which might be shown when exiting!) |
7 | 2443 if (read_stdin || read_buffer) |
2444 p = msg_may_trunc(FALSE, IObuff); | |
2445 else | |
2446 #endif | |
18199
e2be5a6485f5
patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
18139
diff
changeset
|
2447 { |
e2be5a6485f5
patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
18139
diff
changeset
|
2448 if (msg_col > 0) |
e2be5a6485f5
patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
18139
diff
changeset
|
2449 msg_putchar('\r'); // overwrite previous message |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15531
diff
changeset
|
2450 p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0); |
18199
e2be5a6485f5
patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
18139
diff
changeset
|
2451 } |
7 | 2452 if (read_stdin || read_buffer || restart_edit != 0 |
540 | 2453 || (msg_scrolled != 0 && !need_wait_return)) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2454 // Need to repeat the message after redrawing when: |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2455 // - When reading from stdin (the screen will be cleared next). |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2456 // - When restart_edit is set (otherwise there will be a delay |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2457 // before redrawing). |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2458 // - When the screen was scrolled but there is no wait-return |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2459 // prompt. |
679 | 2460 set_keep_msg(p, 0); |
7 | 2461 msg_scrolled_ign = FALSE; |
2462 } | |
2463 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2464 // with errors writing the file requires ":w!" |
7 | 2465 if (newfile && (error |
595 | 2466 || conv_error != 0 |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
2467 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP))) |
7 | 2468 curbuf->b_p_ro = TRUE; |
2469 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2470 u_clearline(); // cannot use "U" command after adding lines |
7 | 2471 |
2472 /* | |
2473 * In Ex mode: cursor at last new line. | |
2474 * Otherwise: cursor at first new line. | |
2475 */ | |
2476 if (exmode_active) | |
2477 curwin->w_cursor.lnum = from + linecnt; | |
2478 else | |
2479 curwin->w_cursor.lnum = from + 1; | |
2480 check_cursor_lnum(); | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2481 beginline(BL_WHITE | BL_FIX); // on first non-blank |
7 | 2482 |
18619
788d76db02ac
patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents:
18199
diff
changeset
|
2483 if (!cmdmod.lockmarks) |
788d76db02ac
patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents:
18199
diff
changeset
|
2484 { |
788d76db02ac
patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents:
18199
diff
changeset
|
2485 // Set '[ and '] marks to the newly read lines. |
788d76db02ac
patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents:
18199
diff
changeset
|
2486 curbuf->b_op_start.lnum = from + 1; |
788d76db02ac
patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents:
18199
diff
changeset
|
2487 curbuf->b_op_start.col = 0; |
788d76db02ac
patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents:
18199
diff
changeset
|
2488 curbuf->b_op_end.lnum = from + linecnt; |
788d76db02ac
patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents:
18199
diff
changeset
|
2489 curbuf->b_op_end.col = 0; |
788d76db02ac
patch 8.1.2302: :lockmarks does not work for '[ and ']
Bram Moolenaar <Bram@vim.org>
parents:
18199
diff
changeset
|
2490 } |
696 | 2491 |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15840
diff
changeset
|
2492 #ifdef MSWIN |
696 | 2493 /* |
2494 * Work around a weird problem: When a file has two links (only | |
2495 * possible on NTFS) and we write through one link, then stat() it | |
1651 | 2496 * through the other link, the timestamp information may be wrong. |
696 | 2497 * It's correct again after reading the file, thus reset the timestamp |
2498 * here. | |
2499 */ | |
2500 if (newfile && !read_stdin && !read_buffer | |
2501 && mch_stat((char *)fname, &st) >= 0) | |
2502 { | |
2503 buf_store_time(curbuf, &st, fname); | |
2504 curbuf->b_mtime_read = curbuf->b_mtime; | |
2505 } | |
2506 #endif | |
7 | 2507 } |
2508 msg_scroll = msg_save; | |
2509 | |
2510 #ifdef FEAT_VIMINFO | |
2511 /* | |
2512 * Get the marks before executing autocommands, so they can be used there. | |
2513 */ | |
2514 check_marks_read(); | |
2515 #endif | |
2516 | |
2517 /* | |
6933 | 2518 * We remember if the last line of the read didn't have |
2519 * an eol even when 'binary' is off, to support turning 'fixeol' off, | |
2520 * or writing the read again with 'binary' on. The latter is required | |
2521 * for ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work. | |
7 | 2522 */ |
2707 | 2523 curbuf->b_no_eol_lnum = read_no_eol_lnum; |
7 | 2524 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2525 // When reloading a buffer put the cursor at the first line that is |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2526 // different. |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2527 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
|
2528 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
|
2529 |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2530 #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
|
2531 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2532 * 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
|
2533 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2534 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
|
2535 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2536 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
|
2537 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2538 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
|
2539 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
|
2540 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2541 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2542 |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
2543 if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL)) |
7 | 2544 { |
2545 int m = msg_scroll; | |
2546 int n = msg_scrolled; | |
2547 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2548 // Save the fileformat now, otherwise the buffer will be considered |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2549 // modified if the format/encoding was automatically detected. |
819 | 2550 if (set_options) |
7 | 2551 save_file_ff(curbuf); |
2552 | |
2553 /* | |
2554 * The output from the autocommands should not overwrite anything and | |
2555 * should not be overwritten: Set msg_scroll, restore its value if no | |
2556 * output was done. | |
2557 */ | |
2558 msg_scroll = TRUE; | |
2559 if (filtering) | |
2560 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname, | |
2561 FALSE, curbuf, eap); | |
9828
e84e45786691
commit https://github.com/vim/vim/commit/f71d7b9ee5ceba75f70c30845332ddd728fd16c6
Christian Brabandt <cb@256bit.org>
parents:
9682
diff
changeset
|
2562 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
|
2563 { |
7 | 2564 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname, |
2565 FALSE, curbuf, eap); | |
8937
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2566 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
|
2567 /* |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2568 * 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
|
2569 * 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
|
2570 */ |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2571 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
|
2572 TRUE, curbuf); |
da4f6e238374
commit https://github.com/vim/vim/commit/c3691332f72169c486066200c0f3935418364900
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
2573 } |
7 | 2574 else |
2575 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname, | |
2576 FALSE, NULL, eap); | |
2577 if (msg_scrolled == n) | |
2578 msg_scroll = m; | |
2707 | 2579 # ifdef FEAT_EVAL |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2580 if (aborting()) // autocmds may abort script processing |
7 | 2581 return FAIL; |
2707 | 2582 # endif |
2583 } | |
2584 | |
7 | 2585 if (recoverymode && error) |
2586 return FAIL; | |
2587 return OK; | |
2588 } | |
2589 | |
9911
74e345d2878c
commit https://github.com/vim/vim/commit/f04507d132fbcb63999167ec006fc6e700b5af4f
Christian Brabandt <cb@256bit.org>
parents:
9828
diff
changeset
|
2590 #if defined(OPEN_CHR_FILES) || defined(PROTO) |
1313 | 2591 /* |
2592 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+", | |
2593 * which is the name of files used for process substitution output by | |
2594 * some shells on some operating systems, e.g., bash on SunOS. | |
2595 * Do not accept "/dev/fd/[012]", opening these may hang Vim. | |
2596 */ | |
9911
74e345d2878c
commit https://github.com/vim/vim/commit/f04507d132fbcb63999167ec006fc6e700b5af4f
Christian Brabandt <cb@256bit.org>
parents:
9828
diff
changeset
|
2597 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2598 is_dev_fd_file(char_u *fname) |
1313 | 2599 { |
2600 return (STRNCMP(fname, "/dev/fd/", 8) == 0 | |
2601 && VIM_ISDIGIT(fname[8]) | |
2602 && *skipdigits(fname + 9) == NUL | |
2603 && (fname[9] != NUL | |
2604 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2'))); | |
2605 } | |
2606 #endif | |
2607 | |
595 | 2608 /* |
2609 * From the current line count and characters read after that, estimate the | |
2610 * line number where we are now. | |
2611 * Used for error messages that include a line number. | |
2612 */ | |
2613 static linenr_T | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2614 readfile_linenr( |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2615 linenr_T linecnt, // line count before reading more bytes |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2616 char_u *p, // start of more bytes read |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2617 char_u *endp) // end of more bytes read |
595 | 2618 { |
2619 char_u *s; | |
2620 linenr_T lnum; | |
2621 | |
2622 lnum = curbuf->b_ml.ml_line_count - linecnt + 1; | |
2623 for (s = p; s < endp; ++s) | |
2624 if (*s == '\n') | |
2625 ++lnum; | |
2626 return lnum; | |
2627 } | |
2628 | |
7 | 2629 /* |
612 | 2630 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be |
2631 * equal to the buffer "buf". Used for calling readfile(). | |
7 | 2632 * Returns OK or FAIL. |
2633 */ | |
2634 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2635 prep_exarg(exarg_T *eap, buf_T *buf) |
7 | 2636 { |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
2637 eap->cmd = alloc(15 + (unsigned)STRLEN(buf->b_p_fenc)); |
7 | 2638 if (eap->cmd == NULL) |
2639 return FAIL; | |
2640 | |
13575
4df23d9bad47
patch 8.0.1660: the terminal API "drop" command doesn't support options
Christian Brabandt <cb@256bit.org>
parents:
13444
diff
changeset
|
2641 sprintf((char *)eap->cmd, "e ++enc=%s", buf->b_p_fenc); |
4df23d9bad47
patch 8.0.1660: the terminal API "drop" command doesn't support options
Christian Brabandt <cb@256bit.org>
parents:
13444
diff
changeset
|
2642 eap->force_enc = 8; |
612 | 2643 eap->bad_char = buf->b_bad_char; |
13575
4df23d9bad47
patch 8.0.1660: the terminal API "drop" command doesn't support options
Christian Brabandt <cb@256bit.org>
parents:
13444
diff
changeset
|
2644 eap->force_ff = *buf->b_p_ff; |
612 | 2645 |
2646 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN; | |
819 | 2647 eap->read_edit = FALSE; |
612 | 2648 eap->forceit = FALSE; |
7 | 2649 return OK; |
2650 } | |
2651 | |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2652 /* |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2653 * Set default or forced 'fileformat' and 'binary'. |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2654 */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2655 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2656 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
|
2657 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2658 // set default 'fileformat' |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2659 if (set_options) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2660 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2661 if (eap != NULL && eap->force_ff != 0) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2662 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
|
2663 else if (*p_ffs != NUL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2664 set_fileformat(default_fileformat(), OPT_LOCAL); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2665 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2666 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2667 // set or reset 'binary' |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2668 if (eap != NULL && eap->force_bin != 0) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2669 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2670 int oldval = curbuf->b_p_bin; |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2671 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2672 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
|
2673 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
|
2674 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2675 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2676 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2677 /* |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2678 * Set forced 'fileencoding'. |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2679 */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2680 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2681 set_forced_fenc(exarg_T *eap) |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2682 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2683 if (eap->force_enc != 0) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2684 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2685 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
|
2686 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2687 if (fenc != NULL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2688 set_string_option_direct((char_u *)"fenc", -1, |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2689 fenc, OPT_FREE|OPT_LOCAL, 0); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2690 vim_free(fenc); |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2691 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2692 } |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
2693 |
7 | 2694 /* |
2695 * Find next fileencoding to use from 'fileencodings'. | |
2696 * "pp" points to fenc_next. It's advanced to the next item. | |
2697 * When there are no more items, an empty string is returned and *pp is set to | |
2698 * NULL. | |
17692
1e3ff1eae4c3
patch 8.1.1843: might be freeing memory that was not allocated
Bram Moolenaar <Bram@vim.org>
parents:
17584
diff
changeset
|
2699 * When *pp is not set to NULL, the result is in allocated memory and "alloced" |
1e3ff1eae4c3
patch 8.1.1843: might be freeing memory that was not allocated
Bram Moolenaar <Bram@vim.org>
parents:
17584
diff
changeset
|
2700 * is set to TRUE. |
7 | 2701 */ |
2702 static char_u * | |
17692
1e3ff1eae4c3
patch 8.1.1843: might be freeing memory that was not allocated
Bram Moolenaar <Bram@vim.org>
parents:
17584
diff
changeset
|
2703 next_fenc(char_u **pp, int *alloced) |
7 | 2704 { |
2705 char_u *p; | |
2706 char_u *r; | |
2707 | |
17692
1e3ff1eae4c3
patch 8.1.1843: might be freeing memory that was not allocated
Bram Moolenaar <Bram@vim.org>
parents:
17584
diff
changeset
|
2708 *alloced = FALSE; |
7 | 2709 if (**pp == NUL) |
2710 { | |
2711 *pp = NULL; | |
2712 return (char_u *)""; | |
2713 } | |
2714 p = vim_strchr(*pp, ','); | |
2715 if (p == NULL) | |
2716 { | |
2717 r = enc_canonize(*pp); | |
2718 *pp += STRLEN(*pp); | |
2719 } | |
2720 else | |
2721 { | |
2722 r = vim_strnsave(*pp, (int)(p - *pp)); | |
2723 *pp = p + 1; | |
2724 if (r != NULL) | |
2725 { | |
2726 p = enc_canonize(r); | |
2727 vim_free(r); | |
2728 r = p; | |
2729 } | |
2730 } | |
17692
1e3ff1eae4c3
patch 8.1.1843: might be freeing memory that was not allocated
Bram Moolenaar <Bram@vim.org>
parents:
17584
diff
changeset
|
2731 if (r != NULL) |
1e3ff1eae4c3
patch 8.1.1843: might be freeing memory that was not allocated
Bram Moolenaar <Bram@vim.org>
parents:
17584
diff
changeset
|
2732 *alloced = TRUE; |
1e3ff1eae4c3
patch 8.1.1843: might be freeing memory that was not allocated
Bram Moolenaar <Bram@vim.org>
parents:
17584
diff
changeset
|
2733 else |
1e3ff1eae4c3
patch 8.1.1843: might be freeing memory that was not allocated
Bram Moolenaar <Bram@vim.org>
parents:
17584
diff
changeset
|
2734 { |
1e3ff1eae4c3
patch 8.1.1843: might be freeing memory that was not allocated
Bram Moolenaar <Bram@vim.org>
parents:
17584
diff
changeset
|
2735 // out of memory |
7 | 2736 r = (char_u *)""; |
2737 *pp = NULL; | |
2738 } | |
2739 return r; | |
2740 } | |
2741 | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
2742 #ifdef FEAT_EVAL |
7 | 2743 /* |
2744 * Convert a file with the 'charconvert' expression. | |
2745 * This closes the file which is to be read, converts it and opens the | |
2746 * resulting file for reading. | |
2747 * Returns name of the resulting converted file (the caller should delete it | |
2748 * after reading it). | |
2749 * Returns NULL if the conversion failed ("*fdp" is not set) . | |
2750 */ | |
2751 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2752 readfile_charconvert( |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2753 char_u *fname, // name of input file |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2754 char_u *fenc, // converted from |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2755 int *fdp) // in/out: file descriptor of file |
7 | 2756 { |
2757 char_u *tmpname; | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15531
diff
changeset
|
2758 char *errmsg = NULL; |
7 | 2759 |
6721 | 2760 tmpname = vim_tempname('r', FALSE); |
7 | 2761 if (tmpname == NULL) |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15531
diff
changeset
|
2762 errmsg = _("Can't find temp file for conversion"); |
7 | 2763 else |
2764 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2765 close(*fdp); // close the input file, ignore errors |
7 | 2766 *fdp = -1; |
2767 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc, | |
2768 fname, tmpname) == FAIL) | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15531
diff
changeset
|
2769 errmsg = _("Conversion with 'charconvert' failed"); |
7 | 2770 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname, |
2771 O_RDONLY | O_EXTRA, 0)) < 0) | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15531
diff
changeset
|
2772 errmsg = _("can't read output of 'charconvert'"); |
7 | 2773 } |
2774 | |
2775 if (errmsg != NULL) | |
2776 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2777 // Don't use emsg(), it breaks mappings, the retry with |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2778 // another type of conversion might still work. |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15531
diff
changeset
|
2779 msg(errmsg); |
7 | 2780 if (tmpname != NULL) |
2781 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2782 mch_remove(tmpname); // delete converted file |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
2783 VIM_CLEAR(tmpname); |
7 | 2784 } |
2785 } | |
2786 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2787 // If the input file is closed, open it (caller should check for error). |
7 | 2788 if (*fdp < 0) |
2789 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
2790 | |
2791 return tmpname; | |
2792 } | |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
2793 #endif |
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
2794 |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2795 #if defined(FEAT_CRYPT) || defined(PROTO) |
7 | 2796 /* |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2797 * Check for magic number used for encryption. Applies to the current buffer. |
7 | 2798 * If found, the magic number is removed from ptr[*sizep] and *sizep and |
2799 * *filesizep are updated. | |
2800 * Return the (new) encryption key, NULL for no encryption. | |
2801 */ | |
2802 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2803 check_for_cryptkey( |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2804 char_u *cryptkey, // previous encryption key or NULL |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2805 char_u *ptr, // pointer to read bytes |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2806 long *sizep, // length of read bytes |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2807 off_T *filesizep, // nr of bytes used from file |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2808 int newfile, // editing a new buffer |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2809 char_u *fname, // file name to display |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2810 int *did_ask) // flag: whether already asked for key |
7 | 2811 { |
6122 | 2812 int method = crypt_method_nr_from_magic((char *)ptr, *sizep); |
5312 | 2813 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
|
2814 |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2815 if (method >= 0) |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2816 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2817 // Mark the buffer as read-only until the decryption has taken place. |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2818 // Avoids accidentally overwriting the file with garbage. |
5312 | 2819 curbuf->b_p_ro = TRUE; |
2820 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2821 // Set the cryptmethod local to the buffer. |
6122 | 2822 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
|
2823 if (cryptkey == NULL && !*did_ask) |
7 | 2824 { |
2825 if (*curbuf->b_p_key) | |
2826 cryptkey = curbuf->b_p_key; | |
2827 else | |
2828 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2829 // When newfile is TRUE, store the typed key in the 'key' |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2830 // option and don't free it. bf needs hash of the key saved. |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2831 // Don't ask for the key again when first time Enter was hit. |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2832 // Happens when retrying to detect encoding. |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15442
diff
changeset
|
2833 smsg(_(need_key_msg), fname); |
2267 | 2834 msg_scroll = TRUE; |
6353 | 2835 crypt_check_method(method); |
6122 | 2836 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
|
2837 *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
|
2838 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2839 // check if empty key entered |
7 | 2840 if (cryptkey != NULL && *cryptkey == NUL) |
2841 { | |
2842 if (cryptkey != curbuf->b_p_key) | |
2843 vim_free(cryptkey); | |
2844 cryptkey = NULL; | |
2845 } | |
2846 } | |
2847 } | |
2848 | |
2849 if (cryptkey != NULL) | |
2850 { | |
6122 | 2851 int header_len; |
2852 | |
2853 curbuf->b_cryptstate = crypt_create_from_header( | |
2854 method, cryptkey, ptr); | |
2855 crypt_set_cm_option(curbuf, method); | |
2856 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2857 // Remove cryptmethod specific header from the text. |
6122 | 2858 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
|
2859 if (*sizep <= header_len) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2860 // invalid header, buffer can't be encrypted |
10221
fb1fde4fcff7
commit https://github.com/vim/vim/commit/680e015bfe19be6772d3bd754486fbd45c1a9d3b
Christian Brabandt <cb@256bit.org>
parents:
10086
diff
changeset
|
2861 return NULL; |
6122 | 2862 *filesizep += header_len; |
2863 *sizep -= header_len; | |
2864 mch_memmove(ptr, ptr + header_len, (size_t)*sizep); | |
2865 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2866 // Restore the read-only flag. |
5312 | 2867 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
|
2868 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2869 } |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2870 // When starting to edit a new file which does not have encryption, clear |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2871 // 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
|
2872 else if (newfile && *curbuf->b_p_key != NUL && !starting) |
7 | 2873 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL); |
2874 | |
2875 return cryptkey; | |
2876 } | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2877 #endif // FEAT_CRYPT |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2878 |
7 | 2879 /* |
1303 | 2880 * Return TRUE if a file appears to be read-only from the file permissions. |
2881 */ | |
2882 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2883 check_file_readonly( |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2884 char_u *fname, // full path to file |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2885 int perm UNUSED) // known permissions on file |
1303 | 2886 { |
2887 #ifndef USE_MCH_ACCESS | |
2888 int fd = 0; | |
2889 #endif | |
2890 | |
2891 return ( | |
2892 #ifdef USE_MCH_ACCESS | |
2893 # ifdef UNIX | |
2894 (perm & 0222) == 0 || | |
2895 # endif | |
2896 mch_access((char *)fname, W_OK) | |
2897 #else | |
2898 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0 | |
2899 ? TRUE : (close(fd), FALSE) | |
2900 #endif | |
2901 ); | |
2902 } | |
2903 | |
15816
40336d427dd2
patch 8.1.0915: fsync() may not work properly on Mac
Bram Moolenaar <Bram@vim.org>
parents:
15794
diff
changeset
|
2904 #if defined(HAVE_FSYNC) || defined(PROTO) |
40336d427dd2
patch 8.1.0915: fsync() may not work properly on Mac
Bram Moolenaar <Bram@vim.org>
parents:
15794
diff
changeset
|
2905 /* |
40336d427dd2
patch 8.1.0915: fsync() may not work properly on Mac
Bram Moolenaar <Bram@vim.org>
parents:
15794
diff
changeset
|
2906 * Call fsync() with Mac-specific exception. |
40336d427dd2
patch 8.1.0915: fsync() may not work properly on Mac
Bram Moolenaar <Bram@vim.org>
parents:
15794
diff
changeset
|
2907 * Return fsync() result: zero for success. |
40336d427dd2
patch 8.1.0915: fsync() may not work properly on Mac
Bram Moolenaar <Bram@vim.org>
parents:
15794
diff
changeset
|
2908 */ |
40336d427dd2
patch 8.1.0915: fsync() may not work properly on Mac
Bram Moolenaar <Bram@vim.org>
parents:
15794
diff
changeset
|
2909 int |
40336d427dd2
patch 8.1.0915: fsync() may not work properly on Mac
Bram Moolenaar <Bram@vim.org>
parents:
15794
diff
changeset
|
2910 vim_fsync(int fd) |
40336d427dd2
patch 8.1.0915: fsync() may not work properly on Mac
Bram Moolenaar <Bram@vim.org>
parents:
15794
diff
changeset
|
2911 { |
40336d427dd2
patch 8.1.0915: fsync() may not work properly on Mac
Bram Moolenaar <Bram@vim.org>
parents:
15794
diff
changeset
|
2912 int r; |
40336d427dd2
patch 8.1.0915: fsync() may not work properly on Mac
Bram Moolenaar <Bram@vim.org>
parents:
15794
diff
changeset
|
2913 |
40336d427dd2
patch 8.1.0915: fsync() may not work properly on Mac
Bram Moolenaar <Bram@vim.org>
parents:
15794
diff
changeset
|
2914 # ifdef MACOS_X |
40336d427dd2
patch 8.1.0915: fsync() may not work properly on Mac
Bram Moolenaar <Bram@vim.org>
parents:
15794
diff
changeset
|
2915 r = fcntl(fd, F_FULLFSYNC); |
15910
da4d1f69374e
patch 8.1.0961: Mac: fsync may fail sometimes
Bram Moolenaar <Bram@vim.org>
parents:
15902
diff
changeset
|
2916 if (r != 0) // F_FULLFSYNC not working or not supported |
15816
40336d427dd2
patch 8.1.0915: fsync() may not work properly on Mac
Bram Moolenaar <Bram@vim.org>
parents:
15794
diff
changeset
|
2917 # endif |
40336d427dd2
patch 8.1.0915: fsync() may not work properly on Mac
Bram Moolenaar <Bram@vim.org>
parents:
15794
diff
changeset
|
2918 r = fsync(fd); |
40336d427dd2
patch 8.1.0915: fsync() may not work properly on Mac
Bram Moolenaar <Bram@vim.org>
parents:
15794
diff
changeset
|
2919 return r; |
40336d427dd2
patch 8.1.0915: fsync() may not work properly on Mac
Bram Moolenaar <Bram@vim.org>
parents:
15794
diff
changeset
|
2920 } |
40336d427dd2
patch 8.1.0915: fsync() may not work properly on Mac
Bram Moolenaar <Bram@vim.org>
parents:
15794
diff
changeset
|
2921 #endif |
40336d427dd2
patch 8.1.0915: fsync() may not work properly on Mac
Bram Moolenaar <Bram@vim.org>
parents:
15794
diff
changeset
|
2922 |
7 | 2923 /* |
633 | 2924 * Set the name of the current buffer. Use when the buffer doesn't have a |
2925 * name and a ":r" or ":w" command with a file name is used. | |
2926 */ | |
18199
e2be5a6485f5
patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
18139
diff
changeset
|
2927 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2928 set_rw_fname(char_u *fname, char_u *sfname) |
633 | 2929 { |
1905 | 2930 buf_T *buf = curbuf; |
2931 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2932 // It's like the unnamed buffer is deleted.... |
633 | 2933 if (curbuf->b_p_bl) |
2934 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); | |
2935 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf); | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
2936 #ifdef FEAT_EVAL |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2937 if (aborting()) // autocmds may abort script processing |
633 | 2938 return FAIL; |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
2939 #endif |
1905 | 2940 if (curbuf != buf) |
2941 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2942 // We are in another buffer now, don't do the renaming. |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15442
diff
changeset
|
2943 emsg(_(e_auchangedbuf)); |
1905 | 2944 return FAIL; |
2945 } | |
633 | 2946 |
2947 if (setfname(curbuf, fname, sfname, FALSE) == OK) | |
2948 curbuf->b_flags |= BF_NOTEDITED; | |
2949 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2950 // ....and a new named one is created |
633 | 2951 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf); |
2952 if (curbuf->b_p_bl) | |
2953 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf); | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
2954 #ifdef FEAT_EVAL |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2955 if (aborting()) // autocmds may abort script processing |
633 | 2956 return FAIL; |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
2957 #endif |
633 | 2958 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
2959 // Do filetype detection now if 'filetype' is empty. |
633 | 2960 if (*curbuf->b_p_ft == NUL) |
2961 { | |
819 | 2962 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
|
2963 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL); |
717 | 2964 do_modelines(0); |
633 | 2965 } |
2966 | |
2967 return OK; | |
2968 } | |
2969 | |
2970 /* | |
7 | 2971 * Put file name into IObuff with quotes. |
2972 */ | |
33 | 2973 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2974 msg_add_fname(buf_T *buf, char_u *fname) |
7 | 2975 { |
2976 if (fname == NULL) | |
2977 fname = (char_u *)"-stdin-"; | |
2978 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE); | |
2979 IObuff[0] = '"'; | |
2980 STRCAT(IObuff, "\" "); | |
2981 } | |
2982 | |
2983 /* | |
2984 * Append message for text mode to IObuff. | |
2985 * Return TRUE if something appended. | |
2986 */ | |
18199
e2be5a6485f5
patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
18139
diff
changeset
|
2987 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2988 msg_add_fileformat(int eol_type) |
7 | 2989 { |
2990 #ifndef USE_CRNL | |
2991 if (eol_type == EOL_DOS) | |
2992 { | |
2993 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]")); | |
2994 return TRUE; | |
2995 } | |
2996 #endif | |
2997 if (eol_type == EOL_MAC) | |
2998 { | |
2999 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]")); | |
3000 return TRUE; | |
3001 } | |
15840
734b1928a5aa
patch 8.1.0927: USE_CR is never defined
Bram Moolenaar <Bram@vim.org>
parents:
15816
diff
changeset
|
3002 #ifdef USE_CRNL |
7 | 3003 if (eol_type == EOL_UNIX) |
3004 { | |
3005 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]")); | |
3006 return TRUE; | |
3007 } | |
3008 #endif | |
3009 return FALSE; | |
3010 } | |
3011 | |
3012 /* | |
3013 * Append line and character count to IObuff. | |
3014 */ | |
33 | 3015 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3016 msg_add_lines( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3017 int insert_space, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3018 long lnum, |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
3019 off_T nchars) |
7 | 3020 { |
3021 char_u *p; | |
3022 | |
3023 p = IObuff + STRLEN(IObuff); | |
3024 | |
3025 if (insert_space) | |
3026 *p++ = ' '; | |
3027 if (shortmess(SHM_LINES)) | |
9391
4c40631238e4
commit https://github.com/vim/vim/commit/bde9810d6103ffe3a22a9330021cb21db1ed1792
Christian Brabandt <cb@256bit.org>
parents:
9389
diff
changeset
|
3028 vim_snprintf((char *)p, IOSIZE - (p - IObuff), |
19477
2bb0e80fcd32
patch 8.2.0296: mixing up "long long" and __int64 may cause problems
Bram Moolenaar <Bram@vim.org>
parents:
19301
diff
changeset
|
3029 "%ldL, %lldC", lnum, (varnumber_T)nchars); |
7 | 3030 else |
3031 { | |
14585
c8f07e8b273e
patch 8.1.0306: plural messages are not translated properly
Christian Brabandt <cb@256bit.org>
parents:
14509
diff
changeset
|
3032 sprintf((char *)p, NGETTEXT("%ld line, ", "%ld lines, ", lnum), lnum); |
7 | 3033 p += STRLEN(p); |
14585
c8f07e8b273e
patch 8.1.0306: plural messages are not translated properly
Christian Brabandt <cb@256bit.org>
parents:
14509
diff
changeset
|
3034 vim_snprintf((char *)p, IOSIZE - (p - IObuff), |
c8f07e8b273e
patch 8.1.0306: plural messages are not translated properly
Christian Brabandt <cb@256bit.org>
parents:
14509
diff
changeset
|
3035 NGETTEXT("%lld character", "%lld characters", nchars), |
19477
2bb0e80fcd32
patch 8.2.0296: mixing up "long long" and __int64 may cause problems
Bram Moolenaar <Bram@vim.org>
parents:
19301
diff
changeset
|
3036 (varnumber_T)nchars); |
7 | 3037 } |
3038 } | |
3039 | |
3040 /* | |
3041 * Append message for missing line separator to IObuff. | |
3042 */ | |
18199
e2be5a6485f5
patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
18139
diff
changeset
|
3043 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3044 msg_add_eol(void) |
7 | 3045 { |
3046 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]")); | |
3047 } | |
3048 | |
18199
e2be5a6485f5
patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
18139
diff
changeset
|
3049 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3050 time_differs(long t1, long t2) |
7 | 3051 { |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3052 #if defined(__linux__) || defined(MSWIN) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3053 // On a FAT filesystem, esp. under Linux, there are only 5 bits to store |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3054 // the seconds. Since the roundoff is done when flushing the inode, the |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3055 // time may change unexpectedly by one second!!! |
7 | 3056 return (t1 - t2 > 1 || t2 - t1 > 1); |
3057 #else | |
3058 return (t1 != t2); | |
3059 #endif | |
3060 } | |
3061 | |
3062 /* | |
1948 | 3063 * Return TRUE if file encoding "fenc" requires conversion from or to |
3064 * 'encoding'. | |
7 | 3065 */ |
18199
e2be5a6485f5
patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
18139
diff
changeset
|
3066 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3067 need_conversion(char_u *fenc) |
7 | 3068 { |
1948 | 3069 int same_encoding; |
3070 int enc_flags; | |
3071 int fenc_flags; | |
3072 | |
3073 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0) | |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
3074 { |
1948 | 3075 same_encoding = TRUE; |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
3076 fenc_flags = 0; |
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
3077 } |
1948 | 3078 else |
3079 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3080 // Ignore difference between "ansi" and "latin1", "ucs-4" and |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3081 // "ucs-4be", etc. |
1948 | 3082 enc_flags = get_fio_flags(p_enc); |
3083 fenc_flags = get_fio_flags(fenc); | |
3084 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags); | |
3085 } | |
3086 if (same_encoding) | |
3087 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3088 // Specified encoding matches with 'encoding'. This requires |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3089 // conversion when 'encoding' is Unicode but not UTF-8. |
1948 | 3090 return enc_unicode != 0; |
3091 } | |
3092 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3093 // Encodings differ. However, conversion is not needed when 'enc' is any |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3094 // Unicode encoding and the file is UTF-8. |
1948 | 3095 return !(enc_utf8 && fenc_flags == FIO_UTF8); |
7 | 3096 } |
3097 | |
3098 /* | |
3099 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the | |
3100 * internal conversion. | |
3101 * if "ptr" is an empty string, use 'encoding'. | |
3102 */ | |
18199
e2be5a6485f5
patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
18139
diff
changeset
|
3103 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3104 get_fio_flags(char_u *ptr) |
7 | 3105 { |
3106 int prop; | |
3107 | |
3108 if (*ptr == NUL) | |
3109 ptr = p_enc; | |
3110 | |
3111 prop = enc_canon_props(ptr); | |
3112 if (prop & ENC_UNICODE) | |
3113 { | |
3114 if (prop & ENC_2BYTE) | |
3115 { | |
3116 if (prop & ENC_ENDIAN_L) | |
3117 return FIO_UCS2 | FIO_ENDIAN_L; | |
3118 return FIO_UCS2; | |
3119 } | |
3120 if (prop & ENC_4BYTE) | |
3121 { | |
3122 if (prop & ENC_ENDIAN_L) | |
3123 return FIO_UCS4 | FIO_ENDIAN_L; | |
3124 return FIO_UCS4; | |
3125 } | |
3126 if (prop & ENC_2WORD) | |
3127 { | |
3128 if (prop & ENC_ENDIAN_L) | |
3129 return FIO_UTF16 | FIO_ENDIAN_L; | |
3130 return FIO_UTF16; | |
3131 } | |
3132 return FIO_UTF8; | |
3133 } | |
3134 if (prop & ENC_LATIN1) | |
3135 return FIO_LATIN1; | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3136 // must be ENC_DBCS, requires iconv() |
7 | 3137 return 0; |
3138 } | |
3139 | |
18199
e2be5a6485f5
patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
18139
diff
changeset
|
3140 #if defined(MSWIN) || defined(PROTO) |
7 | 3141 /* |
3142 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed | |
3143 * for the conversion MS-Windows can do for us. Also accept "utf-8". | |
3144 * Used for conversion between 'encoding' and 'fileencoding'. | |
3145 */ | |
18199
e2be5a6485f5
patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
18139
diff
changeset
|
3146 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3147 get_win_fio_flags(char_u *ptr) |
7 | 3148 { |
3149 int cp; | |
3150 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3151 // Cannot do this when 'encoding' is not utf-8 and not a codepage. |
7 | 3152 if (!enc_utf8 && enc_codepage <= 0) |
3153 return 0; | |
3154 | |
3155 cp = encname2codepage(ptr); | |
3156 if (cp == 0) | |
3157 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3158 # ifdef CP_UTF8 // VC 4.1 doesn't define CP_UTF8 |
7 | 3159 if (STRCMP(ptr, "utf-8") == 0) |
3160 cp = CP_UTF8; | |
3161 else | |
3162 # endif | |
3163 return 0; | |
3164 } | |
3165 return FIO_PUT_CP(cp) | FIO_CODEPAGE; | |
3166 } | |
3167 #endif | |
3168 | |
18199
e2be5a6485f5
patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
18139
diff
changeset
|
3169 #if defined(MACOS_CONVERT) || defined(PROTO) |
7 | 3170 /* |
3171 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags | |
3172 * needed for the internal conversion to/from utf-8 or latin1. | |
3173 */ | |
18199
e2be5a6485f5
patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
18139
diff
changeset
|
3174 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3175 get_mac_fio_flags(char_u *ptr) |
7 | 3176 { |
3177 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
3178 && (enc_canon_props(ptr) & ENC_MACROMAN)) | |
3179 return FIO_MACROMAN; | |
3180 return 0; | |
3181 } | |
3182 #endif | |
3183 | |
3184 /* | |
3185 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size]. | |
3186 * "size" must be at least 2. | |
3187 * Return the name of the encoding and set "*lenp" to the length. | |
3188 * Returns NULL when no BOM found. | |
3189 */ | |
3190 static char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3191 check_for_bom( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3192 char_u *p, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3193 long size, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3194 int *lenp, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3195 int flags) |
7 | 3196 { |
3197 char *name = NULL; | |
3198 int len = 2; | |
3199 | |
3200 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf | |
1688 | 3201 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0)) |
7 | 3202 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3203 name = "utf-8"; // EF BB BF |
7 | 3204 len = 3; |
3205 } | |
3206 else if (p[0] == 0xff && p[1] == 0xfe) | |
3207 { | |
3208 if (size >= 4 && p[2] == 0 && p[3] == 0 | |
3209 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L))) | |
3210 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3211 name = "ucs-4le"; // FF FE 00 00 |
7 | 3212 len = 4; |
3213 } | |
1735 | 3214 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L)) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3215 name = "ucs-2le"; // FF FE |
1735 | 3216 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L)) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3217 // utf-16le is preferred, it also works for ucs-2le text |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3218 name = "utf-16le"; // FF FE |
7 | 3219 } |
3220 else if (p[0] == 0xfe && p[1] == 0xff | |
3221 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16)) | |
3222 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3223 // Default to utf-16, it works also for ucs-2 text. |
1547 | 3224 if (flags == FIO_UCS2) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3225 name = "ucs-2"; // FE FF |
1547 | 3226 else |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3227 name = "utf-16"; // FE FF |
7 | 3228 } |
3229 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe | |
3230 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4)) | |
3231 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3232 name = "ucs-4"; // 00 00 FE FF |
7 | 3233 len = 4; |
3234 } | |
3235 | |
3236 *lenp = len; | |
3237 return (char_u *)name; | |
3238 } | |
3239 | |
3240 /* | |
3241 * Try to find a shortname by comparing the fullname with the current | |
3242 * directory. | |
1411 | 3243 * Returns "full_path" or pointer into "full_path" if shortened. |
3244 */ | |
3245 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3246 shorten_fname1(char_u *full_path) |
1411 | 3247 { |
2770 | 3248 char_u *dirname; |
1411 | 3249 char_u *p = full_path; |
3250 | |
2770 | 3251 dirname = alloc(MAXPATHL); |
3252 if (dirname == NULL) | |
3253 return full_path; | |
1411 | 3254 if (mch_dirname(dirname, MAXPATHL) == OK) |
3255 { | |
3256 p = shorten_fname(full_path, dirname); | |
3257 if (p == NULL || *p == NUL) | |
3258 p = full_path; | |
3259 } | |
2770 | 3260 vim_free(dirname); |
1411 | 3261 return p; |
3262 } | |
3263 | |
3264 /* | |
3265 * Try to find a shortname by comparing the fullname with the current | |
3266 * directory. | |
7 | 3267 * Returns NULL if not shorter name possible, pointer into "full_path" |
3268 * otherwise. | |
3269 */ | |
3270 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3271 shorten_fname(char_u *full_path, char_u *dir_name) |
7 | 3272 { |
3273 int len; | |
3274 char_u *p; | |
3275 | |
3276 if (full_path == NULL) | |
3277 return NULL; | |
3278 len = (int)STRLEN(dir_name); | |
3279 if (fnamencmp(dir_name, full_path, len) == 0) | |
3280 { | |
3281 p = full_path + len; | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3282 #if defined(MSWIN) |
7 | 3283 /* |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15840
diff
changeset
|
3284 * MS-Windows: when a file is in the root directory, dir_name will end |
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15840
diff
changeset
|
3285 * in a slash, since C: by itself does not define a specific dir. In |
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15840
diff
changeset
|
3286 * this case p may already be correct. <negri> |
7 | 3287 */ |
3288 if (!((len > 2) && (*(p - 2) == ':'))) | |
3289 #endif | |
3290 { | |
3291 if (vim_ispathsep(*p)) | |
3292 ++p; | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3293 #ifndef VMS // the path separator is always part of the path |
7 | 3294 else |
3295 p = NULL; | |
3296 #endif | |
3297 } | |
3298 } | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3299 #if defined(MSWIN) |
7 | 3300 /* |
3301 * When using a file in the current drive, remove the drive name: | |
3302 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on | |
3303 * a floppy from "A:\dir" to "B:\dir". | |
3304 */ | |
3305 else if (len > 3 | |
3306 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0]) | |
3307 && full_path[1] == ':' | |
3308 && vim_ispathsep(full_path[2])) | |
3309 p = full_path + 2; | |
3310 #endif | |
3311 else | |
3312 p = NULL; | |
3313 return p; | |
3314 } | |
3315 | |
3316 /* | |
13819
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3317 * Shorten filename of a buffer. |
7 | 3318 * When "force" is TRUE: Use full path from now on for files currently being |
3319 * edited, both for file name and swap file name. Try to shorten the file | |
3320 * names a bit, if safe to do so. | |
3321 * When "force" is FALSE: Only try to shorten absolute file names. | |
3322 * For buffers that have buftype "nofile" or "scratch": never change the file | |
3323 * name. | |
3324 */ | |
3325 void | |
13819
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3326 shorten_buf_fname(buf_T *buf, char_u *dirname, int force) |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3327 { |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3328 char_u *p; |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3329 |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3330 if (buf->b_fname != NULL |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3331 #ifdef FEAT_QUICKFIX |
17095
10e0d7d96cb0
patch 8.1.1547: functionality of bt_nofile() is confusing
Bram Moolenaar <Bram@vim.org>
parents:
16996
diff
changeset
|
3332 && !bt_nofilename(buf) |
13819
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3333 #endif |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3334 && !path_with_url(buf->b_fname) |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3335 && (force |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3336 || buf->b_sfname == NULL |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3337 || mch_isFullName(buf->b_sfname))) |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3338 { |
14917
6f2ce3b311de
patch 8.1.0470: pointer ownership around fname_expand() is unclear
Bram Moolenaar <Bram@vim.org>
parents:
14862
diff
changeset
|
3339 if (buf->b_sfname != buf->b_ffname) |
6f2ce3b311de
patch 8.1.0470: pointer ownership around fname_expand() is unclear
Bram Moolenaar <Bram@vim.org>
parents:
14862
diff
changeset
|
3340 VIM_CLEAR(buf->b_sfname); |
13819
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3341 p = shorten_fname(buf->b_ffname, dirname); |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3342 if (p != NULL) |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3343 { |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3344 buf->b_sfname = vim_strsave(p); |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3345 buf->b_fname = buf->b_sfname; |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3346 } |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3347 if (p == NULL || buf->b_fname == NULL) |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3348 buf->b_fname = buf->b_ffname; |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3349 } |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3350 } |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3351 |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3352 /* |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3353 * Shorten filenames for all buffers. |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3354 */ |
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3355 void |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3356 shorten_fnames(int force) |
7 | 3357 { |
3358 char_u dirname[MAXPATHL]; | |
3359 buf_T *buf; | |
3360 | |
3361 mch_dirname(dirname, MAXPATHL); | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
3362 FOR_ALL_BUFFERS(buf) |
7 | 3363 { |
13819
31bb8e1f7625
patch 8.0.1781: file names in quickfix window are not shortened
Christian Brabandt <cb@256bit.org>
parents:
13810
diff
changeset
|
3364 shorten_buf_fname(buf, dirname, force); |
9 | 3365 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3366 // Always make the swap file name a full path, a "nofile" buffer may |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3367 // also have a swap file. |
9 | 3368 mf_fullname(buf->b_ml.ml_mfp); |
7 | 3369 } |
3370 status_redraw_all(); | |
672 | 3371 redraw_tabline = TRUE; |
18767
068337e86133
patch 8.1.2373: cannot build with +popupwin but without +quickfix
Bram Moolenaar <Bram@vim.org>
parents:
18763
diff
changeset
|
3372 #if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX) |
17584
65a8099fc0e8
patch 8.1.1789: cannot see file name of preview popup window
Bram Moolenaar <Bram@vim.org>
parents:
17565
diff
changeset
|
3373 popup_update_preview_title(); |
65a8099fc0e8
patch 8.1.1789: cannot see file name of preview popup window
Bram Moolenaar <Bram@vim.org>
parents:
17565
diff
changeset
|
3374 #endif |
7 | 3375 } |
3376 | |
3377 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \ | |
3378 || defined(FEAT_GUI_MSWIN) \ | |
3379 || defined(FEAT_GUI_MAC) \ | |
19526
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
3380 || defined(FEAT_GUI_HAIKU) \ |
7 | 3381 || defined(PROTO) |
3382 /* | |
3383 * Shorten all filenames in "fnames[count]" by current directory. | |
3384 */ | |
3385 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3386 shorten_filenames(char_u **fnames, int count) |
7 | 3387 { |
3388 int i; | |
3389 char_u dirname[MAXPATHL]; | |
3390 char_u *p; | |
3391 | |
3392 if (fnames == NULL || count < 1) | |
3393 return; | |
3394 mch_dirname(dirname, sizeof(dirname)); | |
3395 for (i = 0; i < count; ++i) | |
3396 { | |
3397 if ((p = shorten_fname(fnames[i], dirname)) != NULL) | |
3398 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3399 // shorten_fname() returns pointer in given "fnames[i]". If free |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3400 // "fnames[i]" first, "p" becomes invalid. So we need to copy |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3401 // "p" first then free fnames[i]. |
7 | 3402 p = vim_strsave(p); |
3403 vim_free(fnames[i]); | |
3404 fnames[i] = p; | |
3405 } | |
3406 } | |
3407 } | |
3408 #endif | |
3409 | |
3410 /* | |
14475
dddba3937532
patch 8.1.0251: using full path is not supported for 'backupdir'
Christian Brabandt <cb@256bit.org>
parents:
14433
diff
changeset
|
3411 * Add extension to file name - change path/fo.o.h to path/fo.o.h.ext or |
7 | 3412 * fo_o_h.ext for MSDOS or when shortname option set. |
3413 * | |
3414 * Assumed that fname is a valid name found in the filesystem we assure that | |
3415 * the return value is a different name and ends in 'ext'. | |
3416 * "ext" MUST be at most 4 characters long if it starts with a dot, 3 | |
3417 * characters otherwise. | |
3418 * Space for the returned name is allocated, must be freed later. | |
3419 * Returns NULL when out of memory. | |
3420 */ | |
3421 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3422 modname( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3423 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3424 char_u *ext, |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3425 int prepend_dot) // may prepend a '.' to file name |
7 | 3426 { |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8080
diff
changeset
|
3427 return buf_modname((curbuf->b_p_sn || curbuf->b_shortname), |
7 | 3428 fname, ext, prepend_dot); |
3429 } | |
3430 | |
3431 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3432 buf_modname( |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3433 int shortname, // use 8.3 file name |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3434 char_u *fname, |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3435 char_u *ext, |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3436 int prepend_dot) // may prepend a '.' to file name |
7 | 3437 { |
3438 char_u *retval; | |
3439 char_u *s; | |
3440 char_u *e; | |
3441 char_u *ptr; | |
3442 int fnamelen, extlen; | |
3443 | |
3444 extlen = (int)STRLEN(ext); | |
3445 | |
3446 /* | |
3447 * If there is no file name we must get the name of the current directory | |
3448 * (we need the full path in case :cd is used). | |
3449 */ | |
3450 if (fname == NULL || *fname == NUL) | |
3451 { | |
16764
ef00b6bc186b
patch 8.1.1384: using "int" for alloc() often results in compiler warnings
Bram Moolenaar <Bram@vim.org>
parents:
16754
diff
changeset
|
3452 retval = alloc(MAXPATHL + extlen + 3); |
7 | 3453 if (retval == NULL) |
3454 return NULL; | |
3455 if (mch_dirname(retval, MAXPATHL) == FAIL || | |
3456 (fnamelen = (int)STRLEN(retval)) == 0) | |
3457 { | |
3458 vim_free(retval); | |
3459 return NULL; | |
3460 } | |
39 | 3461 if (!after_pathsep(retval, retval + fnamelen)) |
7 | 3462 { |
3463 retval[fnamelen++] = PATHSEP; | |
3464 retval[fnamelen] = NUL; | |
3465 } | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3466 prepend_dot = FALSE; // nothing to prepend a dot to |
7 | 3467 } |
3468 else | |
3469 { | |
3470 fnamelen = (int)STRLEN(fname); | |
16764
ef00b6bc186b
patch 8.1.1384: using "int" for alloc() often results in compiler warnings
Bram Moolenaar <Bram@vim.org>
parents:
16754
diff
changeset
|
3471 retval = alloc(fnamelen + extlen + 3); |
7 | 3472 if (retval == NULL) |
3473 return NULL; | |
3474 STRCPY(retval, fname); | |
3475 #ifdef VMS | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3476 vms_remove_version(retval); // we do not need versions here |
7 | 3477 #endif |
3478 } | |
3479 | |
3480 /* | |
3481 * search backwards until we hit a '/', '\' or ':' replacing all '.' | |
3482 * by '_' for MSDOS or when shortname option set and ext starts with a dot. | |
3483 * Then truncate what is after the '/', '\' or ':' to 8 characters for | |
3484 * MSDOS and 26 characters for AMIGA, a lot more for UNIX. | |
3485 */ | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
3486 for (ptr = retval + fnamelen; ptr > retval; MB_PTR_BACK(retval, ptr)) |
7 | 3487 { |
15794
0d8291665b59
patch 8.1.0904: USE_LONG_FNAME never defined
Bram Moolenaar <Bram@vim.org>
parents:
15776
diff
changeset
|
3488 if (*ext == '.' && shortname) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3489 if (*ptr == '.') // replace '.' by '_' |
7 | 3490 *ptr = '_'; |
3491 if (vim_ispathsep(*ptr)) | |
391 | 3492 { |
3493 ++ptr; | |
7 | 3494 break; |
391 | 3495 } |
3496 } | |
7 | 3497 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3498 // the file name has at most BASENAMELEN characters. |
7 | 3499 if (STRLEN(ptr) > (unsigned)BASENAMELEN) |
3500 ptr[BASENAMELEN] = '\0'; | |
3501 | |
3502 s = ptr + STRLEN(ptr); | |
3503 | |
3504 /* | |
3505 * For 8.3 file names we may have to reduce the length. | |
3506 */ | |
3507 if (shortname) | |
3508 { | |
3509 /* | |
3510 * If there is no file name, or the file name ends in '/', and the | |
3511 * extension starts with '.', put a '_' before the dot, because just | |
3512 * ".ext" is invalid. | |
3513 */ | |
3514 if (fname == NULL || *fname == NUL | |
3515 || vim_ispathsep(fname[STRLEN(fname) - 1])) | |
3516 { | |
3517 if (*ext == '.') | |
3518 *s++ = '_'; | |
3519 } | |
3520 /* | |
3521 * If the extension starts with '.', truncate the base name at 8 | |
3522 * characters | |
3523 */ | |
3524 else if (*ext == '.') | |
3525 { | |
1877 | 3526 if ((size_t)(s - ptr) > (size_t)8) |
7 | 3527 { |
3528 s = ptr + 8; | |
3529 *s = '\0'; | |
3530 } | |
3531 } | |
3532 /* | |
3533 * If the extension doesn't start with '.', and the file name | |
3534 * doesn't have an extension yet, append a '.' | |
3535 */ | |
3536 else if ((e = vim_strchr(ptr, '.')) == NULL) | |
3537 *s++ = '.'; | |
3538 /* | |
3539 * If the extension doesn't start with '.', and there already is an | |
1201 | 3540 * extension, it may need to be truncated |
7 | 3541 */ |
3542 else if ((int)STRLEN(e) + extlen > 4) | |
3543 s = e + 4 - extlen; | |
3544 } | |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15840
diff
changeset
|
3545 #ifdef MSWIN |
7 | 3546 /* |
3547 * If there is no file name, and the extension starts with '.', put a | |
3548 * '_' before the dot, because just ".ext" may be invalid if it's on a | |
3549 * FAT partition, and on HPFS it doesn't matter. | |
3550 */ | |
3551 else if ((fname == NULL || *fname == NUL) && *ext == '.') | |
3552 *s++ = '_'; | |
3553 #endif | |
3554 | |
3555 /* | |
1651 | 3556 * Append the extension. |
7 | 3557 * ext can start with '.' and cannot exceed 3 more characters. |
3558 */ | |
3559 STRCPY(s, ext); | |
3560 | |
3561 /* | |
3562 * Prepend the dot. | |
3563 */ | |
15794
0d8291665b59
patch 8.1.0904: USE_LONG_FNAME never defined
Bram Moolenaar <Bram@vim.org>
parents:
15776
diff
changeset
|
3564 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.') |
7 | 3565 { |
1620 | 3566 STRMOVE(e + 1, e); |
7 | 3567 *e = '.'; |
3568 } | |
3569 | |
3570 /* | |
3571 * Check that, after appending the extension, the file name is really | |
3572 * different. | |
3573 */ | |
3574 if (fname != NULL && STRCMP(fname, retval) == 0) | |
3575 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3576 // we search for a character that can be replaced by '_' |
7 | 3577 while (--s >= ptr) |
3578 { | |
3579 if (*s != '_') | |
3580 { | |
3581 *s = '_'; | |
3582 break; | |
3583 } | |
3584 } | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3585 if (s < ptr) // fname was "________.<ext>", how tricky! |
7 | 3586 *ptr = 'v'; |
3587 } | |
3588 return retval; | |
3589 } | |
3590 | |
3591 /* | |
3592 * Like fgets(), but if the file line is too long, it is truncated and the | |
3593 * rest of the line is thrown away. Returns TRUE for end-of-file. | |
13037
6e81a68d63a1
patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents:
12863
diff
changeset
|
3594 * If the line is truncated then buf[size - 2] will not be NUL. |
7 | 3595 */ |
3596 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3597 vim_fgets(char_u *buf, int size, FILE *fp) |
7 | 3598 { |
3599 char *eof; | |
3600 #define FGETS_SIZE 200 | |
3601 char tbuf[FGETS_SIZE]; | |
3602 | |
3603 buf[size - 2] = NUL; | |
3604 eof = fgets((char *)buf, size, fp); | |
3605 if (buf[size - 2] != NUL && buf[size - 2] != '\n') | |
3606 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3607 buf[size - 1] = NUL; // Truncate the line |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3608 |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3609 // Now throw away the rest of the line: |
7 | 3610 do |
3611 { | |
3612 tbuf[FGETS_SIZE - 2] = NUL; | |
14730
193471015e1a
patch 8.1.0377: xdiff doesn't use the Vim memory allocation functions
Christian Brabandt <cb@256bit.org>
parents:
14700
diff
changeset
|
3613 vim_ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp); |
7 | 3614 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n'); |
3615 } | |
3616 return (eof == NULL); | |
3617 } | |
3618 | |
3619 /* | |
3620 * rename() only works if both files are on the same file system, this | |
3621 * function will (attempts to?) copy the file across if rename fails -- webb | |
3622 * Return -1 for failure, 0 for success. | |
3623 */ | |
3624 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3625 vim_rename(char_u *from, char_u *to) |
7 | 3626 { |
3627 int fd_in; | |
3628 int fd_out; | |
3629 int n; | |
3630 char *errmsg = NULL; | |
3631 char *buffer; | |
3632 #ifdef AMIGA | |
3633 BPTR flock; | |
3634 #endif | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
3635 stat_T st; |
194 | 3636 long perm; |
199 | 3637 #ifdef HAVE_ACL |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3638 vim_acl_T acl; // ACL from original file |
199 | 3639 #endif |
1779 | 3640 int use_tmp_file = FALSE; |
7 | 3641 |
3642 /* | |
1779 | 3643 * When the names are identical, there is nothing to do. When they refer |
3644 * to the same file (ignoring case and slash/backslash differences) but | |
3645 * the file name differs we need to go through a temp file. | |
7 | 3646 */ |
3647 if (fnamecmp(from, to) == 0) | |
1779 | 3648 { |
4242 | 3649 if (p_fic && STRCMP(gettail(from), gettail(to)) != 0) |
1779 | 3650 use_tmp_file = TRUE; |
3651 else | |
3652 return 0; | |
3653 } | |
7 | 3654 |
3655 /* | |
3656 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted. | |
3657 */ | |
3658 if (mch_stat((char *)from, &st) < 0) | |
3659 return -1; | |
3660 | |
1778 | 3661 #ifdef UNIX |
3662 { | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
3663 stat_T st_to; |
1778 | 3664 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3665 // It's possible for the source and destination to be the same file. |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3666 // This happens when "from" and "to" differ in case and are on a FAT32 |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3667 // filesystem. In that case go through a temp file name. |
1778 | 3668 if (mch_stat((char *)to, &st_to) >= 0 |
3669 && st.st_dev == st_to.st_dev | |
3670 && st.st_ino == st_to.st_ino) | |
1779 | 3671 use_tmp_file = TRUE; |
3672 } | |
3673 #endif | |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15840
diff
changeset
|
3674 #ifdef MSWIN |
2793 | 3675 { |
3676 BY_HANDLE_FILE_INFORMATION info1, info2; | |
3677 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3678 // It's possible for the source and destination to be the same file. |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3679 // In that case go through a temp file name. This makes rename("foo", |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3680 // "./foo") a no-op (in a complicated way). |
2793 | 3681 if (win32_fileinfo(from, &info1) == FILEINFO_OK |
3682 && win32_fileinfo(to, &info2) == FILEINFO_OK | |
3683 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber | |
3684 && info1.nFileIndexHigh == info2.nFileIndexHigh | |
3685 && info1.nFileIndexLow == info2.nFileIndexLow) | |
3686 use_tmp_file = TRUE; | |
3687 } | |
3688 #endif | |
1779 | 3689 |
3690 if (use_tmp_file) | |
3691 { | |
3692 char tempname[MAXPATHL + 1]; | |
3693 | |
3694 /* | |
3695 * Find a name that doesn't exist and is in the same directory. | |
3696 * Rename "from" to "tempname" and then rename "tempname" to "to". | |
3697 */ | |
3698 if (STRLEN(from) >= MAXPATHL - 5) | |
3699 return -1; | |
3700 STRCPY(tempname, from); | |
3701 for (n = 123; n < 99999; ++n) | |
3702 { | |
3703 sprintf((char *)gettail((char_u *)tempname), "%d", n); | |
3704 if (mch_stat(tempname, &st) < 0) | |
3705 { | |
3706 if (mch_rename((char *)from, tempname) == 0) | |
1778 | 3707 { |
1779 | 3708 if (mch_rename(tempname, (char *)to) == 0) |
3709 return 0; | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3710 // Strange, the second step failed. Try moving the |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3711 // file back and return failure. |
1779 | 3712 mch_rename(tempname, (char *)from); |
1778 | 3713 return -1; |
3714 } | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3715 // If it fails for one temp name it will most likely fail |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3716 // for any temp name, give up. |
1779 | 3717 return -1; |
3718 } | |
3719 } | |
3720 return -1; | |
1778 | 3721 } |
3722 | |
7 | 3723 /* |
3724 * Delete the "to" file, this is required on some systems to make the | |
3725 * mch_rename() work, on other systems it makes sure that we don't have | |
3726 * two files when the mch_rename() fails. | |
3727 */ | |
3728 | |
3729 #ifdef AMIGA | |
3730 /* | |
3731 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible | |
3732 * that the name of the "to" file is the same as the "from" file, even | |
1201 | 3733 * though the names are different. To avoid the chance of accidentally |
7 | 3734 * deleting the "from" file (horror!) we lock it during the remove. |
3735 * | |
3736 * When used for making a backup before writing the file: This should not | |
3737 * happen with ":w", because startscript() should detect this problem and | |
3738 * set buf->b_shortname, causing modname() to return a correct ".bak" file | |
3739 * name. This problem does exist with ":w filename", but then the | |
3740 * original file will be somewhere else so the backup isn't really | |
3741 * important. If autoscripting is off the rename may fail. | |
3742 */ | |
3743 flock = Lock((UBYTE *)from, (long)ACCESS_READ); | |
3744 #endif | |
3745 mch_remove(to); | |
3746 #ifdef AMIGA | |
3747 if (flock) | |
3748 UnLock(flock); | |
3749 #endif | |
3750 | |
3751 /* | |
3752 * First try a normal rename, return if it works. | |
3753 */ | |
3754 if (mch_rename((char *)from, (char *)to) == 0) | |
3755 return 0; | |
3756 | |
3757 /* | |
3758 * Rename() failed, try copying the file. | |
3759 */ | |
194 | 3760 perm = mch_getperm(from); |
199 | 3761 #ifdef HAVE_ACL |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3762 // For systems that support ACL: get the ACL from the original file. |
199 | 3763 acl = mch_get_acl(from); |
3764 #endif | |
7 | 3765 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0); |
3766 if (fd_in == -1) | |
1651 | 3767 { |
3768 #ifdef HAVE_ACL | |
3769 mch_free_acl(acl); | |
3770 #endif | |
7 | 3771 return -1; |
1651 | 3772 } |
194 | 3773 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3774 // Create the new file with same permissions as the original. |
557 | 3775 fd_out = mch_open((char *)to, |
3776 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm); | |
7 | 3777 if (fd_out == -1) |
3778 { | |
3779 close(fd_in); | |
1651 | 3780 #ifdef HAVE_ACL |
3781 mch_free_acl(acl); | |
3782 #endif | |
7 | 3783 return -1; |
3784 } | |
3785 | |
18199
e2be5a6485f5
patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
18139
diff
changeset
|
3786 buffer = alloc(WRITEBUFSIZE); |
7 | 3787 if (buffer == NULL) |
3788 { | |
1651 | 3789 close(fd_out); |
7 | 3790 close(fd_in); |
1651 | 3791 #ifdef HAVE_ACL |
3792 mch_free_acl(acl); | |
3793 #endif | |
7 | 3794 return -1; |
3795 } | |
3796 | |
18199
e2be5a6485f5
patch 8.1.2094: the fileio.c file is too big
Bram Moolenaar <Bram@vim.org>
parents:
18139
diff
changeset
|
3797 while ((n = read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0) |
2664 | 3798 if (write_eintr(fd_out, buffer, n) != n) |
7 | 3799 { |
3800 errmsg = _("E208: Error writing to \"%s\""); | |
3801 break; | |
3802 } | |
3803 | |
3804 vim_free(buffer); | |
3805 close(fd_in); | |
3806 if (close(fd_out) < 0) | |
3807 errmsg = _("E209: Error closing \"%s\""); | |
3808 if (n < 0) | |
3809 { | |
3810 errmsg = _("E210: Error reading \"%s\""); | |
3811 to = from; | |
3812 } | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3813 #ifndef UNIX // for Unix mch_open() already set the permission |
194 | 3814 mch_setperm(to, perm); |
570 | 3815 #endif |
199 | 3816 #ifdef HAVE_ACL |
3817 mch_set_acl(to, acl); | |
1651 | 3818 mch_free_acl(acl); |
199 | 3819 #endif |
5788 | 3820 #if defined(HAVE_SELINUX) || defined(HAVE_SMACK) |
5483 | 3821 mch_copy_sec(from, to); |
5479 | 3822 #endif |
7 | 3823 if (errmsg != NULL) |
3824 { | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15442
diff
changeset
|
3825 semsg(errmsg, to); |
7 | 3826 return -1; |
3827 } | |
3828 mch_remove(from); | |
3829 return 0; | |
3830 } | |
3831 | |
3832 static int already_warned = FALSE; | |
3833 | |
3834 /* | |
3835 * Check if any not hidden buffer has been changed. | |
3836 * Postpone the check if there are characters in the stuff buffer, a global | |
3837 * command is being executed, a mapping is being executed or an autocommand is | |
3838 * busy. | |
3839 * Returns TRUE if some message was written (screen should be redrawn and | |
3840 * cursor positioned). | |
3841 */ | |
3842 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3843 check_timestamps( |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3844 int focus) // called for GUI focus event |
7 | 3845 { |
3846 buf_T *buf; | |
3847 int didit = 0; | |
3848 int n; | |
3849 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3850 // Don't check timestamps while system() or another low-level function may |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3851 // cause us to lose and gain focus. |
7 | 3852 if (no_check_timestamps > 0) |
3853 return FALSE; | |
3854 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3855 // Avoid doing a check twice. The OK/Reload dialog can cause a focus |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3856 // event and we would keep on checking if the file is steadily growing. |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3857 // Do check again after typing something. |
7 | 3858 if (focus && did_check_timestamps) |
3859 { | |
3860 need_check_timestamps = TRUE; | |
3861 return FALSE; | |
3862 } | |
3863 | |
3864 if (!stuff_empty() || global_busy || !typebuf_typed() | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
3865 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3866 need_check_timestamps = TRUE; // check later |
7 | 3867 else |
3868 { | |
3869 ++no_wait_return; | |
3870 did_check_timestamps = TRUE; | |
3871 already_warned = FALSE; | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9599
diff
changeset
|
3872 FOR_ALL_BUFFERS(buf) |
7 | 3873 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3874 // Only check buffers in a window. |
7 | 3875 if (buf->b_nwindows > 0) |
3876 { | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
3877 bufref_T bufref; |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
3878 |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
3879 set_bufref(&bufref, buf); |
7 | 3880 n = buf_check_timestamp(buf, focus); |
3881 if (didit < n) | |
3882 didit = n; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
3883 if (n > 0 && !bufref_valid(&bufref)) |
7 | 3884 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3885 // Autocommands have removed the buffer, start at the |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3886 // first one again. |
7 | 3887 buf = firstbuf; |
3888 continue; | |
3889 } | |
3890 } | |
3891 } | |
3892 --no_wait_return; | |
3893 need_check_timestamps = FALSE; | |
3894 if (need_wait_return && didit == 2) | |
3895 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3896 // make sure msg isn't overwritten |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15531
diff
changeset
|
3897 msg_puts("\n"); |
7 | 3898 out_flush(); |
3899 } | |
3900 } | |
3901 return didit; | |
3902 } | |
3903 | |
3904 /* | |
3905 * Move all the lines from buffer "frombuf" to buffer "tobuf". | |
3906 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not | |
3907 * empty. | |
3908 */ | |
3909 static int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3910 move_lines(buf_T *frombuf, buf_T *tobuf) |
7 | 3911 { |
3912 buf_T *tbuf = curbuf; | |
3913 int retval = OK; | |
3914 linenr_T lnum; | |
3915 char_u *p; | |
3916 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3917 // Copy the lines in "frombuf" to "tobuf". |
7 | 3918 curbuf = tobuf; |
3919 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum) | |
3920 { | |
3921 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE)); | |
3922 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL) | |
3923 { | |
3924 vim_free(p); | |
3925 retval = FAIL; | |
3926 break; | |
3927 } | |
3928 vim_free(p); | |
3929 } | |
3930 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3931 // Delete all the lines in "frombuf". |
7 | 3932 if (retval != FAIL) |
3933 { | |
3934 curbuf = frombuf; | |
1055 | 3935 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum) |
3936 if (ml_delete(lnum, FALSE) == FAIL) | |
7 | 3937 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3938 // Oops! We could try putting back the saved lines, but that |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3939 // might fail again... |
7 | 3940 retval = FAIL; |
3941 break; | |
3942 } | |
3943 } | |
3944 | |
3945 curbuf = tbuf; | |
3946 return retval; | |
3947 } | |
3948 | |
3949 /* | |
3950 * Check if buffer "buf" has been changed. | |
3951 * Also check if the file for a new buffer unexpectedly appeared. | |
3952 * return 1 if a changed buffer was found. | |
3953 * return 2 if a message has been displayed. | |
3954 * return 0 otherwise. | |
3955 */ | |
3956 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3957 buf_check_timestamp( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
3958 buf_T *buf, |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3959 int focus UNUSED) // called for GUI focus event |
7 | 3960 { |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
3961 stat_T st; |
7 | 3962 int stat_res; |
3963 int retval = 0; | |
3964 char_u *path; | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15442
diff
changeset
|
3965 char *tbuf; |
7 | 3966 char *mesg = NULL; |
188 | 3967 char *mesg2 = ""; |
7 | 3968 int helpmesg = FALSE; |
3969 int reload = FALSE; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
3970 char *reason; |
7 | 3971 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
3972 int can_reload = FALSE; | |
3973 #endif | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
3974 off_T orig_size = buf->b_orig_size; |
7 | 3975 int orig_mode = buf->b_orig_mode; |
3976 #ifdef FEAT_GUI | |
3977 int save_mouse_correct = need_mouse_correct; | |
3978 #endif | |
3979 static int busy = FALSE; | |
179 | 3980 int n; |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
3981 #ifdef FEAT_EVAL |
179 | 3982 char_u *s; |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
3983 #endif |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
3984 bufref_T bufref; |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
3985 |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
3986 set_bufref(&bufref, buf); |
7 | 3987 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3988 // If there is no file name, the buffer is not loaded, 'buftype' is |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3989 // set, we are in the middle of a save or being called recursively: ignore |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
3990 // this buffer. |
7 | 3991 if (buf->b_ffname == NULL |
3992 || buf->b_ml.ml_mfp == NULL | |
14433
4a94173743d9
patch 8.1.0230: directly checking 'buftype' value
Christian Brabandt <cb@256bit.org>
parents:
14290
diff
changeset
|
3993 || !bt_normal(buf) |
7 | 3994 || buf->b_saving |
3995 || busy | |
33 | 3996 #ifdef FEAT_NETBEANS_INTG |
3997 || isNetbeansBuffer(buf) | |
3998 #endif | |
12064
407a475c67fd
patch 8.0.0912: cannot run a job in a hidden terminal
Christian Brabandt <cb@256bit.org>
parents:
11757
diff
changeset
|
3999 #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
|
4000 || 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
|
4001 #endif |
7 | 4002 ) |
4003 return 0; | |
4004 | |
4005 if ( !(buf->b_flags & BF_NOTEDITED) | |
4006 && buf->b_mtime != 0 | |
4007 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0 | |
4008 || time_differs((long)st.st_mtime, buf->b_mtime) | |
5863 | 4009 || st.st_size != buf->b_orig_size |
7 | 4010 #ifdef HAVE_ST_MODE |
4011 || (int)st.st_mode != buf->b_orig_mode | |
4012 #else | |
4013 || mch_getperm(buf->b_ffname) != buf->b_orig_mode | |
4014 #endif | |
4015 )) | |
4016 { | |
17565
0026ea34a8d5
patch 8.1.1780: warning for file no longer available is repeated
Bram Moolenaar <Bram@vim.org>
parents:
17476
diff
changeset
|
4017 long prev_b_mtime = buf->b_mtime; |
0026ea34a8d5
patch 8.1.1780: warning for file no longer available is repeated
Bram Moolenaar <Bram@vim.org>
parents:
17476
diff
changeset
|
4018 |
7 | 4019 retval = 1; |
4020 | |
14290
977cab3d5474
patch 8.1.0161: buffer not updated with 'autoread' set if file was deleted
Christian Brabandt <cb@256bit.org>
parents:
14157
diff
changeset
|
4021 // set b_mtime to stop further warnings (e.g., when executing |
977cab3d5474
patch 8.1.0161: buffer not updated with 'autoread' set if file was deleted
Christian Brabandt <cb@256bit.org>
parents:
14157
diff
changeset
|
4022 // FileChangedShell autocmd) |
7 | 4023 if (stat_res < 0) |
4024 { | |
16754
8b92f3fea477
patch 8.1.1379: filechanged test hangs
Bram Moolenaar <Bram@vim.org>
parents:
16752
diff
changeset
|
4025 // Check the file again later to see if it re-appears. |
8b92f3fea477
patch 8.1.1379: filechanged test hangs
Bram Moolenaar <Bram@vim.org>
parents:
16752
diff
changeset
|
4026 buf->b_mtime = -1; |
7 | 4027 buf->b_orig_size = 0; |
4028 buf->b_orig_mode = 0; | |
4029 } | |
4030 else | |
4031 buf_store_time(buf, &st, buf->b_ffname); | |
4032 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4033 // Don't do anything for a directory. Might contain the file |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4034 // explorer. |
7 | 4035 if (mch_isdir(buf->b_fname)) |
4036 ; | |
4037 | |
4038 /* | |
4039 * If 'autoread' is set, the buffer has no changes and the file still | |
4040 * exists, reload the buffer. Use the buffer-local option value if it | |
4041 * was set, the global option value otherwise. | |
4042 */ | |
4043 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar) | |
4044 && !bufIsChanged(buf) && stat_res >= 0) | |
4045 reload = TRUE; | |
4046 else | |
4047 { | |
179 | 4048 if (stat_res < 0) |
4049 reason = "deleted"; | |
4050 else if (bufIsChanged(buf)) | |
4051 reason = "conflict"; | |
15634
746b95fd25ad
patch 8.1.0825: code for autocommands is mixed with file I/O code
Bram Moolenaar <Bram@vim.org>
parents:
15607
diff
changeset
|
4052 /* |
746b95fd25ad
patch 8.1.0825: code for autocommands is mixed with file I/O code
Bram Moolenaar <Bram@vim.org>
parents:
15607
diff
changeset
|
4053 * Check if the file contents really changed to avoid giving a |
746b95fd25ad
patch 8.1.0825: code for autocommands is mixed with file I/O code
Bram Moolenaar <Bram@vim.org>
parents:
15607
diff
changeset
|
4054 * warning when only the timestamp was set (e.g., checked out of |
746b95fd25ad
patch 8.1.0825: code for autocommands is mixed with file I/O code
Bram Moolenaar <Bram@vim.org>
parents:
15607
diff
changeset
|
4055 * CVS). Always warn when the buffer was changed. |
746b95fd25ad
patch 8.1.0825: code for autocommands is mixed with file I/O code
Bram Moolenaar <Bram@vim.org>
parents:
15607
diff
changeset
|
4056 */ |
179 | 4057 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf)) |
4058 reason = "changed"; | |
4059 else if (orig_mode != buf->b_orig_mode) | |
4060 reason = "mode"; | |
4061 else | |
4062 reason = "time"; | |
4063 | |
7 | 4064 /* |
4065 * Only give the warning if there are no FileChangedShell | |
4066 * autocommands. | |
4067 * Avoid being called recursively by setting "busy". | |
4068 */ | |
4069 busy = TRUE; | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
4070 #ifdef FEAT_EVAL |
179 | 4071 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1); |
4072 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1); | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
4073 #endif |
1834 | 4074 ++allbuf_lock; |
7 | 4075 n = apply_autocmds(EVENT_FILECHANGEDSHELL, |
4076 buf->b_fname, buf->b_fname, FALSE, buf); | |
1834 | 4077 --allbuf_lock; |
7 | 4078 busy = FALSE; |
4079 if (n) | |
4080 { | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
4081 if (!bufref_valid(&bufref)) |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15442
diff
changeset
|
4082 emsg(_("E246: FileChangedShell autocommand deleted buffer")); |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
4083 #ifdef FEAT_EVAL |
179 | 4084 s = get_vim_var_str(VV_FCS_CHOICE); |
4085 if (STRCMP(s, "reload") == 0 && *reason != 'd') | |
4086 reload = TRUE; | |
4087 else if (STRCMP(s, "ask") == 0) | |
4088 n = FALSE; | |
4089 else | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
4090 #endif |
179 | 4091 return 2; |
7 | 4092 } |
179 | 4093 if (!n) |
7 | 4094 { |
179 | 4095 if (*reason == 'd') |
17565
0026ea34a8d5
patch 8.1.1780: warning for file no longer available is repeated
Bram Moolenaar <Bram@vim.org>
parents:
17476
diff
changeset
|
4096 { |
0026ea34a8d5
patch 8.1.1780: warning for file no longer available is repeated
Bram Moolenaar <Bram@vim.org>
parents:
17476
diff
changeset
|
4097 // Only give the message once. |
0026ea34a8d5
patch 8.1.1780: warning for file no longer available is repeated
Bram Moolenaar <Bram@vim.org>
parents:
17476
diff
changeset
|
4098 if (prev_b_mtime != -1) |
0026ea34a8d5
patch 8.1.1780: warning for file no longer available is repeated
Bram Moolenaar <Bram@vim.org>
parents:
17476
diff
changeset
|
4099 mesg = _("E211: File \"%s\" no longer available"); |
0026ea34a8d5
patch 8.1.1780: warning for file no longer available is repeated
Bram Moolenaar <Bram@vim.org>
parents:
17476
diff
changeset
|
4100 } |
7 | 4101 else |
4102 { | |
4103 helpmesg = TRUE; | |
4104 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
4105 can_reload = TRUE; | |
4106 #endif | |
179 | 4107 if (reason[2] == 'n') |
4108 { | |
7 | 4109 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well"); |
179 | 4110 mesg2 = _("See \":help W12\" for more info."); |
4111 } | |
4112 else if (reason[1] == 'h') | |
4113 { | |
7 | 4114 mesg = _("W11: Warning: File \"%s\" has changed since editing started"); |
179 | 4115 mesg2 = _("See \":help W11\" for more info."); |
4116 } | |
4117 else if (*reason == 'm') | |
4118 { | |
7 | 4119 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started"); |
179 | 4120 mesg2 = _("See \":help W16\" for more info."); |
4121 } | |
1913 | 4122 else |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4123 // Only timestamp changed, store it to avoid a warning |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4124 // in check_mtime() later. |
1913 | 4125 buf->b_mtime_read = buf->b_mtime; |
7 | 4126 } |
4127 } | |
4128 } | |
4129 | |
4130 } | |
4131 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W) | |
4132 && vim_fexists(buf->b_ffname)) | |
4133 { | |
4134 retval = 1; | |
4135 mesg = _("W13: Warning: File \"%s\" has been created after editing started"); | |
4136 buf->b_flags |= BF_NEW_W; | |
4137 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
4138 can_reload = TRUE; | |
4139 #endif | |
4140 } | |
4141 | |
4142 if (mesg != NULL) | |
4143 { | |
4144 path = home_replace_save(buf, buf->b_fname); | |
4145 if (path != NULL) | |
4146 { | |
179 | 4147 if (!helpmesg) |
7 | 4148 mesg2 = ""; |
16825
ce04ebdf26b8
patch 8.1.1414: alloc() returning "char_u *" causes a lot of type casts
Bram Moolenaar <Bram@vim.org>
parents:
16782
diff
changeset
|
4149 tbuf = alloc(STRLEN(path) + STRLEN(mesg) + STRLEN(mesg2) + 2); |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15442
diff
changeset
|
4150 sprintf(tbuf, mesg, path); |
1848 | 4151 #ifdef FEAT_EVAL |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4152 // Set warningmsg here, before the unimportant and output-specific |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4153 // mesg2 has been appended. |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15442
diff
changeset
|
4154 set_vim_var_string(VV_WARNINGMSG, (char_u *)tbuf, -1); |
1848 | 4155 #endif |
7 | 4156 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
4157 if (can_reload) | |
4158 { | |
4159 if (*mesg2 != NUL) | |
4160 { | |
4161 STRCAT(tbuf, "\n"); | |
4162 STRCAT(tbuf, mesg2); | |
4163 } | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15442
diff
changeset
|
4164 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"), |
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15442
diff
changeset
|
4165 (char_u *)tbuf, |
2684 | 4166 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2) |
7 | 4167 reload = TRUE; |
4168 } | |
4169 else | |
4170 #endif | |
4171 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned) | |
4172 { | |
4173 if (*mesg2 != NUL) | |
4174 { | |
4175 STRCAT(tbuf, "; "); | |
4176 STRCAT(tbuf, mesg2); | |
4177 } | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15442
diff
changeset
|
4178 emsg(tbuf); |
7 | 4179 retval = 2; |
4180 } | |
4181 else | |
4182 { | |
4183 if (!autocmd_busy) | |
4184 { | |
4185 msg_start(); | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15531
diff
changeset
|
4186 msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST); |
7 | 4187 if (*mesg2 != NUL) |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15531
diff
changeset
|
4188 msg_puts_attr(mesg2, HL_ATTR(HLF_W) + MSG_HIST); |
7 | 4189 msg_clr_eos(); |
4190 (void)msg_end(); | |
4191 if (emsg_silent == 0) | |
4192 { | |
4193 out_flush(); | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
4194 #ifdef FEAT_GUI |
7 | 4195 if (!focus) |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
4196 #endif |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4197 // give the user some time to think about it |
18642
bbea1f108187
patch 8.1.2313: debugging where a delay comes from is not easy
Bram Moolenaar <Bram@vim.org>
parents:
18619
diff
changeset
|
4198 ui_delay(1004L, TRUE); |
7 | 4199 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4200 // don't redraw and erase the message |
7 | 4201 redraw_cmdline = FALSE; |
4202 } | |
4203 } | |
4204 already_warned = TRUE; | |
4205 } | |
4206 | |
4207 vim_free(path); | |
4208 vim_free(tbuf); | |
4209 } | |
4210 } | |
4211 | |
4212 if (reload) | |
3776 | 4213 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4214 // Reload the buffer. |
628 | 4215 buf_reload(buf, orig_mode); |
3776 | 4216 #ifdef FEAT_PERSISTENT_UNDO |
4217 if (buf->b_p_udf && buf->b_ffname != NULL) | |
4218 { | |
4219 char_u hash[UNDO_HASH_SIZE]; | |
4220 buf_T *save_curbuf = curbuf; | |
4221 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4222 // Any existing undo file is unusable, write it now. |
3776 | 4223 curbuf = buf; |
4224 u_compute_hash(hash); | |
4225 u_write_undo(NULL, FALSE, buf, hash); | |
4226 curbuf = save_curbuf; | |
4227 } | |
4228 #endif | |
4229 } | |
7 | 4230 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4231 // 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
|
4232 if (bufref_valid(&bufref) && retval != 0) |
765 | 4233 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, |
4234 buf->b_fname, buf->b_fname, FALSE, buf); | |
7 | 4235 #ifdef FEAT_GUI |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4236 // restore this in case an autocommand has set it; it would break |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4237 // 'mousefocus' |
7 | 4238 need_mouse_correct = save_mouse_correct; |
4239 #endif | |
4240 | |
4241 return retval; | |
4242 } | |
4243 | |
311 | 4244 /* |
4245 * Reload a buffer that is already loaded. | |
4246 * Used when the file was changed outside of Vim. | |
628 | 4247 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected. |
4248 * buf->b_orig_mode may have been reset already. | |
311 | 4249 */ |
4250 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
4251 buf_reload(buf_T *buf, int orig_mode) |
311 | 4252 { |
4253 exarg_T ea; | |
4254 pos_T old_cursor; | |
4255 linenr_T old_topline; | |
4256 int old_ro = buf->b_p_ro; | |
4257 buf_T *savebuf; | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
4258 bufref_T bufref; |
311 | 4259 int saved = OK; |
4260 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
|
4261 int flags = READ_NEW; |
311 | 4262 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4263 // set curwin/curbuf for "buf" and save some things |
311 | 4264 aucmd_prepbuf(&aco, buf); |
4265 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4266 // We only want to read the text from the file, not reset the syntax |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4267 // highlighting, clear marks, diff status, etc. Force the fileformat |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4268 // and encoding to be the same. |
311 | 4269 if (prep_exarg(&ea, buf) == OK) |
4270 { | |
4271 old_cursor = curwin->w_cursor; | |
4272 old_topline = curwin->w_topline; | |
4273 | |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
4274 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
|
4275 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4276 // Save all the text, so that the reload can be undone. |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4277 // Sync first so that this is a separate undo-able action. |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
4278 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
|
4279 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
|
4280 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
|
4281 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
4282 |
311 | 4283 /* |
4284 * To behave like when a new file is edited (matters for | |
4285 * BufReadPost autocommands) we first need to delete the current | |
4286 * buffer contents. But if reading the file fails we should keep | |
4287 * the old contents. Can't use memory only, the file might be | |
4288 * too big. Use a hidden buffer to move the buffer contents to. | |
4289 */ | |
11121
778c10516955
patch 8.0.0448: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
10952
diff
changeset
|
4290 if (BUFEMPTY() || saved == FAIL) |
311 | 4291 savebuf = NULL; |
4292 else | |
4293 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4294 // Allocate a buffer without putting it in the buffer list. |
311 | 4295 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
|
4296 set_bufref(&bufref, savebuf); |
837 | 4297 if (savebuf != NULL && buf == curbuf) |
311 | 4298 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4299 // Open the memline. |
311 | 4300 curbuf = savebuf; |
4301 curwin->w_buffer = savebuf; | |
625 | 4302 saved = ml_open(curbuf); |
311 | 4303 curbuf = buf; |
4304 curwin->w_buffer = buf; | |
4305 } | |
837 | 4306 if (savebuf == NULL || saved == FAIL || buf != curbuf |
311 | 4307 || move_lines(buf, savebuf) == FAIL) |
4308 { | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15442
diff
changeset
|
4309 semsg(_("E462: Could not prepare for reloading \"%s\""), |
311 | 4310 buf->b_fname); |
4311 saved = FAIL; | |
4312 } | |
4313 } | |
4314 | |
4315 if (saved == OK) | |
4316 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4317 curbuf->b_flags |= BF_CHECK_RO; // check for RO again |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4318 keep_filetype = TRUE; // don't detect 'filetype' |
311 | 4319 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, |
4320 (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
|
4321 (linenr_T)MAXLNUM, &ea, flags) != OK) |
311 | 4322 { |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
4323 #if defined(FEAT_EVAL) |
311 | 4324 if (!aborting()) |
4325 #endif | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15442
diff
changeset
|
4326 semsg(_("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
|
4327 if (savebuf != NULL && bufref_valid(&bufref) && buf == curbuf) |
311 | 4328 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4329 // Put the text back from the save buffer. First |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4330 // 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
|
4331 while (!BUFEMPTY()) |
837 | 4332 if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL) |
311 | 4333 break; |
4334 (void)move_lines(savebuf, buf); | |
4335 } | |
4336 } | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4337 else if (buf == curbuf) // "buf" still valid |
311 | 4338 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4339 // Mark the buffer as unmodified and free undo info. |
16996
d5e1e09a829f
patch 8.1.1498: ":write" increments b:changedtick even though nothing changed
Bram Moolenaar <Bram@vim.org>
parents:
16825
diff
changeset
|
4340 unchanged(buf, TRUE, TRUE); |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
4341 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
|
4342 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
4343 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
|
4344 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
|
4345 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
4346 else |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
4347 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4348 // Mark all undo states as changed. |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
4349 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
|
4350 } |
311 | 4351 } |
4352 } | |
4353 vim_free(ea.cmd); | |
4354 | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9391
diff
changeset
|
4355 if (savebuf != NULL && bufref_valid(&bufref)) |
311 | 4356 wipe_buffer(savebuf, FALSE); |
4357 | |
4358 #ifdef FEAT_DIFF | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4359 // Invalidate diff info if necessary. |
837 | 4360 diff_invalidate(curbuf); |
311 | 4361 #endif |
4362 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4363 // Restore the topline and cursor position and check it (lines may |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4364 // have been removed). |
311 | 4365 if (old_topline > curbuf->b_ml.ml_line_count) |
4366 curwin->w_topline = curbuf->b_ml.ml_line_count; | |
4367 else | |
4368 curwin->w_topline = old_topline; | |
4369 curwin->w_cursor = old_cursor; | |
4370 check_cursor(); | |
4371 update_topline(); | |
4372 keep_filetype = FALSE; | |
4373 #ifdef FEAT_FOLDING | |
4374 { | |
1863 | 4375 win_T *wp; |
4376 tabpage_T *tp; | |
311 | 4377 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4378 // Update folds unless they are defined manually. |
1863 | 4379 FOR_ALL_TAB_WINDOWS(tp, wp) |
311 | 4380 if (wp->w_buffer == curwin->w_buffer |
4381 && !foldmethodIsManual(wp)) | |
4382 foldUpdateAll(wp); | |
4383 } | |
4384 #endif | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4385 // If the mode didn't change and 'readonly' was set, keep the old |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4386 // value; the user probably used the ":view" command. But don't |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4387 // reset it, might have had a read error. |
311 | 4388 if (orig_mode == curbuf->b_orig_mode) |
4389 curbuf->b_p_ro |= old_ro; | |
4071 | 4390 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4391 // Modelines must override settings done by autocommands. |
4071 | 4392 do_modelines(0); |
311 | 4393 } |
4394 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4395 // restore curwin/curbuf and a few other things |
311 | 4396 aucmd_restbuf(&aco); |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4397 // Careful: autocommands may have made "buf" invalid! |
311 | 4398 } |
4399 | |
7 | 4400 void |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
4401 buf_store_time(buf_T *buf, stat_T *st, char_u *fname UNUSED) |
7 | 4402 { |
4403 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
|
4404 buf->b_orig_size = st->st_size; |
7 | 4405 #ifdef HAVE_ST_MODE |
4406 buf->b_orig_mode = (int)st->st_mode; | |
4407 #else | |
4408 buf->b_orig_mode = mch_getperm(fname); | |
4409 #endif | |
4410 } | |
4411 | |
4412 /* | |
4413 * Adjust the line with missing eol, used for the next write. | |
4414 * Used for do_filter(), when the input lines for the filter are deleted. | |
4415 */ | |
4416 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
4417 write_lnum_adjust(linenr_T offset) |
7 | 4418 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4419 if (curbuf->b_no_eol_lnum != 0) // only if there is a missing eol |
2707 | 4420 curbuf->b_no_eol_lnum += offset; |
7 | 4421 } |
4422 | |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4423 #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
|
4424 /* |
16752
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4425 * Core part of "readdir()" function. |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4426 * Retrieve the list of files/directories of "path" into "gap". |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4427 * Return OK for success, FAIL for failure. |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4428 */ |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4429 int |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4430 readdir_core( |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4431 garray_T *gap, |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4432 char_u *path, |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4433 void *context, |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4434 int (*checkitem)(void *context, char_u *name)) |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4435 { |
19526
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4436 int failed = FALSE; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4437 char_u *p; |
16752
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4438 |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4439 ga_init2(gap, (int)sizeof(char *), 20); |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4440 |
19301
80baa37506d0
patch 8.2.0209: function a bit far away from where it's used
Bram Moolenaar <Bram@vim.org>
parents:
18896
diff
changeset
|
4441 # ifdef MSWIN |
16752
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4442 { |
19526
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4443 char_u *buf; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4444 int ok; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4445 HANDLE hFind = INVALID_HANDLE_VALUE; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4446 WIN32_FIND_DATAW wfb; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4447 WCHAR *wn = NULL; // UTF-16 name, NULL when not used. |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4448 |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4449 buf = alloc(MAXPATHL); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4450 if (buf == NULL) |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4451 return FAIL; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4452 STRNCPY(buf, path, MAXPATHL-5); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4453 p = buf + STRLEN(buf); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4454 MB_PTR_BACK(buf, p); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4455 if (*p == '\\' || *p == '/') |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4456 *p = NUL; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4457 STRCAT(buf, "\\*"); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4458 |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4459 wn = enc_to_utf16(buf, NULL); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4460 if (wn != NULL) |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4461 hFind = FindFirstFileW(wn, &wfb); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4462 ok = (hFind != INVALID_HANDLE_VALUE); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4463 if (!ok) |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4464 { |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4465 failed = TRUE; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4466 smsg(_(e_notopen), path); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4467 } |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4468 else |
16752
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4469 { |
19526
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4470 while (ok) |
16752
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4471 { |
19526
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4472 int ignore; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4473 |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4474 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4475 if (p == NULL) |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4476 break; // out of memory |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4477 |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4478 ignore = p[0] == '.' && (p[1] == NUL |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4479 || (p[1] == '.' && p[2] == NUL)); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4480 if (!ignore && checkitem != NULL) |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4481 { |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4482 int r = checkitem(context, p); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4483 |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4484 if (r < 0) |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4485 { |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4486 vim_free(p); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4487 break; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4488 } |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4489 if (r == 0) |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4490 ignore = TRUE; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4491 } |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4492 |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4493 if (!ignore) |
16752
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4494 { |
19526
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4495 if (ga_grow(gap, 1) == OK) |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4496 ((char_u**)gap->ga_data)[gap->ga_len++] = vim_strsave(p); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4497 else |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4498 { |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4499 failed = TRUE; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4500 vim_free(p); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4501 break; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4502 } |
16752
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4503 } |
19526
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4504 |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4505 vim_free(p); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4506 ok = FindNextFileW(hFind, &wfb); |
16752
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4507 } |
19526
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4508 FindClose(hFind); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4509 } |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4510 |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4511 vim_free(buf); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4512 vim_free(wn); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4513 } |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4514 # else |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4515 { |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4516 DIR *dirp; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4517 struct dirent *dp; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4518 |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4519 dirp = opendir((char *)path); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4520 if (dirp == NULL) |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4521 { |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4522 failed = TRUE; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4523 smsg(_(e_notopen), path); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4524 } |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4525 else |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4526 { |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4527 for (;;) |
16752
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4528 { |
19526
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4529 int ignore; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4530 |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4531 dp = readdir(dirp); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4532 if (dp == NULL) |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4533 break; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4534 p = (char_u *)dp->d_name; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4535 |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4536 ignore = p[0] == '.' && |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4537 (p[1] == NUL || |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4538 (p[1] == '.' && p[2] == NUL)); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4539 if (!ignore && checkitem != NULL) |
16752
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4540 { |
19526
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4541 int r = checkitem(context, p); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4542 |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4543 if (r < 0) |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4544 break; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4545 if (r == 0) |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4546 ignore = TRUE; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4547 } |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4548 |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4549 if (!ignore) |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4550 { |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4551 if (ga_grow(gap, 1) == OK) |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4552 ((char_u**)gap->ga_data)[gap->ga_len++] = vim_strsave(p); |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4553 else |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4554 { |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4555 failed = TRUE; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4556 break; |
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4557 } |
16752
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4558 } |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4559 } |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4560 |
19526
22f0dda71638
patch 8.2.0320: no Haiku support
Bram Moolenaar <Bram@vim.org>
parents:
19477
diff
changeset
|
4561 closedir(dirp); |
16752
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4562 } |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4563 } |
19301
80baa37506d0
patch 8.2.0209: function a bit far away from where it's used
Bram Moolenaar <Bram@vim.org>
parents:
18896
diff
changeset
|
4564 # endif |
16752
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4565 |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4566 if (!failed && gap->ga_len > 0) |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4567 sort_strings((char_u **)gap->ga_data, gap->ga_len); |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4568 |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4569 return failed ? FAIL : OK; |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4570 } |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4571 |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4572 /* |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4573 * Delete "name" and everything in it, recursively. |
16752
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4574 * return 0 for success, -1 if some file was not deleted. |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4575 */ |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4576 int |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4577 delete_recursive(char_u *name) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4578 { |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4579 int result = 0; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4580 int i; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4581 char_u *exp; |
16752
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4582 garray_T ga; |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4583 |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4584 // A symbolic link to a directory itself is deleted, not the directory it |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4585 // points to. |
7629
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
4586 if ( |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15840
diff
changeset
|
4587 # if defined(UNIX) || defined(MSWIN) |
7657
9c5e8254ea6b
commit https://github.com/vim/vim/commit/203258c3ad2966cc9d08b3805b103333988b30b7
Christian Brabandt <cb@256bit.org>
parents:
7641
diff
changeset
|
4588 mch_isrealdir(name) |
7629
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
4589 # else |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
4590 mch_isdir(name) |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
4591 # endif |
befbed72da87
commit https://github.com/vim/vim/commit/43a34f9f74fdce462fa250baab620264c28b6165
Christian Brabandt <cb@256bit.org>
parents:
7615
diff
changeset
|
4592 ) |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4593 { |
16752
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4594 exp = vim_strsave(name); |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4595 if (exp == NULL) |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4596 return -1; |
16752
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4597 if (readdir_core(&ga, exp, NULL, NULL) == OK) |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4598 { |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4599 for (i = 0; i < ga.ga_len; ++i) |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4600 { |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4601 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/%s", exp, |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4602 ((char_u **)ga.ga_data)[i]); |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4603 if (delete_recursive(NameBuff) != 0) |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4604 result = -1; |
16752
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4605 } |
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4606 ga_clear_strings(&ga); |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4607 } |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4608 else |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4609 result = -1; |
16752
e2d8d83e6721
patch 8.1.1378: delete() can not handle a file name that looks like a pattern
Bram Moolenaar <Bram@vim.org>
parents:
16744
diff
changeset
|
4610 (void)mch_rmdir(exp); |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4611 vim_free(exp); |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4612 } |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4613 else |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4614 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
|
4615 |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4616 return result; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4617 } |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4618 #endif |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4619 |
7 | 4620 #if defined(TEMPDIRNAMES) || defined(PROTO) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4621 static long temp_count = 0; // Temp filename counter. |
7 | 4622 |
4623 /* | |
4624 * Delete the temp directory and all files it contains. | |
4625 */ | |
4626 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
4627 vim_deltempdir(void) |
7 | 4628 { |
4629 if (vim_tempdir != NULL) | |
4630 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4631 // remove the trailing path separator |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4632 gettail(vim_tempdir)[-1] = NUL; |
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4633 delete_recursive(vim_tempdir); |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
4634 VIM_CLEAR(vim_tempdir); |
7 | 4635 } |
4636 } | |
7615
228ff048db20
commit https://github.com/vim/vim/commit/da440d21a6b94d7f525fa7be9b1417c78dd9aa4c
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
4637 |
7 | 4638 /* |
1997 | 4639 * Directory "tempdir" was created. Expand this name to a full path and put |
4640 * it in "vim_tempdir". This avoids that using ":cd" would confuse us. | |
4641 * "tempdir" must be no longer than MAXPATHL. | |
4642 */ | |
4643 static void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
4644 vim_settempdir(char_u *tempdir) |
1997 | 4645 { |
4646 char_u *buf; | |
4647 | |
16764
ef00b6bc186b
patch 8.1.1384: using "int" for alloc() often results in compiler warnings
Bram Moolenaar <Bram@vim.org>
parents:
16754
diff
changeset
|
4648 buf = alloc(MAXPATHL + 2); |
1997 | 4649 if (buf != NULL) |
4650 { | |
4651 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL) | |
4652 STRCPY(buf, tempdir); | |
9536
b2aada04d84e
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Christian Brabandt <cb@256bit.org>
parents:
9509
diff
changeset
|
4653 add_pathsep(buf); |
1997 | 4654 vim_tempdir = vim_strsave(buf); |
4655 vim_free(buf); | |
4656 } | |
4657 } | |
2006 | 4658 #endif |
1997 | 4659 |
4660 /* | |
7 | 4661 * vim_tempname(): Return a unique name that can be used for a temp file. |
4662 * | |
9291
bff3cd5cf922
commit https://github.com/vim/vim/commit/76ae22fef3cb224ca7fbf97517f881e825d4d0c2
Christian Brabandt <cb@256bit.org>
parents:
9260
diff
changeset
|
4663 * 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
|
4664 * guaranteed to NOT be created. |
7 | 4665 * |
4666 * The returned pointer is to allocated memory. | |
4667 * The returned pointer is NULL if no valid name was found. | |
4668 */ | |
4669 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
4670 vim_tempname( |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4671 int extra_char UNUSED, // char to use in the name instead of '?' |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
4672 int keep UNUSED) |
7 | 4673 { |
4674 #ifdef USE_TMPNAM | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4675 char_u itmp[L_tmpnam]; // use tmpnam() |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15840
diff
changeset
|
4676 #elif defined(MSWIN) |
15776
7f91de3f5e35
patch 8.1.0895: MS-Windows: dealing with temp name encoding not quite right
Bram Moolenaar <Bram@vim.org>
parents:
15750
diff
changeset
|
4677 WCHAR itmp[TEMPNAMELEN]; |
7 | 4678 #else |
4679 char_u itmp[TEMPNAMELEN]; | |
4680 #endif | |
4681 | |
4682 #ifdef TEMPDIRNAMES | |
4683 static char *(tempdirs[]) = {TEMPDIRNAMES}; | |
4684 int i; | |
4685 # ifndef EEXIST | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9291
diff
changeset
|
4686 stat_T st; |
7 | 4687 # endif |
4688 | |
4689 /* | |
4690 * This will create a directory for private use by this instance of Vim. | |
4691 * This is done once, and the same directory is used for all temp files. | |
4692 * This method avoids security problems because of symlink attacks et al. | |
4693 * It's also a bit faster, because we only need to check for an existing | |
4694 * file when creating the directory and not for each temp file. | |
4695 */ | |
4696 if (vim_tempdir == NULL) | |
4697 { | |
4698 /* | |
4699 * Try the entries in TEMPDIRNAMES to create the temp directory. | |
4700 */ | |
1877 | 4701 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i) |
7 | 4702 { |
2047
85da03763130
updated for version 7.2.333
Bram Moolenaar <bram@zimbu.org>
parents:
2028
diff
changeset
|
4703 # ifndef HAVE_MKDTEMP |
1997 | 4704 size_t itmplen; |
4705 long nr; | |
4706 long off; | |
4707 # endif | |
4708 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4709 // Expand $TMP, leave room for "/v1100000/999999999". |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4710 // Skip the directory check if the expansion fails. |
7 | 4711 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
|
4712 if (itmp[0] != '$' && mch_isdir(itmp)) |
372c785c04b6
commit https://github.com/vim/vim/commit/e1a61991d9b6fd5f65636d17583f93118268cda5
Christian Brabandt <cb@256bit.org>
parents:
7285
diff
changeset
|
4713 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4714 // directory exists |
9536
b2aada04d84e
commit https://github.com/vim/vim/commit/a06ecab7a5159e744448ace731036f0dc5f87dd4
Christian Brabandt <cb@256bit.org>
parents:
9509
diff
changeset
|
4715 add_pathsep(itmp); |
1997 | 4716 |
4717 # ifdef HAVE_MKDTEMP | |
9211
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
4718 { |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
4719 # if defined(UNIX) || defined(VMS) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4720 // Make sure the umask doesn't remove the executable bit. |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4721 // "repl" has been reported to use "177". |
9211
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
4722 mode_t umask_save = umask(077); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
4723 # endif |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4724 // Leave room for filename |
9211
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
4725 STRCAT(itmp, "vXXXXXX"); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
4726 if (mkdtemp((char *)itmp) != NULL) |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
4727 vim_settempdir(itmp); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
4728 # if defined(UNIX) || defined(VMS) |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
4729 (void)umask(umask_save); |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
4730 # endif |
d2b9e454c73d
commit https://github.com/vim/vim/commit/35d88f4e2ff5dcd9904f04612d5febede996137c
Christian Brabandt <cb@256bit.org>
parents:
9068
diff
changeset
|
4731 } |
1997 | 4732 # else |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4733 // Get an arbitrary number of up to 6 digits. When it's |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4734 // unlikely that it already exists it will be faster, |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4735 // otherwise it doesn't matter. The use of mkdir() avoids any |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4736 // security problems because of the predictable number. |
7 | 4737 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
|
4738 itmplen = STRLEN(itmp); |
7 | 4739 |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4740 // Try up to 10000 different values until we find a name that |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4741 // doesn't exist. |
7 | 4742 for (off = 0; off < 10000L; ++off) |
4743 { | |
4744 int r; | |
1997 | 4745 # if defined(UNIX) || defined(VMS) |
7 | 4746 mode_t umask_save; |
1997 | 4747 # endif |
4748 | |
4749 sprintf((char *)itmp + itmplen, "v%ld", nr + off); | |
4750 # ifndef EEXIST | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4751 // If mkdir() does not set errno to EEXIST, check for |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4752 // existing file here. There is a race condition then, |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4753 // although it's fail-safe. |
7 | 4754 if (mch_stat((char *)itmp, &st) >= 0) |
4755 continue; | |
1997 | 4756 # endif |
4757 # if defined(UNIX) || defined(VMS) | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4758 // Make sure the umask doesn't remove the executable bit. |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4759 // "repl" has been reported to use "177". |
7 | 4760 umask_save = umask(077); |
1997 | 4761 # endif |
7 | 4762 r = vim_mkdir(itmp, 0700); |
1997 | 4763 # if defined(UNIX) || defined(VMS) |
7 | 4764 (void)umask(umask_save); |
1997 | 4765 # endif |
7 | 4766 if (r == 0) |
4767 { | |
1997 | 4768 vim_settempdir(itmp); |
7 | 4769 break; |
4770 } | |
1997 | 4771 # ifdef EEXIST |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4772 // If the mkdir() didn't fail because the file/dir exists, |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4773 // we probably can't create any dir here, try another |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4774 // place. |
7 | 4775 if (errno != EEXIST) |
1997 | 4776 # endif |
7 | 4777 break; |
4778 } | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4779 # endif // HAVE_MKDTEMP |
7 | 4780 if (vim_tempdir != NULL) |
4781 break; | |
4782 } | |
4783 } | |
4784 } | |
4785 | |
4786 if (vim_tempdir != NULL) | |
4787 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4788 // There is no need to check if the file exists, because we own the |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4789 // directory and nobody else creates a file in it. |
7 | 4790 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++); |
4791 return vim_strsave(itmp); | |
4792 } | |
4793 | |
4794 return NULL; | |
4795 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4796 #else // TEMPDIRNAMES |
7 | 4797 |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15840
diff
changeset
|
4798 # ifdef MSWIN |
15776
7f91de3f5e35
patch 8.1.0895: MS-Windows: dealing with temp name encoding not quite right
Bram Moolenaar <Bram@vim.org>
parents:
15750
diff
changeset
|
4799 WCHAR wszTempFile[_MAX_PATH + 1]; |
7f91de3f5e35
patch 8.1.0895: MS-Windows: dealing with temp name encoding not quite right
Bram Moolenaar <Bram@vim.org>
parents:
15750
diff
changeset
|
4800 WCHAR buf4[4]; |
7 | 4801 char_u *retval; |
4802 char_u *p; | |
4803 | |
15776
7f91de3f5e35
patch 8.1.0895: MS-Windows: dealing with temp name encoding not quite right
Bram Moolenaar <Bram@vim.org>
parents:
15750
diff
changeset
|
4804 wcscpy(itmp, L""); |
7f91de3f5e35
patch 8.1.0895: MS-Windows: dealing with temp name encoding not quite right
Bram Moolenaar <Bram@vim.org>
parents:
15750
diff
changeset
|
4805 if (GetTempPathW(_MAX_PATH, wszTempFile) == 0) |
7f91de3f5e35
patch 8.1.0895: MS-Windows: dealing with temp name encoding not quite right
Bram Moolenaar <Bram@vim.org>
parents:
15750
diff
changeset
|
4806 { |
7f91de3f5e35
patch 8.1.0895: MS-Windows: dealing with temp name encoding not quite right
Bram Moolenaar <Bram@vim.org>
parents:
15750
diff
changeset
|
4807 wszTempFile[0] = L'.'; // GetTempPathW() failed, use current dir |
7f91de3f5e35
patch 8.1.0895: MS-Windows: dealing with temp name encoding not quite right
Bram Moolenaar <Bram@vim.org>
parents:
15750
diff
changeset
|
4808 wszTempFile[1] = NUL; |
7f91de3f5e35
patch 8.1.0895: MS-Windows: dealing with temp name encoding not quite right
Bram Moolenaar <Bram@vim.org>
parents:
15750
diff
changeset
|
4809 } |
7f91de3f5e35
patch 8.1.0895: MS-Windows: dealing with temp name encoding not quite right
Bram Moolenaar <Bram@vim.org>
parents:
15750
diff
changeset
|
4810 wcscpy(buf4, L"VIM"); |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4811 buf4[2] = extra_char; // make it "VIa", "VIb", etc. |
15776
7f91de3f5e35
patch 8.1.0895: MS-Windows: dealing with temp name encoding not quite right
Bram Moolenaar <Bram@vim.org>
parents:
15750
diff
changeset
|
4812 if (GetTempFileNameW(wszTempFile, buf4, 0, itmp) == 0) |
7 | 4813 return NULL; |
6721 | 4814 if (!keep) |
15776
7f91de3f5e35
patch 8.1.0895: MS-Windows: dealing with temp name encoding not quite right
Bram Moolenaar <Bram@vim.org>
parents:
15750
diff
changeset
|
4815 // GetTempFileName() will create the file, we don't want that |
7f91de3f5e35
patch 8.1.0895: MS-Windows: dealing with temp name encoding not quite right
Bram Moolenaar <Bram@vim.org>
parents:
15750
diff
changeset
|
4816 (void)DeleteFileW(itmp); |
7f91de3f5e35
patch 8.1.0895: MS-Windows: dealing with temp name encoding not quite right
Bram Moolenaar <Bram@vim.org>
parents:
15750
diff
changeset
|
4817 |
7f91de3f5e35
patch 8.1.0895: MS-Windows: dealing with temp name encoding not quite right
Bram Moolenaar <Bram@vim.org>
parents:
15750
diff
changeset
|
4818 // Backslashes in a temp file name cause problems when filtering with |
7f91de3f5e35
patch 8.1.0895: MS-Windows: dealing with temp name encoding not quite right
Bram Moolenaar <Bram@vim.org>
parents:
15750
diff
changeset
|
4819 // "sh". NOTE: This also checks 'shellcmdflag' to help those people who |
7f91de3f5e35
patch 8.1.0895: MS-Windows: dealing with temp name encoding not quite right
Bram Moolenaar <Bram@vim.org>
parents:
15750
diff
changeset
|
4820 // didn't set 'shellslash'. |
7f91de3f5e35
patch 8.1.0895: MS-Windows: dealing with temp name encoding not quite right
Bram Moolenaar <Bram@vim.org>
parents:
15750
diff
changeset
|
4821 retval = utf16_to_enc(itmp, NULL); |
7 | 4822 if (*p_shcf == '-' || p_ssl) |
4823 for (p = retval; *p; ++p) | |
4824 if (*p == '\\') | |
4825 *p = '/'; | |
4826 return retval; | |
4827 | |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15840
diff
changeset
|
4828 # else // MSWIN |
7 | 4829 |
4830 # ifdef USE_TMPNAM | |
2697 | 4831 char_u *p; |
4832 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4833 // tmpnam() will make its own name |
2697 | 4834 p = tmpnam((char *)itmp); |
4835 if (p == NULL || *p == NUL) | |
7 | 4836 return NULL; |
4837 # else | |
4838 char_u *p; | |
4839 | |
4840 # ifdef VMS_TEMPNAM | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4841 // mktemp() is not working on VMS. It seems to be |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4842 // a do-nothing function. Therefore we use tempnam(). |
7 | 4843 sprintf((char *)itmp, "VIM%c", extra_char); |
4844 p = (char_u *)tempnam("tmp:", (char *)itmp); | |
4845 if (p != NULL) | |
4846 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4847 // VMS will use '.LIS' if we don't explicitly specify an extension, |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4848 // and VIM will then be unable to find the file later |
7 | 4849 STRCPY(itmp, p); |
4850 STRCAT(itmp, ".txt"); | |
4851 free(p); | |
4852 } | |
4853 else | |
4854 return NULL; | |
4855 # else | |
4856 STRCPY(itmp, TEMPNAME); | |
4857 if ((p = vim_strchr(itmp, '?')) != NULL) | |
4858 *p = extra_char; | |
4859 if (mktemp((char *)itmp) == NULL) | |
4860 return NULL; | |
4861 # endif | |
4862 # endif | |
4863 | |
4864 return vim_strsave(itmp); | |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15840
diff
changeset
|
4865 # endif // MSWIN |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4866 #endif // TEMPDIRNAMES |
7 | 4867 } |
4868 | |
4869 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) | |
4870 /* | |
7170
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
4871 * 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
|
4872 * it looks like a URL. |
7 | 4873 */ |
4874 void | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
4875 forward_slash(char_u *fname) |
7 | 4876 { |
4877 char_u *p; | |
4878 | |
7170
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
4879 if (path_with_url(fname)) |
beb67ef38f88
commit https://github.com/vim/vim/commit/b4f6a46b01ed00b642a2271e9d1559e51ab0f2c4
Christian Brabandt <cb@256bit.org>
parents:
7005
diff
changeset
|
4880 return; |
7 | 4881 for (p = fname; *p != NUL; ++p) |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4882 // The Big5 encoding can have '\' in the trail byte. |
474 | 4883 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 4884 ++p; |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
4885 else if (*p == '\\') |
7 | 4886 *p = '/'; |
4887 } | |
4888 #endif | |
4889 | |
4890 /* | |
153 | 4891 * Try matching a filename with a "pattern" ("prog" is NULL), or use the |
4892 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling | |
4893 * vim_regcomp() often. | |
7 | 4894 * Used for autocommands and 'wildignore'. |
4895 * Returns TRUE if there is a match, FALSE otherwise. | |
4896 */ | |
15634
746b95fd25ad
patch 8.1.0825: code for autocommands is mixed with file I/O code
Bram Moolenaar <Bram@vim.org>
parents:
15607
diff
changeset
|
4897 int |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
4898 match_file_pat( |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4899 char_u *pattern, // pattern to match with |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4900 regprog_T **prog, // pre-compiled regprog or NULL |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4901 char_u *fname, // full path of file name |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4902 char_u *sfname, // short file name or NULL |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4903 char_u *tail, // tail of path |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4904 int allow_dirs) // allow matching with dir |
7 | 4905 { |
4906 regmatch_T regmatch; | |
4907 int result = FALSE; | |
4908 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4909 regmatch.rm_ic = p_fic; // ignore case if 'fileignorecase' is set |
6470 | 4910 if (prog != NULL) |
4911 regmatch.regprog = *prog; | |
7 | 4912 else |
6470 | 4913 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC); |
7 | 4914 |
4915 /* | |
4916 * Try for a match with the pattern with: | |
4917 * 1. the full file name, when the pattern has a '/'. | |
4918 * 2. the short file name, when the pattern has a '/'. | |
4919 * 3. the tail of the file name, when the pattern has no '/'. | |
4920 */ | |
6470 | 4921 if (regmatch.regprog != NULL |
7 | 4922 && ((allow_dirs |
4923 && (vim_regexec(®match, fname, (colnr_T)0) | |
4924 || (sfname != NULL | |
4925 && vim_regexec(®match, sfname, (colnr_T)0)))) | |
6470 | 4926 || (!allow_dirs && vim_regexec(®match, tail, (colnr_T)0)))) |
7 | 4927 result = TRUE; |
4928 | |
6375 | 4929 if (prog != NULL) |
4930 *prog = regmatch.regprog; | |
4931 else | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4354
diff
changeset
|
4932 vim_regfree(regmatch.regprog); |
7 | 4933 return result; |
4934 } | |
4935 | |
4936 #if defined(FEAT_WILDIGN) || defined(PROTO) | |
4937 /* | |
4938 * Return TRUE if a file matches with a pattern in "list". | |
4939 * "list" is a comma-separated list of patterns, like 'wildignore'. | |
4940 * "sfname" is the short file name or NULL, "ffname" the long file name. | |
4941 */ | |
4942 int | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
4943 match_file_list(char_u *list, char_u *sfname, char_u *ffname) |
7 | 4944 { |
4945 char_u buf[100]; | |
4946 char_u *tail; | |
4947 char_u *regpat; | |
4948 char allow_dirs; | |
4949 int match; | |
4950 char_u *p; | |
4951 | |
4952 tail = gettail(sfname); | |
4953 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4954 // try all patterns in 'wildignore' |
7 | 4955 p = list; |
4956 while (*p) | |
4957 { | |
4958 copy_option_part(&p, buf, 100, ","); | |
4959 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE); | |
4960 if (regpat == NULL) | |
4961 break; | |
153 | 4962 match = match_file_pat(regpat, NULL, ffname, sfname, |
4963 tail, (int)allow_dirs); | |
7 | 4964 vim_free(regpat); |
4965 if (match) | |
4966 return TRUE; | |
4967 } | |
4968 return FALSE; | |
4969 } | |
4970 #endif | |
4971 | |
4972 /* | |
4973 * Convert the given pattern "pat" which has shell style wildcards in it, into | |
4974 * a regular expression, and return the result in allocated memory. If there | |
4975 * is a directory path separator to be matched, then TRUE is put in | |
4976 * allow_dirs, otherwise FALSE is put there -- webb. | |
4977 * Handle backslashes before special characters, like "\*" and "\ ". | |
4978 * | |
4979 * Returns NULL when out of memory. | |
4980 */ | |
4981 char_u * | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
4982 file_pat_to_reg_pat( |
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
4983 char_u *pat, |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4984 char_u *pat_end, // first char after pattern or NULL |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4985 char *allow_dirs, // Result passed back out in here |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4986 int no_bslash UNUSED) // Don't use a backward slash as pathsep |
7 | 4987 { |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
4988 int size = 2; // '^' at start, '$' at end |
7 | 4989 char_u *endp; |
4990 char_u *reg_pat; | |
4991 char_u *p; | |
4992 int i; | |
4993 int nested = 0; | |
4994 int add_dollar = TRUE; | |
4995 | |
4996 if (allow_dirs != NULL) | |
4997 *allow_dirs = FALSE; | |
4998 if (pat_end == NULL) | |
4999 pat_end = pat + STRLEN(pat); | |
5000 | |
5001 for (p = pat; p < pat_end; p++) | |
5002 { | |
5003 switch (*p) | |
5004 { | |
5005 case '*': | |
5006 case '.': | |
5007 case ',': | |
5008 case '{': | |
5009 case '}': | |
5010 case '~': | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
5011 size += 2; // extra backslash |
7 | 5012 break; |
5013 #ifdef BACKSLASH_IN_FILENAME | |
5014 case '\\': | |
5015 case '/': | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
5016 size += 4; // could become "[\/]" |
7 | 5017 break; |
5018 #endif | |
5019 default: | |
5020 size++; | |
474 | 5021 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 5022 { |
5023 ++p; | |
5024 ++size; | |
5025 } | |
5026 break; | |
5027 } | |
5028 } | |
5029 reg_pat = alloc(size + 1); | |
5030 if (reg_pat == NULL) | |
5031 return NULL; | |
5032 | |
5033 i = 0; | |
5034 | |
5035 if (pat[0] == '*') | |
5036 while (pat[0] == '*' && pat < pat_end - 1) | |
5037 pat++; | |
5038 else | |
5039 reg_pat[i++] = '^'; | |
5040 endp = pat_end - 1; | |
7005 | 5041 if (endp >= pat && *endp == '*') |
7 | 5042 { |
5043 while (endp - pat > 0 && *endp == '*') | |
5044 endp--; | |
5045 add_dollar = FALSE; | |
5046 } | |
5047 for (p = pat; *p && nested >= 0 && p <= endp; p++) | |
5048 { | |
5049 switch (*p) | |
5050 { | |
5051 case '*': | |
5052 reg_pat[i++] = '.'; | |
5053 reg_pat[i++] = '*'; | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
5054 while (p[1] == '*') // "**" matches like "*" |
444 | 5055 ++p; |
7 | 5056 break; |
5057 case '.': | |
5058 case '~': | |
5059 reg_pat[i++] = '\\'; | |
5060 reg_pat[i++] = *p; | |
5061 break; | |
5062 case '?': | |
5063 reg_pat[i++] = '.'; | |
5064 break; | |
5065 case '\\': | |
5066 if (p[1] == NUL) | |
5067 break; | |
5068 #ifdef BACKSLASH_IN_FILENAME | |
5069 if (!no_bslash) | |
5070 { | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
5071 // translate: |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
5072 // "\x" to "\\x" e.g., "dir\file" |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
5073 // "\*" to "\\.*" e.g., "dir\*.c" |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
5074 // "\?" to "\\." e.g., "dir\??.c" |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
5075 // "\+" to "\+" e.g., "fileX\+.c" |
7 | 5076 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?') |
5077 && p[1] != '+') | |
5078 { | |
5079 reg_pat[i++] = '['; | |
5080 reg_pat[i++] = '\\'; | |
5081 reg_pat[i++] = '/'; | |
5082 reg_pat[i++] = ']'; | |
5083 if (allow_dirs != NULL) | |
5084 *allow_dirs = TRUE; | |
5085 break; | |
5086 } | |
5087 } | |
5088 #endif | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
5089 // Undo escaping from ExpandEscape(): |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
5090 // foo\?bar -> foo?bar |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
5091 // foo\%bar -> foo%bar |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
5092 // foo\,bar -> foo,bar |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
5093 // foo\ bar -> foo bar |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
5094 // Don't unescape \, * and others that are also special in a |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
5095 // regexp. |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
5096 // An escaped { must be unescaped since we use magic not |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
5097 // verymagic. Use "\\\{n,m\}"" to get "\{n,m}". |
7 | 5098 if (*++p == '?' |
5099 #ifdef BACKSLASH_IN_FILENAME | |
5100 && no_bslash | |
5101 #endif | |
5102 ) | |
5103 reg_pat[i++] = '?'; | |
5104 else | |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
5008
diff
changeset
|
5105 if (*p == ',' || *p == '%' || *p == '#' |
6999 | 5106 || vim_isspace(*p) || *p == '{' || *p == '}') |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
5107 reg_pat[i++] = *p; |
5259
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
5108 else if (*p == '\\' && p[1] == '\\' && p[2] == '{') |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
5109 { |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
5110 reg_pat[i++] = '\\'; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
5111 reg_pat[i++] = '{'; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
5112 p += 2; |
6b7ab6a4f31a
updated for version 7.4b.006
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
5113 } |
7 | 5114 else |
5115 { | |
5116 if (allow_dirs != NULL && vim_ispathsep(*p) | |
5117 #ifdef BACKSLASH_IN_FILENAME | |
5118 && (!no_bslash || *p != '\\') | |
5119 #endif | |
5120 ) | |
5121 *allow_dirs = TRUE; | |
5122 reg_pat[i++] = '\\'; | |
5123 reg_pat[i++] = *p; | |
5124 } | |
5125 break; | |
5126 #ifdef BACKSLASH_IN_FILENAME | |
5127 case '/': | |
5128 reg_pat[i++] = '['; | |
5129 reg_pat[i++] = '\\'; | |
5130 reg_pat[i++] = '/'; | |
5131 reg_pat[i++] = ']'; | |
5132 if (allow_dirs != NULL) | |
5133 *allow_dirs = TRUE; | |
5134 break; | |
5135 #endif | |
5136 case '{': | |
5137 reg_pat[i++] = '\\'; | |
5138 reg_pat[i++] = '('; | |
5139 nested++; | |
5140 break; | |
5141 case '}': | |
5142 reg_pat[i++] = '\\'; | |
5143 reg_pat[i++] = ')'; | |
5144 --nested; | |
5145 break; | |
5146 case ',': | |
5147 if (nested) | |
5148 { | |
5149 reg_pat[i++] = '\\'; | |
5150 reg_pat[i++] = '|'; | |
5151 } | |
5152 else | |
5153 reg_pat[i++] = ','; | |
5154 break; | |
5155 default: | |
474 | 5156 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 5157 reg_pat[i++] = *p++; |
15595
1ec942f1b648
patch 8.1.0805: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
5158 else if (allow_dirs != NULL && vim_ispathsep(*p)) |
7 | 5159 *allow_dirs = TRUE; |
5160 reg_pat[i++] = *p; | |
5161 break; | |
5162 } | |
5163 } | |
5164 if (add_dollar) | |
5165 reg_pat[i++] = '$'; | |
5166 reg_pat[i] = NUL; | |
5167 if (nested != 0) | |
5168 { | |
5169 if (nested < 0) | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15442
diff
changeset
|
5170 emsg(_("E219: Missing {.")); |
7 | 5171 else |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15442
diff
changeset
|
5172 emsg(_("E220: Missing }.")); |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13240
diff
changeset
|
5173 VIM_CLEAR(reg_pat); |
7 | 5174 } |
5175 return reg_pat; | |
5176 } | |
2664 | 5177 |
5178 #if defined(EINTR) || defined(PROTO) | |
5179 /* | |
5180 * Version of read() that retries when interrupted by EINTR (possibly | |
5181 * by a SIGWINCH). | |
5182 */ | |
5183 long | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5184 read_eintr(int fd, void *buf, size_t bufsize) |
2664 | 5185 { |
5186 long ret; | |
5187 | |
5188 for (;;) | |
5189 { | |
5190 ret = vim_read(fd, buf, bufsize); | |
5191 if (ret >= 0 || errno != EINTR) | |
5192 break; | |
5193 } | |
5194 return ret; | |
5195 } | |
5196 | |
5197 /* | |
5198 * Version of write() that retries when interrupted by EINTR (possibly | |
5199 * by a SIGWINCH). | |
5200 */ | |
5201 long | |
7819
f86adafb28d4
commit https://github.com/vim/vim/commit/78c0b7d43e5048fd71d12816659667834170c76d
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
5202 write_eintr(int fd, void *buf, size_t bufsize) |
2664 | 5203 { |
5204 long ret = 0; | |
5205 long wlen; | |
5206 | |
18779
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
5207 // Repeat the write() so long it didn't fail, other than being interrupted |
8f05b3cf8557
patch 8.1.2379: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18767
diff
changeset
|
5208 // by a signal. |
2664 | 5209 while (ret < (long)bufsize) |
5210 { | |
2666 | 5211 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret); |
2664 | 5212 if (wlen < 0) |
5213 { | |
5214 if (errno != EINTR) | |
5215 break; | |
5216 } | |
5217 else | |
5218 ret += wlen; | |
5219 } | |
5220 return ret; | |
5221 } | |
5222 #endif |