1#compdef pflags pcred pmap pldd psig pstack pfiles pwdx pstop prun pwait ptree
2
3# If given the `-m <pattern>' option, this tries to complete only pids
4# of processes whose command line match the `<pattern>'.
5
6local out pids list expl match desc listargs all nm ret=1
7
8_tags processes || return 1
9
10if [[ "$1" = -m ]]; then
11  all=()
12  match="*[[:blank:]]${PREFIX}[0-9]#${SUFFIX}[[:blank:]]*[/[:blank:]]${2}*"
13  shift 2
14elif [[ "$PREFIX$SUFFIX" = ([%-]*|[0-9]#) ]]; then
15  all=()
16  match="(*[[:blank:]]|)${PREFIX}[0-9]#${SUFFIX}[[:blank:]]*"
17else
18  all=(-U)
19  match="*[[:blank:]]*[[/[:blank:]]$PREFIX*$SUFFIX*"
20  nm="$compstate[nmatches]"
21fi
22
23out=( "${(@f)$(_call_program processes ps 2>/dev/null)}" )
24desc="$out[1]"
25out=( "${(@M)out[2,-1]:#${~match}}" )
26
27if [[ "$desc" = (#i)(|*[[:blank:]])pid(|[[:blank:]]*) ]]; then
28  pids=( "${(@)${(@M)out#${(l.${#desc[1,(r)(#i)[[:blank:]]pid]}..?.)~:-}[^[:blank:]]#}##*[[:blank:]]}" )
29else
30  pids=( "${(@)${(@M)out##[^0-9]#[0-9]#}##*[[:blank:]]}" )
31fi
32
33if zstyle -T ":completion:${curcontext}:processes" verbose; then
34  list=( "${(@Mr:COLUMNS-1:)out}" )
35  desc=(-ld list)
36else
37  desc=()
38fi
39
40_wanted -V processes expl 'process ID' \
41    compadd "$@" "$desc[@]" "$all[@]" -a - pids && ret=0
42
43if [[ -n "$all" ]]; then
44  zstyle -s ":completion:${curcontext}:processes" insert-ids out || out=menu
45
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
53fi
54
55return ret
56