0

我对以下代码有一点问题(它应该删除文件并终止缓冲区):

(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)
      )))

它不起作用:当我对没有文件的缓冲区进行操作时,它给出“如果:错误的类型参数:stringp,nil”,当我对文件的缓冲区进行操作时,它的行为就好像它是缓冲区-没有文件(即它打印“我已经掩埋了 %s 缓冲区” - 而它应该打印“我已经删除了 %s 并掩埋了它的缓冲区 %s”)

4

1 回答 1

1

据我所知,只有一个错误,这是代码无法在与文件无关的缓冲区上工作。在这种情况下,buffer-file-nameisnilfile-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))))
于 2012-08-07T12:15:05.190 回答