1 ;; geiser-image.el -- support for image display
3 ;; Copyright (c) 2012, 2015 Jose Antonio Ortega Ruiz
5 ;; This program is free software; you can redistribute it and/or
6 ;; modify it under the terms of the Modified BSD License. You should
7 ;; have received a copy of the license along with this program. If
8 ;; not, see <http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5>.
10 ;; Authors: Michael Wilber, Jose Antonio Ortega Ruiz <jao@gnu.org>
11 ;; Start date: Sun Sep 02, 2012 00:00
15 (require 'geiser-custom
)
16 (require 'geiser-base
)
17 (require 'geiser-impl
)
22 (defgroup geiser-image nil
23 "Options for image displaying."
26 (geiser-custom--defcustom geiser-image-viewer
"display"
27 "Which system image viewer program to invoke upon M-x
28 `geiser-view-last-image'."
32 (geiser-custom--defcustom geiser-image-cache-keep-last
10
33 "How many images to keep in geiser's image cache."
37 (geiser-custom--defcustom geiser-image-cache-dir nil
38 "Default directory where generated images are stored.
40 If nil,the system wide tmp dir will be used."
44 (geiser-custom--defface image-button
45 'button geiser-image
"image buttons in terminal buffers")
47 (geiser-impl--define-caller geiser-image--cache-dir image-cache-dir
()
48 "Directory where generated images are stored. If this function
49 returns nil, no images are generated.")
53 (defun geiser-image--list-cache ()
54 "List all the images in the image cache."
55 (let ((cdir (geiser-image--cache-dir nil
)))
57 (file-directory-p cdir
)
58 (let ((files (directory-files-and-attributes cdir t
59 "geiser-img-[0-9]*.png")))
60 (mapcar 'car
(sort files
(lambda (a b
)
61 (< (float-time (nth 6 a
))
62 (float-time (nth 6 b
))))))))))
64 (defun geiser-image--clean-cache ()
65 "Clean all except for the last `geiser-image-cache-keep-last'
66 images in `geiser-image--cache-dir'."
68 (dolist (f (butlast (geiser-image--list-cache) geiser-image-cache-keep-last
))
71 (defun geiser-image--display (file)
72 (start-process "Geiser image view" nil geiser-image-viewer file
))
74 (defun geiser-image--button-action (button)
75 (let ((file (button-get button
'geiser-image-file
)))
76 (when (file-exists-p file
) (geiser-image--display file
))))
78 (define-button-type 'geiser-image--button
79 'action
'geiser-image--button-action
82 (defun geiser-image--insert-button (file)
83 (insert-text-button "[image]"
84 :type
'geiser-image--button
85 'face
'geiser-font-lock-image-button
86 'geiser-image-file file
87 'help-echo
"Click to display image"))
89 (defun geiser-image--replace-images (inline-images-p auto-p
)
90 "Replace all image patterns with actual images"
92 (with-silent-modifications
94 (goto-char (point-min))
95 (while (re-search-forward "\"?#<Image: \\([-+.\\\\/_:0-9a-zA-Z]+\\)>\"?"
97 (setq seen
(+ 1 seen
))
98 (let* ((file (match-string 1))
99 (begin (match-beginning 0))
101 (delete-region begin end
)
103 (if (and inline-images-p
(display-images-p))
104 (insert-image (create-image file
) "[image]")
105 (geiser-image--insert-button file
)
106 (when auto-p
(geiser-image--display file
)))))))
109 (defun geiser-view-last-image (n)
110 "Open the last displayed image in the system's image viewer.
112 With prefix arg, open the N-th last shown image in the system's
115 (let ((images (reverse (geiser-image--list-cache))))
116 (if (>= (length images
) n
)
117 (geiser-image--display (nth (- n
1) images
))
118 (error "There aren't %d recent images" n
))))
121 (provide 'geiser-image
)