Refactor spacemacs/rename-current-buffer-file

This commit is contained in:
duianto 2021-02-07 11:19:21 +01:00 committed by Maximilian Wolff
parent ddb0d02c51
commit 60ad8194d7
1 changed files with 80 additions and 74 deletions

View File

@ -409,20 +409,11 @@ projectile cache and updates recentf list."
short-name
(file-name-nondirectory new-filename)))))
;; from magnars
(defun spacemacs/rename-current-buffer-file (&optional arg)
"Rename the current buffer and the file it is visiting.
If the buffer isn't visiting a file, ask if it should
be saved to a file, or just renamed.
If called without a prefix argument, the prompt is
initialized with the current directory instead of filename."
(interactive "P")
;; originally from magnars
(defun spacemacs/rename-buffer-visiting-a-file (&optional arg)
(let* ((old-short-name (buffer-name))
(old-filename (buffer-file-name)))
(if (and old-filename (file-exists-p old-filename))
;; the buffer is visiting a file
(let* ((old-dir (file-name-directory old-filename))
(old-filename (buffer-file-name))
(old-dir (file-name-directory old-filename))
(new-name (read-file-name "New name: " (if arg old-dir old-filename)))
(new-dir (file-name-directory new-name))
(new-short-name (file-name-nondirectory new-name))
@ -461,9 +452,11 @@ initialized with the current directory instead of filename."
(file-renamed-p
(concat "File Renamed\n"
"From: " old-short-name "\n"
"To: " new-short-name)))))))
;; the buffer is not visiting a file
(let ((key))
"To: " new-short-name))))))))
(defun spacemacs/rename-buffer-or-save-new-file ()
(let ((old-short-name (buffer-name))
key)
(while (not (memq key '(?s ?r)))
(setq key (read-key (propertize
(format
@ -490,7 +483,20 @@ initialized with the current directory instead of filename."
"From: " old-short-name "\n"
"To: " new-buffer-name))))
;; ?\a = C-g, ?\e = Esc and C-[
((memq key '(?\a ?\e)) (keyboard-quit))))))))
((memq key '(?\a ?\e)) (keyboard-quit))))))
(defun spacemacs/rename-current-buffer-file (&optional arg)
"Rename the current buffer and the file it is visiting.
If the buffer isn't visiting a file, ask if it should
be saved to a file, or just renamed.
If called without a prefix argument, the prompt is
initialized with the current directory instead of filename."
(interactive "P")
(let ((file (buffer-file-name)))
(if (and file (file-exists-p file))
(spacemacs/rename-buffer-visiting-a-file arg)
(spacemacs/rename-buffer-or-save-new-file))))
(defun spacemacs/show-hide-helm-or-ivy-prompt-msg (msg sec)
"Show a MSG at the helm or ivy prompt for SEC.