25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1405 lines
33 KiB

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