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.
This commit is contained in:
Ade Attwood 2021-11-21 10:38:43 +00:00
parent 956a8e1986
commit ebd73bcc38
2 changed files with 41 additions and 0 deletions

View file

@ -30,6 +30,7 @@
;; Genral and main configuration
(load-file (expand-file-name "src/general.el" user-emacs-directory))
(load-file (expand-file-name "src/file-operations.el" user-emacs-directory))
(load-file (expand-file-name "src/evil.el" user-emacs-directory))
(load-file (expand-file-name "src/ivy.el" user-emacs-directory))
(load-file (expand-file-name "src/projectile.el" user-emacs-directory))

View file

@ -0,0 +1,40 @@
;;; 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))))))