gnus-notifications: add actions support
[gnus] / lisp / gnus-html.el
index 4df9a0f..770904f 100644 (file)
@@ -1,6 +1,6 @@
 ;;; gnus-html.el --- Render HTML in a buffer.
 
-;; Copyright (C) 2010  Free Software Foundation, Inc.
+;; Copyright (C) 2010-2012  Free Software Foundation, Inc.
 
 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
 ;; Keywords: html, web
@@ -38,6 +38,7 @@
 (require 'url-cache)
 (require 'xml)
 (require 'browse-url)
+(require 'mm-util)
 (eval-and-compile (unless (featurep 'xemacs) (require 'help-fns)))
 
 (defcustom gnus-html-image-cache-ttl (days-to-time 7)
@@ -169,7 +170,14 @@ CHARS is a regexp-like character alternative (e.g., \"[)$]\")."
 
 (defun gnus-html-wash-images ()
   "Run through current buffer and replace img tags by images."
-  (let (tag parameters string start end images url alt-text)
+  (let (tag parameters string start end images url alt-text
+           inhibit-images blocked-images)
+    (if (buffer-live-p gnus-summary-buffer)
+       (with-current-buffer gnus-summary-buffer
+         (setq inhibit-images gnus-inhibit-images
+               blocked-images (gnus-blocked-images)))
+      (setq inhibit-images gnus-inhibit-images
+           blocked-images (gnus-blocked-images)))
     (goto-char (point-min))
     ;; Search for all the images first.
     (while (re-search-forward "<img_alt \\([^>]*\\)>" nil t)
@@ -192,29 +200,32 @@ CHARS is a regexp-like character alternative (e.g., \"[)$]\")."
                                   (gnus-html-display-image url start end
                                                            ,alt-text))
               'gnus-image (list url start end alt-text)))
-       (gnus-overlay-put (gnus-make-overlay start end)
-                         'local-map gnus-html-image-map)
+       (widget-convert-button
+        'url-link start (point)
+        :help-echo alt-text
+        :keymap gnus-html-image-map
+        url)
        (if (string-match "\\`cid:" url)
            ;; URLs with cid: have their content stashed in other
            ;; parts of the MIME structure, so just insert them
            ;; immediately.
            (let* ((handle (mm-get-content-id (substring url (match-end 0))))
                   (image (when (and handle
-                                    (not gnus-inhibit-images))
+                                    (not inhibit-images))
                            (gnus-create-image
                             (mm-with-part handle (buffer-string))
                             nil t))))
              (if image
-                 (progn
-                   (gnus-put-image
-                    (gnus-rescale-image
-                     image (gnus-html-maximum-image-size))
-                    (gnus-string-or (prog1
-                                        (buffer-substring start end)
-                                      (delete-region start end))
-                                    "*")
-                    'cid)
-                   (gnus-add-image 'cid image))
+                 (gnus-add-image
+                  'cid
+                  (gnus-put-image
+                   (gnus-rescale-image
+                    image (gnus-html-maximum-image-size))
+                   (gnus-string-or (prog1
+                                       (buffer-substring start end)
+                                     (delete-region start end))
+                                   "*")
+                   'cid))
                (widget-convert-button
                 'link start end
                 :action 'gnus-html-insert-image
@@ -222,13 +233,8 @@ CHARS is a regexp-like character alternative (e.g., \"[)$]\")."
                 :keymap gnus-html-image-map
                 :button-keymap gnus-html-image-map)))
          ;; Normal, external URL.
-         (if (or gnus-inhibit-images
-                 (gnus-html-image-url-blocked-p
-                  url
-                  (if (buffer-live-p gnus-summary-buffer)
-                      (with-current-buffer gnus-summary-buffer
-                        (gnus-blocked-images))
-                    (gnus-blocked-images))))
+         (if (or inhibit-images
+                 (gnus-html-image-url-blocked-p url blocked-images))
              (widget-convert-button
               'link start end
               :action 'gnus-html-insert-image
@@ -381,29 +387,28 @@ Use ALT-TEXT for the image string."
   "Retrieve IMAGE, and place it into BUFFER on arrival."
   (gnus-message 8 "gnus-html-schedule-image-fetching: buffer %s, image %s"
                 buffer image)
-  (let ((args (list (car image)
-                   'gnus-html-image-fetched
-                   (list buffer image))))
-    (when (> (length (if (featurep 'xemacs)
-                        (cdr (split-string (function-arglist 'url-retrieve)))
-                      (help-function-arglist 'url-retrieve)))
-            4)
-      (setq args (nconc args (list t))))
+  (if (fboundp 'url-queue-retrieve)
+      (url-queue-retrieve (car image)
+                         'gnus-html-image-fetched
+                         (list buffer image) t t)
     (ignore-errors
-      (apply #'url-retrieve args))))
+      (url-retrieve (car image)
+                   'gnus-html-image-fetched
+                   (list buffer image)))))
 
 (defun gnus-html-image-fetched (status buffer image)
   "Callback function called when image has been fetched."
   (unless (plist-get status :error)
-    (when gnus-html-image-automatic-caching
-      (url-store-in-cache (current-buffer)))
     (when (and (or (search-forward "\n\n" nil t)
                    (search-forward "\r\n\r\n" nil t))
-               (buffer-live-p buffer))
-      (let ((data (buffer-substring (point) (point-max))))
-        (with-current-buffer buffer
-          (let ((inhibit-read-only t))
-            (gnus-html-put-image data (car image) (cadr image)))))))
+              (not (eobp)))
+      (when gnus-html-image-automatic-caching
+       (url-store-in-cache (current-buffer)))
+      (when (buffer-live-p buffer)
+       (let ((data (buffer-substring (point) (point-max))))
+         (with-current-buffer buffer
+           (let ((inhibit-read-only t))
+             (gnus-html-put-image data (car image) (cadr image))))))))
   (kill-buffer (current-buffer)))
 
 (defun gnus-html-get-image-data (url)
@@ -471,15 +476,22 @@ Return a string with image data."
                   (let ((image (gnus-rescale-image image (gnus-html-maximum-image-size))))
                     (delete-region start end)
                     (gnus-put-image image alt-text 'external)
-                    (gnus-put-text-property start (point) 'help-echo alt-text)
-                    (gnus-overlay-put
-                    (gnus-make-overlay start (point)) 'local-map
-                    gnus-html-displayed-image-map)
+                   (widget-convert-button
+                    'url-link start (point)
+                    :help-echo alt-text
+                    :keymap gnus-html-displayed-image-map
+                    url)
                     (gnus-put-text-property start (point)
                                            'gnus-alt-text alt-text)
                     (when url
-                      (gnus-put-text-property start (point)
-                                             'image-url url))
+                     (gnus-add-text-properties
+                      start (point)
+                      `(image-url
+                        ,url
+                        image-displayer
+                        (lambda (url start end)
+                          (gnus-html-display-image url start end
+                                                   ,alt-text)))))
                     (gnus-add-image 'external image)
                     t)
                 ;; Bad image, try to show something else
@@ -505,13 +517,15 @@ Return a string with image data."
 ;;;###autoload
 (defun gnus-html-prefetch-images (summary)
   (when (buffer-live-p summary)
-    (let ((blocked-images (with-current-buffer summary
-                            (gnus-blocked-images))))
+    (let (inhibit-images blocked-images)
+      (with-current-buffer summary
+       (setq inhibit-images gnus-inhibit-images
+             blocked-images (gnus-blocked-images)))
       (save-match-data
        (while (re-search-forward "<img[^>]+src=[\"']\\(http[^\"']+\\)" nil t)
          (let ((url (gnus-html-encode-url
                      (mm-url-decode-entities-string (match-string 1)))))
-           (unless (or gnus-inhibit-images
+           (unless (or inhibit-images
                        (gnus-html-image-url-blocked-p url blocked-images))
               (when (gnus-html-cache-expired url gnus-html-image-cache-ttl)
                 (gnus-html-schedule-image-fetching nil