Subject: bug#17251: 24.4.50; `dired-guess-default': wrong logic

From: Drew Adams <[email-masked]>
Date: Sat, 12 Apr 2014 09:05:42 -0700 (PDT)

The logic in the implementation of `dired-guess-default' is wrong, or
at least not optimal.  And it is not what users are entitled to expect,
based on the doc of `dired-guess-shell-alist-user'.

If this logic is kept as is (which would be unfortunate), then at least
there is a doc bug here, because the doc of user option
`dired-guess-shell-alist-user' does not reflect this logic.  Users will
thus get behavior that does not correspond to what they expect based on
the doc.

The doc gives the impression that if you have multiple entries in the
option value that use different regexps with the same command, these
will be used together, IOW, any and all of them will be associated
with the same command.

That is the whole point of this user option (and also of variable
`dired-guess-shell-alist-default'): to associate file names with one
or more commands.  The regexps are only a handy means to that end.

For example, from the doc, one can reasonably expect that if the
option value is this:

(("\\.png\\'" "foo.exe") ("\\.jpg\\'" "foo.exe"))

then command "foo.exe" can be used for a list of files such as:

("toto.png" "tata.jpg")

But this is not in fact what the behavior is.  To get that expected
behavior, the user would have to combine those entries into a single
entry such as this:

(("\\.\\(png\\|jpg\\)\\'" "foo.exe"))

This is not at all clear from the doc.  And it means extra work and
better knowledge of regexps on the part of users.

The culprit code is this part of `dired-guess-default':

;; If more than one file, see if all of FILES match regular expression.
(while (and flist (string-match-p regexp (car flist)))
  (setq flist (cdr flist)))

IOW, the code expects each of the files in the list of candidates to
match the given regexp.

In the case above, this means that the regexp "\\.png\\'" would be
tested against each of the files ("toto.png" "tata.jpg"), it would fail,
and so the command "foo.exe", which the user has explicitly configured
as appropriate for both PNG files and JPG files, would not be available.

IOW, instead of working off of the regexp only, the code should work off
of the associated COMMANDS.  What is important here is the COMMAND,
"foo.exe", and it is clearly (according to the user) appropriate for
all files whose names match either regexp (i.e., all PNG and JPG files).

IOW, the implementation is overly restrictive.  The true requirement is
that each of the file names in the list of candidates must be mapped to
the SAME COMMAND via *some* regexp.  The requirement is not (should not
be) that each of the file names must match the SAME REGEXP.

It is the COMMAND that is important.  The regexps are just a means of
associating file names with commands.

If you do not fix the implementation logic of `dired-guess-default'
(which would be unfortunate), then please at least fix the doc string of
option `dired-guess-shell-alist-user' so that it tells users that they
MUST COMBINE regexps that are mapped to the same command, i.e., they
must come up with a single regexp for the given command.  A list of
files whose names are associated with the same command via different
regexps will not be handled by that command.

In GNU Emacs 24.4.50.1 (i686-pc-mingw32)
 of 2014-04-08 on ODIEONE
Bzr revision: 116956 [email-masked]
Windowing system distributor `Microsoft Corp.', version 6.1.7601
Configured using:
 `configure --prefix=/c/Devel/emacs/snapshot/trunk
 --enable-checking=yes,glyphs 'CFLAGS=-O0 -g3'
 LDFLAGS=-Lc:/Devel/emacs/lib 'CPPFLAGS=-DGC_MCHECK=1
 -Ic:/Devel/emacs/include''