add the functions needed for some keybindings
[zsh.git] / .zsh / functions / zpgrep
1 # -*- mode:sh -*-
2
3 local _USER REGEXP
4
5 local usage="USAGE: $0 [-u USER] REGEXP"
6
7 if (( $# == 1 )); then
8 REGEXP="$1"
9 elif (( $# == 3 )); then
10 if [[ "$1" == "-u" ]]; then
11 _USER="$2"
12 REGEXP="$3"
13 elif [[ "$2" == "-u" ]]; then
14 _USER="$3"
15 REGEXP="$1"
16 else
17 warn "$usage"
18 return 1
19 fi
20 else
21 warn "$usage"
22 return 1
23 fi
24
25 if is-callable pgrep; then
26 if [[ -n "$_USER" ]]; then
27 pgrep -u "$_USER" -f "$REGEXP"
28 else
29 pgrep -f "$REGEXP"
30 fi
31 elif is-callable ps; then
32 zmodload zsh/pcre
33
34 local PID __USER COMMAND ret=1
35
36 ps -Ao pid,user,command | (read COMMAND; <&0) | \
37 if [[ -n "$_USER" ]]; then
38 while read PID __USER COMMAND; do
39 if [[ "$__USER" == "$_USER" && "$COMMAND" -pcre-match "$REGEXP" ]]; then
40 ret=0
41 print "$PID"
42 fi
43 done
44 else
45 while read PID __USER COMMAND; do
46 if [[ "$COMMAND" -pcre-match "$REGEXP" ]]; then
47 ret=0
48 print "$PID"
49 fi
50 done
51 fi
52 return $ret
53 else
54 warn "There are no pgrep or ps."
55 return 1
56 fi
57
58 # vim: ft=zsh sts=2 ts=2 sw=2 et fdm=marker fmr={{{,}}}