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.
 
 
 
 
 
 

1370 line
32 KiB

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