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