From 098bc1463eeac231e04dcf395626ae24e0a7ad7b Mon Sep 17 00:00:00 2001 From: Johnson Denen Date: Sun, 23 Apr 2017 13:55:31 -0400 Subject: Close #52 with mastodon-tl--more function You can request more toots by hitting 'j' at the end of a timeline buffer --- lisp/mastodon-tl.el | 53 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 9 deletions(-) (limited to 'lisp/mastodon-tl.el') diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el index a7ab9da..46b0769 100644 --- a/lisp/mastodon-tl.el +++ b/lisp/mastodon-tl.el @@ -56,25 +56,32 @@ (let ((tag (read-string "Tag: "))) (mastodon-tl--get (concat "tag/" tag)))) -(defun mastodon-tl--goto-toot-pos (find-pos &optional pos) - "Search for toot with FIND-POS. Optionally stat from POS." +(defun mastodon-tl--goto-toot-pos (find-pos refresh &optional pos) + "Search for toot with FIND-POS. +If search returns nil, execute REFRESH function. + +Optionally start from POS." (let* ((npos (funcall find-pos (or pos (point)) 'toot-id (current-buffer)))) - (when npos (if (not (get-text-property npos 'toot-id)) - (mastodon-tl--goto-toot-pos find-pos npos) - (when npos (goto-char npos)))))) + (if npos + (if (not (get-text-property npos 'toot-id)) + (mastodon-tl--goto-toot-pos find-pos refresh npos) + (goto-char npos)) + (funcall refresh)))) (defun mastodon-tl--goto-next-toot () "Jump to next toot header." (interactive) - (mastodon-tl--goto-toot-pos 'next-single-property-change)) + (mastodon-tl--goto-toot-pos 'next-single-property-change + 'mastodon-tl--more)) (defun mastodon-tl--goto-prev-toot () "Jump to last toot header." (interactive) - (mastodon-tl--goto-toot-pos 'previous-single-property-change)) + (mastodon-tl--goto-toot-pos 'previous-single-property-change + 'mastodon-tl--update)) (defun mastodon-tl--timeline-name () "Determine timeline from `buffer-name'." @@ -153,6 +160,14 @@ Return value from boosted content if available." (mapcar 'mastodon-tl--toot toots) (replace-regexp "\n\n\n | " "\n | " nil (point-min) (point-max))) +(defun mastodon-tl--more-json (timeline id) + "Return JSON for TIMELINE before ID." + (let ((url (mastodon-http--api (concat "timelines/" + timeline + "?max_id=" + (number-to-string id))))) + (mastodon-http--get-json url))) + ;; TODO ;; Look into the JSON returned here by Local (defun mastodon-tl--updated-json (timeline id) @@ -163,11 +178,13 @@ Return value from boosted content if available." (number-to-string id))))) (mastodon-http--get-json url))) -(defun mastodon-tl--property (prop) +(defun mastodon-tl--property (prop &optional backward) "Get property PROP for toot at point." (or (get-text-property (point) prop) (progn - (mastodon-tl--goto-next-toot) + (if backward + (mastodon-tl--goto-prev-toot) + (mastodon-tl--goto-next-toot)) (get-text-property (point) prop)))) (defun mastodon-tl--newest-id () @@ -175,6 +192,12 @@ Return value from boosted content if available." (goto-char (point-min)) (mastodon-tl--property 'toot-id)) +(defun mastodon-tl--oldest-id () + "Return toot-id from the bottom of the buffer." + (progn + (goto-char (point-max)) + (mastodon-tl--property 'toot-id t))) + (defun mastodon-tl--thread () "Open thread buffer for toot under `point'." (interactive) @@ -191,6 +214,18 @@ Return value from boosted content if available." (cdr (assoc 'descendants context))))) (mastodon-mode))) +(defun mastodon-tl--more () + "Append older toots to timeline." + (interactive) + (let* ((tl (mastodon-tl--timeline-name)) + (id (mastodon-tl--oldest-id)) + (json (mastodon-tl--more-json tl id))) + (when json + (with-current-buffer (current-buffer) + (let ((inhibit-read-only t)) + (goto-char (point-max)) + (mastodon-tl--timeline json)))))) + (defun mastodon-tl--update () "Update timeline with new toots." (interactive) -- cgit v1.2.3