From 3f1d84d593bf864b72043ff2a598b18b5e9b05be Mon Sep 17 00:00:00 2001 From: Joseph Turner 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 argument. * doc/lispref/minibuf.texi (Reading File Names): Document above change. (Bug#66224) --- doc/lispref/minibuf.texi | 2 +- etc/NEWS | 3 +++ lisp/files.el | 13 ++++++++++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/doc/lispref/minibuf.texi b/doc/lispref/minibuf.texi index 42189b9d0d7..3cc206d2e1d 100644 --- a/doc/lispref/minibuf.texi +++ b/doc/lispref/minibuf.texi @@ -1715,7 +1715,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. diff --git a/etc/NEWS b/etc/NEWS index 33f5df84d4b..9b2558d14d7 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1183,6 +1183,9 @@ runs its body, and removes the current buffer from ** The 'rx' category name 'chinese-two-byte' must now be spelled correctly. An old alternative name (without the first 'e') has been removed. ++++ +** 'read-directory-name' now accepts an optional PREDICATE argument. + --- ** All the digit characters now have the 'digit' category. All the characters whose Unicode general-category is Nd now have the diff --git a/lisp/files.el b/lisp/files.el index e5fde17513f..79b744f80df 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -875,7 +875,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 @@ -889,14 +889,21 @@ 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'. +When sixth arg PREDICATE is non-nil, the union of PREDICATE and +`file-directory-p' is passed as the PREDICATE argument to +`read-file-name'. Otherwise, only `file-directory-p' is passed." (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))) (defun pwd (&optional insert)