-
Notifications
You must be signed in to change notification settings - Fork 20
/
alias.elv
118 lines (100 loc) · 2.66 KB
/
alias.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
# DO NOT EDIT THIS FILE DIRECTLY
# This is a file generated from a literate programing source file located at
# https://github.com/zzamboni/elvish-modules/blob/master/alias.org.
# You should make any changes there and regenerate it from Emacs org-mode using C-c C-v t
use re
use str
var dir = ~/.elvish/aliases
var arg-replacer = '{}'
var aliases = [&]
fn -define-alias {|name body|
eval $body
set aliases[$name] = $body
}
fn -load-alias {|name file|
var body = (slurp < $file)
-define-alias $name $body
}
fn -save {|&verbose=$false name|
if (has-key $aliases $name) {
var tmp-file = (mktemp $dir/tmp.XXXXXXXXXX)
var file = $dir/$name.elv
echo $aliases[$name] > $tmp-file
e:mv $tmp-file $file
if $verbose {
echo (styled "Alias "$name" saved to "$file"." green)
}
} else {
echo (styled "Alias "$name" is not defined." red)
}
}
fn save {|&verbose=$false &all=$false @names|
if $all {
set names = [(keys $aliases)]
}
each {|n|
-save &verbose=$verbose $n
} $names
}
fn def {|&verbose=$false &save=$false &use=[] name @cmd|
var use-statements = [(each {|m| put "use "$m";" } $use)]
var args-at-end = '$@_args'
var new-cmd = [
(each {|e|
if (eq $e $arg-replacer) {
put '$@_args'
set args-at-end = ''
} else {
repr $e
}
} $cmd)
]
var body = ({
echo "#alias:new" $name (if (not-eq $use []) { put "&use="(to-string $use) }) (each {|w| repr $w } $cmd)
print "edit:add-var "$name'~ {|@_args| ' $@use-statements $@new-cmd $args-at-end '}'
} | slurp)
-define-alias $name $body
if $save {
save $name
}
if $verbose {
echo (styled "Alias "$name" defined"(if $save { echo " and saved" } else { echo "" })"." green)
}
}
var new~ = $def~
fn bash-alias {|@args|
var line = $@args
var name cmd = (str:split &max=2 '=' $line)
def $name $cmd
}
fn list {
keys $aliases | each {|n|
echo (re:find '^#(alias:new .*)\n' $aliases[$n])[groups][1][text]
}
}
var ls~ = $list~ # ls is an alias for list
fn undef {|name|
if (has-key $aliases $name) {
var file = $dir/$name.elv
e:rm -f $file
del aliases[$name]
edit:add-var $name"~" (external $name)
echo (styled "Alias "$name" removed." green)
} else {
echo (styled "Alias "$name" does not exist." red)
}
}
var rm~ = $undef~ # rm is an alias for undef
fn init {
if (not ?(test -d $dir)) {
mkdir -p $dir
}
for file [(set _ = ?(put $dir/*.elv))] {
var content = (cat $file | slurp)
if (re:match '^#alias:new ' $content) {
var name cmd = (re:find '^#alias:new (\S+)\s+(.*)\n' $content)[groups][1 2][text]
def $name (edit:wordify $cmd)
}
}
}
init