New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy selection to clipboard when invoked. #3

Open
petrosb opened this Issue Feb 28, 2018 · 11 comments

Comments

Projects
None yet
3 participants
@petrosb

petrosb commented Feb 28, 2018

Would be nice if I select something in the text box before opening emacs to have it appear in the emacs buffer so I can edit it before pasting again. Here is how to do it (two trivial changes; feel free to incorporate it if you want, or I can create a pull request if you prefer, or just ignore it):

on run {input, parameters}
	tell application "System Events"
		set appName to name of first application process whose frontmost is true
		keystroke "c" using command down  -- copy before launching emacs; the yank later pastes it in emacs buffer
	end tell
	do shell script "PATH=/Applications/Emacs.app/Contents/MacOS/bin/:$PATH ; emacsclient -a \"\" -c -e \"(progn
  (defun ea-clipboard-save (frame)
    (clipboard-kill-ring-save
     (point-min)
     (point-max))
   (kill-buffer \\\"*Emacs Anywhere*\\\"))
  (defun ea-hook-clipboard-save ()
    (add-hook 'delete-frame-functions 'ea-clipboard-save))
  (defun ea-unhook-clipboard-save ()
    (remove-hook 'delete-frame-functions 'ea-clipboard-save))
  (defun emacs-anywhere ()
    (interactive)
    (if (y-or-n-p \\\"Emacs Anywhere: Delete *Emacs Anywhere* buffer and copy current buffer to clipboard on frame delete? \\\")
        (ea-hook-clipboard-save)
      (ea-unhook-clipboard-save)))
  (ea-hook-clipboard-save)
  (switch-to-buffer \\\"*Emacs Anywhere*\\\")
  (yank)
 (select-frame-set-input-focus (selected-frame)))\""
	tell application appName to activate
	tell application "System Events" to keystroke "v" using command down -- comment this line ine to disable auto-paste
	return
end run

(edited to format code)

@petrosb

This comment has been minimized.

Show comment
Hide comment
@petrosb

petrosb Feb 28, 2018

Great script, btw. Very useful!

petrosb commented Feb 28, 2018

Great script, btw. Very useful!

@zachcurry

This comment has been minimized.

Show comment
Hide comment
@zachcurry

zachcurry Feb 28, 2018

Owner

Hi, @petrosb 👋 !
I like it! I need to separate the Elisp from the Applescript and put it all together in a build step. Then, we can put things like this into their respective script files where one can uncomment some lines to enable certain functionality.

I'll work on cleaning this thing up as I've described. Then, it should be easier to add to via pull requests. Stay tuned...

Owner

zachcurry commented Feb 28, 2018

Hi, @petrosb 👋 !
I like it! I need to separate the Elisp from the Applescript and put it all together in a build step. Then, we can put things like this into their respective script files where one can uncomment some lines to enable certain functionality.

I'll work on cleaning this thing up as I've described. Then, it should be easier to add to via pull requests. Stay tuned...

@zachcurry zachcurry changed the title from Copy the selected buffer to the new emacs window. to Copy selection to clipboard when invoked. Mar 4, 2018

@zachcurry zachcurry referenced this issue Mar 5, 2018

Closed

Edit text #27

@zachcurry

This comment has been minimized.

Show comment
Hide comment
@zachcurry

zachcurry Mar 11, 2018

Owner

@petrosb your feature is ready as a configuration!

Get the latest from master

curl -fsSL https://raw.github.com/zachcurry/emacs-anywhere/master/install | bash

Add the second line below the comment in ~/.emacs_anywhere/config

# Your configs go here
export EA_SHOULD_COPY=true

Cheers!

Owner

zachcurry commented Mar 11, 2018

@petrosb your feature is ready as a configuration!

Get the latest from master

curl -fsSL https://raw.github.com/zachcurry/emacs-anywhere/master/install | bash

Add the second line below the comment in ~/.emacs_anywhere/config

# Your configs go here
export EA_SHOULD_COPY=true

Cheers!

@zachcurry zachcurry closed this Mar 11, 2018

@feigaoxyz

This comment has been minimized.

Show comment
Hide comment
@feigaoxyz

feigaoxyz Mar 12, 2018

A quick follow-up:
Is there a way to paste the copied text into new *Emacs Anywhere* buffer automatically?

feigaoxyz commented Mar 12, 2018

A quick follow-up:
Is there a way to paste the copied text into new *Emacs Anywhere* buffer automatically?

@zachcurry

This comment has been minimized.

Show comment
Hide comment
@zachcurry

zachcurry Mar 12, 2018

Owner

@feigaoxyz no. It seems like a nice compliment to this feature though! Do you think we should add it?

Owner

zachcurry commented Mar 12, 2018

@feigaoxyz no. It seems like a nice compliment to this feature though! Do you think we should add it?

@feigaoxyz

This comment has been minimized.

Show comment
Hide comment
@feigaoxyz

feigaoxyz Mar 12, 2018

I do think so. And that's actually PO proposed.
I mean, the whole purpose to select some text and copy to clipboard before invoking this tool is to have it shown in the buffer, right?

feigaoxyz commented Mar 12, 2018

I do think so. And that's actually PO proposed.
I mean, the whole purpose to select some text and copy to clipboard before invoking this tool is to have it shown in the buffer, right?

@feigaoxyz

This comment has been minimized.

Show comment
Hide comment
@feigaoxyz

feigaoxyz Mar 12, 2018

p.s.
I think the names EA_SHOULD_COPY and EA_SHOULD_PASTE need some work, as they are not so self-explain and README does not make them clear.

feigaoxyz commented Mar 12, 2018

p.s.
I think the names EA_SHOULD_COPY and EA_SHOULD_PASTE need some work, as they are not so self-explain and README does not make them clear.

@zachcurry

This comment has been minimized.

Show comment
Hide comment
@zachcurry

zachcurry Mar 12, 2018

Owner

I think you mean OP and I agree after rereading his/her post that he/she intended for the text to appear in the buffer. I must have glanced at the title when I added this late last night. Also, the README and the names of these options are probably less than ideal like you say.

Owner

zachcurry commented Mar 12, 2018

I think you mean OP and I agree after rereading his/her post that he/she intended for the text to appear in the buffer. I must have glanced at the title when I added this late last night. Also, the README and the names of these options are probably less than ideal like you say.

@petrosb

This comment has been minimized.

Show comment
Hide comment
@petrosb

petrosb Mar 12, 2018

Haven't had a chance to try the latest version, but to clarify the OP's comment (:-P), the script I provided above copies the selected text from the app upon activation using
keystroke "c" using command down -- copy before launching emacs; the yank later pastes it in emacs
and then when the emacs anywhere buffer is activated is using
(yank)
to copy the text from the clipboard into the buffer. Thus, when the script is run, the Emacs window will contain the selected text, ready to be edited.

In hindsight, something I did not consider is what happens if no text is selected, so the Command-C does nothing, and there is old text in the Clipboard. In a quick test I just performed, the old text appears in the window, which might not be desired behavior. If I haven't selected something, I would expect the Emacs Anywhere buffer to be empty (not sure how to fix that easily). For me, it's an acceptable trade-off for now, but you might feel otherwise.

PS. Apologies for nod properly making the code in the original ticket look like code... Don't use github tickets/comments very often, so I completely missed this.

petrosb commented Mar 12, 2018

Haven't had a chance to try the latest version, but to clarify the OP's comment (:-P), the script I provided above copies the selected text from the app upon activation using
keystroke "c" using command down -- copy before launching emacs; the yank later pastes it in emacs
and then when the emacs anywhere buffer is activated is using
(yank)
to copy the text from the clipboard into the buffer. Thus, when the script is run, the Emacs window will contain the selected text, ready to be edited.

In hindsight, something I did not consider is what happens if no text is selected, so the Command-C does nothing, and there is old text in the Clipboard. In a quick test I just performed, the old text appears in the window, which might not be desired behavior. If I haven't selected something, I would expect the Emacs Anywhere buffer to be empty (not sure how to fix that easily). For me, it's an acceptable trade-off for now, but you might feel otherwise.

PS. Apologies for nod properly making the code in the original ticket look like code... Don't use github tickets/comments very often, so I completely missed this.

@zachcurry

This comment has been minimized.

Show comment
Hide comment
@zachcurry

zachcurry Mar 12, 2018

Owner

Thanks @petrosb ! I messed up and I didn't see the part where you call (yank). No apologies necessary. I'll see if I can think of a good way of handling the implementation concerns you've brought up because they are totally valid.

Owner

zachcurry commented Mar 12, 2018

Thanks @petrosb ! I messed up and I didn't see the part where you call (yank). No apologies necessary. I'll see if I can think of a good way of handling the implementation concerns you've brought up because they are totally valid.

@zachcurry zachcurry reopened this Mar 12, 2018

@zachcurry

This comment has been minimized.

Show comment
Hide comment
@zachcurry

zachcurry Mar 22, 2018

Owner

@petrosb I think Emacs Anywhere will let you do this now. In fact, I think you can do anything with it now 😎

Have a look at the README. A lot of things have changed lately.

Let me know if you have any questions 😄

Owner

zachcurry commented Mar 22, 2018

@petrosb I think Emacs Anywhere will let you do this now. In fact, I think you can do anything with it now 😎

Have a look at the README. A lot of things have changed lately.

Let me know if you have any questions 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment