]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/zsh-navigation-tools/n-cd
initial
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / zsh-navigation-tools / n-cd
1 # Copy this file into /usr/share/zsh/site-functions/
2 # and add 'autoload n-cd` to .zshrc
3 #
4 # This function allows to choose a directory from pushd stack
5 #
6 # Uses n-list
7
8 emulate -L zsh
9
10 setopt extendedglob pushdignoredups
11
12 zmodload zsh/curses
13 local IFS="
14 "
15
16 # Unset before configuration is read
17 unset NLIST_COLORING_PATTERN
18
19 [ -f ~/.config/znt/n-list.conf ] && builtin source ~/.config/znt/n-list.conf
20 [ -f ~/.config/znt/n-cd.conf ] && builtin source ~/.config/znt/n-cd.conf
21
22 local list
23 local selected
24
25 NLIST_REMEMBER_STATE=0
26
27 list=( `dirs -p` )
28 list=( "${(@M)list:#(#i)*$1*}" )
29
30 local NLIST_GREP_STRING="$1"
31
32 [ "$#list" -eq 0 ] && echo "No matching directories"
33
34 if [ "$#hotlist" -ge 1 ]; then
35     typeset -a NLIST_NONSELECTABLE_ELEMENTS NLIST_HOP_INDEXES
36     local tmp_list_size="$#list"
37     NLIST_NONSELECTABLE_ELEMENTS=( $(( tmp_list_size+1 )) $(( tmp_list_size+2 )) )
38     list=( "$list[@]" "" $'\x1b[00;31m'"Hotlist"$'\x1b[00;00m': "$hotlist[@]" )
39     (( tmp_list_size+=3 ))
40     local middle_hop=$(( (tmp_list_size+$#list) / 2 ))
41     [[ "$middle_hop" -eq $tmp_list_size || "$middle_hop" -eq $#list ]] && middle_hop=""
42     [ "$tmp_list_size" -eq $#list ] && tmp_list_size=""
43     NLIST_HOP_INDEXES=( 1 $tmp_list_size $middle_hop $#list )
44 else
45     [ "$#list" -eq 0 ] && return 1
46 fi
47
48 n-list "${list[@]}"
49
50 if [ "$REPLY" -gt 0 ]; then
51     selected="$reply[REPLY]"
52     selected="${selected/#\~/$HOME}"
53
54     (( NCD_DONT_PUSHD )) && setopt NO_AUTO_PUSHD
55     cd "$selected"
56     local code=$?
57     (( NCD_DONT_PUSHD )) && setopt AUTO_PUSHD
58
59     if [ "$code" -eq "0" ]; then
60         # ZLE?
61         if [ "${(t)CURSOR}" = "integer-local-special" ]; then
62             zle -M "You have selected $selected"
63         else
64             echo "You have selected $selected"
65         fi
66     fi
67 else
68     [ "${(t)CURSOR}" = "integer-local-special" ] && zle redisplay
69 fi
70
71 # vim: set filetype=zsh: