;;; console.el --- define key sequences for Linux console

;; Author: Robert Sanders, gt8134b@prism.gatech.edu
;; Keywords: terminals, Linux, console
;; $Date: 1993/10/14 17:08:20 $
;; $Source: /usr/local/lib/emacs/site-lisp/RCS/console.el,v $
;; $Revision: 1.4 $
;; $State: Exp $
;;

;;; Commentary:

;; Uses the Emacs 19 terminal initialization features --- won't work 
;; with Emacs 18

;; specifically modified for the Linux console as of version 0.99pl10
;; ...overrides the termcap defaults

;;; Code:

;;; CSI sequences - those that start with "\e[".
;; Termcap or terminfo should set some of these up automatically
;; however, as no distribution seems to have the termcap set up right,
;; we'll define the keys regardless...

;; you can comment out any of these to get the termcap
;; defaults

(defun set-fkey-map-list (pairlist) "Set function keymap from a list.
The list format is ( (string key) ... )"
  (if pairlist
      (let ((pair (car pairlist)))
	(define-key function-key-map (car pair) (car (cdr pair)))
	(set-fkey-map-list (cdr pairlist)))))

(set-fkey-map-list '( ("\e[A" [f1])
		      ("\e[B" [down])
		      ("\e[A" [up])
		      ("\e[B" [down])
		      ("\e[C" [right])
		      ("\e[D" [left])
		      ("\e[5~" [prior])
		      ("\e[6~" [next])
		      ("\e[2~" [insert])
		      ("\e[1~" [home])
		      ("\e[4~" [end])
		      ("\e[3~" [delete])
		      ("\e[[A" [f1])
		      ("\e[[B" [f2])
		      ("\e[[C" [f3])
		      ("\e[[D" [f4])
		      ("\e[[E" [f5])
		      ("\e[17~" [f6])
		      ("\e[18~" [f7])
		      ("\e[19~" [f8])
		      ("\e[20~" [f9])
		      ("\e[21~" [f10])
		      ("\e[23~" [f11])
		      ("\e[24~" [f12])))

(define-key function-key-map "\C-\\" [print])   ; PrintScreen key

;;; SS3 sequences - those that start with "\eO".

;; Terminfo might set these
(defun linux-numlock-off ()
  "Set Linux console keypad to simulated NumLock-off mode"
  (interactive)
  (setq mode-line-numlock-string "")
  (force-mode-line-update)
  (set-fkey-map-list '( ("\eOp" [kp-0]) ; kp0
			("\eOq" [kp-1]) ; kp1
			("\eOr" [kp-2]) ; kp2
			("\eOs" [kp-3]) ; kp3
			("\eOt" [kp-4]) ; kp4
			("\eOu" [kp-5]) ; kp5
			("\eOv" [kp-6]) ; kp6
			("\eOw" [kp-7]) ; kp7
			("\eOx" [kp-8]) ; kp8
			("\eOy" [kp-9]) ; kp9
			("\eOl" [kp-add])
			("\eOS" [kp-subtract])
			("\eOR" [kp-multiply])
			("\eOQ" [kp-divide])
			("\eOM" [kp-enter])
			("\eOn" [kp-decimal]))))
		     
(defun linux-numlock-on () 
  "Set Linux console keypad to simulated NumLock-on mode"
  (interactive)
  (setq mode-line-numlock-string numlock-string)
  (force-mode-line-update)
  (set-fkey-map-list '( ("\eOp" [?0]) ; 0
		       ("\eOq" [?1]) ; 1
		       ("\eOr" [?2]) ; 2
		       ("\eOs" [?3]) ; 3
		       ("\eOt" [?4]) ; 4
		       ("\eOu" [?5]) ; 5
		       ("\eOv" [?6]) ; 6
		       ("\eOw" [?7]) ; 7
		       ("\eOx" [?8]) ; 8
		       ("\eOy" [?9]) ; 9
		       ("\eOl" [?+])
		       ("\eOS" [?-])
		       ("\eOR" [?*])
		       ("\eOQ" [?/])
		       ("\eOM" [?\C-j])
		       ("\eOn" [?.]))))

;; these are the strings the Linux console keypad transmits when
;; in application mode.  Emacs manually turns keypad mode on
;; at the bottom of this file.

(defvar numlock-string "NumLock"
  "String which the mode-line should display if Linux console simulated
NumLock is on")
(defvar linux-console-numlock nil "State of simulated keypad NumLock")

(defvar mode-line-numlock-string "")
(defun do-linux-console-numlock () "Handle the keypad NumLock key."
  (interactive)
  (if (setq linux-console-numlock (not linux-console-numlock))
      (linux-numlock-on)
    (linux-numlock-off)))

;; trim the last element off the end, which should be the "-%-" 
;; infinite row of dashes format, and add the numlock string
(setcdr (nthcdr (- (length mode-line-format) 2) mode-line-format) 
	'("---" mode-line-numlock-string "-%-"))
(define-key function-key-map "\eOP" [kp-numlock])

;; put keypad into application mode so we can differentiate between
;; keypad keys and normal keys...we use the functions 
;; linux-numlock-{on,off} to simulate the keypad functionality.
;; we can fudge the mode-line, but we still can't change the LED :-(

(global-set-key [kp-numlock] 'do-linux-console-numlock)
(linux-numlock-off)
(send-string-to-terminal "\e=")

;;; console.el ends here
