(math-sin-raw): Add optional argument to keep track of
[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
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
b4aa6026 12;; the Free Software Foundation; either version 3, or (at your option)
c7bd5d57
RS
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
086add15
LK
22;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301, USA.
c7bd5d57
RS
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
d90e651e
JL
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)
4a2f0e99
JL
44
45;;;###autoload (push '("\\.x[bp]m\\'" . c-mode) auto-mode-alist)
d90e651e 46;;;###autoload (push '("\\.x[bp]m\\'" . image-mode-maybe) auto-mode-alist)
c7bd5d57 47
4a2f0e99
JL
48;;;###autoload (push '("\\.svgz?\\'" . xml-mode) auto-mode-alist)
49;;;###autoload (push '("\\.svgz?\\'" . image-mode-maybe) auto-mode-alist)
50
bb0cb417
CY
51;;; Image scrolling functions
52
54089c0e
TH
53(defvar image-mode-current-vscroll nil
54 "An alist with elements (WINDOW . VSCROLL).")
3488c456 55(make-variable-buffer-local 'image-mode-current-vscroll)
54089c0e
TH
56
57(defvar image-mode-current-hscroll nil
58 "An alist with elements (WINDOW . HSCROLL).")
3488c456 59(make-variable-buffer-local 'image-mode-current-hscroll)
36e1c289
TH
60
61(defun image-set-window-vscroll (window vscroll &optional pixels-p)
54089c0e 62 (setq image-mode-current-vscroll
3488c456
SM
63 (cons (cons window vscroll)
64 (delq (assq window image-mode-current-vscroll)
65 image-mode-current-vscroll)))
36e1c289
TH
66 (set-window-vscroll window vscroll pixels-p))
67
68(defun image-set-window-hscroll (window ncol)
54089c0e 69 (setq image-mode-current-hscroll
3488c456
SM
70 (cons (cons window ncol)
71 (delq (assq window image-mode-current-hscroll)
72 image-mode-current-hscroll)))
36e1c289
TH
73 (set-window-hscroll window ncol))
74
75(defun image-reset-current-vhscroll ()
3488c456
SM
76 (walk-windows
77 (lambda (win)
78 (with-current-buffer (window-buffer win)
79 ;; When set-window-buffer, set hscroll and vscroll to what they were
80 ;; last time the image was displayed in this window. If it's the first
81 ;; time it's displayed in this window, use the most recent setting.
82 (when image-mode-current-hscroll
83 (set-window-hscroll win (cdr (or (assoc win image-mode-current-hscroll)
84 (car image-mode-current-hscroll)))))
85 (when image-mode-current-vscroll
86 (set-window-vscroll win (cdr (or (assoc win image-mode-current-vscroll)
87 (car image-mode-current-vscroll)))))))
88 'nomini
89 (selected-frame)))
36e1c289 90
91784462
SM
91(defun image-get-display-property ()
92 (get-char-property (point-min) 'display
93 ;; There might be different images for different displays.
94 (if (eq (window-buffer) (current-buffer))
95 (selected-window))))
96
bb0cb417
CY
97(defun image-forward-hscroll (&optional n)
98 "Scroll image in current window to the left by N character widths.
99Stop if the right edge of the image is reached."
100 (interactive "p")
101 (cond ((= n 0) nil)
102 ((< n 0)
36e1c289
TH
103 (image-set-window-hscroll (selected-window)
104 (max 0 (+ (window-hscroll) n))))
bb0cb417 105 (t
91784462 106 (let* ((image (image-get-display-property))
bb0cb417
CY
107 (edges (window-inside-edges))
108 (win-width (- (nth 2 edges) (nth 0 edges)))
109 (img-width (ceiling (car (image-size image)))))
36e1c289
TH
110 (image-set-window-hscroll (selected-window)
111 (min (max 0 (- img-width win-width))
112 (+ n (window-hscroll))))))))
bb0cb417
CY
113
114(defun image-backward-hscroll (&optional n)
115 "Scroll image in current window to the right by N character widths.
116Stop if the left edge of the image is reached."
117 (interactive "p")
118 (image-forward-hscroll (- n)))
119
120(defun image-next-line (&optional n)
121 "Scroll image in current window upward by N lines.
122Stop if the bottom edge of the image is reached."
123 (interactive "p")
124 (cond ((= n 0) nil)
125 ((< n 0)
36e1c289
TH
126 (image-set-window-vscroll (selected-window)
127 (max 0 (+ (window-vscroll) n))))
bb0cb417 128 (t
91784462 129 (let* ((image (image-get-display-property))
bb0cb417
CY
130 (edges (window-inside-edges))
131 (win-height (- (nth 3 edges) (nth 1 edges)))
132 (img-height (ceiling (cdr (image-size image)))))
36e1c289
TH
133 (image-set-window-vscroll (selected-window)
134 (min (max 0 (- img-height win-height))
135 (+ n (window-vscroll))))))))
bb0cb417
CY
136
137(defun image-previous-line (&optional n)
138 "Scroll image in current window downward by N lines.
139Stop if the top edge of the image is reached."
140 (interactive "p")
141 (image-next-line (- n)))
142
143(defun image-scroll-up (&optional n)
144 "Scroll image in current window upward by N lines.
145Stop if the bottom edge of the image is reached.
146If ARG is omitted or nil, scroll upward by a near full screen.
147A near full screen is `next-screen-context-lines' less than a full screen.
148Negative ARG means scroll downward.
149If ARG is the atom `-', scroll downward by nearly full screen.
150When calling from a program, supply as argument a number, nil, or `-'."
151 (interactive "P")
152 (cond ((null n)
153 (let* ((edges (window-inside-edges))
154 (win-height (- (nth 3 edges) (nth 1 edges))))
155 (image-next-line
156 (max 0 (- win-height next-screen-context-lines)))))
157 ((eq n '-)
158 (let* ((edges (window-inside-edges))
159 (win-height (- (nth 3 edges) (nth 1 edges))))
160 (image-next-line
161 (min 0 (- next-screen-context-lines win-height)))))
162 (t (image-next-line (prefix-numeric-value n)))))
163
164(defun image-scroll-down (&optional n)
3488c456 165 "Scroll image in current window downward by N lines.
bb0cb417
CY
166Stop if the top edge of the image is reached.
167If ARG is omitted or nil, scroll downward by a near full screen.
168A near full screen is `next-screen-context-lines' less than a full screen.
169Negative ARG means scroll upward.
170If ARG is the atom `-', scroll upward by nearly full screen.
171When calling from a program, supply as argument a number, nil, or `-'."
172 (interactive "P")
173 (cond ((null n)
174 (let* ((edges (window-inside-edges))
175 (win-height (- (nth 3 edges) (nth 1 edges))))
176 (image-next-line
177 (min 0 (- next-screen-context-lines win-height)))))
178 ((eq n '-)
179 (let* ((edges (window-inside-edges))
180 (win-height (- (nth 3 edges) (nth 1 edges))))
181 (image-next-line
182 (max 0 (- win-height next-screen-context-lines)))))
183 (t (image-next-line (- (prefix-numeric-value n))))))
184
185(defun image-bol (arg)
186 "Scroll horizontally to the left edge of the image in the current window.
187With argument ARG not nil or 1, move forward ARG - 1 lines first,
188stopping if the top or bottom edge of the image is reached."
189 (interactive "p")
190 (and arg
191 (/= (setq arg (prefix-numeric-value arg)) 1)
192 (image-next-line (- arg 1)))
36e1c289 193 (image-set-window-hscroll (selected-window) 0))
bb0cb417
CY
194
195(defun image-eol (arg)
196 "Scroll horizontally to the right edge of the image in the current window.
197With argument ARG not nil or 1, move forward ARG - 1 lines first,
198stopping if the top or bottom edge of the image is reached."
199 (interactive "p")
200 (and arg
201 (/= (setq arg (prefix-numeric-value arg)) 1)
202 (image-next-line (- arg 1)))
91784462 203 (let* ((image (image-get-display-property))
bb0cb417
CY
204 (edges (window-inside-edges))
205 (win-width (- (nth 2 edges) (nth 0 edges)))
206 (img-width (ceiling (car (image-size image)))))
36e1c289
TH
207 (image-set-window-hscroll (selected-window)
208 (max 0 (- img-width win-width)))))
bb0cb417
CY
209
210(defun image-bob ()
211 "Scroll to the top-left corner of the image in the current window."
212 (interactive)
36e1c289
TH
213 (image-set-window-hscroll (selected-window) 0)
214 (image-set-window-vscroll (selected-window) 0))
bb0cb417
CY
215
216(defun image-eob ()
217 "Scroll to the bottom-right corner of the image in the current window."
218 (interactive)
91784462 219 (let* ((image (image-get-display-property))
bb0cb417
CY
220 (edges (window-inside-edges))
221 (win-width (- (nth 2 edges) (nth 0 edges)))
222 (img-width (ceiling (car (image-size image))))
223 (win-height (- (nth 3 edges) (nth 1 edges)))
224 (img-height (ceiling (cdr (image-size image)))))
36e1c289
TH
225 (image-set-window-hscroll (selected-window) (max 0 (- img-width win-width)))
226 (image-set-window-vscroll (selected-window) (max 0 (- img-height win-height)))))
bb0cb417
CY
227
228;;; Image Mode setup
229
d487ca7d
JL
230(defvar image-type nil
231 "Current image type.
232This variable is used to display the current image type in the mode line.")
233(make-variable-buffer-local 'image-type)
234
c7bd5d57 235(defvar image-mode-map
bb0cb417
CY
236 (let ((map (make-sparse-keymap)))
237 (define-key map "\C-c\C-c" 'image-toggle-display)
238 (define-key map [remap forward-char] 'image-forward-hscroll)
239 (define-key map [remap backward-char] 'image-backward-hscroll)
240 (define-key map [remap previous-line] 'image-previous-line)
241 (define-key map [remap next-line] 'image-next-line)
242 (define-key map [remap scroll-up] 'image-scroll-up)
243 (define-key map [remap scroll-down] 'image-scroll-down)
244 (define-key map [remap move-beginning-of-line] 'image-bol)
245 (define-key map [remap move-end-of-line] 'image-eol)
246 (define-key map [remap beginning-of-buffer] 'image-bob)
247 (define-key map [remap end-of-buffer] 'image-eob)
248 map)
249 "Major mode keymap for viewing images in Image mode.")
250
251(defvar image-mode-text-map
c7bd5d57
RS
252 (let ((map (make-sparse-keymap)))
253 (define-key map "\C-c\C-c" 'image-toggle-display)
254 map)
bb0cb417 255 "Major mode keymap for viewing images as text in Image mode.")
c7bd5d57 256
a47d49c0
GM
257(defvar bookmark-make-cell-function)
258
c7bd5d57
RS
259;;;###autoload
260(defun image-mode ()
261 "Major mode for image files.
262You can use \\<image-mode-map>\\[image-toggle-display]
263to toggle between display as an image and display as text."
264 (interactive)
265 (kill-all-local-variables)
d487ca7d 266 (setq mode-name "Image[text]")
c7bd5d57 267 (setq major-mode 'image-mode)
137187c8
TH
268 ;; Use our own bookmarking function for images.
269 (set (make-local-variable 'bookmark-make-cell-function)
270 'image-bookmark-make-cell)
36e1c289
TH
271
272 ;; Keep track of [vh]scroll when switching buffers
54089c0e
TH
273 (image-set-window-hscroll (selected-window) (window-hscroll))
274 (image-set-window-vscroll (selected-window) (window-vscroll))
36e1c289
TH
275 (add-hook 'window-configuration-change-hook
276 'image-reset-current-vhscroll nil t)
277
d90e651e 278 (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t)
f992a935 279 (if (and (display-images-p)
91784462 280 (not (image-get-display-property)))
f992a935
CY
281 (image-toggle-display)
282 ;; Set next vars when image is already displayed but local
283 ;; variables were cleared by kill-all-local-variables
bb0cb417 284 (use-local-map image-mode-map)
f992a935 285 (setq cursor-type nil truncate-lines t))
ab145daf
CY
286 (run-mode-hooks 'image-mode-hook)
287 (if (display-images-p)
288 (message "%s" (concat
289 (substitute-command-keys
9f446ee9 290 "Type \\[image-toggle-display] to view as ")
91784462 291 (if (image-get-display-property)
ab145daf 292 "text" "an image") "."))))
d90e651e
JL
293
294;;;###autoload
295(define-minor-mode image-minor-mode
296 "Toggle Image minor mode.
297With arg, turn Image minor mode on if arg is positive, off otherwise.
298See the command `image-mode' for more information on this mode."
d487ca7d 299 nil (:eval (format " Image[%s]" image-type)) image-mode-text-map
d90e651e
JL
300 :group 'image
301 :version "22.1"
90d0be7d
JL
302 (if (not image-minor-mode)
303 (image-toggle-display-text)
91784462 304 (if (image-get-display-property)
d487ca7d
JL
305 (setq cursor-type nil truncate-lines t)
306 (setq image-type "text"))
90d0be7d 307 (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t)
8a26c165 308 (message "%s" (concat (substitute-command-keys
36e1c289 309 "Type \\[image-toggle-display] to view the image as ")
91784462 310 (if (image-get-display-property)
36e1c289 311 "text" "an image") "."))))
d90e651e
JL
312
313;;;###autoload
314(defun image-mode-maybe ()
315 "Set major or minor mode for image files.
316Set Image major mode only when there are no other major modes
317associated with a filename in `auto-mode-alist'. When an image
318filename matches another major mode in `auto-mode-alist' then
319set that major mode and Image minor mode.
320
321See commands `image-mode' and `image-minor-mode' for more
322information on these modes."
323 (interactive)
324 (let* ((mode-alist
325 (delq nil (mapcar
326 (lambda (elt)
327 (unless (memq (or (car-safe (cdr elt)) (cdr elt))
328 '(image-mode image-mode-maybe))
329 elt))
330 auto-mode-alist))))
331 (if (assoc-default buffer-file-name mode-alist 'string-match)
9fce2d71
JR
332 (let ((auto-mode-alist mode-alist)
333 (magic-mode-alist nil))
d90e651e
JL
334 (set-auto-mode)
335 (image-minor-mode t))
336 (image-mode))))
337
338(defun image-toggle-display-text ()
339 "Showing the text of the image file."
91784462 340 (if (image-get-display-property)
d90e651e 341 (image-toggle-display)))
c7bd5d57 342
f2920ffe
RS
343(defvar archive-superior-buffer)
344(defvar tar-superior-buffer)
345
c7bd5d57
RS
346(defun image-toggle-display ()
347 "Start or stop displaying an image file as the actual image.
348This command toggles between showing the text of the image file
349and showing the image as an image."
350 (interactive)
91784462 351 (if (image-get-display-property)
c7bd5d57 352 (let ((inhibit-read-only t)
837daa0d
RS
353 (buffer-undo-list t)
354 (modified (buffer-modified-p)))
c7bd5d57
RS
355 (remove-list-of-text-properties (point-min) (point-max)
356 '(display intangible read-nonsticky
357 read-only front-sticky))
837daa0d 358 (set-buffer-modified-p modified)
c7bd5d57
RS
359 (kill-local-variable 'cursor-type)
360 (kill-local-variable 'truncate-lines)
bb0cb417
CY
361 (kill-local-variable 'auto-hscroll-mode)
362 (use-local-map image-mode-text-map)
d487ca7d
JL
363 (setq image-type "text")
364 (if (eq major-mode 'image-mode)
365 (setq mode-name "Image[text]"))
d90e651e
JL
366 (if (called-interactively-p)
367 (message "Repeat this command to go back to displaying the image")))
c7bd5d57
RS
368 ;; Turn the image data into a real image, but only if the whole file
369 ;; was inserted
dc255b24 370 (let* ((filename (buffer-file-name))
d487ca7d
JL
371 (data-p (not (and filename
372 (file-readable-p filename)
373 (not (file-remote-p filename))
374 (not (buffer-modified-p))
375 (not (and (boundp 'archive-superior-buffer)
376 archive-superior-buffer))
377 (not (and (boundp 'tar-superior-buffer)
378 tar-superior-buffer)))))
379 (file-or-data (if data-p
380 (string-make-unibyte
381 (buffer-substring-no-properties (point-min) (point-max)))
382 filename))
383 (type (image-type file-or-data nil data-p))
384 (image (create-image file-or-data type data-p))
c7bd5d57
RS
385 (props
386 `(display ,image
36e1c289
TH
387 intangible ,image
388 rear-nonsticky (display intangible)
389 read-only t front-sticky (read-only)))
d90e651e 390 (inhibit-read-only t)
837daa0d
RS
391 (buffer-undo-list t)
392 (modified (buffer-modified-p)))
dc255b24 393 (image-refresh image)
c7bd5d57 394 (add-text-properties (point-min) (point-max) props)
837daa0d 395 (set-buffer-modified-p modified)
c7bd5d57
RS
396 ;; Inhibit the cursor when the buffer contains only an image,
397 ;; because cursors look very strange on top of images.
398 (setq cursor-type nil)
399 ;; This just makes the arrow displayed in the right fringe
400 ;; area look correct when the image is wider than the window.
401 (setq truncate-lines t)
bb0cb417
CY
402 ;; Allow navigation of large images
403 (set (make-local-variable 'auto-hscroll-mode) nil)
404 (use-local-map image-mode-map)
d487ca7d
JL
405 (setq image-type type)
406 (if (eq major-mode 'image-mode)
407 (setq mode-name (format "Image[%s]" type)))
d90e651e
JL
408 (if (called-interactively-p)
409 (message "Repeat this command to go back to displaying the file as text")))))
c7bd5d57 410
137187c8
TH
411;;; Support for bookmark.el
412
413(defun image-bookmark-make-cell (annotation &rest args)
414 (let ((the-record
415 `((filename . ,(buffer-file-name))
416 (image-type . ,image-type)
417 (position . ,(point))
418 (handler . image-bookmark-jump))))
419
420 ;; Take no chances with text properties
421 (set-text-properties 0 (length annotation) nil annotation)
422
423 (when annotation
424 (nconc the-record (list (cons 'annotation annotation))))
425
426 ;; Finally, return the completed record.
427 the-record))
428
a47d49c0
GM
429(declare-function bookmark-get-filename "bookmark" (bookmark))
430(declare-function bookmark-get-bookmark-record "bookmark" (bookmark))
431(declare-function bookmark-get-position "bookmark" (bookmark))
432
137187c8
TH
433;;;###autoload
434(defun image-bookmark-jump (bmk)
03e26a79
KF
435 ;; This implements the `handler' function interface for record type
436 ;; returned by `bookmark-make-cell-function', which see.
137187c8
TH
437 (save-window-excursion
438 (let ((filename (bookmark-get-filename bmk))
439 (type (cdr (assq 'image-type (bookmark-get-bookmark-record bmk))))
440 (pos (bookmark-get-position bmk)))
441 (find-file filename)
442 (when (not (string= image-type type))
443 (image-toggle-display))
444 (when (string= image-type "text")
445 (goto-char pos))
03e26a79 446 `((buffer ,(current-buffer)) (position ,(point))))))
137187c8 447
c7bd5d57
RS
448(provide 'image-mode)
449
8f7ee639 450;; arch-tag: b5b2b7e6-26a7-4b79-96e3-1546b5c4c6cb
c7bd5d57 451;;; image-mode.el ends here