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.
 
 
 
 
 
 

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