-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzman
executable file
·111 lines (104 loc) · 3.9 KB
/
zman
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
#!/usr/bin/env zsh
# Can be run as a script and as a function
emulate -LR zsh
setopt extendedglob warncreateglobal typesetsilent noshortloops
if [[ "$0" != declzsh || -n "$ZSH_SCRIPT" ]]; then
# Handle $0 according to the Zsh Plugin Standard:
# https://github.com/z-shell/zi/wiki/Zsh-Plugin-Standard
0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
0="${${(M)0##/*}:-$PWD/$0}"
fi
autoload -Uz .za-zman-handler
local -a match mbegin mend reply
local MATCH REPLY
integer MBEGIN MEND
local -A opthash
zparseopts -E -D -A opthash c h -help || { echo "Improper options given, see help (-h/--help)"; return 1; }
# Converts plugin spec like "user/plugin" into two parts
# returned in reply, namely the user and plugin
-zi-any-to-user-plugin() {
# Two components given?
# That's a pretty fast track to call this function this way
if [[ -n "$2" ]];then
2=${~2}
reply=( "${1:-${${(M)2#/}:+%}}" "${${${(M)1#%}:+$2}:-${2//---//}}" )
return 0
fi
# Is it absolute path?
if [[ "$1" = "/"* ]]; then
reply=( "%" "$1" )
return 0
fi
# Is it absolute path in zplugin format?
if [[ "$1" = "%"* ]]; then
reply=( "%" "${${${1/\%HOME/$HOME}/\%SNIPPETS/${ZI[SNIPPETS_DIR]}}#%}" )
reply[2]=${~reply[2]}
return 0
fi
# Rest is for single component given
# It doesn't touch $2
if [[ "$1" = */* ]]; then
reply=( "${1%%/*}" "${1#*/}" )
return 0
fi
reply=( "${${(M)1#*---}%---}" "${${${1#*---}//---//}:-_unknown}" )
return 0
}
local ZMAN_REPO_DIR="$1" plgdir="$2" snipdir="$3" pors
shift 3
# TODO: #3 full snippet-path handling
local -a parray sarray
parray=( "$plgdir"/"${^${@//\//---}}"(N) "$plgdir"/*---"$^@"(N) )
sarray=( "$snipdir"/"$^@"(N) )
for pors (${parray[@]}) {
[[ "${pors%/}" = "$plgdir" ]] && continue
if [[ -f ''(#i)$pors/README.md(#qN[1]) && ( ! -f ''(#i)$pors/README(#qN[1]) || ''(#i)$pors/README.md(#qN[1]) -nt ''(#i)$pors/README(#qN[1]) ) ]]; then
-zi-any-to-user-plugin "${pors:t}"
# local type="$1" user="$2" plugin="$3" id_as="$4" dir="$5"
.za-zman-handler plugin "${reply[-2]}" "${reply[-1]}" id_as="${${pors:t}//---//}" "$pors"
elif [[ ! -f ''(#i)$pors/README.md(#qN[1]) ]]; then
-zi-any-to-user-plugin "${pors:t}"
print -P -- "%F{38}zman plugin: %F{154}No README.md in the %F{220}${reply[-1]}%F{154} plugin%f"
fi
}
for pors (${sarray[@]}) {
if [[ -f ''(#i)$pors/README.md(#qN[1]) && ( ! -f ''(#i)$pors/README(#qN[1]) || ''(#i)$pors/README.md(#qN[1]) -nt ''(#i)$pors/README(#qN[1]) ) ]]; then
local snippath="${pors#$snipdir/}"
# local type="$1" url="$2" id_as="$3" dir="$4"
.za-zman-handler snippet "$snippath" id_as="${${snippath:t}//--//}" "$pors"
elif [[ ! -f ''(#i)$pors/README.md(#qN[1]) ]]; then
local snippath="${pors#$snipdir/}"
print -P -- "%F{38}zman plugin: %F{154}No README.md in the %F{220}${snippath}%F{154} snippet%f"
fi
}
if (( !${+opthash[-c]} )); then
local -a farray
farray=( ${^parray[@]}/(#i)README(#qN)
${^sarray[@]}/(#i)README(#qN)
)
if (( ${#farray} )); then
man ${farray[@]}
else
if (( ${#} && !${#parray} && !${#sarray} )); then
print -P -- "%F{38}zman plugin: %F{154}No such plugin(s) or snippet(s): %F{220}${(j:, :)@}%F{154}%f"
elif (( !${#} )); then
print -P -- "%F{38}zman plugin: %F{154}Argument required – a plugin or snippet name%f"
fi
fi
else
local -a farray
farray=( ${^parray[@]}/zsdoc/*.1(N) ${^sarray[@]}/zsdoc/*.1(N) )
if (( ${#farray} )); then
local fle
for fle (${farray[@]}) {
man "$fle"
sleep 2
}
else
if (( ${#} && !${#parray} && !${#sarray} )); then
print -P -- "%F{38}zman plugin: %F{154}No such plugin(s) or snippet(s) or no code documentation available for: %F{220}${(j:, :)@}%F{154}%f"
elif (( !${#} )); then
print -P -- "%F{38}zman plugin: %F{154}Argument required – a plugin or snippet name%f"
fi
fi
fi