Skip to content

Instantly share code, notes, and snippets.

@diasjorge
Forked from calas/FirefoxEmacsHandler
Created September 11, 2009 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save diasjorge/185188 to your computer and use it in GitHub Desktop.
Save diasjorge/185188 to your computer and use it in GitHub Desktop.
Txmt protocol for emacs
#!/usr/bin/env ruby
# To install:
# about:config
# create a new boolean network.protocol-handler.external.txmt with value true
# create a new string network.protocol-handler.app.txmt with value path to the script
require 'rubygems'
require 'cgi'
require 'uri'
require 'logger'
class UrlParser
attr_reader :url, :file, :line, :column
def initialize(url)
@url = CGI::unescape(url.gsub(/(emacs|txmt):\/\/open\?url=/, ''))
parse_url
end
def to_args
[line_and_column, file].compact.join(' ')
end
def line_and_column
if line
"+" << [line, column].compact.join(':')
end
end
private
# url = 'txmt://open?url=file:///url/to/file.txt&line=3&column=8'
# url = 'emacs://open?url=file:///url/to/file.txt&line=1&column=3'
def parse_url
regexp = Regexp.new(/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/bin/emacsclient -a /usr/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