Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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