annotate lisp/image-mode.el @ 60920:242e5edee3ce

* complete.el, thumbs.el: Replace `legal' with `valid'.
author Werner LEMBERG <wl@gnu.org>
date Fri, 25 Mar 2005 09:49:00 +0000
parents 391ee1dd9e7a
children ddcbccff39ce
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
60694
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1 ;;; image-mode.el --- support for visiting image files
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2 ;;
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
3 ;; Copyright (C) 2005 Free Software Foundation, Inc.
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
4 ;;
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
5 ;; Author: Richard Stallman <rms@gnu.org>
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
6 ;; Keywords: multimedia
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
7
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
8 ;; This file is part of GNU Emacs.
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
9
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
11 ;; it under the terms of the GNU General Public License as published by
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
12 ;; the Free Software Foundation; either version 2, or (at your option)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
13 ;; any later version.
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
14
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15 ;; GNU Emacs is distributed in the hope that it will be useful,
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18 ;; GNU General Public License for more details.
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
19
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
20 ;; You should have received a copy of the GNU General Public License
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
23 ;; Boston, MA 02111-1307, USA.
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
24
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
25 ;;; Commentary:
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
26
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
27 ;; Defines a major mode for visiting image files
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
28 ;; that allows conversion between viewing the text of the file
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
29 ;; and viewing the file as an image. Viewing the image
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
30 ;; works by putting a `display' text-property on the
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
31 ;; image data, with the image-data still present underneath; if the
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
32 ;; resulting buffer file is saved to another name it will correctly save
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
33 ;; the image data to the new file.
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
34
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
35 ;;; Code:
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
36
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
37 (require 'image)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
38
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
39 ;;;###autoload (push '("\\.jpg\\'" . image-mode) auto-mode-alist)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
40 ;;;###autoload (push '("\\.jpeg\\'" . image-mode) auto-mode-alist)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
41 ;;;###autoload (push '("\\.gif\\'" . image-mode) auto-mode-alist)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
42 ;;;###autoload (push '("\\.png\\'" . image-mode) auto-mode-alist)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
43 ;;;###autoload (push '("\\.tiff\\'" . image-mode) auto-mode-alist)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
44 ;;;###autoload (push '("\\.tif\\'" . image-mode) auto-mode-alist)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45 ;;;###autoload (push '("\\.xbm\\'" . image-mode) auto-mode-alist)
60780
391ee1dd9e7a Handle .xpm files too.
Richard M. Stallman <rms@gnu.org>
parents: 60697
diff changeset
46 ;;;###autoload (push '("\\.xpm\\'" . image-mode) auto-mode-alist)
60694
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
47 ;;;###autoload (push '("\\.pbm\\'" . image-mode) auto-mode-alist)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
48 ;;;###autoload (push '("\\.pgm\\'" . image-mode) auto-mode-alist)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49 ;;;###autoload (push '("\\.ppm\\'" . image-mode) auto-mode-alist)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
50 ;;;###autoload (push '("\\.pnm\\'" . image-mode) auto-mode-alist)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
52 (defvar image-mode-map
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
53 (let ((map (make-sparse-keymap)))
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
54 (define-key map "\C-c\C-c" 'image-toggle-display)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
55 map)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
56 "Major mode keymap for Image mode.")
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
57
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
58 ;;;###autoload
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
59 (defun image-mode ()
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60 "Major mode for image files.
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61 You can use \\<image-mode-map>\\[image-toggle-display]
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
62 to toggle between display as an image and display as text."
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63 (interactive)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
64 (kill-all-local-variables)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
65 (setq mode-name "Image")
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
66 (setq major-mode 'image-mode)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
67 (use-local-map image-mode-map)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
68 (run-mode-hooks 'image-mode-hook)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
69 (message (substitute-command-keys
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
70 "Type \\[image-toggle-display] to view the image as an image.")))
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
71
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
72 (defun image-toggle-display ()
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
73 "Start or stop displaying an image file as the actual image.
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
74 This command toggles between showing the text of the image file
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
75 and showing the image as an image."
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
76 (interactive)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
77 (if (get-text-property (point-min) 'display)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
78 (let ((inhibit-read-only t)
60780
391ee1dd9e7a Handle .xpm files too.
Richard M. Stallman <rms@gnu.org>
parents: 60697
diff changeset
79 (buffer-undo-list t)
391ee1dd9e7a Handle .xpm files too.
Richard M. Stallman <rms@gnu.org>
parents: 60697
diff changeset
80 (modified (buffer-modified-p)))
60694
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
81 (remove-list-of-text-properties (point-min) (point-max)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
82 '(display intangible read-nonsticky
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
83 read-only front-sticky))
60780
391ee1dd9e7a Handle .xpm files too.
Richard M. Stallman <rms@gnu.org>
parents: 60697
diff changeset
84 (set-buffer-modified-p modified)
60694
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
85 (kill-local-variable 'cursor-type)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
86 (kill-local-variable 'truncate-lines)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
87 (message "Repeat this command to go back to displaying the image"))
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
88 ;; Turn the image data into a real image, but only if the whole file
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
89 ;; was inserted
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
90 (let* ((data
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
91 (string-make-unibyte
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
92 (buffer-substring-no-properties (point-min) (point-max))))
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
93 (image
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
94 (create-image data nil t))
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
95 (props
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
96 `(display ,image
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
97 intangible ,image
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
98 rear-nonsticky (display intangible)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
99 ;; This a cheap attempt to make the whole buffer
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
100 ;; read-only when we're visiting the file (as
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
101 ;; opposed to just inserting it).
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
102 read-only t front-sticky (read-only)))
60780
391ee1dd9e7a Handle .xpm files too.
Richard M. Stallman <rms@gnu.org>
parents: 60697
diff changeset
103 (buffer-undo-list t)
391ee1dd9e7a Handle .xpm files too.
Richard M. Stallman <rms@gnu.org>
parents: 60697
diff changeset
104 (modified (buffer-modified-p)))
60694
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
105 (add-text-properties (point-min) (point-max) props)
60780
391ee1dd9e7a Handle .xpm files too.
Richard M. Stallman <rms@gnu.org>
parents: 60697
diff changeset
106 (set-buffer-modified-p modified)
60694
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
107 ;; Inhibit the cursor when the buffer contains only an image,
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
108 ;; because cursors look very strange on top of images.
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
109 (setq cursor-type nil)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
110 ;; This just makes the arrow displayed in the right fringe
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
111 ;; area look correct when the image is wider than the window.
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
112 (setq truncate-lines t)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
113 (message "Repeat this command to go back to displaying the file as text"))))
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
114
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
115 (provide 'image-mode)
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
116
60697
d9c9ad74e719 Changes from arch/CVS synchronization
Miles Bader <miles@gnu.org>
parents: 60694
diff changeset
117 ;; arch-tag: b5b2b7e6-26a7-4b79-96e3-1546b5c4c6cb
60694
9e7ee067df09 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
118 ;;; image-mode.el ends here