Subject: [PATCH] new function: delete-visited-file

From: Zachary Kanfer <[email-masked]>
Date: Thu, 30 Jun 2022 00:26:06 -0400

When I delete a file, I almost always want the buffer visiting it to go
away also. Why keep it around? So I have to do the following steps:

1. M-x delete-file
2. navigate to the file, select it.
3. C-x k <ret>

So I wrote a function to delete the file a buffer is visiting, and close
the buffer. Now I do everything in a single logical action:

1. M-x delete-visited-file
2. select the buffer.

Patch is attached.
From 7ce913013a022ec84b11f3abc22bc82e06825f1e Mon Sep 17 00:00:00 2001
From: Zachary Kanfer <[email-masked]>
Date: Thu, 30 Jun 2022 00:21:01 -0400
Subject: [PATCH] Add new function delete-visited-file.

* lisp/files.el (delete-visited-file) New command

* doc/emacs/files.texi (Miscellaneous File Operations): Document it.
---
 doc/emacs/files.texi |  2 ++
 etc/NEWS             |  5 +++++
 lisp/files.el        | 11 +++++++++++
 3 files changed, 18 insertions(+)

diff --git a/doc/emacs/files.texi b/doc/emacs/files.texi
index fa02d264f9..4bfda182b4 100644
--- a/doc/emacs/files.texi
+++ b/doc/emacs/files.texi
@@ -1930,6 +1930,8 @@ Misc File Ops
 execution permission for the user who owns the file.  It has no effect
 on operating systems that do not support file modes.  @code{chmod} is a
 convenience alias for this function.
+@findex delete-visited-file
+  @kbd{delete-visited-file} deletes the file visited by a buffer, and closes the buffer.
 
 @node Compressed Files
 @section Accessing Compressed Files
diff --git a/etc/NEWS b/etc/NEWS
index ce32542028..b5524d35fd 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -355,6 +355,11 @@ match those regexps will be ignored by 'switch-to-prev-buffer' and
 This command renames the file visited by the current buffer by moving
 it to a new location, and also makes the buffer visit this new file.
 
++++
+** New command 'delete-visited-file'.
+This command deletes the file visited by a buffer, then closes the
+buffer.
+
 ** Menus
 
 ---
diff --git a/lisp/files.el b/lisp/files.el
index 1295c24c93..f5d512d6be 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -6267,6 +6267,17 @@ delete-directory
 		  directory-exists))
 	(files--force recursive #'delete-directory-internal directory))))))
 
+(defun delete-visited-file (buffer-name)
+  "Delete the file visited by buffer BUFFER-NAME, then close the buffer."
+  (interactive "bDelete file visited by buffer ")
+  (let* ((buffer (get-buffer buffer-name))
+         (filename (buffer-file-name buffer)))
+    (when buffer
+      (when (and filename
+                 (file-exists-p filename))
+        (delete-file filename))
+      (kill-buffer buffer))))
+
 (defun file-equal-p (file1 file2)
   "Return non-nil if files FILE1 and FILE2 name the same file.
 If FILE1 or FILE2 does not exist, the return value is unspecified."
-- 
2.25.1