implement rename and delete file functions

This commit is contained in:
d12frosted 2016-06-01 11:04:23 +03:00 committed by syl20bnr
parent 10864587e9
commit 9382e522b4
1 changed files with 52 additions and 0 deletions

View File

@ -275,6 +275,38 @@ argument takes the kindows rotate backwards."
(not (eq (current-buffer) start-buffer)))
(previous-buffer))))
(defun spacemacs/rename-file (filename &optional new-filename)
"Rename FILENAME to NEW-FILENAME.
When NEW-FILENAME is not specified, asks user for a new name.
Also renames associated buffer (if any exists), invalidates
projectile cache when it's possible and update recentf list."
(interactive "f")
(when (and filename (file-exists-p filename))
(let* ((buffer (find-buffer-visiting filename))
(short-name (file-name-nondirectory filename))
(new-name (if new-filename new-filename
(read-file-name
(format "Rename %s to: " short-name)))))
(cond ((get-buffer new-name)
(error "A buffer named '%s' already exists!" new-name))
(t
(let ((dir (file-name-directory new-name)))
(when (and (not (file-exists-p dir)) (yes-or-no-p (format "Create directory '%s'?" dir)))
(make-directory dir t)))
(rename-file filename new-name 1)
(when buffer
(kill-buffer buffer)
(find-file newfile))
(when (fboundp 'recentf-add-file)
(recentf-add-file new-name)
(recentf-remove-if-non-kept filename))
(when (and (configuration-layer/package-usedp 'projectile)
(projectile-project-p))
(call-interactively #'projectile-invalidate-cache))
(message "File '%s' successfully renamed to '%s'" name (file-name-nondirectory new-name)))))))
;; from magnars
(defun spacemacs/rename-current-buffer-file ()
"Renames current buffer and file it is visiting."
@ -299,6 +331,26 @@ argument takes the kindows rotate backwards."
(recentf-remove-if-non-kept filename))
(message "File '%s' successfully renamed to '%s'" name (file-name-nondirectory new-name))))))))
(defun spacemacs/delete-file (filename &optional ask-user)
"Remove specified file or directory.
Also kills associated buffer (if any exists) and invalidates
projectile cache when it's possible.
When ASK-USER is non-nil, user will be asked to confirm file
removal."
(interactive "f")
(when (and filename (file-exists-p filename))
(let ((buffer (find-buffer-visiting filename)))
(when buffer
(kill-buffer buffer)))
(when (or (not ask-user)
(yes-or-no-p "Are you sure you want to delete this file? "))
(delete-file filename)
(when (and (configuration-layer/package-usedp 'projectile)
(projectile-project-p))
(call-interactively #'projectile-invalidate-cache)))))
;; from magnars
(defun spacemacs/delete-current-buffer-file ()
"Removes file connected to current buffer and kills buffer."