- ;; Here we search for PAT in the range [STARTPOS - OFFSET,
- ;; STARTPOS + OFFSET], with increasing values of OFFSET.
- ;;
- ;; We used to set the initial offset to 1000, but the
- ;; actual sources show that finer-grained control is
- ;; needed (e.g. two `hash_string's in src/symbols.c.) So,
- ;; I changed 100 to 100, and (* 3 offset) to (* 5 offset).
- (setq offset 100)
- (setq pat (concat "^" (regexp-quote linebeg)))
- (or startpos (setq startpos (point-min)))
- (while (and (not found)
- (progn
- (goto-char (- startpos offset))
- (not (bobp))))
- (setq found (re-search-forward pat (+ startpos offset) t))
- (setq offset (* 5 offset)))
- ;; Finally, try finding it anywhere in the buffer.
- (or found
- (re-search-forward pat nil t)
- (error "%s not found in %s" pat file))
- (beginning-of-line)
- (setq startpos (point)))))
+ (if (eq linebeg t)
+ ;; Direct file tag.
+ (cond (line (goto-line line))
+ (startpos (goto-char startpos))
+ (t (error "etags.el BUG: bogus direct file tag")))
+ ;; Here we search for PAT in the range [STARTPOS - OFFSET,
+ ;; STARTPOS + OFFSET], with increasing values of OFFSET.
+ ;;
+ ;; We used to set the initial offset to 1000, but the
+ ;; actual sources show that finer-grained control is
+ ;; needed (e.g. two `hash_string's in src/symbols.c.) So,
+ ;; I changed 1000 to 100, and (* 3 offset) to (* 5 offset).
+ (setq offset 100)
+ (setq pat (concat (if (eq selective-display t)
+ "\\(^\\|\^m\\)" "^")
+ (regexp-quote linebeg)))
+
+ ;; The character position in the tags table is 0-origin.
+ ;; Convert it to a 1-origin Emacs character position.
+ (if startpos (setq startpos (1+ startpos)))
+ ;; If no char pos was given, try the given line number.
+ (or startpos
+ (if line
+ (setq startpos (progn (goto-line line)
+ (point)))))
+ (or startpos
+ (setq startpos (point-min)))
+ ;; First see if the tag is right at the specified location.
+ (goto-char startpos)
+ (setq found (looking-at pat))
+ (while (and (not found)
+ (progn
+ (goto-char (- startpos offset))
+ (not (bobp))))
+ (setq found
+ (re-search-forward pat (+ startpos offset) t)
+ offset (* 5 offset))) ; expand search window
+ ;; Finally, try finding it anywhere in the buffer.
+ (or found
+ (re-search-forward pat nil t)
+ (error "Rerun etags: `%s' not found in %s"
+ pat file))))
+ ;; Position point at the right place
+ ;; if the search string matched an extra Ctrl-m at the beginning.
+ (and (eq selective-display t)
+ (looking-at "\^m")
+ (forward-char 1))
+ (beginning-of-line)
+ (setq startpos (point))))