|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299 |
- #!/bin/sh
-
- # TODO: use tput
-
- ##########################################
- null(){
- "$@" >/dev/null 2>&1
- }
- __safe_run(){
- type $1 >/dev/null 2>&1 && "$@"
- }
-
- __match(){
- # __match str word
- # return 0 if word is found in str
- expr "$1" : ".*$2.*" >/dev/null
- }
-
-
- #################################
- # profile-like setups
- # aliases:
- # isinteractive: true if the current session is interactive
- # issourced: true if this file is sourced from another file (not so assured)
- # __firstload: true if this file is sourced for the first time (not so
- # assured)
-
- __safe_add_path_r(){
- test -d "$1" && PATH="${PATH}:$1"
- }
- __safe_add_path_l(){
- test -d "$1" && PATH="$1:${PATH}"
- }
-
- __safe_add_path_l "$HOME/.local/bin"
- __safe_add_path_l "$HOME/.local/lib/gems/bin"
- __safe_add_path_r "/c/mingw/bin"
- __safe_add_path_r "/c/mingw/msys/1.0/bin"
-
- # macports coreutils
- # isdarwin cannot be used it is not defined yet
- __safe_add_path_l "/opt/local/bin"
- __safe_add_path_l "/opt/local/sbin"
- __safe_add_path_l "/opt/local/libexec/gnubin"
- __safe_add_path_l \
- "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/bin"
-
- test -f "${__dotdir}/rc.py" && export PYTHONSTARTUP="${__dotdir}/rc.py"
-
- test -d "$HOME/.local/lib/python/site-packages" && \
- export PYTHONPATH="${PYTHONPATH}:${HOME}/.local/lib/python/site-packages"
-
- export GEM_HOME="$HOME/.local/lib/gems"
- export RUBYLIB="$RUBYLIB:$HOME/.local/lib/gems/lib"
-
-
- # it is not so good
- # http://archive.linux.or.jp/JF/JFdocs/Program-Library-HOWTO/shared-libraries.html
- # http://superuser.com/questions/324613/installing-a-library-locally-in-home-directory-but-program-doesnt-recognize-it
- # without this ENV i cannot run tmux. another way is to use --disable-shared
- # when building tmux
- if ! __match "$LD_LIBRARY_PATH" "$HOME/.local/lib"
- then
- export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$HOME/.local/lib"
- fi
-
- # in my environment powerdown does not work
- test -z "$SSH_CONNECTION" && \
- type setterm >/dev/null 2>&1 && \
- setterm -blank 30 -powersave on # -powerdown 10
-
-
- ##########################
- # system type
- # aliases:
- # ismsys, iscygwin iswindows, isdarwin, islinux,
- # with_coreutils, inbash, inzsh
-
- alias ismsys=false
- alias iscygwin=false
- alias iswindows=false
- alias isdarwin=false
- alias islinux=false
- alias with_coreutils=false # for mac
-
- case `uname` in
- (MINGW*) alias ismsys=true ;;
- (CYGWIN*) alias iscygwin=true ;;
- (Darwin*) alias isdarwin=true ;;
- (Linux*) alias islinux=true ;;
- esac
- null ls --version && alias with_coreutils=true
-
- ( ismsys || iscygwin ) && alias iswindows=true
-
- alias inbash=false
- alias inzsh=false
-
- if test -n "$BASH_VERSION"
- then
- alias inbash=true
- elif test -n "$ZSH_VERSION"
- then
- alias inzsh=true
- fi
-
- alias isinteractive=false
- __match "$-" i >/dev/null && alias isinteractive=true
- # alias issourced=true
- # expr "$0" : "^.*shrc$" >/dev/null && alias issourced=false # executed
-
-
- #################################
- # file pathes:
- # shrc: Path to this file
- # dotdir: Path to .dotfiles directory
-
-
- if inbash
- then
- __shrc="$BASH_SOURCE"
- elif inzsh
- then
- __shrc="$0"
- fi
-
- __dotdir="`dirname "$__shrc"`"
- __homelocal="$HOME/.local"
- __homevar="$HOME/.var"
- test -d "$__homelocal" || install -d "$__homelocal"
- test -d "$__homevar" || install -d "$__homevar"
-
-
- ##################################
- # EnvVal definitions
-
- export LANG=ja_JP.UTF-8
- export LC_MESSAGES=C
- export LC_TIME=C
-
- export TERMCAP="${TERMCAP}:vb="
- ismsys && export HOSTNAME
- # export ENV=~/.shrc
-
- if ! with_coreutils
- then
- export LSCOLORS=gxfxcxdxbxegedabagacad
- else
- # http://qiita.com/yuyuchu3333/items/84fa4e051c3325098be3
- null type dircolors && eval `dircolors`
- fi
-
- if false iswindows
- then
- export PAGER='tr -d \\r | less'
- else
- export PAGER="less"
- fi
- export LESS="-iRMX"
-
- # Style for lesspipe is defined in esc.style
- _src_hilite_lp_path="`which src-hilite-lesspipe.sh 2>/dev/null`"
- for f in /usr/share/source-highlight/src-hilite-lesspipe.sh
- do
- test -z "$_src_hilite_lp_path" && test -e "$f" && _src_hilite_lp_path="$f"
- done
- test -n "$_src_hilite_lp_path" && export LESSOPEN="| $_src_hilite_lp_path %s"
-
- if null type vim
- then
- export EDITOR=vim
- else
- export EDITOR=vi
- fi
- # export CDPATH=".:~"
- export VISUAL="$EDITOR"
-
- export GIT_PAGER="less -FS"
- export GIT_EDITOR="$EDITOR"
- export GIT_MERGE_AUTOEDIT=no
-
- if test -n "$TMUX" && \
- __match $TERM screen && \
- __match `tmux display -p '#{client_termname}'` 256color
- then
- TERM=screen-256color
- fi
-
- if test -z "$TMP"
- then
- if test -n "$TMPDIR"
- then
- export TMP=$TMPDIR
- elif test -n "$TEMP"
- then
- export TMP="$TEMP"
- else
- export TMP=/tmp
- fi
- fi
- __match "$TMP" "${USER}-tmp" >/dev/null || export TMP="${TMP}/${USER}-tmp"
- export TEMP="$TMP"
- test -d "$TMP" || install -d "$TMP"
-
- ! iswindows && null type stty && {
- stty stop undef # unbind C-s to stop displaying output
- # stty erase '^h'
- }
-
- if iswindows; then
- export USER=$USERNAME
- fi
-
- if test -d ~/dbx
- then
- export CHIT_PATH="$HOME/dbx/.chit"
- fi
-
- ##########################
- # Setups
-
- __download(){
- # download <url> <file>
- if type wget >/dev/null 2>&1
- then
- wget "$1" -O "$2"
- elif type curl >/dev/null 2>&1
- then
- curl --url "$1" --output "$2"
- fi
- }
-
- __mysetup_fetch_script(){
- url="$1"
- name="$2"
- type "$name" >/dev/null 2>&1 || {
- __download "$url" "$HOME/.local/bin/$name" &&
- chmod u+x "$HOME/.local/bin/$name"
- }
- }
-
- __mysetup_darwin_set_defaults(){
- isdarwin || return 1
-
- # http://appdrill.net/60641/mac-boot-mute.html
- #sudo nvram SystemAudioVolume=%80
-
- # add quit entry in menu
- defaults write com.apple.finder QuitMenuItem -bool YES
- # show full path on titlebar
- defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES
- # do not show desktop icons
- defaults write com.apple.finder CreateDesktop -boolean false
-
- killall Finder
-
- # disable dashboard
- #defaults write com.apple.dashboard mcx-disabled -bool YES
- }
-
- __mysetup_darwin_start_daemon(){
- isdarwin || return 1
-
- test "`launchctl getenv LC_ALL`" = C || sudo launchctl setenv LC_ALL C
- if ! (launchctl list | grep com.apple.locate) >/dev/null
- then
- sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
- fi
- }
-
- __mysetup_git_config(){
- if ! null type git
- then
- echo "git not found"
- return 1
- fi
-
- _gitconfig="git config --global"
-
- $_gitconfig user.name '10sr'
- $_gitconfig user.email '8slashes+git@gmail.com'
- $_gitconfig core.autocrlf false
- $_gitconfig core.excludesfile '~/.gitignore'
- $_gitconfig color.ui auto
- $_gitconfig status.relativePaths false
- $_gitconfig status.showUntrackedFiles normal
- $_gitconfig log.date iso
- null type xz && \
- $_gitconfig tar.txz.command "xz -c"
- $_gitconfig push.default current
-
- $_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"
- $_gitconfig alias.st "status -s -b"
- $_gitconfig alias.b "branch"
- $_gitconfig alias.sb "show-branch"
- $_gitconfig alias.ci "commit --verbose"
- $_gitconfig alias.co "checkout"
- $_gitconfig alias.cim "commit --verbose -m"
- $_gitconfig alias.di "diff --color"
- $_gitconfig alias.me "merge --no-ff --stat -v"
- $_gitconfig alias.gr "grep -n"
- $_gitconfig alias.ls "ls-files"
- # $_gitconfig alias.ls "ls-files -v --full-name"
- # $_gitconfig alias.ls "status -u -s ."
- $_gitconfig alias.sl "!sl"
- # $_gitconfig alias.my-ls "ls-files | xargs ls"
- # $_gitconfig alias.ll "!git ls-files | xargs ls -l -CFG --color=auto --time-style=long-iso"
- $_gitconfig alias.addi "add -i"
- $_gitconfig alias.clean-p "!test -z \"\$(git status -s -uno)\""
- $_gitconfig alias.newb "checkout -b"
- $_gitconfig alias.endb \
- "!sh -cx 'git stash && git checkout master && git merge --no-ff -'"
- #$_gitconfig alias.wc "!git ls-files -z | xargs -0 wc"
- # $_gitconfig push.default "simple"
- if iswindows; then
- $_gitconfig core.fileMode false
- fi
- }
-
- __mysetup_mkdirs(){
- install -d "$HOME/.local/bin"
- }
-
- __mysetup(){
- set -x
- __mysetup_mkdirs
- __mysetup_fetch_script \
- https://gist.github.com/10sr/6852317/raw/colortable16.sh colortable16.sh
- __mysetup_fetch_script \
- https://gist.github.com/10sr/6852331/raw/256colors2.pl 256colors2.pl
- if isdarwin
- then
- __mysetup_darwin_set_defaults
- __mysetup_darwin_start_daemon
- fi
- set +x
- }
-
- #######################
- if ! isinteractive
- then
- if test "$1" = setup
- then
- __mysetup
- exit 0
- fi
- # if this file is sourced return, if executed directly exit
- return 2>/dev/null || exit
- fi
-
- ######################
- # Print welcome messages
-
- iswindows && alias tty="echo cmd.exe"
- type fortune >/dev/null 2>&1 && {
- fortune
- echo
- fortune -o
- echo
- }
- uname -a
- echo TERM $TERM $(tput colors) colors connected to $(tty), \
- running $BASH $BASH_VERSION
- if test -n "$TMUX"
- then
- tmux display -p 'Using tmux #S:#I:#W.#P, client is #{client_termname}' \
- 2>/dev/null
- echo
- fi
-
- ###################################
- # some aliases and functions
-
- # __func_name: never used interactively
- # _func_name: usually not used interactively
-
- __safe_alias(){
- # __safe_alias <name>=<command>
- _bin=`expr "$1" : '^[^=]*=\([^ ]*\)'`
- test -n "$_bin" && \
- null type $_bin && \
- alias "$1"
- }
-
- ( ! with_coreutils && isdarwin ) || test "$TERM" = dumb || \
- _coloroption=" --color=auto"
- ( ! with_coreutils && isdarwin ) || iswindows || \
- _timeoption=" --time-style=long-iso"
- ( ! with_coreutils && isdarwin ) || _hideoption=" --hide=[A-Z]*" # do not use
-
- _timeformat_iso="%Y-%m-%dT%H:%M:%S%z"
- _timeformat_rfc2822="%a, %d %b %Y %T %z"
- _timeformat_num="%Y%m%d%H%M%S"
- alias datenum="date +$_timeformat_num"
-
- alias ls="ls -hCF${_coloroption}${_timeoption}"
- # export GREP_OPTIONS=""
- alias gr="grep -n --color=always"
- iswindows && alias grep="grep -n"
- # alias ll="ls -l"
- # alias la="ls -A"
- # alias lla="ls -Al"
- alias less="less -F"
- __safe_alias em="emacs -nw"
- __safe_alias vi=vim
- alias pstree="LANG=C pstree"
- alias cp="cp -v"
- alias mv="mv -v"
- alias rm="rm -v"
- alias psaux="ps auxww"
- alias q=exit
- __safe_alias e3=e3em
- #alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
- alias po=popd
- alias pu=pushd
- __safe_alias sudo="sudo " # use aliases through sudo
- __safe_alias sudoe="sudoedit"
- # __safe_alias halt="sudo halt"
- # __safe_alias reboot="sudo reboot"
- null type dbus-send && {
- alias suspend="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
- /org/freedesktop/UPower org.freedesktop.UPower.Suspend"
- alias hibernate="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
- /org/freedesktop/UPower org.freedesktop.UPower.Hibernate"
- }
- alias rand="echo \$RANDOM"
- __safe_alias xunp="file-roller -h"
- __safe_alias pc="sudo \paco -D"
- alias pycalc="python -i -c 'from math import *' "
- __safe_alias py3=python3
- __safe_alias py2=python2
- alias _reloadrc="exec \"$SHELL\""
- # alias mytime="date +%Y%m%d-%H%M%S"
- alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
- # type trash >/dev/null 2>&1 && alias rm=trash
- __safe_alias mpg123="mpg123 -C -v --title"
- __safe_alias xm="xmms2"
- #export PLAYER="mpg123 -C -v --title"
-
- __safe_alias screen="screen -e^z^z"
- #alias zcd="cd \`zenity --file-selection --directory\`"
- __safe_alias gtags="gtags --verbose"
- __safe_alias htags="htags --xhtml --symbol --line-number \
- --frame --alphabet --verbose"
- __safe_alias au=aunpack
- __safe_alias lv="lv|less"
- __safe_alias rs="rsync --progress --itemize-changes --compress"
- iscygwin && __safe_alias wget="wget --no-check-certificate"
-
- isdarwin && alias updatedb="LC_ALL=C updatedb"
- # do not use locate installed by macports
- isdarwin && test -x /usr/bin/locate && alias locate="/usr/bin/locate"
-
- # pad
- alias pad=notepad
- __safe_alias pad=gedit
- __safe_alias pad=leafpad
- isdarwin && alias pad="open -e"
-
- __safe_alias wic=wicd-curses
- __safe_alias wil="wicd-cli -y -l | head"
- #alias wicn="wicd-cli -y -c -n"
- wicn(){
- if test $# -eq 0
- then
- local num
- wicd-cli -y -l | head
- echo -n "input num: "
- read num
- test -n "$num" && wicd-cli -y -c -n $num
- else
- wicd-cli -y -c -n $1
- fi
- }
-
- __find_latest_vimdir(){
- vimdir=/usr/share/vim
- if test -d "$vimdir"
- then
- find "$vimdir" -name 'vim??' -type d | sort | tail -n 1
- else
- echo ""
- fi
- }
-
- for f in /usr/share/vim/vimcurrent "`__find_latest_vimdir`"
- do
- test -n "$f" || continue
- f="$f/macros/less.sh"
- test -f $f && alias vl=$f && break
- done
-
- alias pa=pacapt
-
- __safe_alias yt=yaourt
- __safe_alias cower="cower --color=auto"
- null type pacmatic && {
- alias pacman="pacmatic"
- export PACMAN="pacmatic"
- }
-
- __my_pacman_update_mirrorlist_with_reflector(){
- ml=/etc/pacman.d/mirrorlist
- cmd="$(expr "$(grep -m 1 reflector $ml)" : '# With: *\(.*\)')"
- if test -z "$cmd"
- then
- cmd="reflector --verbose -l 5 --sort rate --save $ml"
- fi
- echo "Running $cmd ..." 1>&2
- sudo $cmd
- }
- null type reflector && test -f /etc/pacman.d/mirrorlist && \
- alias reflect_mirrorlist=__my_pacman_update_mirrorlist_with_reflector
-
- null type apt-get && {
- alias aupgrade="sudo apt-get autoremove --yes && \
- sudo apt-get update --yes && sudo apt-get upgrade --yes"
- alias aptin="apt-get install"
- alias aptsearch="apt-cache search"
- alias aptshow="apt-cache show"
- }
-
- null type port && {
- alias port="port -v"
- alias pupgrade="sudo port -v selfupdate && \
- { sudo port -v upgrade outdated; }"
- }
-
- if iscygwin; then
- null type windate || \
- alias windate="cmd.exe //c 'echo %DATE%-%TIME%'"
- # alias cygsu="cygstart /cygwinsetup.exe"
- # alias ls="ls -CFG $(iswindows || test "$TERM" = dumb || echo --color=auto)"
- fi
-
- g(){
- if test $# -eq 0 && null type git-info
- then
- git info
- else
- git -c color.ui=always "$@"
- fi
- }
- if null type _git && inbash
- then
- # enable programmable completion for g
- complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
- || complete -o default -o nospace -F _git g
- fi
- git svn --help >/dev/null 2>&1 && alias gsvn="git svn"
-
- __safe_alias m=gitmemo
-
- alias setup.py3="sudo python3 setup.py install --record files.txt"
-
- randomstr(){
- len=$1
- test -z "$len" && len=8
- uuidgen | tr -d - | cut -c 1-len
- }
-
- datestr(){
- # datestr yyyyMMdd-hhmmss
- if test -z "$1" || test "$1" == "-h"
- then
- echo "datestr: usage: datestr <yyyyMMddhhmmss>"
- return 1
- fi
-
- dfmt= # actual format for date command
- while test -n "$1"
- do
- fmt="$1"
- while test -n "$fmt"
- do
- case "$fmt" in
- yyyy*) # year
- dfmt="${dfmt}%Y"
- fmt="`echo "$fmt" | cut -c 5-`"
- ;;
- yy*) # last two digits of year
- dfmt="${dfmt}%y"
- fmt="`echo "$fmt" | cut -c 3-`"
- ;;
- MM*) # month (01..12)
- dfmt="${dfmt}%m"
- fmt="`echo "$fmt" | cut -c 3-`"
- ;;
- dd*) # day of month (01..12)
- dfmt="${dfmt}%d"
- fmt="`echo "$fmt" | cut -c 3-`"
- ;;
- HH* | hh*) # hour (00..23)
- dfmt="${dfmt}%H"
- fmt="`echo "$fmt" | cut -c 3-`"
- ;;
- mm*) # minute (00..59)
- dfmt="${dfmt}%M"
- fmt="`echo "$fmt" | cut -c 3-`"
- ;;
- ss*) # second (00..60)
- dfmt="${dfmt}%S"
- fmt="`echo "$fmt" | cut -c 3-`"
- ;;
- *)
- char=`echo "$fmt" | cut -c 1`
- dfmt="${dfmt}${char}"
- fmt="`echo "$fmt" | cut -c 2-`"
- ;;
- esac
- done
- shift
- done
- date +"$dfmt"
- }
-
- ssh(){
- __my_set_screen_title ssh
- command ssh "$@"
- }
-
- __ssh_with_cd(){
- # __ssh_with_cd <host> <directory> [<arg> ...]
- if test -z "$2"
- then
- echo "usage: __ssh_with_cd <host> <directory> [<arg> ...]"
- return 1
- fi
- host="$1"
- shift
- dir="$1"
- shift
- ssh "$host" "$@" -t "cd \"$dir\"; \$SHELL -l"
- }
-
- memo(){
- if test -z "$1"
- then
- $EDITOR memo.txt
- else
- $EDITOR "$1/memo.txt"
- fi
- }
-
- now(){
- local tformat="%Y/%m/%d %H:%M:%S %z"
- cal
- REPLY=
- printf "\\r`date "+${tformat}"`"
- read -t 1
- while test $? -ne 0
- do
- printf "\\r`date "+${tformat}"`"
- read -t 1
- done
- }
-
- s(){
- if git rev-parse --git-dir >/dev/null 2>&1
- then
- echo ">> git grep -n $@" 1>&2
- git grep -n "$@"
- elif which ag >/dev/null 2>&1
- then
- echo ">> ag --pager=\"$PAGER\" $@" 1>&2
- ag --pager="$PAGER" "$@"
- elif which ack >/dev/null 2>&1
- then
- echo ">> ack --pager=\"$PAGER\" $@" 1>&2
- ack --pager="$PAGER" "$@"
- else
- echo \
- ">> find . " \
- "-path '*/.git' -prune -o" \
- "-path '*/.svn' -prune -o" \
- "-type f -exec grep -nH -e --color=always $@ {} +" 1>&2
- if test $# -eq 0
- then
- echo "No search word given." 1>&2
- return 1
- fi
- find . \
- -path '*/.git' -prune -o \
- -path '*/.svn' -prune -o \
- -type -f -exec grep -nH -e --color=always "$@" {} + \
- | $PAGER
- fi
- }
-
- man(){
- env \
- LESS_TERMCAP_mb=$(printf "\e[1;35m") \
- LESS_TERMCAP_md=$(printf "\e[1;31m") \
- LESS_TERMCAP_me=$(printf "\e[0m") \
- LESS_TERMCAP_se=$(printf "\e[0m") \
- LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
- LESS_TERMCAP_ue=$(printf "\e[0m") \
- LESS_TERMCAP_us=$(printf "\e[1;32m") \
- man "$@"
- }
-
- netwait(){
- while ! ping -c 1 -t 1 example.com
- do
- true
- done
- echo network works.
- }
-
- __realpath(){
- if type realpath >/dev/null 2>&1
- then
- command realpath "$@"
- else
- while ! test -d $1
- do
- shift
- done
- (command cd "$d" && echo "$PWD")
- # local d="$OLDPWD"
- # command cd "$1"
- # echo "$PWD"
- # command cd "$d"
- fi
- }
-
- tx(){
- if test $# -eq 0
- then
- echo ":: tx <session> to attach."
- tmux ls
- elif tmux has -t "$1"
- then
- tmux attach -t "$1"
- else
- tmux new -s "$1"
- fi
- }
-
- _tmux_prefs(){
- null type tmux || return 1
- tmux set -g mode-keys vi
- }
-
- dt(){
- # dt [<name>] [<command ...>]
- __dtach_dir="${TMP}/dtach"
- install -d "${__dtach_dir}"
-
- if test -n "${__MY_DTACH}"
- then
- echo "Current session: ${__MY_DTACH}"
- fi
-
- if test -z "$1"
- then
- echo "Sessions:"
- ls "${__dtach_dir}"
- return 0
- elif test "$1" = "-h"
- then
- echo "dt: usage: dt <name> [<command ...>]" 1>&2
- return 1
- fi
-
- soc_name="${__dtach_dir}/$1"
- shift
-
- if test -n "$__MY_DTACH"
- then
- echo "dtach session cannot be nested." 1>&2
- return 1
- elif test -S "$soc_name"
- then
- dtach -a "$soc_name" -e ^^
- elif test -e "$soc_name"
- then
- echo "dt: File named $soc_name already exists."
- return 1
- elif test -z "$1"
- then
- __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
- # echo "dt: Socket named $soc_name not exists and no command specified."
- # return 1
- else
- __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
- fi
- }
-
- scr(){
- test -n "$1" && pf="${1}-"
- local _tformat="%Y%m%d-%H%M%S%z"
- local _file="${HOME}/${pf}`date +${_tformat}`.script"
- __MY_SCRIPT=${_file} script ${_file} "$@"
- }
-
- dtscr(){
- # dtscr <command ...>
- if test -z "$1"
- then
- echo "dtscr: usage: dtscr <command ...>"
- return 1
- fi
-
- local _cmdstr="`echo $@ | tr ' ' +`"
- local _tformat="%Y%m%d-%H%M%S%z"
- local _name="${pf}`date +${_tformat}`-${_cmdstr}"
-
- local _scr_file="${HOME}/${_name}.script"
-
- local _dt_dir="${TMP}/dtscr"
- install -d "$_dt_dir"
-
- dtach -n "${_dt_dir}/${_name}" script "${_scr_file_}" "$@"
- # echo $_name
- # echo $_file
- }
-
- mcrypt_stream(){
- test $# -eq 2 || return 1
- case $1 in
- en)
- mcrypt --key $2 | base64 ;;
- de)
- base64 -d | mcrypt -d --key $2 ;;
- esac
- }
-
- gpg_stream(){
- test $# -eq 2 || return 1
- case $1 in
- en)
- gpg --passphrase $2 -c --batch |base64 ;;
- de)
- base64 -d|gpg --passphrase $2 -d --batch ;;
- esac
- }
- dgpg(){
- if test "$1" = help || test -z "$2"
- then
- echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
- return
- fi
- local srcs="$2"
- local dsts="$3"
- test -z "$dsts" && dsts="${srcs}.out"
- local pw
- echo -n "dgpg pw: "
- read -s pw
- echo ""
- test -z "$pw" && return 1
- for f in *${srcs}
- do
- local d="$(basename "$f" "${srcs}")${dsts}"
- echo -n "Processing $f to $d..."
- if test -d "$f"
- then
- echo "`printf 'failed (%s is directory)' $f`"
- elif test -f "$d"
- then
- echo "`printf 'failed (%s is already exists)' $d`"
- elif <"$f" gpg_stream $1 $pw >"$d" 2>/dev/null
- then
- echo "done"
- else
- echo "failed"
- test -f "$d" && rm "$d"
- fi
- done
- }
-
- alias enst="gpg_stream en"
- alias dest="gpg_stream de"
-
- showinfo(){
- echo "Japanese letters are 表示可能"
-
- __safe_run diskinfo
-
- ! isdarwin && test -n "${DISPLAY}" && {
- __safe_run xrandr | \grep --color=never ^Screen
- }
-
- iswindows || __safe_run finger $USER
- LANG=C __safe_runc id
- __safe_run xset q
- }
-
- x(){
- if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
- #mkdir -p ~/.var/log
- # nohup startx >~/.var/log/xorg.log 2>&1 &
- # exit
- exec startx
- else
- echo "X cant be started! Another X is already running?" 1>&2
- fi
- }
-
- bak(){
- for file in "$@"
- do
- cp -v ${file} ${file}.bak
- done
- }
-
- di(){
- if type colordiff >/dev/null 2>&1 && test $TERM != dumb
- then
- local diffcmd=colordiff
- else
- local diffcmd=diff
- fi
- ${diffcmd} -u "$@" | ${PAGER}
- }
-
- tb(){
- __datenum=`date +%Y%m%d-%H%M%S`
- __tb="$HOME/.var/tb/$__datenum"
- install -d "$__tb"
- for file in "$@"
- do
- mv -t "$__tb" "$file"
- done
- }
-
- mkcd(){
- if test -z "$1"
- then
- echo "mkcd: usage: mkcd <dir>"
- return 1
- elif test -d "$1"
- then
- echo "Dir \"$1\" already exists."
- else
- install -d "$1"
- echo "Dir \"$1\" created."
- fi
- cd "$1"
- }
-
- mkcdd(){
- # make and change date directory
- _d=`date +%Y%m%d-%H%M%S`
- mkcd "$_d"
- }
-
- if test -n "$TMUX" && null type reattach-to-user-namespace
- then
- alias pbpaste="reattach-to-user-namespace pbpaste"
- alias pbcopy="reattach-to-user-namespace pbcopy"
- fi
-
- catclip(){
- if iswindows
- then
- cat /dev/clipboard | tr -d \\r
- elif isdarwin
- then
- pbpaste
- else
- xclip -o -selection "clipboard"
- fi
- }
-
- setclip(){
- if test $# -eq 0
- then
- exec 3<&0
- else
- exec 3<<__EOF__
- `cat "$@"`
- __EOF__
- fi
- if iswindows
- then
- 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
- elif isdarwin
- then
- pbcopy 0<&3
- else
- 0<&3 xclip -i -f -selection "primary" | \
- xclip -i -f -selection "clipboard"
- fi
- exec 3<&-
- }
-
- open_file(){
- if iscygwin
- then
- cygstart "$@"
- elif ismsys
- then
- cmd.exe //c start "" "$@"
- elif isdarwin
- then
- touch "$@"
- open "$@"
- elif islinux
- then
- touch "$@"
- if null type pcmanfm; then
- LC_MESSAGES= pcmanfm "$@"
- else
- LC_MESSAGES= xdg-open "$@" &
- fi
- else
- cat "$@"
- fi
- }
-
- o(){
- if test $# -eq 0
- then
- open_file .
- else
- for f in "$@"
- do
- open_file "$(realpath "$f")"
- done
- fi
- }
-
- convmv_sjis2utf8_test(){
- convmv -r -f sjis -t utf8 *
- }
-
- convmv_sjis2utf8_notest(){
- convmv -r -f sjis -t utf8 * --notest
- }
-
-
- #################################################
- ## pastebin services
- ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
-
- sprunge(){
- # http://sprunge.us
- if test -z "$1"
- then
- curl -F 'sprunge=<-' http://sprunge.us
- else
- curl http://sprunge.us/$1
- fi
- }
-
- dpaste(){
- # http://dpaste.de
- if test -z "$1"
- then
- curl -F 'content=<-' https://dpaste.de/api/
- echo
- else
- curl https://dpaste.de/$1/raw/
- fi
- }
-
-
- ##########################
- # Zsh specific preferences
-
- if inzsh
- then
- bindkey -e
-
- # http://zsh.sourceforge.net/Guide/zshguide06.html#l147
- autoload compinit; compinit
- unsetopt auto_menu
- zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
- setopt bash_auto_list
-
- autoload colors; colors
-
- autoload -Uz promptinit
- promptinit
- prompt walters
- fi
-
- ######################################
- ## Prompt Settings
-
- __my_moc_state(){
- type mocp >/dev/null 2>&1 || return
- test "`mocp -Q %state 2>/dev/null`" = PLAY || return
- printf "$1" "`mocp -Q %title 2>/dev/null`"
- }
-
- __my_parse_svn_branch() {
- local LANG=C
- local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
- local svn_repository_root=$(svn info 2>/dev/null | \
- sed -ne 's#^Repository Root: ##p')
- echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | \
- awk '{print $1}'
- }
-
- __my_svn_ps1(){
- if svn status >/dev/null 2>&1
- then
- local svn_branch=$(__my_parse_svn_branch)
- test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
- fi
- }
-
- __my_battery_status(){
- local dir=/sys/class/power_supply/BAT0
- if test -d $dir && test -r $dir/status && test -r $dir/charge_full && \
- test -r $dir/charge_now
- then
- local st=$(cat $dir/status)
- local full=$(cat $dir/charge_full)
- local now=$(cat $dir/charge_now)
- local rate=$(expr $now \* 100 / $full)
- printf "$1" "${st}:${rate}%"
- fi
- }
- alias bat='__my_battery_status %s\\n'
-
- ipaddress(){
- type ip >/dev/null 2>&1 || return 1
- local ip=$(LANG=C ip addr show scope global | \
- \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
- test -n "$ip" && printf $1 $ip
- }
-
- __my_ps1_str=""
-
- test -n "$__MY_SCRIPT" && __my_ps1_str="${__my_ps1_str}${__my_c5}SCR${__my_cdef} "
-
- test -n "$SSH_CONNECTION" && __my_ps1_str="${__my_ps1_str}${__my_c5}SSH${__my_cdef} "
-
- test -n "$__MY_DTACH" && __my_ps1_str="${__my_ps1_str}${__my_c5}DTACH${__my_cdef} "
-
- __my_ps1_scale(){
- if null type stty && ! ismsys
- then
- stty size | tr -d $'\n' | tr " " x
- printf " "
- fi
- }
-
- __my_ps1_tmux(){
- null type tmux || return $last
- local tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
- test -n "$TMUX" && echo "[TMUX:$tmuxc]"
- }
-
- __my_ps1_moc(){
- __my_moc_state "[MOC:%s]"
- }
-
- for f in /usr/share/git/git-prompt.sh \
- /opt/local/share/git-core/git-prompt.sh \
- /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
- do
- test -r "$f" && inbash && . "$f" && break
- done
- GIT_PS1_SHOWDIRTYSTATE=t
- GIT_PS1_SHOWUPSTREAM=t
- __my_ps1_git(){
- null type __git_ps1 || return $last
- null git rev-parse --git-dir >/dev/null 2>&1 || return $last
- __git_ps1 "[GIT:$(__safe_run git config --get user.name):%s]"
- }
-
- __my_ps1_ipaddr(){
- ! iswindows && ipaddress [Addr:%s]
- }
-
- __my_ps1_bttry(){
- local bst="${TMP}/batterystatus"
- if test -z "$DISPLAY" && ! iswindows
- then
- test -f $bst && local bstr="$(cat $bst)"
- test -n "$bstr" && ! echo $bstr | grep 100 >/dev/null 2>&1 && \
- echo "[Battery:$bstr]"
- __my_battery_status %s >$bst &
- fi
- }
-
- __my_ps1_dirs(){
- dirs | wc -l
- }
-
- __my_ps1_jobs(){
- jobs | wc -l
- }
-
- __my_alert_fail(){
- test $laststatus -eq 0 || echo '!!! '
- }
-
- # About ansi escape sequences
- # http://archive.linux.or.jp/JF/JFdocs/Bash-Prompt-HOWTO-5.html
- # http://www.grapecity.com/japan/powernews/column/clang/047/page02.htm
-
- __attr_beg='\[\033['
- __attr_end='m\]'
-
- __color_default="${__attr_beg}0${__attr_end}"
-
- __color_black="${__attr_beg}0;30${__attr_end}"
- __color_red="${__attr_beg}0;31${__attr_end}"
- __color_green="${__attr_beg}0;32${__attr_end}"
- __color_brown="${__attr_beg}0;33${__attr_end}"
- __color_blue="${__attr_beg}0;34${__attr_end}"
- __color_purple="${__attr_beg}0;35${__attr_end}"
- __color_cyan="${__attr_beg}0;36${__attr_end}"
- __color_light_gray="${__attr_beg}0;37${__attr_end}"
-
- __color_dark_gray="${__attr_beg}1;30${__attr_end}"
- __color_light_red="${__attr_beg}1;31${__attr_end}"
- __color_light_green="${__attr_beg}1;32${__attr_end}"
- __color_yellow="${__attr_beg}1;33${__attr_end}"
- __color_light_blue="${__attr_beg}1;34${__attr_end}"
- __color_light_purple="${__attr_beg}1;35${__attr_end}"
- __color_light_cyan="${__attr_beg}1;36${__attr_end}"
- __color_white="${__attr_beg}1;37${__attr_end}"
-
- __color_bg_black="${__attr_beg}40${__attr_end}"
- __color_bg_red="${__attr_beg}41${__attr_end}"
- __color_bg_green="${__attr_beg}42${__attr_end}"
- __color_bg_brown="${__attr_beg}43${__attr_end}"
- __color_bg_blue="${__attr_beg}44${__attr_end}"
- __color_bg_purple="${__attr_beg}45${__attr_end}"
- __color_bg_cyan="${__attr_beg}46${__attr_end}"
- __color_bg_light_gray="${__attr_beg}47${__attr_end}"
-
- __attr_underline="${__attr_beg}4${__attr_end}"
- __attr_reverse="${__attr_beg}7${__attr_end}"
- __attr_bold="${__attr_beg}1${__attr_end}"
-
- # NOTE: tput is another easy way to set colors and background
- # For example, "$(tput setab 4)text$(tput sgr0)" print text with background
- # color blue.
-
- if test "$TERM" != dumb
- then
- __my_c1="$__attr_bold$__attr_underline" # color for PWD
- __my_c2="$__attr_bold$__attr_underline" # color for user and hostname
- # color for ::
- case "`hostname`" in
- arch-aspireone)
- __my_c4="$__color_light_blue"
- ;;
- darwin-mba.local)
- __my_c4="$__color_light_cyan"
- ;;
- newkiwi)
- __my_c4="$__color_light_purple"
- ;;
- *)
- __my_c4="$__color_light_green"
- ;;
- esac
- __my_c5="$__color_black$__color_bg_light_gray" # color for SCR
- __my_cdef="$__color_default"
- fi
-
- _ps1_bash="\
- ${__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\
- ${__my_c4}:: ${__my_cdef}l${SHLVL}n\#j\js\$laststatus $(__my_ps1_scale)\D{%T} ${__my_ps1_str}\$(__my_alert_fail)\$ "
- inbash && PS1=$_ps1_bash
- _ps1_zsh="$_ps1_bash"
- #inzsh && PS1="$_ps1_zsh"
-
- __my_set_header_line(){
- # save current position
- printf "\033[s"
- printf "\033[0;0H"
- printf "\033[K"
- printf "\033[7m"
- printf "$1"
- printf "\033[0m"
- # restore saved position
- printf "\033[u"
- }
-
- __my_set_screen_title(){
- if test -n "$TMUX" && test -z "$INSIDE_EMACS"
- then
- echo -ne "\033k$1\033\\"
- fi
- }
-
- __my_set_title(){
- case $TERM in
- (rxvt*|xterm*|aterm|screen*)
- test -t 1 &&
- test -n "$DISPLAY" &&
- test -z "$EMACS" &&
- echo -n -e "\033]0;$1\007"
- ;;
- esac
- }
- PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\:\${PWD};
- __my_set_screen_title \$(basename \"\$PWD\")/"
- PROMPT_COMMAND="laststatus=\$?;$PROMPT_COMMAND"
- laststatus=0
|