Descriptive rename function file variables

problem:
the variable names: `dir`, `name`, and `filename` leads to confusion if they are
the old or new names

solution:
add the prefix `old-` to match `new-`,
use (`old-` `new-`) + `short-name` for just the `file.ext` (the `short-` idea
came from the `spacemacs/rename-file` function that's defined above this one),
and `old-full-path` instead of `old-filename` makes it clear that it's not just
the files name and extension.
This commit is contained in:
duianto 2018-10-17 21:42:10 +02:00 committed by smile13241324
parent 4c728f041a
commit 38752beffa
1 changed files with 94 additions and 94 deletions

View File

@ -209,18 +209,18 @@ WINDOW1 and WINDOW2 must be valid windows. They may contain child windows."
"Toggle between horizontal and vertical layout of two windows."
(interactive)
(if (= (count-windows) 2)
(let* ((window-tree (car (window-tree)))
(current-split-vertical-p (car window-tree))
(first-window (nth 2 window-tree))
(second-window (nth 3 window-tree))
(second-window-state (window-state-get second-window))
(splitter (if current-split-vertical-p
#'split-window-horizontally
#'split-window-vertically)))
(delete-other-windows first-window)
;; `window-state-put' also re-selects the window if needed, so we don't
;; need to call `select-window'
(window-state-put second-window-state (funcall splitter)))
(let* ((window-tree (car (window-tree)))
(current-split-vertical-p (car window-tree))
(first-window (nth 2 window-tree))
(second-window (nth 3 window-tree))
(second-window-state (window-state-get second-window))
(splitter (if current-split-vertical-p
#'split-window-horizontally
#'split-window-vertically)))
(delete-other-windows first-window)
;; `window-state-put' also re-selects the window if needed, so we don't
;; need to call `select-window'
(window-state-put second-window-state (funcall splitter)))
(error "Can't toggle window layout when the number of windows isn't two.")))
;; originally from magnars and modified by ffevotte for dedicated windows
@ -279,11 +279,11 @@ stays on current"
(dotimes (i 9)
(let ((n (+ i 1)))
(eval `(defun ,(intern (format "buffer-to-window-%s" n)) (&optional arg)
,(format "Move buffer to the window with number %i." n)
(interactive "P")
(if arg
(spacemacs/swap-buffers-to-window ,n t)
(spacemacs/move-buffer-to-window ,n t))))
,(format "Move buffer to the window with number %i." n)
(interactive "P")
(if arg
(spacemacs/swap-buffers-to-window ,n t)
(spacemacs/move-buffer-to-window ,n t))))
(eval `(defun ,(intern (format "move-buffer-window-no-follow-%s" n)) ()
(interactive)
(spacemacs/move-buffer-to-window ,n nil)))
@ -348,49 +348,49 @@ 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* ((name (buffer-name))
(filename (buffer-file-name)))
(if (and filename (file-exists-p filename))
(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* ((dir (file-name-directory filename))
(new-name (read-file-name "New name: " (if arg dir filename)))
(let* ((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))
(file-moved-p (not (string-equal new-dir dir)))
(file-renamed-p (not (string-equal new-short-name name))))
(file-moved-p (not (string-equal new-dir old-dir)))
(file-renamed-p (not (string-equal new-short-name old-short-name))))
(cond ((get-buffer new-name)
(error "A buffer named '%s' already exists!" new-name))
((string-equal new-name filename)
((string-equal new-name old-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))
(let ((old-directory (file-name-directory new-name)))
(when (and (not (file-exists-p old-directory))
(yes-or-no-p
(format "Create directory '%s'?" dir)))
(make-directory dir t)))
(rename-file filename new-name 1)
(format "Create directory '%s'?" old-directory)))
(make-directory old-directory t)))
(rename-file old-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))
(recentf-remove-if-non-kept old-filename))
(when (and (configuration-layer/package-used-p 'projectile)
(projectile-project-p))
(call-interactively #'projectile-invalidate-cache))
(message (cond ((and file-moved-p file-renamed-p)
(concat "File Moved & Renamed\n"
"From: " filename "\n"
"From: " old-filename "\n"
"To: " new-name))
(file-moved-p
(concat "File Moved\n"
"From: " filename "\n"
"From: " old-filename "\n"
"To: " new-name))
(file-renamed-p
(concat "File Renamed\n"
"From: " name "\n"
"From: " old-short-name "\n"
"To: " new-short-name)))))))
;; the buffer is not visiting a file
(let ((key))
@ -399,26 +399,26 @@ initialized with the current directory instead of filename."
(format
(concat "Buffer '%s' is not visiting a file: "
"[s]ave to file or [r]ename buffer?")
name)
old-short-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)
(let ((new-buffer-name (read-string "New buffer name: ")))
(while (get-buffer new-buffer-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: "))
new-buffer-name))
(setq new-buffer-name (read-string "New buffer name: "))
(keyboard-quit)))
(rename-buffer new-name)
(rename-buffer new-buffer-name)
(message (concat "Buffer Renamed\n"
"From: " name "\n"
"To: " new-name))))
"From: " old-short-name "\n"
"To: " new-buffer-name))))
;; ?\a = C-g, ?\e = Esc and C-[
((memq key '(?\a ?\e)) (keyboard-quit))))))))
@ -489,7 +489,7 @@ FILENAME is deleted using `spacemacs/delete-file' function.."
(if (not (and filename (file-exists-p filename)))
(ido-kill-buffer)
(if (yes-or-no-p
(format "Are you sure you want to delete this file: '%s'?" name))
(format "Are you sure you want to delete this file: '%s'?" name))
(progn
(delete-file filename t)
(kill-buffer buffer)
@ -576,13 +576,13 @@ If the universal prefix argument is used then will the windows too."
"Toggle dedication state of a window. Commands that change the buffer that a
window is displaying will not typically change the buffer displayed by
a dedicated window."
(interactive)
(let* ((window (selected-window))
(dedicated (window-dedicated-p window)))
(set-window-dedicated-p window (not dedicated))
(message "Window %sdedicated to %s"
(if dedicated "no longer " "")
(buffer-name))))
(interactive)
(let* ((window (selected-window))
(dedicated (window-dedicated-p window)))
(set-window-dedicated-p window (not dedicated))
(message "Window %sdedicated to %s"
(if dedicated "no longer " "")
(buffer-name))))
(defun spacemacs//init-visual-line-keys ()
(evil-define-minor-mode-key 'motion 'visual-line-mode "j" 'evil-next-visual-line)
@ -633,15 +633,15 @@ Returns:
- `nil' in case the current buffer does not have a directory."
(when-let (file-path (spacemacs--file-path-with-line))
(concat
file-path
":"
(number-to-string (if (and
;; Emacs 26 introduced this variable. Remove this
;; check once 26 becomes the minimum version.
(boundp column-number-indicator-zero-based)
(not column-number-indicator-zero-based))
(1+ (current-column))
(current-column))))))
file-path
":"
(number-to-string (if (and
;; Emacs 26 introduced this variable. Remove this
;; check once 26 becomes the minimum version.
(boundp column-number-indicator-zero-based)
(not column-number-indicator-zero-based))
(1+ (current-column))
(current-column))))))
(defun spacemacs/copy-directory-path ()
"Copy and show the directory path of the current buffer.
@ -795,31 +795,31 @@ in a new frame."
(defun spacemacs--window-split-splittable-windows ()
(seq-remove
(lambda (window)
;; TODO: find a way to identify unsplittable side windows reliably!
nil)
(spacemacs--window-split-non-ignored-windows)))
(lambda (window)
;; TODO: find a way to identify unsplittable side windows reliably!
nil)
(spacemacs--window-split-non-ignored-windows)))
(defun spacemacs--window-split-non-ignored-windows ()
"Determines the list of windows to be deleted."
(seq-filter
(lambda (window)
(let* ((name (buffer-name (window-buffer window)))
(prefixes-matching
(seq-filter
(lambda (prefix) (string-prefix-p prefix name))
spacemacs-window-split-ignore-prefixes)))
(not prefixes-matching)))
(window-list (selected-frame))))
(lambda (window)
(let* ((name (buffer-name (window-buffer window)))
(prefixes-matching
(seq-filter
(lambda (prefix) (string-prefix-p prefix name))
spacemacs-window-split-ignore-prefixes)))
(not prefixes-matching)))
(window-list (selected-frame))))
(defun spacemacs/window-split-default-delete ()
"Deletes other windows, except a list of excluded ones."
(if spacemacs-window-split-ignore-prefixes
(let* ((deletable (spacemacs--window-split-non-ignored-windows))
(splittable (spacemacs--window-split-splittable-windows)))
(splittable (spacemacs--window-split-splittable-windows)))
(when splittable
(let* ((selected (car splittable))
(to-delete (delq selected deletable)))
(to-delete (delq selected deletable)))
(select-window selected)
(dolist (window to-delete) (delete-window window)))))
(delete-other-windows)))
@ -865,10 +865,10 @@ as a means to remove windows, regardless of the value in
(funcall spacemacs-window-split-delete-function))
(if (spacemacs--window-split-splittable-windows)
(let* ((previous-files (seq-filter #'buffer-file-name
(delq (current-buffer) (buffer-list))))
(second (split-window-below))
(third (split-window-right))
(fourth (split-window second nil 'right)))
(delq (current-buffer) (buffer-list))))
(second (split-window-below))
(third (split-window-right))
(fourth (split-window second nil 'right)))
(set-window-buffer third (or (car previous-files) "*scratch*"))
(set-window-buffer second (or (cadr previous-files) "*scratch*"))
(set-window-buffer fourth (or (caddr previous-files) "*scratch*"))
@ -891,9 +891,9 @@ as a means to remove windows, regardless of the value in
(funcall spacemacs-window-split-delete-function))
(if (spacemacs--window-split-splittable-windows)
(let* ((previous-files (seq-filter #'buffer-file-name
(delq (current-buffer) (buffer-list))))
(second (split-window-right))
(third (split-window second nil 'right)))
(delq (current-buffer) (buffer-list))))
(second (split-window-right))
(third (split-window second nil 'right)))
(set-window-buffer second (or (car previous-files) "*scratch*"))
(set-window-buffer third (or (cadr previous-files) "*scratch*"))
(balance-windows))
@ -944,7 +944,7 @@ as a means to remove windows, regardless of the value in
(interactive "p")
(let ((p (+ (point) count)))
(save-excursion
(if (eq (line-number-at-pos) 1)
(if (eq (line-number-at-pos) 1)
(evil-move-beginning-of-line)
(progn
(evil-previous-line)
@ -1074,12 +1074,12 @@ useful to use full screen on macOS without animations."
nil
`((maximized
. ,(unless (memq (frame-parameter nil 'fullscreen) '(fullscreen fullboth))
(frame-parameter nil 'fullscreen)))
(frame-parameter nil 'fullscreen)))
(fullscreen
. ,(if (memq (frame-parameter nil 'fullscreen) '(fullscreen fullboth))
(if (eq (frame-parameter nil 'maximized) 'maximized)
'maximized)
'fullboth)))))
(if (eq (frame-parameter nil 'maximized) 'maximized)
'maximized)
'fullboth)))))
(defun spacemacs/safe-revert-buffer ()
"Prompt before reverting the file."
@ -1303,7 +1303,7 @@ A non-nil argument sorts in REVERSE order."
"Sorting by column requires a block/rect selection on 2 or more lines.")))
(defun spacemacs/sort-lines-by-column-reverse ()
"Sort lines by the selected column in reverse order,
"Sort lines by the selected column in reverse order,
using a visual block/rectangle selection."
(interactive)
(spacemacs/sort-lines-by-column -1))
@ -1549,14 +1549,14 @@ Decision is based on `dotspacemacs-line-numbers'."
(unless (boundp 'text-scale-mode-step)
(setq window-initial-margins (window-margins win)))
(set-window-margins win
(ceiling (* (if (boundp 'text-scale-mode-step)
(expt text-scale-mode-step
text-scale-mode-amount)
1)
(or (car (if (boundp 'window-initial-margins)
window-initial-margins
(window-margins win)))
1))))))
(ceiling (* (if (boundp 'text-scale-mode-step)
(expt text-scale-mode-step
text-scale-mode-amount)
1)
(or (car (if (boundp 'window-initial-margins)
window-initial-margins
(window-margins win)))
1))))))
(defun spacemacs//linum-backward-compabitility ()
"Return non-nil if `dotspacemacs-line-numbers' has an old format and if