1#compdef pgrep pkill 
2
3local context state line ret=1 expl
4typeset -A opt_args
5typeset -a arguments
6
7arguments=('-P[parent process id]:parent process id:->ppid' 
8     '-g[match only in process group ids]:group:->pgid' 
9     '-G[match only real group id]:group:_groups' 
10     '-s[match only session id]:session id:->sid' 
11     '-t[match only controlled by terminal]:terminal device:->tty'
12     '-u[match only effective user id]:user:_users' 
13     '-U[match only real user id]:user:_users' 
14           '(-n)-o[oldest process]' 
15     '(-o)-n[newest process]' 
16     '-f[match against full command line]' 
17     '-v[negate matching]' 
18     '-x[match exactly]' 
19     '*:process name:->pname')
20
21if [[ $service == 'pkill' ]]
22then
23  arguments+=('-'${^signals}'[signal]')
24elif [[ $service == 'pgrep' ]]
25then
26  arguments+=('-d[output delimiter]:delimiter:compadd ${(s\:\:)IFS}'
27        '-l[list name in addition to id]')
28fi
29
30_arguments -s -w $arguments && ret=0
31
32case $state in
33  (tty)
34    compset -P '*,'
35
36    local -a used ttys
37    used=(${(s:,:)IPREFIX})
38
39    ttys=( /dev/tty*(N) /dev/pts/*(N) )
40    _wanted tty expl 'terminal device' compadd -S ',' -q -F used ${ttys#/dev/}
41    ;;
42    
43  (sid)
44    compset -P '*,'
45
46    local -a used sid
47    used=(${(s:,:)IPREFIX})
48    sid=(${(uon)$(ps -A o sid=)})
49
50    _wanted sid expl 'session id' compadd -S ',' -q -F used $sid
51    ;;
52  
53  (ppid)
54    compset -P '*,'
55
56    local -a used ppid
57    used=(${(s:,:)IPREFIX})
58    ppid=(${(uon)$(ps -A o ppid=)})
59
60    _wanted ppid expl 'parent process id' compadd -S ',' -q -F used $ppid
61    ;;
62
63  (pgid)
64    compset -P '*,'
65
66    local -a used pgid
67    used=(${(s:,:)IPREFIX})
68    pgid=(${(uon)$(ps -A o pgid=)})
69
70    _wanted pgid expl 'process group id' compadd -S ',' -q -F used $pgid
71    ;;
72  
73  (pname)
74    local ispat="pattern matching "
75    if (( ${+opt_args[-x]} ))
76    then
77      ispat=""
78    fi
79    if (( ${+opt_args[-f]} ))
80    then
81      _wanted pname expl $ispat'process command line' compadd ${(u)${(f)"$(ps -A o cmd=)"}}
82    else
83      _wanted pname expl $ispat'process name' compadd ${(u)${(f)"$(ps -A co cmd=)"}}
84    fi
85    ;;
86  
87esac && ret=0
88
89return ret
90