-
Notifications
You must be signed in to change notification settings - Fork 8
/
to.sh
421 lines (394 loc) · 10.9 KB
/
to.sh
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# to - v1.4.4
# Bookmark locations in bash
#
# Copyright (C) 2013 Mara Kim
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see http://www.gnu.org/licenses/.
### SETTINGS ###
if [ -z "$TO_BOOKMARK_DIR" ]
then
TO_BOOKMARK_DIR=~/.bookmarks
fi
### MAIN ###
to() {
# read arguments
local option
local input
local state
for arg in "$@"
do
if [ "$state" = "input" ]
then
input+=("$arg")
elif [ "$arg" = "-h" -o "$arg" = "--help" ]
then
\printf 'Usage: to [OPTION] [BOOKMARK] [DEST]
Set the current working directory to a saved bookmark or subdirectory,
or create such a bookmark.
To view bookmarks, execute with no parameters
Options
-b Add a new bookmark (overwrites any current bookmark)
-r Remove bookmark
-p Print bookmark path
-h Show help
'
return 0
elif [ "$arg" = "--" ]
then
state="input"
elif [ -z "${arg/-*/}" ]
then
if [ ! "$option" ]
then
option="$arg"
else
\printf 'Ignored option: %q\n' "$arg"
fi
else
input+=("$arg")
fi
done
# create empty bookmarks folder if it does not exist
if [ ! -d "$TO_BOOKMARK_DIR" ]
then
\mkdir -pv -- "$TO_BOOKMARK_DIR"
fi
if [ -z "$option" -a "${input[*]}" = "" ]
then
# show bookmarks
tree --noreport -C "$TO_BOOKMARK_DIR" | tail -n+2
return 0
elif [ "$option" = "-p" ]
then
# print path of bookmarks
local good="good"
local response
for i in "${input[@]}"
do
if [ "$i" ]
then
response+=( "$(\readlink -f -- "$TO_BOOKMARK_DIR/$i")" )
if [ $? != 0 ]
then
good="bad"
fi
fi
done
\printf '%q ' "${response[@]}"
\printf '\n'
if [ "$good" != "good" ]
then
return 1
else
return 0
fi
elif [ "$option" = "-b" ]
then
if [ "$ZSH_VERSION" ]
then
local bound=2
else
local bound=1
fi
# get target
if [ "${#input[@]}" -gt "$bound" ]
then
local end="${#input[@]}-1"
if [ "$ZSH_VERSION" ]
then
local target="${input[-1]}"
else
local target="${input[$end]}"
fi
if [ -d "$target" ]
then
local target="$(\readlink -e -- "$target")"
else
\printf '%q does not refer to a directory\n' "$target"
return 1
fi
else
local target="$PWD"
local end="${#input[@]}"
fi
# add bookmarks
local good="good"
if [ "${input[*]}" = "" ]
then
local name="$(\basename -- "$PWD")"
# create link (symbolic force no-dereference Target)
ln -sfn "$target" -- "$TO_BOOKMARK_DIR/$name"
else
for i in "${input[@]:0:$end}"
do
if [ "$i" ]
then
if [ "$(\basename -- "$i")" != "$i" ]
then
\printf 'Invalid bookmark name: %q\n' "$i"
good="bad"
continue
else
local name="$i"
fi
if [ "$name" = '/' -o "$name" = '.' -o "$name" = '..' ]
then
# special cases
\printf 'Invalid bookmark name: %q\n' "$name"
good="bad"
continue
fi
# create link (symbolic force no-dereference Target)
ln -sfn "$target" -- "$TO_BOOKMARK_DIR/$name"
fi
done
fi
if [ "$good" = "good" ]
then
return 0
else
return 1
fi
elif [ "$option" = "-r" ]
then
for i in "${input[@]}"
do
if [ "$i" ]
then
if [ "${i%/}" = "$(_to_path_head "$i")" -a -h "$TO_BOOKMARK_DIR/${i%/}" ]
then
# remove bookmark
\rm -- "$TO_BOOKMARK_DIR/${i%/}"
else
\printf 'No bookmark: %q\n' "${i%/}"
fi
fi
done
return 0
fi
# go to bookmark
for i in "${input[@]}"
do
if [ "$i" ]
then
if [ -d "$TO_BOOKMARK_DIR/$i" ]
then
\cd -P -- "$TO_BOOKMARK_DIR/$i"
else
\printf 'Invalid link: %q\n' "$i"
return 1
fi
return 0
fi
done
}
### TAB COMPLETION ###
# tab completion generic
# $1 = position of current word (1 is first argument)
# $2-n = words
# Output valid completions
_to() {
local IFS=$'\0'
# read arguments
local word
local cword
local option
local state
local inputpos
local input=-1
local iter=-1
for arg in "$@"
do
if [ -z "$cword" ]
then
# get first argument
cword="$arg"
elif [ "$state" = "input" ]
then
input="$(\expr "$input" + 1)"
elif [ "$arg" = "-h" -o "$arg" = "--help" ]
then
return 0
elif [ "$arg" = "--" ]
then
state="input"
elif [ -z "${arg/-*/}" ]
then
if [ ! "$option" ]
then
option="$arg"
fi
else
input="$(\expr "$input" + 1)"
fi
if [ "$iter" = "$cword" ]
then
word="$arg"
inputpos="$input"
fi
iter="$(\expr "$iter" + 1)"
done
if [ -z "$word" ]
then
inputpos="$(\expr "$inputpos" + 1)"
fi
# create empty bookmarks file if it does not exist
if [ ! -e "$TO_BOOKMARK_DIR" ]
then
\mkdir -pv -- "$TO_BOOKMARK_DIR"
fi
# clean word
word="$(\eval '\printf' '%b' "$word" 2> /dev/null)"
local stat="$?"
if [ "$stat" != 0 ]
then
return 1
fi
# build reply
local compreply
if [ "$option" = "-b" ]
then
if [ "$inputpos" -ge 1 ]
then
# add current directory
compreply+=("$(\basename -- "$PWD" )")
# get bookmarks
local bookmarks
while \read -r -d '' bookmark
do
bookmarks+=( "$bookmark" )
done < <(\find "$TO_BOOKMARK_DIR" -mindepth 1 -maxdepth 1 -type l -printf '%f\0')
compreply+=( "${bookmarks[@]}" )
fi
if [ "$inputpos" -ge 2 ]
then
# normal file completion
word="${word/#-/./-}"
compreply+=( $(\find "$(\dirname -- "${word}0")" -mindepth 1 -maxdepth 1 -type d -printf '%p/\0' 2> /dev/null) )
fi
elif [ "$option" = "-r" ]
then
# get bookmarks
local bookmarks
while \read -r -d '' bookmark
do
bookmarks+=($bookmark)
done < <(\find "$TO_BOOKMARK_DIR" -mindepth 1 -maxdepth 1 -type l -printf '%f\0')
compreply+=( "${bookmarks[@]}" )
else
# get subdirs
local subdirs
while \read -r -d '' file
do
subdirs+=($file)
done < <(\find "$(\dirname -- "$(\readlink -f -- "$TO_BOOKMARK_DIR/${word}0" || \printf '/dev/null' )")" -mindepth 1 -maxdepth 1 -type d -print0 2> /dev/null)
local pattern="$(\readlink -f -- "$TO_BOOKMARK_DIR/$(_to_path_head "$word")")"
local replace="$(_to_path_head "$word")"
subdirs=( "${subdirs[@]/%//}" )
subdirs=( "${subdirs[@]/#$pattern/$replace}" )
compreply+=( "${subdirs[@]}" )
# get subfiles
local subfiles
if [ "$option" = "-p" ]
then
while \read -r -d '' file
do
subfiles+=($file)
done < <(\find "$(\dirname -- "$(\readlink -f -- "$TO_BOOKMARK_DIR/${word}0" || \printf '/dev/null' )")" -mindepth 1 -maxdepth 1 -type f -print0 2> /dev/null)
subfiles=( "${subfiles[@]/#$pattern/$replace}" )
fi
compreply+=( "${subfiles[@]}" )
# get bookmarks (with slash)
local bookmarks
while \read -r -d '' bookmark
do
bookmarks+=($bookmark)
done < <(\find "$TO_BOOKMARK_DIR" -mindepth 1 -maxdepth 1 -type l -printf '%f/\0')
compreply+=( "${bookmarks[@]}" )
fi
# generate reply
local filter
for completion in "${compreply[@]}"
do
if [ -z "${completion/#$word*}" -a "${completion/#$word}" ]
then
filter+=("$completion")
fi
done
COMPREPLY=( "${filter[@]}" )
}
# teleport alias
alias tp="to -p"
# tab completion bash
_to_bash() {
# call generic tab completion function
_to "$COMP_CWORD" "${COMP_WORDS[@]}"
}
_tp_bash() {
COMP_CWORD="$(expr $COMP_CWORD + 1)"
# call generic tab completion function
_to "$COMP_CWORD" -p "${COMP_WORDS[@]}"
}
# tab completion zsh
_to_zsh() {
# call generic tab completion function
_to "$COMP_CWORD" "${COMP_WORDS[@]}"
COMPREPLY=( "${(q)COMPREPLY[@]}" )
}
_tp_zsh() {
COMP_CWORD="$(expr $COMP_CWORD + 1)"
# call generic tab completion function
_to "$COMP_CWORD" -p "${COMP_WORDS[@]}"
COMPREPLY=( "${(q)COMPREPLY[@]}" )
}
# setup tab completion
if [ "$ZSH_VERSION" ]
then
\autoload -U +X bashcompinit && \bashcompinit
\complete -o nospace -F _to_zsh to
\complete -o nospace -F _tp_zsh tp
else
\complete -o filenames -o nospace -F _to_bash to
\complete -o filenames -o nospace -F _tp_bash tp
fi
### HELPER FUNCTIONS ###
# get the first part of the path
_to_path_head() {
if [ "$ZSH_VERSION" ]
then
local target
target=(${(s:/:)${1}})
else
local old="$IFS"
local IFS="/"
local target=( $1 )
fi
local head="$target"
local prev
local first
for part in "${target[@]}"
do
if [ "$prev" != "${prev%\\}" ]
then
head="$head/$part"
elif [ -z "$first" ]
then
first="no"
else
break
fi
prev="$part"
done
\printf '%b' "$head"
}