c1c107cfb9669beeda258efed9a17cc4bddf7aa7

Author
Pavol Juhas <pavol.juhas@gmail.com>
Committer
Pavol Juhas <pavol.juhas@gmail.com>
Date

Message

Add scd plugin for smart change of directory.

Synced with the scd-tracker branch
pavoljuhas/oh-my-zsh@2f78243cad3509058d142aa4b70a604e75ec741e.

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/scd/README.md b/plugins/scd/README.md
  2index ea7c72464131ebd4e931d63dfab07647fb6680e3..197cea50a45df926f924e10c8cd6647d50b7a5e0 100644
  3--- a/plugins/scd/README.md
  4+++ b/plugins/scd/README.md
  5@@ -111,8 +111,7 @@ SCD_MEANLIFE</dt><dd>
  6 
  7 SCD_THRESHOLD</dt><dd>
  8     threshold for cumulative directory likelihood.  Directories with
  9-    lower likelihood are excluded unless they are the only match to
 10-    scd patterns.
 11+    a lower likelihood compared to the best match are excluded (0.005).
 12     </dd><dt>
 13 
 14 SCD_SCRIPT</dt><dd>
 15diff --git a/plugins/scd/scd b/plugins/scd/scd
 16index 9e055eadd4ec60c36be099a8320135fe1308e317..1567d2736cf8f4fb9582dc35dfc97b2bace9d0c9 100755
 17--- a/plugins/scd/scd
 18+++ b/plugins/scd/scd
 19@@ -1,10 +1,11 @@
 20 #!/bin/zsh -f
 21 
 22 emulate -L zsh
 23+local EXIT=return
 24 if [[ $(whence -w $0) == *:' 'command ]]; then
 25     emulate -R zsh
 26-    alias return=exit
 27     local RUNNING_AS_COMMAND=1
 28+    EXIT=exit
 29 fi
 30 
 31 local DOC='scd -- smart change to a recently used directory
 32@@ -37,8 +38,9 @@ local SCD_ALIAS=~/.scdalias.zsh
 33 local ICASE a d m p i tdir maxrank threshold
 34 local opt_help opt_add opt_unindex opt_recursive opt_verbose
 35 local opt_alias opt_unalias opt_list
 36-local -A drank dalias dkey
 37+local -A drank dalias
 38 local dmatching
 39+local last_directory
 40 
 41 setopt extendedhistory extendedglob noautonamedirs brace_ccl
 42 
 43@@ -56,11 +58,11 @@ zparseopts -D -- a=opt_add -add=opt_add -unindex=opt_unindex \
 44     r=opt_recursive -recursive=opt_recursive \
 45     -alias:=opt_alias -unalias=opt_unalias -list=opt_list \
 46     v=opt_verbose -verbose=opt_verbose h=opt_help -help=opt_help \
 47-    || return $?
 48+    || $EXIT $?
 49 
 50 if [[ -n $opt_help ]]; then
 51     print $DOC
 52-    return
 53+    $EXIT
 54 fi
 55 
 56 # load directory aliases if they exist
 57@@ -79,8 +81,8 @@ _scd_Y19oug_abspath() {
 58 # define directory alias
 59 if [[ -n $opt_alias ]]; then
 60     if [[ -n $1 && ! -d $1 ]]; then
 61-        print -u2 "'$1' is not a directory"
 62-        return 1
 63+        print -u2 "'$1' is not a directory."
 64+        $EXIT 1
 65     fi
 66     a=${opt_alias[-1]#=}
 67     _scd_Y19oug_abspath d ${1:-$PWD}
 68@@ -93,19 +95,19 @@ if [[ -n $opt_alias ]]; then
 69         hash -d -- $a=$d
 70         hash -dL >| $SCD_ALIAS
 71     )
 72-    return $?
 73+    $EXIT $?
 74 fi
 75 
 76 # undefine directory alias
 77 if [[ -n $opt_unalias ]]; then
 78     if [[ -n $1 && ! -d $1 ]]; then
 79-        print -u2 "'$1' is not a directory"
 80-        return 1
 81+        print -u2 "'$1' is not a directory."
 82+        $EXIT 1
 83     fi
 84     _scd_Y19oug_abspath a ${1:-$PWD}
 85     a=$(print -rD ${a})
 86     if [[ $a != [~][^/]## ]]; then
 87-        return
 88+        $EXIT
 89     fi
 90     a=${a#[~]}
 91     # unalias in the current shell, update alias file if successful
 92@@ -118,35 +120,39 @@ if [[ -n $opt_unalias ]]; then
 93             hash -dL >| $SCD_ALIAS
 94         )
 95     fi
 96-    return $?
 97+    $EXIT $?
 98 fi
 99 
100-# Rewrite the history file if it is at least 20% oversized
101+# Rewrite directory index if it is at least 20% oversized
102 if [[ -s $SCD_HISTFILE ]] && \
103 (( $(wc -l <$SCD_HISTFILE) > 1.2 * $SCD_HISTSIZE )); then
104     m=( ${(f)"$(<$SCD_HISTFILE)"} )
105     print -lr -- ${m[-$SCD_HISTSIZE,-1]} >| ${SCD_HISTFILE}
106 fi
107 
108+# Determine the last recorded directory
109+if [[ -s ${SCD_HISTFILE} ]]; then
110+    last_directory=${"$(tail -1 ${SCD_HISTFILE})"#*;}
111+fi
112+
113 # Internal functions are prefixed with "_scd_Y19oug_".
114-# The "record" function adds a non-repeating directory to the history
115-# and turns on history writing.
116+# The "record" function adds its arguments to the directory index.
117 _scd_Y19oug_record() {
118-    while [[ -n $1 && $1 == ${history[$HISTCMD]} ]]; do