;;; Code:
+(eval-when-compile (require 'cl))
(require 'browse-url)
(defgroup shr nil
:group 'shr
:type 'regexp)
+(defcustom shr-table-line ?-
+ "Character used to draw table line."
+ :group 'shr
+ :type 'char)
+
+(defcustom shr-table-corner ?+
+ "Character used to draw table corner."
+ :group 'shr
+ :type 'char)
+
+(defcustom shr-hr-line ?-
+ "Character used to draw hr line."
+ :group 'shr
+ :type 'char)
+
(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
(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)
(shr-descend sub)))))
(defun shr-insert (text)
- (when (eq shr-state 'image)
+ (when (and (eq shr-state 'image)
+ (not (string-match "\\`[ \t\n]+\\'" text)))
(insert "\n")
(setq shr-state nil))
(cond
(let ((first t)
column)
(when (and (string-match "\\`[ \t\n]" text)
- (not (bolp)))
- (insert " ")
- (setq shr-state 'space))
+ (not (bolp))
+ (not (eq (char-after (1- (point))) ? )))
+ (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 (> (shr-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 " "))
+ (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)))
+ (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))
(defun shr-ensure-newline ()
(unless (zerop (current-column))
(defun shr-ensure-paragraph ()
(unless (bobp)
- (if (bolp)
+ (if (<= (current-column) shr-indentation)
(unless (save-excursion
(forward-line -1)
(looking-at " *$"))
(insert "\n\n")))))
(defun shr-indent ()
- (insert (make-string shr-indentation ? )))
+ (when (> shr-indentation 0)
+ (insert (make-string shr-indentation ? ))))
(defun shr-fontize-cont (cont &rest types)
(let (shr-start)
(defun shr-tag-p (cont)
(shr-ensure-paragraph)
+ (shr-indent)
(shr-generic cont)
(shr-ensure-paragraph))
(defun shr-tag-pre (cont)
(let ((shr-folding-mode 'none))
(shr-ensure-newline)
+ (shr-indent)
(shr-generic cont)
(shr-ensure-newline)))
(defun shr-tag-blockquote (cont)
(shr-ensure-paragraph)
+ (shr-indent)
(let ((shr-indentation (+ shr-indentation 4)))
(shr-generic cont))
(shr-ensure-paragraph))
(defun shr-tag-ul (cont)
(shr-ensure-paragraph)
(let ((shr-list-mode 'ul))
- (shr-generic cont)))
+ (shr-generic cont))
+ (shr-ensure-paragraph))
(defun shr-tag-ol (cont)
+ (shr-ensure-paragraph)
(let ((shr-list-mode 1))
- (shr-generic cont)))
+ (shr-generic cont))
+ (shr-ensure-paragraph))
(defun shr-tag-li (cont)
- (shr-ensure-newline)
+ (shr-ensure-paragraph)
+ (shr-indent)
(let* ((bullet
(if (numberp shr-list-mode)
(prog1
(defun shr-tag-br (cont)
(unless (bobp)
- (insert "\n"))
+ (insert "\n")
+ (shr-indent))
(shr-generic cont))
(defun shr-tag-h1 (cont)
(defun shr-tag-h6 (cont)
(shr-heading cont))
+(defun shr-tag-hr (cont)
+ (shr-ensure-newline)
+ (insert (make-string shr-width shr-hr-line) "\n"))
+
;;; Table rendering algorithm.
;; Table rendering is the only complicated thing here. We do this by
overlay overlay-line)
(dolist (line lines)
(setq overlay-line (pop overlay-lines))
- (when (> (length line) 0)
- (end-of-line)
- (insert line "|")
- (dolist (overlay overlay-line)
- (let ((o (make-overlay (- (point) (nth 0 overlay) 1)
- (- (point) (nth 1 overlay) 1)))
- (properties (nth 2 overlay)))
- (while properties
- (overlay-put o (pop properties) (pop properties)))))
- (forward-line 1)))
+ (end-of-line)
+ (insert line "|")
+ (dolist (overlay overlay-line)
+ (let ((o (make-overlay (- (point) (nth 0 overlay) 1)
+ (- (point) (nth 1 overlay) 1)))
+ (properties (nth 2 overlay)))
+ (while properties
+ (overlay-put o (pop properties) (pop properties)))))
+ (forward-line 1))
;; Add blank lines at padding at the bottom of the TD,
;; possibly.
(dotimes (i (- height (length lines)))
(defun shr-insert-table-ruler (widths)
(shr-indent)
- (insert "+")
+ (insert shr-table-corner)
(dotimes (i (length widths))
- (insert (make-string (aref widths i) ?-) ?+))
+ (insert (make-string (aref widths i) shr-table-line) shr-table-corner))
(insert "\n"))
(defun shr-table-widths (table suggested-widths)
(let* ((length (length suggested-widths))
- (widths (make-vector length 0)))
+ (widths (make-vector length 0))
+ (natural-widths (make-vector length 0)))
(dolist (row table)
(let ((i 0))
(dolist (column row)
(aset widths i (max (aref widths i)
(car column)))
+ (aset natural-widths i (max (aref natural-widths i)
+ (cadr column)))
(setq i (1+ i)))))
- (let ((extra (- (reduce '+ suggested-widths)
- (reduce '+ widths))))
+ (let ((extra (- (apply '+ (append suggested-widths nil))
+ (apply '+ (append widths nil))))
+ (expanded-columns 0))
(when (> extra 0)
(dotimes (i length)
- (aset widths i
- (+ (aref widths i)
- (truncate
- (* extra
- (/ (* (aref widths i) 1.0)
- (reduce '+ widths)))))))))
+ ;; If the natural width is wider than the rendered width, we
+ ;; want to allow the column to expand.
+ (when (> (aref natural-widths i) (aref widths i))
+ (setq expanded-columns (1+ expanded-columns))))
+ (dotimes (i length)
+ (when (> (aref natural-widths i) (aref widths i))
+ (aset widths i (min
+ (1+ (aref natural-widths i))
+ (+ (/ extra expanded-columns)
+ (aref widths i))))))))
widths))
(defun shr-make-table (cont widths &optional fill)
(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))
(let ((current 0)
(max 0))
(while (not (eobp))
+ (end-of-line)
(setq current (+ current (current-column)))
(unless (get-text-property (point) 'shr-break)
(setq max (max max current)
current 0))
- (forward-line 1))))
+ (forward-line 1))
+ max))
(defun shr-collect-overlays ()
(save-excursion