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.
 
 
 
 
 
 

1365 lignes
32 KiB

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