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