Subject: Re: bug#20992: 25.0.50; `virtual-dired': (1) doc typo, (2) unclear

From: Phil Sainty <[email-masked]>
Date: Fri, 03 Dec 2021 12:14:28 +1300

Michael Heerdegen writes:
> AFAIR this didn't ever work so super sexy - Emacs didn't recognize the
> contents as (virtual) dired, I have to M-x virtual-dired explicitly, 
> and
> this command modifies the buffer (though it didn't touch the text), so 
> I
> have to save it again...

FWIW...

Funnily enough I was encountering these things just the other day.
I had some saved dired buffers, wished that Emacs would automatically
invoke `dired-virtual-mode' on them, noticed the 'modification' issue
while I was at it, and ended up doing this to my config:

;; Recognise saved dired buffers.
;; Supports both 'ls' and 'find+ls' dired buffers.
;; Regexp is fairly specific, to avoid false-positives.
(add-to-list 'magic-mode-alist
              (cons (rx (seq (seq bos "  /" (+ not-newline) ":\n  ")
                             (or (seq "total used in directory"
                                      (+ not-newline) "\n" (+ space))
                                 (seq "find " (+ not-newline) " -ls\n"
                                      (regexp " +[0-9]+ +[0-9]+ +")))
                             (regexp "[-d]\\(?:[-r][-w][-xs]\\)\\{3\\}")
                             (regexp " +[0-9]+ ")))
                    #'my-dired-virtual-mode))

(defun my-dired-virtual-mode ()
   "Enable `dired-virtual-mode' without marking the buffer as modified."
   (with-silent-modifications
     (dired-virtual-mode)))

I'd be surprised if that regexp was good enough for general use, but
that was good enough for my purpose at the time.

Apologies for the seemingly-arbitrary mix of rx and string syntax...
I'd decided to convert it to rx and then found it getting less readable
instead of more readable, and so it wound up like this :)

-Phil