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