unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Joseph Turner via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 66224@debbugs.gnu.org
Subject: bug#66224: [PATCH] Add optional PREDICATE argument to read-directory-name
Date: Tue, 03 Oct 2023 16:08:57 -0700	[thread overview]
Message-ID: <87fs2r47nd.fsf@breatheoutbreathe.in> (raw)
In-Reply-To: <83wmw9yyjr.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 2440 bytes --]


Eli Zaretskii <eliz@gnu.org> writes:

>> Date: Tue, 26 Sep 2023 18:16:40 -0700
>> From:  Joseph Turner via "Bug reports for GNU Emacs,
>>  the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
>>
>> This patch makes read-directory-name accept an optional PREDICATE arg
>> so it can be used like read-file-name.
>
> Any rationale?

Mainly code clarity. If you want to prompt for a certain type of
directory, then IMO the intent is more clear like this:

(read-directory-name "Prompt: " "~/" nil nil nil #'my/predicate)

than like this:

(read-file-name "Prompt: " "~/" nil nil nil
                (lambda (filename)
                  (and (funcall #'file-directory-p filename)
                       (funcall #'my/predicate filename))))

Also, I think that it would follow the principle of least suprise to
have read-file-name and read-directory-name accept the same args.

>> @@ -1719,7 +1719,9 @@ combining @var{directory} (or the current buffer's default directory
>>  if @var{directory} is @code{nil}) and @var{initial}.  If both
>>  @var{default} and @var{initial} are @code{nil}, this function uses
>>  @var{directory} as substitute default, or the current buffer's default
>> -directory if @var{directory} is @code{nil}.
>> +directory if @var{directory} is @code{nil}.  The union of sixth arg
>> +@code{predicate} and @code{file-directory-p} is passed as the
>
> This should be elaborated: what is meant by "the union of sixth arg
> and 'file-directory-p'"?
>
>> +@code{predicate} argument to ‘read-file-name’.
>                                 ^^^^^^^^^^^^^^^^
> That should be @code{read-file-name}
>
>> +** 'read-directory-name' now accepts an optional PREDICATE argument.
>> +The union of sixth arg PREDICATE and 'file-directory-p' is passed
>> +as the PREDICATE argument to 'read-file-name'.
>
> Likewise here, regarding the "union" part.
>
> Also, since you submit the patch for the manual, this NEWS entry
> should be marked with "+++".
>
>>  Fifth arg INITIAL specifies text to start with.
>>  DIR should be an absolute directory name.  It defaults to
>> -the value of `default-directory'."
>> +the value of `default-directory'.
>> +The union of sixth arg PREDICATE and `file-directory-p' is passed
>> +as the PREDICATE argument to `read-file-name'."
>
> The "union" part again.

Thanks for the review!! Please see attached patch.

Best,

Joseph


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-optional-PREDICATE-argument-to-read-directory-na.patch --]
[-- Type: text/x-diff, Size: 4288 bytes --]

From 1c7581914c1b446ea17692d425a4605d915fa7ef Mon Sep 17 00:00:00 2001
From: Joseph Turner <joseph@breatheoutbreathe.in>
Date: Thu, 28 Sep 2023 20:27:47 -0700
Subject: [PATCH] Add optional PREDICATE argument to read-directory-name

* lisp/files.el (read-directory-name): Add optional PREDICATE arg.
* doc/lispref/minibuf.texi (Reading File Names): Document change.
* etc/NEWS: Note change.
---
 doc/lispref/minibuf.texi |  8 ++++++--
 etc/NEWS                 |  7 +++++++
 lisp/files.el            | 14 +++++++++++---
 3 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/doc/lispref/minibuf.texi b/doc/lispref/minibuf.texi
index 620c58ec6e9..eb344595ae3 100644
--- a/doc/lispref/minibuf.texi
+++ b/doc/lispref/minibuf.texi
@@ -1709,7 +1709,7 @@ If this variable is non-@code{nil}, @code{read-file-name} ignores case
 when performing completion.
 @end defopt
 
-@defun read-directory-name prompt &optional directory default require-match initial
+@defun read-directory-name prompt &optional directory default require-match initial predicate
 This function is like @code{read-file-name} but allows only directory
 names as completion alternatives.
 
@@ -1719,7 +1719,11 @@ combining @var{directory} (or the current buffer's default directory
 if @var{directory} is @code{nil}) and @var{initial}.  If both
 @var{default} and @var{initial} are @code{nil}, this function uses
 @var{directory} as substitute default, or the current buffer's default
-directory if @var{directory} is @code{nil}.
+directory if @var{directory} is @code{nil}.  Sixth arg
+@code{predicate}, if non-nil, should be a function of one argument;
+then a file name is considered an acceptable completion alternative if
+both @code{file-directory-p} and @code{predicate} return non-nil with
+the file name argument.
 @end defun
 
 @defopt insert-default-directory
diff --git a/etc/NEWS b/etc/NEWS
index 1b3532b5657..a5a41d6546f 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -3541,6 +3541,13 @@ This function is called to see whether what the user has typed is a
 match.  This is also available from functions that call
 'completing-read', like 'read-file-name'.
 
++++
+** 'read-directory-name' now accepts an optional PREDICATE argument.
+Sixth arg PREDICATE, if non-nil, should be a function of one argument;
+then a file name is considered an acceptable completion alternative if
+both `file-directory-p' and PREDICATE return non-nil with the file
+name argument.
+
 ** 'posn-col-row' can now give position data based on windows.
 Previously, it reported data only based on the frame.
 
diff --git a/lisp/files.el b/lisp/files.el
index b72f141c0ee..1663f1e2d71 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -807,7 +807,7 @@ See Info node `(elisp)Standard File Names' for more details."
     (dos-convert-standard-filename filename))
    (t filename)))
 
-(defun read-directory-name (prompt &optional dir default-dirname mustmatch initial)
+(defun read-directory-name (prompt &optional dir default-dirname mustmatch initial predicate)
   "Read directory name, prompting with PROMPT and completing in directory DIR.
 Value is not expanded---you must call `expand-file-name' yourself.
 Default name to DEFAULT-DIRNAME if user exits with the same
@@ -821,14 +821,22 @@ Fourth arg MUSTMATCH non-nil means require existing directory's name.
  Non-nil and non-t means also require confirmation after completion.
 Fifth arg INITIAL specifies text to start with.
 DIR should be an absolute directory name.  It defaults to
-the value of `default-directory'."
+the value of `default-directory'.
+Sixth arg PREDICATE, if non-nil, should be a function of one
+argument; then a file name is considered an acceptable completion
+alternative if both `file-directory-p' and PREDICATE return
+non-nil with the file name argument."
   (unless dir
     (setq dir default-directory))
   (read-file-name prompt dir (or default-dirname
 				 (if initial (expand-file-name initial dir)
 				   dir))
 		  mustmatch initial
-		  'file-directory-p))
+                  (if predicate
+                      (lambda (filename)
+                        (and (file-directory-p filename)
+                             (funcall predicate filename)))
+                    #'file-directory-p)))
 
 \f
 (defun pwd (&optional insert)
-- 
2.41.0


      reply	other threads:[~2023-10-03 23:08 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-27  1:16 bug#66224: [PATCH] Add optional PREDICATE argument to read-directory-name Joseph Turner via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-28  9:05 ` Philip Kaludercic
2023-09-29  3:28   ` Joseph Turner via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-29  3:54     ` Michael Heerdegen
2023-09-29  4:19       ` Joseph Turner via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-03  3:30         ` Michael Heerdegen
2023-10-03 23:20           ` Joseph Turner via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-05  1:23             ` Michael Heerdegen
2023-10-05  1:34               ` Joseph Turner via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-12 20:42                 ` Joseph Turner via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-01-13 10:53                   ` Stefan Kangas
2024-01-13 19:26                     ` Joseph Turner via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-29  3:28   ` Joseph Turner via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-29 12:05 ` Eli Zaretskii
2023-10-03 23:08   ` Joseph Turner via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]

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=87fs2r47nd.fsf@breatheoutbreathe.in \
    --to=bug-gnu-emacs@gnu.org \
    --cc=66224@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=joseph@breatheoutbreathe.in \
    /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).