You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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