Skip to content

Instantly share code, notes, and snippets.

@sstephenson
Created September 12, 2010 19:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sstephenson/576375 to your computer and use it in GitHub Desktop.
Save sstephenson/576375 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# aquamacs-editor: my $EDITOR for Aquamacs. Starts Aquamacs, waits
# for emacs-server to load, then runs emacsclient with the given
# arguments. If data is piped to stdin, it's buffered to a temporary
# file and passed to emacsclient instead. Waits for 'C-x #' unless
# invoked with -n.
#
# sstephenson 2010-09-12
require "tempfile"
module Aquamacs
extend self
def start
system("aquamacs")
end
def server_socket
`lsof -Fn -aUc Aquamacs`[/^n(.*)/, 1]
end
def wait_for_server(timeout = 10, start_time = Time.now)
start
loop do
if Time.now - start_time > timeout
raise "Aquamacs server not found -- did you (server-start)?"
elsif server_socket
break
else
sleep 0.1
end
end
end
end
module EmacsClient
extend self
def start(args, options = {})
command = ["emacsclient", "-s", Aquamacs.server_socket]
command << "--no-wait" if options[:wait] == false
system(*command.concat(args))
end
def open(io, options = {})
Tempfile.open(File.basename($0)) do |tempfile|
tempfile.write(io.read(4096)) until io.eof?
tempfile.flush
start([tempfile.path], options)
end
end
end
begin
wait = ARGV.delete("-n") != "-n"
Aquamacs.wait_for_server
if ARGV.length > 0
EmacsClient.start(ARGV, :wait => wait)
elsif !$stdin.tty? && !$stdin.closed?
EmacsClient.open($stdin, :wait => wait)
end
rescue => e
warn "#$0: #{e}"
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment