Add 2011 to FSF/AIST copyright years.
[bpt/emacs.git] / lisp / term / xterm.el
index 96681cf..aacee8d 100644 (file)
@@ -1,17 +1,17 @@
 ;;; xterm.el --- define function key sequences and standard colors for xterm
 
 ;; Copyright (C) 1995, 2001, 2002, 2003, 2004, 2005,
-;;   2006, 2007, 2008 Free Software Foundation, Inc.
+;;   2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
 
 ;; Author: FSF
 ;; Keywords: terminals
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs 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 3, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -19,9 +19,7 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
       (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.
     ;; modifyOtherKeys. At this time only xterm does.
     (let ((coding-system-for-read 'binary)
          (chr nil)
-         (str nil))
+         (str nil)
+         (recompute-faces nil)
+         version)
+      ;; Pending input can be mistakenly returned by the calls to
+      ;; read-event below.  Discard it.
+      (discard-input)
       ;; Try to find out the type of terminal by sending a "Secondary
       ;; Device Attributes (DA)" query.
       (send-string-to-terminal "\e[>0c")
          (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)
+                   (setq recompute-faces
+                         (xterm-maybe-set-dark-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)
              ;; need to deal with modify-other-keys.
              (push (frame-terminal (selected-frame))
                    xterm-modify-other-keys-terminal-list)
-             (xterm-turn-on-modify-other-keys))))))
+             (xterm-turn-on-modify-other-keys))
+
+           ;; Recompute faces here in case the background mode was
+           ;; set to dark.  We used to call
+           ;; `tty-set-up-initial-frame-faces' only once, but that
+           ;; caused the light background faces to be computed
+           ;; incorrectly.  See:
+           ;; http://permalink.gmane.org/gmane.emacs.devel/119627
+           (when recompute-faces
+             (tty-set-up-initial-frame-faces))))))
 
     (run-hooks 'terminal-init-xterm-hook))
 
@@ -626,27 +653,34 @@ versions of xterm."
     (clear-face-cache)))
 
 (defun xterm-turn-on-modify-other-keys ()
-  "Turn on the modifyOtherKeys feature of xterm."
+  "Turn the modifyOtherKeys feature of xterm back on."
   (let ((terminal (frame-terminal (selected-frame))))
     (when (and (terminal-live-p terminal)
               (memq terminal xterm-modify-other-keys-terminal-list))
-      (send-string-to-terminal "\e[>4;1m"))))
+      (send-string-to-terminal "\e[>4;1m" terminal))))
 
 (defun xterm-turn-off-modify-other-keys (&optional frame)
-  "Turn off the modifyOtherKeys feature of xterm."
+  "Temporarily turn off the modifyOtherKeys feature of xterm."
   (let ((terminal (when frame (frame-terminal frame))))
-    (when (and (frame-live-p terminal)
+    (when (and (terminal-live-p terminal)
                (memq terminal xterm-modify-other-keys-terminal-list))
-      (send-string-to-terminal "\e[>4m"))))
+      (send-string-to-terminal "\e[>4m" terminal))))
 
 (defun xterm-remove-modify-other-keys (&optional terminal)
-  "Turn off the modifyOtherKeys feature of xterm and remove frame from consideration."
-  (setq terminal (and terminal (frame-terminal (selected-frame))))
+  "Turn off the modifyOtherKeys feature of xterm for good."
+  (setq terminal (or terminal (frame-terminal (selected-frame))))
   (when (and (terminal-live-p terminal)
             (memq terminal xterm-modify-other-keys-terminal-list))
     (setq xterm-modify-other-keys-terminal-list
          (delq terminal xterm-modify-other-keys-terminal-list))
-    (send-string-to-terminal "\e[>4m")))
+    (send-string-to-terminal "\e[>4m" terminal)))
+
+(defun xterm-maybe-set-dark-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)
+    t))
 
 ;; arch-tag: 12e7ebdd-1e6c-4b25-b0f9-35ace25e855a
 ;;; xterm.el ends here