Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

1300 lignes
31 KiB

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