1 ;;; image-mode.el --- support for visiting image files
3 ;; Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
5 ;; Author: Richard Stallman <rms@gnu.org>
6 ;; Keywords: multimedia
8 ;; This file is part of GNU Emacs.
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 3, or (at your option)
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.
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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
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.
39 ;;;###autoload (push '("\\.jpe?g\\'" . image-mode) auto-mode-alist)
40 ;;;###autoload (push '("\\.png\\'" . image-mode) auto-mode-alist)
41 ;;;###autoload (push '("\\.gif\\'" . image-mode) auto-mode-alist)
42 ;;;###autoload (push '("\\.tiff?\\'" . image-mode) auto-mode-alist)
43 ;;;###autoload (push '("\\.p[bpgn]m\\'" . image-mode) auto-mode-alist)
45 ;;;###autoload (push '("\\.x[bp]m\\'" . c-mode) auto-mode-alist)
46 ;;;###autoload (push '("\\.x[bp]m\\'" . image-mode-maybe) auto-mode-alist)
48 ;;;###autoload (push '("\\.svgz?\\'" . xml-mode) auto-mode-alist)
49 ;;;###autoload (push '("\\.svgz?\\'" . image-mode-maybe) auto-mode-alist)
51 ;;; Image scrolling functions
53 (defun image-forward-hscroll (&optional n
)
54 "Scroll image in current window to the left by N character widths.
55 Stop if the right edge of the image is reached."
59 (set-window-hscroll (selected-window)
60 (max 0 (+ (window-hscroll) n
))))
62 (let* ((image (get-text-property 1 'display
))
63 (edges (window-inside-edges))
64 (win-width (- (nth 2 edges
) (nth 0 edges
)))
65 (img-width (ceiling (car (image-size image
)))))
66 (set-window-hscroll (selected-window)
67 (min (max 0 (- img-width win-width
))
68 (+ n
(window-hscroll))))))))
70 (defun image-backward-hscroll (&optional n
)
71 "Scroll image in current window to the right by N character widths.
72 Stop if the left edge of the image is reached."
74 (image-forward-hscroll (- n
)))
76 (defun image-next-line (&optional n
)
77 "Scroll image in current window upward by N lines.
78 Stop if the bottom edge of the image is reached."
82 (set-window-vscroll (selected-window)
83 (max 0 (+ (window-vscroll) n
))))
85 (let* ((image (get-text-property 1 'display
))
86 (edges (window-inside-edges))
87 (win-height (- (nth 3 edges
) (nth 1 edges
)))
88 (img-height (ceiling (cdr (image-size image
)))))
89 (set-window-vscroll (selected-window)
90 (min (max 0 (- img-height win-height
))
91 (+ n
(window-vscroll))))))))
93 (defun image-previous-line (&optional n
)
94 "Scroll image in current window downward by N lines.
95 Stop if the top edge of the image is reached."
97 (image-next-line (- n
)))
99 (defun image-scroll-up (&optional n
)
100 "Scroll image in current window upward by N lines.
101 Stop if the bottom edge of the image is reached.
102 If ARG is omitted or nil, scroll upward by a near full screen.
103 A near full screen is `next-screen-context-lines' less than a full screen.
104 Negative ARG means scroll downward.
105 If ARG is the atom `-', scroll downward by nearly full screen.
106 When calling from a program, supply as argument a number, nil, or `-'."
109 (let* ((edges (window-inside-edges))
110 (win-height (- (nth 3 edges
) (nth 1 edges
))))
112 (max 0 (- win-height next-screen-context-lines
)))))
114 (let* ((edges (window-inside-edges))
115 (win-height (- (nth 3 edges
) (nth 1 edges
))))
117 (min 0 (- next-screen-context-lines win-height
)))))
118 (t (image-next-line (prefix-numeric-value n
)))))
120 (defun image-scroll-down (&optional n
)
121 "Scroll image in current window downward by N lines
122 Stop if the top edge of the image is reached.
123 If ARG is omitted or nil, scroll downward by a near full screen.
124 A near full screen is `next-screen-context-lines' less than a full screen.
125 Negative ARG means scroll upward.
126 If ARG is the atom `-', scroll upward by nearly full screen.
127 When calling from a program, supply as argument a number, nil, or `-'."
130 (let* ((edges (window-inside-edges))
131 (win-height (- (nth 3 edges
) (nth 1 edges
))))
133 (min 0 (- next-screen-context-lines win-height
)))))
135 (let* ((edges (window-inside-edges))
136 (win-height (- (nth 3 edges
) (nth 1 edges
))))
138 (max 0 (- win-height next-screen-context-lines
)))))
139 (t (image-next-line (- (prefix-numeric-value n
))))))
141 (defun image-bol (arg)
142 "Scroll horizontally to the left edge of the image in the current window.
143 With argument ARG not nil or 1, move forward ARG - 1 lines first,
144 stopping if the top or bottom edge of the image is reached."
147 (/= (setq arg
(prefix-numeric-value arg
)) 1)
148 (image-next-line (- arg
1)))
149 (set-window-hscroll (selected-window) 0))
151 (defun image-eol (arg)
152 "Scroll horizontally to the right edge of the image in the current window.
153 With argument ARG not nil or 1, move forward ARG - 1 lines first,
154 stopping if the top or bottom edge of the image is reached."
157 (/= (setq arg
(prefix-numeric-value arg
)) 1)
158 (image-next-line (- arg
1)))
159 (let* ((image (get-text-property 1 'display
))
160 (edges (window-inside-edges))
161 (win-width (- (nth 2 edges
) (nth 0 edges
)))
162 (img-width (ceiling (car (image-size image
)))))
163 (set-window-hscroll (selected-window)
164 (max 0 (- img-width win-width
)))))
167 "Scroll to the top-left corner of the image in the current window."
169 (set-window-hscroll (selected-window) 0)
170 (set-window-vscroll (selected-window) 0))
173 "Scroll to the bottom-right corner of the image in the current window."
175 (let* ((image (get-text-property 1 'display
))
176 (edges (window-inside-edges))
177 (win-width (- (nth 2 edges
) (nth 0 edges
)))
178 (img-width (ceiling (car (image-size image
))))
179 (win-height (- (nth 3 edges
) (nth 1 edges
)))
180 (img-height (ceiling (cdr (image-size image
)))))
181 (set-window-hscroll (selected-window) (max 0 (- img-width win-width
)))
182 (set-window-vscroll (selected-window) (max 0 (- img-height win-height
)))))
186 (defvar image-mode-map
187 (let ((map (make-sparse-keymap)))
188 (define-key map
"\C-c\C-c" 'image-toggle-display
)
189 (define-key map
[remap forward-char
] 'image-forward-hscroll
)
190 (define-key map
[remap backward-char
] 'image-backward-hscroll
)
191 (define-key map
[remap previous-line
] 'image-previous-line
)
192 (define-key map
[remap next-line
] 'image-next-line
)
193 (define-key map
[remap scroll-up
] 'image-scroll-up
)
194 (define-key map
[remap scroll-down
] 'image-scroll-down
)
195 (define-key map
[remap move-beginning-of-line
] 'image-bol
)
196 (define-key map
[remap move-end-of-line
] 'image-eol
)
197 (define-key map
[remap beginning-of-buffer
] 'image-bob
)
198 (define-key map
[remap end-of-buffer
] 'image-eob
)
200 "Major mode keymap for viewing images in Image mode.")
202 (defvar image-mode-text-map
203 (let ((map (make-sparse-keymap)))
204 (define-key map
"\C-c\C-c" 'image-toggle-display
)
206 "Major mode keymap for viewing images as text in Image mode.")
210 "Major mode for image files.
211 You can use \\<image-mode-map>\\[image-toggle-display]
212 to toggle between display as an image and display as text."
214 (kill-all-local-variables)
215 (setq mode-name
"Image")
216 (setq major-mode
'image-mode
)
217 (add-hook 'change-major-mode-hook
'image-toggle-display-text nil t
)
218 (if (and (display-images-p)
219 (not (get-text-property (point-min) 'display
)))
220 (image-toggle-display)
221 ;; Set next vars when image is already displayed but local
222 ;; variables were cleared by kill-all-local-variables
223 (use-local-map image-mode-map
)
224 (setq cursor-type nil truncate-lines t
))
225 (run-mode-hooks 'image-mode-hook
)
226 (if (display-images-p)
227 (message "%s" (concat
228 (substitute-command-keys
229 "Type \\[image-toggle-display] to view as ")
230 (if (get-text-property (point-min) 'display
)
231 "text" "an image") "."))))
234 (define-minor-mode image-minor-mode
235 "Toggle Image minor mode.
236 With arg, turn Image minor mode on if arg is positive, off otherwise.
237 See the command `image-mode' for more information on this mode."
238 nil
" Image" image-mode-text-map
241 (if (not image-minor-mode
)
242 (image-toggle-display-text)
243 (if (get-text-property (point-min) 'display
)
244 (setq cursor-type nil truncate-lines t
))
245 (add-hook 'change-major-mode-hook
(lambda () (image-minor-mode -
1)) nil t
)
246 (message "%s" (concat (substitute-command-keys
247 "Type \\[image-toggle-display] to view the image as ")
248 (if (get-text-property (point-min) 'display
)
249 "text" "an image") "."))))
252 (defun image-mode-maybe ()
253 "Set major or minor mode for image files.
254 Set Image major mode only when there are no other major modes
255 associated with a filename in `auto-mode-alist'. When an image
256 filename matches another major mode in `auto-mode-alist' then
257 set that major mode and Image minor mode.
259 See commands `image-mode' and `image-minor-mode' for more
260 information on these modes."
265 (unless (memq (or (car-safe (cdr elt
)) (cdr elt
))
266 '(image-mode image-mode-maybe
))
269 (if (assoc-default buffer-file-name mode-alist
'string-match
)
270 (let ((auto-mode-alist mode-alist
)
271 (magic-mode-alist nil
))
273 (image-minor-mode t
))
276 (defun image-toggle-display-text ()
277 "Showing the text of the image file."
278 (if (get-text-property (point-min) 'display
)
279 (image-toggle-display)))
281 (defvar archive-superior-buffer
)
282 (defvar tar-superior-buffer
)
284 (defun image-toggle-display ()
285 "Start or stop displaying an image file as the actual image.
286 This command toggles between showing the text of the image file
287 and showing the image as an image."
289 (if (get-text-property (point-min) 'display
)
290 (let ((inhibit-read-only t
)
292 (modified (buffer-modified-p)))
293 (remove-list-of-text-properties (point-min) (point-max)
294 '(display intangible read-nonsticky
295 read-only front-sticky
))
296 (set-buffer-modified-p modified
)
297 (kill-local-variable 'cursor-type
)
298 (kill-local-variable 'truncate-lines
)
299 (kill-local-variable 'auto-hscroll-mode
)
300 (use-local-map image-mode-text-map
)
301 (if (called-interactively-p)
302 (message "Repeat this command to go back to displaying the image")))
303 ;; Turn the image data into a real image, but only if the whole file
305 (let* ((filename (buffer-file-name))
308 (file-readable-p filename
)
309 (not (file-remote-p filename
))
310 (not (buffer-modified-p))
311 (not (and (boundp 'archive-superior-buffer
)
312 archive-superior-buffer
))
313 (not (and (boundp 'tar-superior-buffer
)
314 tar-superior-buffer
)))
315 (create-image filename
)
318 (buffer-substring-no-properties (point-min) (point-max)))
323 rear-nonsticky
(display intangible
)
324 read-only t front-sticky
(read-only)))
325 (inhibit-read-only t
)
327 (modified (buffer-modified-p)))
328 (image-refresh image
)
329 (add-text-properties (point-min) (point-max) props
)
330 (set-buffer-modified-p modified
)
331 ;; Inhibit the cursor when the buffer contains only an image,
332 ;; because cursors look very strange on top of images.
333 (setq cursor-type nil
)
334 ;; This just makes the arrow displayed in the right fringe
335 ;; area look correct when the image is wider than the window.
336 (setq truncate-lines t
)
337 ;; Allow navigation of large images
338 (set (make-local-variable 'auto-hscroll-mode
) nil
)
339 (use-local-map image-mode-map
)
340 (if (called-interactively-p)
341 (message "Repeat this command to go back to displaying the file as text")))))
343 (provide 'image-mode
)
345 ;; arch-tag: b5b2b7e6-26a7-4b79-96e3-1546b5c4c6cb
346 ;;; image-mode.el ends here