我对以下代码有点问题(它应该删除文件并终止缓冲区):
(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)
)))
它不起作用:当我对无文件缓冲区执行操作时,它会给出" if:错误的类型参数:stringp,nil",当我对缓冲区执行操作时-file它的行为好像是缓冲区没有文件(就是它打印"我已经埋没了%s缓冲区" - 虽然它应该打印"我' ve删除%s并隐藏其缓冲区%s")
答案 0 :(得分:1)
据我所知,只有一个错误,这就是代码无法在与文件无关的缓冲区上工作。在这种情况下,buffer-file-name
为nil
,file-exists-p
抱怨没有传递字符串。
这是一个正在运行的版本(对可读性和lisp良好实践进行了一些改进):
(defun bk-deletes-file ()
"Kills the buffer and deletes associated file."
(interactive)
(let ((current-file (buffer-file-name))
(current-buffer (buffer-name)))
(when current-file
(delete-file current-file))
(kill-this-buffer)
(if current-file
(message "I've deleted %s and killed its buffer %s"
(file-name-nondirectory current-file) current-buffer)
(message "I've killed %s buffer" current-buffer))))