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.
 
 
 
 
 
 

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