How about a slightly different beat? When you open the file (which is interesting for you), at this point add it to the TAGS file. You can do this quite easily with the following code:
(setq tags-file-name "/scratch2/TAGS") (setq tags-revert-without-query t) (add-hook 'find-file-hooks 'add-opened-file-to-tags) (defun add-opened-file-to-tags () "every time a file is opened, add it to the TAGS file (if not already present) Note: only add it to the TAGS file when the major mode is one we care about" (when (memq major-mode '(c-mode c++-mode)) (let ((opened-file (buffer-file-name))) (save-excursion (visit-tags-table-buffer) (unless (member opened-file (tags-table-files)) (shell-command (format "etags -a --output %s %s" tags-file-name opened-file))))))) ;; create an empty TAGS file if necessary (unless (file-exists-p tags-file-name) (shell-command (format "touch %s" tags-file-name)))
From time to time, you want to delete the TAGS file to update the content. Or you can use something like Mx refresh-tags-table :
(defun refresh-tags-file () "rebuild the tags file" (interactive) (let ((tags-files (save-excursion (visit-tags-table-buffer) (tags-table-files)))) (delete-file tags-file-name) (dolist (file tags-files) (shell-command (format "etags -a --output %s %s" tags-file-name file)))))
source share