Skip to content

Instantly share code, notes, and snippets.

@calas
Created April 20, 2009 09:05
Show Gist options
  • Save calas/98443 to your computer and use it in GitHub Desktop.
Save calas/98443 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'logger'
class UrlParser
attr_reader :url, :file, :line, :column
def initialize(url)
@url = url
parse_url
end
def to_args
if line && column
str = "+#{line}:#{column} #{file}"
else
file
end
end
private
# url = 'emacs://open?url=file:///home/jorge/dev/neris/source/app/controllers/inicio_controller.rb&line=1&column=3'
def parse_url
regexp = Regexp.new(/emacs:\/\/open\?url=file:\/\/(.+)&line=([0-9]+)&column=([0-9]+)/)
if url.match(regexp)
@file = $1
@line = $2
@column = $3
else
@file = url
end
end
end
class EmacsClient
DEFAULT_COMMAND = "/usr/local/bin/emacsclient -a /usr/local/bin/emacs"
attr_reader :command, :arguments
def initialize(args, command=nil)
@command = command || DEFAULT_COMMAND
@arguments = args
end
def launch
exec "#{command} #{arguments}"
end
end
parser = UrlParser.new(ARGV[0])
emacs = EmacsClient.new(parser.to_args)
emacs.launch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment