@@ -38,9 +38,6 @@ if [ ! -n "$SDIRS" ]; then
38
38
fi
39
39
touch $SDIRS
40
40
41
- RED=" 0;31m"
42
- GREEN=" 0;33m"
43
-
44
41
# save current directory to bookmarks
45
42
function s {
46
43
check_help $1
@@ -54,51 +51,66 @@ function s {
54
51
55
52
# jump to bookmark
56
53
function g {
57
- check_help $1
58
- source $SDIRS
59
- target=" $( eval $( echo echo $( echo \$ DIR_$1 ) ) ) "
60
- if [ -d " $target " ]; then
61
- cd " $target "
62
- elif [ ! -n " $target " ]; then
63
- echo -e " \033[${RED} WARNING: '${1} ' bashmark does not exist\033[00m"
64
- else
65
- echo -e " \033[${RED} WARNING: '${target} ' does not exist\033[00m"
66
- fi
54
+ _bashmarks_check_help $@ || _bashmarks_go $@
67
55
}
68
56
69
57
# print bookmark
70
58
function p {
71
- check_help $1
72
- source $SDIRS
73
- echo " $( eval $( echo echo $( echo \$ DIR_$1 ) ) ) "
59
+ _bashmarks_check_help $@ || _bashmarks_print $@
74
60
}
75
61
76
62
# delete bookmark
77
63
function d {
78
- check_help $1
79
- _bookmark_name_valid " $@ "
80
- if [ -z " $exit_message " ] ; then
81
- _purge_line " $SDIRS " " export DIR_ $1 = "
82
- unset " DIR_ $1 "
83
- fi
64
+ _bashmarks_check_help $@ || _bashmarks_delete $@
65
+ }
66
+
67
+ # list bookmarks with dirname
68
+ function l {
69
+ _bashmarks_check_help $@ || _bashmarks_list $@
84
70
}
85
71
86
72
# print out help for the forgetful
87
- function check_help {
73
+ function _bashmarks_check_help {
88
74
if [ " $1 " = " -h" ] || [ " $1 " = " -help" ] || [ " $1 " = " --help" ] ; then
89
75
echo ' '
90
76
echo ' s <bookmark_name> - Saves the current directory as "bookmark_name"'
91
77
echo ' g <bookmark_name> - Goes (cd) to the directory associated with "bookmark_name"'
92
78
echo ' p <bookmark_name> - Prints the directory associated with "bookmark_name"'
93
79
echo ' d <bookmark_name> - Deletes the bookmark'
94
80
echo ' l - Lists all available bookmarks'
95
- kill -SIGINT $$
81
+ return 0
96
82
fi
83
+ return 1
97
84
}
98
85
99
- # list bookmarks with dirnam
100
- function l {
101
- check_help $1
86
+ function _bashmarks_save {
87
+ _bashmarks_bookmark_name_valid " $@ "
88
+ if [ -z " $_bashmarks_exit_message " ]; then
89
+ _bashmarks_purge_line " $SDIRS " " export DIR_$1 ="
90
+ CURDIR=$( echo $PWD | sed " s#^$HOME #\$ HOME#g" )
91
+ echo " export DIR_$1 =\" $CURDIR \" " >> $SDIRS
92
+ fi
93
+ }
94
+
95
+ function _bashmarks_go {
96
+ source $SDIRS
97
+ cd " $( eval $( echo echo $( echo \$ DIR_$1 ) ) ) "
98
+ }
99
+
100
+ function _bashmarks_print {
101
+ source $SDIRS
102
+ echo " $( eval $( echo echo $( echo \$ DIR_$1 ) ) ) "
103
+ }
104
+
105
+ function _bashmarks_delete {
106
+ _bashmarks_bookmark_name_valid " $@ "
107
+ if [ -z " $_bashmarks_exit_message " ]; then
108
+ _bashmarks_purge_line " $SDIRS " " export DIR_$1 ="
109
+ unset " DIR_$1 "
110
+ fi
111
+ }
112
+
113
+ function _bashmarks_list {
102
114
source $SDIRS
103
115
104
116
# if color output is not working for you, comment out the line below '\033[1;32m' == "red"
@@ -107,63 +119,69 @@ function l {
107
119
# uncomment this line if color output is not working with the line above
108
120
# env | grep "^DIR_" | cut -c5- | sort |grep "^.*="
109
121
}
122
+
110
123
# list bookmarks without dirname
111
- function _l {
124
+ function _bashmarks_list_without_dirname {
112
125
source $SDIRS
113
126
env | grep " ^DIR_" | cut -c5- | sort | grep " ^.*=" | cut -f1 -d " ="
114
127
}
115
128
116
129
# validate bookmark name
117
- function _bookmark_name_valid {
118
- exit_message =" "
130
+ function _bashmarks_bookmark_name_valid {
131
+ _bashmarks_exit_message =" "
119
132
if [ -z $1 ]; then
120
- exit_message =" bookmark name required"
121
- echo $exit_message
133
+ _bashmarks_exit_message =" bookmark name required"
134
+ echo $_bashmarks_exit_message
122
135
elif [ " $1 " != " $( echo $1 | sed ' s/[^A-Za-z0-9_]//g' ) " ]; then
123
- exit_message =" bookmark name is not valid"
124
- echo $exit_message
136
+ _bashmarks_exit_message =" bookmark name is not valid"
137
+ echo $_bashmarks_exit_message
125
138
fi
126
139
}
127
140
128
141
# completion command
129
- function _comp {
142
+ function bashmarks_comp {
130
143
local curw
131
144
COMPREPLY=()
132
145
curw=${COMP_WORDS[COMP_CWORD]}
133
- COMPREPLY=($( compgen -W ' `_l `' -- $curw ) )
146
+ COMPREPLY=($( compgen -W ' `_bashmarks_list_without_dirname `' -- $curw ) )
134
147
return 0
135
148
}
136
149
137
150
# ZSH completion command
138
- function _compzsh {
139
- reply=($( _l ) )
151
+ function bashmarks_compzsh {
152
+ reply=($( _bashmarks_list_without_dirname ) )
140
153
}
141
154
142
155
# safe delete line from sdirs
143
- function _purge_line {
156
+ function _bashmarks_purge_line {
144
157
if [ -s " $1 " ]; then
145
158
# safely create a temp file
146
- t=$( mktemp -t bashmarks.XXXXXX) || exit 1
147
- trap " /bin/rm -f -- '$t '" EXIT
159
+ t=$( mktemp -t bashmarks.XXXXXX)
160
+
161
+ if [ $? -eq 0 ]; then
162
+ trap " rm -f -- '$t '" EXIT
148
163
149
- # purge line
150
- sed " /$2 /d" " $1 " > " $t "
151
- /bin/ mv " $t " " $1 "
164
+ # purge line
165
+ sed " /$2 /d" " $1 " > " $t "
166
+ mv " $t " " $1 "
152
167
153
- # cleanup temp file
154
- /bin/rm -f -- " $t "
155
- trap - EXIT
168
+ # cleanup temp file
169
+ rm -f -- " $t "
170
+ trap - EXIT
171
+ else
172
+ echo " Failed to create temporary file." >&2
173
+ fi
156
174
fi
157
175
}
158
176
159
- # bind completion command for g,p,d to _comp
177
+ # bind completion command for g,p,d to bashmarks_comp
160
178
if [ $ZSH_VERSION ]; then
161
- compctl -K _compzsh g
162
- compctl -K _compzsh p
163
- compctl -K _compzsh d
179
+ compctl -K bashmarks_compzsh g
180
+ compctl -K bashmarks_compzsh p
181
+ compctl -K bashmarks_compzsh d
164
182
else
165
183
shopt -s progcomp
166
- complete -F _comp g
167
- complete -F _comp p
168
- complete -F _comp d
184
+ complete -F bashmarks_comp g
185
+ complete -F bashmarks_comp p
186
+ complete -F bashmarks_comp d
169
187
fi
0 commit comments