Files
sydnix/users/crumb/programs/emacs/lib/syd-file.el
2025-01-19 18:41:38 -07:00

98 lines
3.2 KiB
EmacsLisp
Executable File

;;; 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)
(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
:desc "Move current file. See `doom/move-this-file'."
:interactive t)
(syd-define-stub
syd/find-file-under-emacs-user-directory
:desc "Find under `emacs-user-directory'. See `doom/find-file-in-private-config'."
:interactive t)
(syd-define-stub
syd/find-file-under-here
:desc "Find under CWD. See `+default/find-file-under-here'."
:interactive t)
(syd-define-stub
syd/yank-buffer-path
:desc "Yank buffer path."
: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
:desc "Open current file as root. See `doom/sudo-this-file'."
:interactive t)
(syd-define-stub
syd/find-file-as-root
:desc "Open current file as root. See `doom/sudo-this-file'."
:interactive t)
(provide 'syd-file)