initial
[zsh.git] / .zsh / 05_Functions.zsh
1 # -*- mode: sh;-*-
2
3 ## User-defined functions
4 #
5 cmd_exists ()
6 {
7 \which -p $1 >/dev/null 2>&1
8 }
9
10 preprint()
11 {
12 local my_color i
13 my_color=${2-"$prompt_colors[generic]"}
14
15 hbar=$T_
16 for i in {1..$((74 - ${#1} - 5))}; do
17 hbar=${hbar}$_t_q
18 done
19 hbar=${hbar}$_T
20
21 if [ "$1" != "" ]; then
22 print -Pn "${C_}$my_color;1${_C}${hbar}$T_$_t_u$_T${C_}0;$my_color${_C} $1 ${C_}0;$my_color;1${_C}$T_$_t_t$_t_q$_T\r${C_}0${_C}"
23 else
24 print -Pn "${C_}$my_color;1${_C}${hbar}$T_$_t_q$_t_q$_t_q$_t_q$_t_q$_T${C_}0${_C}"
25 fi
26 }
27
28
29 normal_user ()
30 {
31 if [ -e /etc/login.defs ]; then
32 eval `grep -v '^[$#]' /etc/login.defs | grep "^UID_" | tr -d '[:blank:]' | sed 's/^[A-Z_]\+/&=/'`
33 [ \( $UID -ge $UID_MIN \) ]
34 else
35 [ "`whoami`" != "root" ]
36 fi
37 }
38
39 privileged_user ()
40 {
41 ! normal_user
42 }
43
44 chpwd()
45 {
46 if ( cmd_exists git && test -d .git ); then
47 # Shows tracked branches and modified files
48 git checkout HEAD 2>&1 | sed 's/^/ /'
49 fi
50 }
51
52 # Taken from oh-my-zsh
53 dirpersiststore () {
54 dirs -p | perl -e 'foreach (reverse <STDIN>) {chomp;s/([& ])/\\$1/g ;print "if [ -d $_ ]; then pushd -q $_; fi\n"}' >| $zdirstore
55 }
56
57 dirpersistrestore () {
58 if [ -f $zdirstore ]; then
59 source $zdirstore
60 fi
61 }
62
63 zsh_stats() {
64 history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n20
65 }
66
67 _jj_ssh() {
68 # TERM is one of the variables that is usually allowed to be
69 # transmitted to the remote session. The remote host should have the
70 # appropriate termcap or terminfo file to handle the TERM you
71 # provided. When connecting to random hosts, this may not be the
72 # case if your TERM is somewhat special. A good fallback is
73 # xterm. Most terminals are compatible with xterm and all hosts have
74 # a termcap or terminfo file to handle xterm. Therefore, for some
75 # values of TERM, we fallback to xterm.
76 #
77 # Now, you may connect to a host where your current TERM is fully
78 # supported and you will get xterm instead (which means 8 base
79 # colors only). There is no clean solution for this. You may want to
80 # reexport the appropriate TERM when logged on the remote host or
81 # use commands like this: ssh -t XXXXX env TERM=$TERM emacsclient -t
82 # -c
83 #
84 # If the remote host uses the same zshrc than this one, there is
85 # something in `$ZSH/rc/00-terminfo.zsh` to restore the appropriate
86 # terminal (saved in `LC__ORIGINALTERM`).
87 #
88 # The problem is quite similar for LANG and LC_MESSAGES. We reset
89 # them to C to avoid any problem with hosts not having your locally
90 # installed locales. See this post for more details on this:
91 # http://vincent.bernat.im/en/blog/2011-zsh-zshrc.html
92 #
93 # Also, when the same ZSH configuration is used on the remote host,
94 # the locale is reset with the help of 12-Locale.zsh
95 case "$TERM" in
96 rxvt-256color|rxvt-unicode*)
97 LC__ORIGINALTERM=$TERM TERM=xterm LANG=C LC_MESSAGES=C command ssh "$@"
98 ;;
99 *)
100 LANG=C LC_MESSAGES=C command ssh "$@"
101 ;;
102 esac
103 }
104
105 _jj_first_non_optional_arg() {
106 local args
107 args=( "$@" )
108 args=( ${(R)args:#-*} )
109 print -- $args[1]
110 }
111
112 # Alter window title
113 _jj_title () {
114 [ -t 1 ] || return
115 emulate -L zsh
116 local title
117 title=${@//[^[:alnum:]\/>< ._~:=?@-]/ }
118 case $TERM in
119 screen*)
120 print -n "\ek$title\e\\"
121 print -n "\e]1;$title\a"
122 print -n "\e]2;$title\a"
123 ;;
124 rxvt*|xterm*)
125 print -n "\e]1;$title\a"
126 print -n "\e]2;$title\a"
127 ;;
128 esac
129 }
130
131 ssh() {
132 _jj_title $(_jj_first_non_optional_arg "$@")
133 _jj_ssh "$@"
134 }