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.
 
 
 
 
 
 

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