Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

1298 wiersze
31 KiB

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