emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Putting auto-image-file-mode in Options menu


From: Richard Stallman
Subject: Re: Putting auto-image-file-mode in Options menu
Date: Tue, 15 Mar 2005 13:39:01 -0500

    If you are working through a slow X connection, accidentally visiting
    an image file could be a very expensive mistake.

We could solve that, perhaps, by introducing preemption into
the display of a large image.  I think that is too big a change
to make just now, but it could be made some day.  In the mean time,
I think that slow X connections are rather rare, and it would be better
to ask those userd to turn off auto-image-file-mode if it is better
for everyone else.

    Anyway, there are ASCII-based image file formats like ASCII PBM, PGM,
    PPM, PAM and XBM and XPM.  Much more often than not, when I open such
    files with Emacs, I really don't want to see the picture, but the
    source text (to see comments, assignment of colors and palette, ranges
    and so on).

That sounds like a more cogent argument.  If many people share your
preference for the results, that is a reason not to enable
auto-image-file-mode.

This leads me to think it would be better to make these file types use
a major mode that would have a simple command (C-c C-c) to switch
between lookimg at the text and looking at the image.  If this major
mode were to display an echo-area message to tell the user about the
command, it would be more or less as convenient as
auto-image-file-mode, but would never do the wrong thing.

What do people think?  Here's the code to do it.
(But I wonder, was there some reason why Miles implemented
this feature using a file name handler instead of with a major mode?)


;;; image-mode.el --- support for visiting image files
;;
;; Copyright (C) 2005 Free Software Foundation, Inc.
;;
;; Author: Richard Stallman <address@hidden>
;; Keywords: multimedia

;; This file is part of GNU Emacs.

;; 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)
;; 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
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; 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., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;; Defines a major mode for visiting image files
;; that allows conversion between viewing the text of the file
;; and viewing the file as an image.  Viewing the image
;; works by putting a `display' text-property on the
;; image data, with the image-data still present underneath; if the
;; resulting buffer file is saved to another name it will correctly save
;; the image data to the new file.

;;; Code:

(require 'image)

;;;###autoload (push '("\\.jpg\\'" . image-mode) auto-mode-alist)
;;;###autoload (push '("\\.jpeg\\'" . image-mode) auto-mode-alist)
;;;###autoload (push '("\\.gif\\'" . image-mode) auto-mode-alist)
;;;###autoload (push '("\\.png\\'" . image-mode) auto-mode-alist)
;;;###autoload (push '("\\.tiff\\'" . image-mode) auto-mode-alist)
;;;###autoload (push '("\\.tif\\'" . image-mode) auto-mode-alist)
;;;###autoload (push '("\\.xbm\\'" . image-mode) auto-mode-alist)
;;;###autoload (push '("\\.pbm\\'" . image-mode) auto-mode-alist)
;;;###autoload (push '("\\.pgm\\'" . image-mode) auto-mode-alist)
;;;###autoload (push '("\\.ppm\\'" . image-mode) auto-mode-alist)
;;;###autoload (push '("\\.pnm\\'" . image-mode) auto-mode-alist)

(defvar image-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map "\C-c\C-c" 'image-toggle-display)
    map)
  "Major mode keymap for Image mode.")

;;;###autoload
(defun image-mode ()
  "Major mode for image files.
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 major-mode 'image-mode)
  (use-local-map image-mode-map)
  (run-mode-hooks 'image-mode-hook)
  (message (substitute-command-keys
            "Type \\[image-toggle-display] to view the image as an image.")))

(defun image-toggle-display ()
  "Start or stop displaying an image file as the actual image.
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)
      (let ((inhibit-read-only t)
            (buffer-undo-list t))
        (remove-list-of-text-properties (point-min) (point-max)
                                        '(display intangible read-nonsticky
                                                  read-only front-sticky))
        (kill-local-variable 'cursor-type)
        (kill-local-variable 'truncate-lines)
        (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* ((data
            (string-make-unibyte
             (buffer-substring-no-properties (point-min) (point-max))))
           (image
            (create-image data nil t))
           (props
            `(display ,image
                      intangible ,image
                      rear-nonsticky (display intangible)
                      ;; This a cheap attempt to make the whole buffer
                      ;; read-only when we're visiting the file (as
                      ;; opposed to just inserting it).
                      read-only t front-sticky (read-only)))
           (buffer-undo-list t))
      (add-text-properties (point-min) (point-max) props)
      ;; Inhibit the cursor when the buffer contains only an image,
      ;; because cursors look very strange on top of images.
      (setq cursor-type nil)
      ;; This just makes the arrow displayed in the right fringe
      ;; area look correct when the image is wider than the window.
      (setq truncate-lines t)
      (message "Repeat this command to go back to displaying the file as 
text"))))

(provide 'image-mode)

;;; image-mode.el ends here




reply via email to

[Prev in Thread] Current Thread [Next in Thread]