about summary refs log tree commit diff
path: root/Completion/Builtins/_pids
blob: 5956ddb295ce797aa26dfbdb8cf3733d92f05479 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#autoload

# If given the `-m <pattern>' option, this tries to complete only pids
# of processes whose command line match the `<pattern>'.

local out list expl match desc listargs args all nm ret=1

_tags processes || return 1

if [[ "$1" = -m ]]; then
  all=()
  match="[[:blank:]]#${PREFIX}[0-9]#${SUFFIX}[[:blank:]]*[/[:blank:]]${2}*"
  shift 2
elif [[ "$PREFIX$SUFFIX" = [0-9]# ]]; then
  all=()
  match="[[:blank:]]#${PREFIX}[0-9]#${SUFFIX}[[:blank:]]*"
else
  all=(-U)
  match="*[[:blank:]]*[/[:blank:]]$PREFIX*$SUFFIX*"
  nm="$compstate[nmatches]"
fi

zstyle -s ":completion:${curcontext}:processes" command args

out="$(_call processes ps 2>/dev/null)"

if zstyle -T ":completion:${curcontext}:processes" verbose; then
  zstyle -s ":completion:${curcontext}:processes-list" command listargs
  (( $#listargs )) || listargs=( "$args[@]" )
  if [[ "$listargs" = "$args" ]]; then
    list=("${(@Mr:COLUMNS-1:)${(f@)out}[2,-1]:#${~match}}")
  else
    list=("${(@Mr:COLUMNS-1:)${(f@)$(_call processes-list ps 2>/dev/null)}[2,-1]:#${~match}}")
  fi
  desc=(-ld list)
else
  desc=()
fi

_wanted processes expl 'process ID' \
    compadd "$@" "$desc[@]" "$all[@]" - \
        ${${${(M)${(f)"${out}"}[2,-1]:#${~match}}## #}%% *} && ret=0

if [[ -n "$all" ]]; then
  zstyle -s ":completion:${curcontext}:processes" insert-ids out || out=menu

  case "$out" in
  menu)   compstate[insert]=menu ;;
  single) [[ $compstate[nmatches] -ne nm+1 ]] && compstate[insert]= ;;
  *)      [[ ${#:-$PREFIX$SUFFIX} -gt ${#compstate[unambiguous]} ]] &&
              compstate[insert]=menu ;;
  esac
fi

return ret