diff options
author | mousebot <mousebot@riseup.net> | 2022-02-15 13:03:47 +0100 |
---|---|---|
committer | mousebot <mousebot@riseup.net> | 2022-02-15 13:53:16 +0100 |
commit | 51378f17d14ff877dfea29e496be64383b65da93 (patch) | |
tree | 5ded464561c8af08e6cba0a00a6714582947424d /lisp/mastodon-tl.el | |
parent | 3e80d32fbce9a242c1f7080effbff3dcab5a9871 (diff) |
buffer local keymap, error handling, make filters nicer
- separate goto-next/prev-filter commands
- we use properties toot-id and byline so the navigation works, calqued on
- goto-prev/next-toot
- error handle no word or context supplied
- reload filters view on create or delete
Diffstat (limited to 'lisp/mastodon-tl.el')
-rw-r--r-- | lisp/mastodon-tl.el | 115 |
1 files changed, 79 insertions, 36 deletions
diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el index d69cb1a..0423e2e 100644 --- a/lisp/mastodon-tl.el +++ b/lisp/mastodon-tl.el @@ -64,6 +64,8 @@ "Display NOTIFICATIONS in buffer." t) ; interactive (autoload 'mastodon-search--insert-users-propertized "mastodon-search") (autoload 'mastodon-search--get-user-info "mastodon-search") +(autoload 'mastodon-http--delete "mastodon-http") + (when (require 'mpv nil :no-error) (declare-function mpv-start "mpv")) (defvar mastodon-instance-url) @@ -171,6 +173,14 @@ types of mastodon links and not just shr.el-generated ones.") (defvar mastodon-tl--view-filters-keymap (let ((map (make-sparse-keymap))) (define-key map (kbd "d") 'mastodon-tl--delete-filter) + (define-key map (kbd "c") 'mastodon-tl--create-filter) + (define-key map (kbd "n") 'mastodon-tl--goto-next-filter) + (define-key map (kbd "p") 'mastodon-tl--goto-prev-filter) + (define-key map (kbd "TAB") 'mastodon-tl--goto-next-filter) + (define-key map (kbd "g") 'mastodon-tl--view-filters) + (define-key map (kbd "t") 'mastodon-toot) + (define-key map (kbd "q") 'kill-current-buffer) + (define-key map (kbd "Q") 'kill-buffer-and-window) (keymap-canonicalize map)) "Keymap for viewing filters.") @@ -1102,18 +1112,25 @@ Prompt for a context, must be a list containting at least one of \"home\", (interactive) (let* ((url (mastodon-http--api "filters")) (word (read-string - (format "Word to filter (%s): " (current-word)) - nil nil (current-word))) - (contexts (completing-read-multiple - "Contexts to filter [TAB for options]:" - '("home" "notifications" "public" "thread"))) + (format "Word(s) to filter (%s): " (or (current-word) "")) + nil nil (or (current-word) ""))) + (contexts + (if (equal "" word) + (error "You must select at least one word for a filter") + (completing-read-multiple + "Contexts to filter [TAB for options]:" + '("home" "notifications" "public" "thread") + nil ; no predicate + t))) ; require-match, as context is mandatory (contexts-processed - (mapcar (lambda (x) - (cons "context[]" x)) - contexts)) + (if (equal nil contexts) + (error "You must select at least one context for a filter") + (mapcar (lambda (x) + (cons "context[]" x)) + contexts))) (response (mastodon-http--post url (push - `("phrase" . ,word) - contexts-processed) + `("phrase" . ,word) + contexts-processed) nil))) (mastodon-http--triage response (lambda () @@ -1123,28 +1140,39 @@ Prompt for a context, must be a list containting at least one of \"home\", (mastodon-tl--view-filters)))))) (defun mastodon-tl--view-filters () - "" + "View the user's filters in a new buffer." (interactive) - (let ((url (mastodon-http--api "filters"))) - (mastodon-tl--init-sync "*mastodon-filters*" - "filters" - 'mastodon-tl--insert-filters) - (mastodon-tl--goto-next-toot))) + (mastodon-tl--init-sync "filters" + "filters" + 'mastodon-tl--insert-filters) + (use-local-map mastodon-tl--view-filters-keymap) + (mastodon-tl--goto-next-filter)) (defun mastodon-tl--insert-filters (json) - "" + "Insert the user's current filters. +JSON is what is returned by by the server." (insert (mastodon-tl--set-face (concat "\n ------------\n" " CURRENT FILTERS\n" " ------------\n\n") - 'success)) - (mapc (lambda (x) - (mastodon-tl--insert-filter-string x) - (insert "\n\n")) - json)) + 'success) + (mastodon-tl--set-face + "[c - create filter\n d - delete filter at point\n n/p - go to next/prev filter]\n\n" + 'font-lock-comment-face)) + (if (not (equal json '[])) + (progn + (mapc (lambda (x) + (mastodon-tl--insert-filter-string x) + (insert "\n\n")) + json)) + (insert (propertize + "Looks like you have no filters for now." + 'face font-lock-comment-face + 'byline t + 'toot-id "0")))) ; so point can move here when no filters (defun mastodon-tl--insert-filter-string (filter) - "" + "Insert a single FILTER." (let* ((phrase (alist-get 'phrase filter)) (contexts (alist-get 'context filter)) (id (alist-get 'id filter)) @@ -1152,23 +1180,38 @@ Prompt for a context, must be a list containting at least one of \"home\", (mapconcat #'identity contexts ", ")))) (insert (propertize filter-string - 'toot-id id ;for goto-next-toot compat + 'toot-id id ;for goto-next-filter compat 'phrase phrase - 'help-echo "d to delete filter at point, n/p to go to next/prev filter." - 'keymap mastodon-tl--view-filters-keymap - 'byline t)))) ;for goto-next-toot compat + ;'help-echo "n/p to go to next/prev filter, c to create new filter, d to delete filter at point." + ;'keymap mastodon-tl--view-filters-keymap + 'byline t)))) ;for goto-next-filter compat (defun mastodon-tl--delete-filter () - "" + "Delete filter at point." (interactive) - (let* ((filter-id (mastodon-tl--property 'toot-id)) - (phrase (mastodon-tl--property 'phrase)) - (url (mastodon-http--api - (format "filters/%s" filter-id))) - (response (mastodon-http--delete url))) - (mastodon-http--triage response (lambda () - (message "Filter for \"%s\" deleted!" phrase) - (mastodon-tl--view-filters))))) + (let* ((filter-id (get-text-property (point) 'toot-id)) + (phrase (get-text-property (point) 'phrase)) + (url (mastodon-http--api + (format "filters/%s" filter-id)))) + (if (equal nil filter-id) + (error "No filter at point?") + (when (y-or-n-p (format "Delete this filter? "))) + (let ((response (mastodon-http--delete url))) + (mastodon-http--triage response (lambda () + (mastodon-tl--view-filters) + (message "Filter for \"%s\" deleted!" phrase))))))) + +(defun mastodon-tl--goto-next-filter () + "Jump to next filter." + (interactive) + (mastodon-tl--goto-toot-pos 'next-single-property-change + 'next-line)) + +(defun mastodon-tl--goto-prev-filter () + "Jump to previous filter." + (interactive) + (mastodon-tl--goto-toot-pos 'previous-single-property-change + 'previous-line)) (defun mastodon-tl--get-follow-suggestions () "Display a buffer of suggested accounts to follow." |