Subject: bug#5062: 23.1.50; image-toggle-display overwrites nxml-mode local key map

From: Stefan Monnier <[email-masked]>
Date: Sat, 28 Nov 2009 15:21:49 -0500

> After switching to "text" mode, it sets the local map to
> image-mode-text-map with a single key binding `C-c C-c'
> instead of restoring the original map of the major mode.

> The following patch fixes this bug.  It saves a copy of the original
> map to the internal buffer-local variable `image-mode-local-map'
> with the additional image-mode specific key binding `C-c C-c' to
> switch back to the image mode.  And on switching to the text mode
> restores the original major mode map from this variable.

Better would be to save the major mode, and then switch to
"saved-major-mode + image-minor-mode".  No need to modify anybody's
keymap.  And I think that image-minor-mode should only be used while
displaying text: basically, image-toggle-display should toggle between
image-mode and the other major-mode (complemented with
image-minor-mode).

That would be cleaner,

        Stefan

> Index: lisp/image-mode.el
> ===================================================================
> RCS file: /sources/emacs/emacs/lisp/image-mode.el,v
> retrieving revision 1.58
> diff -u -r1.58 image-mode.el
> --- lisp/image-mode.el	11 Nov 2009 05:49:13 -0000	1.58
> +++ lisp/image-mode.el	28 Nov 2009 17:48:00 -0000
> @@ -306,11 +306,8 @@
>      map)
>    "Major mode keymap for viewing images in Image mode.")
 
> -(defvar image-mode-text-map
> -  (let ((map (make-sparse-keymap)))
> -    (define-key map "\C-c\C-c" 'image-toggle-display)
> -    map)
> -  "Major mode keymap for viewing images as text in Image mode.")
> +(defvar image-mode-local-map nil)
> +(make-variable-buffer-local 'image-mode-local-map)
 
>  (defvar bookmark-make-record-function)
 
> @@ -329,6 +326,9 @@
>    ;; Keep track of [vh]scroll when switching buffers
>    (image-mode-setup-winprops)
 
> +  (setq image-mode-local-map (copy-keymap (current-local-map)))
> +  (define-key image-mode-local-map "\C-c\C-c" 'image-toggle-display)
> +
>    (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t)
>    (if (display-images-p)
>        (if (not (image-get-display-property))
> @@ -339,7 +339,7 @@
>  	(setq cursor-type nil truncate-lines t
>  	      image-type (plist-get (cdr (image-get-display-property)) :type)))
>      (setq image-type "text")
> -    (use-local-map image-mode-text-map))
> +    (use-local-map image-mode-local-map))
>    (setq mode-name (format "Image[%s]" image-type))
>    (run-mode-hooks 'image-mode-hook)
>    (if (display-images-p)
> @@ -354,12 +354,16 @@
>    "Toggle Image minor mode.
>  With arg, turn Image minor mode on if arg is positive, off otherwise.
>  See the command `image-mode' for more information on this mode."
> -  nil (:eval (format " Image[%s]" image-type)) image-mode-text-map
> +  nil (:eval (format " Image[%s]" image-type)) nil
>    :group 'image
>    :version "22.1"
>    (if (not image-minor-mode)
>        (image-toggle-display-text)
>      (image-mode-setup-winprops)
> +
> +    (setq image-mode-local-map (copy-keymap (current-local-map)))
> +    (define-key image-mode-local-map "\C-c\C-c" 'image-toggle-display)
> +
>      (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t)
>      (if (display-images-p)
>  	(if (not (image-get-display-property))
> @@ -367,7 +371,7 @@
>  	  (setq cursor-type nil truncate-lines t
>  		image-type (plist-get (cdr (image-get-display-property)) :type)))
>        (setq image-type "text")
> -      (use-local-map image-mode-text-map))
> +      (use-local-map image-mode-local-map))
>      (if (display-images-p)
>  	(message "%s" (concat
>  		       (substitute-command-keys
> @@ -425,7 +429,7 @@
>  	(kill-local-variable 'cursor-type)
>  	(kill-local-variable 'truncate-lines)
>  	(kill-local-variable 'auto-hscroll-mode)
> -	(use-local-map image-mode-text-map)
> +	(use-local-map image-mode-local-map)
>  	(setq image-type "text")
>  	(if (eq major-mode 'image-mode)
>  	    (setq mode-name "Image[text]"))

> -- 
> Juri Linkov
> http://www.jurta.org/emacs/