I'd like to check whether some (say, current) buffer is visiting a file or not. I could say:
(if (buffer-file-name) ...)
but it seems to be not very elegant - what I'm interested in is only the boolean value, not the actual name of the buffer in question. If the buffer-file-name
function were written in Elisp, I could look into its source to find out what it uses - but it is written in C, and while I could install the Emacs sources, I'm afraid that I wouldn't find an elisp name for the function that checks what I'm after there anyway.
What I need it for is I want to create a directory based on the name of the current buffer's file, and currently I'm doing more or less this:
(make-directory (if (buffer-file-name) (file-name-base) "default-dir"))
So, what would be the Elisp-idiomatic way of doing this?
buffer-file-name
really, it's the right way to do it (if you really really wantt
, do(and (buffer-file-name) t)
but that's uglier IMO). Its implementation is reading thefilename
field of the buffer C struct, which is anyway not accessible directly from Elisp. In the end, it's just a pointer that is either null or not.(and (buffer-file-name) t)
looks strange.(if (buffer-file-name) ... )
is elegant, then you haven't been coding in elisp for very long. It only gets uglier from here.