Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

1387 lignes
32 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. # pad
  368. alias pad=notepad
  369. __safe_alias pad=gedit
  370. __safe_alias pad=leafpad
  371. $isdarwin && alias pad="open -e"
  372. __find_latest_vimdir(){
  373. vimdir=/usr/share/vim
  374. if test -d "$vimdir"
  375. then
  376. find "$vimdir" -name 'vim??' -type d | sort | tail -n 1
  377. else
  378. echo ""
  379. fi
  380. }
  381. for f in /usr/share/vim/vimcurrent "`__find_latest_vimdir`"
  382. do
  383. test -n "$f" || continue
  384. f="$f/macros/less.sh"
  385. test -f $f && alias vl=$f && break
  386. done
  387. __safe_alias pa="pacman --color=always"
  388. __safe_alias pa="pacmatic --color=always"
  389. __safe_alias pa="pacapt"
  390. __safe_alias yt=yaourt
  391. # variable for yaourt
  392. null type pacmatic && export PACMAN="pacmatic"
  393. __safe_alias cower="cower --color=auto"
  394. __my_pacman_update_mirrorlist_with_reflector(){
  395. f=/etc/pacman.d/mirrorlist
  396. cmd="$(expr "$(grep -m 1 reflector $f)" : '# With: *\(.*\)')"
  397. if test -z "$cmd"
  398. then
  399. cmd="reflector --verbose --number 5 --sort rate --protocol http --protocol https --save $f"
  400. fi
  401. echo ">>> $cmd ..." 1>&2
  402. sudo sh -c "$cmd" && cat $f
  403. }
  404. null type reflector && test -d /etc/pacman.d && \
  405. alias reflect_mirrorlist=__my_pacman_update_mirrorlist_with_reflector
  406. null type apt-get && {
  407. alias aupgrade="sudo sh -xc 'apt-get autoremove --yes && \
  408. apt-get update --yes && apt-get upgrade --yes'"
  409. alias aptin="apt-get install"
  410. alias aptsearch="apt-cache search"
  411. alias aptshow="apt-cache show"
  412. }
  413. null type port && {
  414. alias port="port -v"
  415. alias pupgrade="sudo sh -xc 'port -v selfupdate && \
  416. port -v upgrade outdated'"
  417. }
  418. if $iscygwin
  419. then
  420. null type windate || \
  421. alias windate="cmd.exe //c 'echo %DATE%-%TIME%'"
  422. # alias cygsu="cygstart /cygwinsetup.exe"
  423. fi
  424. mkpatch(){
  425. if test $# -eq 0
  426. then
  427. echo "usage: mkpatch <olddir> <newdir>"
  428. elif ! test -d "$1"
  429. then
  430. echo "mkpatch: $1 is not a directory"
  431. elif ! test -d "$2"
  432. then
  433. echo "mkpatch: $2 is not a directory"
  434. else
  435. diff -Naur "$1" "$2"
  436. fi
  437. }
  438. __sdcv_less(){
  439. command sdcv -n "$@" | less --quit-if-one-screen
  440. }
  441. command -v sdcv >/dev/null && alias dict=__sdcv_less
  442. g(){
  443. if test $# -eq 0 && null type git-info
  444. then
  445. git info
  446. else
  447. git -c color.ui=always "$@"
  448. fi
  449. }
  450. if null type _git && $inbash
  451. then
  452. # enable programmable completion for g
  453. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  454. || complete -o default -o nospace -F _git g
  455. fi
  456. #git svn --help >/dev/null 2>&1 && alias gsvn="git svn"
  457. __safe_alias m=gitmemo
  458. alias setup.py3="sudo python3 setup.py install --record files.txt"
  459. randomstr(){
  460. len=$1
  461. test -z "$len" && len=8
  462. uuidgen | tr -d - | cut -c 1-len
  463. }
  464. datestr(){
  465. # datestr YYYYMMDDhhmmss
  466. if test -z "$1" || test "$1" = "-h"
  467. then
  468. echo "datestr: usage: datestr <YYYYMMDDhhmmss>"
  469. return 1
  470. fi
  471. dfmt="" # actual format for date command
  472. while test -n "$1"
  473. do
  474. fmt="$1"
  475. shift
  476. while test -n "$fmt"
  477. do
  478. case "$fmt" in
  479. YYYY*|yyyy*) # year
  480. dfmt="${dfmt}%Y"
  481. fmt="`echo "$fmt" | cut -c 5-`"
  482. ;;
  483. YY*|yy*) # last two digits of year
  484. dfmt="${dfmt}%y"
  485. fmt="`echo "$fmt" | cut -c 3-`"
  486. ;;
  487. MM*) # month (01..12)
  488. dfmt="${dfmt}%m"
  489. fmt="`echo "$fmt" | cut -c 3-`"
  490. ;;
  491. DD*|dd*) # day of month (01..12)
  492. dfmt="${dfmt}%d"
  493. fmt="`echo "$fmt" | cut -c 3-`"
  494. ;;
  495. HH* | hh*) # hour (00..23)
  496. dfmt="${dfmt}%H"
  497. fmt="`echo "$fmt" | cut -c 3-`"
  498. ;;
  499. mm*) # minute (00..59)
  500. dfmt="${dfmt}%M"
  501. fmt="`echo "$fmt" | cut -c 3-`"
  502. ;;
  503. SS*|ss*) # second (00..60)
  504. dfmt="${dfmt}%S"
  505. fmt="`echo "$fmt" | cut -c 3-`"
  506. ;;
  507. *)
  508. char=`echo "$fmt" | cut -c 1`
  509. dfmt="${dfmt}${char}"
  510. fmt="`echo "$fmt" | cut -c 2-`"
  511. ;;
  512. esac
  513. done
  514. done
  515. date +"$dfmt"
  516. }
  517. # ssh(){
  518. # # __my_terminal_title ssh
  519. # command ssh "$@"
  520. # }
  521. __ssh_with_cd(){
  522. # __ssh_with_cd <host> <directory> [<arg> ...]
  523. if test -z "$2"
  524. then
  525. echo "usage: __ssh_with_cd <host> <directory> [<arg> ...]"
  526. return 1
  527. fi
  528. host="$1"
  529. shift
  530. dir="$1"
  531. shift
  532. ssh "$host" "$@" -t "cd \"$dir\"; \$SHELL -l"
  533. }
  534. memo(){
  535. if test -z "$1"
  536. then
  537. _memo="memo.txt"
  538. else
  539. _memo="$1/memo.txt"
  540. fi
  541. $EDITOR "$_memo"
  542. if test -z "`cat "$_memo"`"
  543. then
  544. echo "$_memo is empty. Removing."
  545. rm "$_memo"
  546. fi
  547. }
  548. now(){
  549. ___tformat="%Y/%m/%d %H:%M:%S %z"
  550. cal
  551. REPLY=
  552. printf "\\r`date "+${___tformat}"`"
  553. read -t 1
  554. while test $? -ne 0
  555. do
  556. printf "\\r`date "+${___tformat}"`"
  557. read -t 1
  558. done
  559. }
  560. s(){
  561. if git rev-parse --git-dir >/dev/null 2>&1
  562. then
  563. echo ">> git grep -n $@" 1>&2
  564. git grep -n "$@"
  565. elif which ag >/dev/null 2>&1
  566. then
  567. echo ">> ag --pager=\"$PAGER\" $@" 1>&2
  568. ag --pager="$PAGER" "$@"
  569. elif which ack >/dev/null 2>&1
  570. then
  571. echo ">> ack --pager=\"$PAGER\" $@" 1>&2
  572. ack --pager="$PAGER" "$@"
  573. else
  574. echo \
  575. ">> find . " \
  576. "-path '*/.git' -prune -o" \
  577. "-path '*/.svn' -prune -o" \
  578. "-type f -exec grep -nH -e --color=always $@ {} +" 1>&2
  579. if test $# -eq 0
  580. then
  581. echo "No search word given." 1>&2
  582. return 1
  583. fi
  584. find . \
  585. -path '*/.git' -prune -o \
  586. -path '*/.svn' -prune -o \
  587. -type -f -exec grep -nH -e --color=always "$@" {} + \
  588. | $PAGER
  589. fi
  590. }
  591. MYMANPATH='/usr/lib/erlang/man'
  592. if $inbash || $inzsh
  593. then
  594. man(){
  595. env \
  596. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  597. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  598. LESS_TERMCAP_me=$(printf "\e[0m") \
  599. LESS_TERMCAP_se=$(printf "\e[0m") \
  600. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  601. LESS_TERMCAP_ue=$(printf "\e[0m") \
  602. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  603. MANPATH="`manpath`:$MYMANPATH" \
  604. man "$@"
  605. }
  606. fi
  607. netwait(){
  608. while ! ping -c 1 -t 1 example.com
  609. do
  610. true
  611. done
  612. echo network works.
  613. }
  614. __realpath(){
  615. if type realpath >/dev/null 2>&1
  616. then
  617. command realpath "$@"
  618. else
  619. while ! test -d $1
  620. do
  621. shift
  622. done
  623. (command cd "$d" && echo "$PWD")
  624. # local d="$OLDPWD"
  625. # command cd "$1"
  626. # echo "$PWD"
  627. # command cd "$d"
  628. fi
  629. }
  630. tx(){
  631. tmux start-server
  632. if test -z "$1"
  633. then
  634. echo "usage: tx <session_name>"
  635. tmux list-sessions
  636. elif test -z "$TMUX"
  637. then
  638. # -A: attach if already exists
  639. # tmux new-session -A -c "$HOME" -s "$1"
  640. tmux new-session -A -s "$1"
  641. else
  642. # create new session if not exists yet
  643. # tmux has-session -t "$1" || TMUX= tmux new-session -d -c "$HOME" -s "$1"
  644. tmux has-session -t "$1" || TMUX= tmux new-session -d -s "$1"
  645. tmux switch-client -t "$1"
  646. fi
  647. }
  648. dt(){
  649. # dt [-h] [<name>] [<command ...>]
  650. __dtach_dir="${TMP}/dtach"
  651. mkdir -p "${__dtach_dir}"
  652. if test -n "${__MY_DTACH}"
  653. then
  654. # if already in dtach session print current session
  655. soc_name="`basename "$__MY_DTACH"`"
  656. echo "Current session: ${soc_name}"
  657. fi
  658. if test -z "$1"
  659. then
  660. # if no arg given show list of sessions
  661. echo "Sessions:"
  662. ls "${__dtach_dir}"
  663. return 0
  664. elif test "$1" = "-h"
  665. then
  666. echo "dt: usage: dt [-h] [<name>] [<command ...>]" 1>&2
  667. return 1
  668. fi
  669. # set socket name
  670. soc_name="${__dtach_dir}/$1"
  671. shift
  672. if test -n "$__MY_DTACH"
  673. then
  674. echo "dtach session cannot be nested." 1>&2
  675. return 1
  676. elif test -S "$soc_name"
  677. then
  678. dtach -a "$soc_name" -e ^^
  679. elif test -e "$soc_name"
  680. then
  681. echo "dt: File named $soc_name already exists."
  682. return 1
  683. elif test -z "$1"
  684. then
  685. # if no command given invoke current shell
  686. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
  687. # echo "dt: Socket named $soc_name not exists and no command specified."
  688. # return 1
  689. else
  690. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
  691. fi
  692. }
  693. scr(){
  694. test -n "$1" && pf="${1}-"
  695. ___tformat="%Y%m%d-%H%M%S%z"
  696. ___file="${HOME}/${pf}`date +${___tformat}`.script"
  697. __MY_SCRIPT=${___file} script ${___file} "$@"
  698. }
  699. dtscr(){
  700. # dtscr <command ...>
  701. if test -z "$1"
  702. then
  703. echo "dtscr: usage: dtscr <command ...>"
  704. return 1
  705. fi
  706. ___cmdstr="`echo $@ | tr ' ' +`"
  707. ___tformat="%Y%m%d-%H%M%S%z"
  708. ___name="${pf}`date +${___tformat}`-${___cmdstr}"
  709. ___scr_file="${HOME}/${___name}.script"
  710. ___dt_dir="${TMP}/dtscr"
  711. mkdir -p "$___dt_dir"
  712. dtach -n "${___dt_dir}/${___name}" script "${___scr_file_}" "$@"
  713. # echo $_name
  714. # echo $_file
  715. }
  716. mcrypt_stream(){
  717. test $# -eq 2 || return 1
  718. case $1 in
  719. en)
  720. mcrypt --key $2 | base64 ;;
  721. de)
  722. base64 -d | mcrypt -d --key $2 ;;
  723. esac
  724. }
  725. gpg_stream(){
  726. test $# -eq 2 || return 1
  727. case $1 in
  728. en)
  729. gpg --passphrase $2 -c --batch |base64 ;;
  730. de)
  731. base64 -d|gpg --passphrase $2 -d --batch ;;
  732. esac
  733. }
  734. dgpg(){
  735. if test "$1" = help || test -z "$2"
  736. then
  737. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  738. return
  739. fi
  740. ___srcs="$2"
  741. ___dsts="$3"
  742. test -z "$___dsts" && ___dsts="${___srcs}.out"
  743. ___pw=
  744. echo -n "dgpg pw: "
  745. read -s ___pw
  746. echo ""
  747. test -z "$___pw" && return 1
  748. for f in *${___srcs}
  749. do
  750. ___d="$(basename "$f" "${___srcs}")${___dsts}"
  751. echo -n "Processing $f to $___d..."
  752. if test -d "$f"
  753. then
  754. echo "`printf 'failed (%s is directory)' $f`"
  755. elif test -f "$___d"
  756. then
  757. echo "`printf 'failed (%s is already exists)' $___d`"
  758. elif <"$f" gpg_stream $1 $___pw >"$___d" 2>/dev/null
  759. then
  760. echo "done"
  761. else
  762. echo "failed"
  763. test -f "$___d" && rm "$___d"
  764. fi
  765. done
  766. }
  767. alias enst="gpg_stream en"
  768. alias dest="gpg_stream de"
  769. showinfo(){
  770. echo "Japanese letters are 表示可能"
  771. __safe_run diskinfo
  772. ! $isdarwin && test -n "${DISPLAY}" && {
  773. __safe_run xrandr | \grep --color=never ^Screen
  774. }
  775. $iswindows || __safe_run finger $USER
  776. LANG=C __safe_runc id
  777. __safe_run xset q
  778. }
  779. x(){
  780. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  781. #mkdir -p ~/.var/log
  782. # nohup startx >~/.var/log/xorg.log 2>&1 &
  783. # exit
  784. exec startx
  785. else
  786. echo "X cant be started! Another X is already running?" 1>&2
  787. fi
  788. }
  789. bak(){
  790. for file in "$@"
  791. do
  792. cp -v ${file} ${file}.bak
  793. done
  794. }
  795. di(){
  796. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  797. then
  798. ___diffcmd=colordiff
  799. else
  800. ___diffcmd=diff
  801. fi
  802. ${___diffcmd} -u "$@" | ${PAGER}
  803. }
  804. tb(){
  805. __datenum=`date +%Y%m%d-%H%M%S`
  806. __tb="$HOME/.var/tb/$__datenum"
  807. mkdir -p "$__tb"
  808. mv "$@" "$__tb"
  809. }
  810. mkdd(){
  811. _d=`date +%Y%m%d-%H%M%S` && \
  812. mkdir -p "$_d"
  813. }
  814. mkcd(){
  815. if test -z "$1"
  816. then
  817. echo "mkcd: usage: mkcd <dir>"
  818. return 1
  819. elif test -d "$1"
  820. then
  821. echo "Dir \"$1\" already exists."
  822. else
  823. mkdir -p "$1"
  824. echo "Dir \"$1\" created."
  825. fi
  826. cd "$1"
  827. }
  828. mkcdd(){
  829. # make and change date directory
  830. _d=`date +%Y%m%d-%H%M%S` && \
  831. mkcd "$_d"
  832. }
  833. if test -n "$TMUX" && null type reattach-to-user-namespace
  834. then
  835. alias pbpaste="reattach-to-user-namespace pbpaste"
  836. alias pbcopy="reattach-to-user-namespace pbcopy"
  837. fi
  838. catclip(){
  839. if $iswindows
  840. then
  841. cat /dev/clipboard | tr -d \\r
  842. elif $isdarwin
  843. then
  844. pbpaste
  845. else
  846. xclip -o -selection "clipboard"
  847. fi
  848. }
  849. setclip(){
  850. if test $# -eq 0
  851. then
  852. exec 3<&0
  853. else
  854. exec 3<<__EOF__
  855. `cat "$@"`
  856. __EOF__
  857. fi
  858. if $iswindows
  859. then
  860. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  861. elif $isdarwin
  862. then
  863. pbcopy 0<&3
  864. else
  865. 0<&3 xclip -i -f -selection "primary" | \
  866. xclip -i -f -selection "clipboard"
  867. fi
  868. exec 3<&-
  869. }
  870. open_file(){
  871. if $iscygwin
  872. then
  873. cygstart "$@"
  874. elif $ismsys
  875. then
  876. cmd.exe //c start "" "$@"
  877. elif $isdarwin
  878. then
  879. touch "$@"
  880. open "$@"
  881. elif $islinux
  882. then
  883. touch "$@"
  884. if null type pcmanfm; then
  885. LC_MESSAGES= pcmanfm "$@"
  886. else
  887. LC_MESSAGES= xdg-open "$@" &
  888. fi
  889. else
  890. cat "$@"
  891. fi
  892. }
  893. o(){
  894. if test $# -eq 0
  895. then
  896. open_file .
  897. else
  898. for f in "$@"
  899. do
  900. open_file "$(realpath "$f")"
  901. done
  902. fi
  903. }
  904. convmv_sjis2utf8_test(){
  905. convmv -r -f sjis -t utf8 *
  906. }
  907. convmv_sjis2utf8_notest(){
  908. convmv -r -f sjis -t utf8 * --notest
  909. }
  910. #################################################
  911. ## pastebin services
  912. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  913. sprunge(){
  914. # http://sprunge.us
  915. if test -z "$1"
  916. then
  917. curl -F 'sprunge=<-' http://sprunge.us
  918. else
  919. curl http://sprunge.us/$1
  920. fi
  921. }
  922. dpaste(){
  923. # http://dpaste.de
  924. if test -z "$1"
  925. then
  926. curl -F 'content=<-' https://dpaste.de/api/
  927. echo
  928. else
  929. curl https://dpaste.de/$1/raw/
  930. fi
  931. }
  932. ######################################
  933. ## Prompt Settings
  934. __my_moc_state(){
  935. type mocp >/dev/null 2>&1 || return
  936. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  937. printf "$1" "`mocp -Q %title 2>/dev/null`"
  938. }
  939. __my_parse_svn_branch() {
  940. ___svn_url=$(LANG=C svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  941. ___svn_repository_root=$(LANG=C svn info 2>/dev/null | \
  942. sed -ne 's#^Repository Root: ##p')
  943. echo ${___svn_url} | sed -e 's#^'"${___svn_repository_root}"'##g' | \
  944. awk '{print $1}'
  945. }
  946. __my_svn_ps1(){
  947. if svn status >/dev/null 2>&1
  948. then
  949. ___svn_branch=$(__my_parse_svn_branch)
  950. test -n "${___svn_branch}" && printf "$1" "{$___svn_branch}"
  951. fi
  952. }
  953. __my_battery_status(){
  954. ___dir=/sys/class/power_supply/BAT0
  955. if test -d $___dir && test -r $___dir/status && test -r $___dir/charge_full && \
  956. test -r $___dir/charge_now
  957. then
  958. ___st=$(cat $___dir/status)
  959. ___full=$(cat $___dir/charge_full)
  960. ___now=$(cat $___dir/charge_now)
  961. ___rate=$(expr $now \* 100 / $full)
  962. printf "$1" "${___st}:${___rate}%"
  963. fi
  964. }
  965. alias bat='__my_battery_status %s\\n'
  966. __my_ps1_scale(){
  967. if null type stty && ! $ismsys
  968. then
  969. echo "[LxC:`stty size | tr -d $'\n' | tr " " x`]"
  970. fi
  971. }
  972. __my_ps1_tmux(){
  973. null type tmux || return $last
  974. ___tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  975. test -n "$TMUX" && echo "[TMUX:$___tmuxc]"
  976. }
  977. __my_ps1_moc(){
  978. __my_moc_state "[MOC:%s]"
  979. }
  980. for f in /usr/share/git/git-prompt.sh \
  981. /usr/share/git-core/contrib/completion/git-prompt.sh \
  982. /usr/local/share/git-core/contrib/completion/git-prompt.sh \
  983. /etc/bash_completion.d/git-prompt \
  984. /opt/local/share/git-core/git-prompt.sh \
  985. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  986. do
  987. test -r "$f" && ($inbash || $inzsh) && . "$f" && break
  988. done
  989. GIT_PS1_SHOWDIRTYSTATE=t
  990. GIT_PS1_SHOWUPSTREAM=t
  991. __my_ps1_git(){
  992. null type __git_ps1 || return $last
  993. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  994. __git_ps1 "[GIT:$(__safe_run git config --get user.name):%s]"
  995. }
  996. __printf_ipaddr(){
  997. # ipaddress <fmt>
  998. type ip >/dev/null 2>&1 || return 1
  999. ___ip=$(LANG=C ip addr show scope global | \
  1000. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  1001. test -n "$___ip" && printf "$1" $___ip
  1002. }
  1003. alias addr="__printf_ipaddr '%s
  1004. '"
  1005. __my_ps1_ipaddr(){
  1006. ! $iswindows && __printf_ipaddr '[Addr:%s]'
  1007. }
  1008. __my_ps1_bttry(){
  1009. ___bst="${TMP}/batterystatus"
  1010. if test -z "$DISPLAY" && ! $iswindows
  1011. then
  1012. test -f $___bst && ___bstr="$(cat $___bst)"
  1013. test -n "$___bstr" && ! echo $___bstr | grep 100 >/dev/null 2>&1 && \
  1014. echo "[Battery:$___bstr]"
  1015. __my_battery_status %s >$___bst &
  1016. fi
  1017. }
  1018. __my_ps1_memo(){
  1019. test -f memo.txt && echo "m:`du -b memo.txt|cut -f 1`"
  1020. }
  1021. __my_ps1_dirs(){
  1022. dirs | wc -l
  1023. }
  1024. __my_ps1_jobs(){
  1025. # __my_ps1_jobs [<num>]
  1026. if test -n "$1"
  1027. then
  1028. jobs="$1"
  1029. else
  1030. jobs="`jobs | wc -l`"
  1031. fi
  1032. if test "$jobs" -gt 0
  1033. then
  1034. echo "JOBS:$jobs"
  1035. fi
  1036. }
  1037. __my_alert_fail(){
  1038. test $laststatus -eq 0 || \
  1039. echo "STATUS:${laststatus}"
  1040. }
  1041. # About ansi escape sequences
  1042. # http://archive.linux.or.jp/JF/JFdocs/Bash-Prompt-HOWTO-5.html
  1043. # http://www.grapecity.com/japan/powernews/column/clang/047/page02.htm
  1044. if $inbash || $inzsh
  1045. then
  1046. if $inzsh
  1047. then
  1048. __attr_beg=$'%{\033['
  1049. __attr_end='m%}'
  1050. else
  1051. __attr_beg='\[\033['
  1052. __attr_end='m\]'
  1053. fi
  1054. __color_default="${__attr_beg}0${__attr_end}"
  1055. __color_black="${__attr_beg}0;30${__attr_end}"
  1056. __color_red="${__attr_beg}0;31${__attr_end}"
  1057. __color_green="${__attr_beg}0;32${__attr_end}"
  1058. __color_brown="${__attr_beg}0;33${__attr_end}"
  1059. __color_blue="${__attr_beg}0;34${__attr_end}"
  1060. __color_purple="${__attr_beg}0;35${__attr_end}"
  1061. __color_cyan="${__attr_beg}0;36${__attr_end}"
  1062. __color_light_gray="${__attr_beg}0;37${__attr_end}"
  1063. __color_dark_gray="${__attr_beg}1;30${__attr_end}"
  1064. __color_light_red="${__attr_beg}1;31${__attr_end}"
  1065. __color_light_green="${__attr_beg}1;32${__attr_end}"
  1066. __color_yellow="${__attr_beg}1;33${__attr_end}"
  1067. __color_light_blue="${__attr_beg}1;34${__attr_end}"
  1068. __color_light_purple="${__attr_beg}1;35${__attr_end}"
  1069. __color_light_cyan="${__attr_beg}1;36${__attr_end}"
  1070. __color_white="${__attr_beg}1;37${__attr_end}"
  1071. __color_bg_black="${__attr_beg}40${__attr_end}"
  1072. __color_bg_red="${__attr_beg}41${__attr_end}"
  1073. __color_bg_green="${__attr_beg}42${__attr_end}"
  1074. __color_bg_brown="${__attr_beg}43${__attr_end}"
  1075. __color_bg_blue="${__attr_beg}44${__attr_end}"
  1076. __color_bg_purple="${__attr_beg}45${__attr_end}"
  1077. __color_bg_cyan="${__attr_beg}46${__attr_end}"
  1078. __color_bg_light_gray="${__attr_beg}47${__attr_end}"
  1079. __attr_underline="${__attr_beg}4${__attr_end}"
  1080. __attr_reverse="${__attr_beg}7${__attr_end}"
  1081. __attr_bold="${__attr_beg}1${__attr_end}"
  1082. fi
  1083. # NOTE: tput is another easy way to set colors and background
  1084. # For example, "$(tput setab 4)text$(tput sgr0)" print text with background
  1085. # color blue.
  1086. # http://www.ibm.com/developerworks/jp/linux/aix/library/au-learningtput/index.html
  1087. if test "$TERM" != dumb
  1088. then
  1089. __my_c1="$__attr_bold$__attr_underline" # color for PWD
  1090. __my_c2="$__attr_bold$__attr_underline" # color for user and hostname
  1091. # color for ::
  1092. case "`hostname`" in
  1093. arch-aspireone)
  1094. __my_c4="$__color_yellow"
  1095. ;;
  1096. arch-mba)
  1097. __my_c4="$__color_light_cyan"
  1098. ;;
  1099. newkiwi)
  1100. __my_c4="$__color_light_purple"
  1101. ;;
  1102. debian-vb-win7-opti)
  1103. __my_c4="$__color_light_red"
  1104. ;;
  1105. *)
  1106. __my_c4="$__color_light_green"
  1107. ;;
  1108. esac
  1109. __my_c5="$__color_black$__color_bg_light_gray" # color for SCR
  1110. __my_cdef="$__color_default"
  1111. fi
  1112. if $inbash
  1113. then
  1114. __my_ps1_sh="[BASH:$BASH_VERSION]"
  1115. elif $inzsh
  1116. then
  1117. __my_ps1_sh="[ZSH:$ZSH_VERSION]"
  1118. fi
  1119. __my_ps1_info1(){
  1120. # first line of PS1
  1121. echo "${__my_ps1_sh}$(__my_ps1_scale)$(__my_ps1_git)$(__my_ps1_bttry)$(__my_ps1_moc)"
  1122. }
  1123. test -n "$__MY_SCRIPT" && \
  1124. __my_ps1_str_scr="SCR"
  1125. test -n "$SSH_CONNECTION" && \
  1126. __my_ps1_str_ssh="SSH"
  1127. test -n "$__MY_DTACH" && \
  1128. __my_ps1_str_dt="DT:`basename "$__MY_DTACH$"`"
  1129. __my_ps1_info2(){
  1130. # second line of PS1
  1131. echo $(__my_ps1_memo) $(__my_ps1_jobs) ${__my_ps1_str_scr} \
  1132. ${__my_ps1_str_ssh} ${__my_ps1_str_dt} $(__my_alert_fail) \
  1133. | sed -e 's/ /|/g'
  1134. }
  1135. __my_ps1_beg="${__my_c4}:: ${__my_cdef}"
  1136. __my_ps1_save_pos="\[\033[s\]"
  1137. __my_ps1_restore_pos="\[\033[u\]"
  1138. __my_ps1_move_rightmost="\[\033[\$(tput cols)C\]"
  1139. __my_ps1_move_15left="\[\033[15D\]"
  1140. # collapse when command line is too long and try to write over this string
  1141. # __my_ps1_right="${__my_ps1_save_pos}${__my_ps1_move_rightmost}"
  1142. # ${__my_ps1_move_15left}\D{%Y/%m/%d %H:%M}${__my_ps1_restore_pos}
  1143. if $inzsh
  1144. then
  1145. PROMPT="\
  1146. ${__my_ps1_beg}[${__my_c2}%n@%M${__my_cdef}:${__my_c1}%~/${__my_cdef}]\$(__my_ps1_info1)
  1147. ${__my_ps1_beg}\$(__my_ps1_info2) %# "
  1148. RPROMPT="%D{%Y/%m/%d %H:%M}"
  1149. elif $inbash
  1150. then
  1151. PS1="\
  1152. ${__my_ps1_beg}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_info1)\n\
  1153. ${__my_ps1_beg}\D{%Y/%m/%d %H:%M} \$(__my_ps1_info2)${__my_ps1_right} \$ "
  1154. else
  1155. true
  1156. # PS1="$(printf $(whoami)@$(hostname)$ )"
  1157. fi
  1158. ###################################
  1159. # set header and titles
  1160. __my_set_header_line(){
  1161. # save current position
  1162. printf "\033[s"
  1163. # move to 0,0
  1164. printf "\033[0;0H"
  1165. # clear curent to eol
  1166. printf "\033[K"
  1167. # inverse color
  1168. printf "\033[7m"
  1169. printf "$1"
  1170. # restore color
  1171. printf "\033[0m"
  1172. # restore saved position
  1173. printf "\033[u"
  1174. }
  1175. __my_set_screen_name(){
  1176. # set window name
  1177. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  1178. then
  1179. echo -ne "\033k$1\033\\"
  1180. fi
  1181. }
  1182. __my_set_title(){
  1183. case $TERM in
  1184. (rxvt*|xterm*|aterm|screen*)
  1185. test -t 1 &&
  1186. test -z "$EMACS" &&
  1187. echo -n -e "\033]0;$1\007"
  1188. ;;
  1189. esac
  1190. }
  1191. if test -n "$TMUX"
  1192. then
  1193. # running tmux locally
  1194. __terminal_title="\$(basename \${PWD})"
  1195. elif test -n "$SSH_CONNECTION" && expr "$TERM" : '^screen' >/dev/null
  1196. then
  1197. # ssh connect from tmux terminal
  1198. __terminal_title="`whoami`@`hostname`:\$(basename \${PWD})"
  1199. else
  1200. __terminal_title="`whoami`@`hostname`:\${PWD}"
  1201. fi
  1202. if $inzsh
  1203. then
  1204. precmd(){
  1205. laststatus=$?
  1206. eval __my_set_title ${__terminal_title}
  1207. }
  1208. else
  1209. PROMPT_COMMAND="laststatus=\$?;__my_set_title \"${__terminal_title}\";$PROMPT_COMMAND"
  1210. fi
  1211. laststatus=0