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