From: Tino Calancha <tino.calancha@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: psainty@orcon.net.nz, tzz@lifelogs.com, clement.pit@gmail.com,
michael.albinus@gmx.de, 24150@debbugs.gnu.org, sdl.web@gmail.com
Subject: bug#24150: 26.0.50; New command: dired-create-empty-file
Date: Tue, 31 Jul 2018 13:47:30 +0900 [thread overview]
Message-ID: <87effk1065.fsf@calancha-pc.dy.bbexcite.jp> (raw)
In-Reply-To: <83r2jprs1h.fsf@gnu.org> (Eli Zaretskii's message of "Fri, 27 Jul 2018 11:39:38 +0300")
Eli Zaretskii <eliz@gnu.org> writes:
>> +@deffn 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 non-@code{nil}.
>> +If @var{filename} already exists, then this command signal an error.
> ^^^^^^
> "signals". And I would drop the "then" part, it is not needed here.
Done! Thank you.
>> +(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))
>
> What is the purpose of using this function in dired-create-empty-file?
Same as in `dired-create-directory': to update the new entry in the Dired buffer.
I have extracted `dired--find-topmost-parent-dir' from `dired-create-directory'
to be DRY.
> What you do with the result of this function doesn't seem to be
> reflected in the doc string of dired-create-empty-file.
That's because `dired--find-topmost-parent-dir' is an internal function
(prefixed with 'dired--'). I don't think we need to detail in the docstring
what callers can do with the return value. Even not having a docstring could
be OK: users don't need to know about this function.
Maybe we can add in a comment the purpose of this function. What do you think?
I have added the following comment:
+;; 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.
>> +(defun make-empty-file (filename &optional parents)
>> + "Create an empty file FILENAME.
>> +Optional arg PARENTS, if non-nil then creates parent dirs as needed.
>
> "Optional arg PARENTS, if non-nil, means create parent dirs as needed."
>
>> +If called interactively, then PARENTS is non-nil."
>> + (interactive
>> + (let ((filename (read-file-name "Create empty file: ")))
>> + (list filename t)))
>> + (let ((paren-dir (file-name-directory filename)))
>> + (when paren-dir (make-directory paren-dir parents)))
>> + (if (and (file-exists-p filename) (null parents))
>> + (signal 'file-already-exists `("File exists" ,filename) )
>
> Shouldn't we check for file existing before creating the missing
> parent directories?
Absolutely. Thank you.
Updated patch:
--8<-----------------------------cut here---------------start------------->8---
commit 31d59e2770da0fbfe60808e91efaf7a275aa9b9e
Author: Tino Calancha <tino.calancha@gmail.com>
Date: Tue Jul 31 13:26:43 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.
+@findex 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.
+@deffn 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 non-@code{nil}.
+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
\f
* 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.
\f
* 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..35ac176195 100644
--- a/lisp/dired-aux.el
+++ b/lisp/dired-aux.el
@@ -1989,6 +1989,19 @@ dired-dwim-target-defaults
dired-dirs)))
\f
+
+;; 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))
+
;;;###autoload
(defun dired-create-directory (directory)
"Create a directory called DIRECTORY.
@@ -1997,18 +2010,31 @@ dired-create-directory
(interactive
(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)
(dired-move-to-filename))))
+;;;###autoload
+(defun dired-create-empty-file (file)
+ "Create an empty file called FILE.
+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 editable"
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 2, x86_64-pc-linux-gnu, GTK+ Version 3.22.11)
of 2018-07-31
Repository revision: 63ef79329935b790b9c8107125bce66e1f272c2e
next prev parent reply other threads:[~2018-07-31 4:47 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 ` bug#24150: 26.0.50; " Tino Calancha
2017-07-03 14:24 ` 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 [this message]
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87effk1065.fsf@calancha-pc.dy.bbexcite.jp \
--to=tino.calancha@gmail.com \
--cc=24150@debbugs.gnu.org \
--cc=clement.pit@gmail.com \
--cc=eliz@gnu.org \
--cc=michael.albinus@gmx.de \
--cc=psainty@orcon.net.nz \
--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 public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).