I’ve a little problem with the following code (it supposed to delete the file and kill the buffer):
(defun bk-deletes-file ()
"Closes the buffer and deletes associated file."
(interactive)
(let (curFile curBuffer)
(if (file-exists-p buffer-file-name)
(progn
(setq curFile buffer-file-name)
(delete-file buffer-file-name))
(setq curFile 1))
(setq curBuffer (buffer-name))
(kill-this-buffer)
(if (stringp curFile)
(message "I've deleted %s and buried its buffer %s"
(file-name-nondirectory curFile) curBuffer)
(message "I've buried %s buffer" curBuffer)
)))
It doesn’t work: when I act on buffer-with-no-file it gives “if: Wrong type argument: stringp, nil”, when I act on buffer-of-the-file it behaves as if it was buffer-with-no-file (that is it prints “I’ve buried %s buffer” – while it should print “I’ve deleted %s and buried its buffer %s”)
As far as I can tell there is only one error and this is that the code cannot work on a buffer not associated with a file. In that case,
buffer-file-nameisnilandfile-exists-pcomplains of not being passed a string.Here is a version working (with a few improvements for readability and lisp good practices):