image-mode.el 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. ;;; image-mode.el --- support for visiting image files
  2. ;;
  3. ;; Copyright (C) 2005-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Richard Stallman <rms@gnu.org>
  6. ;; Keywords: multimedia
  7. ;; Package: emacs
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; Defines a major mode for visiting image files
  21. ;; that allows conversion between viewing the text of the file
  22. ;; and viewing the file as an image. Viewing the image
  23. ;; works by putting a `display' text-property on the
  24. ;; image data, with the image-data still present underneath; if the
  25. ;; resulting buffer file is saved to another name it will correctly save
  26. ;; the image data to the new file.
  27. ;;; Code:
  28. (require 'image)
  29. (eval-when-compile (require 'cl))
  30. ;;; Image mode window-info management.
  31. (defvar image-mode-winprops-alist t)
  32. (make-variable-buffer-local 'image-mode-winprops-alist)
  33. (defvar image-mode-new-window-functions nil
  34. "Special hook run when image data is requested in a new window.
  35. It is called with one argument, the initial WINPROPS.")
  36. (defun image-mode-winprops (&optional window cleanup)
  37. "Return winprops of WINDOW.
  38. A winprops object has the shape (WINDOW . ALIST)."
  39. (cond ((null window)
  40. (setq window (selected-window)))
  41. ((not (windowp window))
  42. (error "Not a window: %s" window)))
  43. (when cleanup
  44. (setq image-mode-winprops-alist
  45. (delq nil (mapcar (lambda (winprop)
  46. (if (window-live-p (car-safe winprop))
  47. winprop))
  48. image-mode-winprops-alist))))
  49. (let ((winprops (assq window image-mode-winprops-alist)))
  50. ;; For new windows, set defaults from the latest.
  51. (unless winprops
  52. (setq winprops (cons window
  53. (copy-alist (cdar image-mode-winprops-alist))))
  54. (run-hook-with-args 'image-mode-new-window-functions winprops))
  55. ;; Move window to front.
  56. (setq image-mode-winprops-alist
  57. (cons winprops (delq winprops image-mode-winprops-alist)))
  58. winprops))
  59. (defun image-mode-window-get (prop &optional winprops)
  60. (unless (consp winprops) (setq winprops (image-mode-winprops winprops)))
  61. (cdr (assq prop (cdr winprops))))
  62. (defsetf image-mode-window-get (prop &optional winprops) (val)
  63. `(image-mode-window-put ,prop ,val ,winprops))
  64. (defun image-mode-window-put (prop val &optional winprops)
  65. (unless (consp winprops) (setq winprops (image-mode-winprops winprops)))
  66. (setcdr winprops (cons (cons prop val)
  67. (delq (assq prop (cdr winprops)) (cdr winprops)))))
  68. (defun image-set-window-vscroll (vscroll)
  69. (setf (image-mode-window-get 'vscroll) vscroll)
  70. (set-window-vscroll (selected-window) vscroll))
  71. (defun image-set-window-hscroll (ncol)
  72. (setf (image-mode-window-get 'hscroll) ncol)
  73. (set-window-hscroll (selected-window) ncol))
  74. (defun image-mode-reapply-winprops ()
  75. ;; When set-window-buffer, set hscroll and vscroll to what they were
  76. ;; last time the image was displayed in this window.
  77. (when (and (image-get-display-property)
  78. (listp image-mode-winprops-alist))
  79. (let* ((winprops (image-mode-winprops nil t))
  80. (hscroll (image-mode-window-get 'hscroll winprops))
  81. (vscroll (image-mode-window-get 'vscroll winprops)))
  82. (if hscroll (set-window-hscroll (selected-window) hscroll))
  83. (if vscroll (set-window-vscroll (selected-window) vscroll)))))
  84. (defun image-mode-setup-winprops ()
  85. ;; Record current scroll settings.
  86. (unless (listp image-mode-winprops-alist)
  87. (setq image-mode-winprops-alist nil))
  88. (add-hook 'window-configuration-change-hook
  89. 'image-mode-reapply-winprops nil t))
  90. ;;; Image scrolling functions
  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. (declare-function image-size "image.c" (spec &optional pixels frame))
  97. (defun image-display-size (spec &optional pixels frame)
  98. "Wrapper around `image-size', handling slice display properties.
  99. Like `image-size', the return value is (WIDTH . HEIGHT).
  100. WIDTH and HEIGHT are in canonical character units if PIXELS is
  101. nil, and in pixel units if PIXELS is non-nil.
  102. If SPEC is an image display property, this function is equivalent
  103. to `image-size'. If SPEC is a list of properties containing
  104. `image' and `slice' properties, return the display size taking
  105. the slice property into account. If the list contains `image'
  106. but not `slice', return the `image-size' of the specified image."
  107. (if (eq (car spec) 'image)
  108. (image-size spec pixels frame)
  109. (let ((image (assoc 'image spec))
  110. (slice (assoc 'slice spec)))
  111. (cond ((and image slice)
  112. (if pixels
  113. (cons (nth 3 slice) (nth 4 slice))
  114. (cons (/ (float (nth 3 slice)) (frame-char-width frame))
  115. (/ (float (nth 4 slice)) (frame-char-height frame)))))
  116. (image
  117. (image-size image pixels frame))
  118. (t
  119. (error "Invalid image specification: %s" spec))))))
  120. (defun image-forward-hscroll (&optional n)
  121. "Scroll image in current window to the left by N character widths.
  122. Stop if the right edge of the image is reached."
  123. (interactive "p")
  124. (cond ((= n 0) nil)
  125. ((< n 0)
  126. (image-set-window-hscroll (max 0 (+ (window-hscroll) n))))
  127. (t
  128. (let* ((image (image-get-display-property))
  129. (edges (window-inside-edges))
  130. (win-width (- (nth 2 edges) (nth 0 edges)))
  131. (img-width (ceiling (car (image-display-size image)))))
  132. (image-set-window-hscroll (min (max 0 (- img-width win-width))
  133. (+ n (window-hscroll))))))))
  134. (defun image-backward-hscroll (&optional n)
  135. "Scroll image in current window to the right by N character widths.
  136. Stop if the left edge of the image is reached."
  137. (interactive "p")
  138. (image-forward-hscroll (- n)))
  139. (defun image-next-line (n)
  140. "Scroll image in current window upward by N lines.
  141. Stop if the bottom edge of the image is reached."
  142. (interactive "p")
  143. (cond ((= n 0) nil)
  144. ((< n 0)
  145. (image-set-window-vscroll (max 0 (+ (window-vscroll) n))))
  146. (t
  147. (let* ((image (image-get-display-property))
  148. (edges (window-inside-edges))
  149. (win-height (- (nth 3 edges) (nth 1 edges)))
  150. (img-height (ceiling (cdr (image-display-size image)))))
  151. (image-set-window-vscroll (min (max 0 (- img-height win-height))
  152. (+ n (window-vscroll))))))))
  153. (defun image-previous-line (&optional n)
  154. "Scroll image in current window downward by N lines.
  155. Stop if the top edge of the image is reached."
  156. (interactive "p")
  157. (image-next-line (- n)))
  158. (defun image-scroll-up (&optional n)
  159. "Scroll image in current window upward by N lines.
  160. Stop if the bottom edge of the image is reached.
  161. If ARG is omitted or nil, scroll upward by a near full screen.
  162. A near full screen is `next-screen-context-lines' less than a full screen.
  163. Negative ARG means scroll downward.
  164. If ARG is the atom `-', scroll downward by nearly full screen.
  165. When calling from a program, supply as argument a number, nil, or `-'."
  166. (interactive "P")
  167. (cond ((null n)
  168. (let* ((edges (window-inside-edges))
  169. (win-height (- (nth 3 edges) (nth 1 edges))))
  170. (image-next-line
  171. (max 0 (- win-height next-screen-context-lines)))))
  172. ((eq n '-)
  173. (let* ((edges (window-inside-edges))
  174. (win-height (- (nth 3 edges) (nth 1 edges))))
  175. (image-next-line
  176. (min 0 (- next-screen-context-lines win-height)))))
  177. (t (image-next-line (prefix-numeric-value n)))))
  178. (defun image-scroll-down (&optional n)
  179. "Scroll image in current window downward by N lines.
  180. Stop if the top edge of the image is reached.
  181. If ARG is omitted or nil, scroll downward by a near full screen.
  182. A near full screen is `next-screen-context-lines' less than a full screen.
  183. Negative ARG means scroll upward.
  184. If ARG is the atom `-', scroll upward by nearly full screen.
  185. When calling from a program, supply as argument a number, nil, or `-'."
  186. (interactive "P")
  187. (cond ((null n)
  188. (let* ((edges (window-inside-edges))
  189. (win-height (- (nth 3 edges) (nth 1 edges))))
  190. (image-next-line
  191. (min 0 (- next-screen-context-lines win-height)))))
  192. ((eq n '-)
  193. (let* ((edges (window-inside-edges))
  194. (win-height (- (nth 3 edges) (nth 1 edges))))
  195. (image-next-line
  196. (max 0 (- win-height next-screen-context-lines)))))
  197. (t (image-next-line (- (prefix-numeric-value n))))))
  198. (defun image-bol (arg)
  199. "Scroll horizontally to the left edge of the image in the current window.
  200. With argument ARG not nil or 1, move forward ARG - 1 lines first,
  201. stopping if the top or bottom edge of the image is reached."
  202. (interactive "p")
  203. (and arg
  204. (/= (setq arg (prefix-numeric-value arg)) 1)
  205. (image-next-line (- arg 1)))
  206. (image-set-window-hscroll 0))
  207. (defun image-eol (arg)
  208. "Scroll horizontally to the right edge of the image in the current window.
  209. With argument ARG not nil or 1, move forward ARG - 1 lines first,
  210. stopping if the top or bottom edge of the image is reached."
  211. (interactive "p")
  212. (and arg
  213. (/= (setq arg (prefix-numeric-value arg)) 1)
  214. (image-next-line (- arg 1)))
  215. (let* ((image (image-get-display-property))
  216. (edges (window-inside-edges))
  217. (win-width (- (nth 2 edges) (nth 0 edges)))
  218. (img-width (ceiling (car (image-display-size image)))))
  219. (image-set-window-hscroll (max 0 (- img-width win-width)))))
  220. (defun image-bob ()
  221. "Scroll to the top-left corner of the image in the current window."
  222. (interactive)
  223. (image-set-window-hscroll 0)
  224. (image-set-window-vscroll 0))
  225. (defun image-eob ()
  226. "Scroll to the bottom-right corner of the image in the current window."
  227. (interactive)
  228. (let* ((image (image-get-display-property))
  229. (edges (window-inside-edges))
  230. (win-width (- (nth 2 edges) (nth 0 edges)))
  231. (img-width (ceiling (car (image-display-size image))))
  232. (win-height (- (nth 3 edges) (nth 1 edges)))
  233. (img-height (ceiling (cdr (image-display-size image)))))
  234. (image-set-window-hscroll (max 0 (- img-width win-width)))
  235. (image-set-window-vscroll (max 0 (- img-height win-height)))))
  236. ;; Adjust frame and image size.
  237. (defun image-mode-fit-frame ()
  238. "Toggle whether to fit the frame to the current image.
  239. This function assumes the current frame has only one window."
  240. ;; FIXME: This does not take into account decorations like mode-line,
  241. ;; minibuffer, header-line, ...
  242. (interactive)
  243. (let* ((saved (frame-parameter nil 'image-mode-saved-size))
  244. (display (image-get-display-property))
  245. (size (image-display-size display)))
  246. (if (and saved
  247. (eq (caar saved) (frame-width))
  248. (eq (cdar saved) (frame-height)))
  249. (progn ;; Toggle back to previous non-fitted size.
  250. (set-frame-parameter nil 'image-mode-saved-size nil)
  251. (setq size (cdr saved)))
  252. ;; Round up size, and save current size so we can toggle back to it.
  253. (setcar size (ceiling (car size)))
  254. (setcdr size (ceiling (cdr size)))
  255. (set-frame-parameter nil 'image-mode-saved-size
  256. (cons size (cons (frame-width) (frame-height)))))
  257. (set-frame-width (selected-frame) (car size))
  258. (set-frame-height (selected-frame) (cdr size))))
  259. ;;; Image Mode setup
  260. (defvar image-type nil
  261. "The image type for the current Image mode buffer.")
  262. (make-variable-buffer-local 'image-type)
  263. (defvar image-mode-previous-major-mode nil
  264. "Internal variable to keep the previous non-image major mode.")
  265. (defvar image-mode-map
  266. (let ((map (make-sparse-keymap)))
  267. (set-keymap-parent map special-mode-map)
  268. (define-key map "\C-c\C-c" 'image-toggle-display)
  269. (define-key map (kbd "SPC") 'image-scroll-up)
  270. (define-key map (kbd "DEL") 'image-scroll-down)
  271. (define-key map (kbd "RET") 'image-toggle-animation)
  272. (define-key map [remap forward-char] 'image-forward-hscroll)
  273. (define-key map [remap backward-char] 'image-backward-hscroll)
  274. (define-key map [remap right-char] 'image-forward-hscroll)
  275. (define-key map [remap left-char] 'image-backward-hscroll)
  276. (define-key map [remap previous-line] 'image-previous-line)
  277. (define-key map [remap next-line] 'image-next-line)
  278. (define-key map [remap scroll-up] 'image-scroll-up)
  279. (define-key map [remap scroll-down] 'image-scroll-down)
  280. (define-key map [remap scroll-up-command] 'image-scroll-up)
  281. (define-key map [remap scroll-down-command] 'image-scroll-down)
  282. (define-key map [remap move-beginning-of-line] 'image-bol)
  283. (define-key map [remap move-end-of-line] 'image-eol)
  284. (define-key map [remap beginning-of-buffer] 'image-bob)
  285. (define-key map [remap end-of-buffer] 'image-eob)
  286. map)
  287. "Mode keymap for `image-mode'.")
  288. (defvar image-minor-mode-map
  289. (let ((map (make-sparse-keymap)))
  290. (define-key map "\C-c\C-c" 'image-toggle-display)
  291. map)
  292. "Mode keymap for `image-minor-mode'.")
  293. (defvar bookmark-make-record-function)
  294. (put 'image-mode 'mode-class 'special)
  295. ;;;###autoload
  296. (defun image-mode ()
  297. "Major mode for image files.
  298. You can use \\<image-mode-map>\\[image-toggle-display]
  299. to toggle between display as an image and display as text."
  300. (interactive)
  301. (condition-case err
  302. (progn
  303. (unless (display-images-p)
  304. (error "Display does not support images"))
  305. (kill-all-local-variables)
  306. (setq major-mode 'image-mode)
  307. (if (not (image-get-display-property))
  308. (progn
  309. (image-toggle-display-image)
  310. ;; If attempt to display the image fails.
  311. (if (not (image-get-display-property))
  312. (error "Invalid image")))
  313. ;; Set next vars when image is already displayed but local
  314. ;; variables were cleared by kill-all-local-variables
  315. (setq cursor-type nil truncate-lines t
  316. image-type (plist-get (cdr (image-get-display-property)) :type)))
  317. (setq mode-name (if image-type (format "Image[%s]" image-type) "Image"))
  318. (use-local-map image-mode-map)
  319. ;; Use our own bookmarking function for images.
  320. (set (make-local-variable 'bookmark-make-record-function)
  321. 'image-bookmark-make-record)
  322. ;; Keep track of [vh]scroll when switching buffers
  323. (image-mode-setup-winprops)
  324. (add-hook 'change-major-mode-hook 'image-toggle-display-text nil t)
  325. (add-hook 'after-revert-hook 'image-after-revert-hook nil t)
  326. (run-mode-hooks 'image-mode-hook)
  327. (let ((image (image-get-display-property))
  328. (msg1 (substitute-command-keys
  329. "Type \\[image-toggle-display] to view the image as ")))
  330. (cond
  331. ((null image)
  332. (message "%s" (concat msg1 "an image.")))
  333. ((image-animated-p image)
  334. (message "%s"
  335. (concat msg1 "text, or "
  336. (substitute-command-keys
  337. "\\[image-toggle-animation] to animate."))))
  338. (t
  339. (message "%s" (concat msg1 "text."))))))
  340. (error
  341. (image-mode-as-text)
  342. (funcall
  343. (if (called-interactively-p 'any) 'error 'message)
  344. "Cannot display image: %s" (cdr err)))))
  345. ;;;###autoload
  346. (define-minor-mode image-minor-mode
  347. "Toggle Image minor mode in this buffer.
  348. With a prefix argument ARG, enable Image minor mode if ARG is
  349. positive, and disable it otherwise. If called from Lisp, enable
  350. the mode if ARG is omitted or nil.
  351. Image minor mode provides the key \\<image-mode-map>\\[image-toggle-display],
  352. to switch back to `image-mode' and display an image file as the
  353. actual image."
  354. nil (:eval (if image-type (format " Image[%s]" image-type) " Image"))
  355. image-minor-mode-map
  356. :group 'image
  357. :version "22.1"
  358. (if image-minor-mode
  359. (add-hook 'change-major-mode-hook (lambda () (image-minor-mode -1)) nil t)))
  360. ;;;###autoload
  361. (defun image-mode-as-text ()
  362. "Set a non-image mode as major mode in combination with image minor mode.
  363. A non-image major mode found from `auto-mode-alist' or Fundamental mode
  364. displays an image file as text. `image-minor-mode' provides the key
  365. \\<image-mode-map>\\[image-toggle-display] to switch back to `image-mode'
  366. to display an image file as the actual image.
  367. You can use `image-mode-as-text' in `auto-mode-alist' when you want
  368. to display an image file as text initially.
  369. See commands `image-mode' and `image-minor-mode' for more information
  370. on these modes."
  371. (interactive)
  372. ;; image-mode-as-text = normal-mode + image-minor-mode
  373. (let ((previous-image-type image-type)) ; preserve `image-type'
  374. (if image-mode-previous-major-mode
  375. ;; Restore previous major mode that was already found by this
  376. ;; function and cached in `image-mode-previous-major-mode'
  377. (funcall image-mode-previous-major-mode)
  378. (let ((auto-mode-alist
  379. (delq nil (mapcar
  380. (lambda (elt)
  381. (unless (memq (or (car-safe (cdr elt)) (cdr elt))
  382. '(image-mode image-mode-maybe image-mode-as-text))
  383. elt))
  384. auto-mode-alist)))
  385. (magic-fallback-mode-alist
  386. (delq nil (mapcar
  387. (lambda (elt)
  388. (unless (memq (or (car-safe (cdr elt)) (cdr elt))
  389. '(image-mode image-mode-maybe image-mode-as-text))
  390. elt))
  391. magic-fallback-mode-alist))))
  392. (normal-mode)
  393. (set (make-local-variable 'image-mode-previous-major-mode) major-mode)))
  394. ;; Restore `image-type' after `kill-all-local-variables' in `normal-mode'.
  395. (setq image-type previous-image-type)
  396. ;; Enable image minor mode with `C-c C-c'.
  397. (image-minor-mode 1)
  398. ;; Show the image file as text.
  399. (image-toggle-display-text)
  400. (message "%s" (concat
  401. (substitute-command-keys
  402. "Type \\[image-toggle-display] to view the image as ")
  403. (if (image-get-display-property)
  404. "text" "an image") "."))))
  405. (define-obsolete-function-alias 'image-mode-maybe 'image-mode "23.2")
  406. (defun image-toggle-display-text ()
  407. "Show the image file as text.
  408. Remove text properties that display the image."
  409. (let ((inhibit-read-only t)
  410. (buffer-undo-list t)
  411. (modified (buffer-modified-p)))
  412. (remove-list-of-text-properties (point-min) (point-max)
  413. '(display read-nonsticky ;; intangible
  414. read-only front-sticky))
  415. (set-buffer-modified-p modified)
  416. (if (called-interactively-p 'any)
  417. (message "Repeat this command to go back to displaying the image"))))
  418. (defvar archive-superior-buffer)
  419. (defvar tar-superior-buffer)
  420. (declare-function image-flush "image.c" (spec &optional frame))
  421. (defun image-toggle-display-image ()
  422. "Show the image of the image file.
  423. Turn the image data into a real image, but only if the whole file
  424. was inserted."
  425. (unless (derived-mode-p 'image-mode)
  426. (error "The buffer is not in Image mode"))
  427. (let* ((filename (buffer-file-name))
  428. (data-p (not (and filename
  429. (file-readable-p filename)
  430. (not (file-remote-p filename))
  431. (not (buffer-modified-p))
  432. (not (and (boundp 'archive-superior-buffer)
  433. archive-superior-buffer))
  434. (not (and (boundp 'tar-superior-buffer)
  435. tar-superior-buffer)))))
  436. (file-or-data (if data-p
  437. (string-make-unibyte
  438. (buffer-substring-no-properties (point-min) (point-max)))
  439. filename))
  440. (type (image-type file-or-data nil data-p))
  441. (image (create-image file-or-data type data-p))
  442. (inhibit-read-only t)
  443. (buffer-undo-list t)
  444. (modified (buffer-modified-p))
  445. props)
  446. ;; Discard any stale image data before looking it up again.
  447. (image-flush image)
  448. (setq image (append image (image-transform-properties image)))
  449. (setq props
  450. `(display ,image
  451. ;; intangible ,image
  452. rear-nonsticky (display) ;; intangible
  453. read-only t front-sticky (read-only)))
  454. (let ((buffer-file-truename nil)) ; avoid changing dir mtime by lock_file
  455. (add-text-properties (point-min) (point-max) props)
  456. (restore-buffer-modified-p modified))
  457. ;; Inhibit the cursor when the buffer contains only an image,
  458. ;; because cursors look very strange on top of images.
  459. (setq cursor-type nil)
  460. ;; This just makes the arrow displayed in the right fringe
  461. ;; area look correct when the image is wider than the window.
  462. (setq truncate-lines t)
  463. ;; Disable adding a newline at the end of the image file when it
  464. ;; is written with, e.g., C-x C-w.
  465. (if (coding-system-equal (coding-system-base buffer-file-coding-system)
  466. 'no-conversion)
  467. (set (make-local-variable 'find-file-literally) t))
  468. ;; Allow navigation of large images
  469. (set (make-local-variable 'auto-hscroll-mode) nil)
  470. (setq image-type type)
  471. (if (eq major-mode 'image-mode)
  472. (setq mode-name (format "Image[%s]" type)))
  473. (if (called-interactively-p 'any)
  474. (message "Repeat this command to go back to displaying the file as text"))))
  475. (defun image-toggle-display ()
  476. "Toggle between image and text display.
  477. If the current buffer is displaying an image file as an image,
  478. call `image-mode-as-text' to switch to text. Otherwise, display
  479. the image by calling `image-mode'."
  480. (interactive)
  481. (if (image-get-display-property)
  482. (image-mode-as-text)
  483. (image-mode)))
  484. (defun image-after-revert-hook ()
  485. (when (image-get-display-property)
  486. (image-toggle-display-text)
  487. ;; Update image display.
  488. (mapc (lambda (window) (redraw-frame (window-frame window)))
  489. (get-buffer-window-list (current-buffer) 'nomini 'visible))
  490. (image-toggle-display-image)))
  491. ;;; Animated images
  492. (defcustom image-animate-loop nil
  493. "Non-nil means animated images loop forever, rather than playing once."
  494. :type 'boolean
  495. :version "24.1"
  496. :group 'image)
  497. (defun image-toggle-animation ()
  498. "Start or stop animating the current image.
  499. If `image-animate-loop' is non-nil, animation loops forever.
  500. Otherwise it plays once, then stops."
  501. (interactive)
  502. (let ((image (image-get-display-property))
  503. animation)
  504. (cond
  505. ((null image)
  506. (error "No image is present"))
  507. ((null (setq animation (image-animated-p image)))
  508. (message "No image animation."))
  509. (t
  510. (let ((timer (image-animate-timer image)))
  511. (if timer
  512. (cancel-timer timer)
  513. (let ((index (plist-get (cdr image) :index)))
  514. ;; If we're at the end, restart.
  515. (and index
  516. (>= index (1- (car animation)))
  517. (setq index nil))
  518. (image-animate image index
  519. (if image-animate-loop t)))))))))
  520. ;;; Support for bookmark.el
  521. (declare-function bookmark-make-record-default
  522. "bookmark" (&optional no-file no-context posn))
  523. (declare-function bookmark-prop-get "bookmark" (bookmark prop))
  524. (declare-function bookmark-default-handler "bookmark" (bmk))
  525. (defun image-bookmark-make-record ()
  526. `(,@(bookmark-make-record-default nil 'no-context 0)
  527. (image-type . ,image-type)
  528. (handler . image-bookmark-jump)))
  529. ;;;###autoload
  530. (defun image-bookmark-jump (bmk)
  531. ;; This implements the `handler' function interface for record type
  532. ;; returned by `bookmark-make-record-function', which see.
  533. (prog1 (bookmark-default-handler bmk)
  534. (when (not (string= image-type (bookmark-prop-get bmk 'image-type)))
  535. (image-toggle-display))))
  536. ;; Not yet implemented.
  537. ;;; (defvar image-transform-minor-mode-map
  538. ;;; (let ((map (make-sparse-keymap)))
  539. ;;; ;; (define-key map [(control ?+)] 'image-scale-in)
  540. ;;; ;; (define-key map [(control ?-)] 'image-scale-out)
  541. ;;; ;; (define-key map [(control ?=)] 'image-scale-none)
  542. ;;; ;; (define-key map "c f h" 'image-scale-fit-height)
  543. ;;; ;; (define-key map "c ]" 'image-rotate-right)
  544. ;;; map)
  545. ;;; "Minor mode keymap `image-transform-mode'.")
  546. ;;;
  547. ;;; (define-minor-mode image-transform-mode
  548. ;;; "Minor mode for scaling and rotating images.
  549. ;;; With a prefix argument ARG, enable the mode if ARG is positive,
  550. ;;; and disable it otherwise. If called from Lisp, enable the mode
  551. ;;; if ARG is omitted or nil. This minor mode requires Emacs to have
  552. ;;; been compiled with ImageMagick support."
  553. ;;; nil "image-transform" image-transform-minor-mode-map)
  554. ;; FIXME this doesn't seem mature yet. Document in manual when it is.
  555. (defvar image-transform-resize nil
  556. "The image resize operation.
  557. Its value should be one of the following:
  558. - nil, meaning no resizing.
  559. - `fit-height', meaning to fit the image to the window height.
  560. - `fit-width', meaning to fit the image to the window width.
  561. - A number, which is a scale factor (the default size is 100).")
  562. (defvar image-transform-rotation 0.0
  563. "Rotation angle for the image in the current Image mode buffer.")
  564. (defun image-transform-properties (spec)
  565. "Return rescaling/rotation properties for image SPEC.
  566. These properties are determined by the Image mode variables
  567. `image-transform-resize' and `image-transform-rotation'. The
  568. return value is suitable for appending to an image spec.
  569. Rescaling and rotation properties only take effect if Emacs is
  570. compiled with ImageMagick support."
  571. (when (or image-transform-resize
  572. (not (equal image-transform-rotation 0.0)))
  573. ;; Note: `image-size' looks up and thus caches the untransformed
  574. ;; image. There's no easy way to prevent that.
  575. (let* ((size (image-size spec t))
  576. (height
  577. (cond
  578. ((numberp image-transform-resize)
  579. (unless (= image-transform-resize 100)
  580. (* image-transform-resize (cdr size))))
  581. ((eq image-transform-resize 'fit-height)
  582. (- (nth 3 (window-inside-pixel-edges))
  583. (nth 1 (window-inside-pixel-edges))))))
  584. (width (if (eq image-transform-resize 'fit-width)
  585. (- (nth 2 (window-inside-pixel-edges))
  586. (nth 0 (window-inside-pixel-edges))))))
  587. ;;TODO fit-to-* should consider the rotation angle
  588. `(,@(if height (list :height height))
  589. ,@(if width (list :width width))
  590. ,@(if (not (equal 0.0 image-transform-rotation))
  591. (list :rotation image-transform-rotation))))))
  592. ;; FIXME 2 works, but eg 1.9 or 0.5 don't?
  593. (defun image-transform-set-scale (scale)
  594. "Prompt for a number, and resize the current image by that amount.
  595. This command has no effect unless Emacs is compiled with
  596. ImageMagick support."
  597. (interactive "nScale: ")
  598. (setq image-transform-resize scale)
  599. (image-toggle-display-image))
  600. (defun image-transform-fit-to-height ()
  601. "Fit the current image to the height of the current window.
  602. This command has no effect unless Emacs is compiled with
  603. ImageMagick support."
  604. (interactive)
  605. (setq image-transform-resize 'fit-height)
  606. (image-toggle-display-image))
  607. (defun image-transform-fit-to-width ()
  608. "Fit the current image to the width of the current window.
  609. This command has no effect unless Emacs is compiled with
  610. ImageMagick support."
  611. (interactive)
  612. (setq image-transform-resize 'fit-width)
  613. (image-toggle-display-image))
  614. (defun image-transform-set-rotation (rotation)
  615. "Prompt for an angle ROTATION, and rotate the image by that amount.
  616. ROTATION should be in degrees. This command has no effect unless
  617. Emacs is compiled with ImageMagick support."
  618. (interactive "nRotation angle (in degrees): ")
  619. ;;TODO 0 90 180 270 degrees are the only reasonable angles here
  620. ;;otherwise combining with rescaling will get very awkward
  621. (setq image-transform-rotation (float rotation))
  622. (image-toggle-display-image))
  623. (provide 'image-mode)
  624. ;;; image-mode.el ends here