(or (lookup-key function-key-map "\e[")
(define-key function-key-map "\e[" (make-keymap)))
(mapcar (function (lambda (key-binding)
(define-key function-key-map
(car key-binding) (nth 1 key-binding))))
'(
("\e[H" [home])
("\e[Z" [backtab])
("\e[i" [print])
("\e[@" [insert])
("\e[L" [insertline])
("\e[M" [deleteline])
("\e[U" [next])
("\eOm" [kp-subtract])
("\eOl" [kp-separator])
("\eOn" [kp-decimal])
("\eOM" [kp-enter])
("\e[K" [key_eol]) ("\e[J" [key_eos]) ("\e[2J" [key_clear]) ("\e[P" [key_dc]) ("\e[g" [S-tab]) ("\e[2N" [clearentry]) ("\e[2K" [S-clearentry]) ("\e[E" [?\C-j]) ("\e[g" [S-backtab]) ("\e[?1i" [key_sprint]) ("\e[4h" [key_sic]) ("\e[4l" [S-delete]) ("\e[Q" [S-insertline]) ("\e[1Q" [key_sdl]) ("\e[19l" [key_seol]) ("\e[19h" [S-erasepage]) ("\e[V" [S-page]) ("\eS" [send]) ("\e5" [S-send]) ))
(let ((i 0))
(while (< i 10)
(define-key function-key-map
(format "\eO%c" (+ i ?p))
(vector (intern (format "kp-%d" i))))
(setq i (1+ i))))
(let ((i 0))
(while (< i 16)
(define-key function-key-map
(format "\e?%c" (+ i ?a))
(vector (intern (format "f%d" (1+ i)))))
(define-key function-key-map
(format "\e?%c" (+ i ?A))
(vector (intern (format "S-f%d" (1+ i)))))
(setq i (1+ i))))
(defvar tvi970-keypad-numeric nil
"The terminal should be in numeric keypad mode iff this variable is non-nil.
Do not set this variable! Call the function ``tvi970-set-keypad-mode''.")
(defun tvi970-set-keypad-mode (&optional arg)
"Set the current mode of the TVI 970 numeric keypad.
In ``numeric keypad mode'', the number keys on the keypad act as
ordinary digits. In ``alternate keypad mode'', the keys send distinct
escape sequences, meaning that they can have their own bindings,
independent of the normal number keys.
With no argument, toggle between the two possible modes.
With a positive argument, select alternate keypad mode.
With a negative argument, select numeric keypad mode."
(interactive "P")
(setq tvi970-keypad-numeric
(if (null arg)
(not tvi970-keypad-numeric)
(> (prefix-numeric-value arg) 0)))
(send-string-to-terminal (if tvi970-keypad-numeric "\e=" "\e>")))
(tvi970-set-keypad-mode 1)