- (when images
- (gnus-html-schedule-image-fetching (current-buffer) (nreverse images)))))
-
-(defun gnus-html-schedule-image-fetching (buffer images)
- (let* ((url (caar images))
- (process (start-process
- "images" nil "curl"
- "-s" "--create-dirs"
- "--location"
- "--max-time" "60"
- "-o" (gnus-html-image-id url)
- url)))
- (process-kill-without-query process)
- (set-process-sentinel process 'gnus-html-curl-sentinel)
- (set-process-plist process (list 'images images
- 'buffer buffer))))
-
-(defun gnus-html-image-id (url)
- (expand-file-name (sha1 url) gnus-html-cache-directory))
-
-(defun gnus-html-curl-sentinel (process event)
- (when (string-match "finished" event)
- (let* ((images (getf (process-plist process) 'images))
- (buffer (getf (process-plist process) 'buffer))
- (spec (pop images))
- (file (gnus-html-image-id (car spec))))
- (when (and (buffer-live-p buffer)
- ;; If the position of the marker is 1, then that
- ;; means that the text is was in has been deleted;
- ;; i.e., that the user has selected a different
- ;; article before the image arrived.
- (not (= (marker-position (cadr spec)) 1)))
- (save-excursion
- (set-buffer buffer)
- (let ((buffer-read-only nil))
- (when (gnus-html-put-image file (cadr spec))
- (delete-region (1+ (cadr spec)) (caddr spec))))))
- (when images
- (gnus-html-schedule-image-fetching buffer images)))))
-
-(defun gnus-html-put-image (file point)
- (let ((image (ignore-errors
- (create-image file))))
- (if (and image
- ;; Kludge to avoid displaying 30x30 gif images, which
- ;; seems to be a signal of a broken image.
- (not (and (eq (getf (cdr image) :type) 'gif)
- (= (car (image-size image t)) 30)
- (= (cdr (image-size image t)) 30))))
- (progn
- (gnus-put-image image nil nil point)
- t)
- (when (fboundp 'find-image)
- (gnus-put-image (find-image '((:type xpm :file "lock-broken.xpm")))
- nil nil point))
- nil)))
-
-(defun gnus-html-prune-cache ()
- (let ((total-size 0)
- files)
- (dolist (file (directory-files gnus-html-cache-directory t nil t))
- (let ((attributes (file-attributes file)))
- (unless (nth 0 attributes)
- (incf total-size (nth 7 attributes))
- (push (list (time-to-seconds (nth 5 attributes))
- (nth 7 attributes) file)
- files))))
- (when (> total-size gnus-html-cache-size)
- (setq files (sort files (lambda (f1 f2)
- (< (car f1) (car f2)))))
- (dolist (file files)
- (when (> total-size gnus-html-cache-size)
- (decf total-size (cadr file))
- (delete-file (nth 2 file)))))))
+ (mm-url-decode-entities)))
+
+(defun gnus-html-insert-image ()
+ "Fetch and insert the image under point."
+ (interactive)
+ (apply 'gnus-html-display-image (get-text-property (point) 'gnus-image)))
+
+(defun gnus-html-show-alt-text ()
+ "Show the ALT text of the image under point."
+ (interactive)
+ (message "%s" (get-text-property (point) 'gnus-alt-text)))
+
+(defun gnus-html-browse-image ()
+ "Browse the image under point."
+ (interactive)
+ (browse-url (get-text-property (point) 'gnus-image-url)))
+
+(defun gnus-html-browse-url ()
+ "Browse the image under point."
+ (interactive)
+ (let ((url (get-text-property (point) 'gnus-string)))
+ (if (not url)
+ (message "No URL at point")
+ (browse-url url))))
+
+(defun gnus-html-schedule-image-fetching (buffer image)
+ "Retrieve IMAGE, and place it into BUFFER on arrival."
+ (gnus-message 8 "gnus-html-schedule-image-fetching: buffer %s, image %s"
+ buffer image)
+ (ignore-errors
+ (url-retrieve (car image)
+ 'gnus-html-image-fetched
+ (list buffer image))))
+
+(defun gnus-html-image-fetched (status buffer image)
+ (url-store-in-cache (current-buffer))
+ (when (and (search-forward "\n\n" nil t)
+ (buffer-live-p buffer)
+ ;; If the `image' has no marker, do not replace anything
+ (cadr image)
+ ;; If the position of the marker is 1, then that
+ ;; means that the text it was in has been deleted;
+ ;; i.e., that the user has selected a different
+ ;; article before the image arrived.
+ (not (= (marker-position (cadr image))
+ (with-current-buffer buffer
+ (point-min)))))
+ (let ((data (buffer-substring (point) (point-max))))
+ (with-current-buffer buffer
+ (let ((inhibit-read-only t))
+ (gnus-html-put-image data (cadr image) (caddr image) (car image) (cadddr image))))))
+ (kill-buffer (current-buffer)))
+
+(defun gnus-html-get-image-data (url)
+ "Get image data for URL.
+Return a string with image data."
+ (with-temp-buffer
+ (mm-disable-multibyte)
+ (url-cache-extract (url-cache-create-filename url))
+ (when (search-forward "\n\n" nil t)
+ (buffer-substring (point) (point-max)))))
+
+(defun gnus-html-put-image (data start end &optional url alt-text)
+ (when (gnus-graphic-display-p)
+ (let* ((image (ignore-errors
+ (gnus-create-image data nil t)))
+ (size (and image
+ (if (featurep 'xemacs)
+ (cons (glyph-width image) (glyph-height image))
+ (image-size image t)))))
+ (save-excursion
+ (goto-char start)
+ (let ((alt-text (or alt-text (buffer-substring-no-properties start end))))
+ (if (and image
+ ;; Kludge to avoid displaying 30x30 gif images, which
+ ;; seems to be a signal of a broken image.
+ (not (and (if (featurep 'xemacs)
+ (glyphp image)
+ (listp image))
+ (eq (if (featurep 'xemacs)
+ (let ((d (cdadar (specifier-spec-list
+ (glyph-image image)))))
+ (and (vectorp d)
+ (aref d 0)))
+ (plist-get (cdr image) :type))
+ 'gif)
+ (= (car size) 30)
+ (= (cdr size) 30))))
+ ;; Good image, add it!
+ (let ((image (gnus-html-rescale-image image data size)))
+ (delete-region start end)
+ (gnus-put-image image alt-text 'external)
+ (gnus-overlay-put (gnus-make-overlay start (point)) 'local-map
+ gnus-html-displayed-image-map)
+ (gnus-put-text-property start (point) 'gnus-alt-text alt-text)
+ (when url
+ (gnus-put-text-property start (point) 'gnus-image-url url))
+ (gnus-add-image 'external image)
+ t)
+ ;; Bad image, try to show something else
+ (delete-region start end)
+ (when (fboundp 'find-image)
+ (setq image (find-image '((:type xpm :file "lock-broken.xpm"))))
+ (gnus-put-image image alt-text 'internal)
+ (gnus-add-image 'internal image))
+ nil))))))
+
+(defun gnus-html-rescale-image (image data size)
+ (if (or (not (fboundp 'imagemagick-types))
+ (not (get-buffer-window (current-buffer))))
+ image
+ (let* ((width (car size))
+ (height (cdr size))
+ (edges (window-pixel-edges (get-buffer-window (current-buffer))))
+ (window-width (truncate (* gnus-max-image-proportion
+ (- (nth 2 edges) (nth 0 edges)))))
+ (window-height (truncate (* gnus-max-image-proportion
+ (- (nth 3 edges) (nth 1 edges)))))
+ scaled-image)
+ (when (> height window-height)
+ (setq image (or (create-image data 'imagemagick t
+ :height window-height)
+ image))
+ (setq size (image-size image t)))
+ (when (> (car size) window-width)
+ (setq image (or
+ (create-image data 'imagemagick t
+ :width window-width)
+ image)))
+ image)))
+
+(defun gnus-html-image-url-blocked-p (url blocked-images)
+ "Find out if URL is blocked by BLOCKED-IMAGES."
+ (let ((ret (and blocked-images
+ (string-match blocked-images url))))
+ (if ret
+ (gnus-message 8 "gnus-html-image-url-blocked-p: %s blocked by regex %s"
+ url blocked-images)
+ (gnus-message 9 "gnus-html-image-url-blocked-p: %s passes regex %s"
+ url blocked-images))
+ ret))
+
+(defun gnus-html-show-images ()
+ "Show any images that are in the HTML-rendered article buffer.
+This only works if the article in question is HTML."
+ (interactive)
+ (gnus-with-article-buffer
+ (dolist (overlay (overlays-in (point-min) (point-max)))
+ (let ((o (overlay-get overlay 'gnus-image)))
+ (when o
+ (apply 'gnus-html-display-image o))))))