diff options
author | Hsieh Chin Fan <typebrook@topo.tw> | 2023-01-04 18:06:20 +0800 |
---|---|---|
committer | Hsieh Chin Fan <typebrook@topo.tw> | 2023-01-04 18:06:20 +0800 |
commit | 79eb83861320702ecfcd5d3823c8e12eac874dc6 (patch) | |
tree | d703c8f0788465d1ba65ded72817a6bd7b407601 /zsh | |
parent | d474d957e5e73db003bfac68dd3a35395d2e7e55 (diff) |
Update
Diffstat (limited to 'zsh')
-rw-r--r-- | zsh/hugo.zsh | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/zsh/hugo.zsh b/zsh/hugo.zsh new file mode 100644 index 0000000..d94a87b --- /dev/null +++ b/zsh/hugo.zsh | |||
@@ -0,0 +1,205 @@ | |||
1 | #compdef hugo | ||
2 | |||
3 | # zsh completion for hugo -*- shell-script -*- | ||
4 | |||
5 | __hugo_debug() | ||
6 | { | ||
7 | local file="$BASH_COMP_DEBUG_FILE" | ||
8 | if [[ -n ${file} ]]; then | ||
9 | echo "$*" >> "${file}" | ||
10 | fi | ||
11 | } | ||
12 | |||
13 | _hugo() | ||
14 | { | ||
15 | local shellCompDirectiveError=1 | ||
16 | local shellCompDirectiveNoSpace=2 | ||
17 | local shellCompDirectiveNoFileComp=4 | ||
18 | local shellCompDirectiveFilterFileExt=8 | ||
19 | local shellCompDirectiveFilterDirs=16 | ||
20 | |||
21 | local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace | ||
22 | local -a completions | ||
23 | |||
24 | __hugo_debug "\n========= starting completion logic ==========" | ||
25 | __hugo_debug "CURRENT: ${CURRENT}, words[*]: ${words[*]}" | ||
26 | |||
27 | # The user could have moved the cursor backwards on the command-line. | ||
28 | # We need to trigger completion from the $CURRENT location, so we need | ||
29 | # to truncate the command-line ($words) up to the $CURRENT location. | ||
30 | # (We cannot use $CURSOR as its value does not work when a command is an alias.) | ||
31 | words=("${=words[1,CURRENT]}") | ||
32 | __hugo_debug "Truncated words[*]: ${words[*]}," | ||
33 | |||
34 | lastParam=${words[-1]} | ||
35 | lastChar=${lastParam[-1]} | ||
36 | __hugo_debug "lastParam: ${lastParam}, lastChar: ${lastChar}" | ||
37 | |||
38 | # For zsh, when completing a flag with an = (e.g., hugo -n=<TAB>) | ||
39 | # completions must be prefixed with the flag | ||
40 | setopt local_options BASH_REMATCH | ||
41 | if [[ "${lastParam}" =~ '-.*=' ]]; then | ||
42 | # We are dealing with a flag with an = | ||
43 | flagPrefix="-P ${BASH_REMATCH}" | ||
44 | fi | ||
45 | |||
46 | # Prepare the command to obtain completions | ||
47 | requestComp="${words[1]} __complete ${words[2,-1]}" | ||
48 | if [ "${lastChar}" = "" ]; then | ||
49 | # If the last parameter is complete (there is a space following it) | ||
50 | # We add an extra empty parameter so we can indicate this to the go completion code. | ||
51 | __hugo_debug "Adding extra empty parameter" | ||
52 | requestComp="${requestComp} \"\"" | ||
53 | fi | ||
54 | |||
55 | __hugo_debug "About to call: eval ${requestComp}" | ||
56 | |||
57 | # Use eval to handle any environment variables and such | ||
58 | out=$(eval ${requestComp} 2>/dev/null) | ||
59 | __hugo_debug "completion output: ${out}" | ||
60 | |||
61 | # Extract the directive integer following a : from the last line | ||
62 | local lastLine | ||
63 | while IFS='\n' read -r line; do | ||
64 | lastLine=${line} | ||
65 | done < <(printf "%s\n" "${out[@]}") | ||
66 | __hugo_debug "last line: ${lastLine}" | ||
67 | |||
68 | if [ "${lastLine[1]}" = : ]; then | ||
69 | directive=${lastLine[2,-1]} | ||
70 | # Remove the directive including the : and the newline | ||
71 | local suffix | ||
72 | (( suffix=${#lastLine}+2)) | ||
73 | out=${out[1,-$suffix]} | ||
74 | else | ||
75 | # There is no directive specified. Leave $out as is. | ||
76 | __hugo_debug "No directive found. Setting do default" | ||
77 | directive=0 | ||
78 | fi | ||
79 | |||
80 | __hugo_debug "directive: ${directive}" | ||
81 | __hugo_debug "completions: ${out}" | ||
82 | __hugo_debug "flagPrefix: ${flagPrefix}" | ||
83 | |||
84 | if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then | ||
85 | __hugo_debug "Completion received error. Ignoring completions." | ||
86 | return | ||
87 | fi | ||
88 | |||
89 | local activeHelpMarker="_activeHelp_ " | ||
90 | local endIndex=${#activeHelpMarker} | ||
91 | local startIndex=$((${#activeHelpMarker}+1)) | ||
92 | local hasActiveHelp=0 | ||
93 | while IFS='\n' read -r comp; do | ||
94 | # Check if this is an activeHelp statement (i.e., prefixed with $activeHelpMarker) | ||
95 | if [ "${comp[1,$endIndex]}" = "$activeHelpMarker" ];then | ||
96 | __hugo_debug "ActiveHelp found: $comp" | ||
97 | comp="${comp[$startIndex,-1]}" | ||
98 | if [ -n "$comp" ]; then | ||
99 | compadd -x "${comp}" | ||
100 | __hugo_debug "ActiveHelp will need delimiter" | ||
101 | hasActiveHelp=1 | ||
102 | fi | ||
103 | |||
104 | continue | ||
105 | fi | ||
106 | |||
107 | if [ -n "$comp" ]; then | ||
108 | # If requested, completions are returned with a description. | ||
109 | # The description is preceded by a TAB character. | ||
110 | # For zsh's _describe, we need to use a : instead of a TAB. | ||
111 | # We first need to escape any : as part of the completion itself. | ||
112 | comp=${comp//:/\\:} | ||
113 | |||
114 | local tab="$(printf '\t')" | ||
115 | comp=${comp//$tab/:} | ||
116 | |||
117 | __hugo_debug "Adding completion: ${comp}" | ||
118 | completions+=${comp} | ||
119 | lastComp=$comp | ||
120 | fi | ||
121 | done < <(printf "%s\n" "${out[@]}") | ||
122 | |||
123 | # Add a delimiter after the activeHelp statements, but only if: | ||
124 | # - there are completions following the activeHelp statements, or | ||
125 | # - file completion will be performed (so there will be choices after the activeHelp) | ||
126 | if [ $hasActiveHelp -eq 1 ]; then | ||
127 | if [ ${#completions} -ne 0 ] || [ $((directive & shellCompDirectiveNoFileComp)) -eq 0 ]; then | ||
128 | __hugo_debug "Adding activeHelp delimiter" | ||
129 | compadd -x "--" | ||
130 | hasActiveHelp=0 | ||
131 | fi | ||
132 | fi | ||
133 | |||
134 | if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then | ||
135 | __hugo_debug "Activating nospace." | ||
136 | noSpace="-S ''" | ||
137 | fi | ||
138 | |||
139 | if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then | ||
140 | # File extension filtering | ||
141 | local filteringCmd | ||
142 | filteringCmd='_files' | ||
143 | for filter in ${completions[@]}; do | ||
144 | if [ ${filter[1]} != '*' ]; then | ||
145 | # zsh requires a glob pattern to do file filtering | ||
146 | filter="\*.$filter" | ||
147 | fi | ||
148 | filteringCmd+=" -g $filter" | ||
149 | done | ||
150 | filteringCmd+=" ${flagPrefix}" | ||
151 | |||
152 | __hugo_debug "File filtering command: $filteringCmd" | ||
153 | _arguments '*:filename:'"$filteringCmd" | ||
154 | elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then | ||
155 | # File completion for directories only | ||
156 | local subdir | ||
157 | subdir="${completions[1]}" | ||
158 | if [ -n "$subdir" ]; then | ||
159 | __hugo_debug "Listing directories in $subdir" | ||
160 | pushd "${subdir}" >/dev/null 2>&1 | ||
161 | else | ||
162 | __hugo_debug "Listing directories in ." | ||
163 | fi | ||
164 | |||
165 | local result | ||
166 | _arguments '*:dirname:_files -/'" ${flagPrefix}" | ||
167 | result=$? | ||
168 | if [ -n "$subdir" ]; then | ||
169 | popd >/dev/null 2>&1 | ||
170 | fi | ||
171 | return $result | ||
172 | else | ||
173 | __hugo_debug "Calling _describe" | ||
174 | if eval _describe "completions" completions $flagPrefix $noSpace; then | ||
175 | __hugo_debug "_describe found some completions" | ||
176 | |||
177 | # Return the success of having called _describe | ||
178 | return 0 | ||
179 | else | ||
180 | __hugo_debug "_describe did not find completions." | ||
181 | __hugo_debug "Checking if we should do file completion." | ||
182 | if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then | ||
183 | __hugo_debug "deactivating file completion" | ||
184 | |||
185 | # We must return an error code here to let zsh know that there were no | ||
186 | # completions found by _describe; this is what will trigger other | ||
187 | # matching algorithms to attempt to find completions. | ||
188 | # For example zsh can match letters in the middle of words. | ||
189 | return 1 | ||
190 | else | ||
191 | # Perform file completion | ||
192 | __hugo_debug "Activating file completion" | ||
193 | |||
194 | # We must return the result of this command, so it must be the | ||
195 | # last command, or else we must store its result to return it. | ||
196 | _arguments '*:filename:_files'" ${flagPrefix}" | ||
197 | fi | ||
198 | fi | ||
199 | fi | ||
200 | } | ||
201 | |||
202 | # don't run the completion function when being source-ed or eval-ed | ||
203 | if [ "$funcstack[1]" = "_hugo" ]; then | ||
204 | _hugo | ||
205 | fi | ||