Move file manipulation functions from configuration.org to lisp/gpolonkai/file-utils.el

This commit is contained in:
Gergely Polonkai 2025-05-29 21:52:46 +02:00
parent 2cb666aa7b
commit 7502d37992
No known key found for this signature in database
GPG Key ID: 38F402C8471DDE93
3 changed files with 66 additions and 47 deletions

View File

@ -2,53 +2,6 @@
This is a collection of functions and commands i wrote or stole from all around the internet.
** File manipulation
*** Rename the current file
From [[http://whattheemacsd.com/file-defuns.el-01.html][whattheemacsd.org]].
#+begin_src emacs-lisp
(defun wted/rename-current-buffer-file ()
"Renames current buffer and file it is visiting."
(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 ((new-name (read-file-name "New name: " filename)))
(if (get-buffer new-name)
(error "A buffer named '%s' already exists!" new-name)
(rename-file filename new-name 1)
(rename-buffer new-name)
(set-visited-file-name new-name)
; TODO: this is suspicious for me…
(set-buffer-modified-p nil)
(message "File '%s' successfully renamed to '%s'"
name (file-name-nondirectory new-name)))))))
#+end_src
*** Delete the current file
From [[http://whattheemacsd.com/file-defuns.el-02.html][whattheemacsd.org]].
#+begin_src emacs-lisp
(defun wted/delete-current-buffer-file ()
"Remove file connected to current buffer and kill the buffer."
(interactive)
(let ((filename (buffer-file-name))
(name (buffer-name))
(buffer (current-buffer)))
(if (not (and filename (file-exists-p filename)))
(kill-buffer buffer)
(when (yes-or-no-p "Are you sure you want to remove this file? ")
(delete-file filename)
(kill-buffer buffer)
(message "File '%s' successfully removed" filename)))))
#+end_src
** Magit
*** Go to the beginning of the current section

View File

@ -62,6 +62,7 @@
(load "gpolonkai/org-utils")
(load "gpolonkai/text-utils")
(load "gpolonkai/nav-utils")
(load "gpolonkai/file-utils")
;; I started moving my configuration to this Org file. Its easier to document this way.
(org-babel-load-file (expand-file-name "configuration.org" user-emacs-directory))

View File

@ -0,0 +1,65 @@
;;; gpolonkai/file-utils.el --- File manipulation utilities
;;;
;;; SPDX-License-Identifier: GPL-3.0-or-later
;;; Copyright © 2025 Gergely Polonkai
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Lesser General Public
;;; License as published by the Free Software Foundation; either
;;; version 3 of the License, or (at your option) any later version.
;;;
;;; This library is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with this library; if not, write to the
;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;;; Boston, MA 02111-1307, USA.
;;;
;;; Commentary:
;;;
;;; Utility functions stolen from the Internet or written by me
;;;
;;; Code:
;; From whattheemacsd.org: http://whattheemacsd.com/file-defuns.el-01.html
(defun wted/rename-current-buffer-file ()
"Renames current buffer and the file it is visiting."
(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 ((new-name (read-file-name "New name: " filename)))
(if (get-buffer new-name)
(error "A buffer named '%s' already exists!" new-name)
(rename-file filename new-name 1)
(rename-buffer new-name)
(set-visited-file-name new-name)
; TODO: this is suspicious for me…
(set-buffer-modified-p nil)
(message "File '%s' successfully renamed to '%s'"
name (file-name-nondirectory new-name)))))))
;; From whattheemacsd.org: http://whattheemacsd.com/file-defuns.el-02.html
(defun wted/delete-current-buffer-file ()
"Remove file the current buffer is visiting and kill the buffer."
(interactive)
(let ((filename (buffer-file-name))
(name (buffer-name))
(buffer (current-buffer)))
(if (not (and filename (file-exists-p filename)))
(kill-buffer buffer)
(when (yes-or-no-p "Are you sure you want to remove this file? ")
(delete-file filename)
(kill-buffer buffer)
(message "File '%s' successfully removed" filename)))))
(provide 'gpolonkai/file-utils)
;;; file-utils.el ends here