Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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