forked from u-root/u-root
/
narrow.elv.go
128 lines (116 loc) · 3.72 KB
/
narrow.elv.go
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
package bundled
const narrowElv = `
# Implementation of location, history and lastcmd mode using the new
# -narrow-read mode. One advantage of this is that it allows the
# execution of arbitrary hooks before or after each mode.
#
# Usage:
# use narrow
# narrow:bind-trigger-keys
#
# narrow:bind-trigger-keys binds keys for location, history and lastcmd
# modes. Without options, it uses the default bindings (same as the
# default bindings for edit:location, edit:history and edit:lastcmd),
# but different keys can be specified with the options. To disable a
# binding, specify its key as "".
# Example:
# narrow:bind-trigger-keys &location=Alt-l &lastcmd=""
# Hooks
# Each hook variable is an array which must contain lambdas, all of
# which will be executed in sequence before and after the
# corresponding mode.
# Example (list the new directory after switching to it in location mode):
# narrow:after-location = [ $@narrow:after-location { edit:insert-at-dot "ls"; edit:smart-enter } ]
before-location = []
after-location = []
before-history = []
after-history = []
before-lastcmd = []
after-lastcmd = []
fn location {
for hook $before-location { $hook }
candidates = [(dir-history | each [arg]{
score = (splits . $arg[score] | take 1)
put [
&content=$arg[path]
&display=$score" "(tilde-abbr $arg[path])
]
})]
edit:-narrow-read {
put $@candidates
} [arg]{
cd $arg[content]
for hook $after-location { $hook }
} &modeline="[narrow] Location " &ignore-case=$true
}
fn history {
for hook $before-history { $hook }
candidates = [(edit:command-history | each [arg]{
put [
&content=$arg[cmd]
&display=$arg[id]" "$arg[cmd]
]
})]
edit:-narrow-read {
put $@candidates
} [arg]{
edit:replace-input $arg[content]
for hook $after-history { $hook }
} &modeline="[narrow] History " &keep-bottom=$true &ignore-case=$true
}
fn lastcmd {
for hook $before-lastcmd { $hook }
last = (edit:command-history -1)
cmd = [
&content=$last[cmd]
&display="M-1 "$last[cmd]
&filter-text=""
]
index = 0
candidates = [$cmd ( edit:wordify $last[cmd] | each [arg]{
put [
&content=$arg
&display=$index" "$arg
&filter-text=$index
]
index = (+ $index 1)
})]
edit:-narrow-read {
put $@candidates
} [arg]{
edit:insert-at-dot $arg[content]
for hook $after-lastcmd { $hook }
} &modeline="[narrow] Lastcmd " &auto-commit=$true &bindings=[&M-1={ edit:narrow:accept-close }] &ignore-case=$true
}
fn -bind-insert [k f]{
edit:insert:binding[$k] = $f
}
fn -bind [k f]{
edit:narrow:binding[$k] = $f
}
# Bind keys for location, history and lastcmd modes. Without
# options, it uses the default bindings, but different keys
# can be specified with the options. To disable a binding,
# specify its key as "".
# Example:
# narrow:bind-trigger-keys &location=Alt-l &lastcmd=""
fn bind-trigger-keys [&location=C-l &history=C-r &lastcmd=M-1]{
if (not-eq $location "") { -bind-insert $location $location~ }
if (not-eq $history "") { -bind-insert $history $history~ }
if (not-eq $lastcmd "") { -bind-insert $lastcmd $lastcmd~ }
}
# Set up some default useful bindings for narrow mode
-bind Up $edit:narrow:up~
-bind PageUp $edit:narrow:page-up~
-bind Down $edit:narrow:down~
-bind PageDown $edit:narrow:page-down~
-bind Tab $edit:narrow:down-cycle~
-bind S-Tab $edit:narrow:up-cycle~
-bind Backspace $edit:narrow:backspace~
-bind Enter $edit:narrow:accept-close~
-bind M-Enter $edit:narrow:accept~
-bind Default $edit:narrow:default~
-bind "C-[" $edit:insert:start~
-bind C-G $edit:narrow:toggle-ignore-case~
-bind C-D $edit:narrow:toggle-ignore-duplication~
`