Subject: Re: bug#63744: 28.2; fix dired-guess-default

From: Michael Heerdegen <[email-masked]>
Date: Mon, 29 May 2023 03:23:46 +0200

Eli Zaretskii <[email-masked]> writes:

> > There is no alternative at the moment other than redefining
> > dired-guess-default.
>
> But your suggestion is also a change in that function, so what's the
> difference?

I think he means: the user should not have to do this.

> > If you have a static list it can be expressed as an element in
> > dired-guess-shell-alist-user as such:
> >
> >    (RE "STR1" "STR2" ...)
> >
> > If you have a dynamic list (for example a list from querying the OS) you
> > are stuck. The patch makes this possible and in my view makes
> > dired-guess-shell-alist-user more coherent. (RE "STR1" "STR2" ...) can
> > be regarded as another syntax for
> >
> >    (RE ("STR1" "STR2" ...))
>
> Can't you generate the entire value of dired-guess-shell-alist-user
> dynamically, including the RE part?

It's a real difference, actually a real win.  Please don't only have
constant lists in mind.

"Dynamically" means that dired can inspect the matched file (the file
name is provided via the variable 'file' bound when evaluating the
expression).  This means the EXPR can look at the properties of the
file, it's type and modes etc, which allows a more fine grained user
customization.  With such a change one can have entries looking like

  (RE (if COND (GET-DEFAULTS-FOR-COND) (GET-OTHER-DEFAULTS)))

which would currently be have to be specified as several entries like

  (RE (when COND DEFAULT-FOR-COND-1))
  (RE (when COND DEFAULT-FOR-COND-2))
    ...
  (RE (when COND DEFAULT-FOR-COND-n))
  (RE (when (not COND) OTHER-DEFAULT-1))
  ...
  (RE (when (not COND) OTHER-DEFAULT-m))

and, much worse, testing the CONDITION would have to be performed many
times.

Other things are currently impossible, for example, taking the current
screen setup into account (which can change within an Emacs session).

@Leo:

> However, it's too late for such extensions on the emacs-29 release
> branch, so it could only go to master, for Emacs 30.  And it should be
> properly documented, of course.

`dired-guess-default' lives in "dired-aux" now.

>  If several COMMANDs are given, the first one will be the default
>  and the rest will be added temporarily to the history and can be retrieved
> @@ -975,9 +975,9 @@ See `dired-guess-shell-alist-user'."
>    (let* ((case-fold-search dired-guess-shell-case-fold-search)
>           (programs
>            (delete-dups
> -           (mapcar
> +           (mapcan
>              (lambda (command)
> -              (eval command `((file . ,(car files)))))
> +              (ensure-list (eval command `((file . ,(car files))))))

I think we should avoid `nconc'ing: if a user does specify literal lists.
the config will be destructively altered (right?).  I think we better
use (apply #'append (mapcar ...)) or something like that.

Michael.