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