Motivation / Background
- I use the
ffap
counterparts of common file- and directory-finding commands, i.e.find-file-at-point
,dired-at-point
et al. in place offind-file
,dired
et al., respectively. - I want to set
ffap-file-finder
(the file-finding subroutine defaulting tofind-file
whichffap
uses behind the scenes) tocounsel-find-file
from thecounsel
package. counsel-find-file
returns a plain, non-TRAMP-qualified filename, in contrast withfind-file
et al. which return the buffer visiting the given file.- I thus want to write a function suitable as a value for
ffap-file-finder
(i.e. one that returns a buffer object) usingcounsel-find-file
(which returns a plain filename).
Observations
I have tried calling find-buffer-visiting
on the filename returned by counsel-find-file
, but this approach breaks down when the file in question has been opened with TRAMP sudo
syntax. Using the file /etc/pam.conf
with inode 123
and device number 456
on host foo
as an example, I have made the following observations in such cases:
;; This actually calls `(find-file "/sudo:root@foo:/etc/pam.conf")' internally
(counsel-find-file) ; => "/etc/pam.conf"
;; These follow the call to `counsel-find-file'
(get-buffer "pam.conf") ; => #<buffer pam.conf>
(get-file-buffer "pam.conf") ; => nil
(get-file-buffer "/etc/pam.conf") ; => nil
(find-buffer-visiting "pam.conf") ; => nil
(find-buffer-visiting "/etc/pam.conf") ; => nil
(nthcdr 10 (file-attributes "/etc/pam.conf")) ; => (123 456)
Inside the buffer of the sudo
-opened "/etc/pam.conf"
file:
buffer-file-name ; => #("/sudo:root@foo:/etc/pam.conf" 6 10 (tramp-default t))
buffer-file-number ; => (123 (-1 . 1))
AFAICT, (find-buffer-visiting "/etc/pam.conf")
fails when "/etc/pam.conf"
has been opened via TRAMP because find-buffer-visiting
:
- compares the given plain filename with the TRAMP-syntax values of
buffer-file-name
andbuffer-file-truename
; and - compares both inode and device numbers to determine file equality, but TRAMP seems to modify the device number.
Questions
- Is it necessary for TRAMP to change the
file-attribute-device-number
of visited files? If so, why? - How does TRAMP compare files for equality in such cases?
- Given a plain, non-TRAMP-qualified filename, how do I lookup a TRAMP buffer visiting it?
- How do I make
find-buffer-visiting
work with TRAMP buffers?
(These questions overlap, but I would like to cover all bases.)
Addendum
I realise that I can call (get-buffer (counsel-find-file))
in my example, but I fear this is not a robust approach in the face of multiple buffers with uniquified names visiting the same file. I am also curious as to whether there is a more general and "correct" TRAMP solution to my questions.
counsel-find-file
to return more useful results? Or, assuming it strips the tramp context on purpose, to implement an analog function which does not do that?counsel-find-file
returning a filename instead of a buffer, but note thatcounsel-find-file
: a) is not the main focus of my question; b) does not strip any Tramp context; and c) does not claim to be a drop-infind-file
replacement.