95da671e2011a39a9998e285a7f53201afb3b9ed
[zsh.git] / .zsh / functions / ssh
1 # -*- mode:sh -*-
2
3 _jj_ssh() {
4 # TERM is one of the variables that is usually allowed to be
5 # transmitted to the remote session. The remote host should have the
6 # appropriate termcap or terminfo file to handle the TERM you
7 # provided. When connecting to random hosts, this may not be the
8 # case if your TERM is somewhat special. A good fallback is
9 # xterm or rxvt.
10 # Most terminals are compatible with xterm and all hosts have
11 # a termcap or terminfo file to handle xterm. But as it is incompatible
12 # in some annoying ways with default rxvt, we fall back to rxvt.
13 # (Some keys are different).
14 #
15 # Now, you may connect to a host where your current TERM is fully
16 # supported and you will get rxvt instead (which means 8 base
17 # colors only). There is no clean solution for this. You may want to
18 # reexport the appropriate TERM when logged on the remote host or
19 # use commands like this: ssh -t XXXXX env TERM=$TERM emacsclient -t
20 # -c
21 #
22 # If the remote host uses the same zshrc than this one, there is
23 # something in its config to restore the appropriate terminal (saved
24 # in `LC__ORIGINALTERM`).
25 #
26 # The problem is quite similar for LANG and LC_MESSAGES. We reset
27 # them to C to avoid any problem with hosts not having your locally
28 # installed locales. See this post for more details on this:
29 # http://vincent.bernat.im/en/blog/2011-zsh-zshrc.html
30 #
31 # Also, when the same ZSH configuration is used on the remote host,
32 # the locale is reset to useful values.
33 case "$TERM" in
34 rxvt-256color|rxvt-unicode|rxvt-unicode-256color)
35 LC__ORIGINALTERM=$TERM TERM=rxvt LANG=C LC_MESSAGES=C command ssh "$@"
36 ;;
37 *)
38 LANG=C LC_MESSAGES=C command ssh "$@"
39 ;;
40 esac
41 }
42
43 _jj_first_non_optional_arg() {
44 local args
45 args=( "$@" )
46 args=( ${(R)args:#-*} )
47 print -- $args[1]
48 }
49
50 # Alter window title
51 _jj_title () {
52 [ -t 1 ] || return
53 emulate -L zsh
54 local title
55 title=${@//[^[:alnum:]\/>< ._~:=?@-]/ }
56 case $TERM in
57 screen*)
58 print -n "\ek$title\e\\"
59 print -n "\e]1;$title\a"
60 print -n "\e]2;$title\a"
61 ;;
62 rxvt*|xterm*)
63 print -n "\e]1;$title\a"
64 print -n "\e]2;$title\a"
65 ;;
66 esac
67 }
68
69 _jj_title $(_jj_first_non_optional_arg "$@")
70 _jj_ssh "$@"