Mercurial > vim
comparison src/ex_getln.c @ 5873:8e9db1f27a00 v7.4.279
updated for version 7.4.279
Problem: globpath() returns a string, making it difficult to get a list of
matches. (Greg Novack)
Solution: Add an optional argument like with glob(). (Adnan Zafar)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 07 May 2014 18:35:30 +0200 |
parents | 31217cc48e7d |
children | 0fc665889e8f |
comparison
equal
deleted
inserted
replaced
5872:8661eb2cc9a9 | 5873:8e9db1f27a00 |
---|---|
5093 char_u *pat; | 5093 char_u *pat; |
5094 int *num_file; | 5094 int *num_file; |
5095 char_u ***file; | 5095 char_u ***file; |
5096 char *dirnames[]; | 5096 char *dirnames[]; |
5097 { | 5097 { |
5098 char_u *matches; | |
5099 char_u *s; | 5098 char_u *s; |
5100 char_u *e; | 5099 char_u *e; |
5100 char_u *match; | |
5101 garray_T ga; | 5101 garray_T ga; |
5102 int i; | 5102 int i; |
5103 int pat_len; | 5103 int pat_len; |
5104 | 5104 |
5105 *num_file = 0; | 5105 *num_file = 0; |
5114 { | 5114 { |
5115 ga_clear_strings(&ga); | 5115 ga_clear_strings(&ga); |
5116 return FAIL; | 5116 return FAIL; |
5117 } | 5117 } |
5118 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat); | 5118 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat); |
5119 matches = globpath(p_rtp, s, 0); | 5119 globpath(p_rtp, s, &ga, 0); |
5120 vim_free(s); | 5120 vim_free(s); |
5121 if (matches == NULL) | 5121 } |
5122 continue; | 5122 |
5123 | 5123 for (i = 0; i < ga.ga_len; ++i) |
5124 for (s = matches; *s != NUL; s = e) | 5124 { |
5125 { | 5125 match = ((char_u **)ga.ga_data)[i]; |
5126 e = vim_strchr(s, '\n'); | 5126 s = match; |
5127 if (e == NULL) | 5127 e = s + STRLEN(s); |
5128 e = s + STRLEN(s); | 5128 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0) |
5129 if (ga_grow(&ga, 1) == FAIL) | 5129 { |
5130 break; | 5130 e -= 4; |
5131 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0) | 5131 for (s = e; s > match; mb_ptr_back(match, s)) |
5132 { | 5132 if (s < match || vim_ispathsep(*s)) |
5133 for (s = e - 4; s > matches; mb_ptr_back(matches, s)) | 5133 break; |
5134 if (*s == '\n' || vim_ispathsep(*s)) | 5134 ++s; |
5135 break; | 5135 *e = NUL; |
5136 ++s; | 5136 mch_memmove(match, s, e - s + 1); |
5137 ((char_u **)ga.ga_data)[ga.ga_len] = | 5137 } |
5138 vim_strnsave(s, (int)(e - s - 4)); | 5138 } |
5139 ++ga.ga_len; | 5139 |
5140 } | |
5141 if (*e != NUL) | |
5142 ++e; | |
5143 } | |
5144 vim_free(matches); | |
5145 } | |
5146 if (ga.ga_len == 0) | 5140 if (ga.ga_len == 0) |
5147 return FAIL; | 5141 return FAIL; |
5148 | 5142 |
5149 /* Sort and remove duplicates which can happen when specifying multiple | 5143 /* Sort and remove duplicates which can happen when specifying multiple |
5150 * directories in dirnames. */ | 5144 * directories in dirnames. */ |
5158 #endif | 5152 #endif |
5159 | 5153 |
5160 #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO) | 5154 #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO) |
5161 /* | 5155 /* |
5162 * Expand "file" for all comma-separated directories in "path". | 5156 * Expand "file" for all comma-separated directories in "path". |
5163 * Returns an allocated string with all matches concatenated, separated by | 5157 * Adds the matches to "ga". Caller must init "ga". |
5164 * newlines. Returns NULL for an error or no matches. | 5158 */ |
5165 */ | 5159 void |
5166 char_u * | 5160 globpath(path, file, ga, expand_options) |
5167 globpath(path, file, expand_options) | |
5168 char_u *path; | 5161 char_u *path; |
5169 char_u *file; | 5162 char_u *file; |
5163 garray_T *ga; | |
5170 int expand_options; | 5164 int expand_options; |
5171 { | 5165 { |
5172 expand_T xpc; | 5166 expand_T xpc; |
5173 char_u *buf; | 5167 char_u *buf; |
5174 garray_T ga; | |
5175 int i; | 5168 int i; |
5176 int len; | |
5177 int num_p; | 5169 int num_p; |
5178 char_u **p; | 5170 char_u **p; |
5179 char_u *cur = NULL; | |
5180 | 5171 |
5181 buf = alloc(MAXPATHL); | 5172 buf = alloc(MAXPATHL); |
5182 if (buf == NULL) | 5173 if (buf == NULL) |
5183 return NULL; | 5174 return; |
5184 | 5175 |
5185 ExpandInit(&xpc); | 5176 ExpandInit(&xpc); |
5186 xpc.xp_context = EXPAND_FILES; | 5177 xpc.xp_context = EXPAND_FILES; |
5187 | |
5188 ga_init2(&ga, 1, 100); | |
5189 | 5178 |
5190 /* Loop over all entries in {path}. */ | 5179 /* Loop over all entries in {path}. */ |
5191 while (*path != NUL) | 5180 while (*path != NUL) |
5192 { | 5181 { |
5193 /* Copy one item of the path to buf[] and concatenate the file name. */ | 5182 /* Copy one item of the path to buf[] and concatenate the file name. */ |
5205 STRCAT(buf, file); | 5194 STRCAT(buf, file); |
5206 if (ExpandFromContext(&xpc, buf, &num_p, &p, | 5195 if (ExpandFromContext(&xpc, buf, &num_p, &p, |
5207 WILD_SILENT|expand_options) != FAIL && num_p > 0) | 5196 WILD_SILENT|expand_options) != FAIL && num_p > 0) |
5208 { | 5197 { |
5209 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options); | 5198 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options); |
5210 for (len = 0, i = 0; i < num_p; ++i) | 5199 |
5211 len += (int)STRLEN(p[i]) + 1; | 5200 if (ga_grow(ga, num_p) == OK) |
5212 | 5201 { |
5213 /* Concatenate new results to previous ones. */ | |
5214 if (ga_grow(&ga, len) == OK) | |
5215 { | |
5216 cur = (char_u *)ga.ga_data + ga.ga_len; | |
5217 for (i = 0; i < num_p; ++i) | 5202 for (i = 0; i < num_p; ++i) |
5218 { | 5203 { |
5219 STRCPY(cur, p[i]); | 5204 ((char_u **)ga->ga_data)[ga->ga_len] = |
5220 cur += STRLEN(p[i]); | 5205 vim_strnsave(p[i], STRLEN(p[i])); |
5221 *cur++ = '\n'; | 5206 ++ga->ga_len; |
5222 } | 5207 } |
5223 ga.ga_len += len; | 5208 } |
5224 } | 5209 |
5225 FreeWild(num_p, p); | 5210 FreeWild(num_p, p); |
5226 } | 5211 } |
5227 } | 5212 } |
5228 } | 5213 } |
5229 if (cur != NULL) | |
5230 *--cur = 0; /* Replace trailing newline with NUL */ | |
5231 | 5214 |
5232 vim_free(buf); | 5215 vim_free(buf); |
5233 return (char_u *)ga.ga_data; | |
5234 } | 5216 } |
5235 | 5217 |
5236 #endif | 5218 #endif |
5237 | 5219 |
5238 #if defined(FEAT_CMDHIST) || defined(PROTO) | 5220 #if defined(FEAT_CMDHIST) || defined(PROTO) |