Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

1381 Zeilen
32 KiB

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