feature/evil: rewrite :mv & :rm (file move/delete commands)

This commit is contained in:
Henrik Lissner 2017-05-12 12:06:56 +02:00
parent 877ae26a96
commit 388e5b4711
3 changed files with 76 additions and 45 deletions

View file

@ -172,6 +172,19 @@ See `doom-real-buffer-p' for what 'real' means."
(kill-buffer buffer))) (kill-buffer buffer)))
(eq (current-buffer) buffer))) (eq (current-buffer) buffer)))
;;;###autoload
(defun doom-force-kill-buffer (&optional buffer dont-save)
"Kill BUFFER globally and ensure all windows previously showing BUFFER have
switched to a real buffer."
(interactive)
(let* ((buffer (or buffer (current-buffer)))
(windows (get-buffer-window-list buffer nil t)))
(kill-buffer buffer)
(dolist (win windows)
(with-selected-window win
(unless (doom-real-buffer-p)
(doom/previous-buffer))))))
;;;###autoload ;;;###autoload
(defun doom-kill-buffer-and-windows (buffer) (defun doom-kill-buffer-and-windows (buffer)
"Kill the buffer and delete all the windows it's displayed in." "Kill the buffer and delete all the windows it's displayed in."

View file

@ -1,62 +1,80 @@
;;; feature/evil/autoload/files.el ;;; feature/evil/autoload/files.el
;;;###autoload (autoload '+evil:file-delete "feature/evil/autoload/files" nil t) (defun doom--forget-file (old-path &optional new-path)
(evil-define-command +evil:file-delete (&optional bang filename) "Ensure `recentf', `projectile' and `save-place' forget OLD-PATH."
"Delete current buffer's file. If BANG, don't ask for confirmation." (when (fboundp 'recentf-add-file)
(when new-path
(recentf-add-file new-path))
(recentf-remove-if-non-kept old-path))
(when (and (projectile-project-p)
(projectile-file-cached-p old-path (projectile-project-root)))
(projectile-purge-file-from-cache old-path))
(when (bound-and-true-p save-place-mode)
(save-place-forget-unreadable-files)))
;;;###autoload (autoload '+evil:delete-this-file "feature/evil/autoload/files" nil t)
(evil-define-command +evil:delete-this-file (&optional filename force-p)
"Delete FILENAME (defaults to the file associated with current buffer) and
kills the buffer. If FORCE-P, force the deletion (don't ask for confirmation)."
:repeat nil :repeat nil
;; TODO Test me (interactive "<f><!>")
(interactive "<!><f>")
(let* ((fname (file-truename (or filename (buffer-file-name)))) (let* ((fname (file-truename (or filename (buffer-file-name))))
(fbase (file-name-sans-extension (file-name-nondirectory fname))) (fbase (file-name-sans-extension (file-name-nondirectory fname)))
(buf (current-buffer))) (buf (current-buffer)))
(cond ((not (file-exists-p fname)) (cond ((not (file-exists-p fname))
(error "File doesn't exist: %s" fname)) (error "File doesn't exist: %s" fname))
((not (or bang (y-or-n-p (format "Delete %s?" fbase)))) ((not (or force-p (y-or-n-p (format "Really delete %s?" fbase))))
(message "Aborted")) (message "Aborted"))
(t (t
(unwind-protect (unwind-protect
(delete-file fname) (delete-file fname)
(if (file-exists-p fname) (let ((short-path (file-relative-name fname (doom-project-root))))
(error "Failed to delete %s" (file-relative-name fname)) (if (file-exists-p fname)
(doom/previous-buffer) (error "Failed to delete %s" short-path)
(kill-buffer buf) ;; Ensures that windows displaying this buffer will be switched
(when (bound-and-true-p save-place-mode) ;; to real buffers (`doom-real-buffer-p')
(save-place-forget-unreadable-files)) (doom-force-kill-buffer buf t)
(message "Successfully deleted %s" (file-relative-name fname)))))))) (doom--forget-file fname)
(message "Successfully deleted %s" short-path))))))))
;;;###autoload (autoload '+evil:file-move "feature/evil/autoload/files" nil t) ;;;###autoload (autoload '+evil:move-this-file "feature/evil/autoload/files" nil t)
(evil-define-command +evil:file-move (bang dest-path) (evil-define-command +evil:move-this-file (new-path &optional force-p)
"Move current buffer's file to PATH. Replaces %, # and other variables (see "Move current buffer's file to NEW-PATH. Replaces %, # and other vim-esque
`evil-ex-replace-special-filenames')" filename modifiers (see `+evil*ex-replace-special-filenames'). If FORCE-P,
overwrite the destination file if it exists, without confirmation."
:repeat nil :repeat nil
;; TODO Test me (interactive "<f><!>")
(interactive "<!><f>") (let* ((new-path (expand-file-name new-path))
(let* ((dest-path (expand-file-name dest-path))
(old-path (file-truename (buffer-file-name))) (old-path (file-truename (buffer-file-name)))
(new-path (cond ((file-directory-p dest-path) (new-path (apply #'expand-file-name
(expand-file-name (file-name-nondirectory old-path) dest-path)) (if (or (directory-name-p new-path)
((file-directory-p (file-name-directory dest-path)) (file-directory-p new-path))
(expand-file-name dest-path)) (list (file-name-nondirectory old-path) new-path)
(t (user-error "Not a valid destination: %s" dest-path)))) (list new-path))))
(new-path-dir (file-name-directory new-path))
(project-root (doom-project-root)) (project-root (doom-project-root))
(short-old-path (file-relative-name old-path project-root)) (short-new-name (if (file-in-directory-p new-path project-root)
(short-new-path (file-relative-name new-path project-root))) (file-relative-name new-path project-root)
(when (or bang (abbreviate-file-name new-path))))
(y-or-n-p (format (if (file-exists-p new-path) (unless (file-directory-p new-path-dir)
"File already exists at %s, overwrite?" (make-directory new-path-dir t))
"Renaming %s to %s, proceed?") (when (buffer-modified-p)
short-old-path short-new-path))) (save-buffer))
(when (buffer-modified-p) (cond ((equal (file-truename old-path)
(save-buffer)) (file-truename new-path))
(rename-file old-path new-path 1) (error "Cannot move file to itself"))
(rename-buffer (file-name-nondirectory new-path)) ((and (file-exists-p new-path)
(set-visited-file-name new-path) (not force-p)
(set-buffer-modified-p nil) (not (y-or-n-p (format "File already exists at %s, overwrite?" short-new-name))))
(when (bound-and-true-p save-place-mode) (message "Aborted"))
(save-place-forget-unreadable-files)) (t
(setq doom--spaceline-file-path nil) (rename-file old-path new-path 1)
(message "File '%s' successfully renamed to '%s'" (kill-this-buffer)
short-old-path short-new-path)))) (find-file new-path)
(doom--forget-file old-path new-path)
(message "File successfully moved to %s"
short-new-name)))))

View file

@ -66,8 +66,8 @@
(ex! "todo" '+ivy:todo) (ex! "todo" '+ivy:todo)
;; File operations ;; File operations
(ex! "mv" '+evil:file-move) (ex! "mv" '+evil:move-this-file)
(ex! "rm" '+evil:file-delete) (ex! "rm" '+evil:delete-this-file)
;; Sessions/tabs ;; Sessions/tabs
(ex! "sclear" '+workspace/kill-session) (ex! "sclear" '+workspace/kill-session)