(defvar shr-indentation 0)
(defvar shr-inhibit-images nil)
(defvar shr-list-mode nil)
+(defvar shr-content-cache nil)
(defvar shr-map
(let ((map (make-sparse-keymap)))
;;;###autoload
(defun shr-insert-document (dom)
+ (setq shr-content-cache nil)
(let ((shr-state nil)
(shr-start nil))
(shr-descend (shr-transform-dom dom))))
(message "Browsing %s..." url)
(browse-url url))))
+(defun shr-insert-image ()
+ "Insert the image under point into the buffer."
+ (interactive)
+ (let ((url (get-text-property (point) 'shr-image)))
+ (if (not url)
+ (message "No image under point")
+ (message "Inserting %s..." url)
+ (url-retrieve url 'shr-image-fetched
+ (list (current-buffer) (1- (point)) (point-marker))
+ t))))
+
;;; Utility functions.
(defun shr-transform-dom (dom)
column)
(when (and (string-match "\\`[ \t\n]" text)
(not (bolp)))
- (insert " ")
- (setq shr-state 'space))
+ (insert " "))
(dolist (elem (split-string text))
- (setq column (current-column))
- (when (> column 0)
- (cond
- ((and (or (not first)
- (eq shr-state 'space))
- (> (+ column (length elem) 1) shr-width))
- (insert "\n")
- (put-text-property (1- (point)) (point) 'shr-break t))
- ((not first)
- (insert " "))))
- (setq first nil)
(when (and (bolp)
(> shr-indentation 0))
(shr-indent))
;; starts.
(unless shr-start
(setq shr-start (point)))
- (insert elem))
- (setq shr-state nil)
- (when (and (string-match "[ \t\n]\\'" text)
- (not (bolp)))
- (insert " ")
- (setq shr-state 'space))))))
+ (insert elem)
+ (when (> (current-column) shr-width)
+ (if (not (search-backward " " (line-beginning-position) t))
+ (insert "\n")
+ (delete-char 1)
+ (insert "\n")
+ (put-text-property (1- (point)) (point) 'shr-break t)
+ (when (> shr-indentation 0)
+ (shr-indent))
+ (end-of-line)))
+ (insert " "))))))
(defun shr-ensure-newline ()
(unless (zerop (current-column))
(defun shr-render-td (cont width fill)
(with-temp-buffer
- (let ((shr-width width)
- (shr-indentation 0))
- (shr-generic cont))
- (delete-region
- (point)
- (+ (point)
- (skip-chars-backward " \t\n")))
+ (let ((cache (cdr (assoc (cons width cont) shr-content-cache))))
+ (if cache
+ (insert cache)
+ (let ((shr-width width)
+ (shr-indentation 0))
+ (shr-generic cont))
+ (delete-region
+ (point)
+ (+ (point)
+ (skip-chars-backward " \t\n")))
+ (push (cons (cons width cont) (buffer-string))
+ shr-content-cache)))
(goto-char (point-min))
(let ((max 0))
(while (not (eobp))