Created
April 26, 2010 06:41
-
-
Save fullofcaffeine/379048 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/ruby | |
require 'rubygems' | |
require 'ruby-debug' | |
if $0 =~ /superman\z/ | |
init_filter = ARGV[0] | |
if init_filter | |
ARGV.replace(["--init-filter=#{init_filter}", | |
File.expand_path("~/.supermegadoc/man.cdb")]) | |
else | |
ARGV.replace([File.expand_path("~/.supermegadoc/man.cdb")]) | |
end | |
end | |
$for_emacs = false | |
$no_devhelp = false | |
mega_extra = '' | |
loop do | |
if ARGV[0] == '--for-emacs' | |
$for_emacs = true | |
ARGV.shift | |
redo | |
end | |
if ARGV[0] && ARGV[0][0,14] == '--init-filter=' | |
mega_extra = "'#{ARGV[0]}'" | |
ARGV.shift | |
redo | |
end | |
if ARGV[0] == '--no-devhelp' | |
$no_devhelp = true | |
ARGV.shift | |
redo | |
end | |
break | |
end | |
ARGV[0] or raise("Need path to .cdb") | |
cdb_path = ARGV[0] | |
unless File.readable?(cdb_path) | |
candidate = File.expand_path("~/.supermegadoc/#{cdb_path}") | |
if File.readable?(candidate) | |
cdb_path = candidate | |
else | |
candidate = candidate + ".cdb" | |
if File.readable?(candidate) | |
cdb_path = candidate | |
else | |
raise "given .cdb file not found: #{cdb_path}" | |
end | |
end | |
end | |
extra_args = `cdb -q -- '#{cdb_path}' --extra-args` | |
output = `cdb -l -m '#{cdb_path}' | grep -v -- --extra-args | gpicker #{extra_args} #{mega_extra} --name-separator='\\n' -`.chomp | |
exit if output.empty? | |
alternatives = `cdb -q -- '#{cdb_path}' '#{output}'`.split("\0k") | |
alternatives = [output] | |
alternatives.first.insert(0,"ri:") | |
if $for_emacs | |
print alternatives[0] | |
exit | |
end | |
def silent_exec(*args) | |
exec(*args) | |
rescue Exception | |
end | |
#alternatives = alternatives.reverse | |
alternatives.each do |u| | |
if u[0,8] == 'devhelp:' | |
next if $no_devhelp | |
silent_exec "devhelp", "-s" , u[8..-1] | |
elsif u[0,4] == 'man:' | |
sec, name = u[4..-1].split('/',2) | |
unless STDOUT.isatty && STDIN.isatty | |
ENV['MANOPT'] = '-Hx-www-browser' | |
end | |
silent_exec "man", sec, name | |
elsif u[0,3] == 'ri:' | |
exec 'ri', '-T', u[3..-1] | |
elsif (prefix = u[0,5]) == 'http:' || prefix == 'file:' | |
exec "gnome-www-browser", u | |
# exec "x-www-browser", u | |
end | |
end | |
# if unknown URL scheme | |
print alternatives[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment