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.
 
 
 
 
 
 

1270 lines
29 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. __safe_alias tmux='env -u SHLVL tmux'
  532. tx(){
  533. tmux start-server
  534. if test -z "$1"
  535. then
  536. echo "usage: tx <session_name>"
  537. tmux list-sessions
  538. elif test -z "$TMUX"
  539. then
  540. # -A: attach if already exists
  541. # tmux new-session -A -c "$HOME" -s "$1"
  542. tmux new-session -A -s "$1"
  543. else
  544. # create new session if not exists yet
  545. # tmux has-session -t "$1" || TMUX= tmux new-session -d -c "$HOME" -s "$1"
  546. tmux has-session -t "$1" || TMUX= tmux new-session -d -s "$1"
  547. tmux switch-client -t "$1"
  548. fi
  549. }
  550. dt(){
  551. # dt [-h] [<name>] [<command ...>]
  552. __dtach_dir="${TMP}/dtach"
  553. mkdir -p "${__dtach_dir}"
  554. if test -n "${__MY_DTACH}"
  555. then
  556. # if already in dtach session print current session
  557. soc_name="`basename "$__MY_DTACH"`"
  558. echo "Current session: ${soc_name}"
  559. fi
  560. if test -z "$1"
  561. then
  562. # if no arg given show list of sessions
  563. echo "Sessions:"
  564. ls "${__dtach_dir}"
  565. return 0
  566. elif test "$1" = "-h"
  567. then
  568. echo "dt: usage: dt [-h] [<name>] [<command ...>]" 1>&2
  569. return 1
  570. fi
  571. # set socket name
  572. soc_name="${__dtach_dir}/$1"
  573. shift
  574. if test -n "$__MY_DTACH"
  575. then
  576. echo "dtach session cannot be nested." 1>&2
  577. return 1
  578. elif test -S "$soc_name"
  579. then
  580. dtach -a "$soc_name" -e ^^
  581. elif test -e "$soc_name"
  582. then
  583. echo "dt: File named $soc_name already exists."
  584. return 1
  585. elif test -z "$1"
  586. then
  587. # if no command given invoke current shell
  588. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
  589. # echo "dt: Socket named $soc_name not exists and no command specified."
  590. # return 1
  591. else
  592. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
  593. fi
  594. }
  595. scr(){
  596. test -n "$1" && pf="${1}-"
  597. ___tformat="%Y%m%d-%H%M%S%z"
  598. ___file="${HOME}/${pf}`date +${___tformat}`.script"
  599. __MY_SCRIPT=${___file} script ${___file} "$@"
  600. }
  601. dtscr(){
  602. # dtscr <command ...>
  603. if test -z "$1"
  604. then
  605. echo "dtscr: usage: dtscr <command ...>"
  606. return 1
  607. fi
  608. ___cmdstr="`echo $@ | tr ' ' +`"
  609. ___tformat="%Y%m%d-%H%M%S%z"
  610. ___name="${pf}`date +${___tformat}`-${___cmdstr}"
  611. ___scr_file="${HOME}/${___name}.script"
  612. ___dt_dir="${TMP}/dtscr"
  613. mkdir -p "$___dt_dir"
  614. dtach -n "${___dt_dir}/${___name}" script "${___scr_file_}" "$@"
  615. # echo $_name
  616. # echo $_file
  617. }
  618. mcrypt_stream(){
  619. test $# -eq 2 || return 1
  620. case $1 in
  621. en)
  622. mcrypt --key $2 | base64 ;;
  623. de)
  624. base64 -d | mcrypt -d --key $2 ;;
  625. esac
  626. }
  627. gpg_stream(){
  628. test $# -eq 2 || return 1
  629. case $1 in
  630. en)
  631. gpg --passphrase $2 -c --batch |base64 ;;
  632. de)
  633. base64 -d|gpg --passphrase $2 -d --batch ;;
  634. esac
  635. }
  636. dgpg(){
  637. if test "$1" = help || test -z "$2"
  638. then
  639. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  640. return
  641. fi
  642. ___srcs="$2"
  643. ___dsts="$3"
  644. test -z "$___dsts" && ___dsts="${___srcs}.out"
  645. ___pw=
  646. echo -n "dgpg pw: "
  647. read -s ___pw
  648. echo ""
  649. test -z "$___pw" && return 1
  650. for f in *${___srcs}
  651. do
  652. ___d="$(basename "$f" "${___srcs}")${___dsts}"
  653. echo -n "Processing $f to $___d..."
  654. if test -d "$f"
  655. then
  656. echo "`printf 'failed (%s is directory)' $f`"
  657. elif test -f "$___d"
  658. then
  659. echo "`printf 'failed (%s is already exists)' $___d`"
  660. elif <"$f" gpg_stream $1 $___pw >"$___d" 2>/dev/null
  661. then
  662. echo "done"
  663. else
  664. echo "failed"
  665. test -f "$___d" && rm "$___d"
  666. fi
  667. done
  668. }
  669. alias enst="gpg_stream en"
  670. alias dest="gpg_stream de"
  671. showinfo(){
  672. echo "Japanese letters are 表示可能"
  673. __safe_run diskinfo
  674. ! $isdarwin && test -n "${DISPLAY}" && {
  675. __safe_run xrandr | \grep --color=never ^Screen
  676. }
  677. $iswindows || __safe_run finger $USER
  678. LANG=C __safe_runc id
  679. __safe_run xset q
  680. }
  681. x(){
  682. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  683. #mkdir -p ~/.var/log
  684. # nohup startx >~/.var/log/xorg.log 2>&1 &
  685. # exit
  686. exec startx
  687. else
  688. echo "X cant be started! Another X is already running?" 1>&2
  689. fi
  690. }
  691. bak(){
  692. for file in "$@"
  693. do
  694. cp -v ${file} ${file}.bak
  695. done
  696. }
  697. di(){
  698. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  699. then
  700. ___diffcmd=colordiff
  701. else
  702. ___diffcmd=diff
  703. fi
  704. ${___diffcmd} -u "$@" | ${PAGER}
  705. }
  706. tb(){
  707. __datenum=`date +%Y%m%d-%H%M%S`
  708. __tb="$HOME/.var/tb/$__datenum"
  709. mkdir -p "$__tb"
  710. mv "$@" "$__tb"
  711. }
  712. mkdd(){
  713. _d=`date +%Y%m%d-%H%M%S` && \
  714. mkdir -p "$_d"
  715. }
  716. mkcd(){
  717. if test -z "$1"
  718. then
  719. echo "mkcd: usage: mkcd <dir>"
  720. return 1
  721. elif test -d "$1"
  722. then
  723. echo "Dir \"$1\" already exists."
  724. else
  725. mkdir -p "$1"
  726. echo "Dir \"$1\" created."
  727. fi
  728. cd "$1"
  729. }
  730. mkcdd(){
  731. # make and change date directory
  732. _d=`date +%Y%m%d-%H%M%S` && \
  733. mkcd "$_d"
  734. }
  735. if test -n "$TMUX" && null type reattach-to-user-namespace
  736. then
  737. alias pbpaste="reattach-to-user-namespace pbpaste"
  738. alias pbcopy="reattach-to-user-namespace pbcopy"
  739. fi
  740. catclip(){
  741. if $iswindows
  742. then
  743. cat /dev/clipboard | tr -d \\r
  744. elif $isdarwin
  745. then
  746. pbpaste
  747. else
  748. xclip -o -selection "clipboard"
  749. fi
  750. }
  751. setclip(){
  752. if test $# -eq 0
  753. then
  754. exec 3<&0
  755. else
  756. exec 3<<__EOF__
  757. `cat "$@"`
  758. __EOF__
  759. fi
  760. if $iswindows
  761. then
  762. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  763. elif $isdarwin
  764. then
  765. pbcopy 0<&3
  766. else
  767. 0<&3 xclip -i -f -selection "primary" | \
  768. xclip -i -f -selection "clipboard"
  769. fi
  770. exec 3<&-
  771. }
  772. open_file(){
  773. if $iscygwin
  774. then
  775. cygstart "$@"
  776. elif $ismsys
  777. then
  778. cmd.exe //c start "" "$@"
  779. elif $isdarwin
  780. then
  781. touch "$@"
  782. open "$@"
  783. elif $islinux
  784. then
  785. touch "$@"
  786. if null type pcmanfm; then
  787. LC_MESSAGES= pcmanfm "$@"
  788. else
  789. LC_MESSAGES= xdg-open "$@" &
  790. fi
  791. else
  792. cat "$@"
  793. fi
  794. }
  795. o(){
  796. if test $# -eq 0
  797. then
  798. open_file .
  799. else
  800. for f in "$@"
  801. do
  802. open_file "$(realpath "$f")"
  803. done
  804. fi
  805. }
  806. convmv_sjis2utf8_test(){
  807. convmv -r -f sjis -t utf8 *
  808. }
  809. convmv_sjis2utf8_notest(){
  810. convmv -r -f sjis -t utf8 * --notest
  811. }
  812. #################################################
  813. ## pastebin services
  814. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  815. sprunge(){
  816. # http://sprunge.us
  817. if test -z "$1"
  818. then
  819. curl -F 'sprunge=<-' http://sprunge.us
  820. else
  821. curl http://sprunge.us/$1
  822. fi
  823. }
  824. dpaste(){
  825. # http://dpaste.de
  826. if test -z "$1"
  827. then
  828. curl -F 'content=<-' https://dpaste.de/api/
  829. echo
  830. else
  831. curl https://dpaste.de/$1/raw/
  832. fi
  833. }
  834. ######################################
  835. ## Prompt Settings
  836. __my_moc_state(){
  837. type mocp >/dev/null 2>&1 || return
  838. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  839. printf "$1" "`mocp -Q %title 2>/dev/null`"
  840. }
  841. __my_parse_svn_branch() {
  842. ___svn_url=$(LANG=C svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  843. ___svn_repository_root=$(LANG=C svn info 2>/dev/null | \
  844. sed -ne 's#^Repository Root: ##p')
  845. echo ${___svn_url} | sed -e 's#^'"${___svn_repository_root}"'##g' | \
  846. awk '{print $1}'
  847. }
  848. __my_svn_ps1(){
  849. if svn status >/dev/null 2>&1
  850. then
  851. ___svn_branch=$(__my_parse_svn_branch)
  852. test -n "${___svn_branch}" && printf "$1" "{$___svn_branch}"
  853. fi
  854. }
  855. __my_battery_status(){
  856. ___dir=/sys/class/power_supply/BAT0
  857. if test -d $___dir && test -r $___dir/status && test -r $___dir/charge_full && \
  858. test -r $___dir/charge_now
  859. then
  860. ___st=$(cat $___dir/status)
  861. ___full=$(cat $___dir/charge_full)
  862. ___now=$(cat $___dir/charge_now)
  863. ___rate=$(expr "$___now" \* 100 / "$___full")
  864. printf "$1" "${___st}:${___rate}%"
  865. fi
  866. }
  867. alias bat='__my_battery_status %s\\n'
  868. __my_ps1_scale(){
  869. if null type stty && ! $ismsys
  870. then
  871. echo "[LxC:`stty size | tr -d $'\n' | tr " " x`]"
  872. fi
  873. }
  874. __my_ps1_tmux(){
  875. null type tmux || return $last
  876. ___tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  877. test -n "$TMUX" && echo "[TMUX:$___tmuxc]"
  878. }
  879. __my_ps1_moc(){
  880. __my_moc_state "[MOC:%s]"
  881. }
  882. for f in /usr/share/git/git-prompt.sh \
  883. /usr/share/git-core/contrib/completion/git-prompt.sh \
  884. /usr/local/share/git-core/contrib/completion/git-prompt.sh \
  885. /etc/bash_completion.d/git-prompt \
  886. /opt/local/share/git-core/git-prompt.sh \
  887. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  888. do
  889. test -r "$f" && ($inbash || $inzsh) && . "$f" && break
  890. done
  891. GIT_PS1_SHOWDIRTYSTATE=t
  892. GIT_PS1_SHOWUPSTREAM=t
  893. __my_ps1_git(){
  894. null type __git_ps1 || return $last
  895. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  896. __git_ps1 "[GIT:$(__safe_run git config --get user.name):%s]"
  897. }
  898. __printf_ipaddr(){
  899. # ipaddress <fmt>
  900. type ip >/dev/null 2>&1 || return 1
  901. ___ip=$(LANG=C ip addr show scope global | \
  902. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  903. test -n "$___ip" && printf "$1" $___ip
  904. }
  905. alias addr="__printf_ipaddr '%s'"
  906. __my_ps1_ipaddr(){
  907. ! $iswindows && __printf_ipaddr '[Addr:%s]'
  908. }
  909. __my_ps1_bttry(){
  910. ___bst="$__homerun/batterystatus"
  911. if test -z "$DISPLAY" && ! $iswindows
  912. then
  913. test -f $___bst && ___bstr="$(cat $___bst)"
  914. test -n "$___bstr" && ! echo $___bstr | grep 100 >/dev/null 2>&1 && \
  915. echo "[Battery:${___bstr}]"
  916. __my_battery_status %s >$___bst &
  917. fi
  918. }
  919. __my_ps1_memo(){
  920. test -f memo.txt && echo "m:`du -b memo.txt|cut -f 1`"
  921. }
  922. __my_ps1_dirs(){
  923. dirs | wc -l
  924. }
  925. __my_ps1_jobs(){
  926. # __my_ps1_jobs [<num>]
  927. if test -n "$1"
  928. then
  929. jobs="$1"
  930. else
  931. jobs="`jobs | wc -l`"
  932. fi
  933. if test "$jobs" -gt 0
  934. then
  935. echo "JOBS:$jobs"
  936. fi
  937. }
  938. __my_ps1_dollar(){
  939. if test -z "$SHLVL"
  940. then
  941. printf "$1"
  942. else
  943. perl -e 'while($ARGV[0]-- > 0){print "$ARGV[1]";}' $SHLVL "$1"
  944. fi
  945. }
  946. __my_alert_fail(){
  947. test $laststatus -eq 0 || \
  948. echo "STATUS:${laststatus}"
  949. }
  950. # About ansi escape sequences
  951. # http://archive.linux.or.jp/JF/JFdocs/Bash-Prompt-HOWTO-5.html
  952. # http://www.grapecity.com/japan/powernews/column/clang/047/page02.htm
  953. if $inbash || $inzsh
  954. then
  955. if $inzsh
  956. then
  957. __attr_beg=$'%{\033['
  958. __attr_end='m%}'
  959. else
  960. __attr_beg='\[\033['
  961. __attr_end='m\]'
  962. fi
  963. __color_default="${__attr_beg}0${__attr_end}"
  964. __color_black="${__attr_beg}0;30${__attr_end}"
  965. __color_red="${__attr_beg}0;31${__attr_end}"
  966. __color_green="${__attr_beg}0;32${__attr_end}"
  967. __color_yellow="${__attr_beg}0;33${__attr_end}"
  968. __color_blue="${__attr_beg}0;34${__attr_end}"
  969. __color_magenta="${__attr_beg}0;35${__attr_end}"
  970. __color_cyan="${__attr_beg}0;36${__attr_end}"
  971. __color_white="${__attr_beg}0;37${__attr_end}"
  972. __color_light_black="${__attr_beg}1;30${__attr_end}"
  973. __color_light_red="${__attr_beg}1;31${__attr_end}"
  974. __color_light_green="${__attr_beg}1;32${__attr_end}"
  975. __color_light_yellow="${__attr_beg}1;33${__attr_end}"
  976. __color_light_blue="${__attr_beg}1;34${__attr_end}"
  977. __color_light_magenta="${__attr_beg}1;35${__attr_end}"
  978. __color_light_cyan="${__attr_beg}1;36${__attr_end}"
  979. __color_light_white="${__attr_beg}1;37${__attr_end}"
  980. __color_bg_black="${__attr_beg}40${__attr_end}"
  981. __color_bg_red="${__attr_beg}41${__attr_end}"
  982. __color_bg_green="${__attr_beg}42${__attr_end}"
  983. __color_bg_yellow="${__attr_beg}43${__attr_end}"
  984. __color_bg_blue="${__attr_beg}44${__attr_end}"
  985. __color_bg_magenta="${__attr_beg}45${__attr_end}"
  986. __color_bg_cyan="${__attr_beg}46${__attr_end}"
  987. __color_bg_white="${__attr_beg}47${__attr_end}"
  988. __attr_underline="${__attr_beg}4${__attr_end}"
  989. __attr_reverse="${__attr_beg}7${__attr_end}"
  990. __attr_bold="${__attr_beg}1${__attr_end}"
  991. fi
  992. # NOTE: tput is another easy way to set colors and background
  993. # For example, "$(tput setab 4)text$(tput sgr0)" print text with background
  994. # color blue.
  995. # http://www.ibm.com/developerworks/jp/linux/aix/library/au-learningtput/index.html
  996. if test "$TERM" != dumb
  997. then
  998. __my_c1="$__attr_bold$__attr_underline" # color for PWD
  999. __my_c2="$__attr_bold$__attr_underline" # color for user and hostname
  1000. # color for ::
  1001. eval "__my_c4=\${__color_light_${_HOSTCOLOR_1}}"
  1002. __my_c5="$__color_black$__color_bg_white" # color for SCR
  1003. __my_cdef="$__color_default"
  1004. fi
  1005. if $inbash
  1006. then
  1007. __my_ps1_sh="[BASH:$BASH_VERSION]"
  1008. elif $inzsh
  1009. then
  1010. __my_ps1_sh="[ZSH:$ZSH_VERSION]"
  1011. fi
  1012. __my_ps1_info1(){
  1013. # first line of PS1
  1014. echo "${__my_ps1_sh}$(__my_ps1_scale)$(__my_ps1_git)$(__my_ps1_bttry)$(__my_ps1_moc)"
  1015. }
  1016. test -n "$__MY_SCRIPT" && \
  1017. __my_ps1_str_scr="SCR"
  1018. test -n "$SSH_CONNECTION" && \
  1019. __my_ps1_str_ssh="SSH"
  1020. test -n "$__MY_DTACH" && \
  1021. __my_ps1_str_dt="DT:`basename "$__MY_DTACH$"`"
  1022. __my_ps1_info2(){
  1023. # second line of PS1
  1024. echo $(__my_ps1_memo) $(__my_ps1_jobs) ${__my_ps1_str_scr} \
  1025. ${__my_ps1_str_ssh} ${__my_ps1_str_dt} $(__my_alert_fail) \
  1026. | sed -e 's/ /|/g'
  1027. }
  1028. __my_ps1_beg="${__my_c4}:: ${__my_cdef}"
  1029. __my_ps1_save_pos="\[\033[s\]"
  1030. __my_ps1_restore_pos="\[\033[u\]"
  1031. __my_ps1_move_rightmost="\[\033[\$(tput cols)C\]"
  1032. __my_ps1_move_15left="\[\033[15D\]"
  1033. # collapse when command line is too long and try to write over this string
  1034. # __my_ps1_right="${__my_ps1_save_pos}${__my_ps1_move_rightmost}"
  1035. # ${__my_ps1_move_15left}\D{%Y/%m/%d %H:%M}${__my_ps1_restore_pos}
  1036. if $inzsh
  1037. then
  1038. PROMPT="\
  1039. ${__my_ps1_beg}[${__my_c2}%n@%M${__my_cdef}:${__my_c1}%~/${__my_cdef}]\$(__my_ps1_info1)
  1040. ${__my_ps1_beg}\$(__my_ps1_info2) $(__my_ps1_dollar %#) "
  1041. RPROMPT="%D{%Y/%m/%d %H:%M}"
  1042. elif $inbash
  1043. then
  1044. PS1="\
  1045. ${__my_ps1_beg}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_info1)\n\
  1046. ${__my_ps1_beg}\D{%Y/%m/%d %H:%M} \$(__my_ps1_info2)${__my_ps1_right} $(__my_ps1_dollar \\$) "
  1047. else
  1048. true
  1049. # PS1="$(printf $(whoami)@$(hostname)$ )"
  1050. fi
  1051. ###################################
  1052. # set header and titles
  1053. __my_set_header_line(){
  1054. # save current position
  1055. printf "\033[s"
  1056. # move to 0,0
  1057. printf "\033[0;0H"
  1058. # clear curent to eol
  1059. printf "\033[K"
  1060. # inverse color
  1061. printf "\033[7m"
  1062. printf "$1"
  1063. # restore color
  1064. printf "\033[0m"
  1065. # restore saved position
  1066. printf "\033[u"
  1067. }
  1068. __my_set_screen_name(){
  1069. # set window name
  1070. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  1071. then
  1072. echo -ne "\033k$1\033\\"
  1073. fi
  1074. }
  1075. __my_set_title(){
  1076. case $TERM in
  1077. (rxvt*|xterm*|aterm|screen*)
  1078. test -t 1 &&
  1079. test -z "$EMACS" &&
  1080. echo -n -e "\033]0;$1\007"
  1081. ;;
  1082. esac
  1083. }
  1084. if test -n "$TMUX"
  1085. then
  1086. # running tmux locally
  1087. __terminal_title="\$(basename \${PWD})"
  1088. elif test -n "$SSH_CONNECTION" && expr "$TERM" : '^screen' >/dev/null
  1089. then
  1090. # ssh connect from tmux terminal
  1091. __terminal_title="`whoami`@`hostname`:\$(basename \${PWD})"
  1092. else
  1093. __terminal_title="`whoami`@`hostname`:\${PWD}"
  1094. fi
  1095. if $inzsh
  1096. then
  1097. precmd(){
  1098. laststatus=$?
  1099. eval __my_set_title ${__terminal_title}
  1100. }
  1101. else
  1102. PROMPT_COMMAND="laststatus=\$?;__my_set_title \"${__terminal_title}\";$PROMPT_COMMAND"
  1103. fi
  1104. laststatus=0