diff options
author | Hsieh Chin Fan <typebrook@gmail.com> | 2022-03-02 09:05:27 +0800 |
---|---|---|
committer | Hsieh Chin Fan <typebrook@gmail.com> | 2022-03-02 09:05:27 +0800 |
commit | 0f4b3e995c883dbc504b75156e9ca5e6ebf07db1 (patch) | |
tree | 58614127663617befbfaea58f3627e2f6eb30084 /zsh | |
parent | f425e159ea353939341bf1615ef2ea4c3c2ee7dc (diff) |
update
Diffstat (limited to 'zsh')
-rw-r--r-- | zsh/_hugo | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/zsh/_hugo b/zsh/_hugo new file mode 100644 index 0000000..b6ada8d --- /dev/null +++ b/zsh/_hugo | |||
@@ -0,0 +1,177 @@ | |||
1 | #compdef _hugo 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 | while IFS='\n' read -r comp; do | ||
90 | if [ -n "$comp" ]; then | ||
91 | # If requested, completions are returned with a description. | ||
92 | # The description is preceded by a TAB character. | ||
93 | # For zsh's _describe, we need to use a : instead of a TAB. | ||
94 | # We first need to escape any : as part of the completion itself. | ||
95 | comp=${comp//:/\\:} | ||
96 | |||
97 | local tab=$(printf '\t') | ||
98 | comp=${comp//$tab/:} | ||
99 | |||
100 | __hugo_debug "Adding completion: ${comp}" | ||
101 | completions+=${comp} | ||
102 | lastComp=$comp | ||
103 | fi | ||
104 | done < <(printf "%s\n" "${out[@]}") | ||
105 | |||
106 | if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then | ||
107 | __hugo_debug "Activating nospace." | ||
108 | noSpace="-S ''" | ||
109 | fi | ||
110 | |||
111 | if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then | ||
112 | # File extension filtering | ||
113 | local filteringCmd | ||
114 | filteringCmd='_files' | ||
115 | for filter in ${completions[@]}; do | ||
116 | if [ ${filter[1]} != '*' ]; then | ||
117 | # zsh requires a glob pattern to do file filtering | ||
118 | filter="\*.$filter" | ||
119 | fi | ||
120 | filteringCmd+=" -g $filter" | ||
121 | done | ||
122 | filteringCmd+=" ${flagPrefix}" | ||
123 | |||
124 | __hugo_debug "File filtering command: $filteringCmd" | ||
125 | _arguments '*:filename:'"$filteringCmd" | ||
126 | elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then | ||
127 | # File completion for directories only | ||
128 | local subDir | ||
129 | subdir="${completions[1]}" | ||
130 | if [ -n "$subdir" ]; then | ||
131 | __hugo_debug "Listing directories in $subdir" | ||
132 | pushd "${subdir}" >/dev/null 2>&1 | ||
133 | else | ||
134 | __hugo_debug "Listing directories in ." | ||
135 | fi | ||
136 | |||
137 | local result | ||
138 | _arguments '*:dirname:_files -/'" ${flagPrefix}" | ||
139 | result=$? | ||
140 | if [ -n "$subdir" ]; then | ||
141 | popd >/dev/null 2>&1 | ||
142 | fi | ||
143 | return $result | ||
144 | else | ||
145 | __hugo_debug "Calling _describe" | ||
146 | if eval _describe "completions" completions $flagPrefix $noSpace; then | ||
147 | __hugo_debug "_describe found some completions" | ||
148 | |||
149 | # Return the success of having called _describe | ||
150 | return 0 | ||
151 | else | ||
152 | __hugo_debug "_describe did not find completions." | ||
153 | __hugo_debug "Checking if we should do file completion." | ||
154 | if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then | ||
155 | __hugo_debug "deactivating file completion" | ||
156 | |||
157 | # We must return an error code here to let zsh know that there were no | ||
158 | # completions found by _describe; this is what will trigger other | ||
159 | # matching algorithms to attempt to find completions. | ||
160 | # For example zsh can match letters in the middle of words. | ||
161 | return 1 | ||
162 | else | ||
163 | # Perform file completion | ||
164 | __hugo_debug "Activating file completion" | ||
165 | |||
166 | # We must return the result of this command, so it must be the | ||
167 | # last command, or else we must store its result to return it. | ||
168 | _arguments '*:filename:_files'" ${flagPrefix}" | ||
169 | fi | ||
170 | fi | ||
171 | fi | ||
172 | } | ||
173 | |||
174 | # don't run the completion function when being source-ed or eval-ed | ||
175 | if [ "$funcstack[1]" = "_hugo" ]; then | ||
176 | _hugo | ||
177 | fi | ||