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.
 
 
 
 
 
 

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