Dotfiles/site-modules/core/files/emacs/src/file-operations.el
Ade Attwood ebd73bcc38 feat(emacs): add file operation functions
This adds file operation command to emacs so you can quickly rename and copy
files.

The problem with the normal copy file if that it misses removing the old buffer
if its a rename and dose not move to the new buffer. This leads to (more than I
like to admit) editing the new file thinking its the old file.

By updating the buffers and automatically switching this prevents this.
2021-11-21 10:38:43 +00:00

40 lines
1.5 KiB
EmacsLisp

;;; file-operations.el --- AMACS -*- lexical-binding: t -*-
;;
;; Copyright 2021 Practically.io All rights reserved
;;
;; Use of this source is governed by a BSD-style
;; licence that can be found in the LICENCE file or at
;; https://www.practically.io/copyright/
(defun rename-current-file (new-name)
"Renames both current buffer and file it's visiting to NEW-NAME."
(interactive "FNew 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
(make-directory (file-name-directory new-name) t)
(rename-file filename new-name 1)
(rename-buffer new-name)
(set-visited-file-name new-name)
(set-buffer-modified-p nil))))))
(defun copy-current-file (new-name)
"Copies both current buffer and file it's visiting to NEW-NAME."
(interactive "FNew 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
(make-directory (file-name-directory new-name) t)
(copy-file filename new-name 1)
(find-file new-name)
(set-visited-file-name new-name)
(set-buffer-modified-p nil))))))