Rewrite move/copy/delete file commands

To use rename-file, copy-file and delete-file, so these commands can
enjoy the benefits of any plugins that have advised these, like org-roam
does for rename-file.
This commit is contained in:
Henrik Lissner 2020-04-27 01:42:31 -04:00
parent 64f15bfe3c
commit c1127a5bde
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 71 additions and 102 deletions

View file

@ -203,52 +203,29 @@ single file or nested compound statement of `and' and `or' statements."
;; ;;
;;; Helpers ;;; Helpers
(defun doom--forget-file (path) (defun doom--update-files (&rest files)
"Ensure `recentf', `projectile' and `save-place' forget OLD-PATH." "Ensure FILES are updated in `recentf', `magit' and `save-place'."
(when (bound-and-true-p recentf-mode) (let (toplevels)
(recentf-remove-if-non-kept path)) (dolist (file files)
(when (and (bound-and-true-p projectile-mode) (when (featurep 'vc)
(doom-project-p) (vc-file-clearprops file)
(projectile-file-cached-p path (doom-project-root))) (when-let (buffer (get-file-buffer file))
(projectile-purge-file-from-cache path)) (with-current-buffer buffer
(when (bound-and-true-p save-place-mode) (vc-refresh-state))))
(save-place-forget-unreadable-files))) (when (featurep 'magit)
(when-let (default-directory (magit-toplevel (file-name-directory file)))
(defun doom--update-file (path) (cl-pushnew default-directory toplevels)))
(when (featurep 'vc) (unless (file-readable-p file)
(vc-file-clearprops path) (when (bound-and-true-p recentf-mode)
(vc-resynch-buffer path nil t)) (recentf-remove-if-non-kept file))
(when (featurep 'magit) (when (and (bound-and-true-p projectile-mode)
(when-let (default-directory (magit-toplevel (file-name-directory path))) (doom-project-p)
(magit-refresh)))) (projectile-file-cached-p file (doom-project-root)))
(projectile-purge-file-from-cache file))))
(defun doom--copy-file (old-path new-path &optional force-p) (dolist (default-directory toplevels)
(let* ((new-path (expand-file-name new-path)) (magit-refresh))
(old-path (file-truename old-path)) (when (bound-and-true-p save-place-mode)
(new-path (apply #'expand-file-name (save-place-forget-unreadable-files))))
(if (or (directory-name-p new-path)
(file-directory-p new-path))
(list (file-name-nondirectory old-path) new-path)
(list new-path))))
(new-path-dir (file-name-directory new-path))
(project-root (doom-project-root))
(short-new-name (if (and project-root (file-in-directory-p new-path project-root))
(file-relative-name new-path project-root)
(abbreviate-file-name new-path))))
(unless (file-directory-p new-path-dir)
(make-directory new-path-dir t))
(when (buffer-modified-p)
(save-buffer))
(cond ((file-equal-p old-path new-path)
(throw 'status 'overwrite-self))
((and (file-exists-p new-path)
(not force-p)
(not (y-or-n-p (format "File already exists at %s, overwrite?" short-new-name))))
(throw 'status 'aborted))
((file-exists-p old-path)
(copy-file old-path new-path t)
short-new-name)
(short-new-name))))
;; ;;
@ -256,73 +233,67 @@ single file or nested compound statement of `and' and `or' statements."
;;;###autoload ;;;###autoload
(defun doom/delete-this-file (&optional path force-p) (defun doom/delete-this-file (&optional path force-p)
"Delete FILENAME (defaults to the file associated with current buffer) and "Delete PATH, kill its buffers and expunge it from vc/magit cache.
kills the buffer. If FORCE-P, force the deletion (don't ask for confirmation)."
If PATH is not specified, default to the current buffer's file.
If FORCE-P, delete without confirmation."
(interactive (interactive
(list (file-truename (buffer-file-name)) (list (buffer-file-name (buffer-base-buffer))
current-prefix-arg)) current-prefix-arg))
(let* ((fbase (file-name-sans-extension (file-name-nondirectory path))) (let* ((path (or path (buffer-file-name (buffer-base-buffer))))
(buf (current-buffer))) (short-path (abbreviate-file-name path)))
(cond ((not (file-exists-p path)) (unless (and path (file-exists-p path))
(error "File doesn't exist: %s" path)) (user-error "Buffer is not visiting any file"))
((not (or force-p (y-or-n-p (format "Really delete %s?" fbase)))) (unless (file-exists-p path)
(message "Aborted") (error "File doesn't exist: %s" path))
nil) (unless (or force-p (y-or-n-p (format "Really delete %S?" short-path)))
((unwind-protect (user-error "Aborted"))
(progn (delete-file path) t) (let ((buf (current-buffer)))
(let ((short-path (file-relative-name path (doom-project-root)))) (unwind-protect
(if (file-exists-p path) (progn (delete-file path) t)
(error "Failed to delete %s" short-path) (if (file-exists-p path)
;; Ensures that windows displaying this buffer will be switched (error "Failed to delete %S" short-path)
;; to real buffers (`doom-real-buffer-p') ;; Ensures that windows displaying this buffer will be switched to
(doom/kill-this-buffer-in-all-windows buf t) ;; real buffers (`doom-real-buffer-p')
(doom--forget-file path) (doom/kill-this-buffer-in-all-windows buf t)
(doom--update-file path) (doom--update-files path)
(message "Successfully deleted %s" short-path)))))))) (message "Deleted %S" short-path))))))
;;;###autoload ;;;###autoload
(defun doom/copy-this-file (new-path &optional force-p) (defun doom/copy-this-file (new-path &optional force-p)
"Copy current buffer's file to NEW-PATH. If FORCE-P, overwrite the destination "Copy current buffer's file to NEW-PATH.
file if it exists, without confirmation."
If FORCE-P, overwrite the destination file if it exists, without confirmation."
(interactive (interactive
(list (read-file-name "Copy file to: ") (list (read-file-name "Copy file to: ")
current-prefix-arg)) current-prefix-arg))
(pcase (catch 'status (unless (and buffer-file-name (file-exists-p buffer-file-name))
(when-let (dest (doom--copy-file (buffer-file-name) new-path force-p)) (user-error "Buffer is not visiting any file"))
(doom--update-file new-path) (let ((old-path (buffer-file-name (buffer-base-buffer)))
(message "File successfully copied to %s" dest))) (new-path (expand-file-name new-path)))
(`overwrite-self (error "Cannot overwrite self")) (make-directory (file-name-directory new-path) 't)
(`aborted (message "Aborted")) (copy-file old-path new-path (or force-p 1))
(_ t))) (doom--update-files old-path new-path)
(message "File copied to %S" (abbreviate-file-name new-path))))
;;;###autoload ;;;###autoload
(defun doom/move-this-file (new-path &optional force-p) (defun doom/move-this-file (new-path &optional force-p)
"Move current buffer's file to NEW-PATH. If FORCE-P, overwrite the destination "Move current buffer's file to NEW-PATH.
file if it exists, without confirmation."
If FORCE-P, overwrite the destination file if it exists, without confirmation."
(interactive (interactive
(list (read-file-name "Move file to: ") (list (read-file-name "Move file to: ")
current-prefix-arg)) current-prefix-arg))
(pcase (catch 'status (unless (and buffer-file-name (file-exists-p buffer-file-name))
(let ((old-path (buffer-file-name)) (user-error "Buffer is not visiting any file"))
(new-path (expand-file-name new-path))) (let ((old-path (buffer-file-name (buffer-base-buffer)))
(when-let (dest (doom--copy-file old-path new-path force-p)) (new-path (expand-file-name new-path)))
(doom--forget-file old-path) (make-directory (file-name-directory new-path) 't)
(when (file-exists-p old-path) (rename-file old-path new-path (or force-p 1))
(delete-file old-path)) (set-visited-file-name new-path t t)
(mapc #'doom--update-file (doom--update-files old-path new-path)
(delq (message "File moved to %S" (abbreviate-file-name new-path))))
nil (list (if (ignore-errors
(file-equal-p (doom-project-root old-path)
(doom-project-root new-path)))
nil
old-path)
new-path)))
(kill-current-buffer)
(find-file new-path)
(message "File successfully moved to %s" dest))))
(`overwrite-self (error "Cannot overwrite self"))
(`aborted (message "Aborted"))
(_ t)))
(defun doom--sudo-file-path (file) (defun doom--sudo-file-path (file)
(let ((host (or (file-remote-p file 'host) "localhost"))) (let ((host (or (file-remote-p file 'host) "localhost")))

View file

@ -6,8 +6,7 @@
kills the buffer. If FORCE-P, force the deletion (don't ask for confirmation)." kills the buffer. If FORCE-P, force the deletion (don't ask for confirmation)."
:repeat nil :repeat nil
(interactive "<f><!>") (interactive "<f><!>")
(doom/delete-this-file (or filename (file-truename buffer-file-name)) (doom/delete-this-file filename force-p))
force-p))
;;;###autoload (autoload '+evil:move-this-file "editor/evil/autoload/files" nil t) ;;;###autoload (autoload '+evil:move-this-file "editor/evil/autoload/files" nil t)
(evil-define-command +evil:move-this-file (new-path &optional force-p) (evil-define-command +evil:move-this-file (new-path &optional force-p)
@ -30,4 +29,3 @@ overwrite the destination file if it exists, without confirmation."
(when (or (not new-path) (string-empty-p new-path)) (when (or (not new-path) (string-empty-p new-path))
(user-error "No new path was specified")) (user-error "No new path was specified"))
(doom/copy-this-file new-path force-p)) (doom/copy-this-file new-path force-p))