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

Nice post

Guar protein offer a wide selection of products for your specific requirements. Established in the year 2008, we are one of the leading Manufacturer, Exporter and Supplier of Compound Guar Protein Meal Feeds and Poultry Feeds.
Guar meal exporter india and Organic Poultry feed

by Unregistered Visitor on 09/17/14

Cash advance the financial terms and conditions

Who can apply for instant cash loans must meet certain PoundsBucket criteria in order to retrieve loans. you must be 18 years old, United States citizens must pay the current employment a month should be more than 1000 USD. MoneyWorth financial access to obtain in advance an active bank account. If you have an CheapestQuickLoans emergency expense, these borrowers will apply. for these reasons, often medical bills, mortgage payments, auto repair bills, etc. Cash advance the financial qualification course It is to apply and immediate cash to qualify for funding, it is very easy to do, the only thing you can write a simple application form: this form, your social CashPaydayLoansNoCreditCheck security number, will be evidence of a new pay stub information with a copy of your payment history, birth, LowInterestLoansOnline current address and fill out the employment and driver's license number.

by Unregistered Visitor on 09/20/14

The difference between

The difference between an unsecured debt and secured payday loans direct lenders debt To understand the requirements of the safety margin, and the no guarantor loans main difference between an unsecured debt is now old-fashioned, low-risk mortgage debt and so are loans, equity loans payday loan no credit check unsecured debt, and therefore creditors. on behalf of debt repayment in the case of these loans without warranties payday loans no credit check associated with the high level of risk. risk rate is reflected in the very interest liabilities and risks associated with low level short term loans uk security, unsecured debt if the interest rate is low compared with most unsecured debt loans against car without much higher.

by Unregistered Visitor on 09/21/14

If you are

If you are a creditor payment plan a little more affordable price, may be able to extend such property development loans and low interest rates negotiations, which is to change the mortgage and car forex software are not a lot of time, but you can get the following monthly installments, probably. In addition, a comprehensive forex trading platforms from the federal government, according to the lender's student loan scheme / or income, family size, and a government, you asset based lending can reduce monthly payments by payday loans for people on benefits student loans, you may want to pay for the program.In addition, the public sector, there is payday loans no credit check the case of federal student loan forgiveness loan needs.

by Unregistered Visitor on 09/26/14

Add comment