From aa6e9c55152ac1301efbdbdce9aa7fc3e8fea7c9 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Sat, 6 May 2023 16:00:00 +0200 Subject: eval-when-compile for with-mastodon-buffer, and re-indent --- lisp/mastodon-profile.el | 197 +++++++++++++++++++++++------------------------ lisp/mastodon-search.el | 81 ++++++++++--------- lisp/mastodon-tl.el | 90 +++++++++++----------- lisp/mastodon-toot.el | 45 +++++------ lisp/mastodon-views.el | 43 ++++++----- 5 files changed, 226 insertions(+), 230 deletions(-) diff --git a/lisp/mastodon-profile.el b/lisp/mastodon-profile.el index 5bee7e9..b4812d7 100644 --- a/lisp/mastodon-profile.el +++ b/lisp/mastodon-profile.el @@ -38,8 +38,9 @@ (require 'cl-lib) (require 'persist) (require 'parse-time) +(eval-when-compile + (require 'mastodon-tl)) -(autoload 'with-mastodon-buffer "mastodon") (autoload 'mastodon-auth--get-account-id "mastodon-auth") (autoload 'mastodon-auth--get-account-name "mastodon-auth.el") (autoload 'mastodon-http--api "mastodon-http.el") @@ -494,19 +495,18 @@ This endpoint only holds a few preferences. For others, see (let* ((url (mastodon-http--api "preferences")) (response (mastodon-http--get-json url)) (buf (get-buffer-create "*mastodon-preferences*"))) - (with-mastodon-buffer - buf #'special-mode :other-window - (mastodon-tl--set-buffer-spec (buffer-name buf) - "preferences" - nil) - (while response - (let ((el (pop response))) - (insert - (format "%-30s %s" - (prin1-to-string (car el)) - (prin1-to-string (cdr el))) - "\n\n"))) - (goto-char (point-min))))) + (with-mastodon-buffer buf #'special-mode :other-window + (mastodon-tl--set-buffer-spec (buffer-name buf) + "preferences" + nil) + (while response + (let ((el (pop response))) + (insert + (format "%-30s %s" + (prin1-to-string (car el)) + (prin1-to-string (cdr el))) + "\n\n"))) + (goto-char (point-min))))) ;; PROFILE VIEW DETAILS @@ -603,91 +603,90 @@ HEADERS means also fetch link headers for pagination." (fields (mastodon-profile--fields-get account)) (pinned (mastodon-profile--get-statuses-pinned account)) (joined (mastodon-profile--account-field account 'created_at))) - (with-mastodon-buffer - buffer #'mastodon-mode nil - (mastodon-profile-mode) - (setq mastodon-profile--account account) - (mastodon-tl--set-buffer-spec buffer - endpoint - update-function - link-header) - (let* ((inhibit-read-only t) - (is-statuses (string= endpoint-type "statuses")) - (is-followers (string= endpoint-type "followers")) - (is-following (string= endpoint-type "following")) - (endpoint-name (cond - (is-statuses (if no-reblogs - " TOOTS (no boosts)" - " TOOTS ")) - (is-followers " FOLLOWERS ") - (is-following " FOLLOWING ")))) - (insert - (propertize - (concat - "\n" - (mastodon-profile--image-from-account account 'avatar_static) - (mastodon-profile--image-from-account account 'header_static) - "\n" - (propertize (mastodon-profile--account-field - account 'display_name) - 'face 'mastodon-display-name-face) - "\n" - (propertize (concat "@" acct) - 'face 'default) - (if (equal locked t) - (concat " " (mastodon-tl--symbol 'locked)) - "") - "\n " mastodon-tl--horiz-bar "\n" - ;; profile note: - ;; account here to enable tab-stops in profile note - (mastodon-tl--render-text note account) - ;; meta fields: - (if fields - (concat "\n" - (mastodon-tl--set-face - (mastodon-profile--fields-insert fields) - 'success)) - "") - "\n" - ;; Joined date: - (propertize - (mastodon-profile--format-joined-date-string joined) - 'face 'success) - "\n\n") - 'profile-json account) - ;; insert counts - (mastodon-tl--set-face - (concat " " mastodon-tl--horiz-bar "\n" - " TOOTS: " toots-count " | " - "FOLLOWERS: " followers-count " | " - "FOLLOWING: " following-count "\n" - " " mastodon-tl--horiz-bar "\n\n") - 'success) - ;; insert relationship (follows) - (if followsp - (mastodon-tl--set-face - (concat (when (equal follows-you 't) - " | FOLLOWS YOU") - (when (equal followed-by-you 't) - " | FOLLOWED BY YOU") - (when (equal requested-you 't) - " | REQUESTED TO FOLLOW YOU") - "\n\n") - 'success) - "") ; if no followsp we still need str-or-char-p for insert - ;; insert endpoint - (mastodon-tl--set-face - (concat " " mastodon-tl--horiz-bar "\n" - endpoint-name "\n" - " " mastodon-tl--horiz-bar "\n") - 'success)) - (setq mastodon-tl--update-point (point)) - (mastodon-media--inline-images (point-min) (point)) - ;; insert pinned toots first - (when (and pinned (equal endpoint-type "statuses")) - (mastodon-profile--insert-statuses-pinned pinned) - (setq mastodon-tl--update-point (point))) ;updates to follow pinned toots - (funcall update-function json))) + (with-mastodon-buffer buffer #'mastodon-mode nil + (mastodon-profile-mode) + (setq mastodon-profile--account account) + (mastodon-tl--set-buffer-spec buffer + endpoint + update-function + link-header) + (let* ((inhibit-read-only t) + (is-statuses (string= endpoint-type "statuses")) + (is-followers (string= endpoint-type "followers")) + (is-following (string= endpoint-type "following")) + (endpoint-name (cond + (is-statuses (if no-reblogs + " TOOTS (no boosts)" + " TOOTS ")) + (is-followers " FOLLOWERS ") + (is-following " FOLLOWING ")))) + (insert + (propertize + (concat + "\n" + (mastodon-profile--image-from-account account 'avatar_static) + (mastodon-profile--image-from-account account 'header_static) + "\n" + (propertize (mastodon-profile--account-field + account 'display_name) + 'face 'mastodon-display-name-face) + "\n" + (propertize (concat "@" acct) + 'face 'default) + (if (equal locked t) + (concat " " (mastodon-tl--symbol 'locked)) + "") + "\n " mastodon-tl--horiz-bar "\n" + ;; profile note: + ;; account here to enable tab-stops in profile note + (mastodon-tl--render-text note account) + ;; meta fields: + (if fields + (concat "\n" + (mastodon-tl--set-face + (mastodon-profile--fields-insert fields) + 'success)) + "") + "\n" + ;; Joined date: + (propertize + (mastodon-profile--format-joined-date-string joined) + 'face 'success) + "\n\n") + 'profile-json account) + ;; insert counts + (mastodon-tl--set-face + (concat " " mastodon-tl--horiz-bar "\n" + " TOOTS: " toots-count " | " + "FOLLOWERS: " followers-count " | " + "FOLLOWING: " following-count "\n" + " " mastodon-tl--horiz-bar "\n\n") + 'success) + ;; insert relationship (follows) + (if followsp + (mastodon-tl--set-face + (concat (when (equal follows-you 't) + " | FOLLOWS YOU") + (when (equal followed-by-you 't) + " | FOLLOWED BY YOU") + (when (equal requested-you 't) + " | REQUESTED TO FOLLOW YOU") + "\n\n") + 'success) + "") ; if no followsp we still need str-or-char-p for insert + ;; insert endpoint + (mastodon-tl--set-face + (concat " " mastodon-tl--horiz-bar "\n" + endpoint-name "\n" + " " mastodon-tl--horiz-bar "\n") + 'success)) + (setq mastodon-tl--update-point (point)) + (mastodon-media--inline-images (point-min) (point)) + ;; insert pinned toots first + (when (and pinned (equal endpoint-type "statuses")) + (mastodon-profile--insert-statuses-pinned pinned) + (setq mastodon-tl--update-point (point))) ;updates to follow pinned toots + (funcall update-function json))) (goto-char (point-min)))) (defun mastodon-profile--format-joined-date-string (joined) diff --git a/lisp/mastodon-search.el b/lisp/mastodon-search.el index 26790ea..a6b4492 100644 --- a/lisp/mastodon-search.el +++ b/lisp/mastodon-search.el @@ -29,13 +29,14 @@ ;;; Code: (require 'json) +(eval-when-compile + (require 'mastodon-tl)) (autoload 'mastodon-auth--access-token "mastodon-auth") (autoload 'mastodon-http--api "mastodon-http") (autoload 'mastodon-http--get-json "mastodon-http") (autoload 'mastodon-http--get-search-json "mastodon-http") (autoload 'mastodon-mode "mastodon") -(autoload 'with-mastodon-buffer "mastodon") (autoload 'mastodon-tl--as-string "mastodon-tl") (autoload 'mastodon-tl--as-string "mastodon-tl") (autoload 'mastodon-tl--render-text "mastodon-tl") @@ -119,19 +120,18 @@ PRINT-FUN is the function used to print the data from the response." (message "todo")))) (buffer (get-buffer-create (format "*mastodon-trending-%s*" type)))) - (with-mastodon-buffer - buffer #'mastodon-mode nil - (mastodon-tl--set-buffer-spec (buffer-name buffer) - (format "api/v1/trends/%s" type) - nil) - (insert (mastodon-tl--set-face - (concat "\n " mastodon-tl--horiz-bar "\n" - (upcase (format " TRENDING %s\n" type)) - " " mastodon-tl--horiz-bar "\n\n") - 'success)) - (funcall print-fun data) - (unless (equal type "statuses") - (goto-char (point-min)))))) + (with-mastodon-buffer buffer #'mastodon-mode nil + (mastodon-tl--set-buffer-spec (buffer-name buffer) + (format "api/v1/trends/%s" type) + nil) + (insert (mastodon-tl--set-face + (concat "\n " mastodon-tl--horiz-bar "\n" + (upcase (format " TRENDING %s\n" type)) + " " mastodon-tl--horiz-bar "\n\n") + 'success)) + (funcall print-fun data) + (unless (equal type "statuses") + (goto-char (point-min)))))) ;; functions for mastodon search @@ -151,33 +151,32 @@ PRINT-FUN is the function used to print the data from the response." tags)) (toots-list-json (mastodon-search--get-full-statuses-data statuses))) - (with-mastodon-buffer - buffer #'mastodon-mode nil - (mastodon-tl--set-buffer-spec buffer - "api/v2/search" - nil) - ;; user results: - (insert (mastodon-tl--set-face - (concat "\n " mastodon-tl--horiz-bar "\n" - " USERS\n" - " " mastodon-tl--horiz-bar "\n\n") - 'success)) - (mastodon-search--insert-users-propertized accts :note) - ;; hashtag results: - (insert (mastodon-tl--set-face - (concat "\n " mastodon-tl--horiz-bar "\n" - " HASHTAGS\n" - " " mastodon-tl--horiz-bar "\n\n") - 'success)) - (mastodon-search--print-tags-list tags-list) - ;; status results: - (insert (mastodon-tl--set-face - (concat "\n " mastodon-tl--horiz-bar "\n" - " STATUSES\n" - " " mastodon-tl--horiz-bar "\n") - 'success)) - (mapc #'mastodon-tl--toot toots-list-json) - (goto-char (point-min))))) + (with-mastodon-buffer buffer #'mastodon-mode nil + (mastodon-tl--set-buffer-spec buffer + "api/v2/search" + nil) + ;; user results: + (insert (mastodon-tl--set-face + (concat "\n " mastodon-tl--horiz-bar "\n" + " USERS\n" + " " mastodon-tl--horiz-bar "\n\n") + 'success)) + (mastodon-search--insert-users-propertized accts :note) + ;; hashtag results: + (insert (mastodon-tl--set-face + (concat "\n " mastodon-tl--horiz-bar "\n" + " HASHTAGS\n" + " " mastodon-tl--horiz-bar "\n\n") + 'success)) + (mastodon-search--print-tags-list tags-list) + ;; status results: + (insert (mastodon-tl--set-face + (concat "\n " mastodon-tl--horiz-bar "\n" + " STATUSES\n" + " " mastodon-tl--horiz-bar "\n") + 'success)) + (mapc #'mastodon-tl--toot toots-list-json) + (goto-char (point-min))))) (defun mastodon-search--insert-users-propertized (json &optional note) "Insert users list into the buffer. diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el index f62413f..3352b13 100644 --- a/lisp/mastodon-tl.el +++ b/lisp/mastodon-tl.el @@ -1738,12 +1738,11 @@ ID is that of the toot to view." (mastodon-http--api (concat "statuses/" id))))) (if (equal (caar toot) 'error) (message "Error: %s" (cdar toot)) - (with-mastodon-buffer - buffer #'mastodon-mode nil - (mastodon-tl--set-buffer-spec buffer - (format "statuses/%s" id) - nil) - (mastodon-tl--toot toot :detailed-p))))) + (with-mastodon-buffer buffer #'mastodon-mode nil + (mastodon-tl--set-buffer-spec buffer + (format "statuses/%s" id) + nil) + (mastodon-tl--toot toot :detailed-p))))) (defun mastodon-tl--view-whole-thread () "From a thread view, view entire thread. @@ -1783,23 +1782,22 @@ view all branches of a thread." (length (alist-get 'descendants context))) 0) ;; if we have a thread: - (with-mastodon-buffer - buffer #'mastodon-mode nil - (let ((marker (make-marker))) - (mastodon-tl--set-buffer-spec buffer - endpoint - #'mastodon-tl--thread) - (mastodon-tl--timeline (alist-get 'ancestors context) - :thread) - (goto-char (point-max)) - (move-marker marker (point)) - ;; print re-fetched toot: - (mastodon-tl--toot toot :detailed-p :thread) - (mastodon-tl--timeline (alist-get 'descendants context) - :thread) - ;; put point at the toot: - (goto-char (marker-position marker)) - (mastodon-tl--goto-next-toot))) + (with-mastodon-buffer buffer #'mastodon-mode nil + (let ((marker (make-marker))) + (mastodon-tl--set-buffer-spec buffer + endpoint + #'mastodon-tl--thread) + (mastodon-tl--timeline (alist-get 'ancestors context) + :thread) + (goto-char (point-max)) + (move-marker marker (point)) + ;; print re-fetched toot: + (mastodon-tl--toot toot :detailed-p :thread) + (mastodon-tl--timeline (alist-get 'descendants context) + :thread) + ;; put point at the toot: + (goto-char (marker-position marker)) + (mastodon-tl--goto-next-toot))) ;; else just print the lone toot: (mastodon-tl--single-toot id))))))) @@ -2596,11 +2594,10 @@ RESPONSE is the data returned from the server by JSON and http headers, without it just the JSON." (let ((json (if headers (car response) response))) (if (not json) ; praying this is right here, else try "\n[]" - (message "Looks like nothing returned from endpoint: %s" endpoint) + (message "Looks like nothing returned from endpoint: %s" endpoint) (let* ((headers (if headers (cdr response) nil)) (link-header (mastodon-tl--get-link-header-from-response headers))) - (with-mastodon-buffer buffer - #'mastodon-mode nil + (with-mastodon-buffer buffer #'mastodon-mode nil (mastodon-tl--set-buffer-spec buffer endpoint update-function @@ -2639,27 +2636,26 @@ Optional arg NOTE-TYPE means only get that type of note." (url (mastodon-http--api endpoint)) (buffer (concat "*mastodon-" buffer-name "*")) (json (mastodon-http--get-json url args))) - (with-mastodon-buffer - buffer #'mastodon-mode nil - (setq - ;; Initialize with a minimal interval; we re-scan at least once - ;; every 5 minutes to catch any timestamps we may have missed - mastodon-tl--timestamp-next-update (time-add (current-time) - (seconds-to-time 300))) - (funcall update-function json) - (mastodon-tl--set-buffer-spec buffer endpoint update-function nil args) - (setq mastodon-tl--timestamp-update-timer - (when mastodon-tl--enable-relative-timestamps - (run-at-time (time-to-seconds - (time-subtract mastodon-tl--timestamp-next-update - (current-time))) - nil ;; don't repeat - #'mastodon-tl--update-timestamps-callback - (current-buffer) - nil))) - (unless (mastodon-tl--profile-buffer-p) - ;; FIXME: this breaks test (because test has empty buffer) - (mastodon-tl--goto-first-item))) + (with-mastodon-buffer buffer #'mastodon-mode nil + (setq + ;; Initialize with a minimal interval; we re-scan at least once + ;; every 5 minutes to catch any timestamps we may have missed + mastodon-tl--timestamp-next-update (time-add (current-time) + (seconds-to-time 300))) + (funcall update-function json) + (mastodon-tl--set-buffer-spec buffer endpoint update-function nil args) + (setq mastodon-tl--timestamp-update-timer + (when mastodon-tl--enable-relative-timestamps + (run-at-time (time-to-seconds + (time-subtract mastodon-tl--timestamp-next-update + (current-time))) + nil ;; don't repeat + #'mastodon-tl--update-timestamps-callback + (current-buffer) + nil))) + (unless (mastodon-tl--profile-buffer-p) + ;; FIXME: this breaks test (because test has empty buffer) + (mastodon-tl--goto-first-item))) buffer)) (provide 'mastodon-tl) diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el index 1265132..a2fa35f 100644 --- a/lisp/mastodon-toot.el +++ b/lisp/mastodon-toot.el @@ -44,13 +44,15 @@ (require 'facemenu) (require 'text-property-search) +(eval-when-compile + (require 'mastodon-tl)) + (defvar mastodon-instance-url) (defvar mastodon-tl--buffer-spec) (defvar mastodon-tl--enable-proportional-fonts) (defvar mastodon-profile-account-settings) (autoload 'iso8601-parse "iso8601") -(autoload 'with-mastodon-buffer "mastodon") (autoload 'mastodon-auth--user-acct "mastodon-auth") (autoload 'mastodon-http--api "mastodon-http") (autoload 'mastodon-http--build-array-params-alist "mastodon-http") @@ -902,27 +904,26 @@ instance to edit a toot." (interactive) (let ((id (mastodon-tl--property 'base-toot-id)) (history (mastodon-tl--property 'edit-history))) - (with-mastodon-buffer - "*mastodon-toot-edits*" #'special-mode :other-window - (let ((count 1)) - (mapc (lambda (x) - (insert (propertize (if (= count 1) - (format "%s [original]:\n" count) - (format "%s:\n" count)) - 'face font-lock-comment-face) - (mastodon-toot--insert-toot-iter x) - "\n") - (cl-incf count)) - history)) - (setq-local header-line-format - (propertize - (format "Edits to toot by %s:" - (alist-get 'username - (alist-get 'account (car history)))) - 'face font-lock-comment-face)) - (mastodon-tl--set-buffer-spec (buffer-name (current-buffer)) - (format "statuses/%s/history" id) - nil)))) + (with-mastodon-buffer "*mastodon-toot-edits*" #'special-mode :other-window + (let ((count 1)) + (mapc (lambda (x) + (insert (propertize (if (= count 1) + (format "%s [original]:\n" count) + (format "%s:\n" count)) + 'face font-lock-comment-face) + (mastodon-toot--insert-toot-iter x) + "\n") + (cl-incf count)) + history)) + (setq-local header-line-format + (propertize + (format "Edits to toot by %s:" + (alist-get 'username + (alist-get 'account (car history)))) + 'face font-lock-comment-face)) + (mastodon-tl--set-buffer-spec (buffer-name (current-buffer)) + (format "statuses/%s/history" id) + nil)))) (defun mastodon-toot--insert-toot-iter (it) "Insert iteration IT of toot." diff --git a/lisp/mastodon-views.el b/lisp/mastodon-views.el index 8585bb0..51e4bd6 100644 --- a/lisp/mastodon-views.el +++ b/lisp/mastodon-views.el @@ -36,6 +36,8 @@ (require 'cl-lib) (require 'mastodon-http) +(eval-when-compile + (require 'mastodon-tl)) (defvar mastodon-mode-map) (defvar mastodon-tl--horiz-bar) @@ -799,27 +801,26 @@ INSTANCE is the instance were are working with." (let* ((domain (url-file-nondirectory instance)) (buf (get-buffer-create (format "*mastodon-instance-%s*" domain)))) - (with-mastodon-buffer - buf #'special-mode :other-window - (when brief - (setq response - (list (assoc 'uri response) - (assoc 'title response) - (assoc 'short_description response) - (assoc 'email response) - (cons 'contact_account - (list - (assoc 'username - (assoc 'contact_account response)))) - (assoc 'rules response) - (assoc 'stats response)))) - (mastodon-views--print-json-keys response) - ;; (mastodon-mode) ; breaks our 'q' binding that avoids leaving - ;; split window - (mastodon-tl--set-buffer-spec (buffer-name buf) - "instance" - nil) - (goto-char (point-min)))))) + (with-mastodon-buffer buf #'special-mode :other-window + (when brief + (setq response + (list (assoc 'uri response) + (assoc 'title response) + (assoc 'short_description response) + (assoc 'email response) + (cons 'contact_account + (list + (assoc 'username + (assoc 'contact_account response)))) + (assoc 'rules response) + (assoc 'stats response)))) + (mastodon-views--print-json-keys response) + ;; (mastodon-mode) ; breaks our 'q' binding that avoids leaving + ;; split window + (mastodon-tl--set-buffer-spec (buffer-name buf) + "instance" + nil) + (goto-char (point-min)))))) (defun mastodon-views--format-key (el pad) "Format a key of element EL, a cons, with PAD padding." -- cgit v1.2.3