]> gitweb @ CieloNegro.org - sugar.git/blob - dot-files/_zshrc
Auto commit by The Sugar System.
[sugar.git] / dot-files / _zshrc
1 #!/bin/zsh
2 bindkey -e
3
4 stty -ixon ixany
5 stty erase '^H'
6
7 zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z} r:|[/]=* r:|=*'
8 zstyle ':completion:*' use-cache true
9
10 if [ -d "$HOME/.zfunc" ]; then
11     fpath=($HOME/.zfunc $fpath)
12     autoload -U ~/.zfunc/*(:t)
13     function r() {
14         local f
15         f=(~/.zfunc/*(.))
16         unfunction $f:t 2> /dev/null
17         autoload -U $f:t
18     }
19 fi
20
21 if [ -f "$HOME/.ssh/known_hosts" ]; then
22     _cache_hosts=(`perl -ne  'if (/^([a-zA-Z0-9.-]+)/) { print "$1\n";}' ~/.ssh/known_hosts`)
23 fi
24
25 autoload -U colors
26 colors
27
28 setopt no_beep
29 setopt auto_cd
30 setopt auto_list
31 setopt auto_menu
32 setopt auto_name_dirs
33 setopt auto_param_keys
34 setopt auto_param_slash
35 setopt auto_remove_slash
36 setopt c_bases
37 setopt chase_links
38 setopt complete_aliases
39 setopt correct
40 setopt equals
41 setopt glob_complete
42 setopt prompt_bang
43 setopt prompt_percent
44 setopt prompt_subst
45 setopt pushd_ignore_dups
46 setopt pushd_to_home
47 setopt sh_file_expansion
48 setopt always_last_prompt
49 setopt extended_glob
50 setopt hist_ignore_all_dups
51 setopt hist_ignore_space
52 setopt inc_append_history
53 setopt share_history
54
55 function _we_are_in_gnu_screen () {
56     [[ -n "$WINDOW" ]]
57 }
58
59 function _we_are_in_ssh_session () {
60     [[ -n "$SSH_CONNECTION" ]]
61 }
62
63 _prompt_base='%B[%n@%m] %{%(?.$fg[green].$fg[red])%}%#%{$reset_color%}%b '
64 if ( _we_are_in_gnu_screen ); then
65     # GNU Screen has a capability to set its window title by "ESC k
66     # {string} ESC \". We want to reset the title to `_' to show we
67     # aren't running any command now.
68     PROMPT=$'%{\ek_\e\\%}'$_prompt_base
69 else
70     PROMPT=$_prompt_base
71 fi
72
73 function preexec() { # Hook
74     if { _we_are_in_gnu_screen } then
75         # Set the window title of GNU Screen.
76         local cmd=${1[(wr)^(*=*|sudo|nice|env|time|ssh|-*|[0-9]*)]}
77         printf "\ek$cmd\e\\"
78     fi
79 }
80
81 function precmd() { # Hook
82     local dirinfo="$(print_directory_info $(pwd))"
83
84     case $dirinfo in
85         "")
86             RPROMPT='%U%~%u';;
87         *)
88             RPROMPT="%U%~%u %U($dirinfo)%u";;
89     esac
90 }
91
92 function print_directory_info() {
93     local absdir=$1
94
95     if [[ -r $absdir/.git ]] then
96         # Git
97         git branch --no-color 2>/dev/null \
98             | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
99
100     elif [[ -r $absdir/.hg/branch ]] then
101         # Hg
102         local hgbranch=`cat $absdir/.hg/branch`
103         if [[ -e $absdir/.hg/unpushed ]] then
104             echo "*${hgbranch}*"
105         else
106             echo "$hgbranch"
107         fi
108
109     elif [[ -d $absdir/_darcs ]] then
110         # Darcs
111         basename $absdir
112
113     elif [[ -r $absdir/CVS/Repository ]] then
114         # CVS
115         cat $absdir/CVS/Repository \
116             | sed -e 's!\([^/]*\).*!\1!'
117
118     else
119         # Unknown.
120         if [[ $absdir = "/" ]] then
121             # This is the root directory so exit from the recursion.
122             echo
123         else
124             # Recurse to the parent dir.
125             print_directory_info $(dirname $absdir)
126         fi
127     fi
128 }
129
130 function nfgrep() {
131     fgrep --with-filename --line-number --context=1 --colour=always "$@" | $PAGER
132 }
133
134 function ppgrep() {
135     local -a percol_opts
136
137     if [[ $# -gt 0 ]]; then
138         percol_opts=("--query" "$1")
139     fi
140
141     ps aux | percol "${percol_opts[@]}" | awk '{ print $2 }'
142 }
143
144 function ppkill() {
145     local -a ppgrep_opts
146
147     if [[ $1 =~ "^[^-]" ]]; then
148         ppgrep_opts=("$1")
149         shift
150     fi
151
152     ppgrep "${ppgrep_opts[@]}" | xargs kill "$@"
153 }
154
155 function percol_select_history() {
156     local -a tac_cmd
157
158     if which gtac >&/dev/null; then
159         tac_cmd=("gtac")
160     elif which tac >&/dev/null; then
161         tac_cmd=("tac")
162     else
163         tac_cmd=("cat")
164     fi
165
166     local selected
167     selected=$("${tac_cmd[@]}" "$HISTFILE" | \
168         sed 's/^: [0-9]*:[0-9]*;//' | \
169         awk 'seen[$0] {next} {seen[$0]++; print}' | \
170         percol --match-method regex --query "$LBUFFER")
171     if [[ $? -eq 0 ]]; then
172         BUFFER="$selected"
173         CURSOR="$#BUFFER" # move cursor to the end of line
174         zle redisplay
175         #zle accept-line  # uncomment this to execute the selected one immediately.
176     else
177         zle redisplay
178     fi
179 }
180 if which percol >&/dev/null; then
181     zle -N percol_select_history
182     bindkey '^R' percol_select_history
183 fi
184
185 HISTFILE="$HOME/.zhistory"
186 HISTSIZE=6000000
187 SAVEHIST=6000000
188
189 alias -g L="| $PAGER"
190 alias -g G="| grep"
191 alias -g H="| head"
192 alias -g T="| tail"
193 alias -g X="| hexdump -C"
194
195 alias ..='cd ..'
196 alias cd..='cd ..'
197 alias p='pushd'
198 alias o='popd'
199 alias d='dirs'
200 alias df='df -h'
201 alias du='du -k'
202 alias ed='ed -p "ed> "'
203 alias man="LANG=C man"
204 alias mwget="wget --user-agent='Mozilla/1.4b'"
205 alias nc='nc -vv'
206 alias pkgsrc="pushd /usr/pkgsrc"
207 alias pstow="pushd /usr/local/stow"
208 alias w3m="w3m -O UTF-8"
209 alias w3mb="w3m -O UTF-8 -B"
210
211 alias aria-emacs="ssh -f -X admin@aria.cielonegro.org emacsclient -c"
212 alias pho-dev-boinc="ssh -f -X pho@pho.dev.office.ymir.co.jp env LANG=C /home/pho/var/BOINC/run_manager"
213
214 if [ $(hostname) = "aria.cielonegro.org" ]; then
215     alias safari="open -a Safari"
216     alias unstuff='open -a StuffIt\ Expander.app'
217     alias pa='open -a NiseRingo.app'
218     alias heboris='open /Applications/HeborisUEMac/exe/Heboris\ OpenGL.app'
219     alias hengband='open ~/Applications/hengband*/Hengband*'
220     alias syssleep='sudo shutdown -s now'
221 fi
222
223 if [[ $(uname) = "Darwin" ]]; then
224     alias ldd="otool -L"
225
226     alias ls='ls -Fw'
227     alias la='ls -aw'
228     alias l='ls -lw'
229     alias lls='ls -law'
230
231     alias top='/usr/bin/top -X -o cpu'
232 else
233     alias ls='ls -F'
234     alias la='ls -a'
235     alias l='ls -l'
236     alias lls='ls -la'
237 fi
238
239 function osname () {
240     case $(uname) in
241         "Darwin" | "FreeBSD" | "NetBSD")
242             uname -v;;
243         "Linux")
244             if [[ -f "/etc/redhat-release" ]]; then
245                 cat "/etc/redhat-release"
246             elif [[ -f "/etc/SuSE-release" ]]; then
247                 cat "/etc/SuSE-release"
248             else
249                 echo -n "$0: I know this is "$(uname -o)" but " >&2
250                 echo    "have no idea about its distribution."  >&2
251                 return 1
252             fi;;
253         *)
254             echo "$0: Failed to detect the OS name." >&2
255             return 1
256     esac
257 }
258
259 # The following lines were added by compinstall
260
261 zstyle :compinstall filename "$HOME/.zshrc"
262
263 autoload -U compinit
264 compinit
265 # End of lines added by compinstall
266
267 if ( _we_are_in_gnu_screen ); then
268     # When we are in a GNU Screen session...
269
270     if ( _we_are_in_ssh_session ); then
271         # Propagate remote session's bells to the local one.
272         screen -X vbell off
273     fi
274 else
275     if [ -e ".motd" ]; then
276         cat ".motd"
277     fi
278
279     # Run "fortune" if we aren't in a GNU Screen session.
280     if which fortune 2>&1 >/dev/null; then
281         fortune
282     fi
283 fi
284
285 if which ssh-agent-manager 2>&1 >/dev/null; then
286     eval `ssh-agent-manager -s`
287 fi