From b6ef59d589d3639a005c212a6d6749246f01bfd9 Mon Sep 17 00:00:00 2001 From: Stephen Berman Date: Tue, 15 Apr 2025 19:01:19 +0200 Subject: [PATCH] Optionally add new empty file to Dired subdirectory MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Suggested by Kasper Gałkowski (bug#77668) * etc/NEWS: Announce new Dired user option. * lisp/dired-aux.el (dired-create-empty-file-in-current-directory): New user option. (dired-create-empty-file): Use it. Update and clarify doc string. (cherry picked from commit 0ac12aed09702b2d135312e8dad4e173fcd23dae) --- lisp/dired-aux.el | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el index 8b39a497d81..81e3d3679ed 100644 --- a/lisp/dired-aux.el +++ b/lisp/dired-aux.el @@ -2854,13 +2854,32 @@ If DIRECTORY already exists, signal an error." (dired-add-file new) (dired-move-to-filename)))) +(defcustom dired-create-empty-file-in-current-directory nil + "Whether `dired-create-empty-file' acts on the current directory. +If non-nil, `dired-create-empty-file' creates a new empty file and adds +an entry for it (or its topmost new parent directory if created) under +the current subdirectory in the Dired buffer by default (otherwise, it +adds the new file (and new subdirectories if provided) to whichever +directory the user enters at the prompt). If nil, +`dired-create-empty-file' acts on the default directory by default." + :type 'boolean + :version "31.1") + ;;;###autoload (defun dired-create-empty-file (file) "Create an empty file called FILE. -Add a new entry for the new file in the Dired buffer. Parent directories of FILE are created as needed. +Add an entry in the Dired buffer for the topmost new parent +directory of FILE, if created, otherwise for the new file. +If user option `dired-create-empty-file-in-current-directory' is +non-nil, act on the current subdirectory by default, otherwise act on +the default directory by default. If FILE already exists, signal an error." - (interactive (list (read-file-name "Create empty file: ")) dired-mode) + (interactive + (list (read-file-name "Create empty file: " + (and dired-create-empty-file-in-current-directory + (dired-current-directory)))) + dired-mode) (let* ((expanded (expand-file-name file)) new) (if (file-exists-p expanded) -- 2.39.5