Created
September 12, 2010 19:51
-
-
Save sstephenson/576375 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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