zachcurry / emacs-anywhere

Configurable automation + hooks called with application information
MIT License
1.08k stars 49 forks source link

Populate the buffer with the current contents of the textarea #64

Open awkspace opened 4 years ago

awkspace commented 4 years ago

This project is awesome for writing new text, but I'd like to be able to edit text that's already present. Right now I just copy/paste it into Emacs when invoking emacs-anywhere, but I was wondering if this was possible to get automated?

zachcurry commented 4 years ago

Hi @awkspace,

There is no option for automating this use case via the package. We've had discussions about this use case in the past and decided that selecting, copying, and pasting was preferable to automation because it gives the user more control over how text is edited, but If you can think of a clever way of providing this feature in such a way that the hook function(s) have a say in whether or not to select and copy the text to the buffer, then I'd be open to adding this feature to the package.

The path of least resistance for your purposes might be to just wrap the shell script from this package with a tiny script of your own to do the select and copy steps. Then, paste to your Emacs buffer using the hook function.

tecosaur commented 4 years ago

I thought I might just contribute my approach here. I also run into the situation where I want to over-write some text, and populate emacs-anywhere with it. I do this by selecting the text, which if you want to do in say a textbox is of course as easy as Ctrl A, then add the following into my ea-popup-handler hook:

(if (x-selection-exists-p 'PRIMARY)
    (insert (x-selection)))        

When exiting emacs, via default OS behaviour, the selection is overwritten :tada:.

d1egoaz commented 4 years ago

not sure if its' because I'm on OSX but your idea @tecosaur doesn't work for me.

I've tried:

(insert (gui-selection-value))
(insert (gui-get-selection 'PRIMARY))
(insert (x-get-selection 'PRIMARY))

even with the when clause:

(when (gui-get-primary-selection)
   (insert (gui-get-selection 'PRIMARY)))

it doesn't work, my doom-emacs closes the window immediately, not sure why apparently the call to (gui-get-selection 'PRIMARY) makes the frame to close

tecosaur commented 4 years ago

@d1egoaz I went through a few iterations after that initial comment, I now have

(when (gui-get-selection 'PRIMARY)
  (insert (gui-get-selection 'PRIMARY)))

Which works for me. I'm on linux though so...

d1egoaz commented 4 years ago

yeah, it doesn't work for OSX 😞

tecosaur commented 4 years ago

I've got another iteration of this to make up for (gui-get-selection 'PRIMARY) often giving a bogus non-active selection, thanks to some help I got.

(defun ea-popup-handler (app-name window-title x y w h)
  ;; other stuff here
  (when (gui-get-selection 'PRIMARY) (insert (gui-get-selection 'PRIMARY)))
  (set-transient-map (let ((keymap (make-sparse-keymap)))
                          (define-key keymap (kbd "DEL")   (lambda! (delete-region (point-min) (point-max))))
                          (define-key keymap (kbd "C-SPC") (lambda! (delete-region (point-min) (point-max))))
                          keymap)))

Press DEL or C-SPC immidiately after pulling up EmacsAnywhere and it will clear the buffer.

You can find my complete config for EA here if it's of any use to anyone, https://github.com/tecosaur/emacs-config/blob/e9a8ce2bff967436def88a75135fa4bbe6c86224/config.el#L155-L215