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.
 
 
 
 
 
 

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