[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#24150: 26.0.50; New command: dired-create-empty-file

From: Tino Calancha
Subject: bug#24150: 26.0.50; New command: dired-create-empty-file
Date: Wed, 01 Aug 2018 18:31:36 +0900
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Eli Zaretskii <address@hidden> writes:

>> How about following two (first with long forst line, second split it in 
>> 2):
>> "Create an empty file called FILE, add a new entry for it in the Dired 
>> buffer.
>> Parent directories of FILE are created as needed.
>> If FILE already exists, signal an error."
>> "Create an empty file called FILE.
>> Add a new entry for it in the Dired buffer.
>> Parent directories of FILE are created as needed.
>> If FILE already exists, signal an error."
> The second one is better, IMO.  But instead of "it", I'd say "the new
> file" or "the created file".
OK, thank you.
I update the patch below.  As sooner I get the green light I will push
it to master.
--8<-----------------------------cut here---------------start------------->8---
commit 27a5a905dbd2e32a3b666febf970c45cc9c746a8
Author: Tino Calancha <address@hidden>
Date:   Wed Aug 1 18:28:45 2018 +0900

    New commands to create an empty file
    Similarly as `create-directory', `dired-create-directory',
    the new commands create the parent dirs as needed (Bug#24150).
    * lisp/files.el (make-empty-file): New command.
    * lisp/dired-aux.el (dired-create-empty-file): New command.
    (dired--find-topmost-parent-dir): New function extracted
    from `dired-create-directory'.
    (dired-create-directory, dired-create-empty-file): Use it.
    * lisp/dired.el (dired-mode-map):
    Add menu entry for `dired-create-empty-file'.
    * doc/emacs/dired.texi (Misc Dired Features)
    * doc/lispref/files.texi (Create/Delete Dirs): Update manual.
    ; * etc/NEWS: Announce the changes.

diff --git a/doc/emacs/dired.texi b/doc/emacs/dired.texi
index 007a943714..1b03a3967a 100644
--- a/doc/emacs/dired.texi
+++ b/doc/emacs/dired.texi
@@ -1468,6 +1468,11 @@ Misc Dired Features
 directory's name, and creates that directory.  It signals an error if
 the directory already exists.
address@hidden dired-create-empty-file
+  The command (@code{dired-create-empty-file}) reads a
+file name, and creates that file.  It signals an error if
+the file already exists.
 @cindex searching multiple files via Dired
 @kindex M-s a C-s @r{(Dired)}
 @kindex M-s a M-C-s @r{(Dired)}
diff --git a/doc/lispref/files.texi b/doc/lispref/files.texi
index 068cf05443..536f0292f9 100644
--- a/doc/lispref/files.texi
+++ b/doc/lispref/files.texi
@@ -3006,6 +3006,12 @@ Create/Delete Dirs
 interactive call, that means to create the parent directories first,
 if they don't already exist.
address@hidden Command make-empty-file filename &optional parents
+This command creates an empty file named @var{filename}.
+As @code{make-directory}, this command creates parent directories
+if @var{parents} is address@hidden
+If @var{filename} already exists, this command signals an error.
 @code{mkdir} is an alias for this.
 @end deffn
diff --git a/etc/NEWS b/etc/NEWS
index f1ea835679..e129bff7a4 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -185,6 +185,9 @@ This triggers to search the program on the remote host as 
indicated by
 * Editing Changes in Emacs 27.1
+** New command 'make-empty-file'.
 ** New variable 'x-wait-for-event-timeout'.
 This controls how long Emacs will wait for updates to the graphical
@@ -222,6 +225,11 @@ navigation and editing of large files.
 * Changes in Specialized Modes and Packages in Emacs 27.1
+** Dired
+*** New command 'dired-create-empty-file'.
 ** Change Logs and VC
 *** Recording ChangeLog entries doesn't require an actual file.
diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el
index 925a7d50d6..21ee50ce5c 100644
--- a/lisp/dired-aux.el
+++ b/lisp/dired-aux.el
@@ -1989,6 +1989,19 @@ dired-dwim-target-defaults
+;; We use this function in `dired-create-directory' and
+;; `dired-create-empty-file'; the return value is the new entry
+;; in the updated Dired buffer.
+(defun dired--find-topmost-parent-dir (filename)
+  "Return the topmost nonexistent parent dir of FILENAME.
+FILENAME is a full file name."
+  (let ((try filename) new)
+    (while (and try (not (file-exists-p try)) (not (equal new try)))
+      (setq new try
+           try (directory-file-name (file-name-directory try))))
+    new))
 (defun dired-create-directory (directory)
   "Create a directory called DIRECTORY.
@@ -1997,18 +2010,32 @@ dired-create-directory
    (list (read-file-name "Create directory: " (dired-current-directory))))
   (let* ((expanded (directory-file-name (expand-file-name directory)))
-        (try expanded) new)
+        new)
     (if (file-exists-p expanded)
        (error "Cannot create directory %s: file exists" expanded))
-    ;; Find the topmost nonexistent parent dir (variable `new')
-    (while (and try (not (file-exists-p try)) (not (equal new try)))
-      (setq new try
-           try (directory-file-name (file-name-directory try))))
+    (setq new (dired--find-topmost-parent-dir expanded))
     (make-directory expanded t)
     (when new
       (dired-add-file new)
+(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.
+ If FILE already exists, signal an error."
+  (interactive (list (read-file-name "Create empty file: ")))
+  (let* ((expanded (expand-file-name file))
+         new)
+    (if (file-exists-p expanded)
+        (error "Cannot create file %s: file exists" expanded))
+    (setq new (dired--find-topmost-parent-dir expanded))
+    (make-empty-file file 'parents)
+    (when new
+      (dired-add-file new)
+      (dired-move-to-filename))))
 (defun dired-into-dir-with-symlinks (target)
   (and (file-directory-p target)
        (not (file-symlink-p target))))
diff --git a/lisp/dired.el b/lisp/dired.el
index 1348df6934..26a7449e03 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -1802,6 +1802,9 @@ dired-mode-map
     (define-key map [menu-bar immediate create-directory]
       '(menu-item "Create Directory..." dired-create-directory
                  :help "Create a directory"))
+    (define-key map [menu-bar immediate create-empty-file]
+      '(menu-item "Create Empty file..." dired-create-empty-file
+                 :help "Create an empty file"))
     (define-key map [menu-bar immediate wdired-mode]
       '(menu-item "Edit File Names" wdired-change-to-wdired-mode
                  :help "Put a Dired buffer in a mode in which filenames are 
diff --git a/lisp/files.el b/lisp/files.el
index 6e4f6ca51b..8057def525 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -5519,6 +5519,21 @@ make-directory
          (dolist (dir create-list)
             (files--ensure-directory dir)))))))
+(defun make-empty-file (filename &optional parents)
+  "Create an empty file FILENAME.
+Optional arg PARENTS, if non-nil then creates parent dirs as needed.
+If called interactively, then PARENTS is non-nil."
+  (interactive
+   (let ((filename (read-file-name "Create empty file: ")))
+     (list filename t)))
+  (when (and (file-exists-p filename) (null parents))
+    (signal 'file-already-exists `("File exists" ,filename)))
+  (let ((paren-dir (file-name-directory filename)))
+    (when (and paren-dir (not (file-exists-p paren-dir)))
+      (make-directory paren-dir parents)))
+  (write-region "" nil filename nil 0))
 (defconst directory-files-no-dot-files-regexp
   "Regexp matching any file name except \".\" and \"..\".")

--8<-----------------------------cut here---------------end--------------->8---
In GNU Emacs 27.0.50 (build 9, x86_64-pc-linux-gnu, GTK+ Version 3.22.11)
 of 2018-08-01
Repository revision: e28a37438d4ba71cd8a053e956686ab29ff97b6a
Windowing system distributor 'The X.Org Foundation', version 11.0.11902000
System Description: Debian GNU/Linux 9 (stretch)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]