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.
 
 
 
 
 
 

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