Mercurial > vim
annotate src/misc1.c @ 17480:91cac682b09a v8.1.1738
patch 8.1.1738: testing lambda with timer is slow
commit https://github.com/vim/vim/commit/9bc4dde45d45df732953491d0f2c3fd3b10a627e
Author: Bram Moolenaar <Bram@vim.org>
Date: Wed Jul 24 13:08:29 2019 +0200
patch 8.1.1738: testing lambda with timer is slow
Problem: Testing lambda with timer is slow.
Solution: Do not test timer accuracy, only that it works. (Daniel Hahler,
closes #4723)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Wed, 24 Jul 2019 13:15:06 +0200 |
parents | f12745505a23 |
children | 04245f071792 |
rev | line source |
---|---|
10042
4aead6a9b7a9
commit https://github.com/vim/vim/commit/edf3f97ae2af024708ebb4ac614227327033ca47
Christian Brabandt <cb@256bit.org>
parents:
10002
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 * misc1.c: functions that didn't seem to fit elsewhere | |
12 */ | |
13 | |
14 #include "vim.h" | |
15 #include "version.h" | |
16 | |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15814
diff
changeset
|
17 #if defined(FEAT_CMDL_COMPL) && defined(MSWIN) |
14133
352c2832d17f
patch 8.1.0084: user name completion does not work on MS-Windows
Christian Brabandt <cb@256bit.org>
parents:
13942
diff
changeset
|
18 # include <lm.h> |
352c2832d17f
patch 8.1.0084: user name completion does not work on MS-Windows
Christian Brabandt <cb@256bit.org>
parents:
13942
diff
changeset
|
19 #endif |
352c2832d17f
patch 8.1.0084: user name completion does not work on MS-Windows
Christian Brabandt <cb@256bit.org>
parents:
13942
diff
changeset
|
20 |
7803
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7641
diff
changeset
|
21 static char_u *vim_version_dir(char_u *vimdir); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7641
diff
changeset
|
22 static char_u *remove_tail(char_u *p, char_u *pend, char_u *name); |
7 | 23 |
15814
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
24 #define URL_SLASH 1 /* path_is_url() has found "://" */ |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
25 #define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */ |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
26 |
3744 | 27 /* All user names (for ~user completion as done by shell). */ |
28 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
29 static garray_T ga_users; | |
30 #endif | |
31 | |
7 | 32 /* |
33 * Count the size (in window cells) of the indent in the current line. | |
34 */ | |
35 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
36 get_indent(void) |
7 | 37 { |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
38 #ifdef FEAT_VARTABS |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
39 return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts, |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
40 curbuf->b_p_vts_array, FALSE); |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
41 #else |
5995 | 42 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE); |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
43 #endif |
7 | 44 } |
45 | |
46 /* | |
47 * Count the size (in window cells) of the indent in line "lnum". | |
48 */ | |
49 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
50 get_indent_lnum(linenr_T lnum) |
7 | 51 { |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
52 #ifdef FEAT_VARTABS |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
53 return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts, |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
54 curbuf->b_p_vts_array, FALSE); |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
55 #else |
5995 | 56 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE); |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
57 #endif |
7 | 58 } |
59 | |
60 #if defined(FEAT_FOLDING) || defined(PROTO) | |
61 /* | |
62 * Count the size (in window cells) of the indent in line "lnum" of buffer | |
63 * "buf". | |
64 */ | |
65 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
66 get_indent_buf(buf_T *buf, linenr_T lnum) |
7 | 67 { |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
68 #ifdef FEAT_VARTABS |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
69 return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE), |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
70 (int)curbuf->b_p_ts, buf->b_p_vts_array, FALSE); |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
71 #else |
5995 | 72 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE); |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
73 #endif |
7 | 74 } |
75 #endif | |
76 | |
77 /* | |
78 * count the size (in window cells) of the indent in line "ptr", with | |
79 * 'tabstop' at "ts" | |
80 */ | |
164 | 81 int |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
82 get_indent_str( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
83 char_u *ptr, |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
84 int ts, |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
85 int list) /* if TRUE, count only screen size for tabs */ |
7 | 86 { |
87 int count = 0; | |
88 | |
89 for ( ; *ptr; ++ptr) | |
90 { | |
5995 | 91 if (*ptr == TAB) |
92 { | |
93 if (!list || lcs_tab1) /* count a tab for what it is worth */ | |
94 count += ts - (count % ts); | |
95 else | |
6174 | 96 /* In list mode, when tab is not set, count screen char width |
97 * for Tab, displays: ^I */ | |
5995 | 98 count += ptr2cells(ptr); |
99 } | |
7 | 100 else if (*ptr == ' ') |
101 ++count; /* count a space for one */ | |
102 else | |
103 break; | |
104 } | |
164 | 105 return count; |
7 | 106 } |
107 | |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
108 #ifdef FEAT_VARTABS |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
109 /* |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
110 * Count the size (in window cells) of the indent in line "ptr", using |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
111 * variable tabstops. |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
112 * if "list" is TRUE, count only screen size for tabs. |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
113 */ |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
114 int |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
115 get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list) |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
116 { |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
117 int count = 0; |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
118 |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
119 for ( ; *ptr; ++ptr) |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
120 { |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
121 if (*ptr == TAB) /* count a tab for what it is worth */ |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
122 { |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
123 if (!list || lcs_tab1) |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
124 count += tabstop_padding(count, ts, vts); |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
125 else |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
126 /* In list mode, when tab is not set, count screen char width |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
127 * for Tab, displays: ^I */ |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
128 count += ptr2cells(ptr); |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
129 } |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
130 else if (*ptr == ' ') |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
131 ++count; /* count a space for one */ |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
132 else |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
133 break; |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
134 } |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
135 return count; |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
136 } |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
137 #endif |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
138 |
7 | 139 /* |
140 * Set the indent of the current line. | |
141 * Leaves the cursor on the first non-blank in the line. | |
142 * Caller must take care of undo. | |
143 * "flags": | |
144 * SIN_CHANGED: call changed_bytes() if the line was changed. | |
145 * SIN_INSERT: insert the indent in front of the line. | |
146 * SIN_UNDO: save line for undo before changing it. | |
147 * Returns TRUE if the line was changed. | |
148 */ | |
149 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
150 set_indent( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
151 int size, /* measured in spaces */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
152 int flags) |
7 | 153 { |
154 char_u *p; | |
155 char_u *newline; | |
156 char_u *oldline; | |
157 char_u *s; | |
158 int todo; | |
1324 | 159 int ind_len; /* measured in characters */ |
7 | 160 int line_len; |
161 int doit = FALSE; | |
1324 | 162 int ind_done = 0; /* measured in spaces */ |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
163 #ifdef FEAT_VARTABS |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
164 int ind_col = 0; |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
165 #endif |
7 | 166 int tab_pad; |
217 | 167 int retval = FALSE; |
1359 | 168 int orig_char_len = -1; /* number of initial whitespace chars when |
1324 | 169 'et' and 'pi' are both set */ |
7 | 170 |
171 /* | |
172 * First check if there is anything to do and compute the number of | |
173 * characters needed for the indent. | |
174 */ | |
175 todo = size; | |
176 ind_len = 0; | |
177 p = oldline = ml_get_curline(); | |
178 | |
179 /* Calculate the buffer size for the new indent, and check to see if it | |
180 * isn't already set */ | |
181 | |
1324 | 182 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and |
183 * 'preserveindent' are set count the number of characters at the | |
184 * beginning of the line to be copied */ | |
185 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi)) | |
7 | 186 { |
187 /* If 'preserveindent' is set then reuse as much as possible of | |
188 * the existing indent structure for the new indent */ | |
189 if (!(flags & SIN_INSERT) && curbuf->b_p_pi) | |
190 { | |
191 ind_done = 0; | |
192 | |
193 /* count as many characters as we can use */ | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
194 while (todo > 0 && VIM_ISWHITE(*p)) |
7 | 195 { |
196 if (*p == TAB) | |
197 { | |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
198 #ifdef FEAT_VARTABS |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
199 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
200 curbuf->b_p_vts_array); |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
201 #else |
7 | 202 tab_pad = (int)curbuf->b_p_ts |
203 - (ind_done % (int)curbuf->b_p_ts); | |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
204 #endif |
7 | 205 /* stop if this tab will overshoot the target */ |
206 if (todo < tab_pad) | |
207 break; | |
208 todo -= tab_pad; | |
209 ++ind_len; | |
210 ind_done += tab_pad; | |
211 } | |
212 else | |
213 { | |
214 --todo; | |
215 ++ind_len; | |
216 ++ind_done; | |
217 } | |
218 ++p; | |
219 } | |
220 | |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
221 #ifdef FEAT_VARTABS |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
222 /* These diverge from this point. */ |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
223 ind_col = ind_done; |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
224 #endif |
1324 | 225 /* Set initial number of whitespace chars to copy if we are |
226 * preserving indent but expandtab is set */ | |
227 if (curbuf->b_p_et) | |
228 orig_char_len = ind_len; | |
229 | |
7 | 230 /* Fill to next tabstop with a tab, if possible */ |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
231 #ifdef FEAT_VARTABS |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
232 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
233 curbuf->b_p_vts_array); |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
234 #else |
7 | 235 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
236 #endif |
1359 | 237 if (todo >= tab_pad && orig_char_len == -1) |
7 | 238 { |
239 doit = TRUE; | |
240 todo -= tab_pad; | |
241 ++ind_len; | |
242 /* ind_done += tab_pad; */ | |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
243 #ifdef FEAT_VARTABS |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
244 ind_col += tab_pad; |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
245 #endif |
7 | 246 } |
247 } | |
248 | |
249 /* count tabs required for indent */ | |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
250 #ifdef FEAT_VARTABS |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
251 for (;;) |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
252 { |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
253 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts, |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
254 curbuf->b_p_vts_array); |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
255 if (todo < tab_pad) |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
256 break; |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
257 if (*p != TAB) |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
258 doit = TRUE; |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
259 else |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
260 ++p; |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
261 todo -= tab_pad; |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
262 ++ind_len; |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
263 ind_col += tab_pad; |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
264 } |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
265 #else |
7 | 266 while (todo >= (int)curbuf->b_p_ts) |
267 { | |
268 if (*p != TAB) | |
269 doit = TRUE; | |
270 else | |
271 ++p; | |
272 todo -= (int)curbuf->b_p_ts; | |
273 ++ind_len; | |
274 /* ind_done += (int)curbuf->b_p_ts; */ | |
275 } | |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
276 #endif |
7 | 277 } |
278 /* count spaces required for indent */ | |
279 while (todo > 0) | |
280 { | |
281 if (*p != ' ') | |
282 doit = TRUE; | |
283 else | |
284 ++p; | |
285 --todo; | |
286 ++ind_len; | |
287 /* ++ind_done; */ | |
288 } | |
289 | |
290 /* Return if the indent is OK already. */ | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
291 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT)) |
7 | 292 return FALSE; |
293 | |
294 /* Allocate memory for the new line. */ | |
295 if (flags & SIN_INSERT) | |
296 p = oldline; | |
297 else | |
298 p = skipwhite(p); | |
299 line_len = (int)STRLEN(p) + 1; | |
1324 | 300 |
301 /* If 'preserveindent' and 'expandtab' are both set keep the original | |
302 * characters and allocate accordingly. We will fill the rest with spaces | |
303 * after the if (!curbuf->b_p_et) below. */ | |
1359 | 304 if (orig_char_len != -1) |
1324 | 305 { |
306 newline = alloc(orig_char_len + size - ind_done + line_len); | |
307 if (newline == NULL) | |
308 return FALSE; | |
1359 | 309 todo = size - ind_done; |
310 ind_len = orig_char_len + todo; /* Set total length of indent in | |
311 * characters, which may have been | |
312 * undercounted until now */ | |
1324 | 313 p = oldline; |
314 s = newline; | |
315 while (orig_char_len > 0) | |
316 { | |
317 *s++ = *p++; | |
318 orig_char_len--; | |
319 } | |
1474 | 320 |
1324 | 321 /* Skip over any additional white space (useful when newindent is less |
322 * than old) */ | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
323 while (VIM_ISWHITE(*p)) |
1474 | 324 ++p; |
1348 | 325 |
1324 | 326 } |
327 else | |
328 { | |
329 todo = size; | |
330 newline = alloc(ind_len + line_len); | |
331 if (newline == NULL) | |
332 return FALSE; | |
333 s = newline; | |
334 } | |
7 | 335 |
336 /* Put the characters in the new line. */ | |
337 /* if 'expandtab' isn't set: use TABs */ | |
338 if (!curbuf->b_p_et) | |
339 { | |
340 /* If 'preserveindent' is set then reuse as much as possible of | |
341 * the existing indent structure for the new indent */ | |
342 if (!(flags & SIN_INSERT) && curbuf->b_p_pi) | |
343 { | |
344 p = oldline; | |
345 ind_done = 0; | |
346 | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
347 while (todo > 0 && VIM_ISWHITE(*p)) |
7 | 348 { |
349 if (*p == TAB) | |
350 { | |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
351 #ifdef FEAT_VARTABS |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
352 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
353 curbuf->b_p_vts_array); |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
354 #else |
7 | 355 tab_pad = (int)curbuf->b_p_ts |
356 - (ind_done % (int)curbuf->b_p_ts); | |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
357 #endif |
7 | 358 /* stop if this tab will overshoot the target */ |
359 if (todo < tab_pad) | |
360 break; | |
361 todo -= tab_pad; | |
362 ind_done += tab_pad; | |
363 } | |
364 else | |
365 { | |
366 --todo; | |
367 ++ind_done; | |
368 } | |
369 *s++ = *p++; | |
370 } | |
371 | |
372 /* Fill to next tabstop with a tab, if possible */ | |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
373 #ifdef FEAT_VARTABS |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
374 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
375 curbuf->b_p_vts_array); |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
376 #else |
7 | 377 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
378 #endif |
7 | 379 if (todo >= tab_pad) |
380 { | |
381 *s++ = TAB; | |
382 todo -= tab_pad; | |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
383 #ifdef FEAT_VARTABS |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
384 ind_done += tab_pad; |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
385 #endif |
7 | 386 } |
387 | |
388 p = skipwhite(p); | |
389 } | |
390 | |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
391 #ifdef FEAT_VARTABS |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
392 for (;;) |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
393 { |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
394 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
395 curbuf->b_p_vts_array); |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
396 if (todo < tab_pad) |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
397 break; |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
398 *s++ = TAB; |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
399 todo -= tab_pad; |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
400 ind_done += tab_pad; |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
401 } |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
402 #else |
7 | 403 while (todo >= (int)curbuf->b_p_ts) |
404 { | |
405 *s++ = TAB; | |
406 todo -= (int)curbuf->b_p_ts; | |
407 } | |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
408 #endif |
7 | 409 } |
410 while (todo > 0) | |
411 { | |
412 *s++ = ' '; | |
413 --todo; | |
414 } | |
415 mch_memmove(s, p, (size_t)line_len); | |
416 | |
15398
3e02464faaac
patch 8.1.0707: text property columns are not adjusted for changed indent
Bram Moolenaar <Bram@vim.org>
parents:
15347
diff
changeset
|
417 // Replace the line (unless undo fails). |
7 | 418 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK) |
419 { | |
420 ml_replace(curwin->w_cursor.lnum, newline, FALSE); | |
421 if (flags & SIN_CHANGED) | |
422 changed_bytes(curwin->w_cursor.lnum, 0); | |
15398
3e02464faaac
patch 8.1.0707: text property columns are not adjusted for changed indent
Bram Moolenaar <Bram@vim.org>
parents:
15347
diff
changeset
|
423 |
3e02464faaac
patch 8.1.0707: text property columns are not adjusted for changed indent
Bram Moolenaar <Bram@vim.org>
parents:
15347
diff
changeset
|
424 // Correct saved cursor position if it is in this line. |
5403 | 425 if (saved_cursor.lnum == curwin->w_cursor.lnum) |
426 { | |
427 if (saved_cursor.col >= (colnr_T)(p - oldline)) | |
15398
3e02464faaac
patch 8.1.0707: text property columns are not adjusted for changed indent
Bram Moolenaar <Bram@vim.org>
parents:
15347
diff
changeset
|
428 // cursor was after the indent, adjust for the number of |
3e02464faaac
patch 8.1.0707: text property columns are not adjusted for changed indent
Bram Moolenaar <Bram@vim.org>
parents:
15347
diff
changeset
|
429 // bytes added/removed |
5403 | 430 saved_cursor.col += ind_len - (colnr_T)(p - oldline); |
431 else if (saved_cursor.col >= (colnr_T)(s - newline)) | |
15398
3e02464faaac
patch 8.1.0707: text property columns are not adjusted for changed indent
Bram Moolenaar <Bram@vim.org>
parents:
15347
diff
changeset
|
432 // cursor was in the indent, and is now after it, put it back |
3e02464faaac
patch 8.1.0707: text property columns are not adjusted for changed indent
Bram Moolenaar <Bram@vim.org>
parents:
15347
diff
changeset
|
433 // at the start of the indent (replacing spaces with TAB) |
5403 | 434 saved_cursor.col = (colnr_T)(s - newline); |
435 } | |
15398
3e02464faaac
patch 8.1.0707: text property columns are not adjusted for changed indent
Bram Moolenaar <Bram@vim.org>
parents:
15347
diff
changeset
|
436 #ifdef FEAT_TEXT_PROP |
16682
7847d281cbbf
patch 8.1.1343: text properties not adjusted for Visual block mode delete
Bram Moolenaar <Bram@vim.org>
parents:
16632
diff
changeset
|
437 { |
7847d281cbbf
patch 8.1.1343: text properties not adjusted for Visual block mode delete
Bram Moolenaar <Bram@vim.org>
parents:
16632
diff
changeset
|
438 int added = ind_len - (colnr_T)(p - oldline); |
7847d281cbbf
patch 8.1.1343: text properties not adjusted for Visual block mode delete
Bram Moolenaar <Bram@vim.org>
parents:
16632
diff
changeset
|
439 |
7847d281cbbf
patch 8.1.1343: text properties not adjusted for Visual block mode delete
Bram Moolenaar <Bram@vim.org>
parents:
16632
diff
changeset
|
440 // When increasing indent this behaves like spaces were inserted at |
7847d281cbbf
patch 8.1.1343: text properties not adjusted for Visual block mode delete
Bram Moolenaar <Bram@vim.org>
parents:
16632
diff
changeset
|
441 // the old indent, when decreasing indent it behaves like spaces |
7847d281cbbf
patch 8.1.1343: text properties not adjusted for Visual block mode delete
Bram Moolenaar <Bram@vim.org>
parents:
16632
diff
changeset
|
442 // were deleted at the new indent. |
7847d281cbbf
patch 8.1.1343: text properties not adjusted for Visual block mode delete
Bram Moolenaar <Bram@vim.org>
parents:
16632
diff
changeset
|
443 adjust_prop_columns(curwin->w_cursor.lnum, |
16714
ba592f30c082
patch 8.1.1359: text property wrong after :substitute with backslash
Bram Moolenaar <Bram@vim.org>
parents:
16698
diff
changeset
|
444 (colnr_T)(added > 0 ? (p - oldline) : ind_len), added, 0); |
16682
7847d281cbbf
patch 8.1.1343: text properties not adjusted for Visual block mode delete
Bram Moolenaar <Bram@vim.org>
parents:
16632
diff
changeset
|
445 } |
15398
3e02464faaac
patch 8.1.0707: text property columns are not adjusted for changed indent
Bram Moolenaar <Bram@vim.org>
parents:
15347
diff
changeset
|
446 #endif |
217 | 447 retval = TRUE; |
7 | 448 } |
449 else | |
450 vim_free(newline); | |
451 | |
452 curwin->w_cursor.col = ind_len; | |
217 | 453 return retval; |
7 | 454 } |
455 | |
456 /* | |
457 * Return the indent of the current line after a number. Return -1 if no | |
458 * number was found. Used for 'n' in 'formatoptions': numbered list. | |
41 | 459 * Since a pattern is used it can actually handle more than numbers. |
7 | 460 */ |
461 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
462 get_number_indent(linenr_T lnum) |
7 | 463 { |
464 colnr_T col; | |
465 pos_T pos; | |
466 | |
3632 | 467 regmatch_T regmatch; |
468 int lead_len = 0; /* length of comment leader */ | |
469 | |
7 | 470 if (lnum > curbuf->b_ml.ml_line_count) |
471 return -1; | |
41 | 472 pos.lnum = 0; |
3584 | 473 |
474 #ifdef FEAT_COMMENTS | |
3632 | 475 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */ |
476 if ((State & INSERT) || has_format_option(FO_Q_COMS)) | |
3584 | 477 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE); |
3632 | 478 #endif |
479 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC); | |
480 if (regmatch.regprog != NULL) | |
481 { | |
482 regmatch.rm_ic = FALSE; | |
483 | |
484 /* vim_regexec() expects a pointer to a line. This lets us | |
485 * start matching for the flp beyond any comment leader... */ | |
486 if (vim_regexec(®match, ml_get(lnum) + lead_len, (colnr_T)0)) | |
487 { | |
488 pos.lnum = lnum; | |
489 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum)); | |
490 pos.coladd = 0; | |
491 } | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4752
diff
changeset
|
492 vim_regfree(regmatch.regprog); |
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4752
diff
changeset
|
493 } |
41 | 494 |
495 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL) | |
7 | 496 return -1; |
497 getvcol(curwin, &pos, &col, NULL, NULL); | |
498 return (int)col; | |
499 } | |
500 | |
5995 | 501 #if defined(FEAT_LINEBREAK) || defined(PROTO) |
502 /* | |
503 * Return appropriate space number for breakindent, taking influencing | |
504 * parameters into account. Window must be specified, since it is not | |
505 * necessarily always the current one. | |
506 */ | |
507 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
508 get_breakindent_win( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
509 win_T *wp, |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
510 char_u *line) /* start of the line */ |
5995 | 511 { |
512 static int prev_indent = 0; /* cached indent value */ | |
513 static long prev_ts = 0L; /* cached tabstop value */ | |
514 static char_u *prev_line = NULL; /* cached pointer to line */ | |
10889
5780bd3a5a7e
patch 8.0.0334: can't access b:changedtick from a dict reference
Christian Brabandt <cb@256bit.org>
parents:
10807
diff
changeset
|
515 static varnumber_T prev_tick = 0; /* changedtick of cached value */ |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
516 #ifdef FEAT_VARTABS |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
517 static int *prev_vts = NULL; /* cached vartabs values */ |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
518 #endif |
5995 | 519 int bri = 0; |
520 /* window width minus window margin space, i.e. what rests for text */ | |
12515
972ea22c946f
patch 8.0.1136: W_WIDTH() is always the same
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
521 const int eff_wwidth = wp->w_width |
5995 | 522 - ((wp->w_p_nu || wp->w_p_rnu) |
523 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL) | |
524 ? number_width(wp) + 1 : 0); | |
525 | |
526 /* used cached indent, unless pointer or 'tabstop' changed */ | |
6010 | 527 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
528 || prev_tick != CHANGEDTICK(wp->w_buffer) |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
529 #ifdef FEAT_VARTABS |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
530 || prev_vts != wp->w_buffer->b_p_vts_array |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
531 #endif |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
532 ) |
5995 | 533 { |
534 prev_line = line; | |
535 prev_ts = wp->w_buffer->b_p_ts; | |
10952
835604f3c37a
patch 8.0.0365: might free a dict item that wasn't allocated
Christian Brabandt <cb@256bit.org>
parents:
10932
diff
changeset
|
536 prev_tick = CHANGEDTICK(wp->w_buffer); |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
537 #ifdef FEAT_VARTABS |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
538 prev_vts = wp->w_buffer->b_p_vts_array; |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
539 prev_indent = get_indent_str_vtab(line, |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
540 (int)wp->w_buffer->b_p_ts, |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
541 wp->w_buffer->b_p_vts_array, wp->w_p_list); |
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
542 #else |
5995 | 543 prev_indent = get_indent_str(line, |
6012 | 544 (int)wp->w_buffer->b_p_ts, wp->w_p_list); |
14175
2ad722003b36
patch 8.1.0105: all tab stops are the same
Christian Brabandt <cb@256bit.org>
parents:
14133
diff
changeset
|
545 #endif |
6012 | 546 } |
547 bri = prev_indent + wp->w_p_brishift; | |
5995 | 548 |
549 /* indent minus the length of the showbreak string */ | |
550 if (wp->w_p_brisbr) | |
551 bri -= vim_strsize(p_sbr); | |
552 | |
553 /* Add offset for number column, if 'n' is in 'cpoptions' */ | |
554 bri += win_col_off2(wp); | |
555 | |
556 /* never indent past left window margin */ | |
557 if (bri < 0) | |
558 bri = 0; | |
559 /* always leave at least bri_min characters on the left, | |
560 * if text width is sufficient */ | |
561 else if (bri > eff_wwidth - wp->w_p_brimin) | |
562 bri = (eff_wwidth - wp->w_p_brimin < 0) | |
563 ? 0 : eff_wwidth - wp->w_p_brimin; | |
564 | |
565 return bri; | |
566 } | |
567 #endif | |
568 | |
7 | 569 #if defined(FEAT_COMMENTS) || defined(PROTO) |
570 /* | |
5403 | 571 * get_leader_len() returns the length in bytes of the prefix of the given |
572 * string which introduces a comment. If this string is not a comment then | |
573 * 0 is returned. | |
7 | 574 * When "flags" is not NULL, it is set to point to the flags of the recognized |
575 * comment leader. | |
576 * "backward" must be true for the "O" command. | |
3562 | 577 * If "include_space" is set, include trailing whitespace while calculating the |
578 * length. | |
7 | 579 */ |
580 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
581 get_leader_len( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
582 char_u *line, |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
583 char_u **flags, |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
584 int backward, |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
585 int include_space) |
7 | 586 { |
587 int i, j; | |
3562 | 588 int result; |
7 | 589 int got_com = FALSE; |
590 int found_one; | |
591 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */ | |
592 char_u *string; /* pointer to comment string */ | |
593 char_u *list; | |
2809 | 594 int middle_match_len = 0; |
595 char_u *prev_list; | |
2813 | 596 char_u *saved_flags = NULL; |
7 | 597 |
3562 | 598 result = i = 0; |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
599 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */ |
7 | 600 ++i; |
601 | |
602 /* | |
603 * Repeat to match several nested comment strings. | |
604 */ | |
2809 | 605 while (line[i] != NUL) |
7 | 606 { |
607 /* | |
608 * scan through the 'comments' option for a match | |
609 */ | |
610 found_one = FALSE; | |
611 for (list = curbuf->b_p_com; *list; ) | |
612 { | |
2809 | 613 /* Get one option part into part_buf[]. Advance "list" to next |
614 * one. Put "string" at start of string. */ | |
615 if (!got_com && flags != NULL) | |
616 *flags = list; /* remember where flags started */ | |
617 prev_list = list; | |
7 | 618 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ","); |
619 string = vim_strchr(part_buf, ':'); | |
620 if (string == NULL) /* missing ':', ignore this part */ | |
621 continue; | |
622 *string++ = NUL; /* isolate flags from string */ | |
623 | |
2809 | 624 /* If we found a middle match previously, use that match when this |
625 * is not a middle or end. */ | |
626 if (middle_match_len != 0 | |
627 && vim_strchr(part_buf, COM_MIDDLE) == NULL | |
628 && vim_strchr(part_buf, COM_END) == NULL) | |
629 break; | |
630 | |
631 /* When we already found a nested comment, only accept further | |
632 * nested comments. */ | |
7 | 633 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL) |
634 continue; | |
635 | |
2809 | 636 /* When 'O' flag present and using "O" command skip this one. */ |
7 | 637 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL) |
638 continue; | |
639 | |
2809 | 640 /* Line contents and string must match. |
7 | 641 * When string starts with white space, must have some white space |
642 * (but the amount does not need to match, there might be a mix of | |
2809 | 643 * TABs and spaces). */ |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
644 if (VIM_ISWHITE(string[0])) |
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
645 { |
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
646 if (i == 0 || !VIM_ISWHITE(line[i - 1])) |
4352 | 647 continue; /* missing white space */ |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
648 while (VIM_ISWHITE(string[0])) |
7 | 649 ++string; |
650 } | |
651 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j) | |
652 ; | |
653 if (string[j] != NUL) | |
2809 | 654 continue; /* string doesn't match */ |
655 | |
656 /* When 'b' flag used, there must be white space or an | |
657 * end-of-line after the string in the line. */ | |
7 | 658 if (vim_strchr(part_buf, COM_BLANK) != NULL |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
659 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL) |
7 | 660 continue; |
661 | |
2809 | 662 /* We have found a match, stop searching unless this is a middle |
663 * comment. The middle comment can be a substring of the end | |
664 * comment in which case it's better to return the length of the | |
665 * end comment and its flags. Thus we keep searching with middle | |
666 * and end matches and use an end match if it matches better. */ | |
667 if (vim_strchr(part_buf, COM_MIDDLE) != NULL) | |
668 { | |
669 if (middle_match_len == 0) | |
670 { | |
671 middle_match_len = j; | |
672 saved_flags = prev_list; | |
673 } | |
674 continue; | |
675 } | |
676 if (middle_match_len != 0 && j > middle_match_len) | |
677 /* Use this match instead of the middle match, since it's a | |
678 * longer thus better match. */ | |
679 middle_match_len = 0; | |
680 | |
681 if (middle_match_len == 0) | |
682 i += j; | |
7 | 683 found_one = TRUE; |
684 break; | |
685 } | |
686 | |
2809 | 687 if (middle_match_len != 0) |
688 { | |
689 /* Use the previously found middle match after failing to find a | |
690 * match with an end. */ | |
691 if (!got_com && flags != NULL) | |
692 *flags = saved_flags; | |
693 i += middle_match_len; | |
694 found_one = TRUE; | |
695 } | |
696 | |
697 /* No match found, stop scanning. */ | |
7 | 698 if (!found_one) |
699 break; | |
700 | |
3562 | 701 result = i; |
702 | |
2809 | 703 /* Include any trailing white space. */ |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
704 while (VIM_ISWHITE(line[i])) |
7 | 705 ++i; |
706 | |
3562 | 707 if (include_space) |
708 result = i; | |
709 | |
2809 | 710 /* If this comment doesn't nest, stop here. */ |
711 got_com = TRUE; | |
7 | 712 if (vim_strchr(part_buf, COM_NEST) == NULL) |
713 break; | |
714 } | |
3562 | 715 return result; |
716 } | |
717 | |
718 /* | |
719 * Return the offset at which the last comment in line starts. If there is no | |
720 * comment in the whole line, -1 is returned. | |
721 * | |
722 * When "flags" is not null, it is set to point to the flags describing the | |
723 * recognized comment leader. | |
724 */ | |
725 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
726 get_last_leader_offset(char_u *line, char_u **flags) |
3562 | 727 { |
728 int result = -1; | |
729 int i, j; | |
730 int lower_check_bound = 0; | |
731 char_u *string; | |
732 char_u *com_leader; | |
733 char_u *com_flags; | |
734 char_u *list; | |
735 int found_one; | |
736 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */ | |
737 | |
738 /* | |
739 * Repeat to match several nested comment strings. | |
740 */ | |
741 i = (int)STRLEN(line); | |
742 while (--i >= lower_check_bound) | |
743 { | |
744 /* | |
745 * scan through the 'comments' option for a match | |
746 */ | |
747 found_one = FALSE; | |
748 for (list = curbuf->b_p_com; *list; ) | |
749 { | |
750 char_u *flags_save = list; | |
751 | |
752 /* | |
753 * Get one option part into part_buf[]. Advance list to next one. | |
754 * put string at start of string. | |
755 */ | |
756 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ","); | |
757 string = vim_strchr(part_buf, ':'); | |
758 if (string == NULL) /* If everything is fine, this cannot actually | |
759 * happen. */ | |
760 continue; | |
761 *string++ = NUL; /* Isolate flags from string. */ | |
762 com_leader = string; | |
763 | |
764 /* | |
765 * Line contents and string must match. | |
766 * When string starts with white space, must have some white space | |
767 * (but the amount does not need to match, there might be a mix of | |
768 * TABs and spaces). | |
769 */ | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
770 if (VIM_ISWHITE(string[0])) |
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
771 { |
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
772 if (i == 0 || !VIM_ISWHITE(line[i - 1])) |
3562 | 773 continue; |
15119
6e9f37bf987b
patch 8.1.0570: 'commentstring' not used when adding fold marker
Bram Moolenaar <Bram@vim.org>
parents:
15046
diff
changeset
|
774 while (VIM_ISWHITE(*string)) |
3562 | 775 ++string; |
776 } | |
777 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j) | |
778 /* do nothing */; | |
779 if (string[j] != NUL) | |
780 continue; | |
781 | |
782 /* | |
783 * When 'b' flag used, there must be white space or an | |
784 * end-of-line after the string in the line. | |
785 */ | |
786 if (vim_strchr(part_buf, COM_BLANK) != NULL | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
787 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL) |
3562 | 788 continue; |
15119
6e9f37bf987b
patch 8.1.0570: 'commentstring' not used when adding fold marker
Bram Moolenaar <Bram@vim.org>
parents:
15046
diff
changeset
|
789 |
15127
31a0127813cb
patch 8.1.0574: 'commentstring' not used when adding fold marker in C
Bram Moolenaar <Bram@vim.org>
parents:
15119
diff
changeset
|
790 if (vim_strchr(part_buf, COM_MIDDLE) != NULL) |
31a0127813cb
patch 8.1.0574: 'commentstring' not used when adding fold marker in C
Bram Moolenaar <Bram@vim.org>
parents:
15119
diff
changeset
|
791 { |
31a0127813cb
patch 8.1.0574: 'commentstring' not used when adding fold marker in C
Bram Moolenaar <Bram@vim.org>
parents:
15119
diff
changeset
|
792 // For a middlepart comment, only consider it to match if |
31a0127813cb
patch 8.1.0574: 'commentstring' not used when adding fold marker in C
Bram Moolenaar <Bram@vim.org>
parents:
15119
diff
changeset
|
793 // everything before the current position in the line is |
31a0127813cb
patch 8.1.0574: 'commentstring' not used when adding fold marker in C
Bram Moolenaar <Bram@vim.org>
parents:
15119
diff
changeset
|
794 // whitespace. Otherwise we would think we are inside a |
31a0127813cb
patch 8.1.0574: 'commentstring' not used when adding fold marker in C
Bram Moolenaar <Bram@vim.org>
parents:
15119
diff
changeset
|
795 // comment if the middle part appears somewhere in the middle |
31a0127813cb
patch 8.1.0574: 'commentstring' not used when adding fold marker in C
Bram Moolenaar <Bram@vim.org>
parents:
15119
diff
changeset
|
796 // of the line. E.g. for C the "*" appears often. |
15119
6e9f37bf987b
patch 8.1.0570: 'commentstring' not used when adding fold marker
Bram Moolenaar <Bram@vim.org>
parents:
15046
diff
changeset
|
797 for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++) |
6e9f37bf987b
patch 8.1.0570: 'commentstring' not used when adding fold marker
Bram Moolenaar <Bram@vim.org>
parents:
15046
diff
changeset
|
798 ; |
6e9f37bf987b
patch 8.1.0570: 'commentstring' not used when adding fold marker
Bram Moolenaar <Bram@vim.org>
parents:
15046
diff
changeset
|
799 if (j < i) |
6e9f37bf987b
patch 8.1.0570: 'commentstring' not used when adding fold marker
Bram Moolenaar <Bram@vim.org>
parents:
15046
diff
changeset
|
800 continue; |
3562 | 801 } |
802 | |
803 /* | |
804 * We have found a match, stop searching. | |
805 */ | |
806 found_one = TRUE; | |
807 | |
808 if (flags) | |
809 *flags = flags_save; | |
810 com_flags = flags_save; | |
811 | |
812 break; | |
813 } | |
814 | |
815 if (found_one) | |
816 { | |
817 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */ | |
818 int len1, len2, off; | |
819 | |
820 result = i; | |
821 /* | |
822 * If this comment nests, continue searching. | |
823 */ | |
824 if (vim_strchr(part_buf, COM_NEST) != NULL) | |
825 continue; | |
826 | |
827 lower_check_bound = i; | |
828 | |
829 /* Let's verify whether the comment leader found is a substring | |
830 * of other comment leaders. If it is, let's adjust the | |
831 * lower_check_bound so that we make sure that we have determined | |
832 * the comment leader correctly. | |
833 */ | |
834 | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
835 while (VIM_ISWHITE(*com_leader)) |
3562 | 836 ++com_leader; |
837 len1 = (int)STRLEN(com_leader); | |
838 | |
839 for (list = curbuf->b_p_com; *list; ) | |
840 { | |
841 char_u *flags_save = list; | |
842 | |
843 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ","); | |
844 if (flags_save == com_flags) | |
845 continue; | |
846 string = vim_strchr(part_buf2, ':'); | |
847 ++string; | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
848 while (VIM_ISWHITE(*string)) |
3562 | 849 ++string; |
850 len2 = (int)STRLEN(string); | |
851 if (len2 == 0) | |
852 continue; | |
853 | |
854 /* Now we have to verify whether string ends with a substring | |
855 * beginning the com_leader. */ | |
856 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;) | |
857 { | |
858 --off; | |
859 if (!STRNCMP(string + off, com_leader, len2 - off)) | |
860 { | |
861 if (i - off < lower_check_bound) | |
862 lower_check_bound = i - off; | |
863 } | |
864 } | |
865 } | |
866 } | |
867 } | |
868 return result; | |
7 | 869 } |
870 #endif | |
871 | |
872 /* | |
873 * Return the number of window lines occupied by buffer line "lnum". | |
874 */ | |
875 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
876 plines(linenr_T lnum) |
7 | 877 { |
878 return plines_win(curwin, lnum, TRUE); | |
879 } | |
880 | |
881 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
882 plines_win( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
883 win_T *wp, |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
884 linenr_T lnum, |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
885 int winheight) /* when TRUE limit to window height */ |
7 | 886 { |
887 #if defined(FEAT_DIFF) || defined(PROTO) | |
888 /* Check for filler lines above this buffer line. When folded the result | |
889 * is one line anyway. */ | |
890 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum); | |
891 } | |
892 | |
893 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
894 plines_nofill(linenr_T lnum) |
7 | 895 { |
896 return plines_win_nofill(curwin, lnum, TRUE); | |
897 } | |
898 | |
899 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
900 plines_win_nofill( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
901 win_T *wp, |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
902 linenr_T lnum, |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
903 int winheight) /* when TRUE limit to window height */ |
7 | 904 { |
905 #endif | |
906 int lines; | |
907 | |
908 if (!wp->w_p_wrap) | |
909 return 1; | |
910 | |
911 if (wp->w_width == 0) | |
912 return 1; | |
913 | |
914 #ifdef FEAT_FOLDING | |
915 /* A folded lines is handled just like an empty line. */ | |
916 /* NOTE: Caller must handle lines that are MAYBE folded. */ | |
917 if (lineFolded(wp, lnum) == TRUE) | |
918 return 1; | |
919 #endif | |
920 | |
921 lines = plines_win_nofold(wp, lnum); | |
922 if (winheight > 0 && lines > wp->w_height) | |
923 return (int)wp->w_height; | |
924 return lines; | |
925 } | |
926 | |
927 /* | |
928 * Return number of window lines physical line "lnum" will occupy in window | |
929 * "wp". Does not care about folding, 'wrap' or 'diff'. | |
930 */ | |
931 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
932 plines_win_nofold(win_T *wp, linenr_T lnum) |
7 | 933 { |
934 char_u *s; | |
935 long col; | |
936 int width; | |
937 | |
938 s = ml_get_buf(wp->w_buffer, lnum, FALSE); | |
939 if (*s == NUL) /* empty line */ | |
940 return 1; | |
941 col = win_linetabsize(wp, s, (colnr_T)MAXCOL); | |
942 | |
943 /* | |
944 * If list mode is on, then the '$' at the end of the line may take up one | |
945 * extra column. | |
946 */ | |
947 if (wp->w_p_list && lcs_eol != NUL) | |
948 col += 1; | |
949 | |
950 /* | |
2178
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2162
diff
changeset
|
951 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'. |
7 | 952 */ |
12515
972ea22c946f
patch 8.0.1136: W_WIDTH() is always the same
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
953 width = wp->w_width - win_col_off(wp); |
7 | 954 if (width <= 0) |
955 return 32000; | |
956 if (col <= width) | |
957 return 1; | |
958 col -= width; | |
959 width += win_col_off2(wp); | |
960 return (col + (width - 1)) / width + 1; | |
961 } | |
962 | |
963 /* | |
964 * Like plines_win(), but only reports the number of physical screen lines | |
965 * used from the start of the line to the given column number. | |
966 */ | |
967 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
968 plines_win_col(win_T *wp, linenr_T lnum, long column) |
7 | 969 { |
970 long col; | |
971 char_u *s; | |
972 int lines = 0; | |
973 int width; | |
5995 | 974 char_u *line; |
7 | 975 |
976 #ifdef FEAT_DIFF | |
977 /* Check for filler lines above this buffer line. When folded the result | |
978 * is one line anyway. */ | |
979 lines = diff_check_fill(wp, lnum); | |
980 #endif | |
981 | |
982 if (!wp->w_p_wrap) | |
983 return lines + 1; | |
984 | |
985 if (wp->w_width == 0) | |
986 return lines + 1; | |
987 | |
5995 | 988 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE); |
7 | 989 |
990 col = 0; | |
991 while (*s != NUL && --column >= 0) | |
992 { | |
5995 | 993 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL); |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
994 MB_PTR_ADV(s); |
7 | 995 } |
996 | |
997 /* | |
998 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in | |
999 * INSERT mode, then col must be adjusted so that it represents the last | |
1000 * screen position of the TAB. This only fixes an error when the TAB wraps | |
1001 * from one screen line to the next (when 'columns' is not a multiple of | |
1002 * 'ts') -- webb. | |
1003 */ | |
1004 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1)) | |
5995 | 1005 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1; |
7 | 1006 |
1007 /* | |
2178
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2162
diff
changeset
|
1008 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc. |
7 | 1009 */ |
12515
972ea22c946f
patch 8.0.1136: W_WIDTH() is always the same
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
1010 width = wp->w_width - win_col_off(wp); |
1023 | 1011 if (width <= 0) |
1012 return 9999; | |
1013 | |
1014 lines += 1; | |
1015 if (col > width) | |
1016 lines += (col - width) / (width + win_col_off2(wp)) + 1; | |
1017 return lines; | |
7 | 1018 } |
1019 | |
1020 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1021 plines_m_win(win_T *wp, linenr_T first, linenr_T last) |
7 | 1022 { |
1023 int count = 0; | |
1024 | |
1025 while (first <= last) | |
1026 { | |
1027 #ifdef FEAT_FOLDING | |
1028 int x; | |
1029 | |
1030 /* Check if there are any really folded lines, but also included lines | |
1031 * that are maybe folded. */ | |
1032 x = foldedCount(wp, first, NULL); | |
1033 if (x > 0) | |
1034 { | |
1035 ++count; /* count 1 for "+-- folded" line */ | |
1036 first += x; | |
1037 } | |
1038 else | |
1039 #endif | |
1040 { | |
1041 #ifdef FEAT_DIFF | |
1042 if (first == wp->w_topline) | |
1043 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill; | |
1044 else | |
1045 #endif | |
1046 count += plines_win(wp, first, TRUE); | |
1047 ++first; | |
1048 } | |
1049 } | |
1050 return (count); | |
1051 } | |
1052 | |
1053 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1054 gchar_pos(pos_T *pos) |
7 | 1055 { |
13082
a80082fd1a1d
patch 8.0.1416: crash when searching for a sentence
Christian Brabandt <cb@256bit.org>
parents:
12865
diff
changeset
|
1056 char_u *ptr; |
a80082fd1a1d
patch 8.0.1416: crash when searching for a sentence
Christian Brabandt <cb@256bit.org>
parents:
12865
diff
changeset
|
1057 |
a80082fd1a1d
patch 8.0.1416: crash when searching for a sentence
Christian Brabandt <cb@256bit.org>
parents:
12865
diff
changeset
|
1058 /* When searching columns is sometimes put at the end of a line. */ |
a80082fd1a1d
patch 8.0.1416: crash when searching for a sentence
Christian Brabandt <cb@256bit.org>
parents:
12865
diff
changeset
|
1059 if (pos->col == MAXCOL) |
a80082fd1a1d
patch 8.0.1416: crash when searching for a sentence
Christian Brabandt <cb@256bit.org>
parents:
12865
diff
changeset
|
1060 return NUL; |
a80082fd1a1d
patch 8.0.1416: crash when searching for a sentence
Christian Brabandt <cb@256bit.org>
parents:
12865
diff
changeset
|
1061 ptr = ml_get_pos(pos); |
7 | 1062 if (has_mbyte) |
1063 return (*mb_ptr2char)(ptr); | |
1064 return (int)*ptr; | |
1065 } | |
1066 | |
1067 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1068 gchar_cursor(void) |
7 | 1069 { |
1070 if (has_mbyte) | |
1071 return (*mb_ptr2char)(ml_get_cursor()); | |
1072 return (int)*ml_get_cursor(); | |
1073 } | |
1074 | |
1075 /* | |
1076 * Write a character at the current cursor position. | |
1077 * It is directly written into the block. | |
1078 */ | |
1079 void | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1080 pchar_cursor(int c) |
7 | 1081 { |
1082 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE) | |
1083 + curwin->w_cursor.col) = c; | |
1084 } | |
1085 | |
1086 /* | |
1087 * When extra == 0: Return TRUE if the cursor is before or on the first | |
1088 * non-blank in the line. | |
1089 * When extra == 1: Return TRUE if the cursor is before the first non-blank in | |
1090 * the line. | |
1091 */ | |
1092 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1093 inindent(int extra) |
7 | 1094 { |
1095 char_u *ptr; | |
1096 colnr_T col; | |
1097 | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
1098 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col) |
7 | 1099 ++ptr; |
1100 if (col >= curwin->w_cursor.col + extra) | |
1101 return TRUE; | |
1102 else | |
1103 return FALSE; | |
1104 } | |
1105 | |
1106 /* | |
1107 * Skip to next part of an option argument: Skip space and comma. | |
1108 */ | |
1109 char_u * | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1110 skip_to_option_part(char_u *p) |
7 | 1111 { |
1112 if (*p == ',') | |
1113 ++p; | |
1114 while (*p == ' ') | |
1115 ++p; | |
1116 return p; | |
1117 } | |
1118 | |
1119 /* | |
1120 * check_status: called when the status bars for the buffer 'buf' | |
1121 * need to be updated | |
1122 */ | |
1123 void | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1124 check_status(buf_T *buf) |
7 | 1125 { |
1126 win_T *wp; | |
1127 | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9536
diff
changeset
|
1128 FOR_ALL_WINDOWS(wp) |
7 | 1129 if (wp->w_buffer == buf && wp->w_status_height) |
1130 { | |
1131 wp->w_redr_status = TRUE; | |
1132 if (must_redraw < VALID) | |
1133 must_redraw = VALID; | |
1134 } | |
1135 } | |
1136 | |
1137 /* | |
1138 * Ask for a reply from the user, a 'y' or a 'n'. | |
1139 * No other characters are accepted, the message is repeated until a valid | |
1140 * reply is entered or CTRL-C is hit. | |
1141 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters | |
1142 * from any buffers but directly from the user. | |
1143 * | |
1144 * return the 'y' or 'n' | |
1145 */ | |
1146 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1147 ask_yesno(char_u *str, int direct) |
7 | 1148 { |
1149 int r = ' '; | |
1150 int save_State = State; | |
1151 | |
1152 if (exiting) /* put terminal in raw mode for this question */ | |
1153 settmode(TMODE_RAW); | |
1154 ++no_wait_return; | |
1155 #ifdef USE_ON_FLY_SCROLL | |
1156 dont_scroll = TRUE; /* disallow scrolling here */ | |
1157 #endif | |
1158 State = CONFIRM; /* mouse behaves like with :confirm */ | |
1159 #ifdef FEAT_MOUSE | |
1160 setmouse(); /* disables mouse for xterm */ | |
1161 #endif | |
1162 ++no_mapping; | |
1163 ++allow_keys; /* no mapping here, but recognize keys */ | |
1164 | |
1165 while (r != 'y' && r != 'n') | |
1166 { | |
1167 /* same highlighting as for wait_return */ | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15420
diff
changeset
|
1168 smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str); |
7 | 1169 if (direct) |
1170 r = get_keystroke(); | |
1171 else | |
1474 | 1172 r = plain_vgetc(); |
7 | 1173 if (r == Ctrl_C || r == ESC) |
1174 r = 'n'; | |
1175 msg_putchar(r); /* show what you typed */ | |
1176 out_flush(); | |
1177 } | |
1178 --no_wait_return; | |
1179 State = save_State; | |
1180 #ifdef FEAT_MOUSE | |
1181 setmouse(); | |
1182 #endif | |
1183 --no_mapping; | |
1184 --allow_keys; | |
1185 | |
1186 return r; | |
1187 } | |
1188 | |
4221 | 1189 #if defined(FEAT_MOUSE) || defined(PROTO) |
1190 /* | |
1191 * Return TRUE if "c" is a mouse key. | |
1192 */ | |
1193 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1194 is_mouse_key(int c) |
4221 | 1195 { |
1196 return c == K_LEFTMOUSE | |
1197 || c == K_LEFTMOUSE_NM | |
1198 || c == K_LEFTDRAG | |
1199 || c == K_LEFTRELEASE | |
1200 || c == K_LEFTRELEASE_NM | |
12865
ebb4f6c93598
patch 8.0.1309: cannot use 'balloonexpr' in a terminal
Christian Brabandt <cb@256bit.org>
parents:
12716
diff
changeset
|
1201 || c == K_MOUSEMOVE |
4221 | 1202 || c == K_MIDDLEMOUSE |
1203 || c == K_MIDDLEDRAG | |
1204 || c == K_MIDDLERELEASE | |
1205 || c == K_RIGHTMOUSE | |
1206 || c == K_RIGHTDRAG | |
1207 || c == K_RIGHTRELEASE | |
1208 || c == K_MOUSEDOWN | |
1209 || c == K_MOUSEUP | |
1210 || c == K_MOUSELEFT | |
1211 || c == K_MOUSERIGHT | |
1212 || c == K_X1MOUSE | |
1213 || c == K_X1DRAG | |
1214 || c == K_X1RELEASE | |
1215 || c == K_X2MOUSE | |
1216 || c == K_X2DRAG | |
1217 || c == K_X2RELEASE; | |
1218 } | |
1219 #endif | |
1220 | |
7 | 1221 /* |
1222 * Get a key stroke directly from the user. | |
1223 * Ignores mouse clicks and scrollbar events, except a click for the left | |
1224 * button (used at the more prompt). | |
1225 * Doesn't use vgetc(), because it syncs undo and eats mapped characters. | |
1226 * Disadvantage: typeahead is ignored. | |
1227 * Translates the interrupt character for unix to ESC. | |
1228 */ | |
1229 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1230 get_keystroke(void) |
7 | 1231 { |
3328 | 1232 char_u *buf = NULL; |
1233 int buflen = 150; | |
1234 int maxlen; | |
7 | 1235 int len = 0; |
1236 int n; | |
1237 int save_mapped_ctrl_c = mapped_ctrl_c; | |
964 | 1238 int waited = 0; |
7 | 1239 |
1240 mapped_ctrl_c = FALSE; /* mappings are not used here */ | |
1241 for (;;) | |
1242 { | |
1243 cursor_on(); | |
1244 out_flush(); | |
1245 | |
3328 | 1246 /* Leave some room for check_termcode() to insert a key code into (max |
1247 * 5 chars plus NUL). And fix_input_buffer() can triple the number of | |
1248 * bytes. */ | |
1249 maxlen = (buflen - 6 - len) / 3; | |
1250 if (buf == NULL) | |
1251 buf = alloc(buflen); | |
1252 else if (maxlen < 10) | |
1253 { | |
6596 | 1254 char_u *t_buf = buf; |
1255 | |
3596 | 1256 /* Need some more space. This might happen when receiving a long |
3328 | 1257 * escape sequence. */ |
1258 buflen += 100; | |
1259 buf = vim_realloc(buf, buflen); | |
6596 | 1260 if (buf == NULL) |
1261 vim_free(t_buf); | |
3328 | 1262 maxlen = (buflen - 6 - len) / 3; |
1263 } | |
1264 if (buf == NULL) | |
1265 { | |
1266 do_outofmem_msg((long_u)buflen); | |
1267 return ESC; /* panic! */ | |
1268 } | |
1269 | |
7 | 1270 /* First time: blocking wait. Second time: wait up to 100ms for a |
3328 | 1271 * terminal code to complete. */ |
1272 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0); | |
7 | 1273 if (n > 0) |
1274 { | |
1275 /* Replace zero and CSI by a special key code. */ | |
9896
7b39615c0db1
commit https://github.com/vim/vim/commit/6bff02eb530aa29aafa2cb5627399837be7a5dd5
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
1276 n = fix_input_buffer(buf + len, n); |
7 | 1277 len += n; |
964 | 1278 waited = 0; |
1279 } | |
1280 else if (len > 0) | |
1281 ++waited; /* keep track of the waiting time */ | |
1282 | |
1283 /* Incomplete termcode and not timed out yet: get more characters */ | |
3328 | 1284 if ((n = check_termcode(1, buf, buflen, &len)) < 0 |
964 | 1285 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm))) |
7 | 1286 continue; |
964 | 1287 |
2672 | 1288 if (n == KEYLEN_REMOVED) /* key code removed */ |
2721 | 1289 { |
2723 | 1290 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0) |
2721 | 1291 { |
1292 /* Redrawing was postponed, do it now. */ | |
1293 update_screen(0); | |
1294 setcursor(); /* put cursor back where it belongs */ | |
1295 } | |
2672 | 1296 continue; |
2721 | 1297 } |
2672 | 1298 if (n > 0) /* found a termcode: adjust length */ |
7 | 1299 len = n; |
2672 | 1300 if (len == 0) /* nothing typed yet */ |
7 | 1301 continue; |
1302 | |
1303 /* Handle modifier and/or special key code. */ | |
1304 n = buf[0]; | |
1305 if (n == K_SPECIAL) | |
1306 { | |
1307 n = TO_SPECIAL(buf[1], buf[2]); | |
1308 if (buf[1] == KS_MODIFIER | |
1309 || n == K_IGNORE | |
4225 | 1310 #ifdef FEAT_MOUSE |
4221 | 1311 || (is_mouse_key(n) && n != K_LEFTMOUSE) |
4225 | 1312 #endif |
4221 | 1313 #ifdef FEAT_GUI |
7 | 1314 || n == K_VER_SCROLLBAR |
1315 || n == K_HOR_SCROLLBAR | |
1316 #endif | |
1317 ) | |
1318 { | |
1319 if (buf[1] == KS_MODIFIER) | |
1320 mod_mask = buf[2]; | |
1321 len -= 3; | |
1322 if (len > 0) | |
1323 mch_memmove(buf, buf + 3, (size_t)len); | |
1324 continue; | |
1325 } | |
828 | 1326 break; |
7 | 1327 } |
1328 if (has_mbyte) | |
1329 { | |
1330 if (MB_BYTE2LEN(n) > len) | |
1331 continue; /* more bytes to get */ | |
3328 | 1332 buf[len >= buflen ? buflen - 1 : len] = NUL; |
7 | 1333 n = (*mb_ptr2char)(buf); |
1334 } | |
1335 #ifdef UNIX | |
1336 if (n == intr_char) | |
1337 n = ESC; | |
1338 #endif | |
1339 break; | |
1340 } | |
3328 | 1341 vim_free(buf); |
7 | 1342 |
1343 mapped_ctrl_c = save_mapped_ctrl_c; | |
1344 return n; | |
1345 } | |
1346 | |
1347 /* | |
374 | 1348 * Get a number from the user. |
1349 * When "mouse_used" is not NULL allow using the mouse. | |
7 | 1350 */ |
1351 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1352 get_number( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1353 int colon, /* allow colon to abort */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1354 int *mouse_used) |
7 | 1355 { |
1356 int n = 0; | |
1357 int c; | |
810 | 1358 int typed = 0; |
7 | 1359 |
374 | 1360 if (mouse_used != NULL) |
1361 *mouse_used = FALSE; | |
1362 | |
7 | 1363 /* When not printing messages, the user won't know what to type, return a |
1364 * zero (as if CR was hit). */ | |
1365 if (msg_silent != 0) | |
1366 return 0; | |
1367 | |
1368 #ifdef USE_ON_FLY_SCROLL | |
1369 dont_scroll = TRUE; /* disallow scrolling here */ | |
1370 #endif | |
1371 ++no_mapping; | |
1372 ++allow_keys; /* no mapping here, but recognize keys */ | |
1373 for (;;) | |
1374 { | |
1375 windgoto(msg_row, msg_col); | |
1376 c = safe_vgetc(); | |
1377 if (VIM_ISDIGIT(c)) | |
1378 { | |
1379 n = n * 10 + c - '0'; | |
1380 msg_putchar(c); | |
810 | 1381 ++typed; |
7 | 1382 } |
1383 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H) | |
1384 { | |
810 | 1385 if (typed > 0) |
1386 { | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15525
diff
changeset
|
1387 msg_puts("\b \b"); |
810 | 1388 --typed; |
1389 } | |
7 | 1390 n /= 10; |
1391 } | |
374 | 1392 #ifdef FEAT_MOUSE |
1393 else if (mouse_used != NULL && c == K_LEFTMOUSE) | |
1394 { | |
1395 *mouse_used = TRUE; | |
1396 n = mouse_row + 1; | |
1397 break; | |
1398 } | |
1399 #endif | |
7 | 1400 else if (n == 0 && c == ':' && colon) |
1401 { | |
1402 stuffcharReadbuff(':'); | |
1403 if (!exmode_active) | |
1404 cmdline_row = msg_row; | |
1405 skip_redraw = TRUE; /* skip redraw once */ | |
1406 do_redraw = FALSE; | |
1407 break; | |
1408 } | |
1409 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC) | |
1410 break; | |
1411 } | |
1412 --no_mapping; | |
1413 --allow_keys; | |
1414 return n; | |
1415 } | |
1416 | |
323 | 1417 /* |
1418 * Ask the user to enter a number. | |
374 | 1419 * When "mouse_used" is not NULL allow using the mouse and in that case return |
1420 * the line number. | |
323 | 1421 */ |
1422 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1423 prompt_for_number(int *mouse_used) |
323 | 1424 { |
1425 int i; | |
344 | 1426 int save_cmdline_row; |
1427 int save_State; | |
323 | 1428 |
1429 /* When using ":silent" assume that <CR> was entered. */ | |
375 | 1430 if (mouse_used != NULL) |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15525
diff
changeset
|
1431 msg_puts(_("Type number and <Enter> or click with mouse (empty cancels): ")); |
375 | 1432 else |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15525
diff
changeset
|
1433 msg_puts(_("Type number and <Enter> (empty cancels): ")); |
344 | 1434 |
14629
100a44722322
patch 8.1.0328: inputlist() doesn't work with a timer
Christian Brabandt <cb@256bit.org>
parents:
14585
diff
changeset
|
1435 // Set the state such that text can be selected/copied/pasted and we still |
100a44722322
patch 8.1.0328: inputlist() doesn't work with a timer
Christian Brabandt <cb@256bit.org>
parents:
14585
diff
changeset
|
1436 // get mouse events. redraw_after_callback() will not redraw if cmdline_row |
100a44722322
patch 8.1.0328: inputlist() doesn't work with a timer
Christian Brabandt <cb@256bit.org>
parents:
14585
diff
changeset
|
1437 // is zero. |
344 | 1438 save_cmdline_row = cmdline_row; |
957 | 1439 cmdline_row = 0; |
344 | 1440 save_State = State; |
14629
100a44722322
patch 8.1.0328: inputlist() doesn't work with a timer
Christian Brabandt <cb@256bit.org>
parents:
14585
diff
changeset
|
1441 State = CMDLINE; |
13768
bcef80912112
patch 8.0.1756: GUI: after prompting for a number the mouse shape is wrong
Christian Brabandt <cb@256bit.org>
parents:
13380
diff
changeset
|
1442 #ifdef FEAT_MOUSE |
14629
100a44722322
patch 8.1.0328: inputlist() doesn't work with a timer
Christian Brabandt <cb@256bit.org>
parents:
14585
diff
changeset
|
1443 // May show different mouse shape. |
13768
bcef80912112
patch 8.0.1756: GUI: after prompting for a number the mouse shape is wrong
Christian Brabandt <cb@256bit.org>
parents:
13380
diff
changeset
|
1444 setmouse(); |
bcef80912112
patch 8.0.1756: GUI: after prompting for a number the mouse shape is wrong
Christian Brabandt <cb@256bit.org>
parents:
13380
diff
changeset
|
1445 #endif |
bcef80912112
patch 8.0.1756: GUI: after prompting for a number the mouse shape is wrong
Christian Brabandt <cb@256bit.org>
parents:
13380
diff
changeset
|
1446 |
374 | 1447 i = get_number(TRUE, mouse_used); |
1448 if (KeyTyped) | |
1449 { | |
17018
b1263192461f
patch 8.1.1509: cmdline_row can become negative, causing a crash
Bram Moolenaar <Bram@vim.org>
parents:
16782
diff
changeset
|
1450 // don't call wait_return() now |
b1263192461f
patch 8.1.1509: cmdline_row can become negative, causing a crash
Bram Moolenaar <Bram@vim.org>
parents:
16782
diff
changeset
|
1451 if (msg_row > 0) |
b1263192461f
patch 8.1.1509: cmdline_row can become negative, causing a crash
Bram Moolenaar <Bram@vim.org>
parents:
16782
diff
changeset
|
1452 cmdline_row = msg_row - 1; |
323 | 1453 need_wait_return = FALSE; |
1454 msg_didany = FALSE; | |
1938 | 1455 msg_didout = FALSE; |
323 | 1456 } |
344 | 1457 else |
1458 cmdline_row = save_cmdline_row; | |
1459 State = save_State; | |
13768
bcef80912112
patch 8.0.1756: GUI: after prompting for a number the mouse shape is wrong
Christian Brabandt <cb@256bit.org>
parents:
13380
diff
changeset
|
1460 #ifdef FEAT_MOUSE |
14629
100a44722322
patch 8.1.0328: inputlist() doesn't work with a timer
Christian Brabandt <cb@256bit.org>
parents:
14585
diff
changeset
|
1461 // May need to restore mouse shape. |
13768
bcef80912112
patch 8.0.1756: GUI: after prompting for a number the mouse shape is wrong
Christian Brabandt <cb@256bit.org>
parents:
13380
diff
changeset
|
1462 setmouse(); |
bcef80912112
patch 8.0.1756: GUI: after prompting for a number the mouse shape is wrong
Christian Brabandt <cb@256bit.org>
parents:
13380
diff
changeset
|
1463 #endif |
344 | 1464 |
323 | 1465 return i; |
1466 } | |
1467 | |
7 | 1468 void |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1469 msgmore(long n) |
7 | 1470 { |
1471 long pn; | |
1472 | |
1473 if (global_busy /* no messages now, wait until global is finished */ | |
1474 || !messaging()) /* 'lazyredraw' set, don't do messages now */ | |
1475 return; | |
1476 | |
135 | 1477 /* We don't want to overwrite another important message, but do overwrite |
1478 * a previous "more lines" or "fewer lines" message, so that "5dd" and | |
1479 * then "put" reports the last action. */ | |
1480 if (keep_msg != NULL && !keep_msg_more) | |
1481 return; | |
1482 | |
7 | 1483 if (n > 0) |
1484 pn = n; | |
1485 else | |
1486 pn = -n; | |
1487 | |
1488 if (pn > p_report) | |
1489 { | |
14585
c8f07e8b273e
patch 8.1.0306: plural messages are not translated properly
Christian Brabandt <cb@256bit.org>
parents:
14424
diff
changeset
|
1490 if (n > 0) |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15525
diff
changeset
|
1491 vim_snprintf(msg_buf, MSG_BUF_LEN, |
14585
c8f07e8b273e
patch 8.1.0306: plural messages are not translated properly
Christian Brabandt <cb@256bit.org>
parents:
14424
diff
changeset
|
1492 NGETTEXT("%ld more line", "%ld more lines", pn), pn); |
7 | 1493 else |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15525
diff
changeset
|
1494 vim_snprintf(msg_buf, MSG_BUF_LEN, |
14585
c8f07e8b273e
patch 8.1.0306: plural messages are not translated properly
Christian Brabandt <cb@256bit.org>
parents:
14424
diff
changeset
|
1495 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn); |
7 | 1496 if (got_int) |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15525
diff
changeset
|
1497 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"), |
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15525
diff
changeset
|
1498 MSG_BUF_LEN); |
7 | 1499 if (msg(msg_buf)) |
1500 { | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15525
diff
changeset
|
1501 set_keep_msg((char_u *)msg_buf, 0); |
135 | 1502 keep_msg_more = TRUE; |
7 | 1503 } |
1504 } | |
1505 } | |
1506 | |
1507 /* | |
1508 * flush map and typeahead buffers and give a warning for an error | |
1509 */ | |
1510 void | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1511 beep_flush(void) |
7 | 1512 { |
1513 if (emsg_silent == 0) | |
1514 { | |
14909
c97b4b537572
patch 8.1.0466: autocmd test fails
Bram Moolenaar <Bram@vim.org>
parents:
14862
diff
changeset
|
1515 flush_buffers(FLUSH_MINIMAL); |
6949 | 1516 vim_beep(BO_ERROR); |
1517 } | |
1518 } | |
1519 | |
1520 /* | |
1521 * Give a warning for an error. | |
7 | 1522 */ |
1523 void | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1524 vim_beep( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1525 unsigned val) /* one of the BO_ values, e.g., BO_OPER */ |
7 | 1526 { |
13272
abaebba89fd4
patch 8.0.1510: cannot test if a command causes a beep
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
1527 #ifdef FEAT_EVAL |
abaebba89fd4
patch 8.0.1510: cannot test if a command causes a beep
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
1528 called_vim_beep = TRUE; |
abaebba89fd4
patch 8.0.1510: cannot test if a command causes a beep
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
1529 #endif |
abaebba89fd4
patch 8.0.1510: cannot test if a command causes a beep
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
1530 |
7 | 1531 if (emsg_silent == 0) |
1532 { | |
6949 | 1533 if (!((bo_flags & val) || (bo_flags & BO_ALL))) |
1534 { | |
11601
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1535 #ifdef ELAPSED_FUNC |
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1536 static int did_init = FALSE; |
15525
3ef31ce9d9f9
patch 8.1.0770: inconsistent use of ELAPSED_FUNC
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
1537 static elapsed_T start_tv; |
11601
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1538 |
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1539 /* Only beep once per half a second, otherwise a sequence of beeps |
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1540 * would freeze Vim. */ |
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1541 if (!did_init || ELAPSED_FUNC(start_tv) > 500) |
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1542 { |
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1543 did_init = TRUE; |
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1544 ELAPSED_INIT(start_tv); |
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1545 #endif |
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1546 if (p_vb |
7 | 1547 #ifdef FEAT_GUI |
11601
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1548 /* While the GUI is starting up the termcap is set for |
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1549 * the GUI but the output still goes to a terminal. */ |
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1550 && !(gui.in_use && gui.starting) |
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1551 #endif |
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1552 ) |
13314
65c3e8259124
patch 8.0.1531: cannot use 24 bit colors in MS-Windows console
Christian Brabandt <cb@256bit.org>
parents:
13272
diff
changeset
|
1553 { |
11601
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1554 out_str_cf(T_VB); |
13314
65c3e8259124
patch 8.0.1531: cannot use 24 bit colors in MS-Windows console
Christian Brabandt <cb@256bit.org>
parents:
13272
diff
changeset
|
1555 #ifdef FEAT_VTP |
65c3e8259124
patch 8.0.1531: cannot use 24 bit colors in MS-Windows console
Christian Brabandt <cb@256bit.org>
parents:
13272
diff
changeset
|
1556 /* No restore color information, refresh the screen. */ |
65c3e8259124
patch 8.0.1531: cannot use 24 bit colors in MS-Windows console
Christian Brabandt <cb@256bit.org>
parents:
13272
diff
changeset
|
1557 if (has_vtp_working() != 0 |
65c3e8259124
patch 8.0.1531: cannot use 24 bit colors in MS-Windows console
Christian Brabandt <cb@256bit.org>
parents:
13272
diff
changeset
|
1558 # ifdef FEAT_TERMGUICOLORS |
13823
d0d8125ba692
patch 8.0.1783: cannot use 256 colors in a MS-Windows console
Christian Brabandt <cb@256bit.org>
parents:
13772
diff
changeset
|
1559 && (p_tgc || (!p_tgc && t_colors >= 256)) |
13314
65c3e8259124
patch 8.0.1531: cannot use 24 bit colors in MS-Windows console
Christian Brabandt <cb@256bit.org>
parents:
13272
diff
changeset
|
1560 # endif |
65c3e8259124
patch 8.0.1531: cannot use 24 bit colors in MS-Windows console
Christian Brabandt <cb@256bit.org>
parents:
13272
diff
changeset
|
1561 ) |
65c3e8259124
patch 8.0.1531: cannot use 24 bit colors in MS-Windows console
Christian Brabandt <cb@256bit.org>
parents:
13272
diff
changeset
|
1562 { |
65c3e8259124
patch 8.0.1531: cannot use 24 bit colors in MS-Windows console
Christian Brabandt <cb@256bit.org>
parents:
13272
diff
changeset
|
1563 redraw_later(CLEAR); |
65c3e8259124
patch 8.0.1531: cannot use 24 bit colors in MS-Windows console
Christian Brabandt <cb@256bit.org>
parents:
13272
diff
changeset
|
1564 update_screen(0); |
65c3e8259124
patch 8.0.1531: cannot use 24 bit colors in MS-Windows console
Christian Brabandt <cb@256bit.org>
parents:
13272
diff
changeset
|
1565 redrawcmd(); |
65c3e8259124
patch 8.0.1531: cannot use 24 bit colors in MS-Windows console
Christian Brabandt <cb@256bit.org>
parents:
13272
diff
changeset
|
1566 } |
65c3e8259124
patch 8.0.1531: cannot use 24 bit colors in MS-Windows console
Christian Brabandt <cb@256bit.org>
parents:
13272
diff
changeset
|
1567 #endif |
65c3e8259124
patch 8.0.1531: cannot use 24 bit colors in MS-Windows console
Christian Brabandt <cb@256bit.org>
parents:
13272
diff
changeset
|
1568 } |
11601
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1569 else |
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1570 out_char(BELL); |
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1571 #ifdef ELAPSED_FUNC |
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1572 } |
0a5d405e2520
patch 8.0.0683: visual bell flashes too quickly
Christian Brabandt <cb@256bit.org>
parents:
11384
diff
changeset
|
1573 #endif |
7 | 1574 } |
169 | 1575 |
13272
abaebba89fd4
patch 8.0.1510: cannot test if a command causes a beep
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
1576 /* When 'debug' contains "beep" produce a message. If we are sourcing |
abaebba89fd4
patch 8.0.1510: cannot test if a command causes a beep
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
1577 * a script or executing a function give the user a hint where the beep |
abaebba89fd4
patch 8.0.1510: cannot test if a command causes a beep
Christian Brabandt <cb@256bit.org>
parents:
13244
diff
changeset
|
1578 * comes from. */ |
169 | 1579 if (vim_strchr(p_debug, 'e') != NULL) |
1580 { | |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
1581 msg_source(HL_ATTR(HLF_W)); |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15525
diff
changeset
|
1582 msg_attr(_("Beep!"), HL_ATTR(HLF_W)); |
169 | 1583 } |
7 | 1584 } |
1585 } | |
1586 | |
1587 /* | |
1588 * To get the "real" home directory: | |
1589 * - get value of $HOME | |
1590 * For Unix: | |
1591 * - go to that directory | |
1592 * - do mch_dirname() to get the real name of that directory. | |
1593 * This also works with mounts and links. | |
1594 * Don't do this for MS-DOS, it will change the "current dir" for a drive. | |
15046
c1be7c8bb602
patch 8.1.0534: MS-Windows installer uses different $HOME than Vim
Bram Moolenaar <Bram@vim.org>
parents:
14909
diff
changeset
|
1595 * For Windows: |
c1be7c8bb602
patch 8.1.0534: MS-Windows installer uses different $HOME than Vim
Bram Moolenaar <Bram@vim.org>
parents:
14909
diff
changeset
|
1596 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync! |
7 | 1597 */ |
1598 static char_u *homedir = NULL; | |
1599 | |
1600 void | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1601 init_homedir(void) |
7 | 1602 { |
1603 char_u *var; | |
1604 | |
170 | 1605 /* In case we are called a second time (when 'encoding' changes). */ |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13188
diff
changeset
|
1606 VIM_CLEAR(homedir); |
170 | 1607 |
7 | 1608 #ifdef VMS |
1609 var = mch_getenv((char_u *)"SYS$LOGIN"); | |
1610 #else | |
1611 var = mch_getenv((char_u *)"HOME"); | |
1612 #endif | |
1613 | |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15814
diff
changeset
|
1614 #ifdef MSWIN |
7 | 1615 /* |
12265
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1616 * Typically, $HOME is not defined on Windows, unless the user has |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1617 * specifically defined it for Vim's sake. However, on Windows NT |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1618 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1619 * each user. Try constructing $HOME from these. |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1620 */ |
12269
d2373927d76d
patch 8.0.1014: old compiler doesn't know uint32_t
Christian Brabandt <cb@256bit.org>
parents:
12265
diff
changeset
|
1621 if (var == NULL || *var == NUL) |
12265
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1622 { |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1623 char_u *homedrive, *homepath; |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1624 |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1625 homedrive = mch_getenv((char_u *)"HOMEDRIVE"); |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1626 homepath = mch_getenv((char_u *)"HOMEPATH"); |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1627 if (homepath == NULL || *homepath == NUL) |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1628 homepath = (char_u *)"\\"; |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1629 if (homedrive != NULL |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1630 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL) |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1631 { |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1632 sprintf((char *)NameBuff, "%s%s", homedrive, homepath); |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1633 if (NameBuff[0] != NUL) |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1634 var = NameBuff; |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1635 } |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1636 } |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1637 |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1638 if (var == NULL) |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1639 var = mch_getenv((char_u *)"USERPROFILE"); |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1640 |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1641 /* |
7 | 1642 * Weird but true: $HOME may contain an indirect reference to another |
1643 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set | |
1644 * when $HOME is being set. | |
1645 */ | |
1646 if (var != NULL && *var == '%') | |
1647 { | |
1648 char_u *p; | |
1649 char_u *exp; | |
1650 | |
1651 p = vim_strchr(var + 1, '%'); | |
1652 if (p != NULL) | |
1653 { | |
419 | 1654 vim_strncpy(NameBuff, var + 1, p - (var + 1)); |
7 | 1655 exp = mch_getenv(NameBuff); |
1656 if (exp != NULL && *exp != NUL | |
1657 && STRLEN(exp) + STRLEN(p) < MAXPATHL) | |
1658 { | |
274 | 1659 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1); |
7 | 1660 var = NameBuff; |
12265
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1661 } |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1662 } |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1663 } |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1664 |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1665 if (var != NULL && *var == NUL) /* empty is same as not set */ |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1666 var = NULL; |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1667 |
170 | 1668 if (enc_utf8 && var != NULL) |
1669 { | |
1670 int len; | |
2786 | 1671 char_u *pp = NULL; |
170 | 1672 |
1673 /* Convert from active codepage to UTF-8. Other conversions are | |
1674 * not done, because they would fail for non-ASCII characters. */ | |
835 | 1675 acp_to_enc(var, (int)STRLEN(var), &pp, &len); |
170 | 1676 if (pp != NULL) |
1677 { | |
1678 homedir = pp; | |
1679 return; | |
1680 } | |
1681 } | |
12265
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1682 |
7 | 1683 /* |
1684 * Default home dir is C:/ | |
1685 * Best assumption we can make in such a situation. | |
1686 */ | |
1687 if (var == NULL) | |
8080
b6cb94ad97a4
commit https://github.com/vim/vim/commit/6aa2cd4be287f35f95f35c2cec6d5a24f53c4d3c
Christian Brabandt <cb@256bit.org>
parents:
7829
diff
changeset
|
1688 var = (char_u *)"C:/"; |
7 | 1689 #endif |
12265
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
1690 |
7 | 1691 if (var != NULL) |
1692 { | |
1693 #ifdef UNIX | |
1694 /* | |
1695 * Change to the directory and get the actual path. This resolves | |
1696 * links. Don't do it when we can't return. | |
1697 */ | |
1698 if (mch_dirname(NameBuff, MAXPATHL) == OK | |
1699 && mch_chdir((char *)NameBuff) == 0) | |
1700 { | |
1701 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK) | |
1702 var = IObuff; | |
1703 if (mch_chdir((char *)NameBuff) != 0) | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15420
diff
changeset
|
1704 emsg(_(e_prev_dir)); |
7 | 1705 } |
1706 #endif | |
1707 homedir = vim_strsave(var); | |
1708 } | |
1709 } | |
1710 | |
359 | 1711 #if defined(EXITFREE) || defined(PROTO) |
1712 void | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1713 free_homedir(void) |
359 | 1714 { |
1715 vim_free(homedir); | |
1716 } | |
3744 | 1717 |
1718 # ifdef FEAT_CMDL_COMPL | |
1719 void | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1720 free_users(void) |
3744 | 1721 { |
1722 ga_clear_strings(&ga_users); | |
1723 } | |
1724 # endif | |
359 | 1725 #endif |
1726 | |
7 | 1727 /* |
1408 | 1728 * Call expand_env() and store the result in an allocated string. |
1729 * This is not very memory efficient, this expects the result to be freed | |
1730 * again soon. | |
1731 */ | |
1732 char_u * | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1733 expand_env_save(char_u *src) |
1408 | 1734 { |
1735 return expand_env_save_opt(src, FALSE); | |
1736 } | |
1737 | |
1738 /* | |
1739 * Idem, but when "one" is TRUE handle the string as one file name, only | |
1740 * expand "~" at the start. | |
1741 */ | |
1742 char_u * | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1743 expand_env_save_opt(char_u *src, int one) |
1408 | 1744 { |
1745 char_u *p; | |
1746 | |
1747 p = alloc(MAXPATHL); | |
1748 if (p != NULL) | |
1749 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL); | |
1750 return p; | |
1751 } | |
1752 | |
1753 /* | |
7 | 1754 * Expand environment variable with path name. |
1755 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded. | |
1408 | 1756 * Skips over "\ ", "\~" and "\$" (not for Win32 though). |
7 | 1757 * If anything fails no expansion is done and dst equals src. |
1758 */ | |
1759 void | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1760 expand_env( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1761 char_u *src, /* input string e.g. "$HOME/vim.hlp" */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1762 char_u *dst, /* where to put the result */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1763 int dstlen) /* maximum length of the result */ |
7 | 1764 { |
1408 | 1765 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL); |
7 | 1766 } |
1767 | |
1768 void | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1769 expand_env_esc( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1770 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1771 char_u *dst, /* where to put the result */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1772 int dstlen, /* maximum length of the result */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1773 int esc, /* escape spaces in expanded variables */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1774 int one, /* "srcp" is one file name */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1775 char_u *startstr) /* start again after this (can be NULL) */ |
374 | 1776 { |
1777 char_u *src; | |
7 | 1778 char_u *tail; |
1779 int c; | |
1780 char_u *var; | |
1781 int copy_char; | |
1782 int mustfree; /* var was allocated, need to free it later */ | |
1783 int at_start = TRUE; /* at start of a name */ | |
374 | 1784 int startstr_len = 0; |
1785 | |
1786 if (startstr != NULL) | |
835 | 1787 startstr_len = (int)STRLEN(startstr); |
374 | 1788 |
1789 src = skipwhite(srcp); | |
7 | 1790 --dstlen; /* leave one char space for "\," */ |
1791 while (*src && dstlen > 0) | |
1792 { | |
7038
76042a56ab85
commit https://github.com/vim/vim/commit/be83b73ddb2ee8297037166d243f72e3423a3ce3
Christian Brabandt <cb@256bit.org>
parents:
7036
diff
changeset
|
1793 #ifdef FEAT_EVAL |
76042a56ab85
commit https://github.com/vim/vim/commit/be83b73ddb2ee8297037166d243f72e3423a3ce3
Christian Brabandt <cb@256bit.org>
parents:
7036
diff
changeset
|
1794 /* Skip over `=expr`. */ |
76042a56ab85
commit https://github.com/vim/vim/commit/be83b73ddb2ee8297037166d243f72e3423a3ce3
Christian Brabandt <cb@256bit.org>
parents:
7036
diff
changeset
|
1795 if (src[0] == '`' && src[1] == '=') |
76042a56ab85
commit https://github.com/vim/vim/commit/be83b73ddb2ee8297037166d243f72e3423a3ce3
Christian Brabandt <cb@256bit.org>
parents:
7036
diff
changeset
|
1796 { |
76042a56ab85
commit https://github.com/vim/vim/commit/be83b73ddb2ee8297037166d243f72e3423a3ce3
Christian Brabandt <cb@256bit.org>
parents:
7036
diff
changeset
|
1797 size_t len; |
76042a56ab85
commit https://github.com/vim/vim/commit/be83b73ddb2ee8297037166d243f72e3423a3ce3
Christian Brabandt <cb@256bit.org>
parents:
7036
diff
changeset
|
1798 |
76042a56ab85
commit https://github.com/vim/vim/commit/be83b73ddb2ee8297037166d243f72e3423a3ce3
Christian Brabandt <cb@256bit.org>
parents:
7036
diff
changeset
|
1799 var = src; |
76042a56ab85
commit https://github.com/vim/vim/commit/be83b73ddb2ee8297037166d243f72e3423a3ce3
Christian Brabandt <cb@256bit.org>
parents:
7036
diff
changeset
|
1800 src += 2; |
76042a56ab85
commit https://github.com/vim/vim/commit/be83b73ddb2ee8297037166d243f72e3423a3ce3
Christian Brabandt <cb@256bit.org>
parents:
7036
diff
changeset
|
1801 (void)skip_expr(&src); |
76042a56ab85
commit https://github.com/vim/vim/commit/be83b73ddb2ee8297037166d243f72e3423a3ce3
Christian Brabandt <cb@256bit.org>
parents:
7036
diff
changeset
|
1802 if (*src == '`') |
76042a56ab85
commit https://github.com/vim/vim/commit/be83b73ddb2ee8297037166d243f72e3423a3ce3
Christian Brabandt <cb@256bit.org>
parents:
7036
diff
changeset
|
1803 ++src; |
76042a56ab85
commit https://github.com/vim/vim/commit/be83b73ddb2ee8297037166d243f72e3423a3ce3
Christian Brabandt <cb@256bit.org>
parents:
7036
diff
changeset
|
1804 len = src - var; |
76042a56ab85
commit https://github.com/vim/vim/commit/be83b73ddb2ee8297037166d243f72e3423a3ce3
Christian Brabandt <cb@256bit.org>
parents:
7036
diff
changeset
|
1805 if (len > (size_t)dstlen) |
76042a56ab85
commit https://github.com/vim/vim/commit/be83b73ddb2ee8297037166d243f72e3423a3ce3
Christian Brabandt <cb@256bit.org>
parents:
7036
diff
changeset
|
1806 len = dstlen; |
76042a56ab85
commit https://github.com/vim/vim/commit/be83b73ddb2ee8297037166d243f72e3423a3ce3
Christian Brabandt <cb@256bit.org>
parents:
7036
diff
changeset
|
1807 vim_strncpy(dst, var, len); |
76042a56ab85
commit https://github.com/vim/vim/commit/be83b73ddb2ee8297037166d243f72e3423a3ce3
Christian Brabandt <cb@256bit.org>
parents:
7036
diff
changeset
|
1808 dst += len; |
7066
094c8ccdc279
commit https://github.com/vim/vim/commit/5df1ed2de3fa9dcace996b9a0a4c9b3cea79cf1e
Christian Brabandt <cb@256bit.org>
parents:
7038
diff
changeset
|
1809 dstlen -= (int)len; |
7038
76042a56ab85
commit https://github.com/vim/vim/commit/be83b73ddb2ee8297037166d243f72e3423a3ce3
Christian Brabandt <cb@256bit.org>
parents:
7036
diff
changeset
|
1810 continue; |
76042a56ab85
commit https://github.com/vim/vim/commit/be83b73ddb2ee8297037166d243f72e3423a3ce3
Christian Brabandt <cb@256bit.org>
parents:
7036
diff
changeset
|
1811 } |
76042a56ab85
commit https://github.com/vim/vim/commit/be83b73ddb2ee8297037166d243f72e3423a3ce3
Christian Brabandt <cb@256bit.org>
parents:
7036
diff
changeset
|
1812 #endif |
7 | 1813 copy_char = TRUE; |
22 | 1814 if ((*src == '$' |
1815 #ifdef VMS | |
1816 && at_start | |
1817 #endif | |
1818 ) | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8163
diff
changeset
|
1819 #if defined(MSWIN) |
7 | 1820 || *src == '%' |
1821 #endif | |
1822 || (*src == '~' && at_start)) | |
1823 { | |
1824 mustfree = FALSE; | |
1825 | |
1826 /* | |
1827 * The variable name is copied into dst temporarily, because it may | |
1828 * be a string in read-only memory and a NUL needs to be appended. | |
1829 */ | |
1830 if (*src != '~') /* environment var */ | |
1831 { | |
1832 tail = src + 1; | |
1833 var = dst; | |
1834 c = dstlen - 1; | |
1835 | |
1836 #ifdef UNIX | |
1837 /* Unix has ${var-name} type environment vars */ | |
1838 if (*tail == '{' && !vim_isIDc('{')) | |
1839 { | |
1840 tail++; /* ignore '{' */ | |
1841 while (c-- > 0 && *tail && *tail != '}') | |
1842 *var++ = *tail++; | |
1843 } | |
1844 else | |
1845 #endif | |
1846 { | |
1847 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail)) | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8163
diff
changeset
|
1848 #if defined(MSWIN) |
7 | 1849 || (*src == '%' && *tail != '%') |
1850 #endif | |
1851 )) | |
1852 *var++ = *tail++; | |
1853 } | |
1854 | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8163
diff
changeset
|
1855 #if defined(MSWIN) || defined(UNIX) |
7 | 1856 # ifdef UNIX |
1857 if (src[1] == '{' && *tail != '}') | |
1858 # else | |
1859 if (*src == '%' && *tail != '%') | |
1860 # endif | |
1861 var = NULL; | |
1862 else | |
1863 { | |
1864 # ifdef UNIX | |
1865 if (src[1] == '{') | |
1866 # else | |
1867 if (*src == '%') | |
1868 #endif | |
1869 ++tail; | |
1870 #endif | |
1871 *var = NUL; | |
1872 var = vim_getenv(dst, &mustfree); | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8163
diff
changeset
|
1873 #if defined(MSWIN) || defined(UNIX) |
7 | 1874 } |
1875 #endif | |
1876 } | |
1877 /* home directory */ | |
1878 else if ( src[1] == NUL | |
1879 || vim_ispathsep(src[1]) | |
1880 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL) | |
1881 { | |
1882 var = homedir; | |
1883 tail = src + 1; | |
1884 } | |
1885 else /* user directory */ | |
1886 { | |
1887 #if defined(UNIX) || (defined(VMS) && defined(USER_HOME)) | |
1888 /* | |
1889 * Copy ~user to dst[], so we can put a NUL after it. | |
1890 */ | |
1891 tail = src; | |
1892 var = dst; | |
1893 c = dstlen - 1; | |
1894 while ( c-- > 0 | |
1895 && *tail | |
1896 && vim_isfilec(*tail) | |
1897 && !vim_ispathsep(*tail)) | |
1898 *var++ = *tail++; | |
1899 *var = NUL; | |
1900 # ifdef UNIX | |
1901 /* | |
1902 * If the system supports getpwnam(), use it. | |
1903 * Otherwise, or if getpwnam() fails, the shell is used to | |
1904 * expand ~user. This is slower and may fail if the shell | |
1905 * does not support ~user (old versions of /bin/sh). | |
1906 */ | |
1907 # if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H) | |
1908 { | |
626 | 1909 /* Note: memory allocated by getpwnam() is never freed. |
1910 * Calling endpwent() apparently doesn't help. */ | |
10932
141fe140976c
patch 8.0.0355: using uninitialized memory when 'isfname' is empty
Christian Brabandt <cb@256bit.org>
parents:
10889
diff
changeset
|
1911 struct passwd *pw = (*dst == NUL) |
141fe140976c
patch 8.0.0355: using uninitialized memory when 'isfname' is empty
Christian Brabandt <cb@256bit.org>
parents:
10889
diff
changeset
|
1912 ? NULL : getpwnam((char *)dst + 1); |
141fe140976c
patch 8.0.0355: using uninitialized memory when 'isfname' is empty
Christian Brabandt <cb@256bit.org>
parents:
10889
diff
changeset
|
1913 |
141fe140976c
patch 8.0.0355: using uninitialized memory when 'isfname' is empty
Christian Brabandt <cb@256bit.org>
parents:
10889
diff
changeset
|
1914 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir; |
7 | 1915 } |
1916 if (var == NULL) | |
1917 # endif | |
1918 { | |
1919 expand_T xpc; | |
1920 | |
1921 ExpandInit(&xpc); | |
1922 xpc.xp_context = EXPAND_FILES; | |
1923 var = ExpandOne(&xpc, dst, NULL, | |
1924 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE); | |
1925 mustfree = TRUE; | |
1926 } | |
1927 | |
1928 # else /* !UNIX, thus VMS */ | |
1929 /* | |
1930 * USER_HOME is a comma-separated list of | |
1931 * directories to search for the user account in. | |
1932 */ | |
1933 { | |
1934 char_u test[MAXPATHL], paths[MAXPATHL]; | |
1935 char_u *path, *next_path, *ptr; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9225
diff
changeset
|
1936 stat_T st; |
7 | 1937 |
1938 STRCPY(paths, USER_HOME); | |
1939 next_path = paths; | |
1940 while (*next_path) | |
1941 { | |
1942 for (path = next_path; *next_path && *next_path != ','; | |
1943 next_path++); | |
1944 if (*next_path) | |
1945 *next_path++ = NUL; | |
1946 STRCPY(test, path); | |
1947 STRCAT(test, "/"); | |
1948 STRCAT(test, dst + 1); | |
1949 if (mch_stat(test, &st) == 0) | |
1950 { | |
1951 var = alloc(STRLEN(test) + 1); | |
1952 STRCPY(var, test); | |
1953 mustfree = TRUE; | |
1954 break; | |
1955 } | |
1956 } | |
1957 } | |
1958 # endif /* UNIX */ | |
1959 #else | |
1960 /* cannot expand user's home directory, so don't try */ | |
1961 var = NULL; | |
1962 tail = (char_u *)""; /* for gcc */ | |
1963 #endif /* UNIX || VMS */ | |
1964 } | |
1965 | |
1966 #ifdef BACKSLASH_IN_FILENAME | |
1967 /* If 'shellslash' is set change backslashes to forward slashes. | |
1968 * Can't use slash_adjust(), p_ssl may be set temporarily. */ | |
1969 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL) | |
1970 { | |
1971 char_u *p = vim_strsave(var); | |
1972 | |
1973 if (p != NULL) | |
1974 { | |
1975 if (mustfree) | |
1976 vim_free(var); | |
1977 var = p; | |
1978 mustfree = TRUE; | |
1979 forward_slash(var); | |
1980 } | |
1981 } | |
1982 #endif | |
1983 | |
1984 /* If "var" contains white space, escape it with a backslash. | |
1985 * Required for ":e ~/tt" when $HOME includes a space. */ | |
1986 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL) | |
1987 { | |
1988 char_u *p = vim_strsave_escaped(var, (char_u *)" \t"); | |
1989 | |
1990 if (p != NULL) | |
1991 { | |
1992 if (mustfree) | |
1993 vim_free(var); | |
1994 var = p; | |
1995 mustfree = TRUE; | |
1996 } | |
1997 } | |
1998 | |
1999 if (var != NULL && *var != NUL | |
2000 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen)) | |
2001 { | |
2002 STRCPY(dst, var); | |
2003 dstlen -= (int)STRLEN(var); | |
835 | 2004 c = (int)STRLEN(var); |
7 | 2005 /* if var[] ends in a path separator and tail[] starts |
2006 * with it, skip a character */ | |
39 | 2007 if (*var != NUL && after_pathsep(dst, dst + c) |
7 | 2008 #if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) |
2009 && dst[-1] != ':' | |
2010 #endif | |
2011 && vim_ispathsep(*tail)) | |
2012 ++tail; | |
39 | 2013 dst += c; |
7 | 2014 src = tail; |
2015 copy_char = FALSE; | |
2016 } | |
2017 if (mustfree) | |
2018 vim_free(var); | |
2019 } | |
2020 | |
2021 if (copy_char) /* copy at least one char */ | |
2022 { | |
2023 /* | |
1224 | 2024 * Recognize the start of a new name, for '~'. |
1408 | 2025 * Don't do this when "one" is TRUE, to avoid expanding "~" in |
2026 * ":edit foo ~ foo". | |
7 | 2027 */ |
2028 at_start = FALSE; | |
2029 if (src[0] == '\\' && src[1] != NUL) | |
2030 { | |
2031 *dst++ = *src++; | |
2032 --dstlen; | |
2033 } | |
1408 | 2034 else if ((src[0] == ' ' || src[0] == ',') && !one) |
7 | 2035 at_start = TRUE; |
12005
0496ea4c5c2e
patch 8.0.0883: invalid memory access with nonsensical script
Christian Brabandt <cb@256bit.org>
parents:
11989
diff
changeset
|
2036 if (dstlen > 0) |
0496ea4c5c2e
patch 8.0.0883: invalid memory access with nonsensical script
Christian Brabandt <cb@256bit.org>
parents:
11989
diff
changeset
|
2037 { |
0496ea4c5c2e
patch 8.0.0883: invalid memory access with nonsensical script
Christian Brabandt <cb@256bit.org>
parents:
11989
diff
changeset
|
2038 *dst++ = *src++; |
0496ea4c5c2e
patch 8.0.0883: invalid memory access with nonsensical script
Christian Brabandt <cb@256bit.org>
parents:
11989
diff
changeset
|
2039 --dstlen; |
0496ea4c5c2e
patch 8.0.0883: invalid memory access with nonsensical script
Christian Brabandt <cb@256bit.org>
parents:
11989
diff
changeset
|
2040 |
0496ea4c5c2e
patch 8.0.0883: invalid memory access with nonsensical script
Christian Brabandt <cb@256bit.org>
parents:
11989
diff
changeset
|
2041 if (startstr != NULL && src - startstr_len >= srcp |
0496ea4c5c2e
patch 8.0.0883: invalid memory access with nonsensical script
Christian Brabandt <cb@256bit.org>
parents:
11989
diff
changeset
|
2042 && STRNCMP(src - startstr_len, startstr, |
0496ea4c5c2e
patch 8.0.0883: invalid memory access with nonsensical script
Christian Brabandt <cb@256bit.org>
parents:
11989
diff
changeset
|
2043 startstr_len) == 0) |
0496ea4c5c2e
patch 8.0.0883: invalid memory access with nonsensical script
Christian Brabandt <cb@256bit.org>
parents:
11989
diff
changeset
|
2044 at_start = TRUE; |
0496ea4c5c2e
patch 8.0.0883: invalid memory access with nonsensical script
Christian Brabandt <cb@256bit.org>
parents:
11989
diff
changeset
|
2045 } |
0496ea4c5c2e
patch 8.0.0883: invalid memory access with nonsensical script
Christian Brabandt <cb@256bit.org>
parents:
11989
diff
changeset
|
2046 } |
0496ea4c5c2e
patch 8.0.0883: invalid memory access with nonsensical script
Christian Brabandt <cb@256bit.org>
parents:
11989
diff
changeset
|
2047 |
7 | 2048 } |
2049 *dst = NUL; | |
2050 } | |
2051 | |
2052 /* | |
2053 * Vim's version of getenv(). | |
2054 * Special handling of $HOME, $VIM and $VIMRUNTIME. | |
196 | 2055 * Also does ACP to 'enc' conversion for Win32. |
2786 | 2056 * "mustfree" is set to TRUE when returned is allocated, it must be |
2057 * initialized to FALSE by the caller. | |
7 | 2058 */ |
2059 char_u * | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2060 vim_getenv(char_u *name, int *mustfree) |
7 | 2061 { |
16172
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2062 char_u *p = NULL; |
7 | 2063 char_u *pend; |
2064 int vimruntime; | |
16172
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2065 #ifdef MSWIN |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2066 WCHAR *wn, *wp; |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2067 |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2068 // use "C:/" when $HOME is not set |
7 | 2069 if (STRCMP(name, "HOME") == 0) |
2070 return homedir; | |
16172
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2071 |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2072 // Use Wide function |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2073 wn = enc_to_utf16(name, NULL); |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2074 if (wn == NULL) |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2075 return NULL; |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2076 |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2077 wp = _wgetenv(wn); |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2078 vim_free(wn); |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2079 |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2080 if (wp != NULL && *wp == NUL) // empty is the same as not set |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2081 wp = NULL; |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2082 |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2083 if (wp != NULL) |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2084 { |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2085 p = utf16_to_enc(wp, NULL); |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2086 if (p == NULL) |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2087 return NULL; |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2088 |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2089 *mustfree = TRUE; |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2090 return p; |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2091 } |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2092 #else |
7 | 2093 p = mch_getenv(name); |
16172
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2094 if (p != NULL && *p == NUL) // empty is the same as not set |
7 | 2095 p = NULL; |
2096 | |
2097 if (p != NULL) | |
2098 return p; | |
16172
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2099 #endif |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2100 |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2101 // handling $VIMRUNTIME and $VIM is below, bail out if it's another name. |
7 | 2102 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0); |
2103 if (!vimruntime && STRCMP(name, "VIM") != 0) | |
2104 return NULL; | |
2105 | |
2106 /* | |
2107 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM. | |
2108 * Don't do this when default_vimruntime_dir is non-empty. | |
2109 */ | |
2110 if (vimruntime | |
2111 #ifdef HAVE_PATHDEF | |
2112 && *default_vimruntime_dir == NUL | |
2113 #endif | |
2114 ) | |
2115 { | |
16172
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2116 #ifdef MSWIN |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2117 // Use Wide function |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2118 wp = _wgetenv(L"VIM"); |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2119 if (wp != NULL && *wp == NUL) // empty is the same as not set |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2120 wp = NULL; |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2121 if (wp != NULL) |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2122 { |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2123 char_u *q = utf16_to_enc(wp, NULL); |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2124 if (q != NULL) |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2125 { |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2126 p = vim_version_dir(q); |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2127 *mustfree = TRUE; |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2128 if (p == NULL) |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2129 p = q; |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2130 } |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2131 } |
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2132 #else |
7 | 2133 p = mch_getenv((char_u *)"VIM"); |
16172
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2134 if (p != NULL && *p == NUL) // empty is the same as not set |
7 | 2135 p = NULL; |
2136 if (p != NULL) | |
2137 { | |
2138 p = vim_version_dir(p); | |
2139 if (p != NULL) | |
2140 *mustfree = TRUE; | |
2141 else | |
2142 p = mch_getenv((char_u *)"VIM"); | |
2143 } | |
16172
6b0836727cf3
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
2144 #endif |
7 | 2145 } |
2146 | |
2147 /* | |
2148 * When expanding $VIM or $VIMRUNTIME fails, try using: | |
2149 * - the directory name from 'helpfile' (unless it contains '$') | |
2150 * - the executable name from argv[0] | |
2151 */ | |
2152 if (p == NULL) | |
2153 { | |
2154 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL) | |
2155 p = p_hf; | |
2156 #ifdef USE_EXE_NAME | |
2157 /* | |
2158 * Use the name of the executable, obtained from argv[0]. | |
2159 */ | |
2160 else | |
2161 p = exe_name; | |
2162 #endif | |
2163 if (p != NULL) | |
2164 { | |
2165 /* remove the file name */ | |
2166 pend = gettail(p); | |
2167 | |
2168 /* remove "doc/" from 'helpfile', if present */ | |
2169 if (p == p_hf) | |
2170 pend = remove_tail(p, pend, (char_u *)"doc"); | |
2171 | |
2172 #ifdef USE_EXE_NAME | |
2173 # ifdef MACOS_X | |
768 | 2174 /* remove "MacOS" from exe_name and add "Resources/vim" */ |
7 | 2175 if (p == exe_name) |
2176 { | |
2177 char_u *pend1; | |
768 | 2178 char_u *pnew; |
2179 | |
2180 pend1 = remove_tail(p, pend, (char_u *)"MacOS"); | |
2181 if (pend1 != pend) | |
2182 { | |
16764
ef00b6bc186b
patch 8.1.1384: using "int" for alloc() often results in compiler warnings
Bram Moolenaar <Bram@vim.org>
parents:
16738
diff
changeset
|
2183 pnew = alloc(pend1 - p + 15); |
768 | 2184 if (pnew != NULL) |
2185 { | |
2186 STRNCPY(pnew, p, (pend1 - p)); | |
2187 STRCPY(pnew + (pend1 - p), "Resources/vim"); | |
2188 p = pnew; | |
2189 pend = p + STRLEN(p); | |
2190 } | |
2191 } | |
7 | 2192 } |
2193 # endif | |
2194 /* remove "src/" from exe_name, if present */ | |
2195 if (p == exe_name) | |
2196 pend = remove_tail(p, pend, (char_u *)"src"); | |
2197 #endif | |
2198 | |
2199 /* for $VIM, remove "runtime/" or "vim54/", if present */ | |
2200 if (!vimruntime) | |
2201 { | |
2202 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME); | |
2203 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT); | |
2204 } | |
2205 | |
2206 /* remove trailing path separator */ | |
39 | 2207 if (pend > p && after_pathsep(p, pend)) |
7 | 2208 --pend; |
2209 | |
768 | 2210 #ifdef MACOS_X |
2211 if (p == exe_name || p == p_hf) | |
2212 #endif | |
2213 /* check that the result is a directory name */ | |
2214 p = vim_strnsave(p, (int)(pend - p)); | |
7 | 2215 |
2216 if (p != NULL && !mch_isdir(p)) | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13188
diff
changeset
|
2217 VIM_CLEAR(p); |
7 | 2218 else |
2219 { | |
2220 #ifdef USE_EXE_NAME | |
2221 /* may add "/vim54" or "/runtime" if it exists */ | |
2222 if (vimruntime && (pend = vim_version_dir(p)) != NULL) | |
2223 { | |
2224 vim_free(p); | |
2225 p = pend; | |
2226 } | |
2227 #endif | |
2228 *mustfree = TRUE; | |
2229 } | |
2230 } | |
2231 } | |
2232 | |
2233 #ifdef HAVE_PATHDEF | |
2234 /* When there is a pathdef.c file we can use default_vim_dir and | |
2235 * default_vimruntime_dir */ | |
2236 if (p == NULL) | |
2237 { | |
2238 /* Only use default_vimruntime_dir when it is not empty */ | |
2239 if (vimruntime && *default_vimruntime_dir != NUL) | |
2240 { | |
2241 p = default_vimruntime_dir; | |
2242 *mustfree = FALSE; | |
2243 } | |
2244 else if (*default_vim_dir != NUL) | |
2245 { | |
2246 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL) | |
2247 *mustfree = TRUE; | |
2248 else | |
2249 { | |
2250 p = default_vim_dir; | |
2251 *mustfree = FALSE; | |
2252 } | |
2253 } | |
2254 } | |
2255 #endif | |
2256 | |
2257 /* | |
2258 * Set the environment variable, so that the new value can be found fast | |
2259 * next time, and others can also use it (e.g. Perl). | |
2260 */ | |
2261 if (p != NULL) | |
2262 { | |
2263 if (vimruntime) | |
2264 { | |
2265 vim_setenv((char_u *)"VIMRUNTIME", p); | |
2266 didset_vimruntime = TRUE; | |
2267 } | |
2268 else | |
2269 { | |
2270 vim_setenv((char_u *)"VIM", p); | |
2271 didset_vim = TRUE; | |
2272 } | |
2273 } | |
2274 return p; | |
2275 } | |
2276 | |
2277 /* | |
2278 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists. | |
2279 * Return NULL if not, return its name in allocated memory otherwise. | |
2280 */ | |
2281 static char_u * | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2282 vim_version_dir(char_u *vimdir) |
7 | 2283 { |
2284 char_u *p; | |
2285 | |
2286 if (vimdir == NULL || *vimdir == NUL) | |
2287 return NULL; | |
2288 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE); | |
2289 if (p != NULL && mch_isdir(p)) | |
2290 return p; | |
2291 vim_free(p); | |
2292 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE); | |
2293 if (p != NULL && mch_isdir(p)) | |
2294 return p; | |
2295 vim_free(p); | |
2296 return NULL; | |
2297 } | |
2298 | |
2299 /* | |
2300 * If the string between "p" and "pend" ends in "name/", return "pend" minus | |
2301 * the length of "name/". Otherwise return "pend". | |
2302 */ | |
2303 static char_u * | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2304 remove_tail(char_u *p, char_u *pend, char_u *name) |
7 | 2305 { |
2306 int len = (int)STRLEN(name) + 1; | |
2307 char_u *newend = pend - len; | |
2308 | |
2309 if (newend >= p | |
2310 && fnamencmp(newend, name, len - 1) == 0 | |
39 | 2311 && (newend == p || after_pathsep(p, newend))) |
7 | 2312 return newend; |
2313 return pend; | |
2314 } | |
2315 | |
15555
d89c5b339c2a
patch 8.1.0785: depending on the configuration some functions are unused
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
2316 #if defined(FEAT_EVAL) || defined(PROTO) |
13923
e4d5726e1678
patch 8.0.1832: cannot use :unlet for an environment variable
Christian Brabandt <cb@256bit.org>
parents:
13823
diff
changeset
|
2317 void |
e4d5726e1678
patch 8.0.1832: cannot use :unlet for an environment variable
Christian Brabandt <cb@256bit.org>
parents:
13823
diff
changeset
|
2318 vim_unsetenv(char_u *var) |
e4d5726e1678
patch 8.0.1832: cannot use :unlet for an environment variable
Christian Brabandt <cb@256bit.org>
parents:
13823
diff
changeset
|
2319 { |
e4d5726e1678
patch 8.0.1832: cannot use :unlet for an environment variable
Christian Brabandt <cb@256bit.org>
parents:
13823
diff
changeset
|
2320 #ifdef HAVE_UNSETENV |
e4d5726e1678
patch 8.0.1832: cannot use :unlet for an environment variable
Christian Brabandt <cb@256bit.org>
parents:
13823
diff
changeset
|
2321 unsetenv((char *)var); |
e4d5726e1678
patch 8.0.1832: cannot use :unlet for an environment variable
Christian Brabandt <cb@256bit.org>
parents:
13823
diff
changeset
|
2322 #else |
13942
a039c93f5ff7
patch 8.0.1841: HP-UX does not have setenv()
Christian Brabandt <cb@256bit.org>
parents:
13923
diff
changeset
|
2323 vim_setenv(var, (char_u *)""); |
13923
e4d5726e1678
patch 8.0.1832: cannot use :unlet for an environment variable
Christian Brabandt <cb@256bit.org>
parents:
13823
diff
changeset
|
2324 #endif |
e4d5726e1678
patch 8.0.1832: cannot use :unlet for an environment variable
Christian Brabandt <cb@256bit.org>
parents:
13823
diff
changeset
|
2325 } |
15555
d89c5b339c2a
patch 8.1.0785: depending on the configuration some functions are unused
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
2326 #endif |
13923
e4d5726e1678
patch 8.0.1832: cannot use :unlet for an environment variable
Christian Brabandt <cb@256bit.org>
parents:
13823
diff
changeset
|
2327 |
e4d5726e1678
patch 8.0.1832: cannot use :unlet for an environment variable
Christian Brabandt <cb@256bit.org>
parents:
13823
diff
changeset
|
2328 |
7 | 2329 /* |
2330 * Our portable version of setenv. | |
2331 */ | |
2332 void | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2333 vim_setenv(char_u *name, char_u *val) |
7 | 2334 { |
2335 #ifdef HAVE_SETENV | |
2336 mch_setenv((char *)name, (char *)val, 1); | |
2337 #else | |
2338 char_u *envbuf; | |
2339 | |
2340 /* | |
2341 * Putenv does not copy the string, it has to remain | |
2342 * valid. The allocated memory will never be freed. | |
2343 */ | |
16764
ef00b6bc186b
patch 8.1.1384: using "int" for alloc() often results in compiler warnings
Bram Moolenaar <Bram@vim.org>
parents:
16738
diff
changeset
|
2344 envbuf = alloc(STRLEN(name) + STRLEN(val) + 2); |
7 | 2345 if (envbuf != NULL) |
2346 { | |
2347 sprintf((char *)envbuf, "%s=%s", name, val); | |
2348 putenv((char *)envbuf); | |
2349 } | |
2350 #endif | |
3382 | 2351 #ifdef FEAT_GETTEXT |
2352 /* | |
2353 * When setting $VIMRUNTIME adjust the directory to find message | |
2354 * translations to $VIMRUNTIME/lang. | |
2355 */ | |
2356 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0) | |
2357 { | |
2358 char_u *buf = concat_str(val, (char_u *)"/lang"); | |
2359 | |
2360 if (buf != NULL) | |
2361 { | |
2362 bindtextdomain(VIMPACKAGE, (char *)buf); | |
2363 vim_free(buf); | |
2364 } | |
2365 } | |
2366 #endif | |
7 | 2367 } |
2368 | |
2369 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
2370 /* | |
2371 * Function given to ExpandGeneric() to obtain an environment variable name. | |
2372 */ | |
2373 char_u * | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2374 get_env_name( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2375 expand_T *xp UNUSED, |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2376 int idx) |
7 | 2377 { |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12690
diff
changeset
|
2378 # if defined(AMIGA) |
7 | 2379 /* |
12716
351cf7c67bbe
patch 8.0.1236: Mac features are confusing
Christian Brabandt <cb@256bit.org>
parents:
12690
diff
changeset
|
2380 * No environ[] on the Amiga. |
7 | 2381 */ |
2382 return NULL; | |
2383 # else | |
2384 # ifndef __WIN32__ | |
2385 /* Borland C++ 5.2 has this in a header file. */ | |
2386 extern char **environ; | |
2387 # endif | |
17 | 2388 # define ENVNAMELEN 100 |
2389 static char_u name[ENVNAMELEN]; | |
7 | 2390 char_u *str; |
2391 int n; | |
2392 | |
2393 str = (char_u *)environ[idx]; | |
2394 if (str == NULL) | |
2395 return NULL; | |
2396 | |
17 | 2397 for (n = 0; n < ENVNAMELEN - 1; ++n) |
7 | 2398 { |
2399 if (str[n] == '=' || str[n] == NUL) | |
2400 break; | |
2401 name[n] = str[n]; | |
2402 } | |
2403 name[n] = NUL; | |
2404 return name; | |
2405 # endif | |
2406 } | |
3744 | 2407 |
2408 /* | |
14698
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2409 * Add a user name to the list of users in ga_users. |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2410 * Do nothing if user name is NULL or empty. |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2411 */ |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2412 static void |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2413 add_user(char_u *user, int need_copy) |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2414 { |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2415 char_u *user_copy = (user != NULL && need_copy) |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2416 ? vim_strsave(user) : user; |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2417 |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2418 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL) |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2419 { |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2420 if (need_copy) |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2421 vim_free(user); |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2422 return; |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2423 } |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2424 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy; |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2425 } |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2426 |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2427 /* |
3744 | 2428 * Find all user names for user completion. |
2429 * Done only once and then cached. | |
2430 */ | |
2431 static void | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2432 init_users(void) |
4938
bcb84438bb5b
updated for version 7.3.1214
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
2433 { |
3744 | 2434 static int lazy_init_done = FALSE; |
2435 | |
2436 if (lazy_init_done) | |
2437 return; | |
2438 | |
2439 lazy_init_done = TRUE; | |
2440 ga_init2(&ga_users, sizeof(char_u *), 20); | |
2441 | |
2442 # if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H) | |
2443 { | |
2444 struct passwd* pw; | |
2445 | |
2446 setpwent(); | |
2447 while ((pw = getpwent()) != NULL) | |
14698
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2448 add_user((char_u *)pw->pw_name, TRUE); |
3744 | 2449 endpwent(); |
2450 } | |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15814
diff
changeset
|
2451 # elif defined(MSWIN) |
14133
352c2832d17f
patch 8.1.0084: user name completion does not work on MS-Windows
Christian Brabandt <cb@256bit.org>
parents:
13942
diff
changeset
|
2452 { |
352c2832d17f
patch 8.1.0084: user name completion does not work on MS-Windows
Christian Brabandt <cb@256bit.org>
parents:
13942
diff
changeset
|
2453 DWORD nusers = 0, ntotal = 0, i; |
352c2832d17f
patch 8.1.0084: user name completion does not work on MS-Windows
Christian Brabandt <cb@256bit.org>
parents:
13942
diff
changeset
|
2454 PUSER_INFO_0 uinfo; |
352c2832d17f
patch 8.1.0084: user name completion does not work on MS-Windows
Christian Brabandt <cb@256bit.org>
parents:
13942
diff
changeset
|
2455 |
352c2832d17f
patch 8.1.0084: user name completion does not work on MS-Windows
Christian Brabandt <cb@256bit.org>
parents:
13942
diff
changeset
|
2456 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH, |
352c2832d17f
patch 8.1.0084: user name completion does not work on MS-Windows
Christian Brabandt <cb@256bit.org>
parents:
13942
diff
changeset
|
2457 &nusers, &ntotal, NULL) == NERR_Success) |
352c2832d17f
patch 8.1.0084: user name completion does not work on MS-Windows
Christian Brabandt <cb@256bit.org>
parents:
13942
diff
changeset
|
2458 { |
352c2832d17f
patch 8.1.0084: user name completion does not work on MS-Windows
Christian Brabandt <cb@256bit.org>
parents:
13942
diff
changeset
|
2459 for (i = 0; i < nusers; i++) |
14698
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2460 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE); |
14133
352c2832d17f
patch 8.1.0084: user name completion does not work on MS-Windows
Christian Brabandt <cb@256bit.org>
parents:
13942
diff
changeset
|
2461 |
352c2832d17f
patch 8.1.0084: user name completion does not work on MS-Windows
Christian Brabandt <cb@256bit.org>
parents:
13942
diff
changeset
|
2462 NetApiBufferFree(uinfo); |
352c2832d17f
patch 8.1.0084: user name completion does not work on MS-Windows
Christian Brabandt <cb@256bit.org>
parents:
13942
diff
changeset
|
2463 } |
352c2832d17f
patch 8.1.0084: user name completion does not work on MS-Windows
Christian Brabandt <cb@256bit.org>
parents:
13942
diff
changeset
|
2464 } |
3744 | 2465 # endif |
14698
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2466 # if defined(HAVE_GETPWNAM) |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2467 { |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2468 char_u *user_env = mch_getenv((char_u *)"USER"); |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2469 |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2470 // The $USER environment variable may be a valid remote user name (NIS, |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2471 // LDAP) not already listed by getpwent(), as getpwent() only lists |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2472 // local user names. If $USER is not already listed, check whether it |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2473 // is a valid remote user name using getpwnam() and if it is, add it to |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2474 // the list of user names. |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2475 |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2476 if (user_env != NULL && *user_env != NUL) |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2477 { |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2478 int i; |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2479 |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2480 for (i = 0; i < ga_users.ga_len; i++) |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2481 { |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2482 char_u *local_user = ((char_u **)ga_users.ga_data)[i]; |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2483 |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2484 if (STRCMP(local_user, user_env) == 0) |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2485 break; |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2486 } |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2487 |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2488 if (i == ga_users.ga_len) |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2489 { |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2490 struct passwd *pw = getpwnam((char *)user_env); |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2491 |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2492 if (pw != NULL) |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2493 add_user((char_u *)pw->pw_name, TRUE); |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2494 } |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2495 } |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2496 } |
d2cfca5b178e
patch 8.1.0361: remote user not used for completion
Christian Brabandt <cb@256bit.org>
parents:
14629
diff
changeset
|
2497 # endif |
3744 | 2498 } |
2499 | |
2500 /* | |
2501 * Function given to ExpandGeneric() to obtain an user names. | |
2502 */ | |
2503 char_u* | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2504 get_users(expand_T *xp UNUSED, int idx) |
3744 | 2505 { |
2506 init_users(); | |
2507 if (idx < ga_users.ga_len) | |
2508 return ((char_u **)ga_users.ga_data)[idx]; | |
2509 return NULL; | |
2510 } | |
2511 | |
2512 /* | |
2513 * Check whether name matches a user name. Return: | |
2514 * 0 if name does not match any user name. | |
2515 * 1 if name partially matches the beginning of a user name. | |
2516 * 2 is name fully matches a user name. | |
2517 */ | |
14286
95030c543411
patch 8.1.0159: completion for user names does not work for a prefix.
Christian Brabandt <cb@256bit.org>
parents:
14175
diff
changeset
|
2518 int |
95030c543411
patch 8.1.0159: completion for user names does not work for a prefix.
Christian Brabandt <cb@256bit.org>
parents:
14175
diff
changeset
|
2519 match_user(char_u *name) |
3744 | 2520 { |
2521 int i; | |
2522 int n = (int)STRLEN(name); | |
2523 int result = 0; | |
2524 | |
2525 init_users(); | |
2526 for (i = 0; i < ga_users.ga_len; i++) | |
2527 { | |
2528 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0) | |
2529 return 2; /* full match */ | |
2530 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0) | |
2531 result = 1; /* partial match */ | |
2532 } | |
2533 return result; | |
2534 } | |
7 | 2535 #endif |
2536 | |
2537 /* | |
2538 * Replace home directory by "~" in each space or comma separated file name in | |
2539 * 'src'. | |
2540 * If anything fails (except when out of space) dst equals src. | |
2541 */ | |
2542 void | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2543 home_replace( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2544 buf_T *buf, /* when not NULL, check for help files */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2545 char_u *src, /* input file name */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2546 char_u *dst, /* where to put the result */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2547 int dstlen, /* maximum length of the result */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2548 int one) /* if TRUE, only replace one file name, include |
7 | 2549 spaces and commas in the file name. */ |
2550 { | |
2551 size_t dirlen = 0, envlen = 0; | |
2552 size_t len; | |
3598 | 2553 char_u *homedir_env, *homedir_env_orig; |
7 | 2554 char_u *p; |
2555 | |
2556 if (src == NULL) | |
2557 { | |
2558 *dst = NUL; | |
2559 return; | |
2560 } | |
2561 | |
2562 /* | |
2563 * If the file is a help file, remove the path completely. | |
2564 */ | |
2565 if (buf != NULL && buf->b_help) | |
2566 { | |
11989
0aae4df3ed8e
patch 8.0.0875: crash with weird command sequence
Christian Brabandt <cb@256bit.org>
parents:
11943
diff
changeset
|
2567 vim_snprintf((char *)dst, dstlen, "%s", gettail(src)); |
7 | 2568 return; |
2569 } | |
2570 | |
2571 /* | |
2572 * We check both the value of the $HOME environment variable and the | |
2573 * "real" home directory. | |
2574 */ | |
2575 if (homedir != NULL) | |
2576 dirlen = STRLEN(homedir); | |
2577 | |
2578 #ifdef VMS | |
3598 | 2579 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN"); |
7 | 2580 #else |
3598 | 2581 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME"); |
2582 #endif | |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15814
diff
changeset
|
2583 #ifdef MSWIN |
12265
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
2584 if (homedir_env == NULL) |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
2585 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE"); |
03e4be2e3d53
patch 8.0.1012: MS-Windows: problem with $HOME when is was set internally
Christian Brabandt <cb@256bit.org>
parents:
12238
diff
changeset
|
2586 #endif |
3658 | 2587 /* Empty is the same as not set. */ |
2588 if (homedir_env != NULL && *homedir_env == NUL) | |
2589 homedir_env = NULL; | |
2590 | |
4752
77ecab3bb207
updated for version 7.3.1123
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
2591 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) |
15308
d7a8f390f6d2
patch 8.1.0662: needlessly searching for tilde in string
Bram Moolenaar <Bram@vim.org>
parents:
15138
diff
changeset
|
2592 if (homedir_env != NULL && *homedir_env == '~') |
3598 | 2593 { |
2594 int usedlen = 0; | |
2595 int flen; | |
2596 char_u *fbuf = NULL; | |
2597 | |
2598 flen = (int)STRLEN(homedir_env); | |
14393
c62601adad69
patch 8.1.0211: expanding a file name "~" results in $HOME
Christian Brabandt <cb@256bit.org>
parents:
14286
diff
changeset
|
2599 (void)modify_fname((char_u *)":p", FALSE, &usedlen, |
3608 | 2600 &homedir_env, &fbuf, &flen); |
3598 | 2601 flen = (int)STRLEN(homedir_env); |
2602 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1])) | |
2603 /* Remove the trailing / that is added to a directory. */ | |
2604 homedir_env[flen - 1] = NUL; | |
2605 } | |
7 | 2606 #endif |
2607 | |
2608 if (homedir_env != NULL) | |
2609 envlen = STRLEN(homedir_env); | |
2610 | |
2611 if (!one) | |
2612 src = skipwhite(src); | |
2613 while (*src && dstlen > 0) | |
2614 { | |
2615 /* | |
2616 * Here we are at the beginning of a file name. | |
2617 * First, check to see if the beginning of the file name matches | |
2618 * $HOME or the "real" home directory. Check that there is a '/' | |
2619 * after the match (so that if e.g. the file is "/home/pieter/bla", | |
2620 * and the home directory is "/home/piet", the file does not end up | |
2621 * as "~er/bla" (which would seem to indicate the file "bla" in user | |
2622 * er's home directory)). | |
2623 */ | |
2624 p = homedir; | |
2625 len = dirlen; | |
2626 for (;;) | |
2627 { | |
2628 if ( len | |
2629 && fnamencmp(src, p, len) == 0 | |
2630 && (vim_ispathsep(src[len]) | |
2631 || (!one && (src[len] == ',' || src[len] == ' ')) | |
2632 || src[len] == NUL)) | |
2633 { | |
2634 src += len; | |
2635 if (--dstlen > 0) | |
2636 *dst++ = '~'; | |
2637 | |
2638 /* | |
2639 * If it's just the home directory, add "/". | |
2640 */ | |
2641 if (!vim_ispathsep(src[0]) && --dstlen > 0) | |
2642 *dst++ = '/'; | |
2643 break; | |
2644 } | |
2645 if (p == homedir_env) | |
2646 break; | |
2647 p = homedir_env; | |
2648 len = envlen; | |
2649 } | |
2650 | |
2651 /* if (!one) skip to separator: space or comma */ | |
2652 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0) | |
2653 *dst++ = *src++; | |
2654 /* skip separator */ | |
2655 while ((*src == ' ' || *src == ',') && --dstlen > 0) | |
2656 *dst++ = *src++; | |
2657 } | |
2658 /* if (dstlen == 0) out of space, what to do??? */ | |
2659 | |
2660 *dst = NUL; | |
3598 | 2661 |
2662 if (homedir_env != homedir_env_orig) | |
2663 vim_free(homedir_env); | |
7 | 2664 } |
2665 | |
2666 /* | |
2667 * Like home_replace, store the replaced string in allocated memory. | |
2668 * When something fails, NULL is returned. | |
2669 */ | |
2670 char_u * | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2671 home_replace_save( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2672 buf_T *buf, /* when not NULL, check for help files */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2673 char_u *src) /* input file name */ |
7 | 2674 { |
2675 char_u *dst; | |
2676 unsigned len; | |
2677 | |
2678 len = 3; /* space for "~/" and trailing NUL */ | |
2679 if (src != NULL) /* just in case */ | |
2680 len += (unsigned)STRLEN(src); | |
2681 dst = alloc(len); | |
2682 if (dst != NULL) | |
2683 home_replace(buf, src, dst, len, TRUE); | |
2684 return dst; | |
2685 } | |
2686 | |
2687 /* | |
2688 * Compare two file names and return: | |
2689 * FPC_SAME if they both exist and are the same file. | |
2690 * FPC_SAMEX if they both don't exist and have the same file name. | |
2691 * FPC_DIFF if they both exist and are different files. | |
2692 * FPC_NOTX if they both don't exist. | |
2693 * FPC_DIFFX if one of them doesn't exist. | |
16738
b52ea9c5f1db
patch 8.1.1371: cannot recover from a swap file
Bram Moolenaar <Bram@vim.org>
parents:
16714
diff
changeset
|
2694 * For the first name environment variables are expanded if "expandenv" is |
b52ea9c5f1db
patch 8.1.1371: cannot recover from a swap file
Bram Moolenaar <Bram@vim.org>
parents:
16714
diff
changeset
|
2695 * TRUE. |
7 | 2696 */ |
2697 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2698 fullpathcmp( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2699 char_u *s1, |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2700 char_u *s2, |
16738
b52ea9c5f1db
patch 8.1.1371: cannot recover from a swap file
Bram Moolenaar <Bram@vim.org>
parents:
16714
diff
changeset
|
2701 int checkname, // when both don't exist, check file names |
b52ea9c5f1db
patch 8.1.1371: cannot recover from a swap file
Bram Moolenaar <Bram@vim.org>
parents:
16714
diff
changeset
|
2702 int expandenv) |
7 | 2703 { |
2704 #ifdef UNIX | |
2705 char_u exp1[MAXPATHL]; | |
2706 char_u full1[MAXPATHL]; | |
2707 char_u full2[MAXPATHL]; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9225
diff
changeset
|
2708 stat_T st1, st2; |
7 | 2709 int r1, r2; |
2710 | |
16738
b52ea9c5f1db
patch 8.1.1371: cannot recover from a swap file
Bram Moolenaar <Bram@vim.org>
parents:
16714
diff
changeset
|
2711 if (expandenv) |
b52ea9c5f1db
patch 8.1.1371: cannot recover from a swap file
Bram Moolenaar <Bram@vim.org>
parents:
16714
diff
changeset
|
2712 expand_env(s1, exp1, MAXPATHL); |
b52ea9c5f1db
patch 8.1.1371: cannot recover from a swap file
Bram Moolenaar <Bram@vim.org>
parents:
16714
diff
changeset
|
2713 else |
b52ea9c5f1db
patch 8.1.1371: cannot recover from a swap file
Bram Moolenaar <Bram@vim.org>
parents:
16714
diff
changeset
|
2714 vim_strncpy(exp1, s1, MAXPATHL - 1); |
7 | 2715 r1 = mch_stat((char *)exp1, &st1); |
2716 r2 = mch_stat((char *)s2, &st2); | |
2717 if (r1 != 0 && r2 != 0) | |
2718 { | |
2719 /* if mch_stat() doesn't work, may compare the names */ | |
2720 if (checkname) | |
2721 { | |
2722 if (fnamecmp(exp1, s2) == 0) | |
2723 return FPC_SAMEX; | |
2724 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); | |
2725 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); | |
2726 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0) | |
2727 return FPC_SAMEX; | |
2728 } | |
2729 return FPC_NOTX; | |
2730 } | |
2731 if (r1 != 0 || r2 != 0) | |
2732 return FPC_DIFFX; | |
2733 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) | |
2734 return FPC_SAME; | |
2735 return FPC_DIFF; | |
2736 #else | |
2737 char_u *exp1; /* expanded s1 */ | |
2738 char_u *full1; /* full path of s1 */ | |
2739 char_u *full2; /* full path of s2 */ | |
2740 int retval = FPC_DIFF; | |
2741 int r1, r2; | |
2742 | |
2743 /* allocate one buffer to store three paths (alloc()/free() is slow!) */ | |
2744 if ((exp1 = alloc(MAXPATHL * 3)) != NULL) | |
2745 { | |
2746 full1 = exp1 + MAXPATHL; | |
2747 full2 = full1 + MAXPATHL; | |
2748 | |
16738
b52ea9c5f1db
patch 8.1.1371: cannot recover from a swap file
Bram Moolenaar <Bram@vim.org>
parents:
16714
diff
changeset
|
2749 if (expandenv) |
b52ea9c5f1db
patch 8.1.1371: cannot recover from a swap file
Bram Moolenaar <Bram@vim.org>
parents:
16714
diff
changeset
|
2750 expand_env(s1, exp1, MAXPATHL); |
b52ea9c5f1db
patch 8.1.1371: cannot recover from a swap file
Bram Moolenaar <Bram@vim.org>
parents:
16714
diff
changeset
|
2751 else |
b52ea9c5f1db
patch 8.1.1371: cannot recover from a swap file
Bram Moolenaar <Bram@vim.org>
parents:
16714
diff
changeset
|
2752 vim_strncpy(exp1, s1, MAXPATHL - 1); |
7 | 2753 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); |
2754 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); | |
2755 | |
2756 /* If vim_FullName() fails, the file probably doesn't exist. */ | |
2757 if (r1 != OK && r2 != OK) | |
2758 { | |
2759 if (checkname && fnamecmp(exp1, s2) == 0) | |
2760 retval = FPC_SAMEX; | |
2761 else | |
2762 retval = FPC_NOTX; | |
2763 } | |
2764 else if (r1 != OK || r2 != OK) | |
2765 retval = FPC_DIFFX; | |
2766 else if (fnamecmp(full1, full2)) | |
2767 retval = FPC_DIFF; | |
2768 else | |
2769 retval = FPC_SAME; | |
2770 vim_free(exp1); | |
2771 } | |
2772 return retval; | |
2773 #endif | |
2774 } | |
2775 | |
2776 /* | |
10 | 2777 * Get the tail of a path: the file name. |
2549
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
2778 * When the path ends in a path separator the tail is the NUL after it. |
10 | 2779 * Fail safe: never returns NULL. |
7 | 2780 */ |
2781 char_u * | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2782 gettail(char_u *fname) |
7 | 2783 { |
2784 char_u *p1, *p2; | |
2785 | |
2786 if (fname == NULL) | |
2787 return (char_u *)""; | |
5432 | 2788 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */ |
2789 { | |
2790 if (vim_ispathsep_nocolon(*p2)) | |
7 | 2791 p1 = p2 + 1; |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
2792 MB_PTR_ADV(p2); |
7 | 2793 } |
2794 return p1; | |
2795 } | |
2796 | |
2797 /* | |
39 | 2798 * Get pointer to tail of "fname", including path separators. Putting a NUL |
2799 * here leaves the directory name. Takes care of "c:/" and "//". | |
2800 * Always returns a valid pointer. | |
2801 */ | |
2802 char_u * | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2803 gettail_sep(char_u *fname) |
39 | 2804 { |
2805 char_u *p; | |
2806 char_u *t; | |
2807 | |
2808 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */ | |
2809 t = gettail(fname); | |
2810 while (t > p && after_pathsep(fname, t)) | |
2811 --t; | |
2812 #ifdef VMS | |
2813 /* path separator is part of the path */ | |
2814 ++t; | |
2815 #endif | |
2816 return t; | |
2817 } | |
2818 | |
2819 /* | |
7 | 2820 * get the next path component (just after the next path separator). |
2821 */ | |
2822 char_u * | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2823 getnextcomp(char_u *fname) |
7 | 2824 { |
2825 while (*fname && !vim_ispathsep(*fname)) | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
2826 MB_PTR_ADV(fname); |
7 | 2827 if (*fname) |
2828 ++fname; | |
2829 return fname; | |
2830 } | |
2831 | |
2832 /* | |
2833 * Get a pointer to one character past the head of a path name. | |
2834 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head. | |
2835 * If there is no head, path is returned. | |
2836 */ | |
2837 char_u * | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2838 get_past_head(char_u *path) |
7 | 2839 { |
2840 char_u *retval; | |
2841 | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8163
diff
changeset
|
2842 #if defined(MSWIN) |
7 | 2843 /* may skip "c:" */ |
2844 if (isalpha(path[0]) && path[1] == ':') | |
2845 retval = path + 2; | |
2846 else | |
2847 retval = path; | |
2848 #else | |
2849 # if defined(AMIGA) | |
2850 /* may skip "label:" */ | |
2851 retval = vim_strchr(path, ':'); | |
2852 if (retval == NULL) | |
2853 retval = path; | |
2854 # else /* Unix */ | |
2855 retval = path; | |
2856 # endif | |
2857 #endif | |
2858 | |
2859 while (vim_ispathsep(*retval)) | |
2860 ++retval; | |
2861 | |
2862 return retval; | |
2863 } | |
2864 | |
2865 /* | |
5432 | 2866 * Return TRUE if 'c' is a path separator. |
2867 * Note that for MS-Windows this includes the colon. | |
7 | 2868 */ |
2869 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2870 vim_ispathsep(int c) |
7 | 2871 { |
2823 | 2872 #ifdef UNIX |
7 | 2873 return (c == '/'); /* UNIX has ':' inside file names */ |
2823 | 2874 #else |
2875 # ifdef BACKSLASH_IN_FILENAME | |
7 | 2876 return (c == ':' || c == '/' || c == '\\'); |
2823 | 2877 # else |
2878 # ifdef VMS | |
7 | 2879 /* server"user passwd"::device:[full.path.name]fname.extension;version" */ |
2880 return (c == ':' || c == '[' || c == ']' || c == '/' | |
2881 || c == '<' || c == '>' || c == '"' ); | |
2823 | 2882 # else |
7 | 2883 return (c == ':' || c == '/'); |
2823 | 2884 # endif /* VMS */ |
7 | 2885 # endif |
2823 | 2886 #endif |
7 | 2887 } |
2888 | |
5432 | 2889 /* |
2890 * Like vim_ispathsep(c), but exclude the colon for MS-Windows. | |
2891 */ | |
2892 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2893 vim_ispathsep_nocolon(int c) |
5432 | 2894 { |
2895 return vim_ispathsep(c) | |
2896 #ifdef BACKSLASH_IN_FILENAME | |
2897 && c != ':' | |
2898 #endif | |
2899 ; | |
2900 } | |
2901 | |
819 | 2902 /* |
2903 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname" | |
2904 * It's done in-place. | |
2905 */ | |
2906 void | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2907 shorten_dir(char_u *str) |
819 | 2908 { |
2909 char_u *tail, *s, *d; | |
2910 int skip = FALSE; | |
2911 | |
2912 tail = gettail(str); | |
2913 d = str; | |
2914 for (s = str; ; ++s) | |
2915 { | |
2916 if (s >= tail) /* copy the whole tail */ | |
2917 { | |
2918 *d++ = *s; | |
2919 if (*s == NUL) | |
2920 break; | |
2921 } | |
2922 else if (vim_ispathsep(*s)) /* copy '/' and next char */ | |
2923 { | |
2924 *d++ = *s; | |
2925 skip = FALSE; | |
2926 } | |
2927 else if (!skip) | |
2928 { | |
2929 *d++ = *s; /* copy next char */ | |
2930 if (*s != '~' && *s != '.') /* and leading "~" and "." */ | |
2931 skip = TRUE; | |
2932 if (has_mbyte) | |
2933 { | |
2934 int l = mb_ptr2len(s); | |
2935 | |
2936 while (--l > 0) | |
927 | 2937 *d++ = *++s; |
819 | 2938 } |
2939 } | |
2940 } | |
2941 } | |
2942 | |
594 | 2943 /* |
2944 * Return TRUE if the directory of "fname" exists, FALSE otherwise. | |
2945 * Also returns TRUE if there is no directory name. | |
2946 * "fname" must be writable!. | |
2947 */ | |
2948 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2949 dir_of_file_exists(char_u *fname) |
594 | 2950 { |
2951 char_u *p; | |
2952 int c; | |
2953 int retval; | |
2954 | |
2955 p = gettail_sep(fname); | |
2956 if (p == fname) | |
2957 return TRUE; | |
2958 c = *p; | |
2959 *p = NUL; | |
2960 retval = mch_isdir(fname); | |
2961 *p = c; | |
2962 return retval; | |
2963 } | |
2964 | |
4242 | 2965 /* |
2966 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally | |
2967 * and deal with 'fileignorecase'. | |
7 | 2968 */ |
2969 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2970 vim_fnamecmp(char_u *x, char_u *y) |
7 | 2971 { |
4242 | 2972 #ifdef BACKSLASH_IN_FILENAME |
7 | 2973 return vim_fnamencmp(x, y, MAXPATHL); |
4242 | 2974 #else |
2975 if (p_fic) | |
2976 return MB_STRICMP(x, y); | |
2977 return STRCMP(x, y); | |
2978 #endif | |
7 | 2979 } |
2980 | |
2981 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
2982 vim_fnamencmp(char_u *x, char_u *y, size_t len) |
7 | 2983 { |
4242 | 2984 #ifdef BACKSLASH_IN_FILENAME |
4246 | 2985 char_u *px = x; |
2986 char_u *py = y; | |
2987 int cx = NUL; | |
2988 int cy = NUL; | |
2989 | |
2990 while (len > 0) | |
2991 { | |
2992 cx = PTR2CHAR(px); | |
2993 cy = PTR2CHAR(py); | |
2994 if (cx == NUL || cy == NUL | |
2995 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy) | |
2996 && !(cx == '/' && cy == '\\') | |
2997 && !(cx == '\\' && cy == '/'))) | |
7 | 2998 break; |
4246 | 2999 len -= MB_PTR2LEN(px); |
3000 px += MB_PTR2LEN(px); | |
3001 py += MB_PTR2LEN(py); | |
7 | 3002 } |
3003 if (len == 0) | |
3004 return 0; | |
4246 | 3005 return (cx - cy); |
4242 | 3006 #else |
3007 if (p_fic) | |
3008 return MB_STRNICMP(x, y, len); | |
3009 return STRNCMP(x, y, len); | |
3010 #endif | |
3011 } | |
7 | 3012 |
3013 /* | |
3014 * Concatenate file names fname1 and fname2 into allocated memory. | |
1224 | 3015 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary. |
7 | 3016 */ |
3017 char_u * | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3018 concat_fnames(char_u *fname1, char_u *fname2, int sep) |
7 | 3019 { |
3020 char_u *dest; | |
3021 | |
16764
ef00b6bc186b
patch 8.1.1384: using "int" for alloc() often results in compiler warnings
Bram Moolenaar <Bram@vim.org>
parents:
16738
diff
changeset
|
3022 dest = alloc(STRLEN(fname1) + STRLEN(fname2) + 3); |
7 | 3023 if (dest != NULL) |
3024 { | |
3025 STRCPY(dest, fname1); | |
3026 if (sep) | |
3027 add_pathsep(dest); | |
3028 STRCAT(dest, fname2); | |
3029 } | |
3030 return dest; | |
3031 } | |
3032 | |
115 | 3033 /* |
3034 * Concatenate two strings and return the result in allocated memory. | |
3035 * Returns NULL when out of memory. | |
3036 */ | |
3037 char_u * | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3038 concat_str(char_u *str1, char_u *str2) |
115 | 3039 { |
3040 char_u *dest; | |
3041 size_t l = STRLEN(str1); | |
3042 | |
16764
ef00b6bc186b
patch 8.1.1384: using "int" for alloc() often results in compiler warnings
Bram Moolenaar <Bram@vim.org>
parents:
16738
diff
changeset
|
3043 dest = alloc(l + STRLEN(str2) + 1L); |
115 | 3044 if (dest != NULL) |
3045 { | |
3046 STRCPY(dest, str1); | |
3047 STRCPY(dest + l, str2); | |
3048 } | |
3049 return dest; | |
3050 } | |
3051 | |
7 | 3052 /* |
3053 * Add a path separator to a file name, unless it already ends in a path | |
3054 * separator. | |
3055 */ | |
3056 void | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3057 add_pathsep(char_u *p) |
7 | 3058 { |
39 | 3059 if (*p != NUL && !after_pathsep(p, p + STRLEN(p))) |
7 | 3060 STRCAT(p, PATHSEPSTR); |
3061 } | |
3062 | |
3063 /* | |
3064 * FullName_save - Make an allocated copy of a full file name. | |
3065 * Returns NULL when out of memory. | |
3066 */ | |
3067 char_u * | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3068 FullName_save( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3069 char_u *fname, |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3070 int force) /* force expansion, even when it already looks |
3584 | 3071 * like a full path name */ |
7 | 3072 { |
3073 char_u *buf; | |
3074 char_u *new_fname = NULL; | |
3075 | |
3076 if (fname == NULL) | |
3077 return NULL; | |
3078 | |
16764
ef00b6bc186b
patch 8.1.1384: using "int" for alloc() often results in compiler warnings
Bram Moolenaar <Bram@vim.org>
parents:
16738
diff
changeset
|
3079 buf = alloc(MAXPATHL); |
7 | 3080 if (buf != NULL) |
3081 { | |
3082 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL) | |
3083 new_fname = vim_strsave(buf); | |
3084 else | |
3085 new_fname = vim_strsave(fname); | |
3086 vim_free(buf); | |
3087 } | |
3088 return new_fname; | |
3089 } | |
3090 | |
3091 void | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3092 prepare_to_exit(void) |
7 | 3093 { |
39 | 3094 #if defined(SIGHUP) && defined(SIG_IGN) |
3095 /* Ignore SIGHUP, because a dropped connection causes a read error, which | |
3096 * makes Vim exit and then handling SIGHUP causes various reentrance | |
3097 * problems. */ | |
36 | 3098 signal(SIGHUP, SIG_IGN); |
3099 #endif | |
3100 | |
7 | 3101 #ifdef FEAT_GUI |
3102 if (gui.in_use) | |
3103 { | |
3104 gui.dying = TRUE; | |
3105 out_trash(); /* trash any pending output */ | |
3106 } | |
3107 else | |
3108 #endif | |
3109 { | |
3110 windgoto((int)Rows - 1, 0); | |
3111 | |
3112 /* | |
3113 * Switch terminal mode back now, so messages end up on the "normal" | |
3114 * screen (if there are two screens). | |
3115 */ | |
3116 settmode(TMODE_COOK); | |
10264
c036c0f636d5
commit https://github.com/vim/vim/commit/cea912af725c54f4727a0565e31661f6b29c6bb1
Christian Brabandt <cb@256bit.org>
parents:
10180
diff
changeset
|
3117 stoptermcap(); |
7 | 3118 out_flush(); |
3119 } | |
3120 } | |
3121 | |
3122 /* | |
3123 * Preserve files and exit. | |
3124 * When called IObuff must contain a message. | |
5338 | 3125 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe |
3126 * functions, such as allocating memory. | |
7 | 3127 */ |
3128 void | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3129 preserve_exit(void) |
7 | 3130 { |
3131 buf_T *buf; | |
3132 | |
3133 prepare_to_exit(); | |
3134 | |
625 | 3135 /* Setting this will prevent free() calls. That avoids calling free() |
3136 * recursively when free() was invoked with a bad pointer. */ | |
3137 really_exiting = TRUE; | |
3138 | |
7 | 3139 out_str(IObuff); |
3140 screen_start(); /* don't know where cursor is now */ | |
3141 out_flush(); | |
3142 | |
3143 ml_close_notmod(); /* close all not-modified buffers */ | |
3144 | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9536
diff
changeset
|
3145 FOR_ALL_BUFFERS(buf) |
7 | 3146 { |
3147 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL) | |
3148 { | |
5338 | 3149 OUT_STR("Vim: preserving files...\n"); |
7 | 3150 screen_start(); /* don't know where cursor is now */ |
3151 out_flush(); | |
3152 ml_sync_all(FALSE, FALSE); /* preserve all swap files */ | |
3153 break; | |
3154 } | |
3155 } | |
3156 | |
3157 ml_close_all(FALSE); /* close all memfiles, without deleting */ | |
3158 | |
5338 | 3159 OUT_STR("Vim: Finished.\n"); |
7 | 3160 |
3161 getout(1); | |
3162 } | |
3163 | |
3164 /* | |
3165 * return TRUE if "fname" exists. | |
3166 */ | |
3167 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3168 vim_fexists(char_u *fname) |
7 | 3169 { |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9225
diff
changeset
|
3170 stat_T st; |
7 | 3171 |
3172 if (mch_stat((char *)fname, &st)) | |
3173 return FALSE; | |
3174 return TRUE; | |
3175 } | |
3176 | |
3177 /* | |
3178 * Check for CTRL-C pressed, but only once in a while. | |
3179 * Should be used instead of ui_breakcheck() for functions that check for | |
3180 * each line in the file. Calling ui_breakcheck() each time takes too much | |
3181 * time, because it can be a system call. | |
3182 */ | |
3183 | |
3184 #ifndef BREAKCHECK_SKIP | |
17452
f12745505a23
patch 8.1.1724: too much overhead checking for CTRL-C while processing text
Bram Moolenaar <Bram@vim.org>
parents:
17018
diff
changeset
|
3185 # define BREAKCHECK_SKIP 1000 |
7 | 3186 #endif |
3187 | |
3188 static int breakcheck_count = 0; | |
3189 | |
3190 void | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3191 line_breakcheck(void) |
7 | 3192 { |
3193 if (++breakcheck_count >= BREAKCHECK_SKIP) | |
3194 { | |
3195 breakcheck_count = 0; | |
3196 ui_breakcheck(); | |
3197 } | |
3198 } | |
3199 | |
3200 /* | |
3201 * Like line_breakcheck() but check 10 times less often. | |
3202 */ | |
3203 void | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3204 fast_breakcheck(void) |
7 | 3205 { |
3206 if (++breakcheck_count >= BREAKCHECK_SKIP * 10) | |
3207 { | |
3208 breakcheck_count = 0; | |
3209 ui_breakcheck(); | |
3210 } | |
3211 } | |
3212 | |
3213 /* | |
2016 | 3214 * Invoke expand_wildcards() for one pattern. |
3215 * Expand items like "%:h" before the expansion. | |
3216 * Returns OK or FAIL. | |
3217 */ | |
3218 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3219 expand_wildcards_eval( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3220 char_u **pat, /* pointer to input pattern */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3221 int *num_file, /* resulting number of files */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3222 char_u ***file, /* array of resulting files */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3223 int flags) /* EW_DIR, etc. */ |
2016 | 3224 { |
3225 int ret = FAIL; | |
3226 char_u *eval_pat = NULL; | |
3227 char_u *exp_pat = *pat; | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15420
diff
changeset
|
3228 char *ignored_msg; |
2016 | 3229 int usedlen; |
3230 | |
3231 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<') | |
3232 { | |
3233 ++emsg_off; | |
3234 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen, | |
3235 NULL, &ignored_msg, NULL); | |
3236 --emsg_off; | |
3237 if (eval_pat != NULL) | |
3238 exp_pat = concat_str(eval_pat, exp_pat + usedlen); | |
3239 } | |
3240 | |
3241 if (exp_pat != NULL) | |
3242 ret = expand_wildcards(1, &exp_pat, num_file, file, flags); | |
3243 | |
3244 if (eval_pat != NULL) | |
3245 { | |
3246 vim_free(exp_pat); | |
3247 vim_free(eval_pat); | |
3248 } | |
3249 | |
3250 return ret; | |
3251 } | |
3252 | |
3253 /* | |
7 | 3254 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching |
3255 * 'wildignore'. | |
7119
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3256 * Returns OK or FAIL. When FAIL then "num_files" won't be set. |
7 | 3257 */ |
3258 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3259 expand_wildcards( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3260 int num_pat, /* number of input patterns */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3261 char_u **pat, /* array of input patterns */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3262 int *num_files, /* resulting number of files */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3263 char_u ***files, /* array of resulting files */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3264 int flags) /* EW_DIR, etc. */ |
7 | 3265 { |
3266 int retval; | |
3267 int i, j; | |
3268 char_u *p; | |
3269 int non_suf_match; /* number without matching suffix */ | |
3270 | |
7119
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3271 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags); |
7 | 3272 |
3273 /* When keeping all matches, return here */ | |
2354
f4440cdd59ae
Fixed: crash with ":find " completion, using uninitialized count.
Bram Moolenaar <bram@vim.org>
parents:
2328
diff
changeset
|
3274 if ((flags & EW_KEEPALL) || retval == FAIL) |
7 | 3275 return retval; |
3276 | |
3277 #ifdef FEAT_WILDIGN | |
3278 /* | |
3279 * Remove names that match 'wildignore'. | |
3280 */ | |
3281 if (*p_wig) | |
3282 { | |
3283 char_u *ffname; | |
3284 | |
7119
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3285 /* check all files in (*files)[] */ |
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3286 for (i = 0; i < *num_files; ++i) |
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3287 { |
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3288 ffname = FullName_save((*files)[i], FALSE); |
7 | 3289 if (ffname == NULL) /* out of memory */ |
3290 break; | |
3291 # ifdef VMS | |
3292 vms_remove_version(ffname); | |
3293 # endif | |
7119
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3294 if (match_file_list(p_wig, (*files)[i], ffname)) |
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3295 { |
10932
141fe140976c
patch 8.0.0355: using uninitialized memory when 'isfname' is empty
Christian Brabandt <cb@256bit.org>
parents:
10889
diff
changeset
|
3296 /* remove this matching file from the list */ |
7119
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3297 vim_free((*files)[i]); |
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3298 for (j = i; j + 1 < *num_files; ++j) |
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3299 (*files)[j] = (*files)[j + 1]; |
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3300 --*num_files; |
7 | 3301 --i; |
3302 } | |
3303 vim_free(ffname); | |
3304 } | |
7119
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3305 |
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3306 /* If the number of matches is now zero, we fail. */ |
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3307 if (*num_files == 0) |
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3308 { |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13188
diff
changeset
|
3309 VIM_CLEAR(*files); |
7119
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3310 return FAIL; |
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3311 } |
7 | 3312 } |
3313 #endif | |
3314 | |
3315 /* | |
3316 * Move the names where 'suffixes' match to the end. | |
3317 */ | |
7119
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3318 if (*num_files > 1) |
7 | 3319 { |
3320 non_suf_match = 0; | |
7119
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3321 for (i = 0; i < *num_files; ++i) |
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3322 { |
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3323 if (!match_suffix((*files)[i])) |
7 | 3324 { |
3325 /* | |
3326 * Move the name without matching suffix to the front | |
3327 * of the list. | |
3328 */ | |
7119
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3329 p = (*files)[i]; |
7 | 3330 for (j = i; j > non_suf_match; --j) |
7119
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3331 (*files)[j] = (*files)[j - 1]; |
6bc1695b7f11
commit https://github.com/vim/vim/commit/7b256fe7445b46929f660ea74e9090418f857696
Christian Brabandt <cb@256bit.org>
parents:
7066
diff
changeset
|
3332 (*files)[non_suf_match++] = p; |
7 | 3333 } |
3334 } | |
3335 } | |
3336 | |
3337 return retval; | |
3338 } | |
3339 | |
3340 /* | |
3341 * Return TRUE if "fname" matches with an entry in 'suffixes'. | |
3342 */ | |
3343 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3344 match_suffix(char_u *fname) |
7 | 3345 { |
3346 int fnamelen, setsuflen; | |
3347 char_u *setsuf; | |
3348 #define MAXSUFLEN 30 /* maximum length of a file suffix */ | |
3349 char_u suf_buf[MAXSUFLEN]; | |
3350 | |
3351 fnamelen = (int)STRLEN(fname); | |
3352 setsuflen = 0; | |
3353 for (setsuf = p_su; *setsuf; ) | |
3354 { | |
3355 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,"); | |
1937 | 3356 if (setsuflen == 0) |
3357 { | |
3358 char_u *tail = gettail(fname); | |
3359 | |
3360 /* empty entry: match name without a '.' */ | |
3361 if (vim_strchr(tail, '.') == NULL) | |
3362 { | |
3363 setsuflen = 1; | |
3364 break; | |
3365 } | |
3366 } | |
3367 else | |
3368 { | |
3369 if (fnamelen >= setsuflen | |
3370 && fnamencmp(suf_buf, fname + fnamelen - setsuflen, | |
3371 (size_t)setsuflen) == 0) | |
3372 break; | |
3373 setsuflen = 0; | |
3374 } | |
7 | 3375 } |
3376 return (setsuflen != 0); | |
3377 } | |
3378 | |
3379 #if !defined(NO_EXPANDPATH) || defined(PROTO) | |
3380 | |
3381 # ifdef VIM_BACKTICK | |
7803
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7641
diff
changeset
|
3382 static int vim_backtick(char_u *p); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7641
diff
changeset
|
3383 static int expand_backtick(garray_T *gap, char_u *pat, int flags); |
7 | 3384 # endif |
3385 | |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15814
diff
changeset
|
3386 # if defined(MSWIN) |
7 | 3387 /* |
3388 * File name expansion code for MS-DOS, Win16 and Win32. It's here because | |
3389 * it's shared between these systems. | |
3390 */ | |
3391 | |
3392 /* | |
3393 * comparison function for qsort in dos_expandpath() | |
3394 */ | |
16606
7e733046db1d
patch 8.1.1306: Borland support is outdated and doesn't work
Bram Moolenaar <Bram@vim.org>
parents:
16277
diff
changeset
|
3395 static int |
7 | 3396 pstrcmp(const void *a, const void *b) |
3397 { | |
39 | 3398 return (pathcmp(*(char **)a, *(char **)b, -1)); |
7 | 3399 } |
3400 | |
3401 /* | |
445 | 3402 * Recursively expand one path component into all matching files and/or |
3403 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. | |
7 | 3404 * Return the number of matches found. |
3405 * "path" has backslashes before chars that are not to be expanded, starting | |
3406 * at "path[wildoff]". | |
445 | 3407 * Return the number of matches found. |
3408 * NOTE: much of this is identical to unix_expandpath(), keep in sync! | |
7 | 3409 */ |
3410 static int | |
3411 dos_expandpath( | |
3412 garray_T *gap, | |
3413 char_u *path, | |
3414 int wildoff, | |
445 | 3415 int flags, /* EW_* flags */ |
1224 | 3416 int didstar) /* expanded "**" once already */ |
445 | 3417 { |
3418 char_u *buf; | |
3419 char_u *path_end; | |
3420 char_u *p, *s, *e; | |
3421 int start_len = gap->ga_len; | |
3422 char_u *pat; | |
3423 regmatch_T regmatch; | |
3424 int starts_with_dot; | |
3425 int matches; | |
3426 int len; | |
3427 int starstar = FALSE; | |
16196
973070a30381
patch 8.1.1103: MS-Windows: old API calls are no longer needed
Bram Moolenaar <Bram@vim.org>
parents:
16172
diff
changeset
|
3428 static int stardepth = 0; // depth for "**" expansion |
973070a30381
patch 8.1.1103: MS-Windows: old API calls are no longer needed
Bram Moolenaar <Bram@vim.org>
parents:
16172
diff
changeset
|
3429 HANDLE hFind = INVALID_HANDLE_VALUE; |
7 | 3430 WIN32_FIND_DATAW wfb; |
16196
973070a30381
patch 8.1.1103: MS-Windows: old API calls are no longer needed
Bram Moolenaar <Bram@vim.org>
parents:
16172
diff
changeset
|
3431 WCHAR *wn = NULL; // UCS-2 name, NULL when not used. |
7 | 3432 char_u *matchname; |
445 | 3433 int ok; |
3434 | |
3435 /* Expanding "**" may take a long time, check for CTRL-C. */ | |
3436 if (stardepth > 0) | |
3437 { | |
3438 ui_breakcheck(); | |
3439 if (got_int) | |
3440 return 0; | |
3441 } | |
7 | 3442 |
15597
536dd2bc5ac9
patch 8.1.0806: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
3443 // Make room for file name. When doing encoding conversion the actual |
536dd2bc5ac9
patch 8.1.0806: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15555
diff
changeset
|
3444 // length may be quite a bit longer, thus use the maximum possible length. |
16782
fc58fee685e2
patch 8.1.1393: unnecessary type casts
Bram Moolenaar <Bram@vim.org>
parents:
16764
diff
changeset
|
3445 buf = alloc(MAXPATHL); |
7 | 3446 if (buf == NULL) |
3447 return 0; | |
3448 | |
3449 /* | |
3450 * Find the first part in the path name that contains a wildcard or a ~1. | |
3451 * Copy it into buf, including the preceding characters. | |
3452 */ | |
3453 p = buf; | |
3454 s = buf; | |
3455 e = NULL; | |
3456 path_end = path; | |
3457 while (*path_end != NUL) | |
3458 { | |
3459 /* May ignore a wildcard that has a backslash before it; it will | |
3460 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ | |
3461 if (path_end >= path + wildoff && rem_backslash(path_end)) | |
3462 *p++ = *path_end++; | |
3463 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/') | |
3464 { | |
3465 if (e != NULL) | |
3466 break; | |
3467 s = p + 1; | |
3468 } | |
3469 else if (path_end >= path + wildoff | |
3470 && vim_strchr((char_u *)"*?[~", *path_end) != NULL) | |
3471 e = p; | |
3472 if (has_mbyte) | |
3473 { | |
474 | 3474 len = (*mb_ptr2len)(path_end); |
7 | 3475 STRNCPY(p, path_end, len); |
3476 p += len; | |
3477 path_end += len; | |
3478 } | |
3479 else | |
3480 *p++ = *path_end++; | |
3481 } | |
3482 e = p; | |
3483 *e = NUL; | |
3484 | |
3485 /* now we have one wildcard component between s and e */ | |
3486 /* Remove backslashes between "wildoff" and the start of the wildcard | |
3487 * component. */ | |
3488 for (p = buf + wildoff; p < s; ++p) | |
3489 if (rem_backslash(p)) | |
3490 { | |
1624 | 3491 STRMOVE(p, p + 1); |
7 | 3492 --e; |
3493 --s; | |
3494 } | |
3495 | |
445 | 3496 /* Check for "**" between "s" and "e". */ |
3497 for (p = s; p < e; ++p) | |
3498 if (p[0] == '*' && p[1] == '*') | |
3499 starstar = TRUE; | |
3500 | |
7635
1506f86b120f
commit https://github.com/vim/vim/commit/d82103ed8534a1207742e9666ac7ef1e47dda12d
Christian Brabandt <cb@256bit.org>
parents:
7633
diff
changeset
|
3501 starts_with_dot = *s == '.'; |
7 | 3502 pat = file_pat_to_reg_pat(s, e, NULL, FALSE); |
3503 if (pat == NULL) | |
3504 { | |
3505 vim_free(buf); | |
3506 return 0; | |
3507 } | |
3508 | |
3509 /* compile the regexp into a program */ | |
3261 | 3510 if (flags & (EW_NOERROR | EW_NOTWILD)) |
2966 | 3511 ++emsg_silent; |
7 | 3512 regmatch.rm_ic = TRUE; /* Always ignore case */ |
3513 regmatch.regprog = vim_regcomp(pat, RE_MAGIC); | |
3261 | 3514 if (flags & (EW_NOERROR | EW_NOTWILD)) |
2966 | 3515 --emsg_silent; |
7 | 3516 vim_free(pat); |
3517 | |
3261 | 3518 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
7 | 3519 { |
3520 vim_free(buf); | |
3521 return 0; | |
3522 } | |
3523 | |
3524 /* remember the pattern or file name being looked for */ | |
3525 matchname = vim_strsave(s); | |
3526 | |
445 | 3527 /* If "**" is by itself, this is the first time we encounter it and more |
3528 * is following then find matches without any directory. */ | |
3529 if (!didstar && stardepth < 100 && starstar && e - s == 2 | |
3530 && *path_end == '/') | |
3531 { | |
3532 STRCPY(s, path_end + 1); | |
3533 ++stardepth; | |
3534 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE); | |
3535 --stardepth; | |
3536 } | |
3537 | |
7 | 3538 /* Scan all files in the directory with "dir/ *.*" */ |
3539 STRCPY(s, "*.*"); | |
16196
973070a30381
patch 8.1.1103: MS-Windows: old API calls are no longer needed
Bram Moolenaar <Bram@vim.org>
parents:
16172
diff
changeset
|
3540 wn = enc_to_utf16(buf, NULL); |
973070a30381
patch 8.1.1103: MS-Windows: old API calls are no longer needed
Bram Moolenaar <Bram@vim.org>
parents:
16172
diff
changeset
|
3541 if (wn != NULL) |
973070a30381
patch 8.1.1103: MS-Windows: old API calls are no longer needed
Bram Moolenaar <Bram@vim.org>
parents:
16172
diff
changeset
|
3542 hFind = FindFirstFileW(wn, &wfb); |
7 | 3543 ok = (hFind != INVALID_HANDLE_VALUE); |
3544 | |
3545 while (ok) | |
3546 { | |
16196
973070a30381
patch 8.1.1103: MS-Windows: old API calls are no longer needed
Bram Moolenaar <Bram@vim.org>
parents:
16172
diff
changeset
|
3547 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here |
16231
0761a4c111a7
patch 8.1.1120: cannot easily get directory entry matches
Bram Moolenaar <Bram@vim.org>
parents:
16196
diff
changeset
|
3548 if (p == NULL) |
0761a4c111a7
patch 8.1.1120: cannot easily get directory entry matches
Bram Moolenaar <Bram@vim.org>
parents:
16196
diff
changeset
|
3549 break; // out of memory |
0761a4c111a7
patch 8.1.1120: cannot easily get directory entry matches
Bram Moolenaar <Bram@vim.org>
parents:
16196
diff
changeset
|
3550 |
16196
973070a30381
patch 8.1.1103: MS-Windows: old API calls are no longer needed
Bram Moolenaar <Bram@vim.org>
parents:
16172
diff
changeset
|
3551 // Ignore entries starting with a dot, unless when asked for. Accept |
973070a30381
patch 8.1.1103: MS-Windows: old API calls are no longer needed
Bram Moolenaar <Bram@vim.org>
parents:
16172
diff
changeset
|
3552 // all entries found with "matchname". |
7635
1506f86b120f
commit https://github.com/vim/vim/commit/d82103ed8534a1207742e9666ac7ef1e47dda12d
Christian Brabandt <cb@256bit.org>
parents:
7633
diff
changeset
|
3553 if ((p[0] != '.' || starts_with_dot |
1506f86b120f
commit https://github.com/vim/vim/commit/d82103ed8534a1207742e9666ac7ef1e47dda12d
Christian Brabandt <cb@256bit.org>
parents:
7633
diff
changeset
|
3554 || ((flags & EW_DODOT) |
1506f86b120f
commit https://github.com/vim/vim/commit/d82103ed8534a1207742e9666ac7ef1e47dda12d
Christian Brabandt <cb@256bit.org>
parents:
7633
diff
changeset
|
3555 && p[1] != NUL && (p[1] != '.' || p[2] != NUL))) |
7 | 3556 && (matchname == NULL |
3261 | 3557 || (regmatch.regprog != NULL |
3558 && vim_regexec(®match, p, (colnr_T)0)) | |
2984 | 3559 || ((flags & EW_NOTWILD) |
3560 && fnamencmp(path + (s - buf), p, e - s) == 0))) | |
7 | 3561 { |
3562 STRCPY(s, p); | |
3563 len = (int)STRLEN(buf); | |
445 | 3564 |
3565 if (starstar && stardepth < 100) | |
3566 { | |
3567 /* For "**" in the pattern first go deeper in the tree to | |
3568 * find matches. */ | |
3569 STRCPY(buf + len, "/**"); | |
3570 STRCPY(buf + len + 3, path_end); | |
3571 ++stardepth; | |
3572 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE); | |
3573 --stardepth; | |
3574 } | |
3575 | |
7 | 3576 STRCPY(buf + len, path_end); |
3577 if (mch_has_exp_wildcard(path_end)) | |
3578 { | |
3579 /* need to expand another component of the path */ | |
3580 /* remove backslashes for the remaining components only */ | |
445 | 3581 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE); |
7 | 3582 } |
3583 else | |
3584 { | |
3585 /* no more wildcards, check if there is a match */ | |
3586 /* remove backslashes for the remaining components only */ | |
3587 if (*path_end != 0) | |
3588 backslash_halve(buf + len + 1); | |
3589 if (mch_getperm(buf) >= 0) /* add existing file */ | |
3590 addfile(gap, buf, flags); | |
3591 } | |
3592 } | |
3593 | |
16196
973070a30381
patch 8.1.1103: MS-Windows: old API calls are no longer needed
Bram Moolenaar <Bram@vim.org>
parents:
16172
diff
changeset
|
3594 vim_free(p); |
973070a30381
patch 8.1.1103: MS-Windows: old API calls are no longer needed
Bram Moolenaar <Bram@vim.org>
parents:
16172
diff
changeset
|
3595 ok = FindNextFileW(hFind, &wfb); |
7 | 3596 |
3597 /* If no more matches and no match was used, try expanding the name | |
3598 * itself. Finds the long name of a short filename. */ | |
3599 if (!ok && matchname != NULL && gap->ga_len == start_len) | |
3600 { | |
3601 STRCPY(s, matchname); | |
3602 FindClose(hFind); | |
16196
973070a30381
patch 8.1.1103: MS-Windows: old API calls are no longer needed
Bram Moolenaar <Bram@vim.org>
parents:
16172
diff
changeset
|
3603 vim_free(wn); |
973070a30381
patch 8.1.1103: MS-Windows: old API calls are no longer needed
Bram Moolenaar <Bram@vim.org>
parents:
16172
diff
changeset
|
3604 wn = enc_to_utf16(buf, NULL); |
7 | 3605 if (wn != NULL) |
16196
973070a30381
patch 8.1.1103: MS-Windows: old API calls are no longer needed
Bram Moolenaar <Bram@vim.org>
parents:
16172
diff
changeset
|
3606 hFind = FindFirstFileW(wn, &wfb); |
973070a30381
patch 8.1.1103: MS-Windows: old API calls are no longer needed
Bram Moolenaar <Bram@vim.org>
parents:
16172
diff
changeset
|
3607 else |
973070a30381
patch 8.1.1103: MS-Windows: old API calls are no longer needed
Bram Moolenaar <Bram@vim.org>
parents:
16172
diff
changeset
|
3608 hFind = INVALID_HANDLE_VALUE; |
7 | 3609 ok = (hFind != INVALID_HANDLE_VALUE); |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13188
diff
changeset
|
3610 VIM_CLEAR(matchname); |
7 | 3611 } |
3612 } | |
3613 | |
3614 FindClose(hFind); | |
3615 vim_free(wn); | |
3616 vim_free(buf); | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4752
diff
changeset
|
3617 vim_regfree(regmatch.regprog); |
7 | 3618 vim_free(matchname); |
3619 | |
3620 matches = gap->ga_len - start_len; | |
3621 if (matches > 0) | |
3622 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches, | |
3623 sizeof(char_u *), pstrcmp); | |
3624 return matches; | |
3625 } | |
3626 | |
3627 int | |
3628 mch_expandpath( | |
3629 garray_T *gap, | |
3630 char_u *path, | |
3631 int flags) /* EW_* flags */ | |
3632 { | |
445 | 3633 return dos_expandpath(gap, path, 0, flags, FALSE); |
7 | 3634 } |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15814
diff
changeset
|
3635 # endif // MSWIN |
7 | 3636 |
445 | 3637 #if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \ |
3638 || defined(PROTO) | |
3639 /* | |
3640 * Unix style wildcard expansion code. | |
3641 * It's here because it's used both for Unix and Mac. | |
3642 */ | |
3643 static int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3644 pstrcmp(const void *a, const void *b) |
445 | 3645 { |
3646 return (pathcmp(*(char **)a, *(char **)b, -1)); | |
3647 } | |
3648 | |
3649 /* | |
3650 * Recursively expand one path component into all matching files and/or | |
3651 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. | |
3652 * "path" has backslashes before chars that are not to be expanded, starting | |
3653 * at "path + wildoff". | |
3654 * Return the number of matches found. | |
3655 * NOTE: much of this is identical to dos_expandpath(), keep in sync! | |
3656 */ | |
3657 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3658 unix_expandpath( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3659 garray_T *gap, |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3660 char_u *path, |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3661 int wildoff, |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3662 int flags, /* EW_* flags */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3663 int didstar) /* expanded "**" once already */ |
445 | 3664 { |
3665 char_u *buf; | |
3666 char_u *path_end; | |
3667 char_u *p, *s, *e; | |
3668 int start_len = gap->ga_len; | |
3669 char_u *pat; | |
3670 regmatch_T regmatch; | |
3671 int starts_with_dot; | |
3672 int matches; | |
3673 int len; | |
3674 int starstar = FALSE; | |
3675 static int stardepth = 0; /* depth for "**" expansion */ | |
3676 | |
3677 DIR *dirp; | |
3678 struct dirent *dp; | |
3679 | |
3680 /* Expanding "**" may take a long time, check for CTRL-C. */ | |
3681 if (stardepth > 0) | |
3682 { | |
3683 ui_breakcheck(); | |
3684 if (got_int) | |
3685 return 0; | |
3686 } | |
3687 | |
3688 /* make room for file name */ | |
16782
fc58fee685e2
patch 8.1.1393: unnecessary type casts
Bram Moolenaar <Bram@vim.org>
parents:
16764
diff
changeset
|
3689 buf = alloc(STRLEN(path) + BASENAMELEN + 5); |
445 | 3690 if (buf == NULL) |
3691 return 0; | |
3692 | |
3693 /* | |
3694 * Find the first part in the path name that contains a wildcard. | |
3505 | 3695 * When EW_ICASE is set every letter is considered to be a wildcard. |
445 | 3696 * Copy it into "buf", including the preceding characters. |
3697 */ | |
3698 p = buf; | |
3699 s = buf; | |
3700 e = NULL; | |
3701 path_end = path; | |
3702 while (*path_end != NUL) | |
3703 { | |
3704 /* May ignore a wildcard that has a backslash before it; it will | |
3705 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ | |
3706 if (path_end >= path + wildoff && rem_backslash(path_end)) | |
3707 *p++ = *path_end++; | |
3708 else if (*path_end == '/') | |
3709 { | |
3710 if (e != NULL) | |
3711 break; | |
3712 s = p + 1; | |
3713 } | |
3714 else if (path_end >= path + wildoff | |
3505 | 3715 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL |
4242 | 3716 || (!p_fic && (flags & EW_ICASE) |
3717 && isalpha(PTR2CHAR(path_end))))) | |
445 | 3718 e = p; |
3719 if (has_mbyte) | |
3720 { | |
474 | 3721 len = (*mb_ptr2len)(path_end); |
445 | 3722 STRNCPY(p, path_end, len); |
3723 p += len; | |
3724 path_end += len; | |
3725 } | |
3726 else | |
3727 *p++ = *path_end++; | |
3728 } | |
3729 e = p; | |
3730 *e = NUL; | |
3731 | |
2984 | 3732 /* Now we have one wildcard component between "s" and "e". */ |
445 | 3733 /* Remove backslashes between "wildoff" and the start of the wildcard |
3734 * component. */ | |
3735 for (p = buf + wildoff; p < s; ++p) | |
3736 if (rem_backslash(p)) | |
3737 { | |
1624 | 3738 STRMOVE(p, p + 1); |
445 | 3739 --e; |
3740 --s; | |
3741 } | |
3742 | |
3743 /* Check for "**" between "s" and "e". */ | |
3744 for (p = s; p < e; ++p) | |
3745 if (p[0] == '*' && p[1] == '*') | |
3746 starstar = TRUE; | |
3747 | |
3748 /* convert the file pattern to a regexp pattern */ | |
7635
1506f86b120f
commit https://github.com/vim/vim/commit/d82103ed8534a1207742e9666ac7ef1e47dda12d
Christian Brabandt <cb@256bit.org>
parents:
7633
diff
changeset
|
3749 starts_with_dot = *s == '.'; |
445 | 3750 pat = file_pat_to_reg_pat(s, e, NULL, FALSE); |
3751 if (pat == NULL) | |
3752 { | |
3753 vim_free(buf); | |
3754 return 0; | |
3755 } | |
3756 | |
3757 /* compile the regexp into a program */ | |
2652 | 3758 if (flags & EW_ICASE) |
3759 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */ | |
3760 else | |
4242 | 3761 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */ |
3261 | 3762 if (flags & (EW_NOERROR | EW_NOTWILD)) |
3763 ++emsg_silent; | |
445 | 3764 regmatch.regprog = vim_regcomp(pat, RE_MAGIC); |
3261 | 3765 if (flags & (EW_NOERROR | EW_NOTWILD)) |
3766 --emsg_silent; | |
445 | 3767 vim_free(pat); |
3768 | |
3261 | 3769 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
445 | 3770 { |
3771 vim_free(buf); | |
3772 return 0; | |
3773 } | |
3774 | |
3775 /* If "**" is by itself, this is the first time we encounter it and more | |
3776 * is following then find matches without any directory. */ | |
3777 if (!didstar && stardepth < 100 && starstar && e - s == 2 | |
3778 && *path_end == '/') | |
3779 { | |
3780 STRCPY(s, path_end + 1); | |
3781 ++stardepth; | |
3782 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE); | |
3783 --stardepth; | |
3784 } | |
3785 | |
3786 /* open the directory for scanning */ | |
3787 *s = NUL; | |
3788 dirp = opendir(*buf == NUL ? "." : (char *)buf); | |
3789 | |
3790 /* Find all matching entries */ | |
3791 if (dirp != NULL) | |
3792 { | |
3793 for (;;) | |
3794 { | |
3795 dp = readdir(dirp); | |
3796 if (dp == NULL) | |
3797 break; | |
7635
1506f86b120f
commit https://github.com/vim/vim/commit/d82103ed8534a1207742e9666ac7ef1e47dda12d
Christian Brabandt <cb@256bit.org>
parents:
7633
diff
changeset
|
3798 if ((dp->d_name[0] != '.' || starts_with_dot |
1506f86b120f
commit https://github.com/vim/vim/commit/d82103ed8534a1207742e9666ac7ef1e47dda12d
Christian Brabandt <cb@256bit.org>
parents:
7633
diff
changeset
|
3799 || ((flags & EW_DODOT) |
1506f86b120f
commit https://github.com/vim/vim/commit/d82103ed8534a1207742e9666ac7ef1e47dda12d
Christian Brabandt <cb@256bit.org>
parents:
7633
diff
changeset
|
3800 && dp->d_name[1] != NUL |
1506f86b120f
commit https://github.com/vim/vim/commit/d82103ed8534a1207742e9666ac7ef1e47dda12d
Christian Brabandt <cb@256bit.org>
parents:
7633
diff
changeset
|
3801 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL))) |
3261 | 3802 && ((regmatch.regprog != NULL && vim_regexec(®match, |
3803 (char_u *)dp->d_name, (colnr_T)0)) | |
2984 | 3804 || ((flags & EW_NOTWILD) |
3805 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0))) | |
445 | 3806 { |
3807 STRCPY(s, dp->d_name); | |
3808 len = STRLEN(buf); | |
3809 | |
3810 if (starstar && stardepth < 100) | |
3811 { | |
3812 /* For "**" in the pattern first go deeper in the tree to | |
3813 * find matches. */ | |
3814 STRCPY(buf + len, "/**"); | |
3815 STRCPY(buf + len + 3, path_end); | |
3816 ++stardepth; | |
3817 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE); | |
3818 --stardepth; | |
3819 } | |
3820 | |
3821 STRCPY(buf + len, path_end); | |
3822 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */ | |
3823 { | |
3824 /* need to expand another component of the path */ | |
3825 /* remove backslashes for the remaining components only */ | |
3826 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE); | |
3827 } | |
3828 else | |
3829 { | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9225
diff
changeset
|
3830 stat_T sb; |
6663 | 3831 |
445 | 3832 /* no more wildcards, check if there is a match */ |
3833 /* remove backslashes for the remaining components only */ | |
3834 if (*path_end != NUL) | |
3835 backslash_halve(buf + len + 1); | |
6663 | 3836 /* add existing file or symbolic link */ |
6665 | 3837 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0 |
6663 | 3838 : mch_getperm(buf) >= 0) |
445 | 3839 { |
768 | 3840 #ifdef MACOS_CONVERT |
445 | 3841 size_t precomp_len = STRLEN(buf)+1; |
3842 char_u *precomp_buf = | |
3843 mac_precompose_path(buf, precomp_len, &precomp_len); | |
768 | 3844 |
445 | 3845 if (precomp_buf) |
3846 { | |
3847 mch_memmove(buf, precomp_buf, precomp_len); | |
3848 vim_free(precomp_buf); | |
3849 } | |
3850 #endif | |
3851 addfile(gap, buf, flags); | |
3852 } | |
3853 } | |
3854 } | |
3855 } | |
3856 | |
3857 closedir(dirp); | |
3858 } | |
3859 | |
3860 vim_free(buf); | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4752
diff
changeset
|
3861 vim_regfree(regmatch.regprog); |
445 | 3862 |
3863 matches = gap->ga_len - start_len; | |
3864 if (matches > 0) | |
3865 qsort(((char_u **)gap->ga_data) + start_len, matches, | |
3866 sizeof(char_u *), pstrcmp); | |
3867 return matches; | |
3868 } | |
3869 #endif | |
3870 | |
2433
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
3871 #if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO) |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
3872 /* |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
3873 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
3874 * list of file names in allocated memory. |
2433
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
3875 */ |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
3876 void |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3877 remove_duplicates(garray_T *gap) |
2433
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
3878 { |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
3879 int i; |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
3880 int j; |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
3881 char_u **fnames = (char_u **)gap->ga_data; |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
3882 |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
3883 sort_strings(fnames, gap->ga_len); |
2433
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
3884 for (i = gap->ga_len - 1; i > 0; --i) |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
3885 if (fnamecmp(fnames[i - 1], fnames[i]) == 0) |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
3886 { |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
3887 vim_free(fnames[i]); |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
3888 for (j = i + 1; j < gap->ga_len; ++j) |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
3889 fnames[j - 1] = fnames[j]; |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
3890 --gap->ga_len; |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
3891 } |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
3892 } |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
3893 #endif |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
3894 |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3895 /* |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3896 * Return TRUE if "p" contains what looks like an environment variable. |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3897 * Allowing for escaping. |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3898 */ |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3899 static int |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3900 has_env_var(char_u *p) |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3901 { |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
3902 for ( ; *p; MB_PTR_ADV(p)) |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3903 { |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3904 if (*p == '\\' && p[1] != NUL) |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3905 ++p; |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3906 else if (vim_strchr((char_u *) |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
8163
diff
changeset
|
3907 #if defined(MSWIN) |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3908 "$%" |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3909 #else |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3910 "$" |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3911 #endif |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3912 , *p) != NULL) |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3913 return TRUE; |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3914 } |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3915 return FALSE; |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3916 } |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3917 |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3918 #ifdef SPECIAL_WILDCHAR |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3919 /* |
10932
141fe140976c
patch 8.0.0355: using uninitialized memory when 'isfname' is empty
Christian Brabandt <cb@256bit.org>
parents:
10889
diff
changeset
|
3920 * Return TRUE if "p" contains a special wildcard character, one that Vim |
141fe140976c
patch 8.0.0355: using uninitialized memory when 'isfname' is empty
Christian Brabandt <cb@256bit.org>
parents:
10889
diff
changeset
|
3921 * cannot expand, requires using a shell. |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3922 */ |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3923 static int |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3924 has_special_wildchar(char_u *p) |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3925 { |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
3926 for ( ; *p; MB_PTR_ADV(p)) |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3927 { |
16277
5ef25fa57f71
patch 8.1.1143: may pass weird strings to file name expansion
Bram Moolenaar <Bram@vim.org>
parents:
16231
diff
changeset
|
3928 // Disallow line break characters. |
5ef25fa57f71
patch 8.1.1143: may pass weird strings to file name expansion
Bram Moolenaar <Bram@vim.org>
parents:
16231
diff
changeset
|
3929 if (*p == '\r' || *p == '\n') |
5ef25fa57f71
patch 8.1.1143: may pass weird strings to file name expansion
Bram Moolenaar <Bram@vim.org>
parents:
16231
diff
changeset
|
3930 break; |
5ef25fa57f71
patch 8.1.1143: may pass weird strings to file name expansion
Bram Moolenaar <Bram@vim.org>
parents:
16231
diff
changeset
|
3931 // Allow for escaping. |
5ef25fa57f71
patch 8.1.1143: may pass weird strings to file name expansion
Bram Moolenaar <Bram@vim.org>
parents:
16231
diff
changeset
|
3932 if (*p == '\\' && p[1] != NUL && p[1] != '\r' && p[1] != '\n') |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3933 ++p; |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3934 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL) |
16277
5ef25fa57f71
patch 8.1.1143: may pass weird strings to file name expansion
Bram Moolenaar <Bram@vim.org>
parents:
16231
diff
changeset
|
3935 { |
5ef25fa57f71
patch 8.1.1143: may pass weird strings to file name expansion
Bram Moolenaar <Bram@vim.org>
parents:
16231
diff
changeset
|
3936 // A { must be followed by a matching }. |
5ef25fa57f71
patch 8.1.1143: may pass weird strings to file name expansion
Bram Moolenaar <Bram@vim.org>
parents:
16231
diff
changeset
|
3937 if (*p == '{' && vim_strchr(p, '}') == NULL) |
5ef25fa57f71
patch 8.1.1143: may pass weird strings to file name expansion
Bram Moolenaar <Bram@vim.org>
parents:
16231
diff
changeset
|
3938 continue; |
5ef25fa57f71
patch 8.1.1143: may pass weird strings to file name expansion
Bram Moolenaar <Bram@vim.org>
parents:
16231
diff
changeset
|
3939 // A quote and backtick must be followed by another one. |
5ef25fa57f71
patch 8.1.1143: may pass weird strings to file name expansion
Bram Moolenaar <Bram@vim.org>
parents:
16231
diff
changeset
|
3940 if ((*p == '`' || *p == '\'') && vim_strchr(p, *p) == NULL) |
5ef25fa57f71
patch 8.1.1143: may pass weird strings to file name expansion
Bram Moolenaar <Bram@vim.org>
parents:
16231
diff
changeset
|
3941 continue; |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3942 return TRUE; |
16277
5ef25fa57f71
patch 8.1.1143: may pass weird strings to file name expansion
Bram Moolenaar <Bram@vim.org>
parents:
16231
diff
changeset
|
3943 } |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3944 } |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3945 return FALSE; |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3946 } |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3947 #endif |
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
3948 |
7 | 3949 /* |
3950 * Generic wildcard expansion code. | |
3951 * | |
3952 * Characters in "pat" that should not be expanded must be preceded with a | |
3953 * backslash. E.g., "/path\ with\ spaces/my\*star*" | |
3954 * | |
3955 * Return FAIL when no single file was found. In this case "num_file" is not | |
3956 * set, and "file" may contain an error message. | |
3957 * Return OK when some files found. "num_file" is set to the number of | |
3958 * matches, "file" to the array of matches. Call FreeWild() later. | |
3959 */ | |
3960 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3961 gen_expand_wildcards( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3962 int num_pat, /* number of input patterns */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3963 char_u **pat, /* array of input patterns */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3964 int *num_file, /* resulting number of files */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3965 char_u ***file, /* array of resulting files */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
3966 int flags) /* EW_* flags */ |
7 | 3967 { |
3968 int i; | |
3969 garray_T ga; | |
3970 char_u *p; | |
3971 static int recursive = FALSE; | |
3972 int add_pat; | |
7036
5f00b8d7148f
commit https://github.com/vim/vim/commit/3f188935ec4db5117c4a64cc3f71219175624745
Christian Brabandt <cb@256bit.org>
parents:
6971
diff
changeset
|
3973 int retval = OK; |
2568
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
3974 #if defined(FEAT_SEARCHPATH) |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
3975 int did_expand_in_path = FALSE; |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
3976 #endif |
7 | 3977 |
3978 /* | |
3979 * expand_env() is called to expand things like "~user". If this fails, | |
3980 * it calls ExpandOne(), which brings us back here. In this case, always | |
3981 * call the machine specific expansion function, if possible. Otherwise, | |
3982 * return FAIL. | |
3983 */ | |
3984 if (recursive) | |
3985 #ifdef SPECIAL_WILDCHAR | |
3986 return mch_expand_wildcards(num_pat, pat, num_file, file, flags); | |
3987 #else | |
3988 return FAIL; | |
3989 #endif | |
3990 | |
3991 #ifdef SPECIAL_WILDCHAR | |
3992 /* | |
3993 * If there are any special wildcard characters which we cannot handle | |
3994 * here, call machine specific function for all the expansion. This | |
3995 * avoids starting the shell for each argument separately. | |
3996 * For `=expr` do use the internal function. | |
3997 */ | |
3998 for (i = 0; i < num_pat; i++) | |
3999 { | |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
4000 if (has_special_wildchar(pat[i]) |
7 | 4001 # ifdef VIM_BACKTICK |
4002 && !(vim_backtick(pat[i]) && pat[i][1] == '=') | |
4003 # endif | |
4004 ) | |
4005 return mch_expand_wildcards(num_pat, pat, num_file, file, flags); | |
4006 } | |
4007 #endif | |
4008 | |
4009 recursive = TRUE; | |
4010 | |
4011 /* | |
4012 * The matching file names are stored in a growarray. Init it empty. | |
4013 */ | |
4014 ga_init2(&ga, (int)sizeof(char_u *), 30); | |
4015 | |
4016 for (i = 0; i < num_pat; ++i) | |
4017 { | |
4018 add_pat = -1; | |
4019 p = pat[i]; | |
4020 | |
4021 #ifdef VIM_BACKTICK | |
4022 if (vim_backtick(p)) | |
7036
5f00b8d7148f
commit https://github.com/vim/vim/commit/3f188935ec4db5117c4a64cc3f71219175624745
Christian Brabandt <cb@256bit.org>
parents:
6971
diff
changeset
|
4023 { |
7 | 4024 add_pat = expand_backtick(&ga, p, flags); |
7036
5f00b8d7148f
commit https://github.com/vim/vim/commit/3f188935ec4db5117c4a64cc3f71219175624745
Christian Brabandt <cb@256bit.org>
parents:
6971
diff
changeset
|
4025 if (add_pat == -1) |
5f00b8d7148f
commit https://github.com/vim/vim/commit/3f188935ec4db5117c4a64cc3f71219175624745
Christian Brabandt <cb@256bit.org>
parents:
6971
diff
changeset
|
4026 retval = FAIL; |
5f00b8d7148f
commit https://github.com/vim/vim/commit/3f188935ec4db5117c4a64cc3f71219175624745
Christian Brabandt <cb@256bit.org>
parents:
6971
diff
changeset
|
4027 } |
7 | 4028 else |
4029 #endif | |
4030 { | |
4031 /* | |
4032 * First expand environment variables, "~/" and "~user/". | |
4033 */ | |
16738
b52ea9c5f1db
patch 8.1.1371: cannot recover from a swap file
Bram Moolenaar <Bram@vim.org>
parents:
16714
diff
changeset
|
4034 if ((has_env_var(p) && !(flags & EW_NOTENV)) || *p == '~') |
7 | 4035 { |
1408 | 4036 p = expand_env_save_opt(p, TRUE); |
7 | 4037 if (p == NULL) |
4038 p = pat[i]; | |
4039 #ifdef UNIX | |
4040 /* | |
4041 * On Unix, if expand_env() can't expand an environment | |
4042 * variable, use the shell to do that. Discard previously | |
4043 * found file names and start all over again. | |
4044 */ | |
5104
93cccad6a26b
updated for version 7.3.1295
Bram Moolenaar <bram@vim.org>
parents:
4940
diff
changeset
|
4045 else if (has_env_var(p) || *p == '~') |
7 | 4046 { |
4047 vim_free(p); | |
1914 | 4048 ga_clear_strings(&ga); |
7 | 4049 i = mch_expand_wildcards(num_pat, pat, num_file, file, |
6174 | 4050 flags|EW_KEEPDOLLAR); |
7 | 4051 recursive = FALSE; |
4052 return i; | |
4053 } | |
4054 #endif | |
4055 } | |
4056 | |
4057 /* | |
4058 * If there are wildcards: Expand file names and add each match to | |
4059 * the list. If there is no match, and EW_NOTFOUND is given, add | |
4060 * the pattern. | |
4061 * If there are no wildcards: Add the file name if it exists or | |
4062 * when EW_NOTFOUND is given. | |
4063 */ | |
4064 if (mch_has_exp_wildcard(p)) | |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
4065 { |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
4066 #if defined(FEAT_SEARCHPATH) |
2568
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
4067 if ((flags & EW_PATH) |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
4068 && !mch_isFullName(p) |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
4069 && !(p[0] == '.' |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
4070 && (vim_ispathsep(p[1]) |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
4071 || (p[1] == '.' && vim_ispathsep(p[2])))) |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
4072 ) |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
4073 { |
2568
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
4074 /* :find completion where 'path' is used. |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
4075 * Recursiveness is OK here. */ |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
4076 recursive = FALSE; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
4077 add_pat = expand_in_path(&ga, p, flags); |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
4078 recursive = TRUE; |
2568
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
4079 did_expand_in_path = TRUE; |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
4080 } |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
4081 else |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
4082 #endif |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
4083 add_pat = mch_expandpath(&ga, p, flags); |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
4084 } |
7 | 4085 } |
4086 | |
4087 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND))) | |
4088 { | |
4089 char_u *t = backslash_halve_save(p); | |
4090 | |
4091 /* When EW_NOTFOUND is used, always add files and dirs. Makes | |
4092 * "vim c:/" work. */ | |
4093 if (flags & EW_NOTFOUND) | |
4094 addfile(&ga, t, flags | EW_DIR | EW_FILE); | |
9420
9f78f3f0c81f
commit https://github.com/vim/vim/commit/00efded1064427ab3f84e4d57af62e0aab876fc6
Christian Brabandt <cb@256bit.org>
parents:
9389
diff
changeset
|
4095 else |
7 | 4096 addfile(&ga, t, flags); |
4097 vim_free(t); | |
4098 } | |
4099 | |
2313
e382b66b936d
Fix using freed memory in :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
4100 #if defined(FEAT_SEARCHPATH) |
2568
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
4101 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH)) |
2313
e382b66b936d
Fix using freed memory in :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
4102 uniquefy_paths(&ga, p); |
e382b66b936d
Fix using freed memory in :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
4103 #endif |
7 | 4104 if (p != pat[i]) |
4105 vim_free(p); | |
4106 } | |
4107 | |
4108 *num_file = ga.ga_len; | |
4109 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)""; | |
4110 | |
4111 recursive = FALSE; | |
4112 | |
7641
b44fc33ef92a
commit https://github.com/vim/vim/commit/336bd622c31e1805495c034e1a8cfadcc0bbabc7
Christian Brabandt <cb@256bit.org>
parents:
7635
diff
changeset
|
4113 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL; |
7 | 4114 } |
4115 | |
4116 # ifdef VIM_BACKTICK | |
4117 | |
4118 /* | |
4119 * Return TRUE if we can expand this backtick thing here. | |
4120 */ | |
4121 static int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
4122 vim_backtick(char_u *p) |
7 | 4123 { |
4124 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`'); | |
4125 } | |
4126 | |
4127 /* | |
4128 * Expand an item in `backticks` by executing it as a command. | |
4129 * Currently only works when pat[] starts and ends with a `. | |
7036
5f00b8d7148f
commit https://github.com/vim/vim/commit/3f188935ec4db5117c4a64cc3f71219175624745
Christian Brabandt <cb@256bit.org>
parents:
6971
diff
changeset
|
4130 * Returns number of file names found, -1 if an error is encountered. |
7 | 4131 */ |
4132 static int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
4133 expand_backtick( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
4134 garray_T *gap, |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
4135 char_u *pat, |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
4136 int flags) /* EW_* flags */ |
7 | 4137 { |
4138 char_u *p; | |
4139 char_u *cmd; | |
4140 char_u *buffer; | |
4141 int cnt = 0; | |
4142 int i; | |
4143 | |
4144 /* Create the command: lop off the backticks. */ | |
4145 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2); | |
4146 if (cmd == NULL) | |
7036
5f00b8d7148f
commit https://github.com/vim/vim/commit/3f188935ec4db5117c4a64cc3f71219175624745
Christian Brabandt <cb@256bit.org>
parents:
6971
diff
changeset
|
4147 return -1; |
7 | 4148 |
4149 #ifdef FEAT_EVAL | |
4150 if (*cmd == '=') /* `={expr}`: Expand expression */ | |
714 | 4151 buffer = eval_to_string(cmd + 1, &p, TRUE); |
7 | 4152 else |
4153 #endif | |
24 | 4154 buffer = get_cmd_output(cmd, NULL, |
5808 | 4155 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL); |
7 | 4156 vim_free(cmd); |
4157 if (buffer == NULL) | |
7036
5f00b8d7148f
commit https://github.com/vim/vim/commit/3f188935ec4db5117c4a64cc3f71219175624745
Christian Brabandt <cb@256bit.org>
parents:
6971
diff
changeset
|
4158 return -1; |
7 | 4159 |
4160 cmd = buffer; | |
4161 while (*cmd != NUL) | |
4162 { | |
4163 cmd = skipwhite(cmd); /* skip over white space */ | |
4164 p = cmd; | |
4165 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */ | |
4166 ++p; | |
4167 /* add an entry if it is not empty */ | |
4168 if (p > cmd) | |
4169 { | |
4170 i = *p; | |
4171 *p = NUL; | |
4172 addfile(gap, cmd, flags); | |
4173 *p = i; | |
4174 ++cnt; | |
4175 } | |
4176 cmd = p; | |
4177 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n')) | |
4178 ++cmd; | |
4179 } | |
4180 | |
4181 vim_free(buffer); | |
4182 return cnt; | |
4183 } | |
4184 # endif /* VIM_BACKTICK */ | |
4185 | |
4186 /* | |
4187 * Add a file to a file list. Accepted flags: | |
4188 * EW_DIR add directories | |
4189 * EW_FILE add files | |
716 | 4190 * EW_EXEC add executable files |
7 | 4191 * EW_NOTFOUND add even when it doesn't exist |
4192 * EW_ADDSLASH add slash after directory name | |
6663 | 4193 * EW_ALLLINKS add symlink also when the referred file does not exist |
7 | 4194 */ |
4195 void | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
4196 addfile( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
4197 garray_T *gap, |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
4198 char_u *f, /* filename */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
4199 int flags) |
7 | 4200 { |
4201 char_u *p; | |
4202 int isdir; | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9225
diff
changeset
|
4203 stat_T sb; |
6663 | 4204 |
4205 /* if the file/dir/link doesn't exist, may not add it */ | |
4206 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS) | |
6665 | 4207 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0)) |
7 | 4208 return; |
4209 | |
4210 #ifdef FNAME_ILLEGAL | |
4211 /* if the file/dir contains illegal characters, don't add it */ | |
4212 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL) | |
4213 return; | |
4214 #endif | |
4215 | |
4216 isdir = mch_isdir(f); | |
4217 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE))) | |
4218 return; | |
4219 | |
6695 | 4220 /* If the file isn't executable, may not add it. Do accept directories. |
4221 * When invoked from expand_shellcmd() do not use $PATH. */ | |
4222 if (!isdir && (flags & EW_EXEC) | |
4223 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD))) | |
716 | 4224 return; |
4225 | |
7 | 4226 /* Make room for another item in the file list. */ |
4227 if (ga_grow(gap, 1) == FAIL) | |
4228 return; | |
4229 | |
16764
ef00b6bc186b
patch 8.1.1384: using "int" for alloc() often results in compiler warnings
Bram Moolenaar <Bram@vim.org>
parents:
16738
diff
changeset
|
4230 p = alloc(STRLEN(f) + 1 + isdir); |
7 | 4231 if (p == NULL) |
4232 return; | |
4233 | |
4234 STRCPY(p, f); | |
4235 #ifdef BACKSLASH_IN_FILENAME | |
4236 slash_adjust(p); | |
4237 #endif | |
4238 /* | |
4239 * Append a slash or backslash after directory names if none is present. | |
4240 */ | |
4241 #ifndef DONT_ADD_PATHSEP_TO_DIR | |
4242 if (isdir && (flags & EW_ADDSLASH)) | |
4243 add_pathsep(p); | |
4244 #endif | |
4245 ((char_u **)gap->ga_data)[gap->ga_len++] = p; | |
4246 } | |
4247 #endif /* !NO_EXPANDPATH */ | |
4248 | |
4249 #if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO) | |
4250 | |
4251 #ifndef SEEK_SET | |
4252 # define SEEK_SET 0 | |
4253 #endif | |
4254 #ifndef SEEK_END | |
4255 # define SEEK_END 2 | |
4256 #endif | |
4257 | |
4258 /* | |
4259 * Get the stdout of an external command. | |
5808 | 4260 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not |
4261 * NULL store the length there. | |
7 | 4262 * Returns an allocated string, or NULL for error. |
4263 */ | |
4264 char_u * | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
4265 get_cmd_output( |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
4266 char_u *cmd, |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
4267 char_u *infile, /* optional input file name */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
4268 int flags, /* can be SHELL_SILENT */ |
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
4269 int *ret_len) |
7 | 4270 { |
4271 char_u *tempname; | |
4272 char_u *command; | |
4273 char_u *buffer = NULL; | |
4274 int len; | |
4275 int i = 0; | |
4276 FILE *fd; | |
4277 | |
4278 if (check_restricted() || check_secure()) | |
4279 return NULL; | |
4280 | |
4281 /* get a name for the temp file */ | |
6721 | 4282 if ((tempname = vim_tempname('o', FALSE)) == NULL) |
7 | 4283 { |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15420
diff
changeset
|
4284 emsg(_(e_notmp)); |
7 | 4285 return NULL; |
4286 } | |
4287 | |
4288 /* Add the redirection stuff */ | |
24 | 4289 command = make_filter_cmd(cmd, infile, tempname); |
7 | 4290 if (command == NULL) |
4291 goto done; | |
4292 | |
4293 /* | |
4294 * Call the shell to execute the command (errors are ignored). | |
4295 * Don't check timestamps here. | |
4296 */ | |
4297 ++no_check_timestamps; | |
4298 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags); | |
4299 --no_check_timestamps; | |
4300 | |
4301 vim_free(command); | |
4302 | |
4303 /* | |
4304 * read the names from the file into memory | |
4305 */ | |
4306 # ifdef VMS | |
1224 | 4307 /* created temporary file is not always readable as binary */ |
7 | 4308 fd = mch_fopen((char *)tempname, "r"); |
4309 # else | |
4310 fd = mch_fopen((char *)tempname, READBIN); | |
4311 # endif | |
4312 | |
4313 if (fd == NULL) | |
4314 { | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15420
diff
changeset
|
4315 semsg(_(e_notopen), tempname); |
7 | 4316 goto done; |
4317 } | |
4318 | |
4319 fseek(fd, 0L, SEEK_END); | |
4320 len = ftell(fd); /* get size of temp file */ | |
4321 fseek(fd, 0L, SEEK_SET); | |
4322 | |
4323 buffer = alloc(len + 1); | |
4324 if (buffer != NULL) | |
4325 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd); | |
4326 fclose(fd); | |
4327 mch_remove(tempname); | |
4328 if (buffer == NULL) | |
4329 goto done; | |
4330 #ifdef VMS | |
4331 len = i; /* VMS doesn't give us what we asked for... */ | |
4332 #endif | |
4333 if (i != len) | |
4334 { | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15420
diff
changeset
|
4335 semsg(_(e_notread), tempname); |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
13188
diff
changeset
|
4336 VIM_CLEAR(buffer); |
7 | 4337 } |
5808 | 4338 else if (ret_len == NULL) |
5271
25f67b62afd8
updated for version 7.4b.012
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
4339 { |
25f67b62afd8
updated for version 7.4b.012
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
4340 /* Change NUL into SOH, otherwise the string is truncated. */ |
25f67b62afd8
updated for version 7.4b.012
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
4341 for (i = 0; i < len; ++i) |
5275
3ddec3d25bd1
updated for version 7.4b.014
Bram Moolenaar <bram@vim.org>
parents:
5271
diff
changeset
|
4342 if (buffer[i] == NUL) |
3ddec3d25bd1
updated for version 7.4b.014
Bram Moolenaar <bram@vim.org>
parents:
5271
diff
changeset
|
4343 buffer[i] = 1; |
5271
25f67b62afd8
updated for version 7.4b.012
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
4344 |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
4345 buffer[len] = NUL; /* make sure the buffer is terminated */ |
5271
25f67b62afd8
updated for version 7.4b.012
Bram Moolenaar <bram@vim.org>
parents:
5104
diff
changeset
|
4346 } |
5808 | 4347 else |
4348 *ret_len = len; | |
7 | 4349 |
4350 done: | |
4351 vim_free(tempname); | |
4352 return buffer; | |
4353 } | |
4354 #endif | |
4355 | |
4356 /* | |
4357 * Free the list of files returned by expand_wildcards() or other expansion | |
4358 * functions. | |
4359 */ | |
4360 void | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
4361 FreeWild(int count, char_u **files) |
7 | 4362 { |
838 | 4363 if (count <= 0 || files == NULL) |
7 | 4364 return; |
4365 while (count--) | |
4366 vim_free(files[count]); | |
4367 vim_free(files); | |
4368 } | |
4369 | |
4370 /* | |
2302
488be8cbe19c
Make CTRL-L in command line mode respect 'ignorecase' and 'smartcase'. (Martin
Bram Moolenaar <bram@vim.org>
parents:
2297
diff
changeset
|
4371 * Return TRUE when need to go to Insert mode because of 'insertmode'. |
7 | 4372 * Don't do this when still processing a command or a mapping. |
4373 * Don't do this when inside a ":normal" command. | |
4374 */ | |
4375 int | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
4376 goto_im(void) |
7 | 4377 { |
4378 return (p_im && stuff_empty() && typebuf_typed()); | |
4379 } | |
5867 | 4380 |
4381 /* | |
5911 | 4382 * Returns the isolated name of the shell in allocated memory: |
5867 | 4383 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f". |
4384 * - Remove any argument. E.g., "csh -f" -> "csh". | |
4385 * But don't allow a space in the path, so that this works: | |
4386 * "/usr/bin/csh --rcfile ~/.cshrc" | |
4387 * But don't do that for Windows, it's common to have a space in the path. | |
4388 */ | |
4389 char_u * | |
7829
2a8d6b2dd925
commit https://github.com/vim/vim/commit/9b57814db13c29ecb08260b36923c0e1c8a373a9
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
4390 get_isolated_shell_name(void) |
5867 | 4391 { |
4392 char_u *p; | |
4393 | |
15868
7fad90423bd2
patch 8.1.0941: macros for MS-Windows are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15814
diff
changeset
|
4394 #ifdef MSWIN |
5867 | 4395 p = gettail(p_sh); |
4396 p = vim_strnsave(p, (int)(skiptowhite(p) - p)); | |
4397 #else | |
4398 p = skiptowhite(p_sh); | |
4399 if (*p == NUL) | |
4400 { | |
4401 /* No white space, use the tail. */ | |
4402 p = vim_strsave(gettail(p_sh)); | |
4403 } | |
4404 else | |
4405 { | |
4406 char_u *p1, *p2; | |
4407 | |
4408 /* Find the last path separator before the space. */ | |
4409 p1 = p_sh; | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
4410 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2)) |
5867 | 4411 if (vim_ispathsep(*p2)) |
4412 p1 = p2 + 1; | |
4413 p = vim_strnsave(p1, (int)(p - p1)); | |
4414 } | |
4415 #endif | |
4416 return p; | |
4417 } | |
15814
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4418 |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4419 /* |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4420 * Check if the "://" of a URL is at the pointer, return URL_SLASH. |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4421 * Also check for ":\\", which MS Internet Explorer accepts, return |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4422 * URL_BACKSLASH. |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4423 */ |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4424 int |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4425 path_is_url(char_u *p) |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4426 { |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4427 if (STRNCMP(p, "://", (size_t)3) == 0) |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4428 return URL_SLASH; |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4429 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0) |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4430 return URL_BACKSLASH; |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4431 return 0; |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4432 } |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4433 |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4434 /* |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4435 * Check if "fname" starts with "name://". Return URL_SLASH if it does. |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4436 * Return URL_BACKSLASH for "name:\\". |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4437 * Return zero otherwise. |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4438 */ |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4439 int |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4440 path_with_url(char_u *fname) |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4441 { |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4442 char_u *p; |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4443 |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4444 for (p = fname; isalpha(*p); ++p) |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4445 ; |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4446 return path_is_url(p); |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4447 } |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4448 |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4449 /* |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4450 * Return TRUE if "name" is a full (absolute) path name or URL. |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4451 */ |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4452 int |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4453 vim_isAbsName(char_u *name) |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4454 { |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4455 return (path_with_url(name) != 0 || mch_isFullName(name)); |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4456 } |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4457 |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4458 /* |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4459 * Get absolute file name into buffer "buf[len]". |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4460 * |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4461 * return FAIL for failure, OK otherwise |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4462 */ |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4463 int |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4464 vim_FullName( |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4465 char_u *fname, |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4466 char_u *buf, |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4467 int len, |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4468 int force) /* force expansion even when already absolute */ |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4469 { |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4470 int retval = OK; |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4471 int url; |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4472 |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4473 *buf = NUL; |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4474 if (fname == NULL) |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4475 return FAIL; |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4476 |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4477 url = path_with_url(fname); |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4478 if (!url) |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4479 retval = mch_FullName(fname, buf, len, force); |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4480 if (url || retval == FAIL) |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4481 { |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4482 /* something failed; use the file name (truncate when too long) */ |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4483 vim_strncpy(buf, fname, len - 1); |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4484 } |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4485 #if defined(MSWIN) |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4486 slash_adjust(buf); |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4487 #endif |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4488 return retval; |
99ebf78686a9
patch 8.1.0914: code related to findfile() is spread out
Bram Moolenaar <Bram@vim.org>
parents:
15699
diff
changeset
|
4489 } |