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