diff --git a/layers/+distributions/spacemacs-base/funcs.el b/layers/+distributions/spacemacs-base/funcs.el index f692067f6..02dfb31dd 100644 --- a/layers/+distributions/spacemacs-base/funcs.el +++ b/layers/+distributions/spacemacs-base/funcs.el @@ -293,31 +293,62 @@ projectile cache when it's possible and update recentf list." ;; from magnars (defun spacemacs/rename-current-buffer-file () - "Renames current buffer and file it is visiting." + "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." (interactive) (let* ((name (buffer-name)) - (filename (buffer-file-name))) - (if (not (and filename (file-exists-p filename))) - (error "Buffer '%s' is not visiting a file!" name) - (let* ((dir (file-name-directory filename)) - (new-name (read-file-name "New name: " dir))) - (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) - (rename-buffer new-name) - (set-visited-file-name new-name) - (set-buffer-modified-p nil) - (when (fboundp 'recentf-add-file) + (filename (buffer-file-name))) + (if (and filename (file-exists-p filename)) + ;; the buffer is visiting a file + (let* ((dir (file-name-directory filename)) + (new-name (read-file-name "New name: " dir))) + (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) + (rename-buffer new-name) + (set-visited-file-name new-name) + (set-buffer-modified-p nil) + (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)))))))) + (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))))) + ;; the buffer is not visiting a file + (let ((key)) + (while (not (memq key '(?s ?r))) + (setq key (read-key (propertize + (format + (concat "Buffer '%s' is not visiting a file: " + "[s]ave to file or [r]ename buffer?") + name) 'face 'minibuffer-prompt))) + (cond ((eq key ?s) ; save to file + ;; this allows for saving a new empty (unmodified) buffer + (unless (buffer-modified-p) (set-buffer-modified-p t)) + (save-buffer)) + ((eq key ?r) ; rename buffer + (let ((new-name (read-string "New buffer name: "))) + (while (get-buffer new-name) + ;; ask to rename again, if the new buffer name exists + (if (yes-or-no-p + (format (concat "A buffer named '%s' already exists: " + "Rename again?") new-name)) + (setq new-name (read-string "New buffer name: ")) + (keyboard-quit))) + (rename-buffer new-name) + (message "Buffer '%s' successfully renamed to '%s'" + name new-name))) + ;; ?\a = C-g, ?\e = Esc and C-[ + ((memq key '(?\a ?\e)) (keyboard-quit)))))))) (defun spacemacs/delete-file (filename &optional ask-user) "Remove specified file or directory.