_wd.sh 2.57 KB
Newer Older
Markus Faerevaag's avatar
Markus Faerevaag committed
1
2
#compdef wd

3
zstyle ':completion::complete:wd:*:descriptions' format '%B%d%b'
Markus Faerevaag's avatar
Markus Faerevaag committed
4
5
6
7
8
zstyle ':completion::complete:wd:*:commands' group-name commands
zstyle ':completion::complete:wd:*:warp_points' group-name warp_points
zstyle ':completion::complete:wd::' list-grouped

zmodload zsh/mapfile
9

Markus Faerevaag's avatar
Markus Faerevaag committed
10
function _wd() {
11
  local WD_CONFIG=${WD_CONFIG:-$HOME/.warprc}
12
  local ret=1
Markus Faerevaag's avatar
Markus Faerevaag committed
13
14
15

  local -a commands
  local -a warp_points
16

17
  warp_points=( "${(f)mapfile[$WD_CONFIG]//$HOME/~}" )
Markus Faerevaag's avatar
Markus Faerevaag committed
18

19
20
21
22
23
  typeset -A points
  while read -r line
  do
    arr=(${(s,:,)line})
    name=${arr[1]}
24
    target_path=${arr[2]}
25
26

    # replace ~ from path to fix completion (#17)
27
    target_path=${target_path/#\~/$HOME}
28

29
    points[$name]=$target_path
30
  done < $WD_CONFIG
31

Markus Faerevaag's avatar
Markus Faerevaag committed
32
33
34
  commands=(
    'add:Adds the current working directory to your warp points'
    'add!:Overwrites existing warp point'
35
    'export:Export warp points as static named directories'
Markus Faerevaag's avatar
Markus Faerevaag committed
36
    'rm:Removes the given warp point'
Markus Faerevaag's avatar
Markus Faerevaag committed
37
38
39
    'list:Outputs all stored warp points'
    'ls:Show files from given warp point'
    'path:Show path to given warp point'
40
    'show:Outputs all warp points that point to the current directory or shows a specific target directory for a point'
Markus Faerevaag's avatar
Markus Faerevaag committed
41
    'help:Show this extremely helpful text'
Markus Faerevaag's avatar
Markus Faerevaag committed
42
43
    'clean:Remove points warping to nonexistent directories'
    'clean!:Remove nonexistent directories without confirmation'
Markus Faerevaag's avatar
Markus Faerevaag committed
44
45
46
47
48
49
50
    '..:Go back to last directory'
  )

  _arguments -C \
    '1: :->first_arg' \
    '2: :->second_arg' && ret=0

51
52
  local target=$words[2]

Markus Faerevaag's avatar
Markus Faerevaag committed
53
54
55
56
57
58
  case $state in
    first_arg)
      _describe -t warp_points "Warp points" warp_points && ret=0
      _describe -t commands "Commands" commands && ret=0
      ;;
    second_arg)
59
      case $target in
Markus Faerevaag's avatar
Markus Faerevaag committed
60
61
62
63
64
65
        add\!|rm)
          _describe -t points "Warp points" warp_points && ret=0
          ;;
        add)
          _message 'Write the name of your warp point' && ret=0
          ;;
66
67
68
        show)
          _describe -t points "Warp points" warp_points && ret=0
          ;;
Markus Faerevaag's avatar
Markus Faerevaag committed
69
70
71
72
73
74
        ls)
          _describe -t points "Warp points" warp_points && ret=0
          ;;
        path)
          _describe -t points "Warp points" warp_points && ret=0
          ;;
75
        *)
76
77
78
79
80
81
          if [[ -v points[$target] ]]; then
            # complete sub directories from the warp point
            _path_files -W "(${points[$target]})" -/ && ret=0
          fi
          
          # don't complete anything if warp point is not valid
82
          ;;
Markus Faerevaag's avatar
Markus Faerevaag committed
83
84
85
86
87
      esac
      ;;
  esac

  return $ret
88
89
90
}

_wd "$@"
Markus Faerevaag's avatar
Markus Faerevaag committed
91
92
93
94
95
96
97
98

# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et