diff options
Diffstat (limited to 'lisp')
-rw-r--r-- | lisp/mastodon-tl.el | 115 | ||||
-rw-r--r-- | lisp/mastodon.el | 2 |
2 files changed, 81 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." diff --git a/lisp/mastodon.el b/lisp/mastodon.el index a52bf41..180674e 100644 --- a/lisp/mastodon.el +++ b/lisp/mastodon.el @@ -85,6 +85,7 @@ (autoload 'mastodon-tl--poll-vote "mastodon-http") ;; (autoload 'mastodon-toot--delete-and-redraft-toot "mastodon-toot") (autoload 'mastodon-profile--view-bookmarks "mastodon-profile") +(autoload 'mastoton-tl--view-filters "mastodon-tl") ;; (autoload 'mastodon-toot--bookmark-toot-toggle "mastodon-toot") (defgroup mastodon nil @@ -164,6 +165,7 @@ Use. e.g. \"%c\" for your locale's date and time format." (define-key map (kbd "v") #'mastodon-tl--poll-vote) (define-key map (kbd "k") #'mastodon-toot--bookmark-toot-toggle) (define-key map (kbd "K") #'mastodon-profile--view-bookmarks) + (define-key map (kbd "L") #'mastodon-tl--view-filters) map) "Keymap for `mastodon-mode'.") |