all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Tino Calancha <tino.calancha@gmail.com>
To: 24150@debbugs.gnu.org
Cc: "Ted Zlatanov" <tzz@lifelogs.com>,
	tino.calancha@gmail.com,
	"Clément Pit--Claudel" <clement.pit@gmail.com>,
	"Leo Liu" <sdl.web@gmail.com>
Subject: bug#24150: 26.0.50; New command: dired-create-empty-file
Date: Mon, 03 Jul 2017 13:51:25 +0900	[thread overview]
Message-ID: <87mv8m3yya.fsf@calancha-pc> (raw)
In-Reply-To: <alpine.DEB.2.20.1608042223340.11938@calancha-pc>


>> 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)))
 
 \f
-;;;###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







  parent reply	other threads:[~2017-07-03  4:51 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-04 13:25 bug#24150: 25.1.50; New command: dired-create-empty-file Tino Calancha
2016-08-04 13:54 ` Clément Pit--Claudel
2016-08-04 16:29 ` Leo Liu
2016-08-04 17:13   ` Ted Zlatanov
2016-08-04 17:29     ` Drew Adams
2016-08-05  6:03 ` Tino Calancha
2016-08-05 14:48   ` Drew Adams
2016-08-06 12:38     ` Tino Calancha
2016-08-05  6:07 ` Tino Calancha
2017-05-03  8:23 ` Tino Calancha
2017-07-03  4:51 ` Tino Calancha [this message]
2017-07-03 14:24   ` bug#24150: 26.0.50; " Eli Zaretskii
2017-07-03 15:04     ` Tino Calancha
2017-07-03 16:33       ` Eli Zaretskii
2017-07-03 20:18         ` Thien-Thi Nguyen
2017-07-07 13:13         ` Ted Zlatanov
2017-07-07 13:17           ` Drew Adams
2017-07-07 13:31             ` Ted Zlatanov
2017-07-03 15:12     ` Drew Adams
2017-07-05 18:28   ` Eli Zaretskii
2017-07-05 19:34     ` Drew Adams
2017-07-07  5:36       ` Tino Calancha
2017-07-07 11:11         ` Drew Adams
2018-07-10  7:01           ` Tino Calancha
2018-07-10  7:42             ` Phil Sainty
2018-07-17  7:39               ` Tino Calancha
2018-07-20  9:03                 ` Eli Zaretskii
2018-07-23  3:57                   ` Tino Calancha
2018-07-27  8:39                     ` Eli Zaretskii
2018-07-31  4:47                       ` Tino Calancha
2018-07-31 16:20                         ` Eli Zaretskii
2018-08-01  5:16                           ` Tino Calancha
2018-08-01  6:24                             ` Eli Zaretskii
2018-08-01  7:13                               ` Tino Calancha
2018-08-01  8:56                                 ` Eli Zaretskii
2018-08-01  9:31                                   ` Tino Calancha
2018-08-01 11:45                                     ` Eli Zaretskii
2018-08-02  4:34                                       ` Tino Calancha

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87mv8m3yya.fsf@calancha-pc \
    --to=tino.calancha@gmail.com \
    --cc=24150@debbugs.gnu.org \
    --cc=clement.pit@gmail.com \
    --cc=sdl.web@gmail.com \
    --cc=tzz@lifelogs.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.