wip: feat: Delete file

This commit is contained in:
Madeleine Sydney
2025-01-15 01:45:14 -07:00
parent 65dd7b3967
commit 76828c173a
2 changed files with 171 additions and 8 deletions

View File

@@ -1,16 +1,60 @@
;;; syd-file.el -*- lexical-binding: t; -*-
(require 'syd-prelude)
(require 'syd-buffers)
(syd-define-stub
syd/copy-this-file
:desc "Copy current file. See `doom/copy-this-file'."
:interactive t)
(syd-define-stub
syd/delete-this-file
:desc "Delete current file. See `doom/delete-this-file'."
:interactive t)
(defun syd-files--update-refs (&rest files)
"Ensure FILES are updated in `recentf', `magit' and `save-place'."
(let (toplevels)
(dolist (file files)
(when (featurep 'vc)
(vc-file-clearprops file)
(when-let (buffer (get-file-buffer file))
(with-current-buffer buffer
(vc-refresh-state))))
(when (featurep 'magit)
(when-let (default-directory
(magit-toplevel (file-name-directory file)))
(cl-pushnew default-directory toplevels)))
(unless (file-readable-p file)
(when (bound-and-true-p recentf-mode)
(recentf-remove-if-non-kept file))
(dolist (default-directory toplevels)
(magit-refresh))
(when (bound-and-true-p save-place-mode)
(save-place-forget-unreadable-files))))))
(defun syd/delete-this-file (&optional path force-p)
"Delete PATH, kill its buffers and expunge it from vc/magit cache.
If PATH is not specified, default to the current buffer's file.
If FORCE-P, delete without confirmation."
(interactive (list (buffer-file-name (buffer-base-buffer))
current-prefix-arg))
(let* ((path (or path (buffer-file-name (buffer-base-buffer))))
(short-path (and path (abbreviate-file-name path))))
(unless path
(user-error "Buffer is not visiting any file"))
(unless (file-exists-p path)
(error "File doesn't exist: %s" path))
(unless (or force-p (y-or-n-p (format "Really delete %S?" short-path)))
(user-error "Aborted"))
(let ((buf (current-buffer)))
(unwind-protect
(progn (delete-file path t) t))
(if (file-exists-p path)
(error "Failed to delete %S" short-path)
;; Ensures that windows displaying this buffer will be switched to
;; real buffers (`doom-real-buffer-p')
(syd/kill-this-buffer-in-all-windows buf t)
(syd-files--update-refs path)
(message "Deleted %S" short-path)))))
(syd-define-stub
syd/move-this-file
@@ -32,10 +76,13 @@
:desc "Yank buffer path."
:interactive t)
(syd-define-stub
syd/find-file-in-emacs-user-directory
:desc "Find file in `emacs-user-directory'. See `doom/open-private-config'."
:interactive t)
(defun syd/find-file-in-emacs-user-directory ()
(interactive)
(unless (file-directory-p user-emacs-directory)
(user-error "`emacs-user-directory' doesn't exist! (%s)"
(abbreviate-file-name emacs-user-directory)))
(let ((default-directory user-emacs-directory))
(call-interactively #'find-file)))
(syd-define-stub
syd/open-this-file-as-root