unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Zachary Kanfer <zkanfer@gmail.com>
To: 56311@debbugs.gnu.org
Subject: bug#56311: [PATCH] new function: delete-visited-file
Date: Thu, 30 Jun 2022 00:26:06 -0400	[thread overview]
Message-ID: <CAFXT+ROpz1XOQ11YP+=y=4WruHned-=JMxn2y61LvXFdyNsyCg@mail.gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 415 bytes --]

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.

[-- Attachment #1.2: Type: text/html, Size: 513 bytes --]

[-- Attachment #2: 0001-Add-new-function-delete-visited-file.patch --]
[-- Type: text/x-patch, Size: 2336 bytes --]

From 7ce913013a022ec84b11f3abc22bc82e06825f1e Mon Sep 17 00:00:00 2001
From: Zachary Kanfer <zkanfer@gmail.com>
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


             reply	other threads:[~2022-06-30  4:26 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-30  4:26 Zachary Kanfer [this message]
2022-06-30  5:30 ` bug#56311: [PATCH] new function: delete-visited-file Eli Zaretskii
2022-06-30  5:49   ` Sean Whitton
2022-06-30  5:56     ` Eli Zaretskii
2022-06-30  6:20   ` Visuwesh
2022-06-30 10:27     ` Lars Ingebrigtsen
2022-06-30 16:29       ` Sean Whitton
2022-07-01  3:29         ` Zachary Kanfer
2022-07-01  5:57           ` Eli Zaretskii
2022-07-03  5:06             ` Zachary Kanfer
2022-07-03  6:04               ` Eli Zaretskii
2022-08-02 11:12                 ` Lars Ingebrigtsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAFXT+ROpz1XOQ11YP+=y=4WruHned-=JMxn2y61LvXFdyNsyCg@mail.gmail.com' \
    --to=zkanfer@gmail.com \
    --cc=56311@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).