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