(defun terminal-init-rxvt ()
"Terminal initialization function for rxvt."
(substitute-key-definition [f11] [S-f1] function-key-map)
(substitute-key-definition [f12] [S-f2] function-key-map)
(substitute-key-definition [f13] [S-f3] function-key-map)
(substitute-key-definition [f14] [S-f4] function-key-map)
(substitute-key-definition [f15] [S-f5] function-key-map)
(substitute-key-definition [f16] [S-f6] function-key-map)
(substitute-key-definition [f17] [S-f7] function-key-map)
(substitute-key-definition [f18] [S-f8] function-key-map)
(substitute-key-definition [f19] [S-f9] function-key-map)
(substitute-key-definition [f20] [S-f10] function-key-map)
(substitute-key-definition [f23] [C-f1] function-key-map)
(substitute-key-definition [f24] [C-f2] function-key-map)
(substitute-key-definition [f25] [C-f3] function-key-map)
(substitute-key-definition [f26] [C-f4] function-key-map)
(substitute-key-definition [f27] [C-f5] function-key-map)
(substitute-key-definition [f28] [C-f6] function-key-map)
(substitute-key-definition [f29] [C-f7] function-key-map)
(substitute-key-definition [f30] [C-f8] function-key-map)
(substitute-key-definition [f31] [C-f9] function-key-map)
(substitute-key-definition [f32] [C-f10] function-key-map)
(substitute-key-definition [f33] [C-S-f1] function-key-map)
(substitute-key-definition [f34] [C-S-f2] function-key-map)
(substitute-key-definition [f35] [C-S-f3] function-key-map)
(substitute-key-definition [f36] [C-S-f4] function-key-map)
(substitute-key-definition [f37] [C-S-f5] function-key-map)
(substitute-key-definition [f38] [C-S-f6] function-key-map)
(substitute-key-definition [f39] [C-S-f7] function-key-map)
(substitute-key-definition [f40] [C-S-f8] function-key-map)
(substitute-key-definition [f41] [C-S-f9] function-key-map)
(substitute-key-definition [f42] [C-S-f10] function-key-map)
(let ((map (make-sparse-keymap)))
(define-key map "\e[A" [up])
(define-key map "\e[B" [down])
(define-key map "\e[C" [right])
(define-key map "\e[D" [left])
(define-key map "\e[2~" [insert])
(define-key map "\e[3~" [delete])
(define-key map "\e[4~" [select])
(define-key map "\e[5~" [prior])
(define-key map "\e[6~" [next])
(define-key map "\e[7~" [home])
(define-key map "\e[8~" [end])
(define-key map "\e[11~" [f1])
(define-key map "\e[12~" [f2])
(define-key map "\e[13~" [f3])
(define-key map "\e[14~" [f4])
(define-key map "\e[15~" [f5])
(define-key map "\e[17~" [f6])
(define-key map "\e[18~" [f7])
(define-key map "\e[19~" [f8])
(define-key map "\e[20~" [f9])
(define-key map "\e[21~" [f10])
(define-key map "\e[29~" [print])
(define-key map "\e[11^" [C-f1])
(define-key map "\e[12^" [C-f2])
(define-key map "\e[13^" [C-f3])
(define-key map "\e[14^" [C-f4])
(define-key map "\e[15^" [C-f5])
(define-key map "\e[17^" [C-f6])
(define-key map "\e[18^" [C-f7])
(define-key map "\e[19^" [C-f8])
(define-key map "\e[20^" [C-f9])
(define-key map "\e[21^" [C-f10])
(define-key map "\e[23~" [S-f1])
(define-key map "\e[24~" [S-f2])
(define-key map "\e[25~" [S-f3])
(define-key map "\e[26~" [S-f4])
(define-key map "\e[28~" [S-f5])
(define-key map "\e[29~" [S-f6])
(define-key map "\e[31~" [S-f7])
(define-key map "\e[32~" [S-f8])
(define-key map "\e[33~" [S-f9])
(define-key map "\e[34~" [S-f10])
(define-key map "\e[23^" [C-S-f1])
(define-key map "\e[24^" [C-S-f2])
(define-key map "\e[25^" [C-S-f3])
(define-key map "\e[26^" [C-S-f4])
(define-key map "\e[28^" [C-S-f5])
(define-key map "\e[29^" [C-S-f6])
(define-key map "\e[31^" [C-S-f7])
(define-key map "\e[32^" [C-S-f8])
(define-key map "\e[33^" [C-S-f9])
(define-key map "\e[34^" [C-S-f10])
(define-key map "\e[2^" [C-insert])
(define-key map "\e[3^" [C-delete])
(define-key map "\e[5^" [C-prior])
(define-key map "\e[6^" [C-next])
(define-key map "\e[7^" [C-home])
(define-key map "\e[8^" [C-end])
(define-key map "\eOd" [C-left])
(define-key map "\eOc" [C-right])
(define-key map "\eOa" [C-up])
(define-key map "\eOb" [C-down])
(define-key map "\e[2;2~" [S-insert])
(define-key map "\e[3$" [S-delete])
(define-key map "\e[5$" [S-prior])
(define-key map "\e[6$" [S-next])
(define-key map "\e[7$" [S-home])
(define-key map "\e[8$" [S-end])
(define-key map "\e[d" [S-left])
(define-key map "\e[c" [S-right])
(define-key map "\e[a" [S-up])
(define-key map "\e[b" [S-down])
(set-keymap-parent map (keymap-parent function-key-map))
(set-keymap-parent function-key-map map))
(rxvt-register-default-colors)
(rxvt-set-background-mode)
(tty-set-up-initial-frame-faces))
(defvar rxvt-standard-colors
'(("black" 0 ( 0 0 0)) ("red" 1 (205 0 0)) ("green" 2 ( 0 205 0)) ("yellow" 3 (205 205 0)) ("blue" 4 ( 0 0 205)) ("magenta" 5 (205 0 205)) ("cyan" 6 ( 0 205 205)) ("white" 7 (229 229 229)) ("brightblack" 8 ( 77 77 77)) ("brightred" 9 (255 0 0)) ("brightgreen" 10 ( 0 255 0)) ("brightyellow" 11 (255 255 0)) ("brightblue" 12 ( 0 0 255)) ("brightmagenta" 13 (255 0 255)) ("brightcyan" 14 ( 0 255 255)) ("brightwhite" 15 (255 255 255))) "Names of 16 standard rxvt colors, their numbers, and RGB values.")
(defun rxvt-rgb-convert-to-16bit (prim)
"Convert an 8-bit primary color value PRIM to a corresponding 16-bit value."
(min 65535 (round (* (/ prim 255.0) 65535.0))))
(defun rxvt-register-default-colors ()
"Register the default set of colors for rxvt or compatible emulator.
This function registers the number of colors returned by `display-color-cells'
for the currently selected frame."
(let* ((ncolors (display-color-cells))
(colors rxvt-standard-colors)
(color (car colors)))
(if (> ncolors 0)
(tty-color-clear))
(while (and (> ncolors 0) colors)
(tty-color-define (car color) (cadr color)
(mapcar 'rxvt-rgb-convert-to-16bit
(car (cddr color))))
(setq colors (cdr colors)
color (car colors)
ncolors (1- ncolors)))
(when (> ncolors 0)
(cond
((= ncolors 240) (let ((r 0) (g 0) (b 0))
(while (> ncolors 24)
(tty-color-define (format "color-%d" (- 256 ncolors))
(- 256 ncolors)
(mapcar 'rxvt-rgb-convert-to-16bit
(list (round (* r 42.5))
(round (* g 42.5))
(round (* b 42.5)))))
(setq b (1+ b))
(if (> b 5)
(setq g (1+ g)
b 0))
(if (> g 5)
(setq r (1+ r)
g 0))
(setq ncolors (1- ncolors))))
(while (> ncolors 0)
(setq color (rxvt-rgb-convert-to-16bit (+ 8 (* (- 24 ncolors) 10))))
(tty-color-define (format "color-%d" (- 256 ncolors))
(- 256 ncolors)
(list color color color))
(setq ncolors (1- ncolors))))
((= ncolors 72) (let ((levels '(0 139 205 255))
(r 0) (g 0) (b 0))
(while (> ncolors 8)
(tty-color-define (format "color-%d" (- 88 ncolors))
(- 88 ncolors)
(mapcar 'rxvt-rgb-convert-to-16bit
(list (nth r levels)
(nth g levels)
(nth b levels))))
(setq b (1+ b))
(if (> b 3)
(setq g (1+ g)
b 0))
(if (> g 3)
(setq r (1+ r)
g 0))
(setq ncolors (1- ncolors))))
(while (> ncolors 0)
(setq color (rxvt-rgb-convert-to-16bit
(floor
(if (= ncolors 8)
46.36363636
(+ (* (- 8 ncolors) 23.18181818) 69.54545454)))))
(tty-color-define (format "color-%d" (- 88 ncolors))
(- 88 ncolors)
(list color color color))
(setq ncolors (1- ncolors))))
(t (error "Unsupported number of rxvt colors (%d)" (+ 16 ncolors)))))
(clear-face-cache)))
(defun rxvt-set-background-mode ()
"Set background mode as appropriate for the default rxvt colors."
(let ((fgbg (getenv "COLORFGBG"))
bg rgb)
(setq default-frame-background-mode 'light)
(when (and fgbg
(string-match ".*;\\([0-9][0-9]?\\)\\'" fgbg))
(setq bg (string-to-number (substring fgbg (match-beginning 1))))
(setq rgb (car (cddr (nth bg rxvt-standard-colors))))
(if (< (apply '+ rgb)
(* (apply '+ (car (cddr (nth 15 rxvt-standard-colors)))) 0.6))
(setq default-frame-background-mode 'dark)))
(frame-set-background-mode (selected-frame))))