-
Notifications
You must be signed in to change notification settings - Fork 2
/
zman
executable file
·137 lines (119 loc) · 4.24 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/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://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
0="${${(M)0##/*}:-$PWD/$0}"
fpath+=( "$0:h" )
fi
autoload -Uz za-man-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
-zinit-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 zinit format?
if [[ "$1" = "%"* ]]; then
reply=( "%" "${${${1/\%HOME/$HOME}/\%SNIPPETS/${ZINIT[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: 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
-zinit-any-to-user-plugin "${pors:t}"
# local type="$1" user="$2" plugin="$3" id_as="$4" dir="$5"
za-man-handler plugin "${reply[-2]}" "${reply[-1]}" \
id_as="${${pors:t}//---//}" "$pors"
elif [[ ! -f ''(#i)$pors/README.md(#qN[1]) ]]; then
-zinit-any-to-user-plugin "${pors:t}"
print -P -- "%F{38}zman z-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-man-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 z-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 z-plugin: %F{154}No such plugin(s) or snippet(s): \
%F{220}${(j:, :)@}%F{154}%f"
elif (( !${#} )); then
print -P -- "%F{38}zman z-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 z-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 z-plugin: %F{154}Argument required – a plugin or snippet name%f"
fi
fi
fi
# vim:ft=zsh:sw=4:sts=4:et