New file.
[bpt/emacs.git] / lisp / image-mode.el
index 6ac8641..283f6ae 100644 (file)
@@ -9,7 +9,7 @@
 
 ;; 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 2, or (at your option)
+;; the Free Software Foundation; either version 3, or (at your option)
 ;; any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;;;###autoload (push '("\\.gif\\'"      . image-mode) auto-mode-alist)
 ;;;###autoload (push '("\\.tiff?\\'"    . image-mode) auto-mode-alist)
 ;;;###autoload (push '("\\.p[bpgn]m\\'" . image-mode) auto-mode-alist)
+
+;;;###autoload (push '("\\.x[bp]m\\'"   . c-mode)     auto-mode-alist)
 ;;;###autoload (push '("\\.x[bp]m\\'"   . image-mode-maybe) auto-mode-alist)
 
+;;;###autoload (push '("\\.svgz?\\'"    . xml-mode)   auto-mode-alist)
+;;;###autoload (push '("\\.svgz?\\'"    . image-mode-maybe) auto-mode-alist)
+
 ;;; Image scrolling functions
 
 (defun image-forward-hscroll (&optional n)
@@ -54,7 +59,7 @@ Stop if the right edge of the image is reached."
         (set-window-hscroll (selected-window)
                             (max 0 (+ (window-hscroll) n))))
        (t
-        (let* ((image (get-text-property 1 'display))
+        (let* ((image (get-char-property (point-min) 'display))
                (edges (window-inside-edges))
                (win-width (- (nth 2 edges) (nth 0 edges)))
                (img-width (ceiling (car (image-size image)))))
@@ -77,7 +82,7 @@ Stop if the bottom edge of the image is reached."
         (set-window-vscroll (selected-window)
                             (max 0 (+ (window-vscroll) n))))
        (t
-        (let* ((image (get-text-property 1 'display))
+        (let* ((image (get-char-property (point-min) 'display))
                (edges (window-inside-edges))
                (win-height (- (nth 3 edges) (nth 1 edges)))
                (img-height (ceiling (cdr (image-size image)))))
@@ -151,7 +156,7 @@ stopping if the top or bottom edge of the image is reached."
   (and arg
        (/= (setq arg (prefix-numeric-value arg)) 1)
        (image-next-line (- arg 1)))
-  (let* ((image (get-text-property 1 'display))
+  (let* ((image (get-char-property (point-min) 'display))
         (edges (window-inside-edges))
         (win-width (- (nth 2 edges) (nth 0 edges)))
         (img-width (ceiling (car (image-size image)))))
@@ -167,7 +172,7 @@ stopping if the top or bottom edge of the image is reached."
 (defun image-eob ()
   "Scroll to the bottom-right corner of the image in the current window."
   (interactive)
-  (let* ((image (get-text-property 1 'display))
+  (let* ((image (get-char-property (point-min) 'display))
         (edges (window-inside-edges))
         (win-width (- (nth 2 edges) (nth 0 edges)))
         (img-width (ceiling (car (image-size image))))
@@ -178,6 +183,11 @@ stopping if the top or bottom edge of the image is reached."
 
 ;;; Image Mode setup
 
+(defvar image-type nil
+  "Current image type.
+This variable is used to display the current image type in the mode line.")
+(make-variable-buffer-local 'image-type)
+
 (defvar image-mode-map
   (let ((map (make-sparse-keymap)))
     (define-key map "\C-c\C-c" 'image-toggle-display)
@@ -207,11 +217,11 @@ You can use \\<image-mode-map>\\[image-toggle-display]
 to toggle between display as an image and display as text."
   (interactive)
   (kill-all-local-variables)
-  (setq mode-name "Image")
+  (setq mode-name "Image[text]")
   (setq major-mode 'image-mode)
   (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t)
   (if (and (display-images-p)
-          (not (get-text-property (point-min) 'display)))
+          (not (get-char-property (point-min) 'display)))
       (image-toggle-display)
     ;; Set next vars when image is already displayed but local
     ;; variables were cleared by kill-all-local-variables
@@ -222,7 +232,7 @@ to toggle between display as an image and display as text."
       (message "%s" (concat
                     (substitute-command-keys
                      "Type \\[image-toggle-display] to view as ")
-                    (if (get-text-property (point-min) 'display)
+                    (if (get-char-property (point-min) 'display)
                         "text" "an image") "."))))
 
 ;;;###autoload
@@ -230,17 +240,18 @@ to toggle between display as an image and display as text."
   "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 " Image" image-mode-map
+  nil (:eval (format " Image[%s]" image-type)) image-mode-text-map
   :group 'image
   :version "22.1"
   (if (not image-minor-mode)
       (image-toggle-display-text)
-    (if (get-text-property (point-min) 'display)
-       (setq cursor-type nil truncate-lines t))
+    (if (get-char-property (point-min) 'display)
+       (setq cursor-type nil truncate-lines t)
+      (setq image-type "text"))
     (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t)
     (message "%s" (concat (substitute-command-keys
                      "Type \\[image-toggle-display] to view the image as ")
-                    (if (get-text-property (point-min) 'display)
+                    (if (get-char-property (point-min) 'display)
                         "text" "an image") "."))))
 
 ;;;###autoload
@@ -270,7 +281,7 @@ information on these modes."
 
 (defun image-toggle-display-text ()
   "Showing the text of the image file."
-  (if (get-text-property (point-min) 'display)
+  (if (get-char-property (point-min) 'display)
       (image-toggle-display)))
 
 (defvar archive-superior-buffer)
@@ -281,7 +292,7 @@ information on these modes."
 This command toggles between showing the text of the image file
 and showing the image as an image."
   (interactive)
-  (if (get-text-property (point-min) 'display)
+  (if (get-char-property (point-min) 'display)
       (let ((inhibit-read-only t)
            (buffer-undo-list t)
            (modified (buffer-modified-p)))
@@ -293,25 +304,28 @@ and showing the image as an image."
        (kill-local-variable 'truncate-lines)
        (kill-local-variable 'auto-hscroll-mode)
        (use-local-map image-mode-text-map)
+       (setq image-type "text")
+       (if (eq major-mode 'image-mode)
+           (setq mode-name "Image[text]"))
        (if (called-interactively-p)
            (message "Repeat this command to go back to displaying the image")))
     ;; Turn the image data into a real image, but only if the whole file
     ;; was inserted
     (let* ((filename (buffer-file-name))
-          (image
-           (if (and filename
-                    (file-readable-p filename)
-                    (not (file-remote-p filename))
-                    (not (buffer-modified-p))
-                    (not (and (boundp 'archive-superior-buffer)
-                              archive-superior-buffer))
-                    (not (and (boundp 'tar-superior-buffer)
-                              tar-superior-buffer)))
-               (create-image filename)
-             (create-image
-              (string-make-unibyte
-               (buffer-substring-no-properties (point-min) (point-max)))
-              nil t)))
+          (data-p (not (and filename
+                            (file-readable-p filename)
+                            (not (file-remote-p filename))
+                            (not (buffer-modified-p))
+                            (not (and (boundp 'archive-superior-buffer)
+                                      archive-superior-buffer))
+                            (not (and (boundp 'tar-superior-buffer)
+                                      tar-superior-buffer)))))
+          (file-or-data (if data-p
+                            (string-make-unibyte
+                             (buffer-substring-no-properties (point-min) (point-max)))
+                          filename))
+          (type (image-type file-or-data nil data-p))
+          (image (create-image file-or-data type data-p))
           (props
            `(display ,image
              intangible ,image
@@ -332,6 +346,9 @@ and showing the image as an image."
       ;; Allow navigation of large images
       (set (make-local-variable 'auto-hscroll-mode) nil)
       (use-local-map image-mode-map)
+      (setq image-type type)
+      (if (eq major-mode 'image-mode)
+         (setq mode-name (format "Image[%s]" type)))
       (if (called-interactively-p)
          (message "Repeat this command to go back to displaying the file as text")))))