]> gitweb @ CieloNegro.org - sugar.git/blobdiff - dot-files/_zshrc
Auto commit by The Sugar System.
[sugar.git] / dot-files / _zshrc
index 8b95b52ba3bf87513e9ed35f14a15c5b3937c082..b995aef50d94b638569acf70d52bd9daff603982 100644 (file)
@@ -4,10 +4,17 @@ bindkey -e
 stty -ixon ixany
 stty erase '^H'
 
+# The following lines were added by compinstall
+zstyle :compinstall filename "$HOME/.zshrc"
+
+autoload -U compinit
+compinit
+# End of lines added by compinstall
+
 zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z} r:|[/]=* r:|=*'
 zstyle ':completion:*' use-cache true
 
-if [ -d "$HOME/.zfunc" ]; then
+if [[ -d "$HOME/.zfunc" ]]; then
     fpath=($HOME/.zfunc $fpath)
     autoload -U ~/.zfunc/*(:t)
     function r() {
@@ -18,7 +25,7 @@ if [ -d "$HOME/.zfunc" ]; then
     }
 fi
 
-if [ -f "$HOME/.ssh/known_hosts" ]; then
+if [[ -f "$HOME/.ssh/known_hosts" ]]; then
     _cache_hosts=(`perl -ne  'if (/^([a-zA-Z0-9.-]+)/) { print "$1\n";}' ~/.ssh/known_hosts`)
 fi
 
@@ -53,77 +60,288 @@ setopt inc_append_history
 setopt share_history
 
 function _we_are_in_gnu_screen () {
-    [ "$WINDOW" != "" ]
+    [[ -n "$WINDOW" ]]
+}
+
+function _we_are_in_ssh_session () {
+    [[ -n "$SSH_CONNECTION" ]]
+}
+
+function _we_are_in_emacs () {
+    [[ -n "$INSIDE_EMACS" ]]
 }
 
 _prompt_base='%B[%n@%m] %{%(?.$fg[green].$fg[red])%}%#%{$reset_color%}%b '
 if ( _we_are_in_gnu_screen ); then
-    # Magic escape sequence for GNU Screen to update their titles
-    # dynamically by grabbing prompt line.
-    PROMPT=$'%{\ek\e\\%}'$_prompt_base
+    # GNU Screen has a capability to set its window title by "ESC k
+    # {string} ESC \". We want to reset the title to `_' to show we
+    # aren't running any command now.
+    PROMPT=$'%{\ek_\e\\%}'$_prompt_base
 else
     PROMPT=$_prompt_base
 fi
 
-RPROMPT='%U%D{%m/%d}%u %U%D{%T}%u %U%~%u'
+autoload -U add-zsh-hook
+
+function _gnu_screen_preexec() {
+    if { _we_are_in_gnu_screen } then
+        # Set the window title of GNU Screen.
+        local cmd=${1[(wr)^(*=*|sudo|nice|env|time|ssh|-*|[0-9]*)]}
+        printf "\ek$cmd\e\\"
+    fi
+}
+add-zsh-hook preexec _gnu_screen_preexec
+
+function _dirinfo_precmd() {
+    local dirinfo="$(print_directory_info $(pwd))"
+
+    case $dirinfo in
+        "")
+            RPROMPT='%U%~%u';;
+        *)
+            RPROMPT="%U%~%u %U($dirinfo)%u";;
+    esac
+}
+add-zsh-hook precmd _dirinfo_precmd
+
+function print_directory_info() {
+    local absdir=$1
+
+    if [[ -r $absdir/.git ]] then
+        # Git
+        git branch --no-color 2>/dev/null \
+            | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
+
+    elif [[ -r $absdir/.hg/branch ]] then
+        # Hg
+        local hgbranch=`cat $absdir/.hg/branch`
+        if [[ -e $absdir/.hg/unpushed ]] then
+            echo "*${hgbranch}*"
+        else
+            echo "$hgbranch"
+        fi
+
+    elif [[ -d $absdir/_darcs ]] then
+        # Darcs
+        basename $absdir
+
+    elif [[ -r $absdir/CVS/Repository ]] then
+        # CVS
+        cat $absdir/CVS/Repository \
+            | sed -e 's!\([^/]*\).*!\1!'
+
+    else
+        # Unknown.
+        if [[ $absdir = "/" ]] then
+            # This is the root directory so exit from the recursion.
+            echo
+        else
+            # Recurse to the parent dir.
+            print_directory_info $(dirname $absdir)
+        fi
+    fi
+}
+
+function nfgrep() {
+    fgrep --with-filename --line-number --context=1 --colour=always "$@" | $PAGER
+}
+
+function ppgrep() {
+    local -a percol_opts
+
+    if [[ $# -gt 0 ]]; then
+        percol_opts=("--query" "$1")
+    fi
+
+    ps aux | percol "${percol_opts[@]}" | awk '{ print $2 }'
+}
+
+function ppkill() {
+    local -a ppgrep_opts
+
+    if [[ $1 =~ "^[^-]" ]]; then
+        ppgrep_opts=("$1")
+        shift
+    fi
+
+    ppgrep "${ppgrep_opts[@]}" | xargs kill "$@"
+}
+
+function find_tac_command() {
+    if which gtac >&/dev/null; then
+        echo "gtac"
+    elif which tac >&/dev/null; then
+        echo "tac"
+    else
+        echo "tail -r"
+    fi
+}
+
+function percol_select_history() {
+    local -a tac_cmd
+    tac_cmd=($(find_tac_command))
+
+    local selected
+    selected=$("${tac_cmd[@]}" "$HISTFILE" | \
+        sed 's/^: [0-9]*:[0-9]*;//' | \
+        awk 'seen[$0] {next} {seen[$0]++; print}' | \
+        percol --match-method regex --query "$LBUFFER")
+
+    if [[ $? -eq 0 ]]; then
+        BUFFER="$selected"
+        CURSOR="$#BUFFER" # move cursor to the end of line
+        zle redisplay
+        #zle accept-line  # uncomment this to execute the selected one immediately.
+    else
+        zle redisplay
+    fi
+}
+if which percol >&/dev/null; then
+    zle -N percol_select_history
+    bindkey '^R' percol_select_history
+fi
 
 HISTFILE="$HOME/.zhistory"
 HISTSIZE=6000000
 SAVEHIST=6000000
 
+# Select directories with z(1)
+if [[ -r "$HOME/sandbox/_misc/z/z.sh" ]]; then
+    source "$HOME/sandbox/_misc/z/z.sh"
+
+    function percol_select_directory() {
+        local -a tac_cmd
+        tac_cmd=($(find_tac_command))
+
+        local selected
+        selected=$(_z -r 2>&1 | \
+            "${tac_cmd[@]}" | \
+            percol --query "$LBUFFER" | \
+            sed -r 's/[^[:space:]]+[[:space:]]+//')
+
+        if [[ $? -eq 0 ]]; then
+            cd "${selected}"
+            selected="" # Without this, zsh treats this variable as a
+                        # candidate for a "named directory".
+        fi
+
+        zle reset-prompt
+        return 0
+    }
+
+    if which percol >&/dev/null; then
+        zle -N percol_select_directory
+        bindkey '\C-xd' percol_select_directory
+    fi
+fi
+
+function lv() {
+    local -a lv_cmd
+
+    if whence -p lv >/dev/null; then
+        lv_cmd=("lv")
+    elif which less >&/dev/null; then
+        lv_cmd=("less")
+    elif which more >&/dev/null; then
+        lv_cmd=("more")
+    else
+        echo >&2 "$0: no pager commands are found"
+        return 1
+    fi
+
+    command "${lv_cmd[@]}" "$@"
+}
+
 alias -g L="| $PAGER"
 alias -g G="| grep"
 alias -g H="| head"
 alias -g T="| tail"
+alias -g X="| hexdump -C"
 
 alias ..='cd ..'
 alias cd..='cd ..'
-alias ls='ls -F'
-alias la='ls -a'
-alias l='ls -l'
-alias lls='ls -la'
 alias p='pushd'
 alias o='popd'
 alias d='dirs'
 alias df='df -h'
-alias ed='ed -p "ed> "'
 alias du='du -k'
-alias nc='nc -vv'
+alias ed='ed -p "ed> "'
+alias man="LANG=C man"
 alias mwget="wget --user-agent='Mozilla/1.4b'"
+alias nc='nc -vv'
+alias pkgsrc="pushd /usr/pkgsrc"
 alias pstow="pushd /usr/local/stow"
 alias w3m="w3m -O UTF-8"
 alias w3mb="w3m -O UTF-8 -B"
 
+alias aria-emacs="ssh -f -X admin@aria.cielonegro.org emacsclient -c"
+alias pho-dev-boinc="ssh -f -X pho@pho.dev.office.ymir.co.jp env LANG=C /home/pho/var/BOINC/run_manager"
+
 if [ $(hostname) = "aria.cielonegro.org" ]; then
-    alias safari="open -a Safari"
     alias unstuff='open -a StuffIt\ Expander.app'
     alias pa='open -a NiseRingo.app'
     alias heboris='open /Applications/HeborisUEMac/exe/Heboris\ OpenGL.app'
     alias hengband='open ~/Applications/hengband*/Hengband*'
 fi
 
-if [ $(uname) = "Darwin" ]; then
+if [[ $(uname) = "Darwin" ]]; then
+    alias safari="open -a Safari"
+
     alias ldd="otool -L"
+
+    alias ls='ls -Fw'
+    alias la='ls -aw'
+    alias l='ls -lhw'
+    alias lls='ls -lahw'
+
+    #alias top='/usr/bin/top -X -o cpu'
+    alias top='/usr/bin/top -o cpu'
+else
+    alias ls='ls -F'
+    alias la='ls -a'
+    alias l='ls -lh'
+    alias lls='ls -lah'
 fi
 
-# The following lines were added by compinstall
+function osname () {
+    case $(uname) in
+        "Darwin" | "FreeBSD" | "NetBSD")
+            uname -v;;
+        "Linux")
+            if [[ -f "/etc/redhat-release" ]]; then
+                cat "/etc/redhat-release"
+            elif [[ -f "/etc/SuSE-release" ]]; then
+                cat "/etc/SuSE-release"
+            else
+                echo -n "$0: I know this is "$(uname -o)" but " >&2
+                echo    "have no idea about its distribution."  >&2
+                return 1
+            fi;;
+        *)
+            echo "$0: Failed to detect the OS name." >&2
+            return 1
+    esac
+}
 
-zstyle :compinstall filename "$HOME/.zshrc"
+if _we_are_in_gnu_screen; then
+    # When we are in a GNU Screen session...
 
-autoload -U compinit
-compinit
-# End of lines added by compinstall
+    if _we_are_in_ssh_session; then
+        # Propagate remote session's bells to the local one.
+        screen -X vbell off
+    fi
+else
+    if _we_are_in_emacs; then
+        # Without this, Emacs' term mode has trouble with backspaces.
+        export TERM=vt100
+    fi
 
-if [ "x$WINDOW" = "x" ]; then
-    if which fortune 2>&1 >/dev/null; then
+    # Run "fortune" if we aren't in a GNU Screen session.
+    if which fortune >/dev/null 2>&1; then
         fortune
     fi
 fi
 
-if [ -e ".motd" ]; then
-    cat .motd
-fi
-
-if which ssh-agent-manager 2>&1 >/dev/null; then
+if which ssh-agent-manager >/dev/null 2>&1; then
     eval `ssh-agent-manager -s`
 fi