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.
 
 
 
 
 
 

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