diff --git a/configuration.org b/configuration.org index ceb9276..a2328a7 100644 --- a/configuration.org +++ b/configuration.org @@ -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 diff --git a/init.el b/init.el index db6e6e8..2a34207 100644 --- a/init.el +++ b/init.el @@ -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. It’s easier to document this way. (org-babel-load-file (expand-file-name "configuration.org" user-emacs-directory)) diff --git a/lisp/gpolonkai/file-utils.el b/lisp/gpolonkai/file-utils.el new file mode 100644 index 0000000..1892611 --- /dev/null +++ b/lisp/gpolonkai/file-utils.el @@ -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