Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 412 lines (367 sloc) 12.362 kB
88df6f0 @richo Github, whyfor you break multibyte chracter?!
richo authored
1 # #!/usr/bin/env zsh
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
2 # richo '11
3 #
4 # TODO
5 # Document all of the *TITLE variables
6 # Clean up that infrastructure (honestly, I think I either need to learn zsh
7 # modules, or write a seperate program to do it
75552ca @richo IF that exit code is nonzero, also show it
richo authored
8 # Update everything to use the COLOR constants instead of escape codes
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
9 source ~/.profile
10 zstyle :compinstall filename '/home/richo/.zshrc'
11 autoload -U compinit promptinit
12 autoload -Uz vcs_info
13 compinit
14 autoload colors
15 colors
16
17 promptinit
18 # Lines configured by zsh-newuser-install
19 HISTFILE=~/.histfile
20 HISTSIZE=1000
21 SAVEHIST=1000
22 setopt prompt_subst
23 setopt sharehistory
24
25
26 function parse_ruby_version {
2405e5c @richo Use shell redirection instead of echo
richo authored
27 local gemset=$(awk -F'@' '{print $2}' <<< $GEM_HOME)
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
28 [ "$gemset" != "" ] && gemset="@$gemset"
2405e5c @richo Use shell redirection instead of echo
richo authored
29 local version=$(awk -F'-' '{print $2}' <<< $MY_RUBY_HOME)
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
30 # [ "$version" == "1.8.7" ] && version=""
31 local full="$version$gemset"
32 [ "$full" != "" ] && echo "$full"
33 }
34
35 function cdp
36 {
37 cd $pdir
38 }
39
40 function _rpath
41 {
42 pth='%{\e[0;34m%}%B%~'
43 a=$(parse_ruby_version)
44 if [ -z "$a" ]; then
45 echo $pth
46 else
47 echo ${pth}'%{\e[0;36m%}%B'" ($a)"
48 fi
49 }
50 git_prompt_info() {
51 ref=$(git-symbolic-ref HEAD 2> /dev/null) || return
52 echo "(${ref#refs/heads/})"
53 }
54
45b1ffd @richo prompt shows exit status + color
richo authored
55 function host_r()
56 {
57 n=$((${#?} + 2))
58 echo "$sHost[0,-$n]"
59 }
60
61
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
62 function _prompt()
63 {
ccf92c6 @richo Boldify the errcode
richo authored
64 echo -e "%{\e[0;${SHELL_COLOR}m%}%B%(?.%m.\$(host_r) %{\e[0;31m%}%B%?)%b %{\e[0;34m%}%B%#%b%{\e[0m%} "
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
65 }
66
67
68 function _rprompt()
69 { # Unify so I only need edit one place
70 local git='$vcs_info_msg_0_'
71 echo -e "$(_rpath) %b%{\e[0;36m%}${git}%{\e[0m%}%{\e[0;34m%}%B${ZSH_TIME}"
72 # XXX Maybe this would be cleaner if we just ^^
73 # change the color when we have stashes? Especially since the stash hook
74 # Depends on some wierd, vaguely unreproducable behavior
75 }
76
77 #export PROMPT_COMMAND='echo -ne "\033]0;$(basename "$(dirname "$PWD")")/$(basename "$PWD")\007"'
78 bindkey -v
79 # End of lines configured by zsh-newuser-install
80 #PROMPT='%m %# '
81 PS1=$(_prompt)
82 #PROMPT='\033_${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\033\\'
83 RPS1=$(_rprompt)
84
85 setopt histignoredups
86 bindkey '^R' history-incremental-search-backward
87
88 function _time()
89 {
90 date "+%s"
91 }
92
191627d @richo When we create tags automatically, do that in the background
richo authored
93 function b_tags()
94 {
95 $(cd $1 && ctags -R -f .newtags . 2>/dev/null && mv .newtags tags)
96 }
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
97
b4a072f @richo Rename this function to avoid potentiall collisions
richo authored
98 function __richo_preexec()
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
99 # DOCS
100 # This function has a few variables that it throws around.
101 # I'm thinking pretty seriously about building this show into it's own module and calling that, but load times could become an issue
102 #
103 # reTITLE
104 # -------
105 # This is what the title will be set to after this command finishes
106 #
107 # sTITLE
108 # ------
109 # This is the current value of the title "at rest" (Including things like the prefix)
110 { # {{{ Prexec hax
111 # Potential TODO:
112 # Set a variable for commands where we care about return status
113 # if returnstatus and $! -> Add something to sTITLE
114 reTITLE=$sTITLE
115 case $1 in
116 # Rails kludges
117 "rails "*)
2405e5c @richo Use shell redirection instead of echo
richo authored
118 work=`sed -e 's/^rails //' <<< $1`
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
119 case $work in
120 "s"|"server")
121 arg="WEBRICK"
122 ;;
123 esac
124 ;;
6d667a8 @richo Show full command (sans bundle exec) for things I run in the bundle env
richo authored
125 "bundle exec"*)
2405e5c @richo Use shell redirection instead of echo
richo authored
126 arg=`sed -e 's/bundle exec/BE:/' <<< $1`
6d667a8 @richo Show full command (sans bundle exec) for things I run in the bundle env
richo authored
127 ;;
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
128
aa55d82 @richo Move the cd hook in preexec to it's own specific zsh hook
richo authored
129 "cd"*|"ls"*|"cp"*|"mv"*|"echo"*|"wiki"*|"screen"*|"dig"*|"rm"*|"mkdir"*|"tinfo"*)
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
130 reTITLE=""
131 return ;;
7ad248b @richo clear clears title ( Patch from @onlysamurai )
richo authored
132 "clear"*)
133 arg="zsh";;
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
134
135 # If we're doing it to everything, the command is more interesting than
136 # the target
137 *"*")
2405e5c @richo Use shell redirection instead of echo
richo authored
138 arg=$(awk '{print $1}' <<< $1);;
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
139 # Catch kill early
140 "kill "*)
141 reTITLE=""
2405e5c @richo Use shell redirection instead of echo
richo authored
142 arg=$(awk '{print $NF}' <<< $1);;
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
143 "ctags"*|"killall"*|"screen"*)
144 return ;;
145 "tmux"*)
146 case $rTITLE in
147 *"tmux"*)
148 ;;
149 *)
150 export rTITLE="$rTITLE [tmux]"
106e3ba @richo Refactor title functions, again for collisions
richo authored
151 __set_urxvt_title $rTITLE
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
152 ;;
153 esac
154 ;;
155 "man"*)
156 arg=$1;;
157 "watchr"*)
158 arg="WATCHR";;
159 "./deploy.sh"*)
2405e5c @richo Use shell redirection instead of echo
richo authored
160 arg=$(sed $sed_r -e 's/^\.\/deploy.sh/deploy:/' -e 's/114\.111\.139\.//' -e 's|/var/www/||g' <<< $1);;
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
161
162 # For Source control I want the whole line, I think...
163 "svn"*|"git"*|"hg"*|"cvs"*)
164 arg=$1;;
165
166 "make"*)
167 arg=$(pwd | grep -o "[^/]*/[^/]*$");;
168
169 # TODO Include host
170 "cap"*)
171 # hax
172 #arg=$(echo $1 | grep -o "(deploy[^ ]*)");;
2405e5c @richo Use shell redirection instead of echo
richo authored
173 arg=$(awk '{print $2}' <<< $1);;
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
174 "ncmpc"*)
2405e5c @richo Use shell redirection instead of echo
richo authored
175 arg=$(sed $sed_r -e 's/ ?-h */:/' <<< $1);;
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
176
177 # Webby stuffs
178 "lynx"*|"links"*)
2405e5c @richo Use shell redirection instead of echo
richo authored
179 arg=$(sed $sed_r -e 's/^(lynx|links) (http[s]?:\/\/)?(www\.)?//' -e 's/\/.*$//' <<< $1);;
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
180
181 "su"*)
182 arg="!root!"
183 export reTITLE=$sTITLE
184 ;;
185 "ssh"*)
2405e5c @richo Use shell redirection instead of echo
richo authored
186 arg=$(awk '{print $NF}' <<< $1)
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
187 # Don't care where in the local fs we are
188 export t_prefix=""
189 export reTITLE=$sTITLE
190 ;;
191 "vim"*)
192 # Vim likes to play funny buggers with my terminal. Show that
193 # bastage who's in charge.
194 export reTITLE=$sTITLE
195 # Don't bother setting a title- handles it.
196 ;;
a64046c @richo Put thor commands in window title more intelligently
richo authored
197 "_thor"*|"thor"*)
eedd226 @richo Added thor titling
richo authored
198 export reTITLE=$sTITLE
2405e5c @richo Use shell redirection instead of echo
richo authored
199 arg=`sed $sed_r -e 's/^_?thor //' -e 's/ /:/' <<< $1`
eedd226 @richo Added thor titling
richo authored
200 if [ -z "$INSCREEN" ]; then
106e3ba @richo Refactor title functions, again for collisions
richo authored
201 __set_urxvt_title "$arg: "
eedd226 @richo Added thor titling
richo authored
202 fi
203 ;;
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
204 *)
2405e5c @richo Use shell redirection instead of echo
richo authored
205 arg=$(awk '{print $NF}' <<< $1);;
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
206 esac
207
106e3ba @richo Refactor title functions, again for collisions
richo authored
208 __set_title $arg
b4a072f @richo Rename this function to avoid potentiall collisions
richo authored
209 }
210 add-zsh-hook preexec __richo_preexec
211 # }}}
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
212
aa55d82 @richo Move the cd hook in preexec to it's own specific zsh hook
richo authored
213 # {{{ chpwd hook
214 function __richo_chpwd()
215 {
216 # Clear title if we're going home
217 if [ "$PWD" = "$HOME" ]; then
218 export t_prefix=""
219 arg=$sTITLE
220 else
221 if [ -e .title ]; then
222 export pdir=$PWD
223 # XXX Should this happen for all titles?
224 dTITLE=$(cat .title | sed $sed_r 's/[\r\n]//g')
225 export t_prefix="$dTITLE: "
226 arg=""
227 fi
228 if [ -n "$AUTOTAGS" -a -f .autotags ]; then
229 # TODO
230 # Store some more info about the tags, command to run and
231 # git branch, and use the stat time of the file, rather
232 # than the contents to work out timing
233 if [ $((`cat .autotags` + $TAGS_LIFETIME)) -lt `_time` ]; then
234 _time > .autotags
235 echo "Tags are mad old, regenerating."
236 b_tags $PWD &|
237 fi
238 fi
239 fi
106e3ba @richo Refactor title functions, again for collisions
richo authored
240 __set_title $arg
aa55d82 @richo Move the cd hook in preexec to it's own specific zsh hook
richo authored
241 }
242 add-zsh-hook chpwd __richo_chpwd
243 #}}}
244
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
245 # {{{ Helper functions to set titles
106e3ba @richo Refactor title functions, again for collisions
richo authored
246 function __set_title()
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
247 {
248 if [ ! -z $INSCREEN ] ; then
249 echo -ne "\033k$t_prefix$@\033\\"
250 fi
251 }
106e3ba @richo Refactor title functions, again for collisions
richo authored
252 function __set_urxvt_title()
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
253 {
254 echo -ne "\033]0;$1\007"
255 }
256 # }}}
257 # If we're an ssh connection, just prefix!
258 if [ -n "$SSH_CONNECTION" -a "$TERM" = "screen" -a -z "$TMUX" ]; then
259 export INSCREEN=yes
260 dTITLE=`hostname -s`
261 t_prefix="$dTITLE: "
262 t
263 fi
264
a44d6a5 @richo Moar collision avoidance
richo authored
265 function __richo_precmd()
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
266 { # {{{ postexec hax
267 vcs_info 'prompt'
268 RPS1=$(_rprompt)
269 PS1=$(_prompt)
59376bc @richo Small change, cleanup effort
richo authored
270 if [ -n "$reTITLE" -a -n "$INSCREEN" ]; then
106e3ba @richo Refactor title functions, again for collisions
richo authored
271 __set_title $reTITLE
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
272 export reTITLE=""
273 fi
a44d6a5 @richo Moar collision avoidance
richo authored
274 }
275 add-zsh-hook precmd __richo_precmd
276 # }}}
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
277
278 for COLOR in RED GREEN YELLOW WHITE BLACK CYAN; do
279 eval PR_$COLOR='%{$fg[${(L)COLOR}]%}'
280 eval PR_BRIGHT_$COLOR='%{$fg_bold[${(L)COLOR}]%}'
281 done
282 PR_RESET="%{${reset_color}%}";
283
284 # TODO This is just someone's template, fix.
285 # set formats
286 # %b - branchname
287 # %u - unstagedstr (see below)
288 # %c - stangedstr (see below)
289 # %a - action (e.g. rebase-i)
290 # %R - repository path
291 # %S - path in the repository
292 REPO_COLOR=${PR_CYAN}
293 # ^^ HAX?
294 # # e.g. master¹²
295 FMT_ACTION="(${PR_CYAN}%a${PR_RESET}%)" # e.g. (rebase-i)
296
297 # check-for-changes can be really slow.
298 # you should disable it, if you work with large repositories
299 zstyle ':vcs_info:*' enable hg git bzr svn
300 zstyle ':vcs_info:*:prompt:*' check-for-changes true
301 if [ -n "$BROKEN_MULTIBYTE" ]; then
88df6f0 @richo Github, whyfor you break multibyte chracter?!
richo authored
302 zstyle ':vcs_info:*:prompt:*' unstagedstr '¹' # display ¹ if there are unstaged changes
303 zstyle ':vcs_info:*:prompt:*' stagedstr '²' # display ² if there are staged changes
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
304 else
88df6f0 @richo Github, whyfor you break multibyte chracter?!
richo authored
305 zstyle ':vcs_info:*:prompt:*' unstagedstr '¹' # display ¹ if there are unstaged changes
306 zstyle ':vcs_info:*:prompt:*' stagedstr '²' # display ² if there are staged changes
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
307 fi
308 # Save this for later ³
309 # TODO - Show something if I have unpushed changes.
310 function _init(){
311 FMT_BRANCH="${REPO_COLOR}%b${PR_BRIGHT_CYAN}%u%c${PR_RESET}${PR_RED}%m${PR_RESET}"
312 zstyle ':vcs_info:*:prompt:*' actionformats "${FMT_BRANCH}${FMT_ACTION}"
313 zstyle ':vcs_info:*:prompt:*' formats "${FMT_BRANCH}"
314 zstyle ':vcs_info:*:prompt:*' nvcsformats "" "%~"
315 }
316 function svn_init(){
317 SVN_BRANCH="${REPO_COLOR}%b${PR_BRIGHT_CYAN}%u%c${PR_RESET}${PR_RED}%m${PR_RESET}"
318 zstyle ':vcs_info:svn:prompt:*' actionformats "${SVN_BRANCH}${FMT_ACTION}"
319 zstyle ':vcs_info:svn:prompt:*' formats "${SVN_BRANCH}"
320 }
321 _init
322
323 # Show remote ref name and number of commits ahead-of or behind
cd3dbb9 @richo Clean up the git hook a bit further
richo authored
324 countl () { wc -l | sed $sed_r -e "s/^ +//" }
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
325 function +vi-git-st() { #{{{
326 local ahead remote msg origin
327 local -a gitstatus
328
329 # Are we on a remote-tracking branch?
330 remote=${$(git rev-parse --verify ${hook_com[branch]}@{upstream} \
331 --symbolic-full-name 2>/dev/null)/refs\/remotes\/}
332
333 msg=""
334
335 if [[ -n ${remote} ]] ; then
336 # for git prior to 1.7
337 # ahead=$(git rev-list origin/${hook_com[branch]}..HEAD | wc -l)
cd3dbb9 @richo Clean up the git hook a bit further
richo authored
338 origin=$(git rev-list origin/${hook_com[branch]}..HEAD 2>/dev/null | countl)
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
339 (( $origin )) && msg+="+$origin"
340
cd3dbb9 @richo Clean up the git hook a bit further
richo authored
341 ahead=$(git rev-list ${hook_com[branch]}@{upstream}..HEAD 2>/dev/null | countl)
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
342 (( $ahead )) && msg+="|$ahead|"
343
cd3dbb9 @richo Clean up the git hook a bit further
richo authored
344 behind=$(git rev-list HEAD..${hook_com[branch]}@{upstream} 2>/dev/null | countl)
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
345 (( $behind )) && msg+="-$behind"
346
cd3dbb9 @richo Clean up the git hook a bit further
richo authored
347 stashes=$(git stash list 2>/dev/null | countl)
302cbfa @richo Fixed some broken regexes
richo authored
348 if [ "$stashes" -gt 0 ]; then
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
349 msg+="?${stashes}s"
350 fi
351
352 #(( $ahead )) && hook_com[misc]+=" (+$ahead)"
353 [ -n "$msg" ] && hook_com[misc]=" ($msg)"
354 fi
355 } #}}}
356 function +vi-svn-nochanges() { #{{{
357 REPO_COLOR="${PR_YELLOW}"
358 svn_init
359 zstyle ':vcs_info:svn*+set-message:*' hooks ""
360 } #}}}
361 function +vi-svn-untimeduncommitted() { #{{{
362 v=$(svnversion)
2405e5c @richo Use shell redirection instead of echo
richo authored
363 if grep "M$" > /dev/null 2>&1 <<< $v; then
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
364 hook_com[misc]="**"
365 fi
366 } #}}}
367
368 function +vi-svn-uncommitted() { #{{{
369 v=$(timeout $SVNVERSION_TIMEOUT svnversion)
370 case $? in
371 124)
372 +vi-svn-nochanges
373 ;;
374 0)
2405e5c @richo Use shell redirection instead of echo
richo authored
375 if grep "M$" > /dev/null 2>&1 <<< $v; then
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
376 hook_com[misc]="**"
377 fi
378 ;;
379 esac
380 } #}}}
381
382 zstyle ':vcs_info:git*+set-message:*' hooks git-st
383 if which timeout >/dev/null; then
384 zstyle ':vcs_info:svn*+set-message:*' hooks svn-uncommitted
385 else
386 zstyle ':vcs_info:svn*+set-message:*' hooks svn-untimeduncommitted
387 fi
388
389 alias nochanges="zstyle ':vcs_info:svn*+set-message:*' hooks svn-nochanges"
390 alias changes="zstyle ':vcs_info:svn*+set-message:*' hooks svn-untimeduncommitted"
391
392 # FIXME!!!
f8f3fa9 @richo Now uses .svnignore and .gitignore to work out what not to commit
richo authored
393 # This is horrid, and I'm clearly knackered. There /is/ an easier way to
394 # replace the newlines with pipe symbols. There is.
fc2ee17 @richo Only create these zstyle elements if the source files exist
richo authored
395 [ -e ~/.gitignore ] &&
396 zstyle ':completion:*:*:git-add:*' ignored-patterns `grep -v "^#" ~/.gitignore | xargs echo | sed -e 's/ /|/g'`
397 [ -e ~/.subversion/config ] &&
f0636b9 @richo Remogved an extra xargs
richo authored
398 zstyle ':completion:*:*:svn-add:*' ignored-patterns `grep "^global-ignores" ~/.subversion/config | sed -e 's/^.*= //' -e 's/ /|/g'`
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
399
400 zstyle ':completion:*:functions' ignored-patterns '_*'
401 zstyle ':completion:*:*:kill:*' menu yes select
402 zstyle ':completion:*:kill:*' force-list always
403 # If we're spawning a shell in a urxvt, but we're NOT in shell, put the tty in
404 # the titlebar.
ec0a615 @richo Clean up a test
richo authored
405 if [[ "$TERM" =~ "-256color" && -z "$INSCREEN" ]]; then
106e3ba @richo Refactor title functions, again for collisions
richo authored
406 __set_urxvt_title $rTITLE
aef0fc6 @richo A few files I've quickly checked for sanity
richo authored
407 fi
408
409
fe60a36 @richo Allow for .hostname instead of .local
richo authored
410 [ -e $HOME/.zshrc.$sHost ] && source $HOME/.zshrc.$sHost
49da8fb @richo Experimenting with tmuxinator
richo authored
411 [[ -s $HOME/.tmuxinator/scripts/tmuxinator ]] && source $HOME/.tmuxinator/scripts/tmuxinator
Something went wrong with that request. Please try again.