You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1060 lines
25 KiB

  1. #!/bin/sh
  2. # TODO: use tput
  3. ##########################################
  4. null(){
  5. "$@" >/dev/null 2>&1
  6. }
  7. __safe_run(){
  8. type $1 >/dev/null 2>&1 && "$@"
  9. }
  10. __match(){
  11. # __match str word
  12. # return 0 if word is found in str
  13. expr "$1" : ".*$2.*" >/dev/null
  14. }
  15. ##########################
  16. # system type
  17. # aliases:
  18. # ismsys, iscygwin iswindows, isdarwin, islinux,
  19. # with_coreutils, inbash, inzsh
  20. alias ismsys=false
  21. alias iscygwin=false
  22. alias iswindows=false
  23. alias isdarwin=false
  24. alias islinux=false
  25. alias with_coreutils=false # for mac
  26. case `uname` in
  27. (MINGW*) alias ismsys=true ;;
  28. (CYGWIN*) alias iscygwin=true ;;
  29. (Darwin*) alias isdarwin=true ;;
  30. (Linux*) alias islinux=true ;;
  31. esac
  32. null ls --version && alias with_coreutils=true
  33. ( ismsys || iscygwin ) && alias iswindows=true
  34. alias inbash=false
  35. alias inzsh=false
  36. if test -n "$BASH_VERSION"
  37. then
  38. alias inbash=true
  39. elif test -n "$ZSH_VERSION"
  40. then
  41. alias inzsh=true
  42. fi
  43. #################################
  44. # file pathes:
  45. # shrc: Path to this file
  46. # dotdir: Path to .dotfiles directory
  47. if inbash
  48. then
  49. shrc="$BASH_SOURCE"
  50. elif inzsh
  51. then
  52. shrc="$0"
  53. fi
  54. dotdir="`dirname "$shrc"`"
  55. #################################
  56. # aliases:
  57. # isinteractive: true if the current session is interactive
  58. # issourced: true if this file is sourced from another file (not so assured)
  59. # __firstload: true if this file is sourced for the first time (not so
  60. # assured)
  61. alias isinteractive=false
  62. __match "$-" i >/dev/null && alias isinteractive=true
  63. alias issourced=true
  64. expr "$0" : "^.*shrc$" >/dev/null && alias issourced=false # invoked as script
  65. alias __firstload=false
  66. __match "$PATH" "$HOME/.local/bin" || alias __firstload=true
  67. if __firstload
  68. then
  69. if test -d "$HOME/.local/bin"
  70. then
  71. PATH="${HOME}/.local/bin:${PATH}"
  72. fi
  73. if test -d "$HOME/.local/lib/gems/bin"
  74. then
  75. PATH="$HOME/.local/lib/gems/bin:${PATH}"
  76. fi
  77. fi
  78. if ismsys && ! __match "$PATH" /c/mingw/bin
  79. then
  80. PATH="$PATH:/c/mingw/bin:/c/mingw/msys/1.0/bin"
  81. fi
  82. # # it is not so good
  83. # # http://archive.linux.or.jp/JF/JFdocs/Program-Library-HOWTO/shared-libraries.html
  84. # # http://superuser.com/questions/324613/installing-a-library-locally-in-home-directory-but-program-doesnt-recognize-it
  85. # without this ENV i cannot run tmux. another way is to use --disable-shared
  86. # when building tmux
  87. if ! __match "$LD_LIBRARY_PATH" "$HOME/.local/lib"
  88. then
  89. export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$HOME/.local/lib"
  90. fi
  91. ##################################
  92. # Some EnvVal definitions
  93. export LANG=ja_JP.UTF-8
  94. export LC_MESSAGES=C
  95. export TERMCAP="${TERMCAP}:vb="
  96. ismsys && export HOSTNAME
  97. export ENV=~/.shrc
  98. if ! with_coreutils
  99. then
  100. export LSCOLORS=gxfxcxdxbxegedabagacad
  101. else
  102. # http://qiita.com/yuyuchu3333/items/84fa4e051c3325098be3
  103. null type dircolors && eval `dircolors`
  104. fi
  105. if false iswindows
  106. then
  107. export PAGER='tr -d \\r | less'
  108. else
  109. export PAGER="less"
  110. fi
  111. export LESS="-iRMX"
  112. # Style for lesspipe is defined in esc.style
  113. _src_hilite_lp_path="`which src-hilite-lesspipe.sh 2>/dev/null`"
  114. for f in /usr/share/source-highlight/src-hilite-lesspipe.sh
  115. do
  116. test -z "$_src_hilite_lp_path" && test -e "$f" && _src_hilite_lp_path="$f"
  117. done
  118. test -n "$_src_hilite_lp_path" && export LESSOPEN="| $_src_hilite_lp_path %s"
  119. if null type vim
  120. then
  121. export EDITOR=vim
  122. else
  123. export EDITOR=vi
  124. fi
  125. # export CDPATH=".:~"
  126. export VISUAL="$EDITOR"
  127. export GIT_PAGER="less -FS"
  128. export GIT_EDITOR="$EDITOR"
  129. export GIT_MERGE_AUTOEDIT=no
  130. if test -n "$TMUX" && \
  131. __match $TERM screen && \
  132. __match `tmux display -p '#{client_termname}'` 256color
  133. then
  134. TERM=screen-256color
  135. fi
  136. if test -z "$TMP"
  137. then
  138. if test -n "$TMPDIR"
  139. then
  140. export TMP=$TMPDIR
  141. elif test -n "$TEMP"
  142. then
  143. export TMP="$TEMP"
  144. else
  145. export TMP=/tmp
  146. fi
  147. fi
  148. __match "$TMP" "${USER}-tmp" >/dev/null || export TMP="${TMP}/${USER}-tmp"
  149. export TEMP="$TMP"
  150. mkdir -p "$TMP"
  151. ! iswindows && null type stty && {
  152. stty stop undef # unbind C-s to stop displaying output
  153. # stty erase '^h'
  154. }
  155. if iswindows; then
  156. export USER=$USERNAME
  157. fi
  158. if test -d ~/dbx
  159. then
  160. export CHIT_PATH="$HOME/dbx/.chit"
  161. fi
  162. #######################
  163. # If not running interactively, don't do anything
  164. issourced || exit
  165. isinteractive || return
  166. ######################
  167. # Print welcome messages
  168. iswindows && alias tty="echo cmd.exe"
  169. type fortune >/dev/null 2>&1 && {
  170. fortune
  171. echo
  172. fortune -o
  173. echo
  174. }
  175. uname -a
  176. echo TERM $TERM $(tput colors) colors connected to $(tty), \
  177. running $BASH $BASH_VERSION
  178. if test -n "$TMUX"
  179. then
  180. tmux display -p 'Using tmux #S:#I:#W.#P, client is #{client_termname}' \
  181. 2>/dev/null
  182. echo
  183. fi
  184. ###################################
  185. # some aliases and functions
  186. ( ! with_coreutils && isdarwin ) || test "$TERM" = dumb || \
  187. _coloroption=" --color=auto"
  188. ( ! with_coreutils && isdarwin ) || iswindows || \
  189. _timeoption=" --time-style=long-iso"
  190. ( ! with_coreutils && isdarwin ) || _hideoption=" --hide=[A-Z]*" # do not use
  191. _timeformat_iso="%Y-%m-%dT%H:%M:%S%z"
  192. _timeformat_rfc2822="%a, %d %b %Y %T %z"
  193. _timeformat_num="%Y%m%d%H%M%S"
  194. alias datenum="date +$_timeformat_num"
  195. alias ls="ls -hCF${_coloroption}${_timeoption}"
  196. # export GREP_OPTIONS=""
  197. alias gr="grep -n --color=always"
  198. iswindows && alias grep="grep -n"
  199. # alias ll="ls -l"
  200. # alias la="ls -A"
  201. # alias lla="ls -Al"
  202. alias less="less -F"
  203. null type emacs && alias em="emacs -nw"
  204. null type vim && alias vi=vim
  205. alias pstree="LANG=C pstree"
  206. alias cp="cp -v"
  207. alias mv="mv -v"
  208. alias rm="rm -v"
  209. alias psaux="ps auxww"
  210. alias q=exit
  211. null type e3em && alias e3=e3em
  212. #alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
  213. alias po=popd
  214. alias pu=pushd
  215. null type sudo && alias sudo="sudo " # use aliases through sudo
  216. null type sudoedit && alias sudoe="sudoedit"
  217. null type halt && alias halt="sudo halt"
  218. null type reboot && alias reboot="sudo reboot"
  219. null type dbus-send && {
  220. alias suspend="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  221. /org/freedesktop/UPower org.freedesktop.UPower.Suspend"
  222. alias hibernate="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  223. /org/freedesktop/UPower org.freedesktop.UPower.Hibernate"
  224. }
  225. alias rand="echo \$RANDOM"
  226. null type file-roller && alias xunp="file-roller -h"
  227. null type paco && alias pc="sudo \paco -D"
  228. alias pycalc="python -i -c 'from math import *' "
  229. null type python3 && alias py3=python3
  230. null type python2 && alias py2=python2
  231. alias _reloadrc="exec \"$SHELL\""
  232. # alias mytime="date +%Y%m%d-%H%M%S"
  233. alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
  234. # type trash >/dev/null 2>&1 && alias rm=trash
  235. null type mpg123 && alias mpg123="mpg123 -C -v --title"
  236. null type xmms2 && alias xm="xmms2"
  237. #export PLAYER="mpg123 -C -v --title"
  238. null type screen && alias screen="screen -e^z^z"
  239. #alias zcd="cd \`zenity --file-selection --directory\`"
  240. null type gtags && alias gtags="gtags --verbose"
  241. null type htags && alias htags="htags --xhtml --symbol --line-number \
  242. --frame --alphabet --verbose"
  243. null type aunpack && alias au=aunpack
  244. null type lv && alias lv="lv|less"
  245. null type rsync && alias rs="rsync --progress --itemize-changes --compress"
  246. isdarwin && alias updatedb="LC_ALL=C updatedb"
  247. # do not use locate installed by macports
  248. isdarwin && test -x /usr/bin/locate && alias locate="/usr/bin/locate"
  249. # pad
  250. alias pad=notepad
  251. null type gedit && alias pad=gedit
  252. null type leafpad && alias pad=leafpad
  253. isdarwin && alias pad="open -e"
  254. null type wicd-curses && alias wic=wicd-curses
  255. null type wicd-cli && alias wil="wicd-cli -y -l | head"
  256. #alias wicn="wicd-cli -y -c -n"
  257. wicn(){
  258. if test $# -eq 0
  259. then
  260. local num
  261. wicd-cli -y -l | head
  262. echo -n "input num: "
  263. read num
  264. test -n "$num" && wicd-cli -y -c -n $num
  265. else
  266. wicd-cli -y -c -n $1
  267. fi
  268. }
  269. __find_latest_vimdir(){
  270. vimdir=/usr/share/vim
  271. if test -d "$vimdir"
  272. then
  273. find "$vimdir" -name 'vim??' -type d | sort | tail -n 1
  274. else
  275. echo ""
  276. fi
  277. }
  278. for f in /usr/share/vim/vimcurrent "`__find_latest_vimdir`"
  279. do
  280. test -n "$f" || continue
  281. f="$f/macros/less.sh"
  282. test -f $f && alias vl=$f && break
  283. done
  284. alias pa=pacapt
  285. null type yaourt && alias yt=yaourt
  286. null type cower && alias cower="cower --color=auto"
  287. null type pacmatic && {
  288. alias pacman="pacmatic"
  289. export PACMAN="pacmatic"
  290. }
  291. _pacman_update_mirrorlist_with_reflector(){
  292. ml=/etc/pacman.d/mirrorlist
  293. cmd="$(expr "$(grep -m 1 reflector $ml)" : '# With: *\(.*\)')"
  294. if test -z "$cmd"
  295. then
  296. cmd="reflector --verbose -l 5 --sort rate --save $ml"
  297. fi
  298. echo "Running $cmd ..." 1>&2
  299. sudo $cmd
  300. }
  301. null type reflector && test -f /etc/pacman.d/mirrorlist && \
  302. alias reflect_mirrorlist=_pacman_update_mirrorlist_with_reflector
  303. null type apt-get && {
  304. alias aupgrade="sudo apt-get autoremove --yes && \
  305. sudo apt-get update --yes && sudo apt-get upgrade --yes"
  306. alias aptin="apt-get install"
  307. alias aptsearch="apt-cache search"
  308. alias aptshow="apt-cache show"
  309. }
  310. null type port && {
  311. alias port="port -v"
  312. alias pupgrade="sudo port -v selfupdate && \
  313. { sudo port -v upgrade outdated; }"
  314. }
  315. if iscygwin; then
  316. null type windate || \
  317. alias windate="cmd.exe //c 'echo %DATE%-%TIME%'"
  318. # alias cygsu="cygstart /cygwinsetup.exe"
  319. # alias ls="ls -CFG $(iswindows || test "$TERM" = dumb || echo --color=auto)"
  320. fi
  321. g(){
  322. if test $# -eq 0 && null type git-info
  323. then
  324. git info
  325. else
  326. git -c color.ui=always "$@"
  327. fi
  328. }
  329. if null type _git && inbash
  330. then
  331. # enable programmable completion for g
  332. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  333. || complete -o default -o nospace -F _git g
  334. fi
  335. git svn --help >/dev/null 2>&1 && alias gsvn="git svn"
  336. null type gitmemo && alias m=gitmemo
  337. alias setup.py="sudo python3 setup.py install --record files.txt"
  338. randomstr(){
  339. len=$1
  340. test -z "$len" && len=8
  341. uuidgen | tr -d - | cut -c 1-len
  342. }
  343. datestr(){
  344. # datestr yyyyMMdd-hhmmss
  345. if test -z "$1" || test "$1" == "-h"
  346. then
  347. echo "datestr: usage: datestr <yyyyMMddhhmmss>"
  348. return 1
  349. fi
  350. dfmt= # actual format for date command
  351. while test -n "$1"
  352. do
  353. fmt="$1"
  354. while test -n "$fmt"
  355. do
  356. case "$fmt" in
  357. yyyy*) # year
  358. dfmt="${dfmt}%Y"
  359. fmt="`echo "$fmt" | cut -c 5-`"
  360. ;;
  361. yy*) # last two digits of year
  362. dfmt="${dfmt}%y"
  363. fmt="`echo "$fmt" | cut -c 3-`"
  364. ;;
  365. MM*) # month (01..12)
  366. dfmt="${dfmt}%m"
  367. fmt="`echo "$fmt" | cut -c 3-`"
  368. ;;
  369. dd*) # day of month (01..12)
  370. dfmt="${dfmt}%d"
  371. fmt="`echo "$fmt" | cut -c 3-`"
  372. ;;
  373. HH* | hh*) # hour (00..23)
  374. dfmt="${dfmt}%H"
  375. fmt="`echo "$fmt" | cut -c 3-`"
  376. ;;
  377. mm*) # minute (00..59)
  378. dfmt="${dfmt}%M"
  379. fmt="`echo "$fmt" | cut -c 3-`"
  380. ;;
  381. ss*) # second (00..60)
  382. dfmt="${dfmt}%S"
  383. fmt="`echo "$fmt" | cut -c 3-`"
  384. ;;
  385. *)
  386. char=`echo "$fmt" | cut -c 1`
  387. dfmt="${dfmt}${char}"
  388. fmt="`echo "$fmt" | cut -c 2-`"
  389. ;;
  390. esac
  391. done
  392. shift
  393. done
  394. date +"$dfmt"
  395. }
  396. ssh(){
  397. __my_set_screen_title ssh
  398. command ssh "$@"
  399. }
  400. __ssh_with_cd(){
  401. # __ssh_with_cd <host> <directory> [<arg> ...]
  402. if test -z "$2"
  403. then
  404. echo "usage: __ssh_with_cd <host> <directory> [<arg> ...]"
  405. return 1
  406. fi
  407. host="$1"
  408. shift
  409. dir="$1"
  410. shift
  411. ssh "$host" "$@" -t "cd \"$dir\"; \$SHELL -l"
  412. }
  413. memo(){
  414. if test -z "$1"
  415. then
  416. $EDITOR memo.txt
  417. else
  418. $EDITOR "$1/memo.txt"
  419. fi
  420. }
  421. now(){
  422. local tformat="%Y/%m/%d %H:%M:%S %z"
  423. cal
  424. REPLY=
  425. printf "\\r`date "+${tformat}"`"
  426. read -t 1
  427. while test $? -ne 0
  428. do
  429. printf "\\r`date "+${tformat}"`"
  430. read -t 1
  431. done
  432. }
  433. s(){
  434. if git rev-parse --git-dir >/dev/null 2>&1
  435. then
  436. echo ">> git grep -n $@" 1>&2
  437. git grep -n "$@"
  438. elif which ag >/dev/null 2>&1
  439. then
  440. echo ">> ag --pager=\"$PAGER\" $@" 1>&2
  441. ag --pager="$PAGER" "$@"
  442. elif which ack >/dev/null 2>&1
  443. then
  444. echo ">> ack --pager=\"$PAGER\" $@" 1>&2
  445. ack --pager="$PAGER" "$@"
  446. else
  447. echo \
  448. ">> find . " \
  449. "-path '*/.git' -prune -o" \
  450. "-path '*/.svn' -prune -o" \
  451. "-type f -exec grep -nH -e --color=always $@ {} +" 1>&2
  452. if test $# -eq 0
  453. then
  454. echo "No search word given." 1>&2
  455. return 1
  456. fi
  457. find . \
  458. -path '*/.git' -prune -o \
  459. -path '*/.svn' -prune -o \
  460. -type -f -exec grep -nH -e --color=always "$@" {} + \
  461. | $PAGER
  462. fi
  463. }
  464. man(){
  465. env \
  466. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  467. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  468. LESS_TERMCAP_me=$(printf "\e[0m") \
  469. LESS_TERMCAP_se=$(printf "\e[0m") \
  470. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  471. LESS_TERMCAP_ue=$(printf "\e[0m") \
  472. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  473. man "$@"
  474. }
  475. netwait(){
  476. while ! ping -c 1 -t 1 example.com
  477. do
  478. true
  479. done
  480. echo network works.
  481. }
  482. __realpath(){
  483. if type realpath >/dev/null 2>&1
  484. then
  485. command realpath "$@"
  486. else
  487. while ! test -d $1
  488. do
  489. shift
  490. done
  491. (command cd "$d" && echo "$PWD")
  492. # local d="$OLDPWD"
  493. # command cd "$1"
  494. # echo "$PWD"
  495. # command cd "$d"
  496. fi
  497. }
  498. tx(){
  499. if test $# -eq 0
  500. then
  501. echo ":: tx <session> to attach."
  502. tmux ls
  503. elif tmux has -t "$1"
  504. then
  505. tmux attach -t "$1"
  506. else
  507. tmux new -s "$1"
  508. fi
  509. }
  510. _tmux_prefs(){
  511. null type tmux || return 1
  512. tmux set -g mode-keys vi
  513. }
  514. dt(){
  515. # dt [<name>] [<command ...>]
  516. __dtach_dir="${TMP}/dtach"
  517. install -d "${__dtach_dir}"
  518. if test -n "${__MY_DTACH}"
  519. then
  520. echo "Current session: ${__MY_DTACH}"
  521. fi
  522. if test -z "$1"
  523. then
  524. echo "Sessions:"
  525. ls "${__dtach_dir}"
  526. return 0
  527. elif test "$1" = "-h"
  528. then
  529. echo "dt: usage: dt <name> [<command ...>]" 1>&2
  530. return 1
  531. fi
  532. soc_name="${__dtach_dir}/$1"
  533. shift
  534. if test -n "$__MY_DTACH"
  535. then
  536. echo "dtach session cannot be nested." 1>&2
  537. return 1
  538. elif test -S "$soc_name"
  539. then
  540. dtach -a "$soc_name" -e ^^
  541. elif test -e "$soc_name"
  542. then
  543. echo "dt: File named $soc_name already exists."
  544. return 1
  545. elif test -z "$1"
  546. then
  547. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
  548. # echo "dt: Socket named $soc_name not exists and no command specified."
  549. # return 1
  550. else
  551. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
  552. fi
  553. }
  554. scr(){
  555. test -n "$1" && pf="${1}-"
  556. local _tformat="%Y%m%d-%H%M%S%z"
  557. local _file="${HOME}/${pf}`date +${_tformat}`.script"
  558. __MY_SCRIPT=${_file} script ${_file} "$@"
  559. }
  560. dtscr(){
  561. # dtscr <command ...>
  562. if test -z "$1"
  563. then
  564. echo "dtscr: usage: dtscr <command ...>"
  565. return 1
  566. fi
  567. local _cmdstr="`echo $@ | tr ' ' +`"
  568. local _tformat="%Y%m%d-%H%M%S%z"
  569. local _name="${pf}`date +${_tformat}`-${_cmdstr}"
  570. local _scr_file="${HOME}/${_name}.script"
  571. local _dt_dir="${TMP}/dtscr"
  572. install -d "$_dt_dir"
  573. dtach -n "${_dt_dir}/${_name}" script "${_scr_file_}" "$@"
  574. # echo $_name
  575. # echo $_file
  576. }
  577. mcrypt_stream(){
  578. test $# -eq 2 || return 1
  579. case $1 in
  580. en)
  581. mcrypt --key $2 | base64 ;;
  582. de)
  583. base64 -d | mcrypt -d --key $2 ;;
  584. esac
  585. }
  586. gpg_stream(){
  587. test $# -eq 2 || return 1
  588. case $1 in
  589. en)
  590. gpg --passphrase $2 -c --batch |base64 ;;
  591. de)
  592. base64 -d|gpg --passphrase $2 -d --batch ;;
  593. esac
  594. }
  595. dgpg(){
  596. if test "$1" = help || test -z "$2"
  597. then
  598. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  599. return
  600. fi
  601. local srcs="$2"
  602. local dsts="$3"
  603. test -z "$dsts" && dsts="${srcs}.out"
  604. local pw
  605. echo -n "dgpg pw: "
  606. read -s pw
  607. echo ""
  608. test -z "$pw" && return 1
  609. for f in *${srcs}
  610. do
  611. local d="$(basename "$f" "${srcs}")${dsts}"
  612. echo -n "Processing $f to $d..."
  613. if test -d "$f"
  614. then
  615. echo "`printf 'failed (%s is directory)' $f`"
  616. elif test -f "$d"
  617. then
  618. echo "`printf 'failed (%s is already exists)' $d`"
  619. elif <"$f" gpg_stream $1 $pw >"$d" 2>/dev/null
  620. then
  621. echo "done"
  622. else
  623. echo "failed"
  624. test -f "$d" && rm "$d"
  625. fi
  626. done
  627. }
  628. alias enst="gpg_stream en"
  629. alias dest="gpg_stream de"
  630. showinfo(){
  631. echo "Japanese letters are 表示可能"
  632. __safe_run diskinfo
  633. ! isdarwin && test -n "${DISPLAY}" && {
  634. __safe_run xrandr | \grep --color=never ^Screen
  635. }
  636. iswindows || __safe_run finger $USER
  637. LANG=C __safe_runc id
  638. __safe_run xset q
  639. }
  640. x(){
  641. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  642. #mkdir -p ~/.var/log
  643. # nohup startx >~/.var/log/xorg.log 2>&1 &
  644. # exit
  645. exec startx
  646. else
  647. echo "X cant be started! Another X is already running?" 1>&2
  648. fi
  649. }
  650. bak(){
  651. for file in "$@"
  652. do
  653. cp -v ${file} ${file}.bak
  654. done
  655. }
  656. di(){
  657. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  658. then
  659. local diffcmd=colordiff
  660. else
  661. local diffcmd=diff
  662. fi
  663. ${diffcmd} -u "$@" | ${PAGER}
  664. }
  665. tb(){
  666. local datenum=`date +%Y%m%d-%H%M%S`
  667. local tb="$HOME/.var/tb/$datenum"
  668. mkdir -p "$tb"
  669. for file in "$@"
  670. do
  671. mv -t "$tb" "$file"
  672. done
  673. }
  674. mkcd(){
  675. if test -z "$1"
  676. then
  677. echo "mkcd: usage: mkcd <dir>"
  678. return 1
  679. elif test -d "$1"
  680. then
  681. echo "Dir \"$1\" already exists."
  682. else
  683. mkdir -p "$1"
  684. echo "Dir \"$1\" created."
  685. fi
  686. cd "$1"
  687. }
  688. mkcdd(){
  689. # make and change date directory
  690. _d=`date +%Y%m%d-%H%M%S`
  691. mkcd "$_d"
  692. }
  693. if test -n "$TMUX" && null type reattach-to-user-namespace
  694. then
  695. alias pbpaste="reattach-to-user-namespace pbpaste"
  696. alias pbcopy="reattach-to-user-namespace pbcopy"
  697. fi
  698. catclip(){
  699. if iswindows
  700. then
  701. cat /dev/clipboard | tr -d \\r
  702. elif isdarwin
  703. then
  704. pbpaste
  705. else
  706. xclip -o -selection "clipboard"
  707. fi
  708. }
  709. setclip(){
  710. if test $# -eq 0
  711. then
  712. exec 3<&0
  713. else
  714. exec 3<<__EOF__
  715. `cat "$@"`
  716. __EOF__
  717. fi
  718. if iswindows
  719. then
  720. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  721. elif isdarwin
  722. then
  723. pbcopy 0<&3
  724. else
  725. 0<&3 xclip -i -f -selection "primary" | \
  726. xclip -i -f -selection "clipboard"
  727. fi
  728. exec 3<&-
  729. }
  730. open_file(){
  731. if iswindows
  732. then
  733. cmd.exe //c start "" "$@"
  734. elif isdarwin
  735. then
  736. touch "$@"
  737. open "$@"
  738. elif islinux
  739. then
  740. touch "$@"
  741. if null type pcmanfm; then
  742. LC_MESSAGES= pcmanfm "$@"
  743. else
  744. LC_MESSAGES= xdg-open "$@" &
  745. fi
  746. else
  747. cat "$@"
  748. fi
  749. }
  750. o(){
  751. if test $# -eq 0
  752. then
  753. open_file .
  754. else
  755. for f in "$@"
  756. do
  757. open_file "$(realpath "$f")"
  758. done
  759. fi
  760. }
  761. convmv_sjis2utf8_test(){
  762. convmv -r -f sjis -t utf8 *
  763. }
  764. convmv_sjis2utf8_notest(){
  765. convmv -r -f sjis -t utf8 * --notest
  766. }
  767. #################################################
  768. ## pastebin services
  769. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  770. sprunge(){
  771. # http://sprunge.us
  772. if test -z "$1"
  773. then
  774. curl -F 'sprunge=<-' http://sprunge.us
  775. else
  776. curl http://sprunge.us/$1
  777. fi
  778. }
  779. dpaste(){
  780. # http://dpaste.de
  781. if test -z "$1"
  782. then
  783. curl -F 'content=<-' https://dpaste.de/api/
  784. echo
  785. else
  786. curl https://dpaste.de/$1/raw/
  787. fi
  788. }
  789. ##########################
  790. # Zsh specific preferences
  791. if inzsh
  792. then
  793. # http://zsh.sourceforge.net/Guide/zshguide06.html#l147
  794. autoload -U compinit; compinit
  795. unsetopt auto_menu
  796. setopt bash_auto_list
  797. bindkey -e
  798. fi
  799. ######################################
  800. ## Prompt Settings
  801. __my_moc_state(){
  802. type mocp >/dev/null 2>&1 || return
  803. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  804. printf "$1" "`mocp -Q %title 2>/dev/null`"
  805. }
  806. __my_parse_svn_branch() {
  807. local LANG=C
  808. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  809. local svn_repository_root=$(svn info 2>/dev/null | \
  810. sed -ne 's#^Repository Root: ##p')
  811. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | \
  812. awk '{print $1}'
  813. }
  814. __my_svn_ps1(){
  815. if svn status >/dev/null 2>&1
  816. then
  817. local svn_branch=$(__my_parse_svn_branch)
  818. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  819. fi
  820. }
  821. __my_battery_status(){
  822. local dir=/sys/class/power_supply/BAT0
  823. if test -d $dir && test -r $dir/status && test -r $dir/charge_full && \
  824. test -r $dir/charge_now
  825. then
  826. local st=$(cat $dir/status)
  827. local full=$(cat $dir/charge_full)
  828. local now=$(cat $dir/charge_now)
  829. local rate=$(expr $now \* 100 / $full)
  830. printf "$1" "${st}:${rate}%"
  831. fi
  832. }
  833. alias bat='__my_battery_status %s\\n'
  834. ipaddress(){
  835. type ip >/dev/null 2>&1 || return 1
  836. local ip=$(LANG=C ip addr show scope global | \
  837. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  838. test -n "$ip" && printf $1 $ip
  839. }
  840. __my_ps1_str=""
  841. test -n "$__MY_SCRIPT" && __my_ps1_str="${__my_ps1_str}${__my_c5}SCR${__my_cdef} "
  842. test -n "$SSH_CONNECTION" && __my_ps1_str="${__my_ps1_str}${__my_c5}SSH${__my_cdef} "
  843. test -n "$__MY_DTACH" && __my_ps1_str="${__my_ps1_str}${__my_c5}DTACH${__my_cdef} "
  844. __my_ps1_scale(){
  845. if null type stty && ! ismsys
  846. then
  847. stty size | tr -d $'\n' | tr " " x
  848. printf " "
  849. fi
  850. }
  851. __my_ps1_tmux(){
  852. null type tmux || return $last
  853. local tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  854. test -n "$TMUX" && echo "[TMUX:$tmuxc]"
  855. }
  856. __my_ps1_moc(){
  857. __my_moc_state "[MOC:%s]"
  858. }
  859. for f in /usr/share/git/git-prompt.sh \
  860. /opt/local/share/git-core/git-prompt.sh \
  861. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  862. do
  863. test -r "$f" && inbash && . "$f" && break
  864. done
  865. GIT_PS1_SHOWDIRTYSTATE=t
  866. GIT_PS1_SHOWUPSTREAM=t
  867. __my_ps1_git(){
  868. null type __git_ps1 || return $last
  869. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  870. __git_ps1 "[GIT:$(__safe_run git config --get user.name):%s]"
  871. }
  872. __my_ps1_ipaddr(){
  873. ! iswindows && ipaddress [Addr:%s]
  874. }
  875. __my_ps1_bttry(){
  876. local bst="${TMP}/batterystatus"
  877. if test -z "$DISPLAY" && ! iswindows
  878. then
  879. test -f $bst && local bstr="$(cat $bst)"
  880. test -n "$bstr" && ! echo $bstr | grep 100 >/dev/null 2>&1 && \
  881. echo "[Battery:$bstr]"
  882. __my_battery_status %s >$bst &
  883. fi
  884. }
  885. __my_ps1_dirs(){
  886. dirs | wc -l
  887. }
  888. __my_ps1_jobs(){
  889. jobs | wc -l
  890. }
  891. __my_alert_fail(){
  892. test $laststatus -eq 0 || echo '!!! '
  893. }
  894. if test "$TERM" != dumb
  895. then
  896. __my_c1="\[\e[0;33m\]" # color for PWD
  897. __my_c2="\[\e[0;36m\]" # color for user
  898. __my_c3="\[\e[1;30m\]" # color for OLDPWD
  899. if test "`hostname`" = arch-aspireone; then __my_c4="\[\e[1;34m\]"
  900. elif test "`hostname`" = darwin-mba.local; then __my_c4="\[\e[1;31m\]"
  901. elif test "`hostname`" = newkiwi; then __my_c4="\[\e[1;35m\]"
  902. else __my_c4="\[\e[1;32m\]" # color for ::
  903. fi
  904. __my_c5="\[\e[30;47m\]" # color for SCR
  905. __my_cdef="\[\e[0m\]"
  906. fi
  907. _ps1_bash="\
  908. ${__my_c4}:: ${__my_cdef}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_git)\$(__my_ps1_bttry)\$(__my_ps1_ipaddr)\$(__my_ps1_moc)\n\
  909. ${__my_c4}:: ${__my_cdef}l${SHLVL}n\#j\js\$laststatus $(__my_ps1_scale)\D{%T} ${__my_ps1_str}\$(__my_alert_fail)\$ "
  910. inbash && PS1=$_ps1_bash
  911. __my_set_screen_title(){
  912. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  913. then
  914. echo -ne "\033k$1\033\\"
  915. fi
  916. }
  917. __my_set_title(){
  918. case $TERM in
  919. (rxvt*|xterm*|aterm|screen*)
  920. title="$(echo $@)"
  921. test -t 1 &&
  922. test -n "$DISPLAY" &&
  923. test -z "$EMACS" &&
  924. echo -n -e "\033]0;${title}\007"
  925. ;;
  926. esac
  927. }
  928. PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\:\${PWD};
  929. __my_set_screen_title \$(basename \"\$PWD\")/"
  930. PROMPT_COMMAND="laststatus=\$?;$PROMPT_COMMAND"
  931. laststatus=0