Subject: iso-transl pach (bug#6527: C-h b alters key-translation-map (in a bad way))

From: David Reitter <[email-masked]>
Date: Sun, 18 Jul 2010 12:06:03 +0100

[ Resent from 
  http://lists.gnu.org/archive/html/emacs-devel/2010-07/msg00967.html

  Otherwise how will someone reading this bug report find it? ]

I reported bug 6527 a while ago.

> 6527: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=6527

The problem was that iso-trans.el is autoloaded (e.g., when doing C-h b) and unconditionally binds Alt key combinations, overriding any user/package defined A- keys.  Once loaded, there is no obvious way to undo those bindings.

Here's a fix for this.  It defines a new minor mode, `iso-transl-mode', which controls those A-... key bindings.
I chose to automatically define the C-x 8 prefix key combinations when the package is loaded, so that the C-x 8 autoloading mechanism remains in place for the comfort of the occasional user. 

I don't know how much iso-transl is used by people, so I can't gauge the impact of this.  Please advise.

diff --combined lisp/international/iso-transl.el
index 8a88b50,b11a7a2..0000000
--- a/lisp/international/iso-transl.el
+++ b/lisp/international/iso-transl.el
@@@ -25,21 -25,16 +25,21 @@@
  ;;; Commentary:
  
  ;; Loading this package defines three ways of entering the non-ASCII
 -;; printable characters with codes above 127: the prefix C-x 8, or the
 -;; Alt key, or a dead accent key.  For example, you can enter uppercase
 -;; A-umlaut as `C-x 8 " A' or `Alt-" A' (if you have an Alt key) or
 -;; `umlaut A' (if you have an umlaut/diaeresis key).
 +;; printable characters with codes above 127: the prefix C-x 8, and,
 +;; with `iso-transl-mode' enabled, also the Alt key, and a dead accent
 +;; key.  For example, you can enter uppercase A-umlaut as `C-x 8 " A'
 +;; or, `iso-transl-mode' is enabled, `Alt-" A' (if you have an Alt
 +;; key) or `umlaut A' (if you have an umlaut/diaeresis key).
  
 -;; C-x 8 is set up to autoload this package,
 -;; but Alt keys and dead accent keys are only defined
 -;; once you have loaded the package.  It is nontrivial
 -;; to make all of the Alt keys autoload, and it is not clear
 -;; that the dead accent keys SHOULD autoload this package.
 +;; C-x 8 is set up to autoload this package, but Alt keys and dead
 +;; accent keys are only defined once you have loaded the package and
 +;; enabled `iso-transl-mode'.  It is nontrivial to make all of the Alt
 +;; keys autoload, and it is not clear that the dead accent keys SHOULD
 +;; autoload this package.
 +
 +;; Note that C-h b will autoload this package, which is one reason
 +;; why the more destructive key sequences (without C-x 8 prefix) are
 +;; only defined in `iso-transl-mode', and are thus reversible.
  
  ;;; Code:
  
@@@ -252,41 -247,29 +252,41 @@@ sequence VECTOR.  (VECTOR is normally o
      (setq key-translation-map (make-sparse-keymap)))
  (define-key key-translation-map "\C-x8" iso-transl-ctl-x-8-map)
  
 +(defmacro iso-transl-define-key (keymap key def)
 +  "Back up definition of KEY in KEYMAP, then `define-key'."
 +  `(progn
 +     (push (cons ,key (or (lookup-key ,keymap ,key) 'none))
 +	   (get ',keymap 'iso-transl-backup))
 +     (define-key ,keymap ,key ,def)))
 +
  ;; For each entry in the alist, we'll make up to three ways to generate
  ;; the character in question: the prefix `C-x 8'; the ALT modifier on
  ;; the first key of the sequence; and (if applicable) replacing the first
  ;; key of the sequence with the corresponding dead key.  For example, a
  ;; character associated with the string "~n" can be input with `C-x 8 ~ n'
  ;; or `Alt-~ n' or `mute-asciitilde n'.
 +(defun iso-transl-define-prefix-keys (alist)
 +  (while alist
 +    (let ((translated-vec (cdr (car alist))))
 +      (iso-transl-define-key iso-transl-ctl-x-8-map (car (car alist)) translated-vec))
 +    (setq alist (cdr alist))))
  (defun iso-transl-define-keys (alist)
    (while alist
      (let ((translated-vec (cdr (car alist))))
 -      (define-key iso-transl-ctl-x-8-map (car (car alist)) translated-vec)
 +      ;(iso-transl-define-key iso-transl-ctl-x-8-map (car (car alist)) translated-vec)
        (let ((inchar (aref (car (car alist)) 0))
  	    (vec (vconcat (car (car alist))))
  	    (tail iso-transl-dead-key-alist))
  	(aset vec 0 (logior (aref vec 0) ?\A-\^@))
 -	(define-key key-translation-map vec translated-vec)
 -	(define-key isearch-mode-map (vector (aref vec 0)) nil)
 +	(iso-transl-define-key key-translation-map vec translated-vec)
 +	(iso-transl-define-key isearch-mode-map (vector (aref vec 0)) nil)
  	(while tail
  	  (if (eq (car (car tail)) inchar)
  	      (let ((deadvec (copy-sequence vec))
  		    (deadkey (cdr (car tail))))
  		(aset deadvec 0 deadkey)
 -		(define-key isearch-mode-map (vector deadkey) nil)
 -		(define-key key-translation-map deadvec translated-vec)))
 +		(iso-transl-define-key isearch-mode-map (vector deadkey) nil)
 +		(iso-transl-define-key key-translation-map deadvec translated-vec)))
  	  (setq tail (cdr tail)))))
      (setq alist (cdr alist))))
  
@@@ -297,45 -280,14 +297,45 @@@
    (iso-transl-define-keys (cdr (assoc lang iso-transl-language-alist))))

 -;; The standard mapping comes automatically.  You can partially overlay it
 -;; with a language-specific mapping by using `M-x iso-transl-set-language'.
 -(iso-transl-define-keys iso-transl-char-map)
 -
 +;; unconditional definitions
 +(iso-transl-define-prefix-keys iso-transl-char-map)
  (define-key isearch-mode-map "\C-x" nil)
  (define-key isearch-mode-map [?\C-x t] 'isearch-other-control-char)
  (define-key isearch-mode-map "\C-x8" nil)
  
 +(define-minor-mode iso-transl-mode
 + "ISO Key translation mode.
 +This mode defines two ways of entering the non-ASCII printable
 +characters with codes above 127: the Alt key and 
 +a dead accent key.  For example, you can enter uppercase A-umlaut as
 +`Alt-\" A' (if you have an Alt key) or `umlaut A' (if
 +you have an umlaut/diaeresis key).
 +
 +This character can always be entered as `C-x 8 \" A' regardless of
 +this mode."
 + :group 'i18n
 + :lighter " ISO"
 +
 + (if iso-transl-mode
 +     ;; The standard mapping comes automatically.  You can partially overlay it
 +     ;; with a language-specific mapping by using `M-x iso-transl-set-language'.
 +     (iso-transl-define-keys iso-transl-char-map)
 +   (mapc
 +    (lambda (key-def) 
 +      (define-key key-translation-map (car key-def) 
 +	(and (not (eq (cdr key-def) 'none)) (cdr key-def))))
 +    (nreverse (get 'key-translation-map 'iso-transl-backup)))
 +   (put 'key-translation-map 'iso-transl-backup nil)
 +   (mapc
 +    (lambda (key-def) 
 +      (condition-case nil
 +	  (define-key isearch-mode-map (car key-def) 
 +	    (and (not (eq (cdr key-def) 'none)) (cdr key-def)))
 +	;; if some of the unconditional definitions above are instead made conditional
 +	;; restoring the default keys fails.
 +	(error nil)))
 +    (nreverse (get 'isearch-mode-map 'iso-transl-backup)))
 +   (put 'isearch-mode-map 'iso-transl-backup nil)))
  
  (provide 'iso-transl)