From 2c9a1f7417e5f987a87d943f1fba941cc55ef523 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Thu, 12 Oct 2023 17:30:31 +0200 Subject: rework search into three separate views with view cycling on C-c C-c --- lisp/mastodon-search.el | 81 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 66 insertions(+), 15 deletions(-) (limited to 'lisp') diff --git a/lisp/mastodon-search.el b/lisp/mastodon-search.el index 1d78cc2..f1bc582 100644 --- a/lisp/mastodon-search.el +++ b/lisp/mastodon-search.el @@ -141,30 +141,64 @@ PRINT-FUN is the function used to print the data from the response." " " mastodon-tl--horiz-bar "\n") 'success))) -(defun mastodon-search--search-query (query) +(defun mastodon-search--search-query (query + &optional type following account-id) "Prompt for a search QUERY and return accounts, statuses, and hashtags." (interactive "sSearch mastodon for: ") (let* ((url (format "%s/api/v2/search" mastodon-instance-url)) - (buffer (format "*mastodon-search-%s*" query)) - (response (mastodon-http--get-json url `(("q" . ,query)))) - (accts (alist-get 'accounts response)) - (tags (alist-get 'hashtags response)) - (statuses (alist-get 'statuses response)) - (tags-list (mapcar #'mastodon-search--get-hashtag-info tags)) - (toots-list-json (mastodon-search--get-full-statuses-data statuses))) + (buffer (format "*mastodon-search-%s-%s*" type query)) + (params `(("q" . ,query) + ,(when type `("type" . ,type)) + ,(when following `("following" . ,following)) + ,(when account-id `("account_id" . ,account-id)))) + (response (mastodon-http--get-json url params)) + (accts (when (equal type "accounts") + (alist-get 'accounts response))) + (tags (when (equal type "hashtags") + (alist-get 'hashtags response))) + (statuses (when (equal type "statuses") + (alist-get 'statuses response))) + (tags-list (when tags + (mapcar #'mastodon-search--get-hashtag-info tags)))) + ;; (toots-list-json (when statuses ; v slow, but do we have a choice? + ;; (mastodon-search--get-full-statuses-data statuses)))) (with-mastodon-buffer buffer #'mastodon-mode nil - (mastodon-tl--set-buffer-spec buffer "api/v2/search" nil) + (mastodon-search-mode) + (mastodon-tl--set-buffer-spec buffer "api/v2/search" nil + nil params) ;; user results: - (mastodon-search--format-heading "USERS") - (mastodon-search--insert-users-propertized accts :note) + (when accts + (mastodon-search--format-heading "USERS") + (mastodon-search--insert-users-propertized accts :note)) ;; hashtag results: - (mastodon-search--format-heading "HASHTAGS") - (mastodon-search--print-tags-list tags-list) + (when tags + (mastodon-search--format-heading "HASHTAGS") + (mastodon-search--print-tags-list tags-list)) ;; status results: - (mastodon-search--format-heading "STATUSES") - (mapc #'mastodon-tl--toot toots-list-json) + (when statuses + (mastodon-search--format-heading "STATUSES") + (mapc #'mastodon-tl--toot statuses)) ;toots-list-json)) (goto-char (point-min))))) +(defun mastodon-search--buf-type () + "Return search buffer type, a member of `mastodon-search-types'." + ;; called in `mastodon-tl--get-buffer-type' + (let* ((spec (mastodon-tl--buffer-property 'update-params))) + (alist-get "type" spec nil nil #'equal))) + +(defun mastodon-search--query-cycle () + "Cycle through search types: accounts, hashtags, and statuses." + (interactive) + (let* ((spec (mastodon-tl--buffer-property 'update-params)) + (type (alist-get "type" spec nil nil #'equal)) + (query (alist-get "q" spec nil nil #'equal))) + (cond ((equal type "hashtags") + (mastodon-search--search-query query "accounts")) + ((equal type "accounts") + (mastodon-search--search-query query "statuses")) + ((equal type "statuses") + (mastodon-search--search-query query "hashtags"))))) + (defun mastodon-search--insert-users-propertized (json &optional note) "Insert users list into the buffer. JSON is the data from the server. If NOTE is non-nil, include @@ -250,5 +284,22 @@ render them properly." (json (mastodon-http--get-json url))) json)) + +(defvar mastodon-search-mode-map + (let ((map (make-sparse-keymap))) + (define-key map (kbd "C-c C-c") #'mastodon-search--query-cycle) + map) + "Keymap for `mastodon-search-mode'.") + +(define-minor-mode mastodon-search-mode + "Toggle mastodon search minor mode. +This minor mode is used for mastodon search pages to adds a keybinding." + :init-value nil + :lighter " Search" + :keymap mastodon-search-mode-map + :group 'mastodon + :global nil) + + (provide 'mastodon-search) ;;; mastodon-search.el ends here -- cgit v1.2.3