Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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