Skip to content

Instantly share code, notes, and snippets.

@treyharris
Created July 14, 2019 20:58
Show Gist options
  • Save treyharris/d37cfe98ca1ed395e572bc8a0b0c9dc1 to your computer and use it in GitHub Desktop.
Save treyharris/d37cfe98ca1ed395e572bc8a0b0c9dc1 to your computer and use it in GitHub Desktop.
;; source: http://steve.yegge.googlepages.com/my-dot-emacs-file
;; TODO/FIX: Fix when another buffer is visiting a file with the same
;; basename. In this case, (buffer-name) becomes something like
;; "relative_dirname/filename", which breaks this function.
(defun rename-file-and-buffer (new-name)
"Renames both current buffer and file it's visiting to NEW-NAME."
(interactive "sNew name: ")
(let ((name (buffer-name))
(filename (buffer-file-name)))
(if (not filename)
(message "Buffer '%s' is not visiting a file!" name)
(if (get-buffer new-name)
(message "A buffer named '%s' already exists!" new-name)
(progn
(rename-file name new-name 1)
(rename-buffer new-name)
(set-visited-file-name new-name)
(set-buffer-modified-p nil))))))
; From Steve Yegge
(defun move-buffer-file (dir)
"Moves both current buffer and file it's visiting to DIR."
(interactive "DNew directory: ")
(let* ((name (buffer-name))
(filename (buffer-file-name))
(dir
(if (string-match dir "\\(?:/\\|\\\\)$")
(substring dir 0 -1) dir))
(newname (concat dir "/" name)))
(if (not filename)
(message "Buffer '%s' is not visiting a file!" name)
(progn
(copy-file filename newname 1)
(delete-file filename)
(set-visited-file-name newname)
(set-buffer-modified-p nil)
t))))
(defun move-buffer-file--make-directory (dir)
"Create parent directory if it does not exist."
(unless (file-exists-p dir)
(let ((wanted-dir (file-name-directory dir)))
(unless (file-exists-p wanted-dir)
(make-directory dir t)))
)
)
(advice-add 'move-buffer-file :before #'move-buffer-file--make-directory)
(defun has-ws-p (str)
"Return t if STR contains whitespace."
(if (string-match-p (rx (one-or-more blank)) str)
t
nil
)
)
(defun emacs-d (filename)
"Expand FILENAME relative to `user-emacs-directory'."
(expand-file-name filename user-emacs-directory))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment