Skip to content

Commit 36e3565

Browse files
committed
refactor(cd): move to completions/, rename _cd -> _comp_cmd_cd
1 parent 67b2210 commit 36e3565

File tree

5 files changed

+69
-59
lines changed

5 files changed

+69
-59
lines changed

bash_completion

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,65 +2163,6 @@ _known_hosts_real()
21632163
complete -F _known_hosts traceroute traceroute6 \
21642164
fping fping6 telnet rsh rlogin ftp dig drill mtr ssh-installkeys showmount
21652165

2166-
# This meta-cd function observes the CDPATH variable, so that cd additionally
2167-
# completes on directories under those specified in CDPATH.
2168-
#
2169-
_cd()
2170-
{
2171-
local cur prev words cword comp_args
2172-
_comp_initialize -- "$@" || return
2173-
2174-
if [[ $cur == -* ]]; then
2175-
local cmd=$1
2176-
_comp_compgen COMPREPLY -W '$(_parse_help help "$cmd")' -- "$cur"
2177-
compopt +o nospace
2178-
return
2179-
fi
2180-
2181-
local IFS=$'\n' i j k
2182-
2183-
compopt -o filenames
2184-
2185-
# Use standard dir completion if no CDPATH or parameter starts with /,
2186-
# ./ or ../
2187-
if [[ ! ${CDPATH-} || $cur == ?(.)?(.)/* ]]; then
2188-
_filedir -d
2189-
return
2190-
fi
2191-
2192-
local mark_dirs="" mark_symdirs=""
2193-
_comp_readline_variable_on mark-directories && mark_dirs=y
2194-
_comp_readline_variable_on mark-symlinked-directories && mark_symdirs=y
2195-
2196-
# we have a CDPATH, so loop on its contents
2197-
for i in ${CDPATH//:/$'\n'}; do
2198-
# create an array of matched subdirs
2199-
k=${#COMPREPLY[@]}
2200-
for j in $(compgen -d -- "$i/$cur"); do
2201-
if [[ ($mark_symdirs && -L $j || $mark_dirs && ! -L $j) && ! -d ${j#"$i/"} ]]; then
2202-
j+="/"
2203-
fi
2204-
COMPREPLY[k++]=${j#"$i/"}
2205-
done
2206-
done
2207-
2208-
_filedir -d
2209-
2210-
if ((${#COMPREPLY[@]} == 1)); then
2211-
i=${COMPREPLY[0]}
2212-
if [[ $i == "$cur" && $i != "*/" ]]; then
2213-
COMPREPLY[0]="${i}/"
2214-
fi
2215-
fi
2216-
2217-
return
2218-
}
2219-
if shopt -q cdable_vars; then
2220-
complete -v -F _cd -o nospace cd pushd
2221-
else
2222-
complete -F _cd -o nospace cd pushd
2223-
fi
2224-
22252166
# A meta-command completion function for commands like sudo(8), which need to
22262167
# first complete on a command, then complete according to that command's own
22272168
# completion definition.

bash_completion.d/000_bash_completion_compat.bash

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,11 @@ _init_completion()
237237
# shellcheck disable=SC2154 # defined in the main "bash_completion"
238238
_backup_glob=$_comp_backup_glob
239239

240+
# @deprecated use `_comp_cmd_cd` instead.
241+
_cd()
242+
{
243+
declare -F _comp_cmd_cd &>/dev/null || __load_completion cd
244+
_comp_cmd_cd "$@"
245+
}
246+
240247
# ex: filetype=sh

completions/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169
/puppetmasterd
170170
/puppetqd
171171
/puppetrun
172+
/pushd
172173
/pvchange
173174
/pvcreate
174175
/pvdisplay

completions/Makefile.am

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ bashcomp_DATA = 2to3 \
4343
carton \
4444
ccache \
4545
ccze \
46+
cd \
4647
cfagent \
4748
cfrun \
4849
chage \
@@ -701,6 +702,7 @@ CLEANFILES = \
701702
puppetmasterd \
702703
puppetqd \
703704
puppetrun \
705+
pushd \
704706
pvchange \
705707
pvcreate \
706708
pvdisplay \
@@ -852,6 +854,8 @@ symlinks: $(DATA)
852854
ncal
853855
$(ss) cardctl \
854856
pccardctl
857+
$(ss) cd \
858+
pushd
855859
$(ss) chromium-browser \
856860
chrome chromium google-chrome google-chrome-stable
857861
$(ss) complete \

completions/cd

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# cd(1) completion -*- shell-script -*-
2+
3+
# This meta-cd function observes the CDPATH variable, so that `cd`
4+
# additionally completes on directories under those specified in CDPATH.
5+
_comp_cmd_cd()
6+
{
7+
local cur prev words cword comp_args
8+
_comp_initialize -- "$@" || return
9+
10+
if [[ $cur == -* ]]; then
11+
local cmd=$1
12+
_comp_compgen COMPREPLY -W '$(_parse_help help "$cmd")' -- "$cur"
13+
compopt +o nospace
14+
return
15+
fi
16+
17+
local IFS=$'\n' i j k
18+
19+
compopt -o filenames
20+
21+
# Use standard dir completion if no CDPATH or parameter starts with /,
22+
# ./ or ../
23+
if [[ ! ${CDPATH-} || $cur == ?(.)?(.)/* ]]; then
24+
_filedir -d
25+
return
26+
fi
27+
28+
local mark_dirs="" mark_symdirs=""
29+
_comp_readline_variable_on mark-directories && mark_dirs=y
30+
_comp_readline_variable_on mark-symlinked-directories && mark_symdirs=y
31+
32+
# we have a CDPATH, so loop on its contents
33+
for i in ${CDPATH//:/$'\n'}; do
34+
# create an array of matched subdirs
35+
k=${#COMPREPLY[@]}
36+
for j in $(compgen -d -- "$i/$cur"); do
37+
if [[ ($mark_symdirs && -L $j || $mark_dirs && ! -L $j) && ! -d ${j#"$i/"} ]]; then
38+
j+="/"
39+
fi
40+
COMPREPLY[k++]=${j#"$i/"}
41+
done
42+
done
43+
44+
_filedir -d
45+
46+
if ((${#COMPREPLY[@]} == 1)); then
47+
i=${COMPREPLY[0]}
48+
if [[ $i == "$cur" && $i != "*/" ]]; then
49+
COMPREPLY[0]="${i}/"
50+
fi
51+
fi
52+
}
53+
if shopt -q cdable_vars; then
54+
complete -v -F _comp_cmd_cd -o nospace cd pushd
55+
else
56+
complete -F _comp_cmd_cd -o nospace cd pushd
57+
fi

0 commit comments

Comments
 (0)