選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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