emacs lisp: getting current buffer path

In your elisp program, you may call (buffer-file-name) to get the full path of the file, but sometimes the current buffer isn't associated with a file, so your program will fail.

Here's a idiom: (or (buffer-file-name) default-directory). This way, if the buffer isn't a file, it'll return the directory path of the buffer. (when a buffer is created, its “default-directory” is typically the same as previous buffer. In the case of temp buffers created by emacs such as {*info*, *scratch*, *Bookmark List*, …}, it's usually home dir.)

I just searched all my elisp files for this oversight.

Popular posts from this blog

Browser User Agent Strings 2012

11 Years of Writing About Emacs

does md5 creates more randomness?