2 * Copyright (C) 1984-2012 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information, see the README file.
13 #define WHITESP(c) ((c)==' ' || (c)=='\t')
17 public char *tags = "tags";
37 T_CTAGS, /* 'tags': standard and extended format (ctags) */
38 T_CTAGS_X, /* stdin: cross reference format (ctags) */
39 T_GTAGS, /* 'GTAGS': function defenition (global) */
40 T_GRTAGS, /* 'GRTAGS': function reference (global) */
41 T_GSYMS, /* 'GSYMS': other symbols (global) */
42 T_GPATH /* 'GPATH': path name (global) */
45 static enum tag_result findctag();
46 static enum tag_result findgtag();
47 static char *nextgtag();
48 static char *prevgtag();
49 static POSITION ctagsearch();
50 static POSITION gtagsearch();
51 static int getentry();
54 * The list of tags generated by the last findgtag() call.
56 * Use either pattern or line number.
57 * findgtag() always uses line number, so pattern is always NULL.
58 * findctag() uses either pattern (in which case line number is 0),
59 * or line number (in which case pattern is NULL).
65 #define TAG_END ((struct tag *) &taglist)
66 static struct taglist taglist = { TAG_END, TAG_END };
68 struct tag *next, *prev; /* List links */
69 char *tag_file; /* Source file containing the tag */
70 LINENUM tag_linenum; /* Appropriate line number in source file */
71 char *tag_pattern; /* Pattern used to find the tag */
72 char tag_endline; /* True if the pattern includes '$' */
74 static struct tag *curtag;
77 (tp)->next = TAG_END; \
78 (tp)->prev = taglist.tl_last; \
79 taglist.tl_last->next = (tp); \
80 taglist.tl_last = (tp);
83 (tp)->next->prev = (tp)->prev; \
84 (tp)->prev->next = (tp)->next;
87 * Delete tag structures.
92 register struct tag *tp;
95 * Delete any existing tag list.
96 * {{ Ideally, we wouldn't do this until after we know that we
97 * can load some other tag information. }}
99 while ((tp = taglist.tl_first) != TAG_END)
109 * Create a new tag entry.
112 maketagent(name, file, linenum, pattern, endline)
119 register struct tag *tp;
121 tp = (struct tag *) ecalloc(sizeof(struct tag), 1);
122 tp->tag_file = (char *) ecalloc(strlen(file) + 1, sizeof(char));
123 strcpy(tp->tag_file, file);
124 tp->tag_linenum = linenum;
125 tp->tag_endline = endline;
127 tp->tag_pattern = NULL;
130 tp->tag_pattern = (char *) ecalloc(strlen(pattern) + 1, sizeof(char));
131 strcpy(tp->tag_pattern, pattern);
144 if (strcmp(tags, "GTAGS") == 0)
146 if (strcmp(tags, "GRTAGS") == 0)
148 if (strcmp(tags, "GSYMS") == 0)
150 if (strcmp(tags, "GPATH") == 0)
152 if (strcmp(tags, "-") == 0)
154 f = open(tags, OPEN_READ);
164 * Find tags in tag file.
165 * Find a tag in the "tags" file.
166 * Sets "tag_file" to the name of the file containing the tag,
167 * and "tagpattern" to the search pattern which should be used
174 int type = gettagtype();
175 enum tag_result result;
178 result = findctag(tag);
180 result = findgtag(tag, type);
187 error("No tags file", NULL_PARG);
190 error("No such tag in tags file", NULL_PARG);
193 error("unknown tag type", NULL_PARG);
205 return (NULL_POSITION); /* No gtags loaded! */
206 if (curtag->tag_linenum != 0)
213 * Go to the next tag.
219 char *tagfile = (char *) NULL;
222 tagfile = nextgtag();
227 * Go to the previous tag.
233 char *tagfile = (char *) NULL;
236 tagfile = prevgtag();
241 * Return the total number of tags.
250 * Return the sequence number of current tag.
258 /*****************************************************************************
263 * Find tags in the "tags" file.
264 * Sets curtag to the first tag entry.
266 static enum tag_result
279 char tline[TAGLINE_SIZE];
282 p = shell_unquote(tags);
290 taglen = strlen(tag);
293 * Search the tags file for the desired tag.
295 while (fgets(tline, sizeof(tline), f) != NULL)
298 /* Skip header of extended format. */
300 if (strncmp(tag, tline, taglen) != 0 || !WHITESP(tline[taglen]))
305 * The line contains the tag, the filename and the
306 * location in the file, separated by white space.
307 * The location is either a decimal line number,
308 * or a search pattern surrounded by a pair of delimiters.
309 * Parse the line and extract these parts.
314 * Skip over the whitespace after the tag name.
316 p = skipsp(tline+taglen);
318 /* File name is missing! */
322 * Save the file name.
323 * Skip over the whitespace after the file name.
326 while (!WHITESP(*p) && *p != '\0')
331 /* Pattern is missing! */
335 * First see if it is a line number.
338 taglinenum = getnum(&p, 0, &err);
342 * No, it must be a pattern.
343 * Delete the initial "^" (if present) and
344 * the final "$" from the pattern.
345 * Delete any backslash in the pattern.
352 while (*p != search_char && *p != '\0')
358 tagendline = (p[-1] == '$');
363 tp = maketagent(tag, tagfile, taglinenum, tagpattern, tagendline);
370 curtag = taglist.tl_first;
376 * Edit current tagged file.
383 return (edit(curtag->tag_file));
388 * This is a stripped-down version of search().
389 * We don't use search() for several reasons:
390 * - We don't want to blow away any search string we may have saved.
391 * - The various regular-expression functions (from different systems:
392 * regcmp vs. re_comp) behave differently in the presence of
393 * parentheses (which are almost always found in a tag).
398 POSITION pos, linepos;
404 linenum = find_linenum(pos);
409 * Get lines until we find a matching one or
410 * until we hit end-of-file.
413 return (NULL_POSITION);
416 * Read the next line, and save the
417 * starting position of that line in linepos.
420 pos = forw_raw_line(pos, &line, (int *)NULL);
424 if (pos == NULL_POSITION)
427 * We hit EOF without a match.
429 error("Tag not found", NULL_PARG);
430 return (NULL_POSITION);
434 * If we're using line numbers, we might as well
435 * remember the information we have now (the position
436 * and line number of the current line).
439 add_lnum(linenum, pos);
442 * Test the line to see if we have a match.
443 * Use strncmp because the pattern may be
444 * truncated (in the tags file) if it is too long.
445 * If tagendline is set, make sure we match all
446 * the way to end of line (no extra chars after the match).
448 len = strlen(curtag->tag_pattern);
449 if (strncmp(curtag->tag_pattern, line, len) == 0 &&
450 (!curtag->tag_endline || line[len] == '\0' || line[len] == '\r'))
452 curtag->tag_linenum = find_linenum(linepos);
460 /*******************************************************************************
465 * Find tags in the GLOBAL's tag file.
466 * The findgtag() will try and load information about the requested tag.
467 * It does this by calling "global -x tag" and storing the parsed output
468 * for future use by gtagsearch().
469 * Sets curtag to the first tag entry.
471 static enum tag_result
473 char *tag; /* tag to load */
474 int type; /* tags type */
480 if (type != T_CTAGS_X && tag == NULL)
487 * If type == T_CTAGS_X then read ctags's -x format from stdin
488 * else execute global(1) and read from it.
490 if (type == T_CTAGS_X)
493 /* Set tag default because we cannot read stdin again. */
503 char *cmd = lgetenv("LESSGLOBALTAGS");
505 if (cmd == NULL || *cmd == '\0')
507 /* Get suitable flag value for global(1). */
526 /* Get our data from global(1). */
527 qtag = shell_quote(tag);
530 command = (char *) ecalloc(strlen(cmd) + strlen(flag) +
531 strlen(qtag) + 5, sizeof(char));
532 sprintf(command, "%s -x%s %s", cmd, flag, qtag);
535 fp = popen(command, "r");
541 while (fgets(buf, sizeof(buf), fp))
543 char *name, *file, *line;
555 if (len > 0 && buf[len-1] == '\n')
562 } while (c != '\n' && c != EOF);
565 if (getentry(buf, &name, &file, &line))
568 * Couldn't parse this line for some reason.
569 * We'll just pretend it never happened.
574 /* Make new entry and add to list. */
575 tp = maketagent(name, file, (LINENUM) atoi(line), NULL, 0);
590 /* Check to see if we found anything. */
591 tp = taglist.tl_first;
599 static int circular = 0; /* 1: circular tag structure */
602 * Return the filename required for the next gtag in the queue that was setup
603 * by findgtag(). The next call to gtagsearch() will try to position at the
620 /* Wrapped around to the head of the queue */
621 curtag = taglist.tl_first;
628 return (curtag->tag_file);
632 * Return the filename required for the previous gtag in the queue that was
633 * setup by findgtat(). The next call to gtagsearch() will try to position
634 * at the appropriate tag.
650 /* Wrapped around to the tail of the queue */
651 curtag = taglist.tl_last;
658 return (curtag->tag_file);
662 * Position the current file at at what is hopefully the tag that was chosen
663 * using either findtag() or one of nextgtag() and prevgtag(). Returns -1
664 * if it was unable to position at the tag, 0 if successful.
670 return (NULL_POSITION); /* No gtags loaded! */
671 return (find_pos(curtag->tag_linenum));
675 * The getentry() parses both standard and extended ctags -x format.
678 * <tag> <lineno> <file> <image>
679 * +------------------------------------------------
680 * |main 30 main.c main(argc, argv)
681 * |func 21 subr.c func(arg)
683 * The following commands write this format.
684 * o Traditinal Ctags with -x option
685 * o Global with -x option
686 * See <http://www.gnu.org/software/global/global.html>
689 * <tag> <type> <lineno> <file> <image>
690 * +----------------------------------------------------------
691 * |main function 30 main.c main(argc, argv)
692 * |func function 21 subr.c func(arg)
694 * The following commands write this format.
695 * o Exuberant Ctags with -x option
696 * See <http://ctags.sourceforge.net>
698 * Returns 0 on success, -1 on error.
699 * The tag, file, and line will each be NUL-terminated pointers
703 getentry(buf, tag, file, line)
704 char *buf; /* standard or extended ctags -x format data */
705 char **tag; /* name of the tag we actually found */
706 char **file; /* file in which to find this tag */
707 char **line; /* line number of file where this tag is found */
711 for (*tag = p; *p && !IS_SPACE(*p); p++) /* tag name */
716 for ( ; *p && IS_SPACE(*p); p++) /* (skip blanks) */
721 * If the second part begin with other than digit,
722 * it is assumed tag type. Skip it.
726 for ( ; *p && !IS_SPACE(*p); p++) /* (skip tag type) */
728 for (; *p && IS_SPACE(*p); p++) /* (skip blanks) */
733 *line = p; /* line number */
734 for (*line = p; *p && !IS_SPACE(*p); p++)
739 for ( ; *p && IS_SPACE(*p); p++) /* (skip blanks) */
743 *file = p; /* file name */
744 for (*file = p; *p && !IS_SPACE(*p); p++)
751 if (strlen(*tag) && strlen(*line) && strlen(*file) && atoi(*line) > 0)