您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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