Subject: bug#36110: find-dired not sorted on any field nor provides a way

From: Juri Linkov <[email-masked]>
Date: Fri, 14 Jun 2019 22:09:10 +0300

>>> +@code{'("-exec ls -ld @{@} +" . "-ld")}
>>
>> Oh, I see you've changed it to the "+" form.

I've changed it to the same value as in `find-exec-terminator'.
So instead of documenting a command with a terminator that depends
on the value of `find-exec-terminator', better would be to provide
different customizable options like in the patch below.

>> It looks like this does give sorted output, but I find it surprising.
>> Is it assured?
>
> I verified on a directory with a large number of files that the output
> is not sorted. (I didn't see how it could be, given how find works.)
> But it does appear as if individual "chunks" are sorted, which can give
> the appearance of the whole output being sorted if you don't have a lot
> of files.

I tried to run different commands on all files in the Emacs source tree:

1. find . -ls

It produces completely unsorted output.

2. find . -exec ls -ld {} +

It splits the output into sizeable chunks and sorts files
inside every chunk, so the boundary between chunks
is clearly visible, e.g.:

-rw-rw-r--  1 juri juri     6191 May  1 23:49 ./test/src/timefns-tests.el
-rw-r--r--  1 juri juri    13623 Jan  2 22:43 ./test/src/undo-tests.el
-rw-r--r--  1 juri juri     2915 Jan  2 22:43 ./test/src/xml-tests.el
drwxr-xr-x   2 juri juri      4096 Jun 11 00:09 ./autom4te.cache
-rw-rw-r--   1 juri juri    945417 Jun 11 00:09 ./autom4te.cache/output.0
-rw-r--r--   1 juri juri      3431 Jun 11 00:09 ./autom4te.cache/requests

3. find . -print0 | sort -z | xargs -0 -e ls -ld

It splits files into chinks, but maintains the sorting order
among all files.   Its only drawback is misaligned chunks, e.g.:

-rw-rw-r--   1 juri juri     62707 May 21 00:04 ./info/forms.info
-rw-rw-r--   1 juri juri   1476691 Jun 11 00:16 ./info/gnus.info
-rw-rw-r--   1 juri juri     74148 May 21 00:04 ./info/htmlfontify.info
-rw-rw-r--  1 juri juri   234730 May 21 00:04 ./info/idlwave.info
-rw-rw-r--  1 juri juri    51892 May 21 00:04 ./info/ido.info
-rw-rw-r--  1 juri juri    84948 May 21 00:04 ./info/info.info

Since an output with more than 5000 files (an approx amount in each chuck
in this experiment) is unmanageable by human users, any of the last 2 options
is sufficiently good.  So here is the patch that allows the user
to choose among these options:

diff --git a/lisp/find-dired.el b/lisp/find-dired.el
index 2c76179da0..c563ae533a 100644
--- a/lisp/find-dired.el
+++ b/lisp/find-dired.el
@@ -51,19 +51,23 @@ find-exec-terminator
   :group 'find-dired
   :type 'string)
 
+(defvar find-ls-option-default-ls
+  (cons "-ls" (if (eq system-type 'berkeley-unix) "-gilsb" "-dilsb")))
+
+(defvar find-ls-option-default-exec
+  (cons (format "-exec ls -ld {} %s" find-exec-terminator) "-ld"))
+
+(defvar find-ls-option-default-xargs
+  (cons "-print0 | sort -z | xargs -0 -e ls -ld" "-ld"))
+
 ;; find's -ls corresponds to these switches.
 ;; Note -b, at least GNU find quotes spaces etc. in filenames
 (defcustom find-ls-option
   (if (eq 0
 	  (ignore-errors
 	    (process-file find-program nil nil nil null-device "-ls")))
-      (cons "-ls"
-	    (if (eq system-type 'berkeley-unix)
-		"-gilsb"
-	      "-dilsb"))
-    (cons
-     (format "-exec ls -ld {} %s" find-exec-terminator)
-     "-ld"))
+      find-ls-option-default-ls
+    find-ls-option-default-exec)
   "A pair of options to produce and parse an `ls -l'-type list from `find'.
 This is a cons of two strings (FIND-OPTION . LS-SWITCHES).
 FIND-OPTION is the option (or options) passed to `find' to produce
@@ -78,9 +82,20 @@ find-ls-option
    (\"-ls\" . \"-dilsb\")
 since GNU find's output has the same format as using GNU ls with
 the options \"-dilsb\"."
-  :version "24.1"	       ; add tests for -ls and -exec + support
-  :type '(cons (string :tag "Find Option")
-	       (string :tag "Ls Switches"))
+  :version "27.1"            ; add choice of predefined set of options
+  :type `(choice
+          (cons :tag "find -ls"
+                (string ,(car find-ls-option-default-ls))
+                (string ,(cdr find-ls-option-default-ls)))
+          (cons :tag "find -exec ls -ld"
+                (string ,(car find-ls-option-default-exec))
+                (string ,(cdr find-ls-option-default-exec)))
+          (cons :tag "find -print | sort | xargs"
+                (string ,(car find-ls-option-default-xargs))
+                (string ,(cdr find-ls-option-default-xargs)))
+          (cons :tag "Other values"
+                (string :tag "Find Option")
+                (string :tag "Ls Switches")))
   :group 'find-dired)
 
 (defcustom find-ls-subdir-switches