(goto-address-url-face): Change default value from `bold' to `link'.
[bpt/emacs.git] / lisp / term / xterm.el
index 4c459a4..0fcccc3 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 Free Software Foundation, Inc.
+;;   2006, 2007, 2008, 2009 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:
 
     (define-key map "\e[23;6~" [C-S-f11])
     (define-key map "\e[24;6~" [C-S-f12])
 
-    (define-key map "\eO3P" [A-f1])
-    (define-key map "\eO3Q" [A-f2])
-    (define-key map "\eO3R" [A-f3])
-    (define-key map "\eO3S" [A-f4])
-    (define-key map "\e[15;3~" [A-f5])
-    (define-key map "\e[17;3~" [A-f6])
-    (define-key map "\e[18;3~" [A-f7])
-    (define-key map "\e[19;3~" [A-f8])
-    (define-key map "\e[20;3~" [A-f9])
-    (define-key map "\e[21;3~" [A-f10])
-    (define-key map "\e[23;3~" [A-f11])
-    (define-key map "\e[24;3~" [A-f12])
+    (define-key map "\eO3P" [M-f1])
+    (define-key map "\eO3Q" [M-f2])
+    (define-key map "\eO3R" [M-f3])
+    (define-key map "\eO3S" [M-f4])
+    (define-key map "\e[15;3~" [M-f5])
+    (define-key map "\e[17;3~" [M-f6])
+    (define-key map "\e[18;3~" [M-f7])
+    (define-key map "\e[19;3~" [M-f8])
+    (define-key map "\e[20;3~" [M-f9])
+    (define-key map "\e[21;3~" [M-f10])
+    (define-key map "\e[23;3~" [M-f11])
+    (define-key map "\e[24;3~" [M-f12])
 
     (define-key map "\eO4P" [M-S-f1])
     (define-key map "\eO4Q" [M-S-f2])
     (define-key map "\e[1;8F" [C-M-S-end])
     (define-key map "\e[1;8H" [C-M-S-home])
 
-    (define-key map "\e[1;3A" [A-up])
-    (define-key map "\e[1;3B" [A-down])
-    (define-key map "\e[1;3C" [A-right])
-    (define-key map "\e[1;3D" [A-left])
-    (define-key map "\e[1;3F" [A-end])
-    (define-key map "\e[1;3H" [A-home])
+    (define-key map "\e[1;3A" [M-up])
+    (define-key map "\e[1;3B" [M-down])
+    (define-key map "\e[1;3C" [M-right])
+    (define-key map "\e[1;3D" [M-left])
+    (define-key map "\e[1;3F" [M-end])
+    (define-key map "\e[1;3H" [M-home])
 
     (define-key map "\e[2~" [insert])
     (define-key map "\e[3~" [delete])
     (define-key map "\e[5;8~" [C-M-S-prior])
     (define-key map "\e[6;8~" [C-M-S-next])
 
-    (define-key map "\e[2;3~" [A-insert])
-    (define-key map "\e[3;3~" [A-delete])
-    (define-key map "\e[5;3~" [A-prior])
-    (define-key map "\e[6;3~" [A-next])
+    (define-key map "\e[2;3~" [M-insert])
+    (define-key map "\e[3;3~" [M-delete])
+    (define-key map "\e[5;3~" [M-prior])
+    (define-key map "\e[6;3~" [M-next])
 
     (define-key map "\e[4~" [select])
     (define-key map "\e[29~" [print])
     (define-key map "\e[D" [left])
     (define-key map "\e[1~" [home])
 
-    (define-key map "\e[1;2A" [S-up])
-    (define-key map "\e[1;2B" [S-down])
-    (define-key map "\e[1;2C" [S-right])
-    (define-key map "\e[1;2D" [S-left])
-    (define-key map "\e[1;2F" [S-end])
-    (define-key map "\e[1;2H" [S-home])
+    (define-key map "\eO2A" [S-up])
+    (define-key map "\eO2B" [S-down])
+    (define-key map "\eO2C" [S-right])
+    (define-key map "\eO2D" [S-left])
+    (define-key map "\eO2F" [S-end])
+    (define-key map "\eO2H" [S-home])
 
-    (define-key map "\e[1;5A" [C-up])
-    (define-key map "\e[1;5B" [C-down])
-    (define-key map "\e[1;5C" [C-right])
-    (define-key map "\e[1;5D" [C-left])
-    (define-key map "\e[1;5F" [C-end])
-    (define-key map "\e[1;5H" [C-home])
+    (define-key map "\eO5A" [C-up])
+    (define-key map "\eO5B" [C-down])
+    (define-key map "\eO5C" [C-right])
+    (define-key map "\eO5D" [C-left])
+    (define-key map "\eO5F" [C-end])
+    (define-key map "\eO5H" [C-home])
 
     (define-key map "\e[11~" [f1])
     (define-key map "\e[12~" [f2])
     map)
   "Function key map overrides for xterm.")
 
+(defvar xterm-alternatives-map
+  (let ((map (make-sparse-keymap)))
+    ;; The terminal initialization C code file might have initialized
+    ;; function keys F13->F60 from the termcap/terminfo information.
+    ;; On a PC-style keyboard these keys correspond to
+    ;; MODIFIER-FUNCTION_KEY, where modifier is S-, C, A-, C-S-.  The code
+    ;; here substitutes the corresponding definitions in function-key-map.
+    ;; The mapping from escape sequences to Fn is done in input-decode-map
+    ;; whereas this here mapping is done in local-function-key-map so that
+    ;; bindings to f45 still work, in case your keyboard really has an f45
+    ;; key rather than C-S-f9.
+    (define-key map [f13] [S-f1])
+    (define-key map [f14] [S-f2])
+    (define-key map [f15] [S-f3])
+    (define-key map [f16] [S-f4])
+    (define-key map [f17] [S-f5])
+    (define-key map [f18] [S-f6])
+    (define-key map [f19] [S-f7])
+    (define-key map [f20] [S-f8])
+    (define-key map [f21] [S-f9])
+    (define-key map [f22] [S-f10])
+    (define-key map [f23] [S-f11])
+    (define-key map [f24] [S-f12])
+
+    (define-key map [f25] [C-f1])
+    (define-key map [f26] [C-f2])
+    (define-key map [f27] [C-f3])
+    (define-key map [f28] [C-f4])
+    (define-key map [f29] [C-f5])
+    (define-key map [f30] [C-f6])
+    (define-key map [f31] [C-f7])
+    (define-key map [f32] [C-f8])
+    (define-key map [f33] [C-f9])
+    (define-key map [f34] [C-f10])
+    (define-key map [f35] [C-f11])
+    (define-key map [f36] [C-f12])
+
+    (define-key map [f37] [C-S-f1])
+    (define-key map [f38] [C-S-f2])
+    (define-key map [f39] [C-S-f3])
+    (define-key map [f40] [C-S-f4])
+    (define-key map [f41] [C-S-f5])
+    (define-key map [f42] [C-S-f6])
+    (define-key map [f43] [C-S-f7])
+    (define-key map [f44] [C-S-f8])
+    (define-key map [f45] [C-S-f9])
+    (define-key map [f46] [C-S-f10])
+    (define-key map [f47] [C-S-f11])
+    (define-key map [f48] [C-S-f12])
+
+    (define-key map [f49] [M-f1])
+    (define-key map [f50] [M-f2])
+    (define-key map [f51] [M-f3])
+    (define-key map [f52] [M-f4])
+    (define-key map [f53] [M-f5])
+    (define-key map [f54] [M-f6])
+    (define-key map [f55] [M-f7])
+    (define-key map [f56] [M-f8])
+    (define-key map [f57] [M-f9])
+    (define-key map [f58] [M-f10])
+    (define-key map [f59] [M-f11])
+    (define-key map [f60] [M-f12])
+
+    map)
+  "Keymap of possible alternative meanings for some keys.")
+
 ;; List of terminals for which modify-other-keys has been turned on.
 (defvar xterm-modify-other-keys-terminal-list nil)
 
           (string-match "\\`rxvt" (getenv "COLORTERM" (selected-frame))))
       (tty-run-terminal-initialization (selected-frame) "rxvt")
 
-    ;; The terminal initialization C code file might have initialized
-    ;; function keys F13->F60 from the termcap/terminfo information.
-    ;; On a PC-style keyboard these keys correspond to
-    ;; MODIFIER-FUNCTION_KEY, where modifier is S-, C, A-, C-S-.  The code
-    ;; here substitutes the corresponding definitions in function-key-map.
-    ;; This substitution is needed because if a key definition is found in
-    ;; function-key-map, there are no further lookups in other keymaps.
-    (substitute-key-definition [f13] [S-f1] local-function-key-map)
-    (substitute-key-definition [f14] [S-f2] local-function-key-map)
-    (substitute-key-definition [f15] [S-f3] local-function-key-map)
-    (substitute-key-definition [f16] [S-f4] local-function-key-map)
-    (substitute-key-definition [f17] [S-f5] local-function-key-map)
-    (substitute-key-definition [f18] [S-f6] local-function-key-map)
-    (substitute-key-definition [f19] [S-f7] local-function-key-map)
-    (substitute-key-definition [f20] [S-f8] local-function-key-map)
-    (substitute-key-definition [f21] [S-f9] local-function-key-map)
-    (substitute-key-definition [f22] [S-f10] local-function-key-map)
-    (substitute-key-definition [f23] [S-f11] local-function-key-map)
-    (substitute-key-definition [f24] [S-f12] local-function-key-map)
-
-    (substitute-key-definition [f25] [C-f1] local-function-key-map)
-    (substitute-key-definition [f26] [C-f2] local-function-key-map)
-    (substitute-key-definition [f27] [C-f3] local-function-key-map)
-    (substitute-key-definition [f28] [C-f4] local-function-key-map)
-    (substitute-key-definition [f29] [C-f5] local-function-key-map)
-    (substitute-key-definition [f30] [C-f6] local-function-key-map)
-    (substitute-key-definition [f31] [C-f7] local-function-key-map)
-    (substitute-key-definition [f32] [C-f8] local-function-key-map)
-    (substitute-key-definition [f33] [C-f9] local-function-key-map)
-    (substitute-key-definition [f34] [C-f10] local-function-key-map)
-    (substitute-key-definition [f35] [C-f11] local-function-key-map)
-    (substitute-key-definition [f36] [C-f12] local-function-key-map)
-
-    (substitute-key-definition [f37] [C-S-f1] local-function-key-map)
-    (substitute-key-definition [f38] [C-S-f2] local-function-key-map)
-    (substitute-key-definition [f39] [C-S-f3] local-function-key-map)
-    (substitute-key-definition [f40] [C-S-f4] local-function-key-map)
-    (substitute-key-definition [f41] [C-S-f5] local-function-key-map)
-    (substitute-key-definition [f42] [C-S-f6] local-function-key-map)
-    (substitute-key-definition [f43] [C-S-f7] local-function-key-map)
-    (substitute-key-definition [f44] [C-S-f8] local-function-key-map)
-    (substitute-key-definition [f45] [C-S-f9] local-function-key-map)
-    (substitute-key-definition [f46] [C-S-f10] local-function-key-map)
-    (substitute-key-definition [f47] [C-S-f11] local-function-key-map)
-    (substitute-key-definition [f48] [C-S-f12] local-function-key-map)
-
-    (substitute-key-definition [f49] [A-f1] local-function-key-map)
-    (substitute-key-definition [f50] [A-f2] local-function-key-map)
-    (substitute-key-definition [f51] [A-f3] local-function-key-map)
-    (substitute-key-definition [f52] [A-f4] local-function-key-map)
-    (substitute-key-definition [f53] [A-f5] local-function-key-map)
-    (substitute-key-definition [f54] [A-f6] local-function-key-map)
-    (substitute-key-definition [f55] [A-f7] local-function-key-map)
-    (substitute-key-definition [f56] [A-f8] local-function-key-map)
-    (substitute-key-definition [f57] [A-f9] local-function-key-map)
-    (substitute-key-definition [f58] [A-f10] local-function-key-map)
-    (substitute-key-definition [f59] [A-f11] local-function-key-map)
-    (substitute-key-definition [f60] [A-f12] local-function-key-map)
+    (let ((map (copy-keymap xterm-alternatives-map)))
+      (set-keymap-parent map (keymap-parent local-function-key-map))
+      (set-keymap-parent local-function-key-map map))
 
     (let ((map (copy-keymap xterm-function-map)))
 
       ;; Use inheritance to let the main keymap override those defaults.
       ;; This way we don't override terminfo-derived settings or settings
       ;; made in the .emacs file.
-      (set-keymap-parent map (keymap-parent local-function-key-map))
-      (set-keymap-parent local-function-key-map map)))
+      (set-keymap-parent map (keymap-parent input-decode-map))
+      (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 much more key bindings work: things like
+    ;; When it is turned on many more key bindings work: things like
     ;; C-. C-, etc.
     ;; To do that we need to find out if the current terminal supports
     ;; modifyOtherKeys. At this time only xterm does.
     (let ((coding-system-for-read 'binary)
          (chr nil)
          (str nil))
+      ;; 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")
 
       ;; The reply should be of the form: \e [ > NUMBER1 ; NUMBER2 ; NUMBER3 c
-      (when (equal (read-event nil nil 0.1) ?\e)
-       (when (equal (read-event nil nil 0.1) ?\[)
-         (while (not (equal (setq chr (read-event nil nil 0.1)) ?c))
+      ;; If the timeout is completely removed for read-event, this
+      ;; might hang for terminals that pretend to be xterm, but don't
+      ;; respond to this escape sequence.  RMS' opinion was to remove
+      ;; it completely.  That might be right, but let's first try to
+      ;; see if by using a longer timeout we get rid of most issues.
+      (when (equal (read-event nil nil 2) ?\e)
+       (when (equal (read-event nil nil 2) ?\[)
+         (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)
            ;; NUMBER2 is the xterm version number, look for something
              (add-hook 'suspend-hook 'xterm-turn-off-modify-other-keys)
              (add-hook 'suspend-resume-hook 'xterm-turn-on-modify-other-keys)
              (add-hook 'kill-emacs-hook 'xterm-remove-modify-other-keys)
-             (add-hook 'delete-frame-hook 'xterm-remove-modify-other-keys)
+             (add-hook 'delete-terminal-functions 'xterm-remove-modify-other-keys)
              ;; Add the selected frame to the list of frames that
              ;; 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))))))
+
+    (run-hooks 'terminal-init-xterm-hook))
 
 ;; Set up colors, for those versions of xterm that support it.
 (defvar xterm-standard-colors
@@ -608,28 +627,27 @@ versions of xterm."
     (clear-face-cache)))
 
 (defun xterm-turn-on-modify-other-keys ()
-  "Turn on the modifyOtherKeys feature of xterm."
-  (let ((frame (selected-frame)))
-    (when (and (frame-live-p frame)
-              (memq frame xterm-modify-other-keys-terminal-list))
-      (send-string-to-terminal "\e[>4;1m"))))
+  "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" terminal))))
 
 (defun xterm-turn-off-modify-other-keys (&optional frame)
-  "Turn off the modifyOtherKeys feature of xterm."
-  (setq frame (and frame (selected-frame)))
-  (when (and (frame-live-p frame)
-            (memq frame xterm-modify-other-keys-terminal-list))
-    (send-string-to-terminal "\e[>4m")))
-
-(defun xterm-remove-modify-other-keys (&optional frame)
-  "Turn off the modifyOtherKeys feature of xterm and remove frame from consideration."
-  (setq frame (and frame (selected-frame)))
-  (when (and (frame-live-p frame)
-            (memq frame xterm-modify-other-keys-terminal-list))
+  "Temporarily turn off the modifyOtherKeys feature of xterm."
+  (let ((terminal (when frame (frame-terminal frame))))
+    (when (and (terminal-live-p terminal)
+               (memq terminal xterm-modify-other-keys-terminal-list))
+      (send-string-to-terminal "\e[>4m" terminal))))
+
+(defun xterm-remove-modify-other-keys (&optional terminal)
+  "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 (frame-terminal frame)
-               xterm-modify-other-keys-terminal-list))
-    (send-string-to-terminal "\e[>4m")))
+         (delq terminal xterm-modify-other-keys-terminal-list))
+    (send-string-to-terminal "\e[>4m" terminal)))
 
 ;; arch-tag: 12e7ebdd-1e6c-4b25-b0f9-35ace25e855a
 ;;; xterm.el ends here