Function: find-buffer-visiting

find-buffer-visiting is a byte-compiled function defined in files.el.gz.

Signature

(find-buffer-visiting FILENAME &optional PREDICATE)

Documentation

Return the buffer visiting file FILENAME (a string).

This is like get-file-buffer, except that it checks for any buffer visiting the same file, possibly under a different name.

If PREDICATE is non-nil, only buffers satisfying it are eligible, and others are ignored. PREDICATE is called with the buffer as the only argument, but not with the buffer as the current buffer.

Note that indirect buffers don't count as visiting files, and that therefore this function only ever returns base buffers.

If there is no such live buffer, return nil.

View in manual

Aliases

idlwave-get-buffer-visiting (obsolete since 28.1) reftex-get-buffer-visiting (obsolete since 28.1)

Source Code

;; Defined in /usr/src/emacs/lisp/files.el.gz
(defun find-buffer-visiting (filename &optional predicate)
  "Return the buffer visiting file FILENAME (a string).
This is like `get-file-buffer', except that it checks for any buffer
visiting the same file, possibly under a different name.

If PREDICATE is non-nil, only buffers satisfying it are eligible,
and others are ignored.  PREDICATE is called with the buffer as
the only argument, but not with the buffer as the current buffer.

Note that indirect buffers don't count as visiting files,
and that therefore this function only ever returns base buffers.

If there is no such live buffer, return nil."
  (or (let ((buf (get-file-buffer filename)))
        (when (and buf (or (not predicate) (funcall predicate buf))) buf))
      (let ((truename (abbreviate-file-name (file-truename filename))))
        (or
         (let ((buf (get-truename-buffer truename)))
           (when (and buf (buffer-local-value 'buffer-file-name buf)
                      (or (not predicate) (funcall predicate buf)))
             buf))
         (let* ((attributes (file-attributes truename))
                (number (file-attribute-file-identifier attributes)))
           (and buffer-file-numbers-unique
                (car-safe number)       ;Make sure the inode is not just nil.
                (let* ((buf (find-buffer 'buffer-file-number number))
                       (buf-file-name
                        (and buf (buffer-local-value 'buffer-file-name buf))))
                  (when (and buf-file-name
                             ;; Verify this buffer's file number
                             ;; still belongs to its file.
                             (file-exists-p buf-file-name)
                             (equal (file-attributes
                                     (buffer-local-value
                                      'buffer-file-truename buf))
                                    attributes)
                             (or (not predicate)
                                 (funcall predicate buf)))
                    buf))))))))