changeset 106469:ad4147368f52

Get the background mode from the terminal for xterm, and set faces accordingly. * term/xterm.el (xterm-set-background-mode): New function. (terminal-init-xterm): Use it in case xterm supports background color queries. Recompute faces after getting the background color.
author Dan Nicolaescu <dann@ics.uci.edu>
date Mon, 07 Dec 2009 06:30:30 +0000
parents 838914b06ad0
children 9e96dbbd314e
files etc/NEWS lisp/ChangeLog lisp/term/xterm.el
diffstat 3 files changed, 43 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/etc/NEWS	Mon Dec 07 06:21:27 2009 +0000
+++ b/etc/NEWS	Mon Dec 07 06:30:30 2009 +0000
@@ -339,6 +339,11 @@
 and let commands run under that user permissions.  It works even when
 `default-directory' is already remote.
 
+*** When running in a new enough xterm (newer than version 242), emacs
+asks xterm what the background color is and it sets up faces
+accordingly for a dark background if needed (the current default is to
+consider the background light).
+
 
 * New Modes and Packages in Emacs 23.2
 
--- a/lisp/ChangeLog	Mon Dec 07 06:21:27 2009 +0000
+++ b/lisp/ChangeLog	Mon Dec 07 06:30:30 2009 +0000
@@ -1,3 +1,12 @@
+2009-12-07  Dan Nicolaescu  <dann@ics.uci.edu>
+
+	Get the background mode from the terminal for xterm, and set
+	faces accordingly.
+	* term/xterm.el (xterm-set-background-mode): New function.
+	(terminal-init-xterm): Use it in case xterm supports background
+	color queries.  Recompute faces after getting the background
+	color.
+
 2009-12-07  Ulrich Mueller  <ulm@gentoo.org>
 
 	* emacs-lisp/bytecomp.el (byte-compile-insert-header): Put the version
--- a/lisp/term/xterm.el	Mon Dec 07 06:21:27 2009 +0000
+++ b/lisp/term/xterm.el	Mon Dec 07 06:30:30 2009 +0000
@@ -462,9 +462,7 @@
       (set-keymap-parent input-decode-map map)))
 
     (xterm-register-default-colors)
-    ;; This recomputes all the default faces given the colors we've just set up.
-    (tty-set-up-initial-frame-faces)
-    
+
     ;; Try to turn on the modifyOtherKeys feature on modern xterms.
     ;; When it is turned on many more key bindings work: things like
     ;; C-. C-, etc.
@@ -472,7 +470,8 @@
     ;; modifyOtherKeys. At this time only xterm does.
     (let ((coding-system-for-read 'binary)
 	  (chr nil)
-	  (str nil))
+	  (str nil)
+	  version)
       ;; Pending input can be mistakenly returned by the calls to
       ;; read-event below.  Discard it.
       (discard-input)
@@ -491,11 +490,26 @@
 	  (while (not (equal (setq chr (read-event nil nil 2)) ?c))
 	    (setq str (concat str (string chr))))
 	  (when (string-match ">0;\\([0-9]+\\);0" str)
+	    (setq version (string-to-number
+			   (substring str (match-beginning 1) (match-end 1))))
+	    ;; xterm version 242 supports reporting the background
+	    ;; color, maybe earlier versions do too...
+	    (when (>= version 242)
+	      (send-string-to-terminal "\e]11;?\e\\")
+	      (when (equal (read-event nil nil 2) ?\e)
+		(when (equal (read-event nil nil 2) ?\])
+		  (setq str "")
+		  (while (not (equal (setq chr (read-event nil nil 2)) ?\\))
+		    (setq str (concat str (string chr))))
+		  (when (string-match "11;rgb:\\([a-f0-9]+\\)/\\([a-f0-9]+\\)/\\([a-f0-9]+\\)" str)
+		    (xterm-set-background-mode
+		     (string-to-number (match-string 1 str) 16)
+		     (string-to-number (match-string 2 str) 16)
+		     (string-to-number (match-string 3 str) 16))))))
 	    ;; NUMBER2 is the xterm version number, look for something
 	    ;; greater than 216, the version when modifyOtherKeys was
 	    ;; introduced.
-	    (when (>= (string-to-number
-		       (substring str (match-beginning 1) (match-end 1))) 216)
+	    (when (>= version 216)
 	      ;; Make sure that the modifyOtherKeys state is restored when
 	      ;; suspending, resuming and exiting.
 	      (add-hook 'suspend-hook 'xterm-turn-off-modify-other-keys)
@@ -508,6 +522,9 @@
 		    xterm-modify-other-keys-terminal-list)
 	      (xterm-turn-on-modify-other-keys))))))
 
+    ;; This recomputes all the default faces given the colors we've just set up.
+    (tty-set-up-initial-frame-faces)
+
     (run-hooks 'terminal-init-xterm-hook))
 
 ;; Set up colors, for those versions of xterm that support it.
@@ -649,5 +666,11 @@
 	  (delq terminal xterm-modify-other-keys-terminal-list))
     (send-string-to-terminal "\e[>4m" terminal)))
 
+(defun xterm-set-background-mode (redc greenc bluec)
+  ;; Use the heuristic in `frame-set-background-mode' to decide if a
+  ;; frame is dark.
+  (when (< (+ redc greenc bluec) (* .6 (+ 65535 65535 65535)))
+    (set-terminal-parameter nil 'background-mode 'dark)))
+
 ;; arch-tag: 12e7ebdd-1e6c-4b25-b0f9-35ace25e855a
 ;;; xterm.el ends here