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ů.
 
 
 
 
 
 

1365 řádky
32 KiB

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