bug-gnu-emacs
[Top][All Lists]
Advanced

[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: Mon, 03 Jul 2017 13:51:25 +0900

>> Hi all,
>>
>> It might be useful having a Dired command creating
>> an empty file with a name provided by the user; something
>> like 'dired-create-directory' ('+') but for files.
>It seems people is not interested on this feature, so i am closing the report.

After i found the following threads:

https://stackoverflow.com/questions/20300137/how-to-create-new-file-from-dired-mode
https://stackoverflow.com/questions/2592095/how-do-i-create-an-empty-file-in-emacs

and considering the significant number of views they have, i'd like to
reopen this issue.

1. In my config i've bound the new command to 'M-+'.

2. In the threads above they suggest the keybindings 'c' and '_'.

'c' is already bound to `dired-do-compress-to', so i propose to bind
the new command to '_' (unless you have a better proposal).

If there are no objections, i would like to apply the following patch
in a few days:

--8<-----------------------------cut here---------------start------------->8---
commit 2661c4957b9ef90a2282a697dc5db3fab7e252b8
Author: Tino Calancha <tino.calancha@gmail.com>
Date:   Mon Jul 3 13:36:09 2017 +0900

    New command dired-create-empty-file
    
    Add a dired command, similar to 'dired-create-directory', to create
    an empty file (Bug#24150).
    * lisp/dired-aux.el (dired--create-empty-file-or-directory): New defun.
    (dired-create-directory): Use it.
    (dired-create-empty-file): New command.
    * lisp/dired.el (dired-mode-map): Bind 'dired-create-empty-file' to '_'.
    * doc/emacs/dired.texi: Document the new command in the manual.
    ; * etc/NEWS: Announce it.  Fix a previous Dired entry.

diff --git a/doc/emacs/dired.texi b/doc/emacs/dired.texi
index 28cb51d88b..94a122e7eb 100644
--- a/doc/emacs/dired.texi
+++ b/doc/emacs/dired.texi
@@ -1416,6 +1416,12 @@ Misc Dired Features
 directory name, and creates that directory.  It signals an error if
 the directory already exists.
 
+@kindex _ @r{(Dired)}
+@findex dired-create-empty-file
+  Likewise, @kbd{_} (@code{dired-create-empty-file}) reads a
+file name, and creates an empty file with that name.  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/etc/NEWS b/etc/NEWS
index 39c88c60e7..69e8733d73 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -500,8 +500,12 @@ properties as intact as possible.
 * Changes in Specialized Modes and Packages in Emacs 26.1
 
 ** Dired
-You can now use '`?`' in 'dired-do-shell-command'; as ' ? ', it gets replaced
-by the current file name.
+
+*** New command 'dired-create-empty-file' (similar to
+'dired-create-directory') creates a new empty file; bound to '_'.
+
+*** You can now use '`?`' in 'dired-do-shell-command'; as ' ? ',
+it gets replaced by the current file name.
 
 *** html2text is now marked obsolete.
 
diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el
index e454775858..9c3585d669 100644
--- a/lisp/dired-aux.el
+++ b/lisp/dired-aux.el
@@ -1958,25 +1958,47 @@ dired-dwim-target-defaults
       dired-dirs)))
 
 
-;;;###autoload
-(defun dired-create-directory (directory)
-  "Create a directory called DIRECTORY.
-If DIRECTORY already exists, signal an error."
-  (interactive
-   (list (read-file-name "Create directory: " (dired-current-directory))))
-  (let* ((expanded (directory-file-name (expand-file-name directory)))
-        (try expanded) new)
-    (if (file-exists-p expanded)
-       (error "Cannot create directory %s: file exists" expanded))
+(defun dired--create-empty-file-or-directory (fname &optional create-file)
+  "Create an empty file or directory called FNAME.
+If FNAME already exists, signal an error.
+Optional arg CREATE-FILE if non-nil, then create a file.  Otherwise create
+a directory. "
+  (let* ((expanded (directory-file-name (expand-file-name fname)))
+         (parent (directory-file-name (file-name-directory expanded)))
+         (try expanded) new)
+    (when create-file
+      (setq try parent
+            new expanded))
+    (when (file-exists-p expanded)
+      (error "Cannot create file %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))))
-    (make-directory expanded t)
+            try (directory-file-name (file-name-directory try))))
+    (cond (create-file
+           (unless (file-exists-p parent)
+             (make-directory parent t))
+           (write-region "" nil expanded nil 0))
+          (t
+           (make-directory expanded t)))
     (when new
       (dired-add-file new)
       (dired-move-to-filename))))
 
+;;;###autoload
+(defun dired-create-directory (directory)
+  "Create a directory called DIRECTORY.
+If DIRECTORY already exists, signal an error."
+  (interactive (list (read-file-name "Create directory: ")))
+  (dired--create-empty-file-or-directory directory))
+
+;;;###autoload
+(defun dired-create-empty-file (file)
+  "Create an empty file called FILE.
+If FILE already exists, signal an error."
+  (interactive (list (read-file-name "Create empty file: ")))
+  (dired--create-empty-file-or-directory file 'create-file))
+
 (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 0c1f3e4af6..291c545e87 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -1557,6 +1557,7 @@ dired-mode-map
     (define-key map "x" 'dired-do-flagged-delete)
     (define-key map "y" 'dired-show-file-type)
     (define-key map "+" 'dired-create-directory)
+    (define-key map "_" 'dired-create-empty-file)
     ;; moving
     (define-key map "<" 'dired-prev-dirline)
     (define-key map ">" 'dired-next-dirline)
--8<-----------------------------cut here---------------end--------------->8---
In GNU Emacs 26.0.50 (build 7, x86_64-pc-linux-gnu, GTK+ Version 3.22.11)
 of 2017-07-03
Repository revision: bc3dcd524dfb5c889ed017c093eaf028596fc35c







reply via email to

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