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

From: Juri Linkov <[email-masked]>
Date: Sat, 28 Nov 2009 19:49:08 +0200

> Indeed, SVG files should be handled like postscript files, i.e. use
> image-minor-mode rather than image-mode.

Currently PS files are handled by DocView, but there is the same problem
for .xbm/.xpm files that use c-mode.

> Can someone figure out how to to do that?

The problem is in `(use-local-map image-mode-text-map)'.

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.

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/