;;; elock.el --- Lock your Emacs session until you return.
;; Copyright (C) 1994 Neil Jerram <nj104@amtp.cam.ac.uk>
;;
;; Author: Neil Jerram <nj104@amtp.cam.ac.uk>
;; Created: 8 Dec 1994
;; Version: 1.0
;; Keywords: lock security
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; if not, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

;;; Commentary:
;;
;; elock allows you to lock your Emacs session, while you go elsewhere
;; for a few minutes, such that Emacs will reject all attempted input
;; (keyboard and mouse) until a given password is retyped.  elock does
;; not protect Emacs from other external influences, such as signals
;; and events received from other X clients including the window manager.
;;
;; It is strongly recommended that you try this out in a window system
;; environment (where you can take action from outside Emacs) before using
;; it on a tty or console, in case elock does not work properly for your
;; Emacs configuration (and hence could lock your terminal permanently).
;; It would also be wise to save your buffers before trying elock for the
;; first time.
;;
;; Installation:
;;   place the file `elock.el' somewhere in your load-path, and add
;;   (require 'elock)
;;   as a top level form in your `.emacs' file.  I suggest using require
;;   rather than autoload so that the complete doc string -- include
;;   an appropriate warning -- is seen the first time a user tries
;;   `C-h f elock RET.'

;;; Code:

;; Emacs 18 compatibility
(if (fboundp 'read-char-exclusive)
    (fset 'read-char-ex 'read-char-exclusive)
  (fset 'read-char-ex 'read-char))

(defun elock (password)
  "Lock your Emacs session using PASSWORD.
PASSWORD must be typed again, followed by RET, before Emacs will
accept any further input.

It is strongly recommended that you try this out in a window system
environment (where you can take action from outside Emacs) before using
it on a tty or console, in case elock does not work properly for your
Emacs configuration (and hence could lock your terminal permanently)."
  (interactive "spassword: ")
  (let ((attempt "")
	(echo-keystrokes 0)
	(inhibit-quit t)
	(success nil)
	c tmpbuf)
    (save-window-excursion
      (setq tmpbuf (generate-new-buffer " *Elock*"))
      (switch-to-buffer tmpbuf)
      (erase-buffer)
      (insert "\n\n\n\n\n"
	      "This Emacs has been temporarily locked\n"
	      "I expect to be back in a few moments\n")
      (delete-other-windows)
      (while (null success)
	(setq c (read-char-ex))
	(cond
	 ((= c ?\C-m)
	  (setq success (string= attempt password)
		attempt ""))
	 ((= c ?\C-?)
	  (if (> (length attempt) 0)
	      (setq attempt (substring attempt 0 -1))))
	 ((= c ?\C-g)
	  (setq quit-flag nil))
	 (t
	  (setq attempt (concat attempt (char-to-string c)))))
	(message "%s" attempt)))
    (kill-buffer tmpbuf))
  (message "Unlocked"))

(provide 'elock)

;;; elock.el ends here

