n-cd
1871 bytes
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
8emulate -L zsh
9
10setopt extendedglob pushdignoredups
11
12zmodload zsh/curses
13local IFS="
14"
15
16# Unset before configuration is read
17unset 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
22local list
23local selected
24
25NLIST_REMEMBER_STATE=0
26
27list=( `dirs -p` )
28list=( "${(@M)list:#(#i)*$1*}" )
29
30local NLIST_GREP_STRING="$1"
31
32[ "$#list" -eq 0 ] && echo "No matching directories"
33
34if [ "$#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 )
44else
45 [ "$#list" -eq 0 ] && return 1
46fi
47
48n-list "${list[@]}"
49
50if [ "$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
67else
68 [ "${(t)CURSOR}" = "integer-local-special" ] && zle redisplay
69fi
70
71# vim: set filetype=zsh: