]> ruderich.org/simon Gitweb - config/dotfiles.git/blobdiff - shell/rc
shell/aliases: add rs alias for reset
[config/dotfiles.git] / shell / rc
index 175ed9a78095ae425dc4dc8529303c06c4b1e38c..d0261bbae3c65d61602328506034568c163c596b 100644 (file)
--- a/shell/rc
+++ b/shell/rc
@@ -1,6 +1,6 @@
 # Shell configuration file.
 
-# Copyright (C) 2013  Simon Ruderich
+# Copyright (C) 2013-2014  Simon Ruderich
 #
 # This file is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -23,3 +23,95 @@ if test "`id -u`" -eq 0; then
 else
     umask 077
 fi
+
+# Disable flow control (^s and ^q). I use GNU Screen/Tmux which also supports
+# this in a similar way (entering copy mode) and enabling flow control by
+# accident is annoying.
+if test -t 0; then
+    stty -ixon -ixoff
+fi
+
+# Auto-logout after timeout on TTYs. It's easy to forget to logout after
+# switching back to X11 from a TTY.
+timeout_setup_screen_lock() {
+    echo "Locking GNU screen after $timeout seconds (TTY detected)."
+    screen -X idle $timeout lockscreen
+    echo
+}
+timeout_setup_tmux_lock() {
+    if type vlock >/dev/null; then
+        echo "Locking Tmux after $timeout seconds (TTY detected)."
+        tmux set-option lock-after-time $timeout
+        echo
+
+    else
+        echo "vlock not found, locking won't work in Tmux!" >&2
+        echo "Falling back to shell timeout." >&2
+        echo
+
+        timeout_setup_shell_logout
+    fi
+}
+timeout_setup_shell_logout() {
+    echo "Auto-logout after $timeout seconds (TTY detected)."
+    TMOUT=$timeout
+    echo
+}
+# Timeout in seconds.
+local timeout >/dev/null 2>&1
+timeout=600
+case "$TERM" in
+    # `tty` doesn't work inside a pseudo-terminal as provided by GNU
+    # Screen/Tmux.
+    #
+    # The following settings for GNU Screen/Tmux will only auto-lock new
+    # sessions, not when attaching to existing sessions!
+    screen*)
+        if test -n "$STY"; then
+            case "$STY" in
+                *.tty[0-9]*.*)
+                    timeout_setup_screen_lock
+                    ;;
+            esac
+        elif test -n "$TMUX"; then
+            case "`tmux display -p '#{client_tty}'`" in
+                /dev/tty[0-9]*)
+                    timeout_setup_tmux_lock
+                    ;;
+                /dev/*)
+                    # Not on TTY, nothing to do.
+                    ;;
+                *)
+                    echo 'Tmux < 1.8 found, TTY check might be incorrect!' >&2
+
+                    local client >/dev/null 2>&1
+                    client=`printf '%s' "$TMUX" | sed 's/^.*,\([0-9]*\)$/\1/'`
+
+                    case "`tmux list-clients \
+                            | grep -E "^/dev/.*?: $client "`" in
+                        /dev/tty[0-9]*)
+                            timeout_setup_tmux_lock
+                            ;;
+                    esac
+                    ;;
+            esac
+        else
+            echo 'TERM=screen but neither $STY nor $TMUX found!' >&2
+            timeout_setup_shell_logout
+        fi
+        ;;
+
+    *)
+        case `tty` in
+            /dev/tty[0-9]*)
+                timeout_setup_shell_logout
+                ;;
+        esac
+        ;;
+esac
+unset timeout
+unset timeout_setup_screen_lock
+unset timeout_setup_tmux_lock
+unset timeout_setup_screen_lock
+
+# vim: ft=sh