about summary refs log tree commit diff
path: root/Completion/Base/_jobs
blob: 45983ad16f602325d901ccd59420ca48fd435b7d (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#autoload

local expl disp jobs job jids pfx='%' desc how expls

_wanted jobs || return 1

if [[ "$1" = -t ]]; then
  zstyle -T ":completion:${curcontext}:jobs" prefix-needed &&
      [[ "$PREFIX" != %* && compstate[nmatches] -ne 0 ]] && return 1
  shift
fi
zstyle -t ":completion:${curcontext}:jobs" prefix-hidden && pfx=''
zstyle -T ":completion:${curcontext}:jobs" verbose       && desc=yes

if [[ "$1" = -r ]]; then
  jids=( "${(@k)jobstates[(R)running*]}" )
  shift
  expls='running job'
elif [[ "$1" = -s ]]; then
  jids=( "${(@k)jobstates[(R)running*]}" )
  shift
  expls='suspended job'
else
  [[ "$1" = - ]] && shift
  jids=( "${(@k)jobtexts}" )
  expls=job
fi

if [[ -n "$desc" ]]; then
  disp=()
  for job in "$jids[@]"; do
    [[ -n "$desc" ]] &&
        disp=( "$disp[@]" "${pfx}${(r:2:: :)job} -- ${(r:COLUMNS-8:: :)jobtexts[$job]}" )
  done
fi

zstyle -s ":completion:${curcontext}:jobs" numbers how

if [[ "$how" = (yes|true|on|1) ]]; then
  jobs=( "$jids[@]" )
else
  local texts i text str tmp num max=0

  # Find shortest unambiguous strings.

  texts=( "$jobtexts[@]" )
  jobs=()
  for i in "$jids[@]"; do
    text="$jobtexts[$i]"
    str="${text%% *}"
    if [[ "$text" = *\ * ]]; then
      text="${text#* }"
    else
      text=""
    fi
    tmp=( "${(@M)texts:#${str}*}" )
    num=1
    while [[ -n "$text" && $#tmp -ge 2 ]]; do
      str="${str} ${text%% *}"
      if [[ "$text" = *\ * ]]; then
        text="${text#* }"
      else
        text=""
      fi
      tmp=( "${(@M)texts:#${str}*}" )
      (( num++ ))
    done

    [[ num -gt max ]] && max="$num"

    jobs=( "$jobs[@]" "$str" )
  done

  if [[ "$how" = [0-9]## && max -gt how ]]; then
    jobs=( "$jids[@]" )
  else
    [[ -z "$pfx" && -n "$desc" ]] && disp=( "${(@)disp#%}" )
  fi
fi

if [[ -n "$desc" ]]; then
  _all_labels jobs expl "$expls" compadd "$@" -ld disp - "%$^jobs[@]"
else
  _all_labels jobs expl "$expls" compadd "$@" - "%$^jobs[@]"
fi