Fix typo.
[gnus] / lisp / shr.el
index be87ec6..f2d8f84 100644 (file)
@@ -1,6 +1,6 @@
 ;;; shr.el --- Simple HTML Renderer
 
-;; Copyright (C) 20102011 Free Software Foundation, Inc.
+;; Copyright (C) 2010-2011 Free Software Foundation, Inc.
 
 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
 ;; Keywords: html
@@ -53,17 +53,17 @@ fit these criteria."
   :group 'shr
   :type 'regexp)
 
-(defcustom shr-table-horizontal-line ?-
+(defcustom shr-table-horizontal-line ?\s
   "Character used to draw horizontal table lines."
   :group 'shr
   :type 'character)
 
-(defcustom shr-table-vertical-line ?|
+(defcustom shr-table-vertical-line ?\s
   "Character used to draw vertical table lines."
   :group 'shr
   :type 'character)
 
-(defcustom shr-table-corner ?+
+(defcustom shr-table-corner ?\s
   "Character used to draw table corners."
   :group 'shr
   :type 'character)
@@ -82,35 +82,23 @@ used."
                 (const   :tag "Use the width of the window" nil))
   :group 'shr)
 
-(defface shr-tag-h1 '((t (:bold t :height 2.2)))
-  "Face used for H1 tags."
-  :group 'shr)
-
-(defface shr-tag-h2 '((t (:bold t :height 2.0)))
-  "Face used for H2 tags."
-  :group 'shr)
-
-(defface shr-tag-h3 '((t (:bold t :height 1.8)))
-  "Face used for H3 tags."
-  :group 'shr)
+(defvar shr-content-function nil
+  "If bound, this should be a function that will return the content.
+This is used for cid: URLs, and the function is called with the
+cid: URL as the argument.")
 
-(defface shr-tag-h4 '((t (:bold t :height 1.6)))
-  "Face used for H4 tags."
-  :group 'shr)
+(defvar shr-put-image-function 'shr-put-image
+  "Function called to put image and alt string.")
 
-(defface shr-tag-h5 '((t (:bold t :height 1.4)))
-  "Face used for H5 tags."
+(defface shr-strike-through '((t (:strike-through t)))
+  "Font for <s> elements."
   :group 'shr)
 
-(defface shr-tag-h6 '((t (:bold t :height 1.2)))
-  "Face used for H6 tags."
+(defface shr-link
+  '((t (:inherit link)))
+  "Font for link elements."
   :group 'shr)
 
-(defvar shr-content-function nil
-  "If bound, this should be a function that will return the content.
-This is used for cid: URLs, and the function is called with the
-cid: URL as the argument.")
-
 ;;; Internal variables.
 
 (defvar shr-folding-mode nil)
@@ -123,6 +111,8 @@ cid: URL as the argument.")
 (defvar shr-kinsoku-shorten nil)
 (defvar shr-table-depth 0)
 (defvar shr-stylesheet nil)
+(defvar shr-base nil)
+(defvar shr-ignore-cache nil)
 
 (defvar shr-map
   (let ((map (make-sparse-keymap)))
@@ -137,11 +127,21 @@ cid: URL as the argument.")
 
 ;; Public functions and commands.
 
+(defun shr-visit-file (file)
+  (interactive "fHTML file name: ")
+  (pop-to-buffer "*html*")
+  (erase-buffer)
+  (shr-insert-document
+   (with-temp-buffer
+     (insert-file-contents file)
+     (libxml-parse-html-region (point-min) (point-max)))))
+
 ;;;###autoload
 (defun shr-insert-document (dom)
   (setq shr-content-cache nil)
   (let ((shr-state nil)
        (shr-start nil)
+       (shr-base nil)
        (shr-width (or shr-width (window-width))))
     (shr-descend (shr-transform-dom dom))))
 
@@ -184,14 +184,23 @@ redirects somewhere else."
        (message "No image under point")
       (message "%s" text))))
 
-(defun shr-browse-image ()
-  "Browse the image under point."
-  (interactive)
+(defun shr-browse-image (&optional copy-url)
+  "Browse the image under point.
+If COPY-URL (the prefix if called interactively) is non-nil, copy
+the URL of the image to the kill buffer instead."
+  (interactive "P")
   (let ((url (get-text-property (point) 'image-url)))
-    (if (not url)
-       (message "No image under point")
+    (cond
+     ((not url)
+      (message "No image under point"))
+     (copy-url
+      (with-temp-buffer
+       (insert url)
+       (copy-region-as-kill (point-min) (point-max))
+       (message "Copied %s" url)))
+     (t
       (message "Browsing %s..." url)
-      (browse-url url))))
+      (browse-url url)))))
 
 (defun shr-insert-image ()
   "Insert the image under point into the buffer."
@@ -407,6 +416,19 @@ redirects somewhere else."
         (forward-char 1))))
     (not failed)))
 
+(defun shr-expand-url (url)
+  (cond
+   ;; Absolute URL.
+   ((or (not url)
+       (string-match "\\`[a-z]*:" url)
+       (not shr-base))
+    url)
+   ((and (not (string-match "/\\'" shr-base))
+        (not (string-match "\\`/" url)))
+    (concat shr-base "/" url))
+   (t
+    (concat shr-base url))))
+
 (defun shr-ensure-newline ()
   (unless (zerop (current-column))
     (insert "\n")))
@@ -491,10 +513,11 @@ redirects somewhere else."
                  (inhibit-read-only t))
              (delete-region start end)
              (goto-char start)
-             (shr-put-image data alt)))))))
+             (funcall shr-put-image-function data alt)))))))
   (kill-buffer (current-buffer)))
 
 (defun shr-put-image (data alt)
+  "Put image DATA with a string ALT.  Return image."
   (if (display-graphic-p)
       (let ((image (ignore-errors
                      (shr-rescale-image data))))
@@ -504,14 +527,18 @@ redirects somewhere else."
          (when (and (> (current-column) 0)
                     (> (car (image-size image t)) 400))
            (insert "\n"))
-         (insert-image image (or alt "*"))))
+         (insert-image image (or alt "*"))
+         (when (image-animated-p image)
+           (image-animate image nil 60)))
+       image)
     (insert alt)))
 
 (defun shr-rescale-image (data)
   (if (or (not (fboundp 'imagemagick-types))
          (not (get-buffer-window (current-buffer))))
-      (create-image data nil t)
-    (let* ((image (create-image data nil t))
+      (create-image data nil t
+                   :ascent 100)
+    (let* ((image (create-image data nil t :ascent 100))
           (size (image-size image t))
           (width (car size))
           (height (cdr size))
@@ -530,11 +557,9 @@ redirects somewhere else."
       (when (> (car size) window-width)
        (setq image (or
                     (create-image data 'imagemagick t
-                                  :width window-width)
+                                  :width window-width
+                                  :ascent 100)
                     image)))
-      (when (and (fboundp 'create-animated-image)
-                (eq (image-type data nil t) 'gif))
-       (setq image (create-animated-image data 'gif t)))
       image)))
 
 ;; url-cache-extract autoloads url-cache.
@@ -558,7 +583,7 @@ Return a string with image data."
   "Return a function to display an image.
 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
 is an argument.  The function to be returned takes three arguments URL,
-START, and END.  Note that START and END should be merkers."
+START, and END.  Note that START and END should be markers."
   `(lambda (url start end)
      (when url
        (if (string-match "\\`cid:" url)
@@ -567,8 +592,8 @@ START, and END.  Note that START and END should be merkers."
                                     (substring url (match-end 0)))))
                 (when image
                   (goto-char start)
-                  (shr-put-image image
-                                 (buffer-substring-no-properties start end))
+                  (funcall shr-put-image-function
+                           image (buffer-substring start end))
                   (delete-region (point) end))))
         (url-retrieve url 'shr-image-fetched
                       (list (current-buffer) start end)
@@ -587,6 +612,7 @@ START, and END.  Note that START and END should be merkers."
    :help-echo (if title (format "%s (%s)" url title) url)
    :keymap shr-map
    url)
+  (shr-add-font start (point) 'shr-link)
   (put-text-property start (point) 'shr-url url))
 
 (defun shr-encode-url (url)
@@ -625,9 +651,10 @@ ones, in case fg and bg are nil."
        (when fg
          (shr-put-color start end :foreground (cadr new-colors)))
        (when bg
-         (shr-put-color start end :background (car new-colors)))))))
+         (shr-put-color