Mercurial > vim
annotate src/tag.c @ 3744:99f076ca8d84 v7.3.631
updated for version 7.3.631
Problem: Cannot complete user names.
Solution: Add user name completion. (Dominique Pelle)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 15 Aug 2012 14:05:05 +0200 |
parents | 320cc46d0eb0 |
children | a638ae0f1b0c |
rev | line source |
---|---|
7 | 1 /* vi:set ts=8 sts=4 sw=4: |
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 * Code to handle tags and the tag stack | |
12 */ | |
13 | |
14 #include "vim.h" | |
15 | |
16 /* | |
17 * Structure to hold pointers to various items in a tag line. | |
18 */ | |
19 typedef struct tag_pointers | |
20 { | |
21 /* filled in by parse_tag_line(): */ | |
22 char_u *tagname; /* start of tag name (skip "file:") */ | |
23 char_u *tagname_end; /* char after tag name */ | |
24 char_u *fname; /* first char of file name */ | |
25 char_u *fname_end; /* char after file name */ | |
26 char_u *command; /* first char of command */ | |
27 /* filled in by parse_match(): */ | |
28 char_u *command_end; /* first char after command */ | |
29 char_u *tag_fname; /* file name of the tags file */ | |
30 #ifdef FEAT_EMACS_TAGS | |
31 int is_etag; /* TRUE for emacs tag */ | |
32 #endif | |
33 char_u *tagkind; /* "kind:" value */ | |
34 char_u *tagkind_end; /* end of tagkind */ | |
35 } tagptrs_T; | |
36 | |
37 /* | |
38 * The matching tags are first stored in ga_match[]. In which one depends on | |
39 * the priority of the match. | |
40 * At the end, the matches from ga_match[] are concatenated, to make a list | |
41 * sorted on priority. | |
42 */ | |
43 #define MT_ST_CUR 0 /* static match in current file */ | |
44 #define MT_GL_CUR 1 /* global match in current file */ | |
45 #define MT_GL_OTH 2 /* global match in other file */ | |
46 #define MT_ST_OTH 3 /* static match in other file */ | |
47 #define MT_IC_ST_CUR 4 /* icase static match in current file */ | |
48 #define MT_IC_GL_CUR 5 /* icase global match in current file */ | |
49 #define MT_IC_GL_OTH 6 /* icase global match in other file */ | |
50 #define MT_IC_ST_OTH 7 /* icase static match in other file */ | |
51 #define MT_IC_OFF 4 /* add for icase match */ | |
52 #define MT_RE_OFF 8 /* add for regexp match */ | |
53 #define MT_MASK 7 /* mask for printing priority */ | |
54 #define MT_COUNT 16 | |
55 | |
56 static char *mt_names[MT_COUNT/2] = | |
57 {"FSC", "F C", "F ", "FS ", " SC", " C", " ", " S "}; | |
58 | |
59 #define NOTAGFILE 99 /* return value for jumpto_tag */ | |
60 static char_u *nofile_fname = NULL; /* fname for NOTAGFILE error */ | |
61 | |
62 static void taglen_advance __ARGS((int l)); | |
63 | |
64 static int jumpto_tag __ARGS((char_u *lbuf, int forceit, int keep_help)); | |
65 #ifdef FEAT_EMACS_TAGS | |
66 static int parse_tag_line __ARGS((char_u *lbuf, int is_etag, tagptrs_T *tagp)); | |
67 #else | |
68 static int parse_tag_line __ARGS((char_u *lbuf, tagptrs_T *tagp)); | |
69 #endif | |
70 static int test_for_static __ARGS((tagptrs_T *)); | |
71 static int parse_match __ARGS((char_u *lbuf, tagptrs_T *tagp)); | |
72 static char_u *tag_full_fname __ARGS((tagptrs_T *tagp)); | |
73 static char_u *expand_tag_fname __ARGS((char_u *fname, char_u *tag_fname, int expand)); | |
74 #ifdef FEAT_EMACS_TAGS | |
75 static int test_for_current __ARGS((int, char_u *, char_u *, char_u *, char_u *)); | |
76 #else | |
77 static int test_for_current __ARGS((char_u *, char_u *, char_u *, char_u *)); | |
78 #endif | |
79 static int find_extra __ARGS((char_u **pp)); | |
80 | |
81 static char_u *bottommsg = (char_u *)N_("E555: at bottom of tag stack"); | |
82 static char_u *topmsg = (char_u *)N_("E556: at top of tag stack"); | |
83 | |
84 static char_u *tagmatchname = NULL; /* name of last used tag */ | |
85 | |
86 /* | |
87 * We use ftello() here, if available. It returns off_t instead of long, | |
88 * which helps if long is 32 bit and off_t is 64 bit. | |
2087
3112fcc89238
updated for version 7.2.371
Bram Moolenaar <bram@zimbu.org>
parents:
1883
diff
changeset
|
89 * We assume that when fseeko() is available then ftello() is too. |
7 | 90 */ |
2087
3112fcc89238
updated for version 7.2.371
Bram Moolenaar <bram@zimbu.org>
parents:
1883
diff
changeset
|
91 #ifdef HAVE_FSEEKO |
7 | 92 # define ftell ftello |
93 #endif | |
94 | |
95 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) | |
96 /* | |
97 * Tag for preview window is remembered separately, to avoid messing up the | |
98 * normal tagstack. | |
99 */ | |
1883 | 100 static taggy_T ptag_entry = {NULL, {INIT_POS_T(0, 0, 0), 0}, 0, 0}; |
7 | 101 #endif |
102 | |
103 /* | |
104 * Jump to tag; handling of tag commands and tag stack | |
105 * | |
106 * *tag != NUL: ":tag {tag}", jump to new tag, add to tag stack | |
107 * | |
108 * type == DT_TAG: ":tag [tag]", jump to newer position or same tag again | |
109 * type == DT_HELP: like DT_TAG, but don't use regexp. | |
110 * type == DT_POP: ":pop" or CTRL-T, jump to old position | |
111 * type == DT_NEXT: jump to next match of same tag | |
112 * type == DT_PREV: jump to previous match of same tag | |
113 * type == DT_FIRST: jump to first match of same tag | |
114 * type == DT_LAST: jump to last match of same tag | |
115 * type == DT_SELECT: ":tselect [tag]", select tag from a list of all matches | |
116 * type == DT_JUMP: ":tjump [tag]", jump to tag or select tag from a list | |
359 | 117 * type == DT_CSCOPE: use cscope to find the tag |
649 | 118 * type == DT_LTAG: use location list for displaying tag matches |
359 | 119 * type == DT_FREE: free cached matches |
7 | 120 * |
121 * for cscope, returns TRUE if we jumped to tag or aborted, FALSE otherwise | |
122 */ | |
123 int | |
124 do_tag(tag, type, count, forceit, verbose) | |
125 char_u *tag; /* tag (pattern) to jump to */ | |
126 int type; | |
127 int count; | |
128 int forceit; /* :ta with ! */ | |
129 int verbose; /* print "tag not found" message */ | |
130 { | |
131 taggy_T *tagstack = curwin->w_tagstack; | |
132 int tagstackidx = curwin->w_tagstackidx; | |
133 int tagstacklen = curwin->w_tagstacklen; | |
134 int cur_match = 0; | |
135 int cur_fnum = curbuf->b_fnum; | |
136 int oldtagstackidx = tagstackidx; | |
137 int prevtagstackidx = tagstackidx; | |
138 int prev_num_matches; | |
139 int new_tag = FALSE; | |
140 int other_name; | |
141 int i, j, k; | |
142 int idx; | |
143 int ic; | |
144 char_u *p; | |
145 char_u *name; | |
146 int no_regexp = FALSE; | |
147 int error_cur_match = 0; | |
148 char_u *command_end; | |
149 int save_pos = FALSE; | |
150 fmark_T saved_fmark; | |
151 int taglen; | |
152 #ifdef FEAT_CSCOPE | |
153 int jumped_to_tag = FALSE; | |
154 #endif | |
155 tagptrs_T tagp, tagp2; | |
156 int new_num_matches; | |
157 char_u **new_matches; | |
158 int attr; | |
159 int use_tagstack; | |
160 int skip_msg = FALSE; | |
161 char_u *buf_ffname = curbuf->b_ffname; /* name to use for | |
162 priority computation */ | |
163 | |
164 /* remember the matches for the last used tag */ | |
165 static int num_matches = 0; | |
166 static int max_num_matches = 0; /* limit used for match search */ | |
167 static char_u **matches = NULL; | |
168 static int flags; | |
169 | |
359 | 170 #ifdef EXITFREE |
171 if (type == DT_FREE) | |
172 { | |
173 /* remove the list of matches */ | |
174 FreeWild(num_matches, matches); | |
175 # ifdef FEAT_CSCOPE | |
176 cs_free_tags(); | |
177 # endif | |
178 num_matches = 0; | |
179 return FALSE; | |
180 } | |
181 #endif | |
182 | |
7 | 183 if (type == DT_HELP) |
184 { | |
185 type = DT_TAG; | |
186 no_regexp = TRUE; | |
187 } | |
188 | |
189 prev_num_matches = num_matches; | |
190 free_string_option(nofile_fname); | |
191 nofile_fname = NULL; | |
192 | |
698 | 193 clearpos(&saved_fmark.mark); /* shutup gcc 4.0 */ |
194 saved_fmark.fnum = 0; | |
195 | |
7 | 196 /* |
197 * Don't add a tag to the tagstack if 'tagstack' has been reset. | |
198 */ | |
199 if ((!p_tgst && *tag != NUL)) | |
200 { | |
201 use_tagstack = FALSE; | |
202 new_tag = TRUE; | |
203 } | |
204 else | |
205 { | |
206 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) | |
2713 | 207 if (g_do_tagpreview != 0) |
7 | 208 use_tagstack = FALSE; |
209 else | |
210 #endif | |
211 use_tagstack = TRUE; | |
212 | |
213 /* new pattern, add to the tag stack */ | |
845 | 214 if (*tag != NUL |
215 && (type == DT_TAG || type == DT_SELECT || type == DT_JUMP | |
649 | 216 #ifdef FEAT_QUICKFIX |
217 || type == DT_LTAG | |
218 #endif | |
7 | 219 #ifdef FEAT_CSCOPE |
220 || type == DT_CSCOPE | |
221 #endif | |
222 )) | |
223 { | |
224 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) | |
2713 | 225 if (g_do_tagpreview != 0) |
7 | 226 { |
227 if (ptag_entry.tagname != NULL | |
228 && STRCMP(ptag_entry.tagname, tag) == 0) | |
229 { | |
230 /* Jumping to same tag: keep the current match, so that | |
231 * the CursorHold autocommand example works. */ | |
232 cur_match = ptag_entry.cur_match; | |
233 cur_fnum = ptag_entry.cur_fnum; | |
234 } | |
235 else | |
236 { | |
237 vim_free(ptag_entry.tagname); | |
238 if ((ptag_entry.tagname = vim_strsave(tag)) == NULL) | |
239 goto end_do_tag; | |
240 } | |
241 } | |
242 else | |
243 #endif | |
244 { | |
245 /* | |
246 * If the last used entry is not at the top, delete all tag | |
247 * stack entries above it. | |
248 */ | |
249 while (tagstackidx < tagstacklen) | |
250 vim_free(tagstack[--tagstacklen].tagname); | |
251 | |
252 /* if the tagstack is full: remove oldest entry */ | |
253 if (++tagstacklen > TAGSTACKSIZE) | |
254 { | |
255 tagstacklen = TAGSTACKSIZE; | |
256 vim_free(tagstack[0].tagname); | |
257 for (i = 1; i < tagstacklen; ++i) | |
258 tagstack[i - 1] = tagstack[i]; | |
259 --tagstackidx; | |
260 } | |
261 | |
262 /* | |
263 * put the tag name in the tag stack | |
264 */ | |
265 if ((tagstack[tagstackidx].tagname = vim_strsave(tag)) == NULL) | |
266 { | |
267 curwin->w_tagstacklen = tagstacklen - 1; | |
268 goto end_do_tag; | |
269 } | |
270 curwin->w_tagstacklen = tagstacklen; | |
271 | |
272 save_pos = TRUE; /* save the cursor position below */ | |
273 } | |
274 | |
275 new_tag = TRUE; | |
276 } | |
277 else | |
278 { | |
279 if ( | |
280 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) | |
2713 | 281 g_do_tagpreview != 0 ? ptag_entry.tagname == NULL : |
7 | 282 #endif |
283 tagstacklen == 0) | |
284 { | |
285 /* empty stack */ | |
286 EMSG(_(e_tagstack)); | |
287 goto end_do_tag; | |
288 } | |
289 | |
290 if (type == DT_POP) /* go to older position */ | |
291 { | |
292 #ifdef FEAT_FOLDING | |
293 int old_KeyTyped = KeyTyped; | |
294 #endif | |
295 if ((tagstackidx -= count) < 0) | |
296 { | |
297 EMSG(_(bottommsg)); | |
298 if (tagstackidx + count == 0) | |
299 { | |
300 /* We did [num]^T from the bottom of the stack */ | |
301 tagstackidx = 0; | |
302 goto end_do_tag; | |
303 } | |
304 /* We weren't at the bottom of the stack, so jump all the | |
305 * way to the bottom now. | |
306 */ | |
307 tagstackidx = 0; | |
308 } | |
309 else if (tagstackidx >= tagstacklen) /* count == 0? */ | |
310 { | |
311 EMSG(_(topmsg)); | |
312 goto end_do_tag; | |
313 } | |
314 | |
315 /* Make a copy of the fmark, autocommands may invalidate the | |
316 * tagstack before it's used. */ | |
317 saved_fmark = tagstack[tagstackidx].fmark; | |
318 if (saved_fmark.fnum != curbuf->b_fnum) | |
319 { | |
320 /* | |
321 * Jump to other file. If this fails (e.g. because the | |
322 * file was changed) keep original position in tag stack. | |
323 */ | |
324 if (buflist_getfile(saved_fmark.fnum, saved_fmark.mark.lnum, | |
325 GETF_SETMARK, forceit) == FAIL) | |
326 { | |
327 tagstackidx = oldtagstackidx; /* back to old posn */ | |
328 goto end_do_tag; | |
329 } | |
330 /* An BufReadPost autocommand may jump to the '" mark, but | |
331 * we don't what that here. */ | |
332 curwin->w_cursor.lnum = saved_fmark.mark.lnum; | |
333 } | |
334 else | |
335 { | |
336 setpcmark(); | |
337 curwin->w_cursor.lnum = saved_fmark.mark.lnum; | |
338 } | |
339 curwin->w_cursor.col = saved_fmark.mark.col; | |
340 curwin->w_set_curswant = TRUE; | |
341 check_cursor(); | |
342 #ifdef FEAT_FOLDING | |
343 if ((fdo_flags & FDO_TAG) && old_KeyTyped) | |
344 foldOpenCursor(); | |
345 #endif | |
346 | |
347 /* remove the old list of matches */ | |
348 FreeWild(num_matches, matches); | |
349 #ifdef FEAT_CSCOPE | |
350 cs_free_tags(); | |
351 #endif | |
352 num_matches = 0; | |
845 | 353 tag_freematch(); |
7 | 354 goto end_do_tag; |
355 } | |
356 | |
692 | 357 if (type == DT_TAG |
358 #if defined(FEAT_QUICKFIX) | |
845 | 359 || type == DT_LTAG |
692 | 360 #endif |
845 | 361 ) |
7 | 362 { |
363 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) | |
2713 | 364 if (g_do_tagpreview != 0) |
7 | 365 { |
366 cur_match = ptag_entry.cur_match; | |
367 cur_fnum = ptag_entry.cur_fnum; | |
368 } | |
369 else | |
370 #endif | |
371 { | |
372 /* ":tag" (no argument): go to newer pattern */ | |
373 save_pos = TRUE; /* save the cursor position below */ | |
374 if ((tagstackidx += count - 1) >= tagstacklen) | |
375 { | |
376 /* | |
377 * Beyond the last one, just give an error message and | |
378 * go to the last one. Don't store the cursor | |
1204 | 379 * position. |
7 | 380 */ |
381 tagstackidx = tagstacklen - 1; | |
382 EMSG(_(topmsg)); | |
383 save_pos = FALSE; | |
384 } | |
385 else if (tagstackidx < 0) /* must have been count == 0 */ | |
386 { | |
387 EMSG(_(bottommsg)); | |
388 tagstackidx = 0; | |
389 goto end_do_tag; | |
390 } | |
391 cur_match = tagstack[tagstackidx].cur_match; | |
392 cur_fnum = tagstack[tagstackidx].cur_fnum; | |
393 } | |
394 new_tag = TRUE; | |
395 } | |
396 else /* go to other matching tag */ | |
397 { | |
398 /* Save index for when selection is cancelled. */ | |
399 prevtagstackidx = tagstackidx; | |
400 | |
401 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) | |
2713 | 402 if (g_do_tagpreview != 0) |
7 | 403 { |
404 cur_match = ptag_entry.cur_match; | |
405 cur_fnum = ptag_entry.cur_fnum; | |
406 } | |
407 else | |
408 #endif | |
409 { | |
410 if (--tagstackidx < 0) | |
411 tagstackidx = 0; | |
412 cur_match = tagstack[tagstackidx].cur_match; | |
413 cur_fnum = tagstack[tagstackidx].cur_fnum; | |
414 } | |
415 switch (type) | |
416 { | |
417 case DT_FIRST: cur_match = count - 1; break; | |
418 case DT_SELECT: | |
419 case DT_JUMP: | |
420 #ifdef FEAT_CSCOPE | |
421 case DT_CSCOPE: | |
422 #endif | |
423 case DT_LAST: cur_match = MAXCOL - 1; break; | |
424 case DT_NEXT: cur_match += count; break; | |
425 case DT_PREV: cur_match -= count; break; | |
426 } | |
427 if (cur_match >= MAXCOL) | |
428 cur_match = MAXCOL - 1; | |
429 else if (cur_match < 0) | |
430 { | |
431 EMSG(_("E425: Cannot go before first matching tag")); | |
432 skip_msg = TRUE; | |
433 cur_match = 0; | |
434 cur_fnum = curbuf->b_fnum; | |
435 } | |
436 } | |
437 } | |
438 | |
439 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) | |
2713 | 440 if (g_do_tagpreview != 0) |
7 | 441 { |
442 if (type != DT_SELECT && type != DT_JUMP) | |
443 { | |
444 ptag_entry.cur_match = cur_match; | |
445 ptag_entry.cur_fnum = cur_fnum; | |
446 } | |
447 } | |
448 else | |
449 #endif | |
450 { | |
451 /* | |
452 * For ":tag [arg]" or ":tselect" remember position before the jump. | |
453 */ | |
454 saved_fmark = tagstack[tagstackidx].fmark; | |
455 if (save_pos) | |
456 { | |
457 tagstack[tagstackidx].fmark.mark = curwin->w_cursor; | |
458 tagstack[tagstackidx].fmark.fnum = curbuf->b_fnum; | |
459 } | |
460 | |
461 /* Curwin will change in the call to jumpto_tag() if ":stag" was | |
462 * used or an autocommand jumps to another window; store value of | |
463 * tagstackidx now. */ | |
464 curwin->w_tagstackidx = tagstackidx; | |
465 if (type != DT_SELECT && type != DT_JUMP) | |
466 { | |
467 curwin->w_tagstack[tagstackidx].cur_match = cur_match; | |
468 curwin->w_tagstack[tagstackidx].cur_fnum = cur_fnum; | |
469 } | |
470 } | |
471 } | |
472 | |
473 /* When not using the current buffer get the name of buffer "cur_fnum". | |
474 * Makes sure that the tag order doesn't change when using a remembered | |
475 * position for "cur_match". */ | |
476 if (cur_fnum != curbuf->b_fnum) | |
477 { | |
478 buf_T *buf = buflist_findnr(cur_fnum); | |
479 | |
480 if (buf != NULL) | |
481 buf_ffname = buf->b_ffname; | |
482 } | |
483 | |
484 /* | |
485 * Repeat searching for tags, when a file has not been found. | |
486 */ | |
487 for (;;) | |
488 { | |
489 /* | |
490 * When desired match not found yet, try to find it (and others). | |
491 */ | |
492 if (use_tagstack) | |
493 name = tagstack[tagstackidx].tagname; | |
494 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) | |
2713 | 495 else if (g_do_tagpreview != 0) |
7 | 496 name = ptag_entry.tagname; |
497 #endif | |
498 else | |
499 name = tag; | |
500 other_name = (tagmatchname == NULL || STRCMP(tagmatchname, name) != 0); | |
501 if (new_tag | |
502 || (cur_match >= num_matches && max_num_matches != MAXCOL) | |
503 || other_name) | |
504 { | |
505 if (other_name) | |
506 { | |
507 vim_free(tagmatchname); | |
508 tagmatchname = vim_strsave(name); | |
509 } | |
510 | |
685 | 511 /* |
512 * If a count is supplied to the ":tag <name>" command, then | |
513 * jump to count'th matching tag. | |
514 */ | |
1785 | 515 if (type == DT_TAG && *tag != NUL && count > 0) |
685 | 516 cur_match = count - 1; |
517 | |
692 | 518 if (type == DT_SELECT || type == DT_JUMP |
519 #if defined(FEAT_QUICKFIX) | |
520 || type == DT_LTAG | |
521 #endif | |
522 ) | |
7 | 523 cur_match = MAXCOL - 1; |
524 max_num_matches = cur_match + 1; | |
525 | |
526 /* when the argument starts with '/', use it as a regexp */ | |
527 if (!no_regexp && *name == '/') | |
528 { | |
529 flags = TAG_REGEXP; | |
530 ++name; | |
531 } | |
532 else | |
533 flags = TAG_NOIC; | |
534 | |
535 #ifdef FEAT_CSCOPE | |
536 if (type == DT_CSCOPE) | |
537 flags = TAG_CSCOPE; | |
538 #endif | |
539 if (verbose) | |
540 flags |= TAG_VERBOSE; | |
541 if (find_tags(name, &new_num_matches, &new_matches, flags, | |
542 max_num_matches, buf_ffname) == OK | |
543 && new_num_matches < max_num_matches) | |
544 max_num_matches = MAXCOL; /* If less than max_num_matches | |
545 found: all matches found. */ | |
546 | |
547 /* If there already were some matches for the same name, move them | |
548 * to the start. Avoids that the order changes when using | |
549 * ":tnext" and jumping to another file. */ | |
550 if (!new_tag && !other_name) | |
551 { | |
552 /* Find the position of each old match in the new list. Need | |
553 * to use parse_match() to find the tag line. */ | |
554 idx = 0; | |
555 for (j = 0; j < num_matches; ++j) | |
556 { | |
557 parse_match(matches[j], &tagp); | |
558 for (i = idx; i < new_num_matches; ++i) | |
559 { | |
560 parse_match(new_matches[i], &tagp2); | |
561 if (STRCMP(tagp.tagname, tagp2.tagname) == 0) | |
562 { | |
563 p = new_matches[i]; | |
564 for (k = i; k > idx; --k) | |
565 new_matches[k] = new_matches[k - 1]; | |
566 new_matches[idx++] = p; | |
567 break; | |
568 } | |
569 } | |
570 } | |
571 } | |
572 FreeWild(num_matches, matches); | |
573 num_matches = new_num_matches; | |
574 matches = new_matches; | |
575 } | |
576 | |
577 if (num_matches <= 0) | |
578 { | |
579 if (verbose) | |
580 EMSG2(_("E426: tag not found: %s"), name); | |
581 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) | |
582 g_do_tagpreview = 0; | |
583 #endif | |
584 } | |
585 else | |
586 { | |
587 int ask_for_selection = FALSE; | |
588 | |
589 #ifdef FEAT_CSCOPE | |
590 if (type == DT_CSCOPE && num_matches > 1) | |
591 { | |
592 cs_print_tags(); | |
593 ask_for_selection = TRUE; | |
594 } | |
595 else | |
596 #endif | |
597 if (type == DT_SELECT || (type == DT_JUMP && num_matches > 1)) | |
598 { | |
599 /* | |
600 * List all the matching tags. | |
601 * Assume that the first match indicates how long the tags can | |
602 * be, and align the file names to that. | |
603 */ | |
604 parse_match(matches[0], &tagp); | |
605 taglen = (int)(tagp.tagname_end - tagp.tagname + 2); | |
606 if (taglen < 18) | |
607 taglen = 18; | |
608 if (taglen > Columns - 25) | |
609 taglen = MAXCOL; | |
610 if (msg_col == 0) | |
611 msg_didout = FALSE; /* overwrite previous message */ | |
612 msg_start(); | |
613 MSG_PUTS_ATTR(_(" # pri kind tag"), hl_attr(HLF_T)); | |
614 msg_clr_eos(); | |
615 taglen_advance(taglen); | |
616 MSG_PUTS_ATTR(_("file\n"), hl_attr(HLF_T)); | |
617 | |
1826 | 618 for (i = 0; i < num_matches && !got_int; ++i) |
7 | 619 { |
620 parse_match(matches[i], &tagp); | |
621 if (!new_tag && ( | |
622 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) | |
2713 | 623 (g_do_tagpreview != 0 |
7 | 624 && i == ptag_entry.cur_match) || |
625 #endif | |
626 (use_tagstack | |
627 && i == tagstack[tagstackidx].cur_match))) | |
628 *IObuff = '>'; | |
629 else | |
630 *IObuff = ' '; | |
273 | 631 vim_snprintf((char *)IObuff + 1, IOSIZE - 1, |
632 "%2d %s ", i + 1, | |
7 | 633 mt_names[matches[i][0] & MT_MASK]); |
634 msg_puts(IObuff); | |
635 if (tagp.tagkind != NULL) | |
636 msg_outtrans_len(tagp.tagkind, | |
637 (int)(tagp.tagkind_end - tagp.tagkind)); | |
638 msg_advance(13); | |
639 msg_outtrans_len_attr(tagp.tagname, | |
640 (int)(tagp.tagname_end - tagp.tagname), | |
641 hl_attr(HLF_T)); | |
642 msg_putchar(' '); | |
643 taglen_advance(taglen); | |
644 | |
645 /* Find out the actual file name. If it is long, truncate | |
646 * it and put "..." in the middle */ | |
647 p = tag_full_fname(&tagp); | |
648 if (p != NULL) | |
649 { | |
650 msg_puts_long_attr(p, hl_attr(HLF_D)); | |
651 vim_free(p); | |
652 } | |
653 if (msg_col > 0) | |
654 msg_putchar('\n'); | |
1826 | 655 if (got_int) |
656 break; | |
7 | 657 msg_advance(15); |
658 | |
659 /* print any extra fields */ | |
660 command_end = tagp.command_end; | |
661 if (command_end != NULL) | |
662 { | |
663 p = command_end + 3; | |
664 while (*p && *p != '\r' && *p != '\n') | |
665 { | |
666 while (*p == TAB) | |
667 ++p; | |
668 | |
669 /* skip "file:" without a value (static tag) */ | |
670 if (STRNCMP(p, "file:", 5) == 0 | |
671 && vim_isspace(p[5])) | |
672 { | |
673 p += 5; | |
674 continue; | |
675 } | |
676 /* skip "kind:<kind>" and "<kind>" */ | |
677 if (p == tagp.tagkind | |
678 || (p + 5 == tagp.tagkind | |
679 && STRNCMP(p, "kind:", 5) == 0)) | |
680 { | |
681 p = tagp.tagkind_end; | |
682 continue; | |
683 } | |
684 /* print all other extra fields */ | |
685 attr = hl_attr(HLF_CM); | |
686 while (*p && *p != '\r' && *p != '\n') | |
687 { | |
688 if (msg_col + ptr2cells(p) >= Columns) | |
689 { | |
690 msg_putchar('\n'); | |
1826 | 691 if (got_int) |
692 break; | |
7 | 693 msg_advance(15); |
694 } | |
695 p = msg_outtrans_one(p, attr); | |
696 if (*p == TAB) | |
697 { | |
698 msg_puts_attr((char_u *)" ", attr); | |
699 break; | |
700 } | |
701 if (*p == ':') | |
702 attr = 0; | |
703 } | |
704 } | |
705 if (msg_col > 15) | |
706 { | |
707 msg_putchar('\n'); | |
1826 | 708 if (got_int) |
709 break; | |
7 | 710 msg_advance(15); |
711 } | |
712 } | |
713 else | |
714 { | |
715 for (p = tagp.command; | |
716 *p && *p != '\r' && *p != '\n'; ++p) | |
717 ; | |
718 command_end = p; | |
719 } | |
720 | |
721 /* | |
722 * Put the info (in several lines) at column 15. | |
723 * Don't display "/^" and "?^". | |
724 */ | |
725 p = tagp.command; | |
726 if (*p == '/' || *p == '?') | |
727 { | |
728 ++p; | |
729 if (*p == '^') | |
730 ++p; | |
731 } | |
732 /* Remove leading whitespace from pattern */ | |
733 while (p != command_end && vim_isspace(*p)) | |
734 ++p; | |
735 | |
736 while (p != command_end) | |
737 { | |
738 if (msg_col + (*p == TAB ? 1 : ptr2cells(p)) > Columns) | |
739 msg_putchar('\n'); | |
1826 | 740 if (got_int) |
741 break; | |
7 | 742 msg_advance(15); |
743 | |
744 /* skip backslash used for escaping command char */ | |
745 if (*p == '\\' && *(p + 1) == *tagp.command) | |
746 ++p; | |
747 | |
748 if (*p == TAB) | |
749 { | |
750 msg_putchar(' '); | |
751 ++p; | |
752 } | |
753 else | |
754 p = msg_outtrans_one(p, 0); | |
755 | |
756 /* don't display the "$/;\"" and "$?;\"" */ | |
757 if (p == command_end - 2 && *p == '$' | |
758 && *(p + 1) == *tagp.command) | |
759 break; | |
760 /* don't display matching '/' or '?' */ | |
761 if (p == command_end - 1 && *p == *tagp.command | |
762 && (*p == '/' || *p == '?')) | |
763 break; | |
764 } | |
765 if (msg_col) | |
766 msg_putchar('\n'); | |
767 ui_breakcheck(); | |
768 } | |
1826 | 769 if (got_int) |
770 got_int = FALSE; /* only stop the listing */ | |
7 | 771 ask_for_selection = TRUE; |
772 } | |
649 | 773 #if defined(FEAT_QUICKFIX) && defined(FEAT_EVAL) |
692 | 774 else if (type == DT_LTAG) |
649 | 775 { |
776 list_T *list; | |
777 char_u tag_name[128 + 1]; | |
2770 | 778 char_u *fname; |
779 char_u *cmd; | |
649 | 780 |
781 /* | |
782 * Add the matching tags to the location list for the current | |
783 * window. | |
784 */ | |
785 | |
2770 | 786 fname = alloc(MAXPATHL + 1); |
787 cmd = alloc(CMDBUFFSIZE + 1); | |
649 | 788 list = list_alloc(); |
2770 | 789 if (list == NULL || fname == NULL || cmd == NULL) |
790 { | |
791 vim_free(cmd); | |
792 vim_free(fname); | |
793 if (list != NULL) | |
794 list_free(list, TRUE); | |
649 | 795 goto end_do_tag; |
2770 | 796 } |
649 | 797 |
798 for (i = 0; i < num_matches; ++i) | |
799 { | |
800 int len, cmd_len; | |
801 long lnum; | |
802 dict_T *dict; | |
803 | |
804 parse_match(matches[i], &tagp); | |
805 | |
806 /* Save the tag name */ | |
835 | 807 len = (int)(tagp.tagname_end - tagp.tagname); |
649 | 808 if (len > 128) |
809 len = 128; | |
810 vim_strncpy(tag_name, tagp.tagname, len); | |
811 tag_name[len] = NUL; | |
812 | |
813 /* Save the tag file name */ | |
814 p = tag_full_fname(&tagp); | |
815 if (p == NULL) | |
816 continue; | |
2768 | 817 vim_strncpy(fname, p, MAXPATHL); |
649 | 818 vim_free(p); |
819 | |
820 /* | |
821 * Get the line number or the search pattern used to locate | |
822 * the tag. | |
823 */ | |
824 lnum = 0; | |
825 if (isdigit(*tagp.command)) | |
826 /* Line number is used to locate the tag */ | |
827 lnum = atol((char *)tagp.command); | |
828 else | |
829 { | |
830 char_u *cmd_start, *cmd_end; | |
831 | |
832 /* Search pattern is used to locate the tag */ | |
833 | |
834 /* Locate the end of the command */ | |
835 cmd_start = tagp.command; | |
836 cmd_end = tagp.command_end; | |
837 if (cmd_end == NULL) | |
838 { | |
839 for (p = tagp.command; | |
840 *p && *p != '\r' && *p != '\n'; ++p) | |
841 ; | |
842 cmd_end = p; | |
843 } | |
692 | 844 |
649 | 845 /* |
846 * Now, cmd_end points to the character after the | |
847 * command. Adjust it to point to the last | |
848 * character of the command. | |
849 */ | |
850 cmd_end--; | |
851 | |
852 /* | |
853 * Skip the '/' and '?' characters at the | |
854 * beginning and end of the search pattern. | |
855 */ | |
856 if (*cmd_start == '/' || *cmd_start == '?') | |
857 cmd_start++; | |
858 | |
859 if (*cmd_end == '/' || *cmd_end == '?') | |
860 cmd_end--; | |
861 | |
862 len = 0; | |
863 cmd[0] = NUL; | |
864 | |
865 /* | |
866 * If "^" is present in the tag search pattern, then | |
867 * copy it first. | |
868 */ | |
869 if (*cmd_start == '^') | |
870 { | |
871 STRCPY(cmd, "^"); | |
872 cmd_start++; | |
873 len++; | |
874 } | |
875 | |
876 /* | |
877 * Precede the tag pattern with \V to make it very | |
878 * nomagic. | |
879 */ | |
880 STRCAT(cmd, "\\V"); | |
881 len += 2; | |
882 | |
835 | 883 cmd_len = (int)(cmd_end - cmd_start + 1); |
649 | 884 if (cmd_len > (CMDBUFFSIZE - 5)) |
885 cmd_len = CMDBUFFSIZE - 5; | |
886 STRNCAT(cmd, cmd_start, cmd_len); | |
887 len += cmd_len; | |
888 | |
889 if (cmd[len - 1] == '$') | |
890 { | |
692 | 891 /* |
649 | 892 * Replace '$' at the end of the search pattern |
893 * with '\$' | |
894 */ | |
895 cmd[len - 1] = '\\'; | |
896 cmd[len] = '$'; | |
897 len++; | |
898 } | |
899 | |
900 cmd[len] = NUL; | |
901 } | |
902 | |
903 if ((dict = dict_alloc()) == NULL) | |
904 continue; | |
905 if (list_append_dict(list, dict) == FAIL) | |
906 { | |
907 vim_free(dict); | |
908 continue; | |
909 } | |
910 | |
911 dict_add_nr_str(dict, "text", 0L, tag_name); | |
912 dict_add_nr_str(dict, "filename", 0L, fname); | |
913 dict_add_nr_str(dict, "lnum", lnum, NULL); | |
914 if (lnum == 0) | |
915 dict_add_nr_str(dict, "pattern", 0L, cmd); | |
916 } | |
917 | |
2533 | 918 vim_snprintf((char *)IObuff, IOSIZE, "ltag %s", tag); |
2530
9fe4164cb586
Fix: :ltag command did not set w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
919 set_errorlist(curwin, list, ' ', IObuff); |
649 | 920 |
1009 | 921 list_free(list, TRUE); |
2770 | 922 vim_free(fname); |
923 vim_free(cmd); | |
692 | 924 |
925 cur_match = 0; /* Jump to the first tag */ | |
649 | 926 } |
927 #endif | |
7 | 928 |
929 if (ask_for_selection == TRUE) | |
930 { | |
931 /* | |
932 * Ask to select a tag from the list. | |
933 */ | |
373 | 934 i = prompt_for_number(NULL); |
7 | 935 if (i <= 0 || i > num_matches || got_int) |
936 { | |
937 /* no valid choice: don't change anything */ | |
938 if (use_tagstack) | |
939 { | |
940 tagstack[tagstackidx].fmark = saved_fmark; | |
941 tagstackidx = prevtagstackidx; | |
942 } | |
943 #ifdef FEAT_CSCOPE | |
944 cs_free_tags(); | |
945 jumped_to_tag = TRUE; | |
946 #endif | |
947 break; | |
948 } | |
949 cur_match = i - 1; | |
950 } | |
951 | |
952 if (cur_match >= num_matches) | |
953 { | |
954 /* Avoid giving this error when a file wasn't found and we're | |
955 * looking for a match in another file, which wasn't found. | |
956 * There will be an EMSG("file doesn't exist") below then. */ | |
957 if ((type == DT_NEXT || type == DT_FIRST) | |
958 && nofile_fname == NULL) | |
959 { | |
960 if (num_matches == 1) | |
961 EMSG(_("E427: There is only one matching tag")); | |
962 else | |
963 EMSG(_("E428: Cannot go beyond last matching tag")); | |
964 skip_msg = TRUE; | |
965 } | |
966 cur_match = num_matches - 1; | |
967 } | |
968 if (use_tagstack) | |
969 { | |
970 tagstack[tagstackidx].cur_match = cur_match; | |
971 tagstack[tagstackidx].cur_fnum = cur_fnum; | |
972 ++tagstackidx; | |
973 } | |
974 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) | |
2713 | 975 else if (g_do_tagpreview != 0) |
7 | 976 { |
977 ptag_entry.cur_match = cur_match; | |
978 ptag_entry.cur_fnum = cur_fnum; | |
979 } | |
980 #endif | |
981 | |
982 /* | |
983 * Only when going to try the next match, report that the previous | |
984 * file didn't exist. Otherwise an EMSG() is given below. | |
985 */ | |
986 if (nofile_fname != NULL && error_cur_match != cur_match) | |
273 | 987 smsg((char_u *)_("File \"%s\" does not exist"), nofile_fname); |
7 | 988 |
989 | |
990 ic = (matches[cur_match][0] & MT_IC_OFF); | |
991 if (type != DT_SELECT && type != DT_JUMP | |
992 #ifdef FEAT_CSCOPE | |
993 && type != DT_CSCOPE | |
994 #endif | |
995 && (num_matches > 1 || ic) | |
996 && !skip_msg) | |
997 { | |
998 /* Give an indication of the number of matching tags */ | |
999 sprintf((char *)IObuff, _("tag %d of %d%s"), | |
1000 cur_match + 1, | |
1001 num_matches, | |
1002 max_num_matches != MAXCOL ? _(" or more") : ""); | |
1003 if (ic) | |
1004 STRCAT(IObuff, _(" Using tag with different case!")); | |
1005 if ((num_matches > prev_num_matches || new_tag) | |
1006 && num_matches > 1) | |
1007 { | |
1008 if (ic) | |
1009 msg_attr(IObuff, hl_attr(HLF_W)); | |
1010 else | |
1011 msg(IObuff); | |
1012 msg_scroll = TRUE; /* don't overwrite this message */ | |
1013 } | |
1014 else | |
1015 give_warning(IObuff, ic); | |
1016 if (ic && !msg_scrolled && msg_silent == 0) | |
1017 { | |
1018 out_flush(); | |
1019 ui_delay(1000L, TRUE); | |
1020 } | |
1021 } | |
1022 | |
589 | 1023 #ifdef FEAT_AUTOCMD |
1024 /* Let the SwapExists event know what tag we are jumping to. */ | |
1025 vim_snprintf((char *)IObuff, IOSIZE, ":ta %s\r", name); | |
1026 set_vim_var_string(VV_SWAPCOMMAND, IObuff, -1); | |
1027 #endif | |
1028 | |
7 | 1029 /* |
1030 * Jump to the desired match. | |
1031 */ | |
589 | 1032 i = jumpto_tag(matches[cur_match], forceit, type != DT_CSCOPE); |
1033 | |
1034 #ifdef FEAT_AUTOCMD | |
1035 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1); | |
1036 #endif | |
1037 | |
1038 if (i == NOTAGFILE) | |
7 | 1039 { |
1040 /* File not found: try again with another matching tag */ | |
1041 if ((type == DT_PREV && cur_match > 0) | |
1042 || ((type == DT_TAG || type == DT_NEXT | |
1043 || type == DT_FIRST) | |
1044 && (max_num_matches != MAXCOL | |
1045 || cur_match < num_matches - 1))) | |
1046 { | |
1047 error_cur_match = cur_match; | |
1048 if (use_tagstack) | |
1049 --tagstackidx; | |
1050 if (type == DT_PREV) | |
1051 --cur_match; | |
1052 else | |
1053 { | |
1054 type = DT_NEXT; | |
1055 ++cur_match; | |
1056 } | |
1057 continue; | |
1058 } | |
1059 EMSG2(_("E429: File \"%s\" does not exist"), nofile_fname); | |
1060 } | |
1061 else | |
1062 { | |
1063 /* We may have jumped to another window, check that | |
1064 * tagstackidx is still valid. */ | |
1065 if (use_tagstack && tagstackidx > curwin->w_tagstacklen) | |
1066 tagstackidx = curwin->w_tagstackidx; | |
1067 #ifdef FEAT_CSCOPE | |
1068 jumped_to_tag = TRUE; | |
1069 #endif | |
1070 } | |
1071 } | |
1072 break; | |
1073 } | |
1074 | |
1075 end_do_tag: | |
1076 /* Only store the new index when using the tagstack and it's valid. */ | |
1077 if (use_tagstack && tagstackidx <= curwin->w_tagstacklen) | |
1078 curwin->w_tagstackidx = tagstackidx; | |
1079 #ifdef FEAT_WINDOWS | |
1080 postponed_split = 0; /* don't split next time */ | |
1081 #endif | |
1082 | |
1083 #ifdef FEAT_CSCOPE | |
1084 return jumped_to_tag; | |
1085 #else | |
1086 return FALSE; | |
1087 #endif | |
1088 } | |
1089 | |
1090 /* | |
1091 * Free cached tags. | |
1092 */ | |
1093 void | |
1094 tag_freematch() | |
1095 { | |
1096 vim_free(tagmatchname); | |
1097 tagmatchname = NULL; | |
1098 } | |
1099 | |
1100 static void | |
1101 taglen_advance(l) | |
1102 int l; | |
1103 { | |
1104 if (l == MAXCOL) | |
1105 { | |
1106 msg_putchar('\n'); | |
1107 msg_advance(24); | |
1108 } | |
1109 else | |
1110 msg_advance(13 + l); | |
1111 } | |
1112 | |
1113 /* | |
1114 * Print the tag stack | |
1115 */ | |
1116 void | |
1117 do_tags(eap) | |
1877 | 1118 exarg_T *eap UNUSED; |
7 | 1119 { |
1120 int i; | |
1121 char_u *name; | |
1122 taggy_T *tagstack = curwin->w_tagstack; | |
1123 int tagstackidx = curwin->w_tagstackidx; | |
1124 int tagstacklen = curwin->w_tagstacklen; | |
1125 | |
1126 /* Highlight title */ | |
1127 MSG_PUTS_TITLE(_("\n # TO tag FROM line in file/text")); | |
1128 for (i = 0; i < tagstacklen; ++i) | |
1129 { | |
1130 if (tagstack[i].tagname != NULL) | |
1131 { | |
1132 name = fm_getname(&(tagstack[i].fmark), 30); | |
1133 if (name == NULL) /* file name not available */ | |
1134 continue; | |
1135 | |
1136 msg_putchar('\n'); | |
1137 sprintf((char *)IObuff, "%c%2d %2d %-15s %5ld ", | |
1138 i == tagstackidx ? '>' : ' ', | |
1139 i + 1, | |
1140 tagstack[i].cur_match + 1, | |
1141 tagstack[i].tagname, | |
1142 tagstack[i].fmark.mark.lnum); | |
1143 msg_outtrans(IObuff); | |
1144 msg_outtrans_attr(name, tagstack[i].fmark.fnum == curbuf->b_fnum | |
1145 ? hl_attr(HLF_D) : 0); | |
1146 vim_free(name); | |
1147 } | |
1148 out_flush(); /* show one line at a time */ | |
1149 } | |
1150 if (tagstackidx == tagstacklen) /* idx at top of stack */ | |
1151 MSG_PUTS("\n>"); | |
1152 } | |
1153 | |
1154 /* When not using a CR for line separator, use vim_fgets() to read tag lines. | |
1155 * For the Mac use tag_fgets(). It can handle any line separator, but is much | |
1156 * slower than vim_fgets(). | |
1157 */ | |
1158 #ifndef USE_CR | |
1159 # define tag_fgets vim_fgets | |
1160 #endif | |
1161 | |
1162 #ifdef FEAT_TAG_BINS | |
1163 static int tag_strnicmp __ARGS((char_u *s1, char_u *s2, size_t len)); | |
1164 | |
1165 /* | |
1166 * Compare two strings, for length "len", ignoring case the ASCII way. | |
1167 * return 0 for match, < 0 for smaller, > 0 for bigger | |
1168 * Make sure case is folded to uppercase in comparison (like for 'sort -f') | |
1169 */ | |
1170 static int | |
1171 tag_strnicmp(s1, s2, len) | |
1172 char_u *s1; | |
1173 char_u *s2; | |
1174 size_t len; | |
1175 { | |
1176 int i; | |
1177 | |
1178 while (len > 0) | |
1179 { | |
1180 i = (int)TOUPPER_ASC(*s1) - (int)TOUPPER_ASC(*s2); | |
1181 if (i != 0) | |
1182 return i; /* this character different */ | |
1183 if (*s1 == NUL) | |
1184 break; /* strings match until NUL */ | |
1185 ++s1; | |
1186 ++s2; | |
1187 --len; | |
1188 } | |
1189 return 0; /* strings match */ | |
1190 } | |
1191 #endif | |
1192 | |
1193 /* | |
1194 * Structure to hold info about the tag pattern being used. | |
1195 */ | |
1196 typedef struct | |
1197 { | |
1198 char_u *pat; /* the pattern */ | |
1199 int len; /* length of pat[] */ | |
1200 char_u *head; /* start of pattern head */ | |
1201 int headlen; /* length of head[] */ | |
1202 regmatch_T regmatch; /* regexp program, may be NULL */ | |
1203 } pat_T; | |
1204 | |
1205 static void prepare_pats __ARGS((pat_T *pats, int has_re)); | |
1206 | |
1207 /* | |
1208 * Extract info from the tag search pattern "pats->pat". | |
1209 */ | |
1210 static void | |
1211 prepare_pats(pats, has_re) | |
1212 pat_T *pats; | |
1213 int has_re; | |
1214 { | |
1215 pats->head = pats->pat; | |
1216 pats->headlen = pats->len; | |
1217 if (has_re) | |
1218 { | |
1219 /* When the pattern starts with '^' or "\\<", binary searching can be | |
1220 * used (much faster). */ | |
1221 if (pats->pat[0] == '^') | |
1222 pats->head = pats->pat + 1; | |
1223 else if (pats->pat[0] == '\\' && pats->pat[1] == '<') | |
1224 pats->head = pats->pat + 2; | |
1225 if (pats->head == pats->pat) | |
1226 pats->headlen = 0; | |
1227 else | |
1228 for (pats->headlen = 0; pats->head[pats->headlen] != NUL; | |
1229 ++pats->headlen) | |
1230 if (vim_strchr((char_u *)(p_magic ? ".[~*\\$" : "\\$"), | |
1231 pats->head[pats->headlen]) != NULL) | |
1232 break; | |
1233 if (p_tl != 0 && pats->headlen > p_tl) /* adjust for 'taglength' */ | |
1234 pats->headlen = p_tl; | |
1235 } | |
1236 | |
1237 if (has_re) | |
1238 pats->regmatch.regprog = vim_regcomp(pats->pat, p_magic ? RE_MAGIC : 0); | |
1239 else | |
1240 pats->regmatch.regprog = NULL; | |
1241 } | |
1242 | |
1243 /* | |
1244 * find_tags() - search for tags in tags files | |
1245 * | |
1246 * Return FAIL if search completely failed (*num_matches will be 0, *matchesp | |
1247 * will be NULL), OK otherwise. | |
1248 * | |
1249 * There is a priority in which type of tag is recognized. | |
1250 * | |
1251 * 6. A static or global tag with a full matching tag for the current file. | |
1252 * 5. A global tag with a full matching tag for another file. | |
1253 * 4. A static tag with a full matching tag for another file. | |
1254 * 3. A static or global tag with an ignore-case matching tag for the | |
1255 * current file. | |
1256 * 2. A global tag with an ignore-case matching tag for another file. | |
1257 * 1. A static tag with an ignore-case matching tag for another file. | |
1258 * | |
1259 * Tags in an emacs-style tags file are always global. | |
1260 * | |
1261 * flags: | |
1262 * TAG_HELP only search for help tags | |
1263 * TAG_NAMES only return name of tag | |
1264 * TAG_REGEXP use "pat" as a regexp | |
1265 * TAG_NOIC don't always ignore case | |
1266 * TAG_KEEP_LANG keep language | |
1267 */ | |
1268 int | |
1269 find_tags(pat, num_matches, matchesp, flags, mincount, buf_ffname) | |
1270 char_u *pat; /* pattern to search for */ | |
1271 int *num_matches; /* return: number of matches found */ | |
1272 char_u ***matchesp; /* return: array of matches found */ | |
1273 int flags; | |
1274 int mincount; /* MAXCOL: find all matches | |
1275 other: minimal number of matches */ | |
1276 char_u *buf_ffname; /* name of buffer for priority */ | |
1277 { | |
1278 FILE *fp; | |
1279 char_u *lbuf; /* line buffer */ | |
3131 | 1280 int lbuf_size = LSIZE; /* length of lbuf */ |
7 | 1281 char_u *tag_fname; /* name of tag file */ |
692 | 1282 tagname_T tn; /* info for get_tagfname() */ |
7 | 1283 int first_file; /* trying first tag file */ |
1284 tagptrs_T tagp; | |
1285 int did_open = FALSE; /* did open a tag file */ | |
1286 int stop_searching = FALSE; /* stop when match found or error */ | |
1287 int retval = FAIL; /* return value */ | |
1288 int is_static; /* current tag line is static */ | |
1289 int is_current; /* file name matches */ | |
1290 int eof = FALSE; /* found end-of-file */ | |
1291 char_u *p; | |
1292 char_u *s; | |
1293 int i; | |
1294 #ifdef FEAT_TAG_BINS | |
3131 | 1295 int tag_file_sorted = NUL; /* !_TAG_FILE_SORTED value */ |
7 | 1296 struct tag_search_info /* Binary search file offsets */ |
1297 { | |
1298 off_t low_offset; /* offset for first char of first line that | |
1299 could match */ | |
1300 off_t high_offset; /* offset of char after last line that could | |
1301 match */ | |
1302 off_t curr_offset; /* Current file offset in search range */ | |
1303 off_t curr_offset_used; /* curr_offset used when skipping back */ | |
1304 off_t match_offset; /* Where the binary search found a tag */ | |
1305 int low_char; /* first char at low_offset */ | |
1306 int high_char; /* first char at high_offset */ | |
1307 } search_info; | |
1308 off_t filesize; | |
1309 int tagcmp; | |
1310 off_t offset; | |
1311 int round; | |
1312 #endif | |
1313 enum | |
1314 { | |
1315 TS_START, /* at start of file */ | |
1316 TS_LINEAR /* linear searching forward, till EOF */ | |
1317 #ifdef FEAT_TAG_BINS | |
1318 , TS_BINARY, /* binary searching */ | |
1319 TS_SKIP_BACK, /* skipping backwards */ | |
1320 TS_STEP_FORWARD /* stepping forwards */ | |
1321 #endif | |
1322 } state; /* Current search state */ | |
1323 | |
1324 int cmplen; | |
1325 int match; /* matches */ | |
1326 int match_no_ic = 0;/* matches with rm_ic == FALSE */ | |
1327 int match_re; /* match with regexp */ | |
1328 int matchoff = 0; | |
1329 | |
1330 #ifdef FEAT_EMACS_TAGS | |
1331 /* | |
1332 * Stack for included emacs-tags file. | |
1333 * It has a fixed size, to truncate cyclic includes. jw | |
1334 */ | |
1335 # define INCSTACK_SIZE 42 | |
1336 struct | |
1337 { | |
1338 FILE *fp; | |
1339 char_u *etag_fname; | |
1340 } incstack[INCSTACK_SIZE]; | |
1341 | |
1342 int incstack_idx = 0; /* index in incstack */ | |
1204 | 1343 char_u *ebuf; /* additional buffer for etag fname */ |
7 | 1344 int is_etag; /* current file is emaces style */ |
1345 #endif | |
1346 | |
1347 struct match_found | |
1348 { | |
1349 int len; /* nr of chars of match[] to be compared */ | |
1350 char_u match[1]; /* actually longer */ | |
1351 } *mfp, *mfp2; | |
1352 garray_T ga_match[MT_COUNT]; | |
1353 int match_count = 0; /* number of matches found */ | |
1354 char_u **matches; | |
1355 int mtt; | |
1356 int help_save; | |
1357 #ifdef FEAT_MULTI_LANG | |
1358 int help_pri = 0; | |
1359 char_u *help_lang_find = NULL; /* lang to be found */ | |
1360 char_u help_lang[3]; /* lang of current tags file */ | |
1361 char_u *saved_pat = NULL; /* copy of pat[] */ | |
1362 #endif | |
1363 | |
1364 pat_T orgpat; /* holds unconverted pattern info */ | |
1365 #ifdef FEAT_MBYTE | |
1366 vimconv_T vimconv; | |
1367 #endif | |
1368 | |
1369 #ifdef FEAT_TAG_BINS | |
1370 int findall = (mincount == MAXCOL || mincount == TAG_MANY); | |
1371 /* find all matching tags */ | |
1372 int sort_error = FALSE; /* tags file not sorted */ | |
1373 int linear; /* do a linear search */ | |
1374 int sortic = FALSE; /* tag file sorted in nocase */ | |
1375 #endif | |
1376 int line_error = FALSE; /* syntax error */ | |
1377 int has_re = (flags & TAG_REGEXP); /* regexp used */ | |
1378 int help_only = (flags & TAG_HELP); | |
1379 int name_only = (flags & TAG_NAMES); | |
1380 int noic = (flags & TAG_NOIC); | |
1381 int get_it_again = FALSE; | |
1382 #ifdef FEAT_CSCOPE | |
1383 int use_cscope = (flags & TAG_CSCOPE); | |
1384 #endif | |
1385 int verbose = (flags & TAG_VERBOSE); | |
1386 | |
1387 help_save = curbuf->b_help; | |
1388 orgpat.pat = pat; | |
1389 #ifdef FEAT_MBYTE | |
1390 vimconv.vc_type = CONV_NONE; | |
1391 #endif | |
1392 | |
1393 /* | |
1394 * Allocate memory for the buffers that are used | |
1395 */ | |
3131 | 1396 lbuf = alloc(lbuf_size); |
7 | 1397 tag_fname = alloc(MAXPATHL + 1); |
1398 #ifdef FEAT_EMACS_TAGS | |
1399 ebuf = alloc(LSIZE); | |
1400 #endif | |
1401 for (mtt = 0; mtt < MT_COUNT; ++mtt) | |
1402 ga_init2(&ga_match[mtt], (int)sizeof(struct match_found *), 100); | |
1403 | |
1404 /* check for out of memory situation */ | |
1405 if (lbuf == NULL || tag_fname == NULL | |
1406 #ifdef FEAT_EMACS_TAGS | |
1407 || ebuf == NULL | |
1408 #endif | |
1409 ) | |
1410 goto findtag_end; | |
1411 | |
1412 #ifdef FEAT_CSCOPE | |
1413 STRCPY(tag_fname, "from cscope"); /* for error messages */ | |
1414 #endif | |
1415 | |
1416 /* | |
1417 * Initialize a few variables | |
1418 */ | |
1419 if (help_only) /* want tags from help file */ | |
1420 curbuf->b_help = TRUE; /* will be restored later */ | |
1421 | |
3131 | 1422 orgpat.len = (int)STRLEN(pat); |
7 | 1423 #ifdef FEAT_MULTI_LANG |
1424 if (curbuf->b_help) | |
1425 { | |
1426 /* When "@ab" is specified use only the "ab" language, otherwise | |
1427 * search all languages. */ | |
3131 | 1428 if (orgpat.len > 3 && pat[orgpat.len - 3] == '@' |
1429 && ASCII_ISALPHA(pat[orgpat.len - 2]) | |
1430 && ASCII_ISALPHA(pat[orgpat.len - 1])) | |
7 | 1431 { |
3131 | 1432 saved_pat = vim_strnsave(pat, orgpat.len - 3); |
7 | 1433 if (saved_pat != NULL) |
1434 { | |
3131 | 1435 help_lang_find = &pat[orgpat.len - 2]; |
1436 orgpat.pat = saved_pat; | |
1437 orgpat.len -= 3; | |
7 | 1438 } |
1439 } | |
1440 } | |
1441 #endif | |
3131 | 1442 if (p_tl != 0 && orgpat.len > p_tl) /* adjust for 'taglength' */ |
1443 orgpat.len = p_tl; | |
1444 | |
1445 prepare_pats(&orgpat, has_re); | |
7 | 1446 |
1447 #ifdef FEAT_TAG_BINS | |
1448 /* This is only to avoid a compiler warning for using search_info | |
1449 * uninitialised. */ | |
1450 vim_memset(&search_info, 0, (size_t)1); | |
1451 #endif | |
1452 | |
413 | 1453 /* |
1454 * When finding a specified number of matches, first try with matching | |
1455 * case, so binary search can be used, and try ignore-case matches in a | |
1456 * second loop. | |
1457 * When finding all matches, 'tagbsearch' is off, or there is no fixed | |
1458 * string to look for, ignore case right away to avoid going though the | |
1459 * tags files twice. | |
1460 * When the tag file is case-fold sorted, it is either one or the other. | |
1461 * Only ignore case when TAG_NOIC not used or 'ignorecase' set. | |
1462 */ | |
7 | 1463 #ifdef FEAT_TAG_BINS |
3131 | 1464 orgpat.regmatch.rm_ic = ((p_ic || !noic) |
1465 && (findall || orgpat.headlen == 0 || !p_tbs)); | |
7 | 1466 for (round = 1; round <= 2; ++round) |
1467 { | |
3131 | 1468 linear = (orgpat.headlen == 0 || !p_tbs || round == 2); |
7 | 1469 #else |
3131 | 1470 orgpat.regmatch.rm_ic = (p_ic || !noic); |
7 | 1471 #endif |
1472 | |
1473 /* | |
1474 * Try tag file names from tags option one by one. | |
1475 */ | |
1476 for (first_file = TRUE; | |
1477 #ifdef FEAT_CSCOPE | |
1478 use_cscope || | |
1479 #endif | |
692 | 1480 get_tagfname(&tn, first_file, tag_fname) == OK; |
1481 first_file = FALSE) | |
7 | 1482 { |
1483 /* | |
1484 * A file that doesn't exist is silently ignored. Only when not a | |
1485 * single file is found, an error message is given (further on). | |
1486 */ | |
1487 #ifdef FEAT_CSCOPE | |
1488 if (use_cscope) | |
1489 fp = NULL; /* avoid GCC warning */ | |
1490 else | |
1491 #endif | |
1492 { | |
1493 #ifdef FEAT_MULTI_LANG | |
1494 if (curbuf->b_help) | |
1495 { | |
1496 /* Prefer help tags according to 'helplang'. Put the | |
1497 * two-letter language name in help_lang[]. */ | |
835 | 1498 i = (int)STRLEN(tag_fname); |
7 | 1499 if (i > 3 && tag_fname[i - 3] == '-') |
1500 STRCPY(help_lang, tag_fname + i - 2); | |
1501 else | |
1502 STRCPY(help_lang, "en"); | |
1503 | |
1504 /* When searching for a specific language skip tags files | |
1505 * for other languages. */ | |
1506 if (help_lang_find != NULL | |
1507 && STRICMP(help_lang, help_lang_find) != 0) | |
1508 continue; | |
1509 | |
1510 /* For CTRL-] in a help file prefer a match with the same | |
1511 * language. */ | |
1512 if ((flags & TAG_KEEP_LANG) | |
1513 && help_lang_find == NULL | |
1514 && curbuf->b_fname != NULL | |
835 | 1515 && (i = (int)STRLEN(curbuf->b_fname)) > 4 |
7 | 1516 && curbuf->b_fname[i - 1] == 'x' |
1517 && curbuf->b_fname[i - 4] == '.' | |
1518 && STRNICMP(curbuf->b_fname + i - 3, help_lang, 2) == 0) | |
1519 help_pri = 0; | |
1520 else | |
1521 { | |
1522 help_pri = 1; | |
1523 for (s = p_hlg; *s != NUL; ++s) | |
1524 { | |
1525 if (STRNICMP(s, help_lang, 2) == 0) | |
1526 break; | |
1527 ++help_pri; | |
1528 if ((s = vim_strchr(s, ',')) == NULL) | |
1529 break; | |
1530 } | |
1531 if (s == NULL || *s == NUL) | |
1532 { | |
1533 /* Language not in 'helplang': use last, prefer English, | |
1534 * unless found already. */ | |
1535 ++help_pri; | |
1536 if (STRICMP(help_lang, "en") != 0) | |
1537 ++help_pri; | |
1538 } | |
1539 } | |
1540 } | |
1541 #endif | |
1542 | |
1543 if ((fp = mch_fopen((char *)tag_fname, "r")) == NULL) | |
1544 continue; | |
1545 | |
1546 if (p_verbose >= 5) | |
292 | 1547 { |
1548 verbose_enter(); | |
273 | 1549 smsg((char_u *)_("Searching tags file %s"), tag_fname); |
292 | 1550 verbose_leave(); |
1551 } | |
7 | 1552 } |
1553 did_open = TRUE; /* remember that we found at least one file */ | |
1554 | |
1555 state = TS_START; /* we're at the start of the file */ | |
1556 #ifdef FEAT_EMACS_TAGS | |
1557 is_etag = 0; /* default is: not emacs style */ | |
1558 #endif | |
1559 | |
1560 /* | |
1561 * Read and parse the lines in the file one by one | |
1562 */ | |
1563 for (;;) | |
1564 { | |
1565 line_breakcheck(); /* check for CTRL-C typed */ | |
1566 #ifdef FEAT_INS_EXPAND | |
1567 if ((flags & TAG_INS_COMP)) /* Double brackets for gcc */ | |
464 | 1568 ins_compl_check_keys(30); |
449 | 1569 if (got_int || compl_interrupted) |
7 | 1570 #else |
1571 if (got_int) | |
1572 #endif | |
1573 { | |
1574 stop_searching = TRUE; | |
1575 break; | |
1576 } | |
1577 /* When mincount is TAG_MANY, stop when enough matches have been | |
1578 * found (for completion). */ | |
1579 if (mincount == TAG_MANY && match_count >= TAG_MANY) | |
1580 { | |
1581 stop_searching = TRUE; | |
1582 retval = OK; | |
1583 break; | |
1584 } | |
1585 if (get_it_again) | |
1586 goto line_read_in; | |
1587 #ifdef FEAT_TAG_BINS | |
1588 /* | |
1589 * For binary search: compute the next offset to use. | |
1590 */ | |
1591 if (state == TS_BINARY) | |
1592 { | |
1593 offset = search_info.low_offset + ((search_info.high_offset | |
1594 - search_info.low_offset) / 2); | |
1595 if (offset == search_info.curr_offset) | |
1596 break; /* End the binary search without a match. */ | |
1597 else | |
1598 search_info.curr_offset = offset; | |
1599 } | |
1600 | |
1601 /* | |
1602 * Skipping back (after a match during binary search). | |
1603 */ | |
1604 else if (state == TS_SKIP_BACK) | |
1605 { | |
1606 search_info.curr_offset -= LSIZE * 2; | |
1607 if (search_info.curr_offset < 0) | |
1608 { | |
1609 search_info.curr_offset = 0; | |
1610 rewind(fp); | |
1611 state = TS_STEP_FORWARD; | |
1612 } | |
1613 } | |
1614 | |
1615 /* | |
1616 * When jumping around in the file, first read a line to find the | |
1617 * start of the next line. | |
1618 */ | |
1619 if (state == TS_BINARY || state == TS_SKIP_BACK) | |
1620 { | |
1621 /* Adjust the search file offset to the correct position */ | |
1622 search_info.curr_offset_used = search_info.curr_offset; | |
1623 #ifdef HAVE_FSEEKO | |
1624 fseeko(fp, search_info.curr_offset, SEEK_SET); | |
1625 #else | |
1626 fseek(fp, (long)search_info.curr_offset, SEEK_SET); | |
1627 #endif | |
1628 eof = tag_fgets(lbuf, LSIZE, fp); | |
1629 if (!eof && search_info.curr_offset != 0) | |
1630 { | |
1146 | 1631 /* The explicit cast is to work around a bug in gcc 3.4.2 |
1632 * (repeated below). */ | |
7 | 1633 search_info.curr_offset = ftell(fp); |
1634 if (search_info.curr_offset == search_info.high_offset) | |
1635 { | |
1636 /* oops, gone a bit too far; try from low offset */ | |
1637 #ifdef HAVE_FSEEKO | |
1638 fseeko(fp, search_info.low_offset, SEEK_SET); | |
1639 #else | |
1640 fseek(fp, (long)search_info.low_offset, SEEK_SET); | |
1641 #endif | |
1642 search_info.curr_offset = search_info.low_offset; | |
1643 } | |
1644 eof = tag_fgets(lbuf, LSIZE, fp); | |
1645 } | |
1646 /* skip empty and blank lines */ | |
1647 while (!eof && vim_isblankline(lbuf)) | |
1648 { | |
1649 search_info.curr_offset = ftell(fp); | |
1650 eof = tag_fgets(lbuf, LSIZE, fp); | |
1651 } | |
1652 if (eof) | |
1653 { | |
1654 /* Hit end of file. Skip backwards. */ | |
1655 state = TS_SKIP_BACK; | |
1656 search_info.match_offset = ftell(fp); | |
1657 search_info.curr_offset = search_info.curr_offset_used; | |
1658 continue; | |
1659 } | |
1660 } | |
1661 | |
1662 /* | |
1663 * Not jumping around in the file: Read the next line. | |
1664 */ | |
1665 else | |
1666 #endif | |
1667 { | |
1668 /* skip empty and blank lines */ | |
1669 do | |
1670 { | |
1671 #ifdef FEAT_CSCOPE | |
1672 if (use_cscope) | |
1673 eof = cs_fgets(lbuf, LSIZE); | |
1674 else | |
1675 #endif | |
1676 eof = tag_fgets(lbuf, LSIZE, fp); | |
1677 } while (!eof && vim_isblankline(lbuf)); | |
1678 | |
1679 if (eof) | |
1680 { | |
1681 #ifdef FEAT_EMACS_TAGS | |
1682 if (incstack_idx) /* this was an included file */ | |
1683 { | |
1684 --incstack_idx; | |
1685 fclose(fp); /* end of this file ... */ | |
1686 fp = incstack[incstack_idx].fp; | |
1687 STRCPY(tag_fname, incstack[incstack_idx].etag_fname); | |
1688 vim_free(incstack[incstack_idx].etag_fname); | |
1689 is_etag = 1; /* (only etags can include) */ | |
1690 continue; /* ... continue with parent file */ | |
1691 } | |
1692 else | |
1693 #endif | |
1694 break; /* end of file */ | |
1695 } | |
1696 } | |
1697 line_read_in: | |
1698 | |
3131 | 1699 #ifdef FEAT_MBYTE |
1700 if (vimconv.vc_type != CONV_NONE) | |
1701 { | |
1702 char_u *conv_line; | |
1703 int len; | |
1704 | |
1705 /* Convert every line. Converting the pattern from 'enc' to | |
1706 * the tags file encoding doesn't work, because characters are | |
1707 * not recognized. */ | |
1708 conv_line = string_convert(&vimconv, lbuf, NULL); | |
1709 if (conv_line != NULL) | |
1710 { | |
1711 /* Copy or swap lbuf and conv_line. */ | |
1712 len = (int)STRLEN(conv_line) + 1; | |
1713 if (len > lbuf_size) | |
1714 { | |
1715 vim_free(lbuf); | |
1716 lbuf = conv_line; | |
1717 lbuf_size = len; | |
1718 } | |
1719 else | |
1720 { | |
1721 STRCPY(lbuf, conv_line); | |
1722 vim_free(conv_line); | |
1723 } | |
1724 } | |
1725 } | |
1726 #endif | |
1727 | |
1728 | |
7 | 1729 #ifdef FEAT_EMACS_TAGS |
1730 /* | |
1731 * Emacs tags line with CTRL-L: New file name on next line. | |
1732 * The file name is followed by a ','. | |
1733 */ | |
1734 if (*lbuf == Ctrl_L) /* remember etag file name in ebuf */ | |
1735 { | |
1736 is_etag = 1; /* in case at the start */ | |
1737 state = TS_LINEAR; | |
1738 if (!tag_fgets(ebuf, LSIZE, fp)) | |
1739 { | |
1740 for (p = ebuf; *p && *p != ','; p++) | |
1741 ; | |
1742 *p = NUL; | |
1743 | |
1744 /* | |
1745 * atoi(p+1) is the number of bytes before the next ^L | |
1746 * unless it is an include statement. | |
1747 */ | |
1748 if (STRNCMP(p + 1, "include", 7) == 0 | |
1749 && incstack_idx < INCSTACK_SIZE) | |
1750 { | |
1751 /* Save current "fp" and "tag_fname" in the stack. */ | |
1752 if ((incstack[incstack_idx].etag_fname = | |
1753 vim_strsave(tag_fname)) != NULL) | |
1754 { | |
1755 char_u *fullpath_ebuf; | |
1756 | |
1757 incstack[incstack_idx].fp = fp; | |
1758 fp = NULL; | |
1759 | |
1760 /* Figure out "tag_fname" and "fp" to use for | |
1761 * included file. */ | |
1762 fullpath_ebuf = expand_tag_fname(ebuf, | |
1763 tag_fname, FALSE); | |
1764 if (fullpath_ebuf != NULL) | |
1765 { | |
1766 fp = mch_fopen((char *)fullpath_ebuf, "r"); | |
1767 if (fp != NULL) | |
1768 { | |
1769 if (STRLEN(fullpath_ebuf) > LSIZE) | |
1770 EMSG2(_("E430: Tag file path truncated for %s\n"), ebuf); | |
418 | 1771 vim_strncpy(tag_fname, fullpath_ebuf, |
1772 MAXPATHL); | |
7 | 1773 ++incstack_idx; |
1774 is_etag = 0; /* we can include anything */ | |
1775 } | |
1776 vim_free(fullpath_ebuf); | |
1777 } | |
1778 if (fp == NULL) | |
1779 { | |
1780 /* Can't open the included file, skip it and | |
1781 * restore old value of "fp". */ | |
1782 fp = incstack[incstack_idx].fp; | |
1783 vim_free(incstack[incstack_idx].etag_fname); | |
1784 } | |
1785 } | |
1786 } | |
1787 } | |
1788 continue; | |
1789 } | |
1790 #endif | |
1791 | |
1792 /* | |
1793 * When still at the start of the file, check for Emacs tags file | |
1794 * format, and for "not sorted" flag. | |
1795 */ | |
1796 if (state == TS_START) | |
1797 { | |
3131 | 1798 if (STRNCMP(lbuf, "!_TAG_", 6) <= 0) |
1799 { | |
1800 /* | |
1801 * Read header line. | |
1802 */ | |
1803 #ifdef FEAT_TAG_BINS | |
1804 if (STRNCMP(lbuf, "!_TAG_FILE_SORTED\t", 18) == 0) | |
1805 tag_file_sorted = lbuf[18]; | |
1806 #endif | |
1807 #ifdef FEAT_MBYTE | |
1808 if (STRNCMP(lbuf, "!_TAG_FILE_ENCODING\t", 20) == 0) | |
1809 { | |
1810 /* Prepare to convert every line from the specified | |
1811 * encoding to 'encoding'. */ | |
1812 for (p = lbuf + 20; *p > ' ' && *p < 127; ++p) | |
1813 ; | |
1814 *p = NUL; | |
1815 convert_setup(&vimconv, lbuf + 20, p_enc); | |
1816 } | |
1817 #endif | |
1818 | |
1819 /* Read the next line. Unrecognized flags are ignored. */ | |
1820 continue; | |
1821 } | |
1822 | |
1823 /* Headers ends. */ | |
1824 | |
7 | 1825 #ifdef FEAT_TAG_BINS |
1826 /* | |
1827 * When there is no tag head, or ignoring case, need to do a | |
1828 * linear search. | |
1829 * When no "!_TAG_" is found, default to binary search. If | |
1830 * the tag file isn't sorted, the second loop will find it. | |
1831 * When "!_TAG_FILE_SORTED" found: start binary search if | |
1832 * flag set. | |
1833 * For cscope, it's always linear. | |
1834 */ | |
1835 # ifdef FEAT_CSCOPE | |
1836 if (linear || use_cscope) | |
1837 # else | |
1838 if (linear) | |
1839 # endif | |
1840 state = TS_LINEAR; | |
3131 | 1841 else if (tag_file_sorted == NUL) |
7 | 1842 state = TS_BINARY; |
3131 | 1843 else if (tag_file_sorted == '1') |
7 | 1844 state = TS_BINARY; |
3131 | 1845 else if (tag_file_sorted == '2') |
1846 { | |
1847 state = TS_BINARY; | |
1848 sortic = TRUE; | |
1849 orgpat.regmatch.rm_ic = (p_ic || !noic); | |
7 | 1850 } |
3131 | 1851 else |
1852 state = TS_LINEAR; | |
1853 | |
1854 if (state == TS_BINARY && orgpat.regmatch.rm_ic && !sortic) | |
7 | 1855 { |
3206 | 1856 /* Binary search won't work for ignoring case, use linear |
7 | 1857 * search. */ |
1858 linear = TRUE; | |
1859 state = TS_LINEAR; | |
1860 } | |
1861 #else | |
1862 state = TS_LINEAR; | |
1863 #endif | |
1864 | |
1865 #ifdef FEAT_TAG_BINS | |
1866 /* | |
1867 * When starting a binary search, get the size of the file and | |
1868 * compute the first offset. | |
1869 */ | |
1870 if (state == TS_BINARY) | |
1871 { | |
1872 /* Get the tag file size (don't use mch_fstat(), it's not | |
1873 * portable). */ | |
1874 if ((filesize = lseek(fileno(fp), | |
1875 (off_t)0L, SEEK_END)) <= 0) | |
1876 state = TS_LINEAR; | |
1877 else | |
1878 { | |
1879 lseek(fileno(fp), (off_t)0L, SEEK_SET); | |
1880 | |
1881 /* Calculate the first read offset in the file. Start | |
1882 * the search in the middle of the file. */ | |
1883 search_info.low_offset = 0; | |
1884 search_info.low_char = 0; | |
1885 search_info.high_offset = filesize; | |
1886 search_info.curr_offset = 0; | |
1887 search_info.high_char = 0xff; | |
1888 } | |
1889 continue; | |
1890 } | |
1891 #endif | |
1892 } | |
1893 | |
1894 /* | |
1895 * Figure out where the different strings are in this line. | |
1896 * For "normal" tags: Do a quick check if the tag matches. | |
1897 * This speeds up tag searching a lot! | |
1898 */ | |
3131 | 1899 if (orgpat.headlen |
7 | 1900 #ifdef FEAT_EMACS_TAGS |
1901 && !is_etag | |
1902 #endif | |
1903 ) | |
1904 { | |
1905 tagp.tagname = lbuf; | |
1906 #ifdef FEAT_TAG_ANYWHITE | |
1907 tagp.tagname_end = skiptowhite(lbuf); | |
3192 | 1908 if (*tagp.tagname_end == NUL) |
7 | 1909 #else |
1910 tagp.tagname_end = vim_strchr(lbuf, TAB); | |
3192 | 1911 if (tagp.tagname_end == NULL) |
7 | 1912 #endif |
1913 { | |
3192 | 1914 if (vim_strchr(lbuf, NL) == NULL) |
1915 { | |
1916 /* Truncated line, ignore it. Has been reported for | |
1917 * Mozilla JS with extremely long names. */ | |
1918 if (p_verbose >= 5) | |
1919 { | |
1920 verbose_enter(); | |
1921 MSG(_("Ignoring long line in tags file")); | |
1922 verbose_leave(); | |
1923 } | |
3206 | 1924 #ifdef FEAT_TAG_BINS |
1925 if (state != TS_LINEAR) | |
1926 { | |
1927 /* Avoid getting stuck. */ | |
1928 linear = TRUE; | |
1929 state = TS_LINEAR; | |
1930 # ifdef HAVE_FSEEKO | |
1931 fseeko(fp, search_info.low_offset, SEEK_SET); | |
1932 # else | |
1933 fseek(fp, (long)search_info.low_offset, SEEK_SET); | |
1934 # endif | |
1935 } | |
1936 #endif | |
3192 | 1937 continue; |
1938 } | |
1939 | |
1940 /* Corrupted tag line. */ | |
7 | 1941 line_error = TRUE; |
1942 break; | |
1943 } | |
1944 | |
1945 #ifdef FEAT_TAG_OLDSTATIC | |
1946 /* | |
1947 * Check for old style static tag: "file:tag file .." | |
1948 */ | |
1949 tagp.fname = NULL; | |
1950 for (p = lbuf; p < tagp.tagname_end; ++p) | |
1951 { | |
1952 if (*p == ':') | |
1953 { | |
1954 if (tagp.fname == NULL) | |
1955 #ifdef FEAT_TAG_ANYWHITE | |
1956 tagp.fname = skipwhite(tagp.tagname_end); | |
1957 #else | |
1958 tagp.fname = tagp.tagname_end + 1; | |
1959 #endif | |
1960 if ( fnamencmp(lbuf, tagp.fname, p - lbuf) == 0 | |
1961 #ifdef FEAT_TAG_ANYWHITE | |
1962 && vim_iswhite(tagp.fname[p - lbuf]) | |
1963 #else | |
1964 && tagp.fname[p - lbuf] == TAB | |
1965 #endif | |
1966 ) | |
1967 { | |
1968 /* found one */ | |
1969 tagp.tagname = p + 1; | |
1970 break; | |
1971 } | |
1972 } | |
1973 } | |
1974 #endif | |
1975 | |
1976 /* | |
1977 * Skip this line if the length of the tag is different and | |
1978 * there is no regexp, or the tag is too short. | |
1979 */ | |
1980 cmplen = (int)(tagp.tagname_end - tagp.tagname); | |
1981 if (p_tl != 0 && cmplen > p_tl) /* adjust for 'taglength' */ | |
1982 cmplen = p_tl; | |
3131 | 1983 if (has_re && orgpat.headlen < cmplen) |
1984 cmplen = orgpat.headlen; | |
1985 else if (state == TS_LINEAR && orgpat.headlen != cmplen) | |
7 | 1986 continue; |
1987 | |
1988 #ifdef FEAT_TAG_BINS | |
1989 if (state == TS_BINARY) | |
1990 { | |
1991 /* | |
1992 * Simplistic check for unsorted tags file. | |
1993 */ | |
1994 i = (int)tagp.tagname[0]; | |
1995 if (sortic) | |
1996 i = (int)TOUPPER_ASC(tagp.tagname[0]); | |
1997 if (i < search_info.low_char || i > search_info.high_char) | |
1998 sort_error = TRUE; | |
1999 | |
2000 /* | |
2001 * Compare the current tag with the searched tag. | |
2002 */ | |
2003 if (sortic) | |
3131 | 2004 tagcmp = tag_strnicmp(tagp.tagname, orgpat.head, |
7 | 2005 (size_t)cmplen); |
2006 else | |
3131 | 2007 tagcmp = STRNCMP(tagp.tagname, orgpat.head, cmplen); |
7 | 2008 |
2009 /* | |
2010 * A match with a shorter tag means to search forward. | |
2011 * A match with a longer tag means to search backward. | |
2012 */ | |
2013 if (tagcmp == 0) | |
2014 { | |
3131 | 2015 if (cmplen < orgpat.headlen) |
7 | 2016 tagcmp = -1; |
3131 | 2017 else if (cmplen > orgpat.headlen) |
7 | 2018 tagcmp = 1; |
2019 } | |
2020 | |
2021 if (tagcmp == 0) | |
2022 { | |
2023 /* We've located the tag, now skip back and search | |
2024 * forward until the first matching tag is found. | |
2025 */ | |
2026 state = TS_SKIP_BACK; | |
2027 search_info.match_offset = search_info.curr_offset; | |
2028 continue; | |
2029 } | |
2030 if (tagcmp < 0) | |
2031 { | |
2032 search_info.curr_offset = ftell(fp); | |
2033 if (search_info.curr_offset < search_info.high_offset) | |
2034 { | |
2035 search_info.low_offset = search_info.curr_offset; | |
2036 if (sortic) | |
2037 search_info.low_char = | |
2038 TOUPPER_ASC(tagp.tagname[0]); | |
2039 else | |
2040 search_info.low_char = tagp.tagname[0]; | |
2041 continue; | |
2042 } | |
2043 } | |
2044 if (tagcmp > 0 | |
2045 && search_info.curr_offset != search_info.high_offset) | |
2046 { | |
2047 search_info.high_offset = search_info.curr_offset; | |
2048 if (sortic) | |
2049 search_info.high_char = | |
2050 TOUPPER_ASC(tagp.tagname[0]); | |
2051 else | |
2052 search_info.high_char = tagp.tagname[0]; | |
2053 continue; | |
2054 } | |
2055 | |
2056 /* No match yet and are at the end of the binary search. */ | |
2057 break; | |
2058 } | |
2059 else if (state == TS_SKIP_BACK) | |
2060 { | |
3131 | 2061 if (MB_STRNICMP(tagp.tagname, orgpat.head, cmplen) != 0) |
7 | 2062 state = TS_STEP_FORWARD; |
2063 else | |
2064 /* Have to skip back more. Restore the curr_offset | |
2065 * used, otherwise we get stuck at a long line. */ | |
2066 search_info.curr_offset = search_info.curr_offset_used; | |
2067 continue; | |
2068 } | |
2069 else if (state == TS_STEP_FORWARD) | |
2070 { | |
3131 | 2071 if (MB_STRNICMP(tagp.tagname, orgpat.head, cmplen) != 0) |
7 | 2072 { |
2073 if ((off_t)ftell(fp) > search_info.match_offset) | |
2074 break; /* past last match */ | |
2075 else | |
2076 continue; /* before first match */ | |
2077 } | |
2078 } | |
2079 else | |
2080 #endif | |
2081 /* skip this match if it can't match */ | |
3131 | 2082 if (MB_STRNICMP(tagp.tagname, orgpat.head, cmplen) != 0) |
7 | 2083 continue; |
2084 | |
2085 /* | |
2086 * Can be a matching tag, isolate the file name and command. | |
2087 */ | |
2088 #ifdef FEAT_TAG_OLDSTATIC | |
2089 if (tagp.fname == NULL) | |
2090 #endif | |
2091 #ifdef FEAT_TAG_ANYWHITE | |
2092 tagp.fname = skipwhite(tagp.tagname_end); | |
2093 #else | |
2094 tagp.fname = tagp.tagname_end + 1; | |
2095 #endif | |
2096 #ifdef FEAT_TAG_ANYWHITE | |
2097 tagp.fname_end = skiptowhite(tagp.fname); | |
2098 tagp.command = skipwhite(tagp.fname_end); | |
2099 if (*tagp.command == NUL) | |
2100 #else | |
2101 tagp.fname_end = vim_strchr(tagp.fname, TAB); | |
2102 tagp.command = tagp.fname_end + 1; | |
2103 if (tagp.fname_end == NULL) | |
2104 #endif | |
2105 i = FAIL; | |
2106 else | |
2107 i = OK; | |
2108 } | |
2109 else | |
2110 i = parse_tag_line(lbuf, | |
2111 #ifdef FEAT_EMACS_TAGS | |
2112 is_etag, | |
2113 #endif | |
2114 &tagp); | |
2115 if (i == FAIL) | |
2116 { | |
2117 line_error = TRUE; | |
2118 break; | |
2119 } | |
2120 | |
2121 #ifdef FEAT_EMACS_TAGS | |
2122 if (is_etag) | |
2123 tagp.fname = ebuf; | |
2124 #endif | |
2125 /* | |
2126 * First try matching with the pattern literally (also when it is | |
2127 * a regexp). | |
2128 */ | |
2129 cmplen = (int)(tagp.tagname_end - tagp.tagname); | |
2130 if (p_tl != 0 && cmplen > p_tl) /* adjust for 'taglength' */ | |
2131 cmplen = p_tl; | |
2132 /* if tag length does not match, don't try comparing */ | |
3131 | 2133 if (orgpat.len != cmplen) |
7 | 2134 match = FALSE; |
2135 else | |
2136 { | |
3131 | 2137 if (orgpat.regmatch.rm_ic) |
7 | 2138 { |
3131 | 2139 match = (MB_STRNICMP(tagp.tagname, orgpat.pat, cmplen) == 0); |
7 | 2140 if (match) |
3131 | 2141 match_no_ic = (STRNCMP(tagp.tagname, orgpat.pat, |
7 | 2142 cmplen) == 0); |
2143 } | |
2144 else | |
3131 | 2145 match = (STRNCMP(tagp.tagname, orgpat.pat, cmplen) == 0); |
7 | 2146 } |
2147 | |
2148 /* | |
2149 * Has a regexp: Also find tags matching regexp. | |
2150 */ | |
2151 match_re = FALSE; | |
3131 | 2152 if (!match && orgpat.regmatch.regprog != NULL) |
7 | 2153 { |
2154 int cc; | |
2155 | |
2156 cc = *tagp.tagname_end; | |
2157 *tagp.tagname_end = NUL; | |
3131 | 2158 match = vim_regexec(&orgpat.regmatch, tagp.tagname, (colnr_T)0); |
7 | 2159 if (match) |
2160 { | |
3131 | 2161 matchoff = (int)(orgpat.regmatch.startp[0] - tagp.tagname); |
2162 if (orgpat.regmatch.rm_ic) | |
7 | 2163 { |
3131 | 2164 orgpat.regmatch.rm_ic = FALSE; |
2165 match_no_ic = vim_regexec(&orgpat.regmatch, tagp.tagname, | |
7 | 2166 (colnr_T)0); |
3131 | 2167 orgpat.regmatch.rm_ic = TRUE; |
7 | 2168 } |
2169 } | |
2170 *tagp.tagname_end = cc; | |
2171 match_re = TRUE; | |
2172 } | |
2173 | |
2174 /* | |
2175 * If a match is found, add it to ga_match[]. | |
2176 */ | |
2177 if (match) | |
2178 { | |
2179 #ifdef FEAT_CSCOPE | |
2180 if (use_cscope) | |
2181 { | |
2182 /* Don't change the ordering, always use the same table. */ | |
2183 mtt = MT_GL_OTH; | |
2184 } | |
2185 else | |
2186 #endif | |
2187 { | |
2188 /* Decide in which array to store this match. */ | |
2189 is_current = test_for_current( | |
2190 #ifdef FEAT_EMACS_TAGS | |
2191 is_etag, | |
2192 #endif | |
2193 tagp.fname, tagp.fname_end, tag_fname, | |
2194 buf_ffname); | |
2195 #ifdef FEAT_EMACS_TAGS | |
2196 is_static = FALSE; | |
2197 if (!is_etag) /* emacs tags are never static */ | |
2198 #endif | |
2199 { | |
2200 #ifdef FEAT_TAG_OLDSTATIC | |
2201 if (tagp.tagname != lbuf) | |
2202 is_static = TRUE; /* detected static tag before */ | |
2203 else | |
2204 #endif | |
2205 is_static = test_for_static(&tagp); | |
2206 } | |
2207 | |
2208 /* decide in which of the sixteen tables to store this | |
2209 * match */ | |
2210 if (is_static) | |
2211 { | |
2212 if (is_current) | |
2213 mtt = MT_ST_CUR; | |
2214 else | |
2215 mtt = MT_ST_OTH; | |
2216 } | |
2217 else | |
2218 { | |
2219 if (is_current) | |
2220 mtt = MT_GL_CUR; | |
2221 else | |
2222 mtt = MT_GL_OTH; | |
2223 } | |
3131 | 2224 if (orgpat.regmatch.rm_ic && !match_no_ic) |
7 | 2225 mtt += MT_IC_OFF; |
2226 if (match_re) | |
2227 mtt += MT_RE_OFF; | |
2228 } | |
2229 | |
2230 /* | |
2231 * Add the found match in ga_match[mtt], avoiding duplicates. | |
2232 * Store the info we need later, which depends on the kind of | |
2233 * tags we are dealing with. | |
2234 */ | |
2235 if (ga_grow(&ga_match[mtt], 1) == OK) | |
2236 { | |
3263 | 2237 int len; |
2238 | |
7 | 2239 if (help_only) |
2240 { | |
2241 #ifdef FEAT_MULTI_LANG | |
2242 # define ML_EXTRA 3 | |
2243 #else | |
2244 # define ML_EXTRA 0 | |
2245 #endif | |
2246 /* | |
2247 * Append the help-heuristic number after the | |
2248 * tagname, for sorting it later. | |
2249 */ | |
2250 *tagp.tagname_end = NUL; | |
2251 len = (int)(tagp.tagname_end - tagp.tagname); | |
2252 mfp = (struct match_found *) | |
2253 alloc((int)sizeof(struct match_found) + len | |
2254 + 10 + ML_EXTRA); | |
2255 if (mfp != NULL) | |
2256 { | |
2257 /* "len" includes the language and the NUL, but | |
2258 * not the priority. */ | |
2259 mfp->len = len + ML_EXTRA + 1; | |
2260 #define ML_HELP_LEN 6 | |
2261 p = mfp->match; | |
2262 STRCPY(p, tagp.tagname); | |
2263 #ifdef FEAT_MULTI_LANG | |
2264 p[len] = '@'; | |
2265 STRCPY(p + len + 1, help_lang); | |
2266 #endif | |
2267 sprintf((char *)p + len + 1 + ML_EXTRA, "%06d", | |
2268 help_heuristic(tagp.tagname, | |
2269 match_re ? matchoff : 0, !match_no_ic) | |
2270 #ifdef FEAT_MULTI_LANG | |
2271 + help_pri | |
2272 #endif | |
2273 ); | |
2274 } | |
2275 *tagp.tagname_end = TAB; | |
2276 } | |
2277 else if (name_only) | |
2278 { | |
2279 if (get_it_again) | |
2280 { | |
2281 char_u *temp_end = tagp.command; | |
2282 | |
2283 if (*temp_end == '/') | |
2284 while (*temp_end && *temp_end != '\r' | |
2285 && *temp_end != '\n' | |
2286 && *temp_end != '$') | |
2287 temp_end++; | |
2288 | |
2289 if (tagp.command + 2 < temp_end) | |
2290 { | |
2291 len = (int)(temp_end - tagp.command - 2); | |
2292 mfp = (struct match_found *)alloc( | |
2293 (int)sizeof(struct match_found) + len); | |
2294 if (mfp != NULL) | |
2295 { | |
2296 mfp->len = len + 1; /* include the NUL */ | |
2297 p = mfp->match; | |
418 | 2298 vim_strncpy(p, tagp.command + 2, len); |
7 | 2299 } |
2300 } | |
2301 else | |
2302 mfp = NULL; | |
2303 get_it_again = FALSE; | |
2304 } | |
2305 else | |
2306 { | |
2307 len = (int)(tagp.tagname_end - tagp.tagname); | |
2308 mfp = (struct match_found *)alloc( | |
2309 (int)sizeof(struct match_found) + len); | |
2310 if (mfp != NULL) | |
2311 { | |
2312 mfp->len = len + 1; /* include the NUL */ | |
2313 p = mfp->match; | |
418 | 2314 vim_strncpy(p, tagp.tagname, len); |
7 | 2315 } |
2316 | |
2317 /* if wanted, re-read line to get long form too */ | |
2318 if (State & INSERT) | |
2319 get_it_again = p_sft; | |
2320 } | |
2321 } | |
2322 else | |
2323 { | |
2324 /* Save the tag in a buffer. | |
2325 * Emacs tag: <mtt><tag_fname><NUL><ebuf><NUL><lbuf> | |
2326 * other tag: <mtt><tag_fname><NUL><NUL><lbuf> | |
2327 * without Emacs tags: <mtt><tag_fname><NUL><lbuf> | |
2328 */ | |
359 | 2329 len = (int)STRLEN(tag_fname) |
3131 | 2330 + (int)STRLEN(lbuf) + 3; |
7 | 2331 #ifdef FEAT_EMACS_TAGS |
2332 if (is_etag) | |
2333 len += (int)STRLEN(ebuf) + 1; | |
2334 else | |
2335 ++len; | |
2336 #endif | |
2337 mfp = (struct match_found *)alloc( | |
2338 (int)sizeof(struct match_found) + len); | |
2339 if (mfp != NULL) | |
2340 { | |
2341 mfp->len = len; | |
2342 p = mfp->match; | |
2343 p[0] = mtt; | |
2344 STRCPY(p + 1, tag_fname); | |
2345 #ifdef BACKSLASH_IN_FILENAME | |
2346 /* Ignore differences in slashes, avoid adding | |
2347 * both path/file and path\file. */ | |
2348 slash_adjust(p + 1); | |
2349 #endif | |
2350 s = p + 1 + STRLEN(tag_fname) + 1; | |
2351 #ifdef FEAT_EMACS_TAGS | |
2352 if (is_etag) | |
2353 { | |
2354 STRCPY(s, ebuf); | |
2355 s += STRLEN(ebuf) + 1; | |
2356 } | |
2357 else | |
2358 *s++ = NUL; | |
2359 #endif | |
3131 | 2360 STRCPY(s, lbuf); |
7 | 2361 } |
2362 } | |
2363 | |
2364 if (mfp != NULL) | |
2365 { | |
2366 /* | |
2367 * Don't add identical matches. | |
2368 * This can take a lot of time when finding many | |
2369 * matches, check for CTRL-C now and then. | |
2370 * Add all cscope tags, because they are all listed. | |
2371 */ | |
2372 #ifdef FEAT_CSCOPE | |
2373 if (use_cscope) | |
2374 i = -1; | |
2375 else | |
2376 #endif | |
2377 for (i = ga_match[mtt].ga_len; --i >= 0 && !got_int; ) | |
2378 { | |
2379 mfp2 = ((struct match_found **) | |
2380 (ga_match[mtt].ga_data))[i]; | |
2381 if (mfp2->len == mfp->len | |
2382 && vim_memcmp(mfp2->match, mfp->match, | |
2383 (size_t)mfp->len) == 0) | |
2384 break; | |
2385 line_breakcheck(); | |
2386 } | |
2387 if (i < 0) | |
2388 { | |
2389 ((struct match_found **)(ga_match[mtt].ga_data)) | |
2390 [ga_match[mtt].ga_len++] = mfp; | |
2391 ++match_count; | |
2392 } | |
2393 else | |
2394 vim_free(mfp); | |
2395 } | |
2396 } | |
2397 else /* Out of memory! Just forget about the rest. */ | |
2398 { | |
2399 retval = OK; | |
2400 stop_searching = TRUE; | |
2401 break; | |
2402 } | |
2403 } | |
2404 #ifdef FEAT_CSCOPE | |
2405 if (use_cscope && eof) | |
2406 break; | |
2407 #endif | |
2408 } /* forever */ | |
2409 | |
2410 if (line_error) | |
2411 { | |
2412 EMSG2(_("E431: Format error in tags file \"%s\""), tag_fname); | |
2413 #ifdef FEAT_CSCOPE | |
2414 if (!use_cscope) | |
2415 #endif | |
2416 EMSGN(_("Before byte %ld"), (long)ftell(fp)); | |
2417 stop_searching = TRUE; | |
2418 line_error = FALSE; | |
2419 } | |
2420 | |
2421 #ifdef FEAT_CSCOPE | |
2422 if (!use_cscope) | |
2423 #endif | |
2424 fclose(fp); | |
2425 #ifdef FEAT_EMACS_TAGS | |
2426 while (incstack_idx) | |
2427 { | |
2428 --incstack_idx; | |
2429 fclose(incstack[incstack_idx].fp); | |
2430 vim_free(incstack[incstack_idx].etag_fname); | |
2431 } | |
2432 #endif | |
2433 #ifdef FEAT_MBYTE | |
2434 if (vimconv.vc_type != CONV_NONE) | |
2435 convert_setup(&vimconv, NULL, NULL); | |
2436 #endif | |
2437 | |
2438 #ifdef FEAT_TAG_BINS | |
3131 | 2439 tag_file_sorted = NUL; |
7 | 2440 if (sort_error) |
2441 { | |
2442 EMSG2(_("E432: Tags file not sorted: %s"), tag_fname); | |
2443 sort_error = FALSE; | |
2444 } | |
2445 #endif | |
2446 | |
2447 /* | |
2448 * Stop searching if sufficient tags have been found. | |
2449 */ | |
2450 if (match_count >= mincount) | |
2451 { | |
2452 retval = OK; | |
2453 stop_searching = TRUE; | |
2454 } | |
2455 | |
2456 #ifdef FEAT_CSCOPE | |
2457 if (stop_searching || use_cscope) | |
2458 #else | |
2459 if (stop_searching) | |
2460 #endif | |
2461 break; | |
2462 | |
2463 } /* end of for-each-file loop */ | |
2464 | |
692 | 2465 #ifdef FEAT_CSCOPE |
2466 if (!use_cscope) | |
2467 #endif | |
2468 tagname_free(&tn); | |
2469 | |
7 | 2470 #ifdef FEAT_TAG_BINS |
413 | 2471 /* stop searching when already did a linear search, or when TAG_NOIC |
2472 * used, and 'ignorecase' not set or already did case-ignore search */ | |
3131 | 2473 if (stop_searching || linear || (!p_ic && noic) || orgpat.regmatch.rm_ic) |
7 | 2474 break; |
2475 # ifdef FEAT_CSCOPE | |
2476 if (use_cscope) | |
2477 break; | |
2478 # endif | |
3131 | 2479 orgpat.regmatch.rm_ic = TRUE; /* try another time while ignoring case */ |
7 | 2480 } |
2481 #endif | |
2482 | |
2483 if (!stop_searching) | |
2484 { | |
2485 if (!did_open && verbose) /* never opened any tags file */ | |
2486 EMSG(_("E433: No tags file")); | |
2487 retval = OK; /* It's OK even when no tag found */ | |
2488 } | |
2489 | |
2490 findtag_end: | |
2491 vim_free(lbuf); | |
3131 | 2492 vim_free(orgpat.regmatch.regprog); |
7 | 2493 vim_free(tag_fname); |
2494 #ifdef FEAT_EMACS_TAGS | |
2495 vim_free(ebuf); | |
2496 #endif | |
2497 | |
2498 /* | |
2499 * Move the matches from the ga_match[] arrays into one list of | |
2500 * matches. When retval == FAIL, free the matches. | |
2501 */ | |
2502 if (retval == FAIL) | |
2503 match_count = 0; | |
2504 | |
2505 if (match_count > 0) | |
2506 matches = (char_u **)lalloc((long_u)(match_count * sizeof(char_u *)), | |
2507 TRUE); | |
2508 else | |
2509 matches = NULL; | |
2510 match_count = 0; | |
2511 for (mtt = 0; mtt < MT_COUNT; ++mtt) | |
2512 { | |
2513 for (i = 0; i < ga_match[mtt].ga_len; ++i) | |
2514 { | |
2515 mfp = ((struct match_found **)(ga_match[mtt].ga_data))[i]; | |
2516 if (matches == NULL) | |
2517 vim_free(mfp); | |
2518 else | |
2519 { | |
2520 /* To avoid allocating memory again we turn the struct | |
2521 * match_found into a string. For help the priority was not | |
2522 * included in the length. */ | |
2523 mch_memmove(mfp, mfp->match, | |
2524 (size_t)(mfp->len + (help_only ? ML_HELP_LEN : 0))); | |
2525 matches[match_count++] = (char_u *)mfp; | |
2526 } | |
2527 } | |
2528 ga_clear(&ga_match[mtt]); | |
2529 } | |
2530 | |
2531 *matchesp = matches; | |
2532 *num_matches = match_count; | |
2533 | |
2534 curbuf->b_help = help_save; | |
2535 #ifdef FEAT_MULTI_LANG | |
2536 vim_free(saved_pat); | |
2537 #endif | |
2538 | |
2539 return retval; | |
2540 } | |
2541 | |
2542 static garray_T tag_fnames = GA_EMPTY; | |
236 | 2543 static void found_tagfile_cb __ARGS((char_u *fname, void *cookie)); |
7 | 2544 |
2545 /* | |
2546 * Callback function for finding all "tags" and "tags-??" files in | |
2547 * 'runtimepath' doc directories. | |
2548 */ | |
2549 static void | |
236 | 2550 found_tagfile_cb(fname, cookie) |
7 | 2551 char_u *fname; |
1877 | 2552 void *cookie UNUSED; |
7 | 2553 { |
2554 if (ga_grow(&tag_fnames, 1) == OK) | |
2555 ((char_u **)(tag_fnames.ga_data))[tag_fnames.ga_len++] = | |
2556 vim_strsave(fname); | |
2557 } | |
2558 | |
359 | 2559 #if defined(EXITFREE) || defined(PROTO) |
2560 void | |
2561 free_tag_stuff() | |
2562 { | |
692 | 2563 ga_clear_strings(&tag_fnames); |
359 | 2564 do_tag(NULL, DT_FREE, 0, 0, 0); |
1818 | 2565 tag_freematch(); |
2566 | |
2567 # if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) | |
2568 if (ptag_entry.tagname) | |
2569 { | |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2185
diff
changeset
|
2570 vim_free(ptag_entry.tagname); |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2185
diff
changeset
|
2571 ptag_entry.tagname = NULL; |
1818 | 2572 } |
2573 # endif | |
359 | 2574 } |
2575 #endif | |
2576 | |
7 | 2577 /* |
2578 * Get the next name of a tag file from the tag file list. | |
2579 * For help files, use "tags" file only. | |
2580 * | |
2581 * Return FAIL if no more tag file names, OK otherwise. | |
2582 */ | |
514 | 2583 int |
692 | 2584 get_tagfname(tnp, first, buf) |
2585 tagname_T *tnp; /* holds status info */ | |
7 | 2586 int first; /* TRUE when first file name is wanted */ |
2587 char_u *buf; /* pointer to buffer of MAXPATHL chars */ | |
2588 { | |
2589 char_u *fname = NULL; | |
2590 char_u *r_ptr; | |
2591 | |
692 | 2592 if (first) |
2593 vim_memset(tnp, 0, sizeof(tagname_T)); | |
2594 | |
514 | 2595 if (curbuf->b_help) |
7 | 2596 { |
514 | 2597 /* |
2598 * For help files it's done in a completely different way: | |
2599 * Find "doc/tags" and "doc/tags-??" in all directories in | |
2600 * 'runtimepath'. | |
2601 */ | |
2602 if (first) | |
7 | 2603 { |
2604 ga_clear_strings(&tag_fnames); | |
2605 ga_init2(&tag_fnames, (int)sizeof(char_u *), 10); | |
2606 do_in_runtimepath((char_u *) | |
2607 #ifdef FEAT_MULTI_LANG | |
36 | 2608 # ifdef VMS |
2609 /* Functions decc$to_vms() and decc$translate_vms() crash | |
2610 * on some VMS systems with wildcards "??". Seems ECO | |
2611 * patches do fix the problem in C RTL, but we can't use | |
2612 * an #ifdef for that. */ | |
2613 "doc/tags doc/tags-*" | |
2614 # else | |
7 | 2615 "doc/tags doc/tags-??" |
36 | 2616 # endif |
7 | 2617 #else |
2618 "doc/tags" | |
2619 #endif | |
236 | 2620 , TRUE, found_tagfile_cb, NULL); |
7 | 2621 } |
602 | 2622 |
692 | 2623 if (tnp->tn_hf_idx >= tag_fnames.ga_len) |
7 | 2624 { |
2625 /* Not found in 'runtimepath', use 'helpfile', if it exists and | |
2626 * wasn't used yet, replacing "help.txt" with "tags". */ | |
692 | 2627 if (tnp->tn_hf_idx > tag_fnames.ga_len || *p_hf == NUL) |
7 | 2628 return FAIL; |
692 | 2629 ++tnp->tn_hf_idx; |
7 | 2630 STRCPY(buf, p_hf); |
2631 STRCPY(gettail(buf), "tags"); | |
2632 } | |
2633 else | |
692 | 2634 vim_strncpy(buf, ((char_u **)(tag_fnames.ga_data))[ |
2635 tnp->tn_hf_idx++], MAXPATHL - 1); | |
514 | 2636 return OK; |
2637 } | |
2638 | |
2639 if (first) | |
2640 { | |
692 | 2641 /* Init. We make a copy of 'tags', because autocommands may change |
2642 * the value without notifying us. */ | |
2643 tnp->tn_tags = vim_strsave((*curbuf->b_p_tags != NUL) | |
2644 ? curbuf->b_p_tags : p_tags); | |
2645 if (tnp->tn_tags == NULL) | |
2646 return FAIL; | |
2647 tnp->tn_np = tnp->tn_tags; | |
7 | 2648 } |
692 | 2649 |
2650 /* | |
2651 * Loop until we have found a file name that can be used. | |
2652 * There are two states: | |
2653 * tnp->tn_did_filefind_init == FALSE: setup for next part in 'tags'. | |
2654 * tnp->tn_did_filefind_init == TRUE: find next file in this part. | |
2655 */ | |
2656 for (;;) | |
7 | 2657 { |
692 | 2658 if (tnp->tn_did_filefind_init) |
7 | 2659 { |
692 | 2660 fname = vim_findfile(tnp->tn_search_ctx); |
2661 if (fname != NULL) | |
2662 break; | |
2663 | |
2664 tnp->tn_did_filefind_init = FALSE; | |
2665 } | |
2666 else | |
2667 { | |
2668 char_u *filename = NULL; | |
2669 | |
2670 /* Stop when used all parts of 'tags'. */ | |
2671 if (*tnp->tn_np == NUL) | |
7 | 2672 { |
692 | 2673 vim_findfile_cleanup(tnp->tn_search_ctx); |
2674 tnp->tn_search_ctx = NULL; | |
2675 return FAIL; | |
7 | 2676 } |
692 | 2677 |
2678 /* | |
2679 * Copy next file name into buf. | |
2680 */ | |
2681 buf[0] = NUL; | |
2682 (void)copy_option_part(&tnp->tn_np, buf, MAXPATHL - 1, " ,"); | |
7 | 2683 |
2684 #ifdef FEAT_PATH_EXTRA | |
692 | 2685 r_ptr = vim_findfile_stopdir(buf); |
7 | 2686 #else |
692 | 2687 r_ptr = NULL; |
7 | 2688 #endif |
692 | 2689 /* move the filename one char forward and truncate the |
2690 * filepath with a NUL */ | |
2691 filename = gettail(buf); | |
1620 | 2692 STRMOVE(filename + 1, filename); |
692 | 2693 *filename++ = NUL; |
2694 | |
2695 tnp->tn_search_ctx = vim_findfile_init(buf, filename, | |
2696 r_ptr, 100, | |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2185
diff
changeset
|
2697 FALSE, /* don't free visited list */ |
1541 | 2698 FINDFILE_FILE, /* we search for a file */ |
692 | 2699 tnp->tn_search_ctx, TRUE, curbuf->b_ffname); |
2700 if (tnp->tn_search_ctx != NULL) | |
2701 tnp->tn_did_filefind_init = TRUE; | |
7 | 2702 } |
2703 } | |
2704 | |
692 | 2705 STRCPY(buf, fname); |
2706 vim_free(fname); | |
7 | 2707 return OK; |
2708 } | |
2709 | |
2710 /* | |
692 | 2711 * Free the contents of a tagname_T that was filled by get_tagfname(). |
2712 */ | |
2713 void | |
2714 tagname_free(tnp) | |
2715 tagname_T *tnp; | |
2716 { | |
2717 vim_free(tnp->tn_tags); | |
2718 vim_findfile_cleanup(tnp->tn_search_ctx); | |
1541 | 2719 tnp->tn_search_ctx = NULL; |
692 | 2720 ga_clear_strings(&tag_fnames); |
2721 } | |
2722 | |
2723 /* | |
7 | 2724 * Parse one line from the tags file. Find start/end of tag name, start/end of |
2725 * file name and start of search pattern. | |
2726 * | |
2727 * If is_etag is TRUE, tagp->fname and tagp->fname_end are not set. | |
2728 * | |
2729 * Return FAIL if there is a format error in this line, OK otherwise. | |
2730 */ | |
2731 static int | |
2732 parse_tag_line(lbuf, | |
2733 #ifdef FEAT_EMACS_TAGS | |
2734 is_etag, | |
2735 #endif | |
2736 tagp) | |
2737 char_u *lbuf; /* line to be parsed */ | |
2738 #ifdef FEAT_EMACS_TAGS | |
2739 int is_etag; | |
2740 #endif | |
2741 tagptrs_T *tagp; | |
2742 { | |
2743 char_u *p; | |
2744 | |
2745 #ifdef FEAT_EMACS_TAGS | |
2746 char_u *p_7f; | |
2747 | |
2748 if (is_etag) | |
2749 { | |
2750 /* | |
2751 * There are two formats for an emacs tag line: | |
2752 * 1: struct EnvBase ^?EnvBase^A139,4627 | |
2753 * 2: #define ARPB_WILD_WORLD ^?153,5194 | |
2754 */ | |
2755 p_7f = vim_strchr(lbuf, 0x7f); | |
2756 if (p_7f == NULL) | |
1770 | 2757 { |
2758 etag_fail: | |
2759 if (vim_strchr(lbuf, '\n') == NULL) | |
2760 { | |
2761 /* Truncated line. Ignore it. */ | |
2762 if (p_verbose >= 5) | |
2763 { | |
2764 verbose_enter(); | |
2765 MSG(_("Ignoring long line in tags file")); | |
2766 verbose_leave(); | |
2767 } | |
2768 tagp->command = lbuf; | |
2769 tagp->tagname = lbuf; | |
2770 tagp->tagname_end = lbuf; | |
2771 return OK; | |
2772 } | |
7 | 2773 return FAIL; |
1770 | 2774 } |
7 | 2775 |
2776 /* Find ^A. If not found the line number is after the 0x7f */ | |
2777 p = vim_strchr(p_7f, Ctrl_A); | |
2778 if (p == NULL) | |
2779 p = p_7f + 1; | |
2780 else | |
2781 ++p; | |
2782 | |
2783 if (!VIM_ISDIGIT(*p)) /* check for start of line number */ | |
1770 | 2784 goto etag_fail; |
7 | 2785 tagp->command = p; |
2786 | |
2787 | |
2788 if (p[-1] == Ctrl_A) /* first format: explicit tagname given */ | |
2789 { | |
2790 tagp->tagname = p_7f + 1; | |
2791 tagp->tagname_end = p - 1; | |
2792 } | |
2793 else /* second format: isolate tagname */ | |
2794 { | |
2795 /* find end of tagname */ | |
2796 for (p = p_7f - 1; !vim_iswordc(*p); --p) | |
2797 if (p == lbuf) | |
1770 | 2798 goto etag_fail; |
7 | 2799 tagp->tagname_end = p + 1; |
2800 while (p >= lbuf && vim_iswordc(*p)) | |
2801 --p; | |
2802 tagp->tagname = p + 1; | |
2803 } | |
2804 } | |
2805 else /* not an Emacs tag */ | |
2806 { | |
2807 #endif | |
2808 /* Isolate the tagname, from lbuf up to the first white */ | |
2809 tagp->tagname = lbuf; | |
2810 #ifdef FEAT_TAG_ANYWHITE | |
2811 p = skiptowhite(lbuf); | |
2812 #else | |
2813 p = vim_strchr(lbuf, TAB); | |
2814 if (p == NULL) | |
2815 return FAIL; | |
2816 #endif | |
2817 tagp->tagname_end = p; | |
2818 | |
2819 /* Isolate file name, from first to second white space */ | |
2820 #ifdef FEAT_TAG_ANYWHITE | |
2821 p = skipwhite(p); | |
2822 #else | |
2823 if (*p != NUL) | |
2824 ++p; | |
2825 #endif | |
2826 tagp->fname = p; | |
2827 #ifdef FEAT_TAG_ANYWHITE | |
2828 p = skiptowhite(p); | |
2829 #else | |
2830 p = vim_strchr(p, TAB); | |
2831 if (p == NULL) | |
2832 return FAIL; | |
2833 #endif | |
2834 tagp->fname_end = p; | |
2835 | |
2836 /* find start of search command, after second white space */ | |
2837 #ifdef FEAT_TAG_ANYWHITE | |
2838 p = skipwhite(p); | |
2839 #else | |
2840 if (*p != NUL) | |
2841 ++p; | |
2842 #endif | |
2843 if (*p == NUL) | |
2844 return FAIL; | |
2845 tagp->command = p; | |
2846 #ifdef FEAT_EMACS_TAGS | |
2847 } | |
2848 #endif | |
2849 | |
2850 return OK; | |
2851 } | |
2852 | |
2853 /* | |
2854 * Check if tagname is a static tag | |
2855 * | |
2856 * Static tags produced by the older ctags program have the format: | |
2857 * 'file:tag file /pattern'. | |
1204 | 2858 * This is only recognized when both occurrence of 'file' are the same, to |
7 | 2859 * avoid recognizing "string::string" or ":exit". |
2860 * | |
2861 * Static tags produced by the new ctags program have the format: | |
2862 * 'tag file /pattern/;"<Tab>file:' " | |
2863 * | |
2864 * Return TRUE if it is a static tag and adjust *tagname to the real tag. | |
2865 * Return FALSE if it is not a static tag. | |
2866 */ | |
2867 static int | |
2868 test_for_static(tagp) | |
2869 tagptrs_T *tagp; | |
2870 { | |
2871 char_u *p; | |
2872 | |
2873 #ifdef FEAT_TAG_OLDSTATIC | |
2874 int len; | |
2875 | |
2876 /* | |
2877 * Check for old style static tag: "file:tag file .." | |
2878 */ | |
2879 len = (int)(tagp->fname_end - tagp->fname); | |
2880 p = tagp->tagname + len; | |
2881 if ( p < tagp->tagname_end | |
2882 && *p == ':' | |
2883 && fnamencmp(tagp->tagname, tagp->fname, len) == 0) | |
2884 { | |
2885 tagp->tagname = p + 1; | |
2886 return TRUE; | |
2887 } | |
2888 #endif | |
2889 | |
2890 /* | |
2891 * Check for new style static tag ":...<Tab>file:[<Tab>...]" | |
2892 */ | |
2893 p = tagp->command; | |
2894 while ((p = vim_strchr(p, '\t')) != NULL) | |
2895 { | |
2896 ++p; | |
2897 if (STRNCMP(p, "file:", 5) == 0) | |
2898 return TRUE; | |
2899 } | |
2900 | |
2901 return FALSE; | |
2902 } | |
2903 | |
2904 /* | |
2905 * Parse a line from a matching tag. Does not change the line itself. | |
2906 * | |
2907 * The line that we get looks like this: | |
2908 * Emacs tag: <mtt><tag_fname><NUL><ebuf><NUL><lbuf> | |
2909 * other tag: <mtt><tag_fname><NUL><NUL><lbuf> | |
2910 * without Emacs tags: <mtt><tag_fname><NUL><lbuf> | |
2911 * | |
2912 * Return OK or FAIL. | |
2913 */ | |
2914 static int | |
2915 parse_match(lbuf, tagp) | |
2916 char_u *lbuf; /* input: matching line */ | |
2917 tagptrs_T *tagp; /* output: pointers into the line */ | |
2918 { | |
2919 int retval; | |
2920 char_u *p; | |
2921 char_u *pc, *pt; | |
2922 | |
2923 tagp->tag_fname = lbuf + 1; | |
2924 lbuf += STRLEN(tagp->tag_fname) + 2; | |
2925 #ifdef FEAT_EMACS_TAGS | |
2926 if (*lbuf) | |
2927 { | |
2928 tagp->is_etag = TRUE; | |
2929 tagp->fname = lbuf; | |
2930 lbuf += STRLEN(lbuf); | |
2931 tagp->fname_end = lbuf++; | |
2932 } | |
2933 else | |
2934 { | |
2935 tagp->is_etag = FALSE; | |
2936 ++lbuf; | |
2937 } | |
2938 #endif | |
2939 | |
2940 /* Find search pattern and the file name for non-etags. */ | |
2941 retval = parse_tag_line(lbuf, | |
2942 #ifdef FEAT_EMACS_TAGS | |
2943 tagp->is_etag, | |
2944 #endif | |
2945 tagp); | |
2946 | |
2947 tagp->tagkind = NULL; | |
2948 tagp->command_end = NULL; | |
2949 | |
2950 if (retval == OK) | |
2951 { | |
2952 /* Try to find a kind field: "kind:<kind>" or just "<kind>"*/ | |
2953 p = tagp->command; | |
2954 if (find_extra(&p) == OK) | |
2955 { | |
2956 tagp->command_end = p; | |
2957 p += 2; /* skip ";\"" */ | |
2958 if (*p++ == TAB) | |
2959 while (ASCII_ISALPHA(*p)) | |
2960 { | |
2961 if (STRNCMP(p, "kind:", 5) == 0) | |
2962 { | |
2963 tagp->tagkind = p + 5; | |
2964 break; | |
2965 } | |
2966 pc = vim_strchr(p, ':'); | |
2967 pt = vim_strchr(p, '\t'); | |
2968 if (pc == NULL || (pt != NULL && pc > pt)) | |
2969 { | |
2970 tagp->tagkind = p; | |
2971 break; | |
2972 } | |
2973 if (pt == NULL) | |
2974 break; | |
2975 p = pt + 1; | |
2976 } | |
2977 } | |
2978 if (tagp->tagkind != NULL) | |
2979 { | |
2980 for (p = tagp->tagkind; | |
2981 *p && *p != '\t' && *p != '\r' && *p != '\n'; ++p) | |
2982 ; | |
2983 tagp->tagkind_end = p; | |
2984 } | |
2985 } | |
2986 return retval; | |
2987 } | |
2988 | |
2989 /* | |
2990 * Find out the actual file name of a tag. Concatenate the tags file name | |
2991 * with the matching tag file name. | |
2992 * Returns an allocated string or NULL (out of memory). | |
2993 */ | |
2994 static char_u * | |
2995 tag_full_fname(tagp) | |
2996 tagptrs_T *tagp; | |
2997 { | |
2998 char_u *fullname; | |
2999 int c; | |
3000 | |
3001 #ifdef FEAT_EMACS_TAGS | |
3002 if (tagp->is_etag) | |
3003 c = 0; /* to shut up GCC */ | |
3004 else | |
3005 #endif | |
3006 { | |
3007 c = *tagp->fname_end; | |
3008 *tagp->fname_end = NUL; | |
3009 } | |
3010 fullname = expand_tag_fname(tagp->fname, tagp->tag_fname, FALSE); | |
3011 | |
3012 #ifdef FEAT_EMACS_TAGS | |
3013 if (!tagp->is_etag) | |
3014 #endif | |
3015 *tagp->fname_end = c; | |
3016 | |
3017 return fullname; | |
3018 } | |
3019 | |
3020 /* | |
3021 * Jump to a tag that has been found in one of the tag files | |
3022 * | |
3023 * returns OK for success, NOTAGFILE when file not found, FAIL otherwise. | |
3024 */ | |
3025 static int | |
3026 jumpto_tag(lbuf, forceit, keep_help) | |
3027 char_u *lbuf; /* line from the tags file for this tag */ | |
3028 int forceit; /* :ta with ! */ | |
3029 int keep_help; /* keep help flag (FALSE for cscope) */ | |
3030 { | |
3031 int save_secure; | |
3032 int save_magic; | |
3033 int save_p_ws, save_p_scs, save_p_ic; | |
3034 linenr_T save_lnum; | |
3035 int csave = 0; | |
3036 char_u *str; | |
3037 char_u *pbuf; /* search pattern buffer */ | |
3038 char_u *pbuf_end; | |
3039 char_u *tofree_fname = NULL; | |
3040 char_u *fname; | |
3041 tagptrs_T tagp; | |
3042 int retval = FAIL; | |
3043 int getfile_result; | |
3044 int search_options; | |
3045 #ifdef FEAT_SEARCH_EXTRA | |
3046 int save_no_hlsearch; | |
3047 #endif | |
3048 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) | |
3049 win_T *curwin_save = NULL; | |
3050 #endif | |
3051 char_u *full_fname = NULL; | |
3052 #ifdef FEAT_FOLDING | |
3053 int old_KeyTyped = KeyTyped; /* getting the file may reset it */ | |
3054 #endif | |
3055 | |
3056 pbuf = alloc(LSIZE); | |
3057 | |
3058 /* parse the match line into the tagp structure */ | |
3059 if (pbuf == NULL || parse_match(lbuf, &tagp) == FAIL) | |
3060 { | |
3061 tagp.fname_end = NULL; | |
3062 goto erret; | |
3063 } | |
3064 | |
3065 /* truncate the file name, so it can be used as a string */ | |
3066 csave = *tagp.fname_end; | |
3067 *tagp.fname_end = NUL; | |
3068 fname = tagp.fname; | |
3069 | |
3070 /* copy the command to pbuf[], remove trailing CR/NL */ | |
3071 str = tagp.command; | |
3072 for (pbuf_end = pbuf; *str && *str != '\n' && *str != '\r'; ) | |
3073 { | |
3074 #ifdef FEAT_EMACS_TAGS | |
3075 if (tagp.is_etag && *str == ',')/* stop at ',' after line number */ | |
3076 break; | |
3077 #endif | |
3078 *pbuf_end++ = *str++; | |
3079 } | |
3080 *pbuf_end = NUL; | |
3081 | |
3082 #ifdef FEAT_EMACS_TAGS | |
3083 if (!tagp.is_etag) | |
3084 #endif | |
3085 { | |
3086 /* | |
3087 * Remove the "<Tab>fieldname:value" stuff; we don't need it here. | |
3088 */ | |
3089 str = pbuf; | |
3090 if (find_extra(&str) == OK) | |
3091 { | |
3092 pbuf_end = str; | |
3093 *pbuf_end = NUL; | |
3094 } | |
3095 } | |
3096 | |
3097 /* | |
3098 * Expand file name, when needed (for environment variables). | |
3099 * If 'tagrelative' option set, may change file name. | |
3100 */ | |
3101 fname = expand_tag_fname(fname, tagp.tag_fname, TRUE); | |
3102 if (fname == NULL) | |
3103 goto erret; | |
3104 tofree_fname = fname; /* free() it later */ | |
3105 | |
3106 /* | |
3107 * Check if the file with the tag exists before abandoning the current | |
3108 * file. Also accept a file name for which there is a matching BufReadCmd | |
3109 * autocommand event (e.g., http://sys/file). | |
3110 */ | |
3111 if (mch_getperm(fname) < 0 | |
3112 #ifdef FEAT_AUTOCMD | |
40 | 3113 && !has_autocmd(EVENT_BUFREADCMD, fname, NULL) |
7 | 3114 #endif |
3115 ) | |
3116 { | |
3117 retval = NOTAGFILE; | |
3118 vim_free(nofile_fname); | |
3119 nofile_fname = vim_strsave(fname); | |
3120 if (nofile_fname == NULL) | |
3121 nofile_fname = empty_option; | |
3122 goto erret; | |
3123 } | |
3124 | |
3125 ++RedrawingDisabled; | |
3126 | |
3127 #ifdef FEAT_GUI | |
3128 need_mouse_correct = TRUE; | |
3129 #endif | |
3130 | |
3131 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) | |
2713 | 3132 if (g_do_tagpreview != 0) |
7 | 3133 { |
728 | 3134 postponed_split = 0; /* don't split again below */ |
3135 curwin_save = curwin; /* Save current window */ | |
3136 | |
7 | 3137 /* |
3138 * If we are reusing a window, we may change dir when | |
3139 * entering it (autocommands) so turn the tag filename | |
3140 * into a fullpath | |
3141 */ | |
3142 if (!curwin->w_p_pvw) | |
3143 { | |
3144 full_fname = FullName_save(fname, FALSE); | |
3145 fname = full_fname; | |
3146 | |
3147 /* | |
3148 * Make the preview window the current window. | |
3149 * Open a preview window when needed. | |
3150 */ | |
816 | 3151 prepare_tagpreview(TRUE); |
7 | 3152 } |
3153 } | |
3154 | |
682 | 3155 /* If it was a CTRL-W CTRL-] command split window now. For ":tab tag" |
3156 * open a new tab page. */ | |
3157 if (postponed_split || cmdmod.tab != 0) | |
7 | 3158 { |
3159 win_split(postponed_split > 0 ? postponed_split : 0, | |
3160 postponed_split_flags); | |
2583 | 3161 RESET_BINDING(curwin); |
7 | 3162 } |
3163 #endif | |
3164 | |
3165 if (keep_help) | |
3166 { | |
3167 /* A :ta from a help file will keep the b_help flag set. For ":ptag" | |
3168 * we need to use the flag from the window where we came from. */ | |
3169 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) | |
2713 | 3170 if (g_do_tagpreview != 0) |
7 | 3171 keep_help_flag = curwin_save->w_buffer->b_help; |
3172 else | |
3173 #endif | |
3174 keep_help_flag = curbuf->b_help; | |
3175 } | |
3176 getfile_result = getfile(0, fname, NULL, TRUE, (linenr_T)0, forceit); | |
3177 keep_help_flag = FALSE; | |
3178 | |
3179 if (getfile_result <= 0) /* got to the right file */ | |
3180 { | |
3181 curwin->w_set_curswant = TRUE; | |
3182 #ifdef FEAT_WINDOWS | |
3183 postponed_split = 0; | |
3184 #endif | |
3185 | |
3186 save_secure = secure; | |
3187 secure = 1; | |
3188 #ifdef HAVE_SANDBOX | |
3189 ++sandbox; | |
3190 #endif | |
3191 save_magic = p_magic; | |
3192 p_magic = FALSE; /* always execute with 'nomagic' */ | |
3193 #ifdef FEAT_SEARCH_EXTRA | |
3194 /* Save value of no_hlsearch, jumping to a tag is not a real search */ | |
3195 save_no_hlsearch = no_hlsearch; | |
3196 #endif | |
3197 | |
3198 /* | |
3199 * If 'cpoptions' contains 't', store the search pattern for the "n" | |
3200 * command. If 'cpoptions' does not contain 't', the search pattern | |
3201 * is not stored. | |
3202 */ | |
3203 if (vim_strchr(p_cpo, CPO_TAGPAT) != NULL) | |
3204 search_options = 0; | |
3205 else | |
3206 search_options = SEARCH_KEEP; | |
3207 | |
3208 /* | |
3209 * If the command is a search, try here. | |
3210 * | |
3211 * Reset 'smartcase' for the search, since the search pattern was not | |
3212 * typed by the user. | |
3213 * Only use do_search() when there is a full search command, without | |
3214 * anything following. | |
3215 */ | |
3216 str = pbuf; | |
3217 if (pbuf[0] == '/' || pbuf[0] == '?') | |
3218 str = skip_regexp(pbuf + 1, pbuf[0], FALSE, NULL) + 1; | |
3219 if (str > pbuf_end - 1) /* search command with nothing following */ | |
3220 { | |
3221 save_p_ws = p_ws; | |
3222 save_p_ic = p_ic; | |
3223 save_p_scs = p_scs; | |
3224 p_ws = TRUE; /* need 'wrapscan' for backward searches */ | |
3225 p_ic = FALSE; /* don't ignore case now */ | |
3226 p_scs = FALSE; | |
3227 #if 0 /* disabled for now */ | |
3228 #ifdef FEAT_CMDHIST | |
3229 /* put pattern in search history */ | |
3230 add_to_history(HIST_SEARCH, pbuf + 1, TRUE, pbuf[0]); | |
3231 #endif | |
3232 #endif | |
3233 save_lnum = curwin->w_cursor.lnum; | |
3234 curwin->w_cursor.lnum = 0; /* start search before first line */ | |
1521 | 3235 if (do_search(NULL, pbuf[0], pbuf + 1, (long)1, |
3236 search_options, NULL)) | |
7 | 3237 retval = OK; |
3238 else | |
3239 { | |
3240 int found = 1; | |
3241 int cc; | |
3242 | |
3243 /* | |
3244 * try again, ignore case now | |
3245 */ | |
3246 p_ic = TRUE; | |
3247 if (!do_search(NULL, pbuf[0], pbuf + 1, (long)1, | |
1521 | 3248 search_options, NULL)) |
7 | 3249 { |
3250 /* | |
3251 * Failed to find pattern, take a guess: "^func (" | |
3252 */ | |
3253 found = 2; | |
3254 (void)test_for_static(&tagp); | |
3255 cc = *tagp.tagname_end; | |
3256 *tagp.tagname_end = NUL; | |
3257 sprintf((char *)pbuf, "^%s\\s\\*(", tagp.tagname); | |
1521 | 3258 if (!do_search(NULL, '/', pbuf, (long)1, |
3259 search_options, NULL)) | |
7 | 3260 { |
3261 /* Guess again: "^char * \<func (" */ | |
3262 sprintf((char *)pbuf, "^\\[#a-zA-Z_]\\.\\*\\<%s\\s\\*(", | |
3263 tagp.tagname); | |
3264 if (!do_search(NULL, '/', pbuf, (long)1, | |
1521 | 3265 search_options, NULL)) |
7 | 3266 found = 0; |
3267 } | |
3268 *tagp.tagname_end = cc; | |
3269 } | |
3270 if (found == 0) | |
3271 { | |
3272 EMSG(_("E434: Can't find tag pattern")); | |
3273 curwin->w_cursor.lnum = save_lnum; | |
3274 } | |
3275 else | |
3276 { | |
3277 /* | |
3278 * Only give a message when really guessed, not when 'ic' | |
3279 * is set and match found while ignoring case. | |
3280 */ | |
3281 if (found == 2 || !save_p_ic) | |
3282 { | |
3283 MSG(_("E435: Couldn't find tag, just guessing!")); | |
3284 if (!msg_scrolled && msg_silent == 0) | |
3285 { | |
3286 out_flush(); | |
3287 ui_delay(1000L, TRUE); | |
3288 } | |
3289 } | |
3290 retval = OK; | |
3291 } | |
3292 } | |
3293 p_ws = save_p_ws; | |
3294 p_ic = save_p_ic; | |
3295 p_scs = save_p_scs; | |
3296 | |
3297 /* A search command may have positioned the cursor beyond the end | |
3298 * of the line. May need to correct that here. */ | |
3299 check_cursor(); | |
3300 } | |
3301 else | |
3302 { | |
3303 curwin->w_cursor.lnum = 1; /* start command in line 1 */ | |
3304 do_cmdline_cmd(pbuf); | |
3305 retval = OK; | |
3306 } | |
3307 | |
3308 /* | |
3309 * When the command has done something that is not allowed make sure | |
3310 * the error message can be seen. | |
3311 */ | |
3312 if (secure == 2) | |
3313 wait_return(TRUE); | |
3314 secure = save_secure; | |
3315 p_magic = save_magic; | |
3316 #ifdef HAVE_SANDBOX | |
3317 --sandbox; | |
3318 #endif | |
3319 #ifdef FEAT_SEARCH_EXTRA | |
3320 /* restore no_hlsearch when keeping the old search pattern */ | |
3321 if (search_options) | |
3322 no_hlsearch = save_no_hlsearch; | |
3323 #endif | |
3324 | |
3325 /* Return OK if jumped to another file (at least we found the file!). */ | |
3326 if (getfile_result == -1) | |
3327 retval = OK; | |
3328 | |
3329 if (retval == OK) | |
3330 { | |
3331 /* | |
3332 * For a help buffer: Put the cursor line at the top of the window, | |
3333 * the help subject will be below it. | |
3334 */ | |
3335 if (curbuf->b_help) | |
3336 set_topline(curwin, curwin->w_cursor.lnum); | |
3337 #ifdef FEAT_FOLDING | |
3338 if ((fdo_flags & FDO_TAG) && old_KeyTyped) | |
3339 foldOpenCursor(); | |
3340 #endif | |
3341 } | |
3342 | |
3343 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) | |
2713 | 3344 if (g_do_tagpreview != 0 |
3345 && curwin != curwin_save && win_valid(curwin_save)) | |
7 | 3346 { |
3347 /* Return cursor to where we were */ | |
3348 validate_cursor(); | |
3349 redraw_later(VALID); | |
3350 win_enter(curwin_save, TRUE); | |
3351 } | |
3352 #endif | |
3353 | |
3354 --RedrawingDisabled; | |
3355 } | |
3356 else | |
3357 { | |
3358 --RedrawingDisabled; | |
3359 #ifdef FEAT_WINDOWS | |
3360 if (postponed_split) /* close the window */ | |
3361 { | |
3362 win_close(curwin, FALSE); | |
3363 postponed_split = 0; | |
3364 } | |
3365 #endif | |
3366 } | |
3367 | |
3368 erret: | |
3369 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) | |
3370 g_do_tagpreview = 0; /* For next time */ | |
3371 #endif | |
3372 if (tagp.fname_end != NULL) | |
3373 *tagp.fname_end = csave; | |
3374 vim_free(pbuf); | |
3375 vim_free(tofree_fname); | |
3376 vim_free(full_fname); | |
3377 | |
3378 return retval; | |
3379 } | |
3380 | |
3381 /* | |
3382 * If "expand" is TRUE, expand wildcards in fname. | |
3383 * If 'tagrelative' option set, change fname (name of file containing tag) | |
3384 * according to tag_fname (name of tag file containing fname). | |
3385 * Returns a pointer to allocated memory (or NULL when out of memory). | |
3386 */ | |
3387 static char_u * | |
3388 expand_tag_fname(fname, tag_fname, expand) | |
3389 char_u *fname; | |
3390 char_u *tag_fname; | |
3391 int expand; | |
3392 { | |
3393 char_u *p; | |
3394 char_u *retval; | |
3395 char_u *expanded_fname = NULL; | |
3396 expand_T xpc; | |
3397 | |
3398 /* | |
3399 * Expand file name (for environment variables) when needed. | |
3400 */ | |
3401 if (expand && mch_has_wildcard(fname)) | |
3402 { | |
3403 ExpandInit(&xpc); | |
3404 xpc.xp_context = EXPAND_FILES; | |
3405 expanded_fname = ExpandOne(&xpc, (char_u *)fname, NULL, | |
3406 WILD_LIST_NOTFOUND|WILD_SILENT, WILD_EXPAND_FREE); | |
3407 if (expanded_fname != NULL) | |
3408 fname = expanded_fname; | |
3409 } | |
3410 | |
3411 if ((p_tr || curbuf->b_help) | |
3412 && !vim_isAbsName(fname) | |
3413 && (p = gettail(tag_fname)) != tag_fname) | |
3414 { | |
3415 retval = alloc(MAXPATHL); | |
3416 if (retval != NULL) | |
3417 { | |
3418 STRCPY(retval, tag_fname); | |
418 | 3419 vim_strncpy(retval + (p - tag_fname), fname, |
3420 MAXPATHL - (p - tag_fname) - 1); | |
7 | 3421 /* |
3422 * Translate names like "src/a/../b/file.c" into "src/b/file.c". | |
3423 */ | |
3424 simplify_filename(retval); | |
3425 } | |
3426 } | |
3427 else | |
3428 retval = vim_strsave(fname); | |
3429 | |
3430 vim_free(expanded_fname); | |
3431 | |
3432 return retval; | |
3433 } | |
3434 | |
3435 /* | |
3436 * Converts a file name into a canonical form. It simplifies a file name into | |
3437 * its simplest form by stripping out unneeded components, if any. The | |
3438 * resulting file name is simplified in place and will either be the same | |
3439 * length as that supplied, or shorter. | |
3440 */ | |
3441 void | |
3442 simplify_filename(filename) | |
3443 char_u *filename; | |
3444 { | |
3445 #ifndef AMIGA /* Amiga doesn't have "..", it uses "/" */ | |
3446 int components = 0; | |
3447 char_u *p, *tail, *start; | |
3448 int stripping_disabled = FALSE; | |
3449 int relative = TRUE; | |
3450 | |
3451 p = filename; | |
3452 #ifdef BACKSLASH_IN_FILENAME | |
3453 if (p[1] == ':') /* skip "x:" */ | |
3454 p += 2; | |
3455 #endif | |
3456 | |
3457 if (vim_ispathsep(*p)) | |
3458 { | |
3459 relative = FALSE; | |
3460 do | |
3461 ++p; | |
3462 while (vim_ispathsep(*p)); | |
3463 } | |
3464 start = p; /* remember start after "c:/" or "/" or "///" */ | |
3465 | |
3466 do | |
3467 { | |
3468 /* At this point "p" is pointing to the char following a single "/" | |
3469 * or "p" is at the "start" of the (absolute or relative) path name. */ | |
3470 #ifdef VMS | |
3471 /* VMS allows device:[path] - don't strip the [ in directory */ | |
3472 if ((*p == '[' || *p == '<') && p > filename && p[-1] == ':') | |
3473 { | |
3474 /* :[ or :< composition: vms directory component */ | |
3475 ++components; | |
3476 p = getnextcomp(p + 1); | |
3477 } | |
3478 /* allow remote calls as host"user passwd"::device:[path] */ | |
3479 else if (p[0] == ':' && p[1] == ':' && p > filename && p[-1] == '"' ) | |
3480 { | |
3481 /* ":: composition: vms host/passwd component */ | |
3482 ++components; | |
3483 p = getnextcomp(p + 2); | |
3484 } | |
3485 else | |
3486 #endif | |
3487 if (vim_ispathsep(*p)) | |
1620 | 3488 STRMOVE(p, p + 1); /* remove duplicate "/" */ |
7 | 3489 else if (p[0] == '.' && (vim_ispathsep(p[1]) || p[1] == NUL)) |
3490 { | |
3491 if (p == start && relative) | |
3492 p += 1 + (p[1] != NUL); /* keep single "." or leading "./" */ | |
3493 else | |
3494 { | |
3495 /* Strip "./" or ".///". If we are at the end of the file name | |
3496 * and there is no trailing path separator, either strip "/." if | |
3497 * we are after "start", or strip "." if we are at the beginning | |
3498 * of an absolute path name . */ | |
3499 tail = p + 1; | |
3500 if (p[1] != NUL) | |
3501 while (vim_ispathsep(*tail)) | |
39 | 3502 mb_ptr_adv(tail); |
7 | 3503 else if (p > start) |
3504 --p; /* strip preceding path separator */ | |
1620 | 3505 STRMOVE(p, tail); |
7 | 3506 } |
3507 } | |
3508 else if (p[0] == '.' && p[1] == '.' && | |
3509 (vim_ispathsep(p[2]) || p[2] == NUL)) | |
3510 { | |
3511 /* Skip to after ".." or "../" or "..///". */ | |
3512 tail = p + 2; | |
3513 while (vim_ispathsep(*tail)) | |
39 | 3514 mb_ptr_adv(tail); |
7 | 3515 |
3516 if (components > 0) /* strip one preceding component */ | |
3517 { | |
3518 int do_strip = FALSE; | |
3519 char_u saved_char; | |
3520 struct stat st; | |
3521 | |
3522 /* Don't strip for an erroneous file name. */ | |
3523 if (!stripping_disabled) | |
3524 { | |
3525 /* If the preceding component does not exist in the file | |
3526 * system, we strip it. On Unix, we don't accept a symbolic | |
3527 * link that refers to a non-existent file. */ | |
3528 saved_char = p[-1]; | |
3529 p[-1] = NUL; | |
3530 #ifdef UNIX | |
3531 if (mch_lstat((char *)filename, &st) < 0) | |
3532 #else | |
3533 if (mch_stat((char *)filename, &st) < 0) | |
3534 #endif | |
3535 do_strip = TRUE; | |
3536 p[-1] = saved_char; | |
3537 | |
3538 --p; | |
3539 /* Skip back to after previous '/'. */ | |
39 | 3540 while (p > start && !after_pathsep(start, p)) |
3541 mb_ptr_back(start, p); | |
7 | 3542 |
3543 if (!do_strip) | |
3544 { | |
3545 /* If the component exists in the file system, check | |
3546 * that stripping it won't change the meaning of the | |
3547 * file name. First get information about the | |
3548 * unstripped file name. This may fail if the component | |
3549 * to strip is not a searchable directory (but a regular | |
3550 * file, for instance), since the trailing "/.." cannot | |
3551 * be applied then. We don't strip it then since we | |
3552 * don't want to replace an erroneous file name by | |
3553 * a valid one, and we disable stripping of later | |
3554 * components. */ | |
3555 saved_char = *tail; | |
3556 *tail = NUL; | |
3557 if (mch_stat((char *)filename, &st) >= 0) | |
3558 do_strip = TRUE; | |
3559 else | |
3560 stripping_disabled = TRUE; | |
3561 *tail = saved_char; | |
3562 #ifdef UNIX | |
3563 if (do_strip) | |
3564 { | |
3565 struct stat new_st; | |
3566 | |
3567 /* On Unix, the check for the unstripped file name | |
3568 * above works also for a symbolic link pointing to | |
3569 * a searchable directory. But then the parent of | |
3570 * the directory pointed to by the link must be the | |
3571 * same as the stripped file name. (The latter | |
3572 * exists in the file system since it is the | |
3573 * component's parent directory.) */ | |
3574 if (p == start && relative) | |
3575 (void)mch_stat(".", &new_st); | |
3576 else | |
3577 { | |
3578 saved_char = *p; | |
3579 *p = NUL; | |
3580 (void)mch_stat((char *)filename, &new_st); | |
3581 *p = saved_char; | |
3582 } | |
3583 | |
3584 if (new_st.st_ino != st.st_ino || | |
3585 new_st.st_dev != st.st_dev) | |
3586 { | |
3587 do_strip = FALSE; | |
3588 /* We don't disable stripping of later | |
3589 * components since the unstripped path name is | |
3590 * still valid. */ | |
3591 } | |
3592 } | |
3593 #endif | |
3594 } | |
3595 } | |
3596 | |
3597 if (!do_strip) | |
3598 { | |
3599 /* Skip the ".." or "../" and reset the counter for the | |
3600 * components that might be stripped later on. */ | |
3601 p = tail; | |
3602 components = 0; | |
3603 } | |
3604 else | |
3605 { | |
3606 /* Strip previous component. If the result would get empty | |
3607 * and there is no trailing path separator, leave a single | |
3608 * "." instead. If we are at the end of the file name and | |
3609 * there is no trailing path separator and a preceding | |
3610 * component is left after stripping, strip its trailing | |
3611 * path separator as well. */ | |
3612 if (p == start && relative && tail[-1] == '.') | |
3613 { | |
3614 *p++ = '.'; | |
3615 *p = NUL; | |
3616 } | |
3617 else | |
3618 { | |
3619 if (p > start && tail[-1] == '.') | |
3620 --p; | |
1620 | 3621 STRMOVE(p, tail); /* strip previous component */ |
7 | 3622 } |
3623 | |
3624 --components; | |
3625 } | |
3626 } | |
3627 else if (p == start && !relative) /* leading "/.." or "/../" */ | |
1620 | 3628 STRMOVE(p, tail); /* strip ".." or "../" */ |
7 | 3629 else |
3630 { | |
3631 if (p == start + 2 && p[-2] == '.') /* leading "./../" */ | |
3632 { | |
1620 | 3633 STRMOVE(p - 2, p); /* strip leading "./" */ |
7 | 3634 tail -= 2; |
3635 } | |
3636 p = tail; /* skip to char after ".." or "../" */ | |
3637 } | |
3638 } | |
3639 else | |
3640 { | |
3641 ++components; /* simple path component */ | |
3642 p = getnextcomp(p); | |
3643 } | |
3644 } while (*p != NUL); | |
3645 #endif /* !AMIGA */ | |
3646 } | |
3647 | |
3648 /* | |
3649 * Check if we have a tag for the buffer with name "buf_ffname". | |
3650 * This is a bit slow, because of the full path compare in fullpathcmp(). | |
3651 * Return TRUE if tag for file "fname" if tag file "tag_fname" is for current | |
3652 * file. | |
3653 */ | |
3654 static int | |
3655 #ifdef FEAT_EMACS_TAGS | |
3656 test_for_current(is_etag, fname, fname_end, tag_fname, buf_ffname) | |
3657 int is_etag; | |
3658 #else | |
3659 test_for_current(fname, fname_end, tag_fname, buf_ffname) | |
3660 #endif | |
3661 char_u *fname; | |
3662 char_u *fname_end; | |
3663 char_u *tag_fname; | |
3664 char_u *buf_ffname; | |
3665 { | |
3666 int c; | |
3667 int retval = FALSE; | |
3668 char_u *fullname; | |
3669 | |
3670 if (buf_ffname != NULL) /* if the buffer has a name */ | |
3671 { | |
3672 #ifdef FEAT_EMACS_TAGS | |
3673 if (is_etag) | |
3674 c = 0; /* to shut up GCC */ | |
3675 else | |
3676 #endif | |
3677 { | |
3678 c = *fname_end; | |
3679 *fname_end = NUL; | |
3680 } | |
3681 fullname = expand_tag_fname(fname, tag_fname, TRUE); | |
3682 if (fullname != NULL) | |
3683 { | |
3684 retval = (fullpathcmp(fullname, buf_ffname, TRUE) & FPC_SAME); | |
3685 vim_free(fullname); | |
3686 } | |
3687 #ifdef FEAT_EMACS_TAGS | |
3688 if (!is_etag) | |
3689 #endif | |
3690 *fname_end = c; | |
3691 } | |
3692 | |
3693 return retval; | |
3694 } | |
3695 | |
3696 /* | |
3697 * Find the end of the tagaddress. | |
3698 * Return OK if ";\"" is following, FAIL otherwise. | |
3699 */ | |
3700 static int | |
3701 find_extra(pp) | |
3702 char_u **pp; | |
3703 { | |
3704 char_u *str = *pp; | |
3705 | |
3706 /* Repeat for addresses separated with ';' */ | |
3707 for (;;) | |
3708 { | |
3709 if (VIM_ISDIGIT(*str)) | |
3710 str = skipdigits(str); | |
3711 else if (*str == '/' || *str == '?') | |
3712 { | |
3713 str = skip_regexp(str + 1, *str, FALSE, NULL); | |
3714 if (*str != **pp) | |
3715 str = NULL; | |
3716 else | |
3717 ++str; | |
3718 } | |
3719 else | |
3720 str = NULL; | |
3721 if (str == NULL || *str != ';' | |
3722 || !(VIM_ISDIGIT(str[1]) || str[1] == '/' || str[1] == '?')) | |
3723 break; | |
3724 ++str; /* skip ';' */ | |
3725 } | |
3726 | |
3727 if (str != NULL && STRNCMP(str, ";\"", 2) == 0) | |
3728 { | |
3729 *pp = str; | |
3730 return OK; | |
3731 } | |
3732 return FAIL; | |
3733 } | |
3734 | |
3735 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
3736 int | |
3737 expand_tags(tagnames, pat, num_file, file) | |
3738 int tagnames; /* expand tag names */ | |
3739 char_u *pat; | |
3740 int *num_file; | |
3741 char_u ***file; | |
3742 { | |
3743 int i; | |
3744 int c; | |
3745 int tagnmflag; | |
3746 char_u tagnm[100]; | |
3747 tagptrs_T t_p; | |
3748 int ret; | |
3749 | |
3750 if (tagnames) | |
3751 tagnmflag = TAG_NAMES; | |
3752 else | |
3753 tagnmflag = 0; | |
3754 if (pat[0] == '/') | |
3755 ret = find_tags(pat + 1, num_file, file, | |
3756 TAG_REGEXP | tagnmflag | TAG_VERBOSE, | |
3757 TAG_MANY, curbuf->b_ffname); | |
3758 else | |
3759 ret = find_tags(pat, num_file, file, | |
3760 TAG_REGEXP | tagnmflag | TAG_VERBOSE | TAG_NOIC, | |
3761 TAG_MANY, curbuf->b_ffname); | |
3762 if (ret == OK && !tagnames) | |
3763 { | |
3764 /* Reorganize the tags for display and matching as strings of: | |
3765 * "<tagname>\0<kind>\0<filename>\0" | |
3766 */ | |
3767 for (i = 0; i < *num_file; i++) | |
3768 { | |
3769 parse_match((*file)[i], &t_p); | |
3770 c = (int)(t_p.tagname_end - t_p.tagname); | |
3771 mch_memmove(tagnm, t_p.tagname, (size_t)c); | |
3772 tagnm[c++] = 0; | |
3773 tagnm[c++] = (t_p.tagkind != NULL && *t_p.tagkind) | |
3774 ? *t_p.tagkind : 'f'; | |
3775 tagnm[c++] = 0; | |
3776 mch_memmove((*file)[i] + c, t_p.fname, t_p.fname_end - t_p.fname); | |
3777 (*file)[i][c + (t_p.fname_end - t_p.fname)] = 0; | |
3778 mch_memmove((*file)[i], tagnm, (size_t)c); | |
3779 } | |
3780 } | |
3781 return ret; | |
3782 } | |
3783 #endif | |
179 | 3784 |
3785 #if defined(FEAT_EVAL) || defined(PROTO) | |
3786 static int add_tag_field __ARGS((dict_T *dict, char *field_name, char_u *start, char_u *end)); | |
3787 | |
3788 /* | |
2185 | 3789 * Add a tag field to the dictionary "dict". |
3790 * Return OK or FAIL. | |
179 | 3791 */ |
3792 static int | |
3793 add_tag_field(dict, field_name, start, end) | |
3794 dict_T *dict; | |
3795 char *field_name; | |
211 | 3796 char_u *start; /* start of the value */ |
3797 char_u *end; /* after the value; can be NULL */ | |
179 | 3798 { |
2770 | 3799 char_u *buf; |
188 | 3800 int len = 0; |
2770 | 3801 int retval; |
179 | 3802 |
2185 | 3803 /* check that the field name doesn't exist yet */ |
3804 if (dict_find(dict, (char_u *)field_name, -1) != NULL) | |
3805 { | |
3806 if (p_verbose > 0) | |
3807 { | |
3808 verbose_enter(); | |
3809 smsg((char_u *)_("Duplicate field name: %s"), field_name); | |
3810 verbose_leave(); | |
3811 } | |
3812 return FAIL; | |
3813 } | |
2770 | 3814 buf = alloc(MAXPATHL); |
3815 if (buf == NULL) | |
3816 return FAIL; | |
188 | 3817 if (start != NULL) |
3818 { | |
211 | 3819 if (end == NULL) |
3820 { | |
3821 end = start + STRLEN(start); | |
3822 while (end > start && (end[-1] == '\r' || end[-1] == '\n')) | |
3823 --end; | |
3824 } | |
835 | 3825 len = (int)(end - start); |
2770 | 3826 if (len > MAXPATHL - 1) |
3827 len = MAXPATHL - 1; | |
418 | 3828 vim_strncpy(buf, start, len); |
188 | 3829 } |
179 | 3830 buf[len] = NUL; |
2770 | 3831 retval = dict_add_nr_str(dict, field_name, 0L, buf); |
3832 vim_free(buf); | |
3833 return retval; | |
179 | 3834 } |
3835 | |
3836 /* | |
3837 * Add the tags matching the specified pattern to the list "list" | |
3838 * as a dictionary | |
3839 */ | |
3840 int | |
3841 get_tags(list, pat) | |
3842 list_T *list; | |
3843 char_u *pat; | |
3844 { | |
3845 int num_matches, i, ret; | |
3846 char_u **matches, *p; | |
970 | 3847 char_u *full_fname; |
179 | 3848 dict_T *dict; |
3849 tagptrs_T tp; | |
3850 long is_static; | |
3851 | |
3852 ret = find_tags(pat, &num_matches, &matches, | |
3853 TAG_REGEXP | TAG_NOIC, (int)MAXCOL, NULL); | |
3854 if (ret == OK && num_matches > 0) | |
3855 { | |
3856 for (i = 0; i < num_matches; ++i) | |
3857 { | |
188 | 3858 parse_match(matches[i], &tp); |
3859 is_static = test_for_static(&tp); | |
3860 | |
3861 /* Skip pseudo-tag lines. */ | |
3862 if (STRNCMP(tp.tagname, "!_TAG_", 6) == 0) | |
3863 continue; | |
3864 | |
179 | 3865 if ((dict = dict_alloc()) == NULL) |
3866 ret = FAIL; | |
3867 if (list_append_dict(list, dict) == FAIL) | |
3868 ret = FAIL; | |
3869 | |
970 | 3870 full_fname = tag_full_fname(&tp); |
179 | 3871 if (add_tag_field(dict, "name", tp.tagname, tp.tagname_end) == FAIL |
970 | 3872 || add_tag_field(dict, "filename", full_fname, |
3873 NULL) == FAIL | |
179 | 3874 || add_tag_field(dict, "cmd", tp.command, |
3875 tp.command_end) == FAIL | |
3876 || add_tag_field(dict, "kind", tp.tagkind, | |
3877 tp.tagkind_end) == FAIL | |
3878 || dict_add_nr_str(dict, "static", is_static, NULL) == FAIL) | |
3879 ret = FAIL; | |
3880 | |
970 | 3881 vim_free(full_fname); |
3882 | |
179 | 3883 if (tp.command_end != NULL) |
3884 { | |
3885 for (p = tp.command_end + 3; | |
3886 *p != NUL && *p != '\n' && *p != '\r'; ++p) | |
3887 { | |
3888 if (p == tp.tagkind || (p + 5 == tp.tagkind | |
3889 && STRNCMP(p, "kind:", 5) == 0)) | |
3890 /* skip "kind:<kind>" and "<kind>" */ | |
3891 p = tp.tagkind_end - 1; | |
3892 else if (STRNCMP(p, "file:", 5) == 0) | |
3893 /* skip "file:" (static tag) */ | |
3894 p += 4; | |
188 | 3895 else if (!vim_iswhite(*p)) |
179 | 3896 { |
3897 char_u *s, *n; | |
188 | 3898 int len; |
179 | 3899 |
799 | 3900 /* Add extra field as a dict entry. Fields are |
3901 * separated by Tabs. */ | |
179 | 3902 n = p; |
799 | 3903 while (*p != NUL && *p >= ' ' && *p < 127 && *p != ':') |
179 | 3904 ++p; |
835 | 3905 len = (int)(p - n); |
188 | 3906 if (*p == ':' && len > 0) |
3907 { | |
3908 s = ++p; | |
836 | 3909 while (*p != NUL && *p >= ' ') |
188 | 3910 ++p; |
3911 n[len] = NUL; | |
3912 if (add_tag_field(dict, (char *)n, s, p) == FAIL) | |
3913 ret = FAIL; | |
3914 n[len] = ':'; | |
3915 } | |
836 | 3916 else |
3917 /* Skip field without colon. */ | |
3918 while (*p != NUL && *p >= ' ') | |
3919 ++p; | |
1674 | 3920 if (*p == NUL) |
3921 break; | |
179 | 3922 } |
3923 } | |
3924 } | |
3925 | |
3926 vim_free(matches[i]); | |
3927 } | |
3928 vim_free(matches); | |
3929 } | |
3930 return ret; | |
3931 } | |
3932 #endif |