2012-08-07 95 views
0

我已經用下面的代碼有點問題(它應該刪除該文件,並殺死緩衝劑):簡單elisp的錯誤

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

它不工作:當我作用於緩衝區與 - no-file給出了「if:錯誤的類型參數:stringp,nil」,當我在緩衝區文件上作用時,它的行爲就好像它是緩衝區無文件一樣(就是它打印出「I've掩埋%s緩衝區「 - 雖然它應該打印」我已經刪除%s並掩埋其緩衝區%s「)

+0

順便說一句,「埋」和「殺」是兩個不同的概念。雖然這些詞按照現實世界的說法分享了一些語義,但Emacs緩衝區永遠不會被殺死;它只是推到緩衝區列表的後面。 – tripleee 2012-08-07 12:57:53

+0

@tripleee:好的。我現在不需要「埋葬」了。 – Adobe 2012-08-07 13:14:55

回答

1

據我所知,只有一個錯誤,這是代碼無法工作緩衝區不與文件關聯。在這種情況下,buffer-file-namenilfile-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)))) 
+0

它的工作原理。謝謝。 – Adobe 2012-08-07 12:27:31