From 046f3e38b1441277735bef26e2ea20bfda9544bf Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 22 Feb 2022 10:49:45 +0100 Subject: gkleen@sif: emacs: move-file --- accounts/gkleen@sif/emacs.el | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'accounts') diff --git a/accounts/gkleen@sif/emacs.el b/accounts/gkleen@sif/emacs.el index c57118b4..ad04c9ea 100644 --- a/accounts/gkleen@sif/emacs.el +++ b/accounts/gkleen@sif/emacs.el @@ -188,3 +188,26 @@ necessarily running." (global-set-key (kbd "C-x k") 'kill-buffer-with-special-emacsclient-handling)) (add-hook 'server-switch-hook 'install-emacsclient-wrapped-kill-buffer) + +(defun move-file (new-location) + "Write this file to NEW-LOCATION, and delete the old one." + (interactive (list (expand-file-name + (if buffer-file-name + (read-file-name "Move file to: ") + (read-file-name "Move file to: " + default-directory + (expand-file-name (file-name-nondirectory (buffer-name)) + default-directory)))))) + (when (file-exists-p new-location) + (delete-file new-location)) + (let ((old-location (expand-file-name (buffer-file-name)))) + (message "old file is %s and new file is %s" + old-location + new-location) + (write-file new-location t) + (when (and old-location + (file-exists-p new-location) + (not (string-equal old-location new-location))) + (delete-file old-location)))) + +(bind-key "C-x C-m" #'move-file) -- cgit v1.2.3