shr-color: only return hexadecimal part of colors
[gnus] / lisp / shr.el
index 03c0ec8..fc1286e 100644 (file)
@@ -32,6 +32,8 @@
 
 (eval-when-compile (require 'cl))
 (require 'browse-url)
+(unless (aref (char-category-set (make-char 'japanese-jisx0208 33 35)) ?>)
+  (load "kinsoku" nil t))
 
 (defgroup shr nil
   "Simple HTML Renderer"
@@ -53,29 +55,36 @@ fit these criteria."
   :group 'shr
   :type 'regexp)
 
-(defcustom shr-table-line ?-
-  "Character used to draw table line."
+(defcustom shr-table-horizontal-line ?-
+  "Character used to draw horizontal table lines."
   :group 'shr
-  :type 'char)
+  :type 'character)
+
+(defcustom shr-table-vertical-line ?|
+  "Character used to draw vertical table lines."
+  :group 'shr
+  :type 'character)
 
 (defcustom shr-table-corner ?+
-  "Character used to draw table corner."
+  "Character used to draw table corners."
   :group 'shr
-  :type 'char)
+  :type 'character)
 
 (defcustom shr-hr-line ?-
-  "Character used to draw hr line."
+  "Character used to draw hr lines."
   :group 'shr
-  :type 'char)
+  :type 'character)
+
+(defcustom shr-width fill-column
+  "Frame width to use for rendering."
+  :type 'integer
+  :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.")
 
-(defvar shr-width 70
-  "Frame width to use for rendering.")
-
 ;;; Internal variables.
 
 (defvar shr-folding-mode nil)
@@ -85,6 +94,8 @@ cid: URL as the argument.")
 (defvar shr-inhibit-images nil)
 (defvar shr-list-mode nil)
 (defvar shr-content-cache nil)
+(defvar shr-kinsoku-shorten nil)
+(defvar shr-table-depth 0)
 
 (defvar shr-map
   (let ((map (make-sparse-keymap)))
@@ -93,6 +104,7 @@ cid: URL as the argument.")
     (define-key map "I" 'shr-insert-image)
     (define-key map "u" 'shr-copy-url)
     (define-key map "v" 'shr-browse-url)
+    (define-key map "o" 'shr-save-contents)
     (define-key map "\r" 'shr-browse-url)
     map))
 
@@ -147,7 +159,7 @@ redirects somewhere else."
 (defun shr-browse-image ()
   "Browse the image under point."
   (interactive)
-  (let ((url (get-text-property (point) 'shr-image)))
+  (let ((url (get-text-property (point) 'image-url)))
     (if (not url)
        (message "No image under point")
       (message "Browsing %s..." url)
@@ -156,7 +168,7 @@ redirects somewhere else."
 (defun shr-insert-image ()
   "Insert the image under point into the buffer."
   (interactive)
-  (let ((url (get-text-property (point) 'shr-image)))
+  (let ((url (get-text-property (point) 'image-url)))
     (if (not url)
        (message "No image under point")
       (message "Inserting %s..." url)
@@ -174,20 +186,27 @@ redirects somewhere else."
            result))
     (dolist (sub dom)
       (if (stringp sub)
-         (push (cons :text sub) result)
+         (push (cons 'text sub) result)
        (push (shr-transform-dom sub) result)))
     (nreverse result)))
 
 (defun shr-descend (dom)
-  (let ((function (intern (concat "shr-tag-" (symbol-name (car dom))) obarray)))
+  (let ((function (intern (concat "shr-tag-" (symbol-name (car dom))) obarray))
+       (style (cdr (assq :style (cdr dom))))
+       (start (point)))
+    (when (and style
+              (string-match "color" style))
+      (setq style (shr-parse-style style)))
     (if (fboundp function)
        (funcall function (cdr dom))
-      (shr-generic (cdr dom)))))
+      (shr-generic (cdr dom)))
+    (when (consp style)
+      (shr-insert-color-overlay (cdr (assq 'color style)) start (point)))))
 
 (defun shr-generic (cont)
   (dolist (sub cont)
     (cond
-     ((eq (car sub) :text)
+     ((eq (car sub) 'text)
       (shr-insert (cdr sub)))
      ((listp (cdr sub))
       (shr-descend sub)))))
@@ -201,54 +220,94 @@ redirects somewhere else."
    ((eq shr-folding-mode 'none)
     (insert text))
    (t
-    (let ((first t)
-         column)
-      (when (and (string-match "\\`[ \t\n]" text)
-                (not (bolp))
-                (not (eq (char-after (1- (point))) ? )))
-       (insert " "))
-      (dolist (elem (split-string text))
-       (when (and (bolp)
-                  (> shr-indentation 0))
-         (shr-indent))
-       ;; The shr-start is a special variable that is used to pass
-       ;; upwards the first point in the buffer where the text really
-       ;; starts.
-       (unless shr-start
-         (setq shr-start (point)))
-       (insert elem)
-       (when (> (shr-current-column) shr-width)
-         (if (not (search-backward " " (line-beginning-position) t))
-             (insert "\n")
-           (delete-char 1)
-           (insert "\n")
+    (when (and (string-match "\\`[ \t\n]" text)
+              (not (bolp))
+              (not (eq (char-after (1- (point))) ? )))
+      (insert " "))
+    (dolist (elem (split-string text))
+      (when (and (bolp)
+                (> shr-indentation 0))
+       (shr-indent))
+      ;; The shr-start is a special variable that is used to pass
+      ;; upwards the first point in the buffer where the text really
+      ;; starts.
+      (unless shr-start
+       (setq shr-start (point)))
+      ;; No space is needed behind a wide character categorized as
+      ;; kinsoku-bol, between characters both categorized as nospace,
+      ;; or at the beginning of a line.
+      (let (prev)
+       (when (and (eq (preceding-char) ? )
+                  (or (= (line-beginning-position) (1- (point)))
+                      (and (aref fill-find-break-point-function-table
+                                 (setq prev (char-after (- (point) 2))))
+                           (aref (char-category-set prev) ?>))
+                      (and (aref fill-nospace-between-words-table prev)
+                           (aref fill-nospace-between-words-table
+                                 (aref elem 0)))))
+         (delete-char -1)))
+      (insert elem)
+      (let (found)
+       (while (and (> (current-column) shr-width)
+                   (progn
+                     (setq found (shr-find-fill-point))
+                     (not (eolp))))
+         (when (eq (preceding-char) ? )
+           (delete-char -1))
+         (insert "\n")
+         (unless found
            (put-text-property (1- (point)) (point) 'shr-break t)
-           (when (> shr-indentation 0)
-             (shr-indent))
-           (end-of-line)))
-       (insert " "))
-      (unless (string-match "[ \t\n]\\'" text)
-       (delete-char -1))))))
+           ;; No space is needed at the beginning of a line.
+           (when (eq (following-char) ? )
+             (delete-char 1)))
+         (when (> shr-indentation 0)
+           (shr-indent))
+         (end-of-line))
+       (insert " ")))
+    (unless (string-match "[ \t\n]\\'" text)
+      (delete-char -1)))))
 
 (defun shr-find-fill-point ()
-  (let ((found nil))
-    (while (and (not found)
-               (not (bolp)))
-      (when (or (eq (preceding-char) ? )
-               (aref fill-find-break-point-function-table (preceding-char)))
-       (setq found (point)))
+  (when (> (move-to-column shr-width) shr-width)
+    (backward-char 1))
+  (let (failed)
+    (while (not
+           (or (setq failed (= (current-column) shr-indentation))
+               (eq (preceding-char) ? )
+               (eq (following-char) ? )
+               (aref fill-find-break-point-function-table (preceding-char))))
       (backward-char 1))
-    (or found
-       (end-of-line))))
-
-(defun shr-current-column ()
-  (let ((column 0))
-    (save-excursion
-      (beginning-of-line)
-      (while (not (eolp))
-       (incf column (char-width (following-char)))
-       (forward-char 1)))
-    column))
+    (if failed
+       ;; There's no breakable point, so we give it up.
+       (progn
+         (end-of-line)
+         (while (aref fill-find-break-point-function-table (preceding-char))
+           (backward-char 1))
+         nil)
+      (or (eolp)
+         ;; Don't put kinsoku-bol characters at the beginning of a line,
+         ;; or kinsoku-eol characters at the end of a line,
+         (let ((count 4))
+           (if (or shr-kinsoku-shorten
+                   (and (aref (char-category-set (preceding-char)) ?<)
+                        (progn
+                          (setq count (1- count))
+                          (backward-char 1)
+                          t)))
+               (while (and
+                       (>= (setq count (1- count)) 0)
+                       (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
+                       (or (aref (char-category-set (preceding-char)) ?<)
+                           (aref (char-category-set (following-char)) ?>)))
+                 (backward-char 1))
+             (while (and (>= (setq count (1- count)) 0)
+                         (aref (char-category-set (following-char)) ?>)
+                         (aref fill-find-break-point-function-table
+                               (following-char)))
+               (forward-char 1)))
+           (when (eq (following-char) ? )
+             (forward-char 1))
+           t)))))
 
 (defun shr-ensure-newline ()
   (unless (zerop (current-column))
@@ -277,17 +336,49 @@ redirects somewhere else."
     (dolist (type types)
       (shr-add-font (or shr-start (point)) (point) type))))
 
+;; Add an overlay in the region, but avoid putting the font properties
+;; on blank text at the start of the line, and the newline at the end,
+;; to avoid ugliness.
 (defun shr-add-font (start end type)
-  (let ((overlay (make-overlay start end)))
-    (overlay-put overlay 'face type)))
+  (save-excursion
+    (goto-char start)
+    (while (< (point) end)
+      (when (bolp)
+       (skip-chars-forward " "))
+      (let ((overlay (make-overlay (point) (min (line-end-position) end))))
+       (overlay-put overlay 'face type))
+      (if (< (line-end-position) end)
+         (forward-line 1)
+       (goto-char end)))))
 
 (defun shr-browse-url ()
   "Browse the URL under point."
   (interactive)
+  (let ((url (get-text-property (point) 'shr-url)))
+    (cond
+     ((not url)
+      (message "No link under point"))
+     ((string-match "^mailto:" url)
+      (browse-url-mailto url))
+     (t
+      (browse-url url)))))
+
+(defun shr-save-contents (directory)
+  "Save the contents from URL in a file."
+  (interactive "DSave contents of URL to directory: ")
   (let ((url (get-text-property (point) 'shr-url)))
     (if (not url)
        (message "No link under point")
-      (browse-url url))))
+      (url-retrieve (shr-encode-url url)
+                   'shr-store-contents (list url directory)))))
+
+(defun shr-store-contents (status url directory)
+  (unless (plist-get status :error)
+    (when (or (search-forward "\n\n" nil t)
+             (search-forward "\r\n\r\n" nil t))
+      (write-region (point) (point-max)
+                   (expand-file-name (file-name-nondirectory url)
+                                     directory)))))
 
 (defun shr-image-fetched (status buffer start end)
   (when (and (buffer-name buffer)
@@ -300,16 +391,22 @@ redirects somewhere else."
           (let ((alt (buffer-substring start end))
                (inhibit-read-only t))
            (delete-region start end)
-           (shr-put-image data start alt))))))
+           (goto-char start)
+           (shr-put-image data alt))))))
   (kill-buffer (current-buffer)))
 
-(defun shr-put-image (data point alt)
-  (if (not (display-graphic-p))
-      (insert alt)
-    (let ((image (ignore-errors
-                  (shr-rescale-image data))))
-      (when image
-       (put-image image point alt)))))
+(defun shr-put-image (data alt)
+  (if (display-graphic-p)
+      (let ((image (ignore-errors
+                     (shr-rescale-image data))))
+        (when image
+         ;; When inserting big-ish pictures, put them at the
+         ;; beginning of the line.
+         (when (and (> (current-column) 0)
+                    (> (car (image-size image t)) 400))
+           (insert "\n"))
+         (insert-image image (or alt "*"))))
+    (insert alt)))
 
 (defun shr-rescale-image (data)
   (if (or (not (fboundp 'imagemagick-types))
@@ -338,6 +435,11 @@ redirects somewhere else."
                     image)))
       image)))
 
+;; url-cache-extract autoloads url-cache.
+(declare-function url-cache-create-filename "url-cache" (url))
+(autoload 'mm-disable-multibyte "mm-util")
+(autoload 'browse-url-mailto "browse-url")
+
 (defun shr-get-image-data (url)
   "Get image data for URL.
 Return a string with image data."
@@ -350,11 +452,60 @@ Return a string with image data."
                (search-forward "\r\n\r\n" nil t))
        (buffer-substring (point) (point-max))))))
 
+(defun shr-image-displayer (content-function)
+  "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."
+  `(lambda (url start end)
+     (when url
+       (if (string-match "\\`cid:" url)
+          ,(when content-function
+             `(let ((image (funcall ,content-function
+                                    (substring url (match-end 0)))))
+                (w