summaryrefslogtreecommitdiff
path: root/dot/bashrc
blob: 2e4ed5dd0f3dc2465a251f25960d282be438b7b6 (plain)
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
# ~/.bashrc: executed by bash(1) for non-login shells.

export TZ='America/Detroit'

memoize_retval()
{
  local var="$1" func="$2"
  if [ -z "${!var}" ]; then
    eval "$func"
    eval "$var=$?"
  fi
  return ${!var}
}

### utility functions ###

  quiet() { "$@" >/dev/null 2>&1; }
  match_glob() { eval "case \"$2\" in $1) true ;; *) false ;; esac"; }
  match_path() { match_glob "*:$1:*|$1:*|*:$1" "$PATH"; }
  if match_glob "*zsh" "$SHELL"; then
    have() { for c in "$@"; do quiet whence -p "$c" || return 1; done; }
  else
    have() { for c in "$@"; do quiet type -P "$c" || return 1; done; }
  fi
  add_path()
  {
    if [ "$1" = "-a" ]; then
      local after=1
      shift
    else
      local after=
    fi
    for p in "$@"; do
      if [ -d "$p" ] && ! match_path "$p" ]; then
        [ -n "$after" ] && PATH="$PATH:$p"
	[ -z "$after" ] && PATH="$p:$PATH"
      fi
    done
  }

  add_stack_binpath()
  {
    [ "$STACK_PATH_HAS_BEEN_SET" ] && return
    local result="$( stack path 2>&1 | while read k v
      do
        case "$k" in
          bin-path:)         printf 'PATH=%q:%q\n' "$PATH" "${v%:$PATH}"    # add to _end_ of PATH
                             ;;
          ghc-package-path:) printf 'GHC_PACKAGE_PATH=%q\n' "$v"
                             echo 'export GHC_PACKAGE_PATH'
                             ;;
        esac
      done
    )"
    [ "$result" ] && eval "$result" && export STACK_PATH_HAS_BEEN_SET=y
  }

### bash settings, general environment ###

  add_path -a /usr/games
  add_path "$HOME/.cabal/bin" "$HOME/.local/bin" "$HOME/bin"

# if have stack; then
#   add_stack_binpath
# fi

  if have dbus-autolaunch && have dbus-launch; then
    eval $(dbus-autolaunch)
  fi

  if match_glob "*zsh" "$SHELL"; then
    autoload -U select-word-style
    select-word-style bash
    # WORDCHARS='*?_-[]~\!#$%^(){}<>|`@#$%^*()+:?'
  fi

  if match_glob "*bash" "$SHELL"; then
    export PS1='\u@\h:\w\$ '
  fi
  if have sea; then
    eval `sea`		# sets PS1 and PROMPT_COMMAND
    			# must come before dynamic titles section, below
  fi
  case $SHELL in 
    bash) shopt -s checkwinsize ;;
    *) ;;
  esac
  umask 002

  if have vim; then
    alias vi=vim
    export EDITOR=vim
  elif have vi; then
    export EDITOR=vi
  fi

  if have less; then
    export PAGER=less
    export LESS=-XSR
    have lesspipe && eval "$(lesspipe)"
  elif have w3m; then
    export PAGER=w3m
  elif have more; then
    export PAGER=more
  fi

### dynamic titles for xterm & screen ###

  function set_xterm_title () {
    # This is unneeded since my .screenrc is configured to set the xterm
    # title to the hardstatus line, which is configured to include the
    # current screen title... and a good thing too, or xterm titles
    # wouldn't change when screen windows were switched...
    echo -ne "\033]0;$*\007" >&2
  }
  function set_screen_title () {
    echo -ne "\033k$*\033\\" >&2
  }
  function xterm_preexec () {
    set_xterm_title ${PREEXEC_CMD[@]}
  }
  function screen_preexec () {
    set_screen_title ${PREEXEC_CMD[@]}
  }
  function screen_postexec () {
    set_screen_title "${PWD/$HOME/~}"
  }
  function xterm_postexec () {
    set_xterm_title "$USER@$HOST:${PWD/$HOME/~}"
  }
  if [ -n "$STY" ]; then
    PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND; }screen_postexec"
    PREEXEC_COMMAND=screen_preexec
  elif match_glob "xterm*" "$TERM"; then
    HOST=$(hostname)
    PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND; }xterm_postexec"
    PREEXEC_COMMAND=xterm_preexec
  fi

### program settings and aliases ###

  alias ip=/sbin/ip
  alias ipaddr='ip addr'

  if have ps && match_glob '*procps*' "$(ps -V 2>/dev/null)"; then
    export PS_FORMAT=pid,user:6,tty,\%mem,\%cpu,start,cmd
    alias p='ps x -H'
    have less && alias pp='p a |grep -v " \[[^] ]\+\]\$"| less -RSeX'
#   have less && alias pp='p a |sed -ne '\''1,/^[^ ]* *1 /!p;/^[^ ]* *1 /p'\''| less -RSeX'
  fi

  export PARINIT='rTbgqR B=.,?_A_a Q=_s>|'
  export JACK_START_SERVER=1;

  if have irssi; then
    alias anet='irssi -c Afternet'
    alias freenode='irssi -c Freenode'
  fi

  if have Xvnc; then
    alias startvnc='xinit -- `which Xvnc` :1 -geometry 800x600'
  fi

  if have xemacs; then
    alias gnus='DISPLAY= xemacs -f gnus'
    alias emacs='DISPLAY= xemacs'
  fi

  if have firefox; then
    alias ff='killall firefox-bin; firefox'
  fi

  if quiet ls -d -sCF --color=tty /; then
    alias ls='ls -sCF --color=tty';
    lss () { command ls -sCF --color "$@" | less -RSeX; }; 
  elif quiet ls -d -sCF /; then
    alias ls='ls -sCF';
    lss () { command ls -sCF "$@" | less -RSeX; }; 
  fi

  mkcd()
  {
    if [ $# -gt 1 ]; then
      echo 'mkcd: usage: mkcd <directory>'
    fi
    mkdir -p "$1" && cd "$1";
  }

  alias m-a='m-a -t'
  alias jf='ssh -tX bucky '
  alias jfs='ssh -tX bucky screen -x'

  alias ll='ls -l'; 
  alias lls='lss -l';
  alias pd=pushd

  alias sudo='sudo '
  alias s='screen '

  alias csi='csi -q'
  alias bc='bc -ql'

  alias df='df -x fuse'
  alias mtr='mtr --curses'

  doc () { pushd /usr/share/doc/$1; }

  lsl()
  {
      ls -lrtc "$@" --color=yes|tail -n 20
  }

  lddsize()
  {
    ldd "$@"|while read lib _x file addr; do
      [ -f "$file" ] || continue;
      echo -en "$file\0";
    done | xargs -0 du -cshD;
  }

### w3m bookmarks ###

if have w3m; then
  alias sd='w3m http://www.slashdot.org'
  alias blog='w3m http://insoul.net/blogdate.php?plain=yes'
  alias news='w3m http://news.google.com/?ned=tus'
  alias cl='w3m hartford.craigslist.org'
fi

if ! have srfi; then
  srfi ()
  {
    w3m http://srfi.schemers.org/srfi-"$1"/srfi-"$1".html
  }
fi

# if have nvim; then
#   alias vim=nvim
# fi

if have emacsclient; then
  alias vi='emacsclient -c'
fi


### array stack interface ###

pusha () 
{ 
  local arr="$1";
  shift;
  for t in "$@"; do
    eval $arr'[${#'$arr'[@]}]='"\"$t\""
  done;
}

popa ()  { eval unset $1'[${#'$1'[@]}-1]'; }
topa ()  { eval echo '${'$1'[${#'$1'[@]}-1]}'; }
echoa () { eval echo '${'$1'[@]}'; }
alias irc='jfx jerkface.net screen -xRR irc'
alias code='jfx jerkface.net screen -xRR code'
alias grep='grep --exclude-dir=.git'

dorner ()
{
    DOOMWADDIR=$HOME/src/zdoom/release LD_LIBRARY_PATH=$HOME/src/zdoom/fmodapi42636linux/api/lib/ $HOME/src/zdoom/release/zdoom -file \$HOME/Dorners\ Last\ Stand Skins/DoRnerLastStand.wad
}
avahi_hosts ()
{
    avahi-browse -trap | ( IFS=';';
    while read _ _ _ _ _ _ x _; do
        [ "$x" ] && echo $x;
    done ) | sort -u
}

live_play()
{
    local url="$1";
    mpv --cache-secs=30 --cache=25000 --cache-initial=25000 --cache-pause \
        "$(youtube-dl -g -f 'best[width<=1024],best[width<=1280],best' "$url")";
}

case "$TERM" in
  screen*)
    # http://unix.stackexchange.com/questions/179173/make-less-highlight-search-patterns-instead-of-italicizing-them
    export LESS_TERMCAP_so=$'\E[30;43m'
    export LESS_TERMCAP_se=$'\E[39;49m'
  ;;
esac

pingwait()
{
    while ! ping -c1 "$@"; do
        sleep 1
    done
}

take ()
{
    local n="$1" r=
    while [ "$n" -gt 0 ]; do
        read line && r="$r $line"
        let --n
    done
    if [ "$r" ]; then
        r=${r# }
        echo "$r"
    else
        false
    fi
}

torrent ()
{
    rsync -P "$@" borges:/srv/4T-temp/transmission/torrents/
}

webterm()
{
    PATH="/usr/lib/xorg:$PATH" xpra start --bind-tcp=0.0.0.0:14500 --html=on --start="${1:-xterm}"
    printf '%s\n' 'Stop server by pressing enter.' 'Connect to server via http://localhost:14500/'
    read line
    PATH="/usr/lib/xorg:$PATH" xpra stop
}

tmux_clone()
{
    tmux new-session -t 0 -s 1
}

wifi_AP()
{
    /sbin/iw dev wlan0 info | sed -n 's/^\taddr //p'
}

debian_install_firefox()
{
  which snap || sudo apt-get install snapd
  sudo snap install firefox
  printf '%s\n' 'To run firefox, execute the command:' '  snap run firefox'
}

jsynth()
{
    fluidsynth -g2 /usr/share/sounds/sf2/FluidR3_GM.sf2 -a jack -j -r 48000
}


wmuk1()
{
    icecream --name 'WMUK-1_%Y_%m_%d' http://ice2.wmuk.org:8000/mp31
}
wmuk2()
{
    icecream --name 'WMUK-2_%Y_%m_%d' http://ice2.wmuk.org:8000/mp32
}

reinstall_conf_file()
{
    local f="$1" pkg
    if [ -f "$f" ]; then
        sudo cp --backup=existing --force --preserve=all -- "$f" "$f" || return
        sudo rm -f "$f"
    fi
    [ ! -e "$f" ] || return;
    pkg=$(dpkg -S "$f") || return;
    sudo apt-get -o DPkg::options::=--force-confmiss --reinstall install ${pkg%%:*}
}

fanspeed()
{
    local speed="$1"
    if [ "$speed" ]; then
        [ "$speed" -gt 7 ] 2>/dev/null && speed=full-speed
        echo "level $speed" |  sudo tee /proc/acpi/ibm/fan
    else
        grep -v '^commands:' /proc/acpi/ibm/fan
    fi
}

subvolify()
{
    local path
    path=$(realpath -e "$1") || return
    [ -e "$path".subvol~ -o -e "$path".subvol ] && return 1
    sudo btrfs subvolume create "$path".subvol || return
    sudo chown --reference="$path" "$path".subvol || return
    sudo chmod --reference="$path" "$path".subvol || return
    sudo find "$path" -mindepth 1 -maxdepth 1 -print0 | xargs -0 mv -t "$path".subvol
    mv "$path" "$path".subvol~
    mv "$path".subvol "$path"
    rmdir "$path".subvol~ || true
}

fkill()
{
    killall "$@" firefox-bin 'Web Content' WebExtensions
}

alias fstop='fkill -STOP'
alias fcont='fkill -CONT'

newfile()
{
  find . -maxdepth 1 -type f -printf "%T@ %f\0" | sort -z -k1,1 -r | head -z -n1 | cut -d' ' -f2 | xargs -0 printf %s
}

install_spacemacs()
{
  if [ -d "$HOME/.emacs.d" ]
  then return
  else git clone --depth=1 https://github.com/syl20bnr/spacemacs ~/.emacs.d
  fi
}

local_bashrc=~/.bashrc.$(hostname)

if [ -f "$local_bashrc" ]; then
    . "$local_bashrc"
fi