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