8d08f1634a7b9782e3722ce770e8630f569afe3f
- Author
- Pavol Juhas <pavol.juhas@gmail.com>
- Committer
- GitHub <noreply@github.com>
- Date
Message
Diff
This diff is truncated to protect this page.
1diff --git a/plugins/scd/README.md b/plugins/scd/README.md
2index 8c156da1f3386a637afcaea00c16f1bfd7527e18..d8535f9f22e06d4d0e4a1eb2ffb791dd6e338a07 100644
3--- a/plugins/scd/README.md
4+++ b/plugins/scd/README.md
5@@ -14,8 +14,9 @@ directory aliases, which appear as named directories in zsh session.
6 ## INSTALLATION NOTES
7
8 Besides oh-my-zsh, `scd` can be used with *bash*, *dash* or *tcsh*
9-shells and is also available as [Vim](https://www.vim.org/) plugin and
10-[IPython](https://ipython.org/) extension. For installation details, see
11+shells and is also available as Vim plugin
12+[scd.vim](https://github.com/pavoljuhas/scd.vim) and
13+[IPython](https://ipython.org) extension. For installation details, see
14 https://github.com/pavoljuhas/smart-change-directory.
15
16 ## SYNOPSIS
17@@ -24,11 +25,31 @@ https://github.com/pavoljuhas/smart-change-directory.
18 scd [options] [pattern1 pattern2 ...]
19 ```
20
21+## PATTERNS
22+
23+Patterns may use all zsh [glob operators](
24+http://zsh.sourceforge.net/Doc/Release/Expansion.html#Glob-Operators)
25+available with *extendedglob* option. Specified patterns must match
26+the absolute path and at least one of them must match in the tail.
27+Several special patterns are also recognized as follows:
28+
29+<dl><dt>
30+^PAT</dt><dd>
31+ PAT must match at the beginning of the path, for example, "^/home"</dd><dt>
32+PAT$</dt><dd>
33+ require PAT to match the end of the path, "man$"</dd><dt>
34+./</dt><dd>
35+ match only subdirectories of the current directory</dd><dt>
36+:PAT</dt><dd>
37+ require PAT to match over the tail component, ":doc", ":re/doc"</dd>
38+</dl>
39+
40+
41 ## OPTIONS
42
43 <dl><dt>
44 -a, --add</dt><dd>
45- add specified directories to the directory index.</dd><dt>
46+ add current or specified directories to the directory index.</dd><dt>
47
48 --unindex</dt><dd>
49 remove current or specified directories from the index.</dd><dt>
50@@ -42,11 +63,16 @@ scd [options] [pattern1 pattern2 ...]
51
52 --unalias</dt><dd>
53 remove ALIAS definition for the current or specified directory from
54- <em>~/.scdalias.zsh</em>.</dd><dt>
55+ <em>~/.scdalias.zsh</em>. Use "OLD" to purge aliases to non-existent
56+ directories.</dd><dt>
57
58 -A, --all</dt><dd>
59- include all matching directories. Disregard matching by directory
60- alias and filtering of less likely paths.</dd><dt>
61+ display all directories even those excluded by patterns in
62+ <em>~/.scdignore</em>. Disregard the unique matching for a
63+ directory alias and filtering of less likely paths.</dd><dt>
64+
65+-p, --push</dt><dd>
66+ use "pushd" to change to the target directory.</dd><dt>
67
68 --list</dt><dd>
69 show matching directories and exit.</dd><dt>
70@@ -58,6 +84,7 @@ scd [options] [pattern1 pattern2 ...]
71 display this options summary and exit.</dd>
72 </dl>
73
74+
75 ## Examples
76
77 ```sh
78@@ -83,17 +110,26 @@ scd --alias=xray
79 scd xray
80 ```
81
82-# FILES
83+## FILES
84
85 <dl><dt>
86 ~/.scdhistory</dt><dd>
87 time-stamped index of visited directories.</dd><dt>
88
89 ~/.scdalias.zsh</dt><dd>
90- scd-generated definitions of directory aliases.</dd>
91+ scd-generated definitions of directory aliases.</dd><dt>
92+
93+~/.scdignore</dt><dd>
94+ <a href="http://zsh.sourceforge.net/Doc/Release/Expansion.html#Glob-Operators">
95+ glob patterns</a> for paths to be ignored in the scd search, for example,
96+ <code>/mnt/backup/*</code>. The patterns are specified one per line
97+ and are matched assuming the <em>extendedglob</em> zsh option. Lines
98+ starting with "#" are skipped as comments. The .scdignore patterns
99+ are not applied in the <em>--all</em> mode.</dd>
100 </dl>
101
102-# ENVIRONMENT
103+
104+## ENVIRONMENT
105diff --git a/plugins/scd/_scd b/plugins/scd/_scd
106new file mode 100644
107index 0000000000000000000000000000000000000000..39c7fa463f7e1e559c86aa43d8a6d7811a12a3e1
108--- /dev/null
109+++ b/plugins/scd/_scd
110@@ -0,0 +1,60 @@
111+#compdef scd
112+#description smart change directory
113+
114+local curcontext="$curcontext" state line expl ret=1
115+typeset -A opt_args
116+
117+local -a indexopts myargs
118+indexopts=( --add -a --unindex )
119+
120+myargs=(
121+ # common options
122+ "(--help -h)"{--help,-h}"[print help and exit]"
123+
124+ # options for manipulating directory index
125+ - index
126+ "(--recursive -r)"{--recursive,-r}"[use recursive --add or --unindex]"
127+ "($indexopts)"{--add,-a}"[add specified directories to the index]"
128+ "($indexopts)--unindex[remove specified directories from the index]"
129+ "*:directory:{ (( ${words[(I)-a|--add|--unindex]} )) && _path_files -/ }"
130+
131+ # define new directory alias
132+ - alias
133+ "--alias=[create alias for this or given directory]:directory-alias:()"
134+ '1:directory:{ (( words[(I)--alias*] )) && _path_files -/ }'
135+
136+ # remove definition of directory alias
137+ - unalias
138+ "--unalias[remove definition of directory alias]"
139+ "*::directory alias:->scd-alias-target"
140+
141+ # act on the directory change
142+ - scd
143+ "(--all -A)"{--all,-A}"[include less likely and ignored paths]"
144+ "--list[print matching directories and exit]"
145+ "(--verbose -v)"{--verbose,-v}"[show directory ranking and full paths]"
146+ "(--push -p)"{--push,-p}"[change directory with 'pushd']"
147+ "1::directory alias:->scd-alias-target"
148+ "*:patterns:()"
149+)
150+
151+_arguments -S -C $myargs && ret=0
152+
153+
154+if [[ "$state" == scd-alias-target && -s ~/.scdalias.zsh ]]; then
155+ local -a scdaliases
156+ scdaliases=( )
157+ eval "$(setopt extendedglob
158+ phome="(#b)(#s)${HOME}(/*)#(#e)"
159+ builtin hash -dr
160+ source ~/.scdalias.zsh &&
161+ for k v in ${(kv)nameddirs}; do
162+ scdaliases+=( $k:${v/${~phome}/"~"${match[1]}} )
163+ done
164+ complete_unalias=${+opt_args[unalias---unalias]}
165+ if (( complete_unalias && ! ${+nameddirs[OLD]} )); then
166+ scdaliases+=( 'OLD:all aliases to non-existent paths' )
167+ fi
168+ typeset -p scdaliases )"
169+ _describe -t scdaliases scdalias scdaliases
170+fi
171diff --git a/plugins/scd/scd b/plugins/scd/scd
172old mode 100644
173new mode 100755
174index 39b28237d11809cbd8f81c4a86bbdc85260903fa..a7db6c265b935a8f5560fc9d9ad062dc39bacc67
175--- a/plugins/scd/scd
176+++ b/plugins/scd/scd
177@@ -1,29 +1,39 @@
178 #!/bin/zsh -f
179
180 emulate -L zsh
181+
182+local RUNNING_AS_COMMAND=
183 local EXIT=return
184 if [[ $(whence -w $0) == *:' 'command ]]; then
185- emulate -R zsh
186- local RUNNING_AS_COMMAND=1
187+ RUNNING_AS_COMMAND=1
188 EXIT=exit
189 fi
190
191 local DOC='scd -- smart change to a recently used directory
192 usage: scd [options] [pattern1 pattern2 ...]
193-Go to a directory path that contains all fixed string patterns. Prefer
194-recent or frequently visited directories as found in the directory index.
195+Go to a directory path that matches all patterns. Prefer recent or
196+frequently visited directories as found in the directory index.
197 Display a selection menu in case of multiple matches.
198
199+Special patterns:
200+ ^PAT match at the path root, "^/home"
201+ PAT$ match paths ending with PAT, "man$"
202+ ./ match paths under the current directory
203+ :PAT require PAT to span the tail, ":doc", ":re/doc"
204+
205 Options:
206- -a, --add add specified directories to the directory index.
207+ -a, --add add current or specified directories to the index.
208 --unindex remove current or specified directories from the index.
209 -r, --recursive apply options --add or --unindex recursively.
210 --alias=ALIAS create alias for the current or specified directory and
211 store it in ~/.scdalias.zsh.
212 --unalias remove ALIAS definition for the current or specified
213 directory from ~/.scdalias.zsh.
214- -A, --all include all matching directories. Disregard matching by
215- directory alias and filtering of less likely paths.
216+ Use "OLD" to purge aliases to non-existent directories.
217+ -A, --all display all directories even those excluded by patterns
218+ in ~/.scdignore. Disregard unique match for a directory
219+ alias and filtering of less likely paths.
220+ -p, --push use "pushd" to change to the target directory.
221 --list show matching directories and exit.
222 -v, --verbose display directory rank in the selection menu.
223 -h, --help display this message and exit.
224@@ -36,18 +46,28 @@ local SCD_MEANLIFE=${SCD_MEANLIFE:-86400}
225 local SCD_THRESHOLD=${SCD_THRESHOLD:-0.005}
226 local SCD_SCRIPT=${RUNNING_AS_COMMAND:+$SCD_SCRIPT}
227 local SCD_ALIAS=~/.scdalias.zsh
228+local SCD_IGNORE=~/.scdignore
229
230-local ICASE a d m p i maxrank threshold
231+# Minimum logarithm of probability. Avoids out of range warning in exp().
232+local -r MINLOGPROB=-15
233+
234+# When false, use case-insensitive globbing to fix PWD capitalization.
235+local PWDCASECORRECT=true
236+if [[ ${OSTYPE} == darwin* ]]; then
237+ PWDCASECORRECT=false
238+fi
239+
240+local a d m p i maxrank threshold
241 local opt_help opt_add opt_unindex opt_recursive opt_verbose
242-local opt_alias opt_unalias opt_all opt_list
243-local -A drank dalias
244+local opt_alias opt_unalias opt_all opt_push opt_list
245+local -A drank dalias scdignore
246 local dmatching
247 local last_directory
248
249-setopt extendedhistory extendedglob noautonamedirs brace_ccl
250+setopt extendedglob noautonamedirs brace_ccl
251
252-# If SCD_SCRIPT is defined make sure the file exists and is empty.
253-# This removes any previous old commands.
254+# If SCD_SCRIPT is defined make sure that that file exists and is empty.
255+# This removes any old previous commands from the SCD_SCRIPT file.
256 [[ -n "$SCD_SCRIPT" ]] && [[ -s $SCD_SCRIPT || ! -f $SCD_SCRIPT ]] && (
257 umask 077
258 : >| $SCD_SCRIPT
259@@ -56,13 +76,17 @@ setopt extendedhistory extendedglob noautonamedirs brace_ccl
260 # process command line options
261 zmodload -i zsh/zutil
262 zmodload -i zsh/datetime
263-zparseopts -D -- a=opt_add -add=opt_add -unindex=opt_unindex \
264+zmodload -i zsh/parameter
265+zparseopts -D -E -- a=opt_add -add=opt_add -unindex=opt_unindex \
266 r=opt_recursive -recursive=opt_recursive \
267 -alias:=opt_alias -unalias=opt_unalias \
268- A=opt_all -all=opt_all -list=opt_list \
269+ A=opt_all -all=opt_all p=opt_push -push=opt_push -list=opt_list \
270 v=opt_verbose -verbose=opt_verbose h=opt_help -help=opt_help \
271 || $EXIT $?
272
273+# remove the first instance of "--" from positional arguments
274+argv[(i)--]=( )
275+
276 if [[ -n $opt_help ]]; then
277diff --git a/plugins/scd/scd.plugin.zsh b/plugins/scd/scd.plugin.zsh
278index 0197c53a170227363c085d1a5fc585d5be92af11..1a6c186542d025afcde8c9c8b3ef5ed2de999c62 100644
279--- a/plugins/scd/scd.plugin.zsh
280+++ b/plugins/scd/scd.plugin.zsh
281@@ -1,19 +1,17 @@
282 ## The scd script should autoload as a shell function.
283-autoload scd
284+autoload -Uz scd
285
286
287 ## If the scd function exists, define a change-directory-hook function
288 ## to record visited directories in the scd index.
289 if [[ ${+functions[scd]} == 1 ]]; then
290- scd_chpwd_hook() { scd --add $PWD }
291- autoload add-zsh-hook
292- add-zsh-hook chpwd scd_chpwd_hook
293+ chpwd_scd() { scd --add $PWD }
294+ autoload -Uz add-zsh-hook
295+ add-zsh-hook chpwd chpwd_scd
296 fi
297
298
299-## Allow scd usage with unquoted wildcard characters such as "*" or "?".
300-alias scd='noglob scd'
301-
302-
303 ## Load the directory aliases created by scd if any.
304-if [[ -s ~/.scdalias.zsh ]]; then source ~/.scdalias.zsh; fi
305+if [[ -s ~/.scdalias.zsh ]]; then
306+ source ~/.scdalias.zsh
307+fi