alphapapa / ement.el

A Matrix client for GNU Emacs
GNU General Public License v3.0
476 stars 44 forks source link

[Feature Request] Command to copy message body #171

Open progfolio opened 1 year ago

progfolio commented 1 year ago

As discused in #ement.el:matrix.org, it would be nice to have a command to copy a message's body to the kill-ring. e.g.

(defun ement-room-copy-message (pos &optional  formatted)
  "Copy message at POS to `kill-ring'. POS Defaults to point.
If FORMATTED is non-nil, when called with a prefix argument, retain body formatting.
Otherwise, the plain text message body is copied."
  (interactive (list (point) current-prefix-arg))
  (or (derived-mode-p 'ement-room-mode) (user-error "Not in an ement room"))
  (if-let ((event (ewoc-data (ewoc-locate ement-ewoc pos)))
           ((equal (ement-event-type event) "m.room.message"))
           (content (ement-event-content event))
           (body (or (and formatted (alist-get 'formatted_body content))
                     (alist-get 'body content))))
      (ignore (kill-new body))
    (user-error "No message at point")))
alphapapa commented 1 year ago

Would you submit this as a PR with updates to the documentation and changelog, please?

A few thoughts:

Thanks.

progfolio commented 1 year ago

Would you submit this as a PR with updates to the documentation and changelog, please?

No thanks. I meant it more as a suggestion. Feel free to use or improve upon the example I gave. I don't have the time for a proper PR.

A few thoughts:

* If the function is called non-interactively without POS, it will fail.  Probably there's no need to have it take the POS argument; it would probably be enough to just use point in all cases.

That should've been a required parameter instead of optional. It offers more flexibility when used programmatically. I tend toward designing commands that way because it's usually the case that someone ends up calling it in a lisp program.

* It would probably be more useful and idiomatic to print some kind of message in the echo area rather than using `ignore`.

Agreed.

* It's probably a good idea to ensure that the event has a formatted body before trying to use it, because it's not required that one does.

The body will be used in cases where there is no formatted body. If there is no body, the user-error is signaled.

alphapapa commented 1 year ago

Ok, thanks.