Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

1362 righe
32 KiB

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