Subject: bug#46884: [PATCH] 27.1; Cannot run find-dired with -maxdepth

From: Allen Li <[email-masked]>
Date: Wed, 3 Mar 2021 03:38:49 +0000

On Wed, Mar 3, 2021 at 1:20 AM Allen Li <[email-masked]> wrote:
>
> Some additional context: I'm not asking for find-dired to support
-maxdepth necessarily, but the way find-dired is structured makes it
impossible to reuse any of the code to write a command that could support
-maxdepth.
>
> I attached a patch pulling most of the find-dired logic into a
find-dired-unescaped function so that it can be readily reused.

I made a mistake in my initial analysis. find (at least the version I have)
allows one to pass global options such as -maxdepth like so:

    find . \( -maxdepth 3 ARGS \) -ls

However, find will print a warning message as this usage is not recommended.

Also, I realized that my previous patch, while functional, is awkward since
it exists solely to allow users to specify global options
(based on the discussion on #27456, I believe all other find functionality
should be possible with find-dired).
I have made a new patch adding an optional parameter to find-dired for
passing such global options to find.

Aside: there is a discussion at #32668 about adding a similar command line
editing feature like rgrep
I'm not fond of that as it is impossible to use programmatically and
interacts poorly (read: not at all)
with `repeat-complex-command'

https://debbugs.gnu.org/cgi/bugreport.cgi?bug=27456
From 3831be3067f97734137134e40e3e4467abd49816 Mon Sep 17 00:00:00 2001
From: Allen Li <[email-masked]>
Date: Tue, 2 Mar 2021 14:24:16 -0800
Subject: [PATCH] find-dired: Add global-args to find-dired

The original find-dired does not allow for constructing queries like

 find . -maxdepth 3 \( OTHER-ARGS \) -ls

* lisp/find-dired.el (find-dired): Added optional parameter.
---
 etc/NEWS           |  4 ++++
 lisp/find-dired.el | 10 ++++++----
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 73f136cfa7..40f79f8232 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -653,6 +653,10 @@ keys, add the following to your init file:
 (global-set-key "\C-x\C-j" nil)
 (global-set-key "\C-x4\C-j" nil)
 
+---
+*** Optional 'global-args' parameter added to 'find-dired'.
+This allows passing find global options like -maxdepth.
+
 ** Change Logs and VC
 
 *** More VC commands can be used from non-file buffers.
diff --git a/lisp/find-dired.el b/lisp/find-dired.el
index adc5672eca..fc35cccddd 100644
--- a/lisp/find-dired.el
+++ b/lisp/find-dired.el
@@ -157,14 +157,15 @@ find-args-history
 (defvar dired-sort-inhibit)
 
 ;;;###autoload
-(defun find-dired (dir args)
+(defun find-dired (dir args &optional global-args)
   "Run `find' and go into Dired mode on a buffer of the output.
 The command run (after changing into DIR) is essentially
 
-    find . \\( ARGS \\) -ls
+    find . GLOBAL-ARGS \\( ARGS \\) -ls
 
 except that the car of the variable `find-ls-option' specifies what to
-use in place of \"-ls\" as the final argument.
+use in place of \"-ls\" as the final argument.  GLOBAL-ARGS is empty
+when called interactively.
 
 Collect output in the \"*Find*\" buffer.  To kill the job before
 it finishes, type \\[kill-find]."
@@ -202,6 +203,7 @@ find-dired
     (setq default-directory dir
 	  find-args args	      ; save for next interactive call
 	  args (concat find-program " . "
+                       (if global-args global-args "")
 		       (if (string= args "")
 			   ""
 			 (concat
@@ -393,7 +395,7 @@ find-dired-sentinel
 	      ;; will stay around until M-x `list-processes'.
 	      (delete-process proc)
 	      (force-mode-line-update))))
-	  (message "find-dired %s finished." buf))))
+      (message "find-dired %s finished." buf))))
 
 (defun find-dired-sort-by-filename ()
   "Sort entries in *Find* buffer by file name lexicographically."
-- 
2.30.1