2017-02-09 05:09:21 -05:00
|
|
|
;;; feature/evil/autoload/files.el
|
|
|
|
|
|
|
|
;;;###autoload (autoload '+evil:file-delete "feature/evil/autoload/files" nil t)
|
|
|
|
(evil-define-command +evil:file-delete (&optional bang filename)
|
|
|
|
"Delete current buffer's file. If BANG, don't ask for confirmation."
|
|
|
|
:repeat nil
|
2017-02-19 18:14:46 -05:00
|
|
|
;; TODO Test me
|
2017-02-09 05:09:21 -05:00
|
|
|
(interactive "<!><f>")
|
2017-02-19 18:14:46 -05:00
|
|
|
(let* ((fname (file-truename (or fname (buffer-file-name))))
|
|
|
|
(fbase (file-name-sans-extension (file-name-nondirectory fname)))
|
|
|
|
(buf (current-buffer)))
|
|
|
|
(cond ((not (file-exists-p fname))
|
|
|
|
(error "File doesn't exist: %s" fname))
|
2017-02-09 05:09:21 -05:00
|
|
|
|
2017-02-19 18:14:46 -05:00
|
|
|
((not (or bang (y-or-n-p (format "Delete %s?" fbase))))
|
2017-02-09 05:09:21 -05:00
|
|
|
(message "Aborted"))
|
|
|
|
|
|
|
|
(t
|
|
|
|
(unwind-protect
|
2017-02-19 18:14:46 -05:00
|
|
|
(delete-file fname)
|
|
|
|
(if (file-exists-p fname)
|
|
|
|
(error "Failed to delete %s" (file-relative-name fname))
|
2017-02-09 05:09:21 -05:00
|
|
|
(doom/previous-real-buffer)
|
|
|
|
(kill-buffer buf)
|
|
|
|
(when (bound-and-true-p save-place-mode)
|
|
|
|
(save-place-forget-unreadable-files))
|
2017-02-19 18:14:46 -05:00
|
|
|
(message "Successfully deleted %s" (file-relative-name fname))))))))
|
2017-02-09 05:09:21 -05:00
|
|
|
|
|
|
|
;;;###autoload (autoload '+evil:file-move "feature/evil/autoload/files" nil t)
|
2017-02-19 18:14:46 -05:00
|
|
|
(evil-define-command +evil:file-move (bang dest-path)
|
2017-02-09 05:09:21 -05:00
|
|
|
"Move current buffer's file to PATH. Replaces %, # and other variables (see
|
|
|
|
`evil-ex-replace-special-filenames')"
|
|
|
|
:repeat nil
|
2017-02-19 18:14:46 -05:00
|
|
|
;; TODO Test me
|
|
|
|
(interactive "<!><f>")
|
|
|
|
(let* ((dest-path (expand-file-name dest-path))
|
|
|
|
(old-path (file-truename (buffer-file-name)))
|
|
|
|
(new-path (cond ((file-directory-p dest-path)
|
|
|
|
(expand-file-name (file-name-nondirectory old-path) dest-path))
|
|
|
|
((file-directory-p (file-name-directory dest-path))
|
|
|
|
(expand-file-name dest-path))
|
|
|
|
(t (user-error "Not a valid destination: %s" dest-path))))
|
|
|
|
(project-root (doom-project-root))
|
|
|
|
(short-old-path (file-relative-name old-path project-root))
|
|
|
|
(short-new-path (file-relative-name new-path project-root)))
|
2017-02-20 20:44:46 -05:00
|
|
|
(when (or bang
|
|
|
|
(y-or-n-p (format (if (file-exists-p new-path)
|
|
|
|
"File already exists at %s, overwrite?"
|
|
|
|
"Renaming %s to %s, proceed?")
|
|
|
|
short-old-path short-new-path)))
|
2017-02-19 18:14:46 -05:00
|
|
|
(when (buffer-modified-p)
|
|
|
|
(save-buffer))
|
|
|
|
(rename-file old-path new-path 1)
|
|
|
|
(rename-buffer (file-name-nondirectory new-path))
|
|
|
|
(set-visited-file-name new-path)
|
|
|
|
(set-buffer-modified-p nil)
|
|
|
|
(save-place-forget-unreadable-files)
|
|
|
|
(setq doom--spaceline-file-path nil)
|
|
|
|
(message "File '%s' successfully renamed to '%s'"
|
|
|
|
short-old-path short-new-path))))
|
2017-02-09 05:09:21 -05:00
|
|
|
|