-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy path.bashrc-interactive
executable file
·509 lines (442 loc) · 12.6 KB
/
.bashrc-interactive
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#!/usr/bin/env bash
if test "$EMACS" != t; then
if test -e /etc/bash_completion; then
. /etc/bash_completion
elif test -e /opt/local/etc/bash_completion; then
. /opt/local/etc/bash_completion
fi
fi
export INTERACTIVE_BASH_PID=$$
rm -f ~/.reset-env.$INTERACTIVE_BASH_PID
for x in ~/system-config/.bash_completion.d/*; do
. "$x"
done
function cd_bhj ()
{
local oldpwd=`pwd`
lcd "$@" && return $?
test $# == 1 -o $# == 0 && builtin cd "$@" >/dev/null 2>&1 || cd_failed "$@"
}
function cd_failed()
{
local IFS=$'\n'
dirs=( $(where "$@") )
if test ${#dirs[@]} = 0; then
echo 'Error: no such file or directory'
return 1
elif test ${#dirs[@]} = 1; then
builtin cd ${dirs[0]}
else
builtin cd `my-select "${dirs[@]}"`
fi
return 0
}
function fcd()
{
local IFS=$'\n'
dirs=( $(find . -iname "*$1*") )
if test ${#dirs[@]} = 0; then
echo 'Error: no such file or directory'
return 1
elif test ${#dirs[@]} = 1; then
cd_bhj "${dirs[0]}"
else
cd_bhj `my-select "${dirs[@]}"`
fi
}
function wcd()
{
local IFS=$'\n'
dirs=( "$@" )
if test ${#dirs[@]} = 0; then
echo 'Error: no such file or directory'
return 1
elif test ${#dirs[@]} = 1; then
cd_bhj ${dirs[0]}
else
cd_bhj `my-select "${dirs[@]}"`
fi
}
function fp()
{
f "$@" | tee /dev/stderr | putclip
}
function rbcd()
{
dirs=(`repo branches|perl -npe 's/.* in\b.//; s/,/ /g; s/ /\n/g'|sort -u`);
cd_bhj `my-select "${dirs[@]}"`
}
function re()
{
if test $# = 1 && (
test "$1" = --help -o "$1" = -h
); then
cat <<'EOF'
Usage: re WORDS...<TAB>
re(1) by itself does nothing.
But if you press Tab key after you input `re WORD1 WORD2 ...', then
Bash will prompt you with history commands that match with WORD1 and
WORD2 and all other words that you have inputed so far.
You may need run hir(1) command from time to time to put your command
history (in ~/.bash_history) into re(1)'s command history file (in
~/.cache/system-config/.bash_history.bak).
EOF
fi
true
}
function rex()
{
command rex "$@" ~/.cache/system-config/.bash_history.bak
}
function rcd() {
if test $# = 0; then
cd_bhj .repo/manifests
else
scd "$@"
fi
}
function cds() {
if test $# = 1 -a "${1:0:1}" = \\; then
cd_bhj smb://share.smar"t"isan.cn/share/baohaojun/"${1:1}"
fi
}
unalias lunch >/dev/null 2>&1 || true
my-lunch () {
alias lunch=my-lunch
if (unalias lunch; test "$(type -t lunch)" = function); then
(
unalias lunch
android-set-product "$@"
)
reset-env "alias lunch=my-lunch"
unalias lunch
if test "$#" != 0; then
lunch "$@"
else
buildspec=$(lookup-file -e buildspec.mk || true)
if test -e "$buildspec"; then
local product=$(android-var-from-spec TARGET_PRODUCT)$(android-var-from-spec TARGET_PRODUCT_SMARTISAN_EXTRA_ENV)
local variant=$(android-var-from-spec TARGET_BUILD_VARIANT)
fi
if test "$product" -a "$variant"; then
lunch "$product"-"$variant"
fi
fi
alias lunch=my-lunch
elif test "$(type -a -t lunch)" = alias; then
(
unalias lunch
android-set-product "$@"
)
else
reset-env "alias lunch=my-lunch"
unalias lunch
lunch "$@"
alias lunch=my-lunch
fi
}
alias rm='rm -i'
alias mkgtags='mkgtags -i -v'
alias bcd=cd_beagrep
alias mail=mailx
for x in ~/system-config/etc/bash.d/*; do
if test -f $x; then
. $x
fi
done
pcd()
{
if test "$#" = 0; then
builtin cd -P
return
fi
if test "$#" = 1 -a "${1:0:1}" != /; then
builtin cd -P "$(readlink -f "$PWD/$1")"
else
builtin cd -P "$@"
fi
}
cdr()
{
if [[ "$PWD" =~ ^$HOME/smb/ ]]; then
local pat=$HOME/smb/
local rep=smb://
history -s cd "$(printf %q "${PWD/$pat/$rep}")"
else
history -s cd "$(printf %q "`pwd`")"
fi
}
if tty -s; then
stty -ixon -ixoff start undef stop undef >/dev/null 2>&1
fi
qcd()
{
local branch=$(git symbolic-ref HEAD)
branch=${branch#refs/heads/}
cd .git/patches/$branch
}
lcd()
{
if test $# != 1; then
return 1
elif test "$1" = -; then
cd_ok -
return 0
elif [[ $1 =~ ^file:// ]]; then
lcd "${1#file://}"
return $?
fi
if test "${1:0:3}" = ' \\'; then # copied from mantis webpages, will contain space at start and end
set -- "${1# }"
set -- "${1% }"
fi
if [[ "$1" =~ ^\\\\|^//|^smb://|^file:///[0-9]+\.|/smb/ && `uname` = Linux ]]; then
set -- "$(ip-or-name "$1"|perl -npe 's,/+,/,g')"
smb_server=${1%%/*}
smb_share=$(echo ${1#*/}|perl -npe 's,/.*,,')
if [[ $1 =~ /.*/ ]]; then
set -- "${1#*/*/}"
else
set --
fi
run_gvfs=/run/user/$UID/gvfs
if test ! -d "$run_gvfs" && test -d /var/"$run_gvfs"; then
run_gvfs=/var/"$run_gvfs"
fi
smb_mount=$(select-args -o -p "请选择要使用哪个共享路径?" "$run_gvfs/smb-share:"*"server=$smb_server,share=$smb_share"*)
if test ! -e "$smb_mount"; then
smb_mount="/mnt/.smb/smb-share:server=$smb_server,share=$smb_share"
if test ! -d "$smb_mount" || ! get-mount-1 "$smb_mount" | grep ^// -q; then
hint "你还没有打开过 smb://$smb_server/$smb_share"
if test -e ~/src/github/smartcm/scm-helpers/.smb_mount &&
~/src/github/smartcm/scm-helpers/.smb_mount $smb_server "$smb_share"
then
true
else
return 1
fi
else
true
fi
fi
builtin cd "$smb_mount"/"$1"
return $?
fi
local ori=$1
local start=$PWD
local looking=$PWD
local x=0
while ! test -e "$1" >/dev/null || { [[ "$looking" =~ ^$HOME/smb(/[^/]+)?$ ]] && test "$looking" != "$start"; }; do # [[..]] because test -e not work in smbnetfs top levels
set -- ../"$1";
looking=`dirname "$looking"`
test "$looking" = / && break
if test -e "$looking"/"$ori"; then
if test -d "$looking"/"$ori"; then
builtin cd "$looking"/"$ori"
else
builtin cd "$(dirname "$looking"/"$ori")"
fi
return $?
fi
done
if ! test -e "$1" >/dev/null; then
_ucd $ori
return $?
fi
link=$1
if test -f "$link" && echo "${link##*.}"|grep -i '^lnk$' -q; then
link=`readshortcut "$link"`
fi
if test -d "$link"; then
if test "$link" = "`pwd`"; then
return 1;
fi
builtin cd "$link"
return $?
elif test -L "$link" -a -f "$link"; then
builtin cd "$(dirname "$(readlink -f "$link")")"
return $?
else
builtin cd "$(dirname "$link")"
return $?
fi
}
_ucd() #we go to upper dir which match the $1, for ex, cd 'ho' in /home/bhj will go to /home,
{
local x=0
dir=`dirname "$(pwd)"`
dir_basename=`basename "$dir"`
while ! echo $dir_basename|grep -e $1 -i -q && ((x++ < 10)); do
dir=`dirname "$dir"`
dir_basename=`basename "$dir"`
if test x"$dir" = x/; then
break
fi
done
if test x"$dir" = x/; then #obviously, we very not likely want to go to / with this complex setup
return 1;
fi
builtin cd "$dir"
return $?
}
svn-info-clip()
{
svn info "$1" |grep '^URL:'|sed -e 's/^URL: //; s/^https/http/'|tr -d '\r\n' | putclip
}
alias for-code-reading='for-code-reading -i -v'
function hir() {
if test $# = 0; then
history -r
backup_bash_history
echo "~/.cache/system-config/.bash_history.bak has $(wc -l ~/.cache/system-config/.bash_history.bak|pn 1) lines"
cat ~/.cache/system-config/.where.bak >> ~/.cache/system-config/.where
else
command hil "$@"
fi
}
function _select_cd() {
local IFS=$'\n'
local dirs=(
"$@"
)
if test ${#dirs[@]} = 0; then
echo 'Error: no such file or directory'
return 1
elif test ${#dirs[@]} = 1; then
builtin cd ${dirs[0]}
else
builtin cd `my-select "${dirs[@]}"`
fi
}
function tcd() {
local IFS=$'\n'
_select_cd $(
export LOOKUP_NON_EXIST_FILES=false
lookup-file $(today "$@" | perl -npe 's/(.*)-.*/$1/')
lookup-file `today-- "$@"`
) $(
export LOOKUP_NON_EXIST_FILES=false
pcd ~/today && {
lookup-file $(today "$@" | perl -npe 's/(.*)-.*/$1/')
lookup-file `today-- "$@"`;
}
)
}
function mcd() {
local IFS=$'\n'
_select_cd $(lookup-file "$@")
}
alias hil=hir
cd_record()
{
local ret=$?
if test $ret -ne 0; then
export PROMPT_RET="Ret: $ret @ $(date +%H:%M:%S) "
else
export PROMPT_RET=
fi
export PROMPT_COMMAND="cd_record"
history -a
. reget-env
if test ~/.config/system-config/ssh-agent -nt /proc/$$/cmdline; then
. ~/system-config/bin/set-ssh-agent
touch /proc/$$/cmdline >/dev/null 2>&1
fi
if test "$ANDROID_SERIAL" && ! echo "$SC_EXTRA_PS1" | grep -q -e "adb-$ANDROID_SERIAL\."; then
SC_EXTRA_PS1=$(echo "$SC_EXTRA_PS1" | perl -npe 's/adb-\S+\s*//g;')
if test "$SC_EXTRA_PS1"; then
export SC_EXTRA_PS1="$SC_EXTRA_PS1: ""adb-$ANDROID_SERIAL."
else
export SC_EXTRA_PS1="adb-$ANDROID_SERIAL."
fi
fi
if test "$BHJ_PWD" = "$PWD"; then
return $ret
fi
BHJ_PWD=$PWD
local repo_dir=$(lookup-file -e .repo || true)
if test "$repo_dir" -a -e "$repo_dir/../buildspec.mk"; then
local product=$(android-var-from-spec TARGET_PRODUCT)$(android-var-from-spec TARGET_PRODUCT_SMARTISAN_EXTRA_ENV)
local variant=$(android-var-from-spec TARGET_BUILD_VARIANT)
if test "$TARGET_PRODUCT" -a "$TARGET_BUILD_VARIANT" && test "$product-$variant" != "$TARGET_PRODUCT-$TARGET_BUILD_VARIANT"; then
export PROMPT_ANDROID_INFO="[$TARGET_PRODUCT-$TARGET_BUILD_VARIANT?] "
else
export PROMPT_ANDROID_INFO="[$product-$variant] "
fi
elif test "$TARGET_PRODUCT" -a "$TARGET_BUILD_VARIANT"; then
export PROMPT_ANDROID_INFO="[$TARGET_PRODUCT-$TARGET_BUILD_VARIANT] "
else
unset PROMPT_ANDROID_INFO
fi
(
if test "$LC_ALL" != C -a "$has_python3"; then
where;
fi&
)
return $ret
}
sudo() {
(
umask 0022
command sudo "$@"
)
}
export -f sudo
if which python3 >/dev/null 2>&1; then
has_python3=true
else
has_python3=
fi
export PROMPT_COMMAND="cd_record"
if test -n "$has_python3"; then
alias cd=cd_bhj
fi
alias cow='cp --reflink=always -a'
shopt -s nocaseglob
shopt -s histappend
scd()
{
local IFS=$'\n'
if test $# = 1; then
if pwd|grep -q -e "$1"; then
scd "$1" ''
return $?
fi
for dir in $(lookup-closest-file "$PWD" -- "$1"|tee /dev/stderr); do
dir=$(echo "$dir" | perl -npe 's,/./,/,g')
basedir=$(common-path "$PWD" "$dir")
stem="${dir:${#basedir}+1}"
dir1=$(pwd | perl -npe 's,(\Q'${basedir//,/\\,}'\E/)(.*?)(/|$),$1 . "'$stem'/",e')
if test -e "$dir1"; then
cd "$dir1" && break
else
cd "$(pwd | perl -npe 's,(\Q'${basedir//,/\\,}'\E/),$1 . "'$stem'/",e')" && break
fi
done
else
if ! echo "$PWD" | grep -i -e "$1" -q && echo "$PWD" | grep -i "$2" -q; then
set -- "$2" "$1"
fi
dir1=$(pwd | perl -npe 's#'"$1"'#'"$2"'#g')
test -d "$dir1" && { cd "$dir1"; return; }
dir=$(lookup-file -- "$1"|tee /dev/stderr|head -n 1)
shift
dir=$(builtin cd "$dir"; lookup-file "$1"|tee /dev/stderr|head -n 1)
dir=$(formal-path "$dir")
basedir=$(dirname "$dir")
stem=$(basename "$dir")
cd "$(pwd | perl -npe '$x=""; s,('$basedir'/)(.*?)(/|$),$1 . "'$stem'/",e')"
fi
}
if test -e ~/system-config/.by-user/$USER/.bashrc-interactive; then
. ~/system-config/.by-user/$USER/.bashrc-interactive
fi
. ~/system-config/.bashrc-aliases
export HISTCONTROL="ignoredups"
export HISTIGNORE="bg:fg:exit"
export HISTSIZE=2000
export HISTFILESIZE=2000
bind -x '"\eOP": sc-complete'
if test -e ~/.local-config/.bashrc-interactive; then
. ~/.local-config/.bashrc-interactive
fi