dot/shell-rc, dot/bashrc, dot/zshrc: Introduce a formal notion of hooks.
[profile] / dot / zshrc
1 ### -*-sh-*-
2 ###
3 ### Zsh session things.
4
5 __mdw_shell=zsh
6 case ${INSIDE_EMACS+t},$TERM in t,dumb) unsetopt zle ;; esac
7
8 ###--------------------------------------------------------------------------
9 ### History settings.
10
11 ## Put this here so that `.shellrc-local' can override.
12
13 HISTFILE=~/.zsh-history
14 HISTSIZE=1000
15 SAVEHIST=1000
16
17 ###--------------------------------------------------------------------------
18 ### Hook implementation.
19
20 __mdw_precmd_hook= __mdw_preexec_hook=
21
22 __mdw_run_precmd_hook () { __mdw_runhook __mdw_precmd_hook "$@"; }
23 __mdw_run_preexec_hook () { __mdw_runhook __mdw_preexec_hook "$@"; }
24
25 precmd_functions+=(__mdw_run_precmd_hook)
26 preexec_functions+=(__mdw_run_preexec_hook)
27
28 ###--------------------------------------------------------------------------
29 ### Common shell configuration.
30
31 . "$HOME/.shell-rc"
32
33 ## Fixup: `zsh' has sane quoting rules.
34 __mdw_runhook () {
35 local hk=$1 saverc=$? t i; shift
36
37 eval t=\${$hk+t}
38 case $t in t) ;; *) echo >&2 "unknown hook \`$hk'"; return 2; esac
39
40 eval t=\$$hk
41 for i in ${=t}; do __mdw_setrc $saverc; "$i" "$@"; done
42 }
43
44 ###--------------------------------------------------------------------------
45 ### Prompt hacking.
46
47 __mdw_set_prompt_hacks () {
48 case $TERM in
49 linux*|screen*|xterm*|putty*|vt100*|eterm*)
50 bold=%B unbold=%b
51 gitcolour=%F{cyan} rccolour=%F{red} uncolour=%f
52 ;;
53 esac
54 host=%m dir=" %(6~!%-1~/.../%4~!%~)"
55 more=%F{green}%_%f
56 }
57
58 if [ -t 0 ]; then
59 __mdw_source_if_exists /usr/lib/git-core/git-sh-prompt
60 __mdw_set_prompt_pieces
61 fi
62
63 ###--------------------------------------------------------------------------
64 ### Line editing.
65
66 case $TERM in dumb) unsetopt zle ;; esac
67
68 bindkey -e
69
70 for w in \
71 forward-word backward-word kill-word backward-kill-word \
72 transpose-words capitalize-word up-case-word down-case-word \
73 delete-whole-word select-word
74 do
75 autoload -U $w-match
76 zle -N $w-bash $w-match
77 zle -N $w-shell $w-match
78 zstyle ':zle:*-bash' word-style standard
79 zstyle ':zle:*-bash' skip-whitespace-first true
80 zstyle ':zle:*-bash' word-chars ""
81 zstyle ':zle:*-shell' word-style shell
82 zstyle ':zle:*-shell' skip-whitespace-first false
83 done
84
85 bindkey "\eb" backward-word-bash
86 bindkey "\e^b" backward-word-shell
87 bindkey "\ef" forward-word-bash
88 bindkey "\e^f" forward-word-shell
89 bindkey "\e^?" backward-kill-word-bash
90 bindkey "^w" backward-kill-word-shell
91 bindkey "\ed" kill-word-bash
92 bindkey "\e^d" kill-word-shell
93 bindkey "\et" transpose-words-bash
94 bindkey "\e^t" transpose-words-shell
95 bindkey "\eu" up-case-word-bash
96 bindkey "\e^u" up-case-word-shell
97 bindkey "\el" down-case-word-bash
98 bindkey "\e^l" down-case-word-shell
99 bindkey "\ec" capitalize-word-bash
100 bindkey "\e^c" capitalize-word-shell
101
102 bindkey "\e[1~" beginning-of-line "\e[4~" end-of-line
103
104 bindkey -s "\eOQ" "/" "\eOR" "*" "\eOS" "-"
105 bindkey -s "\eOw" "7" "\eOx" "8" "\eOy" "9"
106 bindkey -s "\eOt" "4" "\eOu" "5" "\eOv" "6" "\eOk" "+"
107 bindkey -s "\eOq" "1" "\eOr" "2" "\eOs" "3"
108 bindkey -s "\eOp" "0" "\eOn" "."; bindkey "\eOM" accept-line
109
110 bindkey -s "\eOQ" "/" "\eOR" "*" "\eOS" "-"
111 bindkey -s "\eOw" "7" "\eOx" "8" "\eOy" "9"
112 bindkey -s "\eOt" "4" "\eOu" "5" "\eOv" "6" "\eOk" "+"
113 bindkey -s "\eOq" "1" "\eOr" "2" "\eOs" "3"
114 bindkey -s "\eOp" "0" "\eOn" "."; bindkey "\eOM" accept-line
115
116 bindkey "\ep" history-beginning-search-backward
117 bindkey "\en" history-beginning-search-forward
118 bindkey "\e," _history-complete-older
119
120 for i in '!' '$' '@' '/' '~'; do
121 bindkey "\e$i" _bash_complete-word
122 bindkey "^X$i" _bash_list-choices
123 done
124
125 setopt interactive_comments
126 bindkey "\e#" pound-insert
127
128 __mdw_delete_horizontal_space () {
129 LBUFFER=${LBUFFER%%[[:space:]]##}
130 RBUFFER=${RBUFFER##[[:space:]]##}
131 }
132 zle -N delete-horizontal-space __mdw_delete_horizontal_space
133 bindkey "\e\\" delete-horizontal-space
134
135 __mdw_just_one_space () {
136 LBUFFER="${LBUFFER%%[[:space:]]##} "
137 RBUFFER=${RBUFFER##[[:space:]]##}
138 }
139 zle -N just-one-space __mdw_just_one_space
140 bindkey "\e " just-one-space
141
142 ###--------------------------------------------------------------------------
143 ### Completion.
144
145 ## Contexts: :completion:FUNCTION:COMPLETER:COMMAND:ARGUMENT:TAG
146 zstyle ':completion:*' completer _expand _complete _ignored _approximate
147 zstyle ':completion:*' insert-unambiguous false
148 zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
149 zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s
150 zstyle ':completion:*' matcher-list '' '+m:{[:lower:]}={[:upper:]} r:|[._-]=** r:|=**' '+l:|=* r:|=*'
151 zstyle ':completion:*' max-errors 0 numeric
152 zstyle ':completion:*' original true
153 zstyle ':completion:*' verbose false
154 zstyle ':completion:*:*:git*:*' verbose true
155
156 ## Initialize the fancy completion machinery.
157 autoload -Uz compinit
158 compinit
159
160 _r () { words[1]=sudo; _normal; }
161 compdef _r rootly
162 compdef _ssh @
163
164 ###--------------------------------------------------------------------------
165 ### Other shell tweaking.
166
167 unsetopt auto_cd
168 unsetopt auto_menu
169 setopt bang_hist
170 unsetopt bash_auto_list
171 unsetopt beep
172 setopt c_bases octal_zeroes
173 setopt extendedglob
174 unsetopt flow_control
175 unsetopt global_export
176 setopt glob_star_short
177 setopt hist_ignore_all_dups
178 setopt hist_ignore_space
179 unsetopt ksh_glob
180 setopt list_ambiguous
181 setopt list_packed
182 setopt multios
183 unsetopt nomatch
184 unsetopt menu_complete
185 setopt notify
186 setopt rc_expand_param
187 setopt share_history
188
189 hash -d t=$TMPDIR
190
191 ###--------------------------------------------------------------------------
192 ### Finishing touches.
193
194 ## Local tweaks.
195 __mdw_source_if_exists "$HOME/.zshrc-local"
196
197 ###----- That's all, folks --------------------------------------------------