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