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.
 
 
 
 
 
 

1303 lines
31 KiB

  1. #!/bin/sh
  2. ##########################################
  3. null(){
  4. "$@" >/dev/null 2>&1
  5. }
  6. __safe_run(){
  7. type $1 >/dev/null 2>&1 && "$@"
  8. }
  9. __match(){
  10. # __match str word
  11. # return 0 if word is found in str
  12. expr "$1" : ".*$2.*" >/dev/null
  13. }
  14. #################################
  15. # profile-like setups
  16. # aliases:
  17. # isinteractive: true if the current session is interactive
  18. # issourced: true if this file is sourced from another file (not so assured)
  19. # __firstload: true if this file is sourced for the first time (not so
  20. # assured)
  21. __safe_add_path_r(){
  22. test -d "$1" && PATH="${PATH}:$1"
  23. }
  24. __safe_add_path_l(){
  25. test -d "$1" && PATH="$1:${PATH}"
  26. }
  27. __safe_add_path_l "$HOME/.local/bin"
  28. __safe_add_path_l "$HOME/.local/lib/gems/bin"
  29. __safe_add_path_r "/c/mingw/bin"
  30. __safe_add_path_r "/c/mingw/msys/1.0/bin"
  31. # macports coreutils
  32. # isdarwin cannot be used it is not defined yet
  33. __safe_add_path_l "/opt/local/bin"
  34. __safe_add_path_l "/opt/local/sbin"
  35. __safe_add_path_l "/opt/local/libexec/gnubin"
  36. __safe_add_path_l \
  37. "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/bin"
  38. test -f "${__dotdir}/rc.py" && export PYTHONSTARTUP="${__dotdir}/rc.py"
  39. test -d "$HOME/.local/lib/python/site-packages" && \
  40. export PYTHONPATH="${PYTHONPATH}:${HOME}/.local/lib/python/site-packages"
  41. export GEM_HOME="$HOME/.local/lib/gems"
  42. export RUBYLIB="$RUBYLIB:$HOME/.local/lib/gems/lib"
  43. # it is not so good
  44. # http://archive.linux.or.jp/JF/JFdocs/Program-Library-HOWTO/shared-libraries.html
  45. # http://superuser.com/questions/324613/installing-a-library-locally-in-home-directory-but-program-doesnt-recognize-it
  46. # without this ENV i cannot run tmux. another way is to use --disable-shared
  47. # when building tmux
  48. if ! __match "$LD_LIBRARY_PATH" "$HOME/.local/lib"
  49. then
  50. export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$HOME/.local/lib"
  51. fi
  52. # in my environment powerdown does not work
  53. test -z "$SSH_CONNECTION" && \
  54. type setterm >/dev/null 2>&1 && \
  55. setterm -blank 30 -powersave on # -powerdown 10
  56. ##########################
  57. # system type
  58. # aliases:
  59. # ismsys, iscygwin iswindows, isdarwin, islinux,
  60. # with_coreutils, inbash, inzsh
  61. alias ismsys=false
  62. alias iscygwin=false
  63. alias iswindows=false
  64. alias isdarwin=false
  65. alias islinux=false
  66. alias with_coreutils=false # for mac
  67. case `uname` in
  68. (MINGW*) alias ismsys=true ;;
  69. (CYGWIN*) alias iscygwin=true ;;
  70. (Darwin*) alias isdarwin=true ;;
  71. (Linux*) alias islinux=true ;;
  72. esac
  73. null ls --version && alias with_coreutils=true
  74. ( ismsys || iscygwin ) && alias iswindows=true
  75. alias inbash=false
  76. alias inzsh=false
  77. if test -n "$BASH_VERSION"
  78. then
  79. alias inbash=true
  80. elif test -n "$ZSH_VERSION"
  81. then
  82. alias inzsh=true
  83. fi
  84. alias isinteractive=false
  85. __match "$-" i >/dev/null && alias isinteractive=true
  86. # alias issourced=true
  87. # expr "$0" : "^.*shrc$" >/dev/null && alias issourced=false # executed
  88. #################################
  89. # file pathes:
  90. # shrc: Path to this file
  91. # dotdir: Path to .dotfiles directory
  92. if inbash
  93. then
  94. __shrc="$BASH_SOURCE"
  95. elif inzsh
  96. then
  97. __shrc="$0"
  98. fi
  99. __dotdir="`dirname "$__shrc"`"
  100. __homelocal="$HOME/.local"
  101. __homevar="$HOME/.var"
  102. test -d "$__homelocal" || install -d "$__homelocal"
  103. test -d "$__homevar" || install -d "$__homevar"
  104. ##################################
  105. # EnvVal definitions
  106. export LANG=ja_JP.UTF-8
  107. export LC_MESSAGES=C
  108. export LC_TIME=C
  109. export TERMCAP="${TERMCAP}:vb="
  110. ismsys && export HOSTNAME
  111. # export ENV=~/.shrc
  112. if ! with_coreutils
  113. then
  114. export LSCOLORS=gxfxcxdxbxegedabagacad
  115. else
  116. # http://qiita.com/yuyuchu3333/items/84fa4e051c3325098be3
  117. null type dircolors && eval `dircolors`
  118. fi
  119. if false iswindows
  120. then
  121. export PAGER='tr -d \\r | less'
  122. else
  123. export PAGER="less"
  124. fi
  125. export LESS="-iRMX"
  126. # Style for lesspipe is defined in esc.style
  127. _src_hilite_lp_path="`which src-hilite-lesspipe.sh 2>/dev/null`"
  128. for f in /usr/share/source-highlight/src-hilite-lesspipe.sh
  129. do
  130. test -z "$_src_hilite_lp_path" && test -e "$f" && _src_hilite_lp_path="$f"
  131. done
  132. test -n "$_src_hilite_lp_path" && export LESSOPEN="| $_src_hilite_lp_path %s"
  133. if null type vim
  134. then
  135. export EDITOR=vim
  136. else
  137. export EDITOR=vi
  138. fi
  139. # export CDPATH=".:~"
  140. export VISUAL="$EDITOR"
  141. export GIT_PAGER="less -FS"
  142. export GIT_EDITOR="$EDITOR"
  143. export GIT_MERGE_AUTOEDIT=no
  144. if test -n "$TMUX" && \
  145. __match $TERM screen && \
  146. __match `tmux display -p '#{client_termname}'` 256color
  147. then
  148. TERM=screen-256color
  149. fi
  150. if test -z "$TMP"
  151. then
  152. if test -n "$TMPDIR"
  153. then
  154. export TMP=$TMPDIR
  155. elif test -n "$TEMP"
  156. then
  157. export TMP="$TEMP"
  158. else
  159. export TMP=/tmp
  160. fi
  161. fi
  162. __match "$TMP" "${USER}-tmp" >/dev/null || export TMP="${TMP}/${USER}-tmp"
  163. export TEMP="$TMP"
  164. test -d "$TMP" || install -d "$TMP"
  165. ! iswindows && null type stty && {
  166. stty stop undef # unbind C-s to stop displaying output
  167. # stty erase '^h'
  168. }
  169. if iswindows; then
  170. export USER=$USERNAME
  171. fi
  172. if test -d ~/dbx
  173. then
  174. export CHIT_PATH="$HOME/dbx/.chit"
  175. fi
  176. ##########################
  177. # Setups
  178. __download(){
  179. # download <url> <file>
  180. if type wget >/dev/null 2>&1
  181. then
  182. wget "$1" -O "$2"
  183. elif type curl >/dev/null 2>&1
  184. then
  185. curl --url "$1" --output "$2"
  186. fi
  187. }
  188. __mysetup_fetch_script(){
  189. url="$1"
  190. name="$2"
  191. type "$name" >/dev/null 2>&1 || {
  192. __download "$url" "$HOME/.local/bin/$name" &&
  193. chmod u+x "$HOME/.local/bin/$name"
  194. }
  195. }
  196. __mysetup_darwin_set_defaults(){
  197. isdarwin || return 1
  198. # http://appdrill.net/60641/mac-boot-mute.html
  199. #sudo nvram SystemAudioVolume=%80
  200. # add quit entry in menu
  201. defaults write com.apple.finder QuitMenuItem -bool YES
  202. # show full path on titlebar
  203. defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES
  204. # do not show desktop icons
  205. defaults write com.apple.finder CreateDesktop -boolean false
  206. killall Finder
  207. # disable dashboard
  208. #defaults write com.apple.dashboard mcx-disabled -bool YES
  209. }
  210. __mysetup_darwin_start_daemon(){
  211. isdarwin || return 1
  212. test "`launchctl getenv LC_ALL`" = C || sudo launchctl setenv LC_ALL C
  213. if ! (launchctl list | grep com.apple.locate) >/dev/null
  214. then
  215. sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
  216. fi
  217. }
  218. __mysetup_git_config(){
  219. if ! null type git
  220. then
  221. echo "git not found"
  222. return 1
  223. fi
  224. _gitconfig="git config --global"
  225. $_gitconfig user.name '10sr'
  226. $_gitconfig user.email '8slashes+git@gmail.com'
  227. $_gitconfig core.autocrlf false
  228. $_gitconfig core.excludesfile '~/.gitignore'
  229. $_gitconfig color.ui auto
  230. $_gitconfig status.relativePaths false
  231. $_gitconfig status.showUntrackedFiles normal
  232. $_gitconfig log.date iso
  233. null type xz && \
  234. $_gitconfig tar.txz.command "xz -c"
  235. $_gitconfig push.default current
  236. $_gitconfig alias.graph "log --graph --date-order -C -M --pretty=tformat:\"%C(green)%h%C(reset) %C(white)%ad%C(reset) %C(red)%an%C(reset)%C(yellow)%d%C(reset) %C(white bold)%s%C(reset)\" --all --date=iso -n 499"
  237. $_gitconfig alias.st "status -s -b"
  238. $_gitconfig alias.b "branch"
  239. $_gitconfig alias.sb "show-branch"
  240. $_gitconfig alias.ci "commit --verbose"
  241. $_gitconfig alias.co "checkout"
  242. $_gitconfig alias.cim "commit --verbose -m"
  243. $_gitconfig alias.di "diff --color"
  244. $_gitconfig alias.me "merge --no-ff --stat -v"
  245. $_gitconfig alias.gr "grep -n"
  246. $_gitconfig alias.ls "ls-files"
  247. # $_gitconfig alias.ls "ls-files -v --full-name"
  248. # $_gitconfig alias.ls "status -u -s ."
  249. $_gitconfig alias.sl "!sl"
  250. # $_gitconfig alias.my-ls "ls-files | xargs ls"
  251. # $_gitconfig alias.ll "!git ls-files | xargs ls -l -CFG --color=auto --time-style=long-iso"
  252. $_gitconfig alias.addi "add -i"
  253. $_gitconfig alias.clean-p "!test -z \"\$(git status -s -uno)\""
  254. $_gitconfig alias.newb "checkout -b"
  255. $_gitconfig alias.endb \
  256. "!sh -cx 'git stash && git checkout master && git merge --no-ff -'"
  257. #$_gitconfig alias.wc "!git ls-files -z | xargs -0 wc"
  258. # $_gitconfig push.default "simple"
  259. if iswindows; then
  260. $_gitconfig core.fileMode false
  261. fi
  262. }
  263. __mysetup_mkdirs(){
  264. install -d "$HOME/.local/bin"
  265. }
  266. __mysetup(){
  267. set -x
  268. __mysetup_mkdirs
  269. __mysetup_fetch_script \
  270. https://gist.github.com/10sr/6852317/raw/colortable16.sh colortable16.sh
  271. __mysetup_fetch_script \
  272. https://gist.github.com/10sr/6852331/raw/256colors2.pl 256colors2.pl
  273. if isdarwin
  274. then
  275. __mysetup_darwin_set_defaults
  276. __mysetup_darwin_start_daemon
  277. fi
  278. set +x
  279. }
  280. #######################
  281. if ! isinteractive
  282. then
  283. if test "$1" = setup
  284. then
  285. __mysetup
  286. exit 0
  287. fi
  288. # if this file is sourced return, if executed directly exit
  289. return 2>/dev/null || exit
  290. fi
  291. ######################
  292. # Print welcome messages
  293. iswindows && alias tty="echo cmd.exe"
  294. type fortune >/dev/null 2>&1 && {
  295. fortune
  296. echo
  297. fortune -o
  298. echo
  299. }
  300. uname -a
  301. echo TERM $TERM $(tput colors) colors connected to $(tty), \
  302. running $BASH $BASH_VERSION
  303. if test -n "$TMUX"
  304. then
  305. tmux display -p 'Using tmux #S:#I:#W.#P, client is #{client_termname}' \
  306. 2>/dev/null
  307. echo
  308. fi
  309. ###################################
  310. # some aliases and functions
  311. # __func_name: never used interactively
  312. # _func_name: usually not used interactively
  313. __safe_alias(){
  314. # __safe_alias <name>=<command>
  315. _bin=`expr "$1" : '^[^=]*=\([^ ]*\)'`
  316. test -n "$_bin" && \
  317. null type $_bin && \
  318. alias "$1"
  319. }
  320. ( ! with_coreutils && isdarwin ) || test "$TERM" = dumb || \
  321. _coloroption=" --color=auto"
  322. ( ! with_coreutils && isdarwin ) || iswindows || \
  323. _timeoption=" --time-style=long-iso"
  324. ( ! with_coreutils && isdarwin ) || _hideoption=" --hide=[A-Z]*" # do not use
  325. _timeformat_iso="%Y-%m-%dT%H:%M:%S%z"
  326. _timeformat_rfc2822="%a, %d %b %Y %T %z"
  327. _timeformat_num="%Y%m%d%H%M%S"
  328. alias datenum="date +$_timeformat_num"
  329. alias ls="ls -hCF${_coloroption}${_timeoption}"
  330. # export GREP_OPTIONS=""
  331. alias gr="grep -n --color=always"
  332. iswindows && alias grep="grep -n"
  333. # alias ll="ls -l"
  334. # alias la="ls -A"
  335. # alias lla="ls -Al"
  336. alias less="less -F"
  337. __safe_alias em="emacs -nw"
  338. __safe_alias vi=vim
  339. alias pstree="LANG=C pstree"
  340. alias cp="cp -v"
  341. alias mv="mv -v"
  342. alias rm="rm -v"
  343. alias psaux="ps auxww"
  344. alias q=exit
  345. __safe_alias e3=e3em
  346. #alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
  347. alias po=popd
  348. alias pu=pushd
  349. __safe_alias sudo="sudo " # use aliases through sudo
  350. __safe_alias sudoe="sudoedit"
  351. # __safe_alias halt="sudo halt"
  352. # __safe_alias reboot="sudo reboot"
  353. null type dbus-send && {
  354. alias suspend="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  355. /org/freedesktop/UPower org.freedesktop.UPower.Suspend"
  356. alias hibernate="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  357. /org/freedesktop/UPower org.freedesktop.UPower.Hibernate"
  358. }
  359. alias rand="echo \$RANDOM"
  360. __safe_alias xunp="file-roller -h"
  361. __safe_alias pc="sudo \paco -D"
  362. alias pycalc="python -i -c 'from math import *' "
  363. __safe_alias py3=python3
  364. __safe_alias py2=python2
  365. alias _reloadrc="exec \"$SHELL\""
  366. # alias mytime="date +%Y%m%d-%H%M%S"
  367. alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
  368. # type trash >/dev/null 2>&1 && alias rm=trash
  369. __safe_alias mpg123="mpg123 -C -v --title"
  370. __safe_alias xm="xmms2"
  371. #export PLAYER="mpg123 -C -v --title"
  372. __safe_alias screen="screen -e^z^z"
  373. #alias zcd="cd \`zenity --file-selection --directory\`"
  374. __safe_alias gtags="gtags --verbose"
  375. __safe_alias htags="htags --xhtml --symbol --line-number \
  376. --frame --alphabet --verbose"
  377. __safe_alias au=aunpack
  378. __safe_alias lv="lv|less"
  379. __safe_alias rs="rsync --progress --itemize-changes --compress"
  380. iscygwin && __safe_alias wget="wget --no-check-certificate"
  381. isdarwin && alias updatedb="LC_ALL=C updatedb"
  382. # do not use locate installed by macports
  383. isdarwin && test -x /usr/bin/locate && alias locate="/usr/bin/locate"
  384. cd(){
  385. command cd "$@"
  386. pwd
  387. }
  388. # pad
  389. alias pad=notepad
  390. __safe_alias pad=gedit
  391. __safe_alias pad=leafpad
  392. isdarwin && alias pad="open -e"
  393. __safe_alias wic=wicd-curses
  394. __safe_alias wil="wicd-cli -y -l | head"
  395. #alias wicn="wicd-cli -y -c -n"
  396. wicn(){
  397. if test $# -eq 0
  398. then
  399. local num
  400. wicd-cli -y -l | head
  401. echo -n "input num: "
  402. read num
  403. test -n "$num" && wicd-cli -y -c -n $num
  404. else
  405. wicd-cli -y -c -n $1
  406. fi
  407. }
  408. __find_latest_vimdir(){
  409. vimdir=/usr/share/vim
  410. if test -d "$vimdir"
  411. then
  412. find "$vimdir" -name 'vim??' -type d | sort | tail -n 1
  413. else
  414. echo ""
  415. fi
  416. }
  417. for f in /usr/share/vim/vimcurrent "`__find_latest_vimdir`"
  418. do
  419. test -n "$f" || continue
  420. f="$f/macros/less.sh"
  421. test -f $f && alias vl=$f && break
  422. done
  423. alias pa=pacapt
  424. __safe_alias yt=yaourt
  425. __safe_alias cower="cower --color=auto"
  426. null type pacmatic && {
  427. alias pacman="pacmatic"
  428. export PACMAN="pacmatic"
  429. }
  430. __my_pacman_update_mirrorlist_with_reflector(){
  431. ml=/etc/pacman.d/mirrorlist
  432. cmd="$(expr "$(grep -m 1 reflector $ml)" : '# With: *\(.*\)')"
  433. if test -z "$cmd"
  434. then
  435. cmd="reflector --verbose -l 5 --sort rate --save $ml"
  436. fi
  437. echo "Running $cmd ..." 1>&2
  438. sudo $cmd
  439. }
  440. null type reflector && test -f /etc/pacman.d/mirrorlist && \
  441. alias reflect_mirrorlist=__my_pacman_update_mirrorlist_with_reflector
  442. null type apt-get && {
  443. alias aupgrade="sudo apt-get autoremove --yes && \
  444. sudo apt-get update --yes && sudo apt-get upgrade --yes"
  445. alias aptin="apt-get install"
  446. alias aptsearch="apt-cache search"
  447. alias aptshow="apt-cache show"
  448. }
  449. null type port && {
  450. alias port="port -v"
  451. alias pupgrade="sudo port -v selfupdate && \
  452. { sudo port -v upgrade outdated; }"
  453. }
  454. if iscygwin; then
  455. null type windate || \
  456. alias windate="cmd.exe //c 'echo %DATE%-%TIME%'"
  457. # alias cygsu="cygstart /cygwinsetup.exe"
  458. # alias ls="ls -CFG $(iswindows || test "$TERM" = dumb || echo --color=auto)"
  459. fi
  460. g(){
  461. if test $# -eq 0 && null type git-info
  462. then
  463. git info
  464. else
  465. git -c color.ui=always "$@"
  466. fi
  467. }
  468. if null type _git && inbash
  469. then
  470. # enable programmable completion for g
  471. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  472. || complete -o default -o nospace -F _git g
  473. fi
  474. #git svn --help >/dev/null 2>&1 && alias gsvn="git svn"
  475. __safe_alias m=gitmemo
  476. alias setup.py3="sudo python3 setup.py install --record files.txt"
  477. randomstr(){
  478. len=$1
  479. test -z "$len" && len=8
  480. uuidgen | tr -d - | cut -c 1-len
  481. }
  482. datestr(){
  483. # datestr yyyyMMdd-hhmmss
  484. if test -z "$1" || test "$1" == "-h"
  485. then
  486. echo "datestr: usage: datestr <yyyyMMddhhmmss>"
  487. return 1
  488. fi
  489. dfmt= # actual format for date command
  490. while test -n "$1"
  491. do
  492. fmt="$1"
  493. while test -n "$fmt"
  494. do
  495. case "$fmt" in
  496. yyyy*) # year
  497. dfmt="${dfmt}%Y"
  498. fmt="`echo "$fmt" | cut -c 5-`"
  499. ;;
  500. yy*) # last two digits of year
  501. dfmt="${dfmt}%y"
  502. fmt="`echo "$fmt" | cut -c 3-`"
  503. ;;
  504. MM*) # month (01..12)
  505. dfmt="${dfmt}%m"
  506. fmt="`echo "$fmt" | cut -c 3-`"
  507. ;;
  508. dd*) # day of month (01..12)
  509. dfmt="${dfmt}%d"
  510. fmt="`echo "$fmt" | cut -c 3-`"
  511. ;;
  512. HH* | hh*) # hour (00..23)
  513. dfmt="${dfmt}%H"
  514. fmt="`echo "$fmt" | cut -c 3-`"
  515. ;;
  516. mm*) # minute (00..59)
  517. dfmt="${dfmt}%M"
  518. fmt="`echo "$fmt" | cut -c 3-`"
  519. ;;
  520. ss*) # second (00..60)
  521. dfmt="${dfmt}%S"
  522. fmt="`echo "$fmt" | cut -c 3-`"
  523. ;;
  524. *)
  525. char=`echo "$fmt" | cut -c 1`
  526. dfmt="${dfmt}${char}"
  527. fmt="`echo "$fmt" | cut -c 2-`"
  528. ;;
  529. esac
  530. done
  531. shift
  532. done
  533. date +"$dfmt"
  534. }
  535. ssh(){
  536. __my_set_screen_title ssh
  537. command ssh "$@"
  538. }
  539. __ssh_with_cd(){
  540. # __ssh_with_cd <host> <directory> [<arg> ...]
  541. if test -z "$2"
  542. then
  543. echo "usage: __ssh_with_cd <host> <directory> [<arg> ...]"
  544. return 1
  545. fi
  546. host="$1"
  547. shift
  548. dir="$1"
  549. shift
  550. ssh "$host" "$@" -t "cd \"$dir\"; \$SHELL -l"
  551. }
  552. memo(){
  553. if test -z "$1"
  554. then
  555. $EDITOR memo.txt
  556. else
  557. $EDITOR "$1/memo.txt"
  558. fi
  559. }
  560. now(){
  561. local tformat="%Y/%m/%d %H:%M:%S %z"
  562. cal
  563. REPLY=
  564. printf "\\r`date "+${tformat}"`"
  565. read -t 1
  566. while test $? -ne 0
  567. do
  568. printf "\\r`date "+${tformat}"`"
  569. read -t 1
  570. done
  571. }
  572. s(){
  573. if git rev-parse --git-dir >/dev/null 2>&1
  574. then
  575. echo ">> git grep -n $@" 1>&2
  576. git grep -n "$@"
  577. elif which ag >/dev/null 2>&1
  578. then
  579. echo ">> ag --pager=\"$PAGER\" $@" 1>&2
  580. ag --pager="$PAGER" "$@"
  581. elif which ack >/dev/null 2>&1
  582. then
  583. echo ">> ack --pager=\"$PAGER\" $@" 1>&2
  584. ack --pager="$PAGER" "$@"
  585. else
  586. echo \
  587. ">> find . " \
  588. "-path '*/.git' -prune -o" \
  589. "-path '*/.svn' -prune -o" \
  590. "-type f -exec grep -nH -e --color=always $@ {} +" 1>&2
  591. if test $# -eq 0
  592. then
  593. echo "No search word given." 1>&2
  594. return 1
  595. fi
  596. find . \
  597. -path '*/.git' -prune -o \
  598. -path '*/.svn' -prune -o \
  599. -type -f -exec grep -nH -e --color=always "$@" {} + \
  600. | $PAGER
  601. fi
  602. }
  603. man(){
  604. env \
  605. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  606. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  607. LESS_TERMCAP_me=$(printf "\e[0m") \
  608. LESS_TERMCAP_se=$(printf "\e[0m") \
  609. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  610. LESS_TERMCAP_ue=$(printf "\e[0m") \
  611. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  612. man "$@"
  613. }
  614. netwait(){
  615. while ! ping -c 1 -t 1 example.com
  616. do
  617. true
  618. done
  619. echo network works.
  620. }
  621. __realpath(){
  622. if type realpath >/dev/null 2>&1
  623. then
  624. command realpath "$@"
  625. else
  626. while ! test -d $1
  627. do
  628. shift
  629. done
  630. (command cd "$d" && echo "$PWD")
  631. # local d="$OLDPWD"
  632. # command cd "$1"
  633. # echo "$PWD"
  634. # command cd "$d"
  635. fi
  636. }
  637. tx(){
  638. if test $# -eq 0
  639. then
  640. echo ":: tx <session> to attach."
  641. tmux ls
  642. elif tmux has -t "$1"
  643. then
  644. tmux attach -t "$1"
  645. else
  646. tmux new -s "$1"
  647. fi
  648. }
  649. _tmux_prefs(){
  650. null type tmux || return 1
  651. tmux set -g mode-keys vi
  652. }
  653. dt(){
  654. # dt [<name>] [<command ...>]
  655. __dtach_dir="${TMP}/dtach"
  656. install -d "${__dtach_dir}"
  657. if test -n "${__MY_DTACH}"
  658. then
  659. echo "Current session: ${__MY_DTACH}"
  660. fi
  661. if test -z "$1"
  662. then
  663. echo "Sessions:"
  664. ls "${__dtach_dir}"
  665. return 0
  666. elif test "$1" = "-h"
  667. then
  668. echo "dt: usage: dt <name> [<command ...>]" 1>&2
  669. return 1
  670. fi
  671. soc_name="${__dtach_dir}/$1"
  672. shift
  673. if test -n "$__MY_DTACH"
  674. then
  675. echo "dtach session cannot be nested." 1>&2
  676. return 1
  677. elif test -S "$soc_name"
  678. then
  679. dtach -a "$soc_name" -e ^^
  680. elif test -e "$soc_name"
  681. then
  682. echo "dt: File named $soc_name already exists."
  683. return 1
  684. elif test -z "$1"
  685. then
  686. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
  687. # echo "dt: Socket named $soc_name not exists and no command specified."
  688. # return 1
  689. else
  690. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
  691. fi
  692. }
  693. scr(){
  694. test -n "$1" && pf="${1}-"
  695. local _tformat="%Y%m%d-%H%M%S%z"
  696. local _file="${HOME}/${pf}`date +${_tformat}`.script"
  697. __MY_SCRIPT=${_file} script ${_file} "$@"
  698. }
  699. dtscr(){
  700. # dtscr <command ...>
  701. if test -z "$1"
  702. then
  703. echo "dtscr: usage: dtscr <command ...>"
  704. return 1
  705. fi
  706. local _cmdstr="`echo $@ | tr ' ' +`"
  707. local _tformat="%Y%m%d-%H%M%S%z"
  708. local _name="${pf}`date +${_tformat}`-${_cmdstr}"
  709. local _scr_file="${HOME}/${_name}.script"
  710. local _dt_dir="${TMP}/dtscr"
  711. install -d "$_dt_dir"
  712. dtach -n "${_dt_dir}/${_name}" script "${_scr_file_}" "$@"
  713. # echo $_name
  714. # echo $_file
  715. }
  716. mcrypt_stream(){
  717. test $# -eq 2 || return 1
  718. case $1 in
  719. en)
  720. mcrypt --key $2 | base64 ;;
  721. de)
  722. base64 -d | mcrypt -d --key $2 ;;
  723. esac
  724. }
  725. gpg_stream(){
  726. test $# -eq 2 || return 1
  727. case $1 in
  728. en)
  729. gpg --passphrase $2 -c --batch |base64 ;;
  730. de)
  731. base64 -d|gpg --passphrase $2 -d --batch ;;
  732. esac
  733. }
  734. dgpg(){
  735. if test "$1" = help || test -z "$2"
  736. then
  737. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  738. return
  739. fi
  740. local srcs="$2"
  741. local dsts="$3"
  742. test -z "$dsts" && dsts="${srcs}.out"
  743. local pw
  744. echo -n "dgpg pw: "
  745. read -s pw
  746. echo ""
  747. test -z "$pw" && return 1
  748. for f in *${srcs}
  749. do
  750. local d="$(basename "$f" "${srcs}")${dsts}"
  751. echo -n "Processing $f to $d..."
  752. if test -d "$f"
  753. then
  754. echo "`printf 'failed (%s is directory)' $f`"
  755. elif test -f "$d"
  756. then
  757. echo "`printf 'failed (%s is already exists)' $d`"
  758. elif <"$f" gpg_stream $1 $pw >"$d" 2>/dev/null
  759. then
  760. echo "done"
  761. else
  762. echo "failed"
  763. test -f "$d" && rm "$d"
  764. fi
  765. done
  766. }
  767. alias enst="gpg_stream en"
  768. alias dest="gpg_stream de"
  769. showinfo(){
  770. echo "Japanese letters are 表示可能"
  771. __safe_run diskinfo
  772. ! isdarwin && test -n "${DISPLAY}" && {
  773. __safe_run xrandr | \grep --color=never ^Screen
  774. }
  775. iswindows || __safe_run finger $USER
  776. LANG=C __safe_runc id
  777. __safe_run xset q
  778. }
  779. x(){
  780. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  781. #mkdir -p ~/.var/log
  782. # nohup startx >~/.var/log/xorg.log 2>&1 &
  783. # exit
  784. exec startx
  785. else
  786. echo "X cant be started! Another X is already running?" 1>&2
  787. fi
  788. }
  789. bak(){
  790. for file in "$@"
  791. do
  792. cp -v ${file} ${file}.bak
  793. done
  794. }
  795. di(){
  796. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  797. then
  798. local diffcmd=colordiff
  799. else
  800. local diffcmd=diff
  801. fi
  802. ${diffcmd} -u "$@" | ${PAGER}
  803. }
  804. tb(){
  805. __datenum=`date +%Y%m%d-%H%M%S`
  806. __tb="$HOME/.var/tb/$__datenum"
  807. install -d "$__tb"
  808. for file in "$@"
  809. do
  810. mv -t "$__tb" "$file"
  811. done
  812. }
  813. mkcd(){
  814. if test -z "$1"
  815. then
  816. echo "mkcd: usage: mkcd <dir>"
  817. return 1
  818. elif test -d "$1"
  819. then
  820. echo "Dir \"$1\" already exists."
  821. else
  822. install -d "$1"
  823. echo "Dir \"$1\" created."
  824. fi
  825. cd "$1"
  826. }
  827. mkcdd(){
  828. # make and change date directory
  829. _d=`date +%Y%m%d-%H%M%S`
  830. mkcd "$_d"
  831. }
  832. if test -n "$TMUX" && null type reattach-to-user-namespace
  833. then
  834. alias pbpaste="reattach-to-user-namespace pbpaste"
  835. alias pbcopy="reattach-to-user-namespace pbcopy"
  836. fi
  837. catclip(){
  838. if iswindows
  839. then
  840. cat /dev/clipboard | tr -d \\r
  841. elif isdarwin
  842. then
  843. pbpaste
  844. else
  845. xclip -o -selection "clipboard"
  846. fi
  847. }
  848. setclip(){
  849. if test $# -eq 0
  850. then
  851. exec 3<&0
  852. else
  853. exec 3<<__EOF__
  854. `cat "$@"`
  855. __EOF__
  856. fi
  857. if iswindows
  858. then
  859. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  860. elif isdarwin
  861. then
  862. pbcopy 0<&3
  863. else
  864. 0<&3 xclip -i -f -selection "primary" | \
  865. xclip -i -f -selection "clipboard"
  866. fi
  867. exec 3<&-
  868. }
  869. open_file(){
  870. if iscygwin
  871. then
  872. cygstart "$@"
  873. elif ismsys
  874. then
  875. cmd.exe //c start "" "$@"
  876. elif isdarwin
  877. then
  878. touch "$@"
  879. open "$@"
  880. elif islinux
  881. then
  882. touch "$@"
  883. if null type pcmanfm; then
  884. LC_MESSAGES= pcmanfm "$@"
  885. else
  886. LC_MESSAGES= xdg-open "$@" &
  887. fi
  888. else
  889. cat "$@"
  890. fi
  891. }
  892. o(){
  893. if test $# -eq 0
  894. then
  895. open_file .
  896. else
  897. for f in "$@"
  898. do
  899. open_file "$(realpath "$f")"
  900. done
  901. fi
  902. }
  903. convmv_sjis2utf8_test(){
  904. convmv -r -f sjis -t utf8 *
  905. }
  906. convmv_sjis2utf8_notest(){
  907. convmv -r -f sjis -t utf8 * --notest
  908. }
  909. #################################################
  910. ## pastebin services
  911. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  912. sprunge(){
  913. # http://sprunge.us
  914. if test -z "$1"
  915. then
  916. curl -F 'sprunge=<-' http://sprunge.us
  917. else
  918. curl http://sprunge.us/$1
  919. fi
  920. }
  921. dpaste(){
  922. # http://dpaste.de
  923. if test -z "$1"
  924. then
  925. curl -F 'content=<-' https://dpaste.de/api/
  926. echo
  927. else
  928. curl https://dpaste.de/$1/raw/
  929. fi
  930. }
  931. ##########################
  932. # Zsh specific preferences
  933. if inzsh
  934. then
  935. bindkey -e
  936. # http://zsh.sourceforge.net/Guide/zshguide06.html#l147
  937. autoload compinit; compinit
  938. unsetopt auto_menu
  939. zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
  940. setopt bash_auto_list
  941. autoload colors; colors
  942. autoload -Uz promptinit
  943. promptinit
  944. prompt walters
  945. fi
  946. ######################################
  947. ## Prompt Settings
  948. __my_moc_state(){
  949. type mocp >/dev/null 2>&1 || return
  950. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  951. printf "$1" "`mocp -Q %title 2>/dev/null`"
  952. }
  953. __my_parse_svn_branch() {
  954. local LANG=C
  955. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  956. local svn_repository_root=$(svn info 2>/dev/null | \
  957. sed -ne 's#^Repository Root: ##p')
  958. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | \
  959. awk '{print $1}'
  960. }
  961. __my_svn_ps1(){
  962. if svn status >/dev/null 2>&1
  963. then
  964. local svn_branch=$(__my_parse_svn_branch)
  965. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  966. fi
  967. }
  968. __my_battery_status(){
  969. local dir=/sys/class/power_supply/BAT0
  970. if test -d $dir && test -r $dir/status && test -r $dir/charge_full && \
  971. test -r $dir/charge_now
  972. then
  973. local st=$(cat $dir/status)
  974. local full=$(cat $dir/charge_full)
  975. local now=$(cat $dir/charge_now)
  976. local rate=$(expr $now \* 100 / $full)
  977. printf "$1" "${st}:${rate}%"
  978. fi
  979. }
  980. alias bat='__my_battery_status %s\\n'
  981. ipaddress(){
  982. type ip >/dev/null 2>&1 || return 1
  983. local ip=$(LANG=C ip addr show scope global | \
  984. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  985. test -n "$ip" && printf $1 $ip
  986. }
  987. __my_ps1_str=""
  988. test -n "$__MY_SCRIPT" && __my_ps1_str="${__my_ps1_str}${__my_c5}SCR${__my_cdef} "
  989. test -n "$SSH_CONNECTION" && __my_ps1_str="${__my_ps1_str}${__my_c5}SSH${__my_cdef} "
  990. test -n "$__MY_DTACH" && __my_ps1_str="${__my_ps1_str}${__my_c5}DTACH${__my_cdef} "
  991. __my_ps1_scale(){
  992. if null type stty && ! ismsys
  993. then
  994. stty size | tr -d $'\n' | tr " " x
  995. printf " "
  996. fi
  997. }
  998. __my_ps1_tmux(){
  999. null type tmux || return $last
  1000. local tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  1001. test -n "$TMUX" && echo "[TMUX:$tmuxc]"
  1002. }
  1003. __my_ps1_moc(){
  1004. __my_moc_state "[MOC:%s]"
  1005. }
  1006. for f in /usr/share/git/git-prompt.sh \
  1007. /opt/local/share/git-core/git-prompt.sh \
  1008. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  1009. do
  1010. test -r "$f" && inbash && . "$f" && break
  1011. done
  1012. GIT_PS1_SHOWDIRTYSTATE=t
  1013. GIT_PS1_SHOWUPSTREAM=t
  1014. __my_ps1_git(){
  1015. null type __git_ps1 || return $last
  1016. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  1017. __git_ps1 "[GIT:$(__safe_run git config --get user.name):%s]"
  1018. }
  1019. __my_ps1_ipaddr(){
  1020. ! iswindows && ipaddress [Addr:%s]
  1021. }
  1022. __my_ps1_bttry(){
  1023. local bst="${TMP}/batterystatus"
  1024. if test -z "$DISPLAY" && ! iswindows
  1025. then
  1026. test -f $bst && local bstr="$(cat $bst)"
  1027. test -n "$bstr" && ! echo $bstr | grep 100 >/dev/null 2>&1 && \
  1028. echo "[Battery:$bstr]"
  1029. __my_battery_status %s >$bst &
  1030. fi
  1031. }
  1032. __my_ps1_dirs(){
  1033. dirs | wc -l
  1034. }
  1035. __my_ps1_jobs(){
  1036. jobs | wc -l
  1037. }
  1038. __my_alert_fail(){
  1039. test $laststatus -eq 0 || echo '!!! '
  1040. }
  1041. # About ansi escape sequences
  1042. # http://archive.linux.or.jp/JF/JFdocs/Bash-Prompt-HOWTO-5.html
  1043. # http://www.grapecity.com/japan/powernews/column/clang/047/page02.htm
  1044. __attr_beg='\[\033['
  1045. __attr_end='m\]'
  1046. __color_default="${__attr_beg}0${__attr_end}"
  1047. __color_black="${__attr_beg}0;30${__attr_end}"
  1048. __color_red="${__attr_beg}0;31${__attr_end}"
  1049. __color_green="${__attr_beg}0;32${__attr_end}"
  1050. __color_brown="${__attr_beg}0;33${__attr_end}"
  1051. __color_blue="${__attr_beg}0;34${__attr_end}"
  1052. __color_purple="${__attr_beg}0;35${__attr_end}"
  1053. __color_cyan="${__attr_beg}0;36${__attr_end}"
  1054. __color_light_gray="${__attr_beg}0;37${__attr_end}"
  1055. __color_dark_gray="${__attr_beg}1;30${__attr_end}"
  1056. __color_light_red="${__attr_beg}1;31${__attr_end}"
  1057. __color_light_green="${__attr_beg}1;32${__attr_end}"
  1058. __color_yellow="${__attr_beg}1;33${__attr_end}"
  1059. __color_light_blue="${__attr_beg}1;34${__attr_end}"
  1060. __color_light_purple="${__attr_beg}1;35${__attr_end}"
  1061. __color_light_cyan="${__attr_beg}1;36${__attr_end}"
  1062. __color_white="${__attr_beg}1;37${__attr_end}"
  1063. __color_bg_black="${__attr_beg}40${__attr_end}"
  1064. __color_bg_red="${__attr_beg}41${__attr_end}"
  1065. __color_bg_green="${__attr_beg}42${__attr_end}"
  1066. __color_bg_brown="${__attr_beg}43${__attr_end}"
  1067. __color_bg_blue="${__attr_beg}44${__attr_end}"
  1068. __color_bg_purple="${__attr_beg}45${__attr_end}"
  1069. __color_bg_cyan="${__attr_beg}46${__attr_end}"
  1070. __color_bg_light_gray="${__attr_beg}47${__attr_end}"
  1071. __attr_underline="${__attr_beg}4${__attr_end}"
  1072. __attr_reverse="${__attr_beg}7${__attr_end}"
  1073. __attr_bold="${__attr_beg}1${__attr_end}"
  1074. # NOTE: tput is another easy way to set colors and background
  1075. # For example, "$(tput setab 4)text$(tput sgr0)" print text with background
  1076. # color blue.
  1077. if test "$TERM" != dumb
  1078. then
  1079. __my_c1="$__attr_bold$__attr_underline" # color for PWD
  1080. __my_c2="$__attr_bold$__attr_underline" # color for user and hostname
  1081. # color for ::
  1082. case "`hostname`" in
  1083. arch-aspireone)
  1084. __my_c4="$__color_light_blue"
  1085. ;;
  1086. darwin-mba.local)
  1087. __my_c4="$__color_light_cyan"
  1088. ;;
  1089. newkiwi)
  1090. __my_c4="$__color_light_purple"
  1091. ;;
  1092. *)
  1093. __my_c4="$__color_light_green"
  1094. ;;
  1095. esac
  1096. __my_c5="$__color_black$__color_bg_light_gray" # color for SCR
  1097. __my_cdef="$__color_default"
  1098. fi
  1099. _ps1_bash="\
  1100. ${__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\
  1101. ${__my_c4}:: ${__my_cdef}l${SHLVL}n\#j\js\$laststatus $(__my_ps1_scale)\D{%T} ${__my_ps1_str}\$(__my_alert_fail)\$ "
  1102. inbash && PS1=$_ps1_bash
  1103. _ps1_zsh="$_ps1_bash"
  1104. #inzsh && PS1="$_ps1_zsh"
  1105. __my_set_header_line(){
  1106. # save current position
  1107. printf "\033[s"
  1108. printf "\033[0;0H"
  1109. printf "\033[K"
  1110. printf "\033[7m"
  1111. printf "$1"
  1112. printf "\033[0m"
  1113. # restore saved position
  1114. printf "\033[u"
  1115. }
  1116. __my_set_screen_title(){
  1117. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  1118. then
  1119. echo -ne "\033k$1\033\\"
  1120. fi
  1121. }
  1122. __my_set_title(){
  1123. case $TERM in
  1124. (rxvt*|xterm*|aterm|screen*)
  1125. test -t 1 &&
  1126. test -n "$DISPLAY" &&
  1127. test -z "$EMACS" &&
  1128. echo -n -e "\033]0;$1\007"
  1129. ;;
  1130. esac
  1131. }
  1132. PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\:\${PWD};
  1133. __my_set_screen_title \$(basename \"\$PWD\")/"
  1134. PROMPT_COMMAND="laststatus=\$?;$PROMPT_COMMAND"
  1135. laststatus=0