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