Begin main content

Piping to an emacs buffer with emacsclient

GNU Emacs buffs will know all about emacsclient - it's a commandline program that allows you to open files in a new buffer in an already running emacs instance. It's very handy. What you may not know is you can also evaluate arbitrary Emacs lisp using emacsclient. Using this fact and based on an existing example from EmacsWiki I have written a perl script that you can pipe to, and the piped data will appear in a buffer via emacsclient.

My ultimate aim is for a PAGER script to use instead of less in my emacs shell. While this isn't quite perfect for that use, it's still pretty useful.

NB: The code below is probably out of date by the time you read this - see this gist for the latest version that you can clone, fork etc.

#!/usr/bin/perl
usestrict;
usewarnings;

useIO::Select;

my$emacsclient = "/usr/local/bin/emacsclient";

# This script uses emacsclient, be sure to have a running server session.
# A server-session can be started by "M-x server-start".
exit 1
    if 0 != system("$emacsclient -n --eval '(progn (pop-to-buffer (get-buffer-create \"*piped*\")))'");

my$s = IO::Select->new;
$s->add(\*STDIN);

while (1)
{
    # block until data available
my$data = <STDIN>;

    # exit if STDIN closed
exit(0)
        if!$data;

    # keep reading while data is available, or we have a bunch of lines
my$lines = 0;
    $data .= <STDIN>
        while$lines++ < 100 && $s->can_read(.5);

    $data =~ s/"/\\"/g;
    $data =~ s/'/'\\''/g;
    $data =~ s/\\/\\\\/g;
    system(qq{$emacsclient -n --eval '(with-current-buffer "*piped*" (goto-char (point-max)) (insert "}
            . $data . qq{"))'});
}

06:23 PM, 02 Mar 2011 by Mark Aufflick Permalink

One more escape

Just needs to escape backslashes too, $data =~ s/\\/\\\\/g;

by Unregistered Visitor on 03/03/11

Good point

I was thinking it's not necessary since the shell single quote doesn't treat backslashes specially, but of course the emacs lisp double quoted string will. I've updated the main post accordingly.

by Mark Aufflick on 03/06/11

Note I've updated the gist

So it now works for file paths as arguments too, since some commands assume the PAGER can do that.

by Unregistered Visitor on 03/04/14

enterprise cloud software provider

The importance of both. But several impediments have constrained a broad-based realization of this goal.enterprise cloud software provider

by Unregistered Visitor on 02/22/15

thanks

Let your idea of doing like this be a successful one. We need these kinds of innovations from you guys. We nerds are searching for innovative ideas in this field. Thanks a lot for sharing your new ideaclick.

by Unregistered Visitor on 02/25/15

website

App Creator It looks like I am raking in money now, but the truth is that it was not always like that! Just 6 months ago I was completely struggling. I had no programming skills, no team and no money. The only thing I had was a great desire to create the game of my dreams. http://03dd4gqsm6tr0yebio1bnhhsxg.hop.clickbank.net/?tid=PHXDAN ...

by Unregistered Visitor on 02/26/15

Internet Marketing Conference

That is good .It gives me happy .I think we will have a harmonious talk.Do you agree? Internet Marketing Conference

by Unregistered Visitor on 03/01/15

car insurance detuctible explained

Here at this site really the fastidious material collection so that everybody can enjoy a lot. car insurance detuctible explained

by Unregistered Visitor on 03/05/15

Thanks

While this isn't quite perfect for that use, it's still pretty useful. how to buy soundcloud followers

by Frank Banes on 03/05/15

new york city loans

The problem is that you provide may be worth our time and also effort. new york city loans

by Unregistered Visitor on 03/10/15

abbycristal

Blackberries are perennial, meaning the same blackberry plant will continue to bear fruit year after year. They produce fruit from late summer to early fall and grow from 4 7 feet tall. They grow best with full sun and in soil with good drainage, in regions with minimum temperatures ranging from 20 to 20 degrees. lighthouseautomotivellccos |

by Unregistered Visitor on 03/11/15

abbycristal

Hi my loved one! I wish to say that this article is awesome, great written and include almost all significant infos. I would like to see more posts like this. vpmautogb |

by Unregistered Visitor on 03/11/15

I just wanted to let you know

I just wanted to let you know that what you do really affects peoples lives and that people - like me - truly appreciate it. www.techviewmedia.com |

by Unregistered Visitor on 03/11/15

Valuable very touchable information.

Valuable very touchable information. I would like to thank you for sharing your great facts. www.collegenowtechprep.com |

by Unregistered Visitor on 03/11/15

James

Because of burning from inside the student is getting sick and having stress that causes a lot of diseases like low blood pressure of fever. The parent’s mood is also very bad with the kid. They used to best essays review read more or scold him also.

by Unregistered Visitor on 03/12/15

EDUCATED

Inquire as to whether they comprehend your exposition theme. In the event that they blankly gesture and seem concerned then dissertation writing service you will need to deal with your methodology or your theme. Your thesis subject can be endorsed quicker in case you're clear and brief about the theme.

by Unregistered Visitor on 03/12/15

hero

If I would have to choose the best website with writing services then write top essays for me is my first choice and. The argumentative essay I once ordered here was a very effective essay and guess what, the topic I asked them was very hard but they were still able to make a great one.

by Unregistered Visitor on 03/18/15

Nice information

Thanks for Share some useful Information with us. we also share some information about our business, we deals in all types of spy cameras, stun gun, cheating playing cards, spy bluetooth earpiece etc. we sell these products all over in india with free shipping service and 1 year replacement warranty. interested person can click on link Spy Camera in Hyderabad to buy spy camera in affordable price. we provide best customer support and cash on delivery facility

by Siddharth Verma on 03/21/15

Zeine

we are know that there are many events celebrated in many countries but few of them are celebrated all over the world. Events are a part of our life's we must celebrate as many as events with our family and other whom we love. My favourite events are easter sunday 2015good friday 2015 and April Fools Pranks what about yours?

by Unregistered Visitor on 03/21/15

abbibarclay

I am truly inspired by this online journal! Extremely clear clarification of issues is given and it is open to every living soul. I have perused your post, truly you have given this extraordinary informative data about it. housecallhomecare.com |

by Unregistered Visitor on 03/23/15

abbibarclay

This is a fantastic website and I can not recommend you guys enough. Full of useful resource and great layout very easy on the eyes. homeporthospice.com |

by Unregistered Visitor on 03/23/15

Angel Rio

Stupendous blog you guys have provided there, I will absolutely valuate your effort.Best Electric Shaver Blog

by Angel Rio on 03/24/15

Thanks

Thank you for this useful info. Fabfurnish Coupons

by Unregistered Visitor on 03/27/15

Justin Kemp

Personally I think overjoyed I discovered the blogs.best Citation service

by Justin Kemp on 03/28/15

Add comment