From 4c728f041a9830b0bad52c8763e96b4836135252 Mon Sep 17 00:00:00 2001 From: duianto Date: Thu, 18 Oct 2018 14:50:48 +0200 Subject: [PATCH] Update file renaming, handle same new and old name problem: renaming a file to it's current name, results in the message: "File 'current-name' successfully renamed to 'current-name'" solution: when the new and old names are the same, show a rename failed message, and call the rename function again --- CHANGELOG.develop | 2 ++ layers/+spacemacs/spacemacs-defaults/funcs.el | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/CHANGELOG.develop b/CHANGELOG.develop index 1b630f719..73e7298f1 100644 --- a/CHANGELOG.develop +++ b/CHANGELOG.develop @@ -1090,6 +1090,8 @@ Other: - Fixed =spacemacs/layouts-ts-close-other= with =ivy= (thanks to duianto) - Fixed =spacemacs/rename-current-buffer-file=, separate messages for move & rename, just move and just rename (thanks to duianto) + - Fixed =spacemacs/rename-current-buffer-file= handle same new and old name + (thanks to duianto) *** Layer changes and fixes **** Agda - Fixes diff --git a/layers/+spacemacs/spacemacs-defaults/funcs.el b/layers/+spacemacs/spacemacs-defaults/funcs.el index e6cf8dcd4..96cdda1c3 100644 --- a/layers/+spacemacs/spacemacs-defaults/funcs.el +++ b/layers/+spacemacs/spacemacs-defaults/funcs.el @@ -360,6 +360,10 @@ initialized with the current directory instead of filename." (file-renamed-p (not (string-equal new-short-name name)))) (cond ((get-buffer new-name) (error "A buffer named '%s' already exists!" new-name)) + ((string-equal new-name filename) + (spacemacs/show-hide-helm-or-ivy-prompt-msg + "Rename failed! Same new and old name" 1.5) + (spacemacs/rename-current-buffer-file)) (t (let ((dir (file-name-directory new-name))) (when (and (not (file-exists-p dir)) @@ -418,6 +422,36 @@ initialized with the current directory instead of filename." ;; ?\a = C-g, ?\e = Esc and C-[ ((memq key '(?\a ?\e)) (keyboard-quit)))))))) +(defun spacemacs/show-hide-helm-or-ivy-prompt-msg (msg sec) + "Show a MSG at the helm or ivy prompt for SEC. +With Helm, remember the path, then restore it after SEC. +With Ivy, the path isn't editable, just remove the MSG after SEC." + (run-at-time + 0 nil + #'(lambda (msg sec) + (let* ((prev-prompt-contents + (buffer-substring (line-beginning-position) + (line-end-position))) + (prev-prompt-contents-p + (not (string= prev-prompt-contents ""))) + (helmp (fboundp 'helm-mode))) + (when prev-prompt-contents-p + (delete-region (line-beginning-position) + (line-end-position))) + (insert (propertize msg 'face 'warning)) + ;; stop checking for candidates + ;; and update the helm prompt + (when helmp (helm-suspend-update t)) + (sit-for sec) + (delete-region (line-beginning-position) + (line-end-position)) + (when prev-prompt-contents-p + (insert prev-prompt-contents) + ;; start checking for candidates + ;; and update the helm prompt + (when helmp (helm-suspend-update nil))))) + msg sec)) + (defun spacemacs/delete-file (filename &optional ask-user) "Remove specified file or directory.