From 9b9431b130c1d8d1a03e445ae1f7803d2a511d70 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Mon, 21 Nov 2022 22:01:07 +0100 Subject: params always in http.el, only ever send alists from elsewhere. probably incomplete but mostly done. --- lisp/mastodon-search.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lisp/mastodon-search.el') diff --git a/lisp/mastodon-search.el b/lisp/mastodon-search.el index 31fcae3..fee79c4 100644 --- a/lisp/mastodon-search.el +++ b/lisp/mastodon-search.el @@ -61,7 +61,7 @@ Returns a nested list containing user handle, display name, and URL." (interactive "sSearch mastodon for: ") (let* ((url (mastodon-http--api "accounts/search")) (response (if (equal mastodon-toot--completion-style-for-mentions "following") - (mastodon-http--get-search-json url query "following=true") + (mastodon-http--get-search-json url query '(("following" . "true"))) (mastodon-http--get-search-json url query)))) (mapcar #'mastodon-search--get-user-info-@ response))) @@ -72,7 +72,7 @@ Returns a nested list containing user handle, display name, and URL." QUERY is the string to search." (interactive "sSearch for hashtag: ") (let* ((url (format "%s/api/v2/search" mastodon-instance-url)) - (type-param (concat "type=hashtags")) + (type-param '(("type" . "hashtags"))) (response (mastodon-http--get-search-json url query type-param)) (tags (alist-get 'hashtags response))) (mapcar #'mastodon-search--get-hashtag-info tags))) -- cgit v1.2.3 From 19051d7ada81e5abc56b42de838ab7b26c31bd9b Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Tue, 22 Nov 2022 15:34:59 +0100 Subject: remove redundant --get-search(-json) funs, use new params --get-json --- lisp/mastodon-http.el | 34 ---------------------------------- lisp/mastodon-search.el | 12 +++++++----- lisp/mastodon.el | 5 +++-- 3 files changed, 10 insertions(+), 41 deletions(-) (limited to 'lisp/mastodon-search.el') diff --git a/lisp/mastodon-http.el b/lisp/mastodon-http.el index c94ea7a..c1ab3fb 100644 --- a/lisp/mastodon-http.el +++ b/lisp/mastodon-http.el @@ -256,40 +256,6 @@ PARAMS should be an alist as required by `url-build-query-string'." (let ((query-string (url-build-query-string params))) (concat url "?" query-string))) -;; search functions: -(defun mastodon-http--process-json-search () - "Process JSON returned by a search query to the server." - (goto-char (point-min)) - (re-search-forward "^$" nil 'move) - (let ((json-string - (decode-coding-string - (buffer-substring-no-properties (point) (point-max)) - 'utf-8))) - (kill-buffer) - (json-read-from-string json-string))) - -(defun mastodon-http--get-search-json (url query &optional params silent) - "Make GET request to URL, searching for QUERY and return JSON response. -PARAMS is an alist of any extra parameters to send with the request. -SILENT means don't message." - (let ((buffer (mastodon-http--get-search url query params silent))) - (with-current-buffer buffer - (mastodon-http--process-json-search)))) - -(defun mastodon-http--get-search (base-url query &optional params silent) - "Make GET request to BASE-URL, searching for QUERY. -Pass response buffer to CALLBACK function. -PARAMS is an alist of any extra parameters to send with the request. -SILENT means don't message." - (mastodon-http--authorized-request - "GET" - (let* ((query-str (mastodon-http--build-query-string - `(("q" . ,(url-hexify-string query))))) - (params-str (mastodon-http--build-query-string params)) - (url (concat base-url "?" query-str (when params-str - (concat "&" params-str))))) - (mastodon-http--url-retrieve-synchronously url silent)))) - ;; profile update functions (defun mastodon-http--patch-json (url &optional params) diff --git a/lisp/mastodon-search.el b/lisp/mastodon-search.el index fee79c4..f83cccb 100644 --- a/lisp/mastodon-search.el +++ b/lisp/mastodon-search.el @@ -61,8 +61,8 @@ Returns a nested list containing user handle, display name, and URL." (interactive "sSearch mastodon for: ") (let* ((url (mastodon-http--api "accounts/search")) (response (if (equal mastodon-toot--completion-style-for-mentions "following") - (mastodon-http--get-search-json url query '(("following" . "true"))) - (mastodon-http--get-search-json url query)))) + (mastodon-http--get-json url `(("q" . ,query) ("following" . "true"))) + (mastodon-http--get-json url `(("q" . ,query)))))) (mapcar #'mastodon-search--get-user-info-@ response))) ;; functions for tags completion: @@ -72,8 +72,10 @@ Returns a nested list containing user handle, display name, and URL." QUERY is the string to search." (interactive "sSearch for hashtag: ") (let* ((url (format "%s/api/v2/search" mastodon-instance-url)) - (type-param '(("type" . "hashtags"))) - (response (mastodon-http--get-search-json url query type-param)) + ;; (type-param '(("type" . "hashtags"))) + (params `(("q" . ,query) + ("type" . "hashtags"))) + (response (mastodon-http--get-json url params)) (tags (alist-get 'hashtags response))) (mapcar #'mastodon-search--get-hashtag-info tags))) @@ -112,7 +114,7 @@ QUERY is the string to search." (interactive "sSearch mastodon for: ") (let* ((url (format "%s/api/v2/search" mastodon-instance-url)) (buffer (format "*mastodon-search-%s*" query)) - (response (mastodon-http--get-search-json url query)) + (response (mastodon-http--get-json url `(("q" . ,query)))) (accts (alist-get 'accounts response)) (tags (alist-get 'hashtags response)) (statuses (alist-get 'statuses response)) diff --git a/lisp/mastodon.el b/lisp/mastodon.el index cfe6681..4097b27 100644 --- a/lisp/mastodon.el +++ b/lisp/mastodon.el @@ -291,8 +291,9 @@ not, just browse the URL in the normal fashion." (browse-url query) (message "Performing lookup...") (let* ((url (format "%s/api/v2/search" mastodon-instance-url)) - (param '(("resolve" . "t"))) ; webfinger - (response (mastodon-http--get-search-json url query param :silent))) + (params `(("q" . ,query) + ("resolve" . "t"))) ; webfinger + (response (mastodon-http--get-json url params :silent))) (cond ((not (seq-empty-p (alist-get 'statuses response))) (let* ((statuses (assoc 'statuses response)) -- cgit v1.2.3 From 04221419595887ad2ac84e4531310235986075e3 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Tue, 22 Nov 2022 20:48:21 +0100 Subject: a first go at capf completion backends for mentions and tags the regex searches still fail sometimes, and completions don't show urls or usernames like the old company backends. --- lisp/mastodon-search.el | 12 +++++-- lisp/mastodon-toot.el | 87 +++++++++++++++++++++++++++++++++++++------------ 2 files changed, 77 insertions(+), 22 deletions(-) (limited to 'lisp/mastodon-search.el') diff --git a/lisp/mastodon-search.el b/lisp/mastodon-search.el index 31fcae3..fc7bd8e 100644 --- a/lisp/mastodon-search.el +++ b/lisp/mastodon-search.el @@ -49,13 +49,19 @@ ;; functions for company completion of mentions in mastodon-toot +(defun mastodon-search--get-user-info-@-capf (account) + "Get user handle, display name and account URL from ACCOUNT." + (list (concat "@" (cdr (assoc 'acct account))) + (cdr (assoc 'url account)) + (cdr (assoc 'display_name account)))) + (defun mastodon-search--get-user-info-@ (account) "Get user handle, display name and account URL from ACCOUNT." (list (cdr (assoc 'display_name account)) (concat "@" (cdr (assoc 'acct account))) (cdr (assoc 'url account)))) -(defun mastodon-search--search-accounts-query (query) +(defun mastodon-search--search-accounts-query (query &optional capf) "Prompt for a search QUERY and return accounts synchronously. Returns a nested list containing user handle, display name, and URL." (interactive "sSearch mastodon for: ") @@ -63,7 +69,9 @@ Returns a nested list containing user handle, display name, and URL." (response (if (equal mastodon-toot--completion-style-for-mentions "following") (mastodon-http--get-search-json url query "following=true") (mastodon-http--get-search-json url query)))) - (mapcar #'mastodon-search--get-user-info-@ response))) + (if capf + (mapcar #'mastodon-search--get-user-info-@-capf response) + (mapcar #'mastodon-search--get-user-info-@ response)))) ;; functions for tags completion: diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el index f195a87..6ba3a75 100644 --- a/lisp/mastodon-toot.el +++ b/lisp/mastodon-toot.el @@ -108,13 +108,14 @@ (defcustom mastodon-toot--enable-completion (if (require 'company nil :noerror) t nil) "Whether to enable completion of mentions and hashtags. - Used for completion in toot compose buffer. - This is only used if company mode is installed." :group 'mastodon-toot :type 'boolean) +(defcustom mastodon-toot--use-company-for-completion t + "Whether to use company for completion.") + (defcustom mastodon-toot--completion-style-for-mentions (if (require 'company nil :noerror) "following" "off") "The company completion style to use for mentions." @@ -913,6 +914,56 @@ meta fields respectively." (annotation (funcall annot-fun arg)) (meta (funcall meta-fun arg))))) +(defun mastodon-toot-mentions-capf () + "Build a mentions completion backend for `completion-at-point-functions'." + (let* ((handle-bounds + ;; hack for @handles@with.domains, as "@" is not inc in any thing at pt! + (save-match-data + (save-excursion + ;; match full handle inc. domain (see the regex for subexp 2) + (when (re-search-backward mastodon-toot-handle-regex nil :no-error) + ;; (when (match-string-no-properties 2) + (cons (match-beginning 2) + (match-end 2)))))) + (start (car handle-bounds)) + (end (cdr handle-bounds))) + (when handle-bounds + (list start + end + ;; only search when necessary: + (completion-table-dynamic + (lambda (_) + (mastodon-search--search-accounts-query + (buffer-substring-no-properties start end) + :capf))) + :exclusive 'no)))) + +(defun mastodon-toot-tags-capf () + "Build a tags completion backend for `completion-at-point-functions'." + (let* ((tag-bounds + (save-match-data + (save-excursion + ;; match full tag with # (see regex for subexp) + (re-search-backward mastodon-toot-tag-regex nil :no-error) + (when (match-string-no-properties 2) + (cons (match-beginning 2) + (match-end 2)))))) + (start (car tag-bounds)) + (end (cdr tag-bounds))) + (when tag-bounds + (list start + end + ;; only search when necessary: + (completion-table-dynamic + (lambda (_) + (let ((tags (mastodon-search--search-tags-query + (buffer-substring-no-properties start end)))) + (mapcar (lambda (x) + (list (concat "#" (car x)) + (cdr x))) + tags)))) + :exclusive 'no)))) + (defun mastodon-toot-mentions (command &optional arg &rest ignored) "A company completion backend for toot mentions. COMMAND is either prefix, to fetch a prefix query, candidates, to @@ -1475,24 +1526,20 @@ a draft into the buffer." ;; no need to fetch from `mastodon-profile-account-settings' as ;; `mastodon-toot--max-toot-chars' is set when we set it (mastodon-toot--get-max-toot-chars)) - ;; set up completion backends: - (when (require 'company nil :noerror) - (when mastodon-toot--enable-completion - ;; convert our company backends into capfs for use with corfu: - ;; FIXME replace this with a customize - (if (and (require 'cape nil :noerror) - (bound-and-true-p corfu-mode)) - (dolist (company-backend (list #'mastodon-toot-tags #'mastodon-toot-mentions)) - (add-hook 'completion-at-point-functions - (cape-company-to-capf company-backend) - nil - 'local)) - ;; else stick with company: - (set (make-local-variable 'company-backends) - (add-to-list 'company-backends 'mastodon-toot-mentions)) - (add-to-list 'company-backends 'mastodon-toot-tags)) - (unless (bound-and-true-p corfu-mode) ; don't clash w corfu mode - (company-mode-on)))) + ;; set up completion: + (when mastodon-toot--enable-completion + ;; (setq-local + (set + (make-local-variable 'completion-at-point-functions) + (add-to-list + 'completion-at-point-functions + #'mastodon-toot-mentions-capf)) + (add-to-list + 'completion-at-point-functions + #'mastodon-toot-tags-capf) + (when mastodon-toot--use-company-for-completion + (company-mode-on))) + ;; after-change: (make-local-variable 'after-change-functions) (push #'mastodon-toot--update-status-fields after-change-functions) (mastodon-toot--refresh-attachments-display) -- cgit v1.2.3 From 468add918b8e1e790294cdc7ff5e34cf1bf862f6 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Tue, 22 Nov 2022 21:44:43 +0100 Subject: silence the tags/handles searche queries --- lisp/mastodon-search.el | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lisp/mastodon-search.el') diff --git a/lisp/mastodon-search.el b/lisp/mastodon-search.el index fc7bd8e..8530b5c 100644 --- a/lisp/mastodon-search.el +++ b/lisp/mastodon-search.el @@ -67,8 +67,8 @@ Returns a nested list containing user handle, display name, and URL." (interactive "sSearch mastodon for: ") (let* ((url (mastodon-http--api "accounts/search")) (response (if (equal mastodon-toot--completion-style-for-mentions "following") - (mastodon-http--get-search-json url query "following=true") - (mastodon-http--get-search-json url query)))) + (mastodon-http--get-search-json url query "following=true" :silent) + (mastodon-http--get-search-json url query nil :silent)))) (if capf (mapcar #'mastodon-search--get-user-info-@-capf response) (mapcar #'mastodon-search--get-user-info-@ response)))) @@ -81,7 +81,7 @@ QUERY is the string to search." (interactive "sSearch for hashtag: ") (let* ((url (format "%s/api/v2/search" mastodon-instance-url)) (type-param (concat "type=hashtags")) - (response (mastodon-http--get-search-json url query type-param)) + (response (mastodon-http--get-search-json url query type-param :silent)) (tags (alist-get 'hashtags response))) (mapcar #'mastodon-search--get-hashtag-info tags))) -- cgit v1.2.3 From e311d491977fb9012d30ed146231f95ea52008af Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Thu, 24 Nov 2022 17:28:52 +0100 Subject: docstrings and autoloads --- lisp/mastodon-profile.el | 8 +++++--- lisp/mastodon-search.el | 3 +-- lisp/mastodon-toot.el | 1 + 3 files changed, 7 insertions(+), 5 deletions(-) (limited to 'lisp/mastodon-search.el') diff --git a/lisp/mastodon-profile.el b/lisp/mastodon-profile.el index 69cd65d..d5ef7a8 100644 --- a/lisp/mastodon-profile.el +++ b/lisp/mastodon-profile.el @@ -154,7 +154,8 @@ contains") (mastodon-tl--property 'toot-json)) (defun mastodon-profile--make-author-buffer (account &optional no-reblogs) - "Take an ACCOUNT json and insert a user account into a new buffer." + "Take an ACCOUNT json and insert a user account into a new buffer. +NO-REBLOGS means do not display boosts in statuses." (mastodon-profile--make-profile-buffer-for account "statuses" #'mastodon-tl--timeline no-reblogs)) @@ -553,7 +554,8 @@ FIELDS means provide a fields vector fetched by other means." (defun mastodon-profile--make-profile-buffer-for (account endpoint-type update-function &optional no-reblogs) - "Display profile of ACCOUNT, using ENDPOINT-TYPE and UPDATE-FUNCTION." + "Display profile of ACCOUNT, using ENDPOINT-TYPE and UPDATE-FUNCTION. +NO-REBLOGS means do not display boosts in statuses." (let* ((id (mastodon-profile--account-field account 'id)) (args (when no-reblogs '(("exclude_reblogs" . "t")))) (url (mastodon-http--api (format "accounts/%s/%s" id endpoint-type))) @@ -664,7 +666,7 @@ FIELDS means provide a fields vector fetched by other means." (goto-char (point-min)))) (defun mastodon-profile--format-joined-date-string (joined) - "Format a Joined timestamp." + "Format a human-readable Joined string from timestamp JOINED." (let ((joined-ts (ts-parse joined))) (format "Joined %s" (concat (ts-month-name joined-ts) " " diff --git a/lisp/mastodon-search.el b/lisp/mastodon-search.el index f83cccb..b037faa 100644 --- a/lisp/mastodon-search.el +++ b/lisp/mastodon-search.el @@ -173,8 +173,7 @@ user's profile note. This is also called by json)) (defun mastodon-search--propertize-user (acct &optional note) - "Propertize display string for ACCT, optionally including profile -NOTE." + "Propertize display string for ACCT, optionally including profile NOTE." (let ((user (mastodon-search--get-user-info acct))) (propertize (concat (propertize (car user) diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el index 4f9fb1b..0e21b0e 100644 --- a/lisp/mastodon-toot.el +++ b/lisp/mastodon-toot.el @@ -84,6 +84,7 @@ (autoload 'mastodon-http--build-array-params-alist "mastodon-http") (autoload 'mastodon-tl--get-endpoint "mastodon-tl") (autoload 'mastodon-http--put "mastodon-http") +(autoload 'mastodon-tl--return-fave-char "mastodon-tl") ;; for mastodon-toot--translate-toot-text (autoload 'mastodon-tl--content "mastodon-tl") -- cgit v1.2.3 From eca8401b6ed04ed0f787efcd8517b022c55f9ed7 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Fri, 25 Nov 2022 17:25:03 +0100 Subject: comment remove company mention --- lisp/mastodon-search.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lisp/mastodon-search.el') diff --git a/lisp/mastodon-search.el b/lisp/mastodon-search.el index 9d8ee65..65c5aba 100644 --- a/lisp/mastodon-search.el +++ b/lisp/mastodon-search.el @@ -47,7 +47,7 @@ (defvar mastodon-toot--enable-completion-for-mentions) (defvar mastodon-tl--buffer-spec) -;; functions for company completion of mentions in mastodon-toot +;; functions for completion of mentions in mastodon-toot (defun mastodon-search--get-user-info-@-capf (account) "Get user handle, display name and account URL from ACCOUNT." -- cgit v1.2.3 From ae8f6c6d7dfdd8048c8bab21b3afd81f981fbc96 Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Sat, 26 Nov 2022 14:51:45 +0100 Subject: always use --set-buffer-spec, not setq --buffer-spec --- lisp/mastodon-async.el | 13 +++++-------- lisp/mastodon-search.el | 19 ++++++++----------- 2 files changed, 13 insertions(+), 19 deletions(-) (limited to 'lisp/mastodon-search.el') diff --git a/lisp/mastodon-async.el b/lisp/mastodon-async.el index 8a08416..58e7b93 100644 --- a/lisp/mastodon-async.el +++ b/lisp/mastodon-async.el @@ -229,14 +229,11 @@ ENDPOINT is the endpoint for the stream and timeline." (mastodon-tl--timeline (mastodon-http--get-json (mastodon-http--api endpoint)))) (mastodon-mode) - (setq mastodon-tl--buffer-spec - `(buffer-name - ,buffer-name - endpoint ,endpoint - update-function - ,(if (equal name "notifications") - 'mastodon-notifications--timeline - 'mastodon-tl--timeline))) + (mastodon-tl--set-buffer-spec buffer-name + endpoint + ,(if (equal name "notifications") + 'mastodon-notifications--timeline + 'mastodon-tl--timeline)) (setq-local mastodon-tl--enable-relative-timestamps nil) (setq-local mastodon-tl--display-media-p t) (current-buffer)))) diff --git a/lisp/mastodon-search.el b/lisp/mastodon-search.el index 65c5aba..4aa5721 100644 --- a/lisp/mastodon-search.el +++ b/lisp/mastodon-search.el @@ -40,12 +40,13 @@ (autoload 'mastodon-auth--access-token "mastodon-auth") (autoload 'mastodon-http--get-search-json "mastodon-http") (autoload 'mastodon-http--api "mastodon-http") +(autoload 'mastodon-tl--set-buffer-spec "mastodon-tl") + (defvar mastodon-toot--completion-style-for-mentions) (defvar mastodon-instance-url) (defvar mastodon-tl--link-keymap) (defvar mastodon-http--timeout) (defvar mastodon-toot--enable-completion-for-mentions) -(defvar mastodon-tl--buffer-spec) ;; functions for completion of mentions in mastodon-toot @@ -101,11 +102,9 @@ QUERY is the string to search." (mastodon-mode) (let ((inhibit-read-only t)) (erase-buffer) - (setq mastodon-tl--buffer-spec - `(buffer-name ,buffer - endpoint ,(format "api/v1/trends") - update-function - (lambda (toot) (message "Trends.")))) + (mastodon-tl--set-buffer-spec buffer + "api/v1/trends" + nil) ;; hashtag results: (insert (mastodon-tl--set-face (concat "\n ------------\n" @@ -141,11 +140,9 @@ QUERY is the string to search." (mastodon-mode) (let ((inhibit-read-only t)) (erase-buffer) - (setq mastodon-tl--buffer-spec - `(buffer-name ,buffer - endpoint ,(format "api/v2/search") - update-function - (lambda (toot) (message "Searched.")))) + (mastodon-tl--set-buffer-spec buffer + "api/v2/search" + nil) ;; user results: (insert (mastodon-tl--set-face (concat "\n ------------\n" -- cgit v1.2.3 From 1f4870555241fcc55f6f2c2e2f6f64993ec2c3ad Mon Sep 17 00:00:00 2001 From: marty hiatt Date: Sat, 26 Nov 2022 16:33:07 +0100 Subject: remove stray company funs in search.el --- lisp/mastodon-search.el | 14 +++----------- lisp/mastodon-toot.el | 3 +-- 2 files changed, 4 insertions(+), 13 deletions(-) (limited to 'lisp/mastodon-search.el') diff --git a/lisp/mastodon-search.el b/lisp/mastodon-search.el index 4aa5721..1aed676 100644 --- a/lisp/mastodon-search.el +++ b/lisp/mastodon-search.el @@ -50,19 +50,13 @@ ;; functions for completion of mentions in mastodon-toot -(defun mastodon-search--get-user-info-@-capf (account) +(defun mastodon-search--get-user-info-@ (account) "Get user handle, display name and account URL from ACCOUNT." (list (concat "@" (cdr (assoc 'acct account))) (cdr (assoc 'url account)) (cdr (assoc 'display_name account)))) -(defun mastodon-search--get-user-info-@ (account) - "Get user handle, display name and account URL from ACCOUNT." - (list (cdr (assoc 'display_name account)) - (concat "@" (cdr (assoc 'acct account))) - (cdr (assoc 'url account)))) - -(defun mastodon-search--search-accounts-query (query &optional capf) +(defun mastodon-search--search-accounts-query (query) "Prompt for a search QUERY and return accounts synchronously. Returns a nested list containing user handle, display name, and URL." (interactive "sSearch mastodon for: ") @@ -70,9 +64,7 @@ Returns a nested list containing user handle, display name, and URL." (response (if (equal mastodon-toot--completion-style-for-mentions "following") (mastodon-http--get-json url `(("q" . ,query) ("following" . "true")) :silent) (mastodon-http--get-json url `(("q" . ,query)) :silent)))) - (if capf - (mapcar #'mastodon-search--get-user-info-@-capf response) - (mapcar #'mastodon-search--get-user-info-@ response)))) + (mapcar #'mastodon-search--get-user-info-@ response))) ;; functions for tags completion: diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el index 688717d..099ce10 100644 --- a/lisp/mastodon-toot.el +++ b/lisp/mastodon-toot.el @@ -853,8 +853,7 @@ eg. \"feduser@fed.social\" -> \"feduser@fed.social\"." ;; just for the annotation-function? (setq mastodon-toot-completions (mastodon-search--search-accounts-query - (buffer-substring-no-properties start end) - :capf)))) + (buffer-substring-no-properties start end))))) :exclusive 'no :annotation-function (lambda (candidate) -- cgit v1.2.3