unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Richard Stallman <rms@gnu.org>
Cc: juri@jurta.org, monnier@iro.umontreal.ca, emacs-devel@gnu.org
Subject: Re: Putting auto-image-file-mode in Options menu
Date: Tue, 15 Mar 2005 13:39:01 -0500	[thread overview]
Message-ID: <E1DBGwX-0002Om-7F@fencepost.gnu.org> (raw)
In-Reply-To: <x51xaiplxr.fsf@lola.goethe.zz> (message from David Kastrup on Mon, 14 Mar 2005 11:21:36 +0100)

    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 <rms@gnu.org>
;; 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

  parent reply	other threads:[~2005-03-15 18:39 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-03-13  2:14 Putting auto-image-file-mode in Options menu Juri Linkov
2005-03-13 13:32 ` Stefan Monnier
2005-03-14  1:23   ` Juri Linkov
2005-03-14 10:21     ` David Kastrup
2005-03-15 13:18       ` Juri Linkov
2005-03-15 18:39       ` Richard Stallman [this message]
2005-03-16 17:55         ` Juri Linkov
2005-03-20  0:22           ` Richard Stallman
2005-03-20  0:54             ` David Kastrup
2005-03-20 18:01               ` Richard Stallman
2005-03-20 18:22                 ` David Kastrup
2005-03-21 17:30                   ` Richard Stallman
2005-03-20 21:47             ` Juri Linkov
2005-03-21 17:29               ` Richard Stallman
2005-03-27  1:39                 ` Miles Bader
2005-03-28 16:25                   ` Richard Stallman
2005-03-28 20:44                     ` Miles Bader
2005-03-28 21:37                       ` Eli Zaretskii
2005-03-29  3:00                         ` Richard Stallman
2005-03-29  3:01                       ` Richard Stallman
2005-03-22 20:43             ` Juri Linkov
2005-03-23  6:21               ` Richard Stallman
2005-03-23 20:48                 ` Juri Linkov
2005-03-25  6:43                   ` Richard Stallman
2005-03-18  0:37         ` Miles Bader
2005-03-14 13:07     ` Stefan Monnier
2005-03-15 13:19       ` Juri Linkov
2005-03-15 14:41         ` Stefan
2005-03-15 17:27           ` Juri Linkov
2005-03-15 18:39     ` Richard Stallman
2005-03-16 17:10       ` Kevin Rodgers
2005-03-17 23:01         ` Richard Stallman
2005-03-17 23:21           ` Stefan Monnier
2005-03-18 18:20             ` Richard Stallman
2005-03-27 23:18               ` auto-compression-mode (was: Putting auto-image-file-mode in Options menu) Juri Linkov
2005-03-28 22:53                 ` Richard Stallman
2005-03-14  3:00 ` Putting auto-image-file-mode in Options menu Richard Stallman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E1DBGwX-0002Om-7F@fencepost.gnu.org \
    --to=rms@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=juri@jurta.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).