all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: net june <netjune@outlook.com>
To: "Andreas Röhler" <andreas.roehler@online.de>,
	"emacs-devel@gnu.org" <emacs-devel@gnu.org>
Subject: Re: Rename, delete and move current buffer and file
Date: Fri, 11 May 2018 16:10:23 +0000	[thread overview]
Message-ID: <SINPR04MB17741443DA6AEEBC87063D17B79F0@SINPR04MB1774.apcprd04.prod.outlook.com> (raw)
In-Reply-To: <81031169-8e1d-48b6-9501-1f371a30aff2@online.de>



On 05/11/2018 02:26 PM, Andreas Röhler wrote:
> Sound cool and should save a keystroke WRT dired-jump and friends.
> May you post the code?
> 

Yes. The following is it.

(defun my-rename-file (arg)
   "Rename or delete the file visited by current buffer."
   (interactive "P")
   (unless buffer-file-name
	(user-error "Not a file buffer"))
   (let ((old-name (file-name-nondirectory buffer-file-name))
		new-file-name)
	(if arg
		(let ((tmp-file-name (read-file-name "Rename to: ")))
		  (setq new-file-name
				(if (file-directory-p tmp-file-name)
					(concat (file-name-as-directory tmp-file-name) old-name)
				  tmp-file-name)))
	  (let* ((new-name (read-string "New file name: " old-name)))
		(when (string= new-name old-name)
		  (user-error "Same as the old name. Not renamed."))
		(if (string= new-name "")
			(when (yes-or-no-p
				   (format "Delete file \"%s\"?" buffer-file-name))
			  ;; On fail, it will signal an error and abort.
			  (delete-file buffer-file-name)
			  (set-buffer-modified-p nil)
			  (kill-buffer)
			  (message "File deleted"))
		  (setq new-file-name (expand-file-name new-name)))))
	(when new-file-name
	  (let ((modify-flag (buffer-modified-p))
			(old-file-name buffer-file-name))
		;; On fail, it will signal an error and abort.
		(rename-file buffer-file-name new-file-name 1)
		(set-visited-file-name new-file-name)
		(rename-buffer (file-name-nondirectory new-file-name) t)
		(set-buffer-modified-p modify-flag)
		(message "Renamed: %s ==> %s" old-file-name new-file-name)))))



      reply	other threads:[~2018-05-11 16:10 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-07 13:58 Rename, delete and move current buffer and file Jarosław Rzeszótko
2018-05-07 14:53 ` Stefan Monnier
2018-05-07 16:20   ` Jarosław Rzeszótko
2018-05-07 17:01     ` Stefan Monnier
2018-05-07 17:47       ` Jarosław Rzeszótko
2018-05-08  1:28         ` Stefan Monnier
2018-05-08  7:05           ` Jarosław Rzeszótko
2018-05-09 12:28             ` Stefan Monnier
2018-05-09 18:12               ` Radon Rosborough
2018-05-09 22:44                 ` Stefan Monnier
2018-05-10 17:01                   ` Radon Rosborough
2018-05-10 21:49                     ` Stefan Monnier
2018-05-11  7:31                       ` Andreas Schwab
2018-05-11 15:33                         ` Stefan Monnier
2018-05-11 15:45                           ` Paul Eggert
2018-05-11 16:06                             ` Clément Pit-Claudel
2018-05-11 16:14                             ` Stefan Monnier
2018-05-09 23:50               ` Van L
2018-05-10  5:37                 ` Jarosław Rzeszótko
2018-05-10 13:18                   ` Van L
2018-05-10  8:20               ` Amit Ramon
2018-05-10  8:59                 ` Phil Sainty
2018-05-10 16:04                 ` Eli Zaretskii
2018-05-07 18:20     ` Andreas Schwab
2018-05-08  1:28       ` Stefan Monnier
2018-05-07 15:28 ` Yuri Khan
2018-05-07 16:20   ` Jarosław Rzeszótko
2018-05-09 11:46   ` Tino Calancha
2018-05-07 15:29 ` Andreas Röhler
2018-05-07 15:38   ` Andreas Röhler
2018-05-10  0:46   ` net june
2018-05-11  6:26     ` Andreas Röhler
2018-05-11 16:10       ` net june [this message]

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

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

  git send-email \
    --in-reply-to=SINPR04MB17741443DA6AEEBC87063D17B79F0@SINPR04MB1774.apcprd04.prod.outlook.com \
    --to=netjune@outlook.com \
    --cc=andreas.roehler@online.de \
    --cc=emacs-devel@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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.