unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Tino Calancha <tino.calancha@gmail.com>
To: Tom Tromey <tom@tromey.com>
Cc: Michael Albinus <michael.albinus@gmx.de>, 31495@debbugs.gnu.org
Subject: bug#31495: 26.1; filename completion -vs- "*"
Date: Sat, 19 May 2018 22:32:03 +0900	[thread overview]
Message-ID: <87wovzzs8c.fsf@gmail.com> (raw)
In-Reply-To: <877eo1gern.fsf@tromey.com> (Tom Tromey's message of "Fri, 18 May 2018 09:33:16 -0600")

Tom Tromey <tom@tromey.com> writes:

> I found a situation where using "*" globbing in filename completion acts
> strangely.
>
> Here is how to set up to see the bug:
>
> $ cd /tmp
> $ mkdir -p a/b/c a/d/c
> $ touch a/b/c/q
>
> Now in Emacs, C-x C-f /tmp/a/*/c TAB
>
> On the first TAB, a "/" is appended, and then point moves to the "/"
> before "c".
Yeah, it's broken.
In this example adding a '/' makes the completion works
C-x C-f /tmp/a/*/c/ TAB

> Now type TAB again.
> At this point, the "/tmp/a" is greyed out and the minibuffer shows:
>
> Find file: /tmp/a//c/
>
> ... so now only completions for "/c" are available.
>
> I think instead the "*" should be preserved.  Often I'm using this
> feature to try to find a specific file where I don't know the exact
> subdirectory it is in.
Since Emacs 26.1 dired supports wildcards in the directory part.
You can use
C-x d /tmp/a/*/c/q RET

If you shell in '/bin/sh' supports globstar (and have it enabled by
default) then you can do just:
C-x d /tmp/**/q RET

We could even let the user enable globstar in those shells supporting
it but having it disable by default (e.g. bash).
For instance, following patch:
--8<-----------------------------cut here---------------start------------->8---
commit 1c1116e6a6ed369db63ddf00865f04f687579e71
Author: Tino Calancha <tino.calancha@gmail.com>
Date:   Sat May 19 21:57:42 2018 +0900

    Handle globstar in dired
    
    Allow user to enable globstar when the shell support
    it and disable it by default (e.g. bash).
    * lisp/dired.el (dired-maybe-use-globstar): New user option.
    (dired-enable-globstar-in-shell): New variable.
    (dired-insert-directory): if `dired-maybe-use-globstar' is
    non-nil and the shell supports globstar, then enable it.
    
    * doc/emacs/dired.texi: Document feature.
    ; * etc/NEWS: Add entry.

diff --git a/doc/emacs/dired.texi b/doc/emacs/dired.texi
index fbb3030c2a..ad63c2f6a5 100644
--- a/doc/emacs/dired.texi
+++ b/doc/emacs/dired.texi
@@ -79,6 +79,24 @@ Dired Enter
 @samp{foo}.  The latter lists the files with extension @samp{.el}
 in all the subdirectories of @samp{foo}.
 
+When the system shell supports globstar and it's enabled, then you
+can use recursive globbing:
+
+@example
+C-x d  ~/foo/**/*.el  @key{RET}
+@end example
+
+This command lists all the files with extension @samp{.el} descending
+recursively in all the subdirectories of @samp{foo}.  Note that there
+are small differences in the implementation of globstar between shells.
+Check your shell manual to know the expected behavior.
+
+@vindex dired-maybe-use-globstar
+@vindex dired-enable-globstar-in-shell
+If the shell supports globstar and disables it by default, you
+can still enable this feature with @code{dired-maybe-use-globstar} if
+the shell is included in @code{dired-enable-globstar-in-shell}.
+
 The usual history and completion commands can be used in the minibuffer;
 in particular, @kbd{M-n} puts the name of the visited file (if any) in
 the minibuffer (@pxref{Minibuffer History}).
diff --git a/etc/NEWS b/etc/NEWS
index c7ffb17ad3..931289f0af 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -266,6 +266,12 @@ unescaping text.
 ** Dired
 
 +++
+*** The new user option 'dired-maybe-use-globstar' enables globstar
+in shells that support this feature.  The new variable
+'dired-enable-globstar-in-shell' lists which shells can have enabled
+globstar.
+
++++
 *** The new user option 'dired-create-destination-dirs' controls whether
 'dired-do-copy' and 'dired-rename-file' should create non-existent
 directories in the destination.
diff --git a/lisp/dired.el b/lisp/dired.el
index 1348df6934..0adffdbd3f 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -77,6 +77,26 @@ dired-subdir-switches
    :type '(choice (const :tag "Use dired-listing-switches" nil)
                   (string :tag "Switches")))
 
+(defcustom dired-maybe-use-globstar nil
+  "If non-nil, enable globstar if the shell support it.
+Some shells enable this feature by default (e.g. zsh or fish).
+
+See `dired-enable-globstar-in-shell' for a list of shells
+that support globstar and disable it by default.
+
+Note that the implementation of globstar have small differences
+between shells.  You must check your shell documentation to see
+what to expect."
+  :type 'boolean
+  :group 'dired)
+
+(defconst dired-enable-globstar-in-shell
+  '(("ksh" . "set -G")
+    ("bash" . "shopt -s globstar"))
+  "Alist of (SHELL . COMMAND), where COMMAND enables globstar in SHELL.
+If `dired-maybe-use-globstar' is non-nil, then `dired-insert-directory'
+checks this alist to enable globstar in the shell subprocess.")
+
 (defcustom dired-chown-program
   (purecopy (cond ((executable-find "chown") "chown")
                   ((file-executable-p "/usr/sbin/chown") "/usr/sbin/chown")
@@ -1297,6 +1317,13 @@ dired-insert-directory
                                  (executable-find explicit-shell-file-name))
                             (executable-find "sh")))
                     (switch (if remotep "-c" shell-command-switch)))
+               ;; Enable globstar
+               (when-let ((globstar dired-maybe-use-globstar)
+                          (enable-it
+                           (assoc-default
+                            (file-truename sh) dired-enable-globstar-in-shell
+                            (lambda (reg shell) (string-match reg shell)))))
+                 (setq script (format "%s; %s" enable-it script)))
                (unless
                    (zerop
                     (process-file sh nil (current-buffer) nil switch script))
--8<-----------------------------cut here---------------end--------------->8---
In GNU Emacs 27.0.50 (build 12, x86_64-pc-linux-gnu, GTK+ Version 3.22.11)
 of 2018-05-17 built on calancha-pc
Repository revision: 593c367b0727affc739832ab4f4bdb9d7dd1ddd7





  reply	other threads:[~2018-05-19 13:32 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-18 15:33 bug#31495: 26.1; filename completion -vs- "*" Tom Tromey
2018-05-19 13:32 ` Tino Calancha [this message]
2018-05-19 18:17   ` Michael Albinus
2018-05-20  2:19     ` Tino Calancha
2018-06-20 12:36   ` Noam Postavsky
2018-06-20 12:48     ` Tino Calancha
2018-06-20 13:08       ` Tino Calancha
2018-06-20 14:05         ` Michael Albinus
2018-06-21  1:50           ` Tino Calancha
2018-06-21  7:19             ` Michael Albinus
2018-06-21  7:44               ` Tino Calancha
2020-08-22 15:40   ` Lars Ingebrigtsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87wovzzs8c.fsf@gmail.com \
    --to=tino.calancha@gmail.com \
    --cc=31495@debbugs.gnu.org \
    --cc=michael.albinus@gmx.de \
    --cc=tom@tromey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).