-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunction_cd.bash
More file actions
64 lines (63 loc) · 2.12 KB
/
function_cd.bash
File metadata and controls
64 lines (63 loc) · 2.12 KB
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
# previous cd at 2005/03/22 (original idea)
# enahnced cd at 2019/03/31 (following)
is_current_bash && \
function cd {
#set -x
local arg="$1" subcommand result rc
if [ -z "$arg" ] ; then ### home directory
# cd 連打で余計な $DIRSTACK を増やさない
test "$PWD" = "$HOME" || pushd $HOME >/dev/null
return
elif [ "${arg:0:1}" = ":" ] ; then ### command mode
# コロンコマンドは xtcd.sh にディスパッチする
subcommand="${arg#:}" ; shift
case "$subcommand" in
history)
result="$( dirs -v | xtcd.sh :history "$@" )"
;;
clear)
dirs -c ; return
;;
help|subcommands)
xtcd.sh :$subcommand "$@"
return
;;
up|down|drop|which|pwdsed|repo|stdin|mdfind|bookmark)
if [ $subcommand = bookmark -a $# -gt 0 ] ; then
# $() 中だと $EDITOR が起動できないので冒頭で処理している
xtcd.sh :bookmark "$@"
return
fi
result="$( xtcd.sh :$subcommand "$@" )"
rc=$?
if [ $rc = 100 ] ; then
# 終了ステータス100の場合、受け取った出力を流して、ここの(呼び出し元)で穏当に終了する
echo "$result"
return 0
elif [ $rc -gt 0 ] ; then
echo "xtcd.sh returns error code" >&2
return 1
fi
;;
*)
echo "unknown command :$subcommand" >&2
return 1
;;
esac
if [ "${result:0:1}" = "~" ] ; then
result=$HOME${result#"~"}
fi
if [ -f "$result" ] ; then
result="${result%/*}"
fi
else
result="$arg"
fi
if [ -n "$result" ] ; then
pushd "$result" >/dev/null
if [ -n "$subcommand" -a "$subcommand" = drop ] ; then
cd :drop
fi
fi
#set +x
}