From adbee20c0e8e486de06ea4de9d6e69394a2fef66 Mon Sep 17 00:00:00 2001 From: Rahguzar Date: Tue, 24 Oct 2023 20:30:23 +0200 Subject: [PATCH 2/5] Allow displaying images inline * lisp/net/shr.el (shr-max-inline-image-size): New custom variable (shr-insert): Use the variable to determine whether to insert newline before an image (shr--inline-image-p): New function (shr-put-image): Use variable and function (shr-tag-img): Use variable --- lisp/net/shr.el | 65 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 13 deletions(-) diff --git a/lisp/net/shr.el b/lisp/net/shr.el index 185f2c0422d..adce66311f1 100644 --- a/lisp/net/shr.el +++ b/lisp/net/shr.el @@ -187,6 +187,24 @@ shr-image-ascent :version "30.1" :type 'integer) +(defcustom shr-max-inline-image-size nil + "If non-nil determines when the images can be displayed inline. +If nil images are never displayed inline. + +It non-nil it should be cons (WIDTH . HEIGHT). + +WIDTH can be an integer which is interpreted as number of pixels. If the width +of an image exceeds this amount, the image is displayed on a separate line. +WIDTH can also be floating point number, in which case the image is displayed +inline if it occupies less than this fraction of window width. + +HEIGHT can be also be an integer or a floating point number. If it is an +integer and the pixel height of an image exceeds it, the image image is +displyed on a separate line. If it is an floating point, the limit is +interpreted as multiples of the height of default font." + :version "30.1" + :type '(choice (const nil) (cons number number))) + (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 @@ -721,7 +739,8 @@ shr--translate-insertion-chars (replace-match " " t t))) (defun shr-insert (text) - (when (and (not (bolp)) + (when (and (not shr-max-inline-image-size) + (not (bolp)) (get-text-property (1- (point)) 'image-url)) (insert "\n")) (cond @@ -1073,6 +1092,19 @@ shr-image-from-data (declare-function image-size "image.c" (spec &optional pixels frame)) (declare-function image-animate "image" (image &optional index limit position)) +(defun shr--inline-image-p (image) + "Return non-nil if IMAGE should be displayed inline." + (when shr-max-inline-image-size + (let ((size (image-size image t)) + (max-width (car shr-max-inline-image-size)) + (max-height (cdr shr-max-inline-image-size))) + (unless (integerp max-width) + (setq max-width (* max-width (window-width nil t)))) + (unless (integerp max-height) + (setq max-width (* max-width (frame-char-height)))) + (and (< (car size) max-width) + (< (cdr size) max-width))))) + (defun shr-put-image (spec alt &optional flags) "Insert image SPEC with a string ALT. Return image. SPEC is either an image data blob, or a list where the first @@ -1103,19 +1135,25 @@ shr-put-image (plist-get flags :width) (plist-get flags :height))))))) (when image + ;; The trailing confuse can confuse shr-insert into not + ;; putting any space after inline images. + (setq alt (string-trim alt)) ;; 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")) - (let ((image-pos (point))) - (if (eq size 'original) - (insert-sliced-image image (or alt "*") nil 20 1) - (insert-image image (or alt "*"))) - (put-text-property start (point) 'image-size size) - (when (and shr-image-animate - (cdr (image-multi-frame-p image))) - (image-animate image nil 60 image-pos)))) + (let ((inline (shr--inline-image-p image))) + (when (and (> (current-column) 0) + (not inline)) + (insert "\n")) + (let ((image-pos (point))) + (if (eq size 'original) + (insert-sliced-image image (or alt "*") nil 20 1) + (insert-image image (or alt "*"))) + (put-text-property start (point) 'image-size size) + (when (and (not inline) shr-max-inline-image-size) + (insert "\n")) + (when (and shr-image-animate + (cdr (image-multi-frame-p image))) + (image-animate image nil 60 image-pos))))) image) (insert (or alt "")))) @@ -1676,7 +1714,8 @@ shr-tag-img (and dom (or (> (length (dom-attr dom 'src)) 0) (> (length (dom-attr dom 'srcset)) 0)))) - (when (> (current-column) 0) + (when (and (not shr-max-inline-image-size) + (> (current-column) 0)) (insert "\n")) (let ((alt (dom-attr dom 'alt)) (width (shr-string-number (dom-attr dom 'width))) -- 2.42.0