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