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.
 
 
 
 
 
 

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