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

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