]> git.phdru.name Git - dotfiles.git/blob - .shellrc
Refactor(.shellrc): Declare `has_completion` once
[dotfiles.git] / .shellrc
1 #
2 # $HOME/.shellrc
3 #
4
5 # ~/.bashrc: executed by bash(1) for non-login shells.
6 # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
7 # for examples
8
9 # If not running interactively, don't do anything
10 case $- in
11     *i*) ;;
12       *) return ;;
13 esac
14
15 # Stop if non-interactive shell
16 [ -z "$PS1" ] && return
17
18 if test -n "$BASH_VERSION"; then
19    # append to the history file, don't overwrite it
20    shopt -s histappend
21
22    # check the window size after each command and, if necessary,
23    # update the values of LINES and COLUMNS.
24    shopt -s checkwinsize
25
26    # If set, the pattern "**" used in a pathname expansion context will
27    # match all files and zero or more directories and subdirectories.
28    #shopt -s globstar
29 fi
30
31
32 #if [ -x /usr/bin/dircolors ]; then
33 #   # I don't like these aliases - they work only in command line
34 #   # but not in scripts I run from command line.
35 #   # Color parameters must be passed via environment.
36 #   # So instead I put them in shell scripts in ~/bin.
37 #   alias ls='ls --color=auto'
38 #   alias dir='dir --color=auto'
39 #   alias vdir='vdir --color=auto'
40 #
41 #   alias grep='grep --color=auto'
42 #   alias fgrep='fgrep --color=auto'
43 #   alias egrep='egrep --color=auto'
44 #
45 #   alias dmesg='dmesg --human'
46 #fi
47
48
49 case "$HOME" in
50    /home/*)
51       if [ -L /home ]; then
52          START_DIR="`pwd`" &&
53          cd "$HOME" &&
54          HOME="`pwd`" &&
55          cd "$START_DIR"
56       fi
57    ;;
58 esac
59
60 if test -n "$BASH_VERSION"; then
61    [ "`type -t ls`" = alias ] && unalias ls
62    [ "`type -t ll`" = alias ] && unalias ll
63    [ "`type -t mc`" = alias ] && unalias mc
64
65    back() { cd - "$@"; }
66    functions() { typeset -f "$@"; }
67    j() { jobs; }
68
69    if [ -z "$BASH_COMPLETION_COMPAT_DIR" ]; then
70       if [ -d /usr/share/bash-completion -a -r /usr/share/bash-completion/bash_completion ]; then
71          _BASH_COMPLETIONS_DIR=/usr/share/bash-completion/completions
72          . /usr/share/bash-completion/bash_completion
73       elif [ -r /etc/bash_completion ]; then
74          _BASH_COMPLETIONS_DIR=/etc/bash_completion.d
75          . /etc/bash_completion
76       elif [ -d /etc/bash_completion.d ]; then
77          _BASH_COMPLETIONS_DIR=/etc/bash_completion.d
78          for _compf in /etc/bash_completion.d/*; do
79             . $_compf
80          done
81       elif [ -d /usr/local/etc/bash_completion.d ]; then
82          _BASH_COMPLETIONS_DIR=/usr/local/etc/bash_completion.d
83          for _compf in /usr/local/etc/bash_completion.d/*; do
84             . $_compf
85          done
86       else
87          echo "Unknown OS type, cannot source bash_completion" >&2
88       fi
89    fi
90
91    has_completion() {
92       [ -n "$_BASH_COMPLETIONS_DIR" -a -d "$_BASH_COMPLETIONS_DIR" -a -r "$_BASH_COMPLETIONS_DIR/$1" ] ||
93       complete -p $1 >/dev/null 2>&1
94    }
95
96    for cmd in builtin cgmem_nice command dbus-launch exec \
97          killall man nice nohup pidof pidOf KillAll pgrep pkill psg pswg \
98          run-all-hosts su sudo time whence whereis which xargs; do
99       ! has_completion $cmd && complete -o default -A command $cmd
100    done
101
102    for cmd in dig host mtr nslookup nc netcat nmap p ping ping6 socat \
103          telnet t tt \
104          tcptraceroute tcptraceroute6 tracert tracert6 traceroute traceroute6 \
105          whois wd wget wget-download wget-m wget-wrapper ww; do
106       ! has_completion $cmd && complete -A hostname $cmd
107    done
108
109    for cmd in distribute ftp lftp r rsync \
110          s scp ssh smbclient tcpdump tshark wireshark; do
111       ! has_completion $cmd && complete -o default -A hostname $cmd
112    done
113
114    if [ -d "$HOME"/.bash_completion.d ]; then
115       for _compf in "$HOME"/.bash_completion.d/*; do
116          . $_compf
117       done
118    fi
119
120    delegate_completion() {
121       local prog programs
122       prog=$1
123       if has_completion $prog; then
124          shift
125          programs="$@"
126          eval "_${prog}_completion_loader() {
127             _completion_loader $prog
128             complete -o nospace -F _$prog $programs
129             unset _${prog}_completion_loader
130             return 124
131          }"
132          complete -F _${prog}_completion_loader $programs
133       fi
134    }
135
136    delegate_completion make m
137    delegate_completion ping p
138    delegate_completion rsync r rsync_cgmn rsync_cgmn_recode
139    delegate_completion ssh s
140    delegate_completion wget wget-m wget-wrapper ww
141
142    complete -A job bg fg j jobs wait
143    complete -A variable -A function unset
144    unset _BASH_COMPLETIONS_DIR has_completion delegate_completion
145
146    if [ -d "$HOME/lib/config" ]; then
147       complete -W "`cd \"$HOME/lib/config\" && echo *`" include
148    fi
149    complete -W "`echo $BROWSER | sed 's/:/ /g'`" start-browser
150
151    if [ "`type -t short_curdir`" != function -a \
152          -r "$HOME"/admin/prog/bash_prompt ]; then
153       . "$HOME"/admin/prog/bash_prompt
154    fi
155
156    #if type -p pip >/dev/null 2>&1; then
157    #   eval "`pip completion --bash`"
158    #   rm -rf /tmp/pip_build_"$USER"
159    #fi
160
161    #if which pyenv >/dev/null 2>&1; then
162    #   eval "`pyenv init -`"
163    #   eval "`pyenv virtualenv-init -`"
164    #fi
165
166    . virtualenvwrapper_lazy.sh 2>/dev/null
167
168    if [ -n "$VIRTUAL_ENV" ] && ! type deactivate >/dev/null 2>&1; then
169       . "$VIRTUAL_ENV/bin/activate"
170    fi
171
172    # Clean up python virtual environment on exit
173    # See https://virtualenvwrapper.readthedocs.io/en/latest/tips.html#clean-up-environments-on-exit
174
175    trap '[ "$VIRTUAL_ENV" ] && deactivate' EXIT
176
177 elif test -n "$KSH_VERSION" -o -n "$FCEDIT"; then
178    back() { cd - "$@"; }
179    j() { jobs; }
180 fi
181
182 PS1="$OPS1"
183
184 # clear screen and history, logout
185 chlo() { clear; cd; >$HISTFILE; unset HISTFILE; history -c; logout || exit; }
186
187
188 if test -x /usr/bin/git >/dev/null 2>&1; then
189    # chdir to a directory
190    cdgitpath() {
191       if [ $# -ne 1 ]; then
192          echo "Usage: cdgitpath path_key" >&2
193          return 1
194       fi
195
196       path_key="$1"
197       if [ -z "$path_key" ]; then
198          echo "Usage: cdgitpath path_key" >&2
199          return 1
200       fi
201
202       path_url="`git config --get --path $path_key`"
203       if echo "$path_url" | grep -q '^file:/'; then
204          path_url="`echo \"$path_url\" | sed 's@^file:/\+@/@'`"
205       fi
206       cd "$path_url"
207    }
208
209    # chdir to a remote's directory (if the remote is on the local FS)
210    cdremote() {
211       if [ $# -gt 1 ]; then
212          echo "Usage: cdremote [remote_name]" >&2
213          return 1
214       fi
215
216       if [ -z "$1" ]; then
217          branch="`git rev-parse --abbrev-ref HEAD`"
218          remote="`git config --get branch.$branch.remote`"
219       else
220          remote="$1"
221       fi
222
223       if [ -n "$remote" ] && git config --get remote.$remote.url |
224             grep -q '^\(file:/\|/\|\.\./\)'; then # (file:/ or / or ../ at the beginning)
225          cdgitpath remote.$remote.url
226       else
227
228          if [ -n "$1" ]; then
229             echo "Cannot find directory for remote $1" >&2
230             echo "Usage: cdremote [remote_name]" >&2
231             return 1
232          fi
233
234          if test -n "$BASH_VERSION"; then
235             _list_remotes '^\(file:/\|/\|\.\./\)' # (file:/ or / or ../ at the beginning)
236             if [ ${#GIT_REMOTES[*]} -eq 1 ]; then
237                remote=${GIT_REMOTES[0]}
238                unset GIT_REMOTES
239                cdgitpath remote.$remote.url
240             else
241                unset GIT_REMOTES
242                echo "Cannot find directory for any remote" >&2
243                echo "Usage: cdremote [remote_name]" >&2
244                return 1
245             fi
246          else
247             echo "Cannot find directory for remote $1" >&2
248             echo "Usage: cdremote [remote_name]" >&2
249             return 1
250          fi
251       fi
252    }
253
254    if test -n "$BASH_VERSION"; then
255       # completion for aliases in global .gitconfig
256
257       # fixup rbi rbia rbiap rbip - do refs name completion
258       _git_fixup() { __gitcomp_nl "$(__git_refs)" ; }
259       _git_rbi() { __gitcomp_nl "$(__git_refs)" ; }
260       _git_rbia() { __gitcomp_nl "$(__git_refs)" ; }
261       _git_rbiap() { __gitcomp_nl "$(__git_refs)" ; }
262       _git_rbip() { __gitcomp_nl "$(__git_refs)" ; }
263       #
264       # push-to-all-remotes - do branch name completion
265       _git_push_to_all_remotes() { __gitcomp_nl "$(__git_heads)" ; }
266
267       _cdgitpath_complete() {
268          local cur="${COMP_WORDS[COMP_CWORD]}"
269          COMPREPLY=(`compgen -W "$(__git_config_get_set_variables)" -- "$cur"`)
270       }
271
272       _git_open() {
273          _cdgitpath_complete
274       }
275
276       complete -F _cdgitpath_complete cdgitpath git-open
277
278       # list remotes with URLs matching a regexp
279       _list_remotes() {
280          if [ $# -ne 1 ]; then
281             echo "Usage: _list_remotes remote_regexp" >&2
282             return 1
283          fi
284          declare -ag GIT_REMOTES=()
285          local remote
286          for remote in `git remote`; do
287             if git config --get remote.$remote.url | grep -q "$1"; then
288                GIT_REMOTES+=($remote)
289             fi
290          done
291       }
292
293       # completion for cdremote and git-open-remote - list remotes with a pattern
294       _list_remotes_completion() {
295          local cur="${COMP_WORDS[COMP_CWORD]}"
296          _list_remotes "$1"
297          COMPREPLY=(`compgen -W "${GIT_REMOTES[*]}" -- "$cur"`)
298          unset GIT_REMOTES
299       }
300
301       # completion for cdremote - list remotes with directories as URLs
302       _cdremote_complete() {
303          _list_remotes_completion '^\(file:/\|/\|\.\./\)' # (file:/ or / or ../ at the beginning)
304          if [ ${#COMPREPLY[*]} -eq 0 ]; then
305             _list_remotes_completion .
306          fi
307       }
308
309       complete -F _cdremote_complete cdremote
310
311       # completion for git-open-remote - list remotes with http(s) URLs
312       _git_open_remote() {
313          _list_remotes_completion '^http\(s\)\?://'
314          if [ ${#COMPREPLY[*]} -eq 0 ]; then
315             _list_remotes_completion .
316          fi
317       }
318
319       complete -F _git_open_remote git-open-remote
320    fi
321 fi
322
323
324 include() {
325    cfg="$1"
326    if [ -f "./$cfg" -a -r "./$cfg" ]; then
327       echo "Reading config file \`$cfg'" 1>&2
328       . "./$cfg"
329    elif [ -f "$HOME/lib/config/$cfg" -a -r "$HOME/lib/config/$cfg" ]; then
330       echo "Reading config file \`$HOME/lib/config/$cfg'" 1>&2
331       . "$HOME/lib/config/$cfg"
332    else
333       echo "Cannot find config file \`$cfg'" 1>&2
334    fi
335 }
336
337
338 mc() {
339    if test -n "$BASH_VERSION"; then
340       MC_SAVE_OPS1="$OPS1"
341       if [ "`type -t short_curdir`" = function ]; then
342          OPS1="\`cgmem_which_prompt\`\u@\${HOSTNAME::5}:\`short_curdir\` "
343          OPS1+="\$(prompt_git)\\$\$SHLVL "
344       else
345          OPS1="\u@\h:\W \\$"
346       fi
347    fi
348
349    if [ -n "$SLOWTERM" ]; then
350       MC_SLOW="--slow"
351    fi
352
353    case "$TERM" in
354       screen*) # screen, screen.rxvt
355          MC_XTERM="-x"
356       ;;
357    esac
358
359    #TMPDIR="$HOME"/tmp
360    #export TMPDIR
361
362    MC_FOUND=NO
363
364    for wrapper in \
365          /usr/local/share/mc/bin/mc-wrapper.sh \
366          /usr/local/libexec/mc/mc-wrapper.sh \
367          /usr/share/mc/bin/mc-wrapper.sh; do
368       if [ -r $wrapper ]; then
369          . $wrapper $MC_SLOW $MC_XTERM "$@"
370          rc=$?
371          MC_FOUND=YES
372          break
373       fi
374    done
375
376    if [ $MC_FOUND = NO ]; then
377       for mc in /usr/local/bin/mc /usr/bin/mc; do
378          if [ -x $mc ]; then
379             MC_TMP_DIR="${TMPDIR-/tmp}/mc-$USER"
380             if [ ! -d MC_TMP_DIR="${TMPDIR-/tmp}/mc-$USER" ]; then
381                MC_TMP_DIR="${TMPDIR-/tmp}"
382             fi
383             MC_PWD_FILE="$MC_TMP_DIR/mc.pwd.$$"
384             mc_type="`mc_type.py $mc`"
385             if [ "$mc_type" = old ]; then
386                $mc -P $MC_SLOW $MC_XTERM "$@" > "$MC_PWD_FILE"
387             elif [ "$mc_type" = new ]; then
388                $mc -P "$MC_PWD_FILE" $MC_SLOW $MC_XTERM "$@"
389             else
390                continue
391             fi
392             rc=$?
393             MC_FOUND=YES
394             cd "`cat \"$MC_PWD_FILE\"`" && /bin/rm "$MC_PWD_FILE"
395             break
396          fi
397       done
398    fi
399
400    if [ $MC_FOUND = NO ]; then
401       echo "Cannot find mc-wrapper.sh or mc" >&2
402       rc=1
403    fi
404
405    [ -n "$MC_SAVE_OPS1" ] && OPS1="$MC_SAVE_OPS1"
406    unset MC_FOUND MC_PWD_FILE MC_SAVE_OPS1 MC_SLOW MC_TMP_DIR MC_XTERM
407
408    return $rc
409 }
410
411
412 mkcd() {
413    if [ $# -ne 1 ]; then
414       echo "Usage: mkcd directory_name" >&2
415    elif [ -d "$1" ]; then
416       cd "$1"
417    elif [ -e "$1" ]; then
418       echo "Error: $1 is a file" >&2
419    else
420       mkdir -p "$1" && cd "$1"
421    fi
422 }
423
424
425 if which tmux >/dev/null 2>&1; then
426    tmux() {
427       case "$TERM" in
428          rxvt)
429             TERM=rxvt-unicode command tmux "$@"
430          ;;
431
432          *)
433             command tmux "$@"
434          ;;
435       esac
436    }
437 fi
438
439
440 if [ "$SHLVL" -eq 1 ] && which startx >/dev/null 2>&1; then
441    X() { startx >> .Xserver.log 2>&1; cyr; rm -f .Xauthority; }
442 fi
443
444 x() { exit; }