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.
 
 
 
 
 
 

1234 lines
29 KiB

  1. #!/bin/sh
  2. expr "$-" : '^.*i' >/dev/null || return
  3. ##########################################
  4. null(){
  5. "$@" >/dev/null 2>&1
  6. }
  7. __safe_run(){
  8. type $1 >/dev/null 2>&1 && "$@"
  9. }
  10. __match(){
  11. # __match str word
  12. # return 0 if word is found in str
  13. expr "$1" : ".*$2.*" >/dev/null
  14. }
  15. #################################
  16. # profile-like setups
  17. # aliases:
  18. # isinteractive: true if the current session is interactive
  19. # issourced: true if this file is sourced from another file (not so assured)
  20. # __firstload: true if this file is sourced for the first time (not so
  21. # assured)
  22. __safe_add_path_r(){
  23. test -d "$1" && PATH="${PATH}:$1"
  24. }
  25. __safe_add_path_l(){
  26. test -d "$1" && PATH="$1:${PATH}"
  27. }
  28. __safe_add_path_l "$HOME/.local/bin"
  29. __safe_add_path_l "$HOME/.local/lib/gems/bin"
  30. __safe_add_path_r "/c/mingw/bin"
  31. __safe_add_path_r "/c/mingw/msys/1.0/bin"
  32. # macports coreutils
  33. # $isdarwin cannot be used it is not defined yet
  34. __safe_add_path_l "/opt/local/bin"
  35. __safe_add_path_l "/opt/local/sbin"
  36. __safe_add_path_l "/opt/local/libexec/gnubin"
  37. __safe_add_path_l \
  38. "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/bin"
  39. test -f "${__dotdir}/rc.py" && export PYTHONSTARTUP="${__dotdir}/rc.py"
  40. test -d "$HOME/.local/lib/python/site-packages" && \
  41. export PYTHONPATH="${PYTHONPATH}:${HOME}/.local/lib/python/site-packages"
  42. export GEM_HOME="$HOME/.local/lib/gems"
  43. export RUBYLIB="$RUBYLIB:$HOME/.local/lib/gems/lib"
  44. # it is not so good
  45. # http://archive.linux.or.jp/JF/JFdocs/Program-Library-HOWTO/shared-libraries.html
  46. # http://superuser.com/questions/324613/installing-a-library-locally-in-home-directory-but-program-doesnt-recognize-it
  47. # without this ENV i cannot run tmux. another way is to use --disable-shared
  48. # when building tmux
  49. if ! __match "$LD_LIBRARY_PATH" "$HOME/.local/lib"
  50. then
  51. export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$HOME/.local/lib"
  52. fi
  53. # in my environment powerdown does not work
  54. test -z "$SSH_CONNECTION" && \
  55. type setterm >/dev/null 2>&1 && \
  56. setterm -blank 30 -powersave on # -powerdown 10
  57. #########################
  58. # shrc.common
  59. # this variable must consistent with setup.sh
  60. __shrc_common="$HOME/.shrc.common"
  61. if test -f "$__shrc_common"
  62. then
  63. . "$__shrc_common"
  64. else
  65. echo "$__shrc_common not found."
  66. echo "Run setup.sh first."
  67. return
  68. fi
  69. ##########################
  70. # system type
  71. gnu_coreutils=false # for mac
  72. null ls --version && gnu_coreutils=true
  73. inbash=false
  74. inzsh=false
  75. if test -n "$BASH_VERSION"
  76. then
  77. inbash=true
  78. elif test -n "$ZSH_VERSION"
  79. then
  80. inzsh=true
  81. fi
  82. #################################
  83. # file pathes:
  84. # shrc: Path to this file
  85. if $inbash
  86. then
  87. __shrc="$BASH_SOURCE"
  88. elif $inzsh
  89. then
  90. __shrc="$0"
  91. fi
  92. ##################################
  93. # EnvVal definitions
  94. export LANG=ja_JP.UTF-8
  95. export LC_MESSAGES=C
  96. export LC_TIME=C
  97. export TERMCAP="${TERMCAP}:vb="
  98. $ismsys && export HOSTNAME
  99. # export ENV=~/.shrc
  100. if ! $gnu_coreutils
  101. then
  102. export LSCOLORS=gxfxcxdxbxegedabagacad
  103. else
  104. # http://qiita.com/yuyuchu3333/items/84fa4e051c3325098be3
  105. null type dircolors && eval `dircolors`
  106. fi
  107. if false $iswindows
  108. then
  109. export PAGER='tr -d \\r | less'
  110. else
  111. export PAGER="less"
  112. fi
  113. export LESS="-iRMX"
  114. # Style for lesspipe is defined in esc.style
  115. _src_hilite_lp_path="`which src-hilite-lesspipe.sh 2>/dev/null`"
  116. for f in /usr/share/source-highlight/src-hilite-lesspipe.sh
  117. do
  118. test -z "$_src_hilite_lp_path" && test -e "$f" && _src_hilite_lp_path="$f"
  119. done
  120. test -n "$_src_hilite_lp_path" && export LESSOPEN="| $_src_hilite_lp_path %s"
  121. if null type vim
  122. then
  123. export EDITOR=vim
  124. else
  125. export EDITOR=vi
  126. fi
  127. # export CDPATH=".:~"
  128. export VISUAL="$EDITOR"
  129. export GIT_PAGER="less -FS"
  130. export GIT_EDITOR="$EDITOR"
  131. export GIT_MERGE_AUTOEDIT=no
  132. if test -n "$TMUX" && \
  133. __match $TERM screen && \
  134. __match `tmux display -p '#{client_termname}'` 256color
  135. then
  136. TERM=screen-256color
  137. fi
  138. if test -z "$TMP"
  139. then
  140. if test -n "$TMPDIR"
  141. then
  142. export TMP=$TMPDIR
  143. elif test -n "$TEMP"
  144. then
  145. export TMP="$TEMP"
  146. else
  147. export TMP=/tmp
  148. fi
  149. fi
  150. __match "$TMP" "${USER}-tmp" >/dev/null || export TMP="${TMP}/${USER}-tmp"
  151. export TEMP="$TMP"
  152. test -d "$TMP" || install -d "$TMP"
  153. if ! $iswindows && null type stty
  154. then
  155. stty stop undef # unbind C-s to stop displaying output
  156. # stty erase '^h'
  157. fi
  158. if $iswindows
  159. then
  160. export USER=$USERNAME
  161. fi
  162. if test -d ~/dbx
  163. then
  164. export CHIT_PATH="$HOME/dbx/.chit"
  165. fi
  166. ##########################
  167. # Zsh specific preferences
  168. # http://www.clear-code.com/blog/2011/9/5.html
  169. if $inzsh
  170. then
  171. bindkey -e
  172. # http://zsh.sourceforge.net/Guide/zshguide06.html#l147
  173. autoload compinit; compinit
  174. # supress cycle by tab
  175. unsetopt auto_menu
  176. # unsetopt correct
  177. setopt auto_list
  178. setopt bash_auto_list
  179. setopt magic_equal_subst
  180. setopt list_types
  181. zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
  182. zstyle ':completion:*' format '%B%d%b'
  183. zstyle ':completion:*' group-name ''
  184. zstyle ':completion:*' use-cache yes
  185. # zstyle ':completion:*:cd:*' tag-order local-directories
  186. zstyle ':completion:*' completer _complete _bash_completions \
  187. _history
  188. # zstyle ':completion:*:*:cd:*' completer
  189. zstyle ':completion:*' accept-exact-dirs true
  190. autoload colors; colors
  191. # autoload -Uz promptinit
  192. # promptinit
  193. # prompt walters
  194. setopt hist_ignore_dups
  195. setopt share_history
  196. setopt prompt_subst
  197. fi
  198. ######################
  199. # Print welcome messages
  200. $iswindows && alias tty="echo cmd.exe"
  201. type fortune >/dev/null 2>&1 && {
  202. fortune
  203. echo
  204. fortune -o
  205. echo
  206. }
  207. uname -a
  208. echo TERM $TERM $(tput colors) colors connected to $(tty), \
  209. running $BASH $BASH_VERSION
  210. if test -n "$TMUX"
  211. then
  212. tmux display -p 'Using tmux #S:#I:#W.#P, client is #{client_termname}' \
  213. 2>/dev/null
  214. echo
  215. fi
  216. ###################################
  217. # some aliases and functions
  218. # __func_name: never used interactively
  219. # _func_name: usually not used interactively
  220. __safe_alias(){
  221. # __safe_alias <name>=<command>
  222. _bin=`expr "$1" : '^[^=]*=\([^ ]*\)'`
  223. test -n "$_bin" && \
  224. null type $_bin && \
  225. alias "$1"
  226. }
  227. ( ! $gnu_coreutils && $isdarwin ) || test "$TERM" = dumb || \
  228. _coloroption=" --color=auto"
  229. ( ! $gnu_coreutils && $isdarwin ) || $iswindows || \
  230. _timeoption=" --time-style=long-iso"
  231. ( ! $gnu_coreutils && $isdarwin ) || _hideoption=" --hide=[A-Z]*" # do not use
  232. _timeformat_iso="%Y-%m-%dT%H:%M:%S%z"
  233. _timeformat_rfc2822="%a, %d %b %Y %T %z"
  234. _timeformat_num="%Y%m%d%H%M%S"
  235. alias datenum="date +$_timeformat_num"
  236. alias ls="ls -hCF${_coloroption}${_timeoption}"
  237. # export GREP_OPTIONS=""
  238. alias gr="grep -n --color=always"
  239. $iswindows && alias grep="grep -n"
  240. # alias ll="ls -l"
  241. # alias la="ls -A"
  242. # alias lla="ls -Al"
  243. alias less="less -F"
  244. __safe_alias em="emacs -nw"
  245. __safe_alias vi=vim
  246. alias pstree="LANG=C pstree"
  247. alias cp="cp -v"
  248. alias mv="mv -v"
  249. alias rm="rm -v"
  250. alias psaux="ps auxww"
  251. alias q=exit
  252. __safe_alias e3=e3em
  253. #alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
  254. alias po=popd
  255. alias pu=pushd
  256. __safe_alias sudo="sudo " # use aliases through sudo
  257. __safe_alias sudoe="sudoedit"
  258. # __safe_alias halt="sudo halt"
  259. # __safe_alias reboot="sudo reboot"
  260. null type dbus-send && {
  261. alias suspend="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  262. /org/freedesktop/UPower org.freedesktop.UPower.Suspend"
  263. alias hibernate="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  264. /org/freedesktop/UPower org.freedesktop.UPower.Hibernate"
  265. }
  266. alias rand="echo \$RANDOM"
  267. __safe_alias xunp="file-roller -h"
  268. __safe_alias pc="sudo \paco -D"
  269. alias pycalc="python -i -c 'from math import *' "
  270. __safe_alias py3=python3
  271. __safe_alias py2=python2
  272. alias _reloadrc="exec \"$SHELL\""
  273. # alias mytime="date +%Y%m%d-%H%M%S"
  274. alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
  275. # type trash >/dev/null 2>&1 && alias rm=trash
  276. __safe_alias mpg123="mpg123 -C -v --title"
  277. __safe_alias xm="xmms2"
  278. #export PLAYER="mpg123 -C -v --title"
  279. __safe_alias screen="screen -e^z^z"
  280. #alias zcd="cd \`zenity --file-selection --directory\`"
  281. __safe_alias gtags="gtags --verbose"
  282. __safe_alias htags="htags --xhtml --symbol --line-number \
  283. --frame --alphabet --verbose"
  284. __safe_alias au=aunpack
  285. __safe_alias lv="lv|less"
  286. __safe_alias rs="rsync --progress --itemize-changes --compress"
  287. if $iscygwin
  288. then
  289. __my_wget_options=" --no-check-certificate"
  290. __safe_alias wget="wget $__my_wget_options"
  291. fi
  292. if $isdarwin
  293. then
  294. alias updatedb="LC_ALL=C updatedb"
  295. # do not use locate installed by macports
  296. test -x /usr/bin/locate && alias locate="/usr/bin/locate"
  297. fi
  298. if $inzsh
  299. then
  300. # somehow not work
  301. # typeset -ga chpwd_functions
  302. # chpwd_functions+=pwd
  303. chpwd(){
  304. pwd
  305. }
  306. else
  307. cd(){
  308. command cd "$@"
  309. pwd
  310. }
  311. fi
  312. # pad
  313. alias pad=notepad
  314. __safe_alias pad=gedit
  315. __safe_alias pad=leafpad
  316. $isdarwin && alias pad="open -e"
  317. __safe_alias wic=wicd-curses
  318. __safe_alias wil="wicd-cli -y -l | head"
  319. #alias wicn="wicd-cli -y -c -n"
  320. wicn(){
  321. if test $# -eq 0
  322. then
  323. local num
  324. wicd-cli -y -l | head
  325. echo -n "input num: "
  326. read num
  327. test -n "$num" && wicd-cli -y -c -n $num
  328. else
  329. wicd-cli -y -c -n $1
  330. fi
  331. }
  332. __find_latest_vimdir(){
  333. vimdir=/usr/share/vim
  334. if test -d "$vimdir"
  335. then
  336. find "$vimdir" -name 'vim??' -type d | sort | tail -n 1
  337. else
  338. echo ""
  339. fi
  340. }
  341. for f in /usr/share/vim/vimcurrent "`__find_latest_vimdir`"
  342. do
  343. test -n "$f" || continue
  344. f="$f/macros/less.sh"
  345. test -f $f && alias vl=$f && break
  346. done
  347. alias pa=pacapt
  348. __safe_alias yt=yaourt
  349. __safe_alias cower="cower --color=auto"
  350. null type pacmatic && {
  351. alias pacman="pacmatic"
  352. export PACMAN="pacmatic"
  353. }
  354. __my_pacman_update_mirrorlist_with_reflector(){
  355. ml=/etc/pacman.d/mirrorlist
  356. cmd="$(expr "$(grep -m 1 reflector $ml)" : '# With: *\(.*\)')"
  357. if test -z "$cmd"
  358. then
  359. cmd="reflector --verbose -l 5 --sort rate --save $ml"
  360. fi
  361. echo "Running $cmd ..." 1>&2
  362. sudo $cmd
  363. }
  364. null type reflector && test -f /etc/pacman.d/mirrorlist && \
  365. alias reflect_mirrorlist=__my_pacman_update_mirrorlist_with_reflector
  366. null type apt-get && {
  367. alias aupgrade="sudo apt-get autoremove --yes && \
  368. sudo apt-get update --yes && sudo apt-get upgrade --yes"
  369. alias aptin="apt-get install"
  370. alias aptsearch="apt-cache search"
  371. alias aptshow="apt-cache show"
  372. }
  373. null type port && {
  374. alias port="port -v"
  375. alias pupgrade="sudo port -v selfupdate && \
  376. { sudo port -v upgrade outdated; }"
  377. }
  378. if $iscygwin
  379. then
  380. null type windate || \
  381. alias windate="cmd.exe //c 'echo %DATE%-%TIME%'"
  382. # alias cygsu="cygstart /cygwinsetup.exe"
  383. fi
  384. g(){
  385. if test $# -eq 0 && null type git-info
  386. then
  387. git info
  388. else
  389. git -c color.ui=always "$@"
  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 yyyyMMdd-hhmmss
  408. if test -z "$1" || test "$1" == "-h"
  409. then
  410. echo "datestr: usage: datestr <yyyyMMddhhmmss>"
  411. return 1
  412. fi
  413. dfmt= # actual format for date command
  414. while test -n "$1"
  415. do
  416. fmt="$1"
  417. while test -n "$fmt"
  418. do
  419. case "$fmt" in
  420. yyyy*) # year
  421. dfmt="${dfmt}%Y"
  422. fmt="`echo "$fmt" | cut -c 5-`"
  423. ;;
  424. yy*) # last two digits of year
  425. dfmt="${dfmt}%y"
  426. fmt="`echo "$fmt" | cut -c 3-`"
  427. ;;
  428. MM*) # month (01..12)
  429. dfmt="${dfmt}%m"
  430. fmt="`echo "$fmt" | cut -c 3-`"
  431. ;;
  432. dd*) # day of month (01..12)
  433. dfmt="${dfmt}%d"
  434. fmt="`echo "$fmt" | cut -c 3-`"
  435. ;;
  436. HH* | hh*) # hour (00..23)
  437. dfmt="${dfmt}%H"
  438. fmt="`echo "$fmt" | cut -c 3-`"
  439. ;;
  440. mm*) # minute (00..59)
  441. dfmt="${dfmt}%M"
  442. fmt="`echo "$fmt" | cut -c 3-`"
  443. ;;
  444. ss*) # second (00..60)
  445. dfmt="${dfmt}%S"
  446. fmt="`echo "$fmt" | cut -c 3-`"
  447. ;;
  448. *)
  449. char=`echo "$fmt" | cut -c 1`
  450. dfmt="${dfmt}${char}"
  451. fmt="`echo "$fmt" | cut -c 2-`"
  452. ;;
  453. esac
  454. done
  455. shift
  456. done
  457. date +"$dfmt"
  458. }
  459. ssh(){
  460. __my_set_screen_title ssh
  461. command ssh "$@"
  462. }
  463. __ssh_with_cd(){
  464. # __ssh_with_cd <host> <directory> [<arg> ...]
  465. if test -z "$2"
  466. then
  467. echo "usage: __ssh_with_cd <host> <directory> [<arg> ...]"
  468. return 1
  469. fi
  470. host="$1"
  471. shift
  472. dir="$1"
  473. shift
  474. ssh "$host" "$@" -t "cd \"$dir\"; \$SHELL -l"
  475. }
  476. memo(){
  477. if test -z "$1"
  478. then
  479. $EDITOR memo.txt
  480. else
  481. $EDITOR "$1/memo.txt"
  482. fi
  483. }
  484. now(){
  485. local tformat="%Y/%m/%d %H:%M:%S %z"
  486. cal
  487. REPLY=
  488. printf "\\r`date "+${tformat}"`"
  489. read -t 1
  490. while test $? -ne 0
  491. do
  492. printf "\\r`date "+${tformat}"`"
  493. read -t 1
  494. done
  495. }
  496. s(){
  497. if git rev-parse --git-dir >/dev/null 2>&1
  498. then
  499. echo ">> git grep -n $@" 1>&2
  500. git grep -n "$@"
  501. elif which ag >/dev/null 2>&1
  502. then
  503. echo ">> ag --pager=\"$PAGER\" $@" 1>&2
  504. ag --pager="$PAGER" "$@"
  505. elif which ack >/dev/null 2>&1
  506. then
  507. echo ">> ack --pager=\"$PAGER\" $@" 1>&2
  508. ack --pager="$PAGER" "$@"
  509. else
  510. echo \
  511. ">> find . " \
  512. "-path '*/.git' -prune -o" \
  513. "-path '*/.svn' -prune -o" \
  514. "-type f -exec grep -nH -e --color=always $@ {} +" 1>&2
  515. if test $# -eq 0
  516. then
  517. echo "No search word given." 1>&2
  518. return 1
  519. fi
  520. find . \
  521. -path '*/.git' -prune -o \
  522. -path '*/.svn' -prune -o \
  523. -type -f -exec grep -nH -e --color=always "$@" {} + \
  524. | $PAGER
  525. fi
  526. }
  527. man(){
  528. env \
  529. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  530. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  531. LESS_TERMCAP_me=$(printf "\e[0m") \
  532. LESS_TERMCAP_se=$(printf "\e[0m") \
  533. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  534. LESS_TERMCAP_ue=$(printf "\e[0m") \
  535. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  536. man "$@"
  537. }
  538. netwait(){
  539. while ! ping -c 1 -t 1 example.com
  540. do
  541. true
  542. done
  543. echo network works.
  544. }
  545. __realpath(){
  546. if type realpath >/dev/null 2>&1
  547. then
  548. command realpath "$@"
  549. else
  550. while ! test -d $1
  551. do
  552. shift
  553. done
  554. (command cd "$d" && echo "$PWD")
  555. # local d="$OLDPWD"
  556. # command cd "$1"
  557. # echo "$PWD"
  558. # command cd "$d"
  559. fi
  560. }
  561. tx(){
  562. if test $# -eq 0
  563. then
  564. echo ":: tx <session> to attach."
  565. tmux ls
  566. elif tmux has -t "$1"
  567. then
  568. tmux attach -t "$1"
  569. else
  570. tmux new -s "$1"
  571. fi
  572. }
  573. _tmux_prefs(){
  574. null type tmux || return 1
  575. tmux set -g mode-keys vi
  576. }
  577. dt(){
  578. # dt [<name>] [<command ...>]
  579. __dtach_dir="${TMP}/dtach"
  580. install -d "${__dtach_dir}"
  581. if test -n "${__MY_DTACH}"
  582. then
  583. soc_name="`echo $__MY_DTACH | sed -e "s ^$__dtach_dir/ "`"
  584. echo "Current session: ${soc_name}"
  585. fi
  586. if test -z "$1"
  587. then
  588. echo "Sessions:"
  589. ls "${__dtach_dir}"
  590. return 0
  591. elif test "$1" = "-h"
  592. then
  593. echo "dt: usage: dt <name> [<command ...>]" 1>&2
  594. return 1
  595. fi
  596. soc_name="${__dtach_dir}/$1"
  597. shift
  598. if test -n "$__MY_DTACH"
  599. then
  600. echo "dtach session cannot be nested." 1>&2
  601. return 1
  602. elif test -S "$soc_name"
  603. then
  604. dtach -a "$soc_name" -e ^^
  605. elif test -e "$soc_name"
  606. then
  607. echo "dt: File named $soc_name already exists."
  608. return 1
  609. elif test -z "$1"
  610. then
  611. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
  612. # echo "dt: Socket named $soc_name not exists and no command specified."
  613. # return 1
  614. else
  615. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
  616. fi
  617. }
  618. scr(){
  619. test -n "$1" && pf="${1}-"
  620. local _tformat="%Y%m%d-%H%M%S%z"
  621. local _file="${HOME}/${pf}`date +${_tformat}`.script"
  622. __MY_SCRIPT=${_file} script ${_file} "$@"
  623. }
  624. dtscr(){
  625. # dtscr <command ...>
  626. if test -z "$1"
  627. then
  628. echo "dtscr: usage: dtscr <command ...>"
  629. return 1
  630. fi
  631. local _cmdstr="`echo $@ | tr ' ' +`"
  632. local _tformat="%Y%m%d-%H%M%S%z"
  633. local _name="${pf}`date +${_tformat}`-${_cmdstr}"
  634. local _scr_file="${HOME}/${_name}.script"
  635. local _dt_dir="${TMP}/dtscr"
  636. install -d "$_dt_dir"
  637. dtach -n "${_dt_dir}/${_name}" script "${_scr_file_}" "$@"
  638. # echo $_name
  639. # echo $_file
  640. }
  641. mcrypt_stream(){
  642. test $# -eq 2 || return 1
  643. case $1 in
  644. en)
  645. mcrypt --key $2 | base64 ;;
  646. de)
  647. base64 -d | mcrypt -d --key $2 ;;
  648. esac
  649. }
  650. gpg_stream(){
  651. test $# -eq 2 || return 1
  652. case $1 in
  653. en)
  654. gpg --passphrase $2 -c --batch |base64 ;;
  655. de)
  656. base64 -d|gpg --passphrase $2 -d --batch ;;
  657. esac
  658. }
  659. dgpg(){
  660. if test "$1" = help || test -z "$2"
  661. then
  662. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  663. return
  664. fi
  665. local srcs="$2"
  666. local dsts="$3"
  667. test -z "$dsts" && dsts="${srcs}.out"
  668. local pw
  669. echo -n "dgpg pw: "
  670. read -s pw
  671. echo ""
  672. test -z "$pw" && return 1
  673. for f in *${srcs}
  674. do
  675. local d="$(basename "$f" "${srcs}")${dsts}"
  676. echo -n "Processing $f to $d..."
  677. if test -d "$f"
  678. then
  679. echo "`printf 'failed (%s is directory)' $f`"
  680. elif test -f "$d"
  681. then
  682. echo "`printf 'failed (%s is already exists)' $d`"
  683. elif <"$f" gpg_stream $1 $pw >"$d" 2>/dev/null
  684. then
  685. echo "done"
  686. else
  687. echo "failed"
  688. test -f "$d" && rm "$d"
  689. fi
  690. done
  691. }
  692. alias enst="gpg_stream en"
  693. alias dest="gpg_stream de"
  694. showinfo(){
  695. echo "Japanese letters are 表示可能"
  696. __safe_run diskinfo
  697. ! $isdarwin && test -n "${DISPLAY}" && {
  698. __safe_run xrandr | \grep --color=never ^Screen
  699. }
  700. $iswindows || __safe_run finger $USER
  701. LANG=C __safe_runc id
  702. __safe_run xset q
  703. }
  704. x(){
  705. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  706. #mkdir -p ~/.var/log
  707. # nohup startx >~/.var/log/xorg.log 2>&1 &
  708. # exit
  709. exec startx
  710. else
  711. echo "X cant be started! Another X is already running?" 1>&2
  712. fi
  713. }
  714. bak(){
  715. for file in "$@"
  716. do
  717. cp -v ${file} ${file}.bak
  718. done
  719. }
  720. di(){
  721. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  722. then
  723. local diffcmd=colordiff
  724. else
  725. local diffcmd=diff
  726. fi
  727. ${diffcmd} -u "$@" | ${PAGER}
  728. }
  729. tb(){
  730. __datenum=`date +%Y%m%d-%H%M%S`
  731. __tb="$HOME/.var/tb/$__datenum"
  732. install -d "$__tb"
  733. for file in "$@"
  734. do
  735. mv -t "$__tb" "$file"
  736. done
  737. }
  738. mkcd(){
  739. if test -z "$1"
  740. then
  741. echo "mkcd: usage: mkcd <dir>"
  742. return 1
  743. elif test -d "$1"
  744. then
  745. echo "Dir \"$1\" already exists."
  746. else
  747. install -d "$1"
  748. echo "Dir \"$1\" created."
  749. fi
  750. cd "$1"
  751. }
  752. mkcdd(){
  753. # make and change date directory
  754. _d=`date +%Y%m%d-%H%M%S`
  755. mkcd "$_d"
  756. }
  757. if test -n "$TMUX" && null type reattach-to-user-namespace
  758. then
  759. alias pbpaste="reattach-to-user-namespace pbpaste"
  760. alias pbcopy="reattach-to-user-namespace pbcopy"
  761. fi
  762. catclip(){
  763. if $iswindows
  764. then
  765. cat /dev/clipboard | tr -d \\r
  766. elif $isdarwin
  767. then
  768. pbpaste
  769. else
  770. xclip -o -selection "clipboard"
  771. fi
  772. }
  773. setclip(){
  774. if test $# -eq 0
  775. then
  776. exec 3<&0
  777. else
  778. exec 3<<__EOF__
  779. `cat "$@"`
  780. __EOF__
  781. fi
  782. if $iswindows
  783. then
  784. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  785. elif $isdarwin
  786. then
  787. pbcopy 0<&3
  788. else
  789. 0<&3 xclip -i -f -selection "primary" | \
  790. xclip -i -f -selection "clipboard"
  791. fi
  792. exec 3<&-
  793. }
  794. open_file(){
  795. if $iscygwin
  796. then
  797. cygstart "$@"
  798. elif $ismsys
  799. then
  800. cmd.exe //c start "" "$@"
  801. elif $isdarwin
  802. then
  803. touch "$@"
  804. open "$@"
  805. elif $islinux
  806. then
  807. touch "$@"
  808. if null type pcmanfm; then
  809. LC_MESSAGES= pcmanfm "$@"
  810. else
  811. LC_MESSAGES= xdg-open "$@" &
  812. fi
  813. else
  814. cat "$@"
  815. fi
  816. }
  817. o(){
  818. if test $# -eq 0
  819. then
  820. open_file .
  821. else
  822. for f in "$@"
  823. do
  824. open_file "$(realpath "$f")"
  825. done
  826. fi
  827. }
  828. convmv_sjis2utf8_test(){
  829. convmv -r -f sjis -t utf8 *
  830. }
  831. convmv_sjis2utf8_notest(){
  832. convmv -r -f sjis -t utf8 * --notest
  833. }
  834. #################################################
  835. ## pastebin services
  836. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  837. sprunge(){
  838. # http://sprunge.us
  839. if test -z "$1"
  840. then
  841. curl -F 'sprunge=<-' http://sprunge.us
  842. else
  843. curl http://sprunge.us/$1
  844. fi
  845. }
  846. dpaste(){
  847. # http://dpaste.de
  848. if test -z "$1"
  849. then
  850. curl -F 'content=<-' https://dpaste.de/api/
  851. echo
  852. else
  853. curl https://dpaste.de/$1/raw/
  854. fi
  855. }
  856. ######################################
  857. ## Prompt Settings
  858. __my_moc_state(){
  859. type mocp >/dev/null 2>&1 || return
  860. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  861. printf "$1" "`mocp -Q %title 2>/dev/null`"
  862. }
  863. __my_parse_svn_branch() {
  864. local LANG=C
  865. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  866. local svn_repository_root=$(svn info 2>/dev/null | \
  867. sed -ne 's#^Repository Root: ##p')
  868. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | \
  869. awk '{print $1}'
  870. }
  871. __my_svn_ps1(){
  872. if svn status >/dev/null 2>&1
  873. then
  874. local svn_branch=$(__my_parse_svn_branch)
  875. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  876. fi
  877. }
  878. __my_battery_status(){
  879. local dir=/sys/class/power_supply/BAT0
  880. if test -d $dir && test -r $dir/status && test -r $dir/charge_full && \
  881. test -r $dir/charge_now
  882. then
  883. local st=$(cat $dir/status)
  884. local full=$(cat $dir/charge_full)
  885. local now=$(cat $dir/charge_now)
  886. local rate=$(expr $now \* 100 / $full)
  887. printf "$1" "${st}:${rate}%"
  888. fi
  889. }
  890. alias bat='__my_battery_status %s\\n'
  891. ipaddress(){
  892. # ipaddress <fmt>
  893. type ip >/dev/null 2>&1 || return 1
  894. local ip=$(LANG=C ip addr show scope global | \
  895. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  896. test -n "$ip" && printf $1 $ip
  897. }
  898. __my_ps1_str=""
  899. test -n "$__MY_SCRIPT" && __my_ps1_str="${__my_ps1_str}${__my_c5}SCR${__my_cdef} "
  900. test -n "$SSH_CONNECTION" && __my_ps1_str="${__my_ps1_str}${__my_c5}SSH${__my_cdef} "
  901. test -n "$__MY_DTACH" && __my_ps1_str="${__my_ps1_str}${__my_c5}DTACH${__my_cdef} "
  902. __my_ps1_scale(){
  903. if null type stty && ! $ismsys
  904. then
  905. echo "[LxC:`stty size | tr -d $'\n' | tr " " x`]"
  906. fi
  907. }
  908. __my_ps1_tmux(){
  909. null type tmux || return $last
  910. local tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  911. test -n "$TMUX" && echo "[TMUX:$tmuxc]"
  912. }
  913. __my_ps1_moc(){
  914. __my_moc_state "[MOC:%s]"
  915. }
  916. for f in /usr/share/git/git-prompt.sh \
  917. /opt/local/share/git-core/git-prompt.sh \
  918. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  919. do
  920. test -r "$f" && ($inbash || $inzsh) && . "$f" && break
  921. done
  922. GIT_PS1_SHOWDIRTYSTATE=t
  923. GIT_PS1_SHOWUPSTREAM=t
  924. __my_ps1_git(){
  925. null type __git_ps1 || return $last
  926. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  927. __git_ps1 "[GIT:$(__safe_run git config --get user.name):%s]"
  928. }
  929. __my_ps1_ipaddr(){
  930. ! $iswindows && ipaddress '[Addr:%s]'
  931. }
  932. __my_ps1_bttry(){
  933. local bst="${TMP}/batterystatus"
  934. if test -z "$DISPLAY" && ! $iswindows
  935. then
  936. test -f $bst && local bstr="$(cat $bst)"
  937. test -n "$bstr" && ! echo $bstr | grep 100 >/dev/null 2>&1 && \
  938. echo "[Battery:$bstr]"
  939. __my_battery_status %s >$bst &
  940. fi
  941. }
  942. __my_ps1_dirs(){
  943. dirs | wc -l
  944. }
  945. __my_ps1_jobs(){
  946. # __my_ps1_jobs [<num>]
  947. if test -n "$1"
  948. then
  949. jobs="$1"
  950. else
  951. jobs="`jobs | wc -l`"
  952. fi
  953. if test "$jobs" -gt 0
  954. then
  955. echo "[JOBS:$jobs] "
  956. fi
  957. }
  958. __my_alert_fail(){
  959. test $laststatus -eq 0 || \
  960. echo "[STATUS:${laststatus}] "
  961. }
  962. # About ansi escape sequences
  963. # http://archive.linux.or.jp/JF/JFdocs/Bash-Prompt-HOWTO-5.html
  964. # http://www.grapecity.com/japan/powernews/column/clang/047/page02.htm
  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_brown="${__attr_beg}0;33${__attr_end}"
  978. __color_blue="${__attr_beg}0;34${__attr_end}"
  979. __color_purple="${__attr_beg}0;35${__attr_end}"
  980. __color_cyan="${__attr_beg}0;36${__attr_end}"
  981. __color_light_gray="${__attr_beg}0;37${__attr_end}"
  982. __color_dark_gray="${__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_yellow="${__attr_beg}1;33${__attr_end}"
  986. __color_light_blue="${__attr_beg}1;34${__attr_end}"
  987. __color_light_purple="${__attr_beg}1;35${__attr_end}"
  988. __color_light_cyan="${__attr_beg}1;36${__attr_end}"
  989. __color_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_brown="${__attr_beg}43${__attr_end}"
  994. __color_bg_blue="${__attr_beg}44${__attr_end}"
  995. __color_bg_purple="${__attr_beg}45${__attr_end}"
  996. __color_bg_cyan="${__attr_beg}46${__attr_end}"
  997. __color_bg_light_gray="${__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. # NOTE: tput is another easy way to set colors and background
  1002. # For example, "$(tput setab 4)text$(tput sgr0)" print text with background
  1003. # color blue.
  1004. if test "$TERM" != dumb
  1005. then
  1006. __my_c1="$__attr_bold$__attr_underline" # color for PWD
  1007. __my_c2="$__attr_bold$__attr_underline" # color for user and hostname
  1008. # color for ::
  1009. case "`hostname`" in
  1010. arch-aspireone)
  1011. __my_c4="$__color_light_blue"
  1012. ;;
  1013. darwin-mba.local)
  1014. __my_c4="$__color_light_cyan"
  1015. ;;
  1016. newkiwi)
  1017. __my_c4="$__color_light_purple"
  1018. ;;
  1019. *)
  1020. __my_c4="$__color_light_green"
  1021. ;;
  1022. esac
  1023. __my_c5="$__color_black$__color_bg_light_gray" # color for SCR
  1024. __my_cdef="$__color_default"
  1025. fi
  1026. if $inbash
  1027. then
  1028. __my_ps1_sh="[BASH:$BASH_VERSION]"
  1029. elif $inzsh
  1030. then
  1031. __my_ps1_sh="[ZSH:$ZSH_VERSION]"
  1032. fi
  1033. __my_ps1_info(){
  1034. echo "${__my_ps1_sh}$(__my_ps1_scale)$(__my_ps1_git)$(__my_ps1_bttry)$(__my_ps1_ipaddr)$(__my_ps1_moc)"
  1035. }
  1036. _ps1_bash="\
  1037. ${__my_c4}:: ${__my_cdef}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_info)\n\
  1038. ${__my_c4}:: ${__my_cdef}\$(__my_ps1_jobs \j)${__my_ps1_str}\$(__my_alert_fail)\$ "
  1039. $inbash && PS1="$_ps1_bash"
  1040. _ps1_zsh="\
  1041. ${__my_c4}:: ${__my_cdef}[${__my_c2}%n@%M${__my_cdef}:${__my_c1}%~/${__my_cdef}]\$(__my_ps1_info)
  1042. ${__my_c4}:: ${__my_cdef}\$(__my_ps1_jobs)${__my_ps1_str}\$(__my_alert_fail)%# "
  1043. $inzsh && PROMPT="$_ps1_zsh"
  1044. __my_set_header_line(){
  1045. # save current position
  1046. printf "\033[s"
  1047. printf "\033[0;0H"
  1048. printf "\033[K"
  1049. printf "\033[7m"
  1050. printf "$1"
  1051. printf "\033[0m"
  1052. # restore saved position
  1053. printf "\033[u"
  1054. }
  1055. __my_set_screen_title(){
  1056. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  1057. then
  1058. echo -ne "\033k$1\033\\"
  1059. fi
  1060. }
  1061. __my_set_title(){
  1062. case $TERM in
  1063. (rxvt*|xterm*|aterm|screen*)
  1064. test -t 1 &&
  1065. test -n "$DISPLAY" &&
  1066. test -z "$EMACS" &&
  1067. echo -n -e "\033]0;$1\007"
  1068. ;;
  1069. esac
  1070. }
  1071. PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\:\${PWD};
  1072. __my_set_screen_title \$(basename \"\$PWD\")/"
  1073. if $inzsh
  1074. then
  1075. precmd(){
  1076. laststatus=$?
  1077. }
  1078. else
  1079. PROMPT_COMMAND="laststatus=\$?;$PROMPT_COMMAND"
  1080. fi
  1081. laststatus=0