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

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