send selection or current line to Terminal/iTerm

classic Classic list List threaded Threaded
39 messages Options
12
Reply | Threaded
Open this post in threaded view
|

send selection or current line to Terminal/iTerm

klebsiella
Dear all,

I am using Aquamacs/Emacs on MacOsX.
What I desire is to be able to send the current active line (and eventually step to the next line) or current selection to the active Terminal session on my iTerm. I know I can use isend for sending the output to a "shell-buffer" launched in emacs but I prefer to use iTerm for that.

Is there a way to achieve that ?

Many thanks for your help
Best
Reply | Threaded
Open this post in threaded view
|

Re: send selection or current line to Terminal/iTerm

klebsiella
Hi Guys,
No reply to my request. Does anyone knows how to deal with the above mentioned issue or at least give me some advises/hints ?

Reply | Threaded
Open this post in threaded view
|

Re: send selection or current line to Terminal/iTerm

Emanuel Berg
In reply to this post by klebsiella
klebsiella <[hidden email]> writes:

> Hi Guys, No reply to my request. Does anyone knows
> how to deal with the above mentioned issue or at
> least give me some advises/hints?

I think you didn't get a reply because the issue was
stated so software-specific - I actually never heard of
iTerm (or Aquamacs for that matter), though perhaps
that is common knowledge...

But, I've done a lot of similar stuff from/to Emacs (in
a Linux VT) to/from tmux which runs on top of other VTs
(or xterm in X).

The easiest way is to setup a function in Emacs that
writes the line (or region) to a file. Then setup a
shortcut in Mac OS X (or iTerm, or the shell that runs
in iTerm: bash, zsh, etc. in the Linux world) to insert
the line at the cursor position.

I can't be more specific but perhaps that'll get you
going. If you are familiar with Emacs Lisp, perhaps
how I did it [1] for the Linux VTs, tmux, and X - using a
tool called xsel - is of interest (which works
cross-system as long as X runs) - but most likely it
will only confuse you, so try the file solution first,
I think :)

Depending on how the clipboard works in Mac OS X, you
can use the variable interprogram-cut-function to get a
completely transparent kill ring. I got that working,
but I thought it was too much overhead for every single
kill, because many (most) of them are intended either
simply to remove stuff, or to yank somewhere else in
Emacs.

[1] http://user.it.uu.se/~embe8573/conf/emacs-init/x-clipboard.el

--
underground experts united:
http://user.it.uu.se/~embe8573
Reply | Threaded
Open this post in threaded view
|

Re: send selection or current line to Terminal/iTerm

Jai Dayal
In reply to this post by klebsiella
Well, how do you expect Emacs to interact with a process executing outside
of its context?

How would you expect any other process, say Firefox, to interact with iterm?


On Thu, Jun 12, 2014 at 9:13 AM, klebsiella <[hidden email]> wrote:

> Hi Guys,
> No reply to my request. Does anyone knows how to deal with the above
> mentioned issue or at least give me some advises/hints ?
>
>
>
>
>
> --
> View this message in context:
> http://emacs.1067599.n5.nabble.com/send-selection-or-current-line-to-Terminal-iTerm-tp324145p324374.html
> Sent from the Emacs - Help mailing list archive at Nabble.com.
>
>
Reply | Threaded
Open this post in threaded view
|

Re: send selection or current line to Terminal/iTerm

klebsiella
Well, The way Sublime Text does it ... This is why I expect it to work with Emacs ...
Reply | Threaded
Open this post in threaded view
|

Re: send selection or current line to Terminal/iTerm

Jai Dayal
Okay, so how does sublime text do it?


On Thu, Jun 12, 2014 at 10:00 AM, klebsiella <[hidden email]>
wrote:

> Well, The way Sublime Text does it ... This is why I expect it to work with
> Emacs ...
>
>
>
> --
> View this message in context:
> http://emacs.1067599.n5.nabble.com/send-selection-or-current-line-to-Terminal-iTerm-tp324145p324377.html
> Sent from the Emacs - Help mailing list archive at Nabble.com.
>
>
Reply | Threaded
Open this post in threaded view
|

Re: send selection or current line to Terminal/iTerm

Emanuel Berg
In reply to this post by klebsiella
Jai Dayal <[hidden email]> writes:

> Well, how do you expect Emacs to interact with a
> process executing outside of its context?

What are you talking about? Of course that is
possible!

Ever heard of IPC, pipes, redirection, temporary files,
shared clipboards, shared memory (or simply agreed-upon
files on the same local disk), clients and servers,
Emacs' executing shell commands, etc., etc.?

--
underground experts united:
http://user.it.uu.se/~embe8573
Reply | Threaded
Open this post in threaded view
|

Re: send selection or current line to Terminal/iTerm

Jai Dayal
Correct, it's possible, but the applications that you're using have to be
programmed to expect information to come that way, right?

I'm surprised this has to be explained to you.

So, that's why I'm asking the OP how does Sublime Text do it with iterm?


On Thu, Jun 12, 2014 at 2:29 PM, Emanuel Berg <[hidden email]>
wrote:

> Jai Dayal <[hidden email]> writes:
>
> > Well, how do you expect Emacs to interact with a
> > process executing outside of its context?
>
> What are you talking about? Of course that is
> possible!
>
> Ever heard of IPC, pipes, redirection, temporary files,
> shared clipboards, shared memory (or simply agreed-upon
> files on the same local disk), clients and servers,
> Emacs' executing shell commands, etc., etc.?
>
> --
> underground experts united:
> http://user.it.uu.se/~embe8573
>
Reply | Threaded
Open this post in threaded view
|

Re: send selection or current line to Terminal/iTerm

Emanuel Berg
In reply to this post by Emanuel Berg
Jai Dayal <[hidden email]> writes:

> Correct, it's possible, but the applications that
> you're using have to be programmed to expect
> information to come that way, right?

Applications *always* have to be programmed to do
anything and everything, including communicating with
other applications.

> I'm surprised this has to be explained to you.

?!

> So, that's why I'm asking the OP how does Sublime
> Text do it with iterm?

Maybe the OP doesn't know *how* Sublime does it, he has
just observed that it can do it so he rightly assumes
other applications can do it as well?

--
underground experts united:
http://user.it.uu.se/~embe8573
Reply | Threaded
Open this post in threaded view
|

Re: send selection or current line to Terminal/iTerm

Jai Dayal
>Applications *always* have to be programmed to do
anything and everything, including communicating with
other applications.

Correct. So your statement of "of course it's possible" is just a strawman
argument because I never said it was impossible. You then went on useless
rant about how to communicate with other processes, which is useless
because I asked specifically how SublimeText does it.

"
Maybe the OP doesn't know *how* Sublime does it, he has
just observed that it can do it so he rightly assumes
other applications can do it as well?"

If you can extend or change the application, sure. Absolutely.

So, again, how does Sublime Text do it?




On Thu, Jun 12, 2014 at 2:36 PM, Emanuel Berg <[hidden email]>
wrote:

> Jai Dayal <[hidden email]> writes:
>
> > Correct, it's possible, but the applications that
> > you're using have to be programmed to expect
> > information to come that way, right?
>
> Applications *always* have to be programmed to do
> anything and everything, including communicating with
> other applications.
>
> > I'm surprised this has to be explained to you.
>
> ?!
>
> > So, that's why I'm asking the OP how does Sublime
> > Text do it with iterm?
>
> Maybe the OP doesn't know *how* Sublime does it, he has
> just observed that it can do it so he rightly assumes
> other applications can do it as well?
>
> --
> underground experts united:
> http://user.it.uu.se/~embe8573
>
Reply | Threaded
Open this post in threaded view
|

Re: send selection or current line to Terminal/iTerm

Emanuel Berg
In reply to this post by Emanuel Berg
Jai Dayal <[hidden email]> writes:

> So your statement of "of course it's possible" is
> just a strawman argument because I never said it was
> impossible. You then went on useless rant about how
> to communicate with other processes, which is useless

I'm not getting into an argument over this. This is is
not a debate. I know what I write and if you consider
that useless to all and everyone that's a statement
that cannot be verified anyway.

> If you can extend or change the application, sure.

You don't necessarily need to change *the* application,
though that would help, of course. There are many other
applications that you may put in-between to do such a
thing.

The example I gave was the code I wrote for Emacs,
which was about half a page - to tmux, it was a
oneliner, and the glue in between - xsel - I simply
installed. Also note the Elisp code is just loaded by
the Emacs Lisp interpreter at startup time - and the
tmux oneliner is in a tmux configuration file. While
this is certainly programming it isn't anything
extremely difficult that involves branching,
recompilation, etc. Besides it doesn't matter how
difficult it supposedly is. If you want to do it, get
in someone's face and do it. Simple as that.

--
underground experts united:
http://user.it.uu.se/~embe8573
Reply | Threaded
Open this post in threaded view
|

Re: send selection or current line to Terminal/iTerm

Jai Dayal
Right, you had to write code for Emacs, meaning you had to extend upon
emacs.
On Jun 12, 2014 3:15 PM, "Emanuel Berg" <[hidden email]> wrote:

> Jai Dayal <[hidden email]> writes:
>
> > So your statement of "of course it's possible" is
> > just a strawman argument because I never said it was
> > impossible. You then went on useless rant about how
> > to communicate with other processes, which is useless
>
> I'm not getting into an argument over this. This is is
> not a debate. I know what I write and if you consider
> that useless to all and everyone that's a statement
> that cannot be verified anyway.
>
> > If you can extend or change the application, sure.
>
> You don't necessarily need to change *the* application,
> though that would help, of course. There are many other
> applications that you may put in-between to do such a
> thing.
>
> The example I gave was the code I wrote for Emacs,
> which was about half a page - to tmux, it was a
> oneliner, and the glue in between - xsel - I simply
> installed. Also note the Elisp code is just loaded by
> the Emacs Lisp interpreter at startup time - and the
> tmux oneliner is in a tmux configuration file. While
> this is certainly programming it isn't anything
> extremely difficult that involves branching,
> recompilation, etc. Besides it doesn't matter how
> difficult it supposedly is. If you want to do it, get
> in someone's face and do it. Simple as that.
>
> --
> underground experts united:
> http://user.it.uu.se/~embe8573
>
Reply | Threaded
Open this post in threaded view
|

Re: send selection or current line to Terminal/iTerm

Hans BKK
In reply to this post by klebsiella
On Thursday, June 12, 2014 11:13:49 AM UTC-4, klebsiella wrote:
> No reply to my request. Does anyone knows how to deal with the above
> mentioned issue or at least give me some advises/hints ?

Maybe have a look at "Comint Mode", which is the basis for many "shell in a buffer" interactive modes such as SQL mode. . .
Reply | Threaded
Open this post in threaded view
|

Re: send selection or current line to Terminal/iTerm

John Mastro
In reply to this post by Jai Dayal
Hi all,

Jai Dayal <[hidden email]> wrote:
> So, again, how does Sublime Text do it?

I did a web search for "send text to iTerm" and found a SublimeText
extension called SendText[1]. I'm not sure if it's what the original
asker is using, and I don't have my Mac handy to test it at the moment,
but the bottom line is that it seems to be possible using a bit of
simple AppleScript.

[1] https://github.com/wch/SendText/blob/master/SendText.py

Best,

John
Reply | Threaded
Open this post in threaded view
|

Re: send selection or current line to Terminal/iTerm

Emanuel Berg
In reply to this post by Emanuel Berg
Jai Dayal <[hidden email]> writes:

> Right, you had to write code for Emacs, meaning you
> had to extend upon emacs.

I don't know if I had to do it or if I just did it - so
what? Sometimes you do that, sometimes you
don't. Whatever solves the problem, solves the
problem. In the Apple world I suspect such hacking is
less common and perhaps more difficult as well
(compared to the Linux world) but Emacs should be just
as configurable/extendable and as for the rest of the
software there is no doubt in my mind that users come
up with little schemes and fixes to get the software as
they like it. And if they don't know how to do it, they
are more than welcome to ask at a place like this.

--
underground experts united:
http://user.it.uu.se/~embe8573
Reply | Threaded
Open this post in threaded view
|

Re: send selection or current line to Terminal/iTerm

klebsiella
In reply to this post by Jai Dayal
@ Emanuel: Thanks for the reply and the suggestions. Unfortunately, I do not know lisp beside the basics to optimize my .emacs.

@jai: I agree with Emanuel statements. Actually, I have posted a similar post on Stackoverflow and somebody already explained how it can be done but I couldn't make it work: You can check it on the link pasted below. However, I posted the request here recently as I thought that their can be a more elegant/builtin way to do it that I am not aware of ...
In summary, Sublime Text calls osascript to paste the active region in the active session of the Terminal ...

Maybe if someone knows lisp enough can follow on the explanation given in the link below and check if he/she can make it work ?

http://stackoverflow.com/questions/22835546/emacs-aquamacs-send-current-line-or-selection-to-active-terminal
Reply | Threaded
Open this post in threaded view
|

Re: send selection or current line to Terminal/iTerm

Robert Thorpe-2
In reply to this post by klebsiella
klebsiella <[hidden email]> writes:

> Hi Guys,
> No reply to my request. Does anyone knows how to deal with the above
> mentioned issue or at least give me some advises/hints ?

When you copy or cut the region to the kill-ring it should be present on
the system clipboard too.  So, AFAIK, this should work, though I don't
use Emacs on OS X that's the general rule.  I think it's an Aquamacs
bug.

Try the "Emacs on OS X" mailing list http://aquamacs.org/support.shtml .

(Are you saying you want the region copied *before* you copy or paste?
That's a different question.)

BR,
Robert Thorpe

Reply | Threaded
Open this post in threaded view
|

Re: send selection or current line to Terminal/iTerm

Jai Dayal
In reply to this post by klebsiella
Okay, good, so it calls applescript.

Is there a way to have emacs call applescript programs?

It seems "yes", but I can't tell for sure.

*http://www.emacswiki.org/emacs/EmacsForMacOS#toc21
<http://www.emacswiki.org/emacs/EmacsForMacOS#toc21>*


On Thu, Jun 12, 2014 at 3:43 PM, klebsiella <[hidden email]> wrote:

> @ Emanuel: Thanks for the reply and the suggestions. Unfortunately, I do
> not
> know lisp beside the basics to optimize my .emacs.
>
> @jai: I agree with Emanuel statements. Actually, I have posted a similar
> post on Stackoverflow and somebody already explained how it can be done but
> I couldn't make it work: You can check it on the link pasted below.
> However,
> I posted the request here recently as I thought that their can be a more
> elegant/builtin way to do it that I am not aware of ...
> In summary, Sublime Text calls osascript to paste the active region in the
> active session of the Terminal ...
>
> Maybe if someone knows lisp enough can follow on the explanation given in
> the link below and check if he/she can make it work ?
>
>
> http://stackoverflow.com/questions/22835546/emacs-aquamacs-send-current-line-or-selection-to-active-terminal
>
>
>
> --
> View this message in context:
> http://emacs.1067599.n5.nabble.com/send-selection-or-current-line-to-Terminal-iTerm-tp324145p324400.html
> Sent from the Emacs - Help mailing list archive at Nabble.com.
>
>
Reply | Threaded
Open this post in threaded view
|

Re: send selection or current line to Terminal/iTerm

John Mastro
Hi klebsiella,

John Mastro <[hidden email]> wrote:
> This is totally untested (sorry, still not around a Mac), but I think
> something along these lines should work. Assuming that SublimeText
> extension works anyway.

By the way, if the code I posted does do approximately want you want,
I'd be happy to improve it a bit for you (e.g. handling escaping, as the
SublimeText version seems to).

If you try it, let me know if/how it works for you.

- John
Reply | Threaded
Open this post in threaded view
|

Re: send selection or current line to Terminal/iTerm

klebsiella
So guys,

First thank you all for your time.

Here is the code in my .emacs (see below) that is supposed to do the job (Thanks go to @JordonBiondo - Stackoverflow and also to you John Mastro)

I am now working on emacs (not Aquamacs) and the code is still not working. Again, as I do not know Lisp, it was hard for me to debug !!!

The error message given is I guess thrown by osascript, but may be linked to a syntax problem in lisp:

100:100: syntax error: Expected string but found end of script. (-2741)
Process send-text-iterm2-proc exited abnormally with code 1

Osascript itself is working. For instance the following line runs correctly on the terminal:

$ osascript -e 'tell application "iTerm" ' -e 'set mysession to current session of current terminal' -e 'tell mysession to write text "ls"' -e 'end tell'

It would be great to make it run (For everybody I believe)

.emacs related chunk
(defun sendtext:escape-text(s):
  (with-temp-buffer
    (insert s)
    (replace-string "\\" "\\\\" nil (point-min) (point-max))
    (replace-string "\"" "\\\"" nil (point-min) (point-max))
    (buffer-substring-no-properties (point-min) (point-max))))

(defun sendtext:send-iterm2(beg end)
  (interactive "r")
  (let ((text (buffer-substring-no-properties beg end)))
    (start-process "send-text-iterm2-proc" "*sendtext:output*"
                   "osascript"
                   "-e" "tell app \"iTerm\""
                   "-e" "set mysession to current session of current terminal"
                   "-e" "tell mysession to write text \""
                   (sendtext:escape-text text)
                   "\""
                   "-e" "end tell")))
John Mastro wrote
Hi klebsiella,

John Mastro <[hidden email]> wrote:
> This is totally untested (sorry, still not around a Mac), but I think
> something along these lines should work. Assuming that SublimeText
> extension works anyway.

By the way, if the code I posted does do approximately want you want,
I'd be happy to improve it a bit for you (e.g. handling escaping, as the
SublimeText version seems to).

If you try it, let me know if/how it works for you.

- John
12