From 94f310c78dd6204176425f3805ec8b67d30c9c92 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Mon, 4 Mar 2024 19:31:47 +0100 Subject: add toggle-spoiler-in-thread cmd --- lisp/mastodon-tl.el | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'lisp/mastodon-tl.el') diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el index 856325e..18aaccc 100644 --- a/lisp/mastodon-tl.el +++ b/lisp/mastodon-tl.el @@ -389,12 +389,14 @@ Optionally start from POS." (funcall refresh) (error "No more items"))))) -(defun mastodon-tl--goto-next-item () +(defun mastodon-tl--goto-next-item (&optional no-refresh) "Jump to next item. -Load more items it no next item." +Load more items it no next item. +NO-REFRESH means do no not try to load more items if no next item +found." (interactive) (mastodon-tl--goto-item-pos 'next-single-property-change - 'mastodon-tl--more)) + (unless no-refresh 'mastodon-tl--more))) (defun mastodon-tl--goto-prev-item () "Jump to previous item. @@ -989,6 +991,18 @@ content should be hidden." (t (mastodon-tl--toggle-spoiler-text (car spoiler-range)))))) +(defun mastodon-tl--toggle-spoiler-in-thread () + "Toggler content warning for all posts in current thread." + (interactive) + (let ((thread-p (eq (mastodon-tl--buffer-property 'update-function) + 'mastodon-tl--thread))) + (if (not thread-p) + (user-error "Not in a thread") + (goto-char (point-min)) + (while (not (equal "No more items" ; improve this hack test! + (mastodon-tl--goto-next-item :no-refresh))) + (mastodon-tl--toggle-spoiler-text-in-toot))))) + (defun mastodon-tl--clean-tabs-and-nl (string) "Remove tabs and newlines from STRING." (replace-regexp-in-string "[\t\n ]*\\'" "" string)) -- cgit v1.2.3 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 ++++++++++++++++++++++++++++++++++ lisp/mastodon-tl.el | 31 +++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 2 deletions(-) (limited to 'lisp/mastodon-tl.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'. diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el index 18aaccc..e9cb6de 100644 --- a/lisp/mastodon-tl.el +++ b/lisp/mastodon-tl.el @@ -262,7 +262,7 @@ types of mastodon links and not just shr.el-generated ones.") (define-key map [remap shr-previous-link] #'mastodon-tl--previous-tab-item) ;; browse-url loads the preview only, we want browse-image ;; on RET to browse full sized image URL - (define-key map [remap shr-browse-url] #'shr-browse-image) + (define-key map [remap shr-browse-url] #'mastodon-tl--view-full-image-or-play-video) ;#'shr-browse-image) ;; remove shr's u binding, as it the maybe-probe-and-copy-url ;; is already bound to w also (define-key map (kbd "u") #'mastodon-tl--update) @@ -1110,6 +1110,26 @@ SENSITIVE is a flag from the item's JSON data." help-echo (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." + (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)))))) + ;; POLLS @@ -1286,12 +1306,19 @@ displayed when the duration is smaller than a minute)." (type (plist-get video :type))) (mastodon-tl--mpv-play-video-at-point url type))) +(defun mastodon-tl--view-full-image-or-play-video () + "View full sized version of image at point, or try to play video." + (interactive) + (if (mastodon-tl--media-video-p) + (mastodon-tl--mpv-play-video-at-point) + (mastodon-tl--view-full-image))) + (defun mastodon-tl--click-image-or-video (_event) "Click to play video with `mpv.el'." (interactive "e") (if (mastodon-tl--media-video-p) (mastodon-tl--mpv-play-video-at-point) - (shr-browse-image))) + (mastodon-tl--view-full-image))) (defun mastodon-tl--media-video-p (&optional type) "T if mastodon-media-type prop is \"gifv\" or \"video\". -- 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-tl.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 From f807db692bc83f4f1000dbdd43e92153a5264d30 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Tue, 5 Mar 2024 20:51:11 +0100 Subject: cw-in-thread: don't move point, only fold if cw --- lisp/mastodon-tl.el | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'lisp/mastodon-tl.el') diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el index 83fdabc..9a3c798 100644 --- a/lisp/mastodon-tl.el +++ b/lisp/mastodon-tl.el @@ -1010,10 +1010,14 @@ content should be hidden." 'mastodon-tl--thread))) (if (not thread-p) (user-error "Not in a thread") - (goto-char (point-min)) - (while (not (equal "No more items" ; improve this hack test! - (mastodon-tl--goto-next-item :no-refresh))) - (mastodon-tl--toggle-spoiler-text-in-toot))))) + (save-excursion + (goto-char (point-min)) + (while (not (equal "No more items" ; improve this hack test! + (mastodon-tl--goto-next-item :no-refresh))) + (let* ((json (mastodon-tl--property 'item-json :no-move)) + (cw (alist-get 'spoiler_text json))) + (when (not (equal "" cw)) + (mastodon-tl--toggle-spoiler-text-in-toot)))))))) (defun mastodon-tl--clean-tabs-and-nl (string) "Remove tabs and newlines from STRING." -- cgit v1.2.3 From 623e29d6d50e68cf6f8e8714cae92c4b704264a3 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Fri, 8 Mar 2024 14:11:24 +0100 Subject: customise no-fill-on-render. FIX: #532 --- lisp/mastodon-tl.el | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'lisp/mastodon-tl.el') diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el index 9a3c798..4034ebf 100644 --- a/lisp/mastodon-tl.el +++ b/lisp/mastodon-tl.el @@ -122,6 +122,11 @@ By default fixed width fonts are used." :type '(boolean :tag "Enable using proportional rather than fixed \ width fonts when rendering HTML text")) +(defcustom mastodon-tl--no-fill-on-render nil + "Non-nil to disable filling by shr.el while rendering toot body. +Use this if your setup isn't compatible with shr's window width filling." + :type '(boolean)) + (defcustom mastodon-tl--display-media-p t "A boolean value stating whether to show media in timelines." :type 'boolean) @@ -777,7 +782,9 @@ links in the text. If TOOT is nil no parsing occurs." (insert string) (let ((shr-use-fonts mastodon-tl--enable-proportional-fonts) (shr-width (when mastodon-tl--enable-proportional-fonts - (- (window-width) 3)))) + (if mastodon-tl--no-fill-on-render + 0 + (- (window-width) 3))))) (shr-render-region (point-min) (point-max))) ;; Make all links a tab stop recognized by our own logic, make things point ;; to our own logic (e.g. hashtags), and update keymaps where needed: -- cgit v1.2.3