Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / image-mode.el
CommitLineData
c7bd5d57
RS
1;;; image-mode.el --- support for visiting image files
2;;
a47d49c0 3;; Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
c7bd5d57
RS
4;;
5;; Author: Richard Stallman <rms@gnu.org>
6;; Keywords: multimedia
7
8;; This file is part of GNU Emacs.
9
eb3fa2cf 10;; GNU Emacs is free software: you can redistribute it and/or modify
c7bd5d57 11;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
c7bd5d57
RS
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
eb3fa2cf 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
c7bd5d57
RS
22
23;;; Commentary:
24
25;; Defines a major mode for visiting image files
26;; that allows conversion between viewing the text of the file
27;; and viewing the file as an image. Viewing the image
28;; works by putting a `display' text-property on the
29;; image data, with the image-data still present underneath; if the
30;; resulting buffer file is saved to another name it will correctly save
31;; the image data to the new file.
32
33;;; Code:
34
35(require 'image)
44e3c7c6 36(eval-when-compile (require 'cl))
c7bd5d57 37
d90e651e
JL
38;;;###autoload (push '("\\.jpe?g\\'" . image-mode) auto-mode-alist)
39;;;###autoload (push '("\\.png\\'" . image-mode) auto-mode-alist)
40;;;###autoload (push '("\\.gif\\'" . image-mode) auto-mode-alist)
41;;;###autoload (push '("\\.tiff?\\'" . image-mode) auto-mode-alist)
42;;;###autoload (push '("\\.p[bpgn]m\\'" . image-mode) auto-mode-alist)
4a2f0e99
JL
43
44;;;###autoload (push '("\\.x[bp]m\\'" . c-mode) auto-mode-alist)
d90e651e 45;;;###autoload (push '("\\.x[bp]m\\'" . image-mode-maybe) auto-mode-alist)
c7bd5d57 46
4a2f0e99
JL
47;;;###autoload (push '("\\.svgz?\\'" . xml-mode) auto-mode-alist)
48;;;###autoload (push '("\\.svgz?\\'" . image-mode-maybe) auto-mode-alist)
49
44e3c7c6
SM
50;;; Image mode window-info management.
51
52(defvar image-mode-winprops-alist t)
53(make-variable-buffer-local 'image-mode-winprops-alist)
54
55(defvar image-mode-new-window-functions nil
56 "Special hook run when image data is requested in a new window.
57It is called with one argument, the initial WINPROPS.")
58
59(defun image-mode-winprops (&optional window)
60 "Return winprops of WINDOW.
61A winprops object has the shape (WINDOW . ALIST)."
62 (unless window (setq window (selected-window)))
63 (let ((winprops (assq window image-mode-winprops-alist)))
64 ;; For new windows, set defaults from the latest.
65 (unless winprops
66 (setq winprops (cons window
67 (copy-alist (cdar image-mode-winprops-alist))))
68 (run-hook-with-args 'image-mode-new-window-functions winprops))
69 ;; Move window to front.
70 (setq image-mode-winprops-alist
71 (cons winprops (delq winprops image-mode-winprops-alist)))
72 winprops))
73
74(defun image-mode-window-get (prop &optional winprops)
75 (unless (consp winprops) (setq winprops (image-mode-winprops winprops)))
76 (cdr (assq prop (cdr winprops))))
77
78(defsetf image-mode-window-get (prop &optional winprops) (val)
79 `(image-mode-window-put ,prop ,val ,winprops))
80
81(defun image-mode-window-put (prop val &optional winprops)
82 (unless (consp winprops) (setq winprops (image-mode-winprops winprops)))
83 (setcdr winprops (cons (cons prop val)
84 (delq (assq prop (cdr winprops)) (cdr winprops)))))
85
86(defun image-set-window-vscroll (vscroll)
87 (setf (image-mode-window-get 'vscroll) vscroll)
88 (set-window-vscroll (selected-window) vscroll))
89
90(defun image-set-window-hscroll (ncol)
60134fd4 91 (setf (image-mode-window-get 'hscroll) ncol)
44e3c7c6
SM
92 (set-window-hscroll (selected-window) ncol))
93
94(defun image-mode-reapply-winprops ()
6d401b4e
SM
95 ;; When set-window-buffer, set hscroll and vscroll to what they were
96 ;; last time the image was displayed in this window.
97 (when (listp image-mode-winprops-alist)
c313b5fe 98 (let* ((winprops (image-mode-winprops))
6d401b4e
SM
99 (hscroll (image-mode-window-get 'hscroll winprops))
100 (vscroll (image-mode-window-get 'vscroll winprops)))
c313b5fe
SM
101 (if hscroll (set-window-hscroll (selected-window) hscroll))
102 (if vscroll (set-window-vscroll (selected-window) vscroll)))))
36e1c289 103
44e3c7c6
SM
104(defun image-mode-setup-winprops ()
105 ;; Record current scroll settings.
106 (unless (listp image-mode-winprops-alist)
107 (setq image-mode-winprops-alist nil))
108 (add-hook 'window-configuration-change-hook
109 'image-mode-reapply-winprops nil t))
110
111;;; Image scrolling functions
112
91784462
SM
113(defun image-get-display-property ()
114 (get-char-property (point-min) 'display
115 ;; There might be different images for different displays.
116 (if (eq (window-buffer) (current-buffer))
117 (selected-window))))
118
bb0cb417
CY
119(defun image-forward-hscroll (&optional n)
120 "Scroll image in current window to the left by N character widths.
121Stop if the right edge of the image is reached."
122 (interactive "p")
123 (cond ((= n 0) nil)
124 ((< n 0)
44e3c7c6 125 (image-set-window-hscroll (max 0 (+ (window-hscroll) n))))
bb0cb417 126 (t
91784462 127 (let* ((image (image-get-display-property))
bb0cb417
CY
128 (edges (window-inside-edges))
129 (win-width (- (nth 2 edges) (nth 0 edges)))
130 (img-width (ceiling (car (image-size image)))))
44e3c7c6 131 (image-set-window-hscroll (min (max 0 (- img-width win-width))
36e1c289 132 (+ n (window-hscroll))))))))
bb0cb417
CY
133
134(defun image-backward-hscroll (&optional n)
135 "Scroll image in current window to the right by N character widths.
136Stop if the left edge of the image is reached."
137 (interactive "p")
138 (image-forward-hscroll (- n)))
139
140(defun image-next-line (&optional n)
141 "Scroll image in current window upward by N lines.
142Stop if the bottom edge of the image is reached."
143 (interactive "p")
144 (cond ((= n 0) nil)
145 ((< n 0)
44e3c7c6 146 (image-set-window-vscroll (max 0 (+ (window-vscroll) n))))
bb0cb417 147 (t
91784462 148 (let* ((image (image-get-display-property))
bb0cb417
CY
149 (edges (window-inside-edges))
150 (win-height (- (nth 3 edges) (nth 1 edges)))
151 (img-height (ceiling (cdr (image-size image)))))
44e3c7c6 152 (image-set-window-vscroll (min (max 0 (- img-height win-height))
36e1c289 153 (+ n (window-vscroll))))))))
bb0cb417
CY
154
155(defun image-previous-line (&optional n)
156 "Scroll image in current window downward by N lines.
157Stop if the top edge of the image is reached."
158 (interactive "p")
159 (image-next-line (- n)))
160
161(defun image-scroll-up (&optional n)
162 "Scroll image in current window upward by N lines.
163Stop if the bottom edge of the image is reached.
164If ARG is omitted or nil, scroll upward by a near full screen.
165A near full screen is `next-screen-context-lines' less than a full screen.
166Negative ARG means scroll downward.
167If ARG is the atom `-', scroll downward by nearly full screen.
168When calling from a program, supply as argument a number, nil, or `-'."
169 (interactive "P")
170 (cond ((null n)
171 (let* ((edges (window-inside-edges))
172 (win-height (- (nth 3 edges) (nth 1 edges))))
173 (image-next-line
174 (max 0 (- win-height next-screen-context-lines)))))
175 ((eq n '-)
176 (let* ((edges (window-inside-edges))
177 (win-height (- (nth 3 edges) (nth 1 edges))))
178 (image-next-line
179 (min 0 (- next-screen-context-lines win-height)))))
180 (t (image-next-line (prefix-numeric-value n)))))
181
182(defun image-scroll-down (&optional n)
3488c456 183 "Scroll image in current window downward by N lines.
bb0cb417
CY
184Stop if the top edge of the image is reached.
185If ARG is omitted or nil, scroll downward by a near full screen.
186A near full screen is `next-screen-context-lines' less than a full screen.
187Negative ARG means scroll upward.
188If ARG is the atom `-', scroll upward by nearly full screen.
189When calling from a program, supply as argument a number, nil, or `-'."
190 (interactive "P")
191 (cond ((null n)
192 (let* ((edges (window-inside-edges))
193 (win-height (- (nth 3 edges) (nth 1 edges))))
194 (image-next-line
195 (min 0 (- next-screen-context-lines win-height)))))
196 ((eq n '-)
197 (let* ((edges (window-inside-edges))
198 (win-height (- (nth 3 edges) (nth 1 edges))))
199 (image-next-line
200 (max 0 (- win-height next-screen-context-lines)))))
201 (t (image-next-line (- (prefix-numeric-value n))))))
202
203(defun image-bol (arg)
204 "Scroll horizontally to the left edge of the image in the current window.
205With argument ARG not nil or 1, move forward ARG - 1 lines first,
206stopping if the top or bottom edge of the image is reached."
207 (interactive "p")
208 (and arg
209 (/= (setq arg (prefix-numeric-value arg)) 1)
210 (image-next-line (- arg 1)))
44e3c7c6 211 (image-set-window-hscroll 0))
bb0cb417
CY
212
213(defun image-eol (arg)
214 "Scroll horizontally to the right edge of the image in the current window.
215With argument ARG not nil or 1, move forward ARG - 1 lines first,
216stopping if the top or bottom edge of the image is reached."
217 (interactive "p")
218 (and arg
219 (/= (setq arg (prefix-numeric-value arg)) 1)
220 (image-next-line (- arg 1)))
91784462 221 (let* ((image (image-get-display-property))
bb0cb417
CY
222 (edges (window-inside-edges))
223 (win-width (- (nth 2 edges) (nth 0 edges)))
224 (img-width (ceiling (car (image-size image)))))
44e3c7c6 225 (image-set-window-hscroll (max 0 (- img-width win-width)))))
bb0cb417
CY
226
227(defun image-bob ()
228 "Scroll to the top-left corner of the image in the current window."
229 (interactive)
44e3c7c6
SM
230 (image-set-window-hscroll 0)
231 (image-set-window-vscroll 0))
bb0cb417
CY
232
233(defun image-eob ()
234 "Scroll to the bottom-right corner of the image in the current window."
235 (interactive)
91784462 236 (let* ((image (image-get-display-property))
bb0cb417
CY
237 (edges (window-inside-edges))
238 (win-width (- (nth 2 edges) (nth 0 edges)))
239 (img-width (ceiling (car (image-size image))))
240 (win-height (- (nth 3 edges) (nth 1 edges)))
241 (img-height (ceiling (cdr (image-size image)))))
44e3c7c6
SM
242 (image-set-window-hscroll (max 0 (- img-width win-width)))
243 (image-set-window-vscroll (max 0 (- img-height win-height)))))
bb0cb417 244
bd1d6a63
SM
245;; Adjust frame and image size.
246
247(defun image-mode-fit-frame ()
248 "Fit the frame to the current image.
249This function assumes the current frame has only one window."
250 ;; FIXME: This does not take into account decorations like mode-line,
251 ;; minibuffer, header-line, ...
252 (interactive)
253 (let* ((saved (frame-parameter nil 'image-mode-saved-size))
254 (display (image-get-display-property))
255 (size (image-size display)))
256 (if (and saved
257 (eq (caar saved) (frame-width))
258 (eq (cdar saved) (frame-height)))
259 (progn ;; Toggle back to previous non-fitted size.
260 (set-frame-parameter nil 'image-mode-saved-size nil)
261 (setq size (cdr saved)))
262 ;; Round up size, and save current size so we can toggle back to it.
263 (setcar size (ceiling (car size)))
264 (setcdr size (ceiling (cdr size)))
265 (set-frame-parameter nil 'image-mode-saved-size
266 (cons size (cons (frame-width) (frame-height)))))
267 (set-frame-width (selected-frame) (car size))
268 (set-frame-height (selected-frame) (cdr size))))
269
bb0cb417
CY
270;;; Image Mode setup
271
d487ca7d
JL
272(defvar image-type nil
273 "Current image type.
274This variable is used to display the current image type in the mode line.")
275(make-variable-buffer-local 'image-type)
276
c7bd5d57 277(defvar image-mode-map
bb0cb417
CY
278 (let ((map (make-sparse-keymap)))
279 (define-key map "\C-c\C-c" 'image-toggle-display)
280 (define-key map [remap forward-char] 'image-forward-hscroll)
281 (define-key map [remap backward-char] 'image-backward-hscroll)
282 (define-key map [remap previous-line] 'image-previous-line)
283 (define-key map [remap next-line] 'image-next-line)
284 (define-key map [remap scroll-up] 'image-scroll-up)
285 (define-key map [remap scroll-down] 'image-scroll-down)
286 (define-key map [remap move-beginning-of-line] 'image-bol)
287 (define-key map [remap move-end-of-line] 'image-eol)
288 (define-key map [remap beginning-of-buffer] 'image-bob)
289 (define-key map [remap end-of-buffer] 'image-eob)
290 map)
291 "Major mode keymap for viewing images in Image mode.")
292
293(defvar image-mode-text-map
c7bd5d57
RS
294 (let ((map (make-sparse-keymap)))
295 (define-key map "\C-c\C-c" 'image-toggle-display)
296 map)
bb0cb417 297 "Major mode keymap for viewing images as text in Image mode.")
c7bd5d57 298
e0385bf4 299(defvar bookmark-make-record-function)
a47d49c0 300
c7bd5d57
RS
301;;;###autoload
302(defun image-mode ()
303 "Major mode for image files.
304You can use \\<image-mode-map>\\[image-toggle-display]
305to toggle between display as an image and display as text."
306 (interactive)
307 (kill-all-local-variables)
d487ca7d 308 (setq mode-name "Image[text]")
c7bd5d57 309 (setq major-mode 'image-mode)
137187c8 310 ;; Use our own bookmarking function for images.
e0385bf4
KF
311 (set (make-local-variable 'bookmark-make-record-function)
312 'image-bookmark-make-record)
36e1c289
TH
313
314 ;; Keep track of [vh]scroll when switching buffers
44e3c7c6 315 (image-mode-setup-winprops)
36e1c289 316
d90e651e 317 (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t)
f992a935 318 (if (and (display-images-p)
91784462 319 (not (image-get-display-property)))
f992a935
CY
320 (image-toggle-display)
321 ;; Set next vars when image is already displayed but local
322 ;; variables were cleared by kill-all-local-variables
bb0cb417 323 (use-local-map image-mode-map)
f992a935 324 (setq cursor-type nil truncate-lines t))
ab145daf
CY
325 (run-mode-hooks 'image-mode-hook)
326 (if (display-images-p)
327 (message "%s" (concat
328 (substitute-command-keys
9f446ee9 329 "Type \\[image-toggle-display] to view as ")
91784462 330 (if (image-get-display-property)
ab145daf 331 "text" "an image") "."))))
d90e651e
JL
332
333;;;###autoload
334(define-minor-mode image-minor-mode
335 "Toggle Image minor mode.
336With arg, turn Image minor mode on if arg is positive, off otherwise.
337See the command `image-mode' for more information on this mode."
d487ca7d 338 nil (:eval (format " Image[%s]" image-type)) image-mode-text-map
d90e651e
JL
339 :group 'image
340 :version "22.1"
90d0be7d
JL
341 (if (not image-minor-mode)
342 (image-toggle-display-text)
91784462 343 (if (image-get-display-property)
d487ca7d
JL
344 (setq cursor-type nil truncate-lines t)
345 (setq image-type "text"))
90d0be7d 346 (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t)
8a26c165 347 (message "%s" (concat (substitute-command-keys
36e1c289 348 "Type \\[image-toggle-display] to view the image as ")
91784462 349 (if (image-get-display-property)
36e1c289 350 "text" "an image") "."))))
d90e651e
JL
351
352;;;###autoload
353(defun image-mode-maybe ()
354 "Set major or minor mode for image files.
355Set Image major mode only when there are no other major modes
356associated with a filename in `auto-mode-alist'. When an image
357filename matches another major mode in `auto-mode-alist' then
358set that major mode and Image minor mode.
359
360See commands `image-mode' and `image-minor-mode' for more
361information on these modes."
362 (interactive)
363 (let* ((mode-alist
364 (delq nil (mapcar
365 (lambda (elt)
366 (unless (memq (or (car-safe (cdr elt)) (cdr elt))
367 '(image-mode image-mode-maybe))
368 elt))
369 auto-mode-alist))))
370 (if (assoc-default buffer-file-name mode-alist 'string-match)
9fce2d71
JR
371 (let ((auto-mode-alist mode-alist)
372 (magic-mode-alist nil))
d90e651e
JL
373 (set-auto-mode)
374 (image-minor-mode t))
375 (image-mode))))
376
377(defun image-toggle-display-text ()
378 "Showing the text of the image file."
91784462 379 (if (image-get-display-property)
d90e651e 380 (image-toggle-display)))
c7bd5d57 381
f2920ffe
RS
382(defvar archive-superior-buffer)
383(defvar tar-superior-buffer)
384
c7bd5d57
RS
385(defun image-toggle-display ()
386 "Start or stop displaying an image file as the actual image.
387This command toggles between showing the text of the image file
388and showing the image as an image."
389 (interactive)
91784462 390 (if (image-get-display-property)
c7bd5d57 391 (let ((inhibit-read-only t)
837daa0d
RS
392 (buffer-undo-list t)
393 (modified (buffer-modified-p)))
c7bd5d57
RS
394 (remove-list-of-text-properties (point-min) (point-max)
395 '(display intangible read-nonsticky
396 read-only front-sticky))
837daa0d 397 (set-buffer-modified-p modified)
c7bd5d57
RS
398 (kill-local-variable 'cursor-type)
399 (kill-local-variable 'truncate-lines)
bb0cb417
CY
400 (kill-local-variable 'auto-hscroll-mode)
401 (use-local-map image-mode-text-map)
d487ca7d
JL
402 (setq image-type "text")
403 (if (eq major-mode 'image-mode)
404 (setq mode-name "Image[text]"))
d90e651e
JL
405 (if (called-interactively-p)
406 (message "Repeat this command to go back to displaying the image")))
c7bd5d57
RS
407 ;; Turn the image data into a real image, but only if the whole file
408 ;; was inserted
dc255b24 409 (let* ((filename (buffer-file-name))
d487ca7d
JL
410 (data-p (not (and filename
411 (file-readable-p filename)
412 (not (file-remote-p filename))
413 (not (buffer-modified-p))
414 (not (and (boundp 'archive-superior-buffer)
415 archive-superior-buffer))
416 (not (and (boundp 'tar-superior-buffer)
417 tar-superior-buffer)))))
418 (file-or-data (if data-p
419 (string-make-unibyte
420 (buffer-substring-no-properties (point-min) (point-max)))
421 filename))
422 (type (image-type file-or-data nil data-p))
423 (image (create-image file-or-data type data-p))
c7bd5d57
RS
424 (props
425 `(display ,image
36e1c289
TH
426 intangible ,image
427 rear-nonsticky (display intangible)
428 read-only t front-sticky (read-only)))
d90e651e 429 (inhibit-read-only t)
837daa0d
RS
430 (buffer-undo-list t)
431 (modified (buffer-modified-p)))
dc255b24 432 (image-refresh image)
c7bd5d57 433 (add-text-properties (point-min) (point-max) props)
837daa0d 434 (set-buffer-modified-p modified)
c7bd5d57
RS
435 ;; Inhibit the cursor when the buffer contains only an image,
436 ;; because cursors look very strange on top of images.
437 (setq cursor-type nil)
438 ;; This just makes the arrow displayed in the right fringe
439 ;; area look correct when the image is wider than the window.
440 (setq truncate-lines t)
bb0cb417
CY
441 ;; Allow navigation of large images
442 (set (make-local-variable 'auto-hscroll-mode) nil)
443 (use-local-map image-mode-map)
d487ca7d
JL
444 (setq image-type type)
445 (if (eq major-mode 'image-mode)
446 (setq mode-name (format "Image[%s]" type)))
d90e651e
JL
447 (if (called-interactively-p)
448 (message "Repeat this command to go back to displaying the file as text")))))
c7bd5d57 449
137187c8
TH
450;;; Support for bookmark.el
451
05d42c21 452(defun image-bookmark-make-record (annotation)
137187c8
TH
453 (let ((the-record
454 `((filename . ,(buffer-file-name))
455 (image-type . ,image-type)
456 (position . ,(point))
457 (handler . image-bookmark-jump))))
458
459 ;; Take no chances with text properties
460 (set-text-properties 0 (length annotation) nil annotation)
461
462 (when annotation
463 (nconc the-record (list (cons 'annotation annotation))))
464
465 ;; Finally, return the completed record.
466 the-record))
467
a47d49c0
GM
468(declare-function bookmark-get-filename "bookmark" (bookmark))
469(declare-function bookmark-get-bookmark-record "bookmark" (bookmark))
470(declare-function bookmark-get-position "bookmark" (bookmark))
471
137187c8
TH
472;;;###autoload
473(defun image-bookmark-jump (bmk)
03e26a79 474 ;; This implements the `handler' function interface for record type
e0385bf4 475 ;; returned by `bookmark-make-record-function', which see.
137187c8
TH
476 (save-window-excursion
477 (let ((filename (bookmark-get-filename bmk))
478 (type (cdr (assq 'image-type (bookmark-get-bookmark-record bmk))))
479 (pos (bookmark-get-position bmk)))
480 (find-file filename)
481 (when (not (string= image-type type))
482 (image-toggle-display))
483 (when (string= image-type "text")
484 (goto-char pos))
03e26a79 485 `((buffer ,(current-buffer)) (position ,(point))))))
137187c8 486
c7bd5d57
RS
487(provide 'image-mode)
488
8f7ee639 489;; arch-tag: b5b2b7e6-26a7-4b79-96e3-1546b5c4c6cb
c7bd5d57 490;;; image-mode.el ends here