Skip to content

Instantly share code, notes, and snippets.

@ieure
Created January 5, 2011 23:40
Show Gist options
  • Save ieure/767239 to your computer and use it in GitHub Desktop.
Save ieure/767239 to your computer and use it in GitHub Desktop.
(defun locate-tags-file (&optional dir)
"Locate a TAGS or TAGS.gz file in DIR or a parent directory."
(let ((dir (or dir default-directory))
(tag-files '("TAGS" "TAGS.gz"))
(tag-dir)
(tag-file))
(while (and (not tag-file) tag-files)
(let ((potential-file (car tag-files)))
(setq tag-files (cdr tag-files))
(when (setq tag-dir (locate-dominating-file dir potential-file))
(setq tag-file (concat tag-dir
(if (not (equal (substring dir -1) "/")) "/" "")
potential-file)))))
tag-file))
(defmacro with-project-tags (&rest body)
"Temporarily bind tags file to the closest tags file, then execute BODY."
`(let* ((local-tags (locate-tags-file))
(tags-file-name (or locate-tags-file tags-file-name))
(tags-revert-without-query t)
(tags-table-list (if local-tags nil tags-table-list)))
,@body))
(defadvice find-tag (around use-local-tags first activate)
"Force find-tag to use the closest tag file."
(with-project-tags
ad-do-it))
(defadvice find-tag-regexp (around use-local-tags first activate)
"Force find-tag-regexp to use the closest tag file."
(with-project-tags
ad-do-it))
(defadvice tags-apropos (around use-local-tags first activate)
"Force tags-apropos to use the closest tag file."
(with-project-tags
ad-do-it))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment