-
Notifications
You must be signed in to change notification settings - Fork 1
/
__diraction-dispatch
124 lines (115 loc) Β· 5.78 KB
/
__diraction-dispatch
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#compdef _diraction-dispatch # -*- mode: sh -*-
# completion for the dispatcher command
# words: 1:function 2:directory 3:pattern
local diract=$words[2]
local dirdir=`eval echo $diract`
local subcommand=$words[3]
# Update Pwd to make subcompleter to consider we are in the target directory
local OLDPWD=$PWD
local PWD=$dirdir
if (( CURRENT == 3 ));
then # Subcomand completion
case $subcommand in
//*) # SubPattern :D
local nslash=$(echo $subcommand | grep -o / |tr -d '\n' | wc -c)
local dirpattern=''; for n ({1..$(($nslash-1))}); do dirpattern="$dirpattern*/"; done
local pattern=$(echo "${subcommand#//}" | sed -e "s:\\(.\\):\\.*\\1\\.*:g")
local cmd="setopt null_glob; builtin cd $dirdir > /dev/null && ls -d -- $dirpattern $dirpattern*/ $dirpattern*/*/ 2>>/dev/null| sed -E 's:^:/:g' | grep \"$pattern\""
local matches="$(eval $cmd 2>/dev/null)"
local paths; if [[ -n "$matches" ]]; then paths=("${(f)matches}"); else paths=(); fi
if [[ ${#paths} -gt 0 ]] && [[ -n ${paths[1]} ]]; then
PREFIX="/" _values "Matching $dirdir Nested Subdirs" ${paths[@]}
return 0
else
_message "No matching nested subdir"
return 1
fi
;;
/*) # Pattern
local nslash=$(($(echo $subcommand | grep -o / | sed 's/^[[:space:]]*//g' | wc -c) - 1))
local dirpattern=''; for n ({1..$nslash}); do dirpattern="$dirpattern*/"; done
local pattern=`eval echo - '${subcommand%/*}/$(echo - ${subcommand##'$dirpattern'} | sed -e "s:\\(.\\):\\.*\\1\\.*:g")'`
local cmd="builtin cd $dirdir > /dev/null && ls -d -- $dirpattern 2>>/dev/null | sed -E 's:^:/:g' | grep \"$pattern\" "
local matches="$(eval $cmd 2>/dev/null)"
local paths; if [[ -n "$matches" ]]; then paths=("${(f)matches}"); else paths=(); fi
if [[ ${#paths} -gt 0 ]] && [[ -n "${paths[1]}" ]]; then
PREFIX="/" _values "Matching $dirdir Subdirs" ${paths[@]}
return 0
else
_message "No matching $dirdir subdir"
return 1
fi
;;
:*) # Leading Pattern
local subpath=${subcommand#:}
local nslash=$(echo $subpath | grep -o / |tr -d '\n' | wc -c)
local dirpattern=''; for n ({0..$nslash}); do dirpattern="$dirpattern*/"; done
local cmd="builtin cd $dirdir > /dev/null && ls -d -- $dirpattern 2>>/dev/null | sed -E 's@^@:@g' | grep -E \"^:$subpath\" "
local matches="$(eval $cmd 2>/dev/null)"
local paths; if [[ -n "$matches" ]]; then paths=("${(f)matches}"); else paths=(); fi
if [[ ${#paths} -gt 0 ]]; then
compadd -x "Matching $dirdir Subdirs starting with '$subpath'" -U ${paths[@]}
# _values dont works : having a special meaning
return 0
else
_message "No matching $dirdir subdir"
return 1
fi
;;
*) # Basic argument
_describe -t commands "Dispatcher subcommand" _DIRACTION_HELP_SUBCOMMAND
compadd -x "Whitelisted commands" $DIRACTION_DISPATCH_WHITELIST
# maybe extend subcommands with these value??
# or extend the array here?
return
esac
elif [[ "$subcommand" =~ "^/." ]]; then
dirdir="$dirdir$subcommand"
shift words; shift words; shift words
((CURRENT--))
words=(__diraction-dispatch $dirdir "$words[@]") __diraction-dispatch
return 0
else # argument completion
shift words; shift words # shifting words args
((CURRENT = CURRENT -2))
case $subcommand in
# note: In order for forwarded sub command completion (_some_subcmd) to work correctly "service" should be set to the $subcmd
#
# see http://zsh.sourceforge.net/Doc/Release/Completion-System.html 20.2.2 Autoloaded files (#compdef section):
# > Each name may also be of the form 'cmd=service'. When completing the command cmd, the function typically behaves as
# if the command (or special context) service was being completed instead. This provides a way of altering the behaviour
# of functions that can perform many different completions. It is implemented by setting the parameter $service when
# calling the function; the function may choose to interpret this how it wishes, and simpler functions will probably ignore it.
e|"exec"|[-,_]) # magic native completion
shift words; ((CURRENT--))
if ((CURRENT == 1 )); then
_command_names
elif functions "_${words[1]}" > /dev/null; then
# note: ${words[1]} is the real subcommand, `-` only forward
cd $PWD && service="${words[1]}" "_${words[1]}" ; cd $OLDPWD
else
_path_files -W $PWD ; _options_set
fi ; return 0
;;
i|interactive|prompt|shell|help)
_message "Just hit enter, and enter the interactive mode :)" && return 0;;
o|open)
_files -W "$PWD"
;;
/)
_directories -W "$PWD"
;;
*)
local compfun="_${subcommand}"
if functions "$compfun" > /dev/null; then
# Β§tofix: git error: zsh:12: command not found: ___diraction-dispatch_main # Β€old
# if so do it up
cd $PWD; service="$subcommand" $compfun ; ret=$? ; cd $OLDPWD; [ $ret -eq 0 ] && return 0
# beware to file completion:
# https://github.com/robbyrussell/oh-my-zsh/issues/2394
else
_message "No Specific completion available for $subcommand"
fi
;;
esac
fi