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.
 
 
 
 
 
 

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