Skip to content

Instantly share code, notes, and snippets.

@vy-let
Created January 3, 2017 01:46
Show Gist options
  • Save vy-let/a37b0b9bf91172761d54d98a6b72f777 to your computer and use it in GitHub Desktop.
Save vy-let/a37b0b9bf91172761d54d98a6b72f777 to your computer and use it in GitHub Desktop.
Make emacsclient work like it should
#!/usr/bin/env ruby
# Violet Baddley
# 2017
#
# Install in your favorite PATH directory as `em`
# chmod a+x
#
# usage: em [emacsclient options] [file...]
# whatever | em [emacsclient options]
#
# Writes the piped command output (if given) to a temp file
# and then launches `emacsclient -t` with your arguments.
require 'tempfile'
def pipe_stdin_to tempf
loop do
file_segment = $stdin.readpartial(4096)
tempf.write file_segment
tempf.flush
end
rescue EOFError => e
# fucking finally.
end
if $stdin.tty?
em_pid = spawn('/usr/local/bin/emacsclient', '-t', *ARGV)
Process.wait em_pid
else
tempfile = Tempfile.new 'emacs-stdin'
tempfile.set_encoding 'ASCII-8BIT', 'ASCII-8BIT'
$stdin.set_encoding 'ASCII-8BIT'
begin
pipe_stdin_to tempfile
em_pid = spawn('/usr/local/bin/emacsclient', '-t', *ARGV, tempfile.path)
Process.wait em_pid
ensure
tempfile.close
tempfile.unlink
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment