From 814b707198d1451bd016a51fbebcd8b32e6d44bb Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Tue, 5 Mar 2024 10:27:00 +0100 Subject: RET/click image to view full-sized version in emacs. --- lisp/mastodon-media.el | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'lisp/mastodon-media.el') diff --git a/lisp/mastodon-media.el b/lisp/mastodon-media.el index 9dd22f4..9014add 100644 --- a/lisp/mastodon-media.el +++ b/lisp/mastodon-media.el @@ -177,6 +177,40 @@ with the image." (set-marker marker nil))) (kill-buffer url-buffer)))))) +(defun mastodon-media--process-full-sized-image-response + (status-plist image-options url) + ;; FIXME: refactor this with but not into + ;; `mastodon-media--process-image-response'. + "Callback function processing the `url-retrieve' response for URL. +URL is a full-sized image URL attached to a timeline image. +STATUS-PLIST is a plist of status events as per `url-retrieve'. +IMAGE-OPTIONS are the precomputed options to apply to the image." + (let ((url-buffer (current-buffer)) + (is-error-response-p (eq :error (car status-plist)))) + (let* ((data (unless is-error-response-p + (goto-char (point-min)) + (search-forward "\n\n") + (buffer-substring (point) (point-max)))) + (image (when data + (apply #'create-image data + (if (version< emacs-version "27.1") + (when image-options 'imagemagick) + nil) ; inbuilt scaling in 27.1 + t nil)))) + (when mastodon-media--enable-image-caching + (unless (url-is-cached url) ;; cache if not already cached + (url-store-in-cache url-buffer))) + (with-current-buffer (get-buffer-create "*masto-image*") + (let ((inhibit-read-only t)) + (erase-buffer) + (insert " ") + (when image + (put-text-property (point-min) (point-max) + 'display image) + (image-mode) + (goto-char (point-min)) + (switch-to-buffer-other-window (current-buffer)))))))) + (defun mastodon-media--load-image-from-url (url media-type start region-length) "Take a URL and MEDIA-TYPE and load the image asynchronously. MEDIA-TYPE is a symbol and either `avatar' or `media-link'. -- cgit v1.2.3 From 0dc21c9c3ed39b489e9220864fa47a1f9daade6a Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Tue, 5 Mar 2024 18:45:32 +0100 Subject: optional full-size image in emacs, RET as fallback for browse-url too --- lisp/mastodon-media.el | 9 +++++++-- lisp/mastodon-tl.el | 42 ++++++++++++++++++++++++++++-------------- 2 files changed, 35 insertions(+), 16 deletions(-) (limited to 'lisp/mastodon-media.el') diff --git a/lisp/mastodon-media.el b/lisp/mastodon-media.el index 9014add..ff40633 100644 --- a/lisp/mastodon-media.el +++ b/lisp/mastodon-media.el @@ -205,8 +205,13 @@ IMAGE-OPTIONS are the precomputed options to apply to the image." (erase-buffer) (insert " ") (when image - (put-text-property (point-min) (point-max) - 'display image) + (add-text-properties (point-min) (point-max) + `( display ,image + keymap ,(if (boundp 'shr-image-map) + shr-image-map + shr-map) + image-url ,url + shr-url ,url)) (image-mode) (goto-char (point-min)) (switch-to-buffer-other-window (current-buffer)))))))) diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el index e9cb6de..83fdabc 100644 --- a/lisp/mastodon-tl.el +++ b/lisp/mastodon-tl.el @@ -36,6 +36,7 @@ (require 'cl-lib) (require 'mastodon-iso) (require 'mpv nil :no-error) +(require 'url-cache) (autoload 'mastodon-mode "mastodon") (autoload 'mastodon-notifications-get "mastodon") @@ -86,6 +87,7 @@ (autoload 'mastodon-views--insert-users-propertized-note "mastodon-views") ; for search pagination (autoload 'mastodon-http--get-response "mastodon-http") (autoload 'mastodon-search--insert-heading "mastodon-search") +(autoload 'mastodon-media--process-full-sized-image-response "mastodon-media") (defvar mastodon-toot--visibility) (defvar mastodon-toot-mode) @@ -96,6 +98,8 @@ (defvar mastodon-instance-url) (defvar mastodon-toot-timestamp-format) (defvar shr-use-fonts) ;; declare it since Emacs24 didn't have this +(defvar mastodon-media--enable-image-caching) + (defvar mastodon-mode-map) @@ -195,6 +199,14 @@ re-load mastodon.el, or restart Emacs." "A list of up to four tags for use with `mastodon-tl--followed-tags-timeline'." :type '(repeat string)) +(defcustom mastodon-tl--load-full-sized-images-in-emacs t + "Whether to load full-sized images inside Emacs. +Full-sized images are loaded when you hit return on or click on +an image in a timeline. +If nil, mastodon.el will instead call `shr-browse-image', which +respects the user's `browse-url' settings." + :type '(boolean)) + ;;; VARIABLES @@ -1111,24 +1123,26 @@ SENSITIVE is a flag from the item's JSON data." (concat help-echo "\nC-RET: play " type " with mpv")))) (defun mastodon-tl--view-full-image () - "Browse full-sized version of image at point in a separate emacs window." + "Browse full-sized version of image at point in a new window." (interactive) (if (not (eq (mastodon-tl--property 'mastodon-tab-stop) 'image)) (user-error "No image at point?") (let* ((url (mastodon-tl--property 'image-url))) - (if (and mastodon-media--enable-image-caching - (url-is-cached url)) - ;; if image url is cached, decompress and use it - (with-current-buffer (url-fetch-from-cache url) - (set-buffer-multibyte nil) - (goto-char (point-min)) - (zlib-decompress-region - (goto-char (search-forward "\n\n")) (point-max)) - (mastodon-media--process-full-sized-image-response - nil nil url)) - ;; else fetch and load: - (url-retrieve url #'mastodon-media--process-full-sized-image-response - (list nil url)))))) + (if (not mastodon-tl--load-full-sized-images-in-emacs) + (shr-browse-image) + (if (and mastodon-media--enable-image-caching + (url-is-cached url)) + ;; if image url is cached, decompress and use it + (with-current-buffer (url-fetch-from-cache url) + (set-buffer-multibyte nil) + (goto-char (point-min)) + (zlib-decompress-region + (goto-char (search-forward "\n\n")) (point-max)) + (mastodon-media--process-full-sized-image-response + nil nil url)) + ;; else fetch and load: + (url-retrieve url #'mastodon-media--process-full-sized-image-response + (list nil url))))))) ;; POLLS -- cgit v1.2.3