]> gitweb @ CieloNegro.org - sugar.git/blob - dot-files/_zshrc
45ac9aff1c98f72aa3f9c0b4a89faf46d19bcc18
[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 function _we_are_in_emacs () {
64     [[ -n "$INSIDE_EMACS" ]]
65 }
66
67 _prompt_base='%B[%n@%m] %{%(?.$fg[green].$fg[red])%}%#%{$reset_color%}%b '
68 if ( _we_are_in_gnu_screen ); then
69     # GNU Screen has a capability to set its window title by "ESC k
70     # {string} ESC \". We want to reset the title to `_' to show we
71     # aren't running any command now.
72     PROMPT=$'%{\ek_\e\\%}'$_prompt_base
73 else
74     PROMPT=$_prompt_base
75 fi
76
77 function preexec() { # Hook
78     if { _we_are_in_gnu_screen } then
79         # Set the window title of GNU Screen.
80         local cmd=${1[(wr)^(*=*|sudo|nice|env|time|ssh|-*|[0-9]*)]}
81         printf "\ek$cmd\e\\"
82     fi
83 }
84
85 function precmd() { # Hook
86     local dirinfo="$(print_directory_info $(pwd))"
87
88     case $dirinfo in
89         "")
90             RPROMPT='%U%~%u';;
91         *)
92             RPROMPT="%U%~%u %U($dirinfo)%u";;
93     esac
94 }
95
96 function print_directory_info() {
97     local absdir=$1
98
99     if [[ -r $absdir/.git ]] then
100         # Git
101         git branch --no-color 2>/dev/null \
102             | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
103
104     elif [[ -r $absdir/.hg/branch ]] then
105         # Hg
106         local hgbranch=`cat $absdir/.hg/branch`
107         if [[ -e $absdir/.hg/unpushed ]] then
108             echo "*${hgbranch}*"
109         else
110             echo "$hgbranch"
111         fi
112
113     elif [[ -d $absdir/_darcs ]] then
114         # Darcs
115         basename $absdir
116
117     elif [[ -r $absdir/CVS/Repository ]] then
118         # CVS
119         cat $absdir/CVS/Repository \
120             | sed -e 's!\([^/]*\).*!\1!'
121
122     else
123         # Unknown.
124         if [[ $absdir = "/" ]] then
125             # This is the root directory so exit from the recursion.
126             echo
127         else
128             # Recurse to the parent dir.
129             print_directory_info $(dirname $absdir)
130         fi
131     fi
132 }
133
134 function nfgrep() {
135     fgrep --with-filename --line-number --context=1 --colour=always "$@" | $PAGER
136 }
137
138 function ppgrep() {
139     local -a percol_opts
140
141     if [[ $# -gt 0 ]]; then
142         percol_opts=("--query" "$1")
143     fi
144
145     ps aux | percol "${percol_opts[@]}" | awk '{ print $2 }'
146 }
147
148 function ppkill() {
149     local -a ppgrep_opts
150
151     if [[ $1 =~ "^[^-]" ]]; then
152         ppgrep_opts=("$1")
153         shift
154     fi
155
156     ppgrep "${ppgrep_opts[@]}" | xargs kill "$@"
157 }
158
159 function percol_select_history() {
160     local -a tac_cmd
161
162     if which gtac >&/dev/null; then
163         tac_cmd=("gtac")
164     elif which tac >&/dev/null; then
165         tac_cmd=("tac")
166     else
167         tac_cmd=("cat")
168     fi
169
170     local selected
171     selected=$("${tac_cmd[@]}" "$HISTFILE" | \
172         sed 's/^: [0-9]*:[0-9]*;//' | \
173         awk 'seen[$0] {next} {seen[$0]++; print}' | \
174         percol --match-method regex --query "$LBUFFER")
175     if [[ $? -eq 0 ]]; then
176         BUFFER="$selected"
177         CURSOR="$#BUFFER" # move cursor to the end of line
178         zle redisplay
179         #zle accept-line  # uncomment this to execute the selected one immediately.
180     else
181         zle redisplay
182     fi
183 }
184 if which percol >&/dev/null; then
185     zle -N percol_select_history
186     bindkey '^R' percol_select_history
187 fi
188
189 HISTFILE="$HOME/.zhistory"
190 HISTSIZE=6000000
191 SAVEHIST=6000000
192
193 function lv() {
194     local -a lv_cmd
195
196     if whence -p lv >/dev/null; then
197         lv_cmd=("lv")
198     elif which less >&/dev/null; then
199         lv_cmd=("less")
200     elif which more >&/dev/null; then
201         lv_cmd=("more")
202     else
203         echo >&2 "$0: no pager commands are found"
204     fi
205
206     command "${lv_cmd[@]}" "$@"
207 }
208
209 alias -g L="| $PAGER"
210 alias -g G="| grep"
211 alias -g H="| head"
212 alias -g T="| tail"
213 alias -g X="| hexdump -C"
214
215 alias ..='cd ..'
216 alias cd..='cd ..'
217 alias p='pushd'
218 alias o='popd'
219 alias d='dirs'
220 alias df='df -h'
221 alias du='du -k'
222 alias ed='ed -p "ed> "'
223 alias man="LANG=C man"
224 alias mwget="wget --user-agent='Mozilla/1.4b'"
225 alias nc='nc -vv'
226 alias pkgsrc="pushd /usr/pkgsrc"
227 alias pstow="pushd /usr/local/stow"
228 alias w3m="w3m -O UTF-8"
229 alias w3mb="w3m -O UTF-8 -B"
230
231 alias aria-emacs="ssh -f -X admin@aria.cielonegro.org emacsclient -c"
232 alias pho-dev-boinc="ssh -f -X pho@pho.dev.office.ymir.co.jp env LANG=C /home/pho/var/BOINC/run_manager"
233
234 if [ $(hostname) = "aria.cielonegro.org" ]; then
235     alias safari="open -a Safari"
236     alias unstuff='open -a StuffIt\ Expander.app'
237     alias pa='open -a NiseRingo.app'
238     alias heboris='open /Applications/HeborisUEMac/exe/Heboris\ OpenGL.app'
239     alias hengband='open ~/Applications/hengband*/Hengband*'
240     alias syssleep='sudo shutdown -s now'
241 fi
242
243 if [[ $(uname) = "Darwin" ]]; then
244     alias ldd="otool -L"
245
246     alias ls='ls -Fw'
247     alias la='ls -aw'
248     alias l='ls -lw'
249     alias lls='ls -law'
250
251     alias top='/usr/bin/top -X -o cpu'
252 else
253     alias ls='ls -F'
254     alias la='ls -a'
255     alias l='ls -l'
256     alias lls='ls -la'
257 fi
258
259 function osname () {
260     case $(uname) in
261         "Darwin" | "FreeBSD" | "NetBSD")
262             uname -v;;
263         "Linux")
264             if [[ -f "/etc/redhat-release" ]]; then
265                 cat "/etc/redhat-release"
266             elif [[ -f "/etc/SuSE-release" ]]; then
267                 cat "/etc/SuSE-release"
268             else
269                 echo -n "$0: I know this is "$(uname -o)" but " >&2
270                 echo    "have no idea about its distribution."  >&2
271                 return 1
272             fi;;
273         *)
274             echo "$0: Failed to detect the OS name." >&2
275             return 1
276     esac
277 }
278
279 # The following lines were added by compinstall
280
281 zstyle :compinstall filename "$HOME/.zshrc"
282
283 autoload -U compinit
284 compinit
285 # End of lines added by compinstall
286
287 if _we_are_in_gnu_screen; then
288     # When we are in a GNU Screen session...
289
290     if _we_are_in_ssh_session; then
291         # Propagate remote session's bells to the local one.
292         screen -X vbell off
293     fi
294 else
295     if _we_are_in_emacs; then
296         # Without this, Emacs' term mode has trouble with backspaces.
297         export TERM=vt100
298     fi
299
300     # Run "fortune" if we aren't in a GNU Screen session.
301     if which fortune >/dev/null 2>&1; then
302         fortune
303     fi
304 fi
305
306 if which ssh-agent-manager >/dev/null 2>&1; then
307     eval `ssh-agent-manager -s`
308 fi