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.
 
 
 
 
 
 

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