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

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