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