diff --git a/pkgs/shell-completion/list-collects.rkt b/pkgs/shell-completion/list-collects.rkt index 8e51612d3e..286f8d62f7 100644 --- a/pkgs/shell-completion/list-collects.rkt +++ b/pkgs/shell-completion/list-collects.rkt @@ -1,15 +1,18 @@ #lang racket/base -(require racket/string setup/dirs setup/link) + +(provide get-top-level-collection-paths) + +(require racket/string racket/list racket/path setup/dirs setup/link) (define (add-directory-collections c s) (if (directory-exists? c) (for/fold ([s s]) ([p (in-list (directory-list c))] #:when (directory-exists? (build-path c p)) #:when (regexp-match? #rx#"^[a-zA-Z_%+-]*$" p)) - (hash-set s (path-element->string p) #t)) + (hash-set s (build-path c (path-element->string p)) #t)) s)) -(define (get-all-top-level-collections) +(define (get-top-level-collection-paths) (hash-keys (for/fold ([s (hash)]) ([l (in-list (current-library-collection-links))]) @@ -19,12 +22,23 @@ (current-library-collection-paths))]) (add-directory-collections c s))] [(path? l) - (let ([s (for*/fold ([s s]) ([c (in-list (links #:file l #:root? #f))]) - (hash-set s c #t))]) + (let ([s (for*/fold ([s s]) ([c (in-list (links #:file l #:root? #f #:with-path? #t))]) + (hash-set s (cdr c) #t))]) (for*/fold ([s s]) ([c (in-list (links #:file l #:root? #t))]) (add-directory-collections c s)))] - [else (error 'get-all-top-level-collections + [else (error 'get-top-level-collection-paths "unexpected value in `current-library-collection-links': ~e" l)])))) -(for-each displayln (get-all-top-level-collections)) +(module+ main + (define var "RACKET_COMPLETION_CLIENT") + (define shell (getenv var)) + (cond [(equal? shell "bash") + (for ([p (get-top-level-collection-paths)]) + (define-values [base name dir?] (split-path p)) + (displayln name))] + [(equal? shell "zsh") + (for-each displayln + (remove-duplicates + (map path-only (get-top-level-collection-paths))))] + [else (error 'list-collects "unknown shell (missing ~a envvar)" var)])) diff --git a/pkgs/shell-completion/racket-completion.bash b/pkgs/shell-completion/racket-completion.bash index 4f9faf385d..0b6a9ca3cc 100644 --- a/pkgs/shell-completion/racket-completion.bash +++ b/pkgs/shell-completion/racket-completion.bash @@ -9,6 +9,9 @@ # will need to make sure that you have bash completions enabled, usually # with "source /etc/bash_completion". +# Identify bash as the completion client +export RACKET_COMPLETION_CLIENT=bash + # This completes only *.{rkt,ss,scm,scrbl} files unless there are none, # in which case it completes other things. _racket_filedir() { @@ -116,7 +119,7 @@ _complete_collects() { local cur="$1" if [[ "${#_racket_collects[@]}" -eq 0 ]]; then _racket_collects=( - $( $_racket_cmd -e '(require shell-completion/list-collects)' ) + $( $_racket_cmd -e '(require (submod shell-completion/list-collects main))' ) ) fi local wordlist="" diff --git a/pkgs/shell-completion/racket-completion.zsh b/pkgs/shell-completion/racket-completion.zsh index 78fb02512e..6dfb432ac5 100644 --- a/pkgs/shell-completion/racket-completion.zsh +++ b/pkgs/shell-completion/racket-completion.zsh @@ -36,6 +36,9 @@ # the function since it's loaded on first use.) # +# Identify zsh as the completion client +export RACKET_COMPLETION_CLIENT=zsh + ############################################################################### _racket_file_expander() { @@ -60,8 +63,7 @@ _racket_call() { _racket_read_libfile_or_collect() { local -a dirs dirs=( - "${(f)$(_racket_call directories ' - (for-each displayln (current-library-collection-paths))')}" + "${(f)$(_racket_call directories '(require (submod shell-completion/list-collects main))')}" ) local -a ignored ignored=('*.dep' '*.zo' 'compiled' '*/compiled')