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.
 
 
 
 
 
 

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