26763: fix problem on failed cd -s to relative path
[zsh.git] / Completion / Unix / Type / _pids
blobea5ed79ee1f456ff7aa785491058620689c50414
1 #compdef pflags pcred pmap pldd psig pstack pfiles pwdx pstop prun pwait ptree
3 # If given the `-m <pattern>' option, this tries to complete only pids
4 # of processes whose command line match the `<pattern>'.
6 local out pids list expl match desc listargs all nm ret=1
8 _tags processes || return 1
10 if [[ "$1" = -m ]]; then
11   all=()
12   match="*[[:blank:]]${PREFIX}[0-9]#${SUFFIX}[[:blank:]]*[/[:blank:]]${2}*"
13   shift 2
14 elif [[ "$PREFIX$SUFFIX" = ([%-]*|[0-9]#) ]]; then
15   all=()
16   match="(*[[:blank:]]|)${PREFIX}[0-9]#${SUFFIX}[[:blank:]]*"
17 else
18   all=(-U)
19   match="*[[:blank:]]*[[/[:blank:]]$PREFIX*$SUFFIX*"
20   nm="$compstate[nmatches]"
23 out=( "${(@f)$(_call_program processes ps 2>/dev/null)}" )
24 desc="$out[1]"
25 out=( "${(@M)out[2,-1]:#${~match}}" )
27 if [[ "$desc" = (#i)(|*[[:blank:]])pid(|[[:blank:]]*) ]]; then
28   pids=( "${(@)${(@M)out#${(l.${#desc[1,(r)(#i)[[:blank:]]pid]}..?.)~:-}[^[:blank:]]#}##*[[:blank:]]}" )
29 else
30   pids=( "${(@)${(@M)out##[^0-9]#[0-9]#}##*[[:blank:]]}" )
33 if zstyle -T ":completion:${curcontext}:processes" verbose; then
34   list=( "${(@Mr:COLUMNS-1:)out}" )
35   desc=(-ld list)
36 else
37   desc=()
40 _wanted processes expl 'process ID' \
41     compadd "$@" "$desc[@]" "$all[@]" -a - pids && ret=0
43 if [[ -n "$all" ]]; then
44   zstyle -s ":completion:${curcontext}:processes" insert-ids out || out=menu
46   case "$out" in
47   menu)   compstate[insert]=menu ;;
48   single) [[ $compstate[nmatches] -ne nm+1 && $compstate[insert] != menu ]] &&
49               compstate[insert]= ;;
50   *)      [[ ${#:-$PREFIX$SUFFIX} -gt ${#compstate[unambiguous]} ]] &&
51               compstate[insert]=menu ;;
52   esac
55 return ret