Skip to content

Instantly share code, notes, and snippets.

@aufflick
Last active February 9, 2018 23:44
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save aufflick/857999 to your computer and use it in GitHub Desktop.
Save aufflick/857999 to your computer and use it in GitHub Desktop.
Piping to an emacs buffer with emacsclient
#!/usr/bin/env perl
# You can use this script in a pipe. It's input will become an emacs buffer
# via emacsclient (so you need server-start etc.)
# See http://mark.aufflick.com/o/886457 for more information
# Copyright (C) 2011 by Mark Aufflick <mark@aufflick.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
use strict;
use warnings;
use IO::Select;
my $emacsclient = "/Applications/Emacs.app/Contents/MacOS/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".
my $buffer_name = "*piped-$$" . '*';
exit 1
if 0 != system("$emacsclient -n --eval '(progn (pop-to-buffer (get-buffer-create \"$buffer_name\")))'");
my $s = IO::Select->new;
if (@ARGV) {
for my $path (@ARGV) {
open my $fh, $path
or die $!;
$s->add($fh);
}
}
else {
$s->add(\*STDIN);
}
my $acc = "";
my $line = 0;
my @ready;
while (@ready = $s->can_read) {
exit(0)
if @ready == 0;
for my $fh (@ready) {
my $data = <$fh>;
$s->remove($fh)
if ! $data;
exit(0)
if $s->count == 0;
# need to escape backslashes first, otherwise we end up escaping the backslashes
# we're using to escape the quotes...
$data =~ s/\\/\\\\/g;
$data =~ s/"/\\"/g;
$data =~ s/'/'\\''/g;
$acc .= $data;
system(qq{$emacsclient -n --eval '(with-current-buffer "$buffer_name" (goto-char (point-max)) (insert "} . $acc . qq{"))'})
if $line++ % 100 == 0;
}
}
@philjackson
Copy link

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

@aufflick
Copy link
Author

aufflick commented Mar 8, 2011

The problem with that approach is that you are forking a process for every single line in the piped input. If you, eg, pipe a large logfile that way it will be very slow.

@jkarres
Copy link

jkarres commented Feb 9, 2018

Don't you need to clear out $acc?

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