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