forked from ryanb/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changed to rbates' ri method, increased history and auto indent
- Loading branch information
Martin Ström
committed
Aug 6, 2008
1 parent
c962355
commit 3d3d532
Showing
1 changed file
with
19 additions
and
80 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,36 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require 'rubygems' | ||
require 'utility_belt' | ||
require 'irb/completion' | ||
require 'irb/ext/save-history' | ||
|
||
IRB.conf[:SAVE_HISTORY] = 100 | ||
IRB.conf[:SAVE_HISTORY] = 1000 | ||
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history" | ||
IRB.conf[:PROMPT_MODE] = :SIMPLE | ||
IRB.conf[:AUTO_INDENT] = true | ||
|
||
load File.dirname(__FILE__) + '/.railsrc' if $0 == 'irb' && ENV['RAILS_ENV'] | ||
|
||
# http://eigenclass.org/hiki/irb+ri+completion | ||
module Kernel | ||
def r(arg) | ||
puts `fri "#{arg}"` | ||
end | ||
private :r | ||
end | ||
alias :x :exit | ||
|
||
class Object | ||
def puts_ri_documentation_for(obj, meth) | ||
case self | ||
when Module | ||
candidates = ancestors.map{|klass| "#{klass}::#{meth}"} | ||
candidates.concat(class << self; ancestors end.map{|k| "#{k}##{meth}"}) | ||
else | ||
candidates = self.class.ancestors.map{|klass| "#{klass}##{meth}"} | ||
end | ||
candidates.each do |candidate| | ||
#puts "TRYING #{candidate}" | ||
desc = `qri '#{candidate}'` | ||
unless desc.chomp == "nil" | ||
# uncomment to use ri (and some patience) | ||
#desc = `ri -T '#{candidate}' 2>/dev/null` | ||
#unless desc.empty? | ||
puts desc | ||
return true | ||
end | ||
end | ||
false | ||
# list methods which aren't in superclass | ||
def local_methods(obj = self) | ||
(obj.methods - obj.class.superclass.instance_methods).sort | ||
end | ||
private :puts_ri_documentation_for | ||
|
||
def method_missing(meth, *args, &block) | ||
if md = /ri_(.*)/.match(meth.to_s) | ||
unless puts_ri_documentation_for(self,md[1]) | ||
"Ri doesn't know about ##{meth}" | ||
end | ||
else | ||
super | ||
end | ||
end | ||
|
||
def ri_(meth) | ||
unless puts_ri_documentation_for(self,meth.to_s) | ||
"Ri doesn't know about ##{meth}" | ||
|
||
# print documentation | ||
# | ||
# ri 'Array#pop' | ||
# Array.ri | ||
# Array.ri :pop | ||
# arr.ri :pop | ||
def ri(method = nil) | ||
unless method && method =~ /^[A-Z]/ # if class isn't specified | ||
klass = self.kind_of?(Class) ? name : self.class.name | ||
method = [klass, method].compact.join('#') | ||
end | ||
puts `ri '#{method}'` | ||
end | ||
end | ||
|
||
RICompletionProc = proc{|input| | ||
bind = IRB.conf[:MAIN_CONTEXT].workspace.binding | ||
case input | ||
when /(\s*(.*)\.ri_)(.*)/ | ||
pre = $1 | ||
receiver = $2 | ||
meth = $3 ? /\A#{Regexp.quote($3)}/ : /./ #} | ||
begin | ||
candidates = eval("#{receiver}.methods", bind).map do |m| | ||
case m | ||
when /[A-Za-z_]/; m | ||
else # needs escaping | ||
%{"#{m}"} | ||
end | ||
end | ||
candidates = candidates.grep(meth) | ||
candidates.map{|s| pre + s } | ||
rescue Exception | ||
candidates = [] | ||
end | ||
when /([A-Z]\w+)#(\w*)/ #} | ||
klass = $1 | ||
meth = $2 ? /\A#{Regexp.quote($2)}/ : /./ | ||
candidates = eval("#{klass}.instance_methods(false)", bind) | ||
candidates = candidates.grep(meth) | ||
candidates.map{|s| "'" + klass + '#' + s + "'"} | ||
else | ||
IRB::InputCompletor::CompletionProc.call(input) | ||
end | ||
} | ||
#Readline.basic_word_break_characters= " \t\n\"\\'`><=;|&{(" | ||
Readline.basic_word_break_characters= " \t\n\\><=;|&" | ||
Readline.completion_proc = RICompletionProc | ||
|
||
alias :x :exit | ||
load File.dirname(__FILE__) + '/.railsrc' if $0 == 'irb' && ENV['RAILS_ENV'] |