-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirstax.plugin.zsh
109 lines (94 loc) · 2.47 KB
/
dirstax.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env zsh
# Entrypoint
.dirstax.init() {
.dirstax.prerequisites
.dirstax.settings
.dirstax.setup_widgets
.dirstax.bind_widgets
.dirstax.setup_hook
}
.dirstax.prerequisites() {
setopt AUTO_PUSHD
setopt NO_PUSHD_IGNORE_DUPS
}
.dirstax.settings() {
# Don't initialize with `=()` to avoid overriding user-defined key bindings
typeset -Ax dirstax
if [[ "$(uname -s)" == 'Darwin' ]]; then
: ${dirstax[keybind_upward]:='^[[1;9A'} # ⌘ + ↑
: ${dirstax[keybind_forward]:='^[[1;9C'} # ⌘ + →
: ${dirstax[keybind_backward]:='^[[1;9D'} # ⌘ + ←
else
: ${dirstax[keybind_upward]:='^[[1;3A'} # alt + ↑
: ${dirstax[keybind_forward]:='^[[1;3C'} # alt + →
: ${dirstax[keybind_backward]:='^[[1;3D'} # alt + ←
fi
# for internal
dirstax[_backtracks]='0'
dirstax[_status]='idle'
}
.dirstax.setup_widgets() {
dirstax-cd-upward() {
zle push-line-or-edit
dirstax[_status]='cd-upward'
# `pushd -q` does not trigger the `chpwd` hook
cd ..
zle accept-line
}
dirstax-cd-forward() {
(( dirstax[_backtracks] == 0 )) && return 1
zle push-line-or-edit
dirstax[_status]='cd-forward'
if [[ ${options[PUSHD_MINUS]} == 'off' ]]; then
pushd -0 >/dev/null 2>&1
else
pushd +0 >/dev/null 2>&1
fi
zle accept-line
}
dirstax-cd-backward() {
(( dirstax[_backtracks] == ${#dirstack} )) && return 1
zle push-line-or-edit
dirstax[_status]='cd-backward'
if [[ ${options[PUSHD_MINUS]} == 'off' ]]; then
pushd +1 >/dev/null 2>&1
else
pushd -1 >/dev/null 2>&1
fi
zle accept-line
}
# register
zle -N dirstax-cd-upward
zle -N dirstax-cd-forward
zle -N dirstax-cd-backward
}
.dirstax.bind_widgets() {
bindkey "${dirstax[keybind_upward]}" dirstax-cd-upward
bindkey "${dirstax[keybind_forward]}" dirstax-cd-forward
bindkey "${dirstax[keybind_backward]}" dirstax-cd-backward
}
.dirstax.setup_hook() {
autoload -Uz add-zsh-hook
dirstax-drop-dirstack-forward-history() {
case "${dirstax[_status]}" in
cd-backward)
(( dirstax[_backtracks]++ )) ;;
cd-forward)
(( dirstax[_backtracks]-- )) ;;
idle|cd-upward)
if (( ${dirstax[_backtracks]} > 0 )); then
dirstack=("${dirstack[@]:: -${dirstax[_backtracks]}}")
fi
dirstax[_backtracks]='0'
;;
*)
print >/dev/stderr 'dirstax:' \
'`${dirstax[_status]}` must be `idle`, `cd-upward`, `cd-backward` or `cd-forward`.'
;;
esac
dirstax[_status]='idle'
}
# register hook
add-zsh-hook chpwd dirstax-drop-dirstack-forward-history
}
.dirstax.init