Fix 'list-tags' when invoked from a non-file buffer

This use case was broken by the improvement that attempts to
offer the current buffer's file name as the default file whose
tags to list.
* lisp/progmodes/etags.el
(tags--get-current-buffer-name-in-tags-file): Doc fix.  Return nil
if no file is associated with the current buffer, and avoid
signaling an error if 'buffer-file-name' returns nil.  (Bug#37611)
(list-tags): Doc fix.  Signal an error if the user specifies no
file name at the prompt.

* doc/emacs/maintaining.texi (List Identifiers): Fix wording of
the documentation of 'list-tags'.
This commit is contained in:
Eli Zaretskii 2024-10-04 14:39:50 +03:00
parent 51ef05f684
commit e9dcf0c57d
2 changed files with 32 additions and 20 deletions

View file

@ -2664,14 +2664,13 @@ loaded, this command can use it to generate completion candidates.
@xref{Symbol Completion}. @xref{Symbol Completion}.
@findex list-tags @findex list-tags
@kbd{M-x list-tags} reads the name of one of the files covered by @kbd{M-x list-tags} reads the name of one of the files covered by the
the selected tags table, and displays a list of tags defined in that selected tags table, with completion, and displays the list of tags
file. Do not include a directory as part of the file name unless the defined in that file; it offers the current buffer's file name as the
file name recorded in the tags table includes a directory. This default file whose tags to list. Do not include a directory as part of
command works only with the etags backend, and requires a tags table the file name unless the file name recorded in the tags table includes a
for the project to be available. @xref{Tags Tables}. If used directory. This command works only with the etags backend, and requires
interactively, the default tag is file name of the current buffer if a tags table for the project to be available. @xref{Tags Tables}.
used interactively.
@findex tags-next-file @findex tags-next-file
@kbd{M-x tags-next-file} visits files covered by the selected tags table. @kbd{M-x tags-next-file} visits files covered by the selected tags table.

View file

@ -1894,27 +1894,40 @@ description of the arguments."
(try-completion string (tags-table-files) predicate)))) (try-completion string (tags-table-files) predicate))))
(defun tags--get-current-buffer-name-in-tags-file () (defun tags--get-current-buffer-name-in-tags-file ()
"Get the file name that the current buffer corresponds in the tags file." "Return file name that corresponds to the current buffer in the tags table.
(let ((tag-dir This returns the file name which corresponds to the current buffer relative
(save-excursion to the directory of the current tags table (see `visit-tags-table-buffer').
(visit-tags-table-buffer) If no file is associated with the current buffer, this function returns nil."
(file-name-directory (buffer-file-name))))) (let ((buf-fname (buffer-file-name)))
(file-relative-name (buffer-file-name) tag-dir))) ;; FIXME: Are there interesting cases where 'buffer-file-name'
;; returns nil, but there's some file we expect to find in TAGS that
;; is associated with the buffer? The obvious cases of Dired and
;; Info buffers are not interesting for TAGS, but are there any
;; others?
(if buf-fname
(let ((tag-dir
(save-excursion
(visit-tags-table-buffer)
(file-name-directory buf-fname))))
(file-relative-name buf-fname tag-dir)))))
;;;###autoload ;;;###autoload
(defun list-tags (file &optional _next-match) (defun list-tags (file &optional _next-match)
"Display list of tags in file FILE. "Display list of tags in file FILE.
This searches only the first table in the list, and no included Interactively, prompt for FILE, with completion, offering the current
tables. FILE should be as it appeared in the `etags' command, buffer's file name as the defaul.
usually without a directory specification. If called This command searches only the first table in the list of tags tables,
interactively, FILE defaults to the file name of the current and does not search included tables.
buffer." FILE should be as it was submitted to the `etags' command, which usually
means relative to the directory of the tags table file."
(interactive (list (completing-read (interactive (list (completing-read
"List tags in file: " "List tags in file: "
'tags-complete-tags-table-file 'tags-complete-tags-table-file
nil t nil t
;; Default FILE to the current buffer. ;; Default FILE to the current buffer's file.
(tags--get-current-buffer-name-in-tags-file)))) (tags--get-current-buffer-name-in-tags-file))))
(if (string-empty-p file)
(user-error "You must specify a file name"))
(with-output-to-temp-buffer "*Tags List*" (with-output-to-temp-buffer "*Tags List*"
(princ (substitute-command-keys "Tags in file `")) (princ (substitute-command-keys "Tags in file `"))
(tags-with-face 'highlight (princ file)) (tags-with-face 'highlight (princ file))