-
Notifications
You must be signed in to change notification settings - Fork 0
/
cde.bash
75 lines (64 loc) · 1.82 KB
/
cde.bash
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
# support for accessing emacs bookmarks in bash
# eval elisp code from stdin
cde_elisp_eval () {
local temp_fname=$(mktemp)
cat > $temp_fname
emacs --no-init-file --no-site-file --batch --load $temp_fname
rm -f $temp_fname;
}
CDE_ELISP_COMMON="
(require 'cl)
(require 'bookmark)
(defun dir-p (location)
(char-equal (elt location (1- (length location))) ?/))
(defun bookmark-to-line (name)
(let ((bookmark (bookmark-get-bookmark name)))
(if bookmark
(format \"\\\"%s\\\" \\\"%s\\\"\" name (bookmark-get-filename bookmark)))))
"
# print all bookmarks: one bookmark per line in such format: "<name>" "<path>"
cde_all_bookmarks () {
cde_elisp_eval <<EOF
$CDE_ELISP_COMMON
(dolist (name (remove-if-not (lambda (name) (dir-p (bookmark-location name)))
(bookmark-all-names)))
(princ (format "%s\n" (bookmark-to-line name))))
EOF
}
# print the path for a bookmark or empty if a bookmark is not found
cde_get_bookmark () {
local name="$1"
cde_elisp_eval <<EOF
$CDE_ELISP_COMMON
(let ((location (bookmark-location "$name")))
(if (and location (dir-p location))
(princ location)))
EOF
}
# cd to bookmark
cde () {
local name="$1"
[ -z "$name" ] && { echo no bookmark name given;return 1;}
local path_=$(cde_get_bookmark $name)
eval path_=$path_
if [ -z "$path_" ]; then
echo no such bookmark: $name
return 1
else
cd $path_
fi
}
_cde () {
[ $COMP_CWORD -eq 1 ] && COMPREPLY=($(compgen -W "$(cde_all_bookmarks|cut -d' ' -f1)" ${COMP_WORDS[$COMP_CWORD]}))
}
complete -o nospace -F _cde cde
# FIXME: maybe someday I will make cd to accept normal directories and bookmarks.
_cde_cd () {
local merged=()
_cde
merged=$COMPREPLY
_cd
merged=(${COMPREPLY[@]} ${merged[@]})
COMPREPLY=($(compgen -W "${merged[*]}" "$2"))
}
#complete -o nospace -F _cde_cd cd