選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

1289 行
30 KiB

  1. #!/bin/sh
  2. # TODO: decide the naming scheme of variables (global, local, ...)
  3. expr "$-" : '^.*i' >/dev/null || return
  4. ##########################################
  5. __shrc_lock=/tmp/shrc_lock.$USER.$$
  6. test -f "$__shrc_lock" && return
  7. touch "$__shrc_lock"
  8. ##########################################
  9. null(){
  10. "$@" >/dev/null 2>&1
  11. }
  12. __safe_run(){
  13. type $1 >/dev/null 2>&1 && "$@"
  14. }
  15. __match(){
  16. # __match str word
  17. # return 0 if word is found in str
  18. expr "$1" : ".*$2.*" >/dev/null
  19. }
  20. __ln=$'\n'
  21. __cr=$'\r'
  22. test -n "$TMP" || export TMP=/tmp/${USER}-tmp
  23. mkdir -p "$TMP"
  24. __homerun="$HOME/.var/run"
  25. mkdir -p "$__homerun"
  26. ##########################
  27. # system type
  28. gnu_coreutils=false # for mac
  29. null ls --version && gnu_coreutils=true
  30. inbash=false
  31. inzsh=false
  32. if test -n "$BASH_VERSION"
  33. then
  34. inbash=true
  35. elif test -n "$ZSH_VERSION"
  36. then
  37. inzsh=true
  38. fi
  39. #################################
  40. # file pathes:
  41. # shrc: Path to this file
  42. if $inbash
  43. then
  44. __shrc="$BASH_SOURCE"
  45. elif $inzsh
  46. then
  47. __shrc="$0"
  48. fi
  49. #########################
  50. # system detection
  51. __system_shrc="$__homerun/system.shrc"
  52. if test -f "$__system_shrc"
  53. then
  54. . "$__system_shrc"
  55. else
  56. ismsys=false
  57. iscygwin=false
  58. iswindows=false
  59. isdarwin=false
  60. isfreebsd=false
  61. isbsd=false
  62. islinux=false
  63. # $OSTYPE is another choice. which is better?
  64. # NOTE: sh on FreeBSD does not define OSTYPE
  65. case `uname` in
  66. MINGW*) ismsys=true ;;
  67. CYGWIN*) iscygwin=true ;;
  68. Darwin*) isdarwin=true ;;
  69. FreeBSD*) isfreebsd=true ;;
  70. Linux*) islinux=true ;;
  71. esac
  72. ($ismsys || $iscygwin) && iswindows=true
  73. # is this true?
  74. ($isdarwin || $isfreebsd) && isbsd=true
  75. # dump system detection result
  76. cat <<__EOC__ >"$__system_shrc"
  77. #!/bin/sh
  78. # $__system_shrc
  79. ismsys=$ismsys
  80. iscygwin=$iscygwin
  81. iswindows=$iswindows
  82. isdarwin=$isdarwin
  83. isfreebsd=$isfreebsd
  84. isbsd=$isbsd
  85. islinux=$islinux
  86. __EOC__
  87. fi
  88. $ismsys && export HOSTNAME
  89. # adhoc fix for ansible on cygwin
  90. # http://blog.s-uni.net/2013/08/27/ansible-running-on-cygwin/
  91. if $iscygwin
  92. then
  93. export ANSIBLE_SSH_ARGS="-o ControlMaster=no"
  94. fi
  95. ##########################
  96. # Terminal setups
  97. if ! $iswindows && null type stty
  98. then
  99. stty stop undef # unbind C-s to stop displaying output
  100. # stty erase '^h'
  101. fi
  102. # Zsh specific preferences
  103. # http://www.clear-code.com/blog/2011/9/5.html
  104. if $inzsh
  105. then
  106. bindkey -e
  107. # http://zsh.sourceforge.net/Guide/zshguide06.html#l147
  108. autoload compinit; compinit
  109. # supress cycle by tab
  110. unsetopt auto_menu
  111. # unsetopt correct
  112. setopt complete_aliases
  113. setopt auto_list
  114. setopt bash_auto_list
  115. setopt magic_equal_subst
  116. setopt list_types
  117. # what is the difference of these two?
  118. setopt auto_param_slash
  119. setopt mark_dirs
  120. zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
  121. zstyle ':completion:*' format '%B%d%b'
  122. zstyle ':completion:*' group-name ''
  123. zstyle ':completion:*' use-cache yes
  124. # zstyle ':completion:*:cd:*' tag-order local-directories
  125. zstyle ':completion:*' completer _complete _bash_completions \
  126. _history
  127. # zstyle ':completion:*:*:cd:*' completer
  128. zstyle ':completion:*' accept-exact-dirs true
  129. zstyle ':completion:*' special-dirs true
  130. autoload colors; colors
  131. # autoload -Uz promptinit
  132. # promptinit
  133. # prompt walters
  134. setopt hist_ignore_dups
  135. setopt hist_ignore_all_dups
  136. setopt hist_save_no_dups
  137. setopt extended_history
  138. setopt share_history
  139. setopt append_history
  140. HISTFILE=$HOME/.zsh-history
  141. HISTSIZE=100000
  142. SAVEHIST=100000
  143. setopt prompt_subst
  144. setopt interactive_comments
  145. fi
  146. ######################
  147. # Print welcome messages
  148. command -v fortune >/dev/null && {
  149. if command -v cowsay >/dev/null
  150. then
  151. fortune | cowsay
  152. echo
  153. fortune -o | cowsay
  154. echo
  155. else
  156. fortune
  157. echo
  158. fortune -o
  159. echo
  160. fi
  161. }
  162. uname -a
  163. $iswindows && alias tty="echo cmd.exe"
  164. echo TERM $TERM with $(tput colors) colors using $(tty)
  165. echo umask: `umask -S`
  166. echo
  167. __safe_run w yuk
  168. # if null type tmux
  169. # then
  170. # echo
  171. # echo tmux sessions:
  172. # tmux ls 2>/dev/null| sed -e 's/^/ /g'
  173. # echo
  174. # fi
  175. # if test -n "$TMUX"
  176. # then
  177. # tmux display -p \
  178. # 'TMUX #S:#I:#W.#P, client TERM: #{client_termname}' \
  179. # 2>/dev/null
  180. # echo
  181. # fi
  182. ###################################
  183. # some aliases and functions
  184. # __func_name: never used interactively
  185. # _func_name: usually not used interactively
  186. __safe_alias(){
  187. # __safe_alias <name>=<command>
  188. _bin=`expr "$1" : '^[^=]*=\([^ ]*\)'`
  189. test -n "$_bin" && \
  190. null type $_bin && \
  191. alias "$1"
  192. }
  193. $gnu_coreutils && _timeoption=" --time-style=long-iso"
  194. # color prefs
  195. if $gnu_coreutils
  196. then
  197. # http://qiita.com/yuyuchu3333/items/84fa4e051c3325098be3
  198. # gnu coreutils LS_COLORS is used
  199. null type dircolors && eval `dircolors`
  200. _coloroption=" --color=auto"
  201. else
  202. # export LSCOLORS=gxfxcxdxbxegedabagacad
  203. export LSCOLORS=DxGxcxdxCxegedabagacad
  204. export CLICOLOR=1
  205. fi
  206. alias ls="ls -hCF${_coloroption}${_timeoption}"
  207. _timeformat_iso="%Y-%m-%dT%H:%M:%S%z"
  208. _timeformat_rfc2822="%a, %d %b %Y %T %z"
  209. _timeformat_num="%Y%m%d%H%M%S"
  210. alias datenum="date +$_timeformat_num"
  211. # export GREP_OPTIONS=""
  212. alias gr="grep -n --color=always"
  213. $iswindows && alias gr="grep -n"
  214. alias less="less -F"
  215. __safe_alias em="emacs -nw"
  216. __safe_alias vi=vim
  217. alias pstree="LANG=C pstree"
  218. alias cp="cp -v"
  219. alias mv="mv -v"
  220. alias rm="rm -v"
  221. alias mkdir="mkdir -v"
  222. alias rmdir="rmdir -v"
  223. alias psaux="ps auxww"
  224. alias modest="ionice -c 2 -n 7 nice -n 19"
  225. alias e=exec
  226. alias q=exit
  227. __safe_alias e3=e3em
  228. #alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
  229. alias po=popd
  230. alias pu=pushd
  231. __safe_alias sudo="sudo " # use aliases through sudo
  232. __safe_alias sudoe="sudoedit"
  233. # __safe_alias halt="sudo halt"
  234. # __safe_alias reboot="sudo reboot"
  235. null type dbus-send && {
  236. alias suspend="dbus-send --system --print-reply \
  237. --dest=org.freedesktop.UPower \
  238. /org/freedesktop/UPower org.freedesktop.UPower.Suspend"
  239. alias hibernate="dbus-send --system --print-reply \
  240. --dest=org.freedesktop.UPower \
  241. /org/freedesktop/UPower org.freedesktop.UPower.Hibernate"
  242. }
  243. alias rand="echo \$RANDOM"
  244. __safe_alias xunp="file-roller -h"
  245. __safe_alias pc="sudo \paco -D"
  246. alias pycalc="python -i -c 'from math import *' "
  247. __safe_alias py3=python3
  248. __safe_alias py2=python2
  249. __safe_alias ipy=ipython
  250. __safe_alias ipy3=ipython3
  251. __safe_alias ipy2=ipython2
  252. # Sometimes SHELL cannot be used. For example, when running bash inside zsh
  253. # SHELL is set to be /bin/zsh
  254. if $inbash
  255. then
  256. alias _reloadrc="rm '$__shrc_lock'; exec bash"
  257. elif $inzsh
  258. then
  259. alias _reloadrc="rm '$__shrc_lock'; exec zsh"
  260. else
  261. alias _reloadrc="rm '$__shrc_lock'; exec $SHELL"
  262. fi
  263. # alias mytime="date +%Y%m%d-%H%M%S"
  264. alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
  265. # some extra utilities
  266. # type trash >/dev/null 2>&1 && alias rm=trash
  267. __safe_alias mpg123="mpg123 -C -v --title"
  268. __safe_alias xm="xmms2"
  269. #export PLAYER="mpg123 -C -v --title"
  270. __safe_alias screen="screen -e^z^z"
  271. #alias zcd="cd \`zenity --file-selection --directory\`"
  272. __safe_alias gtags="gtags --verbose"
  273. __safe_alias htags="htags --xhtml --symbol --line-number \
  274. --frame --alphabet --verbose"
  275. __safe_alias au=aunpack
  276. # __safe_alias lv="lv|less"
  277. __safe_alias rs="rsync --progress --itemize-changes --compress"
  278. __safe_alias vagr=vagrant
  279. __safe_alias ztw='twitter set "`zenity --entry --title ztw --text Status:`"'
  280. if $iscygwin
  281. then
  282. __my_wget_options=" --no-check-certificate"
  283. __safe_alias wget="wget $__my_wget_options"
  284. fi
  285. if $isdarwin
  286. then
  287. alias updatedb="LC_ALL=C updatedb"
  288. # do not use locate installed by macports
  289. test -x /usr/bin/locate && alias locate="/usr/bin/locate"
  290. fi
  291. # somehow not work
  292. # typeset -ga chpwd_functions
  293. # chpwd_functions+=pwd
  294. # chpwd(){
  295. # pwd
  296. # }
  297. cd(){
  298. builtin cd "$@" && pwd
  299. }
  300. root(){
  301. if test "`git rev-parse --is-inside-work-tree 2>/dev/null`" = true
  302. then
  303. cd "`git rev-parse --show-toplevel`"
  304. else
  305. cd /
  306. fi
  307. }
  308. # pad
  309. alias pad=notepad
  310. __safe_alias pad=gedit
  311. __safe_alias pad=leafpad
  312. $isdarwin && alias pad="open -e"
  313. __find_latest_vimdir(){
  314. vimdir=/usr/share/vim
  315. if test -d "$vimdir"
  316. then
  317. find "$vimdir" -name 'vim??' -type d | sort | tail -n 1
  318. else
  319. echo ""
  320. fi
  321. }
  322. for f in /usr/share/vim/vimcurrent "`__find_latest_vimdir`"
  323. do
  324. test -n "$f" || continue
  325. f="$f/macros/less.sh"
  326. test -f $f && alias vl=$f && break
  327. done
  328. __safe_alias pa="pacman --color=always"
  329. __safe_alias pa="pacmatic --color=always"
  330. __safe_alias pa="pacapt"
  331. __safe_alias yt=yaourt
  332. # variable for yaourt
  333. null type pacmatic && export PACMAN="pacmatic"
  334. __safe_alias cower="cower --color=auto"
  335. __my_pacman_update_mirrorlist_with_reflector(){
  336. f=/etc/pacman.d/mirrorlist
  337. cmd="$(expr "$(grep -m 1 reflector $f)" : '# With: *\(.*\)')"
  338. if test -z "$cmd"
  339. then
  340. cmd="reflector --verbose --number 5 --sort rate --protocol http --protocol https --save $f"
  341. fi
  342. echo ">>> $cmd ..." 1>&2
  343. sudo sh -c "$cmd" && cat $f
  344. }
  345. null type reflector && test -d /etc/pacman.d && \
  346. alias reflect_mirrorlist=__my_pacman_update_mirrorlist_with_reflector
  347. null type apt-get && {
  348. alias aupgrade="sudo sh -xc 'apt-get autoremove --yes && \
  349. apt-get update --yes && apt-get upgrade --yes'"
  350. alias aptin="apt-get install"
  351. alias aptsearch="apt-cache search"
  352. alias aptshow="apt-cache show"
  353. }
  354. null type port && {
  355. alias port="port -v"
  356. alias pupgrade="sudo sh -xc 'port -v selfupdate && \
  357. port -v upgrade outdated'"
  358. }
  359. if $iscygwin
  360. then
  361. null type windate || \
  362. alias windate="cmd.exe //c 'echo %DATE%-%TIME%'"
  363. # alias cygsu="cygstart /cygwinsetup.exe"
  364. fi
  365. mkpatch(){
  366. if test $# -eq 0
  367. then
  368. echo "usage: mkpatch <olddir> <newdir>"
  369. elif ! test -d "$1"
  370. then
  371. echo "mkpatch: $1 is not a directory"
  372. elif ! test -d "$2"
  373. then
  374. echo "mkpatch: $2 is not a directory"
  375. else
  376. diff -Naur "$1" "$2"
  377. fi
  378. }
  379. __sdcv_less(){
  380. command sdcv -n "$@" | less --quit-if-one-screen
  381. }
  382. command -v sdcv >/dev/null && alias dict=__sdcv_less
  383. g(){
  384. if test $# -eq 0 && null type git-info
  385. then
  386. git info
  387. else
  388. git "$@"
  389. fi
  390. }
  391. if null type _git && $inbash
  392. then
  393. # enable programmable completion for g
  394. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  395. || complete -o default -o nospace -F _git g
  396. fi
  397. #git svn --help >/dev/null 2>&1 && alias gsvn="git svn"
  398. __safe_alias m=gitmemo
  399. alias setup.py3="sudo python3 setup.py install --record files.txt"
  400. randomstr(){
  401. len=$1
  402. test -z "$len" && len=8
  403. uuidgen | tr -d - | cut -c 1-len
  404. }
  405. datestr(){
  406. # datestr YYYYMMDDhhmmss
  407. if test -z "$1" || test "$1" = "-h"
  408. then
  409. echo "datestr: usage: datestr <YYYYMMDDhhmmss>"
  410. return 1
  411. fi
  412. # actual format for date command
  413. dfmt="`echo "$1" | \
  414. sed -e 's/YYYY/%Y/g' -e 's/yyyy/%Y/g' -e 's/YY/%y/g' -e 's/yy/%y/g' |\
  415. sed -e 's/MM/%m/g' -e 's/DD/%d/g' -e 's/dd/%d/g' | \
  416. sed -e 's/HH/%H/g' -e 's/hh/%H/g' -e 's/mm/%M/g' -e 's/SS/%S/g' -e 's/ss/%S/g'`"
  417. date +"$dfmt"
  418. }
  419. # ssh(){
  420. # # __my_terminal_title ssh
  421. # command ssh "$@"
  422. # }
  423. __ssh_with_cd(){
  424. # __ssh_with_cd <host> <directory> [<arg> ...]
  425. if test -z "$2"
  426. then
  427. echo "usage: __ssh_with_cd <host> <directory> [<arg> ...]"
  428. return 1
  429. fi
  430. host="$1"
  431. shift
  432. dir="$1"
  433. shift
  434. ssh "$host" "$@" -t "cd \"$dir\"; \$SHELL -l"
  435. }
  436. memo(){
  437. if test -z "$1"
  438. then
  439. _memo="memo.txt"
  440. else
  441. _memo="$1/memo.txt"
  442. fi
  443. $EDITOR "$_memo"
  444. if test -z "`cat "$_memo"`"
  445. then
  446. echo "$_memo is empty. Removing."
  447. rm "$_memo"
  448. fi
  449. }
  450. now(){
  451. ___tformat="%Y/%m/%d %H:%M:%S %z"
  452. cal
  453. REPLY=
  454. printf "\\r`date "+${___tformat}"`"
  455. read -t 1
  456. while test $? -ne 0
  457. do
  458. printf "\\r`date "+${___tformat}"`"
  459. read -t 1
  460. done
  461. }
  462. s(){
  463. if git rev-parse --git-dir >/dev/null 2>&1
  464. then
  465. echo ">> git grep -n $@" 1>&2
  466. git grep -n "$@"
  467. elif which ag >/dev/null 2>&1
  468. then
  469. echo ">> ag --pager=\"$PAGER\" $@" 1>&2
  470. ag --pager="$PAGER" "$@"
  471. elif which ack >/dev/null 2>&1
  472. then
  473. echo ">> ack --pager=\"$PAGER\" $@" 1>&2
  474. ack --pager="$PAGER" "$@"
  475. else
  476. echo \
  477. ">> find . " \
  478. "-path '*/.git' -prune -o" \
  479. "-path '*/.svn' -prune -o" \
  480. "-type f -exec grep -nH -e --color=always $@ {} +" 1>&2
  481. if test $# -eq 0
  482. then
  483. echo "No search word given." 1>&2
  484. return 1
  485. fi
  486. find . \
  487. -path '*/.git' -prune -o \
  488. -path '*/.svn' -prune -o \
  489. -type -f -exec grep -nH -e --color=always "$@" {} + \
  490. | $PAGER
  491. fi
  492. }
  493. MYMANPATH='/usr/lib/erlang/man'
  494. if $inbash || $inzsh
  495. then
  496. man(){
  497. env \
  498. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  499. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  500. LESS_TERMCAP_me=$(printf "\e[0m") \
  501. LESS_TERMCAP_se=$(printf "\e[0m") \
  502. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  503. LESS_TERMCAP_ue=$(printf "\e[0m") \
  504. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  505. MANPATH="`manpath`:$MYMANPATH" \
  506. man "$@"
  507. }
  508. fi
  509. netwait(){
  510. while ! ping -c 1 -t 1 example.com
  511. do
  512. true
  513. done
  514. echo network works.
  515. }
  516. __realpath(){
  517. if type realpath >/dev/null 2>&1
  518. then
  519. command realpath "$@"
  520. else
  521. while ! test -d $1
  522. do
  523. shift
  524. done
  525. (command cd "$d" && echo "$PWD")
  526. # local d="$OLDPWD"
  527. # command cd "$1"
  528. # echo "$PWD"
  529. # command cd "$d"
  530. fi
  531. }
  532. if $isdarwin
  533. then
  534. # I want to use env -u, but darwin env does not provide this option
  535. __safe_alias tmux='SHLVL= tmux'
  536. else
  537. __safe_alias tmux='env -u SHLVL tmux'
  538. fi
  539. tx(){
  540. tmux start-server
  541. if test -z "$1"
  542. then
  543. echo "usage: tx <session_name>"
  544. tmux list-sessions
  545. elif test -z "$TMUX"
  546. then
  547. # -A: attach if already exists
  548. # tmux new-session -A -c "$HOME" -s "$1"
  549. tmux new-session -A -s "$1"
  550. else
  551. # create new session if not exists yet
  552. # tmux has-session -t "$1" || TMUX= tmux new-session -d -c "$HOME" -s "$1"
  553. tmux has-session -t "$1" || TMUX= tmux new-session -d -s "$1"
  554. tmux switch-client -t "$1"
  555. fi
  556. }
  557. dt(){
  558. # dt [-h] [<name>] [<command ...>]
  559. __dtach_dir="${TMP}/dtach"
  560. mkdir -p "${__dtach_dir}"
  561. if test -n "${__MY_DTACH}"
  562. then
  563. # if already in dtach session print current session
  564. soc_name="`basename "$__MY_DTACH"`"
  565. echo "Current session: ${soc_name}"
  566. fi
  567. if test -z "$1"
  568. then
  569. # if no arg given show list of sessions
  570. echo "Sessions:"
  571. ls "${__dtach_dir}"
  572. return 0
  573. elif test "$1" = "-h"
  574. then
  575. echo "dt: usage: dt [-h] [<name>] [<command ...>]" 1>&2
  576. return 1
  577. fi
  578. # set socket name
  579. soc_name="${__dtach_dir}/$1"
  580. shift
  581. if test -n "$__MY_DTACH"
  582. then
  583. echo "dtach session cannot be nested." 1>&2
  584. return 1
  585. elif test -S "$soc_name"
  586. then
  587. dtach -a "$soc_name" -e ^^
  588. elif test -e "$soc_name"
  589. then
  590. echo "dt: File named $soc_name already exists."
  591. return 1
  592. elif test -z "$1"
  593. then
  594. # if no command given invoke current shell
  595. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
  596. # echo "dt: Socket named $soc_name not exists and no command specified."
  597. # return 1
  598. else
  599. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
  600. fi
  601. }
  602. scr(){
  603. test -n "$1" && pf="${1}-"
  604. ___tformat="%Y%m%d-%H%M%S%z"
  605. ___file="${HOME}/${pf}`date +${___tformat}`.script"
  606. __MY_SCRIPT=${___file} script ${___file} "$@"
  607. }
  608. dtscr(){
  609. # dtscr <command ...>
  610. if test -z "$1"
  611. then
  612. echo "dtscr: usage: dtscr <command ...>"
  613. return 1
  614. fi
  615. ___cmdstr="`echo $@ | tr ' ' +`"
  616. ___tformat="%Y%m%d-%H%M%S%z"
  617. ___name="${pf}`date +${___tformat}`-${___cmdstr}"
  618. ___scr_file="${HOME}/${___name}.script"
  619. ___dt_dir="${TMP}/dtscr"
  620. mkdir -p "$___dt_dir"
  621. dtach -n "${___dt_dir}/${___name}" script "${___scr_file_}" "$@"
  622. # echo $_name
  623. # echo $_file
  624. }
  625. mcrypt_stream(){
  626. test $# -eq 2 || return 1
  627. case $1 in
  628. en)
  629. mcrypt --key $2 | base64 ;;
  630. de)
  631. base64 -d | mcrypt -d --key $2 ;;
  632. esac
  633. }
  634. gpg_stream(){
  635. test $# -eq 2 || return 1
  636. case $1 in
  637. en)
  638. gpg --passphrase $2 -c --batch |base64 ;;
  639. de)
  640. base64 -d|gpg --passphrase $2 -d --batch ;;
  641. esac
  642. }
  643. dgpg(){
  644. if test "$1" = help || test -z "$2"
  645. then
  646. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  647. return
  648. fi
  649. ___srcs="$2"
  650. ___dsts="$3"
  651. test -z "$___dsts" && ___dsts="${___srcs}.out"
  652. ___pw=
  653. echo -n "dgpg pw: "
  654. read -s ___pw
  655. echo ""
  656. test -z "$___pw" && return 1
  657. for f in *${___srcs}
  658. do
  659. ___d="$(basename "$f" "${___srcs}")${___dsts}"
  660. echo -n "Processing $f to $___d..."
  661. if test -d "$f"
  662. then
  663. echo "`printf 'failed (%s is directory)' $f`"
  664. elif test -f "$___d"
  665. then
  666. echo "`printf 'failed (%s is already exists)' $___d`"
  667. elif <"$f" gpg_stream $1 $___pw >"$___d" 2>/dev/null
  668. then
  669. echo "done"
  670. else
  671. echo "failed"
  672. test -f "$___d" && rm "$___d"
  673. fi
  674. done
  675. }
  676. alias enst="gpg_stream en"
  677. alias dest="gpg_stream de"
  678. showinfo(){
  679. echo "Japanese letters are 表示可能"
  680. __safe_run diskinfo
  681. ! $isdarwin && test -n "${DISPLAY}" && {
  682. __safe_run xrandr | \grep --color=never ^Screen
  683. }
  684. $iswindows || __safe_run finger $USER
  685. LANG=C __safe_runc id
  686. __safe_run xset q
  687. }
  688. x(){
  689. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  690. #mkdir -p ~/.var/log
  691. # nohup startx >~/.var/log/xorg.log 2>&1 &
  692. # exit
  693. exec startx
  694. else
  695. echo "X cant be started! Another X is already running?" 1>&2
  696. fi
  697. }
  698. bak(){
  699. for file in "$@"
  700. do
  701. cp -v ${file} ${file}.bak
  702. done
  703. }
  704. di(){
  705. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  706. then
  707. ___diffcmd=colordiff
  708. else
  709. ___diffcmd=diff
  710. fi
  711. ${___diffcmd} -u "$@" | ${PAGER}
  712. }
  713. tb(){
  714. __datenum=`date +%Y%m%d-%H%M%S`
  715. __tb="$HOME/.var/tb/$__datenum"
  716. mkdir -p "$__tb"
  717. mv "$@" "$__tb"
  718. }
  719. mkdd(){
  720. _d=`date +%Y%m%d-%H%M%S` && \
  721. mkdir -p "$_d"
  722. }
  723. mkcd(){
  724. if test -z "$1"
  725. then
  726. echo "mkcd: usage: mkcd <dir>"
  727. return 1
  728. elif test -d "$1"
  729. then
  730. echo "Dir \"$1\" already exists."
  731. else
  732. mkdir -p "$1"
  733. echo "Dir \"$1\" created."
  734. fi
  735. cd "$1"
  736. }
  737. mkcdd(){
  738. # make and change date directory
  739. _d=`date +%Y%m%d-%H%M%S` && \
  740. mkcd "$_d"
  741. }
  742. if test -n "$TMUX" && null type reattach-to-user-namespace
  743. then
  744. alias pbpaste="reattach-to-user-namespace pbpaste"
  745. alias pbcopy="reattach-to-user-namespace pbcopy"
  746. fi
  747. catclip(){
  748. if $iswindows
  749. then
  750. cat /dev/clipboard | tr -d \\r
  751. elif $isdarwin
  752. then
  753. pbpaste
  754. else
  755. xclip -o -selection "clipboard"
  756. fi
  757. }
  758. setclip(){
  759. if test $# -eq 0
  760. then
  761. exec 3<&0
  762. else
  763. exec 3<<__EOF__
  764. `cat "$@"`
  765. __EOF__
  766. fi
  767. if $iswindows
  768. then
  769. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  770. elif $isdarwin
  771. then
  772. pbcopy 0<&3
  773. else
  774. 0<&3 xclip -i -f -selection "primary" | \
  775. xclip -i -f -selection "clipboard"
  776. fi
  777. exec 3<&-
  778. }
  779. open_file(){
  780. if $iscygwin
  781. then
  782. cygstart "$@"
  783. elif $ismsys
  784. then
  785. cmd.exe //c start "" "$@"
  786. elif $isdarwin
  787. then
  788. touch "$@"
  789. open "$@"
  790. elif $islinux
  791. then
  792. touch "$@"
  793. if null type pcmanfm; then
  794. LC_MESSAGES= pcmanfm "$@"
  795. else
  796. LC_MESSAGES= xdg-open "$@" &
  797. fi
  798. else
  799. cat "$@"
  800. fi
  801. }
  802. o(){
  803. if test $# -eq 0
  804. then
  805. open_file .
  806. else
  807. for f in "$@"
  808. do
  809. open_file "$(realpath "$f")"
  810. done
  811. fi
  812. }
  813. convmv_sjis2utf8_test(){
  814. convmv -r -f sjis -t utf8 *
  815. }
  816. convmv_sjis2utf8_notest(){
  817. convmv -r -f sjis -t utf8 * --notest
  818. }
  819. #################################################
  820. ## pastebin services
  821. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  822. sprunge(){
  823. # http://sprunge.us
  824. if test -z "$1"
  825. then
  826. curl -F 'sprunge=<-' http://sprunge.us
  827. else
  828. curl http://sprunge.us/$1
  829. fi
  830. }
  831. dpaste(){
  832. # http://dpaste.de
  833. if test -z "$1"
  834. then
  835. curl -F 'content=<-' https://dpaste.de/api/
  836. echo
  837. else
  838. curl https://dpaste.de/$1/raw/
  839. fi
  840. }
  841. ######################################
  842. ## Prompt Settings
  843. __my_moc_state(){
  844. type mocp >/dev/null 2>&1 || return
  845. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  846. printf "$1" "`mocp -Q %title 2>/dev/null`"
  847. }
  848. __my_parse_svn_branch() {
  849. ___svn_url=$(LANG=C svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  850. ___svn_repository_root=$(LANG=C svn info 2>/dev/null | \
  851. sed -ne 's#^Repository Root: ##p')
  852. echo ${___svn_url} | sed -e 's#^'"${___svn_repository_root}"'##g' | \
  853. awk '{print $1}'
  854. }
  855. __my_svn_ps1(){
  856. if svn status >/dev/null 2>&1
  857. then
  858. ___svn_branch=$(__my_parse_svn_branch)
  859. test -n "${___svn_branch}" && printf "$1" "{$___svn_branch}"
  860. fi
  861. }
  862. __my_battery_status(){
  863. ___dir=/sys/class/power_supply/BAT0
  864. if test -d $___dir && test -r $___dir/status && test -r $___dir/charge_full && \
  865. test -r $___dir/charge_now
  866. then
  867. ___st=$(cat $___dir/status)
  868. ___full=$(cat $___dir/charge_full)
  869. ___now=$(cat $___dir/charge_now)
  870. ___rate=$(expr "$___now" \* 100 / "$___full")
  871. printf "$1" "${___st}:${___rate}%"
  872. fi
  873. }
  874. alias bat='__my_battery_status %s\\n'
  875. __my_ps1_scale(){
  876. if null type stty && ! $ismsys
  877. then
  878. echo "[LxC:`stty size | tr -d $'\n' | tr " " x`]"
  879. fi
  880. }
  881. __my_ps1_tmux(){
  882. null type tmux || return $last
  883. ___tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  884. test -n "$TMUX" && echo "[TMUX:$___tmuxc]"
  885. }
  886. __my_ps1_moc(){
  887. __my_moc_state "[MOC:%s]"
  888. }
  889. for f in /usr/share/git/git-prompt.sh \
  890. /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh \
  891. /usr/local/opt/bash-git-prompt/share/gitprompt.sh \
  892. /usr/share/git-core/contrib/completion/git-prompt.sh \
  893. /usr/local/share/git-core/contrib/completion/git-prompt.sh \
  894. /etc/bash_completion.d/git-prompt \
  895. /opt/local/share/git-core/git-prompt.sh \
  896. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  897. do
  898. test -r "$f" && ($inbash || $inzsh) && . "$f" && break
  899. done
  900. export GIT_PS1_SHOWDIRTYSTATE=t
  901. export GIT_PS1_SHOWUPSTREAM=t
  902. __my_ps1_git(){
  903. null type __git_ps1 || return $last
  904. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  905. __git_ps1 "[GIT:$(__safe_run git config --get user.name):%s]"
  906. }
  907. __printf_ipaddr(){
  908. # ipaddress <fmt>
  909. type ip >/dev/null 2>&1 || return 1
  910. ___ip=$(LANG=C ip addr show scope global | \
  911. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  912. test -n "$___ip" && printf "$1" $___ip
  913. }
  914. alias addr="__printf_ipaddr '%s'"
  915. __my_ps1_ipaddr(){
  916. ! $iswindows && __printf_ipaddr '[Addr:%s]'
  917. }
  918. __my_ps1_bttry(){
  919. ___bst="$__homerun/batterystatus"
  920. if test -z "$DISPLAY" && ! $iswindows
  921. then
  922. test -f $___bst && ___bstr="$(cat $___bst)"
  923. test -n "$___bstr" && ! echo $___bstr | grep 100 >/dev/null 2>&1 && \
  924. echo "[Battery:${___bstr}]"
  925. __my_battery_status %s >$___bst &
  926. fi
  927. }
  928. __my_ps1_memo(){
  929. test -f memo.txt && echo "m:`du -b memo.txt|cut -f 1`"
  930. }
  931. __my_ps1_dirs(){
  932. dirs | wc -l
  933. }
  934. __my_ps1_jobs(){
  935. # __my_ps1_jobs [<num>]
  936. if test -n "$1"
  937. then
  938. jobs="$1"
  939. else
  940. jobs="`jobs | wc -l`"
  941. fi
  942. if test "$jobs" -gt 0
  943. then
  944. echo "JOBS:$jobs"
  945. fi
  946. }
  947. __my_ps1_dollar(){
  948. if test -z "$SHLVL"
  949. then
  950. printf "$1"
  951. else
  952. perl -e 'while($ARGV[0]-- > 0){print "$ARGV[1]";}' $SHLVL "$1"
  953. fi
  954. }
  955. __my_alert_fail(){
  956. test $laststatus -eq 0 || \
  957. echo "STATUS:${laststatus}"
  958. }
  959. # About ansi escape sequences
  960. # http://archive.linux.or.jp/JF/JFdocs/Bash-Prompt-HOWTO-5.html
  961. # http://www.grapecity.com/japan/powernews/column/clang/047/page02.htm
  962. if $inbash || $inzsh
  963. then
  964. if $inzsh
  965. then
  966. __attr_beg=$'%{\033['
  967. __attr_end='m%}'
  968. else
  969. __attr_beg='\[\033['
  970. __attr_end='m\]'
  971. fi
  972. __color_default="${__attr_beg}0${__attr_end}"
  973. __color_black="${__attr_beg}0;30${__attr_end}"
  974. __color_red="${__attr_beg}0;31${__attr_end}"
  975. __color_green="${__attr_beg}0;32${__attr_end}"
  976. __color_yellow="${__attr_beg}0;33${__attr_end}"
  977. __color_blue="${__attr_beg}0;34${__attr_end}"
  978. __color_magenta="${__attr_beg}0;35${__attr_end}"
  979. __color_cyan="${__attr_beg}0;36${__attr_end}"
  980. __color_white="${__attr_beg}0;37${__attr_end}"
  981. __color_light_black="${__attr_beg}1;30${__attr_end}"
  982. __color_light_red="${__attr_beg}1;31${__attr_end}"
  983. __color_light_green="${__attr_beg}1;32${__attr_end}"
  984. __color_light_yellow="${__attr_beg}1;33${__attr_end}"
  985. __color_light_blue="${__attr_beg}1;34${__attr_end}"
  986. __color_light_magenta="${__attr_beg}1;35${__attr_end}"
  987. __color_light_cyan="${__attr_beg}1;36${__attr_end}"
  988. __color_light_white="${__attr_beg}1;37${__attr_end}"
  989. __color_bg_black="${__attr_beg}40${__attr_end}"
  990. __color_bg_red="${__attr_beg}41${__attr_end}"
  991. __color_bg_green="${__attr_beg}42${__attr_end}"
  992. __color_bg_yellow="${__attr_beg}43${__attr_end}"
  993. __color_bg_blue="${__attr_beg}44${__attr_end}"
  994. __color_bg_magenta="${__attr_beg}45${__attr_end}"
  995. __color_bg_cyan="${__attr_beg}46${__attr_end}"
  996. __color_bg_white="${__attr_beg}47${__attr_end}"
  997. __attr_underline="${__attr_beg}4${__attr_end}"
  998. __attr_reverse="${__attr_beg}7${__attr_end}"
  999. __attr_bold="${__attr_beg}1${__attr_end}"
  1000. fi
  1001. # NOTE: tput is another easy way to set colors and background
  1002. # For example, "$(tput setab 4)text$(tput sgr0)" print text with background
  1003. # color blue.
  1004. # http://www.ibm.com/developerworks/jp/linux/aix/library/au-learningtput/index.html
  1005. if test "$TERM" != dumb
  1006. then
  1007. __my_c1="$__attr_bold$__attr_underline" # color for PWD
  1008. __my_c2="$__attr_bold$__attr_underline" # color for user and hostname
  1009. # color for ::
  1010. if test -z "$_HOSTCOLOR_1"
  1011. then
  1012. # default color used by tmux status line
  1013. _HOSTCOLOR_1=green
  1014. fi
  1015. eval "__my_c4=\${__color_light_${_HOSTCOLOR_1}}"
  1016. __my_c5="$__color_black$__color_bg_white" # color for SCR
  1017. __my_cdef="$__color_default"
  1018. fi
  1019. if $inbash
  1020. then
  1021. __my_ps1_sh="[BASH:$BASH_VERSION]"
  1022. elif $inzsh
  1023. then
  1024. __my_ps1_sh="[ZSH:$ZSH_VERSION]"
  1025. fi
  1026. __my_ps1_info1(){
  1027. # first line of PS1
  1028. echo "${__my_ps1_sh}$(__my_ps1_scale)$(__my_ps1_git)$(__my_ps1_bttry)$(__my_ps1_moc)"
  1029. }
  1030. test -n "$__MY_SCRIPT" && \
  1031. __my_ps1_str_scr="SCR"
  1032. test -n "$SSH_CONNECTION" && \
  1033. __my_ps1_str_ssh="SSH"
  1034. test -n "$__MY_DTACH" && \
  1035. __my_ps1_str_dt="DT:`basename "$__MY_DTACH$"`"
  1036. __my_ps1_info2(){
  1037. # second line of PS1
  1038. echo $(__my_ps1_memo) $(__my_ps1_jobs) ${__my_ps1_str_scr} \
  1039. ${__my_ps1_str_ssh} ${__my_ps1_str_dt} $(__my_alert_fail) \
  1040. | sed -e 's/ /|/g'
  1041. }
  1042. __my_ps1_beg="${__my_c4}:: ${__my_cdef}"
  1043. __my_ps1_save_pos="\[\033[s\]"
  1044. __my_ps1_restore_pos="\[\033[u\]"
  1045. __my_ps1_move_rightmost="\[\033[\$(tput cols)C\]"
  1046. __my_ps1_move_15left="\[\033[15D\]"
  1047. # collapse when command line is too long and try to write over this string
  1048. # __my_ps1_right="${__my_ps1_save_pos}${__my_ps1_move_rightmost}"
  1049. # ${__my_ps1_move_15left}\D{%Y/%m/%d %H:%M}${__my_ps1_restore_pos}
  1050. if $inzsh
  1051. then
  1052. PROMPT="\
  1053. ${__my_ps1_beg}[${__my_c2}%n@%M${__my_cdef}:${__my_c1}%~/${__my_cdef}]\$(__my_ps1_info1)
  1054. ${__my_ps1_beg}\$(__my_ps1_info2) $(__my_ps1_dollar %#) "
  1055. RPROMPT="%D{%Y/%m/%d %H:%M}"
  1056. elif $inbash
  1057. then
  1058. PS1="\
  1059. ${__my_ps1_beg}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_info1)\n\
  1060. ${__my_ps1_beg}\D{%Y/%m/%d %H:%M} \$(__my_ps1_info2)${__my_ps1_right} $(__my_ps1_dollar \\$) "
  1061. else
  1062. true
  1063. # PS1="$(printf $(whoami)@$(hostname)$ )"
  1064. fi
  1065. ###################################
  1066. # set header and titles
  1067. __my_set_header_line(){
  1068. # save current position
  1069. printf "\033[s"
  1070. # move to 0,0
  1071. printf "\033[0;0H"
  1072. # clear curent to eol
  1073. printf "\033[K"
  1074. # inverse color
  1075. printf "\033[7m"
  1076. printf "$1"
  1077. # restore color
  1078. printf "\033[0m"
  1079. # restore saved position
  1080. printf "\033[u"
  1081. }
  1082. __my_set_screen_name(){
  1083. # set window name
  1084. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  1085. then
  1086. echo -ne "\033k$1\033\\"
  1087. fi
  1088. }
  1089. __my_set_title(){
  1090. case $TERM in
  1091. (rxvt*|xterm*|aterm|screen*)
  1092. test -t 1 &&
  1093. test -z "$EMACS" &&
  1094. echo -n -e "\033]0;$1\007"
  1095. ;;
  1096. esac
  1097. }
  1098. if test -n "$TMUX"
  1099. then
  1100. # running tmux locally
  1101. __terminal_title="\$(basename \${PWD})"
  1102. elif test -n "$SSH_CONNECTION" && expr "$TERM" : '^screen' >/dev/null
  1103. then
  1104. # ssh connect from tmux terminal
  1105. __terminal_title="`whoami`@`hostname`:\$(basename \${PWD})"
  1106. else
  1107. __terminal_title="`whoami`@`hostname`:\${PWD}"
  1108. fi
  1109. if $inzsh
  1110. then
  1111. precmd(){
  1112. laststatus=$?
  1113. eval __my_set_title ${__terminal_title}
  1114. }
  1115. else
  1116. if test -n "$PROMPT_COMMAND"
  1117. then
  1118. PROMPT_COMMAND="laststatus=\$?;$PROMPT_COMMAND;__my_set_title \"${__terminal_title}\""
  1119. else
  1120. PROMPT_COMMAND="laststatus=\$?;__my_set_title \"${__terminal_title}\""
  1121. fi
  1122. fi
  1123. laststatus=0