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

1340 строки
31 KiB

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