aboutsummaryrefslogtreecommitdiff
path: root/lisp/mastodon-toot.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/mastodon-toot.el')
-rw-r--r--lisp/mastodon-toot.el333
1 files changed, 160 insertions, 173 deletions
diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el
index 9f46cb6..099ce10 100644
--- a/lisp/mastodon-toot.el
+++ b/lisp/mastodon-toot.el
@@ -41,11 +41,7 @@
(require 'cl-lib)
(require 'persist)
-(when (require 'company nil :noerror)
- (declare-function company-mode-on "company")
- (declare-function company-begin-backend "company")
- (declare-function company-grab-symbol "company")
- (defvar company-backends))
+(require 'mastodon-iso)
(defvar mastodon-instance-url)
(defvar mastodon-tl--buffer-spec)
@@ -79,9 +75,10 @@
(autoload 'mastodon-profile--fetch-server-account-settings "mastodon-profile")
(autoload 'mastodon-tl--render-text "mastodon-tl")
(autoload 'mastodon-profile--fetch-server-account-settings-maybe "mastodon-profile")
-(autoload 'mastodon-http--build-array-args-alist "mastodon-http")
+(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--symbol "mastodon-tl")
;; for mastodon-toot--translate-toot-text
(autoload 'mastodon-tl--content "mastodon-tl")
@@ -103,18 +100,24 @@
:group 'mastodon-toot
:type 'integer)
-(defcustom mastodon-toot--enable-completion
- (if (require 'company nil :noerror) t nil)
+(defcustom mastodon-toot--enable-completion t
"Whether to enable completion of mentions and hashtags.
+Used for completion in toot compose buffer."
+ :group 'mastodon-toot
+ :type 'boolean)
-Used for completion in toot compose buffer.
+(defcustom mastodon-toot--use-company-for-completion nil
+ "Whether to enable company for completion.
-This is only used if company mode is installed."
+When non-nil, `company-mode' is enabled in the toot compose
+buffer, and mastodon completion backends are added to
+`company-capf'.
+
+You need to install company yourself to use this."
:group 'mastodon-toot
:type 'boolean)
-(defcustom mastodon-toot--completion-style-for-mentions
- (if (require 'company nil :noerror) "following" "off")
+(defcustom mastodon-toot--completion-style-for-mentions "all"
"The company completion style to use for mentions."
:group 'mastodon-toot
:type '(choice
@@ -169,6 +172,9 @@ change the setting on the server, see
(defvar-local mastodon-toot-poll nil
"A list of poll options for the toot being composed.")
+(defvar-local mastodon-toot--language nil
+ "The language of the toot being composed, in ISO 639 (two-letter).")
+
(defvar-local mastodon-toot--reply-to-id nil
"Buffer-local variable to hold the id of the toot being replied to.")
@@ -182,6 +188,9 @@ Takes its form from `window-configuration-to-register'.")
(defvar mastodon-toot--max-toot-chars nil
"The maximum allowed characters count for a single toot.")
+(defvar-local mastodon-toot-completions nil
+ "The data of completion candidates for the current completion at point.")
+
(defvar mastodon-toot-current-toot-text nil
"The text of the toot being composed.")
@@ -199,6 +208,13 @@ send.")
"\\(@[^ \n\t]*\\)?\\)" ; with poss domain, * = allow only @
"\\b"))
+(defvar mastodon-toot-tag-regex
+ (concat
+ ;; preceding space or bol [boundary doesn't work with #]
+ "\\([\n\t ]\\|^\\)"
+ "\\(?2:#[1-9a-zA-Z_]+\\)" ; tag
+ "\\b")) ; boundary
+
(defvar mastodon-toot-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-c") #'mastodon-toot--send)
@@ -211,6 +227,7 @@ send.")
(define-key map (kbd "C-c C-a") #'mastodon-toot--attach-media)
(define-key map (kbd "C-c !") #'mastodon-toot--clear-all-attachments)
(define-key map (kbd "C-c C-p") #'mastodon-toot--create-poll)
+ (define-key map (kbd "C-c C-l") #'mastodon-toot--set-toot-lang)
map)
"Keymap for `mastodon-toot'.")
@@ -227,6 +244,7 @@ send.")
NO-TOOT means we are not calling from a toot buffer."
(mastodon-http--get-json-async
(mastodon-http--api "instance")
+ nil
'mastodon-toot--get-max-toot-chars-callback no-toot))
(defun mastodon-toot--get-max-toot-chars-callback (json-response
@@ -326,7 +344,9 @@ TYPE is a symbol, either 'favourite or 'boost."
(list 'boosted-p (not boosted))
(list 'favourited-p (not faved))))
(mastodon-toot--action-success
- (if boost-p "B" "F")
+ (if boost-p
+ (mastodon-tl--symbol 'boost)
+ (mastodon-tl--symbol 'favourite))
byline-region remove))
(message (format "%s #%s" (if boost-p msg action) id))))))
(message (format "Nothing to %s here?!?" action-string)))))
@@ -346,23 +366,21 @@ TYPE is a symbol, either 'favourite or 'boost."
"Bookmark or unbookmark toot at point."
(interactive)
(let* ( ;(toot (mastodon-tl--property 'toot-json))
- (id (mastodon-tl--property 'base-toot-id))
- ;; (mastodon-tl--as-string (mastodon-tl--toot-id toot)))
- (bookmarked-p (mastodon-tl--property 'bookmarked-p))
- (prompt (if bookmarked-p
- (format "Toot already bookmarked. Remove? ")
- (format "Bookmark this toot? ")))
- (byline-region
- (when id
- (mastodon-tl--find-property-range 'byline (point))))
- (action (if bookmarked-p "unbookmark" "bookmark"))
- (bookmark-str (if (fontp (char-displayable-p #10r128278))
- "🔖"
- "K"))
- (message (if bookmarked-p
- "Bookmark removed!"
- "Toot bookmarked!"))
- (remove (when bookmarked-p t)))
+ (id (mastodon-tl--property 'base-toot-id))
+ ;; (mastodon-tl--as-string (mastodon-tl--toot-id toot)))
+ (bookmarked-p (mastodon-tl--property 'bookmarked-p))
+ (prompt (if bookmarked-p
+ (format "Toot already bookmarked. Remove? ")
+ (format "Bookmark this toot? ")))
+ (byline-region
+ (when id
+ (mastodon-tl--find-property-range 'byline (point))))
+ (action (if bookmarked-p "unbookmark" "bookmark"))
+ (bookmark-str (mastodon-tl--symbol 'bookmark))
+ (message (if bookmarked-p
+ "Bookmark removed!"
+ "Toot bookmarked!"))
+ (remove (when bookmarked-p t)))
(if byline-region
(when (y-or-n-p prompt)
(mastodon-toot--action
@@ -379,9 +397,12 @@ TYPE is a symbol, either 'favourite or 'boost."
(message (format "Nothing to %s here?!?" action)))))
(defun mastodon-toot--copy-toot-url ()
- "Copy URL of toot at point."
+ "Copy URL of toot at point.
+If the toot is a fave/boost notification, copy the URLof the
+base toot."
(interactive)
- (let* ((toot (mastodon-tl--property 'toot-json))
+ (let* ((toot (or (mastodon-tl--property 'base-toot)
+ (mastodon-tl--property 'toot-json)))
(url (if (mastodon-tl--field 'reblog toot)
(alist-get 'url (alist-get 'reblog toot))
(alist-get 'url toot))))
@@ -389,9 +410,12 @@ TYPE is a symbol, either 'favourite or 'boost."
(message "Toot URL copied to the clipboard.")))
(defun mastodon-toot--copy-toot-text ()
- "Copy text of toot at point."
+ "Copy text of toot at point.
+If the toot is a fave/boost notification, copy the text of the
+base toot."
(interactive)
- (let* ((toot (mastodon-tl--property 'toot-json)))
+ (let* ((toot (or (mastodon-tl--property 'base-toot)
+ (mastodon-tl--property 'toot-json))))
(kill-new (mastodon-tl--content toot))
(message "Toot content copied to the clipboard.")))
@@ -630,7 +654,7 @@ to `emojify-user-emojis', and the emoji data is updated."
(defun mastodon-toot--build-poll-params ()
"Return an alist of parameters for POSTing a poll status."
(append
- (mastodon-http--build-array-args-alist
+ (mastodon-http--build-array-params-alist
"poll[options][]"
(plist-get mastodon-toot-poll :options))
`(("poll[expires_in]" . ,(plist-get mastodon-toot-poll :expiry)))
@@ -661,9 +685,10 @@ instance to edit a toot."
("visibility" . ,mastodon-toot--visibility)
("sensitive" . ,(when mastodon-toot--content-nsfw
(symbol-name t)))
- ("spoiler_text" . ,spoiler)))
+ ("spoiler_text" . ,spoiler)
+ ("language" . ,mastodon-toot--language)))
(args-media (when mastodon-toot--media-attachments
- (mastodon-http--build-array-args-alist
+ (mastodon-http--build-array-params-alist
"media_ids[]"
mastodon-toot--media-attachment-ids)))
(args-poll (when mastodon-toot-poll
@@ -728,7 +753,7 @@ instance to edit a toot."
(defun mastodon-toot--get-toot-source (id)
"Fetch the source JSON of toot with ID."
(let ((url (mastodon-http--api (format "/statuses/%s/source" id))))
- (mastodon-http--get-json url :silent)))
+ (mastodon-http--get-json url nil :silent)))
(defun mastodon-toot--get-toot-edits (id)
"Return the edit history of toot with ID."
@@ -801,129 +826,74 @@ eg. \"feduser@fed.social\" -> \"feduser@fed.social\"."
(reverse (append mentions nil))
"")))
-(defun mastodon-toot--mentions-company-meta (candidate)
- "Format company completion CANDIDATE's meta field."
- (format " %s"
- (get-text-property 0 'meta candidate)))
-
-(defun mastodon-toot--mentions-company-annotation (candidate)
- "Format company completion CANDIDATE's annotation."
- (format " %s" (get-text-property 0 'annot candidate)))
-
-(defun mastodon-toot--mentions-company-make-candidate (candidate)
- "Construct a company completion CANDIDATE for display."
- (let ((display-name (car candidate))
- (handle (cadr candidate))
- (url (caddr candidate)))
- (propertize handle 'annot display-name 'meta url)))
-
-(defun mastodon-toot--tags-company-make-candidate (candidate)
- "Construct a company completion CANDIDATE for display."
- (let ((tag (concat "#" (car candidate)))
- (url (cadr candidate)))
- (propertize tag 'annot url 'meta url)))
-
-(defun mastodon-toot--company-build-candidates (query list-fun make-fun)
- "Build a list of completion candidates for a company backend.
-QUERY is the search prefix, LIST-FUN builds a list of items to
-match against, and MAKE-FUN builds the actual cadidate list item
-for display by company."
- (let ((query (substring query 1)) ; remove @ or # for search
- (res))
- (dolist (item (funcall list-fun query))
- (when (or (string-prefix-p query (substring (cadr item) 1) t)
- (string-prefix-p query (car item) t))
- (push (funcall make-fun item) res)))
- res))
-
-(defun mastodon-toot--mentions-company-candidates (query)
- "Given a company QUERY, build a list of candidates.
-The query can match both user handles and display names."
- (mastodon-toot--company-build-candidates
- query
- 'mastodon-search--search-accounts-query
- 'mastodon-toot--mentions-company-make-candidate))
-
-(defun mastodon-toot--tags-company-candidates (query)
- "Given a company QUERY, build a list of candidates.
-The query is matched against a tag search on the server."
- (mastodon-toot--company-build-candidates
- query
- 'mastodon-search--search-tags-query
- 'mastodon-toot--tags-company-make-candidate))
-
-(defun mastodon-toot--make-company-backend
- (command _backend-name str-prefix candidates-fun annot-fun meta-fun
- &optional arg
- &rest ignored)
- "Make a company backend for `mastodon-toot-mode'.
-COMMAND, ARG, IGNORED are all company backend args.
-COMMAND is either prefix, to fetch a prefix query, candidates, to
-build a list of candidates with query ARG, annotation, to format
-an annotation for candidate ARG, or meta, to format meta info for
-candidate ARG. IGNORED remains a mystery.
-
-BACKEND-NAME is the backend's name, STR-PREFIX is used to search
-for matches, CANDIDATES-FUN, ANNOT-FUN, and META-FUN are
-functions called on ARG to generate formatted candidates, annotation, and
-meta fields respectively."
- (interactive (list 'interactive))
- (let ((handle-before
- ;; hack to handle @handles@with.domains, as "@" is a word/symbol boundary
- (if (string= str-prefix "@")
- (save-match-data
- (save-excursion
- (re-search-backward mastodon-toot-handle-regex nil :no-error)
- (if (match-string-no-properties 2)
- ;; match full handle inc. domain (see the regex for subexp 2)
- (buffer-substring-no-properties (match-beginning 2) (match-end 2))
- ""))))))
- (cl-case command
- (interactive (company-begin-backend (quote backend-name)))
- (prefix (when (and (bound-and-true-p mastodon-toot-mode) ; if masto toot minor mode
- (save-excursion
- (forward-whitespace -1)
- (forward-whitespace 1)
- (looking-at str-prefix)))
- (if (and (string= str-prefix "@")
- (> (length handle-before) 1)) ; more than just @
- (concat str-prefix (substring-no-properties handle-before 1)) ; handle
- (concat str-prefix (company-grab-symbol))))) ; tag
- (candidates (funcall candidates-fun arg))
- (annotation (funcall annot-fun arg))
- (meta (funcall meta-fun arg)))))
-
-(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
-build a list of candidates with query ARG, annotation, to format
-an annotation for candidate ARG, or meta, to format meta info for
-candidate ARG. IGNORED remains a mystery."
- (mastodon-toot--make-company-backend
- command
- 'mastodon-toot-mentions
- "@"
- 'mastodon-toot--mentions-company-candidates
- 'mastodon-toot--mentions-company-annotation
- 'mastodon-toot--mentions-company-meta
- arg
- ignored))
-
-(defun mastodon-toot-tags (command &optional arg &rest ignored)
- "A company completion backend for toot tags.
-COMMAND is either prefix, to fetch a prefix query, candidates, to
-build a list of candidates with query ARG, annotation, to format
-an annotation for candidate ARG, or meta, to format meta info for
-candidate ARG. IGNORED remains a mystery."
- (mastodon-toot--make-company-backend
- command
- 'mastodon-toot-tags
- "#"
- 'mastodon-toot--tags-company-candidates
- 'mastodon-toot--mentions-company-annotation
- 'mastodon-toot--mentions-company-meta
- arg
- ignored))
+(defun mastodon-toot--get-bounds (regex)
+ "Get bounds of tag or handle before point."
+ ;; needed because # and @ are not part of any existing thing at point
+ (save-match-data
+ (save-excursion
+ ;; match full handle inc. domain, or tag including #
+ ;; (see the regexes for subexp 2)
+ (when (re-search-backward regex nil :no-error)
+ (cons (match-beginning 2)
+ (match-end 2))))))
+
+(defun mastodon-toot--mentions-capf ()
+ "Build a mentions completion backend for `completion-at-point-functions'."
+ (let* ((bounds
+ (mastodon-toot--get-bounds mastodon-toot-handle-regex))
+ (start (car bounds))
+ (end (cdr bounds)))
+ (when bounds
+ (list start
+ end
+ ;; only search when necessary:
+ (completion-table-dynamic
+ (lambda (_)
+ ;; TODO: do we really need to set a local var here
+ ;; just for the annotation-function?
+ (setq mastodon-toot-completions
+ (mastodon-search--search-accounts-query
+ (buffer-substring-no-properties start end)))))
+ :exclusive 'no
+ :annotation-function
+ (lambda (candidate)
+ (concat " "
+ (mastodon-toot--mentions-annotation-fun candidate)))))))
+
+(defun mastodon-toot--tags-capf ()
+ "Build a tags completion backend for `completion-at-point-functions'."
+ (let* ((bounds
+ (mastodon-toot--get-bounds mastodon-toot-tag-regex))
+ (start (car bounds))
+ (end (cdr bounds)))
+ (when bounds
+ (list start
+ end
+ ;; only search when necessary:
+ (completion-table-dynamic
+ (lambda (_)
+ (setq mastodon-toot-completions
+ (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
+ :annotation-function
+ (lambda (candidate)
+ (concat " "
+ (mastodon-toot--tags-annotation-fun candidate)))))))
+
+(defun mastodon-toot--mentions-annotation-fun (candidate)
+ "Given a handle completion CANDIDATE, return its annotation string, a username."
+ (caddr (assoc candidate mastodon-toot-completions)))
+
+(defun mastodon-toot--tags-annotation-fun (candidate)
+ "Given a tag string CANDIDATE, return an annotation, the tag's URL."
+ ;; FIXME check the list returned here? should be cadr
+ ;;or make it an alist and use cdr
+ (caadr (assoc candidate mastodon-toot-completions)))
(defun mastodon-toot--reply ()
"Reply to toot at `point'.
@@ -1141,6 +1111,16 @@ LENGTH is the maximum character length allowed for a poll option."
("14 days" . ,(number-to-string (* 60 60 24 14)))
("30 days" . ,(number-to-string (* 60 60 24 30)))))
+(defun mastodon-toot--set-toot-lang ()
+ "Prompt for a language and set `mastodon-toot--language'.
+Return its two letter ISO 639 1 code."
+ (interactive)
+ (let* ((choice (completing-read "Language for this toot: "
+ mastodon-iso-639-1)))
+ (setq mastodon-toot--language
+ (alist-get choice mastodon-iso-639-1 nil nil 'equal))
+ (message "Language set to %s" choice)))
+
;; we'll need to revisit this if the binds get
;; more diverse than two-chord bindings
(defun mastodon-toot--get-mode-kbinds ()
@@ -1329,7 +1309,6 @@ This is how mastodon does it."
(replace-match (match-string 2))) ; replace with handle only
(length (buffer-substring (point-min) (point-max)))))
-
(defun mastodon-toot--save-toot-text (&rest _args)
"Save the current toot text in `mastodon-toot-current-toot-text'.
Added to `after-change-functions' in new toot buffers."
@@ -1391,13 +1370,12 @@ Added to `after-change-functions'."
;; stops all text after a handle or mention being propertized:
(set-text-properties (cdr header-region) (point-max) nil)
;; TODO: confirm allowed hashtag/handle characters:
- (mastodon-toot--propertize-item "\\([\n\t ]\\|^\\)\\(?2:#[1-9a-zA-Z_]+\\)\\b"
+ (mastodon-toot--propertize-item mastodon-toot-tag-regex
'success
(cdr header-region))
- (mastodon-toot--propertize-item
- mastodon-toot-handle-regex
- 'mastodon-display-name-face
- (cdr header-region)))))
+ (mastodon-toot--propertize-item mastodon-toot-handle-regex
+ 'mastodon-display-name-face
+ (cdr header-region)))))
(defun mastodon-toot--propertize-item (regex face start)
"Propertize item matching REGEX with FACE starting from START."
@@ -1445,13 +1423,22 @@ 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 company backends:
- (when (require 'company nil :noerror)
- (when mastodon-toot--enable-completion
+ ;; set up completion:
+ (when mastodon-toot--enable-completion
+ (set ; (setq-local
+ (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)
+ ;; company
+ (when mastodon-toot--use-company-for-completion
(set (make-local-variable 'company-backends)
- (add-to-list 'company-backends 'mastodon-toot-mentions))
- (add-to-list 'company-backends 'mastodon-toot-tags))
- (company-mode-on))
+ (add-to-list 'company-backends 'company-capf))
+ (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)