embark icon indicating copy to clipboard operation
embark copied to clipboard

Possible addition to the README?

Open apc opened this issue 2 years ago • 1 comments

Those of us trying to just reuse code from embark.el and adapt it to our need could use a brief mention in the README of the role that embark-multitarget-actions plays in determining how arguments are passed to embark actions.

I just spent an embarrassingly long time trying to understand why this didn't work as intended:


(defun my/embark-copy-to-clipboard (strings)
  "Join STRINGS and pass to the system clipboard using `simpleclip-set-contents'.
With a prefix argument, prompt for the separator to join the
STRINGS, which defaults to a newline."
  (simpleclip-set-contents (string-join strings (embark--separator strings))))

(define-key embark-general-map "W" #'my/embark-copy-to-clipboard)

I was looking for a way of mimicking the behavior of embark-copy-as-kill to send stuff to the system clipboard rather than the kill ring. So I figured I could just adapt embark-copy-as-kill to use simpleclip-set-contents rather than kill-new and get exactly what I was looking for.

After running into error message after error message, I discovered that the reason embark-copy-as-kill works the way it does is because it's an element of embark-multitarget-actions. Thus I could do what I wanted (mimic the behavior of embark-copy-as-kill but having the system clipboard play the role of the kill ring) I needed to add this one line of code.


(add-to-list 'embark-multitarget-actions 'my/embark-copy-to-clipboard)

Of course, if I had just read the README and understood all of it, I would have known how to define my function directly, say:


(defun my/embark-copy-to-clipboard (string)
      "Pass STRING to the system clipboard using `simpleclip-set-contents'."
      (simpleclip-set-contents string))
    
(define-key embark-general-map "W" #'my/embark-copy-to-clipboard)

But I'm probably not the only one who just tries to reuse function definitions and tweak them a little rather than start from scratch, even when I don't fully understand why the original definition works the way it does. 😜

apc avatar Nov 18 '23 16:11 apc

I should definitely add something about embark-multitarget-actions to the README! The only reason I haven't is that I mistakenly thought I already had!

By the way, for your last code snippet, I'd cut out the middle man and just use (define-key embark-general-map "W" #'simpleclip-set-contents).

oantolin avatar Nov 18 '23 18:11 oantolin