How can I identify the owner/creator of a Buffer in Emacs

elisp, emacs

Solution

Note the difference between a buffer and a file: A file is something that sits on your hard disk, such as a .jpg image file or a .mp3 aufio file or a .txt file. Some of those files - typically text files - you might want to edit with Emacs. To do so, you can load the file into Emacs - this is called "visiting" a file in Emacs lingo. The contents of the file are displayed in a buffer. But note that you could also have a buffer that is not associated with a file at all - for instance the `*scratch*` buffer that gets displayed if you start up Emacs without specifying a file.

Thus files and buffers are pretty much orthogonal concepts, although often times you create buffers by visiting a file, and you save the contents of a buffer by writing to a file. (You can create a buffer that is not associated with a file by typing C-x b buffer-name where buffer-name is an identifier not used by any of the already existing buffers.)

A buffer exists only inside a running Emacs. This is why the comments and answers you have gotten so far may not have been what you're looking for: the notion of the creator/owner of the buffer is confusing, because it is obviously the person who's sitting at the keyboard at that particular moment.

Speaking of the owner/creator of a file makes much more sense. In a multi-account setup, more than one user can write to the same disk, and so they might have access to the same files. Now it can be interesting to know who has access, and in particular who owns the file or when it was last modified. In Elisp, you can use the function

(file-attributes FILENAME &optional ID-FORMAT)

to get a list of attributes associated with the file. If your current buffer is visiting a file at all, you can combine that function with the function

(buffer-file-name &optional BUFFER)

which returns the file the buffer is visiting. For a buffer that is not visiting a file, this function returns `nil`.

Note, however, that some information you might be interested in is not available through `(file-attributes ...)`, such as who last accessed the file and/or who last modified it. This is not so much Emacs' fault, but comes from the fact that the operating system does not store such information.

Also note that the current owner of a file might not necessarily be the person who created it as someone with the required privileges can `chown` a file after its creation.

To receive information about the current user in the sense of `whoami`, you can check out the variables

user-login-name
user-real-login-name
user-full-name

by typing C-h v variable-name.

Problem

How can I check the identity of someone who has created a buffer in Emacs and then later on check whether is the same user accessing that buffer? I mean something like "Who Am I?" in Unix command.. and then check if the same user is accessing that document? --> I want a function or a way to this in my own code

Original source