-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdir.elv
141 lines (119 loc) · 2.53 KB
/
dir.elv
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use builtin
var before-chooser = []
var after-chooser = []
var max-stack-size = 100
var -dirstack = [ $pwd ]
var -cursor = (- (count $-dirstack) 1)
fn -trimstack {
set -dirstack = $-dirstack[0..(+ $-cursor 1)]
}
var print-message = {|msg|
echo $msg > /dev/tty
}
fn stack { put $@-dirstack }
fn stacksize { count $-dirstack }
fn history {
for index [(range 0 (stacksize))] {
if (== $index $-cursor) {
echo (styled "* "$-dirstack[$index] green)
} else {
echo " "$-dirstack[$index]
}
}
}
fn curdir {
if (> (stacksize) 0) {
put $-dirstack[$-cursor]
} else {
put ""
}
}
fn push {
if (or (== (stacksize) 0) (!=s $pwd (curdir))) {
set -dirstack = [ (all $-dirstack[0..(+ $-cursor 1)]) $pwd ]
if (> (stacksize) $max-stack-size) {
set -dirstack = $-dirstack[(- $max-stack-size)..]
}
set -cursor = (- (stacksize) 1)
}
}
fn back {
if (> $-cursor 0) {
set -cursor = (- $-cursor 1)
builtin:cd $-dirstack[$-cursor]
} else {
$print-message "Beginning of directory history!"
}
}
fn forward {
if (< $-cursor (- (stacksize) 1)) {
set -cursor = (+ $-cursor 1)
builtin:cd $-dirstack[$-cursor]
} else {
$print-message "End of directory history!"
}
}
fn pop {
if (> $-cursor 0) {
back
-trimstack
} else {
$print-message "No previous directory to pop!"
}
}
fn cd {|@dir|
if (and (== (count $dir) 1) (eq $dir[0] "-")) {
builtin:cd $-dirstack[(- $-cursor 1)]
} else {
builtin:cd $@dir
}
}
fn cdb {|p| cd (dirname $p) }
fn left-word-or-prev-dir {
if (> (count $edit:current-command) 0) {
edit:move-dot-left-word
} else {
back
}
}
fn right-word-or-next-dir {
if (> (count $edit:current-command) 0) {
edit:move-dot-right-word
} else {
forward
}
}
fn left-small-word-or-prev-dir {
if (> (count $edit:current-command) 0) {
edit:move-dot-left-small-word
} else {
back
}
}
fn right-small-word-or-next-dir {
if (> (count $edit:current-command) 0) {
edit:move-dot-right-small-word
} else {
forward
}
}
fn history-chooser {
for hook $before-chooser { $hook }
var index = 0
var candidates = [(each {|arg|
put [
&to-accept=$arg
&to-show=$index" "$arg
&to-filter=$index" "$arg
]
set index = (to-string (+ $index 1))
} $-dirstack)]
edit:listing:start-custom $candidates &caption="Dir history " &accept={|arg|
builtin:cd $arg
for hook $after-chooser { $hook }
}
}
fn init {
set after-chdir = [ $@after-chdir {|dir| push } ]
}
init