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